23 define(
'FILE_MODE_MASK', 0666);
24 define(
'DIRECTORY_MODE_MASK', 0777);
26 define(
'DOCUMENT_TYPE_DEFAULT',
'default');
27 define(
'DOCUMENT_TYPE_EXCEL',
'excel');
28 define(
'DOCUMENT_TYPE_HTML',
'html');
29 define(
'DOCUMENT_TYPE_IMAGE',
'image');
30 define(
'DOCUMENT_TYPE_PDF',
'pdf');
31 define(
'DOCUMENT_TYPE_WORD',
'word');
32 define(
'DOCUMENT_TYPE_EPUB',
'epub');
33 define(
'DOCUMENT_TYPE_ZIP',
'zip');
48 if (isset($_FILES[$fileName]) && isset($_FILES[$fileName][
'tmp_name'])
49 && is_uploaded_file($_FILES[$fileName][
'tmp_name'])) {
61 return (isset($_FILES[$fileName]) && $_FILES[$fileName][
'error'] != UPLOAD_ERR_OK);
71 return $_FILES[$fileName][
'error'];
89 if (isset($_FILES[$fileName][
'tmp_name']) && is_uploaded_file($_FILES[$fileName][
'tmp_name'])) {
90 return $_FILES[$fileName][
'tmp_name'];
101 if (isset($_FILES[$fileName][
'name'])) {
102 return $_FILES[$fileName][
'name'];
113 if (isset($_FILES[$fileName])) {
115 $exploded = explode(
'.',$_FILES[$fileName][
'name']);
118 $_FILES[$fileName][
'tmp_name'],
122 if (!empty($type))
return $type;
123 return $_FILES[$fileName][
'type'];
135 $destDir = dirname($destFileName);
140 if (!isset($_FILES[$fileName]))
return false;
141 if (move_uploaded_file($_FILES[$fileName][
'tmp_name'], $destFileName))
142 return $this->
setMode($destFileName, FILE_MODE_MASK);
154 $destDir = dirname($dest);
159 if (($f = fopen($dest,
'wb'))===
false) $success =
false;
160 if ($success && fwrite($f, $contents)===
false) $success =
false;
164 return $this->
setMode($dest, FILE_MODE_MASK);
175 $destDir = dirname($dest);
180 if (copy($source, $dest))
181 return $this->
setMode($dest, FILE_MODE_MASK);
193 if (is_dir($source)) {
195 $destDir = dir($source);
197 while (($entry = $destDir->read()) !==
false) {
198 if ($entry ==
'.' || $entry ==
'..') {
202 $Entry = $source . DIRECTORY_SEPARATOR . $entry;
203 if (is_dir($Entry) ) {
204 $this->
copyDir($Entry, $dest . DIRECTORY_SEPARATOR . $entry );
207 $this->
copyFile($Entry, $dest . DIRECTORY_SEPARATOR . $entry );
228 if (is_readable($filePath)) {
229 $f = fopen($filePath,
'rb');
230 if (!$f)
return false;
233 $data .= fread($f, 4096);
241 if ($output)
return true;
256 function downloadByPath($filePath, $mediaType =
null, $inline =
false, $fileName =
null) {
258 if (
HookRegistry::call(
'FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result, &$fileName)))
return $result;
259 if (is_readable($filePath)) {
260 if ($mediaType ===
null) {
263 if (empty($mediaType)) $mediaType =
'application/octet-stream';
265 if ($fileName ===
null) {
267 $fileName = basename($filePath);
271 header(
"Content-Type: $mediaType");
272 header(
'Content-Length: ' . filesize($filePath));
273 header(
'Accept-Ranges: none');
274 header(
'Content-Disposition: ' . ($inline ?
'inline' :
'attachment') .
"; filename=\"$fileName\"");
275 header(
'Cache-Control: private');
276 header(
'Pragma: public');
294 if (
HookRegistry::call(
'FileManager::deleteFile', array($filePath, &$result)))
return $result;
295 return unlink($filePath);
306 function mkdir($dirPath, $perms =
null) {
307 if ($perms !==
null) {
308 return mkdir($dirPath, $perms);
311 return $this->
setMode($dirPath, DIRECTORY_MODE_MASK);
322 return rmdir($dirPath);
331 if (file_exists($file)) {
333 $handle = opendir($file);
334 while (($filename = readdir($handle)) !==
false) {
335 if ($filename !=
'.' && $filename !=
'..') {
336 if (!$this->
rmtree($file .
'/' . $filename))
return false;
340 if (!
rmdir($file))
return false;
343 if (!unlink($file))
return false;
356 if (!file_exists($dirPath)) {
358 if ($dirPath == dirname($dirPath)) {
359 fatalError(
'There are no readable files in this directory tree. Are safe mode or open_basedir active?');
361 }
else if ($this->
mkdirtree(dirname($dirPath), $perms)) {
362 return $this->
mkdir($dirPath, $perms);
378 return file_exists($filePath);
380 return file_exists($filePath) && is_dir($filePath);
393 return DOCUMENT_TYPE_IMAGE;
396 case 'application/pdf':
397 case 'application/x-pdf':
400 return DOCUMENT_TYPE_PDF;
401 case 'application/msword':
402 case 'application/word':
403 return DOCUMENT_TYPE_WORD;
404 case 'application/excel':
405 return DOCUMENT_TYPE_EXCEL;
407 return DOCUMENT_TYPE_HTML;
408 case 'application/zip':
409 case 'application/x-zip':
410 case 'application/x-zip-compressed':
411 case 'application/x-compress':
412 case 'application/x-compressed':
413 case 'multipart/x-zip':
414 return DOCUMENT_TYPE_ZIP;
415 case 'application/epub':
416 case 'application/epub+zip':
417 return DOCUMENT_TYPE_EPUB;
419 return DOCUMENT_TYPE_DEFAULT;
430 case 'application/pdf':
432 case 'application/word':
438 case 'application/epub+zip':
460 case 'image/vnd.microsoft.icon':
465 case 'image/svg+xml':
468 case 'application/x-shockwave-flash':
471 case 'application/x-flash-video':
472 case 'flv-application/octet-stream':
482 case 'video/quicktime':
486 case 'text/javascript':
500 $fileParts = explode(
'.', $fileName);
501 if (is_array($fileParts)) {
502 $extension = $fileParts[count($fileParts) - 1];
523 $niceFileSizeUnits = array(
'B',
'KB',
'MB',
'GB');
524 for($i = 0; $i < 4 && $size > 1024; $i++) {
527 return $size . $niceFileSizeUnits[$i];
540 return chmod($path, $mask & ~$umask);
549 $fileParts = explode(
'.', $fileName);
550 if (is_array($fileParts)) {
551 $fileExtension = $fileParts[count($fileParts) - 1];
555 if (!isset($fileExtension) || stristr($fileExtension,
'php') || strlen($fileExtension) > 6 || !preg_match(
'/^\w+$/', $fileExtension)) {
556 $fileExtension =
'txt';
560 if (strtolower(substr($fileName, -7)) ==
'.tar.gz') {
561 $fileExtension = substr($fileName, -6);
564 return $fileExtension;
573 return $this->_executeGzip($filePath,
true);
582 return $this->_executeGzip($filePath,
false);
596 private function _executeGzip($filePath, $decompress =
false) {
599 if (!is_executable($gzipPath)) {
600 throw new Exception(__(
'admin.error.executingUtil', array(
'utilPath' => $gzipPath,
'utilVar' =>
'gzip')));
602 $gzipCmd = escapeshellarg($gzipPath);
603 if ($decompress) $gzipCmd .=
' -d';
605 $output = array($filePath);
607 $gzipCmd .=
' ' . escapeshellarg($filePath);
612 exec($gzipCmd, $output, $returnValue);
613 if ($returnValue > 0) {
614 throw new Exception(__(
'admin.error.utilExecutionProblem', array(
'utilPath' => $gzipPath,
'output' => implode(PHP_EOL, $output))));
617 return substr($filePath, 0, -3);
619 return $filePath .
'.gz';