[239] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | /** This file is part of KCFinder project
|
---|
| 4 | *
|
---|
| 5 | * @desc File helper class
|
---|
| 6 | * @package KCFinder
|
---|
| 7 | * @version 2.51
|
---|
| 8 | * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
|
---|
| 9 | * @copyright 2010, 2011 KCFinder Project
|
---|
| 10 | * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
|
---|
| 11 | * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
|
---|
| 12 | * @link http://kcfinder.sunhater.com
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | class file {
|
---|
| 16 |
|
---|
| 17 | static $MIME = array(
|
---|
| 18 | 'ai' => 'application/postscript',
|
---|
| 19 | 'aif' => 'audio/x-aiff',
|
---|
| 20 | 'aifc' => 'audio/x-aiff',
|
---|
| 21 | 'aiff' => 'audio/x-aiff',
|
---|
| 22 | 'avi' => 'video/x-msvideo',
|
---|
| 23 | 'bin' => 'application/macbinary',
|
---|
| 24 | 'bmp' => 'image/bmp',
|
---|
| 25 | 'cpt' => 'application/mac-compactpro',
|
---|
| 26 | 'css' => 'text/css',
|
---|
| 27 | 'csv' => 'text/x-comma-separated-values',
|
---|
| 28 | 'dcr' => 'application/x-director',
|
---|
| 29 | 'dir' => 'application/x-director',
|
---|
| 30 | 'doc' => 'application/msword',
|
---|
| 31 | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
---|
| 32 | 'dvi' => 'application/x-dvi',
|
---|
| 33 | 'dxr' => 'application/x-director',
|
---|
| 34 | 'eml' => 'message/rfc822',
|
---|
| 35 | 'eps' => 'application/postscript',
|
---|
| 36 | 'flv' => 'video/x-flv',
|
---|
| 37 | 'gif' => 'image/gif',
|
---|
| 38 | 'gtar' => 'application/x-gtar',
|
---|
| 39 | 'gz' => 'application/x-gzip',
|
---|
| 40 | 'hqx' => 'application/mac-binhex40',
|
---|
| 41 | 'htm' => 'text/html',
|
---|
| 42 | 'html' => 'text/html',
|
---|
| 43 | 'jpe' => 'image/jpeg',
|
---|
| 44 | 'jpeg' => 'image/jpeg',
|
---|
| 45 | 'jpg' => 'image/jpeg',
|
---|
| 46 | 'js' => 'application/x-javascript',
|
---|
| 47 | 'log' => 'text/plain',
|
---|
| 48 | 'mid' => 'audio/midi',
|
---|
| 49 | 'midi' => 'audio/midi',
|
---|
| 50 | 'mif' => 'application/vnd.mif',
|
---|
| 51 | 'mov' => 'video/quicktime',
|
---|
| 52 | 'movie' => 'video/x-sgi-movie',
|
---|
| 53 | 'mp2' => 'audio/mpeg',
|
---|
| 54 | 'mp3' => 'audio/mpeg',
|
---|
| 55 | 'mpe' => 'video/mpeg',
|
---|
| 56 | 'mpeg' => 'video/mpeg',
|
---|
| 57 | 'mpg' => 'video/mpeg',
|
---|
| 58 | 'mpga' => 'audio/mpeg',
|
---|
| 59 | 'oda' => 'application/oda',
|
---|
| 60 | 'pdf' => 'application/pdf',
|
---|
| 61 | 'php' => 'application/x-httpd-php',
|
---|
| 62 | 'php3' => 'application/x-httpd-php',
|
---|
| 63 | 'php4' => 'application/x-httpd-php',
|
---|
| 64 | 'phps' => 'application/x-httpd-php-source',
|
---|
| 65 | 'phtml' => 'application/x-httpd-php',
|
---|
| 66 | 'png' => 'image/png',
|
---|
| 67 | 'ppt' => 'application/powerpoint',
|
---|
| 68 | 'ps' => 'application/postscript',
|
---|
| 69 | 'psd' => 'application/x-photoshop',
|
---|
| 70 | 'qt' => 'video/quicktime',
|
---|
| 71 | 'ra' => 'audio/x-realaudio',
|
---|
| 72 | 'ram' => 'audio/x-pn-realaudio',
|
---|
| 73 | 'rm' => 'audio/x-pn-realaudio',
|
---|
| 74 | 'rpm' => 'audio/x-pn-realaudio-plugin',
|
---|
| 75 | 'rtf' => 'text/rtf',
|
---|
| 76 | 'rtx' => 'text/richtext',
|
---|
| 77 | 'rv' => 'video/vnd.rn-realvideo',
|
---|
| 78 | 'shtml' => 'text/html',
|
---|
| 79 | 'sit' => 'application/x-stuffit',
|
---|
| 80 | 'smi' => 'application/smil',
|
---|
| 81 | 'smil' => 'application/smil',
|
---|
| 82 | 'swf' => 'application/x-shockwave-flash',
|
---|
| 83 | 'tar' => 'application/x-tar',
|
---|
| 84 | 'tgz' => 'application/x-tar',
|
---|
| 85 | 'text' => 'text/plain',
|
---|
| 86 | 'tif' => 'image/tiff',
|
---|
| 87 | 'tiff' => 'image/tiff',
|
---|
| 88 | 'txt' => 'text/plain',
|
---|
| 89 | 'wav' => 'audio/x-wav',
|
---|
| 90 | 'wbxml' => 'application/wbxml',
|
---|
| 91 | 'wmlc' => 'application/wmlc',
|
---|
| 92 | 'word' => 'application/msword',
|
---|
| 93 | 'xht' => 'application/xhtml+xml',
|
---|
| 94 | 'xhtml' => 'application/xhtml+xml',
|
---|
| 95 | 'xl' => 'application/excel',
|
---|
| 96 | 'xls' => 'application/excel',
|
---|
| 97 | 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
---|
| 98 | 'xml' => 'text/xml',
|
---|
| 99 | 'xsl' => 'text/xml',
|
---|
| 100 | 'zip' => 'application/x-zip'
|
---|
| 101 | );
|
---|
| 102 |
|
---|
| 103 | /** Checks if the given file is really writable. The standard PHP function
|
---|
| 104 | * is_writable() does not work properly on Windows servers.
|
---|
| 105 | * @param string $dir
|
---|
| 106 | * @return bool */
|
---|
| 107 |
|
---|
| 108 | static function isWritable($filename) {
|
---|
| 109 | $filename = path::normalize($filename);
|
---|
| 110 | if (!is_file($filename) || (false === ($fp = @fopen($filename, 'a+'))))
|
---|
| 111 | return false;
|
---|
| 112 | fclose($fp);
|
---|
| 113 | return true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Get the extension from filename
|
---|
| 117 | * @param string $file
|
---|
| 118 | * @param bool $toLower
|
---|
| 119 | * @return string */
|
---|
| 120 |
|
---|
| 121 | static function getExtension($filename, $toLower=true) {
|
---|
| 122 | return preg_match('/^.*\.([^\.]*)$/s', $filename, $patt)
|
---|
| 123 | ? ($toLower ? strtolower($patt[1]) : $patt[1]) : "";
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Get MIME type of the given filename. If Fileinfo PHP extension is
|
---|
| 127 | * available the MIME type will be fetched by the file's content. The
|
---|
| 128 | * second parameter is optional and defines the magic file path. If you
|
---|
| 129 | * skip it, the default one will be loaded.
|
---|
| 130 | * If Fileinfo PHP extension is not available the MIME type will be fetched
|
---|
| 131 | * by filename extension regarding $MIME property. If the file extension
|
---|
| 132 | * does not exist there, returned type will be application/octet-stream
|
---|
| 133 | * @param string $filename
|
---|
| 134 | * @param string $magic
|
---|
| 135 | * @return string */
|
---|
| 136 |
|
---|
| 137 | static function getMimeType($filename, $magic=null) {
|
---|
| 138 | if (class_exists("finfo")) {
|
---|
| 139 | $finfo = ($magic === null)
|
---|
| 140 | ? new finfo(FILEINFO_MIME)
|
---|
| 141 | : new finfo(FILEINFO_MIME, $magic);
|
---|
| 142 | if ($finfo) {
|
---|
| 143 | $mime = $finfo->file($filename);
|
---|
| 144 | $mime = substr($mime, 0, strrpos($mime, ";"));
|
---|
| 145 | return $mime;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | $ext = self::getExtension($filename, true);
|
---|
| 149 | return isset(self::$MIME[$ext]) ? self::$MIME[$ext] : "application/octet-stream";
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /** Get inexistant filename based on the given filename. If you skip $dir
|
---|
| 153 | * parameter the directory will be fetched from $filename and returned
|
---|
| 154 | * value will be full filename path. The third parameter is optional and
|
---|
| 155 | * defines the template, the filename will be renamed to. Default template
|
---|
| 156 | * is {name}({sufix}){ext}. Examples:
|
---|
| 157 | *
|
---|
| 158 | * file::getInexistantFilename("/my/directory/myfile.txt");
|
---|
| 159 | * If myfile.txt does not exist - returns the same path to the file
|
---|
| 160 | * otherwise returns "/my/directory/myfile(1).txt"
|
---|
| 161 | *
|
---|
| 162 | * file::getInexistantFilename("myfile.txt", "/my/directory");
|
---|
| 163 | * returns "myfile.txt" or "myfile(1).txt" or "myfile(2).txt" etc...
|
---|
| 164 | *
|
---|
| 165 | * file::getInexistantFilename("myfile.txt", "/dir", "{name}[{sufix}]{ext}");
|
---|
| 166 | * returns "myfile.txt" or "myfile[1].txt" or "myfile[2].txt" etc...
|
---|
| 167 | *
|
---|
| 168 | * @param string $filename
|
---|
| 169 | * @param string $dir
|
---|
| 170 | * @param string $tpl
|
---|
| 171 | * @return string */
|
---|
| 172 |
|
---|
| 173 | static function getInexistantFilename($filename, $dir=null, $tpl=null) {
|
---|
| 174 | if ($tpl === null) $tpl = "{name}({sufix}){ext}";
|
---|
| 175 | $fullPath = ($dir === null);
|
---|
| 176 | if ($fullPath)
|
---|
| 177 | $dir = path::normalize(dirname($filename));
|
---|
| 178 | else {
|
---|
| 179 | $fdir = dirname($filename);
|
---|
| 180 | $dir = strlen($fdir)
|
---|
| 181 | ? path::normalize("$dir/$fdir")
|
---|
| 182 | : path::normalize($dir);
|
---|
| 183 | }
|
---|
| 184 | $filename = basename($filename);
|
---|
| 185 | $ext = self::getExtension($filename, false);
|
---|
| 186 | $name = strlen($ext) ? substr($filename, 0, -strlen($ext) - 1) : $filename;
|
---|
| 187 | $tpl = str_replace('{name}', $name, $tpl);
|
---|
| 188 | $tpl = str_replace('{ext}', (strlen($ext) ? ".$ext" : ""), $tpl);
|
---|
| 189 | $i = 1; $file = "$dir/$filename";
|
---|
| 190 | while (file_exists($file))
|
---|
| 191 | $file = "$dir/" . str_replace('{sufix}', $i++, $tpl);
|
---|
| 192 |
|
---|
| 193 | return $fullPath
|
---|
| 194 | ? $file
|
---|
| 195 | : (strlen($fdir)
|
---|
| 196 | ? "$fdir/" . basename($file)
|
---|
| 197 | : basename($file));
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | ?> |
---|