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-22', 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 = ($data['title'] ? hsc($data['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; 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']) && (!auth_ismanager())) 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; 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 (auth_ismanager()) $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 416 if ($visible){ 417 ptln('<div class="comment_buttons">', 6); 418 419 // show reply button? 420 if (($data['status'] == 1) && !$reply && $comment['show'] 421 && ($this->getConf('allowguests') || $_SERVER['REMOTE_USER'])) 422 $this->_button($cid, $this->getLang('btn_reply'), 'reply', true); 423 424 // show edit, show/hide and delete button? 425 if ((($user == $_SERVER['REMOTE_USER']) && ($user != '')) || (auth_ismanager())){ 426 $this->_button($cid, $lang['btn_secedit'], 'edit', true); 427 $label = ($comment['show'] ? $this->getLang('btn_hide') : $this->getLang('btn_show')); 428 $this->_button($cid, $label, 'toogle'); 429 $this->_button($cid, $lang['btn_delete'], 'delete'); 430 } 431 ptln('</div>', 6); // class="comment_buttons" 432 } 433 ptln('</div>', 4); // class="hentry" 434 435 // replies to this comment entry? 436 if (count($comment['replies'])){ 437 ptln('<div class="comment_replies"'.$style.'>', 4); 438 $visible = ($comment['show'] && $visible); 439 foreach ($comment['replies'] as $rid){ 440 $this->_print($rid, $data, $cid, $reply, $visible); 441 } 442 ptln('</div>', 4); // class="comment_replies" 443 } 444 445 // reply form 446 if ($reply == $cid){ 447 ptln('<div class="comment_replies">', 4); 448 $this->_form('', 'add', $cid); 449 ptln('</div>', 4); // class="comment_replies" 450 } 451 } 452 453 /** 454 * Outputs the comment form 455 */ 456 function _form($raw = '', $act = 'add', $cid = NULL){ 457 global $lang, $conf, $ID, $INFO; 458 459 // not for unregistered users when guest comments aren't allowed 460 if (!$_SERVER['REMOTE_USER'] && !$this->getConf('allowguests')) return false; 461 462 // fill $raw with $_REQUEST['text'] if it's empty (for failed CAPTCHA check) 463 if (!$raw && ($_REQUEST['comment'] == 'show')) $raw = $_REQUEST['text']; 464 465 ?> 466 467 468 <div class="comment_form"> 469 <form id="discussion__comment_form" method="post" action="<?php echo script() ?>" accept-charset="<?php echo $lang['encoding'] ?>" onsubmit="return validate(this);"> 470 <div class="no"> 471 <input type="hidden" name="id" value="<?php echo $ID ?>" /> 472 <input type="hidden" name="do" value="show" /> 473 <input type="hidden" name="comment" value="<?php echo $act ?>" /> 474 <?php 475 476 // for adding a comment 477 if ($act == 'add'){ 478 ?> 479 <input type="hidden" name="reply" value="<?php echo $cid ?>" /> 480 <?php 481 // for registered user (and we're not in admin import mode) 482 if ($conf['useacl'] && $_SERVER['REMOTE_USER'] 483 && (!($this->getConf('adminimport') && (auth_ismanager())))){ 484 ?> 485 <input type="hidden" name="user" value="<?php echo hsc($_SERVER['REMOTE_USER']) ?>" /> 486 <input type="hidden" name="name" value="<?php echo hsc($INFO['userinfo']['name']) ?>" /> 487 <input type="hidden" name="mail" value="<?php echo hsc($INFO['userinfo']['mail']) ?>" /> 488 <?php 489 // for guest: show name and e-mail entry fields 490 } else { 491 ?> 492 <input type="hidden" name="user" value="<?php echo clientIP() ?>" /> 493 <div class="comment_name"> 494 <label class="block" for="discussion__comment_name"> 495 <span><?php echo $lang['fullname'] ?>:</span> 496 <input type="text" class="edit" name="name" id="discussion__comment_name" size="50" tabindex="1" value="<?php echo hsc($_REQUEST['name'])?>" /> 497 </label> 498 </div> 499 <div class="comment_mail"> 500 <label class="block" for="discussion__comment_mail"> 501 <span><?php echo $lang['email'] ?>:</span> 502 <input type="text" class="edit" name="mail" id="discussion__comment_mail" size="50" tabindex="2" value="<?php echo hsc($_REQUEST['mail'])?>" /> 503 </label> 504 </div> 505 <?php 506 } 507 508 // allow entering an URL 509 if ($this->getConf('urlfield')){ 510 ?> 511 <div class="comment_url"> 512 <label class="block" for="discussion__comment_url"> 513 <span><?php echo $this->getLang('url') ?>:</span> 514 <input type="text" class="edit" name="url" id="discussion__comment_url" size="50" tabindex="3" value="<?php echo hsc($_REQUEST['url'])?>" /> 515 </label> 516 </div> 517 <?php 518 } 519 520 // allow entering an address 521 if ($this->getConf('addressfield')){ 522 ?> 523 <div class="comment_address"> 524 <label class="block" for="discussion__comment_address"> 525 <span><?php echo $this->getLang('address') ?>:</span> 526 <input type="text" class="edit" name="address" id="discussion__comment_address" size="50" tabindex="4" value="<?php echo hsc($_REQUEST['address'])?>" /> 527 </label> 528 </div> 529 <?php 530 } 531 532 // allow setting the comment date 533 if ($this->getConf('adminimport') && (auth_ismanager())){ 534 ?> 535 <div class="comment_date"> 536 <label class="block" for="discussion__comment_date"> 537 <span><?php echo $this->getLang('date') ?>:</span> 538 <input type="text" class="edit" name="date" id="discussion__comment_date" size="50" /> 539 </label> 540 </div> 541 <?php 542 } 543 544 // for saving a comment 545 } else { 546 ?> 547 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 548 <?php 549 } 550 ?> 551 <div class="comment_text"> 552 <?php 553 echo $this->getLang('entercomment'); 554 if ($this->getConf('wikisyntaxok')) echo ' ('.$this->getLang('wikisyntax').')'; 555 echo ':<br />'.DOKU_LF; 556 ?> 557 <textarea class="edit" name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php echo formText($raw) ?></textarea> 558 </div> 559 <?php //bad and dirty event insert hook 560 $evdata = array('writable' => true); 561 trigger_event('HTML_EDITFORM_INJECTION', $evdata); 562 ?> 563 <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save'] ?>" tabindex="6" /> 564 </div> 565 </form> 566 </div> 567 <?php 568 if ($this->getConf('usecocomment')) echo $this->_coComment(); 569 } 570 571 /** 572 * Adds a javascript to interact with coComments 573 */ 574 function _coComment(){ 575 global $ID, $conf, $INFO; 576 577 $user = $_SERVER['REMOTE_USER']; 578 579 ?> 580 <script type="text/javascript"><!--//--><![CDATA[//><!-- 581 var blogTool = "DokuWiki"; 582 var blogURL = "<?php echo DOKU_URL ?>"; 583 var blogTitle = "<?php echo $conf['title'] ?>"; 584 var postURL = "<?php echo wl($ID, '', true) ?>"; 585 var postTitle = "<?php echo tpl_pagetitle($ID, true) ?>"; 586 <?php 587 if ($user){ 588 ?> 589 var commentAuthor = "<?php echo $INFO['userinfo']['name'] ?>"; 590 <?php 591 } else { 592 ?> 593 var commentAuthorFieldName = "name"; 594 <?php 595 } 596 ?> 597 var commentAuthorLoggedIn = <?php echo ($user ? 'true' : 'false') ?>; 598 var commentFormID = "discussion__comment_form"; 599 var commentTextFieldName = "text"; 600 var commentButtonName = "submit"; 601 var cocomment_force = false; 602 //--><!]]></script> 603 <script type="text/javascript" src="http://www.cocomment.com/js/cocomment.js"> 604 </script> 605 <?php 606 } 607 608 /** 609 * General button function 610 */ 611 function _button($cid, $label, $act, $jump = false){ 612 global $ID; 613 614 $anchor = ($jump ? '#discussion__comment_form' : '' ); 615 616 ?> 617 <form class="button" method="post" action="<?php echo script().$anchor ?>"> 618 <div class="no"> 619 <input type="hidden" name="id" value="<?php echo $ID ?>" /> 620 <input type="hidden" name="do" value="show" /> 621 <input type="hidden" name="comment" value="<?php echo $act ?>" /> 622 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 623 <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>" /> 624 </div> 625 </form> 626 <?php 627 return true; 628 } 629 630 /** 631 * Adds an entry to the comments changelog 632 * 633 * @author Esther Brunner <wikidesign@gmail.com> 634 * @author Ben Coburn <btcoburn@silicodon.net> 635 */ 636 function _addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = ''){ 637 global $conf; 638 639 $changelog = $conf['metadir'].'/_comments.changes'; 640 641 if(!$date) $date = time(); //use current time if none supplied 642 $remote = $_SERVER['REMOTE_ADDR']; 643 $user = $_SERVER['REMOTE_USER']; 644 645 $strip = array("\t", "\n"); 646 $logline = array( 647 'date' => $date, 648 'ip' => $remote, 649 'type' => str_replace($strip, '', $type), 650 'id' => $id, 651 'user' => $user, 652 'sum' => str_replace($strip, '', $summary), 653 'extra' => str_replace($strip, '', $extra) 654 ); 655 656 // add changelog line 657 $logline = implode("\t", $logline)."\n"; 658 io_saveFile($changelog, $logline, true); //global changelog cache 659 io_saveFile($conf['metadir'].'/_dokuwiki.changes', $logline, true); 660 $this->_trimRecentCommentsLog($changelog); 661 662 // tell the indexer to re-index the page 663 @unlink(metaFN($id, '.indexed')); 664 } 665 666 /** 667 * Trims the recent comments cache to the last $conf['changes_days'] recent 668 * changes or $conf['recent'] items, which ever is larger. 669 * The trimming is only done once a day. 670 * 671 * @author Ben Coburn <btcoburn@silicodon.net> 672 */ 673 function _trimRecentCommentsLog($changelog){ 674 global $conf; 675 676 if (@file_exists($changelog) && 677 (filectime($changelog) + 86400) < time() && 678 !@file_exists($changelog.'_tmp')){ 679 680 io_lock($changelog); 681 $lines = file($changelog); 682 if (count($lines)<$conf['recent']) { 683 // nothing to trim 684 io_unlock($changelog); 685 return true; 686 } 687 688 io_saveFile($changelog.'_tmp', ''); // presave tmp as 2nd lock 689 $trim_time = time() - $conf['recent_days']*86400; 690 $out_lines = array(); 691 692 for ($i=0; $i<count($lines); $i++) { 693 $log = parseChangelogLine($lines[$i]); 694 if ($log === false) continue; // discard junk 695 if ($log['date'] < $trim_time) { 696 $old_lines[$log['date'].".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) 697 } else { 698 $out_lines[$log['date'].".$i"] = $lines[$i]; // definitely keep these lines 699 } 700 } 701 702 // sort the final result, it shouldn't be necessary, 703 // however the extra robustness in making the changelog cache self-correcting is worth it 704 ksort($out_lines); 705 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 706 if ($extra > 0) { 707 ksort($old_lines); 708 $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines); 709 } 710 711 // save trimmed changelog 712 io_saveFile($changelog.'_tmp', implode('', $out_lines)); 713 @unlink($changelog); 714 if (!rename($changelog.'_tmp', $changelog)) { 715 // rename failed so try another way... 716 io_unlock($changelog); 717 io_saveFile($changelog, implode('', $out_lines)); 718 @unlink($changelog.'_tmp'); 719 } else { 720 io_unlock($changelog); 721 } 722 return true; 723 } 724 } 725 726 /** 727 * Sends a notify mail on new comment 728 * 729 * @param array $comment data array of the new comment 730 * 731 * @author Andreas Gohr <andi@splitbrain.org> 732 * @author Esther Brunner <wikidesign@gmail.com> 733 */ 734 function _notify($comment){ 735 global $conf; 736 global $ID; 737 738 if ((!$conf['subscribers']) && (!$conf['notify'])) return; //subscribers enabled? 739 $bcc = subscriber_addresslist($ID); 740 if ((empty($bcc)) && (!$conf['notify'])) return; 741 $to = $conf['notify']; 742 $text = io_readFile($this->localFN('subscribermail')); 743 744 $search = array( 745 '@PAGE@', 746 '@TITLE@', 747 '@DATE@', 748 '@NAME@', 749 '@TEXT@', 750 '@UNSUBSCRIBE@', 751 '@DOKUWIKIURL@', 752 ); 753 $replace = array( 754 $ID, 755 $conf['title'], 756 date($conf['dformat'], $comment['date']['created']), 757 $comment['user']['name'], 758 $comment['raw'], 759 wl($ID, 'do=unsubscribe', true, '&'), 760 DOKU_URL, 761 ); 762 $text = str_replace($search, $replace, $text); 763 764 $subject = '['.$conf['title'].'] '.$this->getLang('mail_newcomment'); 765 766 mail_send($to, $subject, $text, $conf['mailfrom'], '', $bcc); 767 } 768 769 /** 770 * Counts the number of visible comments 771 */ 772 function _count($data){ 773 $number = 0; 774 foreach ($data['comments'] as $cid => $comment){ 775 if ($comment['parent']) continue; 776 if (!$comment['show']) continue; 777 $number++; 778 $rids = $comment['replies']; 779 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 780 } 781 return $number; 782 } 783 784 function _countReplies(&$data, $rids){ 785 $number = 0; 786 foreach ($rids as $rid){ 787 if (!isset($data['comments'][$rid])) continue; // reply was removed 788 if (!$data['comments'][$rid]['show']) continue; 789 $number++; 790 $rids = $data['comments'][$rid]['replies']; 791 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 792 } 793 return $number; 794 } 795 796 /** 797 * Renders the comment text 798 */ 799 function _render($raw){ 800 if ($this->getConf('wikisyntaxok')){ 801 $xhtml = $this->render($raw); 802 } else { // wiki syntax not allowed -> just encode special chars 803 $xhtml = htmlspecialchars(trim($raw)); 804 } 805 return $xhtml; 806 } 807 808 /** 809 * Adds a TOC item for the discussion section 810 */ 811 function add_toc_item(&$event, $param){ 812 if ($event->data[0] != 'xhtml') return; // nothing to do for us 813 if (!$this->_hasDiscussion($title)) return; // no discussion section 814 815 $pattern = '/<div id="toc__inside">(.*?)<\/div>\s<\/div>/s'; 816 if (!preg_match($pattern, $event->data[1], $match)) return; // no TOC on this page 817 818 // ok, then let's do it! 819 global $conf; 820 821 if (!$title) $title = $this->getLang('discussion'); 822 $section = '#discussion__section'; 823 $level = 3 - $conf['toptoclevel']; 824 825 $item = '<li class="level'.$level.'">'.DOKU_LF. 826 DOKU_TAB.'<div class="li">'.DOKU_LF. 827 DOKU_TAB.DOKU_TAB.'<span class="li"><a href="'.$section.'" class="toc">'.DOKU_LF. 828 DOKU_TAB.DOKU_TAB.DOKU_TAB.$title.DOKU_LF. 829 DOKU_TAB.DOKU_TAB.'</a></span>'.DOKU_LF. 830 DOKU_TAB.'</div>'.DOKU_LF. 831 '</li>'.DOKU_LF; 832 833 if ($level == 1) $search = "</ul>\n</div>"; 834 else $search = "</ul>\n</li></ul>\n</div>"; 835 836 $new = str_replace($search, $item.$search, $match[0]); 837 $event->data[1] = preg_replace($pattern, $new, $event->data[1]); 838 } 839 840 /** 841 * Finds out whether there is a discussion section for the current page 842 */ 843 function _hasDiscussion(&$title){ 844 global $ID; 845 846 $classes = get_declared_classes(); 847 if (in_array('helper_plugin_include', $classes)) return false; 848 849 $cfile = metaFN($ID, '.comments'); 850 851 if (!@file_exists($cfile)){ 852 if ($this->getConf('automatic')) return true; 853 else return false; 854 } 855 856 $comments = unserialize(io_readFile($cfile, false)); 857 858 if ($comments['title']) $title = hsc($comments['title']); 859 $num = $comments['number']; 860 if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return false; 861 else return true; 862 } 863 864 /** 865 * Checks if 'newthread' was given as action or the comment form was submitted 866 */ 867 function handle_act_preprocess(&$event, $param){ 868 if ($event->data == 'newthread'){ 869 // we can handle it -> prevent others 870 // $event->stopPropagation(); 871 $event->preventDefault(); 872 873 $event->data = $this->_newThread(); 874 } 875 if ((in_array($_REQUEST['comment'], array('add', 'save'))) 876 && (@file_exists(DOKU_PLUGIN.'captcha/action.php'))){ 877 $this->_captchaCheck(); 878 } 879 } 880 881 /** 882 * Creates a new thread page 883 */ 884 function _newThread(){ 885 global $ID, $INFO; 886 887 $ns = cleanID($_REQUEST['ns']); 888 $title = str_replace(':', '', $_REQUEST['title']); 889 $back = $ID; 890 $ID = ($ns ? $ns.':' : '').cleanID($title); 891 $INFO = pageinfo(); 892 893 // check if we are allowed to create this file 894 if ($INFO['perm'] >= AUTH_CREATE){ 895 896 //check if locked by anyone - if not lock for my self 897 if ($INFO['locked']) return 'locked'; 898 else lock($ID); 899 900 // prepare the new thread file with default stuff 901 if (!@file_exists($INFO['filepath'])){ 902 global $TEXT; 903 904 $TEXT = pageTemplate(array(($ns ? $ns.':' : '').$title)); 905 if (!$TEXT){ 906 $data = array('id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back); 907 $TEXT = $this->_pageTemplate($data); 908 } 909 return 'preview'; 910 } else { 911 return 'edit'; 912 } 913 } else { 914 return 'show'; 915 } 916 } 917 918 /** 919 * Adapted version of pageTemplate() function 920 */ 921 function _pageTemplate($data){ 922 global $conf, $INFO; 923 924 $id = $data['id']; 925 $user = $_SERVER['REMOTE_USER']; 926 $tpl = io_readFile(DOKU_PLUGIN.'discussion/_template.txt'); 927 928 // standard replacements 929 $replace = array( 930 '@ID@' => $id, 931 '@NS@' => $data['ns'], 932 '@PAGE@' => strtr(noNS($id),'_',' '), 933 '@USER@' => $user, 934 '@NAME@' => $INFO['userinfo']['name'], 935 '@MAIL@' => $INFO['userinfo']['mail'], 936 '@DATE@' => date($conf['dformat']), 937 ); 938 939 // additional replacements 940 $replace['@BACK@'] = $data['back']; 941 $replace['@TITLE@'] = $data['title']; 942 943 // avatar if useavatar and avatar plugin available 944 if ($this->getConf('useavatar') 945 && (@file_exists(DOKU_PLUGIN.'avatar/syntax.php')) 946 && (!plugin_isdisabled('avatar'))){ 947 $replace['@AVATAR@'] = '{{avatar>'.$user.' }} '; 948 } else { 949 $replace['@AVATAR@'] = ''; 950 } 951 952 // tag if tag plugin is available 953 if ((@file_exists(DOKU_PLUGIN.'tag/syntax/tag.php')) 954 && (!plugin_isdisabled('tag'))){ 955 $replace['@TAG@'] = "\n\n{{tag>}}"; 956 } else { 957 $replace['@TAG@'] = ''; 958 } 959 960 // do the replace 961 $tpl = str_replace(array_keys($replace), array_values($replace), $tpl); 962 return $tpl; 963 } 964 965 /** 966 * Checks if the CAPTCHA string submitted is valid 967 * 968 * @author Andreas Gohr <gohr@cosmocode.de> 969 * @adaption Esther Brunner <wikidesign@gmail.com> 970 */ 971 function _captchaCheck(){ 972 if (@file_exists(DOKU_PLUGIN.'captcha/disabled')) return; // CAPTCHA is disabled 973 974 require_once(DOKU_PLUGIN.'captcha/action.php'); 975 $captcha = new action_plugin_captcha; 976 977 // do nothing if logged in user and no CAPTCHA required 978 if (!$captcha->getConf('forusers') && $_SERVER['REMOTE_USER']) return; 979 980 // compare provided string with decrypted captcha 981 $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'], auth_cookiesalt()); 982 $code = $captcha->_generateCAPTCHA($captcha->_fixedIdent(), $rand); 983 984 if (!$_REQUEST['plugin__captcha_secret'] || 985 !$_REQUEST['plugin__captcha'] || 986 strtoupper($_REQUEST['plugin__captcha']) != $code){ 987 988 // CAPTCHA test failed! Continue to edit instead of saving 989 msg($captcha->getLang('testfailed'), -1); 990 if ($_REQUEST['comment'] == 'save') $_REQUEST['comment'] = 'edit'; 991 elseif ($_REQUEST['comment'] == 'add') $_REQUEST['comment'] = 'show'; 992 } 993 // if we arrive here it was a valid save 994 } 995 996 /** 997 * Adds the comments to the index 998 */ 999 function idx_add_discussion(&$event, $param){ 1000 1001 // get .comments meta file name 1002 $file = metaFN($event->data[0], '.comments'); 1003 1004 if (@file_exists($file)) $data = unserialize(io_readFile($file, false)); 1005 if ((!$data['status']) || ($data['number'] == 0)) return; // comments are turned off 1006 1007 // now add the comments 1008 if (isset($data['comments'])){ 1009 foreach ($data['comments'] as $key => $value){ 1010 $event->data[1] .= $this->_addCommentWords($key, $data); 1011 } 1012 } 1013 } 1014 1015 /** 1016 * Adds the words of a given comment to the index 1017 */ 1018 function _addCommentWords($cid, &$data, $parent = ''){ 1019 1020 if (!isset($data['comments'][$cid])) return ''; // comment was removed 1021 $comment = $data['comments'][$cid]; 1022 1023 if (!is_array($comment)) return ''; // corrupt datatype 1024 if ($comment['parent'] != $parent) return ''; // reply to an other comment 1025 if (!$comment['show']) return ''; // hidden comment 1026 1027 $text = $comment['raw']; // we only add the raw comment text 1028 if (is_array($comment['replies'])){ // and the replies 1029 foreach ($comment['replies'] as $rid){ 1030 $text .= $this->_addCommentWords($rid, $data, $cid); 1031 } 1032 } 1033 return ' '.$text; 1034 } 1035 1036} 1037 1038//Setup VIM: ex: et ts=4 enc=utf-8 :