-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
submit.php
624 lines (587 loc) · 18.9 KB
/
submit.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/**
* Submit
*
* @package WikiDocs
* @repository https://github.com/Zavy86/wikidocs
*/
require_once("bootstrap.inc.php");
// mode definition
define("MODE","engine");
define("ACT",htmlspecialchars($_GET['act'] ?? ''));
// switch action
switch(ACT){
// authentication
case "authentication":authentication();break;
// contents
case "content_save":content_save();break;
case "content_restore":content_restore();break;
case "content_delete":content_delete();break;
// images
case "image_upload_ajax":image_upload_ajax();break;
case "image_drop_upload_ajax":image_drop_upload_ajax();break;
case "image_delete_ajax":image_delete_ajax();break;
// attachments
case "attachment_upload_ajax":attachment_upload_ajax();break;
case "attachment_delete_ajax":attachment_delete_ajax();break;
// drafts
case "draft_save_ajax":draft_save_ajax();break;
// default
default:
// alert and redirect
wdf_alert("The action ".ACT." does not exist!","danger");
wdf_redirect(PATH);
}
/**
* Authentication
*/
function authentication(){
// get localization
$TXT=Localization::getInstance();
// debug
wdf_dump($_REQUEST,"_REQUEST");
// reset authentication
$_SESSION['wikidocs']['authenticated']=0;
// acquire variables
$p_document=strtolower($_POST['document']);
$p_password=$_POST['password'];
// check edit code
if(strlen(EDITCODE)==32 && strtolower(md5($p_password))===strtolower(EDITCODE)) {
$_SESSION['wikidocs']['authenticated']=2;
}elseif(password_verify($p_password,EDITCODE)) {
$_SESSION['wikidocs']['authenticated']=2;
}elseif(strlen(VIEWCODE)==32 && strtolower(md5($p_password))===strtolower(VIEWCODE)) {
$_SESSION['wikidocs']['authenticated']=1;
}elseif(password_verify($p_password, VIEWCODE)){
$_SESSION['wikidocs']['authenticated']=1;
}else{
$_SESSION['wikidocs']['authenticated']=0;
}
// check for authentication
if($_SESSION['wikidocs']['authenticated']) {
wdf_alert($TXT->SubmitAuthSuccess,"success");
wdf_redirect(PATH.$p_document);
}else{
wdf_alert($TXT->SubmitAuthInvalid,"danger");
wdf_redirect(PATH.$p_document);
}
}
/**
* Content Save
*/
function content_save(){
// get localization
$TXT=Localization::getInstance();
// debug
wdf_dump($_REQUEST,"_REQUEST");
// acquire variables
$p_revision=boolval($_POST['revision']);
$p_document=strtolower($_POST['document']);
$p_content=$_POST['content'];
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// alert and redirect
wdf_alert($TXT->SubmitNotAuthenticated,"danger");
wdf_redirect(PATH.$p_document);
}
// check document path
if(!strlen($p_document)){
// alert and redirect
wdf_alert($TXT->SubmitDocumentPathCannotBeEmpty,"danger");
wdf_redirect(PATH);
}
// check content
if(!strlen($p_content)){
// alert and redirect
wdf_alert($TXT->SubmitDocumentContentCannotBeEmpty,"danger");
wdf_redirect(PATH.$p_document."?edit");
}
// initialize document
$DOC=new Document($p_document);
// debug
wdf_dump($DOC,"DOCUMENT");
// check if file path is ok.
if (substr_count($DOC->ID, "..") > 0 ||
substr_count($DOC->ID, ':') > 0 || strpos($DOC->ID, '/') === 0) { // These 2 checks are to detect when the user uses a windows or unix absolute path; I'm not sure they are necessary, but they shouldn't do any harm either.
// the file path is not ok, so I short circuit and exit, showing an error to the user.
wdf_alert($TXT->SubmitDocumentError, "danger");
// regenerate sitemap
wdf_regenerate_sitemap();
// redirect
wdf_redirect(PATH.$p_document);
return;
}
// file path is ok.
// check for directory or make it
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
// check revision
if($p_revision){
// check for content file
if(file_exists($DOC->DIR."content.md")){
// check for revisions directory
if(!is_dir($DOC->DIR."versions")){mkdir($DOC->DIR."versions",0755,true);}
rename($DOC->DIR."content.md",$DOC->DIR."versions/".date("Ymd_His").".md");
}
}
// document path definition
define("DOC_PATH",$DOC->PATH."/");
// replace url in images
$p_content=preg_replace_callback('/!\[(.*)\]\s?\((.*)(.png|.gif|.jpg|.jpeg|.svg)(.*)\)/',function($match){return str_replace(DOC_PATH,"{{DOC_PATH}}",$match[0]);},$p_content);
// replace url in images
$p_content=preg_replace_callback('/\[(.*)\]:\s?(.*)(.png|.gif|.jpg|.jpeg|.svg|)/',function($match){return str_replace(DOC_PATH,"{{DOC_PATH}}",$match[0]);},$p_content);
// replace path in url
$p_content=preg_replace_callback('/\[(.*)\]\s?\((.*)\)/',function($match){return str_replace("(".PATH,"({{APP_PATH}}",$match[0]);},$p_content);
// debug
wdf_dump($p_content,"content");
// save content file
$bytes=file_put_contents($DOC->DIR."content.md",$p_content);
// alerts
if($bytes>0){
// delete draft if exist
if(file_exists($DOC->DIR."draft.md")){unlink($DOC->DIR."draft.md");}
// sum size of all images
foreach($DOC->images() as $image_fe){$bytes+=filesize($DOC->DIR.$image_fe);}
if($bytes<1000000){$size=number_format($bytes/1000,2,",",".")." KB";}else{$size=number_format($bytes/1000000,2,",",".")." MB";}
wdf_alert($TXT->SubmitDocumentSaved." [".$size."]","success");
}else{
wdf_alert($TXT->SubmitDocumentError,"danger");
}
// regenerate sitemap
wdf_regenerate_sitemap();
// redirect
wdf_redirect(PATH.$p_document);
}
/**
* Content Restore
*/
function content_restore(){
// get localization
$TXT=Localization::getInstance();
// debug
wdf_dump($_REQUEST,"_REQUEST");
// acquire variables
$p_document=strtolower($_GET['document']);
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// alert and redirect
wdf_alert($TXT->SubmitNotAuthenticated,"danger");
wdf_redirect(PATH.$p_document);
}
// check document path
if(!strlen($p_document)){
// alert and redirect
wdf_alert($TXT->SubmitDocumentPathCannotBeEmpty,"danger");
wdf_redirect(PATH);
}
// initialize document
$DOC=new Document($p_document);
wdf_dump($DOC,'DOC');
// check if version exixts
if(!file_exists($DOC->DIR."versions/".$DOC->VERSION.".md")){
// alert and redirect
wdf_alert($TXT->SubmitDocumentVersionNotFound,"danger");
wdf_redirect(PATH);
}
// check for content file
if(file_exists($DOC->DIR."content.md")){
// check for revisions directory
if(!is_dir($DOC->DIR."versions")){mkdir($DOC->DIR."versions",0755,true);}
// store current version
rename($DOC->DIR."content.md",$DOC->DIR."versions/".date("Ymd_His").".md");
}
// restore selected version
copy($DOC->DIR."versions/".$DOC->VERSION.".md",$DOC->DIR."content.md");
// regenerate sitemap
wdf_regenerate_sitemap();
// alert and redirect
wdf_alert($TXT->SubmitDocumentRestored,"warning");
wdf_redirect(PATH.$p_document);
}
/**
* Content Delete
*/
function content_delete(){
// get localization
$TXT=Localization::getInstance();
// debug
wdf_dump($_REQUEST,"_REQUEST");
// acquire variables
$p_document=strtolower($_GET['document']);
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// alert and redirect
wdf_alert($TXT->SubmitNotAuthenticated,"danger");
wdf_redirect(PATH.$p_document);
}
// check document path
if(!strlen($p_document)){
// alert and redirect
wdf_alert($TXT->SubmitDocumentPathCannotBeEmpty,"danger");
wdf_redirect(PATH);
}
// initialize document
$DOC=new Document($p_document);
wdf_dump($DOC,'DOC');
// check for trash directory or make it
if(!is_dir(DIR."datasets/trash")){mkdir(DIR."datasets/trash",0755,true);}
// set trash id
$trash_id=str_replace('/','___',$DOC->ID)."_".date("Ymd_His");
wdf_dump($trash_id);
// move document to trash
if(is_dir($DOC->DIR)){rename($DOC->DIR,DIR."datasets/trash/".$trash_id);}
// regenerate sitemap
wdf_regenerate_sitemap();
// alert and redirect
wdf_alert($TXT->SubmitDocumentDeleted,"warning");
wdf_redirect(PATH);
}
/**
* Image Upload (AJAX)
*/
function image_upload_ajax(){
// acquire variables
$p_document=strtolower($_POST['document']);
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// error
echo json_encode(array("error"=>1,"code"=>"not_authenticated"));
// return
return false;
}
// check document path
if(!strlen($p_document)){
// error
echo json_encode(array("error"=>1,"code"=>"document_empty", 'document' => $p_document));
// return
return false;
}
// initialize document
$DOC=new Document($p_document);
// check for directory or make it
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
// check for file
if(!isset($_FILES['image'])||!is_uploaded_file($_FILES['image']['tmp_name'])||$_FILES["image"]["error"]>0){
if(!strlen($_POST['image_base64'])){
// error
echo json_encode(array("error"=>1,"code"=>"file_error"));
// return
return false;
}
}
if(isset($_FILES['image'])){
$image=$_FILES['image'];
$image['ext']=strtolower(pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION));
}
if(strlen($_POST['image_base64'] ?? '')){
$image_parts=explode(";base64,",$_POST['image_base64']);
$image['type']=explode("data:",$image_parts[0])[1];
$image['ext']=strtolower(explode("image/",$image_parts[0])[1]);
$image['base64']=str_replace(" ","+",$image_parts[1]);
$image['name']=md5(date("YmdHisu")).".".$image['ext'];
}
// check extension
if(!in_array($image['ext'],array("png","jpg","jpeg","gif","svg"))){
// error
echo json_encode(array("error"=>1,"code"=>"extension_not_allowed","file"=>$image));
// return
return false;
}
// check file type
if(!in_array($image["type"],array("image/png","image/gif","image/jpg","image/jpeg","image/svg+xml"))){
// error
echo json_encode(array("error"=>1,"code"=>"file_not_allowed","file"=>$image));
// return
return false;
}
// make file name
$file_name=strtolower(str_replace(" ","-",$image['name']));
// check for posted image
if($image['tmp_name']){
if(move_uploaded_file($image['tmp_name'],$DOC->DIR.$file_name)){$uploaded=true;}
// check for pasted image
}elseif(strlen($image['base64'])){
$bytes=file_put_contents($DOC->DIR.$file_name,base64_decode($image['base64']));
if($bytes>0){
$image['size']=$bytes;
$uploaded=true;
}
}
// check for uploaded
if($uploaded){
// success
echo json_encode(array("error"=>null,"code"=>"image_uploaded","name"=>$file_name,"path"=>$DOC->PATH."/".$file_name,"size"=>$image['size']));
// return
return true;
}else{
// error
echo json_encode(array("error"=>1,"code"=>"uploading_error"));
// return
return false;
}
}
/**
* Image Upload - drag-n-drop (AJAX)
*/
function image_drop_upload_ajax() {
$document = $_POST['document'];
$image_base64 = $_POST['image_base64'];
$image_filename = $_POST['image_name'];
if(Session::getInstance()->autenticationLevel()!=2){
// error
echo json_encode(array("error"=>1,"code"=>"not_authenticated"));
// return
return false;
}
if(!strlen($image_base64)){
// error
echo json_encode(array("error"=>1,"code"=>"image_empty", 'file' => $image_filename));
// return
return false;
}
// initialize document
$DOC = new Document($document);
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
// We need to remove the "data:image/{type};base64," amd split it into parts from $data
if (preg_match('/^data:image\/(\w+);base64,/', $image_base64, $type)) {
$image_base64 = substr($image_base64, strpos($image_base64, ',') + 1);
$type = strtolower($type[1]); // jpg, png, gif
//check for valid image type
if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) {
echo json_encode(array("error"=>1,"code"=>"extension_not_allowed","file"=>$image_filename));
return false;
}
$image_base64 = str_replace( ' ', '+', $image_base64 );
$image_base64 = base64_decode($image_base64);
if ($image_base64 === false) {
$args = [
'error' => 1,
'code' => 'base64_decode failed',
'file' => $image_filename
];
echo json_encode($args);
return false;
}
} else {
$args = [
'error' => 1,
'code' => 'did not match data URI with image data',
'file' => $image_filename
];
echo json_encode($args);
return false;
}
// create the filename for the image to store with the correct extension
$filename_parts = explode('.', $image_filename);;
$filename_without_extension = $filename_parts[0];
$filename_cleaned = strtolower(str_replace(" ","-",$filename_without_extension));
$filename = "{$filename_cleaned}.{$type}";
$filepath_absolute = $DOC->DIR;
$filepath_relative = $DOC->PATH;
$imageFile = $filepath_absolute.$filename;
$file = file_put_contents($imageFile, $image_base64);
if ($file) {
$args = [
'error' => null,
'code' => 'image_uploaded',
'name' => $filename,
'path' => $filepath_relative."/".$filename
];
echo json_encode($args);
return true;
} else {
$args = [
'error' => 1,
'code' => 'image_not_uploaded',
'name' => $filename,
'path' => $filepath_relative."/".$filename
];
echo json_encode($args);
return false;
}
}
/**
* Image Delete (AJAX)
*/
function image_delete_ajax() {
$document = $_POST['document'];
$image_filename = $_POST['image_name'];
if (Session::getInstance()->autenticationLevel() != 2) {
// error
echo json_encode(array("error" => 1, "code" => "not_authenticated"));
// return
return false;
}
// initialize document
$DOC = new Document($document);
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
if (empty($image_filename)) {
echo json_encode(array("error"=>1,"code"=>"filename_empty"));
return false;
}
$filename = $DOC->DIR.$image_filename;
if (file_exists($filename)) {
$image_deleted = unlink($filename);
} else {
echo json_encode(array("error"=>1,"code"=>"image_not_found","file"=>$filename));
}
if ($image_deleted) {
echo json_encode(array("error" => null, "code" => "image_deleted", "file" => $filename));
return true;
} else {
echo json_encode(array("error"=>1,"code"=>"image_not_deleted","file"=>$filename));
return false;
}
}
/**
* Atachment Upload (AJAX)
*/
function attachment_upload_ajax(){
// acquire variables
$p_document=strtolower($_POST['document']);
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// error
echo json_encode(array("error"=>1,"code"=>"not_authenticated"));
// return
return false;
}
// check document path
if(!strlen($p_document)){
// error
echo json_encode(array("error"=>1,"code"=>"document_empty"));
// return
return false;
}
// initialize document
$DOC=new Document($p_document);
// check for directory or make it
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
// check for file
if(!isset($_FILES['attachment'])||!is_uploaded_file($_FILES['attachment']['tmp_name'])||$_FILES["attachment"]["error"]>0){
// error
echo json_encode(array("error"=>1,"code"=>"file_error"));
// return
return false;
}
// make attachment
$attachment=$_FILES['attachment'];
$attachment['ext']=strtolower(pathinfo($_FILES['attachment']['name'],PATHINFO_EXTENSION));
// check extension
if(!in_array($attachment['ext'],array("pdf","doc","docx","xls","xlsx","ppt","pptx"))){
// error
echo json_encode(array("error"=>1,"code"=>"extension_not_allowed","file"=>$attachment));
// return
return false;
}
// check file type
if(!in_array($attachment["type"],array(
"application/pdf",
"application/msword",
"application/vnd.ms-excel",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
))){
// error
echo json_encode(array("error"=>1,"code"=>"file_not_allowed","file"=>$attachment));
// return
return false;
}
// check file size (limit to 10 MB)
if($attachment['size'] > 10 * 1024 * 1024) {
// error
echo json_encode(array("error"=>1,"code"=>"file_too_large","size"=>$attachment['size']));
// return
return false;
}
// make file name
$file_name=strtolower(str_replace(" ","-",$attachment['name']));
// move temporary file
$uploaded=move_uploaded_file($attachment['tmp_name'],$DOC->DIR.$file_name);
// check for uploaded
if($uploaded){
// success
echo json_encode(array("error"=>null,"code"=>"attachment_uploaded","name"=>$file_name,"path"=>$DOC->PATH."/".$file_name,"size"=>$attachment['size']));
// return
return true;
}else{
// error
echo json_encode(array("error"=>1,"code"=>"uploading_error"));
// return
return false;
}
}
/**
* Attachment Delete (AJAX)
*/
function attachment_delete_ajax() {
$document = $_POST['document'];
$attachment_filename = $_POST['attachment_name'];
if (Session::getInstance()->autenticationLevel() != 2) {
// error
echo json_encode(array("error" => 1, "code" => "not_authenticated"));
// return
return false;
}
// initialize document
$DOC = new Document($document);
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
if (empty($attachment_filename)) {
echo json_encode(array("error"=>1,"code"=>"filename_empty"));
return false;
}
$filename = $DOC->DIR.$attachment_filename;
if (file_exists($filename)) {
$attachment_deleted = unlink($filename);
} else {
echo json_encode(array("error"=>1,"code"=>"attachment_not_found","file"=>$filename));
}
if ($attachment_deleted) {
echo json_encode(array("error" => null, "code" => "attachment_deleted", "file" => $filename));
return true;
} else {
echo json_encode(array("error"=>1,"code"=>"attachment_not_deleted","file"=>$filename));
return false;
}
}
/**
* Draft Save (AJAX)
*/
function draft_save_ajax(){
// acquire variables
$p_document=strtolower($_POST['document']);
$p_content=$_POST['content'];
// check authentication
if(Session::getInstance()->autenticationLevel()!=2){
// error
echo json_encode(array("error"=>1,"code"=>"not_authenticated"));
// return
return false;
}
// check document path
if(!strlen($p_document)){
// error
echo json_encode(array("error"=>1,"code"=>"document_empty"));
// return
return false;
}
// initialize document
$DOC=new Document($p_document);
// check for directory or make it
if(!is_dir($DOC->DIR)){mkdir($DOC->DIR,0755,true);}
// save draft content file
$bytes=file_put_contents($DOC->DIR."draft.md",$p_content);
// check for saved
if($bytes>0){
// success
echo json_encode(array("error"=>null,"code"=>"draft_saved"));
// return
return true;
}else{
// error
echo json_encode(array("error"=>1,"code"=>"draft_saving_error"));
// return
return false;
}
}