forked from dokufreaks/plugin-discussion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
1767 lines (1558 loc) · 61.1 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]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
/**
* Class action_plugin_discussion
*/
class action_plugin_discussion extends DokuWiki_Action_Plugin{
/** @var helper_plugin_avatar */
var $avatar = null;
var $style = null;
var $use_avatar = null;
/** @var helper_plugin_discussion */
var $helper = null;
/**
* load helper
*/
public function __construct() {
$this->helper = plugin_load('helper', 'discussion');
}
/**
* Register the handlers
*
* @param Doku_Event_Handler $contr DokuWiki's event controller object.
*/
public function register(Doku_Event_Handler $contr) {
$contr->register_hook(
'ACTION_ACT_PREPROCESS',
'BEFORE',
$this,
'handle_act_preprocess',
array()
);
$contr->register_hook(
'TPL_ACT_RENDER',
'AFTER',
$this,
'comments',
array()
);
$contr->register_hook(
'INDEXER_PAGE_ADD',
'AFTER',
$this,
'idx_add_discussion',
array('id' => 'page', 'text' => 'body')
);
$contr->register_hook(
'FULLTEXT_SNIPPET_CREATE',
'BEFORE',
$this,
'idx_add_discussion',
array('id' => 'id', 'text' => 'text')
);
$contr->register_hook(
'INDEXER_VERSION_GET',
'BEFORE',
$this,
'idx_version',
array()
);
$contr->register_hook(
'FULLTEXT_PHRASE_MATCH',
'AFTER',
$this,
'ft_phrase_match',
array()
);
$contr->register_hook(
'PARSER_METADATA_RENDER',
'AFTER',
$this,
'update_comment_status',
array()
);
$contr->register_hook(
'TPL_METAHEADER_OUTPUT',
'BEFORE',
$this,
'handle_tpl_metaheader_output',
array()
);
$contr->register_hook(
'TOOLBAR_DEFINE',
'AFTER',
$this,
'handle_toolbar_define',
array()
);
$contr->register_hook(
'AJAX_CALL_UNKNOWN',
'BEFORE',
$this,
'handle_ajax_call',
array()
);
$contr->register_hook(
'TPL_TOC_RENDER',
'BEFORE',
$this,
'handle_toc_render',
array()
);
}
/**
* Preview Comments
*
* @author Michael Klier <[email protected]>
*
* @param Doku_Event $event
* @param $params
*/
public function handle_ajax_call(Doku_Event $event, $params) {
if($event->data != 'discussion_preview') return;
$event->preventDefault();
$event->stopPropagation();
print p_locale_xhtml('preview');
print '<div class="comment_preview">';
if(!$_SERVER['REMOTE_USER'] && !$this->getConf('allowguests')) {
print p_locale_xhtml('denied');
} else {
print $this->_render($_REQUEST['comment']);
}
print '</div>';
}
/**
* Adds a TOC item if a discussion exists
*
* @author Michael Klier <[email protected]>
*
* @param Doku_Event $event
* @param $params
*/
public function handle_toc_render(Doku_Event $event, $params) {
global $ACT;
if($this->_hasDiscussion($title) && $event->data && $ACT != 'admin') {
$tocitem = array( 'hid' => 'discussion__section',
'title' => $this->getLang('discussion'),
'type' => 'ul',
'level' => 1 );
array_push($event->data, $tocitem);
}
}
/**
* Modify Tollbar for use with discussion plugin
*
* @author Michael Klier <[email protected]>
*
* @param Doku_Event $event
* @param $param
*/
public function handle_toolbar_define(Doku_Event $event, $param) {
global $ACT;
if($ACT != 'show') return;
if($this->_hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
$toolbar = array();
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
if(preg_match("/=+?/", $btn['open'])) continue;
array_push($toolbar, $btn);
}
$event->data = $toolbar;
}
}
/**
* Dirty workaround to add a toolbar to the discussion plugin
*
* @author Michael Klier <[email protected]>
*
* @param Doku_Event $event
* @param $param
*/
public function handle_tpl_metaheader_output(Doku_Event $event, $param) {
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();
array_push($event->data['script'], array('type' => 'text/javascript', 'charset' => "utf-8", '_data' => $script));
}
}
/**
* Handles comment actions, dispatches data processing routines
*
* @param Doku_Event $event
* @param $param
* @return bool
*/
public function handle_act_preprocess(Doku_Event $event, $param) {
global $ID;
global $INFO;
global $lang;
// handle newthread ACTs
if ($event->data == 'newthread') {
// we can handle it -> prevent others
$event->preventDefault();
$event->data = $this->_newThread();
}
// enable captchas
if (in_array($_REQUEST['comment'], array('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') {
// ok we can handle it prevent others
$event->preventDefault();
if (!isset($_REQUEST['hash'])) {
return;
} else {
$file = metaFN($ID, '.comments');
$data = unserialize(io_readFile($file));
$themail = '';
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'] == $_REQUEST['hash']) {
$themail = $mail;
}
}
if($themail != '') {
if($event->data == 'discussion_unsubscribe') {
unset($data['subscribers'][$themail]);
msg(sprintf($lang['subscr_unsubscribe_success'], $themail, $ID), 1);
} elseif($event->data == 'discussion_confirmsubscribe') {
$data['subscribers'][$themail]['active'] = true;
msg(sprintf($lang['subscr_subscribe_success'], $themail, $ID), 1);
}
io_saveFile($file, serialize($data));
$event->data = 'show';
}
return;
}
} else {
// do the data processing for comments
$cid = $_REQUEST['cid'];
switch ($_REQUEST['comment']) {
case 'add':
if(empty($_REQUEST['text'])) return; // don't add empty comments
if(isset($_SERVER['REMOTE_USER']) && !$this->getConf('adminimport')) {
$comment['user']['id'] = $_SERVER['REMOTE_USER'];
$comment['user']['name'] = $INFO['userinfo']['name'];
$comment['user']['mail'] = $INFO['userinfo']['mail'];
} elseif((isset($_SERVER['REMOTE_USER']) && $this->getConf('adminimport') && $this->helper->isDiscussionMod()) || !isset($_SERVER['REMOTE_USER'])) {
if(empty($_REQUEST['name']) or empty($_REQUEST['mail'])) return; // don't add anonymous comments
if(!mail_isvalid($_REQUEST['mail'])) {
msg($lang['regbadmail'], -1);
return;
} else {
$comment['user']['id'] = 'test'.hsc($_REQUEST['user']);
$comment['user']['name'] = hsc($_REQUEST['name']);
$comment['user']['mail'] = hsc($_REQUEST['mail']);
}
}
$comment['user']['address'] = ($this->getConf('addressfield')) ? hsc($_REQUEST['address']) : '';
$comment['user']['url'] = ($this->getConf('urlfield')) ? $this->_checkURL($_REQUEST['url']) : '';
$comment['subscribe'] = ($this->getConf('subscribe')) ? $_REQUEST['subscribe'] : '';
$comment['date'] = array('created' => $_REQUEST['date']);
$comment['raw'] = cleanText($_REQUEST['text']);
$repl = $_REQUEST['reply'];
if($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
$comment['show'] = false;
} else {
$comment['show'] = true;
}
$this->_add($comment, $repl);
break;
case 'save':
$raw = cleanText($_REQUEST['text']);
$this->save(array($cid), $raw);
break;
case 'delete':
$this->save(array($cid), '');
break;
case 'toogle':
$this->save(array($cid), '', 'toogle');
break;
}
}
}
/**
* Main function; dispatches the visual comment actions
*/
public function comments(Doku_Event $event, $param) {
if ($event->data != 'show') return; // nothing to do for us
$cid = $_REQUEST['cid'];
if(!$cid) {
$cid = $_REQUEST['reply'];
}
switch ($_REQUEST['comment']) {
case 'edit':
$this->_show(NULL, $cid);
break;
default:
$this->_show($cid);
break;
}
}
/**
* Redirects browser to given comment anchor
*/
protected function _redirect($cid) {
global $ID;
global $ACT;
if ($ACT !== 'show') return;
if($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
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
*/
public function isDiscussionEnabled() {
global $INFO;
if($this->getConf('excluded_ns') == '') {
$isNamespaceExcluded = false;
} else {
$isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $INFO['namespace']);
}
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
*/
protected function _show($reply = null, $edit = null) {
global $ID;
global $INFO;
// get .comments meta file name
$file = metaFN($ID, '.comments');
if (!$INFO['exists']) return false;
if (!@file_exists($file) && !$this->isDiscussionEnabled()) return false;
if (!$_SERVER['REMOTE_USER'] && !$this->getConf('showguests')) return false;
// load data
if (@file_exists($file)) {
$data = unserialize(io_readFile($file, false));
if (!$data['status']) return false; // comments are turned off
} elseif (!@file_exists($file) && $this->isDiscussionEnabled() && $INFO['exists']) {
// set status to show the comment form
$data['status'] = 1;
$data['number'] = 0;
}
// show discussion wrapper only on certain circumstances
$cnt = count($data['comments']);
$keys = @array_keys($data['comments']);
$show = false;
if($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1) || $this->getConf('allowguests') || isset($_SERVER['REMOTE_USER'])) {
$show = true;
// section title
$title = ($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'], '_sortCallback');
}
if($this->getConf('newestfirst')) {
$data['comments'] = array_reverse($data['comments']);
}
foreach ($data['comments'] as $key => $value) {
if ($key == $edit) $this->_form($value['raw'], 'save', $edit); // edit form
else $this->_print($key, $data, '', $reply);
}
}
// comment form
if (($data['status'] == 1) && (!$reply || !$this->getConf('usethreading')) && !$edit) $this->_form('');
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->_print_toggle_button();
}
return true;
}
/**
* @param array $comments
* @param null|array $keys
* @return array
*/
protected function _flattenThreads($comments, $keys = null) {
if (is_null($keys))
$keys = array_keys($comments);
foreach($keys as $cid) {
if (!empty($comments[$cid]['replies'])) {
$rids = $comments[$cid]['replies'];
$comments = $this->_flattenThreads($comments, $rids);
$comments[$cid]['replies'] = array();
}
$comments[$cid]['parent'] = '';
}
return $comments;
}
/**
* Adds a new comment and then displays all comments
*
* @param array $comment
* @param string $parent
* @return bool
*/
protected function _add($comment, $parent) {
global $ID;
global $TEXT;
$otxt = $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'] != $_SERVER['REMOTE_USER'])
) {
return false; // guest comments not allowed
}
$TEXT = $otxt; // 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 = array('status' => 1, 'number' => 0);
io_saveFile($file, serialize($data));
} else {
$data = unserialize(io_readFile($file, false));
if ($data['status'] != 1) return false; // comments off or closed
}
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 (!is_array($data['comments'][$parent])) {
$parent = NULL; // invalid parent comment
}
// render the comment
$xhtml = $this->_render($comment['raw']);
// fill in the new comment
$data['comments'][$cid] = array(
'user' => $comment['user'],
'date' => array('created' => $date),
'raw' => $comment['raw'],
'xhtml' => $xhtml,
'parent' => $parent,
'replies' => array(),
'show' => $comment['show']
);
if($comment['subscribe']) {
$mail = $comment['user']['mail'];
if($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
* @param string $raw
* @param string $act
* @return bool
*/
public function save($cids, $raw, $act = NULL) {
global $ID;
if(!$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 = array($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 != $_SERVER['REMOTE_USER']) && (!$this->helper->isDiscussionMod())) return false;
$date = time();
// need to convert to new format?
if ($convert) {
$data['comments'][$cid]['user'] = array(
'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'] = array(
'created' => $data['comments'][$cid]['date']
);
}
if ($act == 'toogle') { // toogle visibility
$now = $data['comments'][$cid]['show'];
$data['comments'][$cid]['show'] = !$now;
$data['number'] = $this->_count($data);
$type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
} elseif ($act == 'show') { // show comment
$data['comments'][$cid]['show'] = true;
$data['number'] = $this->_count($data);
$type = 'sc'; // show comment
} elseif ($act == 'hide') { // hide comment
$data['comments'][$cid]['show'] = false;
$data['number'] = $this->_count($data);
$type = 'hc'; // hide comment
} elseif (!$raw) { // remove the comment
$data['comments'] = $this->_removeComment($cid, $data['comments']);
$data['number'] = $this->_count($data);
$type = 'dc'; // delete comment
} else { // save changed comment
$xhtml = $this->_render($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
*/
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
* @param array $data
* @param string $parent
* @param string $reply
* @param bool $visible
* @return bool
*/
protected function _print($cid, &$data, $parent = '', $reply = '', $visible = true) {
if (!isset($data['comments'][$cid])) return false; // comment was removed
$comment = $data['comments'][$cid];
if (!is_array($comment)) return false; // corrupt datatype
if ($comment['parent'] != $parent) return true; // reply to an other comment
if (!$comment['show']) { // comment hidden
if ($this->helper->isDiscussionMod()) $hidden = ' comment_hidden';
else return true;
} else {
$hidden = '';
}
// print the actual comment
$this->_print_comment($cid, $data, $parent, $reply, $visible, $hidden);
// replies to this comment entry?
$this->_print_replies($cid, $data, $reply, $visible);
// reply form
$this->_print_form($cid, $reply);
return true;
}
/**
* @param $cid
* @param $data
* @param $parent
* @param $reply
* @param $visible
* @param $hidden
*/
protected function _print_comment($cid, &$data, $parent, $reply, $visible, $hidden) {
global $conf, $lang, $HIGH;
$comment = $data['comments'][$cid];
// comment head with date and user data
ptln('<div class="hentry'.$hidden.'">', 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'];
} else { // old format
$created = $comment['date'];
$modified = $comment['edited'];
}
// show username or real name?
if ((!$this->getConf('userealname')) && ($user)) {
$showname = $user;
} else {
$showname = $name;
}
// show avatar image?
if ($this->_use_avatar()) {
$user_data['name'] = $name;
$user_data['user'] = $user;
$user_data['mail'] = $mail;
$avatar = $this->avatar->getXHTML($user_data, $name, 'left');
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->_use_avatar() ? $this->_get_style() : '').'>', 6);
echo ($HIGH?html_hilight($comment['xhtml'],$HIGH):$comment['xhtml']).DOKU_LF;
ptln('</div>', 6); // class="comment_body"
if ($visible) {
ptln('<div class="comment_buttons">', 6);
// show reply button?
if (($data['status'] == 1) && !$reply && $comment['show']
&& ($this->getConf('allowguests') || $_SERVER['REMOTE_USER']) && $this->getConf('usethreading')
) {
$this->_button($cid, $this->getLang('btn_reply'), 'reply', true);
}
// show edit, show/hide and delete button?
if ((($user == $_SERVER['REMOTE_USER']) && ($user != '')) || ($this->helper->isDiscussionMod())) {
$this->_button($cid, $lang['btn_secedit'], 'edit', true);
$label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
$this->_button($cid, $label, 'toogle');
$this->_button($cid, $lang['btn_delete'], 'delete');
}
ptln('</div>', 6); // class="comment_buttons"
}
ptln('</div>', 4); // class="hentry"
}
/**
* @param string $cid
* @param string $reply
*/
protected function _print_form($cid, $reply)
{
if ($this->getConf('usethreading') && $reply == $cid) {
ptln('<div class="comment_replies">', 4);
$this->_form('', 'add', $cid);
ptln('</div>', 4); // class="comment_replies"
}
}
/**
* @param string $cid
* @param array $data
* @param string $reply
* @param bool $visible
*/
protected function _print_replies($cid, &$data, $reply, &$visible)
{
$comment = $data['comments'][$cid];
if (!count($comment['replies'])) {
return;
}
ptln('<div class="comment_replies"'.$this->_get_style().'>', 4);
$visible = ($comment['show'] && $visible);
foreach ($comment['replies'] as $rid) {
$this->_print($rid, $data, $cid, $reply, $visible);
}
ptln('</div>', 4);
}
/**
* Is an avatar displayed?
*
* @return bool
*/
protected function _use_avatar()
{
if (is_null($this->use_avatar)) {
$this->use_avatar = $this->getConf('useavatar')
&& (!plugin_isdisabled('avatar'))
&& ($this->avatar =& plugin_load('helper', 'avatar'));
}
return $this->use_avatar;
}
/**
* Calculate width of indent
*
* @return string
*/
protected function _get_style() {
if (is_null($this->style)){
if ($this->_use_avatar()) {
$this->style = ' style="margin-left: '.($this->avatar->getConf('size') + 14).'px;"';
} else {
$this->style = ' style="margin-left: 20px;"';
}
}
return $this->style;
}
/**
* Show the button which toggle the visibility of the discussion section
*/
protected function _print_toggle_button() {
ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">');
ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button" value="'.$this->getLang('toggle_display').'">');
ptln('</div>');
}
/**
* Outputs the comment form
*/
protected function _form($raw = '', $act = 'add', $cid = NULL) {
global $lang;
global $conf;
global $ID;
// not for unregistered users when guest comments aren't allowed
if (!$_SERVER['REMOTE_USER'] && !$this->getConf('allowguests')) {
?>
<div class="comment_form">
<?php echo $this->getLang('noguests'); ?>
</div>
<?php
return;
}
// fill $raw with $_REQUEST['text'] if it's empty (for failed CAPTCHA check)
if (!$raw && ($_REQUEST['comment'] == 'show')) {
$raw = $_REQUEST['text'];
}
?>
<div class="comment_form">
<form id="discussion__comment_form" method="post" action="<?php echo script() ?>" accept-charset="<?php echo $lang['encoding'] ?>">
<div class="no">
<input type="hidden" name="id" value="<?php echo $ID ?>" />
<input type="hidden" name="do" value="show" />
<input type="hidden" name="comment" value="<?php echo $act ?>" />
<?php
// for adding a comment
if ($act == 'add') {
?>
<input type="hidden" name="reply" value="<?php echo $cid ?>" />
<?php
// for guest/adminimport: show name, e-mail and subscribe to comments fields
if(!$_SERVER['REMOTE_USER'] or ($this->getConf('adminimport') && $this->helper->isDiscussionMod())) {
?>
<input type="hidden" name="user" value="<?php echo clientIP() ?>" />
<div class="comment_name">
<label class="block" for="discussion__comment_name">
<span><?php echo $lang['fullname'] ?>:</span>
<input type="text" class="edit<?php if($_REQUEST['comment'] == 'add' && empty($_REQUEST['name'])) echo ' error'?>" name="name" id="discussion__comment_name" size="50" tabindex="1" value="<?php echo hsc($_REQUEST['name'])?>" />
</label>
</div>
<div class="comment_mail">
<label class="block" for="discussion__comment_mail">
<span><?php echo $lang['email'] ?>:</span>
<input type="text" class="edit<?php if($_REQUEST['comment'] == 'add' && empty($_REQUEST['mail'])) echo ' error'?>" name="mail" id="discussion__comment_mail" size="50" tabindex="2" value="<?php echo hsc($_REQUEST['mail'])?>" />
</label>
</div>
<?php
}
// allow entering an URL
if ($this->getConf('urlfield')) {
?>
<div class="comment_url">
<label class="block" for="discussion__comment_url">
<span><?php echo $this->getLang('url') ?>:</span>
<input type="text" class="edit" name="url" id="discussion__comment_url" size="50" tabindex="3" value="<?php echo hsc($_REQUEST['url'])?>" />
</label>
</div>