OSDN Git Service

BugTrack2/372 Add auth group - set of multi users on page permission
[pukiwiki/pukiwiki.git] / plugin / pcomment.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: pcomment.inc.php,v 1.48 2011/01/25 15:01:01 henoheno Exp $
4 //
5 // pcomment plugin - Show/Insert comments into specified (another) page
6 //
7 // Usage: #pcomment([page][,max][,options])
8 //
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)
13 //
14 // Options:
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
18
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
22
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);
27
28 // Auto log rotation
29 define('PLUGIN_PCOMMENT_AUTO_LOG', 0); // 0:off 1-N:number of comments per page
30
31 // Update recording page's timestamp instead of parent's page itself
32 define('PLUGIN_PCOMMENT_TIMESTAMP', 0);
33
34 // ----
35 define('PLUGIN_PCOMMENT_FORMAT_NAME',   '[[$name]]');
36 define('PLUGIN_PCOMMENT_FORMAT_MSG',    '$msg');
37 define('PLUGIN_PCOMMENT_FORMAT_NOW',    '&new{$now};');
38
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");
42
43 function plugin_pcomment_action()
44 {
45         global $vars;
46
47         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
48
49         if (! isset($vars['msg']) || $vars['msg'] == '') return array();
50         $refer = isset($vars['refer']) ? $vars['refer'] : '';
51
52         $retval = plugin_pcomment_insert();
53         if ($retval['collided']) {
54                 $vars['page'] = $refer;
55                 return $retval;
56         }
57
58         pkwk_headers_sent();
59         header('Location: ' . get_script_uri() . '?' . pagename_urlencode($refer));
60         exit;
61 }
62
63 function plugin_pcomment_convert()
64 {
65         global $vars;
66         global $_pcmt_messages;
67
68         $params = array(
69                 'noname'=>FALSE,
70                 'nodate'=>FALSE,
71                 'below' =>FALSE,
72                 'above' =>FALSE,
73                 'reply' =>FALSE,
74                 '_args' =>array()
75         );
76
77         foreach(func_get_args() as $arg)
78                 plugin_pcomment_check_arg($arg, $params);
79
80         $vars_page = isset($vars['page']) ? $vars['page'] : '';
81         if (isset($params['_args'][0]) && $params['_args'][0] != '') {
82                 $page = $params['_args'][0];
83         } else {
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))) {
92                                 $page = $page_compat;
93                         }
94                 }
95         }
96         $count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
97         if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
98
99         $_page = get_fullname(strip_bracket($page), $vars_page);
100         if (!is_pagename($_page))
101                 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
102
103         $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
104         if ($params['below']) {
105                 $dir = 0;
106         } elseif ($params['above']) {
107                 $dir = 1;
108         }
109
110         list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
111
112         if (PKWK_READONLY) {
113                 $form_start = $form = $form_end = '';
114         } else {
115                 // Show a form
116
117                 if ($params['noname']) {
118                         $title = $_pcmt_messages['msg_comment'];
119                         $name = '';
120                 } else {
121                         $title = $_pcmt_messages['btn_name'];
122                         $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
123                 }
124
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 . '" />';
128
129                 $s_page   = htmlsc($page);
130                 $s_refer  = htmlsc($vars_page);
131                 $s_nodate = htmlsc($params['nodate']);
132                 $s_count  = htmlsc($count);
133
134                 $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
135                 $form = <<<EOD
136   <div>
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']}" />
146   </div>
147 EOD;
148                 $form_end = '</form>' . "\n";
149         }
150
151         if (! is_page($_page)) {
152                 $link   = make_pagelink($_page);
153                 $recent = $_pcmt_messages['msg_none'];
154         } else {
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) : '';
158         }
159
160         if ($dir) {
161                 return '<div>' .
162                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
163                         $form_start .
164                                 $comments . "\n" .
165                                 $form .
166                         $form_end .
167                         '</div>' . "\n";
168         } else {
169                 return '<div>' .
170                         $form_start .
171                                 $form .
172                                 $comments. "\n" .
173                         $form_end .
174                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
175                         '</div>' . "\n";
176         }
177 }
178
179 function plugin_pcomment_insert()
180 {
181         global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
182
183         $refer = isset($vars['refer']) ? $vars['refer'] : '';
184         $page  = isset($vars['page'])  ? $vars['page']  : '';
185         $page  = get_fullname($page, $refer);
186
187         if (! is_pagename($page))
188                 return array(
189                         'msg' =>'Invalid page name',
190                         'body'=>'Cannot add comment' ,
191                         'collided'=>TRUE
192                 );
193
194         check_editable($page, true, true);
195
196         $ret = array('msg' => $_title_updated, 'collided' => FALSE);
197
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);
207         }
208
209         $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
210         if ($reply_hash || ! is_page($page)) {
211                 $msg = preg_replace('/^\-+/', '', $msg);
212         }
213         $msg = rtrim($msg);
214
215         if (! is_page($page)) {
216                 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
217                         '-' . $msg . "\n";
218         } else {
219                 $postdata = get_source($page);
220                 $count    = count($postdata);
221
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'];
226                 }
227
228                 $start_position = 0;
229                 while ($start_position < $count) {
230                         if (preg_match('/^\-/', $postdata[$start_position])) break;
231                         ++$start_position;
232                 }
233                 $end_position = $start_position;
234
235                 $dir = isset($vars['dir']) ? $vars['dir'] : '';
236
237                 // Find the comment to reply
238                 $level   = 1;
239                 $b_reply = FALSE;
240                 if ($reply_hash != '') {
241                         while ($end_position < $count) {
242                                 $matches = array();
243                                 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
244                                         && md5($matches[2]) === $reply_hash)
245                                 {
246                                         $b_reply = TRUE;
247                                         $level   = strlen($matches[1]) + 1;
248
249                                         while ($end_position < $count) {
250                                                 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
251                                                         && strlen($matches[1]) < $level) break;
252                                                 ++$end_position;
253                                         }
254                                         break;
255                                 }
256                         }
257                 }
258
259                 if ($b_reply == FALSE)
260                         $end_position = ($dir == '0') ? $start_position : $count;
261
262                 // Insert new comment
263                 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
264
265                 if (PLUGIN_PCOMMENT_AUTO_LOG) {
266                         $_count = isset($vars['count']) ? $vars['count'] : '';
267                         plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
268                 }
269
270                 $postdata = join('', $postdata);
271         }
272         page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
273
274         if (PLUGIN_PCOMMENT_TIMESTAMP) {
275                 if ($refer != '') pkwk_touch_file(get_filename($refer));
276                 put_lastmodified();
277         }
278
279         return $ret;
280 }
281
282 // Auto log rotation
283 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
284 {
285         if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
286
287         $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
288         if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
289
290         if ($dir) {
291                 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
292                 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
293         } else {
294                 // Bottom N comments
295                 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
296         }
297
298         // Decide new page name
299         $i = 0;
300         do {
301                 ++$i;
302                 $_page = $page . '/' . $i;
303         } while (is_page($_page));
304
305         page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
306
307         // Recurse :)
308         plugin_pcomment_auto_log($page, $dir, $count, $postdata);
309 }
310
311 // Check arguments
312 function plugin_pcomment_check_arg($val, & $params)
313 {
314         if ($val != '') {
315                 $l_val = strtolower($val);
316                 foreach (array_keys($params) as $key) {
317                         if (strpos($key, $l_val) === 0) {
318                                 $params[$key] = TRUE;
319                                 return;
320                         }
321                 }
322         }
323
324         $params['_args'][] = $val;
325 }
326
327 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
328 {
329         global $_msg_pcomment_restrict;
330
331         if (! check_readable($page, false, false))
332                 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
333
334         $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
335
336         $data = get_source($page);
337         $data = preg_replace('/^#pcomment\(?.*/i', '', $data);  // Avoid eternal recurse
338
339         if (! is_array($data)) return array('', 0);
340
341         $digest = md5(join('', $data));
342
343         // Get latest N comments
344         $num  = $cnt     = 0;
345         $cmts = $matches = array();
346         if ($dir) $data = array_reverse($data);
347         foreach ($data as $line) {
348                 if ($count > 0 && $dir && $cnt == $count) break;
349
350                 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
351                         if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
352
353                         // Ready for radio-buttons
354                         if ($reply) {
355                                 ++$num;
356                                 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
357                                         md5($matches[2]) . "\x03" . $matches[2] . "\n";
358                                 continue;
359                         }
360                 }
361                 $cmts[] = $line;
362         }
363         $data = $cmts;
364         if ($dir) $data = array_reverse($data);
365         unset($cmts, $matches);
366
367         // Remove lines before comments
368         while (! empty($data) && substr($data[0], 0, 1) != '-')
369                 array_shift($data);
370
371         $comments = convert_html($data);
372         unset($data);
373
374         // Add radio buttons
375         if ($reply)
376                 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
377                         '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
378                         $comments);
379
380         return array($comments, $digest);
381 }
382