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