2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: attach.inc.php,v 1.47 2004/08/05 14:16:42 henoheno Exp $
11 changed by Y.MASUI <masui@hisec.co.jp> http://masui.net/pukiwiki/
12 modified by PANDA <panda@arino.jp> http://home.arino.jp/
15 // Max file size for upload on PHP (PHP default: 2MB)
16 ini_set('upload_max_filesize', '2M');
18 // Max file size for upload on script of PukiWikiX_FILESIZE
19 define('MAX_FILESIZE', (1024 * 1024)); // default: 1MB
21 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤ò¥¢¥Ã¥×¥í¡¼¥É¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
22 define('ATTACH_UPLOAD_ADMIN_ONLY', FALSE); // FALSE or TRUE
24 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
25 define('ATTACH_DELETE_ADMIN_ONLY', FALSE); // FALSE or TRUE
27 // ´ÉÍý¼Ô¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¤È¤¤Ï¡¢¥Ð¥Ã¥¯¥¢¥Ã¥×¤òºî¤é¤Ê¤¤
28 // ATTACH_DELETE_ADMIN_ONLY=TRUE¤Î¤È¤Í¸ú
29 define('ATTACH_DELETE_ADMIN_NOBACKUP', FALSE); // FALSE or TRUE
31 // ¥¢¥Ã¥×¥í¡¼¥É/ºï½ü»þ¤Ë¥Ñ¥¹¥ï¡¼¥É¤òÍ׵᤹¤ë(ADMIN_ONLY¤¬Í¥Àè)
32 define('ATTACH_PASSWORD_REQUIRE', FALSE); // FALSE or TRUE
34 // ¥Õ¥¡¥¤¥ë¤Î¥¢¥¯¥»¥¹¸¢
35 define('ATTACH_FILE_MODE', 0644);
36 //define('ATTACH_FILE_MODE', 0604); // for XREA.COM
39 if (! defined('FILE_ICON')) {
40 define('FILE_ICON', '<img src="' . IMAGE_DIR . 'file.png"' .
41 ' width="20" height="20" alt="file"' .
42 ' style="border-width:0px" />');
45 // mime-type¤òµ½Ò¤·¤¿¥Ú¡¼¥¸
46 define('ATTACH_CONFIG_PAGE_MIME', 'plugin/attach/mime-type');
49 function plugin_attach_convert()
53 if (! ini_get('file_uploads'))
54 return 'file_uploads disabled';
56 $nolist = $noform = FALSE;
57 if (func_num_args() > 0) {
58 foreach (func_get_args() as $arg) {
59 $arg = strtolower($arg);
60 $nolist |= ($arg == 'nolist');
61 $noform |= ($arg == 'noform');
67 $obj = &new AttachPages($vars['page']);
68 $ret .= $obj->toString($vars['page'], TRUE);
71 $ret .= attach_form($vars['page']);
78 function plugin_attach_action()
82 // backward compatible
83 if (isset($vars['openfile'])) {
84 $vars['file'] = $vars['openfile'];
85 $vars['pcmd'] = 'open';
87 if (isset($vars['delfile'])) {
88 $vars['file'] = $vars['delfile'];
89 $vars['pcmd'] = 'delete';
92 $pcmd = isset($vars['pcmd']) ? $vars['pcmd'] : '';
93 $refer = isset($vars['refer']) ? $vars['refer'] : '';
94 $pass = isset($vars['pass']) ? $vars['pass'] : NULL;
95 $page = isset($vars['page']) ? $vars['page'] : '';
98 if ($refer != '' and is_pagename($refer)) {
99 $read_cmds = array('info', 'open', 'list');
100 in_array($pcmd, $read_cmds) ?
101 check_readable($refer) :
102 check_editable($refer);
106 if (isset($_FILES['attach_file']))
107 return attach_upload($_FILES['attach_file'], $refer, $pass);
110 case 'info' : return attach_info();
111 case 'delete' : return attach_delete();
112 case 'open' : return attach_open();
113 case 'list' : return attach_list();
114 case 'freeze' : return attach_freeze(TRUE);
115 case 'unfreeze' : return attach_freeze(FALSE);
116 case 'upload' : return attach_showform();
119 if ($page == '' or ! is_page($page)) {
120 return attach_list();
122 return attach_showform();
126 //-------- call from skin
127 function attach_filelist()
129 global $vars, $_attach_messages;
131 $page = isset($vars['page']) ? $vars['page'] : '';
133 $obj = & new AttachPages($page, 0);
135 if (! isset($obj->pages[$page])) {
138 return $_attach_messages['msg_file'] . ': ' .
139 $obj->toString($page, TRUE) . "\n";
144 //¥Õ¥¡¥¤¥ë¥¢¥Ã¥×¥í¡¼¥É
145 function attach_upload($file, $page, $pass = NULL)
147 // $pass=NULL : ¥Ñ¥¹¥ï¡¼¥É¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤
148 // $pass=TRUE : ¥¢¥Ã¥×¥í¡¼¥Éµö²Ä
149 global $_attach_messages;
151 if ($file['tmp_name'] == '' or ! is_uploaded_file($file['tmp_name'])) {
152 return array('result'=>FALSE);
153 } else if ($file['size'] > MAX_FILESIZE) {
154 return array( 'result'=>FALSE,
155 'msg'=>$_attach_messages['err_exceed']);
156 } else if (! is_pagename($page) or ($pass !== TRUE and ! is_editable($page))) {
157 return array( 'result'=>FALSE,'
158 msg'=>$_attach_messages['err_noparm']);
159 } else if (ATTACH_UPLOAD_ADMIN_ONLY and $pass !== TRUE and
160 ($pass === NULL or ! pkwk_login($pass))) {
161 return array( 'result'=>FALSE,
162 'msg'=>$_attach_messages['err_adminpass']);
165 $obj = & new AttachFile($page, $file['name']);
167 return array('result'=>FALSE,
168 'msg'=>$_attach_messages['err_exists']);
171 if (move_uploaded_file($file['tmp_name'], $obj->filename)) {
172 chmod($obj->filename, ATTACH_FILE_MODE);
175 if (is_page($page)) {
176 touch(get_filename($page));
180 $obj->status['pass'] = ($pass !== TRUE and $pass !== NULL) ? $pass : '';
183 return array('result'=>TRUE,
184 'msg'=>$_attach_messages['msg_uploaded']);
188 function attach_info($err = '')
190 global $vars, $_attach_messages;
192 foreach (array('refer', 'file', 'age') as $var) {
193 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
196 $obj = & new AttachFile($refer, $file, $age);
197 return $obj->getstatus() ?
199 array('msg'=>$_attach_messages['err_notfound']);
203 function attach_delete()
205 global $vars, $_attach_messages;
207 foreach (array('refer', 'file', 'age', 'pass') as $var) {
208 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
211 if (is_freeze($refer) or ! is_editable($refer)) {
212 return array('msg'=>$_attach_messages['err_noparm']);
214 $obj = & new AttachFile($refer, $file, $age);
215 return $obj->getstatus() ?
216 $obj->delete($pass) :
217 array('msg'=>$_attach_messages['err_notfound']);
222 function attach_freeze($freeze)
224 global $vars, $_attach_messages;
226 foreach (array('refer', 'file', 'age', 'pass') as $var) {
227 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
230 if (is_freeze($refer) or ! is_editable($refer)) {
231 return array('msg'=>$_attach_messages['err_noparm']);
233 $obj = & new AttachFile($refer, $file, $age);
234 return $obj->getstatus() ?
235 $obj->freeze($freeze, $pass) :
236 array('msg'=>$_attach_messages['err_notfound']);
241 function attach_open()
243 global $vars, $_attach_messages;
245 foreach (array('refer', 'file', 'age') as $var) {
246 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
249 $obj = & new AttachFile($refer, $file, $age);
250 return $obj->getstatus() ?
252 array('msg'=>$_attach_messages['err_notfound']);
256 function attach_list()
258 global $vars, $_attach_messages;
260 $refer = isset($vars['refer']) ? $vars['refer'] : '';
262 $obj = & new AttachPages($refer);
264 $msg = $_attach_messages[($refer == '') ? 'msg_listall' : 'msg_listpage'];
265 $body = ($refer == '' or isset($obj->pages[$refer])) ?
266 $obj->toString($refer, FALSE) :
267 $_attach_messages['err_noexist'];
269 return array('msg'=>$msg, 'body'=>$body);
272 //¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à¤òɽ¼¨
273 function attach_showform()
275 global $vars, $_attach_messages;
277 $page = isset($vars['page']) ? $vars['page'] : '';
279 $vars['refer'] = $page;
280 $body = ini_get('file_uploads') ? attach_form($page) : 'file_uploads disabled.';
282 return array('msg'=>$_attach_messages['msg_upload'], 'body'=>$body);
287 function attach_mime_content_type($filename)
289 $type = 'application/octet-stream'; //default
291 if (! file_exists($filename)) return $type;
293 $size = @getimagesize($filename);
294 if (is_array($size)) {
296 case 1: return 'image/gif';
297 case 2: return 'image/jpeg';
298 case 3: return 'image/png';
299 case 4: return 'application/x-shockwave-flash';
304 if (! preg_match('/_((?:[0-9A-F]{2})+)(?:\.\d+)?$/', $filename, $matches))
307 $filename = decode($matches[1]);
309 // mime-type°ìÍ÷ɽ¤ò¼èÆÀ
310 $config = new Config(ATTACH_CONFIG_PAGE_MIME);
311 $table = $config->read() ? $config->get('mime-type') : array();
312 unset($config); // ¥á¥â¥êÀáÌó
314 foreach ($table as $row) {
315 $_type = trim($row[0]);
316 $exts = preg_split('/\s+|,/', trim($row[1]), -1, PREG_SPLIT_NO_EMPTY);
317 foreach ($exts as $ext) {
318 if (preg_match("/\.$ext$/i", $filename)) return $_type;
325 //¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à
326 function attach_form($page)
328 global $script, $vars, $_attach_messages;
330 $r_page = rawurlencode($page);
331 $s_page = htmlspecialchars($page);
334 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
335 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
339 if (! (bool)ini_get('file_uploads')) return $navi;
341 $maxsize = MAX_FILESIZE;
342 $msg_maxsize = sprintf($_attach_messages['msg_maxsize'], number_format($maxsize/1024) . 'KB');
345 if (ATTACH_PASSWORD_REQUIRE or ATTACH_UPLOAD_ADMIN_ONLY) {
346 $title = $_attach_messages[ATTACH_UPLOAD_ADMIN_ONLY ? 'msg_adminpass' : 'msg_password'];
347 $pass = '<br />' . $title . ': <input type="password" name="pass" size="8" />';
350 <form enctype="multipart/form-data" action="$script" method="post">
352 <input type="hidden" name="plugin" value="attach" />
353 <input type="hidden" name="pcmd" value="post" />
354 <input type="hidden" name="refer" value="$s_page" />
355 <input type="hidden" name="max_file_size" value="$maxsize" />
360 {$_attach_messages['msg_file']}: <input type="file" name="attach_file" />
362 <input type="submit" value="{$_attach_messages['btn_upload']}" />
372 var $page, $file, $age, $basename, $filename, $logname;
377 var $status = array('count'=>array(0), 'age'=>'', 'pass'=>'', 'freeze'=>FALSE);
379 function AttachFile($page, $file, $age = 0)
382 $this->file = basename($file);
383 $this->age = is_numeric($age) ? $age : 0;
385 $this->basename = UPLOAD_DIR . encode($page) . '_' . encode($this->file);
386 $this->filename = $this->basename . ($age ? '.' . $age : '');
387 $this->logname = $this->basename . '.log';
388 $this->exist = file_exists($this->filename);
389 $this->time = $this->exist ? filemtime($this->filename) - LOCALZONE : 0;
390 $this->md5hash = $this->exist ? md5_file($this->filename) : '';
396 if (! $this->exist) return FALSE;
399 if (file_exists($this->logname)) {
400 $data = file($this->logname);
401 foreach ($this->status as $key=>$value) {
402 $this->status[$key] = chop(array_shift($data));
404 $this->status['count'] = explode(',', $this->status['count']);
406 $this->time_str = get_date('Y/m/d H:i:s', $this->time);
407 $this->size = filesize($this->filename);
408 $this->size_str = sprintf('%01.1f', round($this->size/1024, 1)) . 'KB';
409 $this->type = attach_mime_content_type($this->filename);
417 $this->status['count'] = join(',', $this->status['count']);
418 $fp = fopen($this->logname, 'wb') or
419 die_message('cannot write ' . $this->logname);
420 set_file_buffer($fp, 0);
423 foreach ($this->status as $key=>$value) {
424 fwrite($fp, $value . "\n");
431 function datecomp($a, $b) {
432 return ($a->time == $b->time) ? 0 : (($a->time > $b->time) ? -1 : 1);
435 function toString($showicon, $showinfo)
437 global $script, $_attach_messages;
440 $param = '&file=' . rawurlencode($this->file) . '&refer=' . rawurlencode($this->page) .
441 ($this->age ? '&age=' . $this->age : '');
442 $title = $this->time_str . ' ' . $this->size_str;
443 $label = ($showicon ? FILE_ICON : '') . htmlspecialchars($this->file);
445 $label .= ' (backup No.' . $this->age . ')';
449 $_title = str_replace('$1', rawurlencode($this->file), $_attach_messages['msg_info']);
450 $info = "\n<span class=\"small\">[<a href=\"$script?plugin=attach&pcmd=info$param\" title=\"$_title\">{$_attach_messages['btn_info']}</a>]</span>";
451 $count = ($showicon and ! empty($this->status['count'][$this->age])) ?
452 sprintf($_attach_messages['msg_count'], $this->status['count'][$this->age]) : '';
454 return "<a href=\"$script?plugin=attach&pcmd=open$param\" title=\"$title\">$label</a>$count$info";
460 global $script, $_attach_messages;
462 $r_page = rawurlencode($this->page);
463 $s_page = htmlspecialchars($this->page);
464 $s_file = htmlspecialchars($this->file);
465 $s_err = ($err == '') ? '' : '<p style="font-weight:bold">' . $_attach_messages[$err] . '</p>';
469 $msg_delete = '<input type="radio" name="pcmd" value="delete" />' .
470 $_attach_messages['msg_delete'] .
471 $_attach_messages['msg_require'] . '<br />';
474 if ($this->status['freeze']) {
475 $msg_freezed = "<dd>{$_attach_messages['msg_isfreeze']}</dd>";
477 $msg_freeze = '<input type="radio" name="pcmd" value="unfreeze" />' .
478 $_attach_messages['msg_unfreeze'] .
479 $_attach_messages['msg_require'] . '<br />';
482 $msg_delete = '<input type="radio" name="pcmd" value="delete" />' .
483 $_attach_messages['msg_delete'];
484 if (ATTACH_DELETE_ADMIN_ONLY or $this->age) {
485 $msg_delete .= $_attach_messages['msg_require'];
487 $msg_delete .= '<br />';
488 $msg_freeze = '<input type="radio" name="pcmd" value="freeze" />' .
489 $_attach_messages['msg_freeze'] .
490 $_attach_messages['msg_require'] . '<br />';
493 $info = $this->toString(TRUE, FALSE);
495 $retval = array('msg'=>sprintf($_attach_messages['msg_info'], htmlspecialchars($this->file)));
496 $retval['body'] = <<< EOD
498 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
499 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
503 <dd>{$_attach_messages['msg_page']}:$s_page</dd>
504 <dd>{$_attach_messages['msg_filename']}:{$this->filename}</dd>
505 <dd>{$_attach_messages['msg_md5hash']}:{$this->md5hash}</dd>
506 <dd>{$_attach_messages['msg_filesize']}:{$this->size_str} ({$this->size} bytes)</dd>
507 <dd>Content-type:{$this->type}</dd>
508 <dd>{$_attach_messages['msg_date']}:{$this->time_str}</dd>
509 <dd>{$_attach_messages['msg_dlcount']}:{$this->status['count'][$this->age]}</dd>
514 <form action="$script" method="post">
516 <input type="hidden" name="plugin" value="attach" />
517 <input type="hidden" name="refer" value="$s_page" />
518 <input type="hidden" name="file" value="$s_file" />
519 <input type="hidden" name="age" value="{$this->age}" />
522 {$_attach_messages['msg_password']}: <input type="password" name="pass" size="8" />
523 <input type="submit" value="{$_attach_messages['btn_submit']}" />
530 function delete($pass)
532 global $_attach_messages;
534 if ($this->status['freeze']) return attach_info('msg_isfreeze');
536 if (! pkwk_login($pass)) {
537 if (ATTACH_DELETE_ADMIN_ONLY or $this->age) {
538 return attach_info('err_adminpass');
539 } else if (ATTACH_PASSWORD_REQUIRE and
540 md5($pass) != $this->status['pass']) {
541 return attach_info('err_password');
547 (ATTACH_DELETE_ADMIN_ONLY and ATTACH_DELETE_ADMIN_NOBACKUP)) {
548 @unlink($this->filename);
551 $age = ++$this->status['age'];
552 } while (file_exists($this->basename . '.' . $age));
554 if (! rename($this->basename,$this->basename . '.' . $age)) {
556 return array('msg'=>$_attach_messages['err_delete']);
559 $this->status['count'][$age] = $this->status['count'][0];
560 $this->status['count'][0] = 0;
564 if (is_page($this->page)) {
565 touch(get_filename($this->page));
568 return array('msg'=>$_attach_messages['msg_deleted']);
571 function freeze($freeze, $pass)
573 global $_attach_messages;
575 if (! pkwk_login($pass)) return attach_info('err_adminpass');
578 $this->status['freeze'] = $freeze;
581 return array('msg'=>$_attach_messages[$freeze ? 'msg_freezed' : 'msg_unfreezed']);
587 $this->status['count'][$this->age]++;
590 // for Japanese (???)
591 $filename = htmlspecialchars(mb_convert_encoding($this->file,'SJIS','auto'));
593 ini_set('default_charset', '');
594 mb_http_output('pass');
596 header('Content-Disposition: inline; filename="' . $filename . '"');
597 header('Content-Length: ' . $this->size);
598 header('Content-Type: ' . $this->type);
599 @readfile($this->filename);
608 var $files = array();
610 function AttachFiles($page)
615 function add($file, $age)
617 $this->files[$file][$age] = & new AttachFile($this->page, $file, $age);
620 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ
621 function toString($flat)
623 global $_title_cannotread;
625 if (! check_readable($this->page, FALSE, FALSE)) {
626 return str_replace('$1', make_pagelink($this->page), $_title_cannotread);
628 return $this->to_flat();
632 $files = array_keys($this->files);
635 foreach ($files as $file) {
637 foreach (array_keys($this->files[$file]) as $age) {
638 $_files[$age] = $this->files[$file][$age]->toString(FALSE, TRUE);
640 if (! isset($_files[0])) {
641 $_files[0] = htmlspecialchars($file);
646 $ret .= " <li>$_file\n";
647 if (count($_files)) {
648 $ret .= "<ul>\n<li>" . join("</li>\n<li>", $_files) . "</li>\n</ul>\n";
652 return make_pagelink($this->page) . "\n<ul>\n$ret</ul>\n";
655 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ(inline)
660 foreach (array_keys($this->files) as $file) {
661 if (isset($this->files[$file][0])) {
662 $files[$file] = & $this->files[$file][0];
665 uasort($files, array('AttachFile', 'datecomp'));
666 foreach (array_keys($files) as $file) {
667 $ret .= $files[$file]->toString(TRUE, TRUE) . ' ';
677 var $pages = array();
679 function AttachPages($page = '', $age = NULL)
682 $dir = opendir(UPLOAD_DIR) or
683 die('directory ' . UPLOAD_DIR . ' is not exist or not readable.');
685 $page_pattern = ($page == '') ? '(?:[0-9A-F]{2})+' : preg_quote(encode($page), '/');
686 $age_pattern = ($age === NULL) ?
687 '(?:\.([0-9]+))?' : ($age ? "\.($age)" : '');
688 $pattern = "/^({$page_pattern})_((?:[0-9A-F]{2})+){$age_pattern}$/";
691 while ($file = readdir($dir)) {
692 if (! preg_match($pattern, $file, $matches))
695 $_page = decode($matches[1]);
696 $_file = decode($matches[2]);
697 $_age = $matches[3] ? $matches[3] : 0;
698 if (! isset($this->pages[$_page])) {
699 $this->pages[$_page] = & new AttachFiles($_page);
701 $this->pages[$_page]->add($_file, $_age);
706 function toString($page = '', $flat = FALSE)
711 if (! isset($this->pages[$page])) {
714 return $this->pages[$page]->toString($flat);
719 $pages = array_keys($this->pages);
722 foreach ($pages as $page) {
723 if (preg_match("/$non_list/", $page)) continue;
724 $ret .= '<li>' . $this->pages[$page]->toString($flat) . "</li>\n";
726 return "\n<ul>\n" . $ret . "</ul>\n";