OSDN Git Service

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