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' => (isset($_SERVER['REMOTE_USER'])) ? $_SERVER['REMOTE_USER'] : hsc($_REQUEST['user']), 110 'name' => (isset($_SERVER['REMOTE_USER'])) ? $INFO['userinfo']['name'] : hsc($_REQUEST['name']), 111 'mail' => (isset($_SERVER['REMOTE_USER'])) ? $INFO['userinfo']['mail'] : hsc($_REQUEST['mail']), 112 'url' => ($this->getConf('urlfield')) ? hsc($_REQUEST['url']) : '', 113 'address' => hsc($_REQUEST['address'])), 114 'subscribe' => ($this->getConf('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 guest/adminimport: show name, e-mail and subscribe to comments fields 670 if(!$_SERVER['REMOTE_USER'] or ($this->getConf('adminimport') && auth_ismanager())) { 671 ?> 672 <input type="hidden" name="user" value="<?php echo clientIP() ?>" /> 673 <div class="comment_name"> 674 <label class="block" for="discussion__comment_name"> 675 <span><?php echo $lang['fullname'] ?>:</span> 676 <input type="text" class="edit" name="name" id="discussion__comment_name" size="50" tabindex="1" value="<?php echo hsc($_REQUEST['name'])?>" /> 677 </label> 678 </div> 679 <div class="comment_mail"> 680 <label class="block" for="discussion__comment_mail"> 681 <span><?php echo $lang['email'] ?>:</span> 682 <input type="text" class="edit" name="mail" id="discussion__comment_mail" size="50" tabindex="2" value="<?php echo hsc($_REQUEST['mail'])?>" /> 683 </label> 684 </div> 685 <?php 686 } 687 688 // allow entering an URL 689 if ($this->getConf('urlfield')) { 690 ?> 691 <div class="comment_url"> 692 <label class="block" for="discussion__comment_url"> 693 <span><?php echo $this->getLang('url') ?>:</span> 694 <input type="text" class="edit" name="url" id="discussion__comment_url" size="50" tabindex="3" value="<?php echo hsc($_REQUEST['url'])?>" /> 695 </label> 696 </div> 697 <?php 698 } 699 700 // allow entering an address 701 if ($this->getConf('addressfield')) { 702 ?> 703 <div class="comment_address"> 704 <label class="block" for="discussion__comment_address"> 705 <span><?php echo $this->getLang('address') ?>:</span> 706 <input type="text" class="edit" name="address" id="discussion__comment_address" size="50" tabindex="4" value="<?php echo hsc($_REQUEST['address'])?>" /> 707 </label> 708 </div> 709 <?php 710 } 711 712 // allow setting the comment date 713 if ($this->getConf('adminimport') && (auth_ismanager())) { 714 ?> 715 <div class="comment_date"> 716 <label class="block" for="discussion__comment_date"> 717 <span><?php echo $this->getLang('date') ?>:</span> 718 <input type="text" class="edit" name="date" id="discussion__comment_date" size="50" /> 719 </label> 720 </div> 721 <?php 722 } 723 724 // for saving a comment 725 } else { 726 ?> 727 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 728 <?php 729 } 730 ?> 731 <div class="comment_text"> 732 <?php 733 echo $this->getLang('entercomment'); 734 if ($this->getConf('wikisyntaxok')) echo ' ('.$this->getLang('wikisyntax').')'; 735 echo ':<br />'.DOKU_LF; 736 ?> 737 <textarea class="edit" name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php echo formText($raw) ?></textarea> 738 </div> 739 <?php //bad and dirty event insert hook 740 $evdata = array('writable' => true); 741 trigger_event('HTML_EDITFORM_INJECTION', $evdata); 742 ?> 743 <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" /> 744 745 <?php if(!$_SERVER['REMOTE_USER'] && $this->getConf('subscribe')) { ?> 746 <div class="comment_subscribe"> 747 <input type="checkbox" id="discussion__comment_subscribe" name="subscribe" tabindex="6" /> 748 <label class="block" for="discussion__comment_subscribe"> 749 <span><?php echo $this->getLang('subscribe') ?></span> 750 </label> 751 </div> 752 <?php } ?> 753 754 <div class="clearer"></div> 755 756 </div> 757 </form> 758 </div> 759 <?php 760 if ($this->getConf('usecocomment')) echo $this->_coComment(); 761 } 762 763 /** 764 * Adds a javascript to interact with coComments 765 */ 766 function _coComment() { 767 global $ID; 768 global $conf; 769 global $INFO; 770 771 $user = $_SERVER['REMOTE_USER']; 772 773 ?> 774 <script type="text/javascript"><!--//--><![CDATA[//><!-- 775 var blogTool = "DokuWiki"; 776 var blogURL = "<?php echo DOKU_URL ?>"; 777 var blogTitle = "<?php echo $conf['title'] ?>"; 778 var postURL = "<?php echo wl($ID, '', true) ?>"; 779 var postTitle = "<?php echo tpl_pagetitle($ID, true) ?>"; 780 <?php 781 if ($user) { 782 ?> 783 var commentAuthor = "<?php echo $INFO['userinfo']['name'] ?>"; 784 <?php 785 } else { 786 ?> 787 var commentAuthorFieldName = "name"; 788 <?php 789 } 790 ?> 791 var commentAuthorLoggedIn = <?php echo ($user ? 'true' : 'false') ?>; 792 var commentFormID = "discussion__comment_form"; 793 var commentTextFieldName = "text"; 794 var commentButtonName = "submit"; 795 var cocomment_force = false; 796 //--><!]]></script> 797 <script type="text/javascript" src="http://www.cocomment.com/js/cocomment.js"> 798 </script> 799 <?php 800 } 801 802 /** 803 * General button function 804 */ 805 function _button($cid, $label, $act, $jump = false) { 806 global $ID; 807 808 $anchor = ($jump ? '#discussion__comment_form' : '' ); 809 810 ?> 811 <form class="button discussion__<?php echo $act?>" method="get" action="<?php echo script().$anchor ?>"> 812 <div class="no"> 813 <input type="hidden" name="id" value="<?php echo $ID ?>" /> 814 <input type="hidden" name="do" value="show" /> 815 <input type="hidden" name="comment" value="<?php echo $act ?>" /> 816 <input type="hidden" name="cid" value="<?php echo $cid ?>" /> 817 <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>" /> 818 </div> 819 </form> 820 <?php 821 return true; 822 } 823 824 /** 825 * Adds an entry to the comments changelog 826 * 827 * @author Esther Brunner <wikidesign@gmail.com> 828 * @author Ben Coburn <btcoburn@silicodon.net> 829 */ 830 function _addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = '') { 831 global $conf; 832 833 $changelog = $conf['metadir'].'/_comments.changes'; 834 835 if(!$date) $date = time(); //use current time if none supplied 836 $remote = $_SERVER['REMOTE_ADDR']; 837 $user = $_SERVER['REMOTE_USER']; 838 839 $strip = array("\t", "\n"); 840 $logline = array( 841 'date' => $date, 842 'ip' => $remote, 843 'type' => str_replace($strip, '', $type), 844 'id' => $id, 845 'user' => $user, 846 'sum' => str_replace($strip, '', $summary), 847 'extra' => str_replace($strip, '', $extra) 848 ); 849 850 // add changelog line 851 $logline = implode("\t", $logline)."\n"; 852 io_saveFile($changelog, $logline, true); //global changelog cache 853 $this->_trimRecentCommentsLog($changelog); 854 855 // tell the indexer to re-index the page 856 @unlink(metaFN($id, '.indexed')); 857 } 858 859 /** 860 * Trims the recent comments cache to the last $conf['changes_days'] recent 861 * changes or $conf['recent'] items, which ever is larger. 862 * The trimming is only done once a day. 863 * 864 * @author Ben Coburn <btcoburn@silicodon.net> 865 */ 866 function _trimRecentCommentsLog($changelog) { 867 global $conf; 868 869 if (@file_exists($changelog) && 870 (filectime($changelog) + 86400) < time() && 871 !@file_exists($changelog.'_tmp')) { 872 873 io_lock($changelog); 874 $lines = file($changelog); 875 if (count($lines)<$conf['recent']) { 876 // nothing to trim 877 io_unlock($changelog); 878 return true; 879 } 880 881 io_saveFile($changelog.'_tmp', ''); // presave tmp as 2nd lock 882 $trim_time = time() - $conf['recent_days']*86400; 883 $out_lines = array(); 884 885 $num = count($lines); 886 for ($i=0; $i<$num; $i++) { 887 $log = parseChangelogLine($lines[$i]); 888 if ($log === false) continue; // discard junk 889 if ($log['date'] < $trim_time) { 890 $old_lines[$log['date'].".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) 891 } else { 892 $out_lines[$log['date'].".$i"] = $lines[$i]; // definitely keep these lines 893 } 894 } 895 896 // sort the final result, it shouldn't be necessary, 897 // however the extra robustness in making the changelog cache self-correcting is worth it 898 ksort($out_lines); 899 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 900 if ($extra > 0) { 901 ksort($old_lines); 902 $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines); 903 } 904 905 // save trimmed changelog 906 io_saveFile($changelog.'_tmp', implode('', $out_lines)); 907 @unlink($changelog); 908 if (!rename($changelog.'_tmp', $changelog)) { 909 // rename failed so try another way... 910 io_unlock($changelog); 911 io_saveFile($changelog, implode('', $out_lines)); 912 @unlink($changelog.'_tmp'); 913 } else { 914 io_unlock($changelog); 915 } 916 return true; 917 } 918 } 919 920 /** 921 * Sends a notify mail on new comment 922 * 923 * @param array $comment data array of the new comment 924 * 925 * @author Andreas Gohr <andi@splitbrain.org> 926 * @author Esther Brunner <wikidesign@gmail.com> 927 */ 928 function _notify($comment, $subscribers) { 929 global $conf; 930 global $ID; 931 932 $text = io_readfile($this->localfn('subscribermail')); 933 $subject = '['.$conf['title'].'] '.$this->getLang('mail_newcomment'); 934 935 $search = array( 936 '@PAGE@', 937 '@TITLE@', 938 '@DATE@', 939 '@NAME@', 940 '@TEXT@', 941 '@COMMENTURL@', 942 '@UNSUBSCRIBE@', 943 '@DOKUWIKIURL@', 944 ); 945 946 // notify page subscribers 947 if (($conf['subscribers']) && ($conf['notify'])) { 948 $bcc = subscriber_addresslist($ID); 949 $to = $conf['notify']; 950 951 $replace = array( 952 $ID, 953 $conf['title'], 954 strftime($conf['dformat'], $comment['date']['created']), 955 $comment['user']['name'], 956 $comment['raw'], 957 wl($ID, '', true) . '#comment_' . $comment['cid'], 958 wl($ID, 'do=unsubscribe', true, '&'), 959 DOKU_URL, 960 ); 961 962 $body = str_replace($search, $replace, $text); 963 mail_send($to, $subject, $body, $conf['mailfrom'], '', $bcc); 964 } 965 966 // notify comment subscribers 967 if (!empty($subscribers)) { 968 969 foreach($subscribers as $mail => $hash) { 970 $to = $mail; 971 972 $replace = array( 973 $ID, 974 $conf['title'], 975 strftime($conf['dformat'], $comment['date']['created']), 976 $comment['user']['name'], 977 $comment['raw'], 978 wl($ID, '', true) . '#comment_' . $comment['cid'], 979 wl($ID, 'do=unsubscribe&hash=' . $hash, true, '&'), 980 DOKU_URL, 981 ); 982 983 $body = str_replace($search, $replace, $text); 984 mail_send($to, $subject, $body, $conf['mailfrom']); 985 } 986 } 987 } 988 989 /** 990 * Counts the number of visible comments 991 */ 992 function _count($data) { 993 $number = 0; 994 foreach ($data['comments'] as $cid => $comment) { 995 if ($comment['parent']) continue; 996 if (!$comment['show']) continue; 997 $number++; 998 $rids = $comment['replies']; 999 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 1000 } 1001 return $number; 1002 } 1003 1004 function _countReplies(&$data, $rids) { 1005 $number = 0; 1006 foreach ($rids as $rid) { 1007 if (!isset($data['comments'][$rid])) continue; // reply was removed 1008 if (!$data['comments'][$rid]['show']) continue; 1009 $number++; 1010 $rids = $data['comments'][$rid]['replies']; 1011 if (count($rids)) $number = $number + $this->_countReplies($data, $rids); 1012 } 1013 return $number; 1014 } 1015 1016 /** 1017 * Renders the comment text 1018 */ 1019 function _render($raw) { 1020 if ($this->getConf('wikisyntaxok')) { 1021 $xhtml = $this->render($raw); 1022 } else { // wiki syntax not allowed -> just encode special chars 1023 $xhtml = htmlspecialchars(trim($raw)); 1024 } 1025 return $xhtml; 1026 } 1027 1028 /** 1029 * Finds out whether there is a discussion section for the current page 1030 */ 1031 function _hasDiscussion(&$title) { 1032 global $ID; 1033 1034 $cfile = metaFN($ID, '.comments'); 1035 1036 if (!@file_exists($cfile)) { 1037 if ($this->getConf('automatic')) { 1038 return true; 1039 } else { 1040 return false; 1041 } 1042 } 1043 1044 $comments = unserialize(io_readFile($cfile, false)); 1045 1046 if ($comments['title']) $title = hsc($comments['title']); 1047 $num = $comments['number']; 1048 if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return false; 1049 else return true; 1050 } 1051 1052 /** 1053 * Creates a new thread page 1054 */ 1055 function _newThread() { 1056 global $ID, $INFO; 1057 1058 $ns = cleanID($_REQUEST['ns']); 1059 $title = str_replace(':', '', $_REQUEST['title']); 1060 $back = $ID; 1061 $ID = ($ns ? $ns.':' : '').cleanID($title); 1062 $INFO = pageinfo(); 1063 1064 // check if we are allowed to create this file 1065 if ($INFO['perm'] >= AUTH_CREATE) { 1066 1067 //check if locked by anyone - if not lock for my self 1068 if ($INFO['locked']) return 'locked'; 1069 else lock($ID); 1070 1071 // prepare the new thread file with default stuff 1072 if (!@file_exists($INFO['filepath'])) { 1073 global $TEXT; 1074 1075 $TEXT = pageTemplate(array(($ns ? $ns.':' : '').$title)); 1076 if (!$TEXT) { 1077 $data = array('id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back); 1078 $TEXT = $this->_pageTemplate($data); 1079 } 1080 return 'preview'; 1081 } else { 1082 return 'edit'; 1083 } 1084 } else { 1085 return 'show'; 1086 } 1087 } 1088 1089 /** 1090 * Adapted version of pageTemplate() function 1091 */ 1092 function _pageTemplate($data) { 1093 global $conf, $INFO; 1094 1095 $id = $data['id']; 1096 $user = $_SERVER['REMOTE_USER']; 1097 $tpl = io_readFile(DOKU_PLUGIN.'discussion/_template.txt'); 1098 1099 // standard replacements 1100 $replace = array( 1101 '@NS@' => $data['ns'], 1102 '@PAGE@' => strtr(noNS($id),'_',' '), 1103 '@USER@' => $user, 1104 '@NAME@' => $INFO['userinfo']['name'], 1105 '@MAIL@' => $INFO['userinfo']['mail'], 1106 '@DATE@' => strftime($conf['dformat']), 1107 ); 1108 1109 // additional replacements 1110 $replace['@BACK@'] = $data['back']; 1111 $replace['@TITLE@'] = $data['title']; 1112 1113 // avatar if useavatar and avatar plugin available 1114 if ($this->getConf('useavatar') 1115 && (@file_exists(DOKU_PLUGIN.'avatar/syntax.php')) 1116 && (!plugin_isdisabled('avatar'))) { 1117 $replace['@AVATAR@'] = '{{avatar>'.$user.' }} '; 1118 } else { 1119 $replace['@AVATAR@'] = ''; 1120 } 1121 1122 // tag if tag plugin is available 1123 if ((@file_exists(DOKU_PLUGIN.'tag/syntax/tag.php')) 1124 && (!plugin_isdisabled('tag'))) { 1125 $replace['@TAG@'] = "\n\n{{tag>}}"; 1126 } else { 1127 $replace['@TAG@'] = ''; 1128 } 1129 1130 // do the replace 1131 $tpl = str_replace(array_keys($replace), array_values($replace), $tpl); 1132 return $tpl; 1133 } 1134 1135 /** 1136 * Checks if the CAPTCHA string submitted is valid 1137 * 1138 * @author Andreas Gohr <gohr@cosmocode.de> 1139 * @adaption Esther Brunner <wikidesign@gmail.com> 1140 */ 1141 function _captchaCheck() { 1142 if (plugin_isdisabled('captcha') || (!$captcha = plugin_load('helper', 'captcha'))) 1143 return; // CAPTCHA is disabled or not available 1144 1145 // do nothing if logged in user and no CAPTCHA required 1146 if (!$captcha->getConf('forusers') && $_SERVER['REMOTE_USER']) return; 1147 1148 // compare provided string with decrypted captcha 1149 $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'], auth_cookiesalt()); 1150 $code = $captcha->_generateCAPTCHA($captcha->_fixedIdent(), $rand); 1151 1152 if (!$_REQUEST['plugin__captcha_secret'] || 1153 !$_REQUEST['plugin__captcha'] || 1154 strtoupper($_REQUEST['plugin__captcha']) != $code) { 1155 1156 // CAPTCHA test failed! Continue to edit instead of saving 1157 msg($captcha->getLang('testfailed'), -1); 1158 if ($_REQUEST['comment'] == 'save') $_REQUEST['comment'] = 'edit'; 1159 elseif ($_REQUEST['comment'] == 'add') $_REQUEST['comment'] = 'show'; 1160 } 1161 // if we arrive here it was a valid save 1162 } 1163 1164 /** 1165 * Adds the comments to the index 1166 */ 1167 function idx_add_discussion(&$event, $param) { 1168 1169 // get .comments meta file name 1170 $file = metaFN($event->data[0], '.comments'); 1171 1172 if (@file_exists($file)) $data = unserialize(io_readFile($file, false)); 1173 if ((!$data['status']) || ($data['number'] == 0)) return; // comments are turned off 1174 1175 // now add the comments 1176 if (isset($data['comments'])) { 1177 foreach ($data['comments'] as $key => $value) { 1178 $event->data[1] .= $this->_addCommentWords($key, $data); 1179 } 1180 } 1181 } 1182 1183 /** 1184 * Adds the words of a given comment to the index 1185 */ 1186 function _addCommentWords($cid, &$data, $parent = '') { 1187 1188 if (!isset($data['comments'][$cid])) return ''; // comment was removed 1189 $comment = $data['comments'][$cid]; 1190 1191 if (!is_array($comment)) return ''; // corrupt datatype 1192 if ($comment['parent'] != $parent) return ''; // reply to an other comment 1193 if (!$comment['show']) return ''; // hidden comment 1194 1195 $text = $comment['raw']; // we only add the raw comment text 1196 if (is_array($comment['replies'])) { // and the replies 1197 foreach ($comment['replies'] as $rid) { 1198 $text .= $this->_addCommentWords($rid, $data, $cid); 1199 } 1200 } 1201 return ' '.$text; 1202 } 1203} 1204 1205function _sortCallback($a, $b) { 1206 if (is_array($a['date'])) { // new format 1207 $createdA = $a['date']['created']; 1208 } else { // old format 1209 $createdA = $a['date']; 1210 } 1211 1212 if (is_array($b['date'])) { // new format 1213 $createdB = $b['date']['created']; 1214 } else { // old format 1215 $createdB = $b['date']; 1216 } 1217 1218 if ($createdA == $createdB) 1219 return 0; 1220 else 1221 return ($createdA < $createdB) ? -1 : 1; 1222} 1223 1224// vim:ts=4:sw=4:et:enc=utf-8: 1225