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