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)
21 case 'ja': define('PLUGIN_PCOMMENT_PAGE', '[[コメント/%s]]'); break;
22 default: define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]'); break;
25 define('PLUGIN_PCOMMENT_NUM_COMMENTS', 10); // Default 'latest N posts'
26 define('PLUGIN_PCOMMENT_DIRECTION_DEFAULT', 1); // 1: above 0: below
27 define('PLUGIN_PCOMMENT_SIZE_MSG', 70);
28 define('PLUGIN_PCOMMENT_SIZE_NAME', 15);
31 define('PLUGIN_PCOMMENT_AUTO_LOG', 0); // 0:off 1-N:number of comments per page
33 // Update recording page's timestamp instead of parent's page itself
34 define('PLUGIN_PCOMMENT_TIMESTAMP', 0);
37 define('PLUGIN_PCOMMENT_FORMAT_NAME', '[[$name]]');
38 define('PLUGIN_PCOMMENT_FORMAT_MSG', '$msg');
39 define('PLUGIN_PCOMMENT_FORMAT_NOW', '&new{$now};');
41 // "\x01", "\x02", "\x03", and "\x08" are used just as markers
42 define('PLUGIN_PCOMMENT_FORMAT_STRING',
43 "\x08" . 'MSG' . "\x08" . ' -- ' . "\x08" . 'NAME' . "\x08" . ' ' . "\x08" . 'DATE' . "\x08");
45 function plugin_pcomment_action()
49 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
51 if (! isset($vars['msg']) || $vars['msg'] == '') return array();
52 $refer = isset($vars['refer']) ? $vars['refer'] : '';
54 $retval = plugin_pcomment_insert();
55 if ($retval['collided']) {
56 $vars['page'] = $refer;
61 header('Location: ' . get_script_uri() . '?' . pagename_urlencode($refer));
65 function plugin_pcomment_convert()
68 global $_pcmt_messages;
79 foreach(func_get_args() as $arg)
80 plugin_pcomment_check_arg($arg, $params);
82 $vars_page = isset($vars['page']) ? $vars['page'] : '';
83 $page = (isset($params['_args'][0]) && $params['_args'][0] != '') ? $params['_args'][0] :
84 sprintf(PLUGIN_PCOMMENT_PAGE, strip_bracket($vars_page));
85 $count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
86 if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
88 $_page = get_fullname(strip_bracket($page), $vars_page);
89 if (!is_pagename($_page))
90 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
92 $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
93 if ($params['below']) {
95 } elseif ($params['above']) {
99 list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
102 $form_start = $form = $form_end = '';
106 if ($params['noname']) {
107 $title = $_pcmt_messages['msg_comment'];
110 $title = $_pcmt_messages['btn_name'];
111 $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
114 $radio = $params['reply'] ?
115 '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
116 $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
118 $s_page = htmlsc($page);
119 $s_refer = htmlsc($vars_page);
120 $s_nodate = htmlsc($params['nodate']);
121 $s_count = htmlsc($count);
123 $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
126 <input type="hidden" name="digest" value="$digest" />
127 <input type="hidden" name="plugin" value="pcomment" />
128 <input type="hidden" name="refer" value="$s_refer" />
129 <input type="hidden" name="page" value="$s_page" />
130 <input type="hidden" name="nodate" value="$s_nodate" />
131 <input type="hidden" name="dir" value="$dir" />
132 <input type="hidden" name="count" value="$count" />
133 $radio $title $name $comment
134 <input type="submit" value="{$_pcmt_messages['btn_comment']}" />
137 $form_end = '</form>' . "\n";
140 if (! is_page($_page)) {
141 $link = make_pagelink($_page);
142 $recent = $_pcmt_messages['msg_none'];
144 $msg = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
145 $link = make_pagelink($_page, $msg);
146 $recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
151 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
163 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
168 function plugin_pcomment_insert()
170 global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
172 $refer = isset($vars['refer']) ? $vars['refer'] : '';
173 $page = isset($vars['page']) ? $vars['page'] : '';
174 $page = get_fullname($page, $refer);
176 if (! is_pagename($page))
178 'msg' =>'Invalid page name',
179 'body'=>'Cannot add comment' ,
183 check_editable($page, true, true);
185 $ret = array('msg' => $_title_updated, 'collided' => FALSE);
187 $msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
188 $name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
189 $name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
190 $date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
191 str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
192 if ($date != '' || $name != '') {
193 $msg = str_replace("\x08" . 'MSG' . "\x08", $msg, PLUGIN_PCOMMENT_FORMAT_STRING);
194 $msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
195 $msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
198 $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
199 if ($reply_hash || ! is_page($page)) {
200 $msg = preg_replace('/^\-+/', '', $msg);
204 if (! is_page($page)) {
205 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
208 $postdata = get_source($page);
209 $count = count($postdata);
211 $digest = isset($vars['digest']) ? $vars['digest'] : '';
212 if (md5(join('', $postdata)) !== $digest) {
213 $ret['msg'] = $_pcmt_messages['title_collided'];
214 $ret['body'] = $_pcmt_messages['msg_collided'];
218 while ($start_position < $count) {
219 if (preg_match('/^\-/', $postdata[$start_position])) break;
222 $end_position = $start_position;
224 $dir = isset($vars['dir']) ? $vars['dir'] : '';
226 // Find the comment to reply
229 if ($reply_hash != '') {
230 while ($end_position < $count) {
232 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
233 && md5($matches[2]) === $reply_hash)
236 $level = strlen($matches[1]) + 1;
238 while ($end_position < $count) {
239 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
240 && strlen($matches[1]) < $level) break;
248 if ($b_reply == FALSE)
249 $end_position = ($dir == '0') ? $start_position : $count;
251 // Insert new comment
252 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
254 if (PLUGIN_PCOMMENT_AUTO_LOG) {
255 $_count = isset($vars['count']) ? $vars['count'] : '';
256 plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
259 $postdata = join('', $postdata);
261 page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
263 if (PLUGIN_PCOMMENT_TIMESTAMP) {
264 if ($refer != '') pkwk_touch_file(get_filename($refer));
272 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
274 if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
276 $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
277 if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
280 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
281 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
284 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
287 // Decide new page name
291 $_page = $page . '/' . $i;
292 } while (is_page($_page));
294 page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
297 plugin_pcomment_auto_log($page, $dir, $count, $postdata);
301 function plugin_pcomment_check_arg($val, & $params)
304 $l_val = strtolower($val);
305 foreach (array_keys($params) as $key) {
306 if (strpos($key, $l_val) === 0) {
307 $params[$key] = TRUE;
313 $params['_args'][] = $val;
316 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
318 global $_msg_pcomment_restrict;
320 if (! check_readable($page, false, false))
321 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
323 $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
325 $data = get_source($page);
326 $data = preg_replace('/^#pcomment\(?.*/i', '', $data); // Avoid eternal recurse
328 if (! is_array($data)) return array('', 0);
330 $digest = md5(join('', $data));
332 // Get latest N comments
334 $cmts = $matches = array();
335 if ($dir) $data = array_reverse($data);
336 foreach ($data as $line) {
337 if ($count > 0 && $dir && $cnt == $count) break;
339 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
340 if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
342 // Ready for radio-buttons
345 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
346 md5($matches[2]) . "\x03" . $matches[2] . "\n";
353 if ($dir) $data = array_reverse($data);
354 unset($cmts, $matches);
356 // Remove lines before comments
357 while (! empty($data) && substr($data[0], 0, 1) != '-')
360 $comments = convert_html($data);
365 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
366 '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
369 return array($comments, $digest);