2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: ref.inc.php,v 1.47 2004/12/25 00:39:45 henoheno Exp $
7 // Include an attached image-file as an inline-image
10 if (! defined('FILE_ICON')) {
12 '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
13 ' alt="file" style="border-width:0px" />');
16 /////////////////////////////////////////////////
19 // Horizontal alignment
20 define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
23 define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
25 // URL»ØÄê»þ¤Ë²èÁü¥µ¥¤¥º¤ò¼èÆÀ¤¹¤ë¤«
26 define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
28 // UPLOAD_DIR ¤Î¥Ç¡¼¥¿(²èÁü¥Õ¥¡¥¤¥ë¤Î¤ß)¤ËľÀÜ¥¢¥¯¥»¥¹¤µ¤»¤ë
29 define('PLUGIN_REF_DIRECT_ACCESS', FALSE); // FALSE or TRUE
30 // - ¤³¤ì¤Ï½¾Íè¤Î¥¤¥ó¥é¥¤¥ó¥¤¥á¡¼¥¸½èÍý¤ò¸ß´¹¤Î¤¿¤á¤Ë»Ä¤¹¤â¤Î¤Ç
31 // ¤¢¤ê¡¢¹â®²½¤Î¤¿¤á¤Î¥ª¥×¥·¥ç¥ó¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó
32 // - UPLOAD_DIR ¤òWeb¥µ¡¼¥Ð¡¼¾å¤ËϪ½Ð¤µ¤»¤Æ¤ª¤ê¡¢¤«¤ÄľÀÜ¥¢¥¯¥»¥¹
33 // ¤Ç¤¤ë(¥¢¥¯¥»¥¹À©¸Â¤¬¤Ê¤¤)¾õÂ֤Ǥ¢¤ëɬÍפ¬¤¢¤ê¤Þ¤¹
34 // - Apache ¤Ê¤É¤Ç¤Ï UPLOAD_DIR/.htaccess ¤òºï½ü¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹
35 // - ¥Ö¥é¥¦¥¶¤Ë¤è¤Ã¤Æ¤Ï¥¤¥ó¥é¥¤¥ó¥¤¥á¡¼¥¸¤Îɽ¼¨¤ä¡¢¡Ö¥¤¥ó¥é¥¤¥ó
36 // ¥¤¥á¡¼¥¸¤À¤±¤òɽ¼¨¡×¤µ¤»¤¿»þ¤Ê¤É¤ËÉÔ¶ñ¹ç¤¬½Ð¤ë¾ì¹ç¤¬¤¢¤ê¤Þ¤¹
38 /////////////////////////////////////////////////
40 // Image suffixes allowed
41 define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
44 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
46 function plugin_ref_inline()
48 // Not reached, because of "$aryargs[] = & $body" at plugin.php
49 // if (! func_num_args())
50 // return '&ref(): Usage:' . PLUGIN_REF_USAGE . ';';
52 $params = plugin_ref_body(func_get_args());
54 if (isset($params['_error']) && $params['_error'] != '') {
56 return '&ref(): ' . $params['_error'] . ';';
58 return $params['_body'];
62 function plugin_ref_convert()
64 if (! func_num_args())
65 return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
67 $params = plugin_ref_body(func_get_args());
69 if (isset($params['_error']) && $params['_error'] != '') {
70 return "<p>#ref(): {$params['_error']}</p>\n";
73 if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
76 // Mozilla 1.x = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
78 // Netscape 6 = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
79 // IE 6 = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
81 // Mozilla 1.x = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
82 // Opera 6 = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
83 // Netscape 6 = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
85 $margin = ($params['around'] ? '0px' : 'auto');
86 $margin_align = ($params['_align'] == 'center') ? '' : ";margin-{$params['_align']}:0px";
87 $params['_body'] = <<<EOD
88 <table class="style_table" style="margin:$margin$margin_align">
90 <td class="style_td">{$params['_body']}</td>
96 if ($params['around']) {
97 $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
99 $style = "text-align:{$params['_align']}";
103 return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
106 function plugin_ref_body($args)
108 global $script, $vars;
109 global $WikiName, $BracketName; // compat
113 'left' => FALSE, // º¸´ó¤»
114 'center' => FALSE, // Ãæ±û´ó¤»
115 'right' => FALSE, // ±¦´ó¤»
116 'wrap' => FALSE, // TABLE¤Ç°Ï¤à
117 'nowrap' => FALSE, // TABLE¤Ç°Ï¤Þ¤Ê¤¤
118 'around' => FALSE, // ²ó¤ê¹þ¤ß
119 'noicon' => FALSE, // ¥¢¥¤¥³¥ó¤òɽ¼¨¤·¤Ê¤¤
120 'nolink' => FALSE, // ¸µ¥Õ¥¡¥¤¥ë¤Ø¤Î¥ê¥ó¥¯¤òÄ¥¤é¤Ê¤¤
121 'noimg' => FALSE, // ²èÁü¤òŸ³«¤·¤Ê¤¤
122 'zoom' => FALSE, // ½Ä²£Èæ¤òÊÝ»ý¤¹¤ë
123 '_size' => FALSE, // ¥µ¥¤¥º»ØÄꤢ¤ê
132 // źÉÕ¥Õ¥¡¥¤¥ë¤Î¤¢¤ë¥Ú¡¼¥¸: default¤Ï¸½ºß¤Î¥Ú¡¼¥¸Ì¾
133 $page = isset($vars['page']) ? $vars['page'] : '';
135 // źÉÕ¥Õ¥¡¥¤¥ë¤Î¥Õ¥¡¥¤¥ë̾
138 // źÉÕ¥Õ¥¡¥¤¥ë¤Þ¤Ç¤Î¥Ñ¥¹¤ª¤è¤Ó(¼ÂºÝ¤Î)¥Õ¥¡¥¤¥ë̾
141 // Âè°ì°ú¿ô: "[¥Ú¡¼¥¸Ì¾¤ª¤è¤Ó/]źÉÕ¥Õ¥¡¥¤¥ë̾"¡¢¤¢¤ë¤¤¤Ï"URL"¤ò¼èÆÀ
142 $name = array_shift($args);
143 $is_url = is_url($name);
147 if (! is_dir(UPLOAD_DIR)) {
148 $params['_error'] = 'No UPLOAD_DIR';
153 // ¥Õ¥¡¥¤¥ë̾¤Ë¥Ú¡¼¥¸Ì¾(¥Ú¡¼¥¸»²¾È¥Ñ¥¹)¤¬¹çÀ®¤µ¤ì¤Æ¤¤¤ë¤«
154 // (Page_name/maybe-separated-with/slashes/ATTACHED_FILENAME)
155 if (preg_match('#^(.+)/([^/]+)$#', $name, $matches)) {
156 if ($matches[1] == '.' || $matches[1] == '..') {
157 $matches[1] .= '/'; // Restore relative paths
160 $page = get_fullname(strip_bracket($matches[1]), $page); // strip is a compat
161 $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
162 $is_file = is_file($file);
164 // ÂèÆó°ú¿ô°Ê¹ß¤¬Â¸ºß¤·¡¢¤½¤ì¤Ïref¤Î¥ª¥×¥·¥ç¥ó̾¾Î¤Ê¤É¤È°ìÃפ·¤Ê¤¤
165 } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
166 $e_name = encode($name);
168 // Try the second argument, as a page-name or a path-name
169 $_arg = get_fullname(strip_bracket($args[0]), $page); // strip is a compat
170 $file = UPLOAD_DIR . encode($_arg) . '_' . $e_name;
171 $is_file_second = is_file($file);
173 // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
174 $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
176 if ($is_file_second && $is_bracket_bracket) {
177 // Believe the second argument (compat)
182 // Try default page, with default params
183 $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
185 // Promote new design
186 if ($is_file_default && $is_file_second) {
187 // Because of race condition NOW
188 $params['_error'] = htmlspecialchars('The same file name "' .
189 $name . '" at both page: "' . $page . '" and "' . $_arg .
190 '". Try ref(pagename/filname) to specify one of them');
192 // Because of possibility of race condition, in the future
193 $params['_error'] = 'The style ref(filename,pagename) is ambiguous ' .
194 'and become obsolete. ' .
195 'Please try ref(pagename/filname)';
200 // Simple single argument
201 $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
202 $is_file = is_file($file);
205 $params['_error'] = htmlspecialchars('File not found: "' .
206 $name . '" at page "' . $page . '"');
213 foreach ($args as $arg)
214 ref_check_arg($arg, $params);
217 $name¤ò¤â¤È¤Ë°Ê²¼¤ÎÊÑ¿ô¤òÀßÄê
220 $is_image : ²èÁü¤Î¤È¤TRUE
221 $info : ²èÁü¥Õ¥¡¥¤¥ë¤Î¤È¤getimagesize()¤Î'size'
222 ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¥Õ¥¡¥¤¥ë¤Î¾ðÊó
223 źÉÕ¥Õ¥¡¥¤¥ë¤Î¤È¤ : ¥Õ¥¡¥¤¥ë¤ÎºÇ½ª¹¹¿·Æü¤È¥µ¥¤¥º
224 URL¤Î¤È¤ : URL¤½¤Î¤â¤Î
226 $title = $url = $url2 = $info = '';
227 $width = $height = 0;
230 if ($is_url) { // URL
231 $url = $url2 = htmlspecialchars($name);
232 $title = htmlspecialchars(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
234 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
236 if ($is_image && PLUGIN_REF_URL_GET_IMAGE_SIZE && (bool)ini_get('allow_url_fopen')) {
237 $size = @getimagesize($name);
238 if (is_array($size)) {
245 } else { // źÉÕ¥Õ¥¡¥¤¥ë
247 $title = htmlspecialchars($name);
249 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
251 // Count downloads with attach plugin
252 $url = $script . '?plugin=attach' . '&refer=' . rawurlencode($page) .
253 '&openfile=' . rawurlencode($name); // Show its filename at the last
259 // URI for in-line image output
260 if (! PLUGIN_REF_DIRECT_ACCESS) {
261 // With ref plugin (faster than attach)
262 $url = $script . '?plugin=ref' . '&page=' . rawurlencode($page) .
263 '&src=' . rawurlencode($name); // Show its filename at the last
265 // Try direct-access, if possible
269 $width = $height = 0;
270 $size = @getimagesize($file);
271 if (is_array($size)) {
276 $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
277 ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
281 // ³ÈÄ¥¥Ñ¥é¥á¡¼¥¿¤ò¥Á¥§¥Ã¥¯
282 if (! empty($params['_args'])) {
284 foreach ($params['_args'] as $arg) {
285 if (preg_match('/^([0-9]+)x([0-9]+)$/', $arg, $matches)) {
286 $params['_size'] = TRUE;
287 $params['_w'] = $matches[1];
288 $params['_h'] = $matches[2];
290 } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
291 $params['_%'] = $matches[1];
298 if (! empty($_title)) {
299 $title = htmlspecialchars(join(',', $_title));
300 if ($is_image) $title = make_line_rules($title);
306 // »ØÄꤵ¤ì¤¿¥µ¥¤¥º¤ò»ÈÍѤ¹¤ë
307 if ($params['_size']) {
308 if ($width == 0 && $height == 0) {
309 $width = $params['_w'];
310 $height = $params['_h'];
311 } else if ($params['zoom']) {
312 $_w = $params['_w'] ? $width / $params['_w'] : 0;
313 $_h = $params['_h'] ? $height / $params['_h'] : 0;
314 $zoom = max($_w, $_h);
316 $width = (int)($width / $zoom);
317 $height = (int)($height / $zoom);
320 $width = $params['_w'] ? $params['_w'] : $width;
321 $height = $params['_h'] ? $params['_h'] : $height;
325 $width = (int)($width * $params['_%'] / 100);
326 $height = (int)($height * $params['_%'] / 100);
328 if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
331 // ¥¢¥é¥¤¥ó¥á¥ó¥ÈȽÄê
332 $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
333 foreach (array('right', 'left', 'center') as $align) {
334 if ($params[$align]) {
335 $params['_align'] = $align;
340 if ($is_image) { // ²èÁü
341 $params['_body'] = "<img src=\"$url\" alt=\"$title\" title=\"$title\" $info/>";
342 if (! $params['nolink'] && $url2)
343 $params['_body'] = "<a href=\"$url2\" title=\"$title\">{$params['_body']}</a>";
345 $icon = $params['noicon'] ? '' : FILE_ICON;
346 $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
352 // ¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
353 function ref_check_arg($val, & $params)
356 $params['_done'] = TRUE;
360 if (! $params['_done']) {
361 foreach (array_keys($params) as $key) {
362 if (strpos($key, strtolower($val)) === 0) {
363 $params[$key] = TRUE;
367 $params['_done'] = TRUE;
370 $params['_args'][] = $val;
373 // Output an image (fast, non-logging <==> attach plugin)
374 function plugin_ref_action()
378 $usage = 'Usage: plugin=ref&page=page_name&src=attached_image_name';
380 if (! isset($vars['page']) || ! isset($vars['src']))
381 return array('msg'=>'Invalid argument', 'body'=>$usage);
383 $page = $vars['page'];
384 $filename = $vars['src'] ;
386 $ref = UPLOAD_DIR . encode($page) . '_' . encode(preg_replace('#^.*/#', '', $filename));
387 if(! file_exists($ref))
388 return array('msg'=>'Attach file not found', 'body'=>$usage);
390 $got = @getimagesize($ref);
391 if (! isset($got[2])) $got[2] = FALSE;
393 case 1: $type = 'image/gif' ; break;
394 case 2: $type = 'image/jpeg'; break;
395 case 3: $type = 'image/png' ; break;
396 case 4: $type = 'application/x-shockwave-flash'; break;
398 return array('msg'=>'Seems not an image', 'body'=>$usage);
401 // Care for Japanese-character-included file name
403 switch(UA_NAME . '/' . UA_PROFILE){
404 case 'Opera/default':
405 // Care for using _auto-encode-detecting_ function
406 $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
409 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
413 $file = htmlspecialchars($filename);
414 $size = filesize($ref);
417 pkwk_common_headers();
418 header('Content-Disposition: inline; filename="' . $filename . '"');
419 header('Content-Length: ' . $size);
420 header('Content-Type: ' . $type);