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