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 10if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 11if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13 14require_once(DOKU_PLUGIN.'action.php'); 15 16class action_plugin_discussion extends DokuWiki_Action_Plugin{ 17 18 function getInfo() { 19 return array( 20 'author' => 'Gina Häußge, Michael Klier, Esther Brunner', 21 'email' => 'dokuwiki@chimeric.de', 22 'date' => '2008-06-28', 23 'name' => 'Discussion Plugin (action component)', 24 'desc' => 'Enables discussion features', 25 'url' => 'http://wiki.splitbrain.org/plugin:discussion', 26 ); 27 } 28 29 function register(&$contr) { 30 $contr->register_hook( 31 'ACTION_ACT_PREPROCESS', 32 'BEFORE', 33 $this, 34 'handle_act_preprocess', 35 array() 36 ); 37 $contr->register_hook( 38 'TPL_ACT_RENDER', 39 'AFTER', 40 $this, 41 'comments', 42 array() 43 ); 44 $contr->register_hook( 45 'INDEXER_PAGE_ADD', 46 'AFTER', 47 $this, 48 'idx_add_discussion', 49 array() 50 ); 51 } 52 53 /** 54 * Handles comment actions, dispatches data processing routines 55 */ 56 function handle_act_preprocess(&$event, $param) { 57 global $ID; 58 global $conf; 59 60 // handle newthread ACTs 61 if ($event->data == 'newthread') { 62 // we can handle it -> prevent others 63 // $event->stopPropagation(); 64 $event->preventDefault(); 65 66 $event->data = $this->_newThread(); 67 } 68 69 // enable captchas 70 if ((in_array($_REQUEST['comment'], array('add', 'save'))) 71 && (@file_exists(DOKU_PLUGIN.'captcha/action.php'))) { 72 $this->_captchaCheck(); 73 } 74 75 // if we are not in show mode, that was all for now 76 if ($event->data != 'show') return; 77 78 // do the data processing for comments 79 $cid = $_REQUEST['cid']; 80 switch ($_REQUEST['comment']) { 81 case 'add': 82 $comment = array( 83 'user' => array( 84 'id' => hsc($_REQUEST['user']), 85 'name' => hsc($_REQUEST['name']), 86 'mail' => hsc($_REQUEST['mail']), 87 'url' => hsc($_REQUEST['url']), 88 'address' => hsc($_REQUEST['address'])), 89 'date' => array('created' => $_REQUEST['date']), 90 'raw' => cleanText($_REQUEST['text']) 91 ); 92 $repl = $_REQUEST['reply']; 93 $this->_add($comment, $repl); 94 break; 95 96 case 'save': 97 $raw = cleanText($_REQUEST['text']); 98 $this->_save(array($cid), $raw); 99 break; 100 101 case 'delete': 102 $this->_save(array($cid), ''); 103 break; 104 105 case 'toogle': 106 $this->_save(array($cid), '', 'toogle'); 107 break; 108 } 109 110 // FIXME use new TPL_TOC_RENDER event in the future 111 $tocmeta = p_get_metadata($ID, 'description tableofcontents'); 112 if(count($tocmeta) >= ($conf['maxtoclevel']-1)) { 113 114 $TOC = array(); 115 global $TOC; 116 $TOC = $tocmeta; 117 118 $tocitem = array( 'hid' => 'discussion__section', 119 'title' => $this->getLang('discussion'), 120 'type' => 'ul', 121 'level' => 1 ); 122 123 $file = metaFN($ID, '.comments'); 124 if(@file_exists($file)) { 125 $data = unserialize(io_readFile($file)); 126 if($data['status'] != 0 && !empty($TOC)) { 127 $TOC[] = $tocitem; 128 } 129 } 130 } 131 } 132 133 /** 134 * Main function; dispatches the visual comment actions 135 */ 136 function comments(&$event, $param) { 137 if ($event->data != 'show') return; // nothing to do for us 138 139 $cid = $_REQUEST['cid']; 140 switch ($_REQUEST['comment']) { 141 case 'edit': 142 $this->_show(NULL, $cid); 143 break; 144 default: 145 $this->_show($cid); 146 break; 147 } 148 } 149 150 /** 151 * Redirects browser to given comment anchor 152 */ 153 function _redirect($cid) { 154 global $ID; 155 global $ACT; 156 157 if ($ACT !== 'show') return; 158 header('Location: ' . wl($ID) . '#comment__' . $cid); 159 } 160 161 /** 162 * Shows all comments of the current page 163 */ 164 function _show($reply = NULL, $edit = NULL) { 165 global $ID; 166 global $INFO; 167 global $ACT; 168 169 // get .comments meta file name 170 $file = metaFN($ID, '.comments'); 171 172 if (!@file_exists($file) && !$this->getConf('automatic')) return false; 173 174 // load data 175 if (@file_exists($file)) { 176 $data = unserialize(io_readFile($file, false)); 177 if (!$data['status']) return false; // comments are turned off 178 } elseif (!@file_exists($file) && $this->getConf('automatic') && $INFO['exists']) { 179 // set status to show the comment form 180 $data['status'] = 1; 181 $data['number'] = 0; 182 } 183 184 // section title 185 $title = ($data['title'] ? hsc($data['title']) : $this->getLang('discussion')); 186 ptln('<div class="comment_wrapper">'); 187 ptln('<h2><a name="discussion__section" id="discussion__section">', 2); 188 ptln($title, 4); 189 ptln('</a></h2>', 2); 190 ptln('<div class="level2 hfeed">', 2); 191 // now display the comments 192 if (isset($data['comments'])) { 193 foreach ($data['comments'] as $key => $value) { 194 if ($key == $edit) $this->_form($value['raw'], 'save', $edit); // edit form 195 else $this->_print($key, $data, '', $reply); 196 } 197 } 198 199 // comment form 200 if (($data['status'] == 1) && !$reply && !$edit) $this->_form(''); 201 202 ptln('</div>', 2); // level2 hfeed 203 ptln('</div>'); // comment_wrapper 204 205 return true; 206 } 207 208 /** 209 * Adds a new comment and then displays all comments 210 */ 211 function _add($comment, $parent) { 212 global $ID, $TEXT; 213 214 $otxt = $TEXT; // set $TEXT to comment text for wordblock check 215 $TEXT = $comment['raw']; 216 217 // spamcheck against the DokuWiki blacklist 218 if (checkwordblock()) { 219 msg($this->getLang('wordblock'), -1); 220 return false; 221 } 222 223 $TEXT = $otxt; // restore global $TEXT 224 225 // get discussion meta file name 226 $file = metaFN($ID, '.comments'); 227 228 // create comments file if it doesn't exist yet 229 if(!@file_exists($file)) { 230 $data = array('status' => 1, 'number' => 0); 231 io_saveFile($file, serialize($data)); 232 } else { 233 $data = array(); 234 $data = unserialize(io_readFile($file, false)); 235 if ($data['status'] != 1) return false; // comments off or closed 236 } 237 238 if ((!$this->getConf('allowguests')) 239 && ($comment['user']['id'] != $_SERVER['REMOTE_USER'])) 240 return false; // guest comments not allowed 241 242 if ($comment['date']['created']) $date = strtotime($comment['date']['created']); 243 else $date = time(); 244 if ($date == -1) $date = time(); 245 $cid = md5($comment['user']['id'].$date); // create a unique id 246 247 if (!is_array($data['comments'][$parent])) $parent = NULL; // invalid parent comment 248 249 // render the comment 250 $xhtml = $this->_render($comment['raw']); 251 252 // fill in the new comment 253 $data['comments'][$cid] = array( 254 'user' => $comment['user'], 255 'date' => array('created' => $date), 256 'show' => true, 257 'raw' => $comment['raw'], 258 'xhtml' => $xhtml, 259 'parent' => $parent, 260 'replies' => array() 261 ); 262 263 // update parent comment 264 if ($parent) $data['comments'][$parent]['replies'][] = $cid; 265 266 // update the number of comments 267 $data['number']++; 268 269 // save the comment metadata file 270 io_saveFile($file, serialize($data)); 271 $this->_addLogEntry($date, $ID, 'cc', '', $cid); 272 273 // notify subscribers of the page 274 $data['comments'][$cid]['cid'] = $cid; 275 $this->_notify($data['comments'][$cid]); 276 277 $this->_redirect($cid); 278 return true; 279 } 280 281 /** 282 * Saves the comment with the given ID and then displays all comments 283 */ 284 function _save($cids, $raw, $act = NULL) { 285 global $ID; 286 287 if ($raw) { 288 global $TEXT; 289 290 $otxt = $TEXT; // set $TEXT to comment text for wordblock check 291 $TEXT = $raw; 292 293 // spamcheck against the DokuWiki blacklist 294 if (checkwordblock()) { 295 msg($this->getLang('wordblock'), -1); 296 return false; 297 } 298 299 $TEXT = $otxt; // restore global $TEXT 300 } 301 302 // get discussion meta file name 303 $file = metaFN($ID, '.comments'); 304 $data = unserialize(io_readFile($file, false)); 305 306 if (!is_array($cids)) $cids = array($cids); 307 foreach ($cids as $cid) { 308 309 if (is_array($data['comments'][$cid]['user'])) { 310 $user = $data['comments'][$cid]['user']['id']; 311 $convert = false; 312 } else { 313 $user = $data['comments'][$cid]['user']; 314 $convert = true; 315 } 316 317 // someone else was trying to edit our comment -> abort 318 if (($user != $_SERVER['REMOTE_USER']) && (!auth_ismanager())) return false; 319 320 $date = time(); 321 322 // need to convert to new format? 323 if ($convert) { 324 $data['comments'][$cid]['user'] = array( 325 'id' => $user, 326 'name' => $data['comments'][$cid]['name'], 327 'mail' => $data['comments'][$cid]['mail'], 328 'url' => $data['comments'][$cid]['url'], 329 'address' => $data['comments'][$cid]['address'], 330 ); 331 $data['comments'][$cid]['date'] = array( 332 'created' => $data['comments'][$cid]['date'] 333 ); 334 } 335 336 if ($act == 'toogle') { // toogle visibility 337 $now = $data['comments'][$cid]['show']; 338 $data['comments'][$cid]['show'] = !$now; 339 $data['number'] = $this->_count($data); 340 341 $type = ($data['comments'][$cid]['show'] ? 'sc' : 'hc'); 342 343 } elseif ($act == 'show') { // show comment 344 $data['comments'][$cid]['show'] = true; 345 $data['number'] = $this->_count($data); 346 347 $type = 'sc'; // show comment 348 349 } elseif ($act == 'hide') { // hide comment 350 $data['comments'][$cid]['show'] = false; 351 $data['number'] = $this->_count($data); 352 353 $type = 'hc'; // hide comment 354 355 } elseif (!$raw) { // remove the comment 356 $data['comments'] = $this->_removeComment($cid, $data['comments']); 357 $data['number'] = $this->_count($data); 358 359 $type = 'dc'; // delete comment 360 361 } else { // save changed comment 362 $xhtml = $this->_render($raw); 363 364 // now change the comment's content 365 $data['comments'][$cid]['date']['modified'] = $date; 366 $data['comments'][$cid]['raw'] = $raw; 367 $data['comments'][$cid]['xhtml'] = $xhtml; 368 369 $type = 'ec'; // edit comment 370 } 371 } 372 373 // save the comment metadata file 374 io_saveFile($file, serialize($data)); 375 $this->_addLogEntry($date, $ID, $type, '', $cid); 376 377 $this->_redirect($cid); 378 return true; 379 } 380 381 /** 382 * Recursive function to remove a comment 383 */ 384 function _removeComment($cid, $comments) { 385 if (is_array($comments[$cid]['replies'])) { 386 foreach ($comments[$cid]['replies'] as $rid) { 387 $comments = $this->_removeComment($rid, $comments); 388 } 389 } 390 unset($comments[$cid]); 391 return $comments; 392 } 393 394 /** 395 * Prints an individual comment 396 */ 397 function _print($cid, &$data, $parent = '', $reply = '', $visible = true) { 398 global $conf, $lang, $ID; 399 400 if (!isset($data['comments'][$cid])) return false; // comment was removed 401 $comment = $data['comments'][$cid]; 402 403 if (!is_array($comment)) return false; // corrupt datatype 404 405 if ($comment['parent'] != $parent) return true; // reply to an other comment 406 407 if (!$comment['show']) { // comment hidden 408 if (auth_ismanager()) $hidden = ' comment_hidden'; 409 else return true; 410 } else { 411 $hidden = ''; 412 } 413 414 // comment head with date and user data 415 ptln('<div class="hentry'.$hidden.'">', 4); 416 ptln('<div class="comment_head">', 6); 417 ptln('<a name="comment__'.$cid.'" id="comment__'.$cid.'"></a>', 8); 418 $head = '<span class="vcard author">'; 419 420 // prepare variables 421 if (is_array($comment['user'])) { // new format 422 $user = $comment['user']['id']; 423 $name = $comment['user']['name']; 424 $mail = $comment['user']['mail']; 425 $url = $comment['user']['url']; 426 $address = $comment['user']['address']; 427 } else { // old format 428 $user = $comment['user']; 429 $name = $comment['name']; 430 $mail = $comment['mail']; 431 $url = $comment['url']; 432 $address = $comment['address']; 433 } 434 if (is_array($comment['date'])) { // new format 435 $created = $comment['date']['created']; 436 $modified = $comment['date']['modified']; 437 } else { // old format 438 $created = $comment['date']; 439 $modified = $comment['edited']; 440 } 441 442 // show avatar image? 443 if ($this->getConf('useavatar') 444 && (!plugin_isdisabled('avatar')) 445 && ($avatar =& plugin_load('helper', 'avatar'))) { 446 if ($mail) $head .= $avatar->getXHTML($mail, $name, 'left'); 447 else $head .= $avatar->getXHTML($user, $name, 'left'); 448 $style = ' style="margin-left: '.($avatar->getConf('size') + 14).'px;"'; 449 } else { 450 $style = ' style="margin-left: 20px;"'; 451 } 452 453 if ($this->getConf('linkemail') && $mail) { 454 $head .= $this->email($mail, $name, 'email fn'); 455 } elseif ($url) { 456 $head .= $this->external_link($url, $name, 'urlextern url fn'); 457 } else { 458 $head .= '<span class="fn">'.$name.'</span>'; 459 } 460 if ($address) $head .= ', <span class="adr">'.$address.'</span>'; 461 $head .= '</span>, '. 462 '<abbr class="published" title="'.strftime('%Y-%m-%dT%H:%M:%SZ', $created).'">'. 463 strftime($conf['dformat'], $created).'</abbr>'; 464 if ($comment['edited']) $head .= ' (<abbr class="updated" title="'. 465 strftime('%Y-%m-%dT%H:%M:%SZ', $modified).'">'.strftime($conf['dformat'], $modified). 466 '</abbr>)'; 467 ptln($head, 8); 468 ptln('</div>', 6); // class="comment_head" 469 470 // main comment content 471 ptln('<div class="comment_body entry-content"'. 472 ($this->getConf('useavatar') ? $style : '').'>', 6); 473 echo $comment['xhtml'].DOKU_LF; 474 ptln('</div>', 6); // class="comment_body" 475 476 if ($visible) { 477 ptln('<div class="comment_buttons">', 6); 478 479 // show reply button? 480 if (($data['status'] == 1) && !$reply && $comment['show'] 481 && ($this->getConf('allowguests') || $_SERVER['REMOTE_USER'])) 482 $this->_button($cid, $this->getLang('btn_reply'), 'reply', true); 483 484 // show edit, show/hide and delete button? 485 if ((($user == $_SERVER['REMOTE_USER']) && ($user != '')) || (auth_ismanager())) { 486 $this->_button($cid, $lang['btn_secedit'], 'edit', true); 487 $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show')); 488 $this->_button($cid, $label, 'toogle'); 489 $this->_button($cid, $lang['btn_delete'], 'delete'); 490 } 491 ptln('</div>', 6); // class="comment_buttons" 492 } 493 ptln('</div>', 4); // class="hentry" 494 495 // replies to this comment entry? 496 if (count($comment['replies'])) { 497 ptln('<div class="comment_replies"'.$style.'>', 4); 498 $visible = ($comment['show'] && $visible); 499 foreach ($comment['replies'] as $rid) { 500 $this->_print($rid, $data, $cid, $reply, $visible); 501 } 502 ptln('</div>', 4); // class="comment_replies" 503 } 504 505 // reply form 506 if ($reply == $cid) { 507 ptln('<div class="comment_replies">', 4); 508 $this->_form('', 'add', $cid); 509 ptln('</div>', 4); // class="comment_replies" 510 } 511 } 512 513 /** 514 * Outputs the comment form 515 */ 516 function _form($raw = '', $act = 'add', $cid = NULL) { 517 global $lang, $conf, $ID, $INFO; 518 519 // not for unregistered users when guest comments aren't allowed 520 if (!$_SERVER['REMOTE_USER'] && !$this->getConf('allowguests')) return false; 521 522 // fill $raw with $_REQUEST['text'] if it's empty (for failed CAPTCHA check) 523 if (!$raw && ($_REQUEST['comment'] == 'show')) $raw = $_REQUEST['text']; 524 ?> 525 526 <div class="comment_form"> 527 <form id="discussion__comment_form" method="post" action="<?php echo script() ?>" accept-charset="<?php echo $lang['encoding'] ?>" onsubmit="return validate(this);"> 528 <div class="no"> 529 <input type="hidden" name="id" value="<?php echo $ID ?>" /> 530 <input type="hidden" name="do" value="show" /> 531 <input type="hidden" name="comment" value="<?php echo $act ?>" /> 532 533 <?php 534 // for adding a comment 535 if ($act == 'add') { 536 ?> 537 <input type="hidden" name="reply" value="<?php echo $cid ?>" /> 538 <?php 539 // for registered user (and we're not in admin import mode) 540 if ($conf['useacl'] && $_SERVER['REMOTE_USER'] 541 && (!($this->getConf('adminimport') && (auth_ismanager())))) { 542 ?> 543 <input type="hidden" name="user" value="<?php echo hsc($_SERVER['REMOTE_USER']) ?>" /> 544 <input type="hidden" name="name" value="<?php echo hsc($INFO['userinfo']['name']) ?>" /> 545 <input type="hidden" name="mail" value="<?php echo hsc($INFO['userinfo']['mail']) ?>" /> 546 <?php 547 // for guest: show name and e-mail entry fields 548 } else { 549 ?> 550 <input type="hidden" name="user" value="<?php echo clientIP() ?>" /> 551 <div class="comment_name"> 552 <label class="block" for="discussion__comment_name"> 553 <span><?php echo $lang['fullname'] ?>:</span> 554 <input type="text" class="edit" name="name" id="discussion__comment_name" size="50" tabindex="1" value="<?php echo hsc($_REQUEST['name'])?>" /> 555 </label> 556 </div> 557 <div class="comment_mail"> 558 <label class="block" for="discussion__comment_mail"> 559 <span><?php echo $lang['email'] ?>:</span> 560 <input type="text" class="edit" name="mail" id="discussion__comment_mail" size="50" tabindex="2" value="<?php echo hsc($_REQUEST['mail'])?>" /> 561 </label> 562 </div> 563 <?php 564 } 565 566 // allow entering an URL 567 if ($this->getConf('urlfield')) { 568 ?> 569 <div class="comment_url"> 570 <label class="block" for="discussion__comment_url"> 571 <span><?php echo $this->getLang('url') ?>:</span> 572 <input type="text" class="edit" name="url" id="discussion__comment_url" size="50" tabindex="3" value="<?php echo hsc($_REQUEST['url'])?>" /> 573 </label> 574 </div> 575 <?php 576 } 577 578 // allow entering an address 579 if ($this->getConf('addressfield')) { 580 ?> 581 <div class="comment_address"> 582 <label class="block" for="discussion__comment_address"> 583 <span><?php echo $this->getLang('address') ?>:</span> 584 <input type="text" class="edit" name="address" id="discussion__comment_address" size="50" tabindex="4" value="<?php echo hsc($_REQUEST['address'])?>" /> 585 </label> 586 </div> 587 <?php 588 } 589 590 // allow setting the comment date 591 if ($this->getConf('adminimport') && (auth_ismanager())) { 592 ?> 593 <div class="comment_date"> 594 <label class="block" for="discussion__comment_date"> 595 <span><?php echo $this->getLang('date') ?>:</span> 596 <input type="text" class="edit" name="date" id="discussion__comment_date" size="50" /> 597 </label> 598 </div> 599 <?php 600 } 601 602 // for saving a comment 603 } else { 604 ?> 605 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 606 <?php 607 } 608 ?> 609 <div class="comment_text"> 610 <?php 611 echo $this->getLang('entercomment'); 612 if ($this->getConf('wikisyntaxok')) echo ' ('.$this->getLang('wikisyntax').')'; 613 echo ':<br />'.DOKU_LF; 614 ?> 615 <textarea class="edit" name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php echo formText($raw) ?></textarea> 616 </div> 617 <?php //bad and dirty event insert hook 618 $evdata = array('writable' => true); 619 trigger_event('HTML_EDITFORM_INJECTION', $evdata); 620 ?> 621 <input class="button" type="submit" name="submit" accesskey="s" value="<?php echo $lang['btn_save'] ?>" title="<?php echo $lang['btn_save']?> [ALt+S]" tabindex="6" /> 622 </div> 623 </form> 624 </div> 625 <?php 626 if ($this->getConf('usecocomment')) echo $this->_coComment(); 627 } 628 629 /** 630 * Adds a javascript to interact with coComments 631 */ 632 function _coComment() { 633 global $ID; 634 global $conf; 635 global $INFO; 636 637 $user = $_SERVER['REMOTE_USER']; 638 639 ?> 640 <script type="text/javascript"><!--//--><![CDATA[//><!-- 641 var blogTool = "DokuWiki"; 642 var blogURL = "<?php echo DOKU_URL ?>"; 643 var blogTitle = "<?php echo $conf['title'] ?>"; 644 var postURL = "<?php echo wl($ID, '', true) ?>"; 645 var postTitle = "<?php echo tpl_pagetitle($ID, true) ?>"; 646 <?php 647 if ($user) { 648 ?> 649 var commentAuthor = "<?php echo $INFO['userinfo']['name'] ?>"; 650 <?php 651 } else { 652 ?> 653 var commentAuthorFieldName = "name"; 654 <?php 655 } 656 ?> 657 var commentAuthorLoggedIn = <?php echo ($user ? 'true' : 'false') ?>; 658 var commentFormID = "discussion__comment_form"; 659 var commentTextFieldName = "text"; 660 var commentButtonName = "submit"; 661 var cocomment_force = false; 662 //--><!]]></script> 663 <script type="text/javascript" src="http://www.cocomment.com/js/cocomment.js"> 664 </script> 665 <?php 666 } 667 668 /** 669 * General button function 670 */ 671 function _button($cid, $label, $act, $jump = false) { 672 global $ID; 673 674 $anchor = ($jump ? '#discussion__comment_form' : '' ); 675 676 ?> 677 <form class="button discussion__<?php echo $act?>" method="post" action="<?php echo script().$anchor ?>"> 678 <div class="no"> 679 <input type="hidden" name="id" value="<?php echo $ID ?>" /> 680 <input type="hidden" name="do" value="show" /> 681 <input type="hidden" name="comment" value="<?php echo $act ?>" /> 682 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 683 <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>" /> 684 </div> 685 </form> 686 <?php 687 return true; 688 } 689 690 /** 691 * Adds an entry to the comments changelog 692 * 693 * @author Esther Brunner <wikidesign@gmail.com> 694 * @author Ben Coburn <btcoburn@silicodon.net> 695 */ 696 function _addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '') { 697 global $conf; 698 699 $changelog = $conf['metadir'].'/_comments.changes'; 700 701 if(!$date) $date = time(); //use current time if none supplied 702 $remote = $_SERVER['REMOTE_ADDR']; 703 $user = $_SERVER['REMOTE_USER']; 704 705 $strip = array("\t", "\n"); 706 $logline = array( 707 'date' => $date, 708 'ip' => $remote, 709 'type' => str_replace($strip, '', $type), 710 'id' => $id, 711 'user' => $user, 712 'sum' => str_replace($strip, '', $summary), 713 'extra' => str_replace($strip, '', $extra) 714 ); 715 716 // add changelog line 717 $logline = implode("\t", $logline)."\n"; 718 io_saveFile($changelog, $logline, true); //global changelog cache 719 $this->_trimRecentCommentsLog($changelog); 720 721 // tell the indexer to re-index the page 722 @unlink(metaFN($id, '.indexed')); 723 } 724 725 /** 726 * Trims the recent comments cache to the last $conf['changes_days'] recent 727 * changes or $conf['recent'] items, which ever is larger. 728 * The trimming is only done once a day. 729 * 730 * @author Ben Coburn <btcoburn@silicodon.net> 731 */ 732 function _trimRecentCommentsLog($changelog) { 733 global $conf; 734 735 if (@file_exists($changelog) && 736 (filectime($changelog) + 86400) < time() && 737 !@file_exists($changelog.'_tmp')) { 738 739 io_lock($changelog); 740 $lines = file($changelog); 741 if (count($lines)<$conf['recent']) { 742 // nothing to trim 743 io_unlock($changelog); 744 return true; 745 } 746 747 io_saveFile($changelog.'_tmp', ''); // presave tmp as 2nd lock 748 $trim_time = time() - $conf['recent_days']*86400; 749 $out_lines = array(); 750 751 for ($i=0; $i<count($lines); $i++) { 752 $log = parseChangelogLine($lines[$i]); 753 if ($log === false) continue; // discard junk 754 if ($log['date'] < $trim_time) { 755 $old_lines[$log['date'].".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) 756 } else { 757 $out_lines[$log['date'].".$i"] = $lines[$i]; // definitely keep these lines 758 } 759 } 760 761 // sort the final result, it shouldn't be necessary, 762 // however the extra robustness in making the changelog cache self-correcting is worth it 763 ksort($out_lines); 764 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 765 if ($extra > 0) { 766 ksort($old_lines); 767 $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines); 768 } 769 770 // save trimmed changelog 771 io_saveFile($changelog.'_tmp', implode('', $out_lines)); 772 @unlink($changelog); 773 if (!rename($changelog.'_tmp', $changelog)) { 774 // rename failed so try another way... 775 io_unlock($changelog); 776 io_saveFile($changelog, implode('', $out_lines)); 777 @unlink($changelog.'_tmp'); 778 } else { 779 io_unlock($changelog); 780 } 781 return true; 782 } 783 } 784 785 /** 786 * Sends a notify mail on new comment 787 * 788 * @param array $comment data array of the new comment 789 * 790 * @author Andreas Gohr <andi@splitbrain.org> 791 * @author Esther Brunner <wikidesign@gmail.com> 792 */ 793 function _notify($comment) { 794 global $conf; 795 global $ID; 796 797 if ((!$conf['subscribers']) && (!$conf['notify'])) return; //subscribers enabled? 798 $bcc = subscriber_addresslist($ID); 799 if ((empty($bcc)) && (!$conf['notify'])) return; 800 $to = $conf['notify']; 801 $text = io_readFile($this->localFN('subscribermail')); 802 803 $search = array( 804 '@PAGE@', 805 '@TITLE@', 806 '@DATE@', 807 '@NAME@', 808 '@TEXT@', 809 '@COMMENTURL@', 810 '@UNSUBSCRIBE@', 811 '@DOKUWIKIURL@', 812 ); 813 $replace = array( 814 $ID, 815 $conf['title'], 816 strftime($conf['dformat'], $comment['date']['created']), 817 $comment['user']['name'], 818 $comment['raw'], 819 wl($ID, '', true) . '#comment__' . $comment['cid'], 820 wl($ID, 'do=unsubscribe', true, '&'), 821 DOKU_URL, 822 ); 823 $text = str_replace($search, $replace, $text); 824 825 $subject = '['.$conf['title'].'] '.$this->getLang('mail_newcomment'); 826 827 mail_send($to, $subject, $text, $conf['mailfrom'], '', $bcc); 828 } 829 830 /** 831 * Counts the number of visible comments 832 */ 833 function _count($data) { 834 $number = 0; 835 foreach ($data['comments'] as $cid => $comment) { 836 if ($comment['parent']) continue; 837 if (!$comment['show']) continue; 838 $number++; 839 $rids = $comment['replies']; 840 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 841 } 842 return $number; 843 } 844 845 function _countReplies(&$data, $rids) { 846 $number = 0; 847 foreach ($rids as $rid) { 848 if (!isset($data['comments'][$rid])) continue; // reply was removed 849 if (!$data['comments'][$rid]['show']) continue; 850 $number++; 851 $rids = $data['comments'][$rid]['replies']; 852 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 853 } 854 return $number; 855 } 856 857 /** 858 * Renders the comment text 859 */ 860 function _render($raw) { 861 if ($this->getConf('wikisyntaxok')) { 862 $xhtml = $this->render($raw); 863 } else { // wiki syntax not allowed -> just encode special chars 864 $xhtml = htmlspecialchars(trim($raw)); 865 } 866 return $xhtml; 867 } 868 869 /** 870 * Adds a TOC item for the discussion section 871 */ 872 function add_toc_item(&$event, $param) { 873 if ($event->data[0] != 'xhtml') return; // nothing to do for us 874 if (!$this->_hasDiscussion($title)) return; // no discussion section 875 876 $pattern = '/<div id="toc__inside">(.*?)<\/div>\s<\/div>/s'; 877 if (!preg_match($pattern, $event->data[1], $match)) return; // no TOC on this page 878 879 // ok, then let's do it! 880 global $conf; 881 882 if (!$title) $title = $this->getLang('discussion'); 883 $section = '#discussion__section'; 884 $level = 3 - $conf['toptoclevel']; 885 886 $item = '<li class="level'.$level.'">'.DOKU_LF. 887 DOKU_TAB.'<div class="li">'.DOKU_LF. 888 DOKU_TAB.DOKU_TAB.'<span class="li"><a href="'.$section.'" class="toc">'.DOKU_LF. 889 DOKU_TAB.DOKU_TAB.DOKU_TAB.$title.DOKU_LF. 890 DOKU_TAB.DOKU_TAB.'</a></span>'.DOKU_LF. 891 DOKU_TAB.'</div>'.DOKU_LF. 892 '</li>'.DOKU_LF; 893 894 if ($level == 1) $search = "</ul>\n</div>"; 895 else $search = "</ul>\n</li></ul>\n</div>"; 896 897 $new = str_replace($search, $item.$search, $match[0]); 898 $event->data[1] = preg_replace($pattern, $new, $event->data[1]); 899 } 900 901 /** 902 * Finds out whether there is a discussion section for the current page 903 */ 904 function _hasDiscussion(&$title) { 905 global $ID; 906 907 $cfile = metaFN($ID, '.comments'); 908 909 if (!@file_exists($cfile)) { 910 if ($this->getConf('automatic')) { 911 return true; 912 } else { 913 return false; 914 } 915 } 916 917 $comments = unserialize(io_readFile($cfile, false)); 918 919 if ($comments['title']) $title = hsc($comments['title']); 920 $num = $comments['number']; 921 if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return false; 922 else return true; 923 } 924 925 /** 926 * Creates a new thread page 927 */ 928 function _newThread() { 929 global $ID, $INFO; 930 931 $ns = cleanID($_REQUEST['ns']); 932 $title = str_replace(':', '', $_REQUEST['title']); 933 $back = $ID; 934 $ID = ($ns ? $ns.':' : '').cleanID($title); 935 $INFO = pageinfo(); 936 937 // check if we are allowed to create this file 938 if ($INFO['perm'] >= AUTH_CREATE) { 939 940 //check if locked by anyone - if not lock for my self 941 if ($INFO['locked']) return 'locked'; 942 else lock($ID); 943 944 // prepare the new thread file with default stuff 945 if (!@file_exists($INFO['filepath'])) { 946 global $TEXT; 947 948 $TEXT = pageTemplate(array(($ns ? $ns.':' : '').$title)); 949 if (!$TEXT) { 950 $data = array('id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back); 951 $TEXT = $this->_pageTemplate($data); 952 } 953 return 'preview'; 954 } else { 955 return 'edit'; 956 } 957 } else { 958 return 'show'; 959 } 960 } 961 962 /** 963 * Adapted version of pageTemplate() function 964 */ 965 function _pageTemplate($data) { 966 global $conf, $INFO; 967 968 $id = $data['id']; 969 $user = $_SERVER['REMOTE_USER']; 970 $tpl = io_readFile(DOKU_PLUGIN.'discussion/_template.txt'); 971 972 // standard replacements 973 $replace = array( 974 '@NS@' => $data['ns'], 975 '@PAGE@' => strtr(noNS($id),'_',' '), 976 '@USER@' => $user, 977 '@NAME@' => $INFO['userinfo']['name'], 978 '@MAIL@' => $INFO['userinfo']['mail'], 979 '@DATE@' => strftime($conf['dformat']), 980 ); 981 982 // additional replacements 983 $replace['@BACK@'] = $data['back']; 984 $replace['@TITLE@'] = $data['title']; 985 986 // avatar if useavatar and avatar plugin available 987 if ($this->getConf('useavatar') 988 && (@file_exists(DOKU_PLUGIN.'avatar/syntax.php')) 989 && (!plugin_isdisabled('avatar'))) { 990 $replace['@AVATAR@'] = '{{avatar>'.$user.' }} '; 991 } else { 992 $replace['@AVATAR@'] = ''; 993 } 994 995 // tag if tag plugin is available 996 if ((@file_exists(DOKU_PLUGIN.'tag/syntax/tag.php')) 997 && (!plugin_isdisabled('tag'))) { 998 $replace['@TAG@'] = "\n\n{{tag>}}"; 999 } else { 1000 $replace['@TAG@'] = ''; 1001 } 1002 1003 // do the replace 1004 $tpl = str_replace(array_keys($replace), array_values($replace), $tpl); 1005 return $tpl; 1006 } 1007 1008 /** 1009 * Checks if the CAPTCHA string submitted is valid 1010 * 1011 * @author Andreas Gohr <gohr@cosmocode.de> 1012 * @adaption Esther Brunner <wikidesign@gmail.com> 1013 */ 1014 function _captchaCheck() { 1015 if (@file_exists(DOKU_PLUGIN.'captcha/disabled')) return; // CAPTCHA is disabled 1016 1017 require_once(DOKU_PLUGIN.'captcha/action.php'); 1018 $captcha = new action_plugin_captcha; 1019 1020 // do nothing if logged in user and no CAPTCHA required 1021 if (!$captcha->getConf('forusers') && $_SERVER['REMOTE_USER']) return; 1022 1023 // compare provided string with decrypted captcha 1024 $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'], auth_cookiesalt()); 1025 $code = $captcha->_generateCAPTCHA($captcha->_fixedIdent(), $rand); 1026 1027 if (!$_REQUEST['plugin__captcha_secret'] || 1028 !$_REQUEST['plugin__captcha'] || 1029 strtoupper($_REQUEST['plugin__captcha']) != $code) { 1030 1031 // CAPTCHA test failed! Continue to edit instead of saving 1032 msg($captcha->getLang('testfailed'), -1); 1033 if ($_REQUEST['comment'] == 'save') $_REQUEST['comment'] = 'edit'; 1034 elseif ($_REQUEST['comment'] == 'add') $_REQUEST['comment'] = 'show'; 1035 } 1036 // if we arrive here it was a valid save 1037 } 1038 1039 /** 1040 * Adds the comments to the index 1041 */ 1042 function idx_add_discussion(&$event, $param) { 1043 1044 // get .comments meta file name 1045 $file = metaFN($event->data[0], '.comments'); 1046 1047 if (@file_exists($file)) $data = unserialize(io_readFile($file, false)); 1048 if ((!$data['status']) || ($data['number'] == 0)) return; // comments are turned off 1049 1050 // now add the comments 1051 if (isset($data['comments'])) { 1052 foreach ($data['comments'] as $key => $value) { 1053 $event->data[1] .= $this->_addCommentWords($key, $data); 1054 } 1055 } 1056 } 1057 1058 /** 1059 * Adds the words of a given comment to the index 1060 */ 1061 function _addCommentWords($cid, &$data, $parent = '') { 1062 1063 if (!isset($data['comments'][$cid])) return ''; // comment was removed 1064 $comment = $data['comments'][$cid]; 1065 1066 if (!is_array($comment)) return ''; // corrupt datatype 1067 if ($comment['parent'] != $parent) return ''; // reply to an other comment 1068 if (!$comment['show']) return ''; // hidden comment 1069 1070 $text = $comment['raw']; // we only add the raw comment text 1071 if (is_array($comment['replies'])) { // and the replies 1072 foreach ($comment['replies'] as $rid) { 1073 $text .= $this->_addCommentWords($rid, $data, $cid); 1074 } 1075 } 1076 return ' '.$text; 1077 } 1078} 1079// vim:ts=4:sw=4:et:enc=utf-8: 1080