PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/kcfinder/lib/helper_file.php

https://gitlab.com/AhmedMedo/Seo
PHP | 216 lines | 146 code | 17 blank | 53 comment | 9 complexity | f534b4229f1040d26cad4cfc540ab2a6 MD5 | raw file
  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)
  118. ? ($toLower ? strtolower($patt[1]) : $patt[1]) : "";
  119. }
  120. /** Get MIME type of the given filename. If Fileinfo PHP extension is
  121. * available the MIME type will be fetched by the file's content. The
  122. * second parameter is optional and defines the magic file path. If you
  123. * skip it, the default one will be loaded.
  124. * If Fileinfo PHP extension is not available the MIME type will be fetched
  125. * by filename extension regarding $MIME property. If the file extension
  126. * does not exist there, returned type will be application/octet-stream
  127. * @param string $filename
  128. * @param string $magic
  129. * @return string */
  130. static function getMimeType($filename, $magic=null) {
  131. if (class_exists("finfo")) {
  132. $finfo = new \finfo(FILEINFO_MIME, $magic);
  133. if ($finfo) {
  134. $mime = $finfo->file($filename);
  135. $mime = substr($mime, 0, strrpos($mime, ";"));
  136. return $mime;
  137. }
  138. }
  139. $ext = self::getExtension($filename, true);
  140. return isset(self::$MIME[$ext]) ? self::$MIME[$ext] : "application/octet-stream";
  141. }
  142. /** Get inexistant filename based on the given filename. If you skip $dir
  143. * parameter the directory will be fetched from $filename and returned
  144. * value will be full filename path. The third parameter is optional and
  145. * defines the template, the filename will be renamed to. Default template
  146. * is {name}({sufix}){ext}. Examples:
  147. *
  148. * file::getInexistantFilename("/my/directory/myfile.txt");
  149. * If myfile.txt does not exist - returns the same path to the file
  150. * otherwise returns "/my/directory/myfile(1).txt"
  151. *
  152. * file::getInexistantFilename("myfile.txt", "/my/directory");
  153. * returns "myfile.txt" or "myfile(1).txt" or "myfile(2).txt" etc...
  154. *
  155. * file::getInexistantFilename("myfile.txt", "/dir", "{name}[{sufix}]{ext}");
  156. * returns "myfile.txt" or "myfile[1].txt" or "myfile[2].txt" etc...
  157. *
  158. * @param string $filename
  159. * @param string $dir
  160. * @param string $tpl
  161. * @return string */
  162. static function getInexistantFilename($filename, $dir=null, $tpl=null) {
  163. if ($tpl === null) $tpl = "{name}({sufix}){ext}";
  164. $fullPath = ($dir === null);
  165. if ($fullPath)
  166. $dir = path::normalize(dirname($filename));
  167. else {
  168. $fdir = dirname($filename);
  169. $dir = strlen($fdir)
  170. ? path::normalize("$dir/$fdir")
  171. : path::normalize($dir);
  172. }
  173. $filename = basename($filename);
  174. $ext = self::getExtension($filename, false);
  175. $name = strlen($ext) ? substr($filename, 0, -strlen($ext) - 1) : $filename;
  176. $tpl = str_replace('{name}', $name, $tpl);
  177. $tpl = str_replace('{ext}', (strlen($ext) ? ".$ext" : ""), $tpl);
  178. $i = 1; $file = "$dir/$filename";
  179. while (file_exists($file))
  180. $file = "$dir/" . str_replace('{sufix}', $i++, $tpl);
  181. return $fullPath
  182. ? $file
  183. : (strlen($fdir)
  184. ? "$fdir/" . basename($file)
  185. : basename($file));
  186. }
  187. /** Normalize given filename. Accented characters becomes non-accented and
  188. * removes any other special characters. Usable for non-unicode filesystems
  189. * @param $filename
  190. * @return string */
  191. static function normalizeFilename($filename) {
  192. $string = htmlentities($filename, ENT_QUOTES, 'UTF-8');
  193. if (strpos($string, '&') !== false)
  194. $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');
  195. $filename = trim(preg_replace('~[^0-9a-z\.\- ]~i', "_", $filename));
  196. return $filename;
  197. }
  198. }
  199. ?>