Revision | 6c4750bdb1480620eebf87c59254effd715a2c32 (tree) |
---|---|
Time | 2022-01-24 01:19:47 |
Author | hai-fun <haifun129@gmai...> |
Commiter | hai-fun |
attachref that fixed by casualbabies
@@ -0,0 +1,319 @@ | ||
1 | +<?php | |
2 | +///////////////////////////////////////////////// | |
3 | +// PukiWiki - Yet another WikiWikiWeb clone. | |
4 | +// | |
5 | +// $Id: attachref.inc.php,v 0.14 2003/10/08 04:10:29 sha Exp $ | |
6 | +// PHP7 Split関数終了によりexplode関数に変更 : 2019/12/11 | |
7 | +// | |
8 | + | |
9 | +/* | |
10 | +*プラグイン attachref | |
11 | + その場に添付する。attach & ref | |
12 | + | |
13 | +*Usage | |
14 | + &attachref; | |
15 | + &attachref([<file>][,<ref options>][,button]); | |
16 | + | |
17 | +*パラメータ | |
18 | +-<file>: attachすると自動的に追加される。最初から書いておいてもよい。 | |
19 | +-<ref options>: &ref;用の引数。 | |
20 | +-button: [attach]のようなリンクでなく、<form></form>のボタンにする。 | |
21 | + | |
22 | +*動作 | |
23 | +(1)&attachref;を追加すると、[attach]ボタンが表示される。 | |
24 | +(2)[attach]ボタンを押すと、アップロードフォームが現われて、指定したファイル | |
25 | + を添付できる。 | |
26 | +(3)添付されたファイルは&ref(...);で参照したように貼り付けられる。 | |
27 | +(4)そのファイルを削除すると、"file not found"と[attach]ボタンが表示される。 | |
28 | +(5)(4)のときに、そのファイルが追加されると再び復活する。 | |
29 | + | |
30 | +*/ | |
31 | +// max file size for upload on PHP(PHP default 2MB) | |
32 | +ini_set('upload_max_filesize','2M'); | |
33 | + | |
34 | +// max file size for upload on script of PukiWiki(default 1MB) | |
35 | +define('MAX_FILESIZE',1000000); | |
36 | + | |
37 | +// 管理者だけが添付ファイルをアップロードできるようにする | |
38 | +define('ATTACHREF_UPLOAD_ADMIN_ONLY',FALSE); // FALSE or TRUE | |
39 | +// アップロード/削除時にパスワードを要求する(ADMIN_ONLYが優先) | |
40 | +define('ATTACHREF_PASSWORD_REQUIRE',FALSE); // FALSE or TRUE | |
41 | + | |
42 | + | |
43 | +// upload dir(must set end of /) attach.inc.phpと合わせる | |
44 | +define('ATTACHREF_UPLOAD_DIR','./attach/'); | |
45 | + | |
46 | + | |
47 | +function plugin_attachref_init() | |
48 | +{ | |
49 | + $messages = array( | |
50 | + '_attachref_messages' => array( | |
51 | + 'btn_submit' => '[添付]', | |
52 | + 'msg_title' => 'Attach and Ref to $1', | |
53 | + 'msg_title_collided' => '$1 で【更新の衝突】が起きました', | |
54 | + 'msg_collided' => 'あなたがファイルを添付している間に、他の人が同じページを更新してしまったようです。<br /> | |
55 | +ファイルが違う位置に挿入されているかもしれません。<br />' | |
56 | + ), | |
57 | + ); | |
58 | + set_plugin_messages($messages); | |
59 | +} | |
60 | +function plugin_attachref_inline() | |
61 | +{ | |
62 | + global $script,$vars,$digest; | |
63 | + global $_attachref_messages; | |
64 | + static $numbers = array(); | |
65 | + static $no_flag = 0; | |
66 | + | |
67 | + if (!array_key_exists($vars['page'],$numbers)) | |
68 | + { | |
69 | + $numbers[$vars['page']] = 0; | |
70 | + } | |
71 | + $attachref_no = $numbers[$vars['page']]++; | |
72 | + | |
73 | + //戻り値 | |
74 | + $ret = ''; | |
75 | + $dispattach = 1; | |
76 | + $button = 0; | |
77 | + | |
78 | + $args = func_get_args(); | |
79 | + $btn_text = array_pop($args); | |
80 | + $btn_text = $btn_text ? $btn_text : $_attachref_messages['btn_submit']; | |
81 | + | |
82 | + $options = array(); | |
83 | + foreach ( $args as $opt ){ | |
84 | + if ( $opt === 'button' ){ | |
85 | + $button = 1; | |
86 | + } | |
87 | + else if ( $opt === 'number' ){ | |
88 | + $no_flag = 1; | |
89 | + } | |
90 | + else if ( $opt === 'nonumber' ){ | |
91 | + $no_flag = 0; | |
92 | + } | |
93 | + else { | |
94 | + array_push($options, $opt); | |
95 | + } | |
96 | + } | |
97 | + if ( $no_flag == 1 ) $btn_text .= "[$attachref_no]"; | |
98 | + $args = $options; | |
99 | + if ( count($args) and $args[0]!='' ) | |
100 | + { | |
101 | + require_once(PLUGIN_DIR."ref.inc.php"); | |
102 | + $params = plugin_ref_body($args,$vars['page']); | |
103 | + if ($params['_error'] != '') { | |
104 | + $ret = $params['_error']; | |
105 | + $dispattach = 1; | |
106 | + } | |
107 | + else | |
108 | + { | |
109 | + $ret = $params['_body']; | |
110 | + $dispattach = 0; | |
111 | + } | |
112 | + } | |
113 | + if ( $dispattach ) { | |
114 | + //XSS脆弱性問題 - 外部から来た変数をエスケープ | |
115 | + $s_args = trim(join(",", $args)); | |
116 | + if ( $button ){ | |
117 | + $s_args .= ",button"; | |
118 | + $f_page = htmlspecialchars($vars['page']); | |
119 | + $f_args = htmlspecialchars($s_args); | |
120 | + $ret = <<<EOD | |
121 | + <form action="$script" method="post"> | |
122 | + <div> | |
123 | + <input type="hidden" name="encode_hint" value="ぷ" /> | |
124 | + <input type="hidden" name="attachref_no" value="$attachref_no" /> | |
125 | + <input type="hidden" name="attachref_opt" value="$f_args" /> | |
126 | + <input type="hidden" name="digest" value="$digest" /> | |
127 | + <input type="hidden" name="plugin" value="attachref" /> | |
128 | + <input type="hidden" name="refer" value="$f_page" /> | |
129 | + $ret | |
130 | + <input type="submit" value="$btn_text" /> | |
131 | + </div> | |
132 | + </form> | |
133 | +EOD; | |
134 | + } | |
135 | + else { | |
136 | + $f_btn_text = preg_replace('/<[^<>]+>/','',$btn_text); | |
137 | +// echo '[debug]btn=',$f_btn_text; | |
138 | + $f_page = rawurlencode($vars['page']); | |
139 | + $f_args = rawurlencode($s_args); | |
140 | + $ret = <<<EOD | |
141 | + $ret<a href="$script?plugin=attachref&attachref_no=$attachref_no&attachref_opt=$f_args&refer=$f_page&digest=$digest" title="$f_btn_text">$btn_text</a> | |
142 | +EOD; | |
143 | + } | |
144 | + } | |
145 | + return $ret; | |
146 | +} | |
147 | +function plugin_attachref_action() | |
148 | +{ | |
149 | + global $script,$vars; | |
150 | + global $_attachref_messages; | |
151 | + global $html_transitional; | |
152 | + | |
153 | + | |
154 | + //戻り値を初期化 | |
155 | + $retval['msg'] = $_attachref_messages['msg_title']; | |
156 | + $retval['body'] = ''; | |
157 | + | |
158 | + if (array_key_exists('attach_file',$_FILES) | |
159 | + and array_key_exists('refer',$vars) | |
160 | + and is_page($vars['refer'])) | |
161 | + { | |
162 | + $file = $_FILES['attach_file']; | |
163 | + $attachname = $file['name']; | |
164 | + $filename = preg_replace('/\..+$/','', $attachname,1); | |
165 | + | |
166 | + //すでに存在した場合、 ファイル名に'_0','_1',...を付けて回避(姑息) | |
167 | + $count = '_0'; | |
168 | + while (file_exists(ATTACHREF_UPLOAD_DIR.encode($vars['refer']).'_'.encode($attachname))) | |
169 | + { | |
170 | + $attachname = preg_replace('/^[^\.]+/',$filename.$count++,$file['name']); | |
171 | + } | |
172 | + | |
173 | + $file['name'] = $attachname; | |
174 | + | |
175 | + require_once(PLUGIN_DIR."attach.inc.php"); | |
176 | + if (!exist_plugin('attach') or !function_exists('attach_upload')) | |
177 | + { | |
178 | + return array('msg'=>'attach.inc.php not found or not correct version.'); | |
179 | + } | |
180 | + $pass = array_key_exists('pass',$vars) ? md5($vars['pass']) : NULL; | |
181 | + $retval = attach_upload($file,$vars['refer'],$pass); | |
182 | + if ($retval['result'] == TRUE) | |
183 | + { | |
184 | + $retval = attachref_insert_ref($file['name']); | |
185 | + } | |
186 | + } | |
187 | + else | |
188 | + { | |
189 | + $retval = attachref_showform(); | |
190 | + // XHTML 1.0 Transitional | |
191 | + $html_transitional = TRUE; | |
192 | + } | |
193 | + return $retval; | |
194 | +} | |
195 | + | |
196 | +function attachref_insert_ref($filename) | |
197 | +{ | |
198 | + global $script,$vars,$now,$do_backup; | |
199 | + global $_attachref_messages; | |
200 | + | |
201 | + $ret['msg'] = $_attachref_messages['msg_title']; | |
202 | + | |
203 | + $//args = split(",", $vars['attachref_opt']); | |
204 | + $args = explode(",", $vars['attachref_opt']); | |
205 | + if ( count($args) ){ | |
206 | + $args[0] = $filename;//array_shift,unshiftって要するにこれね | |
207 | + $s_args = join(",", $args); | |
208 | + } | |
209 | + else { | |
210 | + $s_args = $filename; | |
211 | + } | |
212 | + $msg = "&attachref($s_args)"; | |
213 | + | |
214 | + $refer = $vars['refer']; | |
215 | + $digest = $vars['digest']; | |
216 | + $postdata_old = get_source($refer); | |
217 | + $thedigest = md5(join('',$postdata_old)); | |
218 | + | |
219 | + $postdata = ''; | |
220 | + $attachref_ct = 0; //'#attachref'の出現回数 | |
221 | + $attachref_no = $vars['attachref_no']; | |
222 | + $skipflag = 0; | |
223 | + foreach ($postdata_old as $line) | |
224 | + { | |
225 | + if ( $skipflag || substr($line,0,1) == ' ' || substr($line,0,2) == '//' ){ | |
226 | + $postdata .= $line; | |
227 | + continue; | |
228 | + } | |
229 | + $ct = preg_match_all('/&attachref(?=[({;])/',$line, $out); | |
230 | + if ( $ct ){ | |
231 | + for($i=0; $i < $ct; $i++){ | |
232 | + if ($attachref_ct++ == $attachref_no ){ | |
233 | + $line = preg_replace('/&attachref(\([^(){};]*\))?(\{[^{}]*\})?;/',$msg.'$2;',$line,1); | |
234 | + $skipflag = 1; | |
235 | + break; | |
236 | + } | |
237 | + else { | |
238 | + $line = preg_replace('/&attachref(\([^(){};]*\))?(\{[^{}]*\})?;/','&___attachref$1$2___;',$line,1); | |
239 | + } | |
240 | + } | |
241 | + $line = preg_replace('/&___attachref(\([^(){};]*\))?(\{[^{}]*\})?___;/','&attachref$1$2;',$line); | |
242 | +// $postdata .= "<hr />$ct|$attachref_no|$line"; | |
243 | + } | |
244 | + $postdata .= $line; | |
245 | + } | |
246 | + | |
247 | + // 更新の衝突を検出 | |
248 | + if ( $thedigest != $digest ) | |
249 | + { | |
250 | + $ret['msg'] = $_attachref_messages['msg_title_collided']; | |
251 | + $ret['body'] = $_attachref_messages['msg_collided']; | |
252 | + } | |
253 | +/* | |
254 | + $postdata .= "<hr />$refer, " . join('/',array_keys($vars)) . ", " . join("/",array_values($vars)) . ", s_args=$s_args"; | |
255 | + $ret['body'] = $postdata; | |
256 | +*/ | |
257 | + page_write($vars['refer'],$postdata); | |
258 | + | |
259 | + return $ret; | |
260 | +} | |
261 | +//アップロードフォームを表示 | |
262 | +function attachref_showform() | |
263 | +{ | |
264 | + global $vars; | |
265 | + global $_attach_messages; | |
266 | + | |
267 | + $vars['page'] = $vars['refer']; | |
268 | + $body = ini_get('file_uploads') ? attachref_form($vars['page']) : 'file_uploads disabled.'; | |
269 | + | |
270 | + return array('msg'=>$_attach_messages['msg_upload'],'body'=>$body); | |
271 | +} | |
272 | +//アップロードフォーム | |
273 | +function attachref_form($page) | |
274 | +{ | |
275 | + global $script,$vars; | |
276 | + global $_attach_messages; | |
277 | + | |
278 | + $s_page = htmlspecialchars($page); | |
279 | + | |
280 | + $f_digest = array_key_exists('digest',$vars) ? $vars['digest'] : ''; | |
281 | + $f_no = (array_key_exists('attachref_no',$vars) and is_numeric($vars['attachref_no'])) ? | |
282 | + $vars['attachref_no'] + 0 : 0; | |
283 | + | |
284 | + | |
285 | + if (!(bool)ini_get('file_uploads')) | |
286 | + { | |
287 | + return ""; | |
288 | + } | |
289 | + | |
290 | + $maxsize = MAX_FILESIZE; | |
291 | + $msg_maxsize = sprintf($_attach_messages['msg_maxsize'],number_format($maxsize/1000)."KB"); | |
292 | + | |
293 | + $pass = ''; | |
294 | + if (ATTACHREF_PASSWORD_REQUIRE or ATTACHREF_UPLOAD_ADMIN_ONLY) | |
295 | + { | |
296 | + $title = $_attach_messages[ATTACHREF_UPLOAD_ADMIN_ONLY ? 'msg_adminpass' : 'msg_password']; | |
297 | + $pass = '<br />'.$title.': <input type="password" name="pass" size="8" />'; | |
298 | + } | |
299 | + return <<<EOD | |
300 | +<form enctype="multipart/form-data" action="$script" method="post"> | |
301 | + <div> | |
302 | + <input type="hidden" name="plugin" value="attachref" /> | |
303 | + <input type="hidden" name="pcmd" value="post" /> | |
304 | + <input type="hidden" name="attachref_no" value="$f_no" /> | |
305 | + <input type="hidden" name="attachref_opt" value="{$vars['attachref_opt']}" /> | |
306 | + <input type="hidden" name="digest" value="$f_digest" /> | |
307 | + <input type="hidden" name="refer" value="$s_page" /> | |
308 | + <input type="hidden" name="max_file_size" value="$maxsize" /> | |
309 | + <span class="small"> | |
310 | + $msg_maxsize | |
311 | + </span><br /> | |
312 | + {$_attach_messages['msg_file']}: <input type="file" name="attach_file" /> | |
313 | + $pass | |
314 | + <input type="submit" value="{$_attach_messages['btn_upload']}" /> | |
315 | + </div> | |
316 | +</form> | |
317 | +EOD; | |
318 | +} | |
319 | +?> | |
\ No newline at end of file |