OSDN Git Service

Convert character code of the source code to UTF-8 from EUC-JP
[pukiwiki/pukiwiki.git] / plugin / ref.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: ref.inc.php,v 1.51 2011/01/25 15:01:01 henoheno Exp $
4 // Copyright (C)
5 //   2002-2006 PukiWiki Developers Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // Image refernce plugin
10 // Include an attached image-file as an inline-image
11
12 // File icon image
13 if (! defined('FILE_ICON'))
14         define('FILE_ICON',
15         '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
16         ' alt="file" style="border-width:0px" />');
17
18 /////////////////////////////////////////////////
19 // Default settings
20
21 // Horizontal alignment
22 define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
23
24 // Text wrapping
25 define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
26
27 // URL指定時に画像サイズを取得するか
28 define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
29
30 // UPLOAD_DIR のデータ(画像ファイルのみ)に直接アクセスさせる
31 define('PLUGIN_REF_DIRECT_ACCESS', FALSE); // FALSE or TRUE
32 // - これは従来のインラインイメージ処理を互換のために残すもので
33 //   あり、高速化のためのオプションではありません
34 // - UPLOAD_DIR をWebサーバー上に露出させており、かつ直接アクセス
35 //   できる(アクセス制限がない)状態である必要があります
36 // - Apache などでは UPLOAD_DIR/.htaccess を削除する必要があります
37 // - ブラウザによってはインラインイメージの表示や、「インライン
38 //   イメージだけを表示」させた時などに不具合が出る場合があります
39
40 /////////////////////////////////////////////////
41
42 // Image suffixes allowed
43 define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
44
45 // Usage (a part of)
46 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
47
48 function plugin_ref_inline()
49 {
50         // Not reached, because of "$aryargs[] = & $body" at plugin.php
51         // if (! func_num_args())
52         //      return '&amp;ref(): Usage:' . PLUGIN_REF_USAGE . ';';
53
54         $params = plugin_ref_body(func_get_args());
55
56         if (isset($params['_error']) && $params['_error'] != '') {
57                 // Error
58                 return '&amp;ref(): ' . $params['_error'] . ';';
59         } else {
60                 return $params['_body'];
61         }
62 }
63
64 function plugin_ref_convert()
65 {
66         if (! func_num_args())
67                 return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
68
69         $params = plugin_ref_body(func_get_args());
70
71         if (isset($params['_error']) && $params['_error'] != '') {
72                 return "<p>#ref(): {$params['_error']}</p>\n";
73         }
74
75         if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
76                 // 枠で包む
77                 // margin:auto
78                 //      Mozilla 1.x  = x (wrap,aroundが効かない)
79                 //      Opera 6      = o
80                 //      Netscape 6   = x (wrap,aroundが効かない)
81                 //      IE 6         = x (wrap,aroundが効かない)
82                 // margin:0px
83                 //      Mozilla 1.x  = x (wrapで寄せが効かない)
84                 //      Opera 6      = x (wrapで寄せが効かない)
85                 //      Netscape 6   = x (wrapで寄せが効かない)
86                 //      IE6          = o
87                 $margin = ($params['around'] ? '0px' : 'auto');
88                 $margin_align = ($params['_align'] == 'center') ? '' : ";margin-{$params['_align']}:0px";
89                 $params['_body'] = <<<EOD
90 <table class="style_table" style="margin:$margin$margin_align">
91  <tr>
92   <td class="style_td">{$params['_body']}</td>
93  </tr>
94 </table>
95 EOD;
96         }
97
98         if ($params['around']) {
99                 $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
100         } else {
101                 $style = "text-align:{$params['_align']}";
102         }
103
104         // divで包む
105         return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
106 }
107
108 function plugin_ref_body($args)
109 {
110         global $script, $vars;
111         global $WikiName, $BracketName; // compat
112
113         // 戻り値
114         $params = array(
115                 'left'   => FALSE, // 左寄せ
116                 'center' => FALSE, // 中央寄せ
117                 'right'  => FALSE, // 右寄せ
118                 'wrap'   => FALSE, // TABLEで囲む
119                 'nowrap' => FALSE, // TABLEで囲まない
120                 'around' => FALSE, // 回り込み
121                 'noicon' => FALSE, // アイコンを表示しない
122                 'nolink' => FALSE, // 元ファイルへのリンクを張らない
123                 'noimg'  => FALSE, // 画像を展開しない
124                 'zoom'   => FALSE, // 縦横比を保持する
125                 '_size'  => FALSE, // サイズ指定あり
126                 '_w'     => 0,       // 幅
127                 '_h'     => 0,       // 高さ
128                 '_%'     => 0,     // 拡大率
129                 '_args'  => array(),
130                 '_done'  => FALSE,
131                 '_error' => ''
132         );
133
134         // 添付ファイルのあるページ: defaultは現在のページ名
135         $page = isset($vars['page']) ? $vars['page'] : '';
136
137         // 添付ファイルのファイル名
138         $name = '';
139
140         // 添付ファイルまでのパスおよび(実際の)ファイル名
141         $file = '';
142
143         // 第一引数: "[ページ名および/]添付ファイル名"、あるいは"URL"を取得
144         $name = array_shift($args);
145         $is_url = is_url($name);
146
147         if(! $is_url) {
148                 // 添付ファイル
149                 if (! is_dir(UPLOAD_DIR)) {
150                         $params['_error'] = 'No UPLOAD_DIR';
151                         return $params;
152                 }
153
154                 $matches = array();
155                 // ファイル名にページ名(ページ参照パス)が合成されているか
156                 //   (Page_name/maybe-separated-with/slashes/ATTACHED_FILENAME)
157                 if (preg_match('#^(.+)/([^/]+)$#', $name, $matches)) {
158                         if ($matches[1] == '.' || $matches[1] == '..') {
159                                 $matches[1] .= '/'; // Restore relative paths
160                         }
161                         $name = $matches[2];
162                         $page = get_fullname(strip_bracket($matches[1]), $page); // strip is a compat
163                         $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
164                         $is_file = is_file($file);
165
166                 // 第二引数以降が存在し、それはrefのオプション名称などと一致しない
167                 } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
168                         $e_name = encode($name);
169
170                         // Try the second argument, as a page-name or a path-name
171                         $_arg = get_fullname(strip_bracket($args[0]), $page); // strip is a compat
172                         $file = UPLOAD_DIR .  encode($_arg) . '_' . $e_name;
173                         $is_file_second = is_file($file);
174
175                         // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
176                         $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
177
178                         if ($is_file_second && $is_bracket_bracket) {
179                                 // Believe the second argument (compat)
180                                 array_shift($args);
181                                 $page = $_arg;
182                                 $is_file = TRUE;
183                         } else {
184                                 // Try default page, with default params
185                                 $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
186
187                                 // Promote new design
188                                 if ($is_file_default && $is_file_second) {
189                                         // Because of race condition NOW
190                                         $params['_error'] = htmlsc('The same file name "' .
191                                                 $name . '" at both page: "' .  $page . '" and "' .  $_arg .
192                                                 '". Try ref(pagename/filename) to specify one of them');
193                                 } else {
194                                         // Because of possibility of race condition, in the future
195                                         $params['_error'] = 'The style ref(filename,pagename) is ambiguous ' .
196                                                 'and become obsolete. ' .
197                                                 'Please try ref(pagename/filename)';
198                                 }
199                                 return $params;
200                         }
201                 } else {
202                         // Simple single argument
203                         $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
204                         $is_file = is_file($file);
205                 }
206                 if (! $is_file) {
207                         $params['_error'] = htmlsc('File not found: "' .
208                                 $name . '" at page "' . $page . '"');
209                         return $params;
210                 }
211         }
212
213         // 残りの引数の処理
214         if (! empty($args))
215                 foreach ($args as $arg)
216                         ref_check_arg($arg, $params);
217
218 /*
219  $nameをもとに以下の変数を設定
220  $url,$url2 : URL
221  $title :タイトル
222  $is_image : 画像のときTRUE
223  $info : 画像ファイルのときgetimagesize()の'size'
224          画像ファイル以外のファイルの情報
225          添付ファイルのとき : ファイルの最終更新日とサイズ
226          URLのとき : URLそのもの
227 */
228         $title = $url = $url2 = $info = '';
229         $width = $height = 0;
230         $matches = array();
231
232         if ($is_url) {  // URL
233                 if (PKWK_DISABLE_INLINE_IMAGE_FROM_URI) {
234                         //$params['_error'] = 'PKWK_DISABLE_INLINE_IMAGE_FROM_URI prohibits this';
235                         //return $params;
236                         $url = htmlsc($name);
237                         $params['_body'] = '<a href="' . $url . '">' . $url . '</a>';
238                         return $params;
239                 }
240
241                 $url = $url2 = htmlsc($name);
242                 $title = htmlsc(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
243
244                 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
245
246                 if ($is_image && PLUGIN_REF_URL_GET_IMAGE_SIZE && (bool)ini_get('allow_url_fopen')) {
247                         $size = @getimagesize($name);
248                         if (is_array($size)) {
249                                 $width  = $size[0];
250                                 $height = $size[1];
251                                 $info   = $size[3];
252                         }
253                 }
254
255         } else { // 添付ファイル
256
257                 $title = htmlsc($name);
258
259                 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
260
261                 // Count downloads with attach plugin
262                 $url = $script . '?plugin=attach' . '&amp;refer=' . rawurlencode($page) .
263                         '&amp;openfile=' . rawurlencode($name); // Show its filename at the last
264
265                 if ($is_image) {
266                         // Swap $url
267                         $url2 = $url;
268
269                         // URI for in-line image output
270                         if (! PLUGIN_REF_DIRECT_ACCESS) {
271                                 // With ref plugin (faster than attach)
272                                 $url = $script . '?plugin=ref' . '&amp;page=' . rawurlencode($page) .
273                                         '&amp;src=' . rawurlencode($name); // Show its filename at the last
274                         } else {
275                                 // Try direct-access, if possible
276                                 $url = $file;
277                         }
278
279                         $width = $height = 0;
280                         $size = @getimagesize($file);
281                         if (is_array($size)) {
282                                 $width  = $size[0];
283                                 $height = $size[1];
284                         }
285                 } else {
286                         $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
287                                 ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
288                 }
289         }
290
291         // 拡張パラメータをチェック
292         if (! empty($params['_args'])) {
293                 $_title = array();
294                 foreach ($params['_args'] as $arg) {
295                         if (preg_match('/^([0-9]+)x([0-9]+)$/', $arg, $matches)) {
296                                 $params['_size'] = TRUE;
297                                 $params['_w'] = $matches[1];
298                                 $params['_h'] = $matches[2];
299
300                         } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
301                                 $params['_%'] = $matches[1];
302
303                         } else {
304                                 $_title[] = $arg;
305                         }
306                 }
307
308                 if (! empty($_title)) {
309                         $title = htmlsc(join(',', $_title));
310                         if ($is_image) $title = make_line_rules($title);
311                 }
312         }
313
314         // 画像サイズ調整
315         if ($is_image) {
316                 // 指定されたサイズを使用する
317                 if ($params['_size']) {
318                         if ($width == 0 && $height == 0) {
319                                 $width  = $params['_w'];
320                                 $height = $params['_h'];
321                         } else if ($params['zoom']) {
322                                 $_w = $params['_w'] ? $width  / $params['_w'] : 0;
323                                 $_h = $params['_h'] ? $height / $params['_h'] : 0;
324                                 $zoom = max($_w, $_h);
325                                 if ($zoom) {
326                                         $width  = (int)($width  / $zoom);
327                                         $height = (int)($height / $zoom);
328                                 }
329                         } else {
330                                 $width  = $params['_w'] ? $params['_w'] : $width;
331                                 $height = $params['_h'] ? $params['_h'] : $height;
332                         }
333                 }
334                 if ($params['_%']) {
335                         $width  = (int)($width  * $params['_%'] / 100);
336                         $height = (int)($height * $params['_%'] / 100);
337                 }
338                 if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
339         }
340
341         // アラインメント判定
342         $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
343         foreach (array('right', 'left', 'center') as $align) {
344                 if ($params[$align])  {
345                         $params['_align'] = $align;
346                         break;
347                 }
348         }
349
350         if ($is_image) { // 画像
351                 $params['_body'] = "<img src=\"$url\" alt=\"$title\" title=\"$title\" $info/>";
352                 if (! $params['nolink'] && $url2)
353                         $params['_body'] = "<a href=\"$url2\" title=\"$title\">{$params['_body']}</a>";
354         } else {
355                 $icon = $params['noicon'] ? '' : FILE_ICON;
356                 $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
357         }
358
359         return $params;
360 }
361
362 // オプションを解析する
363 function ref_check_arg($val, & $params)
364 {
365         if ($val == '') {
366                 $params['_done'] = TRUE;
367                 return;
368         }
369
370         if (! $params['_done']) {
371                 foreach (array_keys($params) as $key) {
372                         if (strpos($key, strtolower($val)) === 0) {
373                                 $params[$key] = TRUE;
374                                 return;
375                         }
376                 }
377                 $params['_done'] = TRUE;
378         }
379
380         $params['_args'][] = $val;
381 }
382
383 // Output an image (fast, non-logging <==> attach plugin)
384 function plugin_ref_action()
385 {
386         global $vars;
387
388         $usage = 'Usage: plugin=ref&amp;page=page_name&amp;src=attached_image_name';
389
390         if (! isset($vars['page']) || ! isset($vars['src']))
391                 return array('msg'=>'Invalid argument', 'body'=>$usage);
392
393         $page     = $vars['page'];
394         $filename = $vars['src'] ;
395
396         $ref = UPLOAD_DIR . encode($page) . '_' . encode(preg_replace('#^.*/#', '', $filename));
397         if(! file_exists($ref))
398                 return array('msg'=>'Attach file not found', 'body'=>$usage);
399
400         $got = @getimagesize($ref);
401         if (! isset($got[2])) $got[2] = FALSE;
402         switch ($got[2]) {
403         case 1: $type = 'image/gif' ; break;
404         case 2: $type = 'image/jpeg'; break;
405         case 3: $type = 'image/png' ; break;
406         case 4: $type = 'application/x-shockwave-flash'; break;
407         default:
408                 return array('msg'=>'Seems not an image', 'body'=>$usage);
409         }
410
411         // Care for Japanese-character-included file name
412         if (LANG == 'ja') {
413                 switch(UA_NAME . '/' . UA_PROFILE){
414                 case 'Opera/default':
415                         // Care for using _auto-encode-detecting_ function
416                         $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
417                         break;
418                 case 'MSIE/default':
419                         $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
420                         break;
421                 }
422         }
423         $utf8filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
424         $size = filesize($ref);
425
426         // Output
427         pkwk_common_headers();
428         header('Content-Disposition: inline; filename="' . $filename . '"; filename*=utf-8\'\'' . rawurlencode($utf8filename));
429         header('Content-Length: ' . $size);
430         header('Content-Type: '   . $type);
431         @readfile($ref);
432         exit;
433 }
434 ?>