xref: /plugin/discussion/action.php (revision 76fdd2cde7c908c2349c3166be5a69b8e0c870de)
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,
18*76fdd2cdSGerrit 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', []);
83c3413364SGerrit Uitslag        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'update_comment_status', []);
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',
124*76fdd2cdSGerrit 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 {
266c3413364SGerrit Uitslag                        $comment['user']['id'] = 'test' . hsc($INPUT->str('user'));
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;
4172b18adb9SMichael Klier        }
418f0fda08aSwikidesign
419a1599850SMichael Klier        // show discussion wrapper only on certain circumstances
420c3413364SGerrit Uitslag        if (empty($data['comments']) || !is_array($data['comments'])) {
421c3413364SGerrit Uitslag            $cnt = 0;
4223e19949cSMark Prins            $keys = [];
423c3413364SGerrit Uitslag        } else {
424c3413364SGerrit Uitslag            $cnt = count($data['comments']);
425c3413364SGerrit Uitslag            $keys = array_keys($data['comments']);
4263e19949cSMark Prins        }
427c3413364SGerrit Uitslag
428de7e6f00SGerrit Uitslag        $show = false;
429c3413364SGerrit Uitslag        if ($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1)
430c3413364SGerrit Uitslag            || $this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) {
431a1599850SMichael Klier            $show = true;
432f0fda08aSwikidesign            // section title
433*76fdd2cdSGerrit Uitslag            $title = (!empty($data['title']) ? hsc($data['title']) : $this->getLang('discussion'));
43446178401Slupo49            ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section
4354a0a1bd2Swikidesign            ptln('<h2><a name="discussion__section" id="discussion__section">', 2);
4364a0a1bd2Swikidesign            ptln($title, 4);
4374a0a1bd2Swikidesign            ptln('</a></h2>', 2);
4384a0a1bd2Swikidesign            ptln('<div class="level2 hfeed">', 2);
439a1599850SMichael Klier        }
440a1599850SMichael Klier
441f0fda08aSwikidesign        // now display the comments
442f0fda08aSwikidesign        if (isset($data['comments'])) {
44331aab30eSGina Haeussge            if (!$this->getConf('usethreading')) {
444c3413364SGerrit Uitslag                $data['comments'] = $this->flattenThreads($data['comments']);
445283a3029SGerrit Uitslag                uasort($data['comments'], [$this, 'sortThreadsOnCreation']);
44631aab30eSGina Haeussge            }
447dbd9d5cdSMichael Klier            if ($this->getConf('newestfirst')) {
448dbd9d5cdSMichael Klier                $data['comments'] = array_reverse($data['comments']);
449dbd9d5cdSMichael Klier            }
450c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
451c3413364SGerrit Uitslag                if ($cid == $edit) { // edit form
452c3413364SGerrit Uitslag                    $this->showCommentForm($value['raw'], 'save', $edit);
453c3413364SGerrit Uitslag                } else {
454c3413364SGerrit Uitslag                    $this->showCommentWithReplies($cid, $data, '', $reply);
455c3413364SGerrit Uitslag                }
456f0fda08aSwikidesign            }
457f0fda08aSwikidesign        }
458f0fda08aSwikidesign
459c3413364SGerrit Uitslag        // comment form shown on the end, if no comment form of $reply or $edit is requested before
460c3413364SGerrit Uitslag        if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) {
461f7bcfbedSGerrit Uitslag            $this->showCommentForm('', 'add');
462c3413364SGerrit Uitslag        }
463f0fda08aSwikidesign
464a1599850SMichael Klier        if ($show) {
4654a0a1bd2Swikidesign            ptln('</div>', 2); // level2 hfeed
4664a0a1bd2Swikidesign            ptln('</div>'); // comment_wrapper
467a1599850SMichael Klier        }
468f0fda08aSwikidesign
46946178401Slupo49        // check for toggle print configuration
47046178401Slupo49        if ($this->getConf('visibilityButton')) {
47146178401Slupo49            // print the hide/show discussion section button
472c3413364SGerrit Uitslag            $this->showDiscussionToggleButton();
47346178401Slupo49        }
474f0fda08aSwikidesign    }
475f0fda08aSwikidesign
476de7e6f00SGerrit Uitslag    /**
477c3413364SGerrit Uitslag     * Remove the parent-child relation, such that the comment structure becomes flat
478c3413364SGerrit Uitslag     *
479c3413364SGerrit Uitslag     * @param array $comments array with all comments
480c3413364SGerrit Uitslag     * @param null|array $cids comment ids of replies, which should be flatten
481c3413364SGerrit Uitslag     * @return array returned array with flattened comment structure
482de7e6f00SGerrit Uitslag     */
483283a3029SGerrit Uitslag    protected function flattenThreads($comments, $cids = null)
484283a3029SGerrit Uitslag    {
485c3413364SGerrit Uitslag        if (is_null($cids)) {
486c3413364SGerrit Uitslag            $cids = array_keys($comments);
487c3413364SGerrit Uitslag        }
48831aab30eSGina Haeussge
489c3413364SGerrit Uitslag        foreach ($cids as $cid) {
49031aab30eSGina Haeussge            if (!empty($comments[$cid]['replies'])) {
49131aab30eSGina Haeussge                $rids = $comments[$cid]['replies'];
492c3413364SGerrit Uitslag                $comments = $this->flattenThreads($comments, $rids);
493c3413364SGerrit Uitslag                $comments[$cid]['replies'] = [];
49431aab30eSGina Haeussge            }
49531aab30eSGina Haeussge            $comments[$cid]['parent'] = '';
49631aab30eSGina Haeussge        }
49731aab30eSGina Haeussge        return $comments;
49831aab30eSGina Haeussge    }
49931aab30eSGina Haeussge
500f0fda08aSwikidesign    /**
501f0fda08aSwikidesign     * Adds a new comment and then displays all comments
502de7e6f00SGerrit Uitslag     *
503c3413364SGerrit Uitslag     * @param array $comment with
504c3413364SGerrit Uitslag     *  'raw' => string comment text,
505c3413364SGerrit Uitslag     *  'user' => [
506c3413364SGerrit Uitslag     *      'id' => string,
507c3413364SGerrit Uitslag     *      'name' => string,
508c3413364SGerrit Uitslag     *      'mail' => string
509c3413364SGerrit Uitslag     *  ],
510c3413364SGerrit Uitslag     *  'date' => [
511c3413364SGerrit Uitslag     *      'created' => int timestamp
512c3413364SGerrit Uitslag     *  ]
513c3413364SGerrit Uitslag     *  'show' => bool
514c3413364SGerrit Uitslag     *  'subscribe' => bool
515c3413364SGerrit Uitslag     * @param string $parent comment id of parent
516de7e6f00SGerrit Uitslag     * @return bool
517f0fda08aSwikidesign     */
518283a3029SGerrit Uitslag    protected function add($comment, $parent)
519283a3029SGerrit Uitslag    {
520c3413364SGerrit Uitslag        global $ID, $TEXT, $INPUT;
521f0fda08aSwikidesign
522c3413364SGerrit Uitslag        $originalTxt = $TEXT; // set $TEXT to comment text for wordblock check
523f0fda08aSwikidesign        $TEXT = $comment['raw'];
524f0fda08aSwikidesign
525f0fda08aSwikidesign        // spamcheck against the DokuWiki blacklist
526f0fda08aSwikidesign        if (checkwordblock()) {
527f0fda08aSwikidesign            msg($this->getLang('wordblock'), -1);
528f0fda08aSwikidesign            return false;
529f0fda08aSwikidesign        }
530f0fda08aSwikidesign
531c3413364SGerrit Uitslag        if (!$this->getConf('allowguests')
532c3413364SGerrit Uitslag            && $comment['user']['id'] != $INPUT->server->str('REMOTE_USER')
5334cded5e1SGerrit Uitslag        ) {
5343011fb8bSMichael Klier            return false; // guest comments not allowed
5354cded5e1SGerrit Uitslag        }
5363011fb8bSMichael Klier
537c3413364SGerrit Uitslag        $TEXT = $originalTxt; // restore global $TEXT
538f0fda08aSwikidesign
539f0fda08aSwikidesign        // get discussion meta file name
540f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
541f0fda08aSwikidesign
5422b18adb9SMichael Klier        // create comments file if it doesn't exist yet
5432b18adb9SMichael Klier        if (!@file_exists($file)) {
544c3413364SGerrit Uitslag            $data = ['status' => 1, 'number' => 0];
5452b18adb9SMichael Klier            io_saveFile($file, serialize($data));
5462b18adb9SMichael Klier        } else {
547f0fda08aSwikidesign            $data = unserialize(io_readFile($file, false));
548c3413364SGerrit Uitslag            // comments off or closed
549c3413364SGerrit Uitslag            if ($data['status'] != 1) {
550c3413364SGerrit Uitslag                return false;
551c3413364SGerrit Uitslag            }
5522b18adb9SMichael Klier        }
5532b18adb9SMichael Klier
5543011fb8bSMichael Klier        if ($comment['date']['created']) {
5553011fb8bSMichael Klier            $date = strtotime($comment['date']['created']);
5563011fb8bSMichael Klier        } else {
5573011fb8bSMichael Klier            $date = time();
5583011fb8bSMichael Klier        }
559f0fda08aSwikidesign
5603011fb8bSMichael Klier        if ($date == -1) {
5613011fb8bSMichael Klier            $date = time();
5623011fb8bSMichael Klier        }
5633011fb8bSMichael Klier
5646046f25cSwikidesign        $cid = md5($comment['user']['id'] . $date); // create a unique id
565f0fda08aSwikidesign
566*76fdd2cdSGerrit Uitslag        if (!isset($data['comments'][$parent]) || !is_array($data['comments'][$parent])) {
567c3413364SGerrit Uitslag            $parent = null; // invalid parent comment
5683011fb8bSMichael Klier        }
569f0fda08aSwikidesign
570f0fda08aSwikidesign        // render the comment
571c3413364SGerrit Uitslag        $xhtml = $this->renderComment($comment['raw']);
572f0fda08aSwikidesign
573f0fda08aSwikidesign        // fill in the new comment
574c3413364SGerrit Uitslag        $data['comments'][$cid] = [
5756046f25cSwikidesign            'user' => $comment['user'],
576c3413364SGerrit Uitslag            'date' => ['created' => $date],
5776046f25cSwikidesign            'raw' => $comment['raw'],
578f0fda08aSwikidesign            'xhtml' => $xhtml,
579f0fda08aSwikidesign            'parent' => $parent,
580c3413364SGerrit Uitslag            'replies' => [],
581a44bc9f7SMichael Klier            'show' => $comment['show']
582c3413364SGerrit Uitslag        ];
583f0fda08aSwikidesign
5843011fb8bSMichael Klier        if ($comment['subscribe']) {
5853011fb8bSMichael Klier            $mail = $comment['user']['mail'];
5863011fb8bSMichael Klier            if ($data['subscribers']) {
5873011fb8bSMichael Klier                if (!$data['subscribers'][$mail]) {
5889881d835SMichael Klier                    $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
5899881d835SMichael Klier                    $data['subscribers'][$mail]['active'] = false;
5909881d835SMichael Klier                    $data['subscribers'][$mail]['confirmsent'] = false;
5919881d835SMichael Klier                } else {
5929881d835SMichael Klier                    // convert old style subscribers and set them active
5939881d835SMichael Klier                    if (!is_array($data['subscribers'][$mail])) {
5949881d835SMichael Klier                        $hash = $data['subscribers'][$mail];
5959881d835SMichael Klier                        $data['subscribers'][$mail]['hash'] = $hash;
5969881d835SMichael Klier                        $data['subscribers'][$mail]['active'] = true;
5979881d835SMichael Klier                        $data['subscribers'][$mail]['confirmsent'] = true;
5989881d835SMichael Klier                    }
5993011fb8bSMichael Klier                }
6003011fb8bSMichael Klier            } else {
6019881d835SMichael Klier                $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
6029881d835SMichael Klier                $data['subscribers'][$mail]['active'] = false;
6039881d835SMichael Klier                $data['subscribers'][$mail]['confirmsent'] = false;
6043011fb8bSMichael Klier            }
6053011fb8bSMichael Klier        }
6063011fb8bSMichael Klier
607f0fda08aSwikidesign        // update parent comment
6084cded5e1SGerrit Uitslag        if ($parent) {
6094cded5e1SGerrit Uitslag            $data['comments'][$parent]['replies'][] = $cid;
6104cded5e1SGerrit Uitslag        }
611f0fda08aSwikidesign
612f0fda08aSwikidesign        // update the number of comments
613f0fda08aSwikidesign        $data['number']++;
614f0fda08aSwikidesign
615f0fda08aSwikidesign        // notify subscribers of the page
6168b42cefbSMichael Klier        $data['comments'][$cid]['cid'] = $cid;
617c3413364SGerrit Uitslag        $this->notify($data['comments'][$cid], $data['subscribers']);
618f0fda08aSwikidesign
6199881d835SMichael Klier        // save the comment metadata file
6209881d835SMichael Klier        io_saveFile($file, serialize($data));
621c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, 'cc', '', $cid);
6229881d835SMichael Klier
623c3413364SGerrit Uitslag        $this->redirect($cid);
624f0fda08aSwikidesign        return true;
625f0fda08aSwikidesign    }
626f0fda08aSwikidesign
627f0fda08aSwikidesign    /**
628f0fda08aSwikidesign     * Saves the comment with the given ID and then displays all comments
629de7e6f00SGerrit Uitslag     *
630c3413364SGerrit Uitslag     * @param array|string $cids array with comment ids to save, or a single string comment id
631c3413364SGerrit Uitslag     * @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!)
632c3413364SGerrit Uitslag     * @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw
633c3413364SGerrit Uitslag     * @return bool succeed?
634f0fda08aSwikidesign     */
635283a3029SGerrit Uitslag    public function save($cids, $raw, $act = null)
636283a3029SGerrit Uitslag    {
637c3413364SGerrit Uitslag        global $ID, $INPUT;
638f0fda08aSwikidesign
639c3413364SGerrit Uitslag        if (empty($cids)) return false; // do nothing if we get no comment id
640757550e8SMichael Klier
6412ee3dca3Swikidesign        if ($raw) {
6422ee3dca3Swikidesign            global $TEXT;
6432ee3dca3Swikidesign
644f0fda08aSwikidesign            $otxt = $TEXT; // set $TEXT to comment text for wordblock check
645f0fda08aSwikidesign            $TEXT = $raw;
646f0fda08aSwikidesign
647f0fda08aSwikidesign            // spamcheck against the DokuWiki blacklist
648f0fda08aSwikidesign            if (checkwordblock()) {
649f0fda08aSwikidesign                msg($this->getLang('wordblock'), -1);
650f0fda08aSwikidesign                return false;
651f0fda08aSwikidesign            }
652f0fda08aSwikidesign
653f0fda08aSwikidesign            $TEXT = $otxt; // restore global $TEXT
6542ee3dca3Swikidesign        }
655f0fda08aSwikidesign
656f0fda08aSwikidesign        // get discussion meta file name
657f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
658f0fda08aSwikidesign        $data = unserialize(io_readFile($file, false));
659f0fda08aSwikidesign
660c3413364SGerrit Uitslag        if (!is_array($cids)) {
661c3413364SGerrit Uitslag            $cids = [$cids];
662c3413364SGerrit Uitslag        }
663264b7327Swikidesign        foreach ($cids as $cid) {
664264b7327Swikidesign
6656046f25cSwikidesign            if (is_array($data['comments'][$cid]['user'])) {
6666046f25cSwikidesign                $user = $data['comments'][$cid]['user']['id'];
6676046f25cSwikidesign                $convert = false;
6686046f25cSwikidesign            } else {
6696046f25cSwikidesign                $user = $data['comments'][$cid]['user'];
6706046f25cSwikidesign                $convert = true;
6716046f25cSwikidesign            }
6726046f25cSwikidesign
673f0fda08aSwikidesign            // someone else was trying to edit our comment -> abort
67483f28d9bSGerrit Uitslag            if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionModerator()) {
675c3413364SGerrit Uitslag                return false;
676c3413364SGerrit Uitslag            }
677f0fda08aSwikidesign
678f0fda08aSwikidesign            $date = time();
679f0fda08aSwikidesign
6806046f25cSwikidesign            // need to convert to new format?
6816046f25cSwikidesign            if ($convert) {
682c3413364SGerrit Uitslag                $data['comments'][$cid]['user'] = [
6836046f25cSwikidesign                    'id' => $user,
6846046f25cSwikidesign                    'name' => $data['comments'][$cid]['name'],
6856046f25cSwikidesign                    'mail' => $data['comments'][$cid]['mail'],
6866046f25cSwikidesign                    'url' => $data['comments'][$cid]['url'],
6876046f25cSwikidesign                    'address' => $data['comments'][$cid]['address'],
688c3413364SGerrit Uitslag                ];
689c3413364SGerrit Uitslag                $data['comments'][$cid]['date'] = [
6906046f25cSwikidesign                    'created' => $data['comments'][$cid]['date']
691c3413364SGerrit Uitslag                ];
6926046f25cSwikidesign            }
6936046f25cSwikidesign
694264b7327Swikidesign            if ($act == 'toogle') {     // toogle visibility
695f0fda08aSwikidesign                $now = $data['comments'][$cid]['show'];
696f0fda08aSwikidesign                $data['comments'][$cid]['show'] = !$now;
697c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
698f0fda08aSwikidesign
699f0fda08aSwikidesign                $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
700f0fda08aSwikidesign
701264b7327Swikidesign            } elseif ($act == 'show') { // show comment
702264b7327Swikidesign                $data['comments'][$cid]['show'] = true;
703c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
704264b7327Swikidesign
705573e23a1Swikidesign                $type = 'sc'; // show comment
706264b7327Swikidesign
707264b7327Swikidesign            } elseif ($act == 'hide') { // hide comment
708264b7327Swikidesign                $data['comments'][$cid]['show'] = false;
709c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
710264b7327Swikidesign
711573e23a1Swikidesign                $type = 'hc'; // hide comment
712264b7327Swikidesign
713f0fda08aSwikidesign            } elseif (!$raw) {          // remove the comment
714c3413364SGerrit Uitslag                $data['comments'] = $this->removeComment($cid, $data['comments']);
715c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
716f0fda08aSwikidesign
717573e23a1Swikidesign                $type = 'dc'; // delete comment
718f0fda08aSwikidesign
719f0fda08aSwikidesign            } else {                   // save changed comment
720c3413364SGerrit Uitslag                $xhtml = $this->renderComment($raw);
721f0fda08aSwikidesign
722f0fda08aSwikidesign                // now change the comment's content
7236046f25cSwikidesign                $data['comments'][$cid]['date']['modified'] = $date;
7246046f25cSwikidesign                $data['comments'][$cid]['raw'] = $raw;
725f0fda08aSwikidesign                $data['comments'][$cid]['xhtml'] = $xhtml;
726f0fda08aSwikidesign
727573e23a1Swikidesign                $type = 'ec'; // edit comment
728f0fda08aSwikidesign            }
729264b7327Swikidesign        }
730264b7327Swikidesign
731f0fda08aSwikidesign        // save the comment metadata file
732f0fda08aSwikidesign        io_saveFile($file, serialize($data));
733c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, $type, '', $cid);
734f0fda08aSwikidesign
735c3413364SGerrit Uitslag        $this->redirect($cid);
736f0fda08aSwikidesign        return true;
737f0fda08aSwikidesign    }
738f0fda08aSwikidesign
739f0fda08aSwikidesign    /**
740c3413364SGerrit Uitslag     * Recursive function to remove a comment from the data array
741c3413364SGerrit Uitslag     *
742c3413364SGerrit Uitslag     * @param string $cid comment id to be removed
743c3413364SGerrit Uitslag     * @param array $comments array with all comments
744c3413364SGerrit Uitslag     * @return array returns modified array with all remaining comments
745efbe59d0Swikidesign     */
746283a3029SGerrit Uitslag    protected function removeComment($cid, $comments)
747283a3029SGerrit Uitslag    {
748efbe59d0Swikidesign        if (is_array($comments[$cid]['replies'])) {
749efbe59d0Swikidesign            foreach ($comments[$cid]['replies'] as $rid) {
750c3413364SGerrit Uitslag                $comments = $this->removeComment($rid, $comments);
751efbe59d0Swikidesign            }
752efbe59d0Swikidesign        }
753efbe59d0Swikidesign        unset($comments[$cid]);
754efbe59d0Swikidesign        return $comments;
755efbe59d0Swikidesign    }
756efbe59d0Swikidesign
757efbe59d0Swikidesign    /**
758f0fda08aSwikidesign     * Prints an individual comment
759de7e6f00SGerrit Uitslag     *
760c3413364SGerrit Uitslag     * @param string $cid comment id
761c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
762c3413364SGerrit Uitslag     * @param string $parent comment id of parent
763c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
764c3413364SGerrit Uitslag     * @param bool $isVisible is marked as visible
765f0fda08aSwikidesign     */
766283a3029SGerrit Uitslag    protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true)
767283a3029SGerrit Uitslag    {
768c3413364SGerrit Uitslag        // comment was removed
769c3413364SGerrit Uitslag        if (!isset($data['comments'][$cid])) {
770c3413364SGerrit Uitslag            return;
771c3413364SGerrit Uitslag        }
772f0fda08aSwikidesign        $comment = $data['comments'][$cid];
773f0fda08aSwikidesign
774c3413364SGerrit Uitslag        // corrupt datatype
775c3413364SGerrit Uitslag        if (!is_array($comment)) {
776c3413364SGerrit Uitslag            return;
777c3413364SGerrit Uitslag        }
778f0fda08aSwikidesign
779c3413364SGerrit Uitslag        // handle only replies to given parent comment
780c3413364SGerrit Uitslag        if ($comment['parent'] != $parent) {
781c3413364SGerrit Uitslag            return;
782c3413364SGerrit Uitslag        }
783f0fda08aSwikidesign
784c3413364SGerrit Uitslag        // comment hidden
785c3413364SGerrit Uitslag        if (!$comment['show']) {
78683f28d9bSGerrit Uitslag            if ($this->helper->isDiscussionModerator()) {
787c3413364SGerrit Uitslag                $hidden = ' comment_hidden';
788c3413364SGerrit Uitslag            } else {
789c3413364SGerrit Uitslag                return;
790c3413364SGerrit Uitslag            }
7914a0a1bd2Swikidesign        } else {
7924a0a1bd2Swikidesign            $hidden = '';
793f0fda08aSwikidesign        }
794f0fda08aSwikidesign
795f1c6610eSpierre.spring        // print the actual comment
79620d55e56SGerrit Uitslag        $this->showComment($cid, $data, $reply, $isVisible, $hidden);
797f1c6610eSpierre.spring        // replies to this comment entry?
798c3413364SGerrit Uitslag        $this->showReplies($cid, $data, $reply, $isVisible);
799f1c6610eSpierre.spring        // reply form
800c3413364SGerrit Uitslag        $this->showReplyForm($cid, $reply);
801f1c6610eSpierre.spring    }
802f1c6610eSpierre.spring
803de7e6f00SGerrit Uitslag    /**
804c3413364SGerrit Uitslag     * Print the comment
805c3413364SGerrit Uitslag     *
806c3413364SGerrit Uitslag     * @param string $cid comment id
80720d55e56SGerrit Uitslag     * @param array $data array with all comments
808c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
809c3413364SGerrit Uitslag     * @param bool $isVisible is marked as visible
810c3413364SGerrit Uitslag     * @param string $hidden extra class, for the admin only hidden view
811de7e6f00SGerrit Uitslag     */
81220d55e56SGerrit Uitslag    protected function showComment($cid, $data, $reply, $isVisible, $hidden)
813283a3029SGerrit Uitslag    {
814c3413364SGerrit Uitslag        global $conf, $lang, $HIGH, $INPUT;
815f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
816f1c6610eSpierre.spring
817f0fda08aSwikidesign        // comment head with date and user data
8184a0a1bd2Swikidesign        ptln('<div class="hentry' . $hidden . '">', 4);
8194a0a1bd2Swikidesign        ptln('<div class="comment_head">', 6);
8201810ba9cSMichael Klier        ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8);
8214a0a1bd2Swikidesign        $head = '<span class="vcard author">';
822f0fda08aSwikidesign
8236046f25cSwikidesign        // prepare variables
8246046f25cSwikidesign        if (is_array($comment['user'])) { // new format
8256046f25cSwikidesign            $user = $comment['user']['id'];
8266046f25cSwikidesign            $name = $comment['user']['name'];
8276046f25cSwikidesign            $mail = $comment['user']['mail'];
8286046f25cSwikidesign            $url = $comment['user']['url'];
8296046f25cSwikidesign            $address = $comment['user']['address'];
8306046f25cSwikidesign        } else {                         // old format
8316046f25cSwikidesign            $user = $comment['user'];
8326046f25cSwikidesign            $name = $comment['name'];
8336046f25cSwikidesign            $mail = $comment['mail'];
8346046f25cSwikidesign            $url = $comment['url'];
8356046f25cSwikidesign            $address = $comment['address'];
8366046f25cSwikidesign        }
8376046f25cSwikidesign        if (is_array($comment['date'])) { // new format
8386046f25cSwikidesign            $created = $comment['date']['created'];
839283a3029SGerrit Uitslag            $modified = $comment['date']['modified'] ?? null;
8406046f25cSwikidesign        } else {                         // old format
8416046f25cSwikidesign            $created = $comment['date'];
8426046f25cSwikidesign            $modified = $comment['edited'];
8436046f25cSwikidesign        }
8446046f25cSwikidesign
8450ff5ab97SMichael Klier        // show username or real name?
846c3413364SGerrit Uitslag        if (!$this->getConf('userealname') && $user) {
8470ff5ab97SMichael Klier            $showname = $user;
8480ff5ab97SMichael Klier        } else {
8490ff5ab97SMichael Klier            $showname = $name;
8500ff5ab97SMichael Klier        }
8510ff5ab97SMichael Klier
852ce7b17cfSwikidesign        // show avatar image?
853c3413364SGerrit Uitslag        if ($this->useAvatar()) {
85412ca6034SMichael Klier            $user_data['name'] = $name;
85512ca6034SMichael Klier            $user_data['user'] = $user;
85612ca6034SMichael Klier            $user_data['mail'] = $mail;
85712ca6034SMichael Klier            $avatar = $this->avatar->getXHTML($user_data, $name, 'left');
858c3413364SGerrit Uitslag            if ($avatar) {
859c3413364SGerrit Uitslag                $head .= $avatar;
860c3413364SGerrit Uitslag            }
861f0fda08aSwikidesign        }
862f0fda08aSwikidesign
8636046f25cSwikidesign        if ($this->getConf('linkemail') && $mail) {
8640ff5ab97SMichael Klier            $head .= $this->email($mail, $showname, 'email fn');
8656046f25cSwikidesign        } elseif ($url) {
866c3413364SGerrit Uitslag            $head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn');
867f0fda08aSwikidesign        } else {
8680ff5ab97SMichael Klier            $head .= '<span class="fn">' . $showname . '</span>';
869f0fda08aSwikidesign        }
8706f1f13d6SMatthias Schulte
8714cded5e1SGerrit Uitslag        if ($address) {
8724cded5e1SGerrit Uitslag            $head .= ', <span class="adr">' . $address . '</span>';
8734cded5e1SGerrit Uitslag        }
8744a0a1bd2Swikidesign        $head .= '</span>, ' .
875f014bc86SMichael Klier            '<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' .
8766f1f13d6SMatthias Schulte            dformat($created, $conf['dformat']) . '</abbr>';
8779c0e06f3SMoisés Braga Ribeiro        if ($modified) {
8789c0e06f3SMoisés Braga Ribeiro            $head .= ', <abbr class="updated" title="' .
8796f1f13d6SMatthias Schulte                strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) .
8809c0e06f3SMoisés Braga Ribeiro                '</abbr>';
8814cded5e1SGerrit Uitslag        }
882f014bc86SMichael Klier        ptln($head, 8);
8834a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_head"
884f0fda08aSwikidesign
885f0fda08aSwikidesign        // main comment content
8864a0a1bd2Swikidesign        ptln('<div class="comment_body entry-content"' .
887c3413364SGerrit Uitslag            ($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6);
88815cdad37Spierre.spring        echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF;
8894a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_body"
890f0fda08aSwikidesign
891c3413364SGerrit Uitslag        if ($isVisible) {
8921184c36aSwikidesign            ptln('<div class="comment_buttons">', 6);
893f0fda08aSwikidesign
894f0fda08aSwikidesign            // show reply button?
895c3413364SGerrit Uitslag            if ($data['status'] == 1 && !$reply && $comment['show']
896c3413364SGerrit Uitslag                && ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER'))
897c3413364SGerrit Uitslag                && $this->getConf('usethreading')
8984cded5e1SGerrit Uitslag            ) {
899c3413364SGerrit Uitslag                $this->showButton($cid, $this->getLang('btn_reply'), 'reply', true);
9004cded5e1SGerrit Uitslag            }
901f0fda08aSwikidesign
9021184c36aSwikidesign            // show edit, show/hide and delete button?
90383f28d9bSGerrit Uitslag            if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionModerator()) {
904c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_secedit'], 'edit', true);
9051184c36aSwikidesign                $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
906c3413364SGerrit Uitslag                $this->showButton($cid, $label, 'toogle');
907c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_delete'], 'delete');
908f0fda08aSwikidesign            }
9091184c36aSwikidesign            ptln('</div>', 6); // class="comment_buttons"
9101184c36aSwikidesign        }
9111184c36aSwikidesign        ptln('</div>', 4); // class="hentry"
912f0fda08aSwikidesign    }
913f0fda08aSwikidesign
914de7e6f00SGerrit Uitslag    /**
915c3413364SGerrit Uitslag     * If requested by user, show comment form to write a reply
916c3413364SGerrit Uitslag     *
917c3413364SGerrit Uitslag     * @param string $cid current comment id
918c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
919de7e6f00SGerrit Uitslag     */
920c3413364SGerrit Uitslag    protected function showReplyForm($cid, $reply)
921f1c6610eSpierre.spring    {
92231aab30eSGina Haeussge        if ($this->getConf('usethreading') && $reply == $cid) {
9234a0a1bd2Swikidesign            ptln('<div class="comment_replies">', 4);
924c3413364SGerrit Uitslag            $this->showCommentForm('', 'add', $cid);
9254a0a1bd2Swikidesign            ptln('</div>', 4); // class="comment_replies"
926f0fda08aSwikidesign        }
927f0fda08aSwikidesign    }
928f0fda08aSwikidesign
929de7e6f00SGerrit Uitslag    /**
930f7bcfbedSGerrit Uitslag     * Show the replies to the given comment
931c3413364SGerrit Uitslag     *
932c3413364SGerrit Uitslag     * @param string $cid comment id
933c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
934f7bcfbedSGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
935f7bcfbedSGerrit Uitslag     * @param bool $isVisible is marked as visible by reference
936de7e6f00SGerrit Uitslag     */
937c3413364SGerrit Uitslag    protected function showReplies($cid, &$data, $reply, &$isVisible)
938f1c6610eSpierre.spring    {
939f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
940f1c6610eSpierre.spring        if (!count($comment['replies'])) {
941f1c6610eSpierre.spring            return;
942f1c6610eSpierre.spring        }
943c3413364SGerrit Uitslag        ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4);
944c3413364SGerrit Uitslag        $isVisible = ($comment['show'] && $isVisible);
945f1c6610eSpierre.spring        foreach ($comment['replies'] as $rid) {
946c3413364SGerrit Uitslag            $this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible);
947f1c6610eSpierre.spring        }
948f1c6610eSpierre.spring        ptln('</div>', 4);
949f1c6610eSpierre.spring    }
950f1c6610eSpierre.spring
951de7e6f00SGerrit Uitslag    /**
952de7e6f00SGerrit Uitslag     * Is an avatar displayed?
953de7e6f00SGerrit Uitslag     *
954de7e6f00SGerrit Uitslag     * @return bool
955de7e6f00SGerrit Uitslag     */
956c3413364SGerrit Uitslag    protected function useAvatar()
957f1c6610eSpierre.spring    {
958c3413364SGerrit Uitslag        if (is_null($this->useAvatar)) {
959c3413364SGerrit Uitslag            $this->useAvatar = $this->getConf('useavatar')
960c3413364SGerrit Uitslag                && ($this->avatar = $this->loadHelper('avatar', false));
961f1c6610eSpierre.spring        }
962c3413364SGerrit Uitslag        return $this->useAvatar;
963f1c6610eSpierre.spring    }
964f1c6610eSpierre.spring
965de7e6f00SGerrit Uitslag    /**
966de7e6f00SGerrit Uitslag     * Calculate width of indent
967de7e6f00SGerrit Uitslag     *
968de7e6f00SGerrit Uitslag     * @return string
969de7e6f00SGerrit Uitslag     */
970283a3029SGerrit Uitslag    protected function getWidthStyle()
971283a3029SGerrit Uitslag    {
972f1c6610eSpierre.spring        if (is_null($this->style)) {
973c3413364SGerrit Uitslag            if ($this->useAvatar()) {
974f1c6610eSpierre.spring                $this->style = ' style="margin-left: ' . ($this->avatar->getConf('size') + 14) . 'px;"';
975f1c6610eSpierre.spring            } else {
976f1c6610eSpierre.spring                $this->style = ' style="margin-left: 20px;"';
977f1c6610eSpierre.spring            }
978f1c6610eSpierre.spring        }
979f1c6610eSpierre.spring        return $this->style;
980f1c6610eSpierre.spring    }
981f1c6610eSpierre.spring
982f0fda08aSwikidesign    /**
983c3413364SGerrit Uitslag     * Show the button which toggles between show/hide of the entire discussion section
98446178401Slupo49     */
985283a3029SGerrit Uitslag    protected function showDiscussionToggleButton()
986283a3029SGerrit Uitslag    {
98746178401Slupo49        ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">');
988283a3029SGerrit Uitslag        ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button"'
989283a3029SGerrit Uitslag            . 'value="' . $this->getLang('toggle_display') . '">');
99046178401Slupo49        ptln('</div>');
99146178401Slupo49    }
99246178401Slupo49
99346178401Slupo49    /**
994f0fda08aSwikidesign     * Outputs the comment form
995f7bcfbedSGerrit Uitslag     *
996f7bcfbedSGerrit Uitslag     * @param string $raw the existing comment text in case of edit
997f7bcfbedSGerrit Uitslag     * @param string $act action 'add' or 'save'
998f7bcfbedSGerrit Uitslag     * @param string|null $cid comment id to be responded to or null
999f0fda08aSwikidesign     */
1000f7bcfbedSGerrit Uitslag    protected function showCommentForm($raw, $act, $cid = null)
1001283a3029SGerrit Uitslag    {
1002c3413364SGerrit Uitslag        global $lang, $conf, $ID, $INPUT;
1003f0fda08aSwikidesign
1004f0fda08aSwikidesign        // not for unregistered users when guest comments aren't allowed
1005c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('allowguests')) {
10067c8e18ffSGina Haeussge            ?>
10077c8e18ffSGina Haeussge            <div class="comment_form">
10087c8e18ffSGina Haeussge                <?php echo $this->getLang('noguests'); ?>
10097c8e18ffSGina Haeussge            </div>
10107c8e18ffSGina Haeussge            <?php
1011de7e6f00SGerrit Uitslag            return;
10127c8e18ffSGina Haeussge        }
1013f0fda08aSwikidesign
1014c3413364SGerrit Uitslag        // fill $raw with $INPUT->str('text') if it's empty (for failed CAPTCHA check)
1015c3413364SGerrit Uitslag        if (!$raw && $INPUT->str('comment') == 'show') {
1016c3413364SGerrit Uitslag            $raw = $INPUT->str('text');
10174cded5e1SGerrit Uitslag        }
1018f0fda08aSwikidesign        ?>
10195ef1705fSiLoveiDo
1020f0fda08aSwikidesign        <div class="comment_form">
1021283a3029SGerrit Uitslag            <form id="discussion__comment_form" method="post" action="<?php echo script() ?>"
1022283a3029SGerrit Uitslag                  accept-charset="<?php echo $lang['encoding'] ?>">
1023f0fda08aSwikidesign                <div class="no">
1024f0fda08aSwikidesign                    <input type="hidden" name="id" value="<?php echo $ID ?>"/>
102561437513Swikidesign                    <input type="hidden" name="do" value="show"/>
1026f0fda08aSwikidesign                    <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1027530693fbSMichael Klier                    <?php
1028f0fda08aSwikidesign                    // for adding a comment
1029f0fda08aSwikidesign                    if ($act == 'add') {
1030f0fda08aSwikidesign                        ?>
1031f0fda08aSwikidesign                        <input type="hidden" name="reply" value="<?php echo $cid ?>"/>
1032f0fda08aSwikidesign                        <?php
103320c152acSMichael Klier                        // for guest/adminimport: show name, e-mail and subscribe to comments fields
103483f28d9bSGerrit Uitslag                        if (!$INPUT->server->has('REMOTE_USER') or ($this->getConf('adminimport') && $this->helper->isDiscussionModerator())) {
1035f0fda08aSwikidesign                            ?>
1036f0fda08aSwikidesign                            <input type="hidden" name="user" value="<?php echo clientIP() ?>"/>
1037f0fda08aSwikidesign                            <div class="comment_name">
1038f0fda08aSwikidesign                                <label class="block" for="discussion__comment_name">
1039f0fda08aSwikidesign                                    <span><?php echo $lang['fullname'] ?>:</span>
1040283a3029SGerrit Uitslag                                    <input type="text"
1041283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('name'))) echo ' error' ?>"
1042283a3029SGerrit Uitslag                                           name="name" id="discussion__comment_name" size="50" tabindex="1"
1043283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('name')) ?>"/>
1044f0fda08aSwikidesign                                </label>
1045f0fda08aSwikidesign                            </div>
1046f0fda08aSwikidesign                            <div class="comment_mail">
1047f0fda08aSwikidesign                                <label class="block" for="discussion__comment_mail">
1048f0fda08aSwikidesign                                    <span><?php echo $lang['email'] ?>:</span>
1049283a3029SGerrit Uitslag                                    <input type="text"
1050283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('mail'))) echo ' error' ?>"
1051283a3029SGerrit Uitslag                                           name="mail" id="discussion__comment_mail" size="50" tabindex="2"
1052283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('mail')) ?>"/>
1053f0fda08aSwikidesign                                </label>
1054f0fda08aSwikidesign                            </div>
1055f0fda08aSwikidesign                            <?php
1056f0fda08aSwikidesign                        }
1057f0fda08aSwikidesign
1058f0fda08aSwikidesign                        // allow entering an URL
1059f0fda08aSwikidesign                        if ($this->getConf('urlfield')) {
1060f0fda08aSwikidesign                            ?>
1061f0fda08aSwikidesign                            <div class="comment_url">
1062f0fda08aSwikidesign                                <label class="block" for="discussion__comment_url">
1063f0fda08aSwikidesign                                    <span><?php echo $this->getLang('url') ?>:</span>
1064283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="url" id="discussion__comment_url" size="50"
1065283a3029SGerrit Uitslag                                           tabindex="3" value="<?php echo hsc($INPUT->str('url')) ?>"/>
1066f0fda08aSwikidesign                                </label>
1067f0fda08aSwikidesign                            </div>
1068f0fda08aSwikidesign                            <?php
1069f0fda08aSwikidesign                        }
1070f0fda08aSwikidesign
1071f0fda08aSwikidesign                        // allow entering an address
1072f0fda08aSwikidesign                        if ($this->getConf('addressfield')) {
1073f0fda08aSwikidesign                            ?>
1074f0fda08aSwikidesign                            <div class="comment_address">
1075f0fda08aSwikidesign                                <label class="block" for="discussion__comment_address">
1076f0fda08aSwikidesign                                    <span><?php echo $this->getLang('address') ?>:</span>
1077283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="address" id="discussion__comment_address"
1078283a3029SGerrit Uitslag                                           size="50" tabindex="4" value="<?php echo hsc($INPUT->str('address')) ?>"/>
1079f0fda08aSwikidesign                                </label>
1080f0fda08aSwikidesign                            </div>
1081f0fda08aSwikidesign                            <?php
1082f0fda08aSwikidesign                        }
1083f0fda08aSwikidesign
1084f0fda08aSwikidesign                        // allow setting the comment date
108583f28d9bSGerrit Uitslag                        if ($this->getConf('adminimport') && ($this->helper->isDiscussionModerator())) {
1086f0fda08aSwikidesign                            ?>
1087f0fda08aSwikidesign                            <div class="comment_date">
1088f0fda08aSwikidesign                                <label class="block" for="discussion__comment_date">
1089f0fda08aSwikidesign                                    <span><?php echo $this->getLang('date') ?>:</span>
1090283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="date" id="discussion__comment_date"
1091283a3029SGerrit Uitslag                                           size="50"/>
1092f0fda08aSwikidesign                                </label>
1093f0fda08aSwikidesign                            </div>
1094f0fda08aSwikidesign                            <?php
1095f0fda08aSwikidesign                        }
1096f0fda08aSwikidesign
1097f0fda08aSwikidesign                        // for saving a comment
1098f0fda08aSwikidesign                    } else {
1099f0fda08aSwikidesign                        ?>
1100f0fda08aSwikidesign                        <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1101f0fda08aSwikidesign                        <?php
1102f0fda08aSwikidesign                    }
1103f0fda08aSwikidesign                    ?>
1104f0fda08aSwikidesign                    <div class="comment_text">
1105283a3029SGerrit Uitslag                        <?php echo $this->getLang('entercomment');
1106283a3029SGerrit Uitslag                        echo($this->getConf('wikisyntaxok') ? "" : ":");
110743ade360Slupo49                        if ($this->getConf('wikisyntaxok')) echo '. ' . $this->getLang('wikisyntax') . ':'; ?>
110843ade360Slupo49
110943ade360Slupo49                        <!-- Fix for disable the toolbar when wikisyntaxok is set to false. See discussion's script.jss -->
111043ade360Slupo49                        <?php if ($this->getConf('wikisyntaxok')) { ?>
111164901b8eSGerrit Uitslag                        <div id="discussion__comment_toolbar" class="toolbar group">
111243ade360Slupo49                            <?php } else { ?>
111343ade360Slupo49                            <div id="discussion__comment_toolbar_disabled">
111443ade360Slupo49                                <?php } ?>
11151de52da1SMichael Klier                            </div>
1116283a3029SGerrit Uitslag                            <textarea
1117283a3029SGerrit Uitslag                                class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('text'))) echo ' error' ?>"
1118283a3029SGerrit Uitslag                                name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php
111937e3c825SMichael Klier                                if ($raw) {
112037e3c825SMichael Klier                                    echo formText($raw);
112137e3c825SMichael Klier                                } else {
1122c3413364SGerrit Uitslag                                    echo hsc($INPUT->str('text'));
112337e3c825SMichael Klier                                }
112437e3c825SMichael Klier                                ?></textarea>
1125f0fda08aSwikidesign                        </div>
11260c613822SMichael Hamann
11270c613822SMichael Hamann                        <?php
11280c613822SMichael Hamann                        /** @var helper_plugin_captcha $captcha */
11290c613822SMichael Hamann                        $captcha = $this->loadHelper('captcha', false);
11300c613822SMichael Hamann                        if ($captcha && $captcha->isEnabled()) {
11310c613822SMichael Hamann                            echo $captcha->getHTML();
11320c613822SMichael Hamann                        }
11330c613822SMichael Hamann
11340c613822SMichael Hamann                        /** @var helper_plugin_recaptcha $recaptcha */
11350c613822SMichael Hamann                        $recaptcha = $this->loadHelper('recaptcha', false);
11360c613822SMichael Hamann                        if ($recaptcha && $recaptcha->isEnabled()) {
11370c613822SMichael Hamann                            echo $recaptcha->getHTML();
11380c613822SMichael Hamann                        }
1139e7c760b3Swikidesign                        ?>
11400c613822SMichael Hamann
1141283a3029SGerrit Uitslag                        <input class="button comment_submit" id="discussion__btn_submit" type="submit" name="submit"
1142283a3029SGerrit Uitslag                               accesskey="s" value="<?php echo $lang['btn_save'] ?>"
1143283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_save'] ?> [S]" tabindex="7"/>
1144283a3029SGerrit Uitslag                        <input class="button comment_preview_button" id="discussion__btn_preview" type="button"
1145283a3029SGerrit Uitslag                               name="preview" accesskey="p" value="<?php echo $lang['btn_preview'] ?>"
1146283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_preview'] ?> [P]"/>
11473011fb8bSMichael Klier
1148283a3029SGerrit Uitslag                        <?php if ((!$INPUT->server->has('REMOTE_USER')
1149283a3029SGerrit Uitslag                                || $INPUT->server->has('REMOTE_USER') && !$conf['subscribers'])
1150283a3029SGerrit Uitslag                                && $this->getConf('subscribe')) { ?>
11513011fb8bSMichael Klier                            <div class="comment_subscribe">
1152283a3029SGerrit Uitslag                                <input type="checkbox" id="discussion__comment_subscribe" name="subscribe"
1153283a3029SGerrit Uitslag                                       tabindex="6"/>
11543011fb8bSMichael Klier                                <label class="block" for="discussion__comment_subscribe">
11553011fb8bSMichael Klier                                    <span><?php echo $this->getLang('subscribe') ?></span>
11563011fb8bSMichael Klier                                </label>
11573011fb8bSMichael Klier                            </div>
11583011fb8bSMichael Klier                        <?php } ?>
11593011fb8bSMichael Klier
11603011fb8bSMichael Klier                        <div class="clearer"></div>
1161ed4846efSMichael Klier                        <div id="discussion__comment_preview">&nbsp;</div>
1162f0fda08aSwikidesign                    </div>
1163f0fda08aSwikidesign            </form>
1164f0fda08aSwikidesign        </div>
1165f0fda08aSwikidesign        <?php
1166f0fda08aSwikidesign    }
1167f0fda08aSwikidesign
1168f0fda08aSwikidesign    /**
1169c3413364SGerrit Uitslag     * Action button below a comment
1170de7e6f00SGerrit Uitslag     *
1171c3413364SGerrit Uitslag     * @param string $cid comment id
1172c3413364SGerrit Uitslag     * @param string $label translated label
1173c3413364SGerrit Uitslag     * @param string $act action
1174c3413364SGerrit Uitslag     * @param bool $jump whether to scroll to the commentform
1175f0fda08aSwikidesign     */
1176283a3029SGerrit Uitslag    protected function showButton($cid, $label, $act, $jump = false)
1177283a3029SGerrit Uitslag    {
1178f0fda08aSwikidesign        global $ID;
11795ef1705fSiLoveiDo
11801e46d176Swikidesign        $anchor = ($jump ? '#discussion__comment_form' : '');
1181f0fda08aSwikidesign
1182f0fda08aSwikidesign        ?>
11836d1f4f20SGina Haeussge        <form class="button discussion__<?php echo $act ?>" method="get" action="<?php echo script() . $anchor ?>">
1184f0fda08aSwikidesign            <div class="no">
1185f0fda08aSwikidesign                <input type="hidden" name="id" value="<?php echo $ID ?>"/>
118661437513Swikidesign                <input type="hidden" name="do" value="show"/>
1187f0fda08aSwikidesign                <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1188f0fda08aSwikidesign                <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1189f0fda08aSwikidesign                <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>"/>
1190f0fda08aSwikidesign            </div>
1191f0fda08aSwikidesign        </form>
1192f0fda08aSwikidesign        <?php
1193f0fda08aSwikidesign    }
1194f0fda08aSwikidesign
1195f0fda08aSwikidesign    /**
1196f0fda08aSwikidesign     * Adds an entry to the comments changelog
1197f0fda08aSwikidesign     *
1198de7e6f00SGerrit Uitslag     * @param int $date
1199de7e6f00SGerrit Uitslag     * @param string $id page id
1200c3413364SGerrit Uitslag     * @param string $type create/edit/delete/show/hide comment 'cc', 'ec', 'dc', 'sc', 'hc'
1201de7e6f00SGerrit Uitslag     * @param string $summary
1202de7e6f00SGerrit Uitslag     * @param string $extra
1203283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1204283a3029SGerrit Uitslag     *
1205283a3029SGerrit Uitslag     * @author Esther Brunner <wikidesign@gmail.com>
1206f0fda08aSwikidesign     */
1207283a3029SGerrit Uitslag    protected function addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '')
1208283a3029SGerrit Uitslag    {
1209c3413364SGerrit Uitslag        global $conf, $INPUT;
1210f0fda08aSwikidesign
1211f0fda08aSwikidesign        $changelog = $conf['metadir'] . '/_comments.changes';
1212f0fda08aSwikidesign
12134cded5e1SGerrit Uitslag        //use current time if none supplied
12144cded5e1SGerrit Uitslag        if (!$date) {
12154cded5e1SGerrit Uitslag            $date = time();
12164cded5e1SGerrit Uitslag        }
1217c3413364SGerrit Uitslag        $remote = $INPUT->server->str('REMOTE_ADDR');
1218c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
1219f0fda08aSwikidesign
1220c3413364SGerrit Uitslag        $strip = ["\t", "\n"];
1221c3413364SGerrit Uitslag        $logline = [
1222f0fda08aSwikidesign            'date' => $date,
1223f0fda08aSwikidesign            'ip' => $remote,
1224f0fda08aSwikidesign            'type' => str_replace($strip, '', $type),
1225f0fda08aSwikidesign            'id' => $id,
1226f0fda08aSwikidesign            'user' => $user,
1227f0fda08aSwikidesign            'sum' => str_replace($strip, '', $summary),
1228f0fda08aSwikidesign            'extra' => str_replace($strip, '', $extra)
1229c3413364SGerrit Uitslag        ];
1230f0fda08aSwikidesign
1231f0fda08aSwikidesign        // add changelog line
1232f0fda08aSwikidesign        $logline = implode("\t", $logline) . "\n";
1233f0fda08aSwikidesign        io_saveFile($changelog, $logline, true); //global changelog cache
1234c3413364SGerrit Uitslag        $this->trimRecentCommentsLog($changelog);
123577a22ba2Swikidesign
123677a22ba2Swikidesign        // tell the indexer to re-index the page
123777a22ba2Swikidesign        @unlink(metaFN($id, '.indexed'));
1238f0fda08aSwikidesign    }
1239f0fda08aSwikidesign
1240f0fda08aSwikidesign    /**
1241f0fda08aSwikidesign     * Trims the recent comments cache to the last $conf['changes_days'] recent
1242f0fda08aSwikidesign     * changes or $conf['recent'] items, which ever is larger.
1243f0fda08aSwikidesign     * The trimming is only done once a day.
1244f0fda08aSwikidesign     *
1245de7e6f00SGerrit Uitslag     * @param string $changelog file path
1246de7e6f00SGerrit Uitslag     * @return bool
1247283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1248283a3029SGerrit Uitslag     *
1249f0fda08aSwikidesign     */
1250283a3029SGerrit Uitslag    protected function trimRecentCommentsLog($changelog)
1251283a3029SGerrit Uitslag    {
1252f0fda08aSwikidesign        global $conf;
1253f0fda08aSwikidesign
1254283a3029SGerrit Uitslag        if (@file_exists($changelog)
1255283a3029SGerrit Uitslag            && (filectime($changelog) + 86400) < time()
1256283a3029SGerrit Uitslag            && !@file_exists($changelog . '_tmp')
12574cded5e1SGerrit Uitslag        ) {
1258f0fda08aSwikidesign
1259f0fda08aSwikidesign            io_lock($changelog);
1260f0fda08aSwikidesign            $lines = file($changelog);
1261f0fda08aSwikidesign            if (count($lines) < $conf['recent']) {
1262f0fda08aSwikidesign                // nothing to trim
1263f0fda08aSwikidesign                io_unlock($changelog);
1264f0fda08aSwikidesign                return true;
1265f0fda08aSwikidesign            }
1266f0fda08aSwikidesign
1267283a3029SGerrit Uitslag            // presave tmp as 2nd lock
1268283a3029SGerrit Uitslag            io_saveFile($changelog . '_tmp', '');
1269f0fda08aSwikidesign            $trim_time = time() - $conf['recent_days'] * 86400;
1270c3413364SGerrit Uitslag            $out_lines = [];
1271f0fda08aSwikidesign
1272e49085a2SMichael Klier            $num = count($lines);
1273e49085a2SMichael Klier            for ($i = 0; $i < $num; $i++) {
1274f0fda08aSwikidesign                $log = parseChangelogLine($lines[$i]);
1275f0fda08aSwikidesign                if ($log === false) continue;                      // discard junk
1276f0fda08aSwikidesign                if ($log['date'] < $trim_time) {
1277f0fda08aSwikidesign                    $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions)
1278f0fda08aSwikidesign                } else {
1279f0fda08aSwikidesign                    $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines
1280f0fda08aSwikidesign                }
1281f0fda08aSwikidesign            }
1282f0fda08aSwikidesign
1283f0fda08aSwikidesign            // sort the final result, it shouldn't be necessary,
1284f0fda08aSwikidesign            // however the extra robustness in making the changelog cache self-correcting is worth it
1285f0fda08aSwikidesign            ksort($out_lines);
1286f0fda08aSwikidesign            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
1287f0fda08aSwikidesign            if ($extra > 0) {
1288f0fda08aSwikidesign                ksort($old_lines);
1289f0fda08aSwikidesign                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
1290f0fda08aSwikidesign            }
1291f0fda08aSwikidesign
1292f0fda08aSwikidesign            // save trimmed changelog
1293f0fda08aSwikidesign            io_saveFile($changelog . '_tmp', implode('', $out_lines));
1294f0fda08aSwikidesign            @unlink($changelog);
1295f0fda08aSwikidesign            if (!rename($changelog . '_tmp', $changelog)) {
1296f0fda08aSwikidesign                // rename failed so try another way...
1297f0fda08aSwikidesign                io_unlock($changelog);
1298f0fda08aSwikidesign                io_saveFile($changelog, implode('', $out_lines));
1299f0fda08aSwikidesign                @unlink($changelog . '_tmp');
1300f0fda08aSwikidesign            } else {
1301f0fda08aSwikidesign                io_unlock($changelog);
1302f0fda08aSwikidesign            }
1303f0fda08aSwikidesign            return true;
1304f0fda08aSwikidesign        }
1305de7e6f00SGerrit Uitslag        return true;
1306f0fda08aSwikidesign    }
1307f0fda08aSwikidesign
1308f0fda08aSwikidesign    /**
1309f0fda08aSwikidesign     * Sends a notify mail on new comment
1310f0fda08aSwikidesign     *
1311f0fda08aSwikidesign     * @param array $comment data array of the new comment
1312c3413364SGerrit Uitslag     * @param array $subscribers data of the subscribers by reference
1313f0fda08aSwikidesign     *
1314f0fda08aSwikidesign     * @author Andreas Gohr <andi@splitbrain.org>
1315f0fda08aSwikidesign     * @author Esther Brunner <wikidesign@gmail.com>
1316f0fda08aSwikidesign     */
1317283a3029SGerrit Uitslag    protected function notify($comment, &$subscribers)
1318283a3029SGerrit Uitslag    {
1319c3413364SGerrit Uitslag        global $conf, $ID, $INPUT, $auth;
1320f0fda08aSwikidesign
13219881d835SMichael Klier        $notify_text = io_readfile($this->localfn('subscribermail'));
13229881d835SMichael Klier        $confirm_text = io_readfile($this->localfn('confirmsubscribe'));
13239881d835SMichael Klier        $subject_notify = '[' . $conf['title'] . '] ' . $this->getLang('mail_newcomment');
13249881d835SMichael Klier        $subject_subscribe = '[' . $conf['title'] . '] ' . $this->getLang('subscribe');
1325f0fda08aSwikidesign
1326451c1100SMichael Hamann        $mailer = new Mailer();
1327c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER')) {
1328451c1100SMichael Hamann            $mailer->from($conf['mailfromnobody']);
1329f4a5ed1cSMatthias Schulte        }
1330f4a5ed1cSMatthias Schulte
1331c3413364SGerrit Uitslag        $replace = [
13323c4953e9SMichael Hamann            'PAGE' => $ID,
13333c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13343c4953e9SMichael Hamann            'DATE' => dformat($comment['date']['created'], $conf['dformat']),
13353c4953e9SMichael Hamann            'NAME' => $comment['user']['name'],
13363c4953e9SMichael Hamann            'TEXT' => $comment['raw'],
13373c4953e9SMichael Hamann            'COMMENTURL' => wl($ID, '', true) . '#comment_' . $comment['cid'],
133806644a74SMichael Hamann            'UNSUBSCRIBE' => wl($ID, 'do=subscribe', true, '&'),
13393c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1340c3413364SGerrit Uitslag        ];
1341451c1100SMichael Hamann
1342c3413364SGerrit Uitslag        $confirm_replace = [
13433c4953e9SMichael Hamann            'PAGE' => $ID,
13443c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13453c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1346c3413364SGerrit Uitslag        ];
1347451c1100SMichael Hamann
1348451c1100SMichael Hamann
1349451c1100SMichael Hamann        $mailer->subject($subject_notify);
1350451c1100SMichael Hamann        $mailer->setBody($notify_text, $replace);
1351451c1100SMichael Hamann
1352f4a5ed1cSMatthias Schulte        // send mail to notify address
1353f4a5ed1cSMatthias Schulte        if ($conf['notify']) {
1354451c1100SMichael Hamann            $mailer->bcc($conf['notify']);
1355451c1100SMichael Hamann            $mailer->send();
1356f4a5ed1cSMatthias Schulte        }
1357f4a5ed1cSMatthias Schulte
1358ca785d71SMichael Hamann        // send email to moderators
1359ca785d71SMichael Hamann        if ($this->getConf('moderatorsnotify')) {
1360c3413364SGerrit Uitslag            $moderatorgrpsString = trim($this->getConf('moderatorgroups'));
1361c3413364SGerrit Uitslag            if (!empty($moderatorgrpsString)) {
1362ca785d71SMichael Hamann                // create a clean mods list
1363c3413364SGerrit Uitslag                $moderatorgroups = explode(',', $moderatorgrpsString);
1364c3413364SGerrit Uitslag                $moderatorgroups = array_map('trim', $moderatorgroups);
1365c3413364SGerrit Uitslag                $moderatorgroups = array_unique($moderatorgroups);
1366c3413364SGerrit Uitslag                $moderatorgroups = array_filter($moderatorgroups);
1367ca785d71SMichael Hamann                // search for moderators users
1368c3413364SGerrit Uitslag                foreach ($moderatorgroups as $moderatorgroup) {
1369c3413364SGerrit Uitslag                    if (!$auth->isCaseSensitive()) {
1370c3413364SGerrit Uitslag                        $moderatorgroup = PhpString::strtolower($moderatorgroup);
1371c3413364SGerrit Uitslag                    }
1372ca785d71SMichael Hamann                    // create a clean mailing list
1373c3413364SGerrit Uitslag                    $bccs = [];
1374c3413364SGerrit Uitslag                    if ($moderatorgroup[0] == '@') {
1375c3413364SGerrit Uitslag                        foreach ($auth->retrieveUsers(0, 0, ['grps' => $auth->cleanGroup(substr($moderatorgroup, 1))]) as $user) {
1376ca785d71SMichael Hamann                            if (!empty($user['mail'])) {
1377c3413364SGerrit Uitslag                                $bccs[] = $user['mail'];
1378ca785d71SMichael Hamann                            }
1379ca785d71SMichael Hamann                        }
1380ca785d71SMichael Hamann                    } else {
1381c3413364SGerrit Uitslag                        //it is an user
1382c3413364SGerrit Uitslag                        $userdata = $auth->getUserData($auth->cleanUser($moderatorgroup));
1383ca785d71SMichael Hamann                        if (!empty($userdata['mail'])) {
1384c3413364SGerrit Uitslag                            $bccs[] = $userdata['mail'];
1385ca785d71SMichael Hamann                        }
1386ca785d71SMichael Hamann                    }
1387c3413364SGerrit Uitslag                    $bccs = array_unique($bccs);
1388ca785d71SMichael Hamann                    // notify the users
1389c3413364SGerrit Uitslag                    $mailer->bcc(implode(',', $bccs));
1390ca785d71SMichael Hamann                    $mailer->send();
1391ca785d71SMichael Hamann                }
1392ca785d71SMichael Hamann            }
1393ca785d71SMichael Hamann        }
1394ca785d71SMichael Hamann
1395f4a5ed1cSMatthias Schulte        // notify page subscribers
1396451c1100SMichael Hamann        if (actionOK('subscribe')) {
1397c3413364SGerrit Uitslag            $data = ['id' => $ID, 'addresslist' => '', 'self' => false];
1398c3413364SGerrit Uitslag            //FIXME default callback, needed to mentioned it again?
1399c3413364SGerrit Uitslag            Event::createAndTrigger(
1400451c1100SMichael Hamann                'COMMON_NOTIFY_ADDRESSLIST', $data,
1401c3413364SGerrit Uitslag                [new SubscriberManager(), 'notifyAddresses']
1402451c1100SMichael Hamann            );
1403c3413364SGerrit Uitslag
1404451c1100SMichael Hamann            $to = $data['addresslist'];
1405451c1100SMichael Hamann            if (!empty($to)) {
1406451c1100SMichael Hamann                $mailer->bcc($to);
1407451c1100SMichael Hamann                $mailer->send();
1408451c1100SMichael Hamann            }
14093011fb8bSMichael Klier        }
1410f0fda08aSwikidesign
14113011fb8bSMichael Klier        // notify comment subscribers
14123011fb8bSMichael Klier        if (!empty($subscribers)) {
14133011fb8bSMichael Klier
14149881d835SMichael Klier            foreach ($subscribers as $mail => $data) {
1415451c1100SMichael Hamann                $mailer->bcc($mail);
14169881d835SMichael Klier                if ($data['active']) {
14173c4953e9SMichael Hamann                    $replace['UNSUBSCRIBE'] = wl($ID, 'do=discussion_unsubscribe&hash=' . $data['hash'], true, '&');
14183011fb8bSMichael Klier
1419451c1100SMichael Hamann                    $mailer->subject($subject_notify);
1420451c1100SMichael Hamann                    $mailer->setBody($notify_text, $replace);
1421451c1100SMichael Hamann                    $mailer->send();
1422c3413364SGerrit Uitslag                } elseif (!$data['confirmsent']) {
14233c4953e9SMichael Hamann                    $confirm_replace['SUBSCRIBE'] = wl($ID, 'do=discussion_confirmsubscribe&hash=' . $data['hash'], true, '&');
14249881d835SMichael Klier
1425451c1100SMichael Hamann                    $mailer->subject($subject_subscribe);
1426451c1100SMichael Hamann                    $mailer->setBody($confirm_text, $confirm_replace);
1427451c1100SMichael Hamann                    $mailer->send();
14289881d835SMichael Klier                    $subscribers[$mail]['confirmsent'] = true;
14299881d835SMichael Klier                }
14303011fb8bSMichael Klier            }
14313011fb8bSMichael Klier        }
1432f0fda08aSwikidesign    }
1433f0fda08aSwikidesign
1434f0fda08aSwikidesign    /**
1435f0fda08aSwikidesign     * Counts the number of visible comments
1436de7e6f00SGerrit Uitslag     *
1437c3413364SGerrit Uitslag     * @param array $data array with all comments
1438de7e6f00SGerrit Uitslag     * @return int
1439f0fda08aSwikidesign     */
1440283a3029SGerrit Uitslag    protected function countVisibleComments($data)
1441283a3029SGerrit Uitslag    {
1442f0fda08aSwikidesign        $number = 0;
1443de7e6f00SGerrit Uitslag        foreach ($data['comments'] as $comment) {
1444f0fda08aSwikidesign            if ($comment['parent']) continue;
1445f0fda08aSwikidesign            if (!$comment['show']) continue;
1446c3413364SGerrit Uitslag
1447f0fda08aSwikidesign            $number++;
1448f0fda08aSwikidesign            $rids = $comment['replies'];
14494cded5e1SGerrit Uitslag            if (count($rids)) {
1450c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14514cded5e1SGerrit Uitslag            }
1452f0fda08aSwikidesign        }
1453f0fda08aSwikidesign        return $number;
1454f0fda08aSwikidesign    }
1455f0fda08aSwikidesign
1456de7e6f00SGerrit Uitslag    /**
1457c3413364SGerrit Uitslag     * Count visible replies on the comments
1458c3413364SGerrit Uitslag     *
1459de7e6f00SGerrit Uitslag     * @param array $data
1460de7e6f00SGerrit Uitslag     * @param array $rids
1461c3413364SGerrit Uitslag     * @return int counted replies
1462de7e6f00SGerrit Uitslag     */
1463283a3029SGerrit Uitslag    protected function countVisibleReplies(&$data, $rids)
1464283a3029SGerrit Uitslag    {
1465f0fda08aSwikidesign        $number = 0;
1466f0fda08aSwikidesign        foreach ($rids as $rid) {
14672ee3dca3Swikidesign            if (!isset($data['comments'][$rid])) continue; // reply was removed
1468f0fda08aSwikidesign            if (!$data['comments'][$rid]['show']) continue;
1469c3413364SGerrit Uitslag
1470f0fda08aSwikidesign            $number++;
1471f0fda08aSwikidesign            $rids = $data['comments'][$rid]['replies'];
14724cded5e1SGerrit Uitslag            if (count($rids)) {
1473c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14744cded5e1SGerrit Uitslag            }
1475f0fda08aSwikidesign        }
1476f0fda08aSwikidesign        return $number;
1477f0fda08aSwikidesign    }
1478f0fda08aSwikidesign
1479f0fda08aSwikidesign    /**
1480c3413364SGerrit Uitslag     * Renders the raw comment (wiki)text to html
1481de7e6f00SGerrit Uitslag     *
1482c3413364SGerrit Uitslag     * @param string $raw comment text
1483de7e6f00SGerrit Uitslag     * @return null|string
1484f0fda08aSwikidesign     */
1485283a3029SGerrit Uitslag    protected function renderComment($raw)
1486283a3029SGerrit Uitslag    {
1487f0fda08aSwikidesign        if ($this->getConf('wikisyntaxok')) {
1488efccf6b0SJeffrey Bergamini            // Note the warning for render_text:
1489efccf6b0SJeffrey Bergamini            //   "very ineffecient for small pieces of data - try not to use"
1490efccf6b0SJeffrey Bergamini            // in dokuwiki/inc/plugin.php
1491efccf6b0SJeffrey Bergamini            $xhtml = $this->render_text($raw);
1492f0fda08aSwikidesign        } else { // wiki syntax not allowed -> just encode special chars
149387bb4e97SMichael Klier            $xhtml = hsc(trim($raw));
149487bb4e97SMichael Klier            $xhtml = str_replace("\n", '<br />', $xhtml);
1495f0fda08aSwikidesign        }
1496f0fda08aSwikidesign        return $xhtml;
1497f0fda08aSwikidesign    }
1498f0fda08aSwikidesign
1499f0fda08aSwikidesign    /**
1500479dd10fSwikidesign     * Finds out whether there is a discussion section for the current page
1501de7e6f00SGerrit Uitslag     *
1502*76fdd2cdSGerrit Uitslag     * @param string $title set to title from metadata or empty string
1503*76fdd2cdSGerrit Uitslag     * @return bool discussion section is shown?
1504479dd10fSwikidesign     */
1505283a3029SGerrit Uitslag    protected function hasDiscussion(&$title)
1506283a3029SGerrit Uitslag    {
1507b2ac3b3bSwikidesign        global $ID;
15084a0a1bd2Swikidesign
1509c3413364SGerrit Uitslag        $file = metaFN($ID, '.comments');
1510479dd10fSwikidesign
1511c3413364SGerrit Uitslag        if (!@file_exists($file)) {
1512f0bcde18SGerrit Uitslag            if ($this->isDiscussionEnabled()) {
15132b18adb9SMichael Klier                return true;
15142b18adb9SMichael Klier            } else {
15152b18adb9SMichael Klier                return false;
15162b18adb9SMichael Klier            }
1517479dd10fSwikidesign        }
1518479dd10fSwikidesign
1519*76fdd2cdSGerrit Uitslag        $data = unserialize(io_readFile($file, false));
1520479dd10fSwikidesign
1521*76fdd2cdSGerrit Uitslag        $title = $data['title'] ?? '';
1522*76fdd2cdSGerrit Uitslag
1523*76fdd2cdSGerrit Uitslag        $num = $data['number'];
1524*76fdd2cdSGerrit Uitslag        if (!$data['status'] || ($data['status'] == 2 && $num == 0)) {
1525c3413364SGerrit Uitslag            //disabled, or closed and no comments
1526c3413364SGerrit Uitslag            return false;
1527c3413364SGerrit Uitslag        } else {
1528c3413364SGerrit Uitslag            return true;
1529c3413364SGerrit Uitslag        }
1530479dd10fSwikidesign    }
1531479dd10fSwikidesign
1532479dd10fSwikidesign    /**
1533e7c760b3Swikidesign     * Creates a new thread page
1534de7e6f00SGerrit Uitslag     *
1535de7e6f00SGerrit Uitslag     * @return string
1536e7c760b3Swikidesign     */
1537283a3029SGerrit Uitslag    protected function newThread()
1538283a3029SGerrit Uitslag    {
1539c3413364SGerrit Uitslag        global $ID, $INFO, $INPUT;
1540f0fda08aSwikidesign
1541c3413364SGerrit Uitslag        $ns = cleanID($INPUT->str('ns'));
1542c3413364SGerrit Uitslag        $title = str_replace(':', '', $INPUT->str('title'));
15432e80cd5fSwikidesign        $back = $ID;
15442e80cd5fSwikidesign        $ID = ($ns ? $ns . ':' : '') . cleanID($title);
15452e80cd5fSwikidesign        $INFO = pageinfo();
1546f0fda08aSwikidesign
1547f0fda08aSwikidesign        // check if we are allowed to create this file
15482e80cd5fSwikidesign        if ($INFO['perm'] >= AUTH_CREATE) {
1549f0fda08aSwikidesign
1550f0fda08aSwikidesign            //check if locked by anyone - if not lock for my self
15514cded5e1SGerrit Uitslag            if ($INFO['locked']) {
15524cded5e1SGerrit Uitslag                return 'locked';
15534cded5e1SGerrit Uitslag            } else {
15544cded5e1SGerrit Uitslag                lock($ID);
15554cded5e1SGerrit Uitslag            }
1556f0fda08aSwikidesign
1557f0fda08aSwikidesign            // prepare the new thread file with default stuff
15582e80cd5fSwikidesign            if (!@file_exists($INFO['filepath'])) {
1559f0fda08aSwikidesign                global $TEXT;
1560f0fda08aSwikidesign
1561c3413364SGerrit Uitslag                $TEXT = pageTemplate(($ns ? $ns . ':' : '') . $title);
15621433886fSwikidesign                if (!$TEXT) {
1563c3413364SGerrit Uitslag                    $data = ['id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back];
1564c3413364SGerrit Uitslag                    $TEXT = $this->pageTemplate($data);
15652e80cd5fSwikidesign                }
15662e80cd5fSwikidesign                return 'preview';
1567f0fda08aSwikidesign            } else {
15682e80cd5fSwikidesign                return 'edit';
1569f0fda08aSwikidesign            }
1570f0fda08aSwikidesign        } else {
15712e80cd5fSwikidesign            return 'show';
1572f0fda08aSwikidesign        }
1573f0fda08aSwikidesign    }
1574f0fda08aSwikidesign
1575e7c760b3Swikidesign    /**
157661437513Swikidesign     * Adapted version of pageTemplate() function
1577de7e6f00SGerrit Uitslag     *
1578de7e6f00SGerrit Uitslag     * @param array $data
1579de7e6f00SGerrit Uitslag     * @return string
158061437513Swikidesign     */
1581283a3029SGerrit Uitslag    protected function pageTemplate($data)
1582283a3029SGerrit Uitslag    {
1583c3413364SGerrit Uitslag        global $conf, $INFO, $INPUT;
158461437513Swikidesign
158561437513Swikidesign        $id = $data['id'];
1586c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
158761437513Swikidesign        $tpl = io_readFile(DOKU_PLUGIN . 'discussion/_template.txt');
158861437513Swikidesign
158961437513Swikidesign        // standard replacements
1590c3413364SGerrit Uitslag        $replace = [
159161437513Swikidesign            '@NS@' => $data['ns'],
159261437513Swikidesign            '@PAGE@' => strtr(noNS($id), '_', ' '),
159361437513Swikidesign            '@USER@' => $user,
159461437513Swikidesign            '@NAME@' => $INFO['userinfo']['name'],
159561437513Swikidesign            '@MAIL@' => $INFO['userinfo']['mail'],
1596d5530824SMichael Hamann            '@DATE@' => dformat(time(), $conf['dformat']),
1597c3413364SGerrit Uitslag        ];
159861437513Swikidesign
159961437513Swikidesign        // additional replacements
160061437513Swikidesign        $replace['@BACK@'] = $data['back'];
160161437513Swikidesign        $replace['@TITLE@'] = $data['title'];
160261437513Swikidesign
160361437513Swikidesign        // avatar if useavatar and avatar plugin available
1604c3413364SGerrit Uitslag        if ($this->getConf('useavatar') && !plugin_isdisabled('avatar')) {
160561437513Swikidesign            $replace['@AVATAR@'] = '{{avatar>' . $user . ' }} ';
160661437513Swikidesign        } else {
160761437513Swikidesign            $replace['@AVATAR@'] = '';
160861437513Swikidesign        }
160961437513Swikidesign
161061437513Swikidesign        // tag if tag plugin is available
1611c3413364SGerrit Uitslag        if (!plugin_isdisabled('tag')) {
161261437513Swikidesign            $replace['@TAG@'] = "\n\n{{tag>}}";
161361437513Swikidesign        } else {
161461437513Swikidesign            $replace['@TAG@'] = '';
161561437513Swikidesign        }
161661437513Swikidesign
1617c3413364SGerrit Uitslag        // perform the replacements in tpl
1618c3413364SGerrit Uitslag        return str_replace(array_keys($replace), array_values($replace), $tpl);
161961437513Swikidesign    }
162061437513Swikidesign
162161437513Swikidesign    /**
1622f7bcfbedSGerrit Uitslag     * Checks if the CAPTCHA string submitted is valid, modifies action if needed
1623e7c760b3Swikidesign     */
1624283a3029SGerrit Uitslag    protected function captchaCheck()
1625283a3029SGerrit Uitslag    {
1626c3413364SGerrit Uitslag        global $INPUT;
162796bc68a7SMichael Hamann        /** @var helper_plugin_captcha $captcha */
1628c3413364SGerrit Uitslag        if (!$captcha = $this->loadHelper('captcha', false)) {
1629c3413364SGerrit Uitslag            // CAPTCHA is disabled or not available
1630c3413364SGerrit Uitslag            return;
1631c3413364SGerrit Uitslag        }
1632e7c760b3Swikidesign
1633d578a059SMichael Hamann        if ($captcha->isEnabled() && !$captcha->check()) {
1634c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1635c3413364SGerrit Uitslag                $INPUT->set('comment', 'edit');
1636c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1637c3413364SGerrit Uitslag                $INPUT->set('comment', 'show');
16384cded5e1SGerrit Uitslag            }
1639e7c760b3Swikidesign        }
1640e7c760b3Swikidesign    }
1641e7c760b3Swikidesign
1642a1ca9e44Swikidesign    /**
1643f7bcfbedSGerrit Uitslag     * checks if the submitted reCAPTCHA string is valid, modifies action if needed
1644bd6dc08eSAdrian Schlegel     *
1645bd6dc08eSAdrian Schlegel     * @author Adrian Schlegel <adrian@liip.ch>
1646bd6dc08eSAdrian Schlegel     */
1647283a3029SGerrit Uitslag    protected function recaptchaCheck()
1648283a3029SGerrit Uitslag    {
1649c3413364SGerrit Uitslag        global $INPUT;
1650c3413364SGerrit Uitslag        /** @var helper_plugin_recaptcha $recaptcha */
1651c3413364SGerrit Uitslag        if (!$recaptcha = plugin_load('helper', 'recaptcha'))
1652bd6dc08eSAdrian Schlegel            return; // reCAPTCHA is disabled or not available
1653bd6dc08eSAdrian Schlegel
1654bd6dc08eSAdrian Schlegel        // do nothing if logged in user and no reCAPTCHA required
1655c3413364SGerrit Uitslag        if (!$recaptcha->getConf('forusers') && $INPUT->server->has('REMOTE_USER')) return;
1656bd6dc08eSAdrian Schlegel
1657c3413364SGerrit Uitslag        $response = $recaptcha->check();
1658c3413364SGerrit Uitslag        if (!$response->is_valid) {
1659bd6dc08eSAdrian Schlegel            msg($recaptcha->getLang('testfailed'), -1);
1660c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1661c3413364SGerrit Uitslag                $INPUT->str('comment', 'edit');
1662c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1663c3413364SGerrit Uitslag                $INPUT->str('comment', 'show');
16644cded5e1SGerrit Uitslag            }
1665bd6dc08eSAdrian Schlegel        }
1666bd6dc08eSAdrian Schlegel    }
1667bd6dc08eSAdrian Schlegel
1668bd6dc08eSAdrian Schlegel    /**
1669ac818938SMichael Hamann     * Add discussion plugin version to the indexer version
1670ac818938SMichael Hamann     * This means that all pages will be indexed again in order to add the comments
1671ac818938SMichael Hamann     * to the index whenever there has been a change that concerns the index content.
1672de7e6f00SGerrit Uitslag     *
1673de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1674ac818938SMichael Hamann     */
1675283a3029SGerrit Uitslag    public function addIndexVersion(Doku_Event $event)
1676283a3029SGerrit Uitslag    {
1677ac818938SMichael Hamann        $event->data['discussion'] = '0.1';
1678ac818938SMichael Hamann    }
1679ac818938SMichael Hamann
1680ac818938SMichael Hamann    /**
1681a1ca9e44Swikidesign     * Adds the comments to the index
1682de7e6f00SGerrit Uitslag     *
1683de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1684c3413364SGerrit Uitslag     * @param array $param with
1685c3413364SGerrit Uitslag     *  'id' => string 'page'/'id' for respectively INDEXER_PAGE_ADD and FULLTEXT_SNIPPET_CREATE event
1686c3413364SGerrit Uitslag     *  'text' => string 'body'/'text'
1687a1ca9e44Swikidesign     */
1688283a3029SGerrit Uitslag    public function addCommentsToIndex(Doku_Event $event, $param)
1689283a3029SGerrit Uitslag    {
1690a1ca9e44Swikidesign        // get .comments meta file name
169110b5d61eSMichael Hamann        $file = metaFN($event->data[$param['id']], '.comments');
1692a1ca9e44Swikidesign
169396b3951aSMichael Hamann        if (!@file_exists($file)) return;
169496b3951aSMichael Hamann        $data = unserialize(io_readFile($file, false));
1695c3413364SGerrit Uitslag
1696c3413364SGerrit Uitslag        // comments are turned off or no comments available to index
1697c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
1698a1ca9e44Swikidesign
1699a1ca9e44Swikidesign        // now add the comments
1700a1ca9e44Swikidesign        if (isset($data['comments'])) {
1701a1ca9e44Swikidesign            foreach ($data['comments'] as $key => $value) {
1702c3413364SGerrit Uitslag                $event->data[$param['text']] .= DOKU_LF . $this->addCommentWords($key, $data);
1703a1ca9e44Swikidesign            }
1704a1ca9e44Swikidesign        }
1705a1ca9e44Swikidesign    }
1706a1ca9e44Swikidesign
1707c3413364SGerrit Uitslag    /**
1708c3413364SGerrit Uitslag     * Checks if the phrase occurs in the comments and return event result true if matching
1709c3413364SGerrit Uitslag     *
1710c3413364SGerrit Uitslag     * @param Doku_Event $event
1711c3413364SGerrit Uitslag     */
1712283a3029SGerrit Uitslag    public function fulltextPhraseMatchInComments(Doku_Event $event)
1713283a3029SGerrit Uitslag    {
171410b5d61eSMichael Hamann        if ($event->result === true) return;
171510b5d61eSMichael Hamann
171610b5d61eSMichael Hamann        // get .comments meta file name
171710b5d61eSMichael Hamann        $file = metaFN($event->data['id'], '.comments');
171810b5d61eSMichael Hamann
171910b5d61eSMichael Hamann        if (!@file_exists($file)) return;
172010b5d61eSMichael Hamann        $data = unserialize(io_readFile($file, false));
1721c3413364SGerrit Uitslag
1722c3413364SGerrit Uitslag        // comments are turned off or no comments available to match
1723c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
172410b5d61eSMichael Hamann
172510b5d61eSMichael Hamann        $matched = false;
172610b5d61eSMichael Hamann
172710b5d61eSMichael Hamann        // now add the comments
172810b5d61eSMichael Hamann        if (isset($data['comments'])) {
1729c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
1730c3413364SGerrit Uitslag                $matched = $this->phraseMatchInComment($event->data['phrase'], $cid, $data);
173110b5d61eSMichael Hamann                if ($matched) break;
173210b5d61eSMichael Hamann            }
173310b5d61eSMichael Hamann        }
173410b5d61eSMichael Hamann
1735c3413364SGerrit Uitslag        if ($matched) {
173610b5d61eSMichael Hamann            $event->result = true;
173710b5d61eSMichael Hamann        }
1738c3413364SGerrit Uitslag    }
173910b5d61eSMichael Hamann
1740c3413364SGerrit Uitslag    /**
1741c3413364SGerrit Uitslag     * Match the phrase in the comment and its replies
1742c3413364SGerrit Uitslag     *
1743c3413364SGerrit Uitslag     * @param string $phrase phrase to search
1744c3413364SGerrit Uitslag     * @param string $cid comment id
1745c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1746c3413364SGerrit Uitslag     * @param string $parent cid of parent
1747c3413364SGerrit Uitslag     * @return bool if match true, otherwise false
1748c3413364SGerrit Uitslag     */
1749283a3029SGerrit Uitslag    protected function phraseMatchInComment($phrase, $cid, &$data, $parent = '')
1750283a3029SGerrit Uitslag    {
175110b5d61eSMichael Hamann        if (!isset($data['comments'][$cid])) return false; // comment was removed
1752c3413364SGerrit Uitslag
175310b5d61eSMichael Hamann        $comment = $data['comments'][$cid];
175410b5d61eSMichael Hamann
175510b5d61eSMichael Hamann        if (!is_array($comment)) return false;             // corrupt datatype
175610b5d61eSMichael Hamann        if ($comment['parent'] != $parent) return false;   // reply to an other comment
175710b5d61eSMichael Hamann        if (!$comment['show']) return false;               // hidden comment
175810b5d61eSMichael Hamann
1759c3413364SGerrit Uitslag        $text = PhpString::strtolower($comment['raw']);
176010b5d61eSMichael Hamann        if (strpos($text, $phrase) !== false) {
176110b5d61eSMichael Hamann            return true;
176210b5d61eSMichael Hamann        }
176310b5d61eSMichael Hamann
176410b5d61eSMichael Hamann        if (is_array($comment['replies'])) {               // and the replies
176510b5d61eSMichael Hamann            foreach ($comment['replies'] as $rid) {
1766c3413364SGerrit Uitslag                if ($this->phraseMatchInComment($phrase, $rid, $data, $cid)) {
176710b5d61eSMichael Hamann                    return true;
176810b5d61eSMichael Hamann                }
176910b5d61eSMichael Hamann            }
177010b5d61eSMichael Hamann        }
177110b5d61eSMichael Hamann        return false;
177210b5d61eSMichael Hamann    }
177310b5d61eSMichael Hamann
1774a1ca9e44Swikidesign    /**
1775c3413364SGerrit Uitslag     * Saves the current comment status and title from metadata into the .comments file
1776de7e6f00SGerrit Uitslag     *
1777de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1778c1530f74SMichael Hamann     */
1779283a3029SGerrit Uitslag    public function update_comment_status(Doku_Event $event)
1780283a3029SGerrit Uitslag    {
1781c1530f74SMichael Hamann        global $ID;
1782c1530f74SMichael Hamann
1783c1530f74SMichael Hamann        $meta = $event->data['current'];
1784c1530f74SMichael Hamann        $file = metaFN($ID, '.comments');
1785f0bcde18SGerrit Uitslag        $status = ($this->isDiscussionEnabled() ? 1 : 0);
1786c3413364SGerrit Uitslag        $title = null;
1787c1530f74SMichael Hamann        if (isset($meta['plugin_discussion'])) {
1788c3413364SGerrit Uitslag            $status = $meta['plugin_discussion']['status']; // 0, 1 or 2
1789c1530f74SMichael Hamann            $title = $meta['plugin_discussion']['title'];
1790c1530f74SMichael Hamann        } elseif ($status == 1) {
1791c1530f74SMichael Hamann            // Don't enable comments when automatic comments are on - this already happens automatically
1792c1530f74SMichael Hamann            // and if comments are turned off in the admin this only updates the .comments file
1793c1530f74SMichael Hamann            return;
1794c1530f74SMichael Hamann        }
1795c1530f74SMichael Hamann
1796c1530f74SMichael Hamann        if ($status || @file_exists($file)) {
1797c3413364SGerrit Uitslag            $data = [];
1798c1530f74SMichael Hamann            if (@file_exists($file)) {
1799c1530f74SMichael Hamann                $data = unserialize(io_readFile($file, false));
1800c1530f74SMichael Hamann            }
1801c1530f74SMichael Hamann
1802c1530f74SMichael Hamann            if (!array_key_exists('title', $data) || $data['title'] !== $title || !isset($data['status']) || $data['status'] !== $status) {
1803c1530f74SMichael Hamann                $data['title'] = $title;
1804c1530f74SMichael Hamann                $data['status'] = $status;
1805c3413364SGerrit Uitslag                if (!isset($data['number'])) {
1806c1530f74SMichael Hamann                    $data['number'] = 0;
1807c3413364SGerrit Uitslag                }
1808c1530f74SMichael Hamann                io_saveFile($file, serialize($data));
1809c1530f74SMichael Hamann            }
1810c1530f74SMichael Hamann        }
1811c1530f74SMichael Hamann    }
1812c1530f74SMichael Hamann
1813c1530f74SMichael Hamann    /**
1814c3413364SGerrit Uitslag     * Return words of a given comment and its replies, suitable to be added to the index
1815de7e6f00SGerrit Uitslag     *
1816c3413364SGerrit Uitslag     * @param string $cid comment id
1817c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1818c3413364SGerrit Uitslag     * @param string $parent cid of parent
1819de7e6f00SGerrit Uitslag     * @return string
1820a1ca9e44Swikidesign     */
1821283a3029SGerrit Uitslag    protected function addCommentWords($cid, &$data, $parent = '')
1822283a3029SGerrit Uitslag    {
1823a1ca9e44Swikidesign
1824efbe59d0Swikidesign        if (!isset($data['comments'][$cid])) return ''; // comment was removed
1825c3413364SGerrit Uitslag
1826a1ca9e44Swikidesign        $comment = $data['comments'][$cid];
1827a1ca9e44Swikidesign
1828efbe59d0Swikidesign        if (!is_array($comment)) return '';             // corrupt datatype
1829efbe59d0Swikidesign        if ($comment['parent'] != $parent) return '';   // reply to an other comment
1830efbe59d0Swikidesign        if (!$comment['show']) return '';               // hidden comment
1831a1ca9e44Swikidesign
1832efbe59d0Swikidesign        $text = $comment['raw'];                        // we only add the raw comment text
1833efbe59d0Swikidesign        if (is_array($comment['replies'])) {            // and the replies
1834efbe59d0Swikidesign            foreach ($comment['replies'] as $rid) {
1835c3413364SGerrit Uitslag                $text .= $this->addCommentWords($rid, $data, $cid);
1836a1ca9e44Swikidesign            }
1837a1ca9e44Swikidesign        }
1838efbe59d0Swikidesign        return ' ' . $text;
1839efbe59d0Swikidesign    }
1840a10b5c98SMichael Klier
1841a10b5c98SMichael Klier    /**
1842a10b5c98SMichael Klier     * Only allow http(s) URLs and append http:// to URLs if needed
1843de7e6f00SGerrit Uitslag     *
1844de7e6f00SGerrit Uitslag     * @param string $url
1845de7e6f00SGerrit Uitslag     * @return string
1846a10b5c98SMichael Klier     */
1847283a3029SGerrit Uitslag    protected function checkURL($url)
1848283a3029SGerrit Uitslag    {
1849a10b5c98SMichael Klier        if (preg_match("#^http://|^https://#", $url)) {
1850a10b5c98SMichael Klier            return hsc($url);
1851a10b5c98SMichael Klier        } elseif (substr($url, 0, 4) == 'www.') {
1852c3413364SGerrit Uitslag            return hsc('https://' . $url);
1853a10b5c98SMichael Klier        } else {
1854a10b5c98SMichael Klier            return '';
1855a10b5c98SMichael Klier        }
1856a10b5c98SMichael Klier    }
185731aab30eSGina Haeussge
1858de7e6f00SGerrit Uitslag    /**
1859de7e6f00SGerrit Uitslag     * Sort threads
1860de7e6f00SGerrit Uitslag     *
1861283a3029SGerrit Uitslag     * @param array $a array with comment properties
1862283a3029SGerrit Uitslag     * @param array $b array with comment properties
1863de7e6f00SGerrit Uitslag     * @return int
1864de7e6f00SGerrit Uitslag     */
1865283a3029SGerrit Uitslag    function sortThreadsOnCreation($a, $b)
1866283a3029SGerrit Uitslag    {
18677e018cb6Slpaulsen93        if (is_array($a['date'])) {
18687e018cb6Slpaulsen93            // new format
186931aab30eSGina Haeussge            $createdA = $a['date']['created'];
18707e018cb6Slpaulsen93        } else {
18717e018cb6Slpaulsen93            // old format
187231aab30eSGina Haeussge            $createdA = $a['date'];
187331aab30eSGina Haeussge        }
187431aab30eSGina Haeussge
18757e018cb6Slpaulsen93        if (is_array($b['date'])) {
18767e018cb6Slpaulsen93            // new format
187731aab30eSGina Haeussge            $createdB = $b['date']['created'];
18787e018cb6Slpaulsen93        } else {
18797e018cb6Slpaulsen93            // old format
188031aab30eSGina Haeussge            $createdB = $b['date'];
188131aab30eSGina Haeussge        }
188231aab30eSGina Haeussge
18834cded5e1SGerrit Uitslag        if ($createdA == $createdB) {
188431aab30eSGina Haeussge            return 0;
18854cded5e1SGerrit Uitslag        } else {
188631aab30eSGina Haeussge            return ($createdA < $createdB) ? -1 : 1;
188731aab30eSGina Haeussge        }
18884cded5e1SGerrit Uitslag    }
188931aab30eSGina Haeussge
1890c3413364SGerrit Uitslag}
1891c3413364SGerrit Uitslag
1892c3413364SGerrit Uitslag
1893