xref: /plugin/discussion/action.php (revision f8c74f2e3422ec044a28e4476a54ab3fd167c333)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7use dokuwiki\Extension\Event;
8use dokuwiki\Subscriptions\SubscriberManager;
9use dokuwiki\Utf8\PhpString;
10
11/**
12 * Class action_plugin_discussion
13 *
14 * Data format of file metadir/<id>.comments:
15 * array = [
16 *  'status' => int whether comments are 0=disabled/1=open/2=closed,
17 *  'number' => int number of visible comments,
18 *  'title' => string alternative title for discussion section
19 *  'comments' => [
20 *      '<cid>'=> [
21 *          'cid' => string comment id - long random string
22 *          'raw' => string comment text,
23 *          'xhtml' => string rendered html,
24 *          'parent' => null|string null or empty string at highest level, otherwise comment id of parent
25 *          'replies' => string[] array with comment ids
26 *          'user' => [
27 *              'id' => string,
28 *              'name' => string,
29 *              'mail' => string,
30 *              'address' => string,
31 *              'url' => string
32 *          ],
33 *          'date' => [
34 *              'created' => int timestamp,
35 *              'modified' => int (not defined if not modified)
36 *          ],
37 *          'show' => bool, whether shown (still be moderated, or hidden by moderator or user self)
38 *      ],
39 *      ...
40 *   ]
41 *   'subscribers' => [
42 *      '<mail>' => [
43 *          'hash' => string unique token,
44 *          'active' => bool, true if confirmed
45 *          'confirmsent' => bool, true if confirmation mail is sent
46 *      ],
47 *      ...
48 *   ]
49 */
50class action_plugin_discussion extends DokuWiki_Action_Plugin
51{
52
53    /** @var helper_plugin_avatar */
54    protected $avatar = null;
55    /** @var null|string */
56    protected $style = null;
57    /** @var null|bool */
58    protected $useAvatar = null;
59    /** @var helper_plugin_discussion */
60    protected $helper = null;
61
62    /**
63     * load helper
64     */
65    public function __construct()
66    {
67        $this->helper = plugin_load('helper', 'discussion');
68    }
69
70    /**
71     * Register the handlers
72     *
73     * @param Doku_Event_Handler $controller DokuWiki's event controller object.
74     */
75    public function register(Doku_Event_Handler $controller)
76    {
77        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentActions');
78        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'renderCommentsSection');
79        $controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'addCommentsToIndex', ['id' => 'page', 'text' => 'body']);
80        $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'addCommentsToIndex', ['id' => 'id', 'text' => 'text']);
81        $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'addIndexVersion', []);
82        $controller->register_hook('FULLTEXT_PHRASE_MATCH', 'AFTER', $this, 'fulltextPhraseMatchInComments', []);
83        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'update_comment_status', []);
84        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addToolbarToCommentfield', []);
85        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'modifyToolbar', []);
86        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxPreviewComments', []);
87        $controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'addDiscussionToTOC', []);
88    }
89
90    /**
91     * Preview Comments
92     *
93     * @param Doku_Event $event
94     * @param $params
95     * @author Michael Klier <chi@chimeric.de>
96     *
97     */
98    public function ajaxPreviewComments(Doku_Event $event)
99    {
100        global $INPUT;
101        if ($event->data != 'discussion_preview') return;
102
103        $event->preventDefault();
104        $event->stopPropagation();
105        print p_locale_xhtml('preview');
106        print '<div class="comment_preview">';
107        if (!$INPUT->server->str('REMOTE_USER') && !$this->getConf('allowguests')) {
108            print p_locale_xhtml('denied');
109        } else {
110            print $this->renderComment($INPUT->post->str('comment'));
111        }
112        print '</div>';
113    }
114
115    /**
116     * Adds a TOC item if a discussion exists
117     *
118     * @param Doku_Event $event
119     * @param $params
120     * @author Michael Klier <chi@chimeric.de>
121     *
122     */
123    public function addDiscussionToTOC(Doku_Event $event)
124    {
125        global $ACT;
126        if ($this->hasDiscussion($title) && $event->data && $ACT != 'admin') {
127            $tocitem = ['hid' => 'discussion__section',
128                'title' => $this->getLang('discussion'),
129                'type' => 'ul',
130                'level' => 1];
131
132            $event->data[] = $tocitem;
133        }
134    }
135
136    /**
137     * Modify Toolbar for use with discussion plugin
138     *
139     * @param Doku_Event $event
140     * @param $param
141     * @author Michael Klier <chi@chimeric.de>
142     *
143     */
144    public function modifyToolbar(Doku_Event $event)
145    {
146        global $ACT;
147        if ($ACT != 'show') return;
148
149        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
150            $toolbar = [];
151            foreach ($event->data as $btn) {
152                if ($btn['type'] == 'mediapopup') continue;
153                if ($btn['type'] == 'signature') continue;
154                if ($btn['type'] == 'linkwiz') continue;
155                if ($btn['type'] == 'NewTable') continue; //skip button for Edittable Plugin
156                if (isset($btn['open']) && preg_match("/=+?/", $btn['open'])) continue;
157
158                $toolbar[] = $btn;
159            }
160            $event->data = $toolbar;
161        }
162    }
163
164    /**
165     * Dirty workaround to add a toolbar to the discussion plugin
166     *
167     * @param Doku_Event $event
168     * @param $param
169     * @author Michael Klier <chi@chimeric.de>
170     *
171     */
172    public function addToolbarToCommentfield(Doku_Event $event)
173    {
174        global $ACT;
175        global $ID;
176        if ($ACT != 'show') return;
177
178        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
179            // FIXME ugly workaround, replace this once DW the toolbar code is more flexible
180            @require_once(DOKU_INC . 'inc/toolbar.php');
181            ob_start();
182            print 'NS = "' . getNS($ID) . '";'; // we have to define NS, otherwise we get get JS errors
183            toolbar_JSdefines('toolbar');
184            $script = ob_get_clean();
185            $event->data['script'][] = ['type' => 'text/javascript', 'charset' => "utf-8", '_data' => $script];
186        }
187    }
188
189    /**
190     * Handles comment actions, dispatches data processing routines
191     *
192     * @param Doku_Event $event
193     * @param $param
194     * @return void
195     */
196    public function handleCommentActions(Doku_Event $event)
197    {
198        global $ID, $INFO, $lang, $INPUT;
199
200        // handle newthread ACTs
201        if ($event->data == 'newthread') {
202            // we can handle it -> prevent others
203            $event->data = $this->newThread();
204        }
205
206        // enable captchas
207        if (in_array($INPUT->str('comment'), ['add', 'save'])) {
208            $this->captchaCheck();
209            $this->recaptchaCheck();
210        }
211
212        // if we are not in show mode or someone wants to unsubscribe, that was all for now
213        if ($event->data != 'show'
214            && $event->data != 'discussion_unsubscribe'
215            && $event->data != 'discussion_confirmsubscribe') {
216            return;
217        }
218
219        if ($event->data == 'discussion_unsubscribe' or $event->data == 'discussion_confirmsubscribe') {
220            if ($INPUT->has('hash')) {
221                $file = metaFN($ID, '.comments');
222                $data = unserialize(io_readFile($file));
223                $matchedMail = '';
224                foreach ($data['subscribers'] as $mail => $info) {
225                    // convert old style subscribers just in case
226                    if (!is_array($info)) {
227                        $hash = $data['subscribers'][$mail];
228                        $data['subscribers'][$mail]['hash'] = $hash;
229                        $data['subscribers'][$mail]['active'] = true;
230                        $data['subscribers'][$mail]['confirmsent'] = true;
231                    }
232
233                    if ($data['subscribers'][$mail]['hash'] == $INPUT->str('hash')) {
234                        $matchedMail = $mail;
235                    }
236                }
237
238                if ($matchedMail != '') {
239                    if ($event->data == 'discussion_unsubscribe') {
240                        unset($data['subscribers'][$matchedMail]);
241                        msg(sprintf($lang['subscr_unsubscribe_success'], $matchedMail, $ID), 1);
242                    } else { //$event->data == 'discussion_confirmsubscribe'
243                        $data['subscribers'][$matchedMail]['active'] = true;
244                        msg(sprintf($lang['subscr_subscribe_success'], $matchedMail, $ID), 1);
245                    }
246                    io_saveFile($file, serialize($data));
247                    $event->data = 'show';
248                }
249
250            }
251            return;
252        } else {
253            // do the data processing for comments
254            $cid = $INPUT->str('cid');
255            switch ($INPUT->str('comment')) {
256                case 'add':
257                    if (empty($INPUT->str('text'))) return; // don't add empty comments
258
259                    if ($INPUT->server->has('REMOTE_USER') && !$this->getConf('adminimport')) {
260                        $comment['user']['id'] = $INPUT->server->str('REMOTE_USER');
261                        $comment['user']['name'] = $INFO['userinfo']['name'];
262                        $comment['user']['mail'] = $INFO['userinfo']['mail'];
263                    } elseif (($INPUT->server->has('REMOTE_USER') && $this->getConf('adminimport') && $this->helper->isDiscussionMod())
264                        || !$INPUT->server->has('REMOTE_USER')) {
265                        // don't add anonymous comments
266                        if (empty($INPUT->str('name')) or empty($INPUT->str('mail'))) {
267                            return;
268                        }
269
270                        if (!mail_isvalid($INPUT->str('mail'))) {
271                            msg($lang['regbadmail'], -1);
272                            return;
273                        } else {
274                            $comment['user']['id'] = 'test' . hsc($INPUT->str('user'));
275                            $comment['user']['name'] = hsc($INPUT->str('name'));
276                            $comment['user']['mail'] = hsc($INPUT->str('mail'));
277                        }
278                    }
279                    $comment['user']['address'] = ($this->getConf('addressfield')) ? hsc($INPUT->str('address')) : '';
280                    $comment['user']['url'] = ($this->getConf('urlfield')) ? $this->checkURL($INPUT->str('url')) : '';
281                    $comment['subscribe'] = ($this->getConf('subscribe')) ? $INPUT->has('subscribe') : '';
282                    $comment['date'] = ['created' => $INPUT->str('date')];
283                    $comment['raw'] = cleanText($INPUT->str('text'));
284                    $reply = $INPUT->str('reply');
285                    if ($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
286                        $comment['show'] = false;
287                    } else {
288                        $comment['show'] = true;
289                    }
290                    $this->add($comment, $reply);
291                    break;
292
293                case 'save':
294                    $raw = cleanText($INPUT->str('text'));
295                    $this->save([$cid], $raw);
296                    break;
297
298                case 'delete':
299                    $this->save([$cid], '');
300                    break;
301
302                case 'toogle':
303                    $this->save([$cid], '', 'toogle');
304                    break;
305            }
306        }
307    }
308
309    /**
310     * Main function; dispatches the visual comment actions
311     */
312    public function renderCommentsSection(Doku_Event $event)
313    {
314        global $INPUT;
315        if ($event->data != 'show') return; // nothing to do for us
316
317        $cid = $INPUT->str('cid');
318
319        if (!$cid) {
320            $cid = $INPUT->str('reply');
321        }
322
323        switch ($INPUT->str('comment')) {
324            case 'edit':
325                $this->showDiscussionSection(null, $cid);
326                break;
327            default: //'reply' or no action specified
328                $this->showDiscussionSection($cid);
329                break;
330        }
331    }
332
333    /**
334     * Redirects browser to given comment anchor
335     */
336    protected function redirect($cid)
337    {
338        global $ID;
339        global $ACT;
340
341        if ($ACT !== 'show') return;
342
343        if ($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
344            msg($this->getLang('moderation'), 1);
345            @session_start();
346            global $MSG;
347            $_SESSION[DOKU_COOKIE]['msg'] = $MSG;
348            session_write_close();
349            $url = wl($ID);
350        } else {
351            $url = wl($ID) . '#comment_' . $cid;
352        }
353
354        if (function_exists('send_redirect')) {
355            send_redirect($url);
356        } else {
357            header('Location: ' . $url);
358        }
359        exit();
360    }
361
362    /**
363     * Checks config settings to enable/disable discussions
364     *
365     * @return bool
366     */
367    public function isDiscussionEnabled()
368    {
369        global $ID;
370
371        if ($this->getConf('excluded_ns') == '') {
372            $isNamespaceExcluded = false;
373        } else {
374            $ns = getNS($ID); // $INFO['namespace'] is not yet available, if used in update_comment_status()
375            $isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $ns);
376        }
377
378        if ($this->getConf('automatic')) {
379            if ($isNamespaceExcluded) {
380                return false;
381            } else {
382                return true;
383            }
384        } else {
385            if ($isNamespaceExcluded) {
386                return true;
387            } else {
388                return false;
389            }
390        }
391    }
392
393    /**
394     * Shows all comments of the current page, if no reply or edit requested, then comment form is shown on the end
395     *
396     * @param null|string $reply comment id on which the user requested a reply
397     * @param null|string $edit comment id which the user requested for editing
398     */
399    protected function showDiscussionSection($reply = null, $edit = null)
400    {
401        global $ID, $INFO, $INPUT;
402
403        // get .comments meta file name
404        $file = metaFN($ID, '.comments');
405
406        if (!$INFO['exists']) return;
407        if (!@file_exists($file) && !$this->isDiscussionEnabled()) return;
408        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('showguests')) return;
409
410        // load data
411        $data = [];
412        if (@file_exists($file)) {
413            $data = unserialize(io_readFile($file, false));
414            // comments are turned off
415            if (!$data['status']) {
416                return;
417            }
418        } elseif (!@file_exists($file) && $this->isDiscussionEnabled() && $INFO['exists']) {
419            // set status to show the comment form
420            $data['status'] = 1;
421            $data['number'] = 0;
422        }
423
424        // show discussion wrapper only on certain circumstances
425        if (empty($data['comments']) || !is_array($data['comments'])) {
426            $cnt = 0;
427            $keys = [];
428        } else {
429            $cnt = count($data['comments']);
430            $keys = array_keys($data['comments']);
431        }
432
433        $show = false;
434        if ($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1)
435            || $this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) {
436            $show = true;
437            // section title
438            $title = ($data['title'] ? hsc($data['title']) : $this->getLang('discussion'));
439            ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section
440            ptln('<h2><a name="discussion__section" id="discussion__section">', 2);
441            ptln($title, 4);
442            ptln('</a></h2>', 2);
443            ptln('<div class="level2 hfeed">', 2);
444        }
445
446        // now display the comments
447        if (isset($data['comments'])) {
448            if (!$this->getConf('usethreading')) {
449                $data['comments'] = $this->flattenThreads($data['comments']);
450                uasort($data['comments'], [$this, 'sortThreadsOnCreation']);
451            }
452            if ($this->getConf('newestfirst')) {
453                $data['comments'] = array_reverse($data['comments']);
454            }
455            foreach ($data['comments'] as $cid => $value) {
456                if ($cid == $edit) { // edit form
457                    $this->showCommentForm($value['raw'], 'save', $edit);
458                } else {
459                    $this->showCommentWithReplies($cid, $data, '', $reply);
460                }
461            }
462        }
463
464        // comment form shown on the end, if no comment form of $reply or $edit is requested before
465        if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) {
466            $this->showCommentForm('');
467        }
468
469        if ($show) {
470            ptln('</div>', 2); // level2 hfeed
471            ptln('</div>'); // comment_wrapper
472        }
473
474        // check for toggle print configuration
475        if ($this->getConf('visibilityButton')) {
476            // print the hide/show discussion section button
477            $this->showDiscussionToggleButton();
478        }
479    }
480
481    /**
482     * Remove the parent-child relation, such that the comment structure becomes flat
483     *
484     * @param array $comments array with all comments
485     * @param null|array $cids comment ids of replies, which should be flatten
486     * @return array returned array with flattened comment structure
487     */
488    protected function flattenThreads($comments, $cids = null)
489    {
490        if (is_null($cids)) {
491            $cids = array_keys($comments);
492        }
493
494        foreach ($cids as $cid) {
495            if (!empty($comments[$cid]['replies'])) {
496                $rids = $comments[$cid]['replies'];
497                $comments = $this->flattenThreads($comments, $rids);
498                $comments[$cid]['replies'] = [];
499            }
500            $comments[$cid]['parent'] = '';
501        }
502        return $comments;
503    }
504
505    /**
506     * Adds a new comment and then displays all comments
507     *
508     * @param array $comment with
509     *  'raw' => string comment text,
510     *  'user' => [
511     *      'id' => string,
512     *      'name' => string,
513     *      'mail' => string
514     *  ],
515     *  'date' => [
516     *      'created' => int timestamp
517     *  ]
518     *  'show' => bool
519     *  'subscribe' => bool
520     * @param string $parent comment id of parent
521     * @return bool
522     */
523    protected function add($comment, $parent)
524    {
525        global $ID, $TEXT, $INPUT;
526
527        $originalTxt = $TEXT; // set $TEXT to comment text for wordblock check
528        $TEXT = $comment['raw'];
529
530        // spamcheck against the DokuWiki blacklist
531        if (checkwordblock()) {
532            msg($this->getLang('wordblock'), -1);
533            return false;
534        }
535
536        if (!$this->getConf('allowguests')
537            && $comment['user']['id'] != $INPUT->server->str('REMOTE_USER')
538        ) {
539            return false; // guest comments not allowed
540        }
541
542        $TEXT = $originalTxt; // restore global $TEXT
543
544        // get discussion meta file name
545        $file = metaFN($ID, '.comments');
546
547        // create comments file if it doesn't exist yet
548        if (!@file_exists($file)) {
549            $data = ['status' => 1, 'number' => 0];
550            io_saveFile($file, serialize($data));
551        } else {
552            $data = unserialize(io_readFile($file, false));
553            // comments off or closed
554            if ($data['status'] != 1) {
555                return false;
556            }
557        }
558
559        if ($comment['date']['created']) {
560            $date = strtotime($comment['date']['created']);
561        } else {
562            $date = time();
563        }
564
565        if ($date == -1) {
566            $date = time();
567        }
568
569        $cid = md5($comment['user']['id'] . $date); // create a unique id
570
571        if (!is_array($data['comments'][$parent])) {
572            $parent = null; // invalid parent comment
573        }
574
575        // render the comment
576        $xhtml = $this->renderComment($comment['raw']);
577
578        // fill in the new comment
579        $data['comments'][$cid] = [
580            'user' => $comment['user'],
581            'date' => ['created' => $date],
582            'raw' => $comment['raw'],
583            'xhtml' => $xhtml,
584            'parent' => $parent,
585            'replies' => [],
586            'show' => $comment['show']
587        ];
588
589        if ($comment['subscribe']) {
590            $mail = $comment['user']['mail'];
591            if ($data['subscribers']) {
592                if (!$data['subscribers'][$mail]) {
593                    $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
594                    $data['subscribers'][$mail]['active'] = false;
595                    $data['subscribers'][$mail]['confirmsent'] = false;
596                } else {
597                    // convert old style subscribers and set them active
598                    if (!is_array($data['subscribers'][$mail])) {
599                        $hash = $data['subscribers'][$mail];
600                        $data['subscribers'][$mail]['hash'] = $hash;
601                        $data['subscribers'][$mail]['active'] = true;
602                        $data['subscribers'][$mail]['confirmsent'] = true;
603                    }
604                }
605            } else {
606                $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
607                $data['subscribers'][$mail]['active'] = false;
608                $data['subscribers'][$mail]['confirmsent'] = false;
609            }
610        }
611
612        // update parent comment
613        if ($parent) {
614            $data['comments'][$parent]['replies'][] = $cid;
615        }
616
617        // update the number of comments
618        $data['number']++;
619
620        // notify subscribers of the page
621        $data['comments'][$cid]['cid'] = $cid;
622        $this->notify($data['comments'][$cid], $data['subscribers']);
623
624        // save the comment metadata file
625        io_saveFile($file, serialize($data));
626        $this->addLogEntry($date, $ID, 'cc', '', $cid);
627
628        $this->redirect($cid);
629        return true;
630    }
631
632    /**
633     * Saves the comment with the given ID and then displays all comments
634     *
635     * @param array|string $cids array with comment ids to save, or a single string comment id
636     * @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!)
637     * @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw
638     * @return bool succeed?
639     */
640    public function save($cids, $raw, $act = null)
641    {
642        global $ID, $INPUT;
643
644        if (empty($cids)) return false; // do nothing if we get no comment id
645
646        if ($raw) {
647            global $TEXT;
648
649            $otxt = $TEXT; // set $TEXT to comment text for wordblock check
650            $TEXT = $raw;
651
652            // spamcheck against the DokuWiki blacklist
653            if (checkwordblock()) {
654                msg($this->getLang('wordblock'), -1);
655                return false;
656            }
657
658            $TEXT = $otxt; // restore global $TEXT
659        }
660
661        // get discussion meta file name
662        $file = metaFN($ID, '.comments');
663        $data = unserialize(io_readFile($file, false));
664
665        if (!is_array($cids)) {
666            $cids = [$cids];
667        }
668        foreach ($cids as $cid) {
669
670            if (is_array($data['comments'][$cid]['user'])) {
671                $user = $data['comments'][$cid]['user']['id'];
672                $convert = false;
673            } else {
674                $user = $data['comments'][$cid]['user'];
675                $convert = true;
676            }
677
678            // someone else was trying to edit our comment -> abort
679            if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionMod()) {
680                return false;
681            }
682
683            $date = time();
684
685            // need to convert to new format?
686            if ($convert) {
687                $data['comments'][$cid]['user'] = [
688                    'id' => $user,
689                    'name' => $data['comments'][$cid]['name'],
690                    'mail' => $data['comments'][$cid]['mail'],
691                    'url' => $data['comments'][$cid]['url'],
692                    'address' => $data['comments'][$cid]['address'],
693                ];
694                $data['comments'][$cid]['date'] = [
695                    'created' => $data['comments'][$cid]['date']
696                ];
697            }
698
699            if ($act == 'toogle') {     // toogle visibility
700                $now = $data['comments'][$cid]['show'];
701                $data['comments'][$cid]['show'] = !$now;
702                $data['number'] = $this->countVisibleComments($data);
703
704                $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
705
706            } elseif ($act == 'show') { // show comment
707                $data['comments'][$cid]['show'] = true;
708                $data['number'] = $this->countVisibleComments($data);
709
710                $type = 'sc'; // show comment
711
712            } elseif ($act == 'hide') { // hide comment
713                $data['comments'][$cid]['show'] = false;
714                $data['number'] = $this->countVisibleComments($data);
715
716                $type = 'hc'; // hide comment
717
718            } elseif (!$raw) {          // remove the comment
719                $data['comments'] = $this->removeComment($cid, $data['comments']);
720                $data['number'] = $this->countVisibleComments($data);
721
722                $type = 'dc'; // delete comment
723
724            } else {                   // save changed comment
725                $xhtml = $this->renderComment($raw);
726
727                // now change the comment's content
728                $data['comments'][$cid]['date']['modified'] = $date;
729                $data['comments'][$cid]['raw'] = $raw;
730                $data['comments'][$cid]['xhtml'] = $xhtml;
731
732                $type = 'ec'; // edit comment
733            }
734        }
735
736        // save the comment metadata file
737        io_saveFile($file, serialize($data));
738        $this->addLogEntry($date, $ID, $type, '', $cid);
739
740        $this->redirect($cid);
741        return true;
742    }
743
744    /**
745     * Recursive function to remove a comment from the data array
746     *
747     * @param string $cid comment id to be removed
748     * @param array $comments array with all comments
749     * @return array returns modified array with all remaining comments
750     */
751    protected function removeComment($cid, $comments)
752    {
753        if (is_array($comments[$cid]['replies'])) {
754            foreach ($comments[$cid]['replies'] as $rid) {
755                $comments = $this->removeComment($rid, $comments);
756            }
757        }
758        unset($comments[$cid]);
759        return $comments;
760    }
761
762    /**
763     * Prints an individual comment
764     *
765     * @param string $cid comment id
766     * @param array $data array with all comments by reference
767     * @param string $parent comment id of parent
768     * @param string $reply comment id on which the user requested a reply
769     * @param bool $isVisible is marked as visible
770     */
771    protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true)
772    {
773        // comment was removed
774        if (!isset($data['comments'][$cid])) {
775            return;
776        }
777        $comment = $data['comments'][$cid];
778
779        // corrupt datatype
780        if (!is_array($comment)) {
781            return;
782        }
783
784        // handle only replies to given parent comment
785        if ($comment['parent'] != $parent) {
786            return;
787        }
788
789        // comment hidden
790        if (!$comment['show']) {
791            if ($this->helper->isDiscussionMod()) {
792                $hidden = ' comment_hidden';
793            } else {
794                return;
795            }
796        } else {
797            $hidden = '';
798        }
799
800        // print the actual comment
801        $this->showComment($cid, $data, $parent, $reply, $isVisible, $hidden);
802        // replies to this comment entry?
803        $this->showReplies($cid, $data, $reply, $isVisible);
804        // reply form
805        $this->showReplyForm($cid, $reply);
806    }
807
808    /**
809     * Print the comment
810     *
811     * @param string $cid comment id
812     * @param array $data array with all comments by reference
813     * @param string $parent comment id of parent
814     * @param string $reply comment id on which the user requested a reply
815     * @param bool $isVisible is marked as visible
816     * @param string $hidden extra class, for the admin only hidden view
817     */
818    protected function showComment($cid, &$data, $parent, $reply, $isVisible, $hidden)
819    {
820        global $conf, $lang, $HIGH, $INPUT;
821        $comment = $data['comments'][$cid];
822
823        // comment head with date and user data
824        ptln('<div class="hentry' . $hidden . '">', 4);
825        ptln('<div class="comment_head">', 6);
826        ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8);
827        $head = '<span class="vcard author">';
828
829        // prepare variables
830        if (is_array($comment['user'])) { // new format
831            $user = $comment['user']['id'];
832            $name = $comment['user']['name'];
833            $mail = $comment['user']['mail'];
834            $url = $comment['user']['url'];
835            $address = $comment['user']['address'];
836        } else {                         // old format
837            $user = $comment['user'];
838            $name = $comment['name'];
839            $mail = $comment['mail'];
840            $url = $comment['url'];
841            $address = $comment['address'];
842        }
843        if (is_array($comment['date'])) { // new format
844            $created = $comment['date']['created'];
845            $modified = $comment['date']['modified'] ?? null;
846        } else {                         // old format
847            $created = $comment['date'];
848            $modified = $comment['edited'];
849        }
850
851        // show username or real name?
852        if (!$this->getConf('userealname') && $user) {
853            $showname = $user;
854        } else {
855            $showname = $name;
856        }
857
858        // show avatar image?
859        if ($this->useAvatar()) {
860            $user_data['name'] = $name;
861            $user_data['user'] = $user;
862            $user_data['mail'] = $mail;
863            $avatar = $this->avatar->getXHTML($user_data, $name, 'left');
864            if ($avatar) {
865                $head .= $avatar;
866            }
867        }
868
869        if ($this->getConf('linkemail') && $mail) {
870            $head .= $this->email($mail, $showname, 'email fn');
871        } elseif ($url) {
872            $head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn');
873        } else {
874            $head .= '<span class="fn">' . $showname . '</span>';
875        }
876
877        if ($address) {
878            $head .= ', <span class="adr">' . $address . '</span>';
879        }
880        $head .= '</span>, ' .
881            '<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' .
882            dformat($created, $conf['dformat']) . '</abbr>';
883        if ($modified) {
884            $head .= ', <abbr class="updated" title="' .
885                strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) .
886                '</abbr>';
887        }
888        ptln($head, 8);
889        ptln('</div>', 6); // class="comment_head"
890
891        // main comment content
892        ptln('<div class="comment_body entry-content"' .
893            ($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6);
894        echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF;
895        ptln('</div>', 6); // class="comment_body"
896
897        if ($isVisible) {
898            ptln('<div class="comment_buttons">', 6);
899
900            // show reply button?
901            if ($data['status'] == 1 && !$reply && $comment['show']
902                && ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER'))
903                && $this->getConf('usethreading')
904            ) {
905                $this->showButton($cid, $this->getLang('btn_reply'), 'reply', true);
906            }
907
908            // show edit, show/hide and delete button?
909            if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionMod()) {
910                $this->showButton($cid, $lang['btn_secedit'], 'edit', true);
911                $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
912                $this->showButton($cid, $label, 'toogle');
913                $this->showButton($cid, $lang['btn_delete'], 'delete');
914            }
915            ptln('</div>', 6); // class="comment_buttons"
916        }
917        ptln('</div>', 4); // class="hentry"
918    }
919
920    /**
921     * If requested by user, show comment form to write a reply
922     *
923     * @param string $cid current comment id
924     * @param string $reply comment id on which the user requested a reply
925     */
926    protected function showReplyForm($cid, $reply)
927    {
928        if ($this->getConf('usethreading') && $reply == $cid) {
929            ptln('<div class="comment_replies">', 4);
930            $this->showCommentForm('', 'add', $cid);
931            ptln('</div>', 4); // class="comment_replies"
932        }
933    }
934
935    /**
936     *
937     *
938     * @param string $cid comment id
939     * @param array $data array with all comments by reference
940     * @param string $reply
941     * @param bool $isVisible
942     */
943    protected function showReplies($cid, &$data, $reply, &$isVisible)
944    {
945        $comment = $data['comments'][$cid];
946        if (!count($comment['replies'])) {
947            return;
948        }
949        ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4);
950        $isVisible = ($comment['show'] && $isVisible);
951        foreach ($comment['replies'] as $rid) {
952            $this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible);
953        }
954        ptln('</div>', 4);
955    }
956
957    /**
958     * Is an avatar displayed?
959     *
960     * @return bool
961     */
962    protected function useAvatar()
963    {
964        if (is_null($this->useAvatar)) {
965            $this->useAvatar = $this->getConf('useavatar')
966                && ($this->avatar = $this->loadHelper('avatar', false));
967        }
968        return $this->useAvatar;
969    }
970
971    /**
972     * Calculate width of indent
973     *
974     * @return string
975     */
976    protected function getWidthStyle()
977    {
978        if (is_null($this->style)) {
979            if ($this->useAvatar()) {
980                $this->style = ' style="margin-left: ' . ($this->avatar->getConf('size') + 14) . 'px;"';
981            } else {
982                $this->style = ' style="margin-left: 20px;"';
983            }
984        }
985        return $this->style;
986    }
987
988    /**
989     * Show the button which toggles between show/hide of the entire discussion section
990     */
991    protected function showDiscussionToggleButton()
992    {
993        ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">');
994        ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button"'
995            . 'value="' . $this->getLang('toggle_display') . '">');
996        ptln('</div>');
997    }
998
999    /**
1000     * Outputs the comment form
1001     */
1002    protected function showCommentForm($raw = '', $act = 'add', $cid = null)
1003    {
1004        global $lang, $conf, $ID, $INPUT;
1005
1006        // not for unregistered users when guest comments aren't allowed
1007        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('allowguests')) {
1008            ?>
1009            <div class="comment_form">
1010                <?php echo $this->getLang('noguests'); ?>
1011            </div>
1012            <?php
1013            return;
1014        }
1015
1016        // fill $raw with $INPUT->str('text') if it's empty (for failed CAPTCHA check)
1017        if (!$raw && $INPUT->str('comment') == 'show') {
1018            $raw = $INPUT->str('text');
1019        }
1020        ?>
1021
1022        <div class="comment_form">
1023            <form id="discussion__comment_form" method="post" action="<?php echo script() ?>"
1024                  accept-charset="<?php echo $lang['encoding'] ?>">
1025                <div class="no">
1026                    <input type="hidden" name="id" value="<?php echo $ID ?>"/>
1027                    <input type="hidden" name="do" value="show"/>
1028                    <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1029                    <?php
1030                    // for adding a comment
1031                    if ($act == 'add') {
1032                        ?>
1033                        <input type="hidden" name="reply" value="<?php echo $cid ?>"/>
1034                        <?php
1035                        // for guest/adminimport: show name, e-mail and subscribe to comments fields
1036                        if (!$INPUT->server->has('REMOTE_USER') or ($this->getConf('adminimport') && $this->helper->isDiscussionMod())) {
1037                            ?>
1038                            <input type="hidden" name="user" value="<?php echo clientIP() ?>"/>
1039                            <div class="comment_name">
1040                                <label class="block" for="discussion__comment_name">
1041                                    <span><?php echo $lang['fullname'] ?>:</span>
1042                                    <input type="text"
1043                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('name'))) echo ' error' ?>"
1044                                           name="name" id="discussion__comment_name" size="50" tabindex="1"
1045                                           value="<?php echo hsc($INPUT->str('name')) ?>"/>
1046                                </label>
1047                            </div>
1048                            <div class="comment_mail">
1049                                <label class="block" for="discussion__comment_mail">
1050                                    <span><?php echo $lang['email'] ?>:</span>
1051                                    <input type="text"
1052                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('mail'))) echo ' error' ?>"
1053                                           name="mail" id="discussion__comment_mail" size="50" tabindex="2"
1054                                           value="<?php echo hsc($INPUT->str('mail')) ?>"/>
1055                                </label>
1056                            </div>
1057                            <?php
1058                        }
1059
1060                        // allow entering an URL
1061                        if ($this->getConf('urlfield')) {
1062                            ?>
1063                            <div class="comment_url">
1064                                <label class="block" for="discussion__comment_url">
1065                                    <span><?php echo $this->getLang('url') ?>:</span>
1066                                    <input type="text" class="edit" name="url" id="discussion__comment_url" size="50"
1067                                           tabindex="3" value="<?php echo hsc($INPUT->str('url')) ?>"/>
1068                                </label>
1069                            </div>
1070                            <?php
1071                        }
1072
1073                        // allow entering an address
1074                        if ($this->getConf('addressfield')) {
1075                            ?>
1076                            <div class="comment_address">
1077                                <label class="block" for="discussion__comment_address">
1078                                    <span><?php echo $this->getLang('address') ?>:</span>
1079                                    <input type="text" class="edit" name="address" id="discussion__comment_address"
1080                                           size="50" tabindex="4" value="<?php echo hsc($INPUT->str('address')) ?>"/>
1081                                </label>
1082                            </div>
1083                            <?php
1084                        }
1085
1086                        // allow setting the comment date
1087                        if ($this->getConf('adminimport') && ($this->helper->isDiscussionMod())) {
1088                            ?>
1089                            <div class="comment_date">
1090                                <label class="block" for="discussion__comment_date">
1091                                    <span><?php echo $this->getLang('date') ?>:</span>
1092                                    <input type="text" class="edit" name="date" id="discussion__comment_date"
1093                                           size="50"/>
1094                                </label>
1095                            </div>
1096                            <?php
1097                        }
1098
1099                        // for saving a comment
1100                    } else {
1101                        ?>
1102                        <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1103                        <?php
1104                    }
1105                    ?>
1106                    <div class="comment_text">
1107                        <?php echo $this->getLang('entercomment');
1108                        echo($this->getConf('wikisyntaxok') ? "" : ":");
1109                        if ($this->getConf('wikisyntaxok')) echo '. ' . $this->getLang('wikisyntax') . ':'; ?>
1110
1111                        <!-- Fix for disable the toolbar when wikisyntaxok is set to false. See discussion's script.jss -->
1112                        <?php if ($this->getConf('wikisyntaxok')) { ?>
1113                        <div id="discussion__comment_toolbar" class="toolbar group">
1114                            <?php } else { ?>
1115                            <div id="discussion__comment_toolbar_disabled">
1116                                <?php } ?>
1117                            </div>
1118                            <textarea
1119                                class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('text'))) echo ' error' ?>"
1120                                name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php
1121                                if ($raw) {
1122                                    echo formText($raw);
1123                                } else {
1124                                    echo hsc($INPUT->str('text'));
1125                                }
1126                                ?></textarea>
1127                        </div>
1128
1129                        <?php
1130                        /** @var helper_plugin_captcha $captcha */
1131                        $captcha = $this->loadHelper('captcha', false);
1132                        if ($captcha && $captcha->isEnabled()) {
1133                            echo $captcha->getHTML();
1134                        }
1135
1136                        /** @var helper_plugin_recaptcha $recaptcha */
1137                        $recaptcha = $this->loadHelper('recaptcha', false);
1138                        if ($recaptcha && $recaptcha->isEnabled()) {
1139                            echo $recaptcha->getHTML();
1140                        }
1141                        ?>
1142
1143                        <input class="button comment_submit" id="discussion__btn_submit" type="submit" name="submit"
1144                               accesskey="s" value="<?php echo $lang['btn_save'] ?>"
1145                               title="<?php echo $lang['btn_save'] ?> [S]" tabindex="7"/>
1146                        <input class="button comment_preview_button" id="discussion__btn_preview" type="button"
1147                               name="preview" accesskey="p" value="<?php echo $lang['btn_preview'] ?>"
1148                               title="<?php echo $lang['btn_preview'] ?> [P]"/>
1149
1150                        <?php if ((!$INPUT->server->has('REMOTE_USER')
1151                                || $INPUT->server->has('REMOTE_USER') && !$conf['subscribers'])
1152                                && $this->getConf('subscribe')) { ?>
1153                            <div class="comment_subscribe">
1154                                <input type="checkbox" id="discussion__comment_subscribe" name="subscribe"
1155                                       tabindex="6"/>
1156                                <label class="block" for="discussion__comment_subscribe">
1157                                    <span><?php echo $this->getLang('subscribe') ?></span>
1158                                </label>
1159                            </div>
1160                        <?php } ?>
1161
1162                        <div class="clearer"></div>
1163                        <div id="discussion__comment_preview">&nbsp;</div>
1164                    </div>
1165            </form>
1166        </div>
1167        <?php
1168    }
1169
1170    /**
1171     * Action button below a comment
1172     *
1173     * @param string $cid comment id
1174     * @param string $label translated label
1175     * @param string $act action
1176     * @param bool $jump whether to scroll to the commentform
1177     */
1178    protected function showButton($cid, $label, $act, $jump = false)
1179    {
1180        global $ID;
1181
1182        $anchor = ($jump ? '#discussion__comment_form' : '');
1183
1184        ?>
1185        <form class="button discussion__<?php echo $act ?>" method="get" action="<?php echo script() . $anchor ?>">
1186            <div class="no">
1187                <input type="hidden" name="id" value="<?php echo $ID ?>"/>
1188                <input type="hidden" name="do" value="show"/>
1189                <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1190                <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1191                <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>"/>
1192            </div>
1193        </form>
1194        <?php
1195    }
1196
1197    /**
1198     * Adds an entry to the comments changelog
1199     *
1200     * @param int $date
1201     * @param string $id page id
1202     * @param string $type create/edit/delete/show/hide comment 'cc', 'ec', 'dc', 'sc', 'hc'
1203     * @param string $summary
1204     * @param string $extra
1205     * @author Ben Coburn <btcoburn@silicodon.net>
1206     *
1207     * @author Esther Brunner <wikidesign@gmail.com>
1208     */
1209    protected function addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '')
1210    {
1211        global $conf, $INPUT;
1212
1213        $changelog = $conf['metadir'] . '/_comments.changes';
1214
1215        //use current time if none supplied
1216        if (!$date) {
1217            $date = time();
1218        }
1219        $remote = $INPUT->server->str('REMOTE_ADDR');
1220        $user = $INPUT->server->str('REMOTE_USER');
1221
1222        $strip = ["\t", "\n"];
1223        $logline = [
1224            'date' => $date,
1225            'ip' => $remote,
1226            'type' => str_replace($strip, '', $type),
1227            'id' => $id,
1228            'user' => $user,
1229            'sum' => str_replace($strip, '', $summary),
1230            'extra' => str_replace($strip, '', $extra)
1231        ];
1232
1233        // add changelog line
1234        $logline = implode("\t", $logline) . "\n";
1235        io_saveFile($changelog, $logline, true); //global changelog cache
1236        $this->trimRecentCommentsLog($changelog);
1237
1238        // tell the indexer to re-index the page
1239        @unlink(metaFN($id, '.indexed'));
1240    }
1241
1242    /**
1243     * Trims the recent comments cache to the last $conf['changes_days'] recent
1244     * changes or $conf['recent'] items, which ever is larger.
1245     * The trimming is only done once a day.
1246     *
1247     * @param string $changelog file path
1248     * @return bool
1249     * @author Ben Coburn <btcoburn@silicodon.net>
1250     *
1251     */
1252    protected function trimRecentCommentsLog($changelog)
1253    {
1254        global $conf;
1255
1256        if (@file_exists($changelog)
1257            && (filectime($changelog) + 86400) < time()
1258            && !@file_exists($changelog . '_tmp')
1259        ) {
1260
1261            io_lock($changelog);
1262            $lines = file($changelog);
1263            if (count($lines) < $conf['recent']) {
1264                // nothing to trim
1265                io_unlock($changelog);
1266                return true;
1267            }
1268
1269            // presave tmp as 2nd lock
1270            io_saveFile($changelog . '_tmp', '');
1271            $trim_time = time() - $conf['recent_days'] * 86400;
1272            $out_lines = [];
1273
1274            $num = count($lines);
1275            for ($i = 0; $i < $num; $i++) {
1276                $log = parseChangelogLine($lines[$i]);
1277                if ($log === false) continue;                      // discard junk
1278                if ($log['date'] < $trim_time) {
1279                    $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions)
1280                } else {
1281                    $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines
1282                }
1283            }
1284
1285            // sort the final result, it shouldn't be necessary,
1286            // however the extra robustness in making the changelog cache self-correcting is worth it
1287            ksort($out_lines);
1288            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
1289            if ($extra > 0) {
1290                ksort($old_lines);
1291                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
1292            }
1293
1294            // save trimmed changelog
1295            io_saveFile($changelog . '_tmp', implode('', $out_lines));
1296            @unlink($changelog);
1297            if (!rename($changelog . '_tmp', $changelog)) {
1298                // rename failed so try another way...
1299                io_unlock($changelog);
1300                io_saveFile($changelog, implode('', $out_lines));
1301                @unlink($changelog . '_tmp');
1302            } else {
1303                io_unlock($changelog);
1304            }
1305            return true;
1306        }
1307        return true;
1308    }
1309
1310    /**
1311     * Sends a notify mail on new comment
1312     *
1313     * @param array $comment data array of the new comment
1314     * @param array $subscribers data of the subscribers by reference
1315     *
1316     * @author Andreas Gohr <andi@splitbrain.org>
1317     * @author Esther Brunner <wikidesign@gmail.com>
1318     */
1319    protected function notify($comment, &$subscribers)
1320    {
1321        global $conf, $ID, $INPUT, $auth;
1322
1323        $notify_text = io_readfile($this->localfn('subscribermail'));
1324        $confirm_text = io_readfile($this->localfn('confirmsubscribe'));
1325        $subject_notify = '[' . $conf['title'] . '] ' . $this->getLang('mail_newcomment');
1326        $subject_subscribe = '[' . $conf['title'] . '] ' . $this->getLang('subscribe');
1327
1328        $mailer = new Mailer();
1329        if (!$INPUT->server->has('REMOTE_USER')) {
1330            $mailer->from($conf['mailfromnobody']);
1331        }
1332
1333        $replace = [
1334            'PAGE' => $ID,
1335            'TITLE' => $conf['title'],
1336            'DATE' => dformat($comment['date']['created'], $conf['dformat']),
1337            'NAME' => $comment['user']['name'],
1338            'TEXT' => $comment['raw'],
1339            'COMMENTURL' => wl($ID, '', true) . '#comment_' . $comment['cid'],
1340            'UNSUBSCRIBE' => wl($ID, 'do=subscribe', true, '&'),
1341            'DOKUWIKIURL' => DOKU_URL
1342        ];
1343
1344        $confirm_replace = [
1345            'PAGE' => $ID,
1346            'TITLE' => $conf['title'],
1347            'DOKUWIKIURL' => DOKU_URL
1348        ];
1349
1350
1351        $mailer->subject($subject_notify);
1352        $mailer->setBody($notify_text, $replace);
1353
1354        // send mail to notify address
1355        if ($conf['notify']) {
1356            $mailer->bcc($conf['notify']);
1357            $mailer->send();
1358        }
1359
1360        // send email to moderators
1361        if ($this->getConf('moderatorsnotify')) {
1362            $moderatorgrpsString = trim($this->getConf('moderatorgroups'));
1363            if (!empty($moderatorgrpsString)) {
1364                // create a clean mods list
1365                $moderatorgroups = explode(',', $moderatorgrpsString);
1366                $moderatorgroups = array_map('trim', $moderatorgroups);
1367                $moderatorgroups = array_unique($moderatorgroups);
1368                $moderatorgroups = array_filter($moderatorgroups);
1369                // search for moderators users
1370                foreach ($moderatorgroups as $moderatorgroup) {
1371                    if (!$auth->isCaseSensitive()) {
1372                        $moderatorgroup = PhpString::strtolower($moderatorgroup);
1373                    }
1374                    // create a clean mailing list
1375                    $bccs = [];
1376                    if ($moderatorgroup[0] == '@') {
1377                        foreach ($auth->retrieveUsers(0, 0, ['grps' => $auth->cleanGroup(substr($moderatorgroup, 1))]) as $user) {
1378                            if (!empty($user['mail'])) {
1379                                $bccs[] = $user['mail'];
1380                            }
1381                        }
1382                    } else {
1383                        //it is an user
1384                        $userdata = $auth->getUserData($auth->cleanUser($moderatorgroup));
1385                        if (!empty($userdata['mail'])) {
1386                            $bccs[] = $userdata['mail'];
1387                        }
1388                    }
1389                    $bccs = array_unique($bccs);
1390                    // notify the users
1391                    $mailer->bcc(implode(',', $bccs));
1392                    $mailer->send();
1393                }
1394            }
1395        }
1396
1397        // notify page subscribers
1398        if (actionOK('subscribe')) {
1399            $data = ['id' => $ID, 'addresslist' => '', 'self' => false];
1400            //FIXME default callback, needed to mentioned it again?
1401            Event::createAndTrigger(
1402                'COMMON_NOTIFY_ADDRESSLIST', $data,
1403                [new SubscriberManager(), 'notifyAddresses']
1404            );
1405
1406            $to = $data['addresslist'];
1407            if (!empty($to)) {
1408                $mailer->bcc($to);
1409                $mailer->send();
1410            }
1411        }
1412
1413        // notify comment subscribers
1414        if (!empty($subscribers)) {
1415
1416            foreach ($subscribers as $mail => $data) {
1417                $mailer->bcc($mail);
1418                if ($data['active']) {
1419                    $replace['UNSUBSCRIBE'] = wl($ID, 'do=discussion_unsubscribe&hash=' . $data['hash'], true, '&');
1420
1421                    $mailer->subject($subject_notify);
1422                    $mailer->setBody($notify_text, $replace);
1423                    $mailer->send();
1424                } elseif (!$data['confirmsent']) {
1425                    $confirm_replace['SUBSCRIBE'] = wl($ID, 'do=discussion_confirmsubscribe&hash=' . $data['hash'], true, '&');
1426
1427                    $mailer->subject($subject_subscribe);
1428                    $mailer->setBody($confirm_text, $confirm_replace);
1429                    $mailer->send();
1430                    $subscribers[$mail]['confirmsent'] = true;
1431                }
1432            }
1433        }
1434    }
1435
1436    /**
1437     * Counts the number of visible comments
1438     *
1439     * @param array $data array with all comments
1440     * @return int
1441     */
1442    protected function countVisibleComments($data)
1443    {
1444        $number = 0;
1445        foreach ($data['comments'] as $comment) {
1446            if ($comment['parent']) continue;
1447            if (!$comment['show']) continue;
1448
1449            $number++;
1450            $rids = $comment['replies'];
1451            if (count($rids)) {
1452                $number = $number + $this->countVisibleReplies($data, $rids);
1453            }
1454        }
1455        return $number;
1456    }
1457
1458    /**
1459     * Count visible replies on the comments
1460     *
1461     * @param array $data
1462     * @param array $rids
1463     * @return int counted replies
1464     */
1465    protected function countVisibleReplies(&$data, $rids)
1466    {
1467        $number = 0;
1468        foreach ($rids as $rid) {
1469            if (!isset($data['comments'][$rid])) continue; // reply was removed
1470            if (!$data['comments'][$rid]['show']) continue;
1471
1472            $number++;
1473            $rids = $data['comments'][$rid]['replies'];
1474            if (count($rids)) {
1475                $number = $number + $this->countVisibleReplies($data, $rids);
1476            }
1477        }
1478        return $number;
1479    }
1480
1481    /**
1482     * Renders the raw comment (wiki)text to html
1483     *
1484     * @param string $raw comment text
1485     * @return null|string
1486     */
1487    protected function renderComment($raw)
1488    {
1489        if ($this->getConf('wikisyntaxok')) {
1490            // Note the warning for render_text:
1491            //   "very ineffecient for small pieces of data - try not to use"
1492            // in dokuwiki/inc/plugin.php
1493            $xhtml = $this->render_text($raw);
1494        } else { // wiki syntax not allowed -> just encode special chars
1495            $xhtml = hsc(trim($raw));
1496            $xhtml = str_replace("\n", '<br />', $xhtml);
1497        }
1498        return $xhtml;
1499    }
1500
1501    /**
1502     * Finds out whether there is a discussion section for the current page
1503     *
1504     * @param string $title
1505     * @return bool
1506     */
1507    protected function hasDiscussion(&$title)
1508    {
1509        global $ID;
1510
1511        $file = metaFN($ID, '.comments');
1512
1513        if (!@file_exists($file)) {
1514            if ($this->isDiscussionEnabled()) {
1515                return true;
1516            } else {
1517                return false;
1518            }
1519        }
1520
1521        $comments = unserialize(io_readFile($file, false));
1522
1523        if ($comments['title']) {
1524            $title = hsc($comments['title']);
1525        }
1526        $num = $comments['number'];
1527        if (!$comments['status'] || ($comments['status'] == 2 && $num == 0)) {
1528            //disabled, or closed and no comments
1529            return false;
1530        } else {
1531            return true;
1532        }
1533    }
1534
1535    /**
1536     * Creates a new thread page
1537     *
1538     * @return string
1539     */
1540    protected function newThread()
1541    {
1542        global $ID, $INFO, $INPUT;
1543
1544        $ns = cleanID($INPUT->str('ns'));
1545        $title = str_replace(':', '', $INPUT->str('title'));
1546        $back = $ID;
1547        $ID = ($ns ? $ns . ':' : '') . cleanID($title);
1548        $INFO = pageinfo();
1549
1550        // check if we are allowed to create this file
1551        if ($INFO['perm'] >= AUTH_CREATE) {
1552
1553            //check if locked by anyone - if not lock for my self
1554            if ($INFO['locked']) {
1555                return 'locked';
1556            } else {
1557                lock($ID);
1558            }
1559
1560            // prepare the new thread file with default stuff
1561            if (!@file_exists($INFO['filepath'])) {
1562                global $TEXT;
1563
1564                $TEXT = pageTemplate(($ns ? $ns . ':' : '') . $title);
1565                if (!$TEXT) {
1566                    $data = ['id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back];
1567                    $TEXT = $this->pageTemplate($data);
1568                }
1569                return 'preview';
1570            } else {
1571                return 'edit';
1572            }
1573        } else {
1574            return 'show';
1575        }
1576    }
1577
1578    /**
1579     * Adapted version of pageTemplate() function
1580     *
1581     * @param array $data
1582     * @return string
1583     */
1584    protected function pageTemplate($data)
1585    {
1586        global $conf, $INFO, $INPUT;
1587
1588        $id = $data['id'];
1589        $user = $INPUT->server->str('REMOTE_USER');
1590        $tpl = io_readFile(DOKU_PLUGIN . 'discussion/_template.txt');
1591
1592        // standard replacements
1593        $replace = [
1594            '@NS@' => $data['ns'],
1595            '@PAGE@' => strtr(noNS($id), '_', ' '),
1596            '@USER@' => $user,
1597            '@NAME@' => $INFO['userinfo']['name'],
1598            '@MAIL@' => $INFO['userinfo']['mail'],
1599            '@DATE@' => dformat(time(), $conf['dformat']),
1600        ];
1601
1602        // additional replacements
1603        $replace['@BACK@'] = $data['back'];
1604        $replace['@TITLE@'] = $data['title'];
1605
1606        // avatar if useavatar and avatar plugin available
1607        if ($this->getConf('useavatar') && !plugin_isdisabled('avatar')) {
1608            $replace['@AVATAR@'] = '{{avatar>' . $user . ' }} ';
1609        } else {
1610            $replace['@AVATAR@'] = '';
1611        }
1612
1613        // tag if tag plugin is available
1614        if (!plugin_isdisabled('tag')) {
1615            $replace['@TAG@'] = "\n\n{{tag>}}";
1616        } else {
1617            $replace['@TAG@'] = '';
1618        }
1619
1620        // perform the replacements in tpl
1621        return str_replace(array_keys($replace), array_values($replace), $tpl);
1622    }
1623
1624    /**
1625     * Checks if the CAPTCHA string submitted is valid
1626     */
1627    protected function captchaCheck()
1628    {
1629        global $INPUT;
1630        /** @var helper_plugin_captcha $captcha */
1631        if (!$captcha = $this->loadHelper('captcha', false)) {
1632            // CAPTCHA is disabled or not available
1633            return;
1634        }
1635
1636        if ($captcha->isEnabled() && !$captcha->check()) {
1637            if ($INPUT->str('comment') == 'save') {
1638                $INPUT->set('comment', 'edit');
1639            } elseif ($INPUT->str('comment') == 'add') {
1640                $INPUT->set('comment', 'show');
1641            }
1642        }
1643    }
1644
1645    /**
1646     * checks if the submitted reCAPTCHA string is valid
1647     *
1648     * @author Adrian Schlegel <adrian@liip.ch>
1649     */
1650    protected function recaptchaCheck()
1651    {
1652        global $INPUT;
1653        /** @var helper_plugin_recaptcha $recaptcha */
1654        if (!$recaptcha = plugin_load('helper', 'recaptcha'))
1655            return; // reCAPTCHA is disabled or not available
1656
1657        // do nothing if logged in user and no reCAPTCHA required
1658        if (!$recaptcha->getConf('forusers') && $INPUT->server->has('REMOTE_USER')) return;
1659
1660        $response = $recaptcha->check();
1661        if (!$response->is_valid) {
1662            msg($recaptcha->getLang('testfailed'), -1);
1663            if ($INPUT->str('comment') == 'save') {
1664                $INPUT->str('comment', 'edit');
1665            } elseif ($INPUT->str('comment') == 'add') {
1666                $INPUT->str('comment', 'show');
1667            }
1668        }
1669    }
1670
1671    /**
1672     * Add discussion plugin version to the indexer version
1673     * This means that all pages will be indexed again in order to add the comments
1674     * to the index whenever there has been a change that concerns the index content.
1675     *
1676     * @param Doku_Event $event
1677     * @param $param
1678     */
1679    public function addIndexVersion(Doku_Event $event)
1680    {
1681        $event->data['discussion'] = '0.1';
1682    }
1683
1684    /**
1685     * Adds the comments to the index
1686     *
1687     * @param Doku_Event $event
1688     * @param array $param with
1689     *  'id' => string 'page'/'id' for respectively INDEXER_PAGE_ADD and FULLTEXT_SNIPPET_CREATE event
1690     *  'text' => string 'body'/'text'
1691     */
1692    public function addCommentsToIndex(Doku_Event $event, $param)
1693    {
1694        // get .comments meta file name
1695        $file = metaFN($event->data[$param['id']], '.comments');
1696
1697        if (!@file_exists($file)) return;
1698        $data = unserialize(io_readFile($file, false));
1699
1700        // comments are turned off or no comments available to index
1701        if (!$data['status'] || $data['number'] == 0) return;
1702
1703        // now add the comments
1704        if (isset($data['comments'])) {
1705            foreach ($data['comments'] as $key => $value) {
1706                $event->data[$param['text']] .= DOKU_LF . $this->addCommentWords($key, $data);
1707            }
1708        }
1709    }
1710
1711    /**
1712     * Checks if the phrase occurs in the comments and return event result true if matching
1713     *
1714     * @param Doku_Event $event
1715     * @param $param
1716     * @return void
1717     */
1718    public function fulltextPhraseMatchInComments(Doku_Event $event)
1719    {
1720        if ($event->result === true) return;
1721
1722        // get .comments meta file name
1723        $file = metaFN($event->data['id'], '.comments');
1724
1725        if (!@file_exists($file)) return;
1726        $data = unserialize(io_readFile($file, false));
1727
1728        // comments are turned off or no comments available to match
1729        if (!$data['status'] || $data['number'] == 0) return;
1730
1731        $matched = false;
1732
1733        // now add the comments
1734        if (isset($data['comments'])) {
1735            foreach ($data['comments'] as $cid => $value) {
1736                $matched = $this->phraseMatchInComment($event->data['phrase'], $cid, $data);
1737                if ($matched) break;
1738            }
1739        }
1740
1741        if ($matched) {
1742            $event->result = true;
1743        }
1744    }
1745
1746    /**
1747     * Match the phrase in the comment and its replies
1748     *
1749     * @param string $phrase phrase to search
1750     * @param string $cid comment id
1751     * @param array $data array with all comments by reference
1752     * @param string $parent cid of parent
1753     * @return bool if match true, otherwise false
1754     */
1755    protected function phraseMatchInComment($phrase, $cid, &$data, $parent = '')
1756    {
1757        if (!isset($data['comments'][$cid])) return false; // comment was removed
1758
1759        $comment = $data['comments'][$cid];
1760
1761        if (!is_array($comment)) return false;             // corrupt datatype
1762        if ($comment['parent'] != $parent) return false;   // reply to an other comment
1763        if (!$comment['show']) return false;               // hidden comment
1764
1765        $text = PhpString::strtolower($comment['raw']);
1766        if (strpos($text, $phrase) !== false) {
1767            return true;
1768        }
1769
1770        if (is_array($comment['replies'])) {               // and the replies
1771            foreach ($comment['replies'] as $rid) {
1772                if ($this->phraseMatchInComment($phrase, $rid, $data, $cid)) {
1773                    return true;
1774                }
1775            }
1776        }
1777        return false;
1778    }
1779
1780    /**
1781     * Saves the current comment status and title from metadata into the .comments file
1782     *
1783     * @param Doku_Event $event
1784     * @param $param
1785     */
1786    public function update_comment_status(Doku_Event $event)
1787    {
1788        global $ID;
1789
1790        $meta = $event->data['current'];
1791        $file = metaFN($ID, '.comments');
1792        $status = ($this->isDiscussionEnabled() ? 1 : 0);
1793        $title = null;
1794        if (isset($meta['plugin_discussion'])) {
1795            $status = $meta['plugin_discussion']['status']; // 0, 1 or 2
1796            $title = $meta['plugin_discussion']['title'];
1797        } elseif ($status == 1) {
1798            // Don't enable comments when automatic comments are on - this already happens automatically
1799            // and if comments are turned off in the admin this only updates the .comments file
1800            return;
1801        }
1802
1803        if ($status || @file_exists($file)) {
1804            $data = [];
1805            if (@file_exists($file)) {
1806                $data = unserialize(io_readFile($file, false));
1807            }
1808
1809            if (!array_key_exists('title', $data) || $data['title'] !== $title || !isset($data['status']) || $data['status'] !== $status) {
1810                $data['title'] = $title;
1811                $data['status'] = $status;
1812                if (!isset($data['number'])) {
1813                    $data['number'] = 0;
1814                }
1815                io_saveFile($file, serialize($data));
1816            }
1817        }
1818    }
1819
1820    /**
1821     * Return words of a given comment and its replies, suitable to be added to the index
1822     *
1823     * @param string $cid comment id
1824     * @param array $data array with all comments by reference
1825     * @param string $parent cid of parent
1826     * @return string
1827     */
1828    protected function addCommentWords($cid, &$data, $parent = '')
1829    {
1830
1831        if (!isset($data['comments'][$cid])) return ''; // comment was removed
1832
1833        $comment = $data['comments'][$cid];
1834
1835        if (!is_array($comment)) return '';             // corrupt datatype
1836        if ($comment['parent'] != $parent) return '';   // reply to an other comment
1837        if (!$comment['show']) return '';               // hidden comment
1838
1839        $text = $comment['raw'];                        // we only add the raw comment text
1840        if (is_array($comment['replies'])) {            // and the replies
1841            foreach ($comment['replies'] as $rid) {
1842                $text .= $this->addCommentWords($rid, $data, $cid);
1843            }
1844        }
1845        return ' ' . $text;
1846    }
1847
1848    /**
1849     * Only allow http(s) URLs and append http:// to URLs if needed
1850     *
1851     * @param string $url
1852     * @return string
1853     */
1854    protected function checkURL($url)
1855    {
1856        if (preg_match("#^http://|^https://#", $url)) {
1857            return hsc($url);
1858        } elseif (substr($url, 0, 4) == 'www.') {
1859            return hsc('https://' . $url);
1860        } else {
1861            return '';
1862        }
1863    }
1864
1865    /**
1866     * Sort threads
1867     *
1868     * @param array $a array with comment properties
1869     * @param array $b array with comment properties
1870     * @return int
1871     */
1872    function sortThreadsOnCreation($a, $b)
1873    {
1874        if (is_array($a['date'])) {
1875            // new format
1876            $createdA = $a['date']['created'];
1877        } else {
1878            // old format
1879            $createdA = $a['date'];
1880        }
1881
1882        if (is_array($b['date'])) {
1883            // new format
1884            $createdB = $b['date']['created'];
1885        } else {
1886            // old format
1887            $createdB = $b['date'];
1888        }
1889
1890        if ($createdA == $createdB) {
1891            return 0;
1892        } else {
1893            return ($createdA < $createdB) ? -1 : 1;
1894        }
1895    }
1896
1897}
1898
1899
1900