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