OSDN Git Service

Convert character code of the source code to UTF-8 from EUC-JP
[pukiwiki/pukiwiki.git] / plugin / ref.inc.php
index 65049af..6df69b6 100644 (file)
@@ -1,84 +1,89 @@
 <?php
-/////////////////////////////////////////////////
-// PukiWiki - Yet another WikiWikiWeb clone.
-//
-// $Id: ref.inc.php,v 1.11 2003/03/05 07:01:26 panda Exp $
+// PukiWiki - Yet another WikiWikiWeb clone
+// $Id: ref.inc.php,v 1.51 2011/01/25 15:01:01 henoheno Exp $
+// Copyright (C)
+//   2002-2006 PukiWiki Developers Team
+//   2001-2002 Originally written by yu-ji
+// License: GPL v2 or (at your option) any later version
 //
+// Image refernce plugin
+// Include an attached image-file as an inline-image
 
-/*
-*¥×¥é¥°¥¤¥ó ref
-¥Ú¡¼¥¸¤ËźÉÕ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤òŸ³«¤¹¤ë
-
-*Usage
- #ref(filename[[,{Left|Center|Right}]|[,{Wrap|Nowrap}]|[,Around]]{}[,comments])
-
-*¥Ñ¥é¥á¡¼¥¿
--filename~
- ÅºÉÕ¥Õ¥¡¥¤¥ë̾¡¢¤¢¤ë¤¤¤ÏURL
- '¥Ú¡¼¥¸Ì¾/źÉÕ¥Õ¥¡¥¤¥ë̾'¤ò»ØÄꤹ¤ë¤È¡¢¤½¤Î¥Ú¡¼¥¸¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò»²¾È¤¹¤ë
--Left|Center|Right~
- ²£¤Î°ÌÃÖ¹ç¤ï¤»
--Wrap|Nowrap~
- ¥Æ¡¼¥Ö¥ë¥¿¥°¤Ç°Ï¤à/°Ï¤Þ¤Ê¤¤
--Around~
- ¥Æ¥­¥¹¥È¤Î²ó¤ê¹þ¤ß
+// File icon image
+if (! defined('FILE_ICON'))
+       define('FILE_ICON',
+       '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
+       ' alt="file" style="border-width:0px" />');
 
-*/
+/////////////////////////////////////////////////
+// Default settings
 
-// upload dir(must set end of /)
-if (!defined('UPLOAD_DIR'))
-{
-       define('UPLOAD_DIR','./attach/');
-}
+// Horizontal alignment
+define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
 
-// file icon image
-if (!defined('FILE_ICON'))
-{
-       define('FILE_ICON','<img src="./image/file.png" width="20" height="20" alt="file" style="border-width:0px" />');
-}
+// Text wrapping
+define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
 
-// default alignment
-define('REF_DEFAULT_ALIGN','left'); // 'left','center','right'
+// URL指定時に画像サイズを取得するか
+define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
 
