-
Notifications
You must be signed in to change notification settings - Fork 40
/
module_glue.inc.php
1571 lines (1404 loc) · 44 KB
/
module_glue.inc.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* module_glue.inc.php
* Main hotglue module
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('common.inc.php');
// html{,_parse}.inc.php are only included where needed
require_once('modules.inc.php');
require_once('util.inc.php');
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
// TODO (later): switch services to using $args['args'] for input validation?
/**
* helper function for revisions_info()
*
* @param array $a array to compare
* @param array $b array to compare
* @return int comparison result
*/
function _cmp_time($a, $b)
{
if ($a['time'] == $b['time']) {
return 0;
}
return ($a['time'] < $b['time']) ? 1 : -1;
}
/**
* lock an object file
*
* @param string $name object name (i.e. page.rev.obj)
* @param mixed $wait false = give up right away, true = wait until successful,
* integer values = wait up to $wait ms
* @return mixed true (on Win32 for now) or lock handle for success, NULL if
* the object doesn't exist, and false if the lock could not be acquired
*/
function _obj_lock($name, $wait = true)
{
// TODO (later): make this work on Windows (opening and writing to files
// after taking the lock doesn't work there atm)
// bandaid below
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
log_msg('warn', 'lock: locking is not supported on WIN32 at the moment');
return true;
}
$start = intval(microtime(true)*1000.0);
$fn = CONTENT_DIR.'/'.str_replace('.', '/', $name);
// resolve symlinks
if (@is_link($fn)) {
$target = @readlink($fn);
if (substr($target, 0, 1) == '/') {
$fn = $target;
} else {
$fn = dirname($fn).'/'.$target;
}
log_msg('debug', 'lock: resolved '.$name.' -> '.$fn);
}
do {
$f = @fopen($fn, 'rb');
if ($f === false) {
// file does not exist
log_msg('debug', 'lock: file '.$fn.' does not exist');
return NULL;
}
// try to acquire lock
if (@flock($f, LOCK_EX|LOCK_NB)) {
// success
log_msg('debug', 'lock: acquired lock for '.$name);
return $f;
} elseif ($wait === false) {
// give up right away
log_msg('debug', 'lock: could not acquire lock');
return false;
} elseif (is_int($wait) && $wait < abs(intval(microtime(true)*1000.0)-$start)) {
// timeout
log_msg('debug', 'lock: could not acquire lock in '.$wait.'ms');
return false;
}
// sleep for a tenth of a second (not sure if this works)
usleep(100000);
} while (true);
}
/**
* unlock an object file
*
* @param mixed $f lock handle (or anything on Win32)
*/
function _obj_unlock($f)
{
// bandaid below
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
log_msg('warn', 'unlock: locking is not supported on WIN32 at the moment');
return;
}
if ($f) {
@flock($f, LOCK_UN);
log_msg('debug', 'lock: released lock');
@fclose($f);
}
}
/**
* create and delete auto- revisions
*
* this function operates on a specific page and takes SNAPSHOT_MIN_AGE and
* SNAPSHOT_MAX_AGE into account.
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* @return array response
* true if successful
*/
function check_auto_snapshot($args)
{
if (!isset($args['page'])) {
return response('Required argument "page" missing', 400);
}
if (!page_exists($args['page'])) {
return response('Page '.quot($args['page']).' does not exist', 400);
}
$a = expl('.', $args['page']);
$revs = revisions_info(array('pagename'=>$a[0], 'sort'=>'time'));
$revs = $revs['#data'];
if ($a[1] == 'head' && SNAPSHOT_MIN_AGE != 0) {
// we're dealing with a head revision and taking snapshots
// find the previous auto- revision
for ($i=0; $i < count($revs); $i++) {
if (substr($revs[$i]['revision'], 0, 5) == 'auto-') {
// got it, check age
if (time()-$revs[$i]['time'] < SNAPSHOT_MIN_AGE) {
log_msg('debug', 'check_auto_snapshot: age is '.(time()-$revs[$i]['time']).' seconds, not creating a snapshot');
break;
}
// check if different
if (dir_is_different(CONTENT_DIR.'/'.str_replace('.', '/', $args['page']), CONTENT_DIR.'/'.str_replace('.', '/', $revs[$i]['page']))) {
snapshot($args);
} else {
log_msg('debug', 'check_auto_snapshot: head is identical to '.$revs[$i]['revision'].', not creating a snapshot');
}
break;
}
if ($i == count($revs)-1) {
// no auto- revision?, create one now
snapshot($args);
}
}
}
// delete old auto- revisions
if (SNAPSHOT_MAX_AGE != 0) {
for ($i=count($revs)-1; 0 <= $i; $i--) {
if (substr($revs[$i]['revision'], 0, 5) == 'auto-' && SNAPSHOT_MAX_AGE < time()-$revs[$i]['time']) {
log_msg('info', 'check_auto_snapshot: deleting an old snapshot');
delete_page(array('page'=>$revs[$i]['page']));
$i--;
}
}
}
return response(true);
}
register_service('glue.check_auto_snapshot', 'check_auto_snapshot', array('auth'=>true));
/**
* duplicate an object
*
* @param array $args arguments
* key 'name' name of the object to duplicate
* @return array response
* string name of new object if successful
*/
function clone_object($args)
{
// load old object
$old = load_object($args);
if ($old['#error']) {
return $old;
} else {
$old = $old['#data'];
}
// create new object
$a = expl('.', $old['name']);
$new = create_object(array('page'=>$a[0].'.'.$a[1]));
if ($new['#error']) {
return $new;
} else {
$new = $new['#data'];
}
// save old object as new
$new = array_merge($old, $new);
$ret = save_object($new);
if ($ret['#error']) {
return $ret;
} else {
// return name
return response($new['name']);
}
}
register_service('glue.clone_object', 'clone_object', array('auth'=>true));
/**
* create an empty object in the content directory
*
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* @return array response
* key 'name' is the name of the object created
*/
function create_object($args)
{
if (!isset($args['page'])) {
return response('Required argument "page" missing', 400);
}
if (!page_exists($args['page'])) {
return response('Page '.quot($args['page']).' does not exist', 400);
}
// try to create new file
$f = false;
$tries = 0;
$mtime = microtime(true);
do {
// use a finer granularity than unix time by default
$name = $args['page'].'.'.intval(floor($mtime)).intval(($mtime-floor($mtime))*100.0+$tries);
$m = umask(0111);
$f = @fopen(CONTENT_DIR.'/'.str_replace('.', '/', $name), 'x');
umask($m);
}
while ($f === false && $tries++ < 9);
if (!$f) {
return response('Error creating an object in page '.quot($args['page']), 500);
} else {
fclose($f);
log_msg('info', 'create_object: created '.quot($name));
return response(array('name'=>$name));
}
}
register_service('glue.create_object', 'create_object', array('auth'=>true));
/**
* create a page
*
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* @return array response
*/
function create_page($args)
{
if (empty($args['page'])) {
return response('Required argument "page" missing or empty', 400);
}
if (page_exists($args['page'])) {
return response('Page '.quot($args['page']).' already exists', 400);
}
if (!valid_pagename($args['page'])) {
return response('Invalid page name '.quot($args['page']), 400);
}
$a = expl('.', $args['page']);
$d = CONTENT_DIR.'/'.$a[0];
if (!is_dir($d)) {
$m = umask(0000);
if (!@mkdir($d, 0777)) {
umask($m);
return response('Error creating directory '.quot($d), 500);
}
umask($m);
}
$d .= '/'.$a[1];
if (!is_dir($d)) {
$m = umask(0000);
if (!@mkdir($d, 0777)) {
umask($m);
return response('Error creating directory '.quot($d), 500);
}
umask($m);
}
log_msg('info', 'create_page: created '.quot($args['page']));
invoke_hook('create_page', array('page'=>$args['page']));
return response(true);
}
register_service('glue.create_page', 'create_page', array('auth'=>true));
register_hook('create_page', 'invoked when a page has been created');
/**
* delete an object from the content directory
*
* @param array $args arguments
* key 'name' is the object name (i.e. page.rev.obj)
* @return array response
*/
function delete_object($args)
{
if (empty($args['name'])) {
return response('Required argument "name" missing or empty', 400);
}
if (!object_exists($args['name'])) {
return response('Object '.quot($args['name']).' does not exist', 404);
}
// check if the object file is writable
// this allows us to make singular objects read-only by setting the file
// permissions to 0444 or similar
if (!is_writable(CONTENT_DIR.'/'.str_replace('.', '/', $args['name']))) {
return response('Object '.quot($args['name']).' is read-only, not deleting it', 500);
}
// call delete_object unless the object is a symlink
// this is because referenced resources are not part of the current page
// anyway, and this way it is easier to handle for the modules
$ret = object_get_symlink($args);
$ret = $ret['#data'];
if ($ret === false) {
$obj = load_object($args);
if ($obj['#error']) {
return $obj;
} else {
$obj = $obj['#data'];
}
invoke_hook('delete_object', array('obj'=>$obj));
}
if (!@unlink(CONTENT_DIR.'/'.str_replace('.', '/', $args['name']))) {
return response('Error deleting object '.quot($args['name']), 500);
} else {
log_msg('info', 'delete_object: deleted '.quot($args['name']));
// drop the all pages from the cache (since the object could have been
// symlinked)
drop_cache('page');
return response(true);
}
}
register_service('glue.delete_object', 'delete_object', array('auth'=>true));
register_hook('delete_object', 'invoked when an object is going to be deleted, should be used for deleting referenced resources');
/**
* delete a page
*
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* @return array response
*/
function delete_page($args)
{
if (empty($args['page'])) {
return response('Required argument "page" missing or empty', 400);
}
if (!page_exists($args['page'])) {
return response('Page '.quot($args['page']).' does not exist', 404);
}
log_msg('info', 'delete_page: deleting '.quot($args['page']));
invoke_hook('delete_page', array('page'=>$args['page']));
// TODO (later): make it possible to delete all revisions at once
// (optimization for frontend)
// TODO (later): how to deal with .svn and .git subdirectories?
// it's objects first
$files = @scandir(CONTENT_DIR.'/'.str_replace('.', '/', $args['page']));
foreach ($files as $f) {
$fn = CONTENT_DIR.'/'.str_replace('.', '/', $args['page']).'/'.$f;
if ($f == '.' || $f == '..') {
continue;
} elseif (substr($f, 0, 1) == '.') {
// delete .svn directories and the like
if (!rm_recursive($fn)) {
log_msg('error', 'delete_page: error deleting '.quot($fn));
}
} elseif (is_link($fn) && !is_file($fn) && !is_dir($fn)) {
// delete dangling symlinks
if (!@unlink($fn)) {
log_msg('error', 'delete_page: error deleting dangling symlink '.quot($fn));
}
} else {
// and everything else through delete_object
$ret = delete_object(array('name'=>$args['page'].'.'.$f));
if ($ret['#error']) {
log_msg('error', 'delete_object: '.$ret['#data']);
}
}
}
// then the revision directory
if (!@rmdir(CONTENT_DIR.'/'.str_replace('.', '/', $args['page']))) {
return response('Error deleting page '.$args['page'], 500);
} else {
log_msg('debug', 'delete_page: deleted '.quot($args['page']));
// drop the page from cache
drop_cache('page', $args['page']);
}
// finally try the shared directory and page directory
$a = expl('.', $args['page']);
@rmdir(CONTENT_DIR.'/'.$a[0].'/shared');
if (@rmdir(CONTENT_DIR.'/'.$a[0])) {
log_msg('info', 'delete_page: parent page directory empty, removing '.quot($a[0]));
}
return response(true);
}
register_service('glue.delete_page', 'delete_page', array('auth'=>true));
register_hook('delete_page', 'invoked when a page is going to be deleted');
register_hook('has_reference', 'used for deleting referenced resources');
/**
* delete a file in the shared directory of a page
*
* this function only deletes the file when there are no references to it
* left. this is not meant to be called directly from the frontend, but
* modules should use it when implementing delete_object.
* @param array $args arguments
* key 'pagename' is the pagename (i.e. page)
* key 'file' filename of file in the shared directory
* key 'max_cnt' delete the file if there are <= max_cnt references (defaults to zero)
* @return array response
* true if the file got deleted for good, false if not
*/
function delete_upload($args)
{
if (@is_numeric($args['max_cnt'])) {
$max_cnt = intval($args['max_cnt']);
} else {
$max_cnt = 0;
}
$refs = upload_references(array_merge($args, array('stop_after'=>$max_cnt+1)));
if ($refs['#error']) {
return $refs;
} else {
$refs = $refs['#data'];
}
$f = CONTENT_DIR.'/'.$args['pagename'].'/shared/'.$args['file'];
if (count($refs) <= $max_cnt) {
if (@unlink($f)) {
log_msg('info', 'delete_upload: deleted '.quot($f));
// being overly tidy, remove the shared dir if empty
@rmdir(CONTENT_DIR.'/'.$args['pagename'].'/shared');
return response(true);
} else {
return response('Error deleting '.quot($f), 500);
}
} else {
log_msg('info', 'delete_upload: not deleting '.quot($f).' because there are still other objects referencing it');
return response(false);
}
}
register_service('glue.delete_upload', 'delete_upload', array('auth'=>true));
/**
* get the startpage
*
* @param array $args unused
* @return array response
* string
*/
function get_startpage($args)
{
$s = @file_get_contents(CONTENT_DIR.'/startpage');
if ($s) {
return response($s);
} else {
return response(DEFAULT_PAGE);
}
}
register_service('glue.get_startpage', 'get_startpage');
/**
* load an object from the content directory
*
* @param array $args arguments
* key 'name' is the object name (i.e. page.rev.obj)
* @return array response
*/
function load_object($args)
{
if (empty($args['name'])) {
return response('Required argument "name" missing or empty', 400);
}
// open file for reading
if (($f = @fopen(CONTENT_DIR.'/'.str_replace('.', '/', $args['name']), 'rb')) === false) {
return response('Error opening '.quot($args['name']).' for reading', 404);
}
// set the name attribute
// TODO (later): declaring arrays like this is probably unnecessary
$ret = array();
$ret['name'] = $args['name'];
// read lines and fill object array
$doing_attribs = true;
while (!feof($f)) {
$l = fgets($f, 4096);
if ($doing_attribs) {
// read attributes first
if (substr($l, -2) == "\r\n") {
$l = substr($l, 0, -2);
} elseif (substr($l, -1) == "\n") {
$l = substr($l, 0, -1);
} elseif (substr($l, -1) == "\r") {
$l = substr($l, 0, -1);
}
$a = expl(':', $l);
if (count($a) == 0) {
$doing_attribs = false;
} elseif (count($a) == 1) {
// value missing, ignoring
} else {
$ret[$a[0]] = implode(':', array_slice($a, 1));
}
} else {
// content starts after the first empty line
if (isset($ret['content'])) {
$ret['content'] .= $l;
} else {
$ret['content'] = $l;
}
}
}
fclose($f);
// re-set the name attribute (in case it got overwritten)
$ret['name'] = $args['name'];
return response($ret);
}
register_service('glue.load_object', 'load_object', array('auth'=>true));
/**
* return the target of an object symlink
*
* @param array $args arguments
* key 'name' is the object name (i.e. page.rev.obj)
* @return array response
* key '#data' either has the target as object name, an
* empty string if the target is outside the content directory or
* false if the object is no symlink
*/
function object_get_symlink($args)
{
if (empty($args['name'])) {
return response('Required argument "name" missing or empty', 400);
}
// TODO (later): think the symlink situation on Windows through
if (is_link(CONTENT_DIR.'/'.str_replace('.', '/', $args['name']))) {
$f = readlink(CONTENT_DIR.'/'.str_replace('.', '/', $args['name']));
if (substr($f, 0, 6) != '../../' || substr($f, 6, 2) == '..') {
log_msg('warn', 'object_get_symlink: target outside of content directory: '.quot($args['name']).' -> '.quot($f));
return response('');
} else {
return response(str_replace('/', '.', substr($f, 6)));
}
} else {
return response(false);
}
}
register_service('glue.object_get_symlink', 'object_get_symlink', array('auth'=>true));
/**
* create a symlink pointing to an object in all other pagename's head revisions
*
* @param array $args arguments
* key 'name' is the object name (i.e. page.rev.obj)
* @return array response
*/
function object_make_symlink($args)
{
if (empty($args['name'])) {
return response('Required argument "name" missing or empty', 400);
}
if (!object_exists($args['name'])) {
return response('Object '.quot($args['name']).' does not exist', 404);
}
$a = expl('.', $args['name']);
// see if the object is itself a symlink
$ret = object_get_symlink($args);
$ret = $ret['#data'];
if ($ret !== false && $ret !== '') {
// create a symlink pointing to the object's target instead
$target = '../../'.str_replace('.', '/', $ret);
// skip both the original object's pagename and the new target's pagename
$a_target = expl('.', $ret);
$skip_pns = array($a[0], $a_target[0]);
} else {
$target = '../../'.str_replace('.', '/', $args['name']);
$skip_pns = array($a[0]);
}
$pns = pagenames(array());
$pns = $pns['#data'];
// for every pagename
foreach ($pns as $pn) {
if (in_array($pn, $skip_pns)) {
continue;
}
// check if the head revision exists
if (is_dir(CONTENT_DIR.'/'.$pn.'/head')) {
$link = CONTENT_DIR.'/'.$pn.'/head/'.$a[2];
if (is_file($link) && !is_link($link)) {
// delete objects with the same name
// these should have been created when shapshotting and the
// revision has later been reverted to
delete_object(array('name'=>$pn.'.head.'.$a[2]));
} elseif (is_link($link) && !is_file($link) && !is_dir($link)) {
// delete dangling symlinks too
if (@unlink($link)) {
log_msg('info', 'object_make_symlink: deleted dangling symlink '.quot($pn.'.head.'.$a[2]));
} else {
log_msg('error', 'object_make_symlink: error deleting dangling symlink '.quot($pn.'.head.'.$a[2]));
}
}
// try to create symlink
if (@symlink($target, $link)) {
log_msg('debug', 'object_make_symlink: '.quot($pn.'.head.'.$a[2]).' -> '.quot($target));
// drop the page from cache
drop_cache('page', $pn.'.head');
}
}
}
return response(true);
}
register_service('glue.object_make_symlink', 'object_make_symlink', array('auth'=>true));
/**
* remove one or more attributes from an object in the content directory
*
* this function takes the object lock.
* @param array $args arguments
* key 'name' is the object name (i.e. page.rev.obj)
* key 'attr' is either a string or an array containing the attribute
* names (keys) to remove
* @return array response
*/
function object_remove_attr($args)
{
if (!isset($args['attr'])) {
return response('Required argument "attr" missing', 400);
}
// LOCK
// TODO (later): $args['name'] might not be set
$_l = _obj_lock($args['name'], LOCK_TIME);
if ($_l === false) {
return response('Could not acquire lock to '.quot($args['name']).' in '.LOCK_TIME.'ms', 500);
}
$obj = load_object($args);
if ($obj['#error']) {
// UNLOCK
_obj_unlock($_l);
return $obj;
} else {
$obj = $obj['#data'];
}
if (is_array($args['attr'])) {
foreach ($args['attr'] as $a) {
if (isset($obj[$a])) {
unset($obj[$a]);
}
}
} elseif (is_string($args['attr'])) {
if (isset($obj[$args['attr']])) {
unset($obj[$args['attr']]);
}
} else {
// UNLOCK
_obj_unlock($_l);
return response('Argument "attr" need to be either array or string', 400);
}
$ret = save_object($obj);
// UNLOCK
_obj_unlock($_l);
return $ret;
}
register_service('glue.object_remove_attr', 'object_remove_attr', array('auth'=>true));
/**
* return an array of all pagenames in the content directory
*
* @param array $args unused
* @return array response
*/
function pagenames($args)
{
if (is_dir(CONTENT_DIR)) {
$files = @scandir(CONTENT_DIR);
$ret = array();
foreach ($files as $f) {
if ($f == '.' || $f == '..' || $f == 'cache' || $f == 'shared') {
continue;
} elseif (!is_dir(CONTENT_DIR.'/'.$f)) {
// skip files
continue;
} elseif (substr($f, 0, 1) == '.') {
// skip directories starting with a dot (like .svn)
continue;
} else {
$ret[] = $f;
}
}
return response($ret);
} else {
return response(array());
}
}
register_service('glue.pagenames', 'pagenames');
/**
* turn an object into an html string
*
* the function also appends the resulting string to the output in
* html.inc.php.
* @param array $args arguments
* string 'name' is the object name (i.e. page.rev.obj)
* bool 'edit' are we editing or not
* @return array response
* html
*/
function render_object($args)
{
// maybe move this to common.inc.php in the future and get rid of some of
// these checks in the beginning
$obj = load_object($args);
if ($obj['#error']) {
return $obj;
} else {
$obj = $obj['#data'];
}
if (!isset($args['edit'])) {
return response('Required argument "edit" missing', 400);
}
if ($args['edit']) {
$args['edit'] = true;
} else {
$args['edit'] = false;
}
log_msg('debug', 'render_object: rendering '.quot($args['name']));
$ret = invoke_hook_while('render_object', false, array('obj'=>$obj, 'edit'=>$args['edit']));
if (empty($ret)) {
log_msg('warn', 'render_object: nobody claimed '.quot($obj['name']));
return response('');
} else {
$temp = array_keys($ret);
log_msg('debug', 'render_object: '.quot($obj['name']).' was handled by '.quot($temp[0]));
$temp = array_values($ret);
// make sure object has a tailing newline
if (0 < strlen($temp[0]) && substr($temp[0], -1) != "\n") {
$temp[0] .= nl();
}
body_append($temp[0]);
// return the element as html-string as well
return response($temp[0]);
}
}
register_service('glue.render_object', 'render_object');
register_hook('render_object', 'render an object');
/**
* turn a page into an html string
*
* the function also appends the resulting string to the output in
* html.inc.php.
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* key 'edit' are we editing or not
* @return array response
* html
*/
function render_page($args)
{
// maybe move this to common.inc.php in the future and get rid of some of
// these checks in the beginning
if (empty($args['page'])) {
return response('Required argument "page" missing or empty', 400);
}
if (!page_exists($args['page'])) {
return response('Page '.quot($args['page']).' does not exist', 404);
}
if (!isset($args['edit'])) {
return response('Required argument "edit" missing', 400);
}
if ($args['edit']) {
$args['edit'] = true;
} else {
$args['edit'] = false;
}
log_msg('debug', 'render_page: rendering '.quot($args['page']));
$bdy = &body();
elem_add_class($bdy, 'page');
elem_attr($bdy, 'id', $args['page']);
invoke_hook('render_page_early', array('page'=>$args['page'], 'edit'=>$args['edit']));
// for every file in the page directory
$files = @scandir(CONTENT_DIR.'/'.str_replace('.', '/', $args['page']));
foreach ($files as $f) {
$fn = CONTENT_DIR.'/'.str_replace('.', '/', $args['page']).'/'.$f;
if ($f == '.' || $f == '..') {
continue;
} elseif (is_link($fn) && !is_file($fn) && !is_dir($fn)) {
// delete dangling symlink
if (@unlink($fn)) {
log_msg('info', 'render_page: deleted dangling symlink '.quot($args['page'].'.'.$f));
} else {
log_msg('error', 'render_page: error deleting dangling symlink '.quot($args['page'].'.'.$f));
}
continue;
}
// render object
render_object(array('name'=>$args['page'].'.'.$f, 'edit'=>$args['edit']));
}
invoke_hook('render_page_late', array('page'=>$args['page'], 'edit'=>$args['edit']));
log_msg('debug', 'render_page: finished '.quot($args['page']));
// return the body element as html-string as well
return response(elem_finalize($bdy));
}
register_service('glue.render_page', 'render_page');
register_hook('render_page_early', 'invoked early in the page rendering');
register_hook('render_page_late', 'invoked after the objects have been rendered');
/**
* rename a page
* @param array $args arguments
* key 'old' old page (i.e. page1.rev)
* key 'new' new page (i.e. page2.rev)
* @return array response
*/
function rename_page($args)
{
if (empty($args['old'])) {
return response('Required argument "old" missing or empty', 400);
}
$pns = pagenames(array());
$pns = $pns['#data'];
if (!in_array($args['old'], $pns)) {
return response('Page name '.quot($args['old']).' does not exist', 404);
}
if (empty($args['new'])) {
return response('Required argument "new" missing or empty', 400);
}
if (in_array($args['new'], $pns)) {
return response('Page name '.quot($args['new']).' already exists', 400);
}
if (!valid_pagename($args['new'].'.head')) {
return response('Invalid page name '.quot($args['new']), 400);
}
if (!@rename(CONTENT_DIR.'/'.$args['old'], CONTENT_DIR.'/'.$args['new'])) {
return response('Error renaming page '.quot($args['old']).' to '.quot($args['new']), 500);
} else {
log_msg('info', 'rename_page: renamed '.quot($args['old']).' to '.quot($args['new']));
// clean up cache as well
$revs = revisions(array('pagename'=>$args['new']));
$revs = $revs['#data'];
foreach ($revs as $rev) {
drop_cache('page', $args['old'].'.'.$rev);
}
invoke_hook('rename_page', array('pagename'=>$args['new']));
return response(true);
}
}
register_service('glue.rename_page', 'rename_page', array('auth'=>true));
register_hook('rename_page', 'invoked when a page has been renamed');
/**
* copy a page
* original page revisions are left out
* @param array $args arguments
* key 'old' old page (i.e. page1.rev)
* key 'new' new page (i.e. page2.rev)
* @return array response
*/
function copy_page($args)
{
if (empty($args['old'])) {
return response('Required argument "old" missing or empty', 400);
}
$pns = pagenames(array());
$pns = $pns['#data'];
if (!in_array($args['old'], $pns)) {
return response('Page name '.quot($args['old']).' does not exist', 404);
}
if (empty($args['new'])) {
return response('Required argument "new" missing or empty', 400);
}
if (in_array($args['new'], $pns)) {
return response('Page name '.quot($args['new']).' already exists', 400);
}
if (!valid_pagename($args['new'].'.head')) {
return response('Invalid page name '.quot($args['new']), 400);
}
$src = CONTENT_DIR.'/'.$args['old'];
$dest = CONTENT_DIR.'/'.$args['new'];
$dirs = scandir($src);
foreach ($dirs as $d) {
// skip root '.', top '..' and revisions 'auto-*'
if ($d == '.' || $d == '..' || substr($d, 0, 5) == 'auto-') {
continue;
// we should only have directories in source
} elseif (is_dir($src.'/'.$d)) {
$m = umask(0000);
if (!@mkdir($dest.'/'.$d, 0777, true)) {
umask($m);
return response('Error creating directory '.quot($dest.'/'.$d), 500);
}
umask($m);
$files = scandir($src.'/'.$d);
foreach ($files as $f) {
if ($f == '.' || $f == '..') {
continue;
} elseif (is_file($src.'/'.$d.'/'.$f)) {
// copy file
$m = umask(0111);
if (!@copy($src.'/'.$d.'/'.$f, $dest.'/'.$d.'/'.$f)) {
log_msg('error', 'copy: error copying '.quot($src.'/'.$d.'/'.$f).' to '.quot($dest.'/'.$d.'/'.$f).', skipping file');
}
umask($m);
}
}
}
}
log_msg('info', 'copy_page: copied '.quot($args['old']).' to '.quot($args['new']));
invoke_hook('copy_page', array('pagename'=>$args['new']));
return response(true);
}
register_service('glue.copy_page', 'copy_page', array('auth'=>true));
register_hook('copy_page', 'invoked when a page has been copied');
/**
* revert to a specific revision of a page
*