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