-// force wrap on default
-define('REF_WRAP_TABLE',FALSE); // TRUE,FALSE
+// UPLOAD_DIR のデータ(画像ファイルのみ)に直接アクセスさせる
+define('PLUGIN_REF_DIRECT_ACCESS', FALSE); // FALSE or TRUE
+// - これは従来のインラインイメージ処理を互換のために残すもので
+//   あり、高速化のためのオプションではありません
+// - UPLOAD_DIR をWebサーバー上に露出させており、かつ直接アクセス
+//   できる(アクセス制限がない)状態である必要があります
+// - Apache などでは UPLOAD_DIR/.htaccess を削除する必要があります
+// - ブラウザによってはインラインイメージの表示や、「インライン
+//   イメージだけを表示」させた時などに不具合が出る場合があります
+
+/////////////////////////////////////////////////
+
+// Image suffixes allowed
+define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
+
+// Usage (a part of)
+define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
 
 function plugin_ref_inline()
 {
-       global $vars;
-       
-       //¥¨¥é¡¼¥Á¥§¥Ã¥¯
-       if (!func_num_args())
-       {
-               return 'no argument(s).';
+       // Not reached, because of "$aryargs[] = & $body" at plugin.php
+       // if (! func_num_args())
+       //      return '&amp;ref(): Usage:' . PLUGIN_REF_USAGE . ';';
+
+       $params = plugin_ref_body(func_get_args());
+
+       if (isset($params['_error']) && $params['_error'] != '') {
+               // Error
+               return '&amp;ref(): ' . $params['_error'] . ';';
+       } else {
+               return $params['_body'];
        }
-       
-       $params = plugin_ref_body(func_get_args(),$vars['page']);
-       
-       return ($params['_error'] != '') ? $params['_error'] : $params['_body'];
 }
+
 function plugin_ref_convert()
 {
-       global $vars;
+       if (! func_num_args())
+               return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
 
-       //¥¨¥é¡¼¥Á¥§¥Ã¥¯
-       if (!func_num_args())
-       {
-               return '<p>no argument(s).</p>';
-       }
-       
-       $params = plugin_ref_body(func_get_args(),$vars['page']);
-       
-       if ($params['_error'] != '')
-       {
-               return "<p>{$params['_error']}</p>";
+       $params = plugin_ref_body(func_get_args());
+
+       if (isset($params['_error']) && $params['_error'] != '') {
+               return "<p>#ref(): {$params['_error']}</p>\n";
        }
-       
-       if ((REF_WRAP_TABLE and !$params['nowrap']) or $params['wrap'])
-       {
-               // ÏȤÇÊñ¤à
-               // margin:auto Moz1=x(wrap,around¤¬¸ú¤«¤Ê¤¤),op6=oNN6=x(wrap,around¤¬¸ú¤«¤Ê¤¤)IE6=x(wrap,around¤¬¸ú¤«¤Ê¤¤)
-               // margin:0px Moz1=x(wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤),op6=x(wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤),nn6=x(wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤),IE6=o
+
+       if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
+               // 枠で包む
+               // margin:auto
+               //      Mozilla 1.x  = x (wrap,aroundが効かない)
+               //      Opera 6      = o
+               //      Netscape 6   = x (wrap,aroundが効かない)
+               //      IE 6         = x (wrap,aroundが効かない)
+               // margin:0px
+               //      Mozilla 1.x  = x (wrapで寄せが効かない)
+               //      Opera 6      = x (wrapで寄せが効かない)
+               //      Netscape 6   = x (wrapで寄せが効かない)
+               //      IE6          = o
                $margin = ($params['around'] ? '0px' : 'auto');
                $margin_align = ($params['_align'] == 'center') ? '' : ";margin-{$params['_align']}:0px";
                $params['_body'] = <<<EOD
@@ -89,267 +94,341 @@ function plugin_ref_convert()
 </table>
 EOD;
        }
-       // div¤ÇÊñ¤à
-       if ($params['around'])
-       {
+
+       if ($params['around']) {
                $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
-       }
-       else
-       {
+       } else {
                $style = "text-align:{$params['_align']}";
        }
+
+       // divで包む
        return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
 }
 
-function plugin_ref_body($args,$page)
+function plugin_ref_body($args)
 {
-       global $script,$WikiName,$BracketName;
-       
-       // Ìá¤êÃÍ
-       $params = array();
-       
-       // ÅºÉÕ¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ
+       global $script, $vars;
+       global $WikiName, $BracketName; // compat
+
+       // 戻り値
+       $params = array(
+               'left'   => FALSE, // 左寄せ
+               'center' => FALSE, // 中央寄せ
+               'right'  => FALSE, // 右寄せ
+               'wrap'   => FALSE, // TABLEで囲む
+               'nowrap' => FALSE, // TABLEで囲まない
+               'around' => FALSE, // 回り込み
+               'noicon' => FALSE, // アイコンを表示しない
+               'nolink' => FALSE, // 元ファイルへのリンクを張らない
+               'noimg'  => FALSE, // 画像を展開しない
+               'zoom'   => FALSE, // 縦横比を保持する
+               '_size'  => FALSE, // サイズ指定あり
+               '_w'     => 0,       // 幅
+               '_h'     => 0,       // 高さ
+               '_%'     => 0,     // 拡大率
+               '_args'  => array(),
+               '_done'  => FALSE,
+               '_error' => ''
+       );
+
+       // 添付ファイルのあるページ: defaultは現在のページ名
+       $page = isset($vars['page']) ? $vars['page'] : '';
+
+       // 添付ファイルのファイル名
+       $name = '';
+
+       // 添付ファイルまでのパスおよび(実際の)ファイル名
+       $file = '';
+
+       // 第一引数: "[ページ名および/]添付ファイル名"、あるいは"URL"を取得
        $name = array_shift($args);
-       
-       // ¼¡¤Î°ú¿ô¤¬¥Ú¡¼¥¸Ì¾¤«¤É¤¦¤«
-       if (count($args) and preg_match("/^($WikiName|\[\[$BracketName\]\])$/",$args[0]))
-       {
-               $_page = get_fullname(strip_bracket($args[0]),$page);
-               if (is_pagename($_page))
-               {
-                       $page = $_page;
-                       array_shift($args);
+       $is_url = is_url($name);
+
+       if(! $is_url) {
+               // 添付ファイル
+               if (! is_dir(UPLOAD_DIR)) {
+                       $params['_error'] = 'No UPLOAD_DIR';
+                       return $params;
+               }
+
+               $matches = array();
+               // ファイル名にページ名(ページ参照パス)が合成されているか
+               //   (Page_name/maybe-separated-with/slashes/ATTACHED_FILENAME)
+               if (preg_match('#^(.+)/([^/]+)$#', $name, $matches)) {
+                       if ($matches[1] == '.' || $matches[1] == '..') {
+                               $matches[1] .= '/'; // Restore relative paths
+                       }
+                       $name = $matches[2];
+                       $page = get_fullname(strip_bracket($matches[1]), $page); // strip is a compat
+                       $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
+                       $is_file = is_file($file);
+
+               // 第二引数以降が存在し、それはrefのオプション名称などと一致しない
+               } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
+                       $e_name = encode($name);
+
+                       // Try the second argument, as a page-name or a path-name
+                       $_arg = get_fullname(strip_bracket($args[0]), $page); // strip is a compat
+                       $file = UPLOAD_DIR .  encode($_arg) . '_' . $e_name;
+                       $is_file_second = is_file($file);
+
+                       // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
+                       $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
+
+                       if ($is_file_second && $is_bracket_bracket) {
+                               // Believe the second argument (compat)
+                               array_shift($args);
+                               $page = $_arg;
+                               $is_file = TRUE;
+                       } else {
+                               // Try default page, with default params
+                               $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
+
+                               // Promote new design
+                               if ($is_file_default && $is_file_second) {
+                                       // Because of race condition NOW
+                                       $params['_error'] = htmlsc('The same file name "' .
+                                               $name . '" at both page: "' .  $page . '" and "' .  $_arg .
+                                               '". Try ref(pagename/filename) to specify one of them');
+                               } else {
+                                       // Because of possibility of race condition, in the future
+                                       $params['_error'] = 'The style ref(filename,pagename) is ambiguous ' .
+                                               'and become obsolete. ' .
+                                               'Please try ref(pagename/filename)';
+                               }
+                               return $params;
+                       }
+               } else {
+                       // Simple single argument
+                       $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
+                       $is_file = is_file($file);
+               }
+               if (! $is_file) {
+                       $params['_error'] = htmlsc('File not found: "' .
+                               $name . '" at page "' . $page . '"');
+                       return $params;
                }
        }
-       
+
+       // 残りの引数の処理
+       if (! empty($args))
+               foreach ($args as $arg)
+                       ref_check_arg($arg, $params);
+
 /*
- $name¤ò¤â¤È¤Ë°Ê²¼¤ÎÊÑ¿ô¤òÀßÄê
+ $nameをもとに以下の変数を設定
  $url,$url2 : URL
- $title :¥¿¥¤¥È¥ë
- $is_image : ²èÁü¤Î¤È¤­TRUE
- $info : ²èÁü¥Õ¥¡¥¤¥ë¤Î¤È¤­getimagesize()¤Î'size'
-         ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¥Õ¥¡¥¤¥ë¤Î¾ðÊó
-         ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¤È¤­ : ¥Õ¥¡¥¤¥ë¤ÎºÇ½ª¹¹¿·Æü¤È¥µ¥¤¥º
-         URL¤Î¤È¤­ : URL¤½¤Î¤â¤Î
+ $title :タイトル
+ $is_image : 画像のときTRUE
+ $info : 画像ファイルのときgetimagesize()の'size'
+         画像ファイル以外のファイルの情報
+         æ·»ä»\98ã\83\95ã\82¡ã\82¤ã\83«ã\81®ã\81¨ã\81\8d : ã\83\95ã\82¡ã\82¤ã\83«ã\81®æ\9c\80çµ\82æ\9b´æ\96°æ\97¥ã\81¨ã\82µã\82¤ã\82º
+         URLのとき : URLそのもの
 */
-       $file = $title = $url = $url2 = $info = $title = '';
+       $title = $url = $url2 = $info = '';
        $width = $height = 0;
-       
-       if (is_url($name))      //URL
-       {
-               $url = htmlspecialchars($name);
-               $title = preg_match('/([^\/]+)$/', $name, $match) ? $match[1] : $url;
-               
-               $is_image = preg_match("/\.(gif|png|jpe?g)$/i",$name);
-               if ($is_image and (bool)ini_get('allow_url_fopen'))
-               {
-                       $size = getimagesize($name);
-                       if (is_array($size))
-                       {
-                               $width = $size[0];
-                               $height = $size[1];
-                               $info = $size[3];
-                       }
-               }
-               else
-               {
-                       $info = $url;
-               }
-       }
-       else    //źÉÕ¥Õ¥¡¥¤¥ë
-       {
-               if (!is_dir(UPLOAD_DIR))
-               {
-                       $params['_error'] = 'no UPLOAD_DIR.';
+       $matches = array();
+
+       if ($is_url) {  // URL
+               if (PKWK_DISABLE_INLINE_IMAGE_FROM_URI) {
+                       //$params['_error'] = 'PKWK_DISABLE_INLINE_IMAGE_FROM_URI prohibits this';
+                       //return $params;
+                       $url = htmlsc($name);
+                       $params['_body'] = '<a href="' . $url . '">' . $url . '</a>';
                        return $params;
                }
-               
-               //¥Ú¡¼¥¸»ØÄê¤Î¥Á¥§¥Ã¥¯
-//             $page = $vars['page'];
-               if (preg_match('/^(.+)\/([^\/]+)$/',$name,$matches))
-               {
-                       if ($matches[1] == '.' or $matches[1] == '..')
-                       {
-                               $matches[1] .= '/';
+
+               $url = $url2 = htmlsc($name);
+               $title = htmlsc(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
+
+               $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
+
+               if ($is_image && PLUGIN_REF_URL_GET_IMAGE_SIZE && (bool)ini_get('allow_url_fopen')) {
+                       $size = @getimagesize($name);
+                       if (is_array($size)) {
+                               $width  = $size[0];
+                               $height = $size[1];
+                               $info   = $size[3];
                        }
-                       $page = get_fullname($matches[1],$page);
-                       $name = $matches[2];
                }
-               $title = $name;
-               $file = UPLOAD_DIR.encode($page).'_'.encode($name);
-               if (!is_file($file))
-               {
-                       $params['_error'] = 'file not found.';
-                       return $params;
-               }
-               $size = getimagesize($file);
-               $is_image = preg_match("/\.(gif|png|jpe?g)$/i",$name);
-               $width = $height = 0;
-               $url = $script.'?plugin=attach&amp;openfile='.rawurlencode($name).'&amp;refer='.rawurlencode($page);
-               if ($is_image)
-               {
+
+       } else { // 添付ファイル
+
+               $title = htmlsc($name);
+
+               $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
+
+               // Count downloads with attach plugin
+               $url = $script . '?plugin=attach' . '&amp;refer=' . rawurlencode($page) .
+                       '&amp;openfile=' . rawurlencode($name); // Show its filename at the last
+
+               if ($is_image) {
+                       // Swap $url
                        $url2 = $url;
-                       $url = $file;
-                       if (is_array($size))
-                       {
-                               $width = $size[0];
+
+                       // URI for in-line image output
+                       if (! PLUGIN_REF_DIRECT_ACCESS) {
+                               // With ref plugin (faster than attach)
+                               $url = $script . '?plugin=ref' . '&amp;page=' . rawurlencode($page) .
+                                       '&amp;src=' . rawurlencode($name); // Show its filename at the last
+                       } else {
+                               // Try direct-access, if possible
+                               $url = $file;
+                       }
+
+                       $width = $height = 0;
+                       $size = @getimagesize($file);
+                       if (is_array($size)) {
+                               $width  = $size[0];
                                $height = $size[1];
                        }
-               }
-               else
-               {
-                       $info = get_date('Y/m/d H:i:s',filemtime($file) - LOCALZONE).' '.sprintf('%01.1f',round(filesize($file)/1000,1)).'KB';
+               } else {
+                       $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
+                               ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
                }
        }
-       
-       //¥Ñ¥é¥á¡¼¥¿
-       $params = array('left'=>FALSE,'center'=>FALSE,'right'=>FALSE,'wrap'=>FALSE,'nowrap'=>FALSE,'around'=>FALSE,'noicon'=>FALSE,
-               'zoom'=>FALSE,'size'=>FALSE,'w'=>0,'h'=>0,'%'=>0,'_args'=>array(),'_done'=>FALSE,'_error'=>'');
-       
-       if (count($args) > 0)
-       {
-               array_walk($args, 'ref_check_arg', &$params);
-       }
-       
-       //³ÈÄ¥¥Ñ¥é¥á¡¼¥¿¤ò¥Á¥§¥Ã¥¯
-       if (count($params['_args']))
-       {
+
+       // 拡張パラメータをチェック
+       if (! empty($params['_args'])) {
                $_title = array();
-               foreach ($params['_args'] as $arg)
-               {
-                       if (preg_match('/^([0-9]+)x([0-9]+)$/',$arg,$m))
-                       {
-                               $params['size'] = TRUE;
-                               $params['w'] = $m[1];
-                               $params['h'] = $m[2];
-                       }
-                       else if (preg_match('/^([0-9.]+)%$/',$arg,$m) and $m[1] > 0)
-                       {
-                               $params['%'] = $m[1];
-                       }
-                       else
-                       {
+               foreach ($params['_args'] as $arg) {
+                       if (preg_match('/^([0-9]+)x([0-9]+)$/', $arg, $matches)) {
+                               $params['_size'] = TRUE;
+                               $params['_w'] = $matches[1];
+                               $params['_h'] = $matches[2];
+
+                       } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
+                               $params['_%'] = $matches[1];
+
+                       } else {
                                $_title[] = $arg;
                        }
                }
-               if (count($_title))
-               {
-                       $title2 = $title;
-                       $title = join(',', $_title);
-                       if ($is_image)
-                       {
-                               if (is_url($title))
-                               {
-                                       $url2 = $title;
-                                       $title = $title2;
-                               }
-                               else
-                               {
-                                       $title = htmlspecialchars($title);
-                               }
-                       }
-                       else
-                       {
-                               $title = make_line_rules($title);
-                       }
+
+               if (! empty($_title)) {
+                       $title = htmlsc(join(',', $_title));
+                       if ($is_image) $title = make_line_rules($title);
                }
        }
-       //²èÁü¥µ¥¤¥ºÄ´À°
-       if ($is_image)
-       {
-               // »ØÄꤵ¤ì¤¿¥µ¥¤¥º¤ò»ÈÍѤ¹¤ë
-               if ($params['size'])
-               {
-                       if ($width == 0 and $height == 0)
-                       {
-                               $width = $params['w'];
-                               $height = $params['h'];
-                       }
-                       else if ($params['zoom'])
-                       {
-                               $_w = $params['w'] ? $width / $params['w'] : 0;
-                               $_h = $params['h'] ? $height / $params['h'] : 0;
-                               $zoom = max($_w,$_h);
-                               if ($zoom)
-                               {
-                                       $width = (int)($width / $zoom);
+
+       // 画像サイズ調整
+       if ($is_image) {
+               // 指定されたサイズを使用する
+               if ($params['_size']) {
+                       if ($width == 0 && $height == 0) {
+                               $width  = $params['_w'];
+                               $height = $params['_h'];
+                       } else if ($params['zoom']) {
+                               $_w = $params['_w'] ? $width  / $params['_w'] : 0;
+                               $_h = $params['_h'] ? $height / $params['_h'] : 0;
+                               $zoom = max($_w, $_h);
+                               if ($zoom) {
+                                       $width  = (int)($width  / $zoom);
                                        $height = (int)($height / $zoom);
                                }
+                       } else {
+                               $width  = $params['_w'] ? $params['_w'] : $width;
+                               $height = $params['_h'] ? $params['_h'] : $height;
                        }
-                       else
-                       {
-                               $width = $params['w'] ? $params['w'] : $width;
-                               $height = $params['h'] ? $params['h'] : $height;
-                       }
-               }
-               if ($params['%'])
-               {
-                       $width = (int)($width * $params['%'] / 100);
-                       $height = (int)($height * $params['%'] / 100);
                }
-               if ($width and $height)
-               {
-                       $info = "width=\"$width\" height=\"$height\"";
+               if ($params['_%']) {
+                       $width  = (int)($width  * $params['_%'] / 100);
+                       $height = (int)($height * $params['_%'] / 100);
                }
-       }
-       
-       //¥¢¥é¥¤¥ó¥á¥ó¥ÈȽÄê
-       if ($params['right'])
-       {
-               $params['_align'] = 'right';
-       }
-       else if ($params['left'])
-       {
-               $params['_align'] = 'left';
-       }
-       else if ($params['center'])
-       {
-               $params['_align'] = 'center';
-       }
-       else
-       {
-               $params['_align'] = REF_DEFAULT_ALIGN;
+               if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
        }
 
-       // ¥Õ¥¡¥¤¥ë¼ïÊÌȽÄê
-       if ($is_image)  // ²èÁü
-       {
-               $_url = "<img src=\"$url\" alt=\"$title\" title=\"$title\" $info />";
-               if ($url2)
-               {
-                       $_url = "<a href=\"$url2\" title=\"$title\">$_url</a>";
+       // アラインメント判定
+       $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
+       foreach (array('right', 'left', 'center') as $align) {
+               if ($params[$align])  {
+                       $params['_align'] = $align;
+                       break;
                }
-               $params['_body'] = $_url;
        }
-       else    // Ä̾ï¥Õ¥¡¥¤¥ë
-       {
+
+       if ($is_image) { // 画像
+               $params['_body'] = "<img src=\"$url\" alt=\"$title\" title=\"$title\" $info/>";
+               if (! $params['nolink'] && $url2)
+                       $params['_body'] = "<a href=\"$url2\" title=\"$title\">{$params['_body']}</a>";
+       } else {
                $icon = $params['noicon'] ? '' : FILE_ICON;
-               $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>\n";
+               $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
        }
+
        return $params;
 }
 
-//-----------------------------------------------------------------------------
-//¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
-function ref_check_arg($val, $_key, &$params)
+// オプションを解析する
+function ref_check_arg($val, & $params)
 {
-       if ($val == '')
-       {
+       if ($val == '') {
                $params['_done'] = TRUE;
                return;
        }
-       if (!$params['_done'])
-       {
-               foreach (array_keys($params) as $key)
-               {
-                       if (strpos($key, strtolower($val)) === 0)
-                       {
+
+       if (! $params['_done']) {
+               foreach (array_keys($params) as $key) {
+                       if (strpos($key, strtolower($val)) === 0) {
                                $params[$key] = TRUE;
                                return;
                        }
                }
                $params['_done'] = TRUE;
        }
+
        $params['_args'][] = $val;
 }
+
+// Output an image (fast, non-logging <==> attach plugin)
+function plugin_ref_action()
+{
+       global $vars;
+
+       $usage = 'Usage: plugin=ref&amp;page=page_name&amp;src=attached_image_name';
+
+       if (! isset($vars['page']) || ! isset($vars['src']))
+               return array('msg'=>'Invalid argument', 'body'=>$usage);
+
+       $page     = $vars['page'];
+       $filename = $vars['src'] ;
+
+       $ref = UPLOAD_DIR . encode($page) . '_' . encode(preg_replace('#^.*/#', '', $filename));
+       if(! file_exists($ref))
+               return array('msg'=>'Attach file not found', 'body'=>$usage);
+
+       $got = @getimagesize($ref);
+       if (! isset($got[2])) $got[2] = FALSE;
+       switch ($got[2]) {
+       case 1: $type = 'image/gif' ; break;
+       case 2: $type = 'image/jpeg'; break;
+       case 3: $type = 'image/png' ; break;
+       case 4: $type = 'application/x-shockwave-flash'; break;
+       default:
+               return array('msg'=>'Seems not an image', 'body'=>$usage);
+       }
+
+       // Care for Japanese-character-included file name
+       if (LANG == 'ja') {
+               switch(UA_NAME . '/' . UA_PROFILE){
+               case 'Opera/default':
+                       // Care for using _auto-encode-detecting_ function
+                       $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
+                       break;
+               case 'MSIE/default':
+                       $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
+                       break;
+               }
+       }
+       $utf8filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
+       $size = filesize($ref);
+
+       // Output
+       pkwk_common_headers();
+       header('Content-Disposition: inline; filename="' . $filename . '"; filename*=utf-8\'\'' . rawurlencode($utf8filename));
+       header('Content-Length: ' . $size);
+       header('Content-Type: '   . $type);
+       @readfile($ref);
+       exit;
+}
 ?>