OSDN Git Service

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