OSDN Git Service

PKWK_READONLY prohibits basic_auth
[pukiwiki/pukiwiki.git] / plugin / comment.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: comment.inc.php,v 1.26 2005/01/23 07:01:56 henoheno Exp $
4 //
5 // Comment plugin
6
7 define('PLUGIN_COMMENT_DIRECTION_DEFAULT', '1'); // 1: above 0: below
8 define('PLUGIN_COMMENT_SIZE_MSG',  70);
9 define('PLUGIN_COMMENT_SIZE_NAME', 15);
10
11 // ----
12 define('PLUGIN_COMMENT_FORMAT_MSG',  '$msg');
13 define('PLUGIN_COMMENT_FORMAT_NAME', '[[$name]]');
14 define('PLUGIN_COMMENT_FORMAT_NOW',  '&new{$now};');
15 define('PLUGIN_COMMENT_FORMAT_STRING', "\x08MSG\x08 -- \x08NAME\x08 \x08NOW\x08");
16 function plugin_comment_action()
17 {
18         global $script, $vars, $now, $_title_updated, $_no_name;
19         global $_msg_comment_collided, $_title_comment_collided;
20
21         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
22         if (! isset($vars['msg']) || $vars['msg'] == '')
23                 return array('msg'=>'', 'body'=>'');
24
25         $vars['msg'] = preg_replace("/\n/", '', $vars['msg']);
26
27         $head = '';
28         $match = array();
29         if (preg_match('/^(-{1,2})(.*)/', $vars['msg'], $match)) {
30                 $head        = $match[1];
31                 $vars['msg'] = $match[2];
32         }
33         unset($match);
34
35         $_msg  = str_replace('$msg', $vars['msg'], PLUGIN_COMMENT_FORMAT_MSG);
36
37         $_name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
38         $_name = ($_name == '') ? '' : str_replace('$name', $_name, PLUGIN_COMMENT_FORMAT_NAME);
39
40         $_now  = ($vars['nodate'] == '1') ? '' : str_replace('$now', $now, PLUGIN_COMMENT_FORMAT_NOW);
41
42         $comment = str_replace("\x08MSG\x08",  $_msg,  PLUGIN_COMMENT_FORMAT_STRING);
43         $comment = str_replace("\x08NAME\x08", $_name, $comment);
44         $comment = str_replace("\x08NOW\x08",  $_now,  $comment);
45         $comment = $head . $comment;
46
47         $postdata = '';
48         $postdata_old  = get_source($vars['refer']);
49         $comment_no = 0;
50         $comment_ins = ($vars['above'] == '1');
51
52         foreach ($postdata_old as $line) {
53                 if (! $comment_ins) $postdata .= $line;
54                 if (preg_match('/^#comment/i', $line) && $comment_no++ == $vars['comment_no']) {
55                         $postdata = rtrim($postdata) . "\n-$comment\n";
56                         if ($comment_ins) $postdata .= "\n";
57                 }
58                 if ($comment_ins) $postdata .= $line;
59         }
60
61         $title = $_title_updated;
62         $body = '';
63         if (md5(@join('', get_source($vars['refer']))) != $vars['digest']) {
64                 $title = $_title_comment_collided;
65                 $body  = $_msg_comment_collided . make_pagelink($vars['refer']);
66         }
67
68         page_write($vars['refer'], $postdata);
69
70         $retvars['msg']  = $title;
71         $retvars['body'] = $body;
72
73         $vars['page'] = $vars['refer'];
74
75         return $retvars;
76 }
77
78 function plugin_comment_convert()
79 {
80         global $script, $vars, $digest;
81         global $_btn_comment, $_btn_name, $_msg_comment;
82         static $numbers = array();
83
84         if (PKWK_READONLY) return ''; // Show nothing
85
86         if (! isset($numbers[$vars['page']])) $numbers[$vars['page']] = 0;
87         $comment_no = $numbers[$vars['page']]++;
88
89         $options = func_num_args() ? func_get_args() : array();
90
91         if (in_array('noname', $options)) {
92                 $nametags = $_msg_comment;
93         } else {
94                 $nametags = $_btn_name .
95                         '<input type="text" name="name" size="' . PLUGIN_COMMENT_SIZE_NAME . "\" />\n";
96         }
97
98         $nodate = in_array('nodate', $options) ? '1' : '0';
99         $above  = in_array('above',  $options) ? '1' :
100                 (in_array('below', $options) ? '0' : PLUGIN_COMMENT_DIRECTION_DEFAULT);
101
102         $s_page = htmlspecialchars($vars['page']);
103         static $comment_cols = PLUGIN_COMMENT_SIZE_MSG;
104         $string = <<<EOD
105 <br />
106 <form action="$script" method="post">
107  <div>
108   <input type="hidden" name="comment_no" value="$comment_no" />
109   <input type="hidden" name="refer"  value="$s_page" />
110   <input type="hidden" name="plugin" value="comment" />
111   <input type="hidden" name="nodate" value="$nodate" />
112   <input type="hidden" name="above"  value="$above" />
113   <input type="hidden" name="digest" value="$digest" />
114   $nametags
115   <input type="text"   name="msg" size="$comment_cols" />
116   <input type="submit" name="comment" value="$_btn_comment" />
117  </div>
118 </form>
119 EOD;
120
121         return $string;
122 }
123 ?>