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