xref: /plugin/discussion/action.php (revision 283a3029a8b60f309e24296d28025c9b1c450296)
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,
18c3413364SGerrit Uitslag *  'title' => string 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 */
50*283a3029SGerrit Uitslagclass action_plugin_discussion extends DokuWiki_Action_Plugin
51*283a3029SGerrit 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     */
65*283a3029SGerrit Uitslag    public function __construct()
66*283a3029SGerrit 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     */
75*283a3029SGerrit Uitslag    public function register(Doku_Event_Handler $controller)
76*283a3029SGerrit 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
94de7e6f00SGerrit Uitslag     * @param $params
95*283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
96*283a3029SGerrit Uitslag     *
97b8fdc796SMichael Klier     */
98*283a3029SGerrit Uitslag    public function ajaxPreviewComments(Doku_Event $event)
99*283a3029SGerrit Uitslag    {
100c3413364SGerrit Uitslag        global $INPUT;
101b8fdc796SMichael Klier        if ($event->data != 'discussion_preview') return;
102c3413364SGerrit Uitslag
103b8fdc796SMichael Klier        $event->preventDefault();
104b8fdc796SMichael Klier        $event->stopPropagation();
105b8fdc796SMichael Klier        print p_locale_xhtml('preview');
106b8fdc796SMichael Klier        print '<div class="comment_preview">';
107c3413364SGerrit Uitslag        if (!$INPUT->server->str('REMOTE_USER') && !$this->getConf('allowguests')) {
108b8fdc796SMichael Klier            print p_locale_xhtml('denied');
109b8fdc796SMichael Klier        } else {
110c3413364SGerrit Uitslag            print $this->renderComment($INPUT->post->str('comment'));
111b8fdc796SMichael Klier        }
112b8fdc796SMichael Klier        print '</div>';
1132d4bee9aSMichael Klier    }
1142d4bee9aSMichael Klier
1152d4bee9aSMichael Klier    /**
1165886c85bSMichael Klier     * Adds a TOC item if a discussion exists
1175886c85bSMichael Klier     *
118de7e6f00SGerrit Uitslag     * @param Doku_Event $event
119de7e6f00SGerrit Uitslag     * @param $params
120*283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
121*283a3029SGerrit Uitslag     *
1225886c85bSMichael Klier     */
123*283a3029SGerrit Uitslag    public function addDiscussionToTOC(Doku_Event $event)
124*283a3029SGerrit Uitslag    {
1258c057533SMichael Klier        global $ACT;
126c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $event->data && $ACT != 'admin') {
127c3413364SGerrit Uitslag            $tocitem = ['hid' => 'discussion__section',
1285886c85bSMichael Klier                'title' => $this->getLang('discussion'),
1295886c85bSMichael Klier                'type' => 'ul',
130c3413364SGerrit Uitslag                'level' => 1];
1315886c85bSMichael Klier
132c3413364SGerrit Uitslag            $event->data[] = $tocitem;
1335886c85bSMichael Klier        }
1345886c85bSMichael Klier    }
1355886c85bSMichael Klier
1365886c85bSMichael Klier    /**
137c3413364SGerrit Uitslag     * Modify Toolbar for use with discussion plugin
1382d4bee9aSMichael Klier     *
139de7e6f00SGerrit Uitslag     * @param Doku_Event $event
140de7e6f00SGerrit Uitslag     * @param $param
141*283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
142*283a3029SGerrit Uitslag     *
1432d4bee9aSMichael Klier     */
144*283a3029SGerrit Uitslag    public function modifyToolbar(Doku_Event $event)
145*283a3029SGerrit Uitslag    {
1462d4bee9aSMichael Klier        global $ACT;
1472d4bee9aSMichael Klier        if ($ACT != 'show') return;
1482d4bee9aSMichael Klier
149c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
150c3413364SGerrit Uitslag            $toolbar = [];
1512d4bee9aSMichael Klier            foreach ($event->data as $btn) {
1522d4bee9aSMichael Klier                if ($btn['type'] == 'mediapopup') continue;
1532d4bee9aSMichael Klier                if ($btn['type'] == 'signature') continue;
154310210d6SGina Haeussge                if ($btn['type'] == 'linkwiz') continue;
1551dc736fbSGerrit Uitslag                if ($btn['type'] == 'NewTable') continue; //skip button for Edittable Plugin
156c3413364SGerrit Uitslag                if (isset($btn['open']) && preg_match("/=+?/", $btn['open'])) continue;
157c3413364SGerrit Uitslag
158c3413364SGerrit Uitslag                $toolbar[] = $btn;
1592d4bee9aSMichael Klier            }
1602d4bee9aSMichael Klier            $event->data = $toolbar;
1612d4bee9aSMichael Klier        }
1622d4bee9aSMichael Klier    }
1632d4bee9aSMichael Klier
1642d4bee9aSMichael Klier    /**
1652d4bee9aSMichael Klier     * Dirty workaround to add a toolbar to the discussion plugin
1662d4bee9aSMichael Klier     *
167de7e6f00SGerrit Uitslag     * @param Doku_Event $event
168de7e6f00SGerrit Uitslag     * @param $param
169*283a3029SGerrit Uitslag     * @author Michael Klier <chi@chimeric.de>
170*283a3029SGerrit Uitslag     *
1712d4bee9aSMichael Klier     */
172*283a3029SGerrit Uitslag    public function addToolbarToCommentfield(Doku_Event $event)
173*283a3029SGerrit Uitslag    {
1742d4bee9aSMichael Klier        global $ACT;
1752d4bee9aSMichael Klier        global $ID;
1762d4bee9aSMichael Klier        if ($ACT != 'show') return;
1772d4bee9aSMichael Klier
178c3413364SGerrit Uitslag        if ($this->hasDiscussion($title) && $this->getConf('wikisyntaxok')) {
1792d4bee9aSMichael Klier            // FIXME ugly workaround, replace this once DW the toolbar code is more flexible
1802d4bee9aSMichael Klier            @require_once(DOKU_INC . 'inc/toolbar.php');
1812d4bee9aSMichael Klier            ob_start();
1822d4bee9aSMichael Klier            print 'NS = "' . getNS($ID) . '";'; // we have to define NS, otherwise we get get JS errors
1832d4bee9aSMichael Klier            toolbar_JSdefines('toolbar');
1842d4bee9aSMichael Klier            $script = ob_get_clean();
185c3413364SGerrit Uitslag            $event->data['script'][] = ['type' => 'text/javascript', 'charset' => "utf-8", '_data' => $script];
1862d4bee9aSMichael Klier        }
187f0fda08aSwikidesign    }
188f0fda08aSwikidesign
189f0fda08aSwikidesign    /**
190a1d93126SGina Haeussge     * Handles comment actions, dispatches data processing routines
191de7e6f00SGerrit Uitslag     *
192de7e6f00SGerrit Uitslag     * @param Doku_Event $event
193de7e6f00SGerrit Uitslag     * @param $param
194c3413364SGerrit Uitslag     * @return void
195f0fda08aSwikidesign     */
196*283a3029SGerrit Uitslag    public function handleCommentActions(Doku_Event $event)
197*283a3029SGerrit Uitslag    {
198c3413364SGerrit Uitslag        global $ID, $INFO, $lang, $INPUT;
199573e23a1Swikidesign
200a1d93126SGina Haeussge        // handle newthread ACTs
201a1d93126SGina Haeussge        if ($event->data == 'newthread') {
202a1d93126SGina Haeussge            // we can handle it -> prevent others
203c3413364SGerrit Uitslag            $event->data = $this->newThread();
204a1d93126SGina Haeussge        }
205a1d93126SGina Haeussge
206a1d93126SGina Haeussge        // enable captchas
207c3413364SGerrit Uitslag        if (in_array($INPUT->str('comment'), ['add', 'save'])) {
208c3413364SGerrit Uitslag            $this->captchaCheck();
209c3413364SGerrit Uitslag            $this->recaptchaCheck();
210bd6dc08eSAdrian Schlegel        }
211a1d93126SGina Haeussge
2123011fb8bSMichael Klier        // if we are not in show mode or someone wants to unsubscribe, that was all for now
213c3413364SGerrit Uitslag        if ($event->data != 'show'
214c3413364SGerrit Uitslag            && $event->data != 'discussion_unsubscribe'
215c3413364SGerrit Uitslag            && $event->data != 'discussion_confirmsubscribe') {
216c3413364SGerrit Uitslag            return;
217c3413364SGerrit Uitslag        }
218a1d93126SGina Haeussge
219b2f2e866SMichael Klier        if ($event->data == 'discussion_unsubscribe' or $event->data == 'discussion_confirmsubscribe') {
220c3413364SGerrit Uitslag            if ($INPUT->has('hash')) {
2213011fb8bSMichael Klier                $file = metaFN($ID, '.comments');
2223011fb8bSMichael Klier                $data = unserialize(io_readFile($file));
223c3413364SGerrit Uitslag                $matchedMail = '';
2249881d835SMichael Klier                foreach ($data['subscribers'] as $mail => $info) {
2259881d835SMichael Klier                    // convert old style subscribers just in case
2269881d835SMichael Klier                    if (!is_array($info)) {
2279881d835SMichael Klier                        $hash = $data['subscribers'][$mail];
2289881d835SMichael Klier                        $data['subscribers'][$mail]['hash'] = $hash;
2299881d835SMichael Klier                        $data['subscribers'][$mail]['active'] = true;
2309881d835SMichael Klier                        $data['subscribers'][$mail]['confirmsent'] = true;
2319881d835SMichael Klier                    }
2329881d835SMichael Klier
233c3413364SGerrit Uitslag                    if ($data['subscribers'][$mail]['hash'] == $INPUT->str('hash')) {
234c3413364SGerrit Uitslag                        $matchedMail = $mail;
2350c54624fSGina Haeussge                    }
2360c54624fSGina Haeussge                }
2370c54624fSGina Haeussge
238c3413364SGerrit Uitslag                if ($matchedMail != '') {
239b2f2e866SMichael Klier                    if ($event->data == 'discussion_unsubscribe') {
240c3413364SGerrit Uitslag                        unset($data['subscribers'][$matchedMail]);
241c3413364SGerrit Uitslag                        msg(sprintf($lang['subscr_unsubscribe_success'], $matchedMail, $ID), 1);
242c3413364SGerrit Uitslag                    } else { //$event->data == 'discussion_confirmsubscribe'
243c3413364SGerrit Uitslag                        $data['subscribers'][$matchedMail]['active'] = true;
244c3413364SGerrit Uitslag                        msg(sprintf($lang['subscr_subscribe_success'], $matchedMail, $ID), 1);
2459881d835SMichael Klier                    }
2469881d835SMichael Klier                    io_saveFile($file, serialize($data));
2473011fb8bSMichael Klier                    $event->data = 'show';
2483011fb8bSMichael Klier                }
249de7e6f00SGerrit Uitslag
2509881d835SMichael Klier            }
251c3413364SGerrit Uitslag            return;
2523011fb8bSMichael Klier        } else {
253a1d93126SGina Haeussge            // do the data processing for comments
254c3413364SGerrit Uitslag            $cid = $INPUT->str('cid');
255c3413364SGerrit Uitslag            switch ($INPUT->str('comment')) {
256f0fda08aSwikidesign                case 'add':
257c3413364SGerrit Uitslag                    if (empty($INPUT->str('text'))) return; // don't add empty comments
258c3413364SGerrit Uitslag
259c3413364SGerrit Uitslag                    if ($INPUT->server->has('REMOTE_USER') && !$this->getConf('adminimport')) {
260c3413364SGerrit Uitslag                        $comment['user']['id'] = $INPUT->server->str('REMOTE_USER');
26194c5d164SMichael Klier                        $comment['user']['name'] = $INFO['userinfo']['name'];
26294c5d164SMichael Klier                        $comment['user']['mail'] = $INFO['userinfo']['mail'];
263c3413364SGerrit Uitslag                    } elseif (($INPUT->server->has('REMOTE_USER') && $this->getConf('adminimport') && $this->helper->isDiscussionMod())
264c3413364SGerrit Uitslag                        || !$INPUT->server->has('REMOTE_USER')) {
265c3413364SGerrit Uitslag                        // don't add anonymous comments
266c3413364SGerrit Uitslag                        if (empty($INPUT->str('name')) or empty($INPUT->str('mail'))) {
267c3413364SGerrit Uitslag                            return;
268c3413364SGerrit Uitslag                        }
269c3413364SGerrit Uitslag
270c3413364SGerrit Uitslag                        if (!mail_isvalid($INPUT->str('mail'))) {
271c9d36b5eSMichael Klier                            msg($lang['regbadmail'], -1);
272c9d36b5eSMichael Klier                            return;
273c9d36b5eSMichael Klier                        } else {
274c3413364SGerrit Uitslag                            $comment['user']['id'] = 'test' . hsc($INPUT->str('user'));
275c3413364SGerrit Uitslag                            $comment['user']['name'] = hsc($INPUT->str('name'));
276c3413364SGerrit Uitslag                            $comment['user']['mail'] = hsc($INPUT->str('mail'));
27794c5d164SMichael Klier                        }
278c9d36b5eSMichael Klier                    }
279c3413364SGerrit Uitslag                    $comment['user']['address'] = ($this->getConf('addressfield')) ? hsc($INPUT->str('address')) : '';
280c3413364SGerrit Uitslag                    $comment['user']['url'] = ($this->getConf('urlfield')) ? $this->checkURL($INPUT->str('url')) : '';
281c3413364SGerrit Uitslag                    $comment['subscribe'] = ($this->getConf('subscribe')) ? $INPUT->has('subscribe') : '';
282c3413364SGerrit Uitslag                    $comment['date'] = ['created' => $INPUT->str('date')];
283c3413364SGerrit Uitslag                    $comment['raw'] = cleanText($INPUT->str('text'));
284c3413364SGerrit Uitslag                    $reply = $INPUT->str('reply');
285e6b2f142Slupo49                    if ($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
286a44bc9f7SMichael Klier                        $comment['show'] = false;
287a44bc9f7SMichael Klier                    } else {
288a44bc9f7SMichael Klier                        $comment['show'] = true;
289a44bc9f7SMichael Klier                    }
290c3413364SGerrit Uitslag                    $this->add($comment, $reply);
291f0fda08aSwikidesign                    break;
292f0fda08aSwikidesign
293f0fda08aSwikidesign                case 'save':
294c3413364SGerrit Uitslag                    $raw = cleanText($INPUT->str('text'));
295c3413364SGerrit Uitslag                    $this->save([$cid], $raw);
296f0fda08aSwikidesign                    break;
297f0fda08aSwikidesign
2981e46d176Swikidesign                case 'delete':
299c3413364SGerrit Uitslag                    $this->save([$cid], '');
3002ee3dca3Swikidesign                    break;
3011e46d176Swikidesign
302f0fda08aSwikidesign                case 'toogle':
303c3413364SGerrit Uitslag                    $this->save([$cid], '', 'toogle');
304f0fda08aSwikidesign                    break;
305a1d93126SGina Haeussge            }
3063011fb8bSMichael Klier        }
307a1d93126SGina Haeussge    }
308a1d93126SGina Haeussge
309a1d93126SGina Haeussge    /**
310a1d93126SGina Haeussge     * Main function; dispatches the visual comment actions
311a1d93126SGina Haeussge     */
312*283a3029SGerrit Uitslag    public function renderCommentsSection(Doku_Event $event)
313*283a3029SGerrit Uitslag    {
314c3413364SGerrit Uitslag        global $INPUT;
315a1d93126SGina Haeussge        if ($event->data != 'show') return; // nothing to do for us
316a1d93126SGina Haeussge
317c3413364SGerrit Uitslag        $cid = $INPUT->str('cid');
318c3413364SGerrit Uitslag
319aa0d6cd7SGerrit Uitslag        if (!$cid) {
320c3413364SGerrit Uitslag            $cid = $INPUT->str('reply');
321aa0d6cd7SGerrit Uitslag        }
322*283a3029SGerrit Uitslag
323c3413364SGerrit Uitslag        switch ($INPUT->str('comment')) {
324a1d93126SGina Haeussge            case 'edit':
325c3413364SGerrit Uitslag                $this->showDiscussionSection(null, $cid);
326a1d93126SGina Haeussge                break;
327*283a3029SGerrit Uitslag            default: //'reply' or no action specified
328c3413364SGerrit Uitslag                $this->showDiscussionSection($cid);
3292b18adb9SMichael Klier                break;
330f0fda08aSwikidesign        }
331f0fda08aSwikidesign    }
332f0fda08aSwikidesign
333f0fda08aSwikidesign    /**
334a1d93126SGina Haeussge     * Redirects browser to given comment anchor
335a1d93126SGina Haeussge     */
336*283a3029SGerrit Uitslag    protected function redirect($cid)
337*283a3029SGerrit Uitslag    {
338a1d93126SGina Haeussge        global $ID;
339a1d93126SGina Haeussge        global $ACT;
340a1d93126SGina Haeussge
341a1d93126SGina Haeussge        if ($ACT !== 'show') return;
342a44bc9f7SMichael Klier
343e6b2f142Slupo49        if ($this->getConf('moderate') && !$this->helper->isDiscussionMod()) {
344a44bc9f7SMichael Klier            msg($this->getLang('moderation'), 1);
345a44bc9f7SMichael Klier            @session_start();
346a44bc9f7SMichael Klier            global $MSG;
347a44bc9f7SMichael Klier            $_SESSION[DOKU_COOKIE]['msg'] = $MSG;
348a44bc9f7SMichael Klier            session_write_close();
349a44bc9f7SMichael Klier            $url = wl($ID);
350a44bc9f7SMichael Klier        } else {
351a44bc9f7SMichael Klier            $url = wl($ID) . '#comment_' . $cid;
352a44bc9f7SMichael Klier        }
353e88de38aSAxel Beckert
354e88de38aSAxel Beckert        if (function_exists('send_redirect')) {
355e88de38aSAxel Beckert            send_redirect($url);
356e88de38aSAxel Beckert        } else {
35769f42a0bSAxel Beckert            header('Location: ' . $url);
358e88de38aSAxel Beckert        }
3596d1f4f20SGina Haeussge        exit();
360a1d93126SGina Haeussge    }
361a1d93126SGina Haeussge
362a1d93126SGina Haeussge    /**
363d406a452SGerrit Uitslag     * Checks config settings to enable/disable discussions
364f0bcde18SGerrit Uitslag     *
365f0bcde18SGerrit Uitslag     * @return bool
366f0bcde18SGerrit Uitslag     */
367*283a3029SGerrit Uitslag    public function isDiscussionEnabled()
368*283a3029SGerrit Uitslag    {
369f0bcde18SGerrit Uitslag        global $INFO;
370f0bcde18SGerrit Uitslag
371d406a452SGerrit Uitslag        if ($this->getConf('excluded_ns') == '') {
372d406a452SGerrit Uitslag            $isNamespaceExcluded = false;
373d406a452SGerrit Uitslag        } else {
374*283a3029SGerrit Uitslag            global $ID;
375*283a3029SGerrit Uitslag            $ns = getNS($ID); // $INFO['namespace'] is not yet available, if used in update_comment_status()
376f0bcde18SGerrit Uitslag            $isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $INFO['namespace']);
377d406a452SGerrit Uitslag        }
378f0bcde18SGerrit Uitslag
379f0bcde18SGerrit Uitslag        if ($this->getConf('automatic')) {
380f0bcde18SGerrit Uitslag            if ($isNamespaceExcluded) {
381f0bcde18SGerrit Uitslag                return false;
382f0bcde18SGerrit Uitslag            } else {
383f0bcde18SGerrit Uitslag                return true;
384f0bcde18SGerrit Uitslag            }
385f0bcde18SGerrit Uitslag        } else {
386f0bcde18SGerrit Uitslag            if ($isNamespaceExcluded) {
387f0bcde18SGerrit Uitslag                return true;
388f0bcde18SGerrit Uitslag            } else {
389f0bcde18SGerrit Uitslag                return false;
390f0bcde18SGerrit Uitslag            }
391f0bcde18SGerrit Uitslag        }
392f0bcde18SGerrit Uitslag    }
393f0bcde18SGerrit Uitslag
394f0bcde18SGerrit Uitslag    /**
395c3413364SGerrit Uitslag     * Shows all comments of the current page, if no reply or edit requested, then comment form is shown on the end
396c3413364SGerrit Uitslag     *
397c3413364SGerrit Uitslag     * @param null|string $reply comment id on which the user requested a reply
398c3413364SGerrit Uitslag     * @param null|string $edit comment id which the user requested for editing
399f0fda08aSwikidesign     */
400*283a3029SGerrit Uitslag    protected function showDiscussionSection($reply = null, $edit = null)
401*283a3029SGerrit Uitslag    {
402c3413364SGerrit Uitslag        global $ID, $INFO, $INPUT;
403573e23a1Swikidesign
404479dd10fSwikidesign        // get .comments meta file name
405f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
406f0fda08aSwikidesign
407c3413364SGerrit Uitslag        if (!$INFO['exists']) return;
408c3413364SGerrit Uitslag        if (!@file_exists($file) && !$this->isDiscussionEnabled()) return;
409c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('showguests')) return;
410f0fda08aSwikidesign
4112b18adb9SMichael Klier        // load data
412c3413364SGerrit Uitslag        $data = [];
4132b18adb9SMichael Klier        if (@file_exists($file)) {
4142b18adb9SMichael Klier            $data = unserialize(io_readFile($file, false));
415c3413364SGerrit Uitslag            // comments are turned off
416c3413364SGerrit Uitslag            if (!$data['status']) {
417c3413364SGerrit Uitslag                return;
418c3413364SGerrit Uitslag            }
419f0bcde18SGerrit Uitslag        } elseif (!@file_exists($file) && $this->isDiscussionEnabled() && $INFO['exists']) {
4202b18adb9SMichael Klier            // set status to show the comment form
4212b18adb9SMichael Klier            $data['status'] = 1;
4222b18adb9SMichael Klier            $data['number'] = 0;
4232b18adb9SMichael Klier        }
424f0fda08aSwikidesign
425a1599850SMichael Klier        // show discussion wrapper only on certain circumstances
426c3413364SGerrit Uitslag        if (empty($data['comments']) || !is_array($data['comments'])) {
427c3413364SGerrit Uitslag            $cnt = 0;
4283e19949cSMark Prins            $keys = [];
429c3413364SGerrit Uitslag        } else {
430c3413364SGerrit Uitslag            $cnt = count($data['comments']);
431c3413364SGerrit Uitslag            $keys = array_keys($data['comments']);
4323e19949cSMark Prins        }
433c3413364SGerrit Uitslag
434de7e6f00SGerrit Uitslag        $show = false;
435c3413364SGerrit Uitslag        if ($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1)
436c3413364SGerrit Uitslag            || $this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) {
437a1599850SMichael Klier            $show = true;
438f0fda08aSwikidesign            // section title
43907c376bbSwikidesign            $title = ($data['title'] ? hsc($data['title']) : $this->getLang('discussion'));
44046178401Slupo49            ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section
4414a0a1bd2Swikidesign            ptln('<h2><a name="discussion__section" id="discussion__section">', 2);
4424a0a1bd2Swikidesign            ptln($title, 4);
4434a0a1bd2Swikidesign            ptln('</a></h2>', 2);
4444a0a1bd2Swikidesign            ptln('<div class="level2 hfeed">', 2);
445a1599850SMichael Klier        }
446a1599850SMichael Klier
447f0fda08aSwikidesign        // now display the comments
448f0fda08aSwikidesign        if (isset($data['comments'])) {
44931aab30eSGina Haeussge            if (!$this->getConf('usethreading')) {
450c3413364SGerrit Uitslag                $data['comments'] = $this->flattenThreads($data['comments']);
451*283a3029SGerrit Uitslag                uasort($data['comments'], [$this, 'sortThreadsOnCreation']);
45231aab30eSGina Haeussge            }
453dbd9d5cdSMichael Klier            if ($this->getConf('newestfirst')) {
454dbd9d5cdSMichael Klier                $data['comments'] = array_reverse($data['comments']);
455dbd9d5cdSMichael Klier            }
456c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
457c3413364SGerrit Uitslag                if ($cid == $edit) { // edit form
458c3413364SGerrit Uitslag                    $this->showCommentForm($value['raw'], 'save', $edit);
459c3413364SGerrit Uitslag                } else {
460c3413364SGerrit Uitslag                    $this->showCommentWithReplies($cid, $data, '', $reply);
461c3413364SGerrit Uitslag                }
462f0fda08aSwikidesign            }
463f0fda08aSwikidesign        }
464f0fda08aSwikidesign
465c3413364SGerrit Uitslag        // comment form shown on the end, if no comment form of $reply or $edit is requested before
466c3413364SGerrit Uitslag        if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) {
467c3413364SGerrit Uitslag            $this->showCommentForm('');
468c3413364SGerrit Uitslag        }
469f0fda08aSwikidesign
470a1599850SMichael Klier        if ($show) {
4714a0a1bd2Swikidesign            ptln('</div>', 2); // level2 hfeed
4724a0a1bd2Swikidesign            ptln('</div>'); // comment_wrapper
473a1599850SMichael Klier        }
474f0fda08aSwikidesign
47546178401Slupo49        // check for toggle print configuration
47646178401Slupo49        if ($this->getConf('visibilityButton')) {
47746178401Slupo49            // print the hide/show discussion section button
478c3413364SGerrit Uitslag            $this->showDiscussionToggleButton();
47946178401Slupo49        }
480f0fda08aSwikidesign    }
481f0fda08aSwikidesign
482de7e6f00SGerrit Uitslag    /**
483c3413364SGerrit Uitslag     * Remove the parent-child relation, such that the comment structure becomes flat
484c3413364SGerrit Uitslag     *
485c3413364SGerrit Uitslag     * @param array $comments array with all comments
486c3413364SGerrit Uitslag     * @param null|array $cids comment ids of replies, which should be flatten
487c3413364SGerrit Uitslag     * @return array returned array with flattened comment structure
488de7e6f00SGerrit Uitslag     */
489*283a3029SGerrit Uitslag    protected function flattenThreads($comments, $cids = null)
490*283a3029SGerrit Uitslag    {
491c3413364SGerrit Uitslag        if (is_null($cids)) {
492c3413364SGerrit Uitslag            $cids = array_keys($comments);
493c3413364SGerrit Uitslag        }
49431aab30eSGina Haeussge
495c3413364SGerrit Uitslag        foreach ($cids as $cid) {
49631aab30eSGina Haeussge            if (!empty($comments[$cid]['replies'])) {
49731aab30eSGina Haeussge                $rids = $comments[$cid]['replies'];
498c3413364SGerrit Uitslag                $comments = $this->flattenThreads($comments, $rids);
499c3413364SGerrit Uitslag                $comments[$cid]['replies'] = [];
50031aab30eSGina Haeussge            }
50131aab30eSGina Haeussge            $comments[$cid]['parent'] = '';
50231aab30eSGina Haeussge        }
50331aab30eSGina Haeussge        return $comments;
50431aab30eSGina Haeussge    }
50531aab30eSGina Haeussge
506f0fda08aSwikidesign    /**
507f0fda08aSwikidesign     * Adds a new comment and then displays all comments
508de7e6f00SGerrit Uitslag     *
509c3413364SGerrit Uitslag     * @param array $comment with
510c3413364SGerrit Uitslag     *  'raw' => string comment text,
511c3413364SGerrit Uitslag     *  'user' => [
512c3413364SGerrit Uitslag     *      'id' => string,
513c3413364SGerrit Uitslag     *      'name' => string,
514c3413364SGerrit Uitslag     *      'mail' => string
515c3413364SGerrit Uitslag     *  ],
516c3413364SGerrit Uitslag     *  'date' => [
517c3413364SGerrit Uitslag     *      'created' => int timestamp
518c3413364SGerrit Uitslag     *  ]
519c3413364SGerrit Uitslag     *  'show' => bool
520c3413364SGerrit Uitslag     *  'subscribe' => bool
521c3413364SGerrit Uitslag     * @param string $parent comment id of parent
522de7e6f00SGerrit Uitslag     * @return bool
523f0fda08aSwikidesign     */
524*283a3029SGerrit Uitslag    protected function add($comment, $parent)
525*283a3029SGerrit Uitslag    {
526c3413364SGerrit Uitslag        global $ID, $TEXT, $INPUT;
527f0fda08aSwikidesign
528c3413364SGerrit Uitslag        $originalTxt = $TEXT; // set $TEXT to comment text for wordblock check
529f0fda08aSwikidesign        $TEXT = $comment['raw'];
530f0fda08aSwikidesign
531f0fda08aSwikidesign        // spamcheck against the DokuWiki blacklist
532f0fda08aSwikidesign        if (checkwordblock()) {
533f0fda08aSwikidesign            msg($this->getLang('wordblock'), -1);
534f0fda08aSwikidesign            return false;
535f0fda08aSwikidesign        }
536f0fda08aSwikidesign
537c3413364SGerrit Uitslag        if (!$this->getConf('allowguests')
538c3413364SGerrit Uitslag            && $comment['user']['id'] != $INPUT->server->str('REMOTE_USER')
5394cded5e1SGerrit Uitslag        ) {
5403011fb8bSMichael Klier            return false; // guest comments not allowed
5414cded5e1SGerrit Uitslag        }
5423011fb8bSMichael Klier
543c3413364SGerrit Uitslag        $TEXT = $originalTxt; // restore global $TEXT
544f0fda08aSwikidesign
545f0fda08aSwikidesign        // get discussion meta file name
546f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
547f0fda08aSwikidesign
5482b18adb9SMichael Klier        // create comments file if it doesn't exist yet
5492b18adb9SMichael Klier        if (!@file_exists($file)) {
550c3413364SGerrit Uitslag            $data = ['status' => 1, 'number' => 0];
5512b18adb9SMichael Klier            io_saveFile($file, serialize($data));
5522b18adb9SMichael Klier        } else {
553f0fda08aSwikidesign            $data = unserialize(io_readFile($file, false));
554c3413364SGerrit Uitslag            // comments off or closed
555c3413364SGerrit Uitslag            if ($data['status'] != 1) {
556c3413364SGerrit Uitslag                return false;
557c3413364SGerrit Uitslag            }
5582b18adb9SMichael Klier        }
5592b18adb9SMichael Klier
5603011fb8bSMichael Klier        if ($comment['date']['created']) {
5613011fb8bSMichael Klier            $date = strtotime($comment['date']['created']);
5623011fb8bSMichael Klier        } else {
5633011fb8bSMichael Klier            $date = time();
5643011fb8bSMichael Klier        }
565f0fda08aSwikidesign
5663011fb8bSMichael Klier        if ($date == -1) {
5673011fb8bSMichael Klier            $date = time();
5683011fb8bSMichael Klier        }
5693011fb8bSMichael Klier
5706046f25cSwikidesign        $cid = md5($comment['user']['id'] . $date); // create a unique id
571f0fda08aSwikidesign
5723011fb8bSMichael Klier        if (!is_array($data['comments'][$parent])) {
573c3413364SGerrit Uitslag            $parent = null; // invalid parent comment
5743011fb8bSMichael Klier        }
575f0fda08aSwikidesign
576f0fda08aSwikidesign        // render the comment
577c3413364SGerrit Uitslag        $xhtml = $this->renderComment($comment['raw']);
578f0fda08aSwikidesign
579f0fda08aSwikidesign        // fill in the new comment
580c3413364SGerrit Uitslag        $data['comments'][$cid] = [
5816046f25cSwikidesign            'user' => $comment['user'],
582c3413364SGerrit Uitslag            'date' => ['created' => $date],
5836046f25cSwikidesign            'raw' => $comment['raw'],
584f0fda08aSwikidesign            'xhtml' => $xhtml,
585f0fda08aSwikidesign            'parent' => $parent,
586c3413364SGerrit Uitslag            'replies' => [],
587a44bc9f7SMichael Klier            'show' => $comment['show']
588c3413364SGerrit Uitslag        ];
589f0fda08aSwikidesign
5903011fb8bSMichael Klier        if ($comment['subscribe']) {
5913011fb8bSMichael Klier            $mail = $comment['user']['mail'];
5923011fb8bSMichael Klier            if ($data['subscribers']) {
5933011fb8bSMichael Klier                if (!$data['subscribers'][$mail]) {
5949881d835SMichael Klier                    $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
5959881d835SMichael Klier                    $data['subscribers'][$mail]['active'] = false;
5969881d835SMichael Klier                    $data['subscribers'][$mail]['confirmsent'] = false;
5979881d835SMichael Klier                } else {
5989881d835SMichael Klier                    // convert old style subscribers and set them active
5999881d835SMichael Klier                    if (!is_array($data['subscribers'][$mail])) {
6009881d835SMichael Klier                        $hash = $data['subscribers'][$mail];
6019881d835SMichael Klier                        $data['subscribers'][$mail]['hash'] = $hash;
6029881d835SMichael Klier                        $data['subscribers'][$mail]['active'] = true;
6039881d835SMichael Klier                        $data['subscribers'][$mail]['confirmsent'] = true;
6049881d835SMichael Klier                    }
6053011fb8bSMichael Klier                }
6063011fb8bSMichael Klier            } else {
6079881d835SMichael Klier                $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand());
6089881d835SMichael Klier                $data['subscribers'][$mail]['active'] = false;
6099881d835SMichael Klier                $data['subscribers'][$mail]['confirmsent'] = false;
6103011fb8bSMichael Klier            }
6113011fb8bSMichael Klier        }
6123011fb8bSMichael Klier
613f0fda08aSwikidesign        // update parent comment
6144cded5e1SGerrit Uitslag        if ($parent) {
6154cded5e1SGerrit Uitslag            $data['comments'][$parent]['replies'][] = $cid;
6164cded5e1SGerrit Uitslag        }
617f0fda08aSwikidesign
618f0fda08aSwikidesign        // update the number of comments
619f0fda08aSwikidesign        $data['number']++;
620f0fda08aSwikidesign
621f0fda08aSwikidesign        // notify subscribers of the page
6228b42cefbSMichael Klier        $data['comments'][$cid]['cid'] = $cid;
623c3413364SGerrit Uitslag        $this->notify($data['comments'][$cid], $data['subscribers']);
624f0fda08aSwikidesign
6259881d835SMichael Klier        // save the comment metadata file
6269881d835SMichael Klier        io_saveFile($file, serialize($data));
627c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, 'cc', '', $cid);
6289881d835SMichael Klier
629c3413364SGerrit Uitslag        $this->redirect($cid);
630f0fda08aSwikidesign        return true;
631f0fda08aSwikidesign    }
632f0fda08aSwikidesign
633f0fda08aSwikidesign    /**
634f0fda08aSwikidesign     * Saves the comment with the given ID and then displays all comments
635de7e6f00SGerrit Uitslag     *
636c3413364SGerrit Uitslag     * @param array|string $cids array with comment ids to save, or a single string comment id
637c3413364SGerrit Uitslag     * @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!)
638c3413364SGerrit Uitslag     * @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw
639c3413364SGerrit Uitslag     * @return bool succeed?
640f0fda08aSwikidesign     */
641*283a3029SGerrit Uitslag    public function save($cids, $raw, $act = null)
642*283a3029SGerrit Uitslag    {
643c3413364SGerrit Uitslag        global $ID, $INPUT;
644f0fda08aSwikidesign
645c3413364SGerrit Uitslag        if (empty($cids)) return false; // do nothing if we get no comment id
646757550e8SMichael Klier
6472ee3dca3Swikidesign        if ($raw) {
6482ee3dca3Swikidesign            global $TEXT;
6492ee3dca3Swikidesign
650f0fda08aSwikidesign            $otxt = $TEXT; // set $TEXT to comment text for wordblock check
651f0fda08aSwikidesign            $TEXT = $raw;
652f0fda08aSwikidesign
653f0fda08aSwikidesign            // spamcheck against the DokuWiki blacklist
654f0fda08aSwikidesign            if (checkwordblock()) {
655f0fda08aSwikidesign                msg($this->getLang('wordblock'), -1);
656f0fda08aSwikidesign                return false;
657f0fda08aSwikidesign            }
658f0fda08aSwikidesign
659f0fda08aSwikidesign            $TEXT = $otxt; // restore global $TEXT
6602ee3dca3Swikidesign        }
661f0fda08aSwikidesign
662f0fda08aSwikidesign        // get discussion meta file name
663f0fda08aSwikidesign        $file = metaFN($ID, '.comments');
664f0fda08aSwikidesign        $data = unserialize(io_readFile($file, false));
665f0fda08aSwikidesign
666c3413364SGerrit Uitslag        if (!is_array($cids)) {
667c3413364SGerrit Uitslag            $cids = [$cids];
668c3413364SGerrit Uitslag        }
669264b7327Swikidesign        foreach ($cids as $cid) {
670264b7327Swikidesign
6716046f25cSwikidesign            if (is_array($data['comments'][$cid]['user'])) {
6726046f25cSwikidesign                $user = $data['comments'][$cid]['user']['id'];
6736046f25cSwikidesign                $convert = false;
6746046f25cSwikidesign            } else {
6756046f25cSwikidesign                $user = $data['comments'][$cid]['user'];
6766046f25cSwikidesign                $convert = true;
6776046f25cSwikidesign            }
6786046f25cSwikidesign
679f0fda08aSwikidesign            // someone else was trying to edit our comment -> abort
680c3413364SGerrit Uitslag            if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionMod()) {
681c3413364SGerrit Uitslag                return false;
682c3413364SGerrit Uitslag            }
683f0fda08aSwikidesign
684f0fda08aSwikidesign            $date = time();
685f0fda08aSwikidesign
6866046f25cSwikidesign            // need to convert to new format?
6876046f25cSwikidesign            if ($convert) {
688c3413364SGerrit Uitslag                $data['comments'][$cid]['user'] = [
6896046f25cSwikidesign                    'id' => $user,
6906046f25cSwikidesign                    'name' => $data['comments'][$cid]['name'],
6916046f25cSwikidesign                    'mail' => $data['comments'][$cid]['mail'],
6926046f25cSwikidesign                    'url' => $data['comments'][$cid]['url'],
6936046f25cSwikidesign                    'address' => $data['comments'][$cid]['address'],
694c3413364SGerrit Uitslag                ];
695c3413364SGerrit Uitslag                $data['comments'][$cid]['date'] = [
6966046f25cSwikidesign                    'created' => $data['comments'][$cid]['date']
697c3413364SGerrit Uitslag                ];
6986046f25cSwikidesign            }
6996046f25cSwikidesign
700264b7327Swikidesign            if ($act == 'toogle') {     // toogle visibility
701f0fda08aSwikidesign                $now = $data['comments'][$cid]['show'];
702f0fda08aSwikidesign                $data['comments'][$cid]['show'] = !$now;
703c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
704f0fda08aSwikidesign
705f0fda08aSwikidesign                $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc');
706f0fda08aSwikidesign
707264b7327Swikidesign            } elseif ($act == 'show') { // show comment
708264b7327Swikidesign                $data['comments'][$cid]['show'] = true;
709c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
710264b7327Swikidesign
711573e23a1Swikidesign                $type = 'sc'; // show comment
712264b7327Swikidesign
713264b7327Swikidesign            } elseif ($act == 'hide') { // hide comment
714264b7327Swikidesign                $data['comments'][$cid]['show'] = false;
715c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
716264b7327Swikidesign
717573e23a1Swikidesign                $type = 'hc'; // hide comment
718264b7327Swikidesign
719f0fda08aSwikidesign            } elseif (!$raw) {          // remove the comment
720c3413364SGerrit Uitslag                $data['comments'] = $this->removeComment($cid, $data['comments']);
721c3413364SGerrit Uitslag                $data['number'] = $this->countVisibleComments($data);
722f0fda08aSwikidesign
723573e23a1Swikidesign                $type = 'dc'; // delete comment
724f0fda08aSwikidesign
725f0fda08aSwikidesign            } else {                   // save changed comment
726c3413364SGerrit Uitslag                $xhtml = $this->renderComment($raw);
727f0fda08aSwikidesign
728f0fda08aSwikidesign                // now change the comment's content
7296046f25cSwikidesign                $data['comments'][$cid]['date']['modified'] = $date;
7306046f25cSwikidesign                $data['comments'][$cid]['raw'] = $raw;
731f0fda08aSwikidesign                $data['comments'][$cid]['xhtml'] = $xhtml;
732f0fda08aSwikidesign
733573e23a1Swikidesign                $type = 'ec'; // edit comment
734f0fda08aSwikidesign            }
735264b7327Swikidesign        }
736264b7327Swikidesign
737f0fda08aSwikidesign        // save the comment metadata file
738f0fda08aSwikidesign        io_saveFile($file, serialize($data));
739c3413364SGerrit Uitslag        $this->addLogEntry($date, $ID, $type, '', $cid);
740f0fda08aSwikidesign
741c3413364SGerrit Uitslag        $this->redirect($cid);
742f0fda08aSwikidesign        return true;
743f0fda08aSwikidesign    }
744f0fda08aSwikidesign
745f0fda08aSwikidesign    /**
746c3413364SGerrit Uitslag     * Recursive function to remove a comment from the data array
747c3413364SGerrit Uitslag     *
748c3413364SGerrit Uitslag     * @param string $cid comment id to be removed
749c3413364SGerrit Uitslag     * @param array $comments array with all comments
750c3413364SGerrit Uitslag     * @return array returns modified array with all remaining comments
751efbe59d0Swikidesign     */
752*283a3029SGerrit Uitslag    protected function removeComment($cid, $comments)
753*283a3029SGerrit Uitslag    {
754efbe59d0Swikidesign        if (is_array($comments[$cid]['replies'])) {
755efbe59d0Swikidesign            foreach ($comments[$cid]['replies'] as $rid) {
756c3413364SGerrit Uitslag                $comments = $this->removeComment($rid, $comments);
757efbe59d0Swikidesign            }
758efbe59d0Swikidesign        }
759efbe59d0Swikidesign        unset($comments[$cid]);
760efbe59d0Swikidesign        return $comments;
761efbe59d0Swikidesign    }
762efbe59d0Swikidesign
763efbe59d0Swikidesign    /**
764f0fda08aSwikidesign     * Prints an individual comment
765de7e6f00SGerrit Uitslag     *
766c3413364SGerrit Uitslag     * @param string $cid comment id
767c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
768c3413364SGerrit Uitslag     * @param string $parent comment id of parent
769c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
770c3413364SGerrit Uitslag     * @param bool $isVisible is marked as visible
771f0fda08aSwikidesign     */
772*283a3029SGerrit Uitslag    protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true)
773*283a3029SGerrit Uitslag    {
774c3413364SGerrit Uitslag        // comment was removed
775c3413364SGerrit Uitslag        if (!isset($data['comments'][$cid])) {
776c3413364SGerrit Uitslag            return;
777c3413364SGerrit Uitslag        }
778f0fda08aSwikidesign        $comment = $data['comments'][$cid];
779f0fda08aSwikidesign
780c3413364SGerrit Uitslag        // corrupt datatype
781c3413364SGerrit Uitslag        if (!is_array($comment)) {
782c3413364SGerrit Uitslag            return;
783c3413364SGerrit Uitslag        }
784f0fda08aSwikidesign
785c3413364SGerrit Uitslag        // handle only replies to given parent comment
786c3413364SGerrit Uitslag        if ($comment['parent'] != $parent) {
787c3413364SGerrit Uitslag            return;
788c3413364SGerrit Uitslag        }
789f0fda08aSwikidesign
790c3413364SGerrit Uitslag        // comment hidden
791c3413364SGerrit Uitslag        if (!$comment['show']) {
792c3413364SGerrit Uitslag            if ($this->helper->isDiscussionMod()) {
793c3413364SGerrit Uitslag                $hidden = ' comment_hidden';
794c3413364SGerrit Uitslag            } else {
795c3413364SGerrit Uitslag                return;
796c3413364SGerrit Uitslag            }
7974a0a1bd2Swikidesign        } else {
7984a0a1bd2Swikidesign            $hidden = '';
799f0fda08aSwikidesign        }
800f0fda08aSwikidesign
801f1c6610eSpierre.spring        // print the actual comment
802c3413364SGerrit Uitslag        $this->showComment($cid, $data, $parent, $reply, $isVisible, $hidden);
803f1c6610eSpierre.spring        // replies to this comment entry?
804c3413364SGerrit Uitslag        $this->showReplies($cid, $data, $reply, $isVisible);
805f1c6610eSpierre.spring        // reply form
806c3413364SGerrit Uitslag        $this->showReplyForm($cid, $reply);
807f1c6610eSpierre.spring    }
808f1c6610eSpierre.spring
809de7e6f00SGerrit Uitslag    /**
810c3413364SGerrit Uitslag     * Print the comment
811c3413364SGerrit Uitslag     *
812c3413364SGerrit Uitslag     * @param string $cid comment id
813c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
814c3413364SGerrit Uitslag     * @param string $parent comment id of parent
815c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
816c3413364SGerrit Uitslag     * @param bool $isVisible is marked as visible
817c3413364SGerrit Uitslag     * @param string $hidden extra class, for the admin only hidden view
818de7e6f00SGerrit Uitslag     */
819*283a3029SGerrit Uitslag    protected function showComment($cid, &$data, $parent, $reply, $isVisible, $hidden)
820*283a3029SGerrit Uitslag    {
821c3413364SGerrit Uitslag        global $conf, $lang, $HIGH, $INPUT;
822f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
823f1c6610eSpierre.spring
824f0fda08aSwikidesign        // comment head with date and user data
8254a0a1bd2Swikidesign        ptln('<div class="hentry' . $hidden . '">', 4);
8264a0a1bd2Swikidesign        ptln('<div class="comment_head">', 6);
8271810ba9cSMichael Klier        ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8);
8284a0a1bd2Swikidesign        $head = '<span class="vcard author">';
829f0fda08aSwikidesign
8306046f25cSwikidesign        // prepare variables
8316046f25cSwikidesign        if (is_array($comment['user'])) { // new format
8326046f25cSwikidesign            $user = $comment['user']['id'];
8336046f25cSwikidesign            $name = $comment['user']['name'];
8346046f25cSwikidesign            $mail = $comment['user']['mail'];
8356046f25cSwikidesign            $url = $comment['user']['url'];
8366046f25cSwikidesign            $address = $comment['user']['address'];
8376046f25cSwikidesign        } else {                         // old format
8386046f25cSwikidesign            $user = $comment['user'];
8396046f25cSwikidesign            $name = $comment['name'];
8406046f25cSwikidesign            $mail = $comment['mail'];
8416046f25cSwikidesign            $url = $comment['url'];
8426046f25cSwikidesign            $address = $comment['address'];
8436046f25cSwikidesign        }
8446046f25cSwikidesign        if (is_array($comment['date'])) { // new format
8456046f25cSwikidesign            $created = $comment['date']['created'];
846*283a3029SGerrit Uitslag            $modified = $comment['date']['modified'] ?? null;
8476046f25cSwikidesign        } else {                         // old format
8486046f25cSwikidesign            $created = $comment['date'];
8496046f25cSwikidesign            $modified = $comment['edited'];
8506046f25cSwikidesign        }
8516046f25cSwikidesign
8520ff5ab97SMichael Klier        // show username or real name?
853c3413364SGerrit Uitslag        if (!$this->getConf('userealname') && $user) {
8540ff5ab97SMichael Klier            $showname = $user;
8550ff5ab97SMichael Klier        } else {
8560ff5ab97SMichael Klier            $showname = $name;
8570ff5ab97SMichael Klier        }
8580ff5ab97SMichael Klier
859ce7b17cfSwikidesign        // show avatar image?
860c3413364SGerrit Uitslag        if ($this->useAvatar()) {
86112ca6034SMichael Klier            $user_data['name'] = $name;
86212ca6034SMichael Klier            $user_data['user'] = $user;
86312ca6034SMichael Klier            $user_data['mail'] = $mail;
86412ca6034SMichael Klier            $avatar = $this->avatar->getXHTML($user_data, $name, 'left');
865c3413364SGerrit Uitslag            if ($avatar) {
866c3413364SGerrit Uitslag                $head .= $avatar;
867c3413364SGerrit Uitslag            }
868f0fda08aSwikidesign        }
869f0fda08aSwikidesign
8706046f25cSwikidesign        if ($this->getConf('linkemail') && $mail) {
8710ff5ab97SMichael Klier            $head .= $this->email($mail, $showname, 'email fn');
8726046f25cSwikidesign        } elseif ($url) {
873c3413364SGerrit Uitslag            $head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn');
874f0fda08aSwikidesign        } else {
8750ff5ab97SMichael Klier            $head .= '<span class="fn">' . $showname . '</span>';
876f0fda08aSwikidesign        }
8776f1f13d6SMatthias Schulte
8784cded5e1SGerrit Uitslag        if ($address) {
8794cded5e1SGerrit Uitslag            $head .= ', <span class="adr">' . $address . '</span>';
8804cded5e1SGerrit Uitslag        }
8814a0a1bd2Swikidesign        $head .= '</span>, ' .
882f014bc86SMichael Klier            '<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' .
8836f1f13d6SMatthias Schulte            dformat($created, $conf['dformat']) . '</abbr>';
8849c0e06f3SMoisés Braga Ribeiro        if ($modified) {
8859c0e06f3SMoisés Braga Ribeiro            $head .= ', <abbr class="updated" title="' .
8866f1f13d6SMatthias Schulte                strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) .
8879c0e06f3SMoisés Braga Ribeiro                '</abbr>';
8884cded5e1SGerrit Uitslag        }
889f014bc86SMichael Klier        ptln($head, 8);
8904a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_head"
891f0fda08aSwikidesign
892f0fda08aSwikidesign        // main comment content
8934a0a1bd2Swikidesign        ptln('<div class="comment_body entry-content"' .
894c3413364SGerrit Uitslag            ($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6);
89515cdad37Spierre.spring        echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF;
8964a0a1bd2Swikidesign        ptln('</div>', 6); // class="comment_body"
897f0fda08aSwikidesign
898c3413364SGerrit Uitslag        if ($isVisible) {
8991184c36aSwikidesign            ptln('<div class="comment_buttons">', 6);
900f0fda08aSwikidesign
901f0fda08aSwikidesign            // show reply button?
902c3413364SGerrit Uitslag            if ($data['status'] == 1 && !$reply && $comment['show']
903c3413364SGerrit Uitslag                && ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER'))
904c3413364SGerrit Uitslag                && $this->getConf('usethreading')
9054cded5e1SGerrit Uitslag            ) {
906c3413364SGerrit Uitslag                $this->showButton($cid, $this->getLang('btn_reply'), 'reply', true);
9074cded5e1SGerrit Uitslag            }
908f0fda08aSwikidesign
9091184c36aSwikidesign            // show edit, show/hide and delete button?
910c3413364SGerrit Uitslag            if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionMod()) {
911c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_secedit'], 'edit', true);
9121184c36aSwikidesign                $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show'));
913c3413364SGerrit Uitslag                $this->showButton($cid, $label, 'toogle');
914c3413364SGerrit Uitslag                $this->showButton($cid, $lang['btn_delete'], 'delete');
915f0fda08aSwikidesign            }
9161184c36aSwikidesign            ptln('</div>', 6); // class="comment_buttons"
9171184c36aSwikidesign        }
9181184c36aSwikidesign        ptln('</div>', 4); // class="hentry"
919f0fda08aSwikidesign    }
920f0fda08aSwikidesign
921de7e6f00SGerrit Uitslag    /**
922c3413364SGerrit Uitslag     * If requested by user, show comment form to write a reply
923c3413364SGerrit Uitslag     *
924c3413364SGerrit Uitslag     * @param string $cid current comment id
925c3413364SGerrit Uitslag     * @param string $reply comment id on which the user requested a reply
926de7e6f00SGerrit Uitslag     */
927c3413364SGerrit Uitslag    protected function showReplyForm($cid, $reply)
928f1c6610eSpierre.spring    {
92931aab30eSGina Haeussge        if ($this->getConf('usethreading') && $reply == $cid) {
9304a0a1bd2Swikidesign            ptln('<div class="comment_replies">', 4);
931c3413364SGerrit Uitslag            $this->showCommentForm('', 'add', $cid);
9324a0a1bd2Swikidesign            ptln('</div>', 4); // class="comment_replies"
933f0fda08aSwikidesign        }
934f0fda08aSwikidesign    }
935f0fda08aSwikidesign
936de7e6f00SGerrit Uitslag    /**
937c3413364SGerrit Uitslag     *
938c3413364SGerrit Uitslag     *
939c3413364SGerrit Uitslag     * @param string $cid comment id
940c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
941de7e6f00SGerrit Uitslag     * @param string $reply
942c3413364SGerrit Uitslag     * @param bool $isVisible
943de7e6f00SGerrit Uitslag     */
944c3413364SGerrit Uitslag    protected function showReplies($cid, &$data, $reply, &$isVisible)
945f1c6610eSpierre.spring    {
946f1c6610eSpierre.spring        $comment = $data['comments'][$cid];
947f1c6610eSpierre.spring        if (!count($comment['replies'])) {
948f1c6610eSpierre.spring            return;
949f1c6610eSpierre.spring        }
950c3413364SGerrit Uitslag        ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4);
951c3413364SGerrit Uitslag        $isVisible = ($comment['show'] && $isVisible);
952f1c6610eSpierre.spring        foreach ($comment['replies'] as $rid) {
953c3413364SGerrit Uitslag            $this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible);
954f1c6610eSpierre.spring        }
955f1c6610eSpierre.spring        ptln('</div>', 4);
956f1c6610eSpierre.spring    }
957f1c6610eSpierre.spring
958de7e6f00SGerrit Uitslag    /**
959de7e6f00SGerrit Uitslag     * Is an avatar displayed?
960de7e6f00SGerrit Uitslag     *
961de7e6f00SGerrit Uitslag     * @return bool
962de7e6f00SGerrit Uitslag     */
963c3413364SGerrit Uitslag    protected function useAvatar()
964f1c6610eSpierre.spring    {
965c3413364SGerrit Uitslag        if (is_null($this->useAvatar)) {
966c3413364SGerrit Uitslag            $this->useAvatar = $this->getConf('useavatar')
967c3413364SGerrit Uitslag                && ($this->avatar = $this->loadHelper('avatar', false));
968f1c6610eSpierre.spring        }
969c3413364SGerrit Uitslag        return $this->useAvatar;
970f1c6610eSpierre.spring    }
971f1c6610eSpierre.spring
972de7e6f00SGerrit Uitslag    /**
973de7e6f00SGerrit Uitslag     * Calculate width of indent
974de7e6f00SGerrit Uitslag     *
975de7e6f00SGerrit Uitslag     * @return string
976de7e6f00SGerrit Uitslag     */
977*283a3029SGerrit Uitslag    protected function getWidthStyle()
978*283a3029SGerrit Uitslag    {
979f1c6610eSpierre.spring        if (is_null($this->style)) {
980c3413364SGerrit Uitslag            if ($this->useAvatar()) {
981f1c6610eSpierre.spring                $this->style = ' style="margin-left: ' . ($this->avatar->getConf('size') + 14) . 'px;"';
982f1c6610eSpierre.spring            } else {
983f1c6610eSpierre.spring                $this->style = ' style="margin-left: 20px;"';
984f1c6610eSpierre.spring            }
985f1c6610eSpierre.spring        }
986f1c6610eSpierre.spring        return $this->style;
987f1c6610eSpierre.spring    }
988f1c6610eSpierre.spring
989f0fda08aSwikidesign    /**
990c3413364SGerrit Uitslag     * Show the button which toggles between show/hide of the entire discussion section
99146178401Slupo49     */
992*283a3029SGerrit Uitslag    protected function showDiscussionToggleButton()
993*283a3029SGerrit Uitslag    {
99446178401Slupo49        ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">');
995*283a3029SGerrit Uitslag        ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button"'
996*283a3029SGerrit Uitslag            . 'value="' . $this->getLang('toggle_display') . '">');
99746178401Slupo49        ptln('</div>');
99846178401Slupo49    }
99946178401Slupo49
100046178401Slupo49    /**
1001f0fda08aSwikidesign     * Outputs the comment form
1002f0fda08aSwikidesign     */
1003*283a3029SGerrit Uitslag    protected function showCommentForm($raw = '', $act = 'add', $cid = null)
1004*283a3029SGerrit Uitslag    {
1005c3413364SGerrit Uitslag        global $lang, $conf, $ID, $INPUT;
1006f0fda08aSwikidesign
1007f0fda08aSwikidesign        // not for unregistered users when guest comments aren't allowed
1008c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('allowguests')) {
10097c8e18ffSGina Haeussge            ?>
10107c8e18ffSGina Haeussge            <div class="comment_form">
10117c8e18ffSGina Haeussge                <?php echo $this->getLang('noguests'); ?>
10127c8e18ffSGina Haeussge            </div>
10137c8e18ffSGina Haeussge            <?php
1014de7e6f00SGerrit Uitslag            return;
10157c8e18ffSGina Haeussge        }
1016f0fda08aSwikidesign
1017c3413364SGerrit Uitslag        // fill $raw with $INPUT->str('text') if it's empty (for failed CAPTCHA check)
1018c3413364SGerrit Uitslag        if (!$raw && $INPUT->str('comment') == 'show') {
1019c3413364SGerrit Uitslag            $raw = $INPUT->str('text');
10204cded5e1SGerrit Uitslag        }
1021f0fda08aSwikidesign        ?>
10225ef1705fSiLoveiDo
1023f0fda08aSwikidesign        <div class="comment_form">
1024*283a3029SGerrit Uitslag            <form id="discussion__comment_form" method="post" action="<?php echo script() ?>"
1025*283a3029SGerrit Uitslag                  accept-charset="<?php echo $lang['encoding'] ?>">
1026f0fda08aSwikidesign                <div class="no">
1027f0fda08aSwikidesign                    <input type="hidden" name="id" value="<?php echo $ID ?>"/>
102861437513Swikidesign                    <input type="hidden" name="do" value="show"/>
1029f0fda08aSwikidesign                    <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1030530693fbSMichael Klier                    <?php
1031f0fda08aSwikidesign                    // for adding a comment
1032f0fda08aSwikidesign                    if ($act == 'add') {
1033f0fda08aSwikidesign                        ?>
1034f0fda08aSwikidesign                        <input type="hidden" name="reply" value="<?php echo $cid ?>"/>
1035f0fda08aSwikidesign                        <?php
103620c152acSMichael Klier                        // for guest/adminimport: show name, e-mail and subscribe to comments fields
1037c3413364SGerrit Uitslag                        if (!$INPUT->server->has('REMOTE_USER') or ($this->getConf('adminimport') && $this->helper->isDiscussionMod())) {
1038f0fda08aSwikidesign                            ?>
1039f0fda08aSwikidesign                            <input type="hidden" name="user" value="<?php echo clientIP() ?>"/>
1040f0fda08aSwikidesign                            <div class="comment_name">
1041f0fda08aSwikidesign                                <label class="block" for="discussion__comment_name">
1042f0fda08aSwikidesign                                    <span><?php echo $lang['fullname'] ?>:</span>
1043*283a3029SGerrit Uitslag                                    <input type="text"
1044*283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('name'))) echo ' error' ?>"
1045*283a3029SGerrit Uitslag                                           name="name" id="discussion__comment_name" size="50" tabindex="1"
1046*283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('name')) ?>"/>
1047f0fda08aSwikidesign                                </label>
1048f0fda08aSwikidesign                            </div>
1049f0fda08aSwikidesign                            <div class="comment_mail">
1050f0fda08aSwikidesign                                <label class="block" for="discussion__comment_mail">
1051f0fda08aSwikidesign                                    <span><?php echo $lang['email'] ?>:</span>
1052*283a3029SGerrit Uitslag                                    <input type="text"
1053*283a3029SGerrit Uitslag                                           class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('mail'))) echo ' error' ?>"
1054*283a3029SGerrit Uitslag                                           name="mail" id="discussion__comment_mail" size="50" tabindex="2"
1055*283a3029SGerrit Uitslag                                           value="<?php echo hsc($INPUT->str('mail')) ?>"/>
1056f0fda08aSwikidesign                                </label>
1057f0fda08aSwikidesign                            </div>
1058f0fda08aSwikidesign                            <?php
1059f0fda08aSwikidesign                        }
1060f0fda08aSwikidesign
1061f0fda08aSwikidesign                        // allow entering an URL
1062f0fda08aSwikidesign                        if ($this->getConf('urlfield')) {
1063f0fda08aSwikidesign                            ?>
1064f0fda08aSwikidesign                            <div class="comment_url">
1065f0fda08aSwikidesign                                <label class="block" for="discussion__comment_url">
1066f0fda08aSwikidesign                                    <span><?php echo $this->getLang('url') ?>:</span>
1067*283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="url" id="discussion__comment_url" size="50"
1068*283a3029SGerrit Uitslag                                           tabindex="3" value="<?php echo hsc($INPUT->str('url')) ?>"/>
1069f0fda08aSwikidesign                                </label>
1070f0fda08aSwikidesign                            </div>
1071f0fda08aSwikidesign                            <?php
1072f0fda08aSwikidesign                        }
1073f0fda08aSwikidesign
1074f0fda08aSwikidesign                        // allow entering an address
1075f0fda08aSwikidesign                        if ($this->getConf('addressfield')) {
1076f0fda08aSwikidesign                            ?>
1077f0fda08aSwikidesign                            <div class="comment_address">
1078f0fda08aSwikidesign                                <label class="block" for="discussion__comment_address">
1079f0fda08aSwikidesign                                    <span><?php echo $this->getLang('address') ?>:</span>
1080*283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="address" id="discussion__comment_address"
1081*283a3029SGerrit Uitslag                                           size="50" tabindex="4" value="<?php echo hsc($INPUT->str('address')) ?>"/>
1082f0fda08aSwikidesign                                </label>
1083f0fda08aSwikidesign                            </div>
1084f0fda08aSwikidesign                            <?php
1085f0fda08aSwikidesign                        }
1086f0fda08aSwikidesign
1087f0fda08aSwikidesign                        // allow setting the comment date
1088e6b2f142Slupo49                        if ($this->getConf('adminimport') && ($this->helper->isDiscussionMod())) {
1089f0fda08aSwikidesign                            ?>
1090f0fda08aSwikidesign                            <div class="comment_date">
1091f0fda08aSwikidesign                                <label class="block" for="discussion__comment_date">
1092f0fda08aSwikidesign                                    <span><?php echo $this->getLang('date') ?>:</span>
1093*283a3029SGerrit Uitslag                                    <input type="text" class="edit" name="date" id="discussion__comment_date"
1094*283a3029SGerrit Uitslag                                           size="50"/>
1095f0fda08aSwikidesign                                </label>
1096f0fda08aSwikidesign                            </div>
1097f0fda08aSwikidesign                            <?php
1098f0fda08aSwikidesign                        }
1099f0fda08aSwikidesign
1100f0fda08aSwikidesign                        // for saving a comment
1101f0fda08aSwikidesign                    } else {
1102f0fda08aSwikidesign                        ?>
1103f0fda08aSwikidesign                        <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1104f0fda08aSwikidesign                        <?php
1105f0fda08aSwikidesign                    }
1106f0fda08aSwikidesign                    ?>
1107f0fda08aSwikidesign                    <div class="comment_text">
1108*283a3029SGerrit Uitslag                        <?php echo $this->getLang('entercomment');
1109*283a3029SGerrit Uitslag                        echo($this->getConf('wikisyntaxok') ? "" : ":");
111043ade360Slupo49                        if ($this->getConf('wikisyntaxok')) echo '. ' . $this->getLang('wikisyntax') . ':'; ?>
111143ade360Slupo49
111243ade360Slupo49                        <!-- Fix for disable the toolbar when wikisyntaxok is set to false. See discussion's script.jss -->
111343ade360Slupo49                        <?php if ($this->getConf('wikisyntaxok')) { ?>
111464901b8eSGerrit Uitslag                        <div id="discussion__comment_toolbar" class="toolbar group">
111543ade360Slupo49                            <?php } else { ?>
111643ade360Slupo49                            <div id="discussion__comment_toolbar_disabled">
111743ade360Slupo49                                <?php } ?>
11181de52da1SMichael Klier                            </div>
1119*283a3029SGerrit Uitslag                            <textarea
1120*283a3029SGerrit Uitslag                                class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('text'))) echo ' error' ?>"
1121*283a3029SGerrit Uitslag                                name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php
112237e3c825SMichael Klier                                if ($raw) {
112337e3c825SMichael Klier                                    echo formText($raw);
112437e3c825SMichael Klier                                } else {
1125c3413364SGerrit Uitslag                                    echo hsc($INPUT->str('text'));
112637e3c825SMichael Klier                                }
112737e3c825SMichael Klier                                ?></textarea>
1128f0fda08aSwikidesign                        </div>
11290c613822SMichael Hamann
11300c613822SMichael Hamann                        <?php
11310c613822SMichael Hamann                        /** @var helper_plugin_captcha $captcha */
11320c613822SMichael Hamann                        $captcha = $this->loadHelper('captcha', false);
11330c613822SMichael Hamann                        if ($captcha && $captcha->isEnabled()) {
11340c613822SMichael Hamann                            echo $captcha->getHTML();
11350c613822SMichael Hamann                        }
11360c613822SMichael Hamann
11370c613822SMichael Hamann                        /** @var helper_plugin_recaptcha $recaptcha */
11380c613822SMichael Hamann                        $recaptcha = $this->loadHelper('recaptcha', false);
11390c613822SMichael Hamann                        if ($recaptcha && $recaptcha->isEnabled()) {
11400c613822SMichael Hamann                            echo $recaptcha->getHTML();
11410c613822SMichael Hamann                        }
1142e7c760b3Swikidesign                        ?>
11430c613822SMichael Hamann
1144*283a3029SGerrit Uitslag                        <input class="button comment_submit" id="discussion__btn_submit" type="submit" name="submit"
1145*283a3029SGerrit Uitslag                               accesskey="s" value="<?php echo $lang['btn_save'] ?>"
1146*283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_save'] ?> [S]" tabindex="7"/>
1147*283a3029SGerrit Uitslag                        <input class="button comment_preview_button" id="discussion__btn_preview" type="button"
1148*283a3029SGerrit Uitslag                               name="preview" accesskey="p" value="<?php echo $lang['btn_preview'] ?>"
1149*283a3029SGerrit Uitslag                               title="<?php echo $lang['btn_preview'] ?> [P]"/>
11503011fb8bSMichael Klier
1151*283a3029SGerrit Uitslag                        <?php if ((!$INPUT->server->has('REMOTE_USER')
1152*283a3029SGerrit Uitslag                                || $INPUT->server->has('REMOTE_USER') && !$conf['subscribers'])
1153*283a3029SGerrit Uitslag                                && $this->getConf('subscribe')) { ?>
11543011fb8bSMichael Klier                            <div class="comment_subscribe">
1155*283a3029SGerrit Uitslag                                <input type="checkbox" id="discussion__comment_subscribe" name="subscribe"
1156*283a3029SGerrit Uitslag                                       tabindex="6"/>
11573011fb8bSMichael Klier                                <label class="block" for="discussion__comment_subscribe">
11583011fb8bSMichael Klier                                    <span><?php echo $this->getLang('subscribe') ?></span>
11593011fb8bSMichael Klier                                </label>
11603011fb8bSMichael Klier                            </div>
11613011fb8bSMichael Klier                        <?php } ?>
11623011fb8bSMichael Klier
11633011fb8bSMichael Klier                        <div class="clearer"></div>
1164ed4846efSMichael Klier                        <div id="discussion__comment_preview">&nbsp;</div>
1165f0fda08aSwikidesign                    </div>
1166f0fda08aSwikidesign            </form>
1167f0fda08aSwikidesign        </div>
1168f0fda08aSwikidesign        <?php
1169f0fda08aSwikidesign    }
1170f0fda08aSwikidesign
1171f0fda08aSwikidesign    /**
1172c3413364SGerrit Uitslag     * Action button below a comment
1173de7e6f00SGerrit Uitslag     *
1174c3413364SGerrit Uitslag     * @param string $cid comment id
1175c3413364SGerrit Uitslag     * @param string $label translated label
1176c3413364SGerrit Uitslag     * @param string $act action
1177c3413364SGerrit Uitslag     * @param bool $jump whether to scroll to the commentform
1178f0fda08aSwikidesign     */
1179*283a3029SGerrit Uitslag    protected function showButton($cid, $label, $act, $jump = false)
1180*283a3029SGerrit Uitslag    {
1181f0fda08aSwikidesign        global $ID;
11825ef1705fSiLoveiDo
11831e46d176Swikidesign        $anchor = ($jump ? '#discussion__comment_form' : '');
1184f0fda08aSwikidesign
1185f0fda08aSwikidesign        ?>
11866d1f4f20SGina Haeussge        <form class="button discussion__<?php echo $act ?>" method="get" action="<?php echo script() . $anchor ?>">
1187f0fda08aSwikidesign            <div class="no">
1188f0fda08aSwikidesign                <input type="hidden" name="id" value="<?php echo $ID ?>"/>
118961437513Swikidesign                <input type="hidden" name="do" value="show"/>
1190f0fda08aSwikidesign                <input type="hidden" name="comment" value="<?php echo $act ?>"/>
1191f0fda08aSwikidesign                <input type="hidden" name="cid" value="<?php echo $cid ?>"/>
1192f0fda08aSwikidesign                <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>"/>
1193f0fda08aSwikidesign            </div>
1194f0fda08aSwikidesign        </form>
1195f0fda08aSwikidesign        <?php
1196f0fda08aSwikidesign    }
1197f0fda08aSwikidesign
1198f0fda08aSwikidesign    /**
1199f0fda08aSwikidesign     * Adds an entry to the comments changelog
1200f0fda08aSwikidesign     *
1201de7e6f00SGerrit Uitslag     * @param int $date
1202de7e6f00SGerrit Uitslag     * @param string $id page id
1203c3413364SGerrit Uitslag     * @param string $type create/edit/delete/show/hide comment 'cc', 'ec', 'dc', 'sc', 'hc'
1204de7e6f00SGerrit Uitslag     * @param string $summary
1205de7e6f00SGerrit Uitslag     * @param string $extra
1206*283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1207*283a3029SGerrit Uitslag     *
1208*283a3029SGerrit Uitslag     * @author Esther Brunner <wikidesign@gmail.com>
1209f0fda08aSwikidesign     */
1210*283a3029SGerrit Uitslag    protected function addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '')
1211*283a3029SGerrit Uitslag    {
1212c3413364SGerrit Uitslag        global $conf, $INPUT;
1213f0fda08aSwikidesign
1214f0fda08aSwikidesign        $changelog = $conf['metadir'] . '/_comments.changes';
1215f0fda08aSwikidesign
12164cded5e1SGerrit Uitslag        //use current time if none supplied
12174cded5e1SGerrit Uitslag        if (!$date) {
12184cded5e1SGerrit Uitslag            $date = time();
12194cded5e1SGerrit Uitslag        }
1220c3413364SGerrit Uitslag        $remote = $INPUT->server->str('REMOTE_ADDR');
1221c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
1222f0fda08aSwikidesign
1223c3413364SGerrit Uitslag        $strip = ["\t", "\n"];
1224c3413364SGerrit Uitslag        $logline = [
1225f0fda08aSwikidesign            'date' => $date,
1226f0fda08aSwikidesign            'ip' => $remote,
1227f0fda08aSwikidesign            'type' => str_replace($strip, '', $type),
1228f0fda08aSwikidesign            'id' => $id,
1229f0fda08aSwikidesign            'user' => $user,
1230f0fda08aSwikidesign            'sum' => str_replace($strip, '', $summary),
1231f0fda08aSwikidesign            'extra' => str_replace($strip, '', $extra)
1232c3413364SGerrit Uitslag        ];
1233f0fda08aSwikidesign
1234f0fda08aSwikidesign        // add changelog line
1235f0fda08aSwikidesign        $logline = implode("\t", $logline) . "\n";
1236f0fda08aSwikidesign        io_saveFile($changelog, $logline, true); //global changelog cache
1237c3413364SGerrit Uitslag        $this->trimRecentCommentsLog($changelog);
123877a22ba2Swikidesign
123977a22ba2Swikidesign        // tell the indexer to re-index the page
124077a22ba2Swikidesign        @unlink(metaFN($id, '.indexed'));
1241f0fda08aSwikidesign    }
1242f0fda08aSwikidesign
1243f0fda08aSwikidesign    /**
1244f0fda08aSwikidesign     * Trims the recent comments cache to the last $conf['changes_days'] recent
1245f0fda08aSwikidesign     * changes or $conf['recent'] items, which ever is larger.
1246f0fda08aSwikidesign     * The trimming is only done once a day.
1247f0fda08aSwikidesign     *
1248de7e6f00SGerrit Uitslag     * @param string $changelog file path
1249de7e6f00SGerrit Uitslag     * @return bool
1250*283a3029SGerrit Uitslag     * @author Ben Coburn <btcoburn@silicodon.net>
1251*283a3029SGerrit Uitslag     *
1252f0fda08aSwikidesign     */
1253*283a3029SGerrit Uitslag    protected function trimRecentCommentsLog($changelog)
1254*283a3029SGerrit Uitslag    {
1255f0fda08aSwikidesign        global $conf;
1256f0fda08aSwikidesign
1257*283a3029SGerrit Uitslag        if (@file_exists($changelog)
1258*283a3029SGerrit Uitslag            && (filectime($changelog) + 86400) < time()
1259*283a3029SGerrit Uitslag            && !@file_exists($changelog . '_tmp')
12604cded5e1SGerrit Uitslag        ) {
1261f0fda08aSwikidesign
1262f0fda08aSwikidesign            io_lock($changelog);
1263f0fda08aSwikidesign            $lines = file($changelog);
1264f0fda08aSwikidesign            if (count($lines) < $conf['recent']) {
1265f0fda08aSwikidesign                // nothing to trim
1266f0fda08aSwikidesign                io_unlock($changelog);
1267f0fda08aSwikidesign                return true;
1268f0fda08aSwikidesign            }
1269f0fda08aSwikidesign
1270*283a3029SGerrit Uitslag            // presave tmp as 2nd lock
1271*283a3029SGerrit Uitslag            io_saveFile($changelog . '_tmp', '');
1272f0fda08aSwikidesign            $trim_time = time() - $conf['recent_days'] * 86400;
1273c3413364SGerrit Uitslag            $out_lines = [];
1274f0fda08aSwikidesign
1275e49085a2SMichael Klier            $num = count($lines);
1276e49085a2SMichael Klier            for ($i = 0; $i < $num; $i++) {
1277f0fda08aSwikidesign                $log = parseChangelogLine($lines[$i]);
1278f0fda08aSwikidesign                if ($log === false) continue;                      // discard junk
1279f0fda08aSwikidesign                if ($log['date'] < $trim_time) {
1280f0fda08aSwikidesign                    $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions)
1281f0fda08aSwikidesign                } else {
1282f0fda08aSwikidesign                    $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines
1283f0fda08aSwikidesign                }
1284f0fda08aSwikidesign            }
1285f0fda08aSwikidesign
1286f0fda08aSwikidesign            // sort the final result, it shouldn't be necessary,
1287f0fda08aSwikidesign            // however the extra robustness in making the changelog cache self-correcting is worth it
1288f0fda08aSwikidesign            ksort($out_lines);
1289f0fda08aSwikidesign            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
1290f0fda08aSwikidesign            if ($extra > 0) {
1291f0fda08aSwikidesign                ksort($old_lines);
1292f0fda08aSwikidesign                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
1293f0fda08aSwikidesign            }
1294f0fda08aSwikidesign
1295f0fda08aSwikidesign            // save trimmed changelog
1296f0fda08aSwikidesign            io_saveFile($changelog . '_tmp', implode('', $out_lines));
1297f0fda08aSwikidesign            @unlink($changelog);
1298f0fda08aSwikidesign            if (!rename($changelog . '_tmp', $changelog)) {
1299f0fda08aSwikidesign                // rename failed so try another way...
1300f0fda08aSwikidesign                io_unlock($changelog);
1301f0fda08aSwikidesign                io_saveFile($changelog, implode('', $out_lines));
1302f0fda08aSwikidesign                @unlink($changelog . '_tmp');
1303f0fda08aSwikidesign            } else {
1304f0fda08aSwikidesign                io_unlock($changelog);
1305f0fda08aSwikidesign            }
1306f0fda08aSwikidesign            return true;
1307f0fda08aSwikidesign        }
1308de7e6f00SGerrit Uitslag        return true;
1309f0fda08aSwikidesign    }
1310f0fda08aSwikidesign
1311f0fda08aSwikidesign    /**
1312f0fda08aSwikidesign     * Sends a notify mail on new comment
1313f0fda08aSwikidesign     *
1314f0fda08aSwikidesign     * @param array $comment data array of the new comment
1315c3413364SGerrit Uitslag     * @param array $subscribers data of the subscribers by reference
1316f0fda08aSwikidesign     *
1317f0fda08aSwikidesign     * @author Andreas Gohr <andi@splitbrain.org>
1318f0fda08aSwikidesign     * @author Esther Brunner <wikidesign@gmail.com>
1319f0fda08aSwikidesign     */
1320*283a3029SGerrit Uitslag    protected function notify($comment, &$subscribers)
1321*283a3029SGerrit Uitslag    {
1322c3413364SGerrit Uitslag        global $conf, $ID, $INPUT, $auth;
1323f0fda08aSwikidesign
13249881d835SMichael Klier        $notify_text = io_readfile($this->localfn('subscribermail'));
13259881d835SMichael Klier        $confirm_text = io_readfile($this->localfn('confirmsubscribe'));
13269881d835SMichael Klier        $subject_notify = '[' . $conf['title'] . '] ' . $this->getLang('mail_newcomment');
13279881d835SMichael Klier        $subject_subscribe = '[' . $conf['title'] . '] ' . $this->getLang('subscribe');
1328f0fda08aSwikidesign
1329451c1100SMichael Hamann        $mailer = new Mailer();
1330c3413364SGerrit Uitslag        if (!$INPUT->server->has('REMOTE_USER')) {
1331451c1100SMichael Hamann            $mailer->from($conf['mailfromnobody']);
1332f4a5ed1cSMatthias Schulte        }
1333f4a5ed1cSMatthias Schulte
1334c3413364SGerrit Uitslag        $replace = [
13353c4953e9SMichael Hamann            'PAGE' => $ID,
13363c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13373c4953e9SMichael Hamann            'DATE' => dformat($comment['date']['created'], $conf['dformat']),
13383c4953e9SMichael Hamann            'NAME' => $comment['user']['name'],
13393c4953e9SMichael Hamann            'TEXT' => $comment['raw'],
13403c4953e9SMichael Hamann            'COMMENTURL' => wl($ID, '', true) . '#comment_' . $comment['cid'],
134106644a74SMichael Hamann            'UNSUBSCRIBE' => wl($ID, 'do=subscribe', true, '&'),
13423c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1343c3413364SGerrit Uitslag        ];
1344451c1100SMichael Hamann
1345c3413364SGerrit Uitslag        $confirm_replace = [
13463c4953e9SMichael Hamann            'PAGE' => $ID,
13473c4953e9SMichael Hamann            'TITLE' => $conf['title'],
13483c4953e9SMichael Hamann            'DOKUWIKIURL' => DOKU_URL
1349c3413364SGerrit Uitslag        ];
1350451c1100SMichael Hamann
1351451c1100SMichael Hamann
1352451c1100SMichael Hamann        $mailer->subject($subject_notify);
1353451c1100SMichael Hamann        $mailer->setBody($notify_text, $replace);
1354451c1100SMichael Hamann
1355f4a5ed1cSMatthias Schulte        // send mail to notify address
1356f4a5ed1cSMatthias Schulte        if ($conf['notify']) {
1357451c1100SMichael Hamann            $mailer->bcc($conf['notify']);
1358451c1100SMichael Hamann            $mailer->send();
1359f4a5ed1cSMatthias Schulte        }
1360f4a5ed1cSMatthias Schulte
1361ca785d71SMichael Hamann        // send email to moderators
1362ca785d71SMichael Hamann        if ($this->getConf('moderatorsnotify')) {
1363c3413364SGerrit Uitslag            $moderatorgrpsString = trim($this->getConf('moderatorgroups'));
1364c3413364SGerrit Uitslag            if (!empty($moderatorgrpsString)) {
1365ca785d71SMichael Hamann                // create a clean mods list
1366c3413364SGerrit Uitslag                $moderatorgroups = explode(',', $moderatorgrpsString);
1367c3413364SGerrit Uitslag                $moderatorgroups = array_map('trim', $moderatorgroups);
1368c3413364SGerrit Uitslag                $moderatorgroups = array_unique($moderatorgroups);
1369c3413364SGerrit Uitslag                $moderatorgroups = array_filter($moderatorgroups);
1370ca785d71SMichael Hamann                // search for moderators users
1371c3413364SGerrit Uitslag                foreach ($moderatorgroups as $moderatorgroup) {
1372c3413364SGerrit Uitslag                    if (!$auth->isCaseSensitive()) {
1373c3413364SGerrit Uitslag                        $moderatorgroup = PhpString::strtolower($moderatorgroup);
1374c3413364SGerrit Uitslag                    }
1375ca785d71SMichael Hamann                    // create a clean mailing list
1376c3413364SGerrit Uitslag                    $bccs = [];
1377c3413364SGerrit Uitslag                    if ($moderatorgroup[0] == '@') {
1378c3413364SGerrit Uitslag                        foreach ($auth->retrieveUsers(0, 0, ['grps' => $auth->cleanGroup(substr($moderatorgroup, 1))]) as $user) {
1379ca785d71SMichael Hamann                            if (!empty($user['mail'])) {
1380c3413364SGerrit Uitslag                                $bccs[] = $user['mail'];
1381ca785d71SMichael Hamann                            }
1382ca785d71SMichael Hamann                        }
1383ca785d71SMichael Hamann                    } else {
1384c3413364SGerrit Uitslag                        //it is an user
1385c3413364SGerrit Uitslag                        $userdata = $auth->getUserData($auth->cleanUser($moderatorgroup));
1386ca785d71SMichael Hamann                        if (!empty($userdata['mail'])) {
1387c3413364SGerrit Uitslag                            $bccs[] = $userdata['mail'];
1388ca785d71SMichael Hamann                        }
1389ca785d71SMichael Hamann                    }
1390c3413364SGerrit Uitslag                    $bccs = array_unique($bccs);
1391ca785d71SMichael Hamann                    // notify the users
1392c3413364SGerrit Uitslag                    $mailer->bcc(implode(',', $bccs));
1393ca785d71SMichael Hamann                    $mailer->send();
1394ca785d71SMichael Hamann                }
1395ca785d71SMichael Hamann            }
1396ca785d71SMichael Hamann        }
1397ca785d71SMichael Hamann
1398f4a5ed1cSMatthias Schulte        // notify page subscribers
1399451c1100SMichael Hamann        if (actionOK('subscribe')) {
1400c3413364SGerrit Uitslag            $data = ['id' => $ID, 'addresslist' => '', 'self' => false];
1401c3413364SGerrit Uitslag            //FIXME default callback, needed to mentioned it again?
1402c3413364SGerrit Uitslag            Event::createAndTrigger(
1403451c1100SMichael Hamann                'COMMON_NOTIFY_ADDRESSLIST', $data,
1404c3413364SGerrit Uitslag                [new SubscriberManager(), 'notifyAddresses']
1405451c1100SMichael Hamann            );
1406c3413364SGerrit Uitslag
1407451c1100SMichael Hamann            $to = $data['addresslist'];
1408451c1100SMichael Hamann            if (!empty($to)) {
1409451c1100SMichael Hamann                $mailer->bcc($to);
1410451c1100SMichael Hamann                $mailer->send();
1411451c1100SMichael Hamann            }
14123011fb8bSMichael Klier        }
1413f0fda08aSwikidesign
14143011fb8bSMichael Klier        // notify comment subscribers
14153011fb8bSMichael Klier        if (!empty($subscribers)) {
14163011fb8bSMichael Klier
14179881d835SMichael Klier            foreach ($subscribers as $mail => $data) {
1418451c1100SMichael Hamann                $mailer->bcc($mail);
14199881d835SMichael Klier                if ($data['active']) {
14203c4953e9SMichael Hamann                    $replace['UNSUBSCRIBE'] = wl($ID, 'do=discussion_unsubscribe&hash=' . $data['hash'], true, '&');
14213011fb8bSMichael Klier
1422451c1100SMichael Hamann                    $mailer->subject($subject_notify);
1423451c1100SMichael Hamann                    $mailer->setBody($notify_text, $replace);
1424451c1100SMichael Hamann                    $mailer->send();
1425c3413364SGerrit Uitslag                } elseif (!$data['confirmsent']) {
14263c4953e9SMichael Hamann                    $confirm_replace['SUBSCRIBE'] = wl($ID, 'do=discussion_confirmsubscribe&hash=' . $data['hash'], true, '&');
14279881d835SMichael Klier
1428451c1100SMichael Hamann                    $mailer->subject($subject_subscribe);
1429451c1100SMichael Hamann                    $mailer->setBody($confirm_text, $confirm_replace);
1430451c1100SMichael Hamann                    $mailer->send();
14319881d835SMichael Klier                    $subscribers[$mail]['confirmsent'] = true;
14329881d835SMichael Klier                }
14333011fb8bSMichael Klier            }
14343011fb8bSMichael Klier        }
1435f0fda08aSwikidesign    }
1436f0fda08aSwikidesign
1437f0fda08aSwikidesign    /**
1438f0fda08aSwikidesign     * Counts the number of visible comments
1439de7e6f00SGerrit Uitslag     *
1440c3413364SGerrit Uitslag     * @param array $data array with all comments
1441de7e6f00SGerrit Uitslag     * @return int
1442f0fda08aSwikidesign     */
1443*283a3029SGerrit Uitslag    protected function countVisibleComments($data)
1444*283a3029SGerrit Uitslag    {
1445f0fda08aSwikidesign        $number = 0;
1446de7e6f00SGerrit Uitslag        foreach ($data['comments'] as $comment) {
1447f0fda08aSwikidesign            if ($comment['parent']) continue;
1448f0fda08aSwikidesign            if (!$comment['show']) continue;
1449c3413364SGerrit Uitslag
1450f0fda08aSwikidesign            $number++;
1451f0fda08aSwikidesign            $rids = $comment['replies'];
14524cded5e1SGerrit Uitslag            if (count($rids)) {
1453c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14544cded5e1SGerrit Uitslag            }
1455f0fda08aSwikidesign        }
1456f0fda08aSwikidesign        return $number;
1457f0fda08aSwikidesign    }
1458f0fda08aSwikidesign
1459de7e6f00SGerrit Uitslag    /**
1460c3413364SGerrit Uitslag     * Count visible replies on the comments
1461c3413364SGerrit Uitslag     *
1462de7e6f00SGerrit Uitslag     * @param array $data
1463de7e6f00SGerrit Uitslag     * @param array $rids
1464c3413364SGerrit Uitslag     * @return int counted replies
1465de7e6f00SGerrit Uitslag     */
1466*283a3029SGerrit Uitslag    protected function countVisibleReplies(&$data, $rids)
1467*283a3029SGerrit Uitslag    {
1468f0fda08aSwikidesign        $number = 0;
1469f0fda08aSwikidesign        foreach ($rids as $rid) {
14702ee3dca3Swikidesign            if (!isset($data['comments'][$rid])) continue; // reply was removed
1471f0fda08aSwikidesign            if (!$data['comments'][$rid]['show']) continue;
1472c3413364SGerrit Uitslag
1473f0fda08aSwikidesign            $number++;
1474f0fda08aSwikidesign            $rids = $data['comments'][$rid]['replies'];
14754cded5e1SGerrit Uitslag            if (count($rids)) {
1476c3413364SGerrit Uitslag                $number = $number + $this->countVisibleReplies($data, $rids);
14774cded5e1SGerrit Uitslag            }
1478f0fda08aSwikidesign        }
1479f0fda08aSwikidesign        return $number;
1480f0fda08aSwikidesign    }
1481f0fda08aSwikidesign
1482f0fda08aSwikidesign    /**
1483c3413364SGerrit Uitslag     * Renders the raw comment (wiki)text to html
1484de7e6f00SGerrit Uitslag     *
1485c3413364SGerrit Uitslag     * @param string $raw comment text
1486de7e6f00SGerrit Uitslag     * @return null|string
1487f0fda08aSwikidesign     */
1488*283a3029SGerrit Uitslag    protected function renderComment($raw)
1489*283a3029SGerrit Uitslag    {
1490f0fda08aSwikidesign        if ($this->getConf('wikisyntaxok')) {
1491efccf6b0SJeffrey Bergamini            // Note the warning for render_text:
1492efccf6b0SJeffrey Bergamini            //   "very ineffecient for small pieces of data - try not to use"
1493efccf6b0SJeffrey Bergamini            // in dokuwiki/inc/plugin.php
1494efccf6b0SJeffrey Bergamini            $xhtml = $this->render_text($raw);
1495f0fda08aSwikidesign        } else { // wiki syntax not allowed -> just encode special chars
149687bb4e97SMichael Klier            $xhtml = hsc(trim($raw));
149787bb4e97SMichael Klier            $xhtml = str_replace("\n", '<br />', $xhtml);
1498f0fda08aSwikidesign        }
1499f0fda08aSwikidesign        return $xhtml;
1500f0fda08aSwikidesign    }
1501f0fda08aSwikidesign
1502f0fda08aSwikidesign    /**
1503479dd10fSwikidesign     * Finds out whether there is a discussion section for the current page
1504de7e6f00SGerrit Uitslag     *
1505de7e6f00SGerrit Uitslag     * @param string $title
1506de7e6f00SGerrit Uitslag     * @return bool
1507479dd10fSwikidesign     */
1508*283a3029SGerrit Uitslag    protected function hasDiscussion(&$title)
1509*283a3029SGerrit Uitslag    {
1510b2ac3b3bSwikidesign        global $ID;
15114a0a1bd2Swikidesign
1512c3413364SGerrit Uitslag        $file = metaFN($ID, '.comments');
1513479dd10fSwikidesign
1514c3413364SGerrit Uitslag        if (!@file_exists($file)) {
1515f0bcde18SGerrit Uitslag            if ($this->isDiscussionEnabled()) {
15162b18adb9SMichael Klier                return true;
15172b18adb9SMichael Klier            } else {
15182b18adb9SMichael Klier                return false;
15192b18adb9SMichael Klier            }
1520479dd10fSwikidesign        }
1521479dd10fSwikidesign
1522c3413364SGerrit Uitslag        $comments = unserialize(io_readFile($file, false));
1523479dd10fSwikidesign
15244cded5e1SGerrit Uitslag        if ($comments['title']) {
15254cded5e1SGerrit Uitslag            $title = hsc($comments['title']);
15264cded5e1SGerrit Uitslag        }
1527479dd10fSwikidesign        $num = $comments['number'];
1528c3413364SGerrit Uitslag        if (!$comments['status'] || ($comments['status'] == 2 && $num == 0)) {
1529c3413364SGerrit Uitslag            //disabled, or closed and no comments
1530c3413364SGerrit Uitslag            return false;
1531c3413364SGerrit Uitslag        } else {
1532c3413364SGerrit Uitslag            return true;
1533c3413364SGerrit Uitslag        }
1534479dd10fSwikidesign    }
1535479dd10fSwikidesign
1536479dd10fSwikidesign    /**
1537e7c760b3Swikidesign     * Creates a new thread page
1538de7e6f00SGerrit Uitslag     *
1539de7e6f00SGerrit Uitslag     * @return string
1540e7c760b3Swikidesign     */
1541*283a3029SGerrit Uitslag    protected function newThread()
1542*283a3029SGerrit Uitslag    {
1543c3413364SGerrit Uitslag        global $ID, $INFO, $INPUT;
1544f0fda08aSwikidesign
1545c3413364SGerrit Uitslag        $ns = cleanID($INPUT->str('ns'));
1546c3413364SGerrit Uitslag        $title = str_replace(':', '', $INPUT->str('title'));
15472e80cd5fSwikidesign        $back = $ID;
15482e80cd5fSwikidesign        $ID = ($ns ? $ns . ':' : '') . cleanID($title);
15492e80cd5fSwikidesign        $INFO = pageinfo();
1550f0fda08aSwikidesign
1551f0fda08aSwikidesign        // check if we are allowed to create this file
15522e80cd5fSwikidesign        if ($INFO['perm'] >= AUTH_CREATE) {
1553f0fda08aSwikidesign
1554f0fda08aSwikidesign            //check if locked by anyone - if not lock for my self
15554cded5e1SGerrit Uitslag            if ($INFO['locked']) {
15564cded5e1SGerrit Uitslag                return 'locked';
15574cded5e1SGerrit Uitslag            } else {
15584cded5e1SGerrit Uitslag                lock($ID);
15594cded5e1SGerrit Uitslag            }
1560f0fda08aSwikidesign
1561f0fda08aSwikidesign            // prepare the new thread file with default stuff
15622e80cd5fSwikidesign            if (!@file_exists($INFO['filepath'])) {
1563f0fda08aSwikidesign                global $TEXT;
1564f0fda08aSwikidesign
1565c3413364SGerrit Uitslag                $TEXT = pageTemplate(($ns ? $ns . ':' : '') . $title);
15661433886fSwikidesign                if (!$TEXT) {
1567c3413364SGerrit Uitslag                    $data = ['id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back];
1568c3413364SGerrit Uitslag                    $TEXT = $this->pageTemplate($data);
15692e80cd5fSwikidesign                }
15702e80cd5fSwikidesign                return 'preview';
1571f0fda08aSwikidesign            } else {
15722e80cd5fSwikidesign                return 'edit';
1573f0fda08aSwikidesign            }
1574f0fda08aSwikidesign        } else {
15752e80cd5fSwikidesign            return 'show';
1576f0fda08aSwikidesign        }
1577f0fda08aSwikidesign    }
1578f0fda08aSwikidesign
1579e7c760b3Swikidesign    /**
158061437513Swikidesign     * Adapted version of pageTemplate() function
1581de7e6f00SGerrit Uitslag     *
1582de7e6f00SGerrit Uitslag     * @param array $data
1583de7e6f00SGerrit Uitslag     * @return string
158461437513Swikidesign     */
1585*283a3029SGerrit Uitslag    protected function pageTemplate($data)
1586*283a3029SGerrit Uitslag    {
1587c3413364SGerrit Uitslag        global $conf, $INFO, $INPUT;
158861437513Swikidesign
158961437513Swikidesign        $id = $data['id'];
1590c3413364SGerrit Uitslag        $user = $INPUT->server->str('REMOTE_USER');
159161437513Swikidesign        $tpl = io_readFile(DOKU_PLUGIN . 'discussion/_template.txt');
159261437513Swikidesign
159361437513Swikidesign        // standard replacements
1594c3413364SGerrit Uitslag        $replace = [
159561437513Swikidesign            '@NS@' => $data['ns'],
159661437513Swikidesign            '@PAGE@' => strtr(noNS($id), '_', ' '),
159761437513Swikidesign            '@USER@' => $user,
159861437513Swikidesign            '@NAME@' => $INFO['userinfo']['name'],
159961437513Swikidesign            '@MAIL@' => $INFO['userinfo']['mail'],
1600d5530824SMichael Hamann            '@DATE@' => dformat(time(), $conf['dformat']),
1601c3413364SGerrit Uitslag        ];
160261437513Swikidesign
160361437513Swikidesign        // additional replacements
160461437513Swikidesign        $replace['@BACK@'] = $data['back'];
160561437513Swikidesign        $replace['@TITLE@'] = $data['title'];
160661437513Swikidesign
160761437513Swikidesign        // avatar if useavatar and avatar plugin available
1608c3413364SGerrit Uitslag        if ($this->getConf('useavatar') && !plugin_isdisabled('avatar')) {
160961437513Swikidesign            $replace['@AVATAR@'] = '{{avatar>' . $user . ' }} ';
161061437513Swikidesign        } else {
161161437513Swikidesign            $replace['@AVATAR@'] = '';
161261437513Swikidesign        }
161361437513Swikidesign
161461437513Swikidesign        // tag if tag plugin is available
1615c3413364SGerrit Uitslag        if (!plugin_isdisabled('tag')) {
161661437513Swikidesign            $replace['@TAG@'] = "\n\n{{tag>}}";
161761437513Swikidesign        } else {
161861437513Swikidesign            $replace['@TAG@'] = '';
161961437513Swikidesign        }
162061437513Swikidesign
1621c3413364SGerrit Uitslag        // perform the replacements in tpl
1622c3413364SGerrit Uitslag        return str_replace(array_keys($replace), array_values($replace), $tpl);
162361437513Swikidesign    }
162461437513Swikidesign
162561437513Swikidesign    /**
1626e7c760b3Swikidesign     * Checks if the CAPTCHA string submitted is valid
1627e7c760b3Swikidesign     */
1628*283a3029SGerrit Uitslag    protected function captchaCheck()
1629*283a3029SGerrit Uitslag    {
1630c3413364SGerrit Uitslag        global $INPUT;
163196bc68a7SMichael Hamann        /** @var helper_plugin_captcha $captcha */
1632c3413364SGerrit Uitslag        if (!$captcha = $this->loadHelper('captcha', false)) {
1633c3413364SGerrit Uitslag            // CAPTCHA is disabled or not available
1634c3413364SGerrit Uitslag            return;
1635c3413364SGerrit Uitslag        }
1636e7c760b3Swikidesign
1637d578a059SMichael Hamann        if ($captcha->isEnabled() && !$captcha->check()) {
1638c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1639c3413364SGerrit Uitslag                $INPUT->set('comment', 'edit');
1640c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1641c3413364SGerrit Uitslag                $INPUT->set('comment', 'show');
16424cded5e1SGerrit Uitslag            }
1643e7c760b3Swikidesign        }
1644e7c760b3Swikidesign    }
1645e7c760b3Swikidesign
1646a1ca9e44Swikidesign    /**
1647bd6dc08eSAdrian Schlegel     * checks if the submitted reCAPTCHA string is valid
1648bd6dc08eSAdrian Schlegel     *
1649bd6dc08eSAdrian Schlegel     * @author Adrian Schlegel <adrian@liip.ch>
1650bd6dc08eSAdrian Schlegel     */
1651*283a3029SGerrit Uitslag    protected function recaptchaCheck()
1652*283a3029SGerrit Uitslag    {
1653c3413364SGerrit Uitslag        global $INPUT;
1654c3413364SGerrit Uitslag        /** @var helper_plugin_recaptcha $recaptcha */
1655c3413364SGerrit Uitslag        if (!$recaptcha = plugin_load('helper', 'recaptcha'))
1656bd6dc08eSAdrian Schlegel            return; // reCAPTCHA is disabled or not available
1657bd6dc08eSAdrian Schlegel
1658bd6dc08eSAdrian Schlegel        // do nothing if logged in user and no reCAPTCHA required
1659c3413364SGerrit Uitslag        if (!$recaptcha->getConf('forusers') && $INPUT->server->has('REMOTE_USER')) return;
1660bd6dc08eSAdrian Schlegel
1661c3413364SGerrit Uitslag        $response = $recaptcha->check();
1662c3413364SGerrit Uitslag        if (!$response->is_valid) {
1663bd6dc08eSAdrian Schlegel            msg($recaptcha->getLang('testfailed'), -1);
1664c3413364SGerrit Uitslag            if ($INPUT->str('comment') == 'save') {
1665c3413364SGerrit Uitslag                $INPUT->str('comment', 'edit');
1666c3413364SGerrit Uitslag            } elseif ($INPUT->str('comment') == 'add') {
1667c3413364SGerrit Uitslag                $INPUT->str('comment', 'show');
16684cded5e1SGerrit Uitslag            }
1669bd6dc08eSAdrian Schlegel        }
1670bd6dc08eSAdrian Schlegel    }
1671bd6dc08eSAdrian Schlegel
1672bd6dc08eSAdrian Schlegel    /**
1673ac818938SMichael Hamann     * Add discussion plugin version to the indexer version
1674ac818938SMichael Hamann     * This means that all pages will be indexed again in order to add the comments
1675ac818938SMichael Hamann     * to the index whenever there has been a change that concerns the index content.
1676de7e6f00SGerrit Uitslag     *
1677de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1678de7e6f00SGerrit Uitslag     * @param $param
1679ac818938SMichael Hamann     */
1680*283a3029SGerrit Uitslag    public function addIndexVersion(Doku_Event $event)
1681*283a3029SGerrit Uitslag    {
1682ac818938SMichael Hamann        $event->data['discussion'] = '0.1';
1683ac818938SMichael Hamann    }
1684ac818938SMichael Hamann
1685ac818938SMichael Hamann    /**
1686a1ca9e44Swikidesign     * Adds the comments to the index
1687de7e6f00SGerrit Uitslag     *
1688de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1689c3413364SGerrit Uitslag     * @param array $param with
1690c3413364SGerrit Uitslag     *  'id' => string 'page'/'id' for respectively INDEXER_PAGE_ADD and FULLTEXT_SNIPPET_CREATE event
1691c3413364SGerrit Uitslag     *  'text' => string 'body'/'text'
1692a1ca9e44Swikidesign     */
1693*283a3029SGerrit Uitslag    public function addCommentsToIndex(Doku_Event $event, $param)
1694*283a3029SGerrit Uitslag    {
1695a1ca9e44Swikidesign        // get .comments meta file name
169610b5d61eSMichael Hamann        $file = metaFN($event->data[$param['id']], '.comments');
1697a1ca9e44Swikidesign
169896b3951aSMichael Hamann        if (!@file_exists($file)) return;
169996b3951aSMichael Hamann        $data = unserialize(io_readFile($file, false));
1700c3413364SGerrit Uitslag
1701c3413364SGerrit Uitslag        // comments are turned off or no comments available to index
1702c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
1703a1ca9e44Swikidesign
1704a1ca9e44Swikidesign        // now add the comments
1705a1ca9e44Swikidesign        if (isset($data['comments'])) {
1706a1ca9e44Swikidesign            foreach ($data['comments'] as $key => $value) {
1707c3413364SGerrit Uitslag                $event->data[$param['text']] .= DOKU_LF . $this->addCommentWords($key, $data);
1708a1ca9e44Swikidesign            }
1709a1ca9e44Swikidesign        }
1710a1ca9e44Swikidesign    }
1711a1ca9e44Swikidesign
1712c3413364SGerrit Uitslag    /**
1713c3413364SGerrit Uitslag     * Checks if the phrase occurs in the comments and return event result true if matching
1714c3413364SGerrit Uitslag     *
1715c3413364SGerrit Uitslag     * @param Doku_Event $event
1716c3413364SGerrit Uitslag     * @param $param
1717c3413364SGerrit Uitslag     * @return void
1718c3413364SGerrit Uitslag     */
1719*283a3029SGerrit Uitslag    public function fulltextPhraseMatchInComments(Doku_Event $event)
1720*283a3029SGerrit Uitslag    {
172110b5d61eSMichael Hamann        if ($event->result === true) return;
172210b5d61eSMichael Hamann
172310b5d61eSMichael Hamann        // get .comments meta file name
172410b5d61eSMichael Hamann        $file = metaFN($event->data['id'], '.comments');
172510b5d61eSMichael Hamann
172610b5d61eSMichael Hamann        if (!@file_exists($file)) return;
172710b5d61eSMichael Hamann        $data = unserialize(io_readFile($file, false));
1728c3413364SGerrit Uitslag
1729c3413364SGerrit Uitslag        // comments are turned off or no comments available to match
1730c3413364SGerrit Uitslag        if (!$data['status'] || $data['number'] == 0) return;
173110b5d61eSMichael Hamann
173210b5d61eSMichael Hamann        $matched = false;
173310b5d61eSMichael Hamann
173410b5d61eSMichael Hamann        // now add the comments
173510b5d61eSMichael Hamann        if (isset($data['comments'])) {
1736c3413364SGerrit Uitslag            foreach ($data['comments'] as $cid => $value) {
1737c3413364SGerrit Uitslag                $matched = $this->phraseMatchInComment($event->data['phrase'], $cid, $data);
173810b5d61eSMichael Hamann                if ($matched) break;
173910b5d61eSMichael Hamann            }
174010b5d61eSMichael Hamann        }
174110b5d61eSMichael Hamann
1742c3413364SGerrit Uitslag        if ($matched) {
174310b5d61eSMichael Hamann            $event->result = true;
174410b5d61eSMichael Hamann        }
1745c3413364SGerrit Uitslag    }
174610b5d61eSMichael Hamann
1747c3413364SGerrit Uitslag    /**
1748c3413364SGerrit Uitslag     * Match the phrase in the comment and its replies
1749c3413364SGerrit Uitslag     *
1750c3413364SGerrit Uitslag     * @param string $phrase phrase to search
1751c3413364SGerrit Uitslag     * @param string $cid comment id
1752c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1753c3413364SGerrit Uitslag     * @param string $parent cid of parent
1754c3413364SGerrit Uitslag     * @return bool if match true, otherwise false
1755c3413364SGerrit Uitslag     */
1756*283a3029SGerrit Uitslag    protected function phraseMatchInComment($phrase, $cid, &$data, $parent = '')
1757*283a3029SGerrit Uitslag    {
175810b5d61eSMichael Hamann        if (!isset($data['comments'][$cid])) return false; // comment was removed
1759c3413364SGerrit Uitslag
176010b5d61eSMichael Hamann        $comment = $data['comments'][$cid];
176110b5d61eSMichael Hamann
176210b5d61eSMichael Hamann        if (!is_array($comment)) return false;             // corrupt datatype
176310b5d61eSMichael Hamann        if ($comment['parent'] != $parent) return false;   // reply to an other comment
176410b5d61eSMichael Hamann        if (!$comment['show']) return false;               // hidden comment
176510b5d61eSMichael Hamann
1766c3413364SGerrit Uitslag        $text = PhpString::strtolower($comment['raw']);
176710b5d61eSMichael Hamann        if (strpos($text, $phrase) !== false) {
176810b5d61eSMichael Hamann            return true;
176910b5d61eSMichael Hamann        }
177010b5d61eSMichael Hamann
177110b5d61eSMichael Hamann        if (is_array($comment['replies'])) {               // and the replies
177210b5d61eSMichael Hamann            foreach ($comment['replies'] as $rid) {
1773c3413364SGerrit Uitslag                if ($this->phraseMatchInComment($phrase, $rid, $data, $cid)) {
177410b5d61eSMichael Hamann                    return true;
177510b5d61eSMichael Hamann                }
177610b5d61eSMichael Hamann            }
177710b5d61eSMichael Hamann        }
177810b5d61eSMichael Hamann        return false;
177910b5d61eSMichael Hamann    }
178010b5d61eSMichael Hamann
1781a1ca9e44Swikidesign    /**
1782c3413364SGerrit Uitslag     * Saves the current comment status and title from metadata into the .comments file
1783de7e6f00SGerrit Uitslag     *
1784de7e6f00SGerrit Uitslag     * @param Doku_Event $event
1785de7e6f00SGerrit Uitslag     * @param $param
1786c1530f74SMichael Hamann     */
1787*283a3029SGerrit Uitslag    public function update_comment_status(Doku_Event $event)
1788*283a3029SGerrit Uitslag    {
1789c1530f74SMichael Hamann        global $ID;
1790c1530f74SMichael Hamann
1791c1530f74SMichael Hamann        $meta = $event->data['current'];
1792c1530f74SMichael Hamann        $file = metaFN($ID, '.comments');
1793f0bcde18SGerrit Uitslag        $status = ($this->isDiscussionEnabled() ? 1 : 0);
1794c3413364SGerrit Uitslag        $title = null;
1795c1530f74SMichael Hamann        if (isset($meta['plugin_discussion'])) {
1796c3413364SGerrit Uitslag            $status = $meta['plugin_discussion']['status']; // 0, 1 or 2
1797c1530f74SMichael Hamann            $title = $meta['plugin_discussion']['title'];
1798c1530f74SMichael Hamann        } elseif ($status == 1) {
1799c1530f74SMichael Hamann            // Don't enable comments when automatic comments are on - this already happens automatically
1800c1530f74SMichael Hamann            // and if comments are turned off in the admin this only updates the .comments file
1801c1530f74SMichael Hamann            return;
1802c1530f74SMichael Hamann        }
1803c1530f74SMichael Hamann
1804c1530f74SMichael Hamann        if ($status || @file_exists($file)) {
1805c3413364SGerrit Uitslag            $data = [];
1806c1530f74SMichael Hamann            if (@file_exists($file)) {
1807c1530f74SMichael Hamann                $data = unserialize(io_readFile($file, false));
1808c1530f74SMichael Hamann            }
1809c1530f74SMichael Hamann
1810c1530f74SMichael Hamann            if (!array_key_exists('title', $data) || $data['title'] !== $title || !isset($data['status']) || $data['status'] !== $status) {
1811c1530f74SMichael Hamann                $data['title'] = $title;
1812c1530f74SMichael Hamann                $data['status'] = $status;
1813c3413364SGerrit Uitslag                if (!isset($data['number'])) {
1814c1530f74SMichael Hamann                    $data['number'] = 0;
1815c3413364SGerrit Uitslag                }
1816c1530f74SMichael Hamann                io_saveFile($file, serialize($data));
1817c1530f74SMichael Hamann            }
1818c1530f74SMichael Hamann        }
1819c1530f74SMichael Hamann    }
1820c1530f74SMichael Hamann
1821c1530f74SMichael Hamann    /**
1822c3413364SGerrit Uitslag     * Return words of a given comment and its replies, suitable to be added to the index
1823de7e6f00SGerrit Uitslag     *
1824c3413364SGerrit Uitslag     * @param string $cid comment id
1825c3413364SGerrit Uitslag     * @param array $data array with all comments by reference
1826c3413364SGerrit Uitslag     * @param string $parent cid of parent
1827de7e6f00SGerrit Uitslag     * @return string
1828a1ca9e44Swikidesign     */
1829*283a3029SGerrit Uitslag    protected function addCommentWords($cid, &$data, $parent = '')
1830*283a3029SGerrit Uitslag    {
1831a1ca9e44Swikidesign
1832efbe59d0Swikidesign        if (!isset($data['comments'][$cid])) return ''; // comment was removed
1833c3413364SGerrit Uitslag
1834a1ca9e44Swikidesign        $comment = $data['comments'][$cid];
1835a1ca9e44Swikidesign
1836efbe59d0Swikidesign        if (!is_array($comment)) return '';             // corrupt datatype
1837efbe59d0Swikidesign        if ($comment['parent'] != $parent) return '';   // reply to an other comment
1838efbe59d0Swikidesign        if (!$comment['show']) return '';               // hidden comment
1839a1ca9e44Swikidesign
1840efbe59d0Swikidesign        $text = $comment['raw'];                        // we only add the raw comment text
1841efbe59d0Swikidesign        if (is_array($comment['replies'])) {            // and the replies
1842efbe59d0Swikidesign            foreach ($comment['replies'] as $rid) {
1843c3413364SGerrit Uitslag                $text .= $this->addCommentWords($rid, $data, $cid);
1844a1ca9e44Swikidesign            }
1845a1ca9e44Swikidesign        }
1846efbe59d0Swikidesign        return ' ' . $text;
1847efbe59d0Swikidesign    }
1848a10b5c98SMichael Klier
1849a10b5c98SMichael Klier    /**
1850a10b5c98SMichael Klier     * Only allow http(s) URLs and append http:// to URLs if needed
1851de7e6f00SGerrit Uitslag     *
1852de7e6f00SGerrit Uitslag     * @param string $url
1853de7e6f00SGerrit Uitslag     * @return string
1854a10b5c98SMichael Klier     */
1855*283a3029SGerrit Uitslag    protected function checkURL($url)
1856*283a3029SGerrit Uitslag    {
1857a10b5c98SMichael Klier        if (preg_match("#^http://|^https://#", $url)) {
1858a10b5c98SMichael Klier            return hsc($url);
1859a10b5c98SMichael Klier        } elseif (substr($url, 0, 4) == 'www.') {
1860c3413364SGerrit Uitslag            return hsc('https://' . $url);
1861a10b5c98SMichael Klier        } else {
1862a10b5c98SMichael Klier            return '';
1863a10b5c98SMichael Klier        }
1864a10b5c98SMichael Klier    }
186531aab30eSGina Haeussge
1866de7e6f00SGerrit Uitslag    /**
1867de7e6f00SGerrit Uitslag     * Sort threads
1868de7e6f00SGerrit Uitslag     *
1869*283a3029SGerrit Uitslag     * @param array $a array with comment properties
1870*283a3029SGerrit Uitslag     * @param array $b array with comment properties
1871de7e6f00SGerrit Uitslag     * @return int
1872de7e6f00SGerrit Uitslag     */
1873*283a3029SGerrit Uitslag    function sortThreadsOnCreation($a, $b)
1874*283a3029SGerrit Uitslag    {
18757e018cb6Slpaulsen93        if (is_array($a['date'])) {
18767e018cb6Slpaulsen93            // new format
187731aab30eSGina Haeussge            $createdA = $a['date']['created'];
18787e018cb6Slpaulsen93        } else {
18797e018cb6Slpaulsen93            // old format
188031aab30eSGina Haeussge            $createdA = $a['date'];
188131aab30eSGina Haeussge        }
188231aab30eSGina Haeussge
18837e018cb6Slpaulsen93        if (is_array($b['date'])) {
18847e018cb6Slpaulsen93            // new format
188531aab30eSGina Haeussge            $createdB = $b['date']['created'];
18867e018cb6Slpaulsen93        } else {
18877e018cb6Slpaulsen93            // old format
188831aab30eSGina Haeussge            $createdB = $b['date'];
188931aab30eSGina Haeussge        }
189031aab30eSGina Haeussge
18914cded5e1SGerrit Uitslag        if ($createdA == $createdB) {
189231aab30eSGina Haeussge            return 0;
18934cded5e1SGerrit Uitslag        } else {
189431aab30eSGina Haeussge            return ($createdA < $createdB) ? -1 : 1;
189531aab30eSGina Haeussge        }
18964cded5e1SGerrit Uitslag    }
189731aab30eSGina Haeussge
1898c3413364SGerrit Uitslag}
1899c3413364SGerrit Uitslag
1900c3413364SGerrit Uitslag
1901