2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: attach.inc.php,v 1.69 2005/01/09 08:12:21 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('PLUGIN_ATTACH_MAX_FILESIZE', (1024 * 1024)); // default: 1MB
21 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤ò¥¢¥Ã¥×¥í¡¼¥É¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
22 define('PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY', FALSE); // FALSE or TRUE
24 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
25 define('PLUGIN_ATTACH_DELETE_ADMIN_ONLY', FALSE); // FALSE or TRUE
27 // ´ÉÍý¼Ô¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¤È¤¤Ï¡¢¥Ð¥Ã¥¯¥¢¥Ã¥×¤òºî¤é¤Ê¤¤
28 // PLUGIN_ATTACH_DELETE_ADMIN_ONLY=TRUE¤Î¤È¤Í¸ú
29 define('PLUGIN_ATTACH_DELETE_ADMIN_NOBACKUP', FALSE); // FALSE or TRUE
31 // ¥¢¥Ã¥×¥í¡¼¥É/ºï½ü»þ¤Ë¥Ñ¥¹¥ï¡¼¥É¤òÍ׵᤹¤ë(ADMIN_ONLY¤¬Í¥Àè)
32 define('PLUGIN_ATTACH_PASSWORD_REQUIRE', FALSE); // FALSE or TRUE
34 // ¥Õ¥¡¥¤¥ë¤Î¥¢¥¯¥»¥¹¸¢
35 define('PLUGIN_ATTACH_FILE_MODE', 0644);
36 //define('PLUGIN_ATTACH_FILE_MODE', 0604); // for XREA.COM
39 define('PLUGIN_ATTACH_FILE_ICON', '<img src="' . IMAGE_DIR . 'file.png"' .
40 ' width="20" height="20" alt="file"' .
41 ' style="border-width:0px" />');
43 // mime-type¤òµ½Ò¤·¤¿¥Ú¡¼¥¸
44 define('PLUGIN_ATTACH_CONFIG_PAGE_MIME', 'plugin/attach/mime-type');
47 function plugin_attach_convert()
51 $page = isset($vars['page']) ? $vars['page'] : '';
53 $nolist = $noform = FALSE;
54 if (func_num_args() > 0) {
55 foreach (func_get_args() as $arg) {
56 $arg = strtolower($arg);
57 $nolist |= ($arg == 'nolist');
58 $noform |= ($arg == 'noform');
64 $obj = & new AttachPages($page);
65 $ret .= $obj->toString($page, TRUE);
68 $ret .= attach_form($page);
75 function plugin_attach_action()
77 global $vars, $_attach_messages;
79 // Backward compatible
80 if (isset($vars['openfile'])) {
81 $vars['file'] = $vars['openfile'];
82 $vars['pcmd'] = 'open';
84 if (isset($vars['delfile'])) {
85 $vars['file'] = $vars['delfile'];
86 $vars['pcmd'] = 'delete';
89 $pcmd = isset($vars['pcmd']) ? $vars['pcmd'] : '';
90 $refer = isset($vars['refer']) ? $vars['refer'] : '';
91 $pass = isset($vars['pass']) ? $vars['pass'] : NULL;
92 $page = isset($vars['page']) ? $vars['page'] : '';
94 if ($refer != '' && is_pagename($refer)) {
95 if(in_array($pcmd, array('info', 'open', 'list'))) {
96 check_readable($refer);
98 check_editable($refer);
103 if (isset($_FILES['attach_file'])) {
105 return attach_upload($_FILES['attach_file'], $refer, $pass);
108 case 'info' : return attach_info();
109 case 'delete' : return attach_delete();
110 case 'open' : return attach_open();
111 case 'list' : return attach_list();
112 case 'freeze' : return attach_freeze(TRUE);
113 case 'unfreeze' : return attach_freeze(FALSE);
114 case 'upload' : return attach_showform();
116 if ($page == '' || ! is_page($page)) {
117 return attach_list();
119 return attach_showform();
123 //-------- call from skin
124 function attach_filelist()
126 global $vars, $_attach_messages;
128 $page = isset($vars['page']) ? $vars['page'] : '';
130 $obj = & new AttachPages($page, 0);
132 if (! isset($obj->pages[$page])) {
135 return $_attach_messages['msg_file'] . ': ' .
136 $obj->toString($page, TRUE) . "\n";
141 // ¥Õ¥¡¥¤¥ë¥¢¥Ã¥×¥í¡¼¥É
142 // $pass = NULL : ¥Ñ¥¹¥ï¡¼¥É¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤
143 // $pass = TRUE : ¥¢¥Ã¥×¥í¡¼¥Éµö²Ä
144 function attach_upload($file, $page, $pass = NULL)
146 global $_attach_messages;
148 if (! is_page($page)) {
149 die_message("No such page");
150 } else if ($file['tmp_name'] == '' || ! is_uploaded_file($file['tmp_name'])) {
151 return array('result'=>FALSE);
152 } else if ($file['size'] > PLUGIN_ATTACH_MAX_FILESIZE) {
155 'msg'=>$_attach_messages['err_exceed']);
156 } else if (! is_pagename($page) || ($pass !== TRUE && ! is_editable($page))) {
159 msg'=>$_attach_messages['err_noparm']);
160 } else if (PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY && $pass !== TRUE &&
161 ($pass === NULL || ! pkwk_login($pass))) {
164 'msg'=>$_attach_messages['err_adminpass']);
167 $obj = & new AttachFile($page, $file['name']);
169 return array('result'=>FALSE,
170 'msg'=>$_attach_messages['err_exists']);
173 if (move_uploaded_file($file['tmp_name'], $obj->filename)) {
174 chmod($obj->filename, PLUGIN_ATTACH_FILE_MODE);
177 if (is_page($page)) {
178 touch(get_filename($page));
182 $obj->status['pass'] = ($pass !== TRUE && $pass !== NULL) ? md5($pass) : '';
187 'msg'=>$_attach_messages['msg_uploaded']);
190 // ¾ÜºÙ¥Õ¥©¡¼¥à¤òɽ¼¨
191 function attach_info($err = '')
193 global $vars, $_attach_messages;
195 foreach (array('refer', 'file', 'age') as $var) {
196 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
199 $obj = & new AttachFile($refer, $file, $age);
200 return $obj->getstatus() ?
202 array('msg'=>$_attach_messages['err_notfound']);
206 function attach_delete()
208 global $vars, $_attach_messages;
210 foreach (array('refer', 'file', 'age', 'pass') as $var) {
211 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
214 if (is_freeze($refer) || ! is_editable($refer)) {
215 return array('msg'=>$_attach_messages['err_noparm']);
217 $obj = & new AttachFile($refer, $file, $age);
218 return $obj->getstatus() ?
219 $obj->delete($pass) :
220 array('msg'=>$_attach_messages['err_notfound']);
225 function attach_freeze($freeze)
227 global $vars, $_attach_messages;
229 foreach (array('refer', 'file', 'age', 'pass') as $var) {
230 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
233 if (is_freeze($refer) || ! is_editable($refer)) {
234 return array('msg'=>$_attach_messages['err_noparm']);
236 $obj = & new AttachFile($refer, $file, $age);
237 return $obj->getstatus() ?
238 $obj->freeze($freeze, $pass) :
239 array('msg'=>$_attach_messages['err_notfound']);
244 function attach_open()
246 global $vars, $_attach_messages;
248 foreach (array('refer', 'file', 'age') as $var) {
249 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
252 $obj = & new AttachFile($refer, $file, $age);
253 return $obj->getstatus() ?
255 array('msg'=>$_attach_messages['err_notfound']);
259 function attach_list()
261 global $vars, $_attach_messages;
263 $refer = isset($vars['refer']) ? $vars['refer'] : '';
265 $obj = & new AttachPages($refer);
267 $msg = $_attach_messages[($refer == '') ? 'msg_listall' : 'msg_listpage'];
268 $body = ($refer == '' || isset($obj->pages[$refer])) ?
269 $obj->toString($refer, FALSE) :
270 $_attach_messages['err_noexist'];
272 return array('msg'=>$msg, 'body'=>$body);
275 // ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à¤òɽ¼¨ (action»þ)
276 function attach_showform()
278 global $vars, $_attach_messages;
280 $page = isset($vars['page']) ? $vars['page'] : '';
281 $vars['refer'] = $page;
282 $body = attach_form($page);
284 return array('msg'=>$_attach_messages['msg_upload'], 'body'=>$body);
289 function attach_mime_content_type($filename)
291 $type = 'application/octet-stream'; // default
293 if (! file_exists($filename)) return $type;
295 $size = @getimagesize($filename);
296 if (is_array($size)) {
298 case 1: return 'image/gif';
299 case 2: return 'image/jpeg';
300 case 3: return 'image/png';
301 case 4: return 'application/x-shockwave-flash';
306 if (! preg_match('/_((?:[0-9A-F]{2})+)(?:\.\d+)?$/', $filename, $matches))
309 $filename = decode($matches[1]);
311 // mime-type°ìÍ÷ɽ¤ò¼èÆÀ
312 $config = new Config(PLUGIN_ATTACH_CONFIG_PAGE_MIME);
313 $table = $config->read() ? $config->get('mime-type') : array();
314 unset($config); // ¥á¥â¥êÀáÌó
316 foreach ($table as $row) {
317 $_type = trim($row[0]);
318 $exts = preg_split('/\s+|,/', trim($row[1]), -1, PREG_SPLIT_NO_EMPTY);
319 foreach ($exts as $ext) {
320 if (preg_match("/\.$ext$/i", $filename)) return $_type;
327 // ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à¤Î½ÐÎÏ
328 function attach_form($page)
330 global $script, $vars, $_attach_messages;
332 $r_page = rawurlencode($page);
333 $s_page = htmlspecialchars($page);
336 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
337 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
341 if (! ini_get('file_uploads')) return '#attach(): file_uploads disabled<br />' . $navi;
342 if (! is_page($page)) return '#attach(): No such page<br />' . $navi;
344 $maxsize = PLUGIN_ATTACH_MAX_FILESIZE;
345 $msg_maxsize = sprintf($_attach_messages['msg_maxsize'], number_format($maxsize/1024) . 'KB');
348 if (PLUGIN_ATTACH_PASSWORD_REQUIRE || PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY) {
349 $title = $_attach_messages[PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY ? 'msg_adminpass' : 'msg_password'];
350 $pass = '<br />' . $title . ': <input type="password" name="pass" size="8" />';
353 <form enctype="multipart/form-data" action="$script" method="post">
355 <input type="hidden" name="plugin" value="attach" />
356 <input type="hidden" name="pcmd" value="post" />
357 <input type="hidden" name="refer" value="$s_page" />
358 <input type="hidden" name="max_file_size" value="$maxsize" />
363 {$_attach_messages['msg_file']}: <input type="file" name="attach_file" />
365 <input type="submit" value="{$_attach_messages['btn_upload']}" />
375 var $page, $file, $age, $basename, $filename, $logname;
380 var $status = array('count'=>array(0), 'age'=>'', 'pass'=>'', 'freeze'=>FALSE);
382 function AttachFile($page, $file, $age = 0)
385 $this->file = preg_replace('#^.*/#','',$file);
386 $this->age = is_numeric($age) ? $age : 0;
388 $this->basename = UPLOAD_DIR . encode($page) . '_' . encode($this->file);
389 $this->filename = $this->basename . ($age ? '.' . $age : '');
390 $this->logname = $this->basename . '.log';
391 $this->exist = file_exists($this->filename);
392 $this->time = $this->exist ? filemtime($this->filename) - LOCALZONE : 0;
393 $this->md5hash = $this->exist ? md5_file($this->filename) : '';
399 if (! $this->exist) return FALSE;
402 if (file_exists($this->logname)) {
403 $data = file($this->logname);
404 foreach ($this->status as $key=>$value) {
405 $this->status[$key] = chop(array_shift($data));
407 $this->status['count'] = explode(',', $this->status['count']);
409 $this->time_str = get_date('Y/m/d H:i:s', $this->time);
410 $this->size = filesize($this->filename);
411 $this->size_str = sprintf('%01.1f', round($this->size/1024, 1)) . 'KB';
412 $this->type = attach_mime_content_type($this->filename);
420 $this->status['count'] = join(',', $this->status['count']);
421 $fp = fopen($this->logname, 'wb') or
422 die_message('cannot write ' . $this->logname);
423 set_file_buffer($fp, 0);
426 foreach ($this->status as $key=>$value) {
427 fwrite($fp, $value . "\n");
434 function datecomp($a, $b) {
435 return ($a->time == $b->time) ? 0 : (($a->time > $b->time) ? -1 : 1);
438 function toString($showicon, $showinfo)
440 global $script, $_attach_messages;
443 $param = '&file=' . rawurlencode($this->file) . '&refer=' . rawurlencode($this->page) .
444 ($this->age ? '&age=' . $this->age : '');
445 $title = $this->time_str . ' ' . $this->size_str;
446 $label = ($showicon ? PLUGIN_ATTACH_FILE_ICON : '') . htmlspecialchars($this->file);
448 $label .= ' (backup No.' . $this->age . ')';
452 $_title = str_replace('$1', rawurlencode($this->file), $_attach_messages['msg_info']);
453 $info = "\n<span class=\"small\">[<a href=\"$script?plugin=attach&pcmd=info$param\" title=\"$_title\">{$_attach_messages['btn_info']}</a>]</span>\n";
454 $count = ($showicon && ! empty($this->status['count'][$this->age])) ?
455 sprintf($_attach_messages['msg_count'], $this->status['count'][$this->age]) : '';
457 return "<a href=\"$script?plugin=attach&pcmd=open$param\" title=\"$title\">$label</a>$count$info";
463 global $script, $_attach_messages;
465 $r_page = rawurlencode($this->page);
466 $s_page = htmlspecialchars($this->page);
467 $s_file = htmlspecialchars($this->file);
468 $s_err = ($err == '') ? '' : '<p style="font-weight:bold">' . $_attach_messages[$err] . '</p>';
472 $msg_delete = '<input type="radio" name="pcmd" value="delete" />' .
473 $_attach_messages['msg_delete'] .
474 $_attach_messages['msg_require'] . '<br />';
477 if ($this->status['freeze']) {
478 $msg_freezed = "<dd>{$_attach_messages['msg_isfreeze']}</dd>";
480 $msg_freeze = '<input type="radio" name="pcmd" value="unfreeze" />' .
481 $_attach_messages['msg_unfreeze'] .
482 $_attach_messages['msg_require'] . '<br />';
485 $msg_delete = '<input type="radio" name="pcmd" value="delete" />' .
486 $_attach_messages['msg_delete'];
487 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
488 $msg_delete .= $_attach_messages['msg_require'];
490 $msg_delete .= '<br />';
491 $msg_freeze = '<input type="radio" name="pcmd" value="freeze" />' .
492 $_attach_messages['msg_freeze'] .
493 $_attach_messages['msg_require'] . '<br />';
496 $info = $this->toString(TRUE, FALSE);
498 $retval = array('msg'=>sprintf($_attach_messages['msg_info'], htmlspecialchars($this->file)));
499 $retval['body'] = <<< EOD
501 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
502 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
506 <dd>{$_attach_messages['msg_page']}:$s_page</dd>
507 <dd>{$_attach_messages['msg_filename']}:{$this->filename}</dd>
508 <dd>{$_attach_messages['msg_md5hash']}:{$this->md5hash}</dd>
509 <dd>{$_attach_messages['msg_filesize']}:{$this->size_str} ({$this->size} bytes)</dd>
510 <dd>Content-type:{$this->type}</dd>
511 <dd>{$_attach_messages['msg_date']}:{$this->time_str}</dd>
512 <dd>{$_attach_messages['msg_dlcount']}:{$this->status['count'][$this->age]}</dd>
517 <form action="$script" method="post">
519 <input type="hidden" name="plugin" value="attach" />
520 <input type="hidden" name="refer" value="$s_page" />
521 <input type="hidden" name="file" value="$s_file" />
522 <input type="hidden" name="age" value="{$this->age}" />
525 {$_attach_messages['msg_password']}: <input type="password" name="pass" size="8" />
526 <input type="submit" value="{$_attach_messages['btn_submit']}" />
533 function delete($pass)
535 global $_attach_messages;
537 if ($this->status['freeze']) return attach_info('msg_isfreeze');
539 if (! pkwk_login($pass)) {
540 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
541 return attach_info('err_adminpass');
542 } else if (PLUGIN_ATTACH_PASSWORD_REQUIRE &&
543 md5($pass) != $this->status['pass']) {
544 return attach_info('err_password');
550 (PLUGIN_ATTACH_DELETE_ADMIN_ONLY && PLUGIN_ATTACH_DELETE_ADMIN_NOBACKUP)) {
551 @unlink($this->filename);
554 $age = ++$this->status['age'];
555 } while (file_exists($this->basename . '.' . $age));
557 if (! rename($this->basename,$this->basename . '.' . $age)) {
559 return array('msg'=>$_attach_messages['err_delete']);
562 $this->status['count'][$age] = $this->status['count'][0];
563 $this->status['count'][0] = 0;
567 if (is_page($this->page)) {
568 touch(get_filename($this->page));
571 return array('msg'=>$_attach_messages['msg_deleted']);
574 function freeze($freeze, $pass)
576 global $_attach_messages;
578 if (! pkwk_login($pass)) return attach_info('err_adminpass');
581 $this->status['freeze'] = $freeze;
584 return array('msg'=>$_attach_messages[$freeze ? 'msg_freezed' : 'msg_unfreezed']);
590 $this->status['count'][$this->age]++;
592 $filename = $this->file;
594 // Care for Japanese-character-included file name
596 switch(UA_NAME . '/' . UA_PROFILE){
597 case 'Opera/default':
598 // Care for using _auto-encode-detecting_ function
599 $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
602 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
606 $filename = htmlspecialchars($filename);
608 ini_set('default_charset', '');
609 mb_http_output('pass');
611 pkwk_common_headers();
612 header('Content-Disposition: inline; filename="' . $filename . '"');
613 header('Content-Length: ' . $this->size);
614 header('Content-Type: ' . $this->type);
616 @readfile($this->filename);
625 var $files = array();
627 function AttachFiles($page)
632 function add($file, $age)
634 $this->files[$file][$age] = & new AttachFile($this->page, $file, $age);
637 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ
638 function toString($flat)
640 global $_title_cannotread;
642 if (! check_readable($this->page, FALSE, FALSE)) {
643 return str_replace('$1', make_pagelink($this->page), $_title_cannotread);
645 return $this->to_flat();
649 $files = array_keys($this->files);
652 foreach ($files as $file) {
654 foreach (array_keys($this->files[$file]) as $age) {
655 $_files[$age] = $this->files[$file][$age]->toString(FALSE, TRUE);
657 if (! isset($_files[0])) {
658 $_files[0] = htmlspecialchars($file);
663 $ret .= " <li>$_file\n";
664 if (count($_files)) {
665 $ret .= "<ul>\n<li>" . join("</li>\n<li>", $_files) . "</li>\n</ul>\n";
669 return make_pagelink($this->page) . "\n<ul>\n$ret</ul>\n";
672 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ(inline)
677 foreach (array_keys($this->files) as $file) {
678 if (isset($this->files[$file][0])) {
679 $files[$file] = & $this->files[$file][0];
682 uasort($files, array('AttachFile', 'datecomp'));
683 foreach (array_keys($files) as $file) {
684 $ret .= $files[$file]->toString(TRUE, TRUE) . ' ';
694 var $pages = array();
696 function AttachPages($page = '', $age = NULL)
699 $dir = opendir(UPLOAD_DIR) or
700 die('directory ' . UPLOAD_DIR . ' is not exist or not readable.');
702 $page_pattern = ($page == '') ? '(?:[0-9A-F]{2})+' : preg_quote(encode($page), '/');
703 $age_pattern = ($age === NULL) ?
704 '(?:\.([0-9]+))?' : ($age ? "\.($age)" : '');
705 $pattern = "/^({$page_pattern})_((?:[0-9A-F]{2})+){$age_pattern}$/";
708 while ($file = readdir($dir)) {
709 if (! preg_match($pattern, $file, $matches))
712 $_page = decode($matches[1]);
713 $_file = decode($matches[2]);
714 $_age = isset($matches[3]) ? $matches[3] : 0;
715 if (! isset($this->pages[$_page])) {
716 $this->pages[$_page] = & new AttachFiles($_page);
718 $this->pages[$_page]->add($_file, $_age);
723 function toString($page = '', $flat = FALSE)
728 if (! isset($this->pages[$page])) {
731 return $this->pages[$page]->toString($flat);
736 $pages = array_keys($this->pages);
739 $non_list_pattern = '/' . $non_list . '/';
740 foreach ($pages as $page) {
741 if (preg_match($non_list_pattern, $page)) continue;
742 $ret .= '<li>' . $this->pages[$page]->toString($flat) . "</li>\n";
744 return "\n" . '<ul>' . "\n" . $ret . '</ul>' . "\n";