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

/modules/storecommander/ead6f6fce09/SC/lib/js/ckeditor/kcfinder/lib/helper_file.php

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