2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: ref.inc.php,v 1.49 2005/05/28 13:31:57 henoheno Exp $
5 // Image refernce plugin
6 // Include an attached image-file as an inline-image
9 if (! defined('FILE_ICON'))
11 '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
12 ' alt="file" style="border-width:0px" />');
14 /////////////////////////////////////////////////
17 // Horizontal alignment
18 define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
21 define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
23 // URL»ØÄê»þ¤Ë²èÁü¥µ¥¤¥º¤ò¼èÆÀ¤¹¤ë¤«
24 define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
26 // UPLOAD_DIR ¤Î¥Ç¡¼¥¿(²èÁü¥Õ¥¡¥¤¥ë¤Î¤ß)¤ËľÀÜ¥¢¥¯¥»¥¹¤µ¤»¤ë
27 define('PLUGIN_REF_DIRECT_ACCESS', FALSE); // FALSE or TRUE
28 // - ¤³¤ì¤Ï½¾Íè¤Î¥¤¥ó¥é¥¤¥ó¥¤¥á¡¼¥¸½èÍý¤ò¸ß´¹¤Î¤¿¤á¤Ë»Ä¤¹¤â¤Î¤Ç
29 // ¤¢¤ê¡¢¹â®²½¤Î¤¿¤á¤Î¥ª¥×¥·¥ç¥ó¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó
30 // - UPLOAD_DIR ¤òWeb¥µ¡¼¥Ð¡¼¾å¤ËϪ½Ð¤µ¤»¤Æ¤ª¤ê¡¢¤«¤ÄľÀÜ¥¢¥¯¥»¥¹
31 // ¤Ç¤¤ë(¥¢¥¯¥»¥¹À©¸Â¤¬¤Ê¤¤)¾õÂ֤Ǥ¢¤ëɬÍפ¬¤¢¤ê¤Þ¤¹
32 // - Apache ¤Ê¤É¤Ç¤Ï UPLOAD_DIR/.htaccess ¤òºï½ü¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹
33 // - ¥Ö¥é¥¦¥¶¤Ë¤è¤Ã¤Æ¤Ï¥¤¥ó¥é¥¤¥ó¥¤¥á¡¼¥¸¤Îɽ¼¨¤ä¡¢¡Ö¥¤¥ó¥é¥¤¥ó
34 // ¥¤¥á¡¼¥¸¤À¤±¤òɽ¼¨¡×¤µ¤»¤¿»þ¤Ê¤É¤ËÉÔ¶ñ¹ç¤¬½Ð¤ë¾ì¹ç¤¬¤¢¤ê¤Þ¤¹
36 /////////////////////////////////////////////////
38 // Image suffixes allowed
39 define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
42 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
44 function plugin_ref_inline()
46 // Not reached, because of "$aryargs[] = & $body" at plugin.php
47 // if (! func_num_args())
48 // return '&ref(): Usage:' . PLUGIN_REF_USAGE . ';';
50 $params = plugin_ref_body(func_get_args());
52 if (isset($params['_error']) && $params['_error'] != '') {
54 return '&ref(): ' . $params['_error'] . ';';
56 return $params['_body'];
60 function plugin_ref_convert()
62 if (! func_num_args())
63 return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
65 $params = plugin_ref_body(func_get_args());
67 if (isset($params['_error']) && $params['_error'] != '') {
68 return "<p>#ref(): {$params['_error']}</p>\n";
71 if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
74 // Mozilla 1.x = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
76 // Netscape 6 = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
77 // IE 6 = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
79 // Mozilla 1.x = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
80 // Opera 6 = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
81 // Netscape 6 = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
83 $margin = ($params['around'] ? '0px' : 'auto');
84 $margin_align = ($params['_align'] == 'center') ? '' : ";margin-{$params['_align']}:0px";
85 $params['_body'] = <<<EOD
86 <table class="style_table" style="margin:$margin$margin_align">
88 <td class="style_td">{$params['_body']}</td>
94 if ($params['around']) {
95 $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
97 $style = "text-align:{$params['_align']}";
101 return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
104 function plugin_ref_body($args)
106 global $script, $vars;
107 global $WikiName, $BracketName; // compat
111 'left' => FALSE, // º¸´ó¤»
112 'center' => FALSE, // Ãæ±û´ó¤»
113 'right' => FALSE, // ±¦´ó¤»
114 'wrap' => FALSE, // TABLE¤Ç°Ï¤à
115 'nowrap' => FALSE, // TABLE¤Ç°Ï¤Þ¤Ê¤¤
116 'around' => FALSE, // ²ó¤ê¹þ¤ß
117 'noicon' => FALSE, // ¥¢¥¤¥³¥ó¤òɽ¼¨¤·¤Ê¤¤
118 'nolink' => FALSE, // ¸µ¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯¤òÄ¥¤é¤Ê¤¤
119 'noimg' => FALSE, // ²èÁü¤òŸ³«¤·¤Ê¤¤
120 'zoom' => FALSE, // ½Ä²£Èæ¤òÊÝ»ý¤¹¤ë
121 '_size' => FALSE, // ¥µ¥¤¥º»ØÄꤢ¤ê
130 // źÉÕ¥Õ¥¡¥¤¥ë¤Î¤¢¤ë¥Ú¡¼¥¸: default¤Ï¸½ºß¤Î¥Ú¡¼¥¸Ì¾
131 $page = isset($vars['page']) ? $vars['page'] : '';
133 // źÉÕ¥Õ¥¡¥¤¥ë¤Î¥Õ¥¡¥¤¥ë̾
136 // źÉÕ¥Õ¥¡¥¤¥ë¤Þ¤Ç¤Î¥Ñ¥¹¤ª¤è¤Ó(¼ÂºÝ¤Î)¥Õ¥¡¥¤¥ë̾
139 // Âè°ì°ú¿ô: "[¥Ú¡¼¥¸Ì¾¤ª¤è¤Ó/]źÉÕ¥Õ¥¡¥¤¥ë̾"¡¢¤¢¤ë¤¤¤Ï"URL"¤ò¼èÆÀ
140 $name = array_shift($args);
141 $is_url = is_url($name);
145 if (! is_dir(UPLOAD_DIR)) {
146 $params['_error'] = 'No UPLOAD_DIR';
151 // ¥Õ¥¡¥¤¥ë̾¤Ë¥Ú¡¼¥¸Ì¾(¥Ú¡¼¥¸»²¾È¥Ñ¥¹)¤¬¹çÀ®¤µ¤ì¤Æ¤¤¤ë¤«
152 // (Page_name/maybe-separated-with/slashes/ATTACHED_FILENAME)
153 if (preg_match('#^(.+)/([^/]+)$#', $name, $matches)) {
154 if ($matches[1] == '.' || $matches[1] == '..') {
155 $matches[1] .= '/'; // Restore relative paths
158 $page = get_fullname(strip_bracket($matches[1]), $page); // strip is a compat
159 $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
160 $is_file = is_file($file);
162 // ÂèÆó°ú¿ô°Ê¹ß¤¬Â¸ºß¤·¡¢¤½¤ì¤Ïref¤Î¥ª¥×¥·¥ç¥ó̾¾Î¤Ê¤É¤È°ìÃפ·¤Ê¤¤
163 } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
164 $e_name = encode($name);
166 // Try the second argument, as a page-name or a path-name
167 $_arg = get_fullname(strip_bracket($args[0]), $page); // strip is a compat
168 $file = UPLOAD_DIR . encode($_arg) . '_' . $e_name;
169 $is_file_second = is_file($file);
171 // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
172 $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
174 if ($is_file_second && $is_bracket_bracket) {
175 // Believe the second argument (compat)
180 // Try default page, with default params
181 $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
183 // Promote new design
184 if ($is_file_default && $is_file_second) {
185 // Because of race condition NOW
186 $params['_error'] = htmlspecialchars('The same file name "' .
187 $name . '" at both page: "' . $page . '" and "' . $_arg .
188 '". Try ref(pagename/filename) to specify one of them');
190 // Because of possibility of race condition, in the future
191 $params['_error'] = 'The style ref(filename,pagename) is ambiguous ' .
192 'and become obsolete. ' .
193 'Please try ref(pagename/filname)';
198 // Simple single argument
199 $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
200 $is_file = is_file($file);
203 $params['_error'] = htmlspecialchars('File not found: "' .
204 $name . '" at page "' . $page . '"');
211 foreach ($args as $arg)
212 ref_check_arg($arg, $params);
215 $name¤ò¤â¤È¤Ë°Ê²¼¤ÎÊÑ¿ô¤òÀßÄê
218 $is_image : ²èÁü¤Î¤È¤TRUE
219 $info : ²èÁü¥Õ¥¡¥¤¥ë¤Î¤È¤getimagesize()¤Î'size'
220 ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¥Õ¥¡¥¤¥ë¤Î¾ðÊó
221 źÉÕ¥Õ¥¡¥¤¥ë¤Î¤È¤ : ¥Õ¥¡¥¤¥ë¤ÎºÇ½ª¹¹¿·Æü¤È¥µ¥¤¥º
222 URL¤Î¤È¤ : URL¤½¤Î¤â¤Î
224 $title = $url = $url2 = $info = '';
225 $width = $height = 0;
228 if ($is_url) { // URL
229 if (PKWK_DISABLE_INLINE_IMAGE_FROM_URI) {
230 //$params['_error'] = 'PKWK_DISABLE_INLINE_IMAGE_FROM_URI prohibits this';
232 $url = htmlspecialchars($name);
233 $params['_body'] = '<a href="' . $url . '">' . $url . '</a>';
237 $url = $url2 = htmlspecialchars($name);
238 $title = htmlspecialchars(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
240 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
242 if ($is_image && PLUGIN_REF_URL_GET_IMAGE_SIZE && (bool)ini_get('allow_url_fopen')) {
243 $size = @getimagesize($name);
244 if (is_array($size)) {
251 } else { // źÉÕ¥Õ¥¡¥¤¥ë
253 $title = htmlspecialchars($name);
255 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
257 // Count downloads with attach plugin
258 $url = $script . '?plugin=attach' . '&refer=' . rawurlencode($page) .
259 '&openfile=' . rawurlencode($name); // Show its filename at the last
265 // URI for in-line image output
266 if (! PLUGIN_REF_DIRECT_ACCESS) {
267 // With ref plugin (faster than attach)
268 $url = $script . '?plugin=ref' . '&page=' . rawurlencode($page) .
269 '&src=' . rawurlencode($name); // Show its filename at the last
271 // Try direct-access, if possible
275 $width = $height = 0;
276 $size = @getimagesize($file);
277 if (is_array($size)) {
282 $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
283 ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
287 // ³ÈÄ¥¥Ñ¥é¥á¡¼¥¿¤ò¥Á¥§¥Ã¥¯
288 if (! empty($params['_args'])) {
290 foreach ($params['_args'] as $arg) {
291 if (preg_match('/^([0-9]+)x([0-9]+)$/', $arg, $matches)) {
292 $params['_size'] = TRUE;
293 $params['_w'] = $matches[1];
294 $params['_h'] = $matches[2];
296 } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
297 $params['_%'] = $matches[1];
304 if (! empty($_title)) {
305 $title = htmlspecialchars(join(',', $_title));
306 if ($is_image) $title = make_line_rules($title);
312 // »ØÄꤵ¤ì¤¿¥µ¥¤¥º¤ò»ÈÍѤ¹¤ë
313 if ($params['_size']) {
314 if ($width == 0 && $height == 0) {
315 $width = $params['_w'];
316 $height = $params['_h'];
317 } else if ($params['zoom']) {
318 $_w = $params['_w'] ? $width / $params['_w'] : 0;
319 $_h = $params['_h'] ? $height / $params['_h'] : 0;
320 $zoom = max($_w, $_h);
322 $width = (int)($width / $zoom);
323 $height = (int)($height / $zoom);
326 $width = $params['_w'] ? $params['_w'] : $width;
327 $height = $params['_h'] ? $params['_h'] : $height;
331 $width = (int)($width * $params['_%'] / 100);
332 $height = (int)($height * $params['_%'] / 100);
334 if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
337 // ¥¢¥é¥¤¥ó¥á¥ó¥ÈȽÄê
338 $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
339 foreach (array('right', 'left', 'center') as $align) {
340 if ($params[$align]) {
341 $params['_align'] = $align;
346 if ($is_image) { // ²èÁü
347 $params['_body'] = "<img src=\"$url\" alt=\"$title\" title=\"$title\" $info/>";
348 if (! $params['nolink'] && $url2)
349 $params['_body'] = "<a href=\"$url2\" title=\"$title\">{$params['_body']}</a>";
351 $icon = $params['noicon'] ? '' : FILE_ICON;
352 $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
358 // ¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
359 function ref_check_arg($val, & $params)
362 $params['_done'] = TRUE;
366 if (! $params['_done']) {
367 foreach (array_keys($params) as $key) {
368 if (strpos($key, strtolower($val)) === 0) {
369 $params[$key] = TRUE;
373 $params['_done'] = TRUE;
376 $params['_args'][] = $val;
379 // Output an image (fast, non-logging <==> attach plugin)
380 function plugin_ref_action()
384 $usage = 'Usage: plugin=ref&page=page_name&src=attached_image_name';
386 if (! isset($vars['page']) || ! isset($vars['src']))
387 return array('msg'=>'Invalid argument', 'body'=>$usage);
389 $page = $vars['page'];
390 $filename = $vars['src'] ;
392 $ref = UPLOAD_DIR . encode($page) . '_' . encode(preg_replace('#^.*/#', '', $filename));
393 if(! file_exists($ref))
394 return array('msg'=>'Attach file not found', 'body'=>$usage);
396 $got = @getimagesize($ref);
397 if (! isset($got[2])) $got[2] = FALSE;
399 case 1: $type = 'image/gif' ; break;
400 case 2: $type = 'image/jpeg'; break;
401 case 3: $type = 'image/png' ; break;
402 case 4: $type = 'application/x-shockwave-flash'; break;
404 return array('msg'=>'Seems not an image', 'body'=>$usage);
407 // Care for Japanese-character-included file name
409 switch(UA_NAME . '/' . UA_PROFILE){
410 case 'Opera/default':
411 // Care for using _auto-encode-detecting_ function
412 $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
415 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
419 $file = htmlspecialchars($filename);
420 $size = filesize($ref);
423 pkwk_common_headers();
424 header('Content-Disposition: inline; filename="' . $filename . '"');
425 header('Content-Length: ' . $size);
426 header('Content-Type: ' . $type);