PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branding/default/js/ckeditor/kcfinder/lib/helper_file.php

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 202 lines | 139 code | 14 blank | 49 comment | 8 complexity | 9aa699a6154b905b94198e3a11e3c1be MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc File helper class
  5. * @package KCFinder
  6. * @version {version}
  7. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  8. * @copyright 2010 KCFinder Project
  9. * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  10. * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. class file {
  14. static $MIME = array(
  15. 'ai' => 'application/postscript',
  16. 'aif' => 'audio/x-aiff',
  17. 'aifc' => 'audio/x-aiff',
  18. 'aiff' => 'audio/x-aiff',
  19. 'avi' => 'video/x-msvideo',
  20. 'bin' => 'application/macbinary',
  21. 'bmp' => 'image/bmp',
  22. 'cpt' => 'application/mac-compactpro',
  23. 'css' => 'text/css',
  24. 'csv' => 'text/x-comma-separated-values',
  25. 'dcr' => 'application/x-director',
  26. 'dir' => 'application/x-director',
  27. 'doc' => 'application/msword',
  28. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  29. 'dvi' => 'application/x-dvi',
  30. 'dxr' => 'application/x-director',
  31. 'eml' => 'message/rfc822',
  32. 'eps' => 'application/postscript',
  33. 'flv' => 'video/x-flv',
  34. 'gif' => 'image/gif',
  35. 'gtar' => 'application/x-gtar',
  36. 'gz' => 'application/x-gzip',
  37. 'hqx' => 'application/mac-binhex40',
  38. 'htm' => 'text/html',
  39. 'html' => 'text/html',
  40. 'jpe' => 'image/jpeg',
  41. 'jpeg' => 'image/jpeg',
  42. 'jpg' => 'image/jpeg',
  43. 'js' => 'application/x-javascript',
  44. 'log' => 'text/plain',
  45. 'mid' => 'audio/midi',
  46. 'midi' => 'audio/midi',
  47. 'mif' => 'application/vnd.mif',
  48. 'mov' => 'video/quicktime',
  49. 'movie' => 'video/x-sgi-movie',
  50. 'mp2' => 'audio/mpeg',
  51. 'mp3' => 'audio/mpeg',
  52. 'mpe' => 'video/mpeg',
  53. 'mpeg' => 'video/mpeg',
  54. 'mpg' => 'video/mpeg',
  55. 'mpga' => 'audio/mpeg',
  56. 'oda' => 'application/oda',
  57. 'pdf' => 'application/pdf',
  58. 'php' => 'application/x-httpd-php',
  59. 'php3' => 'application/x-httpd-php',
  60. 'php4' => 'application/x-httpd-php',
  61. 'phps' => 'application/x-httpd-php-source',
  62. 'phtml' => 'application/x-httpd-php',
  63. 'png' => 'image/png',
  64. 'ppt' => 'application/powerpoint',
  65. 'ps' => 'application/postscript',
  66. 'psd' => 'application/x-photoshop',
  67. 'qt' => 'video/quicktime',
  68. 'ra' => 'audio/x-realaudio',
  69. 'ram' => 'audio/x-pn-realaudio',
  70. 'rm' => 'audio/x-pn-realaudio',
  71. 'rpm' => 'audio/x-pn-realaudio-plugin',
  72. 'rtf' => 'text/rtf',
  73. 'rtx' => 'text/richtext',
  74. 'rv' => 'video/vnd.rn-realvideo',
  75. 'shtml' => 'text/html',
  76. 'sit' => 'application/x-stuffit',
  77. 'smi' => 'application/smil',
  78. 'smil' => 'application/smil',
  79. 'swf' => 'application/x-shockwave-flash',
  80. 'tar' => 'application/x-tar',
  81. 'tgz' => 'application/x-tar',
  82. 'text' => 'text/plain',
  83. 'tif' => 'image/tiff',
  84. 'tiff' => 'image/tiff',
  85. 'txt' => 'text/plain',
  86. 'wav' => 'audio/x-wav',
  87. 'wbxml' => 'application/wbxml',
  88. 'wmlc' => 'application/wmlc',
  89. 'word' => 'application/msword',
  90. 'xht' => 'application/xhtml+xml',
  91. 'xhtml' => 'application/xhtml+xml',
  92. 'xl' => 'application/excel',
  93. 'xls' => 'application/excel',
  94. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  95. 'xml' => 'text/xml',
  96. 'xsl' => 'text/xml',
  97. 'zip' => 'application/x-zip'
  98. );
  99. /** Checks if the given file is really writable. The standard PHP function
  100. * is_writable() does not work properly on Windows servers.
  101. * @param string $dir
  102. * @return bool */
  103. static function isWritable($filename) {
  104. $filename = path::normalize($filename);
  105. if (!is_file($filename) || (false === ($fp = @fopen($filename, 'a+'))))
  106. return false;
  107. fclose($fp);
  108. return true;
  109. }
  110. /** Get the extension from filename
  111. * @param string $file
  112. * @param bool $toLower
  113. * @return string */
  114. static function getExtension($filename, $toLower=true) {
  115. return preg_match('/^.*\.([^\.]*)$/s', $filename, $patt)
  116. ? ($toLower ? strtolower($patt[1]) : $patt[1]) : "";
  117. }
  118. /** Get MIME type of the given filename. If Fileinfo PHP extension is
  119. * available the MIME type will be fetched by the file's content. The
  120. * second parameter is optional and defines the magic file path. If you
  121. * skip it, the default one will be loaded.
  122. * If Fileinfo PHP extension is not available the MIME type will be fetched
  123. * by filename extension regarding $MIME property. If the file extension
  124. * does not exist there, returned type will be application/octet-stream
  125. * @param string $filename
  126. * @param string $magic
  127. * @return string */
  128. static function getMimeType($filename, $magic=null) {
  129. if (class_exists("finfo")) {
  130. $finfo = ($magic === null)
  131. ? new finfo(FILEINFO_MIME)
  132. : 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. }
  188. ?>