OSDN Git Service

BugTrack2/236: If you compare two numerical strings, they are compared as integers
[pukiwiki/pukiwiki.git] / plugin / pcomment.inc.php
index 6df5d51..6dadab0 100644 (file)
 <?php
-/////////////////////////////////////////////////
-// PukiWiki - Yet another WikiWikiWeb clone.
+// PukiWiki - Yet another WikiWikiWeb clone
+// $Id: pcomment.inc.php,v 1.48 2011/01/25 15:01:01 henoheno Exp $
 //
-// $Id: pcomment.inc.php,v 1.11 2003/04/17 02:37:02 arino Exp $
+// pcomment plugin - Show/Insert comments into specified (another) page
 //
+// Usage: #pcomment([page][,max][,options])
+//
+//   page -- An another page-name that holds comments
+//           (default:PLUGIN_PCOMMENT_PAGE)
+//   max  -- Max number of recent comments to show
+//           (0:Show all, default:PLUGIN_PCOMMENT_NUM_COMMENTS)
+//
+// Options:
+//   above -- Comments are listed above the #pcomment (added by chronological order)
+//   below -- Comments are listed below the #pcomment (by reverse order)
+//   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;
+}
 
-*¥×¥é¥°¥¤¥ó pcomment
-»ØÄꤷ¤¿¥Ú¡¼¥¸¤Ë¥³¥á¥ó¥È¤òÁÞÆþ
+define('PLUGIN_PCOMMENT_NUM_COMMENTS',     10); // Default 'latest N posts'
+define('PLUGIN_PCOMMENT_DIRECTION_DEFAULT', 1); // 1: above 0: below
+define('PLUGIN_PCOMMENT_SIZE_MSG',  70);
+define('PLUGIN_PCOMMENT_SIZE_NAME', 15);
 
-*Usage
- #pcomment([¥Ú¡¼¥¸Ì¾][,ɽ¼¨¤¹¤ë¥³¥á¥ó¥È¿ô][,¥ª¥×¥·¥ç¥ó])
+// Auto log rotation
+define('PLUGIN_PCOMMENT_AUTO_LOG', 0); // 0:off 1-N:number of comments per page
 
-*¥Ñ¥é¥á¡¼¥¿
--¥Ú¡¼¥¸Ì¾~
- Åê¹Æ¤µ¤ì¤¿¥³¥á¥ó¥È¤òµ­Ï¿¤¹¤ë¥Ú¡¼¥¸¤Î̾Á°
--ɽ¼¨¤¹¤ë¥³¥á¥ó¥È¿ô~
- ²áµî¤Î¥³¥á¥ó¥È¤ò²¿·ïɽ¼¨¤¹¤ë¤«(0¤ÇÁ´·ï)
+// Update recording page's timestamp instead of parent's page itself
+define('PLUGIN_PCOMMENT_TIMESTAMP', 0);
 
-*¥ª¥×¥·¥ç¥ó
--above~
- ¥³¥á¥ó¥È¤ò¥Õ¥£¡¼¥ë¥É¤ÎÁ°¤Ëɽ¼¨(¿·¤·¤¤µ­»ö¤¬²¼)
--below~
- ¥³¥á¥ó¥È¤ò¥Õ¥£¡¼¥ë¥É¤Î¸å¤Ëɽ¼¨(¿·¤·¤¤µ­»ö¤¬¾å)
--reply~
- 2¥ì¥Ù¥ë¤Þ¤Ç¤Î¥³¥á¥ó¥È¤Ë¥ê¥×¥é¥¤¤ò¤Ä¤±¤ëradio¥Ü¥¿¥ó¤òɽ¼¨
+// ----
+define('PLUGIN_PCOMMENT_FORMAT_NAME',  '[[$name]]');
+define('PLUGIN_PCOMMENT_FORMAT_MSG',   '$msg');
+define('PLUGIN_PCOMMENT_FORMAT_NOW',   '&new{$now};');
+
+// "\x01", "\x02", "\x03", and "\x08" are used just as markers
+define('PLUGIN_PCOMMENT_FORMAT_STRING',
+       "\x08" . 'MSG' . "\x08" . ' -- ' . "\x08" . 'NAME' . "\x08" . ' ' . "\x08" . 'DATE' . "\x08");
 
