00001 <?php
00002
00015
00016
00017
00018 class FilesHandler extends ManagerHandler {
00019
00023 function files($args) {
00024 parent::validate();
00025 parent::setupTemplate(true);
00026
00027 import('file.FileManager');
00028
00029 $templateMgr = &TemplateManager::getManager();
00030 $templateMgr->assign('pageHierarchy', array(array(Request::url(null, 'manager'), 'manager.journalManagement')));
00031
00032 FilesHandler::parseDirArg($args, $currentDir, $parentDir);
00033 $currentPath = FilesHandler::getRealFilesDir($currentDir);
00034
00035 if (@is_file($currentPath)) {
00036 $fileMgr = &new FileManager();
00037 if (Request::getUserVar('download')) {
00038 $fileMgr->downloadFile($currentPath);
00039 } else {
00040 $fileMgr->viewFile($currentPath, FilesHandler::fileMimeType($currentPath));
00041 }
00042
00043 } else {
00044 $files = array();
00045 if ($dh = @opendir($currentPath)) {
00046 while (($file = readdir($dh)) !== false) {
00047 if ($file != '.' && $file != '..') {
00048 $filePath = $currentPath . '/'. $file;
00049 $isDir = is_dir($filePath);
00050 $info = array(
00051 'name' => $file,
00052 'isDir' => $isDir,
00053 'mimetype' => $isDir ? '' : FilesHandler::fileMimeType($filePath),
00054 'mtime' => filemtime($filePath),
00055 'size' => $isDir ? '' : FileManager::getNiceFileSize(filesize($filePath)),
00056 );
00057 $files[$file] = $info;
00058 }
00059 }
00060 closedir($dh);
00061 }
00062 ksort($files);
00063 $templateMgr->assign_by_ref('files', $files);
00064 $templateMgr->assign('currentDir', $currentDir);
00065 $templateMgr->assign('parentDir', $parentDir);
00066 $templateMgr->assign('helpTopicId','journal.managementPages.fileBrowser');
00067 $templateMgr->display('manager/files/index.tpl');
00068 }
00069 }
00070
00074 function fileUpload($args) {
00075 parent::validate();
00076
00077 FilesHandler::parseDirArg($args, $currentDir, $parentDir);
00078 $currentPath = FilesHandler::getRealFilesDir($currentDir);
00079
00080 import('file.FileManager');
00081 $fileMgr = &new FileManager();
00082 if ($fileMgr->uploadedFileExists('file')) {
00083 $destPath = $currentPath . '/' . FilesHandler::cleanFileName($fileMgr->getUploadedFileName('file'));
00084 @$fileMgr->uploadFile('file', $destPath);
00085 }
00086
00087 Request::redirect(null, null, 'files', explode('/', $currentDir));
00088
00089 }
00090
00094 function fileMakeDir($args) {
00095 parent::validate();
00096
00097 FilesHandler::parseDirArg($args, $currentDir, $parentDir);
00098
00099 if ($dirName = Request::getUserVar('dirName')) {
00100 $currentPath = FilesHandler::getRealFilesDir($currentDir);
00101 $newDir = $currentPath . '/' . FilesHandler::cleanFileName($dirName);
00102
00103 import('file.FileManager');
00104 $fileMgr = &new FileManager();
00105 @$fileMgr->mkdir($newDir);
00106 }
00107
00108 Request::redirect(null, null, 'files', explode('/', $currentDir));
00109 }
00110
00111 function fileDelete($args) {
00112 parent::validate();
00113
00114 FilesHandler::parseDirArg($args, $currentDir, $parentDir);
00115 $currentPath = FilesHandler::getRealFilesDir($currentDir);
00116
00117 import('file.FileManager');
00118 $fileMgr = &new FileManager();
00119
00120 if (@is_file($currentPath)) {
00121 $fileMgr->deleteFile($currentPath);
00122 } else {
00123
00124 @$fileMgr->rmdir($currentPath);
00125 }
00126
00127 Request::redirect(null, null, 'files', explode('/', $parentDir));
00128 }
00129
00130
00131
00132
00133
00134
00135
00136 function parseDirArg($args, &$currentDir, &$parentDir) {
00137 $pathArray = array_filter($args, array('FilesHandler', 'fileNameFilter'));
00138 $currentDir = join($pathArray, '/');
00139 array_pop($pathArray);
00140 $parentDir = join($pathArray, '/');
00141 }
00142
00143 function getRealFilesDir($currentDir) {
00144 $journal = &Request::getJournal();
00145 return Config::getVar('files', 'files_dir') . '/journals/' . $journal->getJournalId() .'/' . $currentDir;
00146 }
00147
00148 function fileNameFilter($var) {
00149 return (!empty($var) && $var != '..' && $var != '.');
00150 }
00151
00152 function cleanFileName($var) {
00153 $var = String::regexp_replace('/[^\w\-\.]/', '', $var);
00154 if (!FilesHandler::fileNameFilter($var)) {
00155 $var = time() . '';
00156 }
00157 return $var;
00158 }
00159
00160 function fileMimeType($filePath) {
00161 return String::mime_content_type($filePath);
00162 }
00163
00164 }
00165 ?>