OSDN Git Service

BugTrack/2283 ls2 plugin: Improve include loop handling
[pukiwiki/pukiwiki.git] / plugin / pcomment.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // pcomment.inc.php
4 // Copyright 2002-2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // pcomment plugin - Show/Insert comments into specified (another) page
8
9 // Usage: #pcomment([page][,max][,options])
10 //
11 //   page -- An another page-name that holds comments
12 //           (default:PLUGIN_PCOMMENT_PAGE)
13 //   max  -- Max number of recent comments to show
14 //           (0:Show all, default:PLUGIN_PCOMMENT_NUM_COMMENTS)
15 //
16 // Options:
17 //   above -- Comments are listed above the #pcomment (added by chronological order)
18 //   below -- Comments are listed below the #pcomment (by reverse order)
19 //   reply -- Show radio buttons allow to specify where to reply
20
21 // Default recording page name (%s = $vars['page'] = original page name)
22 define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]');
23 define('PLUGIN_PCOMMENT_PAGE_COMPATIBLE', '[[コメント/%s]]'); // for backword compatible of 'ja' pcomment
24
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);
29
30 // Auto log rotation
31 define('PLUGIN_PCOMMENT_AUTO_LOG', 0); // 0:off 1-N:number of comments per page
32
33 // Update recording page's timestamp instead of parent's page itself
34 define('PLUGIN_PCOMMENT_TIMESTAMP', 0);
35
36 // ----
37 define('PLUGIN_PCOMMENT_FORMAT_NAME',   '[[$name]]');
38 define('PLUGIN_PCOMMENT_FORMAT_MSG',    '$msg');
39 define('PLUGIN_PCOMMENT_FORMAT_NOW',    '&new{$now};');
40
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");
44
45 function plugin_pcomment_action()
46 {
47         global $vars;
48
49         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
50
51         if (! isset($vars['msg']) || $vars['msg'] == '') return array();
52         $refer = isset($vars['refer']) ? $vars['refer'] : '';
53
54         $retval = plugin_pcomment_insert();
55         if ($retval['collided']) {
56                 $vars['page'] = $refer;
57                 return $retval;
58         }
59
60         pkwk_headers_sent();
61         header('Location: ' . get_page_uri($refer, PKWK_URI_ROOT));
62         exit;
63 }
64
65 function plugin_pcomment_convert()
66 {
67         global $vars;
68         global $_pcmt_messages;
69
70         $params = array(
71                 'noname'=>FALSE,
72                 'nodate'=>FALSE,
73                 'below' =>FALSE,
74                 'above' =>FALSE,
75                 'reply' =>FALSE,
76                 '_args' =>array()
77         );
78
79         foreach(func_get_args() as $arg)
80                 plugin_pcomment_check_arg($arg, $params);
81
82         $vars_page = isset($vars['page']) ? $vars['page'] : '';
83         if (isset($params['_args'][0]) && $params['_args'][0] != '') {
84                 $page = $params['_args'][0];
85         } else {
86                 $raw_vars_page = strip_bracket($vars_page);
87                 $page = sprintf(PLUGIN_PCOMMENT_PAGE, $raw_vars_page);
88                 $raw_page = strip_bracket($page);
89                 if (!is_page($raw_page)) {
90                         // If the page doesn't exist, search backward-compatible page
91                         // If only compatible page exists, set the page as comment target
92                         $page_compat = sprintf(PLUGIN_PCOMMENT_PAGE_COMPATIBLE, $raw_vars_page);
93                         if (is_page(strip_bracket($page_compat))) {
94                                 $page = $page_compat;
95                         }
96                 }
97         }
98         $count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
99         if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
100
101         $_page = get_fullname(strip_bracket($page), $vars_page);
102         if (!is_pagename($_page))
103                 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
104
105         $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
106         if ($params['below']) {
107                 $dir = 0;
108         } elseif ($params['above']) {
109                 $dir = 1;
110         }
111
112         list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
113
114         if (PKWK_READONLY) {
115                 $form_start = $form = $form_end = '';
116         } else {
117                 // Show a form
118
119                 if ($params['noname']) {
120                         $title = $_pcmt_messages['msg_comment'];
121                         $name = '';
122                 } else {
123                         $title = $_pcmt_messages['btn_name'];
124                         $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
125                 }
126
127                 $radio   = $params['reply'] ?
128                         '<input type="radio" name="reply" value="0" tabindex="0" checked="checked" />' : '';
129                 $comment = '<input type="text" name="msg" size="' . PLUGIN_PCOMMENT_SIZE_MSG . '" />';
130
131                 $s_page   = htmlsc($page);
132                 $s_refer  = htmlsc($vars_page);
133                 $s_nodate = htmlsc($params['nodate']);
134                 $s_count  = htmlsc($count);
135
136                 $form_start = '<form action="' . get_base_uri() . '" method="post">' . "\n";
137                 $form = <<<EOD
138   <div>
139   <input type="hidden" name="digest" value="$digest" />
140   <input type="hidden" name="plugin" value="pcomment" />
141   <input type="hidden" name="refer"  value="$s_refer" />
142   <input type="hidden" name="page"   value="$s_page" />
143   <input type="hidden" name="nodate" value="$s_nodate" />
144   <input type="hidden" name="dir"    value="$dir" />
145   <input type="hidden" name="count"  value="$count" />
146   $radio $title $name $comment
147   <input type="submit" value="{$_pcmt_messages['btn_comment']}" />
148   </div>
149 EOD;
150                 $form_end = '</form>' . "\n";
151         }
152
153         if (! is_page($_page)) {
154                 $link   = make_pagelink($_page);
155                 $recent = $_pcmt_messages['msg_none'];
156         } else {
157                 $msg    = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
158                 $link   = make_pagelink($_page, $msg);
159                 $recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
160         }
161
162         if ($dir) {
163                 return '<div>' .
164                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
165                         $form_start .
166                                 $comments . "\n" .
167                                 $form .
168                         $form_end .
169                         '</div>' . "\n";
170         } else {
171                 return '<div>' .
172                         $form_start .
173                                 $form .
174                                 $comments. "\n" .
175                         $form_end .
176                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
177                         '</div>' . "\n";
178         }
179 }
180
181 function plugin_pcomment_insert()
182 {
183         global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
184
185         $refer = isset($vars['refer']) ? $vars['refer'] : '';
186         $page  = isset($vars['page'])  ? $vars['page']  : '';
187         $page  = get_fullname($page, $refer);
188
189         if (! is_pagename($page))
190                 return array(
191                         'msg' =>'Invalid page name',
192                         'body'=>'Cannot add comment' ,
193                         'collided'=>TRUE
194                 );
195
196         check_editable($page, true, true);
197
198         $ret = array('msg' => $_title_updated, 'collided' => FALSE);
199
200         $msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
201         $name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
202         $name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
203         $date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
204                 str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
205         if ($date != '' || $name != '') {
206                 $msg = str_replace("\x08" . 'MSG'  . "\x08", $msg,  PLUGIN_PCOMMENT_FORMAT_STRING);
207                 $msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
208                 $msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
209         }
210
211         $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
212         if ($reply_hash || ! is_page($page)) {
213                 $msg = preg_replace('/^\-+/', '', $msg);
214         }
215         $msg = rtrim($msg);
216
217         if (! is_page($page)) {
218                 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
219                         '-' . $msg . "\n";
220         } else {
221                 $postdata = get_source($page);
222                 $count    = count($postdata);
223
224                 $digest = isset($vars['digest']) ? $vars['digest'] : '';
225                 if (md5(join('', $postdata)) !== $digest) {
226                         $ret['msg']  = $_pcmt_messages['title_collided'];
227                         $ret['body'] = $_pcmt_messages['msg_collided'];
228                 }
229
230                 $start_position = 0;
231                 while ($start_position < $count) {
232                         if (preg_match('/^\-/', $postdata[$start_position])) break;
233                         ++$start_position;
234                 }
235                 $end_position = $start_position;
236
237                 $dir = isset($vars['dir']) ? $vars['dir'] : '';
238
239                 // Find the comment to reply
240                 $level   = 1;
241                 $b_reply = FALSE;
242                 if ($reply_hash != '') {
243                         while ($end_position < $count) {
244                                 $matches = array();
245                                 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
246                                         && md5($matches[2]) === $reply_hash)
247                                 {
248                                         $b_reply = TRUE;
249                                         $level   = strlen($matches[1]) + 1;
250
251                                         while ($end_position < $count) {
252                                                 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
253                                                         && strlen($matches[1]) < $level) break;
254                                                 ++$end_position;
255                                         }
256                                         break;
257                                 }
258                         }
259                 }
260
261                 if ($b_reply == FALSE)
262                         $end_position = ($dir == '0') ? $start_position : $count;
263
264                 // Insert new comment
265                 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
266
267                 if (PLUGIN_PCOMMENT_AUTO_LOG) {
268                         $_count = isset($vars['count']) ? $vars['count'] : '';
269                         plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
270                 }
271
272                 $postdata = join('', $postdata);
273         }
274         page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
275
276         if (PLUGIN_PCOMMENT_TIMESTAMP) {
277                 if ($refer != '') pkwk_touch_file(get_filename($refer));
278                 put_lastmodified();
279         }
280
281         return $ret;
282 }
283
284 // Auto log rotation
285 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
286 {
287         if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
288
289         $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
290         if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
291
292         if ($dir) {
293                 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
294                 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
295         } else {
296                 // Bottom N comments
297                 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
298         }
299
300         // Decide new page name
301         $i = 0;
302         do {
303                 ++$i;
304                 $_page = $page . '/' . $i;
305         } while (is_page($_page));
306
307         page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
308
309         // Recurse :)
310         plugin_pcomment_auto_log($page, $dir, $count, $postdata);
311 }
312
313 // Check arguments
314 function plugin_pcomment_check_arg($val, & $params)
315 {
316         if ($val != '') {
317                 $l_val = strtolower($val);
318                 foreach (array_keys($params) as $key) {
319                         if (strpos($key, $l_val) === 0) {
320                                 $params[$key] = TRUE;
321                                 return;
322                         }
323                 }
324         }
325
326         $params['_args'][] = $val;
327 }
328
329 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
330 {
331         global $_msg_pcomment_restrict;
332
333         if (! check_readable($page, false, false))
334                 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
335
336         $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
337
338         $data = get_source($page);
339         $data = preg_replace('/^#pcomment\(?.*/i', '', $data);  // Avoid eternal recurse
340
341         if (! is_array($data)) return array('', 0);
342
343         $digest = md5(join('', $data));
344
345         // Get latest N comments
346         $num  = $cnt     = 0;
347         $cmts = $matches = array();
348         if ($dir) $data = array_reverse($data);
349         foreach ($data as $line) {
350                 if ($count > 0 && $dir && $cnt == $count) break;
351
352                 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
353                         if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
354
355                         // Ready for radio-buttons
356                         if ($reply) {
357                                 ++$num;
358                                 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
359                                         md5($matches[2]) . "\x03" . $matches[2] . "\n";
360                                 continue;
361                         }
362                 }
363                 $cmts[] = $line;
364         }
365         $data = $cmts;
366         if ($dir) $data = array_reverse($data);
367         unset($cmts, $matches);
368
369         // Remove lines before comments
370         while (! empty($data) && substr($data[0], 0, 1) != '-')
371                 array_shift($data);
372
373         $comments = convert_html($data);
374         unset($data);
375
376         // Add radio buttons
377         if ($reply)
378                 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
379                         '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
380                         $comments);
381
382         return array($comments, $digest);
383 }