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