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