OSDN Git Service

BugTrack/2422 Counter plugin supports MySQL
[pukiwiki/pukiwiki.git] / plugin / comment.inc.php
index e8c39d5..6ef906f 100644 (file)
@@ -1,6 +1,10 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: comment.inc.php,v 1.29 2005/05/06 01:50:29 henoheno Exp $
+// comment.inc.php
+// Copyright
+//   2002-2016 PukiWiki Development Team
+//   2001-2002 Originally written by yu-ji
+// License: GPL v2 or (at your option) any later version
 //
 // Comment plugin
 
@@ -18,64 +22,69 @@ function plugin_comment_action()
 {
        global $script, $vars, $now, $_title_updated, $_no_name;
        global $_msg_comment_collided, $_title_comment_collided;
+       global $_comment_plugin_fail_msg;
 
        if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
 
-       if (! isset($vars['msg']) || $vars['msg'] == '')
-               return array('msg'=>'', 'body'=>'');
-
-       $vars['msg'] = preg_replace("/\n/", '', $vars['msg']); // Cut LFs
+       if (! isset($vars['msg'])) return array('msg'=>'', 'body'=>''); // Do nothing
 
+       $vars['msg'] = str_replace("\n", '', $vars['msg']); // Cut LFs
        $head = '';
        $match = array();
-       if (preg_match('/^(-{1,2})\s*(.*)/', $vars['msg'], $match)) {
+       if (preg_match('/^(-{1,2})-*\s*(.*)/', $vars['msg'], $match)) {
                $head        = & $match[1];
                $vars['msg'] = & $match[2];
        }
+       if ($vars['msg'] == '') return array('msg'=>'', 'body'=>''); // Do nothing
 
        $comment  = str_replace('$msg', $vars['msg'], PLUGIN_COMMENT_FORMAT_MSG);
        if(isset($vars['name']) || ($vars['nodate'] != '1')) {
                $_name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
                $_name = ($_name == '') ? '' : str_replace('$name', $_name, PLUGIN_COMMENT_FORMAT_NAME);
-
                $_now  = ($vars['nodate'] == '1') ? '' :
                        str_replace('$now', $now, PLUGIN_COMMENT_FORMAT_NOW);
-
                $comment = str_replace("\x08MSG\x08",  $comment, PLUGIN_COMMENT_FORMAT_STRING);
                $comment = str_replace("\x08NAME\x08", $_name, $comment);
                $comment = str_replace("\x08NOW\x08",  $_now,  $comment);
        }
-       $comment = $head . $comment;
-
-       $postdata      = '';
-       $postdata_old  = get_source($vars['refer']);
-       $comment_no    = 0;
-       $comment_ins   = ($vars['above'] == '1');
-
-       foreach ($postdata_old as $line) {
-               if (! $comment_ins) $postdata .= $line;
+       $comment = '-' . $head . ' ' . $comment;
+
+       $postdata    = '';
+       $comment_no  = 0;
+       $above       = (isset($vars['above']) && $vars['above'] == '1');
+       $comment_added = FALSE;
+       foreach (get_source($vars['refer']) as $line) {
+               if (! $above) $postdata .= $line;
                if (preg_match('/^#comment/i', $line) && $comment_no++ == $vars['comment_no']) {
-                       $postdata = rtrim($postdata) . "\n" .
-                               '-' . $comment . "\n";
-                       if ($comment_ins) $postdata .= "\n";
+                       $comment_added = TRUE;
+                       if ($above) {
+                               $postdata = rtrim($postdata) . "\n" .
+                                       $comment . "\n" .
+                                       "\n";  // Insert one blank line above #commment, to avoid indentation
+                       } else {
+                               $postdata = rtrim($postdata) . "\n" .
+                                       $comment . "\n";
+                       }
                }
-               if ($comment_ins) $postdata .= $line;
+               if ($above) $postdata .= $line;
        }
-
        $title = $_title_updated;
        $body = '';
-       if (md5(@join('', get_source($vars['refer']))) != $vars['digest']) {
+       if ($comment_added) {
+               // new comment added
+               if (md5(get_source($vars['refer'], TRUE, TRUE)) !== $vars['digest']) {
+                       $title = $_title_comment_collided;
+                       $body  = $_msg_comment_collided . make_pagelink($vars['refer']);
+               }
+               page_write($vars['refer'], $postdata);
+       } else {
+               // failed to add the comment
                $title = $_title_comment_collided;
-               $body  = $_msg_comment_collided . make_pagelink($vars['refer']);
+               $body  = $_comment_plugin_fail_msg . make_pagelink($vars['refer']);
        }
-
-       page_write($vars['refer'], $postdata);
-
        $retvars['msg']  = $title;
        $retvars['body'] = $body;
-
        $vars['page'] = $vars['refer'];
-
        return $retvars;
 }
 
@@ -106,7 +115,7 @@ function plugin_comment_convert()
                (in_array('below', $options) ? '0' : PLUGIN_COMMENT_DIRECTION_DEFAULT);
 
        $script = get_script_uri();
-       $s_page = htmlspecialchars($vars['page']);
+       $s_page = htmlsc($vars['page']);
        $string = <<<EOD
 <br />
 <form action="$script" method="post">
@@ -126,4 +135,3 @@ EOD;
 
        return $string;
 }
-?>