xref: /plugin/discussion/action.php (revision 1ce4168f06ec5ab21bc2e46cc36da31e129885ee)
1f0fda08aSwikidesign<?php
2f0fda08aSwikidesign/**
3f0fda08aSwikidesign * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4f0fda08aSwikidesign * @author     Esther Brunner <wikidesign@gmail.com>
5f0fda08aSwikidesign */
6f0fda08aSwikidesign
7c3413364SGerrit Uitslaguse dokuwiki\Extension\Event;
8c3413364SGerrit Uitslaguse dokuwiki\Subscriptions\SubscriberManager;
9c3413364SGerrit Uitslaguse dokuwiki\Utf8\PhpString;
10c3413364SGerrit Uitslag
11de7e6f00SGerrit Uitslag/**
12de7e6f00SGerrit Uitslag * Class action_plugin_discussion
13c3413364SGerrit Uitslag *
14c3413364SGerrit Uitslag * Data format of file metadir/<id>.comments:
15c3413364SGerrit Uitslag * array = [
16c3413364SGerrit Uitslag *  'status' => int whether comments are 0=disabled/1=open/2=closed,
17c3413364SGerrit Uitslag *  'number' => int number of visible comments,
1876fdd2cdSGerrit Uitslag *  'title' => string|null alternative title for discussion section
19c3413364SGerrit Uitslag *  'comments' => [
20c3413364SGerrit Uitslag *      '<cid>'=> [
21c3413364SGerrit Uitslag *          'cid' => string comment id - long random string
22c3413364SGerrit Uitslag *          'raw' => string comment text,
23c3413364SGerrit Uitslag *          'xhtml' => string rendered html,
24c3413364SGerrit Uitslag *          'parent' => null|string null or empty string at highest level, otherwise comment id of parent
25c3413364SGerrit Uitslag *          'replies' => string[] array with comment ids
26c3413364SGerrit Uitslag *          'user' => [
27c3413364SGerrit Uitslag *              'id' => string,
28c3413364SGerrit Uitslag *              'name' => string,
29c3413364SGerrit Uitslag *              'mail' => string,
30c3413364SGerrit Uitslag *              'address' => string,
31c3413364SGerrit Uitslag *              'url' => string
32c3413364SGerrit Uitslag *          ],
33c3413364SGerrit Uitslag *          'date' => [
34c3413364SGerrit Uitslag *              'created' => int timestamp,
35c3413364SGerrit Uitslag *              'modified' => int (not defined if not modified)
36c3413364SGerrit Uitslag *          ],
37c3413364SGerrit Uitslag *          'show' => bool, whether shown (still be moderated, or hidden by moderator or user self)
38c3413364SGerrit Uitslag *      ],
39c3413364SGerrit Uitslag *      ...
40c3413364SGerrit Uitslag *   ]
41c3413364SGerrit Uitslag *   'subscribers' => [
42c3413364SGerrit Uitslag *      '<mail>' => [
43c3413364SGerrit Uitslag *          'hash' => string unique token,
44c3413364SGerrit Uitslag *          'active' => bool, true if confirmed
45c3413364SGerrit Uitslag *          'confirmsent' => bool, true if confirmation mail is sent
46c3413364SGerrit Uitslag *      ],
47c3413364SGerrit Uitslag *      ...
48c3413364SGerrit Uitslag *   ]
49de7e6f00SGerrit Uitslag */
50283a3029SGerrit Uitslagclass action_plugin_discussion extends DokuWiki_Action_Plugin
51283a3029SGerrit Uitslag{
52f0fda08aSwikidesign
53de7e6f00SGerrit Uitslag    /** @var helper_plugin_avatar */
54c3413364SGerrit Uitslag    protected $avatar = null;
55c3413364SGerrit Uitslag    /** @var null|string */
56c3413364SGerrit Uitslag    protected $style = null;
57c3413364SGerrit Uitslag    /** @var null|bool */
58c3413364SGerrit Uitslag    protected $useAvatar = null;
59de7e6f00SGerrit Uitslag    /** @var helper_plugin_discussion */
60c3413364SGerrit Uitslag    protected $helper = null;
61e6b2f142Slupo49
62de7e6f00SGerrit Uitslag    /**
63de7e6f00SGerrit Uitslag     * load helper
64de7e6f00SGerrit Uitslag     */
65283a3029SGerrit Uitslag    public function __construct()
66283a3029SGerrit Uitslag    {
67e6b2f142Slupo49        $this->helper = plugin_load('helper', 'discussion');
68e6b2f142Slupo49    }
69f1c6610eSpierre.spring
70de7e6f00SGerrit Uitslag    /**
71de7e6f00SGerrit Uitslag     * Register the handlers
72de7e6f00SGerrit Uitslag     *
73c3413364SGerrit Uitslag     * @param Doku_Event_Handler $controller DokuWiki's event controller object.
74de7e6f00SGerrit Uitslag     */
75283a3029SGerrit Uitslag    public function register(Doku_Event_Handler $controller)
76283a3029SGerrit Uitslag    {
77c3413364SGerrit Uitslag        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentActions');
78c3413364SGerrit Uitslag        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'renderCommentsSection');
79c3413364SGerrit Uitslag        $controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'addCommentsToIndex', ['id' => 'page', 'text' => 'body']);
80c3413364SGerrit Uitslag        $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'addCommentsToIndex', ['id' => 'id', 'text' => 'text']);
81c3413364SGerrit Uitslag        $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'addIndexVersion', []);
82c3413364SGerrit Uitslag        $controller->register_hook('FULLTEXT_PHRASE_MATCH', 'AFTER', $this, 'fulltextPhraseMatchInComments', []);
83*1ce4168fSGerrit Uitslag        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'updateCommentStatusFromMetadata', []);
84c3413364SGerrit Uitslag        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addToolbarToCommentfield', []);
85c3413364SGerrit Uitslag        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'modifyToolbar', []);
86c3413364SGerrit Uitslag        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxPreviewComments', []);
87c3413364SGerrit Uitslag        $controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'addDiscussionToTOC', []);
88b8fdc796SMichael Klier    }
89b8fdc796SMichael Klier
90b8fdc796SMichael Klier    /**
91b8fdc796SMichael Klier     * Preview Comments
92b8fdc796SMichael Klier     *
93de7e6f00SGerrit Uitslag     * @param Doku_Event $event
94283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
95b8fdc796SMichael Klier     */
96283a3029SGerrit Uitslag    public function ajaxPreviewComments(Doku_Event $event)
97283a3029SGerrit Uitslag    {
98c3413364SGerrit Uitslag        global $INPUT;
99b8fdc796SMichael Klier        if ($event->data != 'discussion_preview') return;
100c3413364SGerrit Uitslag
101b8fdc796SMichael Klier        $event->preventDefault();
102b8fdc796SMichael Klier        $event->stopPropagation();
103b8fdc796SMichael Klier        print p_locale_xhtml('preview');
104b8fdc796SMichael Klier        print '<div class="comment_preview">';
105c3413364SGerrit Uitslag        if (!$INPUT->server->str('REMOTE_USER') && !$this->getConf('allowguests')) {
106b8fdc796SMichael Klier            print p_locale_xhtml('denied');
107b8fdc796SMichael Klier        } else {
108c3413364SGerrit Uitslag            print $this->renderComment($INPUT->post->str('comment'));
109b8fdc796SMichael Klier        }
110b8fdc796SMichael Klier        print '</div>';
1112d4bee9aSMichael Klier    }
1122d4bee9aSMichael Klier
1132d4bee9aSMichael Klier    /**
1145886c85bSMichael Klier     * Adds a TOC item if a discussion exists
1155886c85bSMichael Klier     *
116de7e6f00SGerrit Uitslag     * @param Doku_Event $event
117283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
1185886c85bSMichael Klier     */
119283a3029SGerrit Uitslag    public function addDiscussionToTOC(Doku_Event $event)
120283a3029SGerrit Uitslag    {
1218c057533SMichael Klier        global $ACT;
122c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $event->data && $ACT != 'admin') {
123c3413364SGerrit Uitslag            $tocitem = ['hid' => 'discussion__section',
12476fdd2cdSGerrit Uitslag                'title' => $title ?: $this->getLang('discussion'),
1255886c85bSMichael Klier                'type' => 'ul',
126c3413364SGerrit Uitslag                'level' => 1];
1275886c85bSMichael Klier
128c3413364SGerrit Uitslag            $event->data[] = $tocitem;
1295886c85bSMichael Klier        }
1305886c85bSMichael Klier    }
1315886c85bSMichael Klier
1325886c85bSMichael Klier    /**
133c3413364SGerrit Uitslag     * Modify Toolbar for use with discussion plugin
1342d4bee9aSMichael Klier     *
135de7e6f00SGerrit Uitslag     * @param Doku_Event $event
136283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
1372d4bee9aSMichael Klier     */
138283a3029SGerrit Uitslag    public function modifyToolbar(Doku_Event $event)
139283a3029SGerrit Uitslag    {
1402d4bee9aSMichael Klier        global $ACT;
1412d4bee9aSMichael Klier        if ($ACT != 'show') return;
1422d4bee9aSMichael Klier
143c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
144c3413364SGerrit Uitslag            $toolbar = [];
1452d4bee9aSMichael Klier            foreach ($event->data as $btn) {
1462d4bee9aSMichael Klier                if ($btn['type'] == 'mediapopup') continue;
1472d4bee9aSMichael Klier                if ($btn['type'] == 'signature') continue;
148310210d6SGina Haeussge                if ($btn['type'] == 'linkwiz') continue;
1491dc736fbSGerrit Uitslag                if ($btn['type'] == 'NewTable') continue; //skip button for Edittable Plugin
150f7bcfbedSGerrit Uitslag                //FIXME does nothing. Checks for '=' on toplevel, but today it are special buttons and a picker with subarray
151c3413364SGerrit Uitslag                if (isset($btn['open']) && preg_match("/=+?/", $btn['open'])) continue;
152c3413364SGerrit Uitslag
153c3413364SGerrit Uitslag                $toolbar[] = $btn;
1542d4bee9aSMichael Klier            }
1552d4bee9aSMichael Klier            $event->data = $toolbar;
1562d4bee9aSMichael Klier        }
1572d4bee9aSMichael Klier    }
1582d4bee9aSMichael Klier
1592d4bee9aSMichael Klier    /**
1602d4bee9aSMichael Klier     * Dirty workaround to add a toolbar to the discussion plugin
1612d4bee9aSMichael Klier     *
162de7e6f00SGerrit Uitslag     * @param Doku_Event $event
163283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
1642d4bee9aSMichael Klier     */
165283a3029SGerrit Uitslag    public function addToolbarToCommentfield(Doku_Event $event)
166283a3029SGerrit Uitslag    {
1672d4bee9aSMichael Klier        global $ACT;
1682d4bee9aSMichael Klier        global $ID;
1692d4bee9aSMichael Klier        if ($ACT != 'show') return;
1702d4bee9aSMichael Klier
171c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
1722d4bee9aSMichael Klier            // FIXME ugly workaround, replace this once DW the toolbar code is more flexible
1732d4bee9aSMichael Klier            @require_once(DOKU_INC . 'inc/toolbar.php');
1742d4bee9aSMichael Klier            ob_start();
1752d4bee9aSMichael Klier            print 'NS = "' . getNS($ID) . '";'; // we have to define NS, otherwise we get get JS errors
1762d4bee9aSMichael Klier            toolbar_JSdefines('toolbar');
1772d4bee9aSMichael Klier            $script = ob_get_clean();
178c3413364SGerrit Uitslag            $event->data['script'][] = ['type' => 'text/javascript', 'charset' => "utf-8", '_data' => $script];
1792d4bee9aSMichael Klier        }
180f0fda08aSwikidesign    }
181f0fda08aSwikidesign
182f0fda08aSwikidesign    /**
183a1d93126SGina Haeussge     * Handles comment actions, dispatches data processing routines
184de7e6f00SGerrit Uitslag     *
185de7e6f00SGerrit Uitslag     * @param Doku_Event $event
186f0fda08aSwikidesign     */
187283a3029SGerrit Uitslag    public function handleCommentActions(Doku_Event $event)
188283a3029SGerrit Uitslag    {
189c3413364SGerrit Uitslag        global $ID, $INFO, $lang, $INPUT;
190573e23a1Swikidesign
191a1d93126SGina Haeussge        // handle newthread ACTs
192a1d93126SGina Haeussge        if ($event->data == 'newthread') {
193a1d93126SGina Haeussge            // we can handle it -> prevent others
194c3413364SGerrit Uitslag            $event->data = $this->newThread();
195a1d93126SGina Haeussge        }
196a1d93126SGina Haeussge
197a1d93126SGina Haeussge        // enable captchas
198c3413364SGerrit Uitslag        if (in_array($INPUT->str('comment'), ['add', 'save'])) {
199c3413364SGerrit Uitslag            $this->captchaCheck();
200c3413364SGerrit Uitslag            $this->recaptchaCheck();
201bd6dc08eSAdrian Schlegel        }
202a1d93126SGina Haeussge
2033011fb8bSMichael Klier        // if we are not in show mode or someone wants to unsubscribe, that was all for now
204c3413364SGerrit Uitslag        if ($event->data != 'show'
205c3413364SGerrit Uitslag            && $event->data != 'discussion_unsubscribe'
206c3413364SGerrit Uitslag            && $event->data != 'discussion_confirmsubscribe') {
207c3413364SGerrit Uitslag            return;
208c3413364SGerrit Uitslag        }
209a1d93126SGina Haeussge
210b2f2e866SMichael Klier        if ($event->data == 'discussion_unsubscribe' or $event->data == 'discussion_confirmsubscribe') {
211c3413364SGerrit Uitslag            if ($INPUT->has('hash')) {
2123011fb8bSMichael Klier                $file = metaFN($ID, '.comments');
2133011fb8bSMichael Klier                $data = unserialize(io_readFile($file));
214c3413364SGerrit Uitslag                $matchedMail = '';
2159881d835SMichael Klier                foreach ($data['subscribers'] as $mail => $info) {
2169881d835SMichael Klier                    // convert old style subscribers just in case
2179881d835SMichael Klier                    if (!is_array($info)) {
2189881d835SMichael Klier                        $hash = $data['subscribers'][$mail];
2199881d835SMichael Klier                        $data['subscribers'][$mail]['hash'] = $hash;
2209881d835SMichael Klier                        $data['subscribers'][$mail]['active'] = true;
2219881d835SMichael Klier                        $data['subscribers'][$mail]['confirmsent'] = true;
2229881d835SMichael Klier                    }
2239881d835SMichael Klier
224c3413364SGerrit Uitslag                    if ($data['subscribers'][$mail]['hash'] == $INPUT->str('hash')) {
225c3413364SGerrit Uitslag                        $matchedMail = $mail;
2260c54624fSGina Haeussge                    }
2270c54624fSGina Haeussge                }
2280c54624fSGina Haeussge
229c3413364SGerrit Uitslag                if ($matchedMail != '') {
230b2f2e866SMichael Klier                    if ($event->data == 'discussion_unsubscribe') {
231c3413364SGerrit Uitslag                        unset($data['subscribers'][$matchedMail]);
232c3413364SGerrit Uitslag                        msg(sprintf($lang['subscr_unsubscribe_success'], $matchedMail, $ID), 1);
233c3413364SGerrit Uitslag                    } else { //$event->data == 'discussion_confirmsubscribe'
234c3413364SGerrit Uitslag                        $data['subscribers'][$matchedMail]['active'] = true;
235c3413364SGerrit Uitslag                        msg(sprintf($lang['subscr_subscribe_success'], $matchedMail, $ID), 1);
2369881d835SMichael Klier                    }
2379881d835SMichael Klier                    io_saveFile($file, serialize($data));
2383011fb8bSMichael Klier                    $event->data = 'show';
2393011fb8bSMichael Klier                }
240de7e6f00SGerrit Uitslag
2419881d835SMichael Klier            }
242c3413364SGerrit Uitslag            return;
243f7bcfbedSGerrit Uitslag        }
244f7bcfbedSGerrit Uitslag
245a1d93126SGina Haeussge        // do the data processing for comments
246c3413364SGerrit Uitslag        $cid = $INPUT->str('cid');
247c3413364SGerrit Uitslag        switch ($INPUT->str('comment')) {
248f0fda08aSwikidesign            case 'add':
249c3413364SGerrit Uitslag                if (empty($INPUT->str('text'))) return; // don't add empty comments
250c3413364SGerrit Uitslag
251c3413364SGerrit Uitslag                if ($INPUT->server->has('REMOTE_USER') && !$this->getConf('adminimport')) {
252c3413364SGerrit Uitslag                    $comment['user']['id'] = $INPUT->server->str('REMOTE_USER');
25394c5d164SMichael Klier                    $comment['user']['name'] = $INFO['userinfo']['name'];
25494c5d164SMichael Klier                    $comment['user']['mail'] = $INFO['userinfo']['mail'];
25583f28d9bSGerrit Uitslag                } elseif (($INPUT->server->has('REMOTE_USER') && $this->getConf('adminimport') && $this->helper->isDiscussionModerator())
256c3413364SGerrit Uitslag                    || !$INPUT->server->has('REMOTE_USER')) {
257c3413364SGerrit Uitslag                    // don't add anonymous comments
258c3413364SGerrit Uitslag                    if (empty($INPUT->str('name')) or empty($INPUT->str('mail'))) {
259c3413364SGerrit Uitslag                        return;
260c3413364SGerrit Uitslag                    }
261c3413364SGerrit Uitslag
262c3413364SGerrit Uitslag                    if (!mail_isvalid($INPUT->str('mail'))) {
263c9d36b5eSMichael Klier                        msg($lang['regbadmail'], -1);
264c9d36b5eSMichael Klier                        return;
265c9d36b5eSMichael Klier                    } else {
26600d916a2SGerrit Uitslag                        $comment['user']['id'] = ''; //prevent overlap with loggedin users, before: 'test<ipadress>'
267c3413364SGerrit Uitslag                        $comment['user']['name'] = hsc($INPUT->str('name'));
268c3413364SGerrit Uitslag                        $comment['user']['mail'] = hsc($INPUT->str('mail'));
26994c5d164SMichael Klier                    }
270c9d36b5eSMichael Klier                }
271c3413364SGerrit Uitslag                $comment['user']['address'] = ($this->getConf('addressfield')) ? hsc($INPUT->str('address')) : '';
272c3413364SGerrit Uitslag                $comment['user']['url'] = ($this->getConf('urlfield')) ? $this->checkURL($INPUT->str('url')) : '';
273c3413364SGerrit Uitslag                $comment['subscribe'] = ($this->getConf('subscribe')) ? $INPUT->has('subscribe') : '';
274c3413364SGerrit Uitslag                $comment['date'] = ['created' => $INPUT->str('date')];
275c3413364SGerrit Uitslag                $comment['raw'] = cleanText($INPUT->str('text'));
276c3413364SGerrit Uitslag                $reply = $INPUT->str('reply');
27783f28d9bSGerrit Uitslag                if ($this->getConf('moderate') && !$this->helper->isDiscussionModerator()) {
278a44bc9f7SMichael Klier                    $comment['show'] = false;
279a44bc9f7SMichael Klier                } else {
280a44bc9f7SMichael Klier                    $comment['show'] = true;
281a44bc9f7SMichael Klier                }
282c3413364SGerrit Uitslag                $this->add($comment, $reply);
283f0fda08aSwikidesign                break;
284f0fda08aSwikidesign
285f0fda08aSwikidesign            case 'save':
286c3413364SGerrit Uitslag                $raw = cleanText($INPUT->str('text'));
287c3413364SGerrit Uitslag                $this->save([$cid], $raw);
288f0fda08aSwikidesign                break;
289f0fda08aSwikidesign
2901e46d176Swikidesign            case 'delete':
291c3413364SGerrit Uitslag                $this->save([$cid], '');
2922ee3dca3Swikidesign                break;
2931e46d176Swikidesign
294f0fda08aSwikidesign            case 'toogle':
295c3413364SGerrit Uitslag                $this->save([$cid], '', 'toogle');
296f0fda08aSwikidesign                break;
297a1d93126SGina Haeussge        }
2983011fb8bSMichael Klier    }
299a1d93126SGina Haeussge
300a1d93126SGina Haeussge    /**
301a1d93126SGina Haeussge     * Main function; dispatches the visual comment actions
302f7bcfbedSGerrit Uitslag     *
303f7bcfbedSGerrit Uitslag     * @param Doku_Event $event
304a1d93126SGina Haeussge     */
305283a3029SGerrit Uitslag    public function renderCommentsSection(Doku_Event $event)
306283a3029SGerrit Uitslag    {
307c3413364SGerrit Uitslag        global $INPUT;
308a1d93126SGina Haeussge        if ($event->data != 'show') return; // nothing to do for us
309a1d93126SGina Haeussge
310c3413364SGerrit Uitslag        $cid = $INPUT->str('cid');
311c3413364SGerrit Uitslag
312aa0d6cd7SGerrit Uitslag        if (!$cid) {
313c3413364SGerrit Uitslag            $cid = $INPUT->str('reply');
314aa0d6cd7SGerrit Uitslag        }
315283a3029SGerrit Uitslag
316c3413364SGerrit Uitslag        switch ($INPUT->str('comment')) {
317a1d93126SGina Haeussge            case 'edit':
318c3413364SGerrit Uitslag                $this->showDiscussionSection(null, $cid);
319a1d93126SGina Haeussge                break;
320283a3029SGerrit Uitslag            default: //'reply' or no action specified
321c3413364SGerrit Uitslag                $this->showDiscussionSection($cid);
3222b18adb9SMichael Klier                break;
323f0fda08aSwikidesign        }
324f0fda08aSwikidesign    }
325f0fda08aSwikidesign
326f0fda08aSwikidesign    /**
327a1d93126SGina Haeussge     * Redirects browser to given comment anchor
328f7bcfbedSGerrit Uitslag     *
329f7bcfbedSGerrit Uitslag     * @param string $cid comment id
330a1d93126SGina Haeussge     */
331283a3029SGerrit Uitslag    protected function redirect($cid)
332283a3029SGerrit Uitslag    {
333a1d93126SGina Haeussge        global $ID;
334a1d93126SGina Haeussge        global $ACT;
335a1d93126SGina Haeussge
336a1d93126SGina Haeussge        if ($ACT !== 'show') return;
337a44bc9f7SMichael Klier
33883f28d9bSGerrit Uitslag        if ($this->getConf('moderate') && !$this->helper->isDiscussionModerator()) {
339a44bc9f7SMichael Klier            msg($this->getLang('moderation'), 1);
340a44bc9f7SMichael Klier            @session_start();
341a44bc9f7SMichael Klier            global $MSG;
342a44bc9f7SMichael Klier            $_SESSION[DOKU_COOKIE]['msg'] = $MSG;
343a44bc9f7SMichael Klier            session_write_close();
344a44bc9f7SMichael Klier            $url = wl($ID);
345a44bc9f7SMichael Klier        } else {
346a44bc9f7SMichael Klier            $url = wl($ID) . '#comment_' . $cid;
347a44bc9f7SMichael Klier        }
348e88de38aSAxel Beckert
349e88de38aSAxel Beckert        if (function_exists('send_redirect')) {
350e88de38aSAxel Beckert            send_redirect($url);
351e88de38aSAxel Beckert        } else {
35269f42a0bSAxel Beckert            header('Location: ' . $url);
353e88de38aSAxel Beckert        }
3546d1f4f20SGina Haeussge        exit();
355a1d93126SGina Haeussge    }
356a1d93126SGina Haeussge
357a1d93126SGina Haeussge    /**
358d406a452SGerrit Uitslag     * Checks config settings to enable/disable discussions
359f0bcde18SGerrit Uitslag     *
360f7bcfbedSGerrit Uitslag     * @return bool true if enabled
361f0bcde18SGerrit Uitslag     */
362283a3029SGerrit Uitslag    public function isDiscussionEnabled()
363283a3029SGerrit Uitslag    {
364f8c74f2eSGerrit Uitslag        global $ID;
365f0bcde18SGerrit Uitslag
366d406a452SGerrit Uitslag        if ($this->getConf('excluded_ns') == '') {
367d406a452SGerrit Uitslag            $isNamespaceExcluded = false;
368d406a452SGerrit Uitslag        } else {
369283a3029SGerrit Uitslag            $ns = getNS($ID); // $INFO['namespace'] is not yet available, if used in update_comment_status()
370f8c74f2eSGerrit Uitslag            $isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $ns);
371d406a452SGerrit Uitslag        }
372f0bcde18SGerrit Uitslag
373f0bcde18SGerrit Uitslag        if ($this->getConf('automatic')) {
374f0bcde18SGerrit Uitslag            if ($isNamespaceExcluded) {
375f0bcde18SGerrit Uitslag                return false;
376f0bcde18SGerrit Uitslag            } else {
377f0bcde18SGerrit Uitslag                return true;
378f0bcde18SGerrit Uitslag            }
379f0bcde18SGerrit Uitslag        } else {
380f0bcde18SGerrit Uitslag            if ($isNamespaceExcluded) {
381f0bcde18SGerrit Uitslag                return true;
382f0bcde18SGerrit Uitslag            } else {
383f0bcde18SGerrit Uitslag                return false;
384f0bcde18SGerrit Uitslag            }
385f0bcde18SGerrit Uitslag        }
386f0bcde18SGerrit Uitslag    }
387f0bcde18SGerrit Uitslag
388f0bcde18SGerrit Uitslag    /**
389c3413364SGerrit Uitslag     * Shows all comments of the current page, if no reply or edit requested, then comment form is shown on the end
390c3413364SGerrit Uitslag     *
391c3413364SGerrit Uitslag     * @param null|string $reply comment id on which the user requested a reply
392c3413364SGerrit Uitslag     * @param null|string $edit comment id which the user requested for editing
393f0fda08aSwikidesign     */
394283a3029SGerrit Uitslag    protected function showDiscussionSection($reply = null, $edit = null)
395283a3029SGerrit Uitslag    {
396c3413364SGerrit Uitslag        global $ID, $INFO, $INPUT;
397573e23a1Swikidesign
398479dd10fSwikidesign        // get .comments meta file name
399f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
400f0fda08aSwikidesign
401c3413364SGerrit Uitslag        if (!$INFO['exists']) return;
402c3413364SGerrit Uitslag        if (!@file_exists($file) && !$this->isDiscussionEnabled()) return;
403c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('showguests')) return;
404f0fda08aSwikidesign
4052b18adb9SMichael Klier        // load data
406c3413364SGerrit Uitslag        $data = [];
4072b18adb9SMichael Klier        if (@file_exists($file)) {
4082b18adb9SMichael Klier            $data = unserialize(io_readFile($file, false));
409c3413364SGerrit Uitslag            // comments are turned off
410c3413364SGerrit Uitslag            if (!$data['status']) {
411c3413364SGerrit Uitslag                return;
412c3413364SGerrit Uitslag            }
413f7bcfbedSGerrit Uitslag        } elseif (!@file_exists($file) && $this->isDiscussionEnabled()) {
4142b18adb9SMichael Klier            // set status to show the comment form
4152b18adb9SMichael Klier            $data['status'] = 1;
4162b18adb9SMichael Klier            $data['number'] = 0;
417*1ce4168fSGerrit Uitslag            $data['title'] = null;
4182b18adb9SMichael Klier        }
419f0fda08aSwikidesign
420a1599850SMichael Klier        // show discussion wrapper only on certain circumstances
421c3413364SGerrit Uitslag        if (empty($data['comments']) || !is_array($data['comments'])) {
422c3413364SGerrit Uitslag            $cnt = 0;
4233e19949cSMark Prins            $keys = [];
424c3413364SGerrit Uitslag        } else {
425c3413364SGerrit Uitslag            $cnt = count($data['comments']);
426c3413364SGerrit Uitslag            $keys = array_keys($data['comments']);
4273e19949cSMark Prins        }
428c3413364SGerrit Uitslag
429de7e6f00SGerrit Uitslag        $show = false;
430c3413364SGerrit Uitslag        if ($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1)
431c3413364SGerrit Uitslag            || $this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) {
432a1599850SMichael Klier            $show = true;
433f0fda08aSwikidesign            // section title
43476fdd2cdSGerrit Uitslag            $title = (!empty($data['title']) ? hsc($data['title']) : $this->getLang('discussion'));
43546178401Slupo49            ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section
4364a0a1bd2Swikidesign            ptln('<h2><a name="discussion__section" id="discussion__section">', 2);
4374a0a1bd2Swikidesign            ptln($title, 4);
4384a0a1bd2Swikidesign            ptln('</a></h2>', 2);
4394a0a1bd2Swikidesign            ptln('<div class="level2 hfeed">', 2);
440a1599850SMichael Klier        }
441a1599850SMichael Klier
442f0fda08aSwikidesign        // now display the comments
443f0fda08aSwikidesign        if (isset($data['comments'])) {
44431aab30eSGina Haeussge            if (!$this->getConf('usethreading')) {
445c3413364SGerrit Uitslag                $data['comments'] = $this->flattenThreads($data['comments']);
446283a3029SGerrit Uitslag                uasort($data['comments'], [$this, 'sortThreadsOnCreation']);
44731aab30eSGina Haeussge            }
448dbd9d5cdSMichael Klier            if ($this->getConf('newestfirst')) {
449dbd9d5cdSMichael Klier                $data['comments'] = array_reverse($data['comments']);
450dbd9d5cdSMichael Klier            }
451c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
452c3413364SGerrit Uitslag                if ($cid == $edit) { // edit form
453c3413364SGerrit Uitslag                    $this->showCommentForm($value['raw'], 'save', $edit);
454c3413364SGerrit Uitslag                } else {
455c3413364SGerrit Uitslag                    $this->showCommentWithReplies($cid, $data, '', $reply);
456c3413364SGerrit Uitslag                }
457f0fda08aSwikidesign            }
458f0fda08aSwikidesign        }
459f0fda08aSwikidesign
460c3413364SGerrit Uitslag        // comment form shown on the end, if no comment form of $reply or $edit is requested before
461c3413364SGerrit Uitslag        if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) {
462f7bcfbedSGerrit Uitslag            $this->showCommentForm('', 'add');
463c3413364SGerrit Uitslag        }
464f0fda08aSwikidesign
465a1599850SMichael Klier        if ($show) {
4664a0a1bd2Swikidesign            ptln('</div>', 2); // level2 hfeed
4674a0a1bd2Swikidesign            ptln('</div>'); // comment_wrapper
468a1599850SMichael Klier        }
469f0fda08aSwikidesign
47046178401Slupo49        // check for toggle print configuration
47146178401Slupo49        if ($this->getConf('visibilityButton')) {
47246178401Slupo49            // print the hide/show discussion section button
473c3413364SGerrit Uitslag            $this->showDiscussionToggleButton();
47446178401Slupo49        }
475f0fda08aSwikidesign    }
476f0fda08aSwikidesign
477de7e6f00SGerrit Uitslag    /**
478c3413364SGerrit Uitslag     * Remove the parent-child relation, such that the comment structure becomes flat
479c3413364SGerrit Uitslag     *
480c3413364SGerrit Uitslag     * @param array $comments array with all comments
481c3413364SGerrit Uitslag     * @param null|array $cids comment ids of replies, which should be flatten
482c3413364SGerrit Uitslag     * @return array returned array with flattened comment structure
483de7e6f00SGerrit Uitslag     */
484283a3029SGerrit Uitslag    protected function flattenThreads($comments, $cids = null)
485283a3029SGerrit Uitslag    {
486c3413364SGerrit Uitslag        if (is_null($cids)) {
487c3413364SGerrit Uitslag            $cids = array_keys($comments);
488c3413364SGerrit Uitslag        }
48931aab30eSGina Haeussge
490c3413364SGerrit Uitslag        foreach ($cids as $cid) {
49131aab30eSGina Haeussge            if (!empty($comments[$cid]['replies'])) {
49231aab30eSGina Haeussge                $rids = $comments[$cid]['replies'];
493c3413364SGerrit Uitslag                $comments = $this->flattenThreads($comments, $rids);
494c3413364SGerrit Uitslag                $comments[$cid]['replies'] = [];
49531aab30eSGina Haeussge            }
49631aab30eSGina Haeussge            $comments[$cid]['parent'] = '';
49731aab30eSGina Haeussge        }
49831aab30eSGina Haeussge        return $comments;
49931aab30eSGina Haeussge    }
50031aab30eSGina Haeussge
501f0fda08aSwikidesign    /**
502f0fda08aSwikidesign     * Adds a new comment and then displays all comments
503de7e6f00SGerrit Uitslag     *
504c3413364SGerrit Uitslag     * @param array $comment with
505c3413364SGerrit Uitslag     *  'raw' => string comment text,
506c3413364SGerrit Uitslag     *  'user' => [
507c3413364SGerrit Uitslag     *      'id' => string,
508c3413364SGerrit Uitslag     *      'name' => string,
509c3413364SGerrit Uitslag     *      'mail' => string
510c3413364SGerrit Uitslag     *  ],
511c3413364SGerrit Uitslag     *  'date' => [
512c3413364SGerrit Uitslag     *      'created' => int timestamp
513c3413364SGerrit Uitslag     *  ]
514c3413364SGerrit Uitslag     *  'show' => bool
515c3413364SGerrit Uitslag     *  'subscribe' => bool
516c3413364SGerrit Uitslag     * @param string $parent comment id of parent
517de7e6f00SGerrit Uitslag     * @return bool
518f0fda08aSwikidesign     */
519283a3029SGerrit Uitslag    protected function add($comment, $parent)
520283a3029SGerrit Uitslag    {
521c3413364SGerrit Uitslag        global $ID, $TEXT, $INPUT;
522f0fda08aSwikidesign
523c3413364SGerrit Uitslag        $originalTxt = $TEXT; // set $TEXT to comment text for wordblock check
524f0fda08aSwikidesign        $TEXT = $comment['raw'];
525f0fda08aSwikidesign
526f0fda08aSwikidesign        // spamcheck against the DokuWiki blacklist
527f0fda08aSwikidesign        if (checkwordblock()) {
528f0fda08aSwikidesign            msg($this->getLang('wordblock'), -1);
529f0fda08aSwikidesign            return false;
530f0fda08aSwikidesign        }
531f0fda08aSwikidesign
532c3413364SGerrit Uitslag        if (!$this->getConf('allowguests')
533c3413364SGerrit Uitslag            && $comment['user']['id'] != $INPUT->server->str('REMOTE_USER')
5344cded5e1SGerrit Uitslag        ) {
5353011fb8bSMichael Klier            return false; // guest comments not allowed
5364cded5e1SGerrit Uitslag        }
5373011fb8bSMichael Klier
538c3413364SGerrit Uitslag        $TEXT = $originalTxt; // restore global $TEXT
539f0fda08aSwikidesign
540f0fda08aSwikidesign        // get discussion meta file name
541f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
542f0fda08aSwikidesign
5432b18adb9SMichael Klier        // create comments file if it doesn't exist yet
5442b18adb9SMichael Klier        if (!@file_exists($file)) {
545*1ce4168fSGerrit Uitslag            $data = [
546*1ce4168fSGerrit Uitslag                'status' => 1,
547*1ce4168fSGerrit Uitslag                'number' => 0,
548*1ce4168fSGerrit Uitslag                'title' => null
549*1ce4168fSGerrit Uitslag            ];
5502b18adb9SMichael Klier            io_saveFile($file, serialize($data));
5512b18adb9SMichael Klier        } else {
552f0fda08aSwikidesign            $data = unserialize(io_readFile($file, false));
553c3413364SGerrit Uitslag            // comments off or closed
554c3413364SGerrit Uitslag            if ($data['status'] != 1) {
555c3413364SGerrit Uitslag                return false;
556c3413364SGerrit Uitslag            }
5572b18adb9SMichael Klier        }
5582b18adb9SMichael Klier
5593011fb8bSMichael Klier        if ($comment['date']['created']) {
5603011fb8bSMichael Klier            $date = strtotime($comment['date']['created']);
5613011fb8bSMichael Klier        } else {
5623011fb8bSMichael Klier            $date = time();
5633011fb8bSMichael Klier        }
564f0fda08aSwikidesign
5653011fb8bSMichael Klier        if ($date == -1) {
5663011fb8bSMichael Klier            $date = time();
5673011fb8bSMichael Klier        }
5683011fb8bSMichael Klier
5696046f25cSwikidesign        $cid = md5($comment['user']['id'] . $date); // create a unique id
570f0fda08aSwikidesign
57176fdd2cdSGerrit Uitslag        if (!isset($data['comments'][$parent]) || !is_array($data['comments'][$parent])) {
572c3413364SGerrit Uitslag            $parent = null; // invalid parent comment
5733011fb8bSMichael Klier        }
574f0fda08aSwikidesign
575f0fda08aSwikidesign        // render the comment
576c3413364SGerrit Uitslag        $xhtml = $this->renderComment($comment['raw']);
577f0fda08aSwikidesign
578f0fda08aSwikidesign        // fill in the new comment
579c3413364SGerrit Uitslag        $data['comments'][$cid] = [
5806046f25cSwikidesign            'user' => $comment['user'],
581c3413364SGerrit Uitslag            'date' => ['created' => $date],
5826046f25cSwikidesign            'raw' => $comment['raw'],
583f0fda08aSwikidesign            'xhtml' => $xhtml,
584f0fda08aSwikidesign            'parent' => $parent,
585c3413364SGerrit Uitslag            'replies' => [],
586a44bc9f7SMichael Klier            'show' => $comment['show']
587c3413364SGerrit Uitslag        ];
588f0fda08aSwikidesign
5893011fb8bSMichael Klier        if ($comment['subscribe']) {
5903011fb8bSMichael Klier            $mail = $comment['user']['mail'];
5913011fb8bSMichael Klier            if ($data['subscribers']) {
5923011fb8bSMichael Klier                if (!$data['subscribers'][$mail]) {
5939881d835SMichael Klier                    $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
5949881d835SMichael Klier                    $data['subscribers'][$mail]['active'] = false;
5959881d835SMichael Klier                    $data['subscribers'][$mail]['confirmsent'] = false;
5969881d835SMichael Klier                } else {
5979881d835SMichael Klier                    // convert old style subscribers and set them active
5989881d835SMichael Klier                    if (!is_array($data['subscribers'][$mail])) {
5999881d835SMichael Klier                        $hash = $data['subscribers'][$mail];
6009881d835SMichael Klier                        $data['subscribers'][$mail]['hash'] = $hash;
6019881d835SMichael Klier                        $data['subscribers'][$mail]['active'] = true;
6029881d835SMichael Klier                        $data['subscribers'][$mail]['confirmsent'] = true;
6039881d835SMichael Klier                    }
6043011fb8bSMichael Klier                }
6053011fb8bSMichael Klier            } else {
6069881d835SMichael Klier                $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
6079881d835SMichael Klier                $data['subscribers'][$mail]['active'] = false;
6089881d835SMichael Klier                $data['subscribers'][$mail]['confirmsent'] = false;
6093011fb8bSMichael Klier            }
6103011fb8bSMichael Klier        }
6113011fb8bSMichael Klier
612f0fda08aSwikidesign        // update parent comment
6134cded5e1SGerrit Uitslag        if ($parent) {
6144cded5e1SGerrit Uitslag            $data['comments'][$parent]['replies'][] = $cid;
6154cded5e1SGerrit Uitslag        }
616f0fda08aSwikidesign
617f0fda08aSwikidesign        // update the number of comments
618f0fda08aSwikidesign        $data['number']++;
619f0fda08aSwikidesign
620f0fda08aSwikidesign        // notify subscribers of the page
6218b42cefbSMichael Klier        $data['comments'][$cid]['cid'] = $cid;
622c3413364SGerrit Uitslag        $this->notify($data['comments'][$cid], $data['subscribers']);
623f0fda08aSwikidesign
6249881d835SMichael Klier        // save the comment metadata file
6259881d835SMichael Klier        io_saveFile($file, serialize($data));
626c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, 'cc', '', $cid);
6279881d835SMichael Klier
628c3413364SGerrit Uitslag        $this->redirect($cid);
629f0fda08aSwikidesign        return true;
630f0fda08aSwikidesign    }
631f0fda08aSwikidesign
632f0fda08aSwikidesign    /**
633f0fda08aSwikidesign     * Saves the comment with the given ID and then displays all comments
634de7e6f00SGerrit Uitslag     *
635c3413364SGerrit Uitslag     * @param array|string $cids array with comment ids to save, or a single string comment id
636c3413364SGerrit Uitslag     * @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!)
637c3413364SGerrit Uitslag     * @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw
638c3413364SGerrit Uitslag     * @return bool succeed?
639f0fda08aSwikidesign     */
640283a3029SGerrit Uitslag    public function save($cids, $raw, $act = null)
641283a3029SGerrit Uitslag    {
642c3413364SGerrit Uitslag        global $ID, $INPUT;
643f0fda08aSwikidesign
644c3413364SGerrit Uitslag        if (empty($cids)) return false; // do nothing if we get no comment id
645757550e8SMichael Klier
6462ee3dca3Swikidesign        if ($raw) {
6472ee3dca3Swikidesign            global $TEXT;
6482ee3dca3Swikidesign
649f0fda08aSwikidesign            $otxt = $TEXT; // set $TEXT to comment text for wordblock check
650f0fda08aSwikidesign            $TEXT = $raw;
651f0fda08aSwikidesign
652f0fda08aSwikidesign            // spamcheck against the DokuWiki blacklist
653f0fda08aSwikidesign            if (checkwordblock()) {
654f0fda08aSwikidesign                msg($this->getLang('wordblock'), -1);
655f0fda08aSwikidesign                return false;
656f0fda08aSwikidesign            }
657f0fda08aSwikidesign
658f0fda08aSwikidesign            $TEXT = $otxt; // restore global $TEXT
6592ee3dca3Swikidesign        }
660f0fda08aSwikidesign
661f0fda08aSwikidesign        // get discussion meta file name
662f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
663f0fda08aSwikidesign        $data = unserialize(io_readFile($file, false));
664f0fda08aSwikidesign
665c3413364SGerrit Uitslag        if (!is_array($cids)) {
666c3413364SGerrit Uitslag            $cids = [$cids];
667c3413364SGerrit Uitslag        }
668264b7327Swikidesign        foreach ($cids as $cid) {
669264b7327Swikidesign
6706046f25cSwikidesign            if (is_array($data['comments'][$cid]['user'])) {
6716046f25cSwikidesign                $user = $data['comments'][$cid]['user']['id'];
6726046f25cSwikidesign                $convert = false;
6736046f25cSwikidesign            } else {
6746046f25cSwikidesign                $user = $data['comments'][$cid]['user'];
6756046f25cSwikidesign                $convert = true;
6766046f25cSwikidesign            }
6776046f25cSwikidesign
678f0fda08aSwikidesign            // someone else was trying to edit our comment -> abort
67983f28d9bSGerrit Uitslag            if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionModerator()) {
680c3413364SGerrit Uitslag                return false;
681c3413364SGerrit Uitslag            }
682f0fda08aSwikidesign
683f0fda08aSwikidesign            $date = time();
684f0fda08aSwikidesign
6856046f25cSwikidesign            // need to convert to new format?
6866046f25cSwikidesign            if ($convert) {
687c3413364SGerrit Uitslag                $data['comments'][$cid]['user'] = [
6886046f25cSwikidesign                    'id' => $user,
6896046f25cSwikidesign                    'name' => $data['comments'][$cid]['name'],
6906046f25cSwikidesign                    'mail' => $data['comments'][$cid]['mail'],
6916046f25cSwikidesign                    'url' => $data['comments'][$cid]['url'],
6926046f25cSwikidesign                    'address' => $data['comments'][$cid]['address'],
693c3413364SGerrit Uitslag                ];
694c3413364SGerrit Uitslag                $data['comments'][$cid]['date'] = [
6956046f25cSwikidesign                    'created' => $data['comments'][$cid]['date']
696c3413364SGerrit Uitslag                ];
6976046f25cSwikidesign            }
6986046f25cSwikidesign
699264b7327Swikidesign            if ($act == 'toogle') {     // toogle visibility
700f0fda08aSwikidesign                $now = $data['comments'][$cid]['show'];
701f0fda08aSwikidesign                $data['comments'][$cid]['show'] = !$now;
702c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
703f0fda08aSwikidesign
704f0fda08aSwikidesign                $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
705f0fda08aSwikidesign
706264b7327Swikidesign            } elseif ($act == 'show') { // show comment
707264b7327Swikidesign                $data['comments'][$cid]['show'] = true;
708c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
709264b7327Swikidesign
710573e23a1Swikidesign                $type = 'sc'; // show comment
711264b7327Swikidesign
712264b7327Swikidesign            } elseif ($act == 'hide') { // hide comment
713264b7327Swikidesign                $data['comments'][$cid]['show'] = false;
714c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
715264b7327Swikidesign
716573e23a1Swikidesign                $type = 'hc'; // hide comment
717264b7327Swikidesign
718f0fda08aSwikidesign            } elseif (!$raw) {          // remove the comment
719c3413364SGerrit Uitslag                $data['comments'] = $this->removeComment($cid, $data['comments']);
720c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
721f0fda08aSwikidesign
722573e23a1Swikidesign                $type = 'dc'; // delete comment
723f0fda08aSwikidesign
724f0fda08aSwikidesign            } else {                   // save changed comment
725c3413364SGerrit Uitslag                $xhtml = $this->renderComment($raw);
726f0fda08aSwikidesign
727f0fda08aSwikidesign                // now change the comment's content
7286046f25cSwikidesign                $data['comments'][$cid]['date']['modified'] = $date;
7296046f25cSwikidesign                $data['comments'][$cid]['raw'] = $raw;
730f0fda08aSwikidesign                $data['comments'][$cid]['xhtml'] = $xhtml;
731f0fda08aSwikidesign
732573e23a1Swikidesign                $type = 'ec'; // edit comment
733f0fda08aSwikidesign            }
734264b7327Swikidesign        }
735264b7327Swikidesign
736f0fda08aSwikidesign        // save the comment metadata file
737f0fda08aSwikidesign        io_saveFile($file, serialize($data));
738c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, $type, '', $cid);
739f0fda08aSwikidesign
740c3413364SGerrit Uitslag        $this->redirect($cid);
741f0fda08aSwikidesign        return true;
742f0fda08aSwikidesign    }
743f0fda08aSwikidesign
744f0fda08aSwikidesign    /**
745c3413364SGerrit Uitslag     * Recursive function to remove a comment from the data array
746c3413364SGerrit Uitslag     *
747c3413364SGerrit Uitslag     * @param string $cid comment id to be removed
748c3413364SGerrit Uitslag     * @param array $comments array with all comments
749c3413364SGerrit Uitslag     * @return array returns modified array with all remaining comments
750efbe59d0Swikidesign     */
751283a3029SGerrit Uitslag    protected function removeComment($cid, $comments)
752283a3029SGerrit Uitslag    {
753efbe59d0Swikidesign        if (is_array($comments[$cid]['replies'])) {
754efbe59d0Swikidesign            foreach ($comments[$cid]['replies'] as $rid) {
755c3413364SGerrit Uitslag                $comments = $this->removeComment($rid, $comments);
756efbe59d0Swikidesign            }
757efbe59d0Swikidesign        }
758efbe59d0Swikidesign        unset($comments[$cid]);
759efbe59d0Swikidesign        return $comments;
760efbe59d0Swikidesign    }
761efbe59d0Swikidesign
762efbe59d0Swikidesign    /**
763f0fda08aSwikidesign     * Prints an individual comment
764de7e6f00SGerrit Uitslag     *
765c3413364SGerrit Uitslag     * @param string $cid comment id
766c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
767c3413364SGerrit Uitslag     * @param string $parent comment id of parent
768c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
769c3413364SGerrit Uitslag     * @param bool $isVisible is marked as visible
770f0fda08aSwikidesign     */
771283a3029SGerrit Uitslag    protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true)
772283a3029SGerrit Uitslag    {
773c3413364SGerrit Uitslag        // comment was removed
774c3413364SGerrit Uitslag        if (!isset($data['comments'][$cid])) {
775c3413364SGerrit Uitslag            return;
776c3413364SGerrit Uitslag        }
777f0fda08aSwikidesign        $comment = $data['comments'][$cid];
778f0fda08aSwikidesign
779c3413364SGerrit Uitslag        // corrupt datatype
780c3413364SGerrit Uitslag        if (!is_array($comment)) {
781c3413364SGerrit Uitslag            return;
782c3413364SGerrit Uitslag        }
783f0fda08aSwikidesign
784c3413364SGerrit Uitslag        // handle only replies to given parent comment
785c3413364SGerrit Uitslag        if ($comment['parent'] != $parent) {
786c3413364SGerrit Uitslag            return;
787c3413364SGerrit Uitslag        }
788f0fda08aSwikidesign
78903ab0c03SGerrit Uitslag        // comment hidden, only shown for moderators
79003ab0c03SGerrit Uitslag        if (!$comment['show'] && !$this->helper->isDiscussionModerator()) {
791c3413364SGerrit Uitslag            return;
792c3413364SGerrit Uitslag        }
793f0fda08aSwikidesign
794f1c6610eSpierre.spring        // print the actual comment
79503ab0c03SGerrit Uitslag        $this->showComment($cid, $data, $reply, $isVisible);
796f1c6610eSpierre.spring        // replies to this comment entry?
797c3413364SGerrit Uitslag        $this->showReplies($cid, $data, $reply, $isVisible);
798f1c6610eSpierre.spring        // reply form
799c3413364SGerrit Uitslag        $this->showReplyForm($cid, $reply);
800f1c6610eSpierre.spring    }
801f1c6610eSpierre.spring
802de7e6f00SGerrit Uitslag    /**
803c3413364SGerrit Uitslag     * Print the comment
804c3413364SGerrit Uitslag     *
805c3413364SGerrit Uitslag     * @param string $cid comment id
80620d55e56SGerrit Uitslag     * @param array $data array with all comments
807c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
80803ab0c03SGerrit Uitslag     * @param bool $isVisible (grand)parent is marked as visible
809de7e6f00SGerrit Uitslag     */
81003ab0c03SGerrit Uitslag    protected function showComment($cid, $data, $reply, $isVisible)
811283a3029SGerrit Uitslag    {
812c3413364SGerrit Uitslag        global $conf, $lang, $HIGH, $INPUT;
813f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
814f1c6610eSpierre.spring
81503ab0c03SGerrit Uitslag        //only moderators can arrive here if hidden
81603ab0c03SGerrit Uitslag        $hiddenclass = '';
81703ab0c03SGerrit Uitslag        if (!$comment['show'] || !$isVisible) {
81803ab0c03SGerrit Uitslag            $hiddenclass = ' comment_hidden';
81903ab0c03SGerrit Uitslag        }
820f0fda08aSwikidesign        // comment head with date and user data
82103ab0c03SGerrit Uitslag        ptln('<div class="hentry' . $hiddenclass . '">', 4);
8224a0a1bd2Swikidesign        ptln('<div class="comment_head">', 6);
8231810ba9cSMichael Klier        ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8);
8244a0a1bd2Swikidesign        $head = '<span class="vcard author">';
825f0fda08aSwikidesign
8266046f25cSwikidesign        // prepare variables
8276046f25cSwikidesign        if (is_array($comment['user'])) { // new format
8286046f25cSwikidesign            $user = $comment['user']['id'];
8296046f25cSwikidesign            $name = $comment['user']['name'];
8306046f25cSwikidesign            $mail = $comment['user']['mail'];
8316046f25cSwikidesign            $url = $comment['user']['url'];
8326046f25cSwikidesign            $address = $comment['user']['address'];
8336046f25cSwikidesign        } else {                         // old format
8346046f25cSwikidesign            $user = $comment['user'];
8356046f25cSwikidesign            $name = $comment['name'];
8366046f25cSwikidesign            $mail = $comment['mail'];
8376046f25cSwikidesign            $url = $comment['url'];
8386046f25cSwikidesign            $address = $comment['address'];
8396046f25cSwikidesign        }
8406046f25cSwikidesign        if (is_array($comment['date'])) { // new format
8416046f25cSwikidesign            $created = $comment['date']['created'];
842283a3029SGerrit Uitslag            $modified = $comment['date']['modified'] ?? null;
8436046f25cSwikidesign        } else {                         // old format
8446046f25cSwikidesign            $created = $comment['date'];
8456046f25cSwikidesign            $modified = $comment['edited'];
8466046f25cSwikidesign        }
8476046f25cSwikidesign
8480ff5ab97SMichael Klier        // show username or real name?
849c3413364SGerrit Uitslag        if (!$this->getConf('userealname') && $user) {
85000d916a2SGerrit Uitslag            //not logged-in users have currently username set to '', but before 'test<Ipaddress>'
85100d916a2SGerrit Uitslag            if(substr($user, 0,4) === 'test'
85200d916a2SGerrit Uitslag                && (strpos($user, ':', 4) !== false || strpos($user, '.', 4) !== false)) {
85300d916a2SGerrit Uitslag                $showname = $name;
85400d916a2SGerrit Uitslag            } else {
8550ff5ab97SMichael Klier                $showname = $user;
85600d916a2SGerrit Uitslag            }
8570ff5ab97SMichael Klier        } else {
8580ff5ab97SMichael Klier            $showname = $name;
8590ff5ab97SMichael Klier        }
8600ff5ab97SMichael Klier
861ce7b17cfSwikidesign        // show avatar image?
862c3413364SGerrit Uitslag        if ($this->useAvatar()) {
86312ca6034SMichael Klier            $user_data['name'] = $name;
86412ca6034SMichael Klier            $user_data['user'] = $user;
86512ca6034SMichael Klier            $user_data['mail'] = $mail;
86612ca6034SMichael Klier            $avatar = $this->avatar->getXHTML($user_data, $name, 'left');
867c3413364SGerrit Uitslag            if ($avatar) {
868c3413364SGerrit Uitslag                $head .= $avatar;
869c3413364SGerrit Uitslag            }
870f0fda08aSwikidesign        }
871f0fda08aSwikidesign
8726046f25cSwikidesign        if ($this->getConf('linkemail') && $mail) {
8730ff5ab97SMichael Klier            $head .= $this->email($mail, $showname, 'email fn');
8746046f25cSwikidesign        } elseif ($url) {
875c3413364SGerrit Uitslag            $head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn');
876f0fda08aSwikidesign        } else {
8770ff5ab97SMichael Klier            $head .= '<span class="fn">' . $showname . '</span>';
878f0fda08aSwikidesign        }
8796f1f13d6SMatthias Schulte
8804cded5e1SGerrit Uitslag        if ($address) {
8814cded5e1SGerrit Uitslag            $head .= ', <span class="adr">' . $address . '</span>';
8824cded5e1SGerrit Uitslag        }
8834a0a1bd2Swikidesign        $head .= '</span>, ' .
884f014bc86SMichael Klier            '<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' .
8856f1f13d6SMatthias Schulte            dformat($created, $conf['dformat']) . '</abbr>';
8869c0e06f3SMoisés Braga Ribeiro        if ($modified) {
8879c0e06f3SMoisés Braga Ribeiro            $head .= ', <abbr class="updated" title="' .
8886f1f13d6SMatthias Schulte                strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) .
8899c0e06f3SMoisés Braga Ribeiro                '</abbr>';
8904cded5e1SGerrit Uitslag        }
891f014bc86SMichael Klier        ptln($head, 8);
8924a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_head"
893f0fda08aSwikidesign
894f0fda08aSwikidesign        // main comment content
8954a0a1bd2Swikidesign        ptln('<div class="comment_body entry-content"' .
896c3413364SGerrit Uitslag            ($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6);
89715cdad37Spierre.spring        echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF;
8984a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_body"
899f0fda08aSwikidesign
900c3413364SGerrit Uitslag        if ($isVisible) {
9011184c36aSwikidesign            ptln('<div class="comment_buttons">', 6);
902f0fda08aSwikidesign
903f0fda08aSwikidesign            // show reply button?
904c3413364SGerrit Uitslag            if ($data['status'] == 1 && !$reply && $comment['show']
905c3413364SGerrit Uitslag                && ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER'))
906c3413364SGerrit Uitslag                && $this->getConf('usethreading')
9074cded5e1SGerrit Uitslag            ) {
908c3413364SGerrit Uitslag                $this->showButton($cid, $this->getLang('btn_reply'), 'reply', true);
9094cded5e1SGerrit Uitslag            }
910f0fda08aSwikidesign
9111184c36aSwikidesign            // show edit, show/hide and delete button?
91283f28d9bSGerrit Uitslag            if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionModerator()) {
913c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_secedit'], 'edit', true);
9141184c36aSwikidesign                $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
915c3413364SGerrit Uitslag                $this->showButton($cid, $label, 'toogle');
916c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_delete'], 'delete');
917f0fda08aSwikidesign            }
9181184c36aSwikidesign            ptln('</div>', 6); // class="comment_buttons"
9191184c36aSwikidesign        }
9201184c36aSwikidesign        ptln('</div>', 4); // class="hentry"
921f0fda08aSwikidesign    }
922f0fda08aSwikidesign
923de7e6f00SGerrit Uitslag    /**
924c3413364SGerrit Uitslag     * If requested by user, show comment form to write a reply
925c3413364SGerrit Uitslag     *
926c3413364SGerrit Uitslag     * @param string $cid current comment id
927c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
928de7e6f00SGerrit Uitslag     */
929c3413364SGerrit Uitslag    protected function showReplyForm($cid, $reply)
930f1c6610eSpierre.spring    {
93131aab30eSGina Haeussge        if ($this->getConf('usethreading') && $reply == $cid) {
9324a0a1bd2Swikidesign            ptln('<div class="comment_replies">', 4);
933c3413364SGerrit Uitslag            $this->showCommentForm('', 'add', $cid);
9344a0a1bd2Swikidesign            ptln('</div>', 4); // class="comment_replies"
935f0fda08aSwikidesign        }
936f0fda08aSwikidesign    }
937f0fda08aSwikidesign
938de7e6f00SGerrit Uitslag    /**
939f7bcfbedSGerrit Uitslag     * Show the replies to the given comment
940c3413364SGerrit Uitslag     *
941c3413364SGerrit Uitslag     * @param string $cid comment id
942c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
943f7bcfbedSGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
944f7bcfbedSGerrit Uitslag     * @param bool $isVisible is marked as visible by reference
945de7e6f00SGerrit Uitslag     */
946c3413364SGerrit Uitslag    protected function showReplies($cid, &$data, $reply, &$isVisible)
947f1c6610eSpierre.spring    {
948f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
949f1c6610eSpierre.spring        if (!count($comment['replies'])) {
950f1c6610eSpierre.spring            return;
951f1c6610eSpierre.spring        }
952c3413364SGerrit Uitslag        ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4);
953c3413364SGerrit Uitslag        $isVisible = ($comment['show'] && $isVisible);
954f1c6610eSpierre.spring        foreach ($comment['replies'] as $rid) {
955c3413364SGerrit Uitslag            $this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible);
956f1c6610eSpierre.spring        }
957f1c6610eSpierre.spring        ptln('</div>', 4);
958f1c6610eSpierre.spring    }
959f1c6610eSpierre.spring
960de7e6f00SGerrit Uitslag    /**
961de7e6f00SGerrit Uitslag     * Is an avatar displayed?
962de7e6f00SGerrit Uitslag     *
963de7e6f00SGerrit Uitslag     * @return bool
964de7e6f00SGerrit Uitslag     */
965c3413364SGerrit Uitslag    protected function useAvatar()
966f1c6610eSpierre.spring    {
967c3413364SGerrit Uitslag        if (is_null($this->useAvatar)) {
968c3413364SGerrit Uitslag            $this->useAvatar = $this->getConf('useavatar')
969c3413364SGerrit Uitslag                && ($this->avatar = $this->loadHelper('avatar', false));
970f1c6610eSpierre.spring        }
971c3413364SGerrit Uitslag        return $this->useAvatar;
972f1c6610eSpierre.spring    }
973f1c6610eSpierre.spring
974de7e6f00SGerrit Uitslag    /**
975de7e6f00SGerrit Uitslag     * Calculate width of indent
976de7e6f00SGerrit Uitslag     *
977de7e6f00SGerrit Uitslag     * @return string
978de7e6f00SGerrit Uitslag     */
979283a3029SGerrit Uitslag    protected function getWidthStyle()
980283a3029SGerrit Uitslag    {
981f1c6610eSpierre.spring        if (is_null($this->style)) {
982c3413364SGerrit Uitslag            if ($this->useAvatar()) {
983f1c6610eSpierre.spring                $this->style = ' style="margin-left: ' . ($this->avatar->getConf('size') + 14) . 'px;"';
984f1c6610eSpierre.spring            } else {
985f1c6610eSpierre.spring                $this->style = ' style="margin-left: 20px;"';
986f1c6610eSpierre.spring            }
987f1c6610eSpierre.spring        }
988f1c6610eSpierre.spring        return $this->style;
989f1c6610eSpierre.spring    }
990f1c6610eSpierre.spring
991f0fda08aSwikidesign    /**
992c3413364SGerrit Uitslag     * Show the button which toggles between show/hide of the entire discussion section
99346178401Slupo49     */
994283a3029SGerrit Uitslag    protected function showDiscussionToggleButton()
995283a3029SGerrit Uitslag    {
99646178401Slupo49        ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">');
997283a3029SGerrit Uitslag        ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button"'
998283a3029SGerrit Uitslag            . 'value="' . $this->getLang('toggle_display') . '">');
99946178401Slupo49        ptln('</div>');
100046178401Slupo49    }
100146178401Slupo49
100246178401Slupo49    /**
1003f0fda08aSwikidesign     * Outputs the comment form
1004f7bcfbedSGerrit Uitslag     *
1005f7bcfbedSGerrit Uitslag     * @param string $raw the existing comment text in case of edit
1006f7bcfbedSGerrit Uitslag     * @param string $act action 'add' or 'save'
1007f7bcfbedSGerrit Uitslag     * @param string|null $cid comment id to be responded to or null
1008f0fda08aSwikidesign     */
1009f7bcfbedSGerrit Uitslag    protected function showCommentForm($raw, $act, $cid = null)
1010283a3029SGerrit Uitslag    {
1011c3413364SGerrit Uitslag        global $lang, $conf, $ID, $INPUT;
1012f0fda08aSwikidesign
1013f0fda08aSwikidesign        // not for unregistered users when guest comments aren't allowed
1014c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('allowguests')) {
10157c8e18ffSGina Haeussge            ?>
10167c8e18ffSGina Haeussge            <div class="comment_form">
10177c8e18ffSGina Haeussge                <?php echo $this->getLang('noguests'); ?>
10187c8e18ffSGina Haeussge            </div>
10197c8e18ffSGina Haeussge            <?php
1020de7e6f00SGerrit Uitslag            return;
10217c8e18ffSGina Haeussge        }
1022f0fda08aSwikidesign
1023c3413364SGerrit Uitslag        // fill $raw with $INPUT->str('text') if it's empty (for failed CAPTCHA check)
1024c3413364SGerrit Uitslag        if (!$raw && $INPUT->str('comment') == 'show') {
1025c3413364SGerrit Uitslag            $raw = $INPUT->str('text');
10264cded5e1SGerrit Uitslag        }
1027f0fda08aSwikidesign        ?>
10285ef1705fSiLoveiDo
1029f0fda08aSwikidesign        <div class="comment_form">
1030283a3029SGerrit Uitslag            <form id="discussion__comment_form" method="post" action="<?php echo script() ?>"
1031283a3029SGerrit Uitslag                  accept-charset="<?php echo $lang['encoding'] ?>">
1032f0fda08aSwikidesign                <div class="no">
1033f0fda08aSwikidesign                    <input type="hidden" name="id" value="<?php echo $ID ?>"/>
103461437513Swikidesign                    <input type="hidden" name="do" value="show"/>
1035f0fda08aSwikidesign                    <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1036530693fbSMichael Klier                    <?php
1037f0fda08aSwikidesign                    // for adding a comment
1038f0fda08aSwikidesign                    if ($act == 'add') {
1039f0fda08aSwikidesign                        ?>
1040f0fda08aSwikidesign                        <input type="hidden" name="reply" value="<?php echo $cid ?>"/>
1041f0fda08aSwikidesign                        <?php
104220c152acSMichael Klier                        // for guest/adminimport: show name, e-mail and subscribe to comments fields
104383f28d9bSGerrit Uitslag                        if (!$INPUT->server->has('REMOTE_USER') or ($this->getConf('adminimport') && $this->helper->isDiscussionModerator())) {
1044f0fda08aSwikidesign                            ?>
104500d916a2SGerrit Uitslag                            <input type="hidden" name="user" value=""/>
1046f0fda08aSwikidesign                            <div class="comment_name">
1047f0fda08aSwikidesign                                <label class="block" for="discussion__comment_name">
1048f0fda08aSwikidesign                                    <span><?php echo $lang['fullname'] ?>:</span>
1049283a3029SGerrit Uitslag                                    <input type="text"
1050283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('name'))) echo ' error' ?>"
1051283a3029SGerrit Uitslag                                           name="name" id="discussion__comment_name" size="50" tabindex="1"
1052283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('name')) ?>"/>
1053f0fda08aSwikidesign                                </label>
1054f0fda08aSwikidesign                            </div>
1055f0fda08aSwikidesign                            <div class="comment_mail">
1056f0fda08aSwikidesign                                <label class="block" for="discussion__comment_mail">
1057f0fda08aSwikidesign                                    <span><?php echo $lang['email'] ?>:</span>
1058283a3029SGerrit Uitslag                                    <input type="text"
1059283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('mail'))) echo ' error' ?>"
1060283a3029SGerrit Uitslag                                           name="mail" id="discussion__comment_mail" size="50" tabindex="2"
1061283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('mail')) ?>"/>
1062f0fda08aSwikidesign                                </label>
1063f0fda08aSwikidesign                            </div>
1064f0fda08aSwikidesign                            <?php
1065f0fda08aSwikidesign                        }
1066f0fda08aSwikidesign
1067f0fda08aSwikidesign                        // allow entering an URL
1068f0fda08aSwikidesign                        if ($this->getConf('urlfield')) {
1069f0fda08aSwikidesign                            ?>
1070f0fda08aSwikidesign                            <div class="comment_url">
1071f0fda08aSwikidesign                                <label class="block" for="discussion__comment_url">
1072f0fda08aSwikidesign                                    <span><?php echo $this->getLang('url') ?>:</span>
1073283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="url" id="discussion__comment_url" size="50"
1074283a3029SGerrit Uitslag                                           tabindex="3" value="<?php echo hsc($INPUT->str('url')) ?>"/>
1075f0fda08aSwikidesign                                </label>
1076f0fda08aSwikidesign                            </div>
1077f0fda08aSwikidesign                            <?php
1078f0fda08aSwikidesign                        }
1079f0fda08aSwikidesign
1080f0fda08aSwikidesign                        // allow entering an address
1081f0fda08aSwikidesign                        if ($this->getConf('addressfield')) {
1082f0fda08aSwikidesign                            ?>
1083f0fda08aSwikidesign                            <div class="comment_address">
1084f0fda08aSwikidesign                                <label class="block" for="discussion__comment_address">
1085f0fda08aSwikidesign                                    <span><?php echo $this->getLang('address') ?>:</span>
1086283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="address" id="discussion__comment_address"
1087283a3029SGerrit Uitslag                                           size="50" tabindex="4" value="<?php echo hsc($INPUT->str('address')) ?>"/>
1088f0fda08aSwikidesign                                </label>
1089f0fda08aSwikidesign                            </div>
1090f0fda08aSwikidesign                            <?php
1091f0fda08aSwikidesign                        }
1092f0fda08aSwikidesign
1093f0fda08aSwikidesign                        // allow setting the comment date
109483f28d9bSGerrit Uitslag                        if ($this->getConf('adminimport') && ($this->helper->isDiscussionModerator())) {
1095f0fda08aSwikidesign                            ?>
1096f0fda08aSwikidesign                            <div class="comment_date">
1097f0fda08aSwikidesign                                <label class="block" for="discussion__comment_date">
1098f0fda08aSwikidesign                                    <span><?php echo $this->getLang('date') ?>:</span>
1099283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="date" id="discussion__comment_date"
1100283a3029SGerrit Uitslag                                           size="50"/>
1101f0fda08aSwikidesign                                </label>
1102f0fda08aSwikidesign                            </div>
1103f0fda08aSwikidesign                            <?php
1104f0fda08aSwikidesign                        }
1105f0fda08aSwikidesign
1106f0fda08aSwikidesign                        // for saving a comment
1107f0fda08aSwikidesign                    } else {
1108f0fda08aSwikidesign                        ?>
1109f0fda08aSwikidesign                        <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1110f0fda08aSwikidesign                        <?php
1111f0fda08aSwikidesign                    }
1112f0fda08aSwikidesign                    ?>
1113f0fda08aSwikidesign                    <div class="comment_text">
1114283a3029SGerrit Uitslag                        <?php echo $this->getLang('entercomment');
1115283a3029SGerrit Uitslag                        echo($this->getConf('wikisyntaxok') ? "" : ":");
111643ade360Slupo49                        if ($this->getConf('wikisyntaxok')) echo '. ' . $this->getLang('wikisyntax') . ':'; ?>
111743ade360Slupo49
111843ade360Slupo49                        <!-- Fix for disable the toolbar when wikisyntaxok is set to false. See discussion's script.jss -->
111943ade360Slupo49                        <?php if ($this->getConf('wikisyntaxok')) { ?>
112064901b8eSGerrit Uitslag                        <div id="discussion__comment_toolbar" class="toolbar group">
112143ade360Slupo49                            <?php } else { ?>
112243ade360Slupo49                            <div id="discussion__comment_toolbar_disabled">
112343ade360Slupo49                                <?php } ?>
11241de52da1SMichael Klier                            </div>
1125283a3029SGerrit Uitslag                            <textarea
1126283a3029SGerrit Uitslag                                class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('text'))) echo ' error' ?>"
1127283a3029SGerrit Uitslag                                name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php
112837e3c825SMichael Klier                                if ($raw) {
112937e3c825SMichael Klier                                    echo formText($raw);
113037e3c825SMichael Klier                                } else {
1131c3413364SGerrit Uitslag                                    echo hsc($INPUT->str('text'));
113237e3c825SMichael Klier                                }
113337e3c825SMichael Klier                                ?></textarea>
1134f0fda08aSwikidesign                        </div>
11350c613822SMichael Hamann
11360c613822SMichael Hamann                        <?php
11370c613822SMichael Hamann                        /** @var helper_plugin_captcha $captcha */
11380c613822SMichael Hamann                        $captcha = $this->loadHelper('captcha', false);
11390c613822SMichael Hamann                        if ($captcha && $captcha->isEnabled()) {
11400c613822SMichael Hamann                            echo $captcha->getHTML();
11410c613822SMichael Hamann                        }
11420c613822SMichael Hamann
11430c613822SMichael Hamann                        /** @var helper_plugin_recaptcha $recaptcha */
11440c613822SMichael Hamann                        $recaptcha = $this->loadHelper('recaptcha', false);
11450c613822SMichael Hamann                        if ($recaptcha && $recaptcha->isEnabled()) {
11460c613822SMichael Hamann                            echo $recaptcha->getHTML();
11470c613822SMichael Hamann                        }
1148e7c760b3Swikidesign                        ?>
11490c613822SMichael Hamann
1150283a3029SGerrit Uitslag                        <input class="button comment_submit" id="discussion__btn_submit" type="submit" name="submit"
1151283a3029SGerrit Uitslag                               accesskey="s" value="<?php echo $lang['btn_save'] ?>"
1152283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_save'] ?> [S]" tabindex="7"/>
1153283a3029SGerrit Uitslag                        <input class="button comment_preview_button" id="discussion__btn_preview" type="button"
1154283a3029SGerrit Uitslag                               name="preview" accesskey="p" value="<?php echo $lang['btn_preview'] ?>"
1155283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_preview'] ?> [P]"/>
11563011fb8bSMichael Klier
1157283a3029SGerrit Uitslag                        <?php if ((!$INPUT->server->has('REMOTE_USER')
1158283a3029SGerrit Uitslag                                || $INPUT->server->has('REMOTE_USER') && !$conf['subscribers'])
1159283a3029SGerrit Uitslag                                && $this->getConf('subscribe')) { ?>
11603011fb8bSMichael Klier                            <div class="comment_subscribe">
1161283a3029SGerrit Uitslag                                <input type="checkbox" id="discussion__comment_subscribe" name="subscribe"
1162283a3029SGerrit Uitslag                                       tabindex="6"/>
11633011fb8bSMichael Klier                                <label class="block" for="discussion__comment_subscribe">
11643011fb8bSMichael Klier                                    <span><?php echo $this->getLang('subscribe') ?></span>
11653011fb8bSMichael Klier                                </label>
11663011fb8bSMichael Klier                            </div>
11673011fb8bSMichael Klier                        <?php } ?>
11683011fb8bSMichael Klier
11693011fb8bSMichael Klier                        <div class="clearer"></div>
1170ed4846efSMichael Klier                        <div id="discussion__comment_preview">&nbsp;</div>
1171f0fda08aSwikidesign                    </div>
1172f0fda08aSwikidesign            </form>
1173f0fda08aSwikidesign        </div>
1174f0fda08aSwikidesign        <?php
1175f0fda08aSwikidesign    }
1176f0fda08aSwikidesign
1177f0fda08aSwikidesign    /**
1178c3413364SGerrit Uitslag     * Action button below a comment
1179de7e6f00SGerrit Uitslag     *
1180c3413364SGerrit Uitslag     * @param string $cid comment id
1181c3413364SGerrit Uitslag     * @param string $label translated label
1182c3413364SGerrit Uitslag     * @param string $act action
1183c3413364SGerrit Uitslag     * @param bool $jump whether to scroll to the commentform
1184f0fda08aSwikidesign     */
1185283a3029SGerrit Uitslag    protected function showButton($cid, $label, $act, $jump = false)
1186283a3029SGerrit Uitslag    {
1187f0fda08aSwikidesign        global $ID;
11885ef1705fSiLoveiDo
11891e46d176Swikidesign        $anchor = ($jump ? '#discussion__comment_form' : '');
1190f0fda08aSwikidesign
11910980d234SGerrit Uitslag        $submitClass = '';
11920980d234SGerrit Uitslag        if($act === 'delete') {
11930980d234SGerrit Uitslag            $submitClass = ' dcs_confirmdelete';
11940980d234SGerrit Uitslag        }
1195f0fda08aSwikidesign        ?>
11966d1f4f20SGina Haeussge        <form class="button discussion__<?php echo $act ?>" method="get" action="<?php echo script() . $anchor ?>">
1197f0fda08aSwikidesign            <div class="no">
1198f0fda08aSwikidesign                <input type="hidden" name="id" value="<?php echo $ID ?>"/>
119961437513Swikidesign                <input type="hidden" name="do" value="show"/>
1200f0fda08aSwikidesign                <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1201f0fda08aSwikidesign                <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
12020980d234SGerrit Uitslag                <input type="submit" value="<?php echo $label ?>" class="button<?php echo $submitClass ?>" title="<?php echo $label ?>"/>
1203f0fda08aSwikidesign            </div>
1204f0fda08aSwikidesign        </form>
1205f0fda08aSwikidesign        <?php
1206f0fda08aSwikidesign    }
1207f0fda08aSwikidesign
1208f0fda08aSwikidesign    /**
1209f0fda08aSwikidesign     * Adds an entry to the comments changelog
1210f0fda08aSwikidesign     *
1211de7e6f00SGerrit Uitslag     * @param int $date
1212de7e6f00SGerrit Uitslag     * @param string $id page id
1213c3413364SGerrit Uitslag     * @param string $type create/edit/delete/show/hide comment 'cc', 'ec', 'dc', 'sc', 'hc'
1214de7e6f00SGerrit Uitslag     * @param string $summary
1215de7e6f00SGerrit Uitslag     * @param string $extra
1216283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1217283a3029SGerrit Uitslag     *
1218283a3029SGerrit Uitslag     * @author Esther Brunner <wikidesign@gmail.com>
1219f0fda08aSwikidesign     */
1220283a3029SGerrit Uitslag    protected function addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '')
1221283a3029SGerrit Uitslag    {
1222c3413364SGerrit Uitslag        global $conf, $INPUT;
1223f0fda08aSwikidesign
1224f0fda08aSwikidesign        $changelog = $conf['metadir'] . '/_comments.changes';
1225f0fda08aSwikidesign
12264cded5e1SGerrit Uitslag        //use current time if none supplied
12274cded5e1SGerrit Uitslag        if (!$date) {
12284cded5e1SGerrit Uitslag            $date = time();
12294cded5e1SGerrit Uitslag        }
1230c3413364SGerrit Uitslag        $remote = $INPUT->server->str('REMOTE_ADDR');
1231c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
1232f0fda08aSwikidesign
1233c3413364SGerrit Uitslag        $strip = ["\t", "\n"];
1234c3413364SGerrit Uitslag        $logline = [
1235f0fda08aSwikidesign            'date' => $date,
1236f0fda08aSwikidesign            'ip' => $remote,
1237f0fda08aSwikidesign            'type' => str_replace($strip, '', $type),
1238f0fda08aSwikidesign            'id' => $id,
1239f0fda08aSwikidesign            'user' => $user,
1240f0fda08aSwikidesign            'sum' => str_replace($strip, '', $summary),
1241f0fda08aSwikidesign            'extra' => str_replace($strip, '', $extra)
1242c3413364SGerrit Uitslag        ];
1243f0fda08aSwikidesign
1244f0fda08aSwikidesign        // add changelog line
1245f0fda08aSwikidesign        $logline = implode("\t", $logline) . "\n";
1246f0fda08aSwikidesign        io_saveFile($changelog, $logline, true); //global changelog cache
1247c3413364SGerrit Uitslag        $this->trimRecentCommentsLog($changelog);
124877a22ba2Swikidesign
124977a22ba2Swikidesign        // tell the indexer to re-index the page
125077a22ba2Swikidesign        @unlink(metaFN($id, '.indexed'));
1251f0fda08aSwikidesign    }
1252f0fda08aSwikidesign
1253f0fda08aSwikidesign    /**
1254f0fda08aSwikidesign     * Trims the recent comments cache to the last $conf['changes_days'] recent
1255f0fda08aSwikidesign     * changes or $conf['recent'] items, which ever is larger.
1256f0fda08aSwikidesign     * The trimming is only done once a day.
1257f0fda08aSwikidesign     *
1258de7e6f00SGerrit Uitslag     * @param string $changelog file path
1259de7e6f00SGerrit Uitslag     * @return bool
1260283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1261283a3029SGerrit Uitslag     *
1262f0fda08aSwikidesign     */
1263283a3029SGerrit Uitslag    protected function trimRecentCommentsLog($changelog)
1264283a3029SGerrit Uitslag    {
1265f0fda08aSwikidesign        global $conf;
1266f0fda08aSwikidesign
1267283a3029SGerrit Uitslag        if (@file_exists($changelog)
1268283a3029SGerrit Uitslag            && (filectime($changelog) + 86400) < time()
1269283a3029SGerrit Uitslag            && !@file_exists($changelog . '_tmp')
12704cded5e1SGerrit Uitslag        ) {
1271f0fda08aSwikidesign
1272f0fda08aSwikidesign            io_lock($changelog);
1273f0fda08aSwikidesign            $lines = file($changelog);
1274f0fda08aSwikidesign            if (count($lines) < $conf['recent']) {
1275f0fda08aSwikidesign                // nothing to trim
1276f0fda08aSwikidesign                io_unlock($changelog);
1277f0fda08aSwikidesign                return true;
1278f0fda08aSwikidesign            }
1279f0fda08aSwikidesign
1280283a3029SGerrit Uitslag            // presave tmp as 2nd lock
1281283a3029SGerrit Uitslag            io_saveFile($changelog . '_tmp', '');
1282f0fda08aSwikidesign            $trim_time = time() - $conf['recent_days'] * 86400;
1283c3413364SGerrit Uitslag            $out_lines = [];
1284f0fda08aSwikidesign
1285e49085a2SMichael Klier            $num = count($lines);
1286e49085a2SMichael Klier            for ($i = 0; $i < $num; $i++) {
1287f0fda08aSwikidesign                $log = parseChangelogLine($lines[$i]);
1288f0fda08aSwikidesign                if ($log === false) continue;                      // discard junk
1289f0fda08aSwikidesign                if ($log['date'] < $trim_time) {
1290f0fda08aSwikidesign                    $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions)
1291f0fda08aSwikidesign                } else {
1292f0fda08aSwikidesign                    $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines
1293f0fda08aSwikidesign                }
1294f0fda08aSwikidesign            }
1295f0fda08aSwikidesign
1296f0fda08aSwikidesign            // sort the final result, it shouldn't be necessary,
1297f0fda08aSwikidesign            // however the extra robustness in making the changelog cache self-correcting is worth it
1298f0fda08aSwikidesign            ksort($out_lines);
1299f0fda08aSwikidesign            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
1300f0fda08aSwikidesign            if ($extra > 0) {
1301f0fda08aSwikidesign                ksort($old_lines);
1302f0fda08aSwikidesign                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
1303f0fda08aSwikidesign            }
1304f0fda08aSwikidesign
1305f0fda08aSwikidesign            // save trimmed changelog
1306f0fda08aSwikidesign            io_saveFile($changelog . '_tmp', implode('', $out_lines));
1307f0fda08aSwikidesign            @unlink($changelog);
1308f0fda08aSwikidesign            if (!rename($changelog . '_tmp', $changelog)) {
1309f0fda08aSwikidesign                // rename failed so try another way...
1310f0fda08aSwikidesign                io_unlock($changelog);
1311f0fda08aSwikidesign                io_saveFile($changelog, implode('', $out_lines));
1312f0fda08aSwikidesign                @unlink($changelog . '_tmp');
1313f0fda08aSwikidesign            } else {
1314f0fda08aSwikidesign                io_unlock($changelog);
1315f0fda08aSwikidesign            }
1316f0fda08aSwikidesign            return true;
1317f0fda08aSwikidesign        }
1318de7e6f00SGerrit Uitslag        return true;
1319f0fda08aSwikidesign    }
1320f0fda08aSwikidesign
1321f0fda08aSwikidesign    /**
1322f0fda08aSwikidesign     * Sends a notify mail on new comment
1323f0fda08aSwikidesign     *
1324f0fda08aSwikidesign     * @param array $comment data array of the new comment
1325c3413364SGerrit Uitslag     * @param array $subscribers data of the subscribers by reference
1326f0fda08aSwikidesign     *
1327f0fda08aSwikidesign     * @author Andreas Gohr <andi@splitbrain.org>
1328f0fda08aSwikidesign     * @author Esther Brunner <wikidesign@gmail.com>
1329f0fda08aSwikidesign     */
1330283a3029SGerrit Uitslag    protected function notify($comment, &$subscribers)
1331283a3029SGerrit Uitslag    {
1332c3413364SGerrit Uitslag        global $conf, $ID, $INPUT, $auth;
1333f0fda08aSwikidesign
13349881d835SMichael Klier        $notify_text = io_readfile($this->localfn('subscribermail'));
13359881d835SMichael Klier        $confirm_text = io_readfile($this->localfn('confirmsubscribe'));
13369881d835SMichael Klier        $subject_notify = '[' . $conf['title'] . '] ' . $this->getLang('mail_newcomment');
13379881d835SMichael Klier        $subject_subscribe = '[' . $conf['title'] . '] ' . $this->getLang('subscribe');
1338f0fda08aSwikidesign
1339451c1100SMichael Hamann        $mailer = new Mailer();
1340c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER')) {
1341451c1100SMichael Hamann            $mailer->from($conf['mailfromnobody']);
1342f4a5ed1cSMatthias Schulte        }
1343f4a5ed1cSMatthias Schulte
1344c3413364SGerrit Uitslag        $replace = [
13453c4953e9SMichael Hamann            'PAGE' => $ID,
13463c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13473c4953e9SMichael Hamann            'DATE' => dformat($comment['date']['created'], $conf['dformat']),
13483c4953e9SMichael Hamann            'NAME' => $comment['user']['name'],
13493c4953e9SMichael Hamann            'TEXT' => $comment['raw'],
13503c4953e9SMichael Hamann            'COMMENTURL' => wl($ID, '', true) . '#comment_' . $comment['cid'],
135106644a74SMichael Hamann            'UNSUBSCRIBE' => wl($ID, 'do=subscribe', true, '&'),
13523c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1353c3413364SGerrit Uitslag        ];
1354451c1100SMichael Hamann
1355c3413364SGerrit Uitslag        $confirm_replace = [
13563c4953e9SMichael Hamann            'PAGE' => $ID,
13573c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13583c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1359c3413364SGerrit Uitslag        ];
1360451c1100SMichael Hamann
1361451c1100SMichael Hamann
1362451c1100SMichael Hamann        $mailer->subject($subject_notify);
1363451c1100SMichael Hamann        $mailer->setBody($notify_text, $replace);
1364451c1100SMichael Hamann
1365f4a5ed1cSMatthias Schulte        // send mail to notify address
1366f4a5ed1cSMatthias Schulte        if ($conf['notify']) {
1367451c1100SMichael Hamann            $mailer->bcc($conf['notify']);
1368451c1100SMichael Hamann            $mailer->send();
1369f4a5ed1cSMatthias Schulte        }
1370f4a5ed1cSMatthias Schulte
1371ca785d71SMichael Hamann        // send email to moderators
1372ca785d71SMichael Hamann        if ($this->getConf('moderatorsnotify')) {
1373c3413364SGerrit Uitslag            $moderatorgrpsString = trim($this->getConf('moderatorgroups'));
1374c3413364SGerrit Uitslag            if (!empty($moderatorgrpsString)) {
1375ca785d71SMichael Hamann                // create a clean mods list
1376c3413364SGerrit Uitslag                $moderatorgroups = explode(',', $moderatorgrpsString);
1377c3413364SGerrit Uitslag                $moderatorgroups = array_map('trim', $moderatorgroups);
1378c3413364SGerrit Uitslag                $moderatorgroups = array_unique($moderatorgroups);
1379c3413364SGerrit Uitslag                $moderatorgroups = array_filter($moderatorgroups);
1380ca785d71SMichael Hamann                // search for moderators users
1381c3413364SGerrit Uitslag                foreach ($moderatorgroups as $moderatorgroup) {
1382c3413364SGerrit Uitslag                    if (!$auth->isCaseSensitive()) {
1383c3413364SGerrit Uitslag                        $moderatorgroup = PhpString::strtolower($moderatorgroup);
1384c3413364SGerrit Uitslag                    }
1385ca785d71SMichael Hamann                    // create a clean mailing list
1386c3413364SGerrit Uitslag                    $bccs = [];
1387c3413364SGerrit Uitslag                    if ($moderatorgroup[0] == '@') {
1388c3413364SGerrit Uitslag                        foreach ($auth->retrieveUsers(0, 0, ['grps' => $auth->cleanGroup(substr($moderatorgroup, 1))]) as $user) {
1389ca785d71SMichael Hamann                            if (!empty($user['mail'])) {
1390c3413364SGerrit Uitslag                                $bccs[] = $user['mail'];
1391ca785d71SMichael Hamann                            }
1392ca785d71SMichael Hamann                        }
1393ca785d71SMichael Hamann                    } else {
1394c3413364SGerrit Uitslag                        //it is an user
1395c3413364SGerrit Uitslag                        $userdata = $auth->getUserData($auth->cleanUser($moderatorgroup));
1396ca785d71SMichael Hamann                        if (!empty($userdata['mail'])) {
1397c3413364SGerrit Uitslag                            $bccs[] = $userdata['mail'];
1398ca785d71SMichael Hamann                        }
1399ca785d71SMichael Hamann                    }
1400c3413364SGerrit Uitslag                    $bccs = array_unique($bccs);
1401ca785d71SMichael Hamann                    // notify the users
1402c3413364SGerrit Uitslag                    $mailer->bcc(implode(',', $bccs));
1403ca785d71SMichael Hamann                    $mailer->send();
1404ca785d71SMichael Hamann                }
1405ca785d71SMichael Hamann            }
1406ca785d71SMichael Hamann        }
1407ca785d71SMichael Hamann
1408f4a5ed1cSMatthias Schulte        // notify page subscribers
1409451c1100SMichael Hamann        if (actionOK('subscribe')) {
1410c3413364SGerrit Uitslag            $data = ['id' => $ID, 'addresslist' => '', 'self' => false];
1411c3413364SGerrit Uitslag            //FIXME default callback, needed to mentioned it again?
1412c3413364SGerrit Uitslag            Event::createAndTrigger(
1413451c1100SMichael Hamann                'COMMON_NOTIFY_ADDRESSLIST', $data,
1414c3413364SGerrit Uitslag                [new SubscriberManager(), 'notifyAddresses']
1415451c1100SMichael Hamann            );
1416c3413364SGerrit Uitslag
1417451c1100SMichael Hamann            $to = $data['addresslist'];
1418451c1100SMichael Hamann            if (!empty($to)) {
1419451c1100SMichael Hamann                $mailer->bcc($to);
1420451c1100SMichael Hamann                $mailer->send();
1421451c1100SMichael Hamann            }
14223011fb8bSMichael Klier        }
1423f0fda08aSwikidesign
14243011fb8bSMichael Klier        // notify comment subscribers
14253011fb8bSMichael Klier        if (!empty($subscribers)) {
14263011fb8bSMichael Klier
14279881d835SMichael Klier            foreach ($subscribers as $mail => $data) {
1428451c1100SMichael Hamann                $mailer->bcc($mail);
14299881d835SMichael Klier                if ($data['active']) {
14303c4953e9SMichael Hamann                    $replace['UNSUBSCRIBE'] = wl($ID, 'do=discussion_unsubscribe&hash=' . $data['hash'], true, '&');
14313011fb8bSMichael Klier
1432451c1100SMichael Hamann                    $mailer->subject($subject_notify);
1433451c1100SMichael Hamann                    $mailer->setBody($notify_text, $replace);
1434451c1100SMichael Hamann                    $mailer->send();
1435c3413364SGerrit Uitslag                } elseif (!$data['confirmsent']) {
14363c4953e9SMichael Hamann                    $confirm_replace['SUBSCRIBE'] = wl($ID, 'do=discussion_confirmsubscribe&hash=' . $data['hash'], true, '&');
14379881d835SMichael Klier
1438451c1100SMichael Hamann                    $mailer->subject($subject_subscribe);
1439451c1100SMichael Hamann                    $mailer->setBody($confirm_text, $confirm_replace);
1440451c1100SMichael Hamann                    $mailer->send();
14419881d835SMichael Klier                    $subscribers[$mail]['confirmsent'] = true;
14429881d835SMichael Klier                }
14433011fb8bSMichael Klier            }
14443011fb8bSMichael Klier        }
1445f0fda08aSwikidesign    }
1446f0fda08aSwikidesign
1447f0fda08aSwikidesign    /**
1448f0fda08aSwikidesign     * Counts the number of visible comments
1449de7e6f00SGerrit Uitslag     *
1450c3413364SGerrit Uitslag     * @param array $data array with all comments
1451de7e6f00SGerrit Uitslag     * @return int
1452f0fda08aSwikidesign     */
1453283a3029SGerrit Uitslag    protected function countVisibleComments($data)
1454283a3029SGerrit Uitslag    {
1455f0fda08aSwikidesign        $number = 0;
1456de7e6f00SGerrit Uitslag        foreach ($data['comments'] as $comment) {
1457f0fda08aSwikidesign            if ($comment['parent']) continue;
1458f0fda08aSwikidesign            if (!$comment['show']) continue;
1459c3413364SGerrit Uitslag
1460f0fda08aSwikidesign            $number++;
1461f0fda08aSwikidesign            $rids = $comment['replies'];
14624cded5e1SGerrit Uitslag            if (count($rids)) {
1463c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14644cded5e1SGerrit Uitslag            }
1465f0fda08aSwikidesign        }
1466f0fda08aSwikidesign        return $number;
1467f0fda08aSwikidesign    }
1468f0fda08aSwikidesign
1469de7e6f00SGerrit Uitslag    /**
1470c3413364SGerrit Uitslag     * Count visible replies on the comments
1471c3413364SGerrit Uitslag     *
1472de7e6f00SGerrit Uitslag     * @param array $data
1473de7e6f00SGerrit Uitslag     * @param array $rids
1474c3413364SGerrit Uitslag     * @return int counted replies
1475de7e6f00SGerrit Uitslag     */
1476283a3029SGerrit Uitslag    protected function countVisibleReplies(&$data, $rids)
1477283a3029SGerrit Uitslag    {
1478f0fda08aSwikidesign        $number = 0;
1479f0fda08aSwikidesign        foreach ($rids as $rid) {
14802ee3dca3Swikidesign            if (!isset($data['comments'][$rid])) continue; // reply was removed
1481f0fda08aSwikidesign            if (!$data['comments'][$rid]['show']) continue;
1482c3413364SGerrit Uitslag
1483f0fda08aSwikidesign            $number++;
1484f0fda08aSwikidesign            $rids = $data['comments'][$rid]['replies'];
14854cded5e1SGerrit Uitslag            if (count($rids)) {
1486c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14874cded5e1SGerrit Uitslag            }
1488f0fda08aSwikidesign        }
1489f0fda08aSwikidesign        return $number;
1490f0fda08aSwikidesign    }
1491f0fda08aSwikidesign
1492f0fda08aSwikidesign    /**
1493c3413364SGerrit Uitslag     * Renders the raw comment (wiki)text to html
1494de7e6f00SGerrit Uitslag     *
1495c3413364SGerrit Uitslag     * @param string $raw comment text
1496de7e6f00SGerrit Uitslag     * @return null|string
1497f0fda08aSwikidesign     */
1498283a3029SGerrit Uitslag    protected function renderComment($raw)
1499283a3029SGerrit Uitslag    {
1500f0fda08aSwikidesign        if ($this->getConf('wikisyntaxok')) {
1501efccf6b0SJeffrey Bergamini            // Note the warning for render_text:
1502efccf6b0SJeffrey Bergamini            //   "very ineffecient for small pieces of data - try not to use"
1503efccf6b0SJeffrey Bergamini            // in dokuwiki/inc/plugin.php
1504efccf6b0SJeffrey Bergamini            $xhtml = $this->render_text($raw);
1505f0fda08aSwikidesign        } else { // wiki syntax not allowed -> just encode special chars
150687bb4e97SMichael Klier            $xhtml = hsc(trim($raw));
150787bb4e97SMichael Klier            $xhtml = str_replace("\n", '<br />', $xhtml);
1508f0fda08aSwikidesign        }
1509f0fda08aSwikidesign        return $xhtml;
1510f0fda08aSwikidesign    }
1511f0fda08aSwikidesign
1512f0fda08aSwikidesign    /**
1513479dd10fSwikidesign     * Finds out whether there is a discussion section for the current page
1514de7e6f00SGerrit Uitslag     *
151576fdd2cdSGerrit Uitslag     * @param string $title set to title from metadata or empty string
151676fdd2cdSGerrit Uitslag     * @return bool discussion section is shown?
1517479dd10fSwikidesign     */
1518283a3029SGerrit Uitslag    protected function hasDiscussion(&$title)
1519283a3029SGerrit Uitslag    {
1520b2ac3b3bSwikidesign        global $ID;
15214a0a1bd2Swikidesign
1522c3413364SGerrit Uitslag        $file = metaFN($ID, '.comments');
1523479dd10fSwikidesign
1524c3413364SGerrit Uitslag        if (!@file_exists($file)) {
1525f0bcde18SGerrit Uitslag            if ($this->isDiscussionEnabled()) {
15262b18adb9SMichael Klier                return true;
15272b18adb9SMichael Klier            } else {
15282b18adb9SMichael Klier                return false;
15292b18adb9SMichael Klier            }
1530479dd10fSwikidesign        }
1531479dd10fSwikidesign
153276fdd2cdSGerrit Uitslag        $data = unserialize(io_readFile($file, false));
1533479dd10fSwikidesign
153476fdd2cdSGerrit Uitslag        $title = $data['title'] ?? '';
153576fdd2cdSGerrit Uitslag
153676fdd2cdSGerrit Uitslag        $num = $data['number'];
153776fdd2cdSGerrit Uitslag        if (!$data['status'] || ($data['status'] == 2 && $num == 0)) {
1538c3413364SGerrit Uitslag            //disabled, or closed and no comments
1539c3413364SGerrit Uitslag            return false;
1540c3413364SGerrit Uitslag        } else {
1541c3413364SGerrit Uitslag            return true;
1542c3413364SGerrit Uitslag        }
1543479dd10fSwikidesign    }
1544479dd10fSwikidesign
1545479dd10fSwikidesign    /**
1546e7c760b3Swikidesign     * Creates a new thread page
1547de7e6f00SGerrit Uitslag     *
1548de7e6f00SGerrit Uitslag     * @return string
1549e7c760b3Swikidesign     */
1550283a3029SGerrit Uitslag    protected function newThread()
1551283a3029SGerrit Uitslag    {
1552c3413364SGerrit Uitslag        global $ID, $INFO, $INPUT;
1553f0fda08aSwikidesign
1554c3413364SGerrit Uitslag        $ns = cleanID($INPUT->str('ns'));
1555c3413364SGerrit Uitslag        $title = str_replace(':', '', $INPUT->str('title'));
15562e80cd5fSwikidesign        $back = $ID;
15572e80cd5fSwikidesign        $ID = ($ns ? $ns . ':' : '') . cleanID($title);
15582e80cd5fSwikidesign        $INFO = pageinfo();
1559f0fda08aSwikidesign
1560f0fda08aSwikidesign        // check if we are allowed to create this file
15612e80cd5fSwikidesign        if ($INFO['perm'] >= AUTH_CREATE) {
1562f0fda08aSwikidesign
1563f0fda08aSwikidesign            //check if locked by anyone - if not lock for my self
15644cded5e1SGerrit Uitslag            if ($INFO['locked']) {
15654cded5e1SGerrit Uitslag                return 'locked';
15664cded5e1SGerrit Uitslag            } else {
15674cded5e1SGerrit Uitslag                lock($ID);
15684cded5e1SGerrit Uitslag            }
1569f0fda08aSwikidesign
1570f0fda08aSwikidesign            // prepare the new thread file with default stuff
15712e80cd5fSwikidesign            if (!@file_exists($INFO['filepath'])) {
1572f0fda08aSwikidesign                global $TEXT;
1573f0fda08aSwikidesign
1574c3413364SGerrit Uitslag                $TEXT = pageTemplate(($ns ? $ns . ':' : '') . $title);
15751433886fSwikidesign                if (!$TEXT) {
1576c3413364SGerrit Uitslag                    $data = ['id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back];
1577c3413364SGerrit Uitslag                    $TEXT = $this->pageTemplate($data);
15782e80cd5fSwikidesign                }
15792e80cd5fSwikidesign                return 'preview';
1580f0fda08aSwikidesign            } else {
15812e80cd5fSwikidesign                return 'edit';
1582f0fda08aSwikidesign            }
1583f0fda08aSwikidesign        } else {
15842e80cd5fSwikidesign            return 'show';
1585f0fda08aSwikidesign        }
1586f0fda08aSwikidesign    }
1587f0fda08aSwikidesign
1588e7c760b3Swikidesign    /**
158961437513Swikidesign     * Adapted version of pageTemplate() function
1590de7e6f00SGerrit Uitslag     *
1591de7e6f00SGerrit Uitslag     * @param array $data
1592de7e6f00SGerrit Uitslag     * @return string
159361437513Swikidesign     */
1594283a3029SGerrit Uitslag    protected function pageTemplate($data)
1595283a3029SGerrit Uitslag    {
1596c3413364SGerrit Uitslag        global $conf, $INFO, $INPUT;
159761437513Swikidesign
159861437513Swikidesign        $id = $data['id'];
1599c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
160061437513Swikidesign        $tpl = io_readFile(DOKU_PLUGIN . 'discussion/_template.txt');
160161437513Swikidesign
160261437513Swikidesign        // standard replacements
1603c3413364SGerrit Uitslag        $replace = [
160461437513Swikidesign            '@NS@' => $data['ns'],
160561437513Swikidesign            '@PAGE@' => strtr(noNS($id), '_', ' '),
160661437513Swikidesign            '@USER@' => $user,
160761437513Swikidesign            '@NAME@' => $INFO['userinfo']['name'],
160861437513Swikidesign            '@MAIL@' => $INFO['userinfo']['mail'],
1609d5530824SMichael Hamann            '@DATE@' => dformat(time(), $conf['dformat']),
1610c3413364SGerrit Uitslag        ];
161161437513Swikidesign
161261437513Swikidesign        // additional replacements
161361437513Swikidesign        $replace['@BACK@'] = $data['back'];
161461437513Swikidesign        $replace['@TITLE@'] = $data['title'];
161561437513Swikidesign
161661437513Swikidesign        // avatar if useavatar and avatar plugin available
1617c3413364SGerrit Uitslag        if ($this->getConf('useavatar') && !plugin_isdisabled('avatar')) {
161861437513Swikidesign            $replace['@AVATAR@'] = '{{avatar>' . $user . ' }} ';
161961437513Swikidesign        } else {
162061437513Swikidesign            $replace['@AVATAR@'] = '';
162161437513Swikidesign        }
162261437513Swikidesign
162361437513Swikidesign        // tag if tag plugin is available
1624c3413364SGerrit Uitslag        if (!plugin_isdisabled('tag')) {
162561437513Swikidesign            $replace['@TAG@'] = "\n\n{{tag>}}";
162661437513Swikidesign        } else {
162761437513Swikidesign            $replace['@TAG@'] = '';
162861437513Swikidesign        }
162961437513Swikidesign
1630c3413364SGerrit Uitslag        // perform the replacements in tpl
1631c3413364SGerrit Uitslag        return str_replace(array_keys($replace), array_values($replace), $tpl);
163261437513Swikidesign    }
163361437513Swikidesign
163461437513Swikidesign    /**
1635f7bcfbedSGerrit Uitslag     * Checks if the CAPTCHA string submitted is valid, modifies action if needed
1636e7c760b3Swikidesign     */
1637283a3029SGerrit Uitslag    protected function captchaCheck()
1638283a3029SGerrit Uitslag    {
1639c3413364SGerrit Uitslag        global $INPUT;
164096bc68a7SMichael Hamann        /** @var helper_plugin_captcha $captcha */
1641c3413364SGerrit Uitslag        if (!$captcha = $this->loadHelper('captcha', false)) {
1642c3413364SGerrit Uitslag            // CAPTCHA is disabled or not available
1643c3413364SGerrit Uitslag            return;
1644c3413364SGerrit Uitslag        }
1645e7c760b3Swikidesign
1646d578a059SMichael Hamann        if ($captcha->isEnabled() && !$captcha->check()) {
1647c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1648c3413364SGerrit Uitslag                $INPUT->set('comment', 'edit');
1649c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1650c3413364SGerrit Uitslag                $INPUT->set('comment', 'show');
16514cded5e1SGerrit Uitslag            }
1652e7c760b3Swikidesign        }
1653e7c760b3Swikidesign    }
1654e7c760b3Swikidesign
1655a1ca9e44Swikidesign    /**
1656f7bcfbedSGerrit Uitslag     * checks if the submitted reCAPTCHA string is valid, modifies action if needed
1657bd6dc08eSAdrian Schlegel     *
1658bd6dc08eSAdrian Schlegel     * @author Adrian Schlegel <adrian@liip.ch>
1659bd6dc08eSAdrian Schlegel     */
1660283a3029SGerrit Uitslag    protected function recaptchaCheck()
1661283a3029SGerrit Uitslag    {
1662c3413364SGerrit Uitslag        global $INPUT;
1663c3413364SGerrit Uitslag        /** @var helper_plugin_recaptcha $recaptcha */
1664c3413364SGerrit Uitslag        if (!$recaptcha = plugin_load('helper', 'recaptcha'))
1665bd6dc08eSAdrian Schlegel            return; // reCAPTCHA is disabled or not available
1666bd6dc08eSAdrian Schlegel
1667bd6dc08eSAdrian Schlegel        // do nothing if logged in user and no reCAPTCHA required
1668c3413364SGerrit Uitslag        if (!$recaptcha->getConf('forusers') && $INPUT->server->has('REMOTE_USER')) return;
1669bd6dc08eSAdrian Schlegel
1670c3413364SGerrit Uitslag        $response = $recaptcha->check();
1671c3413364SGerrit Uitslag        if (!$response->is_valid) {
1672bd6dc08eSAdrian Schlegel            msg($recaptcha->getLang('testfailed'), -1);
1673c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1674c3413364SGerrit Uitslag                $INPUT->str('comment', 'edit');
1675c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1676c3413364SGerrit Uitslag                $INPUT->str('comment', 'show');
16774cded5e1SGerrit Uitslag            }
1678bd6dc08eSAdrian Schlegel        }
1679bd6dc08eSAdrian Schlegel    }
1680bd6dc08eSAdrian Schlegel
1681bd6dc08eSAdrian Schlegel    /**
1682ac818938SMichael Hamann     * Add discussion plugin version to the indexer version
1683ac818938SMichael Hamann     * This means that all pages will be indexed again in order to add the comments
1684ac818938SMichael Hamann     * to the index whenever there has been a change that concerns the index content.
1685de7e6f00SGerrit Uitslag     *
1686de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1687ac818938SMichael Hamann     */
1688283a3029SGerrit Uitslag    public function addIndexVersion(Doku_Event $event)
1689283a3029SGerrit Uitslag    {
1690ac818938SMichael Hamann        $event->data['discussion'] = '0.1';
1691ac818938SMichael Hamann    }
1692ac818938SMichael Hamann
1693ac818938SMichael Hamann    /**
1694a1ca9e44Swikidesign     * Adds the comments to the index
1695de7e6f00SGerrit Uitslag     *
1696de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1697c3413364SGerrit Uitslag     * @param array $param with
1698c3413364SGerrit Uitslag     *  'id' => string 'page'/'id' for respectively INDEXER_PAGE_ADD and FULLTEXT_SNIPPET_CREATE event
1699c3413364SGerrit Uitslag     *  'text' => string 'body'/'text'
1700a1ca9e44Swikidesign     */
1701283a3029SGerrit Uitslag    public function addCommentsToIndex(Doku_Event $event, $param)
1702283a3029SGerrit Uitslag    {
1703a1ca9e44Swikidesign        // get .comments meta file name
170410b5d61eSMichael Hamann        $file = metaFN($event->data[$param['id']], '.comments');
1705a1ca9e44Swikidesign
170696b3951aSMichael Hamann        if (!@file_exists($file)) return;
170796b3951aSMichael Hamann        $data = unserialize(io_readFile($file, false));
1708c3413364SGerrit Uitslag
1709c3413364SGerrit Uitslag        // comments are turned off or no comments available to index
1710c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
1711a1ca9e44Swikidesign
1712a1ca9e44Swikidesign        // now add the comments
1713a1ca9e44Swikidesign        if (isset($data['comments'])) {
1714a1ca9e44Swikidesign            foreach ($data['comments'] as $key => $value) {
1715c3413364SGerrit Uitslag                $event->data[$param['text']] .= DOKU_LF . $this->addCommentWords($key, $data);
1716a1ca9e44Swikidesign            }
1717a1ca9e44Swikidesign        }
1718a1ca9e44Swikidesign    }
1719a1ca9e44Swikidesign
1720c3413364SGerrit Uitslag    /**
1721c3413364SGerrit Uitslag     * Checks if the phrase occurs in the comments and return event result true if matching
1722c3413364SGerrit Uitslag     *
1723c3413364SGerrit Uitslag     * @param Doku_Event $event
1724c3413364SGerrit Uitslag     */
1725283a3029SGerrit Uitslag    public function fulltextPhraseMatchInComments(Doku_Event $event)
1726283a3029SGerrit Uitslag    {
172710b5d61eSMichael Hamann        if ($event->result === true) return;
172810b5d61eSMichael Hamann
172910b5d61eSMichael Hamann        // get .comments meta file name
173010b5d61eSMichael Hamann        $file = metaFN($event->data['id'], '.comments');
173110b5d61eSMichael Hamann
173210b5d61eSMichael Hamann        if (!@file_exists($file)) return;
173310b5d61eSMichael Hamann        $data = unserialize(io_readFile($file, false));
1734c3413364SGerrit Uitslag
1735c3413364SGerrit Uitslag        // comments are turned off or no comments available to match
1736c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
173710b5d61eSMichael Hamann
173810b5d61eSMichael Hamann        $matched = false;
173910b5d61eSMichael Hamann
174010b5d61eSMichael Hamann        // now add the comments
174110b5d61eSMichael Hamann        if (isset($data['comments'])) {
1742c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
1743c3413364SGerrit Uitslag                $matched = $this->phraseMatchInComment($event->data['phrase'], $cid, $data);
174410b5d61eSMichael Hamann                if ($matched) break;
174510b5d61eSMichael Hamann            }
174610b5d61eSMichael Hamann        }
174710b5d61eSMichael Hamann
1748c3413364SGerrit Uitslag        if ($matched) {
174910b5d61eSMichael Hamann            $event->result = true;
175010b5d61eSMichael Hamann        }
1751c3413364SGerrit Uitslag    }
175210b5d61eSMichael Hamann
1753c3413364SGerrit Uitslag    /**
1754c3413364SGerrit Uitslag     * Match the phrase in the comment and its replies
1755c3413364SGerrit Uitslag     *
1756c3413364SGerrit Uitslag     * @param string $phrase phrase to search
1757c3413364SGerrit Uitslag     * @param string $cid comment id
1758c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1759c3413364SGerrit Uitslag     * @param string $parent cid of parent
1760c3413364SGerrit Uitslag     * @return bool if match true, otherwise false
1761c3413364SGerrit Uitslag     */
1762283a3029SGerrit Uitslag    protected function phraseMatchInComment($phrase, $cid, &$data, $parent = '')
1763283a3029SGerrit Uitslag    {
176410b5d61eSMichael Hamann        if (!isset($data['comments'][$cid])) return false; // comment was removed
1765c3413364SGerrit Uitslag
176610b5d61eSMichael Hamann        $comment = $data['comments'][$cid];
176710b5d61eSMichael Hamann
176810b5d61eSMichael Hamann        if (!is_array($comment)) return false;             // corrupt datatype
176910b5d61eSMichael Hamann        if ($comment['parent'] != $parent) return false;   // reply to an other comment
177010b5d61eSMichael Hamann        if (!$comment['show']) return false;               // hidden comment
177110b5d61eSMichael Hamann
1772c3413364SGerrit Uitslag        $text = PhpString::strtolower($comment['raw']);
177310b5d61eSMichael Hamann        if (strpos($text, $phrase) !== false) {
177410b5d61eSMichael Hamann            return true;
177510b5d61eSMichael Hamann        }
177610b5d61eSMichael Hamann
177710b5d61eSMichael Hamann        if (is_array($comment['replies'])) {               // and the replies
177810b5d61eSMichael Hamann            foreach ($comment['replies'] as $rid) {
1779c3413364SGerrit Uitslag                if ($this->phraseMatchInComment($phrase, $rid, $data, $cid)) {
178010b5d61eSMichael Hamann                    return true;
178110b5d61eSMichael Hamann                }
178210b5d61eSMichael Hamann            }
178310b5d61eSMichael Hamann        }
178410b5d61eSMichael Hamann        return false;
178510b5d61eSMichael Hamann    }
178610b5d61eSMichael Hamann
1787a1ca9e44Swikidesign    /**
1788c3413364SGerrit Uitslag     * Saves the current comment status and title from metadata into the .comments file
1789de7e6f00SGerrit Uitslag     *
1790de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1791c1530f74SMichael Hamann     */
1792*1ce4168fSGerrit Uitslag    public function updateCommentStatusFromMetadata(Doku_Event $event)
1793283a3029SGerrit Uitslag    {
1794c1530f74SMichael Hamann        global $ID;
1795c1530f74SMichael Hamann
1796c1530f74SMichael Hamann        $meta = $event->data['current'];
1797*1ce4168fSGerrit Uitslag
1798c1530f74SMichael Hamann        $file = metaFN($ID, '.comments');
1799*1ce4168fSGerrit Uitslag        $configurationStatus = ($this->isDiscussionEnabled() ? 1 : 0); // 0=off, 1=enabled
1800c3413364SGerrit Uitslag        $title = null;
1801c1530f74SMichael Hamann        if (isset($meta['plugin_discussion'])) {
1802*1ce4168fSGerrit Uitslag            $status = (int) $meta['plugin_discussion']['status']; // 0=off, 1=enabled or 2=closed
1803c1530f74SMichael Hamann            $title = $meta['plugin_discussion']['title'];
1804*1ce4168fSGerrit Uitslag
1805*1ce4168fSGerrit Uitslag            // do we have metadata that differs from general config?
1806*1ce4168fSGerrit Uitslag            $saveNeededFromMetadata = $configurationStatus !== $status || ($status > 0 && $title);
1807*1ce4168fSGerrit Uitslag        } else {
1808*1ce4168fSGerrit Uitslag            $status = $configurationStatus;
1809*1ce4168fSGerrit Uitslag            $saveNeededFromMetadata = false;
1810c1530f74SMichael Hamann        }
1811c1530f74SMichael Hamann
1812*1ce4168fSGerrit Uitslag        // if .comment file exists always update it with latest status
1813*1ce4168fSGerrit Uitslag        if ($saveNeededFromMetadata || file_exists($file)) {
1814*1ce4168fSGerrit Uitslag
1815c3413364SGerrit Uitslag            $data = [];
1816c1530f74SMichael Hamann            if (@file_exists($file)) {
1817c1530f74SMichael Hamann                $data = unserialize(io_readFile($file, false));
1818c1530f74SMichael Hamann            }
1819c1530f74SMichael Hamann
1820c1530f74SMichael Hamann            if (!array_key_exists('title', $data) || $data['title'] !== $title || !isset($data['status']) || $data['status'] !== $status) {
1821*1ce4168fSGerrit Uitslag                //title can be only set from metadata
1822c1530f74SMichael Hamann                $data['title'] = $title;
1823c1530f74SMichael Hamann                $data['status'] = $status;
1824c3413364SGerrit Uitslag                if (!isset($data['number'])) {
1825c1530f74SMichael Hamann                    $data['number'] = 0;
1826c3413364SGerrit Uitslag                }
1827c1530f74SMichael Hamann                io_saveFile($file, serialize($data));
1828c1530f74SMichael Hamann            }
1829c1530f74SMichael Hamann        }
1830c1530f74SMichael Hamann    }
1831c1530f74SMichael Hamann
1832c1530f74SMichael Hamann    /**
1833c3413364SGerrit Uitslag     * Return words of a given comment and its replies, suitable to be added to the index
1834de7e6f00SGerrit Uitslag     *
1835c3413364SGerrit Uitslag     * @param string $cid comment id
1836c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1837c3413364SGerrit Uitslag     * @param string $parent cid of parent
1838de7e6f00SGerrit Uitslag     * @return string
1839a1ca9e44Swikidesign     */
1840283a3029SGerrit Uitslag    protected function addCommentWords($cid, &$data, $parent = '')
1841283a3029SGerrit Uitslag    {
1842a1ca9e44Swikidesign
1843efbe59d0Swikidesign        if (!isset($data['comments'][$cid])) return ''; // comment was removed
1844c3413364SGerrit Uitslag
1845a1ca9e44Swikidesign        $comment = $data['comments'][$cid];
1846a1ca9e44Swikidesign
1847efbe59d0Swikidesign        if (!is_array($comment)) return '';             // corrupt datatype
1848efbe59d0Swikidesign        if ($comment['parent'] != $parent) return '';   // reply to an other comment
1849efbe59d0Swikidesign        if (!$comment['show']) return '';               // hidden comment
1850a1ca9e44Swikidesign
1851efbe59d0Swikidesign        $text = $comment['raw'];                        // we only add the raw comment text
1852efbe59d0Swikidesign        if (is_array($comment['replies'])) {            // and the replies
1853efbe59d0Swikidesign            foreach ($comment['replies'] as $rid) {
1854c3413364SGerrit Uitslag                $text .= $this->addCommentWords($rid, $data, $cid);
1855a1ca9e44Swikidesign            }
1856a1ca9e44Swikidesign        }
1857efbe59d0Swikidesign        return ' ' . $text;
1858efbe59d0Swikidesign    }
1859a10b5c98SMichael Klier
1860a10b5c98SMichael Klier    /**
1861a10b5c98SMichael Klier     * Only allow http(s) URLs and append http:// to URLs if needed
1862de7e6f00SGerrit Uitslag     *
1863de7e6f00SGerrit Uitslag     * @param string $url
1864de7e6f00SGerrit Uitslag     * @return string
1865a10b5c98SMichael Klier     */
1866283a3029SGerrit Uitslag    protected function checkURL($url)
1867283a3029SGerrit Uitslag    {
1868a10b5c98SMichael Klier        if (preg_match("#^http://|^https://#", $url)) {
1869a10b5c98SMichael Klier            return hsc($url);
1870a10b5c98SMichael Klier        } elseif (substr($url, 0, 4) == 'www.') {
1871c3413364SGerrit Uitslag            return hsc('https://' . $url);
1872a10b5c98SMichael Klier        } else {
1873a10b5c98SMichael Klier            return '';
1874a10b5c98SMichael Klier        }
1875a10b5c98SMichael Klier    }
187631aab30eSGina Haeussge
1877de7e6f00SGerrit Uitslag    /**
1878de7e6f00SGerrit Uitslag     * Sort threads
1879de7e6f00SGerrit Uitslag     *
1880283a3029SGerrit Uitslag     * @param array $a array with comment properties
1881283a3029SGerrit Uitslag     * @param array $b array with comment properties
1882de7e6f00SGerrit Uitslag     * @return int
1883de7e6f00SGerrit Uitslag     */
1884283a3029SGerrit Uitslag    function sortThreadsOnCreation($a, $b)
1885283a3029SGerrit Uitslag    {
18867e018cb6Slpaulsen93        if (is_array($a['date'])) {
18877e018cb6Slpaulsen93            // new format
188831aab30eSGina Haeussge            $createdA = $a['date']['created'];
18897e018cb6Slpaulsen93        } else {
18907e018cb6Slpaulsen93            // old format
189131aab30eSGina Haeussge            $createdA = $a['date'];
189231aab30eSGina Haeussge        }
189331aab30eSGina Haeussge
18947e018cb6Slpaulsen93        if (is_array($b['date'])) {
18957e018cb6Slpaulsen93            // new format
189631aab30eSGina Haeussge            $createdB = $b['date']['created'];
18977e018cb6Slpaulsen93        } else {
18987e018cb6Slpaulsen93            // old format
189931aab30eSGina Haeussge            $createdB = $b['date'];
190031aab30eSGina Haeussge        }
190131aab30eSGina Haeussge
19024cded5e1SGerrit Uitslag        if ($createdA == $createdB) {
190331aab30eSGina Haeussge            return 0;
19044cded5e1SGerrit Uitslag        } else {
190531aab30eSGina Haeussge            return ($createdA < $createdB) ? -1 : 1;
190631aab30eSGina Haeussge        }
19074cded5e1SGerrit Uitslag    }
190831aab30eSGina Haeussge
1909c3413364SGerrit Uitslag}
1910c3413364SGerrit Uitslag
1911c3413364SGerrit Uitslag
1912