OSDN Git Service

BugTrack/2436 ESLint - Linting utility for JavaScript
[pukiwiki/pukiwiki.git] / plugin / comment.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // comment.inc.php
4 // Copyright
5 //   2002-2017 PukiWiki Development 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 $vars, $now, $_title_updated, $_no_name;
24         global $_msg_comment_collided, $_title_comment_collided;
25         global $_comment_plugin_fail_msg;
26
27         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
28
29         if (! isset($vars['msg'])) return array('msg'=>'', 'body'=>''); // Do nothing
30
31         $vars['msg'] = str_replace("\n", '', $vars['msg']); // Cut LFs
32         $head = '';
33         $match = array();
34         if (preg_match('/^(-{1,2})-*\s*(.*)/', $vars['msg'], $match)) {
35                 $head        = & $match[1];
36                 $vars['msg'] = & $match[2];
37         }
38         if ($vars['msg'] == '') return array('msg'=>'', 'body'=>''); // Do nothing
39
40         $comment  = str_replace('$msg', $vars['msg'], PLUGIN_COMMENT_FORMAT_MSG);
41         if(isset($vars['name']) || ($vars['nodate'] != '1')) {
42                 $_name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
43                 $_name = ($_name == '') ? '' : str_replace('$name', $_name, PLUGIN_COMMENT_FORMAT_NAME);
44                 $_now  = ($vars['nodate'] == '1') ? '' :
45                         str_replace('$now', $now, PLUGIN_COMMENT_FORMAT_NOW);
46                 $comment = str_replace("\x08MSG\x08",  $comment, PLUGIN_COMMENT_FORMAT_STRING);
47                 $comment = str_replace("\x08NAME\x08", $_name, $comment);
48                 $comment = str_replace("\x08NOW\x08",  $_now,  $comment);
49         }
50         $comment = '-' . $head . ' ' . $comment;
51
52         $postdata    = '';
53         $comment_no  = 0;
54         $above       = (isset($vars['above']) && $vars['above'] == '1');
55         $comment_added = FALSE;
56         foreach (get_source($vars['refer']) as $line) {
57                 if (! $above) $postdata .= $line;
58                 if (preg_match('/^#comment/i', $line) && $comment_no++ == $vars['comment_no']) {
59                         $comment_added = TRUE;
60                         if ($above) {
61                                 $postdata = rtrim($postdata) . "\n" .
62                                         $comment . "\n" .
63                                         "\n";  // Insert one blank line above #commment, to avoid indentation
64                         } else {
65                                 $postdata = rtrim($postdata) . "\n" .
66                                         $comment . "\n";
67                         }
68                 }
69                 if ($above) $postdata .= $line;
70         }
71         $title = $_title_updated;
72         $body = '';
73         if ($comment_added) {
74                 // new comment added
75                 if (md5(get_source($vars['refer'], TRUE, TRUE)) !== $vars['digest']) {
76                         $title = $_title_comment_collided;
77                         $body  = $_msg_comment_collided . make_pagelink($vars['refer']);
78                 }
79                 page_write($vars['refer'], $postdata);
80         } else {
81                 // failed to add the comment
82                 $title = $_title_comment_collided;
83                 $body  = $_comment_plugin_fail_msg . make_pagelink($vars['refer']);
84         }
85         $retvars['msg']  = $title;
86         $retvars['body'] = $body;
87         $vars['page'] = $vars['refer'];
88         return $retvars;
89 }
90
91 function plugin_comment_convert()
92 {
93         global $vars, $digest, $_btn_comment, $_btn_name, $_msg_comment;
94         static $numbers = array();
95         static $comment_cols = PLUGIN_COMMENT_SIZE_MSG;
96
97         if (PKWK_READONLY) return ''; // Show nothing
98
99         $page = $vars['page'];
100         if (! isset($numbers[$page])) $numbers[$page] = 0;
101         $comment_no = $numbers[$page]++;
102
103         $options = func_num_args() ? func_get_args() : array();
104         if (in_array('noname', $options)) {
105                 $nametags = '<label for="_p_comment_comment_' . $comment_no . '">' .
106                         $_msg_comment . '</label>';
107         } else {
108                 $nametags = '<label for="_p_comment_name_' . $comment_no . '">' .
109                         $_btn_name . '</label>' .
110                         '<input type="text" name="name" id="_p_comment_name_' .
111                         $comment_no .  '" size="' . PLUGIN_COMMENT_SIZE_NAME .
112                         '" />' . "\n";
113         }
114         $nodate = in_array('nodate', $options) ? '1' : '0';
115         $above  = in_array('above',  $options) ? '1' :
116                 (in_array('below', $options) ? '0' : PLUGIN_COMMENT_DIRECTION_DEFAULT);
117
118         $script = get_page_uri($page);
119         $s_page = htmlsc($page);
120         $string = <<<EOD
121 <br />
122 <form action="$script" method="post">
123  <div>
124   <input type="hidden" name="plugin" value="comment" />
125   <input type="hidden" name="refer"  value="$s_page" />
126   <input type="hidden" name="comment_no" value="$comment_no" />
127   <input type="hidden" name="nodate" value="$nodate" />
128   <input type="hidden" name="above"  value="$above" />
129   <input type="hidden" name="digest" value="$digest" />
130   $nametags
131   <input type="text"   name="msg" id="_p_comment_comment_{$comment_no}" size="$comment_cols" />
132   <input type="submit" name="comment" value="$_btn_comment" />
133  </div>
134 </form>
135 EOD;
136
137         return $string;
138 }