OSDN Git Service

BugTrack2/139: Correct. Deleted a first space at preview.
[pukiwiki/pukiwiki.git] / plugin / comment.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: comment.inc.php,v 1.35 2005/05/06 04:44:20 henoheno Exp $
4 // Copyright (C)
5 //   2002-2005 PukiWiki Developers Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // Comment plugin
10
11 define('PLUGIN_COMMENT_DIRECTION_DEFAULT', '1'); // 1: above 0: below
12 define('PLUGIN_COMMENT_SIZE_MSG',  70);
13 define('PLUGIN_COMMENT_SIZE_NAME', 15);
14
15 // ----
16 define('PLUGIN_COMMENT_FORMAT_MSG',  '$msg');
17 define('PLUGIN_COMMENT_FORMAT_NAME', '[[$name]]');
18 define('PLUGIN_COMMENT_FORMAT_NOW',  '&new{$now};');
19 define('PLUGIN_COMMENT_FORMAT_STRING', "\x08MSG\x08 -- \x08NAME\x08 \x08NOW\x08");
20
21 function plugin_comment_action()
22 {
23         global $script, $vars, $now, $_title_updated, $_no_name;
24         global $_msg_comment_collided, $_title_comment_collided;
25
26         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
27
28         if (! isset($vars['msg'])) return array('msg'=>'', 'body'=>''); // Do nothing
29
30         $vars['msg'] = str_replace("\n", '', $vars['msg']); // Cut LFs
31         $head = '';
32         $match = array();
33         if (preg_match('/^(-{1,2})-*\s*(.*)/', $vars['msg'], $match)) {
34                 $head        = & $match[1];
35                 $vars['msg'] = & $match[2];
36         }
37         if ($vars['msg'] == '') return array('msg'=>'', 'body'=>''); // Do nothing
38
39         $comment  = str_replace('$msg', $vars['msg'], PLUGIN_COMMENT_FORMAT_MSG);
40         if(isset($vars['name']) || ($vars['nodate'] != '1')) {
41                 $_name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
42                 $_name = ($_name == '') ? '' : str_replace('$name', $_name, PLUGIN_COMMENT_FORMAT_NAME);
43                 $_now  = ($vars['nodate'] == '1') ? '' :
44                         str_replace('$now', $now, PLUGIN_COMMENT_FORMAT_NOW);
45                 $comment = str_replace("\x08MSG\x08",  $comment, PLUGIN_COMMENT_FORMAT_STRING);
46                 $comment = str_replace("\x08NAME\x08", $_name, $comment);
47                 $comment = str_replace("\x08NOW\x08",  $_now,  $comment);
48         }
49         $comment = '-' . $head . ' ' . $comment;
50
51         $postdata    = '';
52         $comment_no  = 0;
53         $above       = (isset($vars['above']) && $vars['above'] == '1');
54         foreach (get_source($vars['refer']) as $line) {
55                 if (! $above) $postdata .= $line;
56                 if (preg_match('/^#comment/i', $line) && $comment_no++ == $vars['comment_no']) {
57                         if ($above) {
58                                 $postdata = rtrim($postdata) . "\n" .
59                                         $comment . "\n" .
60                                         "\n";  // Insert one blank line above #commment, to avoid indentation
61                         } else {
62                                 $postdata = rtrim($postdata) . "\n" .
63                                         "\n" . // Insert one blank line below #commment, too (only by design)
64                                         $comment; // "\n" is already there or EOF
65                         }
66                 }
67                 if ($above) $postdata .= $line;
68         }
69
70         $title = $_title_updated;
71         $body = '';
72         if (md5(@join('', get_source($vars['refer']))) != $vars['digest']) {
73                 $title = $_title_comment_collided;
74                 $body  = $_msg_comment_collided . make_pagelink($vars['refer']);
75         }
76
77         page_write($vars['refer'], $postdata);
78
79         $retvars['msg']  = $title;
80         $retvars['body'] = $body;
81
82         $vars['page'] = $vars['refer'];
83
84         return $retvars;
85 }
86
87 function plugin_comment_convert()
88 {
89         global $vars, $digest, $_btn_comment, $_btn_name, $_msg_comment;
90         static $numbers = array();
91         static $comment_cols = PLUGIN_COMMENT_SIZE_MSG;
92
93         if (PKWK_READONLY) return ''; // Show nothing
94
95         if (! isset($numbers[$vars['page']])) $numbers[$vars['page']] = 0;
96         $comment_no = $numbers[$vars['page']]++;
97
98         $options = func_num_args() ? func_get_args() : array();
99         if (in_array('noname', $options)) {
100                 $nametags = '<label for="_p_comment_comment_' . $comment_no . '">' .
101                         $_msg_comment . '</label>';
102         } else {
103                 $nametags = '<label for="_p_comment_name_' . $comment_no . '">' .
104                         $_btn_name . '</label>' .
105                         '<input type="text" name="name" id="_p_comment_name_' .
106                         $comment_no .  '" size="' . PLUGIN_COMMENT_SIZE_NAME .
107                         '" />' . "\n";
108         }
109         $nodate = in_array('nodate', $options) ? '1' : '0';
110         $above  = in_array('above',  $options) ? '1' :
111                 (in_array('below', $options) ? '0' : PLUGIN_COMMENT_DIRECTION_DEFAULT);
112
113         $script = get_script_uri();
114         $s_page = htmlspecialchars($vars['page']);
115         $string = <<<EOD
116 <br />
117 <form action="$script" method="post">
118  <div>
119   <input type="hidden" name="plugin" value="comment" />
120   <input type="hidden" name="refer"  value="$s_page" />
121   <input type="hidden" name="comment_no" value="$comment_no" />
122   <input type="hidden" name="nodate" value="$nodate" />
123   <input type="hidden" name="above"  value="$above" />
124   <input type="hidden" name="digest" value="$digest" />
125   $nametags
126   <input type="text"   name="msg" id="_p_comment_comment_{$comment_no}" size="$comment_cols" />
127   <input type="submit" name="comment" value="$_btn_comment" />
128  </div>
129 </form>
130 EOD;
131
132         return $string;
133 }
134 ?>