xref: /plugin/discussion/action.php (revision 07c376bb4fb524ac7b8b99a9000fbe184f7add6d)
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-21',
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, $INFO;
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']) && ($INFO['perm'] != AUTH_ADMIN)) 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, $INFO;
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 ($INFO['perm'] == AUTH_ADMIN) $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 ($INFO['perm'] == AUTH_ADMIN){
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 != ''))
434        || ($INFO['perm'] == AUTH_ADMIN))
435        $this->_button($cid, $lang['btn_secedit'], 'edit', true);
436      if ($INFO['perm'] == AUTH_ADMIN)
437        $this->_button($cid, $lang['btn_delete'], 'delete');
438      ptln('</div>', 2); // class="comment_buttons"
439    }
440
441    // replies to this comment entry?
442    if (count($comment['replies'])){
443      ptln('<div class="comment_replies"'.$style.'>', 4);
444      $visible = ($comment['show'] && $visible);
445      foreach ($comment['replies'] as $rid){
446        $this->_print($rid, $data, $cid, $reply, $visible);
447      }
448      ptln('</div>', 4); // class="comment_replies"
449    }
450
451    // reply form
452    if ($reply == $cid){
453      ptln('<div class="comment_replies">', 4);
454      $this->_form('', 'add', $cid);
455      ptln('</div>', 4); // class="comment_replies"
456    }
457  }
458
459  /**
460   * Outputs the comment form
461   */
462  function _form($raw = '', $act = 'add', $cid = NULL){
463    global $lang, $conf, $ID, $INFO;
464
465    // not for unregistered users when guest comments aren't allowed
466    if (!$_SERVER['REMOTE_USER'] && !$this->getConf('allowguests')) return false;
467
468    // fill $raw with $_REQUEST['text'] if it's empty (for failed CAPTCHA check)
469    if (!$raw && ($_REQUEST['comment'] == 'show')) $raw = $_REQUEST['text'];
470
471    ?>
472
473
474    <div class="comment_form">
475      <form id="discussion__comment_form" method="post" action="<?php echo script() ?>" accept-charset="<?php echo $lang['encoding'] ?>" onsubmit="return validate(this);">
476        <div class="no">
477          <input type="hidden" name="id" value="<?php echo $ID ?>" />
478          <input type="hidden" name="do" value="show" />
479          <input type="hidden" name="comment" value="<?php echo $act ?>" />
480    <?php
481
482    // for adding a comment
483    if ($act == 'add'){
484      ?>
485          <input type="hidden" name="reply" value="<?php echo $cid ?>" />
486      <?php
487      // for registered user (and we're not in admin import mode)
488      if ($conf['useacl'] && $_SERVER['REMOTE_USER']
489        && (!($this->getConf('adminimport') && ($INFO['perm'] == AUTH_ADMIN)))){
490      ?>
491          <input type="hidden" name="user" value="<?php echo hsc($_SERVER['REMOTE_USER']) ?>" />
492          <input type="hidden" name="name" value="<?php echo hsc($INFO['userinfo']['name']) ?>" />
493          <input type="hidden" name="mail" value="<?php echo hsc($INFO['userinfo']['mail']) ?>" />
494      <?php
495      // for guest: show name and e-mail entry fields
496      } else {
497      ?>
498          <input type="hidden" name="user" value="<?php echo clientIP() ?>" />
499          <div class="comment_name">
500            <label class="block" for="discussion__comment_name">
501              <span><?php echo $lang['fullname'] ?>:</span>
502              <input type="text" class="edit" name="name" id="discussion__comment_name" size="50" tabindex="1" value="<?php echo hsc($_REQUEST['name'])?>" />
503            </label>
504          </div>
505          <div class="comment_mail">
506            <label class="block" for="discussion__comment_mail">
507              <span><?php echo $lang['email'] ?>:</span>
508              <input type="text" class="edit" name="mail" id="discussion__comment_mail" size="50" tabindex="2" value="<?php echo hsc($_REQUEST['mail'])?>" />
509            </label>
510          </div>
511      <?php
512      }
513
514      // allow entering an URL
515      if ($this->getConf('urlfield')){
516      ?>
517          <div class="comment_url">
518            <label class="block" for="discussion__comment_url">
519              <span><?php echo $this->getLang('url') ?>:</span>
520              <input type="text" class="edit" name="url" id="discussion__comment_url" size="50" tabindex="3" value="<?php echo hsc($_REQUEST['url'])?>" />
521            </label>
522          </div>
523      <?php
524      }
525
526      // allow entering an address
527      if ($this->getConf('addressfield')){
528      ?>
529          <div class="comment_address">
530            <label class="block" for="discussion__comment_address">
531              <span><?php echo $this->getLang('address') ?>:</span>
532              <input type="text" class="edit" name="address" id="discussion__comment_address" size="50" tabindex="4" value="<?php echo hsc($_REQUEST['address'])?>" />
533            </label>
534          </div>
535      <?php
536      }
537
538      // allow setting the comment date
539      if ($this->getConf('adminimport') && ($INFO['perm'] == AUTH_ADMIN)){
540      ?>
541          <div class="comment_date">
542            <label class="block" for="discussion__comment_date">
543              <span><?php echo $this->getLang('date') ?>:</span>
544              <input type="text" class="edit" name="date" id="discussion__comment_date" size="50" />
545            </label>
546          </div>
547      <?php
548      }
549
550    // for saving a comment
551    } else {
552    ?>
553          <input type="hidden" name="cid" value="<?php echo $cid ?>" />
554    <?php
555    }
556    ?>
557          <div class="comment_text">
558    <?php
559    echo $this->getLang('entercomment');
560    if ($this->getConf('wikisyntaxok')) echo ' ('.$this->getLang('wikisyntax').')';
561    echo ':<br />';
562    ?>
563            <textarea class="edit" name="text" cols="80" rows="10" id="discussion__comment_text" tabindex="5"><?php echo formText($raw) ?></textarea>
564          </div>
565    <?php //bad and dirty event insert hook
566    $evdata = array('writable' => true);
567    trigger_event('HTML_EDITFORM_INJECTION', $evdata);
568    ?>
569          <input class="button" type="submit" name="submit" value="<?php echo $lang['btn_save'] ?>" tabindex="6" />
570        </div>
571      </form>
572    </div>
573    <?php
574    if ($this->getConf('usecocomment')) echo $this->_coComment();
575  }
576
577  /**
578   * Adds a javascript to interact with coComments
579   */
580  function _coComment(){
581    global $ID, $conf, $INFO;
582
583    $user = $_SERVER['REMOTE_USER'];
584
585    ?>
586    <script type="text/javascript"><!--//--><![CDATA[//><!--
587      var blogTool  = "DokuWiki";
588      var blogURL   = "<?php echo DOKU_URL ?>";
589      var blogTitle = "<?php echo $conf['title'] ?>";
590      var postURL   = "<?php echo wl($ID, '', true) ?>";
591      var postTitle = "<?php echo tpl_pagetitle($ID, true) ?>";
592    <?php
593    if ($user){
594    ?>
595      var commentAuthor = "<?php echo $INFO['userinfo']['name'] ?>";
596    <?php
597    } else {
598    ?>
599      var commentAuthorFieldName = "name";
600    <?php
601    }
602    ?>
603      var commentAuthorLoggedIn = <?php echo ($user ? 'true' : 'false') ?>;
604      var commentFormID         = "discussion__comment_form";
605      var commentTextFieldName  = "text";
606      var commentButtonName     = "submit";
607      var cocomment_force       = false;
608    //--><!]]></script>
609    <script type="text/javascript" src="http://www.cocomment.com/js/cocomment.js">
610    </script>
611    <?php
612  }
613
614  /**
615   * General button function
616   */
617  function _button($cid, $label, $act, $jump = false){
618    global $ID;
619
620    $anchor = ($jump ? '#discussion__comment_form' : '' );
621
622    ?>
623    <form class="button" method="post" action="<?php echo script().$anchor ?>">
624      <div class="no">
625        <input type="hidden" name="id" value="<?php echo $ID ?>" />
626        <input type="hidden" name="do" value="show" />
627        <input type="hidden" name="comment" value="<?php echo $act ?>" />
628        <input type="hidden" name="cid" value="<?php echo $cid ?>" />
629        <input type="submit" value="<?php echo $label ?>" class="button" title="<?php echo $label ?>" />
630      </div>
631    </form>
632    <?php
633    return true;
634  }
635
636  /**
637   * Adds an entry to the comments changelog
638   *
639   * @author Esther Brunner <wikidesign@gmail.com>
640   * @author Ben Coburn <btcoburn@silicodon.net>
641   */
642  function _addLogEntry($date, $id, $type = 'cc', $summary = '', $extra = ''){
643    global $conf;
644
645    $changelog = $conf['metadir'].'/_comments.changes';
646
647    if(!$date) $date = time(); //use current time if none supplied
648    $remote = $_SERVER['REMOTE_ADDR'];
649    $user   = $_SERVER['REMOTE_USER'];
650
651    $strip = array("\t", "\n");
652    $logline = array(
653      'date'  => $date,
654      'ip'    => $remote,
655      'type'  => str_replace($strip, '', $type),
656      'id'    => $id,
657      'user'  => $user,
658      'sum'   => str_replace($strip, '', $summary),
659      'extra' => str_replace($strip, '', $extra)
660    );
661
662    // add changelog line
663    $logline = implode("\t", $logline)."\n";
664    io_saveFile($changelog, $logline, true); //global changelog cache
665    io_saveFile($conf['metadir'].'/_dokuwiki.changes', $logline, true);
666    $this->_trimRecentCommentsLog($changelog);
667
668    // tell the indexer to re-index the page
669    @unlink(metaFN($id, '.indexed'));
670  }
671
672  /**
673   * Trims the recent comments cache to the last $conf['changes_days'] recent
674   * changes or $conf['recent'] items, which ever is larger.
675   * The trimming is only done once a day.
676   *
677   * @author Ben Coburn <btcoburn@silicodon.net>
678   */
679  function _trimRecentCommentsLog($changelog){
680    global $conf;
681
682    if (@file_exists($changelog) &&
683      (filectime($changelog) + 86400) < time() &&
684      !@file_exists($changelog.'_tmp')){
685
686      io_lock($changelog);
687      $lines = file($changelog);
688      if (count($lines)<$conf['recent']) {
689          // nothing to trim
690          io_unlock($changelog);
691          return true;
692      }
693
694      io_saveFile($changelog.'_tmp', '');                  // presave tmp as 2nd lock
695      $trim_time = time() - $conf['recent_days']*86400;
696      $out_lines = array();
697
698      for ($i=0; $i<count($lines); $i++) {
699        $log = parseChangelogLine($lines[$i]);
700        if ($log === false) continue;                      // discard junk
701        if ($log['date'] < $trim_time) {
702          $old_lines[$log['date'].".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
703        } else {
704          $out_lines[$log['date'].".$i"] = $lines[$i];     // definitely keep these lines
705        }
706      }
707
708      // sort the final result, it shouldn't be necessary,
709      // however the extra robustness in making the changelog cache self-correcting is worth it
710      ksort($out_lines);
711      $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
712      if ($extra > 0) {
713        ksort($old_lines);
714        $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines);
715      }
716
717      // save trimmed changelog
718      io_saveFile($changelog.'_tmp', implode('', $out_lines));
719      @unlink($changelog);
720      if (!rename($changelog.'_tmp', $changelog)) {
721        // rename failed so try another way...
722        io_unlock($changelog);
723        io_saveFile($changelog, implode('', $out_lines));
724        @unlink($changelog.'_tmp');
725      } else {
726        io_unlock($changelog);
727      }
728      return true;
729    }
730  }
731
732  /**
733   * Sends a notify mail on new comment
734   *
735   * @param  array  $comment  data array of the new comment
736   *
737   * @author Andreas Gohr <andi@splitbrain.org>
738   * @author Esther Brunner <wikidesign@gmail.com>
739   */
740  function _notify($comment){
741    global $conf;
742    global $ID;
743
744    if ((!$conf['subscribers']) && (!$conf['notify'])) return; //subscribers enabled?
745    $bcc  = subscriber_addresslist($ID);
746    if ((empty($bcc)) && (!$conf['notify'])) return;
747    $to   = $conf['notify'];
748    $text = io_readFile($this->localFN('subscribermail'));
749
750    $search = array(
751      '@PAGE@',
752      '@TITLE@',
753      '@DATE@',
754      '@NAME@',
755      '@TEXT@',
756      '@UNSUBSCRIBE@',
757      '@DOKUWIKIURL@',
758    );
759    $replace = array(
760      $ID,
761      $conf['title'],
762      date($conf['dformat'], $comment['date']['created']),
763      $comment['user']['name'],
764      $comment['raw'],
765      wl($ID, 'do=unsubscribe', true, '&'),
766      DOKU_URL,
767    );
768    $text = str_replace($search, $replace, $text);
769
770    $subject = '['.$conf['title'].'] '.$this->getLang('mail_newcomment');
771
772    mail_send($to, $subject, $text, $conf['mailfrom'], '', $bcc);
773  }
774
775  /**
776   * Counts the number of visible comments
777   */
778  function _count($data){
779    $number = 0;
780    foreach ($data['comments'] as $cid => $comment){
781      if ($comment['parent']) continue;
782      if (!$comment['show']) continue;
783      $number++;
784      $rids = $comment['replies'];
785      if (count($rids)) $number = $number + $this->_countReplies($data, $rids);
786    }
787    return $number;
788  }
789
790  function _countReplies(&$data, $rids){
791    $number = 0;
792    foreach ($rids as $rid){
793      if (!isset($data['comments'][$rid])) continue; // reply was removed
794      if (!$data['comments'][$rid]['show']) continue;
795      $number++;
796      $rids = $data['comments'][$rid]['replies'];
797      if (count($rids)) $number = $number + $this->_countReplies($data, $rids);
798    }
799    return $number;
800  }
801
802  /**
803   * Renders the comment text
804   */
805  function _render($raw){
806    if ($this->getConf('wikisyntaxok')){
807      $xhtml = $this->render($raw);
808    } else { // wiki syntax not allowed -> just encode special chars
809      $xhtml = htmlspecialchars(trim($raw));
810    }
811    return $xhtml;
812  }
813
814  /**
815   * Adds a TOC item for the discussion section
816   */
817  function add_toc_item(&$event, $param){
818    if ($event->data[0] != 'xhtml') return;       // nothing to do for us
819    if (!$this->_hasDiscussion($title)) return;   // no discussion section
820
821    $pattern = '/<div id="toc__inside">(.*?)<\/div>\s<\/div>/s';
822    if (!preg_match($pattern, $event->data[1], $match)) return; // no TOC on this page
823
824    // ok, then let's do it!
825    global $conf;
826
827    if (!$title) $title = $this->getLang('discussion');
828    $section = '#discussion__section';
829    $level   = 3 - $conf['toptoclevel'];
830
831    $item = '<li class="level'.$level.'">'.DOKU_LF.
832      DOKU_TAB.'<div class="li">'.DOKU_LF.
833      DOKU_TAb.DOKU_TAB.'<span class="li"><a href="'.$section.'" class="toc">'.DOKU_LF.
834      DOKU_TAB.DOKU_TAB.DOKU_TAB.$title.DOKU_LF.
835      DOKU_TAB.DOKU_TAB.'</a></span>'.DOKU_LF.
836      DOKU_TAB.'</div>'.DOKU_LF.
837      '</li>'.DOKU_LF;
838
839    if ($level == 1) $search = "</ul>\n</div>";
840    else $search = "</ul>\n</li></ul>\n</div>";
841
842    $new = str_replace($search, $item.$search, $match[0]);
843    $event->data[1] = preg_replace($pattern, $new, $event->data[1]);
844  }
845
846  /**
847   * Finds out whether there is a discussion section for the current page
848   */
849  function _hasDiscussion(&$title = ''){
850    global $ID;
851
852    $classes = get_declared_classes();
853    if (in_array('helper_plugin_include', $classes)) return false;
854
855    $cfile = metaFN($ID, '.comments');
856
857    if (!@file_exists($cfile)){
858      if ($this->getConf('automatic')) return true;
859      else return false;
860    }
861
862    $comments = unserialize(io_readFile($cfile, false));
863
864    if ($comments['title']) $title = hsc($comments['title']);
865    $num = $comments['number'];
866    if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return false;
867    else return true;
868  }
869
870  /**
871   * Checks if 'newthread' was given as action or the comment form was submitted
872   */
873  function handle_act_preprocess(&$event, $param){
874    if ($event->data == 'newthread'){
875      // we can handle it -> prevent others
876      // $event->stopPropagation();
877      $event->preventDefault();
878
879      $event->data = $this->_newThread();
880    }
881    if ((in_array($_REQUEST['comment'], array('add', 'save')))
882      && (@file_exists(DOKU_PLUGIN.'captcha/action.php'))){
883      $this->_captchaCheck();
884    }
885  }
886
887  /**
888   * Creates a new thread page
889   */
890  function _newThread(){
891    global $ID, $INFO;
892
893    $ns    = cleanID($_REQUEST['ns']);
894    $title = str_replace(':', '', $_REQUEST['title']);
895    $back  = $ID;
896    $ID    = ($ns ? $ns.':' : '').cleanID($title);
897    $INFO  = pageinfo();
898
899    // check if we are allowed to create this file
900    if ($INFO['perm'] >= AUTH_CREATE){
901
902      //check if locked by anyone - if not lock for my self
903      if ($INFO['locked']) return 'locked';
904      else lock($ID);
905
906      // prepare the new thread file with default stuff
907      if (!@file_exists($INFO['filepath'])){
908        global $TEXT;
909
910        $TEXT = pageTemplate(array(($ns ? $ns.':' : '').$title));
911        if (!$TEXT){
912          $data = array('id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back);
913          $TEXT = $this->_pageTemplate($data);
914        }
915        return 'preview';
916      } else {
917        return 'edit';
918      }
919    } else {
920      return 'show';
921    }
922  }
923
924  /**
925   * Adapted version of pageTemplate() function
926   */
927  function _pageTemplate($data){
928    global $conf, $INFO;
929
930    $id   = $data['id'];
931    $user = $_SERVER['REMOTE_USER'];
932    $tpl  = io_readFile(DOKU_PLUGIN.'discussion/_template.txt');
933
934    // standard replacements
935    $replace = array(
936      '@ID@'   => $id,
937      '@NS@'   => $data['ns'],
938      '@PAGE@' => strtr(noNS($id),'_',' '),
939      '@USER@' => $user,
940      '@NAME@' => $INFO['userinfo']['name'],
941      '@MAIL@' => $INFO['userinfo']['mail'],
942      '@DATE@' => date($conf['dformat']),
943    );
944
945    // additional replacements
946    $replace['@BACK@']  = $data['back'];
947    $replace['@TITLE@'] = $data['title'];
948
949    // avatar if useavatar and avatar plugin available
950    if ($this->getConf('useavatar')
951      && (@file_exists(DOKU_PLUGIN.'avatar/syntax.php'))
952      && (!plugin_isdisabled('avatar'))){
953      $replace['@AVATAR@'] = '{{avatar>'.$user.' }} ';
954    } else {
955      $replace['@AVATAR@'] = '';
956    }
957
958    // tag if tag plugin is available
959    if ((@file_exists(DOKU_PLUGIN.'tag/syntax/tag.php'))
960      && (!plugin_isdisabled('tag'))){
961      $replace['@TAG@'] = "\n\n{{tag>}}";
962    } else {
963      $replace['@TAG@'] = '';
964    }
965
966    // do the replace
967    $tpl = str_replace(array_keys($replace), array_values($replace), $tpl);
968    return $tpl;
969  }
970
971  /**
972   * Checks if the CAPTCHA string submitted is valid
973   *
974   * @author     Andreas Gohr <gohr@cosmocode.de>
975   * @adaption   Esther Brunner <wikidesign@gmail.com>
976   */
977  function _captchaCheck(){
978    if (@file_exists(DOKU_PLUGIN.'captcha/disabled')) return; // CAPTCHA is disabled
979
980    require_once(DOKU_PLUGIN.'captcha/action.php');
981    $captcha = new action_plugin_captcha;
982
983    // do nothing if logged in user and no CAPTCHA required
984    if (!$captcha->getConf('forusers') && $_SERVER['REMOTE_USER']) return;
985
986    // compare provided string with decrypted captcha
987    $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'], auth_cookiesalt());
988    $code = $captcha->_generateCAPTCHA($captcha->_fixedIdent(), $rand);
989
990    if (!$_REQUEST['plugin__captcha_secret'] ||
991      !$_REQUEST['plugin__captcha'] ||
992      strtoupper($_REQUEST['plugin__captcha']) != $code){
993
994      // CAPTCHA test failed! Continue to edit instead of saving
995      msg($captcha->getLang('testfailed'), -1);
996      if ($_REQUEST['comment'] == 'save') $_REQUEST['comment'] = 'edit';
997      elseif ($_REQUEST['comment'] == 'add') $_REQUEST['comment'] = 'show';
998    }
999    // if we arrive here it was a valid save
1000  }
1001
1002  /**
1003   * Adds the comments to the index
1004   */
1005  function idx_add_discussion(&$event, $param){
1006
1007    // get .comments meta file name
1008    $file = metaFN($event->data[0], '.comments');
1009
1010    if (@file_exists($file)) $data = unserialize(io_readFile($file, false));
1011    if ((!$data['status']) || ($data['number'] == 0)) return; // comments are turned off
1012
1013    // now add the comments
1014    if (isset($data['comments'])){
1015      foreach ($data['comments'] as $key => $value){
1016        $event->data[1] .= $this->_addCommentWords($key, $data);
1017      }
1018    }
1019  }
1020
1021  /**
1022   * Adds the words of a given comment to the index
1023   */
1024  function _addCommentWords($cid, &$data, $parent = ''){
1025
1026    if (!isset($data['comments'][$cid])) return ''; // comment was removed
1027    $comment = $data['comments'][$cid];
1028
1029    if (!is_array($comment)) return '';             // corrupt datatype
1030    if ($comment['parent'] != $parent) return '';   // reply to an other comment
1031    if (!$comment['show']) return '';               // hidden comment
1032
1033    $text = $comment['raw'];                        // we only add the raw comment text
1034    if (is_array($comment['replies'])){             // and the replies
1035      foreach ($comment['replies'] as $rid){
1036        $text .= $this->_addCommentWords($rid, $data, $cid);
1037      }
1038    }
1039    return ' '.$text;
1040  }
1041
1042}
1043
1044//Setup VIM: ex: et ts=4 enc=utf-8 :