OSDN Git Service

BugTrack/2213 Set Absolute URI or Root relative path
[pukiwiki/pukiwiki.git] / plugin / pcomment.inc.php
index 234227d..4130774 100644 (file)
@@ -1,9 +1,11 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: pcomment.inc.php,v 1.42 2005/05/29 10:49:59 henoheno Exp $
+// pcomment.inc.php
+// Copyright 2002-2017 PukiWiki Development Team
+// License: GPL v2 or (at your option) any later version
 //
 // pcomment plugin - Show/Insert comments into specified (another) page
-//
+
 // Usage: #pcomment([page][,max][,options])
 //
 //   page -- An another page-name that holds comments
 //   reply -- Show radio buttons allow to specify where to reply
 
 // Default recording page name (%s = $vars['page'] = original page name)
-switch (LANG) {
-case 'ja': define('PLUGIN_PCOMMENT_PAGE', '[[¥³¥á¥ó¥È/%s]]'); break;
-default:   define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]'); break;
-}
+define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]');
+define('PLUGIN_PCOMMENT_PAGE_COMPATIBLE', '[[コメント/%s]]'); // for backword compatible of 'ja' pcomment
 
 define('PLUGIN_PCOMMENT_NUM_COMMENTS',     10); // Default 'latest N posts'
 define('PLUGIN_PCOMMENT_DIRECTION_DEFAULT', 1); // 1: above 0: below
@@ -58,7 +58,7 @@ function plugin_pcomment_action()
        }
 
        pkwk_headers_sent();
-       header('Location: ' . get_script_uri() . '?' . rawurlencode($refer));
+       header('Location: ' . get_page_uri($refer, PKWK_URI_ROOT));
        exit;
 }
 
@@ -67,8 +67,6 @@ function plugin_pcomment_convert()
        global $vars;
        global $_pcmt_messages;
 
-       $ret = '';
-
        $params = array(
                'noname'=>FALSE,
                'nodate'=>FALSE,
@@ -77,18 +75,32 @@ function plugin_pcomment_convert()
                'reply' =>FALSE,
                '_args' =>array()
        );
-       array_walk(func_get_args(), 'plugin_pcomment_check_arg', & $params);
+
+       foreach(func_get_args() as $arg)
+               plugin_pcomment_check_arg($arg, $params);
 
        $vars_page = isset($vars['page']) ? $vars['page'] : '';
-       $page  = (isset($params['_args'][0]) && $params['_args'][0] != '') ? $params['_args'][0] :
-               sprintf(PLUGIN_PCOMMENT_PAGE, strip_bracket($vars_page));
-       $count = (isset($params['_args'][1]) && $params['_args'][1] != '') ? $params['_args'][1] : 0;
-       if ($count == 0 && $count !== '0')
-               $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
+       if (isset($params['_args'][0]) && $params['_args'][0] != '') {
+               $page = $params['_args'][0];
+       } else {
+               $raw_vars_page = strip_bracket($vars_page);
+               $page = sprintf(PLUGIN_PCOMMENT_PAGE, $raw_vars_page);
+               $raw_page = strip_bracket($page);
+               if (!is_page($raw_page)) {
+                       // If the page doesn't exist, search backward-compatible page
+                       // If only compatible page exists, set the page as comment target
+                       $page_compat = sprintf(PLUGIN_PCOMMENT_PAGE_COMPATIBLE, $raw_vars_page);
+                       if (is_page(strip_bracket($page_compat))) {
+                               $page = $page_compat;
+                       }
+               }
+       }
+       $count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
+       if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
 
        $_page = get_fullname(strip_bracket($page), $vars_page);
        if (!is_pagename($_page))
-               return sprintf($_pcmt_messages['err_pagename'], htmlspecialchars($_page));
+               return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
 
        $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
        if ($params['below']) {
@@ -116,10 +128,10 @@ function plugin_pcomment_convert()
                        '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
                $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
 
-               $s_page   = htmlspecialchars($page);
-               $s_refer  = htmlspecialchars($vars_page);
-               $s_nodate = htmlspecialchars($params['nodate']);
-               $s_count  = htmlspecialchars($count);
+               $s_page   = htmlsc($page);
+               $s_refer  = htmlsc($vars_page);
+               $s_nodate = htmlsc($params['nodate']);
+               $s_count  = htmlsc($count);
 
                $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
                $form = <<<EOD
@@ -168,8 +180,7 @@ EOD;
 
 function plugin_pcomment_insert()
 {
-       global $script, $vars, $now;
-       global $_title_updated, $_no_name, $_pcmt_messages;
+       global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
 
        $refer = isset($vars['refer']) ? $vars['refer'] : '';
        $page  = isset($vars['page'])  ? $vars['page']  : '';
@@ -204,14 +215,14 @@ function plugin_pcomment_insert()
        $msg = rtrim($msg);
 
        if (! is_page($page)) {
-               $postdata = '[[' . htmlspecialchars(strip_bracket($refer)) . ']]' . "\n\n" .
+               $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
                        '-' . $msg . "\n";
        } else {
                $postdata = get_source($page);
                $count    = count($postdata);
 
                $digest = isset($vars['digest']) ? $vars['digest'] : '';
-               if (md5(join('', $postdata)) != $digest) {
+               if (md5(join('', $postdata)) !== $digest) {
                        $ret['msg']  = $_pcmt_messages['title_collided'];
                        $ret['body'] = $_pcmt_messages['msg_collided'];
                }
@@ -232,7 +243,7 @@ function plugin_pcomment_insert()
                        while ($end_position < $count) {
                                $matches = array();
                                if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
-                                       && md5($matches[2]) == $reply_hash)
+                                       && md5($matches[2]) === $reply_hash)
                                {
                                        $b_reply = TRUE;
                                        $level   = strlen($matches[1]) + 1;
@@ -300,7 +311,7 @@ function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
 }
 
 // Check arguments
-function plugin_pcomment_check_arg($val, $key, & $params)
+function plugin_pcomment_check_arg($val, & $params)
 {
        if ($val != '') {
                $l_val = strtolower($val);
@@ -370,4 +381,3 @@ function plugin_pcomment_get_comments($page, $count, $dir, $reply)
 
        return array($comments, $digest);
 }
-?>