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