helper_file.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc File helper class
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Pavel Tzonkov <sunhater@sunhater.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. namespace kcfinder;
  14. class file {
  15. static $MIME = array(
  16. 'ai' => 'application/postscript',
  17. 'aif' => 'audio/x-aiff',
  18. 'aifc' => 'audio/x-aiff',
  19. 'aiff' => 'audio/x-aiff',
  20. 'avi' => 'video/x-msvideo',
  21. 'bin' => 'application/macbinary',
  22. 'bmp' => 'image/bmp',
  23. 'cpt' => 'application/mac-compactpro',
  24. 'css' => 'text/css',
  25. 'csv' => 'text/x-comma-separated-values',
  26. 'dcr' => 'application/x-director',
  27. 'dir' => 'application/x-director',
  28. 'doc' => 'application/msword',
  29. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  30. 'dvi' => 'application/x-dvi',
  31. 'dxr' => 'application/x-director',
  32. 'eml' => 'message/rfc822',
  33. 'eps' => 'application/postscript',
  34. 'flv' => 'video/x-flv',
  35. 'gif' => 'image/gif',
  36. 'gtar' => 'application/x-gtar',
  37. 'gz' => 'application/x-gzip',
  38. 'hqx' => 'application/mac-binhex40',
  39. 'htm' => 'text/html',
  40. 'html' => 'text/html',
  41. 'jpe' => 'image/jpeg',
  42. 'jpeg' => 'image/jpeg',
  43. 'jpg' => 'image/jpeg',
  44. 'js' => 'application/x-javascript',
  45. 'log' => 'text/plain',
  46. 'mid' => 'audio/midi',
  47. 'midi' => 'audio/midi',
  48. 'mif' => 'application/vnd.mif',
  49. 'mov' => 'video/quicktime',
  50. 'movie' => 'video/x-sgi-movie',
  51. 'mp2' => 'audio/mpeg',
  52. 'mp3' => 'audio/mpeg',
  53. 'mp4' => 'video/mpeg',
  54. 'mpe' => 'video/mpeg',
  55. 'mpeg' => 'video/mpeg',
  56. 'mpg' => 'video/mpeg',
  57. 'mpga' => 'audio/mpeg',
  58. 'oda' => 'application/oda',
  59. 'pdf' => 'application/pdf',
  60. 'php' => 'application/x-httpd-php',
  61. 'php3' => 'application/x-httpd-php',
  62. 'php4' => 'application/x-httpd-php',
  63. 'phps' => 'application/x-httpd-php-source',
  64. 'phtml' => 'application/x-httpd-php',
  65. 'png' => 'image/png',
  66. 'ppt' => 'application/powerpoint',
  67. 'ps' => 'application/postscript',
  68. 'psd' => 'application/x-photoshop',
  69. 'qt' => 'video/quicktime',
  70. 'ra' => 'audio/x-realaudio',
  71. 'ram' => 'audio/x-pn-realaudio',
  72. 'rm' => 'audio/x-pn-realaudio',
  73. 'rpm' => 'audio/x-pn-realaudio-plugin',
  74. 'rtf' => 'text/rtf',
  75. 'rtx' => 'text/richtext',
  76. 'rv' => 'video/vnd.rn-realvideo',
  77. 'shtml' => 'text/html',
  78. 'sit' => 'application/x-stuffit',
  79. 'smi' => 'application/smil',
  80. 'smil' => 'application/smil',
  81. 'swf' => 'application/x-shockwave-flash',
  82. 'tar' => 'application/x-tar',
  83. 'tgz' => 'application/x-tar',
  84. 'text' => 'text/plain',
  85. 'tif' => 'image/tiff',
  86. 'tiff' => 'image/tiff',
  87. 'txt' => 'text/plain',
  88. 'wav' => 'audio/x-wav',
  89. 'wbxml' => 'application/wbxml',
  90. 'wmlc' => 'application/wmlc',
  91. 'word' => 'application/msword',
  92. 'xht' => 'application/xhtml+xml',
  93. 'xhtml' => 'application/xhtml+xml',
  94. 'xl' => 'application/excel',
  95. 'xls' => 'application/excel',
  96. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  97. 'xml' => 'text/xml',
  98. 'xsl' => 'text/xml',
  99. 'zip' => 'application/x-zip'
  100. );
  101. /** Checks if the given file is really writable. The standard PHP function
  102. * is_writable() does not work properly on Windows servers.
  103. * @param string $filename
  104. * @return bool */
  105. static function isWritable($filename) {
  106. $filename = path::normalize($filename);
  107. if (!is_file($filename) || (false === ($fp = @fopen($filename, 'a+'))))
  108. return false;
  109. fclose($fp);
  110. return true;
  111. }
  112. /** Get the extension from filename
  113. * @param string $filename
  114. * @param bool $toLower
  115. * @return string */
  116. static function getExtension($filename, $toLower=true) {
  117. return preg_match('/^.*\.([^\.]*)$/s', $filename, $patt)? ( $toLower ? strtolower($patt[1]) : $patt[1]) : "";
  118. }
  119. /** Get MIME type of the given filename. If Fileinfo PHP extension is
  120. * available the MIME type will be fetched by the file's content. The
  121. * second parameter is optional and defines the magic file path. If you
  122. * skip it, the default one will be loaded.
  123. * If Fileinfo PHP extension is not available the MIME type will be fetched
  124. * by filename extension regarding $MIME property. If the file extension
  125. * does not exist there, returned type will be application/octet-stream
  126. * @param string $filename
  127. * @param string $magic
  128. * @return string */
  129. static function getMimeType($filename, $magic=null) {
  130. if (class_exists("finfo")) {
  131. $finfo = new \finfo(FILEINFO_MIME, $magic);
  132. if ($finfo) {
  133. $mime = $finfo->file($filename);
  134. $mime = substr($mime, 0, strrpos($mime, ";"));
  135. return $mime;
  136. }
  137. }
  138. $ext = self::getExtension($filename, true);
  139. return isset(self::$MIME[$ext]) ? self::$MIME[$ext] : "application/octet-stream";
  140. }
  141. /** Get inexistant filename based on the given filename. If you skip $dir
  142. * parameter the directory will be fetched from $filename and returned
  143. * value will be full filename path. The third parameter is optional and
  144. * defines the template, the filename will be renamed to. Default template
  145. * is {name}({sufix}){ext}. Examples:
  146. *
  147. * file::getInexistantFilename("/my/directory/myfile.txt");
  148. * If myfile.txt does not exist - returns the same path to the file
  149. * otherwise returns "/my/directory/myfile(1).txt"
  150. *
  151. * file::getInexistantFilename("myfile.txt", "/my/directory");
  152. * returns "myfile.txt" or "myfile(1).txt" or "myfile(2).txt" etc...
  153. *
  154. * file::getInexistantFilename("myfile.txt", "/dir", "{name}[{sufix}]{ext}");
  155. * returns "myfile.txt" or "myfile[1].txt" or "myfile[2].txt" etc...
  156. *
  157. * @param string $filename
  158. * @param string $dir
  159. * @param string $tpl
  160. * @return string */
  161. static function getInexistantFilename($filename, $dir=null, $tpl=null) {
  162. if ($tpl === null) $tpl = "{name}({sufix}){ext}";
  163. $fullPath = ($dir === null);
  164. if ($fullPath)
  165. $dir = path::normalize(dirname($filename));
  166. else {
  167. $fdir = dirname($filename);
  168. $dir = strlen($fdir)
  169. ? path::normalize("$dir/$fdir")
  170. : path::normalize($dir);
  171. }
  172. $filename = basename($filename);
  173. $ext = self::getExtension($filename, false);
  174. $name = strlen($ext) ? substr($filename, 0, -strlen($ext) - 1) : $filename;
  175. $tpl = str_replace('{name}', $name, $tpl);
  176. $tpl = str_replace('{ext}', (strlen($ext) ? ".$ext" : ""), $tpl);
  177. $i = 1; $file = "$dir/$filename";
  178. while (file_exists($file))
  179. $file = "$dir/" . str_replace('{sufix}', $i++, $tpl);
  180. return $fullPath
  181. ? $file
  182. : (strlen($fdir)
  183. ? "$fdir/" . basename($file)
  184. : basename($file));
  185. }
  186. /** Normalize given filename. Accented characters becomes non-accented and
  187. * removes any other special characters. Usable for non-unicode filesystems
  188. * @param $filename
  189. * @return string */
  190. static function normalizeFilename($filename) {
  191. $string = htmlentities($filename, ENT_QUOTES, 'UTF-8');
  192. if (strpos($string, '&') !== false)
  193. $filename = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
  194. $filename = trim(preg_replace('~[^0-9a-z\.\- ]~i', "_", $filename));
  195. return $filename;
  196. }
  197. protected function translitIt($str){
  198. $tr = array(
  199. "А"=>"a","Б"=>"b","В"=>"v","Г"=>"g",
  200. "Д"=>"d","Е"=>"e","Ж"=>"j","З"=>"z","И"=>"i",
  201. "Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n",
  202. "О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
  203. "У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
  204. "Ш"=>"sh","Щ"=>"sch","Ъ"=>"","Ы"=>"yi","Ь"=>"",
  205. "Э"=>"e","Ю"=>"yu","Я"=>"ya","а"=>"a","б"=>"b",
  206. "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
  207. "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
  208. "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
  209. "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
  210. "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
  211. "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
  212. " "=> "-", "%"=> "-", "/"=> "-"
  213. );
  214. $urlstr = strtr($str,$tr);
  215. $urlstr = preg_replace('/[^A-Za-z0-9_\-\.]/', '', $urlstr);
  216. return $urlstr;
  217. }
  218. }
  219. ?>