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