OSDN Git Service

BugTrack2/361 Simplify URL. Keep slash / and colon : in page name URL
[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 switch (LANG) {
21 case 'ja': define('PLUGIN_PCOMMENT_PAGE', '[[コメント/%s]]'); break;
22 default:   define('PLUGIN_PCOMMENT_PAGE', '[[Comments/%s]]'); break;
23 }
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_script_uri() . '?' . pagename_urlencode($refer));
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         $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;
87
88         $_page = get_fullname(strip_bracket($page), $vars_page);
89         if (!is_pagename($_page))
90                 return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
91
92         $dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
93         if ($params['below']) {
94                 $dir = 0;
95         } elseif ($params['above']) {
96                 $dir = 1;
97         }
98
99         list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
100
101         if (PKWK_READONLY) {
102                 $form_start = $form = $form_end = '';
103         } else {
104                 // Show a form
105
106                 if ($params['noname']) {
107                         $title = $_pcmt_messages['msg_comment'];
108                         $name = '';
109                 } else {
110                         $title = $_pcmt_messages['btn_name'];
111                         $name = '<input type="text" name="name" size="' . PLUGIN_PCOMMENT_SIZE_NAME . '" />';
112                 }
113
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 . '" />';
117
118                 $s_page   = htmlsc($page);
119                 $s_refer  = htmlsc($vars_page);
120                 $s_nodate = htmlsc($params['nodate']);
121                 $s_count  = htmlsc($count);
122
123                 $form_start = '<form action="' . get_script_uri() . '" method="post">' . "\n";
124                 $form = <<<EOD
125   <div>
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']}" />
135   </div>
136 EOD;
137                 $form_end = '</form>' . "\n";
138         }
139
140         if (! is_page($_page)) {
141                 $link   = make_pagelink($_page);
142                 $recent = $_pcmt_messages['msg_none'];
143         } else {
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) : '';
147         }
148
149         if ($dir) {
150                 return '<div>' .
151                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
152                         $form_start .
153                                 $comments . "\n" .
154                                 $form .
155                         $form_end .
156                         '</div>' . "\n";
157         } else {
158                 return '<div>' .
159                         $form_start .
160                                 $form .
161                                 $comments. "\n" .
162                         $form_end .
163                         '<p>' . $recent . ' ' . $link . '</p>' . "\n" .
164                         '</div>' . "\n";
165         }
166 }
167
168 function plugin_pcomment_insert()
169 {
170         global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
171
172         $refer = isset($vars['refer']) ? $vars['refer'] : '';
173         $page  = isset($vars['page'])  ? $vars['page']  : '';
174         $page  = get_fullname($page, $refer);
175
176         if (! is_pagename($page))
177                 return array(
178                         'msg' =>'Invalid page name',
179                         'body'=>'Cannot add comment' ,
180                         'collided'=>TRUE
181                 );
182
183         check_editable($page, true, true);
184
185         $ret = array('msg' => $_title_updated, 'collided' => FALSE);
186
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);
196         }
197
198         $reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
199         if ($reply_hash || ! is_page($page)) {
200                 $msg = preg_replace('/^\-+/', '', $msg);
201         }
202         $msg = rtrim($msg);
203
204         if (! is_page($page)) {
205                 $postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
206                         '-' . $msg . "\n";
207         } else {
208                 $postdata = get_source($page);
209                 $count    = count($postdata);
210
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'];
215                 }
216
217                 $start_position = 0;
218                 while ($start_position < $count) {
219                         if (preg_match('/^\-/', $postdata[$start_position])) break;
220                         ++$start_position;
221                 }
222                 $end_position = $start_position;
223
224                 $dir = isset($vars['dir']) ? $vars['dir'] : '';
225
226                 // Find the comment to reply
227                 $level   = 1;
228                 $b_reply = FALSE;
229                 if ($reply_hash != '') {
230                         while ($end_position < $count) {
231                                 $matches = array();
232                                 if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
233                                         && md5($matches[2]) === $reply_hash)
234                                 {
235                                         $b_reply = TRUE;
236                                         $level   = strlen($matches[1]) + 1;
237
238                                         while ($end_position < $count) {
239                                                 if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
240                                                         && strlen($matches[1]) < $level) break;
241                                                 ++$end_position;
242                                         }
243                                         break;
244                                 }
245                         }
246                 }
247
248                 if ($b_reply == FALSE)
249                         $end_position = ($dir == '0') ? $start_position : $count;
250
251                 // Insert new comment
252                 array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
253
254                 if (PLUGIN_PCOMMENT_AUTO_LOG) {
255                         $_count = isset($vars['count']) ? $vars['count'] : '';
256                         plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
257                 }
258
259                 $postdata = join('', $postdata);
260         }
261         page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
262
263         if (PLUGIN_PCOMMENT_TIMESTAMP) {
264                 if ($refer != '') pkwk_touch_file(get_filename($refer));
265                 put_lastmodified();
266         }
267
268         return $ret;
269 }
270
271 // Auto log rotation
272 function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
273 {
274         if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
275
276         $keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
277         if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
278
279         if ($dir) {
280                 // Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
281                 $old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
282         } else {
283                 // Bottom N comments
284                 $old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
285         }
286
287         // Decide new page name
288         $i = 0;
289         do {
290                 ++$i;
291                 $_page = $page . '/' . $i;
292         } while (is_page($_page));
293
294         page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
295
296         // Recurse :)
297         plugin_pcomment_auto_log($page, $dir, $count, $postdata);
298 }
299
300 // Check arguments
301 function plugin_pcomment_check_arg($val, & $params)
302 {
303         if ($val != '') {
304                 $l_val = strtolower($val);
305                 foreach (array_keys($params) as $key) {
306                         if (strpos($key, $l_val) === 0) {
307                                 $params[$key] = TRUE;
308                                 return;
309                         }
310                 }
311         }
312
313         $params['_args'][] = $val;
314 }
315
316 function plugin_pcomment_get_comments($page, $count, $dir, $reply)
317 {
318         global $_msg_pcomment_restrict;
319
320         if (! check_readable($page, false, false))
321                 return array(str_replace('$1', $page, $_msg_pcomment_restrict));
322
323         $reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
324
325         $data = get_source($page);
326         $data = preg_replace('/^#pcomment\(?.*/i', '', $data);  // Avoid eternal recurse
327
328         if (! is_array($data)) return array('', 0);
329
330         $digest = md5(join('', $data));
331
332         // Get latest N comments
333         $num  = $cnt     = 0;
334         $cmts = $matches = array();
335         if ($dir) $data = array_reverse($data);
336         foreach ($data as $line) {
337                 if ($count > 0 && $dir && $cnt == $count) break;
338
339                 if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
340                         if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
341
342                         // Ready for radio-buttons
343                         if ($reply) {
344                                 ++$num;
345                                 $cmts[] = $matches[1] . "\x01" . $num . "\x02" .
346                                         md5($matches[2]) . "\x03" . $matches[2] . "\n";
347                                 continue;
348                         }
349                 }
350                 $cmts[] = $line;
351         }
352         $data = $cmts;
353         if ($dir) $data = array_reverse($data);
354         unset($cmts, $matches);
355
356         // Remove lines before comments
357         while (! empty($data) && substr($data[0], 0, 1) != '-')
358                 array_shift($data);
359
360         $comments = convert_html($data);
361         unset($data);
362
363         // Add radio buttons
364         if ($reply)
365                 $comments = preg_replace('/<li>' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
366                         '<li class="pcmt"><input class="pcmt" type="radio" name="reply" value="$2" tabindex="$1" />',
367                         $comments);
368
369         return array($comments, $digest);
370 }
371