OSDN Git Service

BugTrack2/139: Correct. Deleted a first space at preview.
[pukiwiki/pukiwiki.git] / plugin / ref.inc.php
index 65049af..45f0fc1 100644 (file)
@@ -1,84 +1,85 @@
 <?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.49 2005/05/28 13:31:57 henoheno Exp $
 //
+// 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'])
-       {
+
+       if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $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
+               // 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,39 +90,127 @@ 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;
-       
+       global $script, $vars;
+       global $WikiName, $BracketName; // compat
+
        // Ìá¤êÃÍ
-       $params = array();
-       
-       // ÅºÉÕ¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ
+       $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'] = htmlspecialchars('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/filname)';
+                               }
+                               return $params;
+                       }
+               } else {
+                       // Simple single argument
+                       $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
+                       $is_file = is_file($file);
+               }
+               if (! $is_file) {
+                       $params['_error'] = htmlspecialchars('File not found: "' .
+                               $name . '" at page "' . $page . '"');
+                       return $params;
                }
        }
-       
+
+       // »Ä¤ê¤Î°ú¿ô¤Î½èÍý
+       if (! empty($args))
+               foreach ($args as $arg)
+                       ref_check_arg($arg, $params);
+
 /*
  $name¤ò¤â¤È¤Ë°Ê²¼¤ÎÊÑ¿ô¤òÀßÄê
  $url,$url2 : URL
@@ -132,224 +221,210 @@ function plugin_ref_body($args,$page)
          ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¤È¤­ : ¥Õ¥¡¥¤¥ë¤ÎºÇ½ª¹¹¿·Æü¤È¥µ¥¤¥º
          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 = htmlspecialchars($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 = htmlspecialchars($name);
+               $title = htmlspecialchars(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 = htmlspecialchars($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 = htmlspecialchars(join(',', $_title));
+                       if ($is_image) $title = make_line_rules($title);
                }
        }
-       //²èÁü¥µ¥¤¥ºÄ´À°
-       if ($is_image)
-       {
+
+       // ²èÁü¥µ¥¤¥ºÄ´À°
+       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 ($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;
+               }
+       }
+       $file = htmlspecialchars($filename);
+       $size = filesize($ref);
+
+       // Output
+       pkwk_common_headers();
+       header('Content-Disposition: inline; filename="' . $filename . '"');
+       header('Content-Length: ' . $size);
+       header('Content-Type: '   . $type);
+       @readfile($ref);
+       exit;
+}
 ?>