xref: /plugin/discussion/action.php (revision 5ea84b208d8cbed9360a59c0ca7383e5e7f97bb4)
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-18',
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 = $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
669  /**
670   * Trims the recent comments cache to the last $conf['changes_days'] recent
671   * changes or $conf['recent'] items, which ever is larger.
672   * The trimming is only done once a day.
673   *
674   * @author Ben Coburn <btcoburn@silicodon.net>
675   */
676  function _trimRecentCommentsLog($changelog){
677    global $conf;
678
679    if (@file_exists($changelog) &&
680      (filectime($changelog) + 86400) < time() &&
681      !@file_exists($changelog.'_tmp')){
682
683      io_lock($changelog);
684      $lines = file($changelog);
685      if (count($lines)<$conf['recent']) {
686          // nothing to trim
687          io_unlock($changelog);
688          return true;
689      }
690
691      io_saveFile($changelog.'_tmp', '');                  // presave tmp as 2nd lock
692      $trim_time = time() - $conf['recent_days']*86400;
693      $out_lines = array();
694
695      for ($i=0; $i<count($lines); $i++) {
696        $log = parseChangelogLine($lines[$i]);
697        if ($log === false) continue;                      // discard junk
698        if ($log['date'] < $trim_time) {
699          $old_lines[$log['date'].".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
700        } else {
701          $out_lines[$log['date'].".$i"] = $lines[$i];     // definitely keep these lines
702        }
703      }
704
705      // sort the final result, it shouldn't be necessary,
706      // however the extra robustness in making the changelog cache self-correcting is worth it
707      ksort($out_lines);
708      $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
709      if ($extra > 0) {
710        ksort($old_lines);
711        $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines);
712      }
713
714      // save trimmed changelog
715      io_saveFile($changelog.'_tmp', implode('', $out_lines));
716      @unlink($changelog);
717      if (!rename($changelog.'_tmp', $changelog)) {
718        // rename failed so try another way...
719        io_unlock($changelog);
720        io_saveFile($changelog, implode('', $out_lines));
721        @unlink($changelog.'_tmp');
722      } else {
723        io_unlock($changelog);
724      }
725      return true;
726    }
727  }
728
729  /**
730   * Sends a notify mail on new comment
731   *
732   * @param  array  $comment  data array of the new comment
733   *
734   * @author Andreas Gohr <andi@splitbrain.org>
735   * @author Esther Brunner <wikidesign@gmail.com>
736   */
737  function _notify($comment){
738    global $conf;
739    global $ID;
740
741    if ((!$conf['subscribers']) && (!$conf['notify'])) return; //subscribers enabled?
742    $bcc  = subscriber_addresslist($ID);
743    if ((empty($bcc)) && (!$conf['notify'])) return;
744    $to   = $conf['notify'];
745    $text = io_readFile($this->localFN('subscribermail'));
746
747    $search = array(
748      '@PAGE@',
749      '@TITLE@',
750      '@DATE@',
751      '@NAME@',
752      '@TEXT@',
753      '@UNSUBSCRIBE@',
754      '@DOKUWIKIURL@',
755    );
756    $replace = array(
757      $ID,
758      $conf['title'],
759      date($conf['dformat'], $comment['date']['created']),
760      $comment['user']['name'],
761      $comment['raw'],
762      wl($ID, 'do=unsubscribe', true, '&'),
763      DOKU_URL,
764    );
765    $text = str_replace($search, $replace, $text);
766
767    $subject = '['.$conf['title'].'] '.$this->getLang('mail_newcomment');
768
769    mail_send($to, $subject, $text, $conf['mailfrom'], '', $bcc);
770  }
771
772  /**
773   * Counts the number of visible comments
774   */
775  function _count($data){
776    $number = 0;
777    foreach ($data['comments'] as $cid => $comment){
778      if ($comment['parent']) continue;
779      if (!$comment['show']) continue;
780      $number++;
781      $rids = $comment['replies'];
782      if (count($rids)) $number = $number + $this->_countReplies($data, $rids);
783    }
784    return $number;
785  }
786
787  function _countReplies(&$data, $rids){
788    $number = 0;
789    foreach ($rids as $rid){
790      if (!isset($data['comments'][$rid])) continue; // reply was removed
791      if (!$data['comments'][$rid]['show']) continue;
792      $number++;
793      $rids = $data['comments'][$rid]['replies'];
794      if (count($rids)) $number = $number + $this->_countReplies($data, $rids);
795    }
796    return $number;
797  }
798
799  /**
800   * Renders the comment text
801   */
802  function _render($raw){
803    if ($this->getConf('wikisyntaxok')){
804      $xhtml = $this->render($raw);
805    } else { // wiki syntax not allowed -> just encode special chars
806      $xhtml = htmlspecialchars(trim($raw));
807    }
808    return $xhtml;
809  }
810
811  /**
812   * Adds a TOC item for the discussion section
813   */
814  function add_toc_item(&$event, $param){
815    if ($event->data[0] != 'xhtml') return; // nothing to do for us
816    if (!$this->_hasDiscussion()) return;   // no discussion section
817
818    $pattern = '/<div id="toc__inside">(.*?)<\/div>\s<\/div>/s';
819    if (!preg_match($pattern, $event->data[1], $match)) return; // no TOC on this page
820
821    // ok, then let's do it!
822    global $conf;
823
824    $title   = $this->getLang('discussion');
825    $section = '#discussion__section';
826    $level   = 3 - $conf['toptoclevel'];
827
828    $item = '<li class="level'.$level.'"><div class="li"><span class="li"><a href="'.
829      $section.'" class="toc">'.$title.'</a></span></div></li>';
830
831    if ($level == 1) $search = "</ul>\n</div>";
832    else $search = "</ul>\n</li></ul>\n</div>";
833
834    $new = str_replace($search, $item.$search, $match[0]);
835    $event->data[1] = preg_replace($pattern, $new, $event->data[1]);
836  }
837
838  /**
839   * Finds out whether there is a discussion section for the current page
840   */
841  function _hasDiscussion(){
842    global $ID;
843
844    $classes = get_declared_classes();
845    if (in_array('helper_plugin_include', $classes)) return false;
846
847    $cfile = metaFN($ID, '.comments');
848
849    if (!@file_exists($cfile)){
850      if ($this->getConf('automatic')) return true;
851      else return false;
852    }
853
854    $comments = unserialize(io_readFile($cfile, false));
855
856    $num = $comments['number'];
857    if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return false;
858    else return true;
859  }
860
861  /**
862   * Checks if 'newthread' was given as action or the comment form was submitted
863   */
864  function handle_act_preprocess(&$event, $param){
865    if ($event->data == 'newthread'){
866      // we can handle it -> prevent others
867      // $event->stopPropagation();
868      $event->preventDefault();
869
870      $event->data = $this->_newThread();
871    }
872    if ((in_array($_REQUEST['comment'], array('add', 'save')))
873      && (@file_exists(DOKU_PLUGIN.'captcha/action.php'))){
874      $this->_captchaCheck();
875    }
876  }
877
878  /**
879   * Creates a new thread page
880   */
881  function _newThread(){
882    global $ID, $INFO;
883
884    $ns    = cleanID($_REQUEST['ns']);
885    $title = str_replace(':', '', $_REQUEST['title']);
886    $back  = $ID;
887    $ID    = ($ns ? $ns.':' : '').cleanID($title);
888    $INFO  = pageinfo();
889
890    // check if we are allowed to create this file
891    if ($INFO['perm'] >= AUTH_CREATE){
892
893      //check if locked by anyone - if not lock for my self
894      if ($INFO['locked']) return 'locked';
895      else lock($ID);
896
897      // prepare the new thread file with default stuff
898      if (!@file_exists($INFO['filepath'])){
899        global $TEXT;
900
901        $TEXT = pageTemplate(array(($ns ? $ns.':' : '').$title));
902        if (!$TEXT){
903          $data = array('id' => $ID, 'ns' => $ns, 'title' => $title, 'back' => $back);
904          $TEXT = $this->_pageTemplate($data);
905        }
906        return 'preview';
907      } else {
908        return 'edit';
909      }
910    } else {
911      return 'show';
912    }
913  }
914
915  /**
916   * Adapted version of pageTemplate() function
917   */
918  function _pageTemplate($data){
919    global $conf, $INFO;
920
921    $id   = $data['id'];
922    $user = $_SERVER['REMOTE_USER'];
923    $tpl  = io_readFile(DOKU_PLUGIN.'discussion/_template.txt');
924
925    // standard replacements
926    $replace = array(
927      '@ID@'   => $id,
928      '@NS@'   => $data['ns'],
929      '@PAGE@' => strtr(noNS($id),'_',' '),
930      '@USER@' => $user,
931      '@NAME@' => $INFO['userinfo']['name'],
932      '@MAIL@' => $INFO['userinfo']['mail'],
933      '@DATE@' => date($conf['dformat']),
934    );
935
936    // additional replacements
937    $replace['@BACK@']  = $data['back'];
938    $replace['@TITLE@'] = $data['title'];
939
940    // avatar if useavatar and avatar plugin available
941    if ($this->getConf('useavatar')
942      && (@file_exists(DOKU_PLUGIN.'avatar/syntax.php'))
943      && (!plugin_isdisabled('avatar'))){
944      $replace['@AVATAR@'] = '{{avatar>'.$user.' }} ';
945    } else {
946      $replace['@AVATAR@'] = '';
947    }
948
949    // tag if tag plugin is available
950    if ((@file_exists(DOKU_PLUGIN.'tag/syntax/tag.php'))
951      && (!plugin_isdisabled('tag'))){
952      $replace['@TAG@'] = "\n\n{{tag>}}";
953    } else {
954      $replace['@TAG@'] = '';
955    }
956
957    // do the replace
958    $tpl = str_replace(array_keys($replace), array_values($replace), $tpl);
959    return $tpl;
960  }
961
962  /**
963   * Checks if the CAPTCHA string submitted is valid
964   *
965   * @author     Andreas Gohr <gohr@cosmocode.de>
966   * @adaption   Esther Brunner <wikidesign@gmail.com>
967   */
968  function _captchaCheck(){
969    if (@file_exists(DOKU_PLUGIN.'captcha/disabled')) return; // CAPTCHA is disabled
970
971    require_once(DOKU_PLUGIN.'captcha/action.php');
972    $captcha = new action_plugin_captcha;
973
974    // do nothing if logged in user and no CAPTCHA required
975    if (!$captcha->getConf('forusers') && $_SERVER['REMOTE_USER']) return;
976
977    // compare provided string with decrypted captcha
978    $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'], auth_cookiesalt());
979    $code = $captcha->_generateCAPTCHA($captcha->_fixedIdent(), $rand);
980
981    if (!$_REQUEST['plugin__captcha_secret'] ||
982      !$_REQUEST['plugin__captcha'] ||
983      strtoupper($_REQUEST['plugin__captcha']) != $code){
984
985      // CAPTCHA test failed! Continue to edit instead of saving
986      msg($captcha->getLang('testfailed'), -1);
987      if ($_REQUEST['comment'] == 'save') $_REQUEST['comment'] = 'edit';
988      elseif ($_REQUEST['comment'] == 'add') $_REQUEST['comment'] = 'show';
989    }
990    // if we arrive here it was a valid save
991  }
992
993  /**
994   * Adds the comments to the index
995   */
996  function idx_add_discussion(&$event, $param){
997
998    // get .comments meta file name
999    $file = metaFN($event->data[0], '.comments');
1000
1001    if (@file_exists($file)) $data = unserialize(io_readFile($file, false));
1002    if ((!$data['status']) || ($data['number'] == 0)) return; // comments are turned off
1003
1004    // now add the comments
1005    if (isset($data['comments'])){
1006      foreach ($data['comments'] as $key => $value){
1007        $event->data[1] .= _addCommentWords($key, $data);
1008      }
1009    }
1010  }
1011
1012  /**
1013   * Adds the words of a given comment to the index
1014   */
1015  function _addCommentWords($cid, &$data, $parent = ''){
1016
1017    if (!isset($data['comments'][$cid])) return ''; // comment was removed
1018    $comment = $data['comments'][$cid];
1019
1020    if (!is_array($comment)) return '';             // corrupt datatype
1021    if ($comment['parent'] != $parent) return '';   // reply to an other comment
1022    if (!$comment['show']) return '';               // hidden comment
1023
1024    $text = $comment['raw'];                        // we only add the raw comment text
1025    if (is_array($comment['replies'])){             // and the replies
1026      foreach ($comment['replies'] as $rid){
1027        $text .= $this->_addCommentWords($rid, $data, $cid);
1028      }
1029    }
1030    return ' '.$text;
1031  }
1032
1033}
1034
1035//Setup VIM: ex: et ts=4 enc=utf-8 :