-
Notifications
You must be signed in to change notification settings - Fork 28
/
action.php
1924 lines (1696 loc) · 68.9 KB
/
action.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
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <[email protected]>
*/
use dokuwiki\Extension\Event;
use dokuwiki\Subscriptions\SubscriberManager;
use dokuwiki\Utf8\PhpString;
/**
* Class action_plugin_discussion
*
* Data format of file metadir/<id>.comments:
* array = [
* 'status' => int whether comments are 0=disabled/1=open/2=closed,
* 'number' => int number of visible comments,
* 'title' => string|null alternative title for discussion section
* 'comments' => [
* '<cid>'=> [
* 'cid' => string comment id - long random string
* 'raw' => string comment text,
* 'xhtml' => string rendered html,
* 'parent' => null|string null or empty string at highest level, otherwise comment id of parent
* 'replies' => string[] array with comment ids
* 'user' => [
* 'id' => string,
* 'name' => string,
* 'mail' => string,
* 'address' => string,
* 'url' => string
* ],
* 'date' => [
* 'created' => int timestamp,
* 'modified' => int (not defined if not modified)
* ],
* 'show' => bool, whether shown (still be moderated, or hidden by moderator or user self)
* ],
* ...
* ]
* 'subscribers' => [
* '<email>' => [
* 'hash' => string unique token,
* 'active' => bool, true if confirmed
* 'confirmsent' => bool, true if confirmation mail is sent
* ],
* ...
* ]
*/
class action_plugin_discussion extends DokuWiki_Action_Plugin
{
/** @var helper_plugin_avatar */
protected $avatar = null;
/** @var null|string */
protected $style = null;
/** @var null|bool */
protected $useAvatar = null;
/** @var helper_plugin_discussion */
protected $helper = null;
/**
* load helper
*/
public function __construct()
{
$this->helper = plugin_load('helper', 'discussion');
}
/**
* Register the handlers
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object.
*/
public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentActions');
$controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'renderCommentsSection');
$controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'addCommentsToIndex', ['id' => 'page', 'text' => 'body']);
$controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'addCommentsToIndex', ['id' => 'id', 'text' => 'text']);
$controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'addIndexVersion', []);
$controller->register_hook('FULLTEXT_PHRASE_MATCH', 'AFTER', $this, 'fulltextPhraseMatchInComments', []);
$controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'updateCommentStatusFromMetadata', []);
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addToolbarToCommentfield', []);
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'modifyToolbar', []);
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxPreviewComments', []);
$controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'addDiscussionToTOC', []);
}
/**
* Preview Comments
*
* @param Doku_Event $event
* @author Michael Klier <[email protected]>
*/
public function ajaxPreviewComments(Doku_Event $event)
{
global $INPUT;
if ($event->data != 'discussion_preview') return;
$event->preventDefault();
$event->stopPropagation();
print p_locale_xhtml('preview');
print '<div class="comment_preview">';
if (!$INPUT->server->str('REMOTE_USER') && !$this->getConf('allowguests')) {
print p_locale_xhtml('denied');
} else {
print $this->renderComment($INPUT->post->str('comment'));
}
print '</div>';
}
/**
* Adds a TOC item if a discussion exists
*
* @param Doku_Event $event
* @author Michael Klier <[email protected]>
*/
public function addDiscussionToTOC(Doku_Event $event)
{
global $ACT;
if ($this->hasDiscussion($title) && $event->data && $ACT != 'admin') {
$tocitem = [
'hid' => 'discussion__section',
'title' => $title ?: $this->getLang('discussion'),
'type' => 'ul',
'level' => 1
];
$event->data[] = $tocitem;
}
}
/**
* Modify Toolbar for use with discussion plugin
*
* @param Doku_Event $event
* @author Michael Klier <[email protected]>
*/
public function modifyToolbar(Doku_Event $event)
{
global $ACT;
if ($ACT != 'show') return;
if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
$toolbar = [];
foreach ($event->data as $btn) {
if ($btn['type'] == 'mediapopup') continue;
if ($btn['type'] == 'signature') continue;
if ($btn['type'] == 'linkwiz') continue;
if ($btn['type'] == 'NewTable') continue; //skip button for Edittable Plugin
//FIXME does nothing. Checks for '=' on toplevel, but today it are special buttons and a picker with subarray
if (isset($btn['open']) && preg_match("/=+?/", $btn['open'])) continue;
$toolbar[] = $btn;
}
$event->data = $toolbar;
}
}
/**
* Dirty workaround to add a toolbar to the discussion plugin
*
* @param Doku_Event $event
* @author Michael Klier <[email protected]>
*/
public function addToolbarToCommentfield(Doku_Event $event)
{
global $ACT;
global $ID;
if ($ACT != 'show') return;
if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
// FIXME ugly workaround, replace this once DW the toolbar code is more flexible
@require_once(DOKU_INC . 'inc/toolbar.php');
ob_start();
print 'NS = "' . getNS($ID) . '";'; // we have to define NS, otherwise we get get JS errors
toolbar_JSdefines('toolbar');
$script = ob_get_clean();
$event->data['script'][] = ['type' => 'text/javascript', 'charset' => "utf-8", '_data' => $script];
}
}
/**
* Handles comment actions, dispatches data processing routines
*
* @param Doku_Event $event
*/
public function handleCommentActions(Doku_Event $event)
{
global $ID, $INFO, $lang, $INPUT;
// handle newthread ACTs
if ($event->data == 'newthread') {
// we can handle it -> prevent others
$event->data = $this->newThread();
}
// enable captchas
if (in_array($INPUT->str('comment'), ['add', 'save'])) {
$this->captchaCheck();
$this->recaptchaCheck();
}
// if we are not in show mode or someone wants to unsubscribe, that was all for now
if ($event->data != 'show'
&& $event->data != 'discussion_unsubscribe'
&& $event->data != 'discussion_confirmsubscribe') {
return;
}
if ($event->data == 'discussion_unsubscribe' or $event->data == 'discussion_confirmsubscribe') {
if ($INPUT->has('hash')) {
$file = metaFN($ID, '.comments');
$data = unserialize(io_readFile($file));
$matchedMail = '';
foreach ($data['subscribers'] as $mail => $info) {
// convert old style subscribers just in case
if (!is_array($info)) {
$hash = $data['subscribers'][$mail];
$data['subscribers'][$mail]['hash'] = $hash;
$data['subscribers'][$mail]['active'] = true;
$data['subscribers'][$mail]['confirmsent'] = true;
}
if ($data['subscribers'][$mail]['hash'] == $INPUT->str('hash')) {
$matchedMail = $mail;
}
}
if ($matchedMail != '') {
if ($event->data == 'discussion_unsubscribe') {
unset($data['subscribers'][$matchedMail]);
msg(sprintf($lang['subscr_unsubscribe_success'], $matchedMail, $ID), 1);
} else { //$event->data == 'discussion_confirmsubscribe'
$data['subscribers'][$matchedMail]['active'] = true;
msg(sprintf($lang['subscr_subscribe_success'], $matchedMail, $ID), 1);
}
io_saveFile($file, serialize($data));
$event->data = 'show';
}
}
return;
}
// do the data processing for comments
$cid = $INPUT->str('cid');
switch ($INPUT->str('comment')) {
case 'add':
if (empty($INPUT->str('text'))) return; // don't add empty comments
if ($INPUT->server->has('REMOTE_USER') && !$this->getConf('adminimport')) {
$comment['user']['id'] = $INPUT->server->str('REMOTE_USER');
$comment['user']['name'] = $INFO['userinfo']['name'];
$comment['user']['mail'] = $INFO['userinfo']['mail'];
} elseif (($INPUT->server->has('REMOTE_USER') && $this->getConf('adminimport') && $this->helper->isDiscussionModerator())
|| !$INPUT->server->has('REMOTE_USER')) {
// don't add anonymous comments
if (empty($INPUT->str('name')) or empty($INPUT->str('mail'))) {
return;
}
if (!mail_isvalid($INPUT->str('mail'))) {
msg($lang['regbadmail'], -1);
return;
} else {
$comment['user']['id'] = ''; //prevent overlap with loggedin users, before: 'test<ipadress>'
$comment['user']['name'] = hsc($INPUT->str('name'));
$comment['user']['mail'] = hsc($INPUT->str('mail'));
}
}
$comment['user']['address'] = ($this->getConf('addressfield')) ? hsc($INPUT->str('address')) : '';
$comment['user']['url'] = ($this->getConf('urlfield')) ? $this->checkURL($INPUT->str('url')) : '';
$comment['subscribe'] = ($this->getConf('subscribe')) ? $INPUT->has('subscribe') : '';
$comment['date'] = ['created' => $INPUT->str('date')];
$comment['raw'] = cleanText($INPUT->str('text'));
$reply = $INPUT->str('reply');
if ($this->getConf('moderate') && !$this->helper->isDiscussionModerator()) {
$comment['show'] = false;
} else {
$comment['show'] = true;
}
$this->add($comment, $reply);
break;
case 'save':
$raw = cleanText($INPUT->str('text'));
$this->save([$cid], $raw);
break;
case 'delete':
$this->save([$cid], '');
break;
case 'toogle':
$this->save([$cid], '', 'toogle');
break;
}
}
/**
* Main function; dispatches the visual comment actions
*
* @param Doku_Event $event
*/
public function renderCommentsSection(Doku_Event $event)
{
global $INPUT;
if ($event->data != 'show') return; // nothing to do for us
$cid = $INPUT->str('cid');
if (!$cid) {
$cid = $INPUT->str('reply');
}
switch ($INPUT->str('comment')) {
case 'edit':
$this->showDiscussionSection(null, $cid);
break;
default: //'reply' or no action specified
$this->showDiscussionSection($cid);
break;
}
}
/**
* Redirects browser to given comment anchor
*
* @param string $cid comment id
*/
protected function redirect($cid)
{
global $ID;
global $ACT;
if ($ACT !== 'show') return;
if ($this->getConf('moderate') && !$this->helper->isDiscussionModerator()) {
msg($this->getLang('moderation'), 1);
@session_start();
global $MSG;
$_SESSION[DOKU_COOKIE]['msg'] = $MSG;
session_write_close();
$url = wl($ID);
} else {
$url = wl($ID) . '#comment_' . $cid;
}
if (function_exists('send_redirect')) {
send_redirect($url);
} else {
header('Location: ' . $url);
}
exit();
}
/**
* Checks config settings to enable/disable discussions
*
* @return bool true if enabled
*/
public function isDiscussionEnabled()
{
global $ID;
if ($this->getConf('excluded_ns') == '') {
$isNamespaceExcluded = false;
} else {
$ns = getNS($ID); // $INFO['namespace'] is not yet available, if used in update_comment_status()
$isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $ns);
}
if ($this->getConf('automatic')) {
if ($isNamespaceExcluded) {
return false;
} else {
return true;
}
} else {
if ($isNamespaceExcluded) {
return true;
} else {
return false;
}
}
}
/**
* Shows all comments of the current page, if no reply or edit requested, then comment form is shown on the end
*
* @param null|string $reply comment id on which the user requested a reply
* @param null|string $edit comment id which the user requested for editing
*/
protected function showDiscussionSection($reply = null, $edit = null)
{
global $ID, $INFO, $INPUT;
// get .comments meta file name
$file = metaFN($ID, '.comments');
if (!$INFO['exists']) return;
if (!@file_exists($file) && !$this->isDiscussionEnabled()) return;
if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('showguests')) return;
// load data
$data = [];
if (@file_exists($file)) {
$data = unserialize(io_readFile($file, false));
// comments are turned off
if (!$data['status']) {
return;
}
} elseif (!@file_exists($file) && $this->isDiscussionEnabled()) {
// set status to show the comment form
$data['status'] = 1;
$data['number'] = 0;
$data['title'] = null;
}
// show discussion wrapper only on certain circumstances
if (empty($data['comments']) || !is_array($data['comments'])) {
$cnt = 0;
$cids = [];
} else {
$cnt = count($data['comments']);
$cids = array_keys($data['comments']);
}
$show = false;
if ($cnt > 1 || ($cnt == 1 && $data['comments'][$cids[0]]['show'] == 1)
|| $this->getConf('allowguests')
|| $INPUT->server->has('REMOTE_USER')) {
$show = true;
// section title
$title = (!empty($data['title']) ? hsc($data['title']) : $this->getLang('discussion'));
ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section
ptln('<h2><a name="discussion__section" id="discussion__section">', 2);
ptln($title, 4);
ptln('</a></h2>', 2);
ptln('<div class="level2 hfeed">', 2);
}
// now display the comments
if (isset($data['comments'])) {
if (!$this->getConf('usethreading')) {
$data['comments'] = $this->flattenThreads($data['comments']);
uasort($data['comments'], [$this, 'sortThreadsOnCreation']);
}
if ($this->getConf('newestfirst')) {
$data['comments'] = array_reverse($data['comments']);
}
foreach ($data['comments'] as $cid => $value) {
if ($cid == $edit) { // edit form
$this->showCommentForm($value['raw'], 'save', $edit);
} else {
$this->showCommentWithReplies($cid, $data, '', $reply);
}
}
}
// comment form shown on the end, if no comment form of $reply or $edit is requested before
if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) {
$this->showCommentForm('', 'add');
}
if ($show) {
ptln('</div>', 2); // level2 hfeed
ptln('</div>'); // comment_wrapper
}
// check for toggle print configuration
if ($this->getConf('visibilityButton')) {
// print the hide/show discussion section button
$this->showDiscussionToggleButton();
}
}
/**
* Remove the parent-child relation, such that the comment structure becomes flat
*
* @param array $comments array with all comments
* @param null|array $cids comment ids of replies, which should be flatten
* @return array returned array with flattened comment structure
*/
protected function flattenThreads($comments, $cids = null)
{
if (is_null($cids)) {
$cids = array_keys($comments);
}
foreach ($cids as $cid) {
if (!empty($comments[$cid]['replies'])) {
$rids = $comments[$cid]['replies'];
$comments = $this->flattenThreads($comments, $rids);
$comments[$cid]['replies'] = [];
}
$comments[$cid]['parent'] = '';
}
return $comments;
}
/**
* Adds a new comment and then displays all comments
*
* @param array $comment with
* 'raw' => string comment text,
* 'user' => [
* 'id' => string,
* 'name' => string,
* 'mail' => string
* ],
* 'date' => [
* 'created' => int timestamp
* ]
* 'show' => bool
* 'subscribe' => bool
* @param string $parent comment id of parent
* @return bool
*/
protected function add($comment, $parent)
{
global $ID, $TEXT, $INPUT;
$originalTxt = $TEXT; // set $TEXT to comment text for wordblock check
$TEXT = $comment['raw'];
// spamcheck against the DokuWiki blacklist
if (checkwordblock()) {
msg($this->getLang('wordblock'), -1);
return false;
}
if (!$this->getConf('allowguests')
&& $comment['user']['id'] != $INPUT->server->str('REMOTE_USER')
) {
return false; // guest comments not allowed
}
$TEXT = $originalTxt; // restore global $TEXT
// get discussion meta file name
$file = metaFN($ID, '.comments');
// create comments file if it doesn't exist yet
if (!@file_exists($file)) {
$data = [
'status' => 1,
'number' => 0,
'title' => null
];
io_saveFile($file, serialize($data));
} else {
$data = unserialize(io_readFile($file, false));
// comments off or closed
if ($data['status'] != 1) {
return false;
}
}
if ($comment['date']['created']) {
$date = strtotime($comment['date']['created']);
} else {
$date = time();
}
if ($date == -1) {
$date = time();
}
$cid = md5($comment['user']['id'] . $date); // create a unique id
if (!isset($data['comments'][$parent]) || !is_array($data['comments'][$parent])) {
$parent = null; // invalid parent comment
}
// render the comment
$xhtml = $this->renderComment($comment['raw']);
// fill in the new comment
$data['comments'][$cid] = [
'user' => $comment['user'],
'date' => ['created' => $date],
'raw' => $comment['raw'],
'xhtml' => $xhtml,
'parent' => $parent,
'replies' => [],
'show' => $comment['show']
];
if ($comment['subscribe']) {
$mail = $comment['user']['mail'];
if (isset($data['subscribers'])) {
if (!$data['subscribers'][$mail]) {
$data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
$data['subscribers'][$mail]['active'] = false;
$data['subscribers'][$mail]['confirmsent'] = false;
} else {
// convert old style subscribers and set them active
if (!is_array($data['subscribers'][$mail])) {
$hash = $data['subscribers'][$mail];
$data['subscribers'][$mail]['hash'] = $hash;
$data['subscribers'][$mail]['active'] = true;
$data['subscribers'][$mail]['confirmsent'] = true;
}
}
} else {
$data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
$data['subscribers'][$mail]['active'] = false;
$data['subscribers'][$mail]['confirmsent'] = false;
}
}
// update parent comment
if ($parent) {
$data['comments'][$parent]['replies'][] = $cid;
}
// update the number of comments
$data['number']++;
// notify subscribers of the page
$data['comments'][$cid]['cid'] = $cid;
$this->notify($data['comments'][$cid], $data['subscribers']);
// save the comment metadata file
io_saveFile($file, serialize($data));
$this->addLogEntry($date, $ID, 'cc', '', $cid);
$this->redirect($cid);
return true;
}
/**
* Saves the comment with the given ID and then displays all comments
*
* @param array|string $cids array with comment ids to save, or a single string comment id
* @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!)
* @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw
* @return bool succeed?
*/
public function save($cids, $raw, $act = null)
{
global $ID, $INPUT;
if (empty($cids)) return false; // do nothing if we get no comment id
if ($raw) {
global $TEXT;
$otxt = $TEXT; // set $TEXT to comment text for wordblock check
$TEXT = $raw;
// spamcheck against the DokuWiki blacklist
if (checkwordblock()) {
msg($this->getLang('wordblock'), -1);
return false;
}
$TEXT = $otxt; // restore global $TEXT
}
// get discussion meta file name
$file = metaFN($ID, '.comments');
$data = unserialize(io_readFile($file, false));
if (!is_array($cids)) {
$cids = [$cids];
}
foreach ($cids as $cid) {
if (is_array($data['comments'][$cid]['user'])) {
$user = $data['comments'][$cid]['user']['id'];
$convert = false;
} else {
$user = $data['comments'][$cid]['user'];
$convert = true;
}
// someone else was trying to edit our comment -> abort
if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionModerator()) {
return false;
}
$date = time();
// need to convert to new format?
if ($convert) {
$data['comments'][$cid]['user'] = [
'id' => $user,
'name' => $data['comments'][$cid]['name'],
'mail' => $data['comments'][$cid]['mail'],
'url' => $data['comments'][$cid]['url'],
'address' => $data['comments'][$cid]['address'],
];
$data['comments'][$cid]['date'] = [
'created' => $data['comments'][$cid]['date']
];
}
if ($act == 'toogle') { // toogle visibility
$now = $data['comments'][$cid]['show'];
$data['comments'][$cid]['show'] = !$now;
$data['number'] = $this->countVisibleComments($data);
$type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
} elseif ($act == 'show') { // show comment
$data['comments'][$cid]['show'] = true;
$data['number'] = $this->countVisibleComments($data);
$type = 'sc'; // show comment
} elseif ($act == 'hide') { // hide comment
$data['comments'][$cid]['show'] = false;
$data['number'] = $this->countVisibleComments($data);
$type = 'hc'; // hide comment
} elseif (!$raw) { // remove the comment
$data['comments'] = $this->removeComment($cid, $data['comments']);
$data['number'] = $this->countVisibleComments($data);
$type = 'dc'; // delete comment
} else { // save changed comment
$xhtml = $this->renderComment($raw);
// now change the comment's content
$data['comments'][$cid]['date']['modified'] = $date;
$data['comments'][$cid]['raw'] = $raw;
$data['comments'][$cid]['xhtml'] = $xhtml;
$type = 'ec'; // edit comment
}
}
// save the comment metadata file
io_saveFile($file, serialize($data));
$this->addLogEntry($date, $ID, $type, '', $cid);
$this->redirect($cid);
return true;
}
/**
* Recursive function to remove a comment from the data array
*
* @param string $cid comment id to be removed
* @param array $comments array with all comments
* @return array returns modified array with all remaining comments
*/
protected function removeComment($cid, $comments)
{
if (is_array($comments[$cid]['replies'])) {
foreach ($comments[$cid]['replies'] as $rid) {
$comments = $this->removeComment($rid, $comments);
}
}
unset($comments[$cid]);
return $comments;
}
/**
* Prints an individual comment
*
* @param string $cid comment id
* @param array $data array with all comments by reference
* @param string $parent comment id of parent
* @param string $reply comment id on which the user requested a reply
* @param bool $isVisible is marked as visible
*/
protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true)
{
// comment was removed
if (!isset($data['comments'][$cid])) {
return;
}
$comment = $data['comments'][$cid];
// corrupt datatype
if (!is_array($comment)) {
return;
}
// handle only replies to given parent comment
if ($comment['parent'] != $parent) {
return;
}
// comment hidden, only shown for moderators
if (!$comment['show'] && !$this->helper->isDiscussionModerator()) {
return;
}
// print the actual comment
$this->showComment($cid, $data, $reply, $isVisible);
// replies to this comment entry?
$this->showReplies($cid, $data, $reply, $isVisible);
// reply form
$this->showReplyForm($cid, $reply);
}
/**
* Print the comment
*
* @param string $cid comment id
* @param array $data array with all comments
* @param string $reply comment id on which the user requested a reply
* @param bool $isVisible (grand)parent is marked as visible
*/
protected function showComment($cid, $data, $reply, $isVisible)
{
global $conf, $lang, $HIGH, $INPUT;
$comment = $data['comments'][$cid];
//only moderators can arrive here if hidden
$class = '';
if (!$comment['show'] || !$isVisible) {
$class = ' comment_hidden';
}
if($cid === $reply) {
$class .= ' reply';
}
// comment head with date and user data
ptln('<div class="hentry' . $class . '">', 4);
ptln('<div class="comment_head">', 6);
ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8);
$head = '<span class="vcard author">';
// prepare variables
if (is_array($comment['user'])) { // new format
$user = $comment['user']['id'];
$name = $comment['user']['name'];
$mail = $comment['user']['mail'];
$url = $comment['user']['url'];
$address = $comment['user']['address'];
} else { // old format
$user = $comment['user'];
$name = $comment['name'];
$mail = $comment['mail'];
$url = $comment['url'];
$address = $comment['address'];
}
if (is_array($comment['date'])) { // new format
$created = $comment['date']['created'];
$modified = $comment['date']['modified'] ?? null;
} else { // old format
$created = $comment['date'];
$modified = $comment['edited'];
}
// show username or real name?
if (!$this->getConf('userealname') && $user) {
//not logged-in users have currently username set to '', but before 'test<Ipaddress>'
if(substr($user, 0,4) === 'test'
&& (strpos($user, ':', 4) !== false || strpos($user, '.', 4) !== false)) {
$showname = $name;
} else {
$showname = $user;
}
} else {
$showname = $name;
}
// show avatar image?
if ($this->useAvatar()) {
$user_data['name'] = $name;
$user_data['user'] = $user;
$user_data['mail'] = $mail;
$align = $lang['direction'] === 'ltr' ? 'left' : 'right';
$avatar = $this->avatar->getXHTML($user_data, $name, $align);
if ($avatar) {
$head .= $avatar;
}
}
if ($this->getConf('linkemail') && $mail) {
$head .= $this->email($mail, $showname, 'email fn');
} elseif ($url) {
$head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn');
} else {
$head .= '<span class="fn">' . $showname . '</span>';
}
if ($address) {
$head .= ', <span class="adr">' . $address . '</span>';
}
$head .= '</span>, ' .
'<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' .
dformat($created, $conf['dformat']) . '</abbr>';
if ($modified) {
$head .= ', <abbr class="updated" title="' .
strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) .
'</abbr>';
}
ptln($head, 8);
ptln('</div>', 6); // class="comment_head"
// main comment content
ptln('<div class="comment_body entry-content"' .
($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6);
echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF;
ptln('</div>', 6); // class="comment_body"
if ($isVisible) {
ptln('<div class="comment_buttons">', 6);
// show reply button?
if ($data['status'] == 1 && !$reply && $comment['show']
&& ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER'))
&& $this->getConf('usethreading')
) {
$this->showButton($cid, $this->getLang('btn_reply'), 'reply', true);
}
// show edit, show/hide and delete button?
if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionModerator()) {
$this->showButton($cid, $lang['btn_secedit'], 'edit', true);
$label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
$this->showButton($cid, $label, 'toogle');
$this->showButton($cid, $lang['btn_delete'], 'delete');
}
ptln('</div>', 6); // class="comment_buttons"
}
ptln('</div>', 4); // class="hentry"
}
/**
* If requested by user, show comment form to write a reply
*
* @param string $cid current comment id
* @param string $reply comment id on which the user requested a reply
*/
protected function showReplyForm($cid, $reply)
{
if ($this->getConf('usethreading') && $reply == $cid) {
ptln('<div class="comment_replies reply">', 4);
$this->showCommentForm('', 'add', $cid);
ptln('</div>', 4); // class="comment_replies"
}
}
/**
* Show the replies to the given comment
*
* @param string $cid comment id
* @param array $data array with all comments by reference
* @param string $reply comment id on which the user requested a reply
* @param bool $isVisible is marked as visible by reference
*/
protected function showReplies($cid, &$data, $reply, &$isVisible)
{
$comment = $data['comments'][$cid];
if (!empty($comment['replies'])) {
return;
}
ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4);
$isVisible = ($comment['show'] && $isVisible);
foreach ($comment['replies'] as $rid) {
$this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible);
}
ptln('</div>', 4);
}
/**
* Is an avatar displayed?
*
* @return bool
*/
protected function useAvatar()
{
if (is_null($this->useAvatar)) {
$this->useAvatar = $this->getConf('useavatar')
&& ($this->avatar = $this->loadHelper('avatar', false));
}
return $this->useAvatar;
}
/**
* Calculate width of indent
*
* @return string
*/
protected function getWidthStyle()
{
global $lang;
if (is_null($this->style)) {
$side = $lang['direction'] === 'ltr' ? 'left' : 'right';
if ($this->useAvatar()) {
$this->style = ' style="margin-' . $side . ': ' . ($this->avatar->getConf('size') + 14) . 'px;"';
} else {
$this->style = ' style="margin-' . $side . ': 20px;"';
}
}
return $this->style;
}