2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: attach.inc.php,v 1.92 2011/01/25 15:01:01 henoheno Exp $
5 // 2003-2006 PukiWiki Developers Team
6 // 2002-2003 PANDA <panda@arino.jp> http://home.arino.jp/
7 // 2002 Y.MASUI <masui@hisec.co.jp> http://masui.net/pukiwiki/
8 // 2001-2002 Originally written by yu-ji
9 // License: GPL v2 or (at your option) any later version
13 // NOTE (PHP > 4.2.3):
14 // This feature is disabled at newer version of PHP.
15 // Set this at php.ini if you want.
16 // Max file size for upload on PHP (PHP default: 2MB)
17 ini_set('upload_max_filesize', '2M');
19 // Max file size for upload on script of PukiWikiX_FILESIZE
20 define('PLUGIN_ATTACH_MAX_FILESIZE', (1024 * 1024)); // default: 1MB
22 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤ò¥¢¥Ã¥×¥í¡¼¥É¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
23 define('PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY', TRUE); // FALSE or TRUE
25 // ´ÉÍý¼Ô¤À¤±¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
26 define('PLUGIN_ATTACH_DELETE_ADMIN_ONLY', TRUE); // FALSE or TRUE
28 // ´ÉÍý¼Ô¤¬ÅºÉÕ¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¤È¤¤Ï¡¢¥Ð¥Ã¥¯¥¢¥Ã¥×¤òºî¤é¤Ê¤¤
29 // PLUGIN_ATTACH_DELETE_ADMIN_ONLY=TRUE¤Î¤È¤Í¸ú
30 define('PLUGIN_ATTACH_DELETE_ADMIN_NOBACKUP', TRUE); // FALSE or TRUE
32 // ¥¢¥Ã¥×¥í¡¼¥É/ºï½ü»þ¤Ë¥Ñ¥¹¥ï¡¼¥É¤òÍ׵᤹¤ë(ADMIN_ONLY¤¬Í¥Àè)
33 define('PLUGIN_ATTACH_PASSWORD_REQUIRE', FALSE); // FALSE or TRUE
35 // źÉÕ¥Õ¥¡¥¤¥ë̾¤òÊѹ¹¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë
36 define('PLUGIN_ATTACH_RENAME_ENABLE', TRUE); // FALSE or TRUE
38 // ¥Õ¥¡¥¤¥ë¤Î¥¢¥¯¥»¥¹¸¢
39 define('PLUGIN_ATTACH_FILE_MODE', 0644);
40 //define('PLUGIN_ATTACH_FILE_MODE', 0604); // for XREA.COM
43 define('PLUGIN_ATTACH_FILE_ICON', '<img src="' . IMAGE_DIR . 'file.png"' .
44 ' width="20" height="20" alt="file"' .
45 ' style="border-width:0px" />');
47 // mime-type¤òµ½Ò¤·¤¿¥Ú¡¼¥¸
48 define('PLUGIN_ATTACH_CONFIG_PAGE_MIME', 'plugin/attach/mime-type');
51 function plugin_attach_convert()
55 $page = isset($vars['page']) ? $vars['page'] : '';
57 $nolist = $noform = FALSE;
58 if (func_num_args() > 0) {
59 foreach (func_get_args() as $arg) {
60 $arg = strtolower($arg);
61 $nolist |= ($arg == 'nolist');
62 $noform |= ($arg == 'noform');
68 $obj = & new AttachPages($page);
69 $ret .= $obj->toString($page, TRUE);
72 $ret .= attach_form($page);
79 function plugin_attach_action()
81 global $vars, $_attach_messages;
83 // Backward compatible
84 if (isset($vars['openfile'])) {
85 $vars['file'] = $vars['openfile'];
86 $vars['pcmd'] = 'open';
88 if (isset($vars['delfile'])) {
89 $vars['file'] = $vars['delfile'];
90 $vars['pcmd'] = 'delete';
93 $pcmd = isset($vars['pcmd']) ? $vars['pcmd'] : '';
94 $refer = isset($vars['refer']) ? $vars['refer'] : '';
95 $pass = isset($vars['pass']) ? $vars['pass'] : NULL;
96 $page = isset($vars['page']) ? $vars['page'] : '';
98 if ($refer != '' && is_pagename($refer)) {
99 if(in_array($pcmd, array('info', 'open', 'list'))) {
100 check_readable($refer);
102 check_editable($refer);
107 if (isset($_FILES['attach_file'])) {
109 return attach_upload($_FILES['attach_file'], $refer, $pass);
112 case 'delete': /*FALLTHROUGH*/
115 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
118 case 'info' : return attach_info();
119 case 'delete' : return attach_delete();
120 case 'open' : return attach_open();
121 case 'list' : return attach_list();
122 case 'freeze' : return attach_freeze(TRUE);
123 case 'unfreeze' : return attach_freeze(FALSE);
124 case 'rename' : return attach_rename();
125 case 'upload' : return attach_showform();
127 if ($page == '' || ! is_page($page)) {
128 return attach_list();
130 return attach_showform();
135 //-------- call from skin
136 function attach_filelist()
138 global $vars, $_attach_messages;
140 $page = isset($vars['page']) ? $vars['page'] : '';
142 $obj = & new AttachPages($page, 0);
144 if (! isset($obj->pages[$page])) {
147 return $_attach_messages['msg_file'] . ': ' .
148 $obj->toString($page, TRUE) . "\n";
153 // ¥Õ¥¡¥¤¥ë¥¢¥Ã¥×¥í¡¼¥É
154 // $pass = NULL : ¥Ñ¥¹¥ï¡¼¥É¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤
155 // $pass = TRUE : ¥¢¥Ã¥×¥í¡¼¥Éµö²Ä
156 function attach_upload($file, $page, $pass = NULL)
158 global $_attach_messages, $notify, $notify_subject;
160 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
162 // Check query-string
163 $query = 'plugin=attach&pcmd=info&refer=' . rawurlencode($page) .
164 '&file=' . rawurlencode($file['name']);
166 if (PKWK_QUERY_STRING_MAX && strlen($query) > PKWK_QUERY_STRING_MAX) {
167 pkwk_common_headers();
168 echo('Query string (page name and/or file name) too long');
170 } else if (! is_page($page)) {
171 die_message('No such page');
172 } else if ($file['tmp_name'] == '' || ! is_uploaded_file($file['tmp_name'])) {
173 return array('result'=>FALSE);
174 } else if ($file['size'] > PLUGIN_ATTACH_MAX_FILESIZE) {
177 'msg'=>$_attach_messages['err_exceed']);
178 } else if (! is_pagename($page) || ($pass !== TRUE && ! is_editable($page))) {
181 msg'=>$_attach_messages['err_noparm']);
182 } else if (PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY && $pass !== TRUE &&
183 ($pass === NULL || ! pkwk_login($pass))) {
186 'msg'=>$_attach_messages['err_adminpass']);
189 $obj = & new AttachFile($page, $file['name']);
191 return array('result'=>FALSE,
192 'msg'=>$_attach_messages['err_exists']);
194 if (move_uploaded_file($file['tmp_name'], $obj->filename))
195 chmod($obj->filename, PLUGIN_ATTACH_FILE_MODE);
198 touch(get_filename($page));
201 $obj->status['pass'] = ($pass !== TRUE && $pass !== NULL) ? md5($pass) : '';
205 $footer['ACTION'] = 'File attached';
206 $footer['FILENAME'] = & $file['name'];
207 $footer['FILESIZE'] = & $file['size'];
208 $footer['PAGE'] = & $page;
210 $footer['URI'] = get_script_uri() .
211 //'?' . rawurlencode($page);
215 '&refer=' . rawurlencode($page) .
216 '&file=' . rawurlencode($file['name']) .
219 $footer['USER_AGENT'] = TRUE;
220 $footer['REMOTE_ADDR'] = TRUE;
222 pkwk_mail_notify($notify_subject, "\n", $footer) or
223 die('pkwk_mail_notify(): Failed');
228 'msg'=>$_attach_messages['msg_uploaded']);
231 // ¾ÜºÙ¥Õ¥©¡¼¥à¤òɽ¼¨
232 function attach_info($err = '')
234 global $vars, $_attach_messages;
236 foreach (array('refer', 'file', 'age') as $var)
237 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
239 $obj = & new AttachFile($refer, $file, $age);
240 return $obj->getstatus() ?
242 array('msg'=>$_attach_messages['err_notfound']);
246 function attach_delete()
248 global $vars, $_attach_messages;
250 foreach (array('refer', 'file', 'age', 'pass') as $var)
251 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
253 if (is_freeze($refer) || ! is_editable($refer))
254 return array('msg'=>$_attach_messages['err_noparm']);
256 $obj = & new AttachFile($refer, $file, $age);
257 if (! $obj->getstatus())
258 return array('msg'=>$_attach_messages['err_notfound']);
260 return $obj->delete($pass);
264 function attach_freeze($freeze)
266 global $vars, $_attach_messages;
268 foreach (array('refer', 'file', 'age', 'pass') as $var) {
269 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
272 if (is_freeze($refer) || ! is_editable($refer)) {
273 return array('msg'=>$_attach_messages['err_noparm']);
275 $obj = & new AttachFile($refer, $file, $age);
276 return $obj->getstatus() ?
277 $obj->freeze($freeze, $pass) :
278 array('msg'=>$_attach_messages['err_notfound']);
283 function attach_rename()
285 global $vars, $_attach_messages;
287 foreach (array('refer', 'file', 'age', 'pass', 'newname') as $var) {
288 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
291 if (is_freeze($refer) || ! is_editable($refer)) {
292 return array('msg'=>$_attach_messages['err_noparm']);
294 $obj = & new AttachFile($refer, $file, $age);
295 if (! $obj->getstatus())
296 return array('msg'=>$_attach_messages['err_notfound']);
298 return $obj->rename($pass, $newname);
303 function attach_open()
305 global $vars, $_attach_messages;
307 foreach (array('refer', 'file', 'age') as $var) {
308 ${$var} = isset($vars[$var]) ? $vars[$var] : '';
311 $obj = & new AttachFile($refer, $file, $age);
312 return $obj->getstatus() ?
314 array('msg'=>$_attach_messages['err_notfound']);
318 function attach_list()
320 global $vars, $_attach_messages;
322 $refer = isset($vars['refer']) ? $vars['refer'] : '';
324 $obj = & new AttachPages($refer);
326 $msg = $_attach_messages[($refer == '') ? 'msg_listall' : 'msg_listpage'];
327 $body = ($refer == '' || isset($obj->pages[$refer])) ?
328 $obj->toString($refer, FALSE) :
329 $_attach_messages['err_noexist'];
331 return array('msg'=>$msg, 'body'=>$body);
334 // ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à¤òɽ¼¨ (action»þ)
335 function attach_showform()
337 global $vars, $_attach_messages;
339 $page = isset($vars['page']) ? $vars['page'] : '';
340 $vars['refer'] = $page;
341 $body = attach_form($page);
343 return array('msg'=>$_attach_messages['msg_upload'], 'body'=>$body);
348 function attach_mime_content_type($filename)
350 $type = 'application/octet-stream'; // default
352 if (! file_exists($filename)) return $type;
354 $size = @getimagesize($filename);
355 if (is_array($size)) {
357 case 1: return 'image/gif';
358 case 2: return 'image/jpeg';
359 case 3: return 'image/png';
360 case 4: return 'application/x-shockwave-flash';
365 if (! preg_match('/_((?:[0-9A-F]{2})+)(?:\.\d+)?$/', $filename, $matches))
368 $filename = decode($matches[1]);
370 // mime-type°ìÍ÷ɽ¤ò¼èÆÀ
371 $config = new Config(PLUGIN_ATTACH_CONFIG_PAGE_MIME);
372 $table = $config->read() ? $config->get('mime-type') : array();
373 unset($config); // ¥á¥â¥êÀáÌó
375 foreach ($table as $row) {
376 $_type = trim($row[0]);
377 $exts = preg_split('/\s+|,/', trim($row[1]), -1, PREG_SPLIT_NO_EMPTY);
378 foreach ($exts as $ext) {
379 if (preg_match("/\.$ext$/i", $filename)) return $_type;
386 // ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥©¡¼¥à¤Î½ÐÎÏ
387 function attach_form($page)
389 global $script, $vars, $_attach_messages;
391 $r_page = rawurlencode($page);
392 $s_page = htmlsc($page);
395 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
396 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
400 if (! ini_get('file_uploads')) return '#attach(): file_uploads disabled<br />' . $navi;
401 if (! is_page($page)) return '#attach(): No such page<br />' . $navi;
403 $maxsize = PLUGIN_ATTACH_MAX_FILESIZE;
404 $msg_maxsize = sprintf($_attach_messages['msg_maxsize'], number_format($maxsize/1024) . 'KB');
407 if (PLUGIN_ATTACH_PASSWORD_REQUIRE || PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY) {
408 $title = $_attach_messages[PLUGIN_ATTACH_UPLOAD_ADMIN_ONLY ? 'msg_adminpass' : 'msg_password'];
409 $pass = '<br />' . $title . ': <input type="password" name="pass" size="8" />';
412 <form enctype="multipart/form-data" action="$script" method="post">
414 <input type="hidden" name="plugin" value="attach" />
415 <input type="hidden" name="pcmd" value="post" />
416 <input type="hidden" name="refer" value="$s_page" />
417 <input type="hidden" name="max_file_size" value="$maxsize" />
422 <label for="_p_attach_file">{$_attach_messages['msg_file']}:</label> <input type="file" name="attach_file" id="_p_attach_file" />
424 <input type="submit" value="{$_attach_messages['btn_upload']}" />
434 var $page, $file, $age, $basename, $filename, $logname;
439 var $status = array('count'=>array(0), 'age'=>'', 'pass'=>'', 'freeze'=>FALSE);
441 function AttachFile($page, $file, $age = 0)
444 $this->file = preg_replace('#^.*/#','',$file);
445 $this->age = is_numeric($age) ? $age : 0;
447 $this->basename = UPLOAD_DIR . encode($page) . '_' . encode($this->file);
448 $this->filename = $this->basename . ($age ? '.' . $age : '');
449 $this->logname = $this->basename . '.log';
450 $this->exist = file_exists($this->filename);
451 $this->time = $this->exist ? filemtime($this->filename) - LOCALZONE : 0;
452 $this->md5hash = $this->exist ? md5_file($this->filename) : '';
458 if (! $this->exist) return FALSE;
461 if (file_exists($this->logname)) {
462 $data = file($this->logname);
463 foreach ($this->status as $key=>$value) {
464 $this->status[$key] = chop(array_shift($data));
466 $this->status['count'] = explode(',', $this->status['count']);
468 $this->time_str = get_date('Y/m/d H:i:s', $this->time);
469 $this->size = filesize($this->filename);
470 $this->size_str = sprintf('%01.1f', round($this->size/1024, 1)) . 'KB';
471 $this->type = attach_mime_content_type($this->filename);
479 $this->status['count'] = join(',', $this->status['count']);
480 $fp = fopen($this->logname, 'wb') or
481 die_message('cannot write ' . $this->logname);
482 set_file_buffer($fp, 0);
485 foreach ($this->status as $key=>$value) {
486 fwrite($fp, $value . "\n");
493 function datecomp($a, $b) {
494 return ($a->time == $b->time) ? 0 : (($a->time > $b->time) ? -1 : 1);
497 function toString($showicon, $showinfo)
499 global $script, $_attach_messages;
502 $param = '&file=' . rawurlencode($this->file) . '&refer=' . rawurlencode($this->page) .
503 ($this->age ? '&age=' . $this->age : '');
504 $title = $this->time_str . ' ' . $this->size_str;
505 $label = ($showicon ? PLUGIN_ATTACH_FILE_ICON : '') . htmlsc($this->file);
507 $label .= ' (backup No.' . $this->age . ')';
511 $_title = str_replace('$1', rawurlencode($this->file), $_attach_messages['msg_info']);
512 $info = "\n<span class=\"small\">[<a href=\"$script?plugin=attach&pcmd=info$param\" title=\"$_title\">{$_attach_messages['btn_info']}</a>]</span>\n";
513 $count = ($showicon && ! empty($this->status['count'][$this->age])) ?
514 sprintf($_attach_messages['msg_count'], $this->status['count'][$this->age]) : '';
516 return "<a href=\"$script?plugin=attach&pcmd=open$param\" title=\"$title\">$label</a>$count$info";
522 global $script, $_attach_messages;
524 $r_page = rawurlencode($this->page);
525 $s_page = htmlsc($this->page);
526 $s_file = htmlsc($this->file);
527 $s_err = ($err == '') ? '' : '<p style="font-weight:bold">' . $_attach_messages[$err] . '</p>';
532 $msg_delete = '<input type="radio" name="pcmd" id="_p_attach_delete" value="delete" />' .
533 '<label for="_p_attach_delete">' . $_attach_messages['msg_delete'] .
534 $_attach_messages['msg_require'] . '</label><br />';
537 if ($this->status['freeze']) {
538 $msg_freezed = "<dd>{$_attach_messages['msg_isfreeze']}</dd>";
540 $msg_freeze = '<input type="radio" name="pcmd" id="_p_attach_unfreeze" value="unfreeze" />' .
541 '<label for="_p_attach_unfreeze">' . $_attach_messages['msg_unfreeze'] .
542 $_attach_messages['msg_require'] . '</label><br />';
545 $msg_delete = '<input type="radio" name="pcmd" id="_p_attach_delete" value="delete" />' .
546 '<label for="_p_attach_delete">' . $_attach_messages['msg_delete'];
547 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age)
548 $msg_delete .= $_attach_messages['msg_require'];
549 $msg_delete .= '</label><br />';
550 $msg_freeze = '<input type="radio" name="pcmd" id="_p_attach_freeze" value="freeze" />' .
551 '<label for="_p_attach_freeze">' . $_attach_messages['msg_freeze'] .
552 $_attach_messages['msg_require'] . '</label><br />';
554 if (PLUGIN_ATTACH_RENAME_ENABLE) {
555 $msg_rename = '<input type="radio" name="pcmd" id="_p_attach_rename" value="rename" />' .
556 '<label for="_p_attach_rename">' . $_attach_messages['msg_rename'] .
557 $_attach_messages['msg_require'] . '</label><br /> ' .
558 '<label for="_p_attach_newname">' . $_attach_messages['msg_newname'] .
560 '<input type="text" name="newname" id="_p_attach_newname" size="40" value="' .
561 $this->file . '" /><br />';
565 $info = $this->toString(TRUE, FALSE);
567 $retval = array('msg'=>sprintf($_attach_messages['msg_info'], htmlsc($this->file)));
568 $retval['body'] = <<< EOD
570 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
571 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
575 <dd>{$_attach_messages['msg_page']}:$s_page</dd>
576 <dd>{$_attach_messages['msg_filename']}:{$this->filename}</dd>
577 <dd>{$_attach_messages['msg_md5hash']}:{$this->md5hash}</dd>
578 <dd>{$_attach_messages['msg_filesize']}:{$this->size_str} ({$this->size} bytes)</dd>
579 <dd>Content-type:{$this->type}</dd>
580 <dd>{$_attach_messages['msg_date']}:{$this->time_str}</dd>
581 <dd>{$_attach_messages['msg_dlcount']}:{$this->status['count'][$this->age]}</dd>
586 <form action="$script" method="post">
588 <input type="hidden" name="plugin" value="attach" />
589 <input type="hidden" name="refer" value="$s_page" />
590 <input type="hidden" name="file" value="$s_file" />
591 <input type="hidden" name="age" value="{$this->age}" />
596 <label for="_p_attach_password">{$_attach_messages['msg_password']}:</label>
597 <input type="password" name="pass" id="_p_attach_password" size="8" />
598 <input type="submit" value="{$_attach_messages['btn_submit']}" />
605 function delete($pass)
607 global $_attach_messages, $notify, $notify_subject;
609 if ($this->status['freeze']) return attach_info('msg_isfreeze');
611 if (! pkwk_login($pass)) {
612 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
613 return attach_info('err_adminpass');
614 } else if (PLUGIN_ATTACH_PASSWORD_REQUIRE &&
615 md5($pass) != $this->status['pass']) {
616 return attach_info('err_password');
622 (PLUGIN_ATTACH_DELETE_ADMIN_ONLY && PLUGIN_ATTACH_DELETE_ADMIN_NOBACKUP)) {
623 @unlink($this->filename);
626 $age = ++$this->status['age'];
627 } while (file_exists($this->basename . '.' . $age));
629 if (! rename($this->basename,$this->basename . '.' . $age)) {
631 return array('msg'=>$_attach_messages['err_delete']);
634 $this->status['count'][$age] = $this->status['count'][0];
635 $this->status['count'][0] = 0;
639 if (is_page($this->page))
640 touch(get_filename($this->page));
643 $footer['ACTION'] = 'File deleted';
644 $footer['FILENAME'] = & $this->file;
645 $footer['PAGE'] = & $this->page;
646 $footer['URI'] = get_script_uri() .
647 '?' . rawurlencode($this->page);
648 $footer['USER_AGENT'] = TRUE;
649 $footer['REMOTE_ADDR'] = TRUE;
650 pkwk_mail_notify($notify_subject, "\n", $footer) or
651 die('pkwk_mail_notify(): Failed');
654 return array('msg'=>$_attach_messages['msg_deleted']);
657 function rename($pass, $newname)
659 global $_attach_messages, $notify, $notify_subject;
661 if ($this->status['freeze']) return attach_info('msg_isfreeze');
663 if (! pkwk_login($pass)) {
664 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
665 return attach_info('err_adminpass');
666 } else if (PLUGIN_ATTACH_PASSWORD_REQUIRE &&
667 md5($pass) != $this->status['pass']) {
668 return attach_info('err_password');
671 $newbase = UPLOAD_DIR . encode($this->page) . '_' . encode($newname);
672 if (file_exists($newbase)) {
673 return array('msg'=>$_attach_messages['err_exists']);
675 if (! PLUGIN_ATTACH_RENAME_ENABLE || ! rename($this->basename, $newbase)) {
676 return array('msg'=>$_attach_messages['err_rename']);
679 return array('msg'=>$_attach_messages['msg_renamed']);
682 function freeze($freeze, $pass)
684 global $_attach_messages;
686 if (! pkwk_login($pass)) return attach_info('err_adminpass');
689 $this->status['freeze'] = $freeze;
692 return array('msg'=>$_attach_messages[$freeze ? 'msg_freezed' : 'msg_unfreezed']);
698 $this->status['count'][$this->age]++;
700 $filename = $this->file;
702 // Care for Japanese-character-included file name
704 switch(UA_NAME . '/' . UA_PROFILE){
705 case 'Opera/default':
706 // Care for using _auto-encode-detecting_ function
707 $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
710 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
714 $filename = htmlsc($filename);
716 ini_set('default_charset', '');
717 mb_http_output('pass');
719 pkwk_common_headers();
720 header('Content-Disposition: inline; filename="' . $filename . '"');
721 header('Content-Length: ' . $this->size);
722 header('Content-Type: ' . $this->type);
724 @readfile($this->filename);
733 var $files = array();
735 function AttachFiles($page)
740 function add($file, $age)
742 $this->files[$file][$age] = & new AttachFile($this->page, $file, $age);
745 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ
746 function toString($flat)
748 global $_title_cannotread;
750 if (! check_readable($this->page, FALSE, FALSE)) {
751 return str_replace('$1', make_pagelink($this->page), $_title_cannotread);
753 return $this->to_flat();
757 $files = array_keys($this->files);
760 foreach ($files as $file) {
762 foreach (array_keys($this->files[$file]) as $age) {
763 $_files[$age] = $this->files[$file][$age]->toString(FALSE, TRUE);
765 if (! isset($_files[0])) {
766 $_files[0] = htmlsc($file);
771 $ret .= " <li>$_file\n";
772 if (count($_files)) {
773 $ret .= "<ul>\n<li>" . join("</li>\n<li>", $_files) . "</li>\n</ul>\n";
777 return make_pagelink($this->page) . "\n<ul>\n$ret</ul>\n";
780 // ¥Õ¥¡¥¤¥ë°ìÍ÷¤ò¼èÆÀ(inline)
785 foreach (array_keys($this->files) as $file) {
786 if (isset($this->files[$file][0])) {
787 $files[$file] = & $this->files[$file][0];
790 uasort($files, array('AttachFile', 'datecomp'));
791 foreach (array_keys($files) as $file) {
792 $ret .= $files[$file]->toString(TRUE, TRUE) . ' ';
802 var $pages = array();
804 function AttachPages($page = '', $age = NULL)
807 $dir = opendir(UPLOAD_DIR) or
808 die('directory ' . UPLOAD_DIR . ' is not exist or not readable.');
810 $page_pattern = ($page == '') ? '(?:[0-9A-F]{2})+' : preg_quote(encode($page), '/');
811 $age_pattern = ($age === NULL) ?
812 '(?:\.([0-9]+))?' : ($age ? "\.($age)" : '');
813 $pattern = "/^({$page_pattern})_((?:[0-9A-F]{2})+){$age_pattern}$/";
816 while ($file = readdir($dir)) {
817 if (! preg_match($pattern, $file, $matches))
820 $_page = decode($matches[1]);
821 $_file = decode($matches[2]);
822 $_age = isset($matches[3]) ? $matches[3] : 0;
823 if (! isset($this->pages[$_page])) {
824 $this->pages[$_page] = & new AttachFiles($_page);
826 $this->pages[$_page]->add($_file, $_age);
831 function toString($page = '', $flat = FALSE)
834 if (! isset($this->pages[$page])) {
837 return $this->pages[$page]->toString($flat);
842 $pages = array_keys($this->pages);
845 foreach ($pages as $page) {
846 if (check_non_list($page)) continue;
847 $ret .= '<li>' . $this->pages[$page]->toString($flat) . '</li>' . "\n";
849 return "\n" . '<ul>' . "\n" . $ret . '</ul>' . "\n";