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