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