-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.php
440 lines (366 loc) · 14.7 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* Jotter - open-source organized note-taking web app
*
* @license LGPL v3 (http://www.gnu.org/licenses/lgpl.html)
* @author Yosko <webmaster@yosko.net>
* @version v0.4
* @link https://github.com/yosko/jotter
*/
define( 'VERSION', '0.4' );
define( 'ROOT', __DIR__.'/' );
define( 'DIR_DATA', ROOT.'data/' );
define( 'DIR_TPL', ROOT.'tpl/' );
define( 'URL',
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'?'https':'http')
.'://'
.$_SERVER['HTTP_HOST']
.rtrim(dirname($_SERVER['SCRIPT_NAME']),'/')
.'/'
);
define( 'URL_TPL', URL.'tpl/' );
define( 'ENV_DEMO', 'demo' );
define( 'ENV_DEV', 'dev' );
define( 'ENV_PROD', 'prod' );
define( 'ENV_CURRENT', ENV_DEV );
//display errors & warnings
if (ENV_CURRENT == ENV_DEV) {
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors','On');
ini_set('log_errors', 'On');
ini_set('error_log', ROOT.'errors.log');
} else {
ini_set('display_errors','Off');
}
// external libraries
// https://github.com/michelf/php-markdown/
require_once( ROOT.'lib/ext/Markdown.php');
require_once( ROOT.'lib/ext/MarkdownExtra.php');
// https://github.com/nickcernis/html-to-markdown
require_once( ROOT.'lib/ext/HTML_To_Markdown.php');
// https://github.com/yosko/yoslogin/
require_once( ROOT.'lib/ext/yoslogin.lib.php');
//Jotter libraries
require_once( ROOT.'lib/utils.class.php');
require_once( ROOT.'lib/jotter.class.php');
require_once( ROOT.'lib/login.class.php');
$jotter = new Jotter();
$errors = array();
$isNote = false;
$isConfigMode = false;
$isEditMode = false;
$isWysiwyg = false;
$isDir = false;
$appInstalled = file_exists(DIR_DATA.'users.json');
//check if user is logged in
$logger = new Login( 'jotter' );
//user is trying to log in
if( !empty($_POST['submitLoginForm']) ) {
//install app and create first user
if(!$appInstalled) {
$logger->createUser(
htmlspecialchars(trim($_POST['login'])),
htmlspecialchars(trim($_POST['password']))
);
}
$user = $logger->logIn(
htmlspecialchars(trim($_POST['login'])),
htmlspecialchars(trim($_POST['password'])),
isset($_POST['remember'])
);
//logging out
} elseif( !empty($_GET['action']) && $_GET['action'] == 'logout' ) {
$logger->logOut();
} else {
$user = $logger->authUser();
}
// ajax calls
if(!empty($_GET['action']) && $_GET['action'] == 'ajax') {
$data = false;
if (ENV_CURRENT == ENV_DEMO) {
$data = true;
}
// always return false if user is not authenticated
if($user['isLoggedIn'] && ENV_CURRENT != ENV_DEMO) {
$option = isset($_GET['option'])?$_GET['option']:false;
$notebookName = isset($_GET['nb'])?urlencode($_GET['nb']):false;
$itemPath = isset($_GET['item'])?$_GET['item']:false;
//load the complete list of notebooks
$notebooks = $jotter->loadNotebooks();
$notebook = ($notebookName !== false)?$jotter->loadNotebook($notebookName, $user['login']):false;
//move an item into another directory
if($option == 'moveItem') {
$sourcePath = $_GET['source'];
$destPath = $_GET['destination'];
if(!is_dir(DIR_DATA.$user['login'].'/'.$notebookName.'/'.$destPath)) {
$destPath = dirname($destPath);
}
if($sourcePath == '.') { $sourcePath = ''; }
if($destPath == '.') { $destPath = ''; }
//TODO: check if source parent of destination
//TODO: check if source & destination are "identical"
//make sure the requested move is possible and safe
$error = strpos($notebookName, '..') !== false
|| strpos($sourcePath, '..') !== false
|| strpos($destPath, '..') !== false
|| !isset($notebooks[$user['login']][$notebookName])
|| !file_exists(DIR_DATA.$user['login'].'/'.$notebookName.'/'.$sourcePath)
|| !file_exists(DIR_DATA.$user['login'].'/'.$notebookName.'/'.$destPath);
if(!$error) {
$notebook = $jotter->loadNotebook($notebookName, $user['login']);
$error = !$jotter->moveItem($sourcePath, $destPath);
}
$data = !$error;
// save current note
} elseif($option == 'save') {
//only load notebook if it is owned by current user
if(isset($notebooks[$user['login']][$notebookName])) {
$itemData = Utils::getArrayItem($notebook['tree'], $itemPath);
$isNote = $itemData === true;
if($isNote && isset($_POST['text'])) {
if (ENV_CURRENT != ENV_DEMO) {
//save the note
$data = $jotter->setNoteText($itemPath, $_POST['text']);
} else {
$data = true;
}
}
}
// preview a markdown note in HTML
} elseif($option == 'preview') {
$data = $jotter->loadNote($itemPath, true);
}
}
header('Content-type: application/json');
echo json_encode($data);
exit;
//login form
} elseif(!$user['isLoggedIn']) {
//display form as an installation process
if(!$appInstalled) {
$phpMinVersion = '5.3';
$phpIsMinVersion = (version_compare(PHP_VERSION, $phpMinVersion) >= 0);
$isWritable = is_writable(DIR_DATA) && is_writable(ROOT.'cache/')
|| !file_exists(DIR_DATA) && !file_exists(ROOT.'cache/')
&& is_writable(dirname(DIR_DATA));
}
include( DIR_TPL.'loginForm.tpl.php' );
//notebook pages
} elseif( !empty($_GET['nb']) ) {
$itemPath = '';
$notebook = false;
$notebookName = urlencode($_GET['nb']);
//load the complete list of notebooks
$notebooks = $jotter->loadNotebooks();
//only load notebook if it is owned by current user
if(isset($notebooks[$user['login']][$notebookName])) {
$notebook = $jotter->loadNotebook($notebookName, $user['login']);
}
// notebook wasn't loaded
if($notebook == false) {
include( DIR_TPL.'error.tpl.php' );
// rename current notebook
} elseif( !empty($_GET['action']) && $_GET['action'] == 'edit' && empty($_GET['item']) ) {
d('edit notebook');
// delete current notebook
} elseif( !empty($_GET['action']) && $_GET['action'] == 'delete' && empty($_GET['item']) ) {
//confirmation was sent
if(isset($_POST['delete'])) {
if (ENV_CURRENT != ENV_DEMO) {
$jotter->unsetNotebook($notebookName, $user['login']);
}
header('Location: '.URL);
exit;
}
include( DIR_TPL.'itemDelete.tpl.php' );
// add a subdirectory or a note to the current directory
} elseif( !empty($_GET['action']) && ($_GET['action'] == 'adddir' || $_GET['action'] == 'addnote') ) {
if(isset($_POST['name'])) {
$item['name'] = $_POST['name'];
$path = $item['name'];
if(!empty($_GET['item'])) {
if(!is_dir(DIR_DATA.$user['login'].'/'.$notebookName.'/'.$_GET['item'])) {
if(dirname($_GET['item']) != '.') {
$path = dirname($_GET['item']).'/'.$path;
}
} else {
if(!empty($_GET['item'])) {
$path = $_GET['item'].'/'.$path;
}
}
}
$errors['empty'] = empty($item['name']);
$errors['alreadyExists'] = !is_null(Utils::getArrayItem($notebook['tree'], $path));
if(!in_array(true, $errors)) {
if (ENV_CURRENT != ENV_DEMO) {
if($_GET['action'] == 'addnote') {
$path .= '.md';
$jotter->setNote($path);
}
else {
$jotter->setDirectory($path);
}
}
header('Location: '.URL.'?nb='.$notebookName.'&item='.$path);
exit;
}
}
include( DIR_TPL.'itemForm.tpl.php' );
// notebook item
} elseif( !empty($_GET['item']) && strpos($itemPath, '..') === false ) {
$itemPath = $_GET['item'];
$itemData = Utils::getArrayItem($notebook['tree'], $itemPath);
$isNote = $itemData === true;
if(!$isNote) {
$dirPath = DIR_DATA.$user['login'].'/'.$notebookName.'/'.$itemPath;
$isDir = file_exists($dirPath) && is_dir($dirPath);
}
//item not found: show notebook root
if(!$isDir && !$isNote) {
include( DIR_TPL.'notebook.tpl.php' );
// rename current item
} elseif( !empty($_GET['action']) && $_GET['action'] == 'edit' ) {
//confirmation was sent
if(isset($_POST['name'])) {
$item['name'] = $_POST['name'];
$path = $item['name'];
$path = (dirname($itemPath)!='.'?dirname($itemPath).'/':'').$path;
$errors['empty'] = empty($item['name']);
$errors['sameName'] = $itemPath == $path.'.md';
$errors['alreadyExists'] = !is_null(Utils::getArrayItem($notebook['tree'], $path));
if(!in_array(true, $errors)) {
if (ENV_CURRENT != ENV_DEMO) {
if($isNote) {
$path .= '.md';
$item['name'] .= '.md';
$jotter->setNote($itemPath, $item['name']);
}
elseif($isDir) {
$jotter->setDirectory($itemPath, $item['name']);
}
}
header('Location: '.URL.'?nb='.$notebookName.'&item='.$path);
exit;
}
}
include( DIR_TPL.'itemForm.tpl.php' );
// delete current item
} elseif( !empty($_GET['action']) && $_GET['action'] == 'delete' ) {
//confirmation was sent
if(isset($_POST['delete'])) {
if (ENV_CURRENT != ENV_DEMO) {
if($isNote) {
$jotter->unsetNote($itemPath);
} elseif($isDir) {
$jotter->unsetDirectory($itemPath);
}
}
header('Location: '.URL.'?nb='.$notebookName.'&item='.(dirname($itemPath)!='.'?dirname($itemPath):''));
exit;
}
include( DIR_TPL.'itemDelete.tpl.php' );
//show item
} else {
if($isNote) {
//we are dealing with a note: load it
$note = $jotter->loadNote($_GET['item']);
// show editor toolbar
$isEditMode = true;
$isWysiwyg = !isset($notebook['editor']) || $notebook['editor'] == 'wysiwyg';
include( DIR_TPL.'note.tpl.php' );
} elseif($isDir) {
//for a directory, just show the notebook's "hompage"
include( DIR_TPL.'notebook.tpl.php' );
} else {
//TODO: show error
}
}
//default: show notebook root
} else {
include( DIR_TPL.'notebook.tpl.php' );
}
//add a notebook
} elseif( !empty($_GET['action']) && $_GET['action'] == 'add' ) {
// user wants to make a new notebook
if(isset($_POST['name'])) {
$notebook = array(
'name' => urlencode($_POST['name']),
'user' => $user['login'],
'editor' => (isset($_POST['editor']) && $_POST['editor'] == 'wysiwyg')?$_POST['editor']:'markdown',
'safe' => isset($_POST['safe-wysiwyg'])
);
$errors['empty'] = empty($notebook['name']);
$errors['alreadyExists'] = isset($notebooks[$user['login']][$notebook['name']]);
if(!in_array(true, $errors)) {
if (ENV_CURRENT != ENV_DEMO) {
$notebooks = $jotter->setNotebook($notebook['name'], $notebook['user'], $notebook['editor'], $notebook['safe']);
}
header('Location: '.URL.'?nb='.$notebook['name']);
exit;
}
}
include( DIR_TPL.'notebookForm.tpl.php' );
//configuration page
} elseif( !empty($_GET['action']) && $_GET['action'] == 'config' ) {
$isConfigMode = true;
$users = $logger->getUsers();
$option = isset($_GET['option'])?$_GET['option']:false;
if($option == 'myPassword') {
if (isset($_POST['password'])) {
$password = htmlspecialchars(trim($_POST['password']));
$errors['emptyPassword'] = (!isset($_POST['password']) || trim($_POST['password']) == "");
if(!in_array(true, $errors)) {
if (ENV_CURRENT != ENV_DEMO) {
//save password
$errors['save'] = !$logger->setUser($user['login'], $password);
}
header('Location: '.URL.'?action=config&option=myPassword');
exit;
}
}
} elseif($option == 'addUser') {
if (isset($_POST['login']) && isset($_POST['password'])) {
$login = htmlspecialchars(trim($_POST['login']));
$password = htmlspecialchars(trim($_POST['password']));
$errors['emptyLogin'] = $login == '';
$errors['emptyPassword'] = $password == '';
$errors['notAvailable'] = false;
foreach ($users as $key => $value) {
if($value['login'] == $login)
$errors['notAvailable'] = true;
}
if(!in_array(true, $errors)) {
if (ENV_CURRENT != ENV_DEMO) {
$logger->createUser($login, $password);
}
header('Location: '.URL.'?action=config');
exit;
}
}
} elseif($option == 'deleteUser') {
$login = htmlspecialchars(trim($_GET['user']));
if(isset($_POST['deleteUserSubmit'])) {
//delete user's notebooks
$notebooks = $jotter->loadNotebooks();
foreach($notebooks[$user['login']] as $key => $value) {
if($value['user'] == $login && ENV_CURRENT != ENV_DEMO) {
$jotter->unsetNotebook($key);
}
}
//delete user
$logger->deleteUser($login, $password);
header('Location: '.URL.'?action=config');
exit;
}
} else {
}
include( DIR_TPL.'config.tpl.php' );
//markdown syntax page
} elseif( !empty($_GET['action']) && $_GET['action'] == 'markdown' ) {
include( DIR_TPL.'markdown.tpl.php' );
//homepage: notebooks list
} else {
$notebooks = $jotter->loadNotebooks();
include( DIR_TPL.'notebooks.tpl.php' );
}
?>