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 */ 50283a3029SGerrit Uitslagclass action_plugin_discussion extends DokuWiki_Action_Plugin 51283a3029SGerrit Uitslag{ 52f0fda08aSwikidesign 53de7e6f00SGerrit Uitslag /** @var helper_plugin_avatar */ 54c3413364SGerrit Uitslag protected $avatar = null; 55c3413364SGerrit Uitslag /** @var null|string */ 56c3413364SGerrit Uitslag protected $style = null; 57c3413364SGerrit Uitslag /** @var null|bool */ 58c3413364SGerrit Uitslag protected $useAvatar = null; 59de7e6f00SGerrit Uitslag /** @var helper_plugin_discussion */ 60c3413364SGerrit Uitslag protected $helper = null; 61e6b2f142Slupo49 62de7e6f00SGerrit Uitslag /** 63de7e6f00SGerrit Uitslag * load helper 64de7e6f00SGerrit Uitslag */ 65283a3029SGerrit Uitslag public function __construct() 66283a3029SGerrit Uitslag { 67e6b2f142Slupo49 $this->helper = plugin_load('helper', 'discussion'); 68e6b2f142Slupo49 } 69f1c6610eSpierre.spring 70de7e6f00SGerrit Uitslag /** 71de7e6f00SGerrit Uitslag * Register the handlers 72de7e6f00SGerrit Uitslag * 73c3413364SGerrit Uitslag * @param Doku_Event_Handler $controller DokuWiki's event controller object. 74de7e6f00SGerrit Uitslag */ 75283a3029SGerrit Uitslag public function register(Doku_Event_Handler $controller) 76283a3029SGerrit Uitslag { 77c3413364SGerrit Uitslag $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentActions'); 78c3413364SGerrit Uitslag $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'renderCommentsSection'); 79c3413364SGerrit Uitslag $controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'addCommentsToIndex', ['id' => 'page', 'text' => 'body']); 80c3413364SGerrit Uitslag $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'addCommentsToIndex', ['id' => 'id', 'text' => 'text']); 81c3413364SGerrit Uitslag $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'addIndexVersion', []); 82c3413364SGerrit Uitslag $controller->register_hook('FULLTEXT_PHRASE_MATCH', 'AFTER', $this, 'fulltextPhraseMatchInComments', []); 83c3413364SGerrit Uitslag $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'update_comment_status', []); 84c3413364SGerrit Uitslag $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addToolbarToCommentfield', []); 85c3413364SGerrit Uitslag $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'modifyToolbar', []); 86c3413364SGerrit Uitslag $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxPreviewComments', []); 87c3413364SGerrit Uitslag $controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'addDiscussionToTOC', []); 88b8fdc796SMichael Klier } 89b8fdc796SMichael Klier 90b8fdc796SMichael Klier /** 91b8fdc796SMichael Klier * Preview Comments 92b8fdc796SMichael Klier * 93de7e6f00SGerrit Uitslag * @param Doku_Event $event 94de7e6f00SGerrit Uitslag * @param $params 95283a3029SGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 96283a3029SGerrit Uitslag * 97b8fdc796SMichael Klier */ 98283a3029SGerrit Uitslag public function ajaxPreviewComments(Doku_Event $event) 99283a3029SGerrit 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 120283a3029SGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 121283a3029SGerrit Uitslag * 1225886c85bSMichael Klier */ 123283a3029SGerrit Uitslag public function addDiscussionToTOC(Doku_Event $event) 124283a3029SGerrit 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 141283a3029SGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 142283a3029SGerrit Uitslag * 1432d4bee9aSMichael Klier */ 144283a3029SGerrit Uitslag public function modifyToolbar(Doku_Event $event) 145283a3029SGerrit 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 169283a3029SGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 170283a3029SGerrit Uitslag * 1712d4bee9aSMichael Klier */ 172283a3029SGerrit Uitslag public function addToolbarToCommentfield(Doku_Event $event) 173283a3029SGerrit 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 */ 196283a3029SGerrit Uitslag public function handleCommentActions(Doku_Event $event) 197283a3029SGerrit 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 */ 312283a3029SGerrit Uitslag public function renderCommentsSection(Doku_Event $event) 313283a3029SGerrit 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 } 322283a3029SGerrit Uitslag 323c3413364SGerrit Uitslag switch ($INPUT->str('comment')) { 324a1d93126SGina Haeussge case 'edit': 325c3413364SGerrit Uitslag $this->showDiscussionSection(null, $cid); 326a1d93126SGina Haeussge break; 327283a3029SGerrit 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 */ 336283a3029SGerrit Uitslag protected function redirect($cid) 337283a3029SGerrit 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 */ 367283a3029SGerrit Uitslag public function isDiscussionEnabled() 368283a3029SGerrit Uitslag { 369*f8c74f2eSGerrit Uitslag global $ID; 370f0bcde18SGerrit Uitslag 371d406a452SGerrit Uitslag if ($this->getConf('excluded_ns') == '') { 372d406a452SGerrit Uitslag $isNamespaceExcluded = false; 373d406a452SGerrit Uitslag } else { 374283a3029SGerrit Uitslag $ns = getNS($ID); // $INFO['namespace'] is not yet available, if used in update_comment_status() 375*f8c74f2eSGerrit Uitslag $isNamespaceExcluded = preg_match($this->getConf('excluded_ns'), $ns); 376d406a452SGerrit Uitslag } 377f0bcde18SGerrit Uitslag 378f0bcde18SGerrit Uitslag if ($this->getConf('automatic')) { 379f0bcde18SGerrit Uitslag if ($isNamespaceExcluded) { 380f0bcde18SGerrit Uitslag return false; 381f0bcde18SGerrit Uitslag } else { 382f0bcde18SGerrit Uitslag return true; 383f0bcde18SGerrit Uitslag } 384f0bcde18SGerrit Uitslag } else { 385f0bcde18SGerrit Uitslag if ($isNamespaceExcluded) { 386f0bcde18SGerrit Uitslag return true; 387f0bcde18SGerrit Uitslag } else { 388f0bcde18SGerrit Uitslag return false; 389f0bcde18SGerrit Uitslag } 390f0bcde18SGerrit Uitslag } 391f0bcde18SGerrit Uitslag } 392f0bcde18SGerrit Uitslag 393f0bcde18SGerrit Uitslag /** 394c3413364SGerrit Uitslag * Shows all comments of the current page, if no reply or edit requested, then comment form is shown on the end 395c3413364SGerrit Uitslag * 396c3413364SGerrit Uitslag * @param null|string $reply comment id on which the user requested a reply 397c3413364SGerrit Uitslag * @param null|string $edit comment id which the user requested for editing 398f0fda08aSwikidesign */ 399283a3029SGerrit Uitslag protected function showDiscussionSection($reply = null, $edit = null) 400283a3029SGerrit Uitslag { 401c3413364SGerrit Uitslag global $ID, $INFO, $INPUT; 402573e23a1Swikidesign 403479dd10fSwikidesign // get .comments meta file name 404f0fda08aSwikidesign $file = metaFN($ID, '.comments'); 405f0fda08aSwikidesign 406c3413364SGerrit Uitslag if (!$INFO['exists']) return; 407c3413364SGerrit Uitslag if (!@file_exists($file) && !$this->isDiscussionEnabled()) return; 408c3413364SGerrit Uitslag if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('showguests')) return; 409f0fda08aSwikidesign 4102b18adb9SMichael Klier // load data 411c3413364SGerrit Uitslag $data = []; 4122b18adb9SMichael Klier if (@file_exists($file)) { 4132b18adb9SMichael Klier $data = unserialize(io_readFile($file, false)); 414c3413364SGerrit Uitslag // comments are turned off 415c3413364SGerrit Uitslag if (!$data['status']) { 416c3413364SGerrit Uitslag return; 417c3413364SGerrit Uitslag } 418f0bcde18SGerrit Uitslag } elseif (!@file_exists($file) && $this->isDiscussionEnabled() && $INFO['exists']) { 4192b18adb9SMichael Klier // set status to show the comment form 4202b18adb9SMichael Klier $data['status'] = 1; 4212b18adb9SMichael Klier $data['number'] = 0; 4222b18adb9SMichael Klier } 423f0fda08aSwikidesign 424a1599850SMichael Klier // show discussion wrapper only on certain circumstances 425c3413364SGerrit Uitslag if (empty($data['comments']) || !is_array($data['comments'])) { 426c3413364SGerrit Uitslag $cnt = 0; 4273e19949cSMark Prins $keys = []; 428c3413364SGerrit Uitslag } else { 429c3413364SGerrit Uitslag $cnt = count($data['comments']); 430c3413364SGerrit Uitslag $keys = array_keys($data['comments']); 4313e19949cSMark Prins } 432c3413364SGerrit Uitslag 433de7e6f00SGerrit Uitslag $show = false; 434c3413364SGerrit Uitslag if ($cnt > 1 || ($cnt == 1 && $data['comments'][$keys[0]]['show'] == 1) 435c3413364SGerrit Uitslag || $this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) { 436a1599850SMichael Klier $show = true; 437f0fda08aSwikidesign // section title 43807c376bbSwikidesign $title = ($data['title'] ? hsc($data['title']) : $this->getLang('discussion')); 43946178401Slupo49 ptln('<div class="comment_wrapper" id="comment_wrapper">'); // the id value is used for visibility toggling the section 4404a0a1bd2Swikidesign ptln('<h2><a name="discussion__section" id="discussion__section">', 2); 4414a0a1bd2Swikidesign ptln($title, 4); 4424a0a1bd2Swikidesign ptln('</a></h2>', 2); 4434a0a1bd2Swikidesign ptln('<div class="level2 hfeed">', 2); 444a1599850SMichael Klier } 445a1599850SMichael Klier 446f0fda08aSwikidesign // now display the comments 447f0fda08aSwikidesign if (isset($data['comments'])) { 44831aab30eSGina Haeussge if (!$this->getConf('usethreading')) { 449c3413364SGerrit Uitslag $data['comments'] = $this->flattenThreads($data['comments']); 450283a3029SGerrit Uitslag uasort($data['comments'], [$this, 'sortThreadsOnCreation']); 45131aab30eSGina Haeussge } 452dbd9d5cdSMichael Klier if ($this->getConf('newestfirst')) { 453dbd9d5cdSMichael Klier $data['comments'] = array_reverse($data['comments']); 454dbd9d5cdSMichael Klier } 455c3413364SGerrit Uitslag foreach ($data['comments'] as $cid => $value) { 456c3413364SGerrit Uitslag if ($cid == $edit) { // edit form 457c3413364SGerrit Uitslag $this->showCommentForm($value['raw'], 'save', $edit); 458c3413364SGerrit Uitslag } else { 459c3413364SGerrit Uitslag $this->showCommentWithReplies($cid, $data, '', $reply); 460c3413364SGerrit Uitslag } 461f0fda08aSwikidesign } 462f0fda08aSwikidesign } 463f0fda08aSwikidesign 464c3413364SGerrit Uitslag // comment form shown on the end, if no comment form of $reply or $edit is requested before 465c3413364SGerrit Uitslag if ($data['status'] == 1 && (!$reply || !$this->getConf('usethreading')) && !$edit) { 466c3413364SGerrit Uitslag $this->showCommentForm(''); 467c3413364SGerrit Uitslag } 468f0fda08aSwikidesign 469a1599850SMichael Klier if ($show) { 4704a0a1bd2Swikidesign ptln('</div>', 2); // level2 hfeed 4714a0a1bd2Swikidesign ptln('</div>'); // comment_wrapper 472a1599850SMichael Klier } 473f0fda08aSwikidesign 47446178401Slupo49 // check for toggle print configuration 47546178401Slupo49 if ($this->getConf('visibilityButton')) { 47646178401Slupo49 // print the hide/show discussion section button 477c3413364SGerrit Uitslag $this->showDiscussionToggleButton(); 47846178401Slupo49 } 479f0fda08aSwikidesign } 480f0fda08aSwikidesign 481de7e6f00SGerrit Uitslag /** 482c3413364SGerrit Uitslag * Remove the parent-child relation, such that the comment structure becomes flat 483c3413364SGerrit Uitslag * 484c3413364SGerrit Uitslag * @param array $comments array with all comments 485c3413364SGerrit Uitslag * @param null|array $cids comment ids of replies, which should be flatten 486c3413364SGerrit Uitslag * @return array returned array with flattened comment structure 487de7e6f00SGerrit Uitslag */ 488283a3029SGerrit Uitslag protected function flattenThreads($comments, $cids = null) 489283a3029SGerrit Uitslag { 490c3413364SGerrit Uitslag if (is_null($cids)) { 491c3413364SGerrit Uitslag $cids = array_keys($comments); 492c3413364SGerrit Uitslag } 49331aab30eSGina Haeussge 494c3413364SGerrit Uitslag foreach ($cids as $cid) { 49531aab30eSGina Haeussge if (!empty($comments[$cid]['replies'])) { 49631aab30eSGina Haeussge $rids = $comments[$cid]['replies']; 497c3413364SGerrit Uitslag $comments = $this->flattenThreads($comments, $rids); 498c3413364SGerrit Uitslag $comments[$cid]['replies'] = []; 49931aab30eSGina Haeussge } 50031aab30eSGina Haeussge $comments[$cid]['parent'] = ''; 50131aab30eSGina Haeussge } 50231aab30eSGina Haeussge return $comments; 50331aab30eSGina Haeussge } 50431aab30eSGina Haeussge 505f0fda08aSwikidesign /** 506f0fda08aSwikidesign * Adds a new comment and then displays all comments 507de7e6f00SGerrit Uitslag * 508c3413364SGerrit Uitslag * @param array $comment with 509c3413364SGerrit Uitslag * 'raw' => string comment text, 510c3413364SGerrit Uitslag * 'user' => [ 511c3413364SGerrit Uitslag * 'id' => string, 512c3413364SGerrit Uitslag * 'name' => string, 513c3413364SGerrit Uitslag * 'mail' => string 514c3413364SGerrit Uitslag * ], 515c3413364SGerrit Uitslag * 'date' => [ 516c3413364SGerrit Uitslag * 'created' => int timestamp 517c3413364SGerrit Uitslag * ] 518c3413364SGerrit Uitslag * 'show' => bool 519c3413364SGerrit Uitslag * 'subscribe' => bool 520c3413364SGerrit Uitslag * @param string $parent comment id of parent 521de7e6f00SGerrit Uitslag * @return bool 522f0fda08aSwikidesign */ 523283a3029SGerrit Uitslag protected function add($comment, $parent) 524283a3029SGerrit Uitslag { 525c3413364SGerrit Uitslag global $ID, $TEXT, $INPUT; 526f0fda08aSwikidesign 527c3413364SGerrit Uitslag $originalTxt = $TEXT; // set $TEXT to comment text for wordblock check 528f0fda08aSwikidesign $TEXT = $comment['raw']; 529f0fda08aSwikidesign 530f0fda08aSwikidesign // spamcheck against the DokuWiki blacklist 531f0fda08aSwikidesign if (checkwordblock()) { 532f0fda08aSwikidesign msg($this->getLang('wordblock'), -1); 533f0fda08aSwikidesign return false; 534f0fda08aSwikidesign } 535f0fda08aSwikidesign 536c3413364SGerrit Uitslag if (!$this->getConf('allowguests') 537c3413364SGerrit Uitslag && $comment['user']['id'] != $INPUT->server->str('REMOTE_USER') 5384cded5e1SGerrit Uitslag ) { 5393011fb8bSMichael Klier return false; // guest comments not allowed 5404cded5e1SGerrit Uitslag } 5413011fb8bSMichael Klier 542c3413364SGerrit Uitslag $TEXT = $originalTxt; // restore global $TEXT 543f0fda08aSwikidesign 544f0fda08aSwikidesign // get discussion meta file name 545f0fda08aSwikidesign $file = metaFN($ID, '.comments'); 546f0fda08aSwikidesign 5472b18adb9SMichael Klier // create comments file if it doesn't exist yet 5482b18adb9SMichael Klier if (!@file_exists($file)) { 549c3413364SGerrit Uitslag $data = ['status' => 1, 'number' => 0]; 5502b18adb9SMichael Klier io_saveFile($file, serialize($data)); 5512b18adb9SMichael Klier } else { 552f0fda08aSwikidesign $data = unserialize(io_readFile($file, false)); 553c3413364SGerrit Uitslag // comments off or closed 554c3413364SGerrit Uitslag if ($data['status'] != 1) { 555c3413364SGerrit Uitslag return false; 556c3413364SGerrit Uitslag } 5572b18adb9SMichael Klier } 5582b18adb9SMichael Klier 5593011fb8bSMichael Klier if ($comment['date']['created']) { 5603011fb8bSMichael Klier $date = strtotime($comment['date']['created']); 5613011fb8bSMichael Klier } else { 5623011fb8bSMichael Klier $date = time(); 5633011fb8bSMichael Klier } 564f0fda08aSwikidesign 5653011fb8bSMichael Klier if ($date == -1) { 5663011fb8bSMichael Klier $date = time(); 5673011fb8bSMichael Klier } 5683011fb8bSMichael Klier 5696046f25cSwikidesign $cid = md5($comment['user']['id'] . $date); // create a unique id 570f0fda08aSwikidesign 5713011fb8bSMichael Klier if (!is_array($data['comments'][$parent])) { 572c3413364SGerrit Uitslag $parent = null; // invalid parent comment 5733011fb8bSMichael Klier } 574f0fda08aSwikidesign 575f0fda08aSwikidesign // render the comment 576c3413364SGerrit Uitslag $xhtml = $this->renderComment($comment['raw']); 577f0fda08aSwikidesign 578f0fda08aSwikidesign // fill in the new comment 579c3413364SGerrit Uitslag $data['comments'][$cid] = [ 5806046f25cSwikidesign 'user' => $comment['user'], 581c3413364SGerrit Uitslag 'date' => ['created' => $date], 5826046f25cSwikidesign 'raw' => $comment['raw'], 583f0fda08aSwikidesign 'xhtml' => $xhtml, 584f0fda08aSwikidesign 'parent' => $parent, 585c3413364SGerrit Uitslag 'replies' => [], 586a44bc9f7SMichael Klier 'show' => $comment['show'] 587c3413364SGerrit Uitslag ]; 588f0fda08aSwikidesign 5893011fb8bSMichael Klier if ($comment['subscribe']) { 5903011fb8bSMichael Klier $mail = $comment['user']['mail']; 5913011fb8bSMichael Klier if ($data['subscribers']) { 5923011fb8bSMichael Klier if (!$data['subscribers'][$mail]) { 5939881d835SMichael Klier $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand()); 5949881d835SMichael Klier $data['subscribers'][$mail]['active'] = false; 5959881d835SMichael Klier $data['subscribers'][$mail]['confirmsent'] = false; 5969881d835SMichael Klier } else { 5979881d835SMichael Klier // convert old style subscribers and set them active 5989881d835SMichael Klier if (!is_array($data['subscribers'][$mail])) { 5999881d835SMichael Klier $hash = $data['subscribers'][$mail]; 6009881d835SMichael Klier $data['subscribers'][$mail]['hash'] = $hash; 6019881d835SMichael Klier $data['subscribers'][$mail]['active'] = true; 6029881d835SMichael Klier $data['subscribers'][$mail]['confirmsent'] = true; 6039881d835SMichael Klier } 6043011fb8bSMichael Klier } 6053011fb8bSMichael Klier } else { 6069881d835SMichael Klier $data['subscribers'][$mail]['hash'] = md5($mail . mt_rand()); 6079881d835SMichael Klier $data['subscribers'][$mail]['active'] = false; 6089881d835SMichael Klier $data['subscribers'][$mail]['confirmsent'] = false; 6093011fb8bSMichael Klier } 6103011fb8bSMichael Klier } 6113011fb8bSMichael Klier 612f0fda08aSwikidesign // update parent comment 6134cded5e1SGerrit Uitslag if ($parent) { 6144cded5e1SGerrit Uitslag $data['comments'][$parent]['replies'][] = $cid; 6154cded5e1SGerrit Uitslag } 616f0fda08aSwikidesign 617f0fda08aSwikidesign // update the number of comments 618f0fda08aSwikidesign $data['number']++; 619f0fda08aSwikidesign 620f0fda08aSwikidesign // notify subscribers of the page 6218b42cefbSMichael Klier $data['comments'][$cid]['cid'] = $cid; 622c3413364SGerrit Uitslag $this->notify($data['comments'][$cid], $data['subscribers']); 623f0fda08aSwikidesign 6249881d835SMichael Klier // save the comment metadata file 6259881d835SMichael Klier io_saveFile($file, serialize($data)); 626c3413364SGerrit Uitslag $this->addLogEntry($date, $ID, 'cc', '', $cid); 6279881d835SMichael Klier 628c3413364SGerrit Uitslag $this->redirect($cid); 629f0fda08aSwikidesign return true; 630f0fda08aSwikidesign } 631f0fda08aSwikidesign 632f0fda08aSwikidesign /** 633f0fda08aSwikidesign * Saves the comment with the given ID and then displays all comments 634de7e6f00SGerrit Uitslag * 635c3413364SGerrit Uitslag * @param array|string $cids array with comment ids to save, or a single string comment id 636c3413364SGerrit Uitslag * @param string $raw if empty comment is deleted, otherwise edited text is stored (note: storing is per one cid!) 637c3413364SGerrit Uitslag * @param string|null $act 'toogle', 'show', 'hide', null. If null, it depends on $raw 638c3413364SGerrit Uitslag * @return bool succeed? 639f0fda08aSwikidesign */ 640283a3029SGerrit Uitslag public function save($cids, $raw, $act = null) 641283a3029SGerrit Uitslag { 642c3413364SGerrit Uitslag global $ID, $INPUT; 643f0fda08aSwikidesign 644c3413364SGerrit Uitslag if (empty($cids)) return false; // do nothing if we get no comment id 645757550e8SMichael Klier 6462ee3dca3Swikidesign if ($raw) { 6472ee3dca3Swikidesign global $TEXT; 6482ee3dca3Swikidesign 649f0fda08aSwikidesign $otxt = $TEXT; // set $TEXT to comment text for wordblock check 650f0fda08aSwikidesign $TEXT = $raw; 651f0fda08aSwikidesign 652f0fda08aSwikidesign // spamcheck against the DokuWiki blacklist 653f0fda08aSwikidesign if (checkwordblock()) { 654f0fda08aSwikidesign msg($this->getLang('wordblock'), -1); 655f0fda08aSwikidesign return false; 656f0fda08aSwikidesign } 657f0fda08aSwikidesign 658f0fda08aSwikidesign $TEXT = $otxt; // restore global $TEXT 6592ee3dca3Swikidesign } 660f0fda08aSwikidesign 661f0fda08aSwikidesign // get discussion meta file name 662f0fda08aSwikidesign $file = metaFN($ID, '.comments'); 663f0fda08aSwikidesign $data = unserialize(io_readFile($file, false)); 664f0fda08aSwikidesign 665c3413364SGerrit Uitslag if (!is_array($cids)) { 666c3413364SGerrit Uitslag $cids = [$cids]; 667c3413364SGerrit Uitslag } 668264b7327Swikidesign foreach ($cids as $cid) { 669264b7327Swikidesign 6706046f25cSwikidesign if (is_array($data['comments'][$cid]['user'])) { 6716046f25cSwikidesign $user = $data['comments'][$cid]['user']['id']; 6726046f25cSwikidesign $convert = false; 6736046f25cSwikidesign } else { 6746046f25cSwikidesign $user = $data['comments'][$cid]['user']; 6756046f25cSwikidesign $convert = true; 6766046f25cSwikidesign } 6776046f25cSwikidesign 678f0fda08aSwikidesign // someone else was trying to edit our comment -> abort 679c3413364SGerrit Uitslag if ($user != $INPUT->server->str('REMOTE_USER') && !$this->helper->isDiscussionMod()) { 680c3413364SGerrit Uitslag return false; 681c3413364SGerrit Uitslag } 682f0fda08aSwikidesign 683f0fda08aSwikidesign $date = time(); 684f0fda08aSwikidesign 6856046f25cSwikidesign // need to convert to new format? 6866046f25cSwikidesign if ($convert) { 687c3413364SGerrit Uitslag $data['comments'][$cid]['user'] = [ 6886046f25cSwikidesign 'id' => $user, 6896046f25cSwikidesign 'name' => $data['comments'][$cid]['name'], 6906046f25cSwikidesign 'mail' => $data['comments'][$cid]['mail'], 6916046f25cSwikidesign 'url' => $data['comments'][$cid]['url'], 6926046f25cSwikidesign 'address' => $data['comments'][$cid]['address'], 693c3413364SGerrit Uitslag ]; 694c3413364SGerrit Uitslag $data['comments'][$cid]['date'] = [ 6956046f25cSwikidesign 'created' => $data['comments'][$cid]['date'] 696c3413364SGerrit Uitslag ]; 6976046f25cSwikidesign } 6986046f25cSwikidesign 699264b7327Swikidesign if ($act == 'toogle') { // toogle visibility 700f0fda08aSwikidesign $now = $data['comments'][$cid]['show']; 701f0fda08aSwikidesign $data['comments'][$cid]['show'] = !$now; 702c3413364SGerrit Uitslag $data['number'] = $this->countVisibleComments($data); 703f0fda08aSwikidesign 704f0fda08aSwikidesign $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc'); 705f0fda08aSwikidesign 706264b7327Swikidesign } elseif ($act == 'show') { // show comment 707264b7327Swikidesign $data['comments'][$cid]['show'] = true; 708c3413364SGerrit Uitslag $data['number'] = $this->countVisibleComments($data); 709264b7327Swikidesign 710573e23a1Swikidesign $type = 'sc'; // show comment 711264b7327Swikidesign 712264b7327Swikidesign } elseif ($act == 'hide') { // hide comment 713264b7327Swikidesign $data['comments'][$cid]['show'] = false; 714c3413364SGerrit Uitslag $data['number'] = $this->countVisibleComments($data); 715264b7327Swikidesign 716573e23a1Swikidesign $type = 'hc'; // hide comment 717264b7327Swikidesign 718f0fda08aSwikidesign } elseif (!$raw) { // remove the comment 719c3413364SGerrit Uitslag $data['comments'] = $this->removeComment($cid, $data['comments']); 720c3413364SGerrit Uitslag $data['number'] = $this->countVisibleComments($data); 721f0fda08aSwikidesign 722573e23a1Swikidesign $type = 'dc'; // delete comment 723f0fda08aSwikidesign 724f0fda08aSwikidesign } else { // save changed comment 725c3413364SGerrit Uitslag $xhtml = $this->renderComment($raw); 726f0fda08aSwikidesign 727f0fda08aSwikidesign // now change the comment's content 7286046f25cSwikidesign $data['comments'][$cid]['date']['modified'] = $date; 7296046f25cSwikidesign $data['comments'][$cid]['raw'] = $raw; 730f0fda08aSwikidesign $data['comments'][$cid]['xhtml'] = $xhtml; 731f0fda08aSwikidesign 732573e23a1Swikidesign $type = 'ec'; // edit comment 733f0fda08aSwikidesign } 734264b7327Swikidesign } 735264b7327Swikidesign 736f0fda08aSwikidesign // save the comment metadata file 737f0fda08aSwikidesign io_saveFile($file, serialize($data)); 738c3413364SGerrit Uitslag $this->addLogEntry($date, $ID, $type, '', $cid); 739f0fda08aSwikidesign 740c3413364SGerrit Uitslag $this->redirect($cid); 741f0fda08aSwikidesign return true; 742f0fda08aSwikidesign } 743f0fda08aSwikidesign 744f0fda08aSwikidesign /** 745c3413364SGerrit Uitslag * Recursive function to remove a comment from the data array 746c3413364SGerrit Uitslag * 747c3413364SGerrit Uitslag * @param string $cid comment id to be removed 748c3413364SGerrit Uitslag * @param array $comments array with all comments 749c3413364SGerrit Uitslag * @return array returns modified array with all remaining comments 750efbe59d0Swikidesign */ 751283a3029SGerrit Uitslag protected function removeComment($cid, $comments) 752283a3029SGerrit Uitslag { 753efbe59d0Swikidesign if (is_array($comments[$cid]['replies'])) { 754efbe59d0Swikidesign foreach ($comments[$cid]['replies'] as $rid) { 755c3413364SGerrit Uitslag $comments = $this->removeComment($rid, $comments); 756efbe59d0Swikidesign } 757efbe59d0Swikidesign } 758efbe59d0Swikidesign unset($comments[$cid]); 759efbe59d0Swikidesign return $comments; 760efbe59d0Swikidesign } 761efbe59d0Swikidesign 762efbe59d0Swikidesign /** 763f0fda08aSwikidesign * Prints an individual comment 764de7e6f00SGerrit Uitslag * 765c3413364SGerrit Uitslag * @param string $cid comment id 766c3413364SGerrit Uitslag * @param array $data array with all comments by reference 767c3413364SGerrit Uitslag * @param string $parent comment id of parent 768c3413364SGerrit Uitslag * @param string $reply comment id on which the user requested a reply 769c3413364SGerrit Uitslag * @param bool $isVisible is marked as visible 770f0fda08aSwikidesign */ 771283a3029SGerrit Uitslag protected function showCommentWithReplies($cid, &$data, $parent = '', $reply = '', $isVisible = true) 772283a3029SGerrit Uitslag { 773c3413364SGerrit Uitslag // comment was removed 774c3413364SGerrit Uitslag if (!isset($data['comments'][$cid])) { 775c3413364SGerrit Uitslag return; 776c3413364SGerrit Uitslag } 777f0fda08aSwikidesign $comment = $data['comments'][$cid]; 778f0fda08aSwikidesign 779c3413364SGerrit Uitslag // corrupt datatype 780c3413364SGerrit Uitslag if (!is_array($comment)) { 781c3413364SGerrit Uitslag return; 782c3413364SGerrit Uitslag } 783f0fda08aSwikidesign 784c3413364SGerrit Uitslag // handle only replies to given parent comment 785c3413364SGerrit Uitslag if ($comment['parent'] != $parent) { 786c3413364SGerrit Uitslag return; 787c3413364SGerrit Uitslag } 788f0fda08aSwikidesign 789c3413364SGerrit Uitslag // comment hidden 790c3413364SGerrit Uitslag if (!$comment['show']) { 791c3413364SGerrit Uitslag if ($this->helper->isDiscussionMod()) { 792c3413364SGerrit Uitslag $hidden = ' comment_hidden'; 793c3413364SGerrit Uitslag } else { 794c3413364SGerrit Uitslag return; 795c3413364SGerrit Uitslag } 7964a0a1bd2Swikidesign } else { 7974a0a1bd2Swikidesign $hidden = ''; 798f0fda08aSwikidesign } 799f0fda08aSwikidesign 800f1c6610eSpierre.spring // print the actual comment 801c3413364SGerrit Uitslag $this->showComment($cid, $data, $parent, $reply, $isVisible, $hidden); 802f1c6610eSpierre.spring // replies to this comment entry? 803c3413364SGerrit Uitslag $this->showReplies($cid, $data, $reply, $isVisible); 804f1c6610eSpierre.spring // reply form 805c3413364SGerrit Uitslag $this->showReplyForm($cid, $reply); 806f1c6610eSpierre.spring } 807f1c6610eSpierre.spring 808de7e6f00SGerrit Uitslag /** 809c3413364SGerrit Uitslag * Print the comment 810c3413364SGerrit Uitslag * 811c3413364SGerrit Uitslag * @param string $cid comment id 812c3413364SGerrit Uitslag * @param array $data array with all comments by reference 813c3413364SGerrit Uitslag * @param string $parent comment id of parent 814c3413364SGerrit Uitslag * @param string $reply comment id on which the user requested a reply 815c3413364SGerrit Uitslag * @param bool $isVisible is marked as visible 816c3413364SGerrit Uitslag * @param string $hidden extra class, for the admin only hidden view 817de7e6f00SGerrit Uitslag */ 818283a3029SGerrit Uitslag protected function showComment($cid, &$data, $parent, $reply, $isVisible, $hidden) 819283a3029SGerrit Uitslag { 820c3413364SGerrit Uitslag global $conf, $lang, $HIGH, $INPUT; 821f1c6610eSpierre.spring $comment = $data['comments'][$cid]; 822f1c6610eSpierre.spring 823f0fda08aSwikidesign // comment head with date and user data 8244a0a1bd2Swikidesign ptln('<div class="hentry' . $hidden . '">', 4); 8254a0a1bd2Swikidesign ptln('<div class="comment_head">', 6); 8261810ba9cSMichael Klier ptln('<a name="comment_' . $cid . '" id="comment_' . $cid . '"></a>', 8); 8274a0a1bd2Swikidesign $head = '<span class="vcard author">'; 828f0fda08aSwikidesign 8296046f25cSwikidesign // prepare variables 8306046f25cSwikidesign if (is_array($comment['user'])) { // new format 8316046f25cSwikidesign $user = $comment['user']['id']; 8326046f25cSwikidesign $name = $comment['user']['name']; 8336046f25cSwikidesign $mail = $comment['user']['mail']; 8346046f25cSwikidesign $url = $comment['user']['url']; 8356046f25cSwikidesign $address = $comment['user']['address']; 8366046f25cSwikidesign } else { // old format 8376046f25cSwikidesign $user = $comment['user']; 8386046f25cSwikidesign $name = $comment['name']; 8396046f25cSwikidesign $mail = $comment['mail']; 8406046f25cSwikidesign $url = $comment['url']; 8416046f25cSwikidesign $address = $comment['address']; 8426046f25cSwikidesign } 8436046f25cSwikidesign if (is_array($comment['date'])) { // new format 8446046f25cSwikidesign $created = $comment['date']['created']; 845283a3029SGerrit Uitslag $modified = $comment['date']['modified'] ?? null; 8466046f25cSwikidesign } else { // old format 8476046f25cSwikidesign $created = $comment['date']; 8486046f25cSwikidesign $modified = $comment['edited']; 8496046f25cSwikidesign } 8506046f25cSwikidesign 8510ff5ab97SMichael Klier // show username or real name? 852c3413364SGerrit Uitslag if (!$this->getConf('userealname') && $user) { 8530ff5ab97SMichael Klier $showname = $user; 8540ff5ab97SMichael Klier } else { 8550ff5ab97SMichael Klier $showname = $name; 8560ff5ab97SMichael Klier } 8570ff5ab97SMichael Klier 858ce7b17cfSwikidesign // show avatar image? 859c3413364SGerrit Uitslag if ($this->useAvatar()) { 86012ca6034SMichael Klier $user_data['name'] = $name; 86112ca6034SMichael Klier $user_data['user'] = $user; 86212ca6034SMichael Klier $user_data['mail'] = $mail; 86312ca6034SMichael Klier $avatar = $this->avatar->getXHTML($user_data, $name, 'left'); 864c3413364SGerrit Uitslag if ($avatar) { 865c3413364SGerrit Uitslag $head .= $avatar; 866c3413364SGerrit Uitslag } 867f0fda08aSwikidesign } 868f0fda08aSwikidesign 8696046f25cSwikidesign if ($this->getConf('linkemail') && $mail) { 8700ff5ab97SMichael Klier $head .= $this->email($mail, $showname, 'email fn'); 8716046f25cSwikidesign } elseif ($url) { 872c3413364SGerrit Uitslag $head .= $this->external_link($this->checkURL($url), $showname, 'urlextern url fn'); 873f0fda08aSwikidesign } else { 8740ff5ab97SMichael Klier $head .= '<span class="fn">' . $showname . '</span>'; 875f0fda08aSwikidesign } 8766f1f13d6SMatthias Schulte 8774cded5e1SGerrit Uitslag if ($address) { 8784cded5e1SGerrit Uitslag $head .= ', <span class="adr">' . $address . '</span>'; 8794cded5e1SGerrit Uitslag } 8804a0a1bd2Swikidesign $head .= '</span>, ' . 881f014bc86SMichael Klier '<abbr class="published" title="' . strftime('%Y-%m-%dT%H:%M:%SZ', $created) . '">' . 8826f1f13d6SMatthias Schulte dformat($created, $conf['dformat']) . '</abbr>'; 8839c0e06f3SMoisés Braga Ribeiro if ($modified) { 8849c0e06f3SMoisés Braga Ribeiro $head .= ', <abbr class="updated" title="' . 8856f1f13d6SMatthias Schulte strftime('%Y-%m-%dT%H:%M:%SZ', $modified) . '">' . dformat($modified, $conf['dformat']) . 8869c0e06f3SMoisés Braga Ribeiro '</abbr>'; 8874cded5e1SGerrit Uitslag } 888f014bc86SMichael Klier ptln($head, 8); 8894a0a1bd2Swikidesign ptln('</div>', 6); // class="comment_head" 890f0fda08aSwikidesign 891f0fda08aSwikidesign // main comment content 8924a0a1bd2Swikidesign ptln('<div class="comment_body entry-content"' . 893c3413364SGerrit Uitslag ($this->useAvatar() ? $this->getWidthStyle() : '') . '>', 6); 89415cdad37Spierre.spring echo ($HIGH ? html_hilight($comment['xhtml'], $HIGH) : $comment['xhtml']) . DOKU_LF; 8954a0a1bd2Swikidesign ptln('</div>', 6); // class="comment_body" 896f0fda08aSwikidesign 897c3413364SGerrit Uitslag if ($isVisible) { 8981184c36aSwikidesign ptln('<div class="comment_buttons">', 6); 899f0fda08aSwikidesign 900f0fda08aSwikidesign // show reply button? 901c3413364SGerrit Uitslag if ($data['status'] == 1 && !$reply && $comment['show'] 902c3413364SGerrit Uitslag && ($this->getConf('allowguests') || $INPUT->server->has('REMOTE_USER')) 903c3413364SGerrit Uitslag && $this->getConf('usethreading') 9044cded5e1SGerrit Uitslag ) { 905c3413364SGerrit Uitslag $this->showButton($cid, $this->getLang('btn_reply'), 'reply', true); 9064cded5e1SGerrit Uitslag } 907f0fda08aSwikidesign 9081184c36aSwikidesign // show edit, show/hide and delete button? 909c3413364SGerrit Uitslag if (($user == $INPUT->server->str('REMOTE_USER') && $user != '') || $this->helper->isDiscussionMod()) { 910c3413364SGerrit Uitslag $this->showButton($cid, $lang['btn_secedit'], 'edit', true); 9111184c36aSwikidesign $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show')); 912c3413364SGerrit Uitslag $this->showButton($cid, $label, 'toogle'); 913c3413364SGerrit Uitslag $this->showButton($cid, $lang['btn_delete'], 'delete'); 914f0fda08aSwikidesign } 9151184c36aSwikidesign ptln('</div>', 6); // class="comment_buttons" 9161184c36aSwikidesign } 9171184c36aSwikidesign ptln('</div>', 4); // class="hentry" 918f0fda08aSwikidesign } 919f0fda08aSwikidesign 920de7e6f00SGerrit Uitslag /** 921c3413364SGerrit Uitslag * If requested by user, show comment form to write a reply 922c3413364SGerrit Uitslag * 923c3413364SGerrit Uitslag * @param string $cid current comment id 924c3413364SGerrit Uitslag * @param string $reply comment id on which the user requested a reply 925de7e6f00SGerrit Uitslag */ 926c3413364SGerrit Uitslag protected function showReplyForm($cid, $reply) 927f1c6610eSpierre.spring { 92831aab30eSGina Haeussge if ($this->getConf('usethreading') && $reply == $cid) { 9294a0a1bd2Swikidesign ptln('<div class="comment_replies">', 4); 930c3413364SGerrit Uitslag $this->showCommentForm('', 'add', $cid); 9314a0a1bd2Swikidesign ptln('</div>', 4); // class="comment_replies" 932f0fda08aSwikidesign } 933f0fda08aSwikidesign } 934f0fda08aSwikidesign 935de7e6f00SGerrit Uitslag /** 936c3413364SGerrit Uitslag * 937c3413364SGerrit Uitslag * 938c3413364SGerrit Uitslag * @param string $cid comment id 939c3413364SGerrit Uitslag * @param array $data array with all comments by reference 940de7e6f00SGerrit Uitslag * @param string $reply 941c3413364SGerrit Uitslag * @param bool $isVisible 942de7e6f00SGerrit Uitslag */ 943c3413364SGerrit Uitslag protected function showReplies($cid, &$data, $reply, &$isVisible) 944f1c6610eSpierre.spring { 945f1c6610eSpierre.spring $comment = $data['comments'][$cid]; 946f1c6610eSpierre.spring if (!count($comment['replies'])) { 947f1c6610eSpierre.spring return; 948f1c6610eSpierre.spring } 949c3413364SGerrit Uitslag ptln('<div class="comment_replies"' . $this->getWidthStyle() . '>', 4); 950c3413364SGerrit Uitslag $isVisible = ($comment['show'] && $isVisible); 951f1c6610eSpierre.spring foreach ($comment['replies'] as $rid) { 952c3413364SGerrit Uitslag $this->showCommentWithReplies($rid, $data, $cid, $reply, $isVisible); 953f1c6610eSpierre.spring } 954f1c6610eSpierre.spring ptln('</div>', 4); 955f1c6610eSpierre.spring } 956f1c6610eSpierre.spring 957de7e6f00SGerrit Uitslag /** 958de7e6f00SGerrit Uitslag * Is an avatar displayed? 959de7e6f00SGerrit Uitslag * 960de7e6f00SGerrit Uitslag * @return bool 961de7e6f00SGerrit Uitslag */ 962c3413364SGerrit Uitslag protected function useAvatar() 963f1c6610eSpierre.spring { 964c3413364SGerrit Uitslag if (is_null($this->useAvatar)) { 965c3413364SGerrit Uitslag $this->useAvatar = $this->getConf('useavatar') 966c3413364SGerrit Uitslag && ($this->avatar = $this->loadHelper('avatar', false)); 967f1c6610eSpierre.spring } 968c3413364SGerrit Uitslag return $this->useAvatar; 969f1c6610eSpierre.spring } 970f1c6610eSpierre.spring 971de7e6f00SGerrit Uitslag /** 972de7e6f00SGerrit Uitslag * Calculate width of indent 973de7e6f00SGerrit Uitslag * 974de7e6f00SGerrit Uitslag * @return string 975de7e6f00SGerrit Uitslag */ 976283a3029SGerrit Uitslag protected function getWidthStyle() 977283a3029SGerrit Uitslag { 978f1c6610eSpierre.spring if (is_null($this->style)) { 979c3413364SGerrit Uitslag if ($this->useAvatar()) { 980f1c6610eSpierre.spring $this->style = ' style="margin-left: ' . ($this->avatar->getConf('size') + 14) . 'px;"'; 981f1c6610eSpierre.spring } else { 982f1c6610eSpierre.spring $this->style = ' style="margin-left: 20px;"'; 983f1c6610eSpierre.spring } 984f1c6610eSpierre.spring } 985f1c6610eSpierre.spring return $this->style; 986f1c6610eSpierre.spring } 987f1c6610eSpierre.spring 988f0fda08aSwikidesign /** 989c3413364SGerrit Uitslag * Show the button which toggles between show/hide of the entire discussion section 99046178401Slupo49 */ 991283a3029SGerrit Uitslag protected function showDiscussionToggleButton() 992283a3029SGerrit Uitslag { 99346178401Slupo49 ptln('<div id="toggle_button" class="toggle_button" style="text-align: right;">'); 994283a3029SGerrit Uitslag ptln('<input type="submit" id="discussion__btn_toggle_visibility" title="Toggle Visibiliy" class="button"' 995283a3029SGerrit Uitslag . 'value="' . $this->getLang('toggle_display') . '">'); 99646178401Slupo49 ptln('</div>'); 99746178401Slupo49 } 99846178401Slupo49 99946178401Slupo49 /** 1000f0fda08aSwikidesign * Outputs the comment form 1001f0fda08aSwikidesign */ 1002283a3029SGerrit Uitslag protected function showCommentForm($raw = '', $act = 'add', $cid = null) 1003283a3029SGerrit Uitslag { 1004c3413364SGerrit Uitslag global $lang, $conf, $ID, $INPUT; 1005f0fda08aSwikidesign 1006f0fda08aSwikidesign // not for unregistered users when guest comments aren't allowed 1007c3413364SGerrit Uitslag if (!$INPUT->server->has('REMOTE_USER') && !$this->getConf('allowguests')) { 10087c8e18ffSGina Haeussge ?> 10097c8e18ffSGina Haeussge <div class="comment_form"> 10107c8e18ffSGina Haeussge <?php echo $this->getLang('noguests'); ?> 10117c8e18ffSGina Haeussge </div> 10127c8e18ffSGina Haeussge <?php 1013de7e6f00SGerrit Uitslag return; 10147c8e18ffSGina Haeussge } 1015f0fda08aSwikidesign 1016c3413364SGerrit Uitslag // fill $raw with $INPUT->str('text') if it's empty (for failed CAPTCHA check) 1017c3413364SGerrit Uitslag if (!$raw && $INPUT->str('comment') == 'show') { 1018c3413364SGerrit Uitslag $raw = $INPUT->str('text'); 10194cded5e1SGerrit Uitslag } 1020f0fda08aSwikidesign ?> 10215ef1705fSiLoveiDo 1022f0fda08aSwikidesign <div class="comment_form"> 1023283a3029SGerrit Uitslag <form id="discussion__comment_form" method="post" action="<?php echo script() ?>" 1024283a3029SGerrit Uitslag accept-charset="<?php echo $lang['encoding'] ?>"> 1025f0fda08aSwikidesign <div class="no"> 1026f0fda08aSwikidesign <input type="hidden" name="id" value="<?php echo $ID ?>"/> 102761437513Swikidesign <input type="hidden" name="do" value="show"/> 1028f0fda08aSwikidesign <input type="hidden" name="comment" value="<?php echo $act ?>"/> 1029530693fbSMichael Klier <?php 1030f0fda08aSwikidesign // for adding a comment 1031f0fda08aSwikidesign if ($act == 'add') { 1032f0fda08aSwikidesign ?> 1033f0fda08aSwikidesign <input type="hidden" name="reply" value="<?php echo $cid ?>"/> 1034f0fda08aSwikidesign <?php 103520c152acSMichael Klier // for guest/adminimport: show name, e-mail and subscribe to comments fields 1036c3413364SGerrit Uitslag if (!$INPUT->server->has('REMOTE_USER') or ($this->getConf('adminimport') && $this->helper->isDiscussionMod())) { 1037f0fda08aSwikidesign ?> 1038f0fda08aSwikidesign <input type="hidden" name="user" value="<?php echo clientIP() ?>"/> 1039f0fda08aSwikidesign <div class="comment_name"> 1040f0fda08aSwikidesign <label class="block" for="discussion__comment_name"> 1041f0fda08aSwikidesign <span><?php echo $lang['fullname'] ?>:</span> 1042283a3029SGerrit Uitslag <input type="text" 1043283a3029SGerrit Uitslag class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('name'))) echo ' error' ?>" 1044283a3029SGerrit Uitslag name="name" id="discussion__comment_name" size="50" tabindex="1" 1045283a3029SGerrit Uitslag value="<?php echo hsc($INPUT->str('name')) ?>"/> 1046f0fda08aSwikidesign </label> 1047f0fda08aSwikidesign </div> 1048f0fda08aSwikidesign <div class="comment_mail"> 1049f0fda08aSwikidesign <label class="block" for="discussion__comment_mail"> 1050f0fda08aSwikidesign <span><?php echo $lang['email'] ?>:</span> 1051283a3029SGerrit Uitslag <input type="text" 1052283a3029SGerrit Uitslag class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('mail'))) echo ' error' ?>" 1053283a3029SGerrit Uitslag name="mail" id="discussion__comment_mail" size="50" tabindex="2" 1054283a3029SGerrit Uitslag value="<?php echo hsc($INPUT->str('mail')) ?>"/> 1055f0fda08aSwikidesign </label> 1056f0fda08aSwikidesign </div> 1057f0fda08aSwikidesign <?php 1058f0fda08aSwikidesign } 1059f0fda08aSwikidesign 1060f0fda08aSwikidesign // allow entering an URL 1061f0fda08aSwikidesign if ($this->getConf('urlfield')) { 1062f0fda08aSwikidesign ?> 1063f0fda08aSwikidesign <div class="comment_url"> 1064f0fda08aSwikidesign <label class="block" for="discussion__comment_url"> 1065f0fda08aSwikidesign <span><?php echo $this->getLang('url') ?>:</span> 1066283a3029SGerrit Uitslag <input type="text" class="edit" name="url" id="discussion__comment_url" size="50" 1067283a3029SGerrit Uitslag tabindex="3" value="<?php echo hsc($INPUT->str('url')) ?>"/> 1068f0fda08aSwikidesign </label> 1069f0fda08aSwikidesign </div> 1070f0fda08aSwikidesign <?php 1071f0fda08aSwikidesign } 1072f0fda08aSwikidesign 1073f0fda08aSwikidesign // allow entering an address 1074f0fda08aSwikidesign if ($this->getConf('addressfield')) { 1075f0fda08aSwikidesign ?> 1076f0fda08aSwikidesign <div class="comment_address"> 1077f0fda08aSwikidesign <label class="block" for="discussion__comment_address"> 1078f0fda08aSwikidesign <span><?php echo $this->getLang('address') ?>:</span> 1079283a3029SGerrit Uitslag <input type="text" class="edit" name="address" id="discussion__comment_address" 1080283a3029SGerrit Uitslag size="50" tabindex="4" value="<?php echo hsc($INPUT->str('address')) ?>"/> 1081f0fda08aSwikidesign </label> 1082f0fda08aSwikidesign </div> 1083f0fda08aSwikidesign <?php 1084f0fda08aSwikidesign } 1085f0fda08aSwikidesign 1086f0fda08aSwikidesign // allow setting the comment date 1087e6b2f142Slupo49 if ($this->getConf('adminimport') && ($this->helper->isDiscussionMod())) { 1088f0fda08aSwikidesign ?> 1089f0fda08aSwikidesign <div class="comment_date"> 1090f0fda08aSwikidesign <label class="block" for="discussion__comment_date"> 1091f0fda08aSwikidesign <span><?php echo $this->getLang('date') ?>:</span> 1092283a3029SGerrit Uitslag <input type="text" class="edit" name="date" id="discussion__comment_date" 1093283a3029SGerrit Uitslag size="50"/> 1094f0fda08aSwikidesign </label> 1095f0fda08aSwikidesign </div> 1096f0fda08aSwikidesign <?php 1097f0fda08aSwikidesign } 1098f0fda08aSwikidesign 1099f0fda08aSwikidesign // for saving a comment 1100f0fda08aSwikidesign } else { 1101f0fda08aSwikidesign ?> 1102f0fda08aSwikidesign <input type="hidden" name="cid" value="<?php echo $cid ?>"/> 1103f0fda08aSwikidesign <?php 1104f0fda08aSwikidesign } 1105f0fda08aSwikidesign ?> 1106f0fda08aSwikidesign <div class="comment_text"> 1107283a3029SGerrit Uitslag <?php echo $this->getLang('entercomment'); 1108283a3029SGerrit Uitslag echo($this->getConf('wikisyntaxok') ? "" : ":"); 110943ade360Slupo49 if ($this->getConf('wikisyntaxok')) echo '. ' . $this->getLang('wikisyntax') . ':'; ?> 111043ade360Slupo49 111143ade360Slupo49 <!-- Fix for disable the toolbar when wikisyntaxok is set to false. See discussion's script.jss --> 111243ade360Slupo49 <?php if ($this->getConf('wikisyntaxok')) { ?> 111364901b8eSGerrit Uitslag <div id="discussion__comment_toolbar" class="toolbar group"> 111443ade360Slupo49 <?php } else { ?> 111543ade360Slupo49 <div id="discussion__comment_toolbar_disabled"> 111643ade360Slupo49 <?php } ?> 11171de52da1SMichael Klier </div> 1118283a3029SGerrit Uitslag <textarea 1119283a3029SGerrit Uitslag class="edit<?php if ($INPUT->str('comment') == 'add' && empty($INPUT->str('text'))) echo ' error' ?>" 1120283a3029SGerrit Uitslag name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php 112137e3c825SMichael Klier if ($raw) { 112237e3c825SMichael Klier echo formText($raw); 112337e3c825SMichael Klier } else { 1124c3413364SGerrit Uitslag echo hsc($INPUT->str('text')); 112537e3c825SMichael Klier } 112637e3c825SMichael Klier ?></textarea> 1127f0fda08aSwikidesign </div> 11280c613822SMichael Hamann 11290c613822SMichael Hamann <?php 11300c613822SMichael Hamann /** @var helper_plugin_captcha $captcha */ 11310c613822SMichael Hamann $captcha = $this->loadHelper('captcha', false); 11320c613822SMichael Hamann if ($captcha && $captcha->isEnabled()) { 11330c613822SMichael Hamann echo $captcha->getHTML(); 11340c613822SMichael Hamann } 11350c613822SMichael Hamann 11360c613822SMichael Hamann /** @var helper_plugin_recaptcha $recaptcha */ 11370c613822SMichael Hamann $recaptcha = $this->loadHelper('recaptcha', false); 11380c613822SMichael Hamann if ($recaptcha && $recaptcha->isEnabled()) { 11390c613822SMichael Hamann echo $recaptcha->getHTML(); 11400c613822SMichael Hamann } 1141e7c760b3Swikidesign ?> 11420c613822SMichael Hamann 1143283a3029SGerrit Uitslag <input class="button comment_submit" id="discussion__btn_submit" type="submit" name="submit" 1144283a3029SGerrit Uitslag accesskey="s" value="<?php echo $lang['btn_save'] ?>" 1145283a3029SGerrit Uitslag title="<?php echo $lang['btn_save'] ?> [S]" tabindex="7"/> 1146283a3029SGerrit Uitslag <input class="button comment_preview_button" id="discussion__btn_preview" type="button" 1147283a3029SGerrit Uitslag name="preview" accesskey="p" value="<?php echo $lang['btn_preview'] ?>" 1148283a3029SGerrit Uitslag title="<?php echo $lang['btn_preview'] ?> [P]"/> 11493011fb8bSMichael Klier 1150283a3029SGerrit Uitslag <?php if ((!$INPUT->server->has('REMOTE_USER') 1151283a3029SGerrit Uitslag || $INPUT->server->has('REMOTE_USER') && !$conf['subscribers']) 1152283a3029SGerrit Uitslag && $this->getConf('subscribe')) { ?> 11533011fb8bSMichael Klier <div class="comment_subscribe"> 1154283a3029SGerrit Uitslag <input type="checkbox" id="discussion__comment_subscribe" name="subscribe" 1155283a3029SGerrit Uitslag tabindex="6"/> 11563011fb8bSMichael Klier <label class="block" for="discussion__comment_subscribe"> 11573011fb8bSMichael Klier <span><?php echo $this->getLang('subscribe') ?></span> 11583011fb8bSMichael Klier </label> 11593011fb8bSMichael Klier </div> 11603011fb8bSMichael Klier <?php } ?> 11613011fb8bSMichael Klier 11623011fb8bSMichael Klier <div class="clearer"></div> 1163ed4846efSMichael Klier <div id="discussion__comment_preview"> </div> 1164f0fda08aSwikidesign </div> 1165f0fda08aSwikidesign </form> 1166f0fda08aSwikidesign </div> 1167f0fda08aSwikidesign <?php 1168f0fda08aSwikidesign } 1169f0fda08aSwikidesign 1170f0fda08aSwikidesign /** 1171c3413364SGerrit Uitslag * Action button below a comment 1172de7e6f00SGerrit Uitslag * 1173c3413364SGerrit Uitslag * @param string $cid comment id 1174c3413364SGerrit Uitslag * @param string $label translated label 1175c3413364SGerrit Uitslag * @param string $act action 1176c3413364SGerrit Uitslag * @param bool $jump whether to scroll to the commentform 1177f0fda08aSwikidesign */ 1178283a3029SGerrit Uitslag protected function showButton($cid, $label, $act, $jump = false) 1179283a3029SGerrit Uitslag { 1180f0fda08aSwikidesign global $ID; 11815ef1705fSiLoveiDo 11821e46d176Swikidesign $anchor = ($jump ? '#discussion__comment_form' : ''); 1183f0fda08aSwikidesign 1184f0fda08aSwikidesign ?> 11856d1f4f20SGina Haeussge <form class="button discussion__<?php echo $act ?>" method="get" action="<?php echo script() . $anchor ?>"> 1186f0fda08aSwikidesign <div class="no"> 1187f0fda08aSwikidesign <input type="hidden" name="id" value="<?php echo $ID ?>"/> 118861437513Swikidesign <input type="hidden" name="do" value="show"/> 1189f0fda08aSwikidesign <input type="hidden" name="comment" value="<?php echo $act ?>"/> 1190f0fda08aSwikidesign <input type="hidden" name="cid" value="<?php echo $cid ?>"/> 1191f0fda08aSwikidesign <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>"/> 1192f0fda08aSwikidesign </div> 1193f0fda08aSwikidesign </form> 1194f0fda08aSwikidesign <?php 1195f0fda08aSwikidesign } 1196f0fda08aSwikidesign 1197f0fda08aSwikidesign /** 1198f0fda08aSwikidesign * Adds an entry to the comments changelog 1199f0fda08aSwikidesign * 1200de7e6f00SGerrit Uitslag * @param int $date 1201de7e6f00SGerrit Uitslag * @param string $id page id 1202c3413364SGerrit Uitslag * @param string $type create/edit/delete/show/hide comment 'cc', 'ec', 'dc', 'sc', 'hc' 1203de7e6f00SGerrit Uitslag * @param string $summary 1204de7e6f00SGerrit Uitslag * @param string $extra 1205283a3029SGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 1206283a3029SGerrit Uitslag * 1207283a3029SGerrit Uitslag * @author Esther Brunner <wikidesign@gmail.com> 1208f0fda08aSwikidesign */ 1209283a3029SGerrit Uitslag protected function addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '') 1210283a3029SGerrit Uitslag { 1211c3413364SGerrit Uitslag global $conf, $INPUT; 1212f0fda08aSwikidesign 1213f0fda08aSwikidesign $changelog = $conf['metadir'] . '/_comments.changes'; 1214f0fda08aSwikidesign 12154cded5e1SGerrit Uitslag //use current time if none supplied 12164cded5e1SGerrit Uitslag if (!$date) { 12174cded5e1SGerrit Uitslag $date = time(); 12184cded5e1SGerrit Uitslag } 1219c3413364SGerrit Uitslag $remote = $INPUT->server->str('REMOTE_ADDR'); 1220c3413364SGerrit Uitslag $user = $INPUT->server->str('REMOTE_USER'); 1221f0fda08aSwikidesign 1222c3413364SGerrit Uitslag $strip = ["\t", "\n"]; 1223c3413364SGerrit Uitslag $logline = [ 1224f0fda08aSwikidesign 'date' => $date, 1225f0fda08aSwikidesign 'ip' => $remote, 1226f0fda08aSwikidesign 'type' => str_replace($strip, '', $type), 1227f0fda08aSwikidesign 'id' => $id, 1228f0fda08aSwikidesign 'user' => $user, 1229f0fda08aSwikidesign 'sum' => str_replace($strip, '', $summary), 1230f0fda08aSwikidesign 'extra' => str_replace($strip, '', $extra) 1231c3413364SGerrit Uitslag ]; 1232f0fda08aSwikidesign 1233f0fda08aSwikidesign // add changelog line 1234f0fda08aSwikidesign $logline = implode("\t", $logline) . "\n"; 1235f0fda08aSwikidesign io_saveFile($changelog, $logline, true); //global changelog cache 1236c3413364SGerrit Uitslag $this->trimRecentCommentsLog($changelog); 123777a22ba2Swikidesign 123877a22ba2Swikidesign // tell the indexer to re-index the page 123977a22ba2Swikidesign @unlink(metaFN($id, '.indexed')); 1240f0fda08aSwikidesign } 1241f0fda08aSwikidesign 1242f0fda08aSwikidesign /** 1243f0fda08aSwikidesign * Trims the recent comments cache to the last $conf['changes_days'] recent 1244f0fda08aSwikidesign * changes or $conf['recent'] items, which ever is larger. 1245f0fda08aSwikidesign * The trimming is only done once a day. 1246f0fda08aSwikidesign * 1247de7e6f00SGerrit Uitslag * @param string $changelog file path 1248de7e6f00SGerrit Uitslag * @return bool 1249283a3029SGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 1250283a3029SGerrit Uitslag * 1251f0fda08aSwikidesign */ 1252283a3029SGerrit Uitslag protected function trimRecentCommentsLog($changelog) 1253283a3029SGerrit Uitslag { 1254f0fda08aSwikidesign global $conf; 1255f0fda08aSwikidesign 1256283a3029SGerrit Uitslag if (@file_exists($changelog) 1257283a3029SGerrit Uitslag && (filectime($changelog) + 86400) < time() 1258283a3029SGerrit Uitslag && !@file_exists($changelog . '_tmp') 12594cded5e1SGerrit Uitslag ) { 1260f0fda08aSwikidesign 1261f0fda08aSwikidesign io_lock($changelog); 1262f0fda08aSwikidesign $lines = file($changelog); 1263f0fda08aSwikidesign if (count($lines) < $conf['recent']) { 1264f0fda08aSwikidesign // nothing to trim 1265f0fda08aSwikidesign io_unlock($changelog); 1266f0fda08aSwikidesign return true; 1267f0fda08aSwikidesign } 1268f0fda08aSwikidesign 1269283a3029SGerrit Uitslag // presave tmp as 2nd lock 1270283a3029SGerrit Uitslag io_saveFile($changelog . '_tmp', ''); 1271f0fda08aSwikidesign $trim_time = time() - $conf['recent_days'] * 86400; 1272c3413364SGerrit Uitslag $out_lines = []; 1273f0fda08aSwikidesign 1274e49085a2SMichael Klier $num = count($lines); 1275e49085a2SMichael Klier for ($i = 0; $i < $num; $i++) { 1276f0fda08aSwikidesign $log = parseChangelogLine($lines[$i]); 1277f0fda08aSwikidesign if ($log === false) continue; // discard junk 1278f0fda08aSwikidesign if ($log['date'] < $trim_time) { 1279f0fda08aSwikidesign $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) 1280f0fda08aSwikidesign } else { 1281f0fda08aSwikidesign $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines 1282f0fda08aSwikidesign } 1283f0fda08aSwikidesign } 1284f0fda08aSwikidesign 1285f0fda08aSwikidesign // sort the final result, it shouldn't be necessary, 1286f0fda08aSwikidesign // however the extra robustness in making the changelog cache self-correcting is worth it 1287f0fda08aSwikidesign ksort($out_lines); 1288f0fda08aSwikidesign $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 1289f0fda08aSwikidesign if ($extra > 0) { 1290f0fda08aSwikidesign ksort($old_lines); 1291f0fda08aSwikidesign $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); 1292f0fda08aSwikidesign } 1293f0fda08aSwikidesign 1294f0fda08aSwikidesign // save trimmed changelog 1295f0fda08aSwikidesign io_saveFile($changelog . '_tmp', implode('', $out_lines)); 1296f0fda08aSwikidesign @unlink($changelog); 1297f0fda08aSwikidesign if (!rename($changelog . '_tmp', $changelog)) { 1298f0fda08aSwikidesign // rename failed so try another way... 1299f0fda08aSwikidesign io_unlock($changelog); 1300f0fda08aSwikidesign io_saveFile($changelog, implode('', $out_lines)); 1301f0fda08aSwikidesign @unlink($changelog . '_tmp'); 1302f0fda08aSwikidesign } else { 1303f0fda08aSwikidesign io_unlock($changelog); 1304f0fda08aSwikidesign } 1305f0fda08aSwikidesign return true; 1306f0fda08aSwikidesign } 1307de7e6f00SGerrit Uitslag return true; 1308f0fda08aSwikidesign } 1309f0fda08aSwikidesign 1310f0fda08aSwikidesign /** 1311f0fda08aSwikidesign * Sends a notify mail on new comment 1312f0fda08aSwikidesign * 1313f0fda08aSwikidesign * @param array $comment data array of the new comment 1314c3413364SGerrit Uitslag * @param array $subscribers data of the subscribers by reference 1315f0fda08aSwikidesign * 1316f0fda08aSwikidesign * @author Andreas Gohr <andi@splitbrain.org> 1317f0fda08aSwikidesign * @author Esther Brunner <wikidesign@gmail.com> 1318f0fda08aSwikidesign */ 1319283a3029SGerrit Uitslag protected function notify($comment, &$subscribers) 1320283a3029SGerrit Uitslag { 1321c3413364SGerrit Uitslag global $conf, $ID, $INPUT, $auth; 1322f0fda08aSwikidesign 13239881d835SMichael Klier $notify_text = io_readfile($this->localfn('subscribermail')); 13249881d835SMichael Klier $confirm_text = io_readfile($this->localfn('confirmsubscribe')); 13259881d835SMichael Klier $subject_notify = '[' . $conf['title'] . '] ' . $this->getLang('mail_newcomment'); 13269881d835SMichael Klier $subject_subscribe = '[' . $conf['title'] . '] ' . $this->getLang('subscribe'); 1327f0fda08aSwikidesign 1328451c1100SMichael Hamann $mailer = new Mailer(); 1329c3413364SGerrit Uitslag if (!$INPUT->server->has('REMOTE_USER')) { 1330451c1100SMichael Hamann $mailer->from($conf['mailfromnobody']); 1331f4a5ed1cSMatthias Schulte } 1332f4a5ed1cSMatthias Schulte 1333c3413364SGerrit Uitslag $replace = [ 13343c4953e9SMichael Hamann 'PAGE' => $ID, 13353c4953e9SMichael Hamann 'TITLE' => $conf['title'], 13363c4953e9SMichael Hamann 'DATE' => dformat($comment['date']['created'], $conf['dformat']), 13373c4953e9SMichael Hamann 'NAME' => $comment['user']['name'], 13383c4953e9SMichael Hamann 'TEXT' => $comment['raw'], 13393c4953e9SMichael Hamann 'COMMENTURL' => wl($ID, '', true) . '#comment_' . $comment['cid'], 134006644a74SMichael Hamann 'UNSUBSCRIBE' => wl($ID, 'do=subscribe', true, '&'), 13413c4953e9SMichael Hamann 'DOKUWIKIURL' => DOKU_URL 1342c3413364SGerrit Uitslag ]; 1343451c1100SMichael Hamann 1344c3413364SGerrit Uitslag $confirm_replace = [ 13453c4953e9SMichael Hamann 'PAGE' => $ID, 13463c4953e9SMichael Hamann 'TITLE' => $conf['title'], 13473c4953e9SMichael Hamann 'DOKUWIKIURL' => DOKU_URL 1348c3413364SGerrit Uitslag ]; 1349451c1100SMichael Hamann 1350451c1100SMichael Hamann 1351451c1100SMichael Hamann $mailer->subject($subject_notify); 1352451c1100SMichael Hamann $mailer->setBody($notify_text, $replace); 1353451c1100SMichael Hamann 1354f4a5ed1cSMatthias Schulte // send mail to notify address 1355f4a5ed1cSMatthias Schulte if ($conf['notify']) { 1356451c1100SMichael Hamann $mailer->bcc($conf['notify']); 1357451c1100SMichael Hamann $mailer->send(); 1358f4a5ed1cSMatthias Schulte } 1359f4a5ed1cSMatthias Schulte 1360ca785d71SMichael Hamann // send email to moderators 1361ca785d71SMichael Hamann if ($this->getConf('moderatorsnotify')) { 1362c3413364SGerrit Uitslag $moderatorgrpsString = trim($this->getConf('moderatorgroups')); 1363c3413364SGerrit Uitslag if (!empty($moderatorgrpsString)) { 1364ca785d71SMichael Hamann // create a clean mods list 1365c3413364SGerrit Uitslag $moderatorgroups = explode(',', $moderatorgrpsString); 1366c3413364SGerrit Uitslag $moderatorgroups = array_map('trim', $moderatorgroups); 1367c3413364SGerrit Uitslag $moderatorgroups = array_unique($moderatorgroups); 1368c3413364SGerrit Uitslag $moderatorgroups = array_filter($moderatorgroups); 1369ca785d71SMichael Hamann // search for moderators users 1370c3413364SGerrit Uitslag foreach ($moderatorgroups as $moderatorgroup) { 1371c3413364SGerrit Uitslag if (!$auth->isCaseSensitive()) { 1372c3413364SGerrit Uitslag $moderatorgroup = PhpString::strtolower($moderatorgroup); 1373c3413364SGerrit Uitslag } 1374ca785d71SMichael Hamann // create a clean mailing list 1375c3413364SGerrit Uitslag $bccs = []; 1376c3413364SGerrit Uitslag if ($moderatorgroup[0] == '@') { 1377c3413364SGerrit Uitslag foreach ($auth->retrieveUsers(0, 0, ['grps' => $auth->cleanGroup(substr($moderatorgroup, 1))]) as $user) { 1378ca785d71SMichael Hamann if (!empty($user['mail'])) { 1379c3413364SGerrit Uitslag $bccs[] = $user['mail']; 1380ca785d71SMichael Hamann } 1381ca785d71SMichael Hamann } 1382ca785d71SMichael Hamann } else { 1383c3413364SGerrit Uitslag //it is an user 1384c3413364SGerrit Uitslag $userdata = $auth->getUserData($auth->cleanUser($moderatorgroup)); 1385ca785d71SMichael Hamann if (!empty($userdata['mail'])) { 1386c3413364SGerrit Uitslag $bccs[] = $userdata['mail']; 1387ca785d71SMichael Hamann } 1388ca785d71SMichael Hamann } 1389c3413364SGerrit Uitslag $bccs = array_unique($bccs); 1390ca785d71SMichael Hamann // notify the users 1391c3413364SGerrit Uitslag $mailer->bcc(implode(',', $bccs)); 1392ca785d71SMichael Hamann $mailer->send(); 1393ca785d71SMichael Hamann } 1394ca785d71SMichael Hamann } 1395ca785d71SMichael Hamann } 1396ca785d71SMichael Hamann 1397f4a5ed1cSMatthias Schulte // notify page subscribers 1398451c1100SMichael Hamann if (actionOK('subscribe')) { 1399c3413364SGerrit Uitslag $data = ['id' => $ID, 'addresslist' => '', 'self' => false]; 1400c3413364SGerrit Uitslag //FIXME default callback, needed to mentioned it again? 1401c3413364SGerrit Uitslag Event::createAndTrigger( 1402451c1100SMichael Hamann 'COMMON_NOTIFY_ADDRESSLIST', $data, 1403c3413364SGerrit Uitslag [new SubscriberManager(), 'notifyAddresses'] 1404451c1100SMichael Hamann ); 1405c3413364SGerrit Uitslag 1406451c1100SMichael Hamann $to = $data['addresslist']; 1407451c1100SMichael Hamann if (!empty($to)) { 1408451c1100SMichael Hamann $mailer->bcc($to); 1409451c1100SMichael Hamann $mailer->send(); 1410451c1100SMichael Hamann } 14113011fb8bSMichael Klier } 1412f0fda08aSwikidesign 14133011fb8bSMichael Klier // notify comment subscribers 14143011fb8bSMichael Klier if (!empty($subscribers)) { 14153011fb8bSMichael Klier 14169881d835SMichael Klier foreach ($subscribers as $mail => $data) { 1417451c1100SMichael Hamann $mailer->bcc($mail); 14189881d835SMichael Klier if ($data['active']) { 14193c4953e9SMichael Hamann $replace['UNSUBSCRIBE'] = wl($ID, 'do=discussion_unsubscribe&hash=' . $data['hash'], true, '&'); 14203011fb8bSMichael Klier 1421451c1100SMichael Hamann $mailer->subject($subject_notify); 1422451c1100SMichael Hamann $mailer->setBody($notify_text, $replace); 1423451c1100SMichael Hamann $mailer->send(); 1424c3413364SGerrit Uitslag } elseif (!$data['confirmsent']) { 14253c4953e9SMichael Hamann $confirm_replace['SUBSCRIBE'] = wl($ID, 'do=discussion_confirmsubscribe&hash=' . $data['hash'], true, '&'); 14269881d835SMichael Klier 1427451c1100SMichael Hamann $mailer->subject($subject_subscribe); 1428451c1100SMichael Hamann $mailer->setBody($confirm_text, $confirm_replace); 1429451c1100SMichael Hamann $mailer->send(); 14309881d835SMichael Klier $subscribers[$mail]['confirmsent'] = true; 14319881d835SMichael Klier } 14323011fb8bSMichael Klier } 14333011fb8bSMichael Klier } 1434f0fda08aSwikidesign } 1435f0fda08aSwikidesign 1436f0fda08aSwikidesign /** 1437f0fda08aSwikidesign * Counts the number of visible comments 1438de7e6f00SGerrit Uitslag * 1439c3413364SGerrit Uitslag * @param array $data array with all comments 1440de7e6f00SGerrit Uitslag * @return int 1441f0fda08aSwikidesign */ 1442283a3029SGerrit Uitslag protected function countVisibleComments($data) 1443283a3029SGerrit Uitslag { 1444f0fda08aSwikidesign $number = 0; 1445de7e6f00SGerrit Uitslag foreach ($data['comments'] as $comment) { 1446f0fda08aSwikidesign if ($comment['parent']) continue; 1447f0fda08aSwikidesign if (!$comment['show']) continue; 1448c3413364SGerrit Uitslag 1449f0fda08aSwikidesign $number++; 1450f0fda08aSwikidesign $rids = $comment['replies']; 14514cded5e1SGerrit Uitslag if (count($rids)) { 1452c3413364SGerrit Uitslag $number = $number + $this->countVisibleReplies($data, $rids); 14534cded5e1SGerrit Uitslag } 1454f0fda08aSwikidesign } 1455f0fda08aSwikidesign return $number; 1456f0fda08aSwikidesign } 1457f0fda08aSwikidesign 1458de7e6f00SGerrit Uitslag /** 1459c3413364SGerrit Uitslag * Count visible replies on the comments 1460c3413364SGerrit Uitslag * 1461de7e6f00SGerrit Uitslag * @param array $data 1462de7e6f00SGerrit Uitslag * @param array $rids 1463c3413364SGerrit Uitslag * @return int counted replies 1464de7e6f00SGerrit Uitslag */ 1465283a3029SGerrit Uitslag protected function countVisibleReplies(&$data, $rids) 1466283a3029SGerrit Uitslag { 1467f0fda08aSwikidesign $number = 0; 1468f0fda08aSwikidesign foreach ($rids as $rid) { 14692ee3dca3Swikidesign if (!isset($data['comments'][$rid])) continue; // reply was removed 1470f0fda08aSwikidesign if (!$data['comments'][$rid]['show']) continue; 1471c3413364SGerrit Uitslag 1472f0fda08aSwikidesign $number++; 1473f0fda08aSwikidesign $rids = $data['comments'][$rid]['replies']; 14744cded5e1SGerrit Uitslag if (count($rids)) { 1475c3413364SGerrit Uitslag $number = $number + $this->countVisibleReplies($data, $rids); 14764cded5e1SGerrit Uitslag } 1477f0fda08aSwikidesign } 1478f0fda08aSwikidesign return $number; 1479f0fda08aSwikidesign } 1480f0fda08aSwikidesign 1481f0fda08aSwikidesign /** 1482c3413364SGerrit Uitslag * Renders the raw comment (wiki)text to html 1483de7e6f00SGerrit Uitslag * 1484c3413364SGerrit Uitslag * @param string $raw comment text 1485de7e6f00SGerrit Uitslag * @return null|string 1486f0fda08aSwikidesign */ 1487283a3029SGerrit Uitslag protected function renderComment($raw) 1488283a3029SGerrit Uitslag { 1489f0fda08aSwikidesign if ($this->getConf('wikisyntaxok')) { 1490efccf6b0SJeffrey Bergamini // Note the warning for render_text: 1491efccf6b0SJeffrey Bergamini // "very ineffecient for small pieces of data - try not to use" 1492efccf6b0SJeffrey Bergamini // in dokuwiki/inc/plugin.php 1493efccf6b0SJeffrey Bergamini $xhtml = $this->render_text($raw); 1494f0fda08aSwikidesign } else { // wiki syntax not allowed -> just encode special chars 149587bb4e97SMichael Klier $xhtml = hsc(trim($raw)); 149687bb4e97SMichael Klier $xhtml = str_replace("\n", '<br />', $xhtml); 1497f0fda08aSwikidesign } 1498f0fda08aSwikidesign return $xhtml; 1499f0fda08aSwikidesign } 1500f0fda08aSwikidesign 1501f0fda08aSwikidesign /** 1502479dd10fSwikidesign * Finds out whether there is a discussion section for the current page 1503de7e6f00SGerrit Uitslag * 1504de7e6f00SGerrit Uitslag * @param string $title 1505de7e6f00SGerrit Uitslag * @return bool 1506479dd10fSwikidesign */ 1507283a3029SGerrit Uitslag protected function hasDiscussion(&$title) 1508283a3029SGerrit Uitslag { 1509b2ac3b3bSwikidesign global $ID; 15104a0a1bd2Swikidesign 1511c3413364SGerrit Uitslag $file = metaFN($ID, '.comments'); 1512479dd10fSwikidesign 1513c3413364SGerrit Uitslag if (!@file_exists($file)) { 1514f0bcde18SGerrit Uitslag if ($this->isDiscussionEnabled()) { 15152b18adb9SMichael Klier return true; 15162b18adb9SMichael Klier } else { 15172b18adb9SMichael Klier return false; 15182b18adb9SMichael Klier } 1519479dd10fSwikidesign } 1520479dd10fSwikidesign 1521c3413364SGerrit Uitslag $comments = unserialize(io_readFile($file, false)); 1522479dd10fSwikidesign 15234cded5e1SGerrit Uitslag if ($comments['title']) { 15244cded5e1SGerrit Uitslag $title = hsc($comments['title']); 15254cded5e1SGerrit Uitslag } 1526479dd10fSwikidesign $num = $comments['number']; 1527c3413364SGerrit Uitslag if (!$comments['status'] || ($comments['status'] == 2 && $num == 0)) { 1528c3413364SGerrit Uitslag //disabled, or closed and no comments 1529c3413364SGerrit Uitslag return false; 1530c3413364SGerrit Uitslag } else { 1531c3413364SGerrit Uitslag return true; 1532c3413364SGerrit Uitslag } 1533479dd10fSwikidesign } 1534479dd10fSwikidesign 1535479dd10fSwikidesign /** 1536e7c760b3Swikidesign * Creates a new thread page 1537de7e6f00SGerrit Uitslag * 1538de7e6f00SGerrit Uitslag * @return string 1539e7c760b3Swikidesign */ 1540283a3029SGerrit Uitslag protected function newThread() 1541283a3029SGerrit Uitslag { 1542c3413364SGerrit Uitslag global $ID, $INFO, $INPUT; 1543f0fda08aSwikidesign 1544c3413364SGerrit Uitslag $ns = cleanID($INPUT->str('ns')); 1545c3413364SGerrit Uitslag $title = str_replace(':', '', $INPUT->str('title')); 15462e80cd5fSwikidesign $back = $ID; 15472e80cd5fSwikidesign $ID = ($ns ? $ns . ':' : '') . cleanID($title); 15482e80cd5fSwikidesign $INFO = pageinfo(); 1549f0fda08aSwikidesign 1550f0fda08aSwikidesign // check if we are allowed to create this file 15512e80cd5fSwikidesign if ($INFO['perm'] >= AUTH_CREATE) { 1552f0fda08aSwikidesign 1553f0fda08aSwikidesign //check if locked by anyone - if not lock for my self 15544cded5e1SGerrit Uitslag if ($INFO['locked']) { 15554cded5e1SGerrit Uitslag return 'locked'; 15564cded5e1SGerrit Uitslag } else { 15574cded5e1SGerrit Uitslag lock($ID); 15584cded5e1SGerrit Uitslag } 1559f0fda08aSwikidesign 1560f0fda08aSwikidesign // prepare the new thread file with default stuff 15612e80cd5fSwikidesign if (!@file_exists($INFO['filepath'])) { 1562f0fda08aSwikidesign global $TEXT; 1563f0fda08aSwikidesign 1564c3413364SGerrit Uitslag $TEXT = pageTemplate(($ns ? $ns . ':' : '') . $title); 15651433886fSwikidesign if (!$TEXT) { 1566c3413364SGerrit Uitslag $data = ['id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back]; 1567c3413364SGerrit Uitslag $TEXT = $this->pageTemplate($data); 15682e80cd5fSwikidesign } 15692e80cd5fSwikidesign return 'preview'; 1570f0fda08aSwikidesign } else { 15712e80cd5fSwikidesign return 'edit'; 1572f0fda08aSwikidesign } 1573f0fda08aSwikidesign } else { 15742e80cd5fSwikidesign return 'show'; 1575f0fda08aSwikidesign } 1576f0fda08aSwikidesign } 1577f0fda08aSwikidesign 1578e7c760b3Swikidesign /** 157961437513Swikidesign * Adapted version of pageTemplate() function 1580de7e6f00SGerrit Uitslag * 1581de7e6f00SGerrit Uitslag * @param array $data 1582de7e6f00SGerrit Uitslag * @return string 158361437513Swikidesign */ 1584283a3029SGerrit Uitslag protected function pageTemplate($data) 1585283a3029SGerrit Uitslag { 1586c3413364SGerrit Uitslag global $conf, $INFO, $INPUT; 158761437513Swikidesign 158861437513Swikidesign $id = $data['id']; 1589c3413364SGerrit Uitslag $user = $INPUT->server->str('REMOTE_USER'); 159061437513Swikidesign $tpl = io_readFile(DOKU_PLUGIN . 'discussion/_template.txt'); 159161437513Swikidesign 159261437513Swikidesign // standard replacements 1593c3413364SGerrit Uitslag $replace = [ 159461437513Swikidesign '@NS@' => $data['ns'], 159561437513Swikidesign '@PAGE@' => strtr(noNS($id), '_', ' '), 159661437513Swikidesign '@USER@' => $user, 159761437513Swikidesign '@NAME@' => $INFO['userinfo']['name'], 159861437513Swikidesign '@MAIL@' => $INFO['userinfo']['mail'], 1599d5530824SMichael Hamann '@DATE@' => dformat(time(), $conf['dformat']), 1600c3413364SGerrit Uitslag ]; 160161437513Swikidesign 160261437513Swikidesign // additional replacements 160361437513Swikidesign $replace['@BACK@'] = $data['back']; 160461437513Swikidesign $replace['@TITLE@'] = $data['title']; 160561437513Swikidesign 160661437513Swikidesign // avatar if useavatar and avatar plugin available 1607c3413364SGerrit Uitslag if ($this->getConf('useavatar') && !plugin_isdisabled('avatar')) { 160861437513Swikidesign $replace['@AVATAR@'] = '{{avatar>' . $user . ' }} '; 160961437513Swikidesign } else { 161061437513Swikidesign $replace['@AVATAR@'] = ''; 161161437513Swikidesign } 161261437513Swikidesign 161361437513Swikidesign // tag if tag plugin is available 1614c3413364SGerrit Uitslag if (!plugin_isdisabled('tag')) { 161561437513Swikidesign $replace['@TAG@'] = "\n\n{{tag>}}"; 161661437513Swikidesign } else { 161761437513Swikidesign $replace['@TAG@'] = ''; 161861437513Swikidesign } 161961437513Swikidesign 1620c3413364SGerrit Uitslag // perform the replacements in tpl 1621c3413364SGerrit Uitslag return str_replace(array_keys($replace), array_values($replace), $tpl); 162261437513Swikidesign } 162361437513Swikidesign 162461437513Swikidesign /** 1625e7c760b3Swikidesign * Checks if the CAPTCHA string submitted is valid 1626e7c760b3Swikidesign */ 1627283a3029SGerrit Uitslag protected function captchaCheck() 1628283a3029SGerrit Uitslag { 1629c3413364SGerrit Uitslag global $INPUT; 163096bc68a7SMichael Hamann /** @var helper_plugin_captcha $captcha */ 1631c3413364SGerrit Uitslag if (!$captcha = $this->loadHelper('captcha', false)) { 1632c3413364SGerrit Uitslag // CAPTCHA is disabled or not available 1633c3413364SGerrit Uitslag return; 1634c3413364SGerrit Uitslag } 1635e7c760b3Swikidesign 1636d578a059SMichael Hamann if ($captcha->isEnabled() && !$captcha->check()) { 1637c3413364SGerrit Uitslag if ($INPUT->str('comment') == 'save') { 1638c3413364SGerrit Uitslag $INPUT->set('comment', 'edit'); 1639c3413364SGerrit Uitslag } elseif ($INPUT->str('comment') == 'add') { 1640c3413364SGerrit Uitslag $INPUT->set('comment', 'show'); 16414cded5e1SGerrit Uitslag } 1642e7c760b3Swikidesign } 1643e7c760b3Swikidesign } 1644e7c760b3Swikidesign 1645a1ca9e44Swikidesign /** 1646bd6dc08eSAdrian Schlegel * checks if the submitted reCAPTCHA string is valid 1647bd6dc08eSAdrian Schlegel * 1648bd6dc08eSAdrian Schlegel * @author Adrian Schlegel <adrian@liip.ch> 1649bd6dc08eSAdrian Schlegel */ 1650283a3029SGerrit Uitslag protected function recaptchaCheck() 1651283a3029SGerrit Uitslag { 1652c3413364SGerrit Uitslag global $INPUT; 1653c3413364SGerrit Uitslag /** @var helper_plugin_recaptcha $recaptcha */ 1654c3413364SGerrit Uitslag if (!$recaptcha = plugin_load('helper', 'recaptcha')) 1655bd6dc08eSAdrian Schlegel return; // reCAPTCHA is disabled or not available 1656bd6dc08eSAdrian Schlegel 1657bd6dc08eSAdrian Schlegel // do nothing if logged in user and no reCAPTCHA required 1658c3413364SGerrit Uitslag if (!$recaptcha->getConf('forusers') && $INPUT->server->has('REMOTE_USER')) return; 1659bd6dc08eSAdrian Schlegel 1660c3413364SGerrit Uitslag $response = $recaptcha->check(); 1661c3413364SGerrit Uitslag if (!$response->is_valid) { 1662bd6dc08eSAdrian Schlegel msg($recaptcha->getLang('testfailed'), -1); 1663c3413364SGerrit Uitslag if ($INPUT->str('comment') == 'save') { 1664c3413364SGerrit Uitslag $INPUT->str('comment', 'edit'); 1665c3413364SGerrit Uitslag } elseif ($INPUT->str('comment') == 'add') { 1666c3413364SGerrit Uitslag $INPUT->str('comment', 'show'); 16674cded5e1SGerrit Uitslag } 1668bd6dc08eSAdrian Schlegel } 1669bd6dc08eSAdrian Schlegel } 1670bd6dc08eSAdrian Schlegel 1671bd6dc08eSAdrian Schlegel /** 1672ac818938SMichael Hamann * Add discussion plugin version to the indexer version 1673ac818938SMichael Hamann * This means that all pages will be indexed again in order to add the comments 1674ac818938SMichael Hamann * to the index whenever there has been a change that concerns the index content. 1675de7e6f00SGerrit Uitslag * 1676de7e6f00SGerrit Uitslag * @param Doku_Event $event 1677de7e6f00SGerrit Uitslag * @param $param 1678ac818938SMichael Hamann */ 1679283a3029SGerrit Uitslag public function addIndexVersion(Doku_Event $event) 1680283a3029SGerrit Uitslag { 1681ac818938SMichael Hamann $event->data['discussion'] = '0.1'; 1682ac818938SMichael Hamann } 1683ac818938SMichael Hamann 1684ac818938SMichael Hamann /** 1685a1ca9e44Swikidesign * Adds the comments to the index 1686de7e6f00SGerrit Uitslag * 1687de7e6f00SGerrit Uitslag * @param Doku_Event $event 1688c3413364SGerrit Uitslag * @param array $param with 1689c3413364SGerrit Uitslag * 'id' => string 'page'/'id' for respectively INDEXER_PAGE_ADD and FULLTEXT_SNIPPET_CREATE event 1690c3413364SGerrit Uitslag * 'text' => string 'body'/'text' 1691a1ca9e44Swikidesign */ 1692283a3029SGerrit Uitslag public function addCommentsToIndex(Doku_Event $event, $param) 1693283a3029SGerrit Uitslag { 1694a1ca9e44Swikidesign // get .comments meta file name 169510b5d61eSMichael Hamann $file = metaFN($event->data[$param['id']], '.comments'); 1696a1ca9e44Swikidesign 169796b3951aSMichael Hamann if (!@file_exists($file)) return; 169896b3951aSMichael Hamann $data = unserialize(io_readFile($file, false)); 1699c3413364SGerrit Uitslag 1700c3413364SGerrit Uitslag // comments are turned off or no comments available to index 1701c3413364SGerrit Uitslag if (!$data['status'] || $data['number'] == 0) return; 1702a1ca9e44Swikidesign 1703a1ca9e44Swikidesign // now add the comments 1704a1ca9e44Swikidesign if (isset($data['comments'])) { 1705a1ca9e44Swikidesign foreach ($data['comments'] as $key => $value) { 1706c3413364SGerrit Uitslag $event->data[$param['text']] .= DOKU_LF . $this->addCommentWords($key, $data); 1707a1ca9e44Swikidesign } 1708a1ca9e44Swikidesign } 1709a1ca9e44Swikidesign } 1710a1ca9e44Swikidesign 1711c3413364SGerrit Uitslag /** 1712c3413364SGerrit Uitslag * Checks if the phrase occurs in the comments and return event result true if matching 1713c3413364SGerrit Uitslag * 1714c3413364SGerrit Uitslag * @param Doku_Event $event 1715c3413364SGerrit Uitslag * @param $param 1716c3413364SGerrit Uitslag * @return void 1717c3413364SGerrit Uitslag */ 1718283a3029SGerrit Uitslag public function fulltextPhraseMatchInComments(Doku_Event $event) 1719283a3029SGerrit Uitslag { 172010b5d61eSMichael Hamann if ($event->result === true) return; 172110b5d61eSMichael Hamann 172210b5d61eSMichael Hamann // get .comments meta file name 172310b5d61eSMichael Hamann $file = metaFN($event->data['id'], '.comments'); 172410b5d61eSMichael Hamann 172510b5d61eSMichael Hamann if (!@file_exists($file)) return; 172610b5d61eSMichael Hamann $data = unserialize(io_readFile($file, false)); 1727c3413364SGerrit Uitslag 1728c3413364SGerrit Uitslag // comments are turned off or no comments available to match 1729c3413364SGerrit Uitslag if (!$data['status'] || $data['number'] == 0) return; 173010b5d61eSMichael Hamann 173110b5d61eSMichael Hamann $matched = false; 173210b5d61eSMichael Hamann 173310b5d61eSMichael Hamann // now add the comments 173410b5d61eSMichael Hamann if (isset($data['comments'])) { 1735c3413364SGerrit Uitslag foreach ($data['comments'] as $cid => $value) { 1736c3413364SGerrit Uitslag $matched = $this->phraseMatchInComment($event->data['phrase'], $cid, $data); 173710b5d61eSMichael Hamann if ($matched) break; 173810b5d61eSMichael Hamann } 173910b5d61eSMichael Hamann } 174010b5d61eSMichael Hamann 1741c3413364SGerrit Uitslag if ($matched) { 174210b5d61eSMichael Hamann $event->result = true; 174310b5d61eSMichael Hamann } 1744c3413364SGerrit Uitslag } 174510b5d61eSMichael Hamann 1746c3413364SGerrit Uitslag /** 1747c3413364SGerrit Uitslag * Match the phrase in the comment and its replies 1748c3413364SGerrit Uitslag * 1749c3413364SGerrit Uitslag * @param string $phrase phrase to search 1750c3413364SGerrit Uitslag * @param string $cid comment id 1751c3413364SGerrit Uitslag * @param array $data array with all comments by reference 1752c3413364SGerrit Uitslag * @param string $parent cid of parent 1753c3413364SGerrit Uitslag * @return bool if match true, otherwise false 1754c3413364SGerrit Uitslag */ 1755283a3029SGerrit Uitslag protected function phraseMatchInComment($phrase, $cid, &$data, $parent = '') 1756283a3029SGerrit Uitslag { 175710b5d61eSMichael Hamann if (!isset($data['comments'][$cid])) return false; // comment was removed 1758c3413364SGerrit Uitslag 175910b5d61eSMichael Hamann $comment = $data['comments'][$cid]; 176010b5d61eSMichael Hamann 176110b5d61eSMichael Hamann if (!is_array($comment)) return false; // corrupt datatype 176210b5d61eSMichael Hamann if ($comment['parent'] != $parent) return false; // reply to an other comment 176310b5d61eSMichael Hamann if (!$comment['show']) return false; // hidden comment 176410b5d61eSMichael Hamann 1765c3413364SGerrit Uitslag $text = PhpString::strtolower($comment['raw']); 176610b5d61eSMichael Hamann if (strpos($text, $phrase) !== false) { 176710b5d61eSMichael Hamann return true; 176810b5d61eSMichael Hamann } 176910b5d61eSMichael Hamann 177010b5d61eSMichael Hamann if (is_array($comment['replies'])) { // and the replies 177110b5d61eSMichael Hamann foreach ($comment['replies'] as $rid) { 1772c3413364SGerrit Uitslag if ($this->phraseMatchInComment($phrase, $rid, $data, $cid)) { 177310b5d61eSMichael Hamann return true; 177410b5d61eSMichael Hamann } 177510b5d61eSMichael Hamann } 177610b5d61eSMichael Hamann } 177710b5d61eSMichael Hamann return false; 177810b5d61eSMichael Hamann } 177910b5d61eSMichael Hamann 1780a1ca9e44Swikidesign /** 1781c3413364SGerrit Uitslag * Saves the current comment status and title from metadata into the .comments file 1782de7e6f00SGerrit Uitslag * 1783de7e6f00SGerrit Uitslag * @param Doku_Event $event 1784de7e6f00SGerrit Uitslag * @param $param 1785c1530f74SMichael Hamann */ 1786283a3029SGerrit Uitslag public function update_comment_status(Doku_Event $event) 1787283a3029SGerrit Uitslag { 1788c1530f74SMichael Hamann global $ID; 1789c1530f74SMichael Hamann 1790c1530f74SMichael Hamann $meta = $event->data['current']; 1791c1530f74SMichael Hamann $file = metaFN($ID, '.comments'); 1792f0bcde18SGerrit Uitslag $status = ($this->isDiscussionEnabled() ? 1 : 0); 1793c3413364SGerrit Uitslag $title = null; 1794c1530f74SMichael Hamann if (isset($meta['plugin_discussion'])) { 1795c3413364SGerrit Uitslag $status = $meta['plugin_discussion']['status']; // 0, 1 or 2 1796c1530f74SMichael Hamann $title = $meta['plugin_discussion']['title']; 1797c1530f74SMichael Hamann } elseif ($status == 1) { 1798c1530f74SMichael Hamann // Don't enable comments when automatic comments are on - this already happens automatically 1799c1530f74SMichael Hamann // and if comments are turned off in the admin this only updates the .comments file 1800c1530f74SMichael Hamann return; 1801c1530f74SMichael Hamann } 1802c1530f74SMichael Hamann 1803c1530f74SMichael Hamann if ($status || @file_exists($file)) { 1804c3413364SGerrit Uitslag $data = []; 1805c1530f74SMichael Hamann if (@file_exists($file)) { 1806c1530f74SMichael Hamann $data = unserialize(io_readFile($file, false)); 1807c1530f74SMichael Hamann } 1808c1530f74SMichael Hamann 1809c1530f74SMichael Hamann if (!array_key_exists('title', $data) || $data['title'] !== $title || !isset($data['status']) || $data['status'] !== $status) { 1810c1530f74SMichael Hamann $data['title'] = $title; 1811c1530f74SMichael Hamann $data['status'] = $status; 1812c3413364SGerrit Uitslag if (!isset($data['number'])) { 1813c1530f74SMichael Hamann $data['number'] = 0; 1814c3413364SGerrit Uitslag } 1815c1530f74SMichael Hamann io_saveFile($file, serialize($data)); 1816c1530f74SMichael Hamann } 1817c1530f74SMichael Hamann } 1818c1530f74SMichael Hamann } 1819c1530f74SMichael Hamann 1820c1530f74SMichael Hamann /** 1821c3413364SGerrit Uitslag * Return words of a given comment and its replies, suitable to be added to the index 1822de7e6f00SGerrit Uitslag * 1823c3413364SGerrit Uitslag * @param string $cid comment id 1824c3413364SGerrit Uitslag * @param array $data array with all comments by reference 1825c3413364SGerrit Uitslag * @param string $parent cid of parent 1826de7e6f00SGerrit Uitslag * @return string 1827a1ca9e44Swikidesign */ 1828283a3029SGerrit Uitslag protected function addCommentWords($cid, &$data, $parent = '') 1829283a3029SGerrit Uitslag { 1830a1ca9e44Swikidesign 1831efbe59d0Swikidesign if (!isset($data['comments'][$cid])) return ''; // comment was removed 1832c3413364SGerrit Uitslag 1833a1ca9e44Swikidesign $comment = $data['comments'][$cid]; 1834a1ca9e44Swikidesign 1835efbe59d0Swikidesign if (!is_array($comment)) return ''; // corrupt datatype 1836efbe59d0Swikidesign if ($comment['parent'] != $parent) return ''; // reply to an other comment 1837efbe59d0Swikidesign if (!$comment['show']) return ''; // hidden comment 1838a1ca9e44Swikidesign 1839efbe59d0Swikidesign $text = $comment['raw']; // we only add the raw comment text 1840efbe59d0Swikidesign if (is_array($comment['replies'])) { // and the replies 1841efbe59d0Swikidesign foreach ($comment['replies'] as $rid) { 1842c3413364SGerrit Uitslag $text .= $this->addCommentWords($rid, $data, $cid); 1843a1ca9e44Swikidesign } 1844a1ca9e44Swikidesign } 1845efbe59d0Swikidesign return ' ' . $text; 1846efbe59d0Swikidesign } 1847a10b5c98SMichael Klier 1848a10b5c98SMichael Klier /** 1849a10b5c98SMichael Klier * Only allow http(s) URLs and append http:// to URLs if needed 1850de7e6f00SGerrit Uitslag * 1851de7e6f00SGerrit Uitslag * @param string $url 1852de7e6f00SGerrit Uitslag * @return string 1853a10b5c98SMichael Klier */ 1854283a3029SGerrit Uitslag protected function checkURL($url) 1855283a3029SGerrit Uitslag { 1856a10b5c98SMichael Klier if (preg_match("#^http://|^https://#", $url)) { 1857a10b5c98SMichael Klier return hsc($url); 1858a10b5c98SMichael Klier } elseif (substr($url, 0, 4) == 'www.') { 1859c3413364SGerrit Uitslag return hsc('https://' . $url); 1860a10b5c98SMichael Klier } else { 1861a10b5c98SMichael Klier return ''; 1862a10b5c98SMichael Klier } 1863a10b5c98SMichael Klier } 186431aab30eSGina Haeussge 1865de7e6f00SGerrit Uitslag /** 1866de7e6f00SGerrit Uitslag * Sort threads 1867de7e6f00SGerrit Uitslag * 1868283a3029SGerrit Uitslag * @param array $a array with comment properties 1869283a3029SGerrit Uitslag * @param array $b array with comment properties 1870de7e6f00SGerrit Uitslag * @return int 1871de7e6f00SGerrit Uitslag */ 1872283a3029SGerrit Uitslag function sortThreadsOnCreation($a, $b) 1873283a3029SGerrit Uitslag { 18747e018cb6Slpaulsen93 if (is_array($a['date'])) { 18757e018cb6Slpaulsen93 // new format 187631aab30eSGina Haeussge $createdA = $a['date']['created']; 18777e018cb6Slpaulsen93 } else { 18787e018cb6Slpaulsen93 // old format 187931aab30eSGina Haeussge $createdA = $a['date']; 188031aab30eSGina Haeussge } 188131aab30eSGina Haeussge 18827e018cb6Slpaulsen93 if (is_array($b['date'])) { 18837e018cb6Slpaulsen93 // new format 188431aab30eSGina Haeussge $createdB = $b['date']['created']; 18857e018cb6Slpaulsen93 } else { 18867e018cb6Slpaulsen93 // old format 188731aab30eSGina Haeussge $createdB = $b['date']; 188831aab30eSGina Haeussge } 188931aab30eSGina Haeussge 18904cded5e1SGerrit Uitslag if ($createdA == $createdB) { 189131aab30eSGina Haeussge return 0; 18924cded5e1SGerrit Uitslag } else { 189331aab30eSGina Haeussge return ($createdA < $createdB) ? -1 : 1; 189431aab30eSGina Haeussge } 18954cded5e1SGerrit Uitslag } 189631aab30eSGina Haeussge 1897c3413364SGerrit Uitslag} 1898c3413364SGerrit Uitslag 1899c3413364SGerrit Uitslag 1900