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() . '?' . rawurlencode($refer));
65 function plugin_pcomment_convert()
68 global $_pcmt_messages;
81 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
82 $args = func_get_args(); // with array_walk()
83 array_walk($args, 'plugin_pcomment_check_arg', & $params);
85 $vars_page = isset($vars['page']) ? $vars['page'] : '';
86 $page = (isset($params['_args'][0]) && $params['_args'][0] != '') ? $params['_args'][0] :
87 sprintf(PLUGIN_PCOMMENT_PAGE, strip_bracket($vars_page));
88 $count = (isset($params['_args'][1]) && $params['_args'][1] != '') ? $params['_args'][1] : 0;
89 if ($count == 0 && $count !== '0')
90 $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
92 $_page = get_fullname(strip_bracket($page), $vars_page);
93 if (!is_pagename($_page))
94 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
96 $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
97 if ($params['below']) {
99 } elseif ($params['above']) {
103 list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
106 $form_start = $form = $form_end = '';
110 if ($params['noname']) {
111 $title = $_pcmt_messages['msg_comment'];
114 $title = $_pcmt_messages['btn_name'];
115 $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
118 $radio = $params['reply'] ?
119 '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
120 $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
122 $s_page = htmlsc($page);
123 $s_refer = htmlsc($vars_page);
124 $s_nodate = htmlsc($params['nodate']);
125 $s_count = htmlsc($count);
127 $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
130 <input type="hidden" name="digest" value="$digest" />
131 <input type="hidden" name="plugin" value="pcomment" />
132 <input type="hidden" name="refer" value="$s_refer" />
133 <input type="hidden" name="page" value="$s_page" />
134 <input type="hidden" name="nodate" value="$s_nodate" />
135 <input type="hidden" name="dir" value="$dir" />
136 <input type="hidden" name="count" value="$count" />
137 $radio $title $name $comment
138 <input type="submit" value="{$_pcmt_messages['btn_comment']}" />
141 $form_end = '</form>' . "\n";
144 if (! is_page($_page)) {
145 $link = make_pagelink($_page);
146 $recent = $_pcmt_messages['msg_none'];
148 $msg = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
149 $link = make_pagelink($_page, $msg);
150 $recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
155 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
167 '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
172 function plugin_pcomment_insert()
174 global $script, $vars, $now;
175 global $_title_updated, $_no_name, $_pcmt_messages;
177 $refer = isset($vars['refer']) ? $vars['refer'] : '';
178 $page = isset($vars['page']) ? $vars['page'] : '';
179 $page = get_fullname($page, $refer);
181 if (! is_pagename($page))
183 'msg' =>'Invalid page name',
184 'body'=>'Cannot add comment' ,
188 check_editable($page, true, true);
190 $ret = array('msg' => $_title_updated, 'collided' => FALSE);
192 $msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
193 $name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
194 $name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
195 $date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
196 str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
197 if ($date != '' || $name != '') {
198 $msg = str_replace("\x08" . 'MSG' . "\x08", $msg, PLUGIN_PCOMMENT_FORMAT_STRING);
199 $msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
200 $msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
203 $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
204 if ($reply_hash || ! is_page($page)) {
205 $msg = preg_replace('/^\-+/', '', $msg);
209 if (! is_page($page)) {
210 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
213 $postdata = get_source($page);
214 $count = count($postdata);
216 $digest = isset($vars['digest']) ? $vars['digest'] : '';
217 if (md5(join('', $postdata)) != $digest) {
218 $ret['msg'] = $_pcmt_messages['title_collided'];
219 $ret['body'] = $_pcmt_messages['msg_collided'];
223 while ($start_position < $count) {
224 if (preg_match('/^\-/', $postdata[$start_position])) break;
227 $end_position = $start_position;
229 $dir = isset($vars['dir']) ? $vars['dir'] : '';
231 // Find the comment to reply
234 if ($reply_hash != '') {
235 while ($end_position < $count) {
237 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
238 && md5($matches[2]) == $reply_hash)
241 $level = strlen($matches[1]) + 1;
243 while ($end_position < $count) {
244 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
245 && strlen($matches[1]) < $level) break;
253 if ($b_reply == FALSE)
254 $end_position = ($dir == '0') ? $start_position : $count;
256 // Insert new comment
257 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
259 if (PLUGIN_PCOMMENT_AUTO_LOG) {
260 $_count = isset($vars['count']) ? $vars['count'] : '';
261 plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
264 $postdata = join('', $postdata);
266 page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
268 if (PLUGIN_PCOMMENT_TIMESTAMP) {
269 if ($refer != '') pkwk_touch_file(get_filename($refer));
277 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
279 if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
281 $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
282 if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
285 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
286 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
289 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
292 // Decide new page name
296 $_page = $page . '/' . $i;
297 } while (is_page($_page));
299 page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
302 plugin_pcomment_auto_log($page, $dir, $count, $postdata);
306 function plugin_pcomment_check_arg($val, $key, & $params)
309 $l_val = strtolower($val);
310 foreach (array_keys($params) as $key) {
311 if (strpos($key, $l_val) === 0) {
312 $params[$key] = TRUE;
318 $params['_args'][] = $val;
321 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
323 global $_msg_pcomment_restrict;
325 if (! check_readable($page, false, false))
326 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
328 $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
330 $data = get_source($page);
331 $data = preg_replace('/^#pcomment\(?.*/i', '', $data); // Avoid eternal recurse
333 if (! is_array($data)) return array('', 0);
335 $digest = md5(join('', $data));
337 // Get latest N comments
339 $cmts = $matches = array();
340 if ($dir) $data = array_reverse($data);
341 foreach ($data as $line) {
342 if ($count > 0 && $dir && $cnt == $count) break;
344 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
345 if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
347 // Ready for radio-buttons
350 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
351 md5($matches[2]) . "\x03" . $matches[2] . "\n";
358 if ($dir) $data = array_reverse($data);
359 unset($cmts, $matches);
361 // Remove lines before comments
362 while (! empty($data) && substr($data[0], 0, 1) != '-')
365 $comments = convert_html($data);
370 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
371 '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
374 return array($comments, $digest);