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