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