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', FALSE); // FALSE or TRUE
32 // アップロード/削除時にパスワードを要求する(ADMIN_ONLYが優先)
33 define('PLUGIN_ATTACH_PASSWORD_REQUIRE', FALSE); // FALSE or TRUE
36 define('PLUGIN_ATTACH_RENAME_ENABLE', TRUE); // FALSE or TRUE
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" />');
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";
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 pkwk_touch_file(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 //'?' . pagename_urlencode($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']);
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]);
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;
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;
456 return $this->exist ? md5_file($this->filename) : '';
462 if (! $this->exist) return FALSE;
465 if (file_exists($this->logname)) {
466 $data = file($this->logname);
467 foreach ($this->status as $key=>$value) {
468 $this->status[$key] = chop(array_shift($data));
470 $this->status['count'] = explode(',', $this->status['count']);
472 $this->time_str = get_date('Y/m/d H:i:s', $this->time);
473 $this->size = filesize($this->filename);
474 $this->size_str = sprintf('%01.1f', round($this->size/1024, 1)) . 'KB';
475 $this->type = attach_mime_content_type($this->filename);
483 $this->status['count'] = join(',', $this->status['count']);
484 $fp = fopen($this->logname, 'wb') or
485 die_message('cannot write ' . $this->logname);
486 set_file_buffer($fp, 0);
489 foreach ($this->status as $key=>$value) {
490 fwrite($fp, $value . "\n");
497 function datecomp($a, $b) {
498 return ($a->time == $b->time) ? 0 : (($a->time > $b->time) ? -1 : 1);
501 function toString($showicon, $showinfo)
503 global $script, $_attach_messages;
506 $param = '&file=' . rawurlencode($this->file) . '&refer=' . rawurlencode($this->page) .
507 ($this->age ? '&age=' . $this->age : '');
508 $title = $this->time_str . ' ' . $this->size_str;
509 $label = ($showicon ? PLUGIN_ATTACH_FILE_ICON : '') . htmlsc($this->file);
511 $label .= ' (backup No.' . $this->age . ')';
515 $_title = str_replace('$1', rawurlencode($this->file), $_attach_messages['msg_info']);
516 $info = "\n<span class=\"small\">[<a href=\"$script?plugin=attach&pcmd=info$param\" title=\"$_title\">{$_attach_messages['btn_info']}</a>]</span>\n";
517 $count = ($showicon && ! empty($this->status['count'][$this->age])) ?
518 sprintf($_attach_messages['msg_count'], $this->status['count'][$this->age]) : '';
520 return "<a href=\"$script?plugin=attach&pcmd=open$param\" title=\"$title\">$label</a>$count$info";
526 global $script, $_attach_messages;
528 $r_page = rawurlencode($this->page);
529 $s_page = htmlsc($this->page);
530 $s_file = htmlsc($this->file);
531 $s_err = ($err == '') ? '' : '<p style="font-weight:bold">' . $_attach_messages[$err] . '</p>';
536 $msg_delete = '<input type="radio" name="pcmd" id="_p_attach_delete" value="delete" />' .
537 '<label for="_p_attach_delete">' . $_attach_messages['msg_delete'] .
538 $_attach_messages['msg_require'] . '</label><br />';
541 if ($this->status['freeze']) {
542 $msg_freezed = "<dd>{$_attach_messages['msg_isfreeze']}</dd>";
544 $msg_freeze = '<input type="radio" name="pcmd" id="_p_attach_unfreeze" value="unfreeze" />' .
545 '<label for="_p_attach_unfreeze">' . $_attach_messages['msg_unfreeze'] .
546 $_attach_messages['msg_require'] . '</label><br />';
549 $msg_delete = '<input type="radio" name="pcmd" id="_p_attach_delete" value="delete" />' .
550 '<label for="_p_attach_delete">' . $_attach_messages['msg_delete'];
551 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age)
552 $msg_delete .= $_attach_messages['msg_require'];
553 $msg_delete .= '</label><br />';
554 $msg_freeze = '<input type="radio" name="pcmd" id="_p_attach_freeze" value="freeze" />' .
555 '<label for="_p_attach_freeze">' . $_attach_messages['msg_freeze'] .
556 $_attach_messages['msg_require'] . '</label><br />';
558 if (PLUGIN_ATTACH_RENAME_ENABLE) {
559 $msg_rename = '<input type="radio" name="pcmd" id="_p_attach_rename" value="rename" />' .
560 '<label for="_p_attach_rename">' . $_attach_messages['msg_rename'] .
561 $_attach_messages['msg_require'] . '</label><br /> ' .
562 '<label for="_p_attach_newname">' . $_attach_messages['msg_newname'] .
564 '<input type="text" name="newname" id="_p_attach_newname" size="40" value="' .
565 $this->file . '" /><br />';
569 $info = $this->toString(TRUE, FALSE);
570 $hash = $this->gethash();
572 $retval = array('msg'=>sprintf($_attach_messages['msg_info'], htmlsc($this->file)));
573 $retval['body'] = <<< EOD
575 [<a href="$script?plugin=attach&pcmd=list&refer=$r_page">{$_attach_messages['msg_list']}</a>]
576 [<a href="$script?plugin=attach&pcmd=list">{$_attach_messages['msg_listall']}</a>]
580 <dd>{$_attach_messages['msg_page']}:$s_page</dd>
581 <dd>{$_attach_messages['msg_filename']}:{$this->filename}</dd>
582 <dd>{$_attach_messages['msg_md5hash']}:$hash</dd>
583 <dd>{$_attach_messages['msg_filesize']}:{$this->size_str} ({$this->size} bytes)</dd>
584 <dd>Content-type:{$this->type}</dd>
585 <dd>{$_attach_messages['msg_date']}:{$this->time_str}</dd>
586 <dd>{$_attach_messages['msg_dlcount']}:{$this->status['count'][$this->age]}</dd>
591 <form action="$script" method="post">
593 <input type="hidden" name="plugin" value="attach" />
594 <input type="hidden" name="refer" value="$s_page" />
595 <input type="hidden" name="file" value="$s_file" />
596 <input type="hidden" name="age" value="{$this->age}" />
601 <label for="_p_attach_password">{$_attach_messages['msg_password']}:</label>
602 <input type="password" name="pass" id="_p_attach_password" size="8" />
603 <input type="submit" value="{$_attach_messages['btn_submit']}" />
610 function delete($pass)
612 global $_attach_messages, $notify, $notify_subject;
614 if ($this->status['freeze']) return attach_info('msg_isfreeze');
616 if (! pkwk_login($pass)) {
617 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
618 return attach_info('err_adminpass');
619 } else if (PLUGIN_ATTACH_PASSWORD_REQUIRE &&
620 md5($pass) !== $this->status['pass']) {
621 return attach_info('err_password');
627 (PLUGIN_ATTACH_DELETE_ADMIN_ONLY && PLUGIN_ATTACH_DELETE_ADMIN_NOBACKUP)) {
628 @unlink($this->filename);
631 $age = ++$this->status['age'];
632 } while (file_exists($this->basename . '.' . $age));
634 if (! rename($this->basename,$this->basename . '.' . $age)) {
636 return array('msg'=>$_attach_messages['err_delete']);
639 $this->status['count'][$age] = $this->status['count'][0];
640 $this->status['count'][0] = 0;
644 if (is_page($this->page))
645 touch(get_filename($this->page));
648 $footer['ACTION'] = 'File deleted';
649 $footer['FILENAME'] = & $this->file;
650 $footer['PAGE'] = & $this->page;
651 $footer['URI'] = get_script_uri() .
652 '?' . pagename_urlencode($this->page);
653 $footer['USER_AGENT'] = TRUE;
654 $footer['REMOTE_ADDR'] = TRUE;
655 pkwk_mail_notify($notify_subject, "\n", $footer) or
656 die('pkwk_mail_notify(): Failed');
659 return array('msg'=>$_attach_messages['msg_deleted']);
662 function rename($pass, $newname)
664 global $_attach_messages, $notify, $notify_subject;
666 if ($this->status['freeze']) return attach_info('msg_isfreeze');
668 if (! pkwk_login($pass)) {
669 if (PLUGIN_ATTACH_DELETE_ADMIN_ONLY || $this->age) {
670 return attach_info('err_adminpass');
671 } else if (PLUGIN_ATTACH_PASSWORD_REQUIRE &&
672 md5($pass) !== $this->status['pass']) {
673 return attach_info('err_password');
676 $newbase = UPLOAD_DIR . encode($this->page) . '_' . encode($newname);
677 if (file_exists($newbase)) {
678 return array('msg'=>$_attach_messages['err_exists']);
680 if (! PLUGIN_ATTACH_RENAME_ENABLE || ! rename($this->basename, $newbase)) {
681 return array('msg'=>$_attach_messages['err_rename']);
684 return array('msg'=>$_attach_messages['msg_renamed']);
687 function freeze($freeze, $pass)
689 global $_attach_messages;
691 if (! pkwk_login($pass)) return attach_info('err_adminpass');
694 $this->status['freeze'] = $freeze;
697 return array('msg'=>$_attach_messages[$freeze ? 'msg_freezed' : 'msg_unfreezed']);
703 $this->status['count'][$this->age]++;
705 $filename = $this->file;
707 // Care for Japanese-character-included file name
709 switch(UA_NAME . '/' . UA_PROFILE){
710 case 'Opera/default':
711 // Care for using _auto-encode-detecting_ function
712 $filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
715 $filename = mb_convert_encoding($filename, 'SJIS', 'auto');
719 $utf8filename = mb_convert_encoding($filename, 'UTF-8', 'auto');
721 ini_set('default_charset', '');
722 mb_http_output('pass');
724 pkwk_common_headers();
725 header('Content-Disposition: inline; filename="' . $filename . '"; filename*=utf-8\'\'' . rawurlencode($utf8filename));
726 header('Content-Length: ' . $this->size);
727 header('Content-Type: ' . $this->type);
729 @readfile($this->filename);
738 var $files = array();
740 function AttachFiles($page)
745 function add($file, $age)
747 $this->files[$file][$age] = & new AttachFile($this->page, $file, $age);
751 function toString($flat)
753 global $_title_cannotread;
755 if (! check_readable($this->page, FALSE, FALSE)) {
756 return str_replace('$1', make_pagelink($this->page), $_title_cannotread);
758 return $this->to_flat();
762 $files = array_keys($this->files);
763 sort($files, SORT_STRING);
765 foreach ($files as $file) {
767 foreach (array_keys($this->files[$file]) as $age) {
768 $_files[$age] = $this->files[$file][$age]->toString(FALSE, TRUE);
770 if (! isset($_files[0])) {
771 $_files[0] = htmlsc($file);
773 ksort($_files, SORT_NUMERIC);
776 $ret .= " <li>$_file\n";
777 if (count($_files)) {
778 $ret .= "<ul>\n<li>" . join("</li>\n<li>", $_files) . "</li>\n</ul>\n";
782 return make_pagelink($this->page) . "\n<ul>\n$ret</ul>\n";
790 foreach (array_keys($this->files) as $file) {
791 if (isset($this->files[$file][0])) {
792 $files[$file] = & $this->files[$file][0];
795 uasort($files, array('AttachFile', 'datecomp'));
796 foreach (array_keys($files) as $file) {
797 $ret .= $files[$file]->toString(TRUE, TRUE) . ' ';
807 var $pages = array();
809 function AttachPages($page = '', $age = NULL)
812 $dir = opendir(UPLOAD_DIR) or
813 die('directory ' . UPLOAD_DIR . ' is not exist or not readable.');
815 $page_pattern = ($page == '') ? '(?:[0-9A-F]{2})+' : preg_quote(encode($page), '/');
816 $age_pattern = ($age === NULL) ?
817 '(?:\.([0-9]+))?' : ($age ? "\.($age)" : '');
818 $pattern = "/^({$page_pattern})_((?:[0-9A-F]{2})+){$age_pattern}$/";
821 while (($file = readdir($dir)) !== FALSE) {
822 if (! preg_match($pattern, $file, $matches)) continue;
824 $_page = decode($matches[1]);
825 $_file = decode($matches[2]);
826 $_age = isset($matches[3]) ? $matches[3] : 0;
827 if (! isset($this->pages[$_page])) {
828 $this->pages[$_page] = & new AttachFiles($_page);
830 $this->pages[$_page]->add($_file, $_age);
835 function toString($page = '', $flat = FALSE)
838 if (! isset($this->pages[$page])) {
841 return $this->pages[$page]->toString($flat);
846 $pages = array_keys($this->pages);
847 sort($pages, SORT_STRING);
849 foreach ($pages as $page) {
850 if (check_non_list($page)) continue;
851 $ret .= '<li>' . $this->pages[$page]->toString($flat) . '</li>' . "\n";
853 return "\n" . '<ul>' . "\n" . $ret . '</ul>' . "\n";