OSDN Git Service

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