2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: pcomment.inc.php,v 1.48 2011/01/25 15:01:01 henoheno Exp $
5 // pcomment plugin - Show/Insert comments into specified (another) page
7 // Usage: #pcomment([page][,max][,options])
9 // page -- An another page-name that holds comments
10 // (default:PLUGIN_PCOMMENT_PAGE)
11 // max -- Max number of recent comments to show
12 // (0:Show all, default:PLUGIN_PCOMMENT_NUM_COMMENTS)
15 // above -- Comments are listed above the #pcomment (added by chronological order)
16 // below -- Comments are listed below the #pcomment (by reverse order)
17 // reply -- Show radio buttons allow to specify where to reply
19 // Default recording page name (%s = $vars['page'] = original page name)
20 define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]');
21 define('PLUGIN_PCOMMENT_PAGE_COMPATIBLE', '[[コメント/%s]]'); // for backword compatible of 'ja' pcomment
23 define('PLUGIN_PCOMMENT_NUM_COMMENTS', 10); // Default 'latest N posts'
24 define('PLUGIN_PCOMMENT_DIRECTION_DEFAULT', 1); // 1: above 0: below
25 define('PLUGIN_PCOMMENT_SIZE_MSG', 70);
26 define('PLUGIN_PCOMMENT_SIZE_NAME', 15);
29 define('PLUGIN_PCOMMENT_AUTO_LOG', 0); // 0:off 1-N:number of comments per page
31 // Update recording page's timestamp instead of parent's page itself
32 define('PLUGIN_PCOMMENT_TIMESTAMP', 0);
35 define('PLUGIN_PCOMMENT_FORMAT_NAME', '[[$name]]');
36 define('PLUGIN_PCOMMENT_FORMAT_MSG', '$msg');
37 define('PLUGIN_PCOMMENT_FORMAT_NOW', '&new{$now};');
39 // "\x01", "\x02", "\x03", and "\x08" are used just as markers
40 define('PLUGIN_PCOMMENT_FORMAT_STRING',
41 "\x08" . 'MSG' . "\x08" . ' -- ' . "\x08" . 'NAME' . "\x08" . ' ' . "\x08" . 'DATE' . "\x08");
43 function plugin_pcomment_action()
47 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
49 if (! isset($vars['msg']) || $vars['msg'] == '') return array();
50 $refer = isset($vars['refer']) ? $vars['refer'] : '';
52 $retval = plugin_pcomment_insert();
53 if ($retval['collided']) {
54 $vars['page'] = $refer;
59 header('Location: ' . get_script_uri() . '?' . pagename_urlencode($refer));
63 function plugin_pcomment_convert()
66 global $_pcmt_messages;
77 foreach(func_get_args() as $arg)
78 plugin_pcomment_check_arg($arg, $params);
80 $vars_page = isset($vars['page']) ? $vars['page'] : '';
81 if (isset($params['_args'][0]) && $params['_args'][0] != '') {
82 $page = $params['_args'][0];
84 $raw_vars_page = strip_bracket($vars_page);
85 $page = sprintf(PLUGIN_PCOMMENT_PAGE, $raw_vars_page);
86 $raw_page = strip_bracket($page);
87 if (!is_page($raw_page)) {
88 // If the page doesn't exist, search backward-compatible page
89 // If only compatible page exists, set the page as comment target
90 $page_compat = sprintf(PLUGIN_PCOMMENT_PAGE_COMPATIBLE, $raw_vars_page);
91 if (is_page(strip_bracket($page_compat))) {
96 $count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
97 if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
99 $_page = get_fullname(strip_bracket($page), $vars_page);
100 if (!is_pagename($_page))
101 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
103 $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
104 if ($params['below']) {
106 } elseif ($params['above']) {
110 list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
113 $form_start = $form = $form_end = '';
117 if ($params['noname']) {
118 $title = $_pcmt_messages['msg_comment'];
121 $title = $_pcmt_messages['btn_name'];
122 $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
125 $radio = $params['reply'] ?
126 '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
127 $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
129 $s_page = htmlsc($page);
130 $s_refer = htmlsc($vars_page);
131 $s_nodate = htmlsc($params['nodate']);
132 $s_count = htmlsc($count);
134 $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
137 <input type="hidden" name="digest" value="$digest" />
138 <input type="hidden" name="plugin" value="pcomment" />
139 <input type="hidden" name="refer" value="$s_refer" />
140 <input type="hidden" name="page" value="$s_page" />
141 <input type="hidden" name="nodate" value="$s_nodate" />
142 <input type="hidden" name="dir" value="$dir" />
143 <input type="hidden" name="count" value="$count" />
144 $radio $title $name $comment
145 <input type="submit" value="{$_pcmt_messages['btn_comment']}" />
148 $form_end = '</form>' . "\n";
151 if (! is_page($_page)) {
152 $link = make_pagelink($_page);
153 $recent = $_pcmt_messages['msg_none'];
155 $msg = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
156 $link = make_pagelink($_page, $msg);
157 $recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
162 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
174 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
179 function plugin_pcomment_insert()
181 global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
183 $refer = isset($vars['refer']) ? $vars['refer'] : '';
184 $page = isset($vars['page']) ? $vars['page'] : '';
185 $page = get_fullname($page, $refer);
187 if (! is_pagename($page))
189 'msg' =>'Invalid page name',
190 'body'=>'Cannot add comment' ,
194 check_editable($page, true, true);
196 $ret = array('msg' => $_title_updated, 'collided' => FALSE);
198 $msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
199 $name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
200 $name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
201 $date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
202 str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
203 if ($date != '' || $name != '') {
204 $msg = str_replace("\x08" . 'MSG' . "\x08", $msg, PLUGIN_PCOMMENT_FORMAT_STRING);
205 $msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
206 $msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
209 $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
210 if ($reply_hash || ! is_page($page)) {
211 $msg = preg_replace('/^\-+/', '', $msg);
215 if (! is_page($page)) {
216 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
219 $postdata = get_source($page);
220 $count = count($postdata);
222 $digest = isset($vars['digest']) ? $vars['digest'] : '';
223 if (md5(join('', $postdata)) !== $digest) {
224 $ret['msg'] = $_pcmt_messages['title_collided'];
225 $ret['body'] = $_pcmt_messages['msg_collided'];
229 while ($start_position < $count) {
230 if (preg_match('/^\-/', $postdata[$start_position])) break;
233 $end_position = $start_position;
235 $dir = isset($vars['dir']) ? $vars['dir'] : '';
237 // Find the comment to reply
240 if ($reply_hash != '') {
241 while ($end_position < $count) {
243 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
244 && md5($matches[2]) === $reply_hash)
247 $level = strlen($matches[1]) + 1;
249 while ($end_position < $count) {
250 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
251 && strlen($matches[1]) < $level) break;
259 if ($b_reply == FALSE)
260 $end_position = ($dir == '0') ? $start_position : $count;
262 // Insert new comment
263 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
265 if (PLUGIN_PCOMMENT_AUTO_LOG) {
266 $_count = isset($vars['count']) ? $vars['count'] : '';
267 plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
270 $postdata = join('', $postdata);
272 page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
274 if (PLUGIN_PCOMMENT_TIMESTAMP) {
275 if ($refer != '') pkwk_touch_file(get_filename($refer));
283 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
285 if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
287 $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
288 if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
291 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
292 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
295 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
298 // Decide new page name
302 $_page = $page . '/' . $i;
303 } while (is_page($_page));
305 page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
308 plugin_pcomment_auto_log($page, $dir, $count, $postdata);
312 function plugin_pcomment_check_arg($val, & $params)
315 $l_val = strtolower($val);
316 foreach (array_keys($params) as $key) {
317 if (strpos($key, $l_val) === 0) {
318 $params[$key] = TRUE;
324 $params['_args'][] = $val;
327 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
329 global $_msg_pcomment_restrict;
331 if (! check_readable($page, false, false))
332 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
334 $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
336 $data = get_source($page);
337 $data = preg_replace('/^#pcomment\(?.*/i', '', $data); // Avoid eternal recurse
339 if (! is_array($data)) return array('', 0);
341 $digest = md5(join('', $data));
343 // Get latest N comments
345 $cmts = $matches = array();
346 if ($dir) $data = array_reverse($data);
347 foreach ($data as $line) {
348 if ($count > 0 && $dir && $cnt == $count) break;
350 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
351 if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
353 // Ready for radio-buttons
356 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
357 md5($matches[2]) . "\x03" . $matches[2] . "\n";
364 if ($dir) $data = array_reverse($data);
365 unset($cmts, $matches);
367 // Remove lines before comments
368 while (! empty($data) && substr($data[0], 0, 1) != '-')
371 $comments = convert_html($data);
376 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
377 '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
380 return array($comments, $digest);