OSDN Git Service

InterWikiName: Correct Google.jp => Google, Yahoo.jp => Yahoo
[pukiwiki/pukiwiki.git] / plugin / ref.inc.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: ref.inc.php,v 1.47 2004/12/25 00:39:45 henoheno Exp $
6 //
7 // Include an attached image-file as an inline-image
8
9 // File icon image
10 if (! defined('FILE_ICON')) {
11         define('FILE_ICON',
12         '<img src="' . IMAGE_DIR . 'file.png" width="20" height="20"' .
13         ' alt="file" style="border-width:0px" />');
14 }
15
16 /////////////////////////////////////////////////
17 // Default settings
18
19 // Horizontal alignment
20 define('PLUGIN_REF_DEFAULT_ALIGN', 'left'); // 'left', 'center', 'right'
21
22 // Text wrapping
23 define('PLUGIN_REF_WRAP_TABLE', FALSE); // TRUE, FALSE
24
25 // URL»ØÄê»þ¤Ë²èÁü¥µ¥¤¥º¤ò¼èÆÀ¤¹¤ë¤«
26 define('PLUGIN_REF_URL_GET_IMAGE_SIZE', FALSE); // FALSE, TRUE
27
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 //   ¥¤¥á¡¼¥¸¤À¤±¤òɽ¼¨¡×¤µ¤»¤¿»þ¤Ê¤É¤ËÉÔ¶ñ¹ç¤¬½Ð¤ë¾ì¹ç¤¬¤¢¤ê¤Þ¤¹
37
38 /////////////////////////////////////////////////
39
40 // Image suffixes allowed
41 define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
42
43 // Usage (a part of)
44 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
45
46 function plugin_ref_inline()
47 {
48         // Not reached, because of "$aryargs[] = & $body" at plugin.php
49         // if (! func_num_args())
50         //      return '&amp;ref(): Usage:' . PLUGIN_REF_USAGE . ';';
51
52         $params = plugin_ref_body(func_get_args());
53
54         if (isset($params['_error']) && $params['_error'] != '') {
55                 // Error
56                 return '&amp;ref(): ' . $params['_error'] . ';';
57         } else {
58                 return $params['_body'];
59         }
60 }
61
62 function plugin_ref_convert()
63 {
64         if (! func_num_args())
65                 return '<p>#ref(): Usage:' . PLUGIN_REF_USAGE . "</p>\n";
66
67         $params = plugin_ref_body(func_get_args());
68
69         if (isset($params['_error']) && $params['_error'] != '') {
70                 return "<p>#ref(): {$params['_error']}</p>\n";
71         }
72
73         if ((PLUGIN_REF_WRAP_TABLE && ! $params['nowrap']) || $params['wrap']) {
74                 // ÏȤÇÊñ¤à
75                 // margin:auto
76                 //      Mozilla 1.x  = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
77                 //      Opera 6      = o
78                 //      Netscape 6   = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
79                 //      IE 6         = x (wrap,around¤¬¸ú¤«¤Ê¤¤)
80                 // margin:0px
81                 //      Mozilla 1.x  = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
82                 //      Opera 6      = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
83                 //      Netscape 6   = x (wrap¤Ç´ó¤»¤¬¸ú¤«¤Ê¤¤)
84                 //      IE6          = o
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">
89  <tr>
90   <td class="style_td">{$params['_body']}</td>
91  </tr>
92 </table>
93 EOD;
94         }
95
96         if ($params['around']) {
97                 $style = ($params['_align'] == 'right') ? 'float:right' : 'float:left';
98         } else {
99                 $style = "text-align:{$params['_align']}";
100         }
101
102         // div¤ÇÊñ¤à
103         return "<div class=\"img_margin\" style=\"$style\">{$params['_body']}</div>\n";
104 }
105
106 function plugin_ref_body($args)
107 {
108         global $script, $vars;
109         global $WikiName, $BracketName; // compat
110
111         // Ìá¤êÃÍ
112         $params = array(
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, // ¥µ¥¤¥º»ØÄꤢ¤ê
124                 '_w'     => 0,       // Éý
125                 '_h'     => 0,       // ¹â¤µ
126                 '_%'     => 0,     // ³ÈÂçΨ
127                 '_args'  => array(),
128                 '_done'  => FALSE,
129                 '_error' => ''
130         );
131
132         // ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¤¢¤ë¥Ú¡¼¥¸: default¤Ï¸½ºß¤Î¥Ú¡¼¥¸Ì¾
133         $page = isset($vars['page']) ? $vars['page'] : '';
134
135         // ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¥Õ¥¡¥¤¥ë̾
136         $name = '';
137
138         // ÅºÉÕ¥Õ¥¡¥¤¥ë¤Þ¤Ç¤Î¥Ñ¥¹¤ª¤è¤Ó(¼ÂºÝ¤Î)¥Õ¥¡¥¤¥ë̾
139         $file = '';
140
141         // Âè°ì°ú¿ô: "[¥Ú¡¼¥¸Ì¾¤ª¤è¤Ó/]źÉÕ¥Õ¥¡¥¤¥ë̾"¡¢¤¢¤ë¤¤¤Ï"URL"¤ò¼èÆÀ
142         $name = array_shift($args);
143         $is_url = is_url($name);
144
145         if(! $is_url) {
146                 // ÅºÉÕ¥Õ¥¡¥¤¥ë
147                 if (! is_dir(UPLOAD_DIR)) {
148                         $params['_error'] = 'No UPLOAD_DIR';
149                         return $params;
150                 }
151
152                 $matches = array();
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
158                         }
159                         $name = $matches[2];
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);
163
164                 // ÂèÆó°ú¿ô°Ê¹ß¤¬Â¸ºß¤·¡¢¤½¤ì¤Ïref¤Î¥ª¥×¥·¥ç¥ó̾¾Î¤Ê¤É¤È°ìÃפ·¤Ê¤¤
165                 } else if (isset($args[0]) && $args[0] != '' && ! isset($params[$args[0]])) {
166                         $e_name = encode($name);
167
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);
172
173                         // If the second argument is WikiName, or double-bracket-inserted pagename (compat)
174                         $is_bracket_bracket = preg_match("/^($WikiName|\[\[$BracketName\]\])$/", $args[0]);
175
176                         if ($is_file_second && $is_bracket_bracket) {
177                                 // Believe the second argument (compat)
178                                 array_shift($args);
179                                 $page = $_arg;
180                                 $is_file = TRUE;
181                         } else {
182                                 // Try default page, with default params
183                                 $is_file_default = is_file(UPLOAD_DIR . encode($page) . '_' . $e_name);
184
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');
191                                 } else {
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)';
196                                 }
197                                 return $params;
198                         }
199                 } else {
200                         // Simple single argument
201                         $file = UPLOAD_DIR . encode($page) . '_' . encode($name);
202                         $is_file = is_file($file);
203                 }
204                 if (! $is_file) {
205                         $params['_error'] = htmlspecialchars('File not found: "' .
206                                 $name . '" at page "' . $page . '"');
207                         return $params;
208                 }
209         }
210
211         // »Ä¤ê¤Î°ú¿ô¤Î½èÍý
212         if (! empty($args))
213                 foreach ($args as $arg)
214                         ref_check_arg($arg, $params);
215
216 /*
217  $name¤ò¤â¤È¤Ë°Ê²¼¤ÎÊÑ¿ô¤òÀßÄê
218  $url,$url2 : URL
219  $title :¥¿¥¤¥È¥ë
220  $is_image : ²èÁü¤Î¤È¤­TRUE
221  $info : ²èÁü¥Õ¥¡¥¤¥ë¤Î¤È¤­getimagesize()¤Î'size'
222          ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¥Õ¥¡¥¤¥ë¤Î¾ðÊó
223          ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¤È¤­ : ¥Õ¥¡¥¤¥ë¤ÎºÇ½ª¹¹¿·Æü¤È¥µ¥¤¥º
224          URL¤Î¤È¤­ : URL¤½¤Î¤â¤Î
225 */
226         $title = $url = $url2 = $info = '';
227         $width = $height = 0;
228         $matches = array();
229
230         if ($is_url) {  // URL
231                 $url = $url2 = htmlspecialchars($name);
232                 $title = htmlspecialchars(preg_match('/([^\/]+)$/', $name, $matches) ? $matches[1] : $url);
233
234                 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
235
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)) {
239                                 $width  = $size[0];
240                                 $height = $size[1];
241                                 $info   = $size[3];
242                         }
243                 }
244
245         } else { // ÅºÉÕ¥Õ¥¡¥¤¥ë
246
247                 $title = htmlspecialchars($name);
248
249                 $is_image = (! $params['noimg'] && preg_match(PLUGIN_REF_IMAGE, $name));
250
251                 // Count downloads with attach plugin
252                 $url = $script . '?plugin=attach' . '&amp;refer=' . rawurlencode($page) .
253                         '&amp;openfile=' . rawurlencode($name); // Show its filename at the last
254
255                 if ($is_image) {
256                         // Swap $url
257                         $url2 = $url;
258
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' . '&amp;page=' . rawurlencode($page) .
263                                         '&amp;src=' . rawurlencode($name); // Show its filename at the last
264                         } else {
265                                 // Try direct-access, if possible
266                                 $url = $file;
267                         }
268
269                         $width = $height = 0;
270                         $size = @getimagesize($file);
271                         if (is_array($size)) {
272                                 $width  = $size[0];
273                                 $height = $size[1];
274                         }
275                 } else {
276                         $info = get_date('Y/m/d H:i:s', filemtime($file) - LOCALZONE) .
277                                 ' ' . sprintf('%01.1f', round(filesize($file)/1024, 1)) . 'KB';
278                 }
279         }
280
281         // ³ÈÄ¥¥Ñ¥é¥á¡¼¥¿¤ò¥Á¥§¥Ã¥¯
282         if (! empty($params['_args'])) {
283                 $_title = array();
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];
289
290                         } else if (preg_match('/^([0-9.]+)%$/', $arg, $matches) && $matches[1] > 0) {
291                                 $params['_%'] = $matches[1];
292
293                         } else {
294                                 $_title[] = $arg;
295                         }
296                 }
297
298                 if (! empty($_title)) {
299                         $title = htmlspecialchars(join(',', $_title));
300                         if ($is_image) $title = make_line_rules($title);
301                 }
302         }
303
304         // ²èÁü¥µ¥¤¥ºÄ´À°
305         if ($is_image) {
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);
315                                 if ($zoom) {
316                                         $width  = (int)($width  / $zoom);
317                                         $height = (int)($height / $zoom);
318                                 }
319                         } else {
320                                 $width  = $params['_w'] ? $params['_w'] : $width;
321                                 $height = $params['_h'] ? $params['_h'] : $height;
322                         }
323                 }
324                 if ($params['_%']) {
325                         $width  = (int)($width  * $params['_%'] / 100);
326                         $height = (int)($height * $params['_%'] / 100);
327                 }
328                 if ($width && $height) $info = "width=\"$width\" height=\"$height\" ";
329         }
330
331         // ¥¢¥é¥¤¥ó¥á¥ó¥ÈȽÄê
332         $params['_align'] = PLUGIN_REF_DEFAULT_ALIGN;
333         foreach (array('right', 'left', 'center') as $align) {
334                 if ($params[$align])  {
335                         $params['_align'] = $align;
336                         break;
337                 }
338         }
339
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>";
344         } else {
345                 $icon = $params['noicon'] ? '' : FILE_ICON;
346                 $params['_body'] = "<a href=\"$url\" title=\"$info\">$icon$title</a>";
347         }
348
349         return $params;
350 }
351
352 // ¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
353 function ref_check_arg($val, & $params)
354 {
355         if ($val == '') {
356                 $params['_done'] = TRUE;
357                 return;
358         }
359
360         if (! $params['_done']) {
361                 foreach (array_keys($params) as $key) {
362                         if (strpos($key, strtolower($val)) === 0) {
363                                 $params[$key] = TRUE;
364                                 return;
365                         }
366                 }
367                 $params['_done'] = TRUE;
368         }
369
370         $params['_args'][] = $val;
371 }
372
373 // Output an image (fast, non-logging <==> attach plugin)
374 function plugin_ref_action()
375 {
376         global $vars;
377
378         $usage = 'Usage: plugin=ref&amp;page=page_name&amp;src=attached_image_name';
379
380         if (! isset($vars['page']) || ! isset($vars['src']))
381                 return array('msg'=>'Invalid argument', 'body'=>$usage);
382
383         $page     = $vars['page'];
384         $filename = $vars['src'] ;
385
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);
389
390         $got = @getimagesize($ref);
391         if (! isset($got[2])) $got[2] = FALSE;
392         switch ($got[2]) {
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;
397         default:
398                 return array('msg'=>'Seems not an image', 'body'=>$usage);
399         }
400
401         // Care for Japanese-character-included file name
402         if (LANG == 'ja') {
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');
407                         break;
408                 case 'MSIE/default':
409                         $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
410                         break;
411                 }
412         }
413         $file = htmlspecialchars($filename);
414         $size = filesize($ref);
415
416         // Output
417         pkwk_common_headers();
418         header('Content-Disposition: inline; filename="' . $filename . '"');
419         header('Content-Length: ' . $size);
420         header('Content-Type: '   . $type);
421         @readfile($ref);
422         exit;
423 }
424 ?>