2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: ref.inc.php,v 1.51 2011/01/25 15:01:01 henoheno Exp $
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
9 // Image refernce plugin
10 // Include an attached image-file as an inline-image
13 if (! defined('FILE_ICON'))
15 '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
16 ' alt="file" style="border-width:0px" />');
18 /////////////////////////////////////////////////
21 // Horizontal alignment
22 define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
25 define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
28 define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
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 // イメージだけを表示」させた時などに不具合が出る場合があります
40 /////////////////////////////////////////////////
42 // Image suffixes allowed
43 define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
46 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
48 function plugin_ref_inline()
50 // Not reached, because of "$aryargs[] = & $body" at plugin.php
51 // if (! func_num_args())
52 // return '&ref(): Usage:' . PLUGIN_REF_USAGE . ';';
54 $params = plugin_ref_body(func_get_args());
56 if (isset($params['_error']) && $params['_error'] != '') {
58 return '&ref(): ' . $params['_error'] . ';';
60 return $params['_body'];
64 function plugin_ref_convert()
66 if (! func_num_args())
67 return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
69 $params = plugin_ref_body(func_get_args());
71 if (isset($params['_error']) && $params['_error'] != '') {
72 return "<p>#ref(): {$params['_error']}</p>\n";
75 if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
78 // Mozilla 1.x = x (wrap,aroundが効かない)
80 // Netscape 6 = x (wrap,aroundが効かない)
81 // IE 6 = x (wrap,aroundが効かない)
83 // Mozilla 1.x = x (wrapで寄せが効かない)
84 // Opera 6 = x (wrapで寄せが効かない)
85 // Netscape 6 = x (wrapで寄せが効かない)
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">
92 <td class="style_td">{$params['_body']}</td>
98 if ($params['around']) {
99 $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
101 $style = "text-align:{$params['_align']}";
105 return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
108 function plugin_ref_body($args)
110 global $script, $vars;
111 global $WikiName, $BracketName; // compat
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, // サイズ指定あり
134 // 添付ファイルのあるページ: defaultは現在のページ名
135 $page = isset($vars['page']) ? $vars['page'] : '';
140 // 添付ファイルまでのパスおよび(実際の)ファイル名
143 // 第一引数: "[ページ名および/]添付ファイル名"、あるいは"URL"を取得
144 $name = array_shift($args);
145 $is_url = is_url($name);
149 if (! is_dir(UPLOAD_DIR)) {
150 $params['_error'] = 'No UPLOAD_DIR';
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
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);
166 // 第二引数以降が存在し、それはrefのオプション名称などと一致しない
167 } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
168 $e_name = encode($name);
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);
175 // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
176 $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
178 if ($is_file_second && $is_bracket_bracket) {
179 // Believe the second argument (compat)
184 // Try default page, with default params
185 $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
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');
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)';
202 // Simple single argument
203 $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
204 $is_file = is_file($file);
207 $params['_error'] = htmlsc('File not found: "' .
208 $name . '" at page "' . $page . '"');
215 foreach ($args as $arg)
216 ref_check_arg($arg, $params);
222 $is_image : 画像のときTRUE
223 $info : 画像ファイルのときgetimagesize()の'size'
225 添付ファイルのとき : ファイルの最終更新日とサイズ
228 $title = $url = $url2 = $info = '';
229 $width = $height = 0;
232 if ($is_url) { // URL
233 if (PKWK_DISABLE_INLINE_IMAGE_FROM_URI) {
234 //$params['_error'] = 'PKWK_DISABLE_INLINE_IMAGE_FROM_URI prohibits this';
236 $url = htmlsc($name);
237 $params['_body'] = '<a href="' . $url . '">' . $url . '</a>';
241 $url = $url2 = htmlsc($name);
242 $title = htmlsc(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
244 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
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)) {
257 $title = htmlsc($name);
259 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
261 // Count downloads with attach plugin
262 $url = $script . '?plugin=attach' . '&refer=' . rawurlencode($page) .
263 '&openfile=' . rawurlencode($name); // Show its filename at the last
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' . '&page=' . rawurlencode($page) .
273 '&src=' . rawurlencode($name); // Show its filename at the last
275 // Try direct-access, if possible
279 $width = $height = 0;
280 $size = @getimagesize($file);
281 if (is_array($size)) {
286 $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
287 ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
292 if (! empty($params['_args'])) {
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];
300 } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
301 $params['_%'] = $matches[1];
308 if (! empty($_title)) {
309 $title = htmlsc(join(',', $_title));
310 if ($is_image) $title = make_line_rules($title);
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);
326 $width = (int)($width / $zoom);
327 $height = (int)($height / $zoom);
330 $width = $params['_w'] ? $params['_w'] : $width;
331 $height = $params['_h'] ? $params['_h'] : $height;
335 $width = (int)($width * $params['_%'] / 100);
336 $height = (int)($height * $params['_%'] / 100);
338 if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
342 $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
343 foreach (array('right', 'left', 'center') as $align) {
344 if ($params[$align]) {
345 $params['_align'] = $align;
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>";
355 $icon = $params['noicon'] ? '' : FILE_ICON;
356 $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
363 function ref_check_arg($val, & $params)
366 $params['_done'] = TRUE;
370 if (! $params['_done']) {
371 foreach (array_keys($params) as $key) {
372 if (strpos($key, strtolower($val)) === 0) {
373 $params[$key] = TRUE;
377 $params['_done'] = TRUE;
380 $params['_args'][] = $val;
383 // Output an image (fast, non-logging <==> attach plugin)
384 function plugin_ref_action()
388 $usage = 'Usage: plugin=ref&page=page_name&src=attached_image_name';
390 if (! isset($vars['page']) || ! isset($vars['src']))
391 return array('msg'=>'Invalid argument', 'body'=>$usage);
393 $page = $vars['page'];
394 $filename = $vars['src'] ;
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);
400 $got = @getimagesize($ref);
401 if (! isset($got[2])) $got[2] = FALSE;
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;
408 return array('msg'=>'Seems not an image', 'body'=>$usage);
411 // Care for Japanese-character-included file name
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');
419 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
423 $utf8filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
424 $size = filesize($ref);
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);