-*/
-// ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥Õ¥©¥ë¥È(%s¤Ë$vars['page']¤¬Æþ¤ë)
-define('PCMT_PAGE','[[¥³¥á¥ó¥È/%s]]');
-//
-// ¥Ú¡¼¥¸¤Î¥«¥Æ¥´¥ê(¿·µ¬ºîÀ®»þ¤ËÁÞÆþ)
-define('PCMT_CATEGORY','[[:Comment]]');
-//
-// É½¼¨¤¹¤ë¥³¥á¥ó¥È¿ô¤Î¥Ç¥Õ¥©¥ë¥È
-define('PCMT_NUM_COMMENTS',10);
-//
-// ¥³¥á¥ó¥È¤Î̾Á°¥Æ¥­¥¹¥È¥¨¥ê¥¢¤Î¥«¥é¥à¿ô
-define('PCMT_COLS_NAME',15);
-//
-// ¥³¥á¥ó¥È¤Î¥Æ¥­¥¹¥È¥¨¥ê¥¢¤Î¥«¥é¥à¿ô
-define('PCMT_COLS_COMMENT',70);
-//
-// ÁÞÆþ¤¹¤ë°ÌÃÖ 1:ËöÈø 0:ÀèƬ
-define('PCMT_INSERT_INS',1);
-//
-//¥³¥á¥ó¥È¤ÎÁÞÆþ¥Õ¥©¡¼¥Þ¥Ã¥È
-define('PCMT_FORMAT_NAME','[[%s]]');
-define('PCMT_FORMAT_MSG','%s');
-define('PCMT_FORMAT_DATE','SIZE(10){%s}');
-// \x08¤Ï¡¢Åê¹Æ¤µ¤ì¤¿Ê¸»úÎóÃæ¤Ë¸½¤ì¤Ê¤¤Ê¸»ú¤Ç¤¢¤ì¤Ð¤Ê¤ó¤Ç¤â¤¤¤¤¡£
-define('PCMT_FORMAT',"\x08MSG\x08 -- \x08NAME\x08 \x08DATE\x08");
-
-function plugin_pcomment_init() {
-       $messages = array(
-               '_pcmt_messages'=>array(
-                       'btn_name' => '¤ªÌ¾Á°: ',
-                       'btn_comment' => '¥³¥á¥ó¥È¤ÎÁÞÆþ',
-                       'msg_comment' => '¥³¥á¥ó¥È: ',
-                       'msg_recent' => 'ºÇ¿·¤Î%d·ï¤òɽ¼¨¤·¤Æ¤¤¤Þ¤¹¡£',
-                       'msg_all' => '¥³¥á¥ó¥È¥Ú¡¼¥¸¤ò»²¾È',
-                       'msg_none' => '¥³¥á¥ó¥È¤Ï¤¢¤ê¤Þ¤»¤ó¡£',
-                       'title_collided' => '$1 ¤Ç¡Ú¹¹¿·¤Î¾×ÆÍ¡Û¤¬µ¯¤­¤Þ¤·¤¿',
-                       'msg_collided' => '¤¢¤Ê¤¿¤¬¤³¤Î¥Ú¡¼¥¸¤òÊÔ½¸¤·¤Æ¤¤¤ë´Ö¤Ë¡¢Â¾¤Î¿Í¤¬Æ±¤¸¥Ú¡¼¥¸¤ò¹¹¿·¤·¤Æ¤·¤Þ¤Ã¤¿¤è¤¦¤Ç¤¹¡£<br />
-¥³¥á¥ó¥È¤òÄɲä·¤Þ¤·¤¿¤¬¡¢°ã¤¦°ÌÃÖ¤ËÁÞÆþ¤µ¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Þ¤»¤ó¡£<br />',
-               )
-       );
-       set_plugin_messages($messages);
-}
 function plugin_pcomment_action()
 {
-       global $script,$post,$vars;
-       
-       if ($post['msg'] == '')
-       {
-               return array();
-       }
-       
-       $retval = pcmt_insert();
-       
-       if ($retval['collided'])
-       {
-               $vars['page'] = $post['page'] = $post['refer'];
+       global $vars;
+
+       if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
+
+       if (! isset($vars['msg']) || $vars['msg'] == '') return array();
+       $refer = isset($vars['refer']) ? $vars['refer'] : '';
+
+       $retval = plugin_pcomment_insert();
+       if ($retval['collided']) {
+               $vars['page'] = $refer;
                return $retval;
        }
-       
-       header("Location: $script?".rawurlencode($post['refer']));
+
+       pkwk_headers_sent();
+       header('Location: ' . get_script_uri() . '?' . rawurlencode($refer));
        exit;
 }
 
 function plugin_pcomment_convert()
 {
-       global $script,$vars,$BracketName;
+       global $vars;
        global $_pcmt_messages;
 
-       //Ìá¤êÃÍ
-       $ret = '';
+       $params = array(
+               'noname'=>FALSE,
+               'nodate'=>FALSE,
+               'below' =>FALSE,
+               'above' =>FALSE,
+               'reply' =>FALSE,
+               '_args' =>array()
+       );
 
-       //¥Ñ¥é¥á¡¼¥¿ÊÑ´¹
-       $params = array('noname'=>FALSE,'nodate'=>FALSE,'below'=>FALSE,'above'=>FALSE,'reply'=>FALSE,'_args'=>array(),'_done'=>FALSE);
-       array_walk(func_get_args(), 'pcmt_check_arg', &$params);
+       foreach(func_get_args() as $arg)
+               plugin_pcomment_check_arg($arg, $params);
 
-       //ʸ»úÎó¤ò¼èÆÀ
-       $page = array_key_exists(0,$params['_args']) ? $params['_args'][0] : '';
-       $count = array_key_exists(1,$params['_args']) ? $params['_args'][1] : 0;
-       
-       if ($page == '')
-       {
-               $page = sprintf(PCMT_PAGE,strip_bracket($vars['page']));
-       }
-       
-       $_page = get_fullname(strip_bracket($page),$vars['page']);
-       if (!preg_match("/^$BracketName$/",$_page))
-       {
-               return 'invalid page name.';
-       }
-       if ($count == 0 and $count !== '0')
-       {
-               $count = PCMT_NUM_COMMENTS;
-       }
-       
-       //¸þ¤­¤ò·èÄê
-       $dir = PCMT_INSERT_INS;
-       if ($params['above'])
-       {
-               $dir = 1;
-       }
-       if ($params['below']) //ξÊý»ØÄꤵ¤ì¤¿¤é²¼¤Ë (^^;
-       {
+       $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]) ? 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'], htmlsc($_page));
+
+       $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
+       if ($params['below']) {
                $dir = 0;
+       } elseif ($params['above']) {
+               $dir = 1;
        }
 
-       //¥³¥á¥ó¥È¤ò¼èÆÀ
-       list($comments, $digest) = pcmt_get_comments($_page,$count,$dir,$params['reply']);
+       list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
 
-       //¥Õ¥©¡¼¥à¤òɽ¼¨
-       if ($params['noname'])
-       {
-               $title = $_pcmt_messages['msg_comment'];
-               $name = '';
-       }
-       else
-       {
-               $title = $_pcmt_messages['btn_name'];
-               $name = '<input type="text" name="name" size="'.PCMT_COLS_NAME.'" />';
-       }
+       if (PKWK_READONLY) {
+               $form_start = $form = $form_end = '';
+       } else {
+               // Show a form
 
-       $radio = $params['reply'] ? '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
-       $comment = '<input type="text" name="msg" size="'.PCMT_COLS_COMMENT.'" />';
+               if ($params['noname']) {
+                       $title = $_pcmt_messages['msg_comment'];
+                       $name = '';
+               } else {
+                       $title = $_pcmt_messages['btn_name'];
+                       $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
+               }
+
+               $radio   = $params['reply'] ?
+                       '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
+               $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
 
-       //XSSÀȼåÀ­ÌäÂê - ³°Éô¤«¤éÍ褿ÊÑ¿ô¤ò¥¨¥¹¥±¡¼¥×
-       $s_page = htmlspecialchars($page);
-       $s_refer = htmlspecialchars($vars['page']);
-       $s_nodate = htmlspecialchars($params['nodate']);
+               $s_page   = htmlsc($page);
+               $s_refer  = htmlsc($vars_page);
+               $s_nodate = htmlsc($params['nodate']);
+               $s_count  = htmlsc($count);
 
-       $form = <<<EOD
+               $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
+               $form = <<<EOD
   <div>
   <input type="hidden" name="digest" value="$digest" />
   <input type="hidden" name="plugin" value="pcomment" />
-  <input type="hidden" name="refer" value="$s_refer" />
-  <input type="hidden" name="page" value="$s_page" />
+  <input type="hidden" name="refer"  value="$s_refer" />
+  <input type="hidden" name="page"   value="$s_page" />
   <input type="hidden" name="nodate" value="$s_nodate" />
-  <input type="hidden" name="dir" value="$dir" />
+  <input type="hidden" name="dir"    value="$dir" />
+  <input type="hidden" name="count"  value="$count" />
   $radio $title $name $comment
   <input type="submit" value="{$_pcmt_messages['btn_comment']}" />
   </div>
 EOD;
-       if (!is_page($_page))
-       {
-               $link = make_pagelink($_page);
-               $recent = $_pcmt_messages['msg_none'];
+               $form_end = '</form>' . "\n";
        }
-       else
-       {
-               $msg = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
-               $link = make_pagelink($_page,$msg);
-               $recent = ($count > 0) ? sprintf($_pcmt_messages['msg_recent'],$count) : '';
+
+       if (! is_page($_page)) {
+               $link   = make_pagelink($_page);
+               $recent = $_pcmt_messages['msg_none'];
+       } else {
+               $msg    = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
+               $link   = make_pagelink($_page, $msg);
+               $recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
        }
 
-       return $dir ?
-               "<div><p>$recent $link</p>\n<form action=\"$script\" method=\"post\">$comments$form</form></div>" :
-               "<div><form action=\"$script\" method=\"post\">$form$comments</form>\n<p>$recent $link</p></div>";
+       if ($dir) {
+               return '<div>' .
+                       '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
+                       $form_start .
+                               $comments . "\n" .
+                               $form .
+                       $form_end .
+                       '</div>' . "\n";
+       } else {
+               return '<div>' .
+                       $form_start .
+                               $form .
+                               $comments. "\n" .
+                       $form_end .
+                       '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
+                       '</div>' . "\n";
+       }
 }
 
-function pcmt_insert()
+function plugin_pcomment_insert()
 {
-       global $script,$post,$vars,$now,$do_backup,$BracketName;
-       global $_title_updated,$_pcmt_messages;
+       global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
 
-       $page = $post['page'];
-       if (!preg_match("/^$BracketName$/",$page))
-       {
-               return array('msg'=>'invalid page name.','body'=>'cannot add comment.','collided'=>TRUE);
-       }
-       
-       $ret['msg'] = $_title_updated;
-       $ret['collided'] = FALSE;
-
-       //¥³¥á¥ó¥È¥Õ¥©¡¼¥Þ¥Ã¥È¤òŬÍÑ
-       $msg = sprintf(PCMT_FORMAT_MSG, rtrim($post['msg']));
-       $name = ($post['name'] == '') ? '' :  sprintf(PCMT_FORMAT_NAME, $post['name']);
-       $date = ($post['nodate'] == '1') ? '' : sprintf(PCMT_FORMAT_DATE, $now);
-       if ($date != '' or $name != '')
-       { 
-               $msg = str_replace("\x08MSG\x08", $msg,  PCMT_FORMAT);
-               $msg = str_replace("\x08NAME\x08",$name, $msg);
-               $msg = str_replace("\x08DATE\x08",$date, $msg);
+       $refer = isset($vars['refer']) ? $vars['refer'] : '';
+       $page  = isset($vars['page'])  ? $vars['page']  : '';
+       $page  = get_fullname($page, $refer);
+
+       if (! is_pagename($page))
+               return array(
+                       'msg' =>'Invalid page name',
+                       'body'=>'Cannot add comment' ,
+                       'collided'=>TRUE
+               );
+
+       check_editable($page, true, true);
+
+       $ret = array('msg' => $_title_updated, 'collided' => FALSE);
+
+       $msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
+       $name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
+       $name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
+       $date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
+               str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
+       if ($date != '' || $name != '') {
+               $msg = str_replace("\x08" . 'MSG'  . "\x08", $msg,  PLUGIN_PCOMMENT_FORMAT_STRING);
+               $msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
+               $msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
        }
-       $reply_hash = array_key_exists('reply',$post) ? $post['reply'] : '';
-       if ($reply_hash or !is_page($page))
-       {
-               $msg = preg_replace('/^\-+/','',$msg);
+
+       $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
+       if ($reply_hash || ! is_page($page)) {
+               $msg = preg_replace('/^\-+/', '', $msg);
        }
        $msg = rtrim($msg);
-       
-       if (!is_page($page))
-       {
-               $postdata = PCMT_CATEGORY.' [['.htmlspecialchars(strip_bracket($post['refer']))."]]\n\n-$msg\n";
-       }
-       else
-       {
-               //¥Ú¡¼¥¸¤òÆɤ߽Ф¹
+
+       if (! is_page($page)) {
+               $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
+                       '-' . $msg . "\n";
+       } else {
                $postdata = get_source($page);
-               
-               // ¹¹¿·¤Î¾×Æͤò¸¡½Ð
-               if (md5(join('',$postdata)) != $post['digest'])
-               {
-                       $ret['msg'] = $_pcmt_messages['title_collided'];
+               $count    = count($postdata);
+
+               $digest = isset($vars['digest']) ? $vars['digest'] : '';
+               if (md5(join('', $postdata)) !== $digest) {
+                       $ret['msg']  = $_pcmt_messages['title_collided'];
                        $ret['body'] = $_pcmt_messages['msg_collided'];
                }
 
-               // ½é´üÃÍ
-               $level = 1;
-               $pos = 0;
-               
-               // ¥³¥á¥ó¥È¤Î³«»Ï°ÌÃÖ¤ò¸¡º÷
-               while ($pos < count($postdata))
-               {
-                       if (preg_match('/^\-/',$postdata[$pos]))
-                       {
-                               break;
-                       }
-                       $pos++;
+               $start_position = 0;
+               while ($start_position < $count) {
+                       if (preg_match('/^\-/', $postdata[$start_position])) break;
+                       ++$start_position;
                }
-               $start_pos = $pos;
-               //¥ê¥×¥é¥¤Àè¤Î¥³¥á¥ó¥È¤ò¸¡º÷
-               if ($reply_hash != '')
-               {
-                       while ($pos < count($postdata))
-                       {
-                               if (preg_match('/^(\-{1,2})(?!\-)(.*)$/',$postdata[$pos++],$matches)
-                                       and md5($matches[2]) == $reply_hash)
+               $end_position = $start_position;
+
+               $dir = isset($vars['dir']) ? $vars['dir'] : '';
+
+               // Find the comment to reply
+               $level   = 1;
+               $b_reply = FALSE;
+               if ($reply_hash != '') {
+                       while ($end_position < $count) {
+                               $matches = array();
+                               if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
+                                       && md5($matches[2]) == $reply_hash)
                                {
-                                       $level = strlen($matches[1]) + 1; //ÁÞÆþ¤¹¤ë¥ì¥Ù¥ë
-                                       
-                                       // ¥³¥á¥ó¥È¤ÎËöÈø¤ò¸¡º÷
-                                       while ($pos < count($postdata))
-                                       {
-                                               if (preg_match('/^(\-{1,3})(?!\-)/',$postdata[$pos],$matches)
-                                                       and strlen($matches[1]) < $level)
-                                               {
-                                                       break;
-                                               }
-                                               $pos++;
+                                       $b_reply = TRUE;
+                                       $level   = strlen($matches[1]) + 1;
+
+                                       while ($end_position < $count) {
+                                               if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
+                                                       && strlen($matches[1]) < $level) break;
+                                               ++$end_position;
                                        }
                                        break;
                                }
                        }
                }
-               else
-               {
-                       $pos = ($post['dir'] == 0) ? $start_pos : count($postdata);
-               }
-               
-               if ($post['dir'] == '0')
-               {
-                       if ($pos == count($postdata))
-                       {
-                               $pos = $start_pos; //ÀèƬ
-                       }
-               }
-               else
-               {
-                       if ($pos == 0)
-                       {
-                               $pos = count($postdata); //ËöÈø
-                       }
+
+               if ($b_reply == FALSE)
+                       $end_position = ($dir == '0') ? $start_position : $count;
+
+               // Insert new comment
+               array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
+
+               if (PLUGIN_PCOMMENT_AUTO_LOG) {
+                       $_count = isset($vars['count']) ? $vars['count'] : '';
+                       plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
                }
-               
-               //¥³¥á¥ó¥È¤òÁÞÆþ
-               array_splice($postdata,$pos,0,str_repeat('-',$level)."$msg\n");
-               
-               $postdata = join('',$postdata);
+
+               $postdata = join('', $postdata);
        }
-       // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
-       page_write($page,$postdata);
-       
+       page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
+
+       if (PLUGIN_PCOMMENT_TIMESTAMP) {
+               if ($refer != '') pkwk_touch_file(get_filename($refer));
+               put_lastmodified();
+       }
+
        return $ret;
 }
-//¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
-function pcmt_check_arg($val, $key, &$params)
+
+// Auto log rotation
+function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
 {
-       foreach (array_keys($params) as $key)
-       {
-               if ($val == '')
-               {
-                       return;
-               }
-               if (strpos($key, strtolower($val)) === 0)
-               {
-                       $params[$key] = TRUE;
-                       return;
+       if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
+
+       $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
+       if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
+
+       if ($dir) {
+               // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
+               $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
+       } else {
+               // Bottom N comments
+               $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
+       }
+
+       // Decide new page name
+       $i = 0;
+       do {
+               ++$i;
+               $_page = $page . '/' . $i;
+       } while (is_page($_page));
+
+       page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
+
+       // Recurse :)
+       plugin_pcomment_auto_log($page, $dir, $count, $postdata);
+}
+
+// Check arguments
+function plugin_pcomment_check_arg($val, & $params)
+{
+       if ($val != '') {
+               $l_val = strtolower($val);
+               foreach (array_keys($params) as $key) {
+                       if (strpos($key, $l_val) === 0) {
+                               $params[$key] = TRUE;
+                               return;
+                       }
                }
        }
+
        $params['_args'][] = $val;
 }
-function pcmt_get_comments($page,$count,$dir,$reply)
+
+function plugin_pcomment_get_comments($page, $count, $dir, $reply)
 {
+       global $_msg_pcomment_restrict;
+
+       if (! check_readable($page, false, false))
+               return array(str_replace('$1', $page, $_msg_pcomment_restrict));
+
+       $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
+
        $data = get_source($page);
-       
-       if (!is_array($data))
-       {
-               return array('',0);
-       }
+       $data = preg_replace('/^#pcomment\(?.*/i', '', $data);  // Avoid eternal recurse
 
-       $digest = md5(join('',$data));
+       if (! is_array($data)) return array('', 0);
 
-       //¥³¥á¥ó¥È¤ò»ØÄꤵ¤ì¤¿·ï¿ô¤À¤±ÀÚ¤ê¼è¤ë
-       if ($dir)
-       {
-               $data = array_reverse($data);
-       }
-       $num = $cnt = 0;
-       $cmts = array();
-       foreach ($data as $line)
-       {
-               if ($count > 0 and $dir and $cnt == $count)
-               {
-                       break;
-               }
-               if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $line, $matches))
-               {
-                       if ($count > 0 and strlen($matches[1]) == 1 and ++$cnt > $count)
-                       {
-                               break; 
-                       }
-                       if ($reply)
-                       {
+       $digest = md5(join('', $data));
+
+       // Get latest N comments
+       $num  = $cnt     = 0;
+       $cmts = $matches = array();
+       if ($dir) $data = array_reverse($data);
+       foreach ($data as $line) {
+               if ($count > 0 && $dir && $cnt == $count) break;
+
+               if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
+                       if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
+
+                       // Ready for radio-buttons
+                       if ($reply) {
                                ++$num;
-                               $cmts[] = "$matches[1]\x01$num\x02".md5($matches[2])."\x03$matches[2]\n";
-                       }
-                       else
-                       {
-                               $cmts[] = $line;
+                               $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
+                                       md5($matches[2]) . "\x03" . $matches[2] . "\n";
+                               continue;
                        }
                }
-               else
-               {
-                       $cmts[] = $line;
-               }
+               $cmts[] = $line;
        }
        $data = $cmts;
-       if ($dir)
-       {
-               $data = array_reverse($data);
-       }
-       unset($cmts);
+       if ($dir) $data = array_reverse($data);
+       unset($cmts, $matches);
 
-       //¥³¥á¥ó¥È¤è¤êÁ°¤Î¥Ç¡¼¥¿¤ò¼è¤ê½ü¤¯¡£
-       while (count($data) > 0 and substr($data[0],0,1) != '-')
-       {
+       // Remove lines before comments
+       while (! empty($data) && substr($data[0], 0, 1) != '-')
                array_shift($data);
-       }
 
-       //htmlÊÑ´¹
        $comments = convert_html($data);
        unset($data);
 
-       //¥³¥á¥ó¥È¤Ë¥é¥¸¥ª¥Ü¥¿¥ó¤Î°õ¤ò¤Ä¤±¤ë
+       // Add radio buttons
        if ($reply)
-       {
-               $comments = preg_replace("/\x01(\d+)\x02(.*)\x03/",'<input type="radio" name="reply" value="$2" tabindex="$1" accesskey="c" />', $comments);
-       }
-       return array($comments,$digest);
+               $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
+                       '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
+                       $comments);
+
+       return array($comments, $digest);
 }
 ?>