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