PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/la/admin3120tsmcd/filemanager/include/utils.php

https://gitlab.com/elasa/shop2.elasa.ir
PHP | 313 lines | 296 code | 17 blank | 0 comment | 39 complexity | cd441a1e0976ebd6e25ce16ad7353d8e MD5 | raw file
  1. <?php
  2. if($_SESSION["verify"] != "RESPONSIVEfilemanager") die('forbiden');
  3. function deleteDir($dir) {
  4. if (!file_exists($dir)) return true;
  5. if (!is_dir($dir)) return unlink($dir);
  6. foreach (scandir($dir) as $item) {
  7. if ($item == '.' || $item == '..') continue;
  8. if (!deleteDir($dir.DIRECTORY_SEPARATOR.$item)) return false;
  9. }
  10. return rmdir($dir);
  11. }
  12. function duplicate_file($old_path,$name){
  13. if(file_exists($old_path)){
  14. $info=pathinfo($old_path);
  15. $new_path=$info['dirname']."/".$name.".".$info['extension'];
  16. if(file_exists($new_path)) return false;
  17. return copy($old_path,$new_path);
  18. }
  19. }
  20. function rename_file($old_path,$name,$transliteration){
  21. $name=fix_filename($name,$transliteration);
  22. if(file_exists($old_path)){
  23. $info=pathinfo($old_path);
  24. $new_path=$info['dirname']."/".$name.".".$info['extension'];
  25. if(file_exists($new_path)) return false;
  26. return rename($old_path,$new_path);
  27. }
  28. }
  29. function rename_folder($old_path,$name,$transliteration){
  30. $name=fix_filename($name,$transliteration);
  31. if(file_exists($old_path)){
  32. $new_path=fix_dirname($old_path)."/".$name;
  33. if(file_exists($new_path)) return false;
  34. return rename($old_path,$new_path);
  35. }
  36. }
  37. function create_img_gd($imgfile, $imgthumb, $newwidth, $newheight="") {
  38. if(image_check_memory_usage($imgfile,$newwidth,$newheight)){
  39. require_once('php_image_magician.php');
  40. $magicianObj = new imageLib($imgfile);
  41. $magicianObj -> resizeImage($newwidth, $newheight, 'crop');
  42. $magicianObj -> saveImage($imgthumb,80);
  43. return true;
  44. }
  45. return false;
  46. }
  47. function create_img($imgfile, $imgthumb, $newwidth, $newheight="") {
  48. if(image_check_memory_usage($imgfile,$newwidth,$newheight)){
  49. require_once('php_image_magician.php');
  50. $magicianObj = new imageLib($imgfile);
  51. $magicianObj -> resizeImage($newwidth, $newheight, 'auto');
  52. $magicianObj -> saveImage($imgthumb,80);
  53. return true;
  54. }else{
  55. return false;
  56. }
  57. }
  58. function makeSize($size) {
  59. $units = array('B','KB','MB','GB','TB');
  60. $u = 0;
  61. while ( (round($size / 1024) > 0) && ($u < 4) ) {
  62. $size = $size / 1024;
  63. $u++;
  64. }
  65. return (number_format($size, 0) . " " . $units[$u]);
  66. }
  67. function foldersize($path) {
  68. $total_size = 0;
  69. $files = scandir($path);
  70. $cleanPath = rtrim($path, '/'). '/';
  71. foreach($files as $t) {
  72. if ($t<>"." && $t<>"..") {
  73. $currentFile = $cleanPath . $t;
  74. if (is_dir($currentFile)) {
  75. $size = foldersize($currentFile);
  76. $total_size += $size;
  77. }
  78. else {
  79. $size = filesize($currentFile);
  80. $total_size += $size;
  81. }
  82. }
  83. }
  84. return $total_size;
  85. }
  86. function create_folder($path=false,$path_thumbs=false){
  87. $oldumask = umask(0);
  88. if ($path && !file_exists($path))
  89. mkdir($path, 0777, true); // or even 01777 so you get the sticky bit set
  90. if($path_thumbs && !file_exists($path_thumbs))
  91. mkdir($path_thumbs, 0777, true) or die("$path_thumbs cannot be found"); // or even 01777 so you get the sticky bit set
  92. umask($oldumask);
  93. }
  94. function check_files_extensions_on_path($path,$ext){
  95. if(!is_dir($path)){
  96. $fileinfo = pathinfo($path);
  97. if (function_exists('mb_strtolower'))
  98. if(!in_array(mb_strtolower($fileinfo['extension']),$ext))
  99. unlink($path);
  100. else
  101. if(!in_array(Tools::strtolower($fileinfo['extension']),$ext))
  102. unlink($path);
  103. }else{
  104. $files = scandir($path);
  105. foreach($files as $file){
  106. check_files_extensions_on_path(trim($path,'/')."/".$file,$ext);
  107. }
  108. }
  109. }
  110. function check_files_extensions_on_phar( $phar, &$files, $basepath, $ext ) {
  111. foreach( $phar as $file )
  112. {
  113. if( $file->isFile() )
  114. {
  115. if (function_exists('mb_strtolower'))
  116. if(in_array(mb_strtolower($file->getExtension()),$ext))
  117. $files[] = $basepath.$file->getFileName( );
  118. else
  119. if(in_array(Tools::strtolower($file->getExtension()),$ext))
  120. $files[] = $basepath.$file->getFileName( );
  121. }
  122. else if( $file->isDir() )
  123. {
  124. $iterator = new DirectoryIterator( $file );
  125. check_files_extensions_on_phar($iterator, $files, $basepath.$file->getFileName().'/', $ext);
  126. }
  127. }
  128. }
  129. function fix_filename($str,$transliteration){
  130. if($transliteration){
  131. if( function_exists( 'transliterator_transliterate' ) )
  132. {
  133. $str = transliterator_transliterate( 'Accents-Any', $str );
  134. }
  135. else
  136. {
  137. $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
  138. }
  139. $str = preg_replace( "/[^a-zA-Z0-9\.\[\]_| -]/", '', $str );
  140. }
  141. $str=str_replace(array('"',"'","/","\\"),"",$str);
  142. $str=strip_tags($str);
  143. // Empty or incorrectly transliterated filename.
  144. // Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
  145. // So we add that default 'file' name to fix that issue.
  146. if( strpos( $str, '.' ) === 0 )
  147. {
  148. $str = 'file'.$str;
  149. }
  150. return trim( $str );
  151. }
  152. function fix_dirname($str){
  153. return str_replace('~',' ',dirname(str_replace(' ','~',$str)));
  154. }
  155. function fix_strtoupper($str){
  156. if( function_exists( 'mb_strtoupper' ) )
  157. return mb_strtoupper($str);
  158. else
  159. return strtoupper($str);
  160. }
  161. function fix_strtolower($str){
  162. if( function_exists( 'mb_strtoupper' ) )
  163. return mb_strtolower($str);
  164. else
  165. return strtolower($str);
  166. }
  167. function fix_path($path,$transliteration){
  168. $info=pathinfo($path);
  169. if (($s = strrpos($path, '/')) !== false) $s++;
  170. if (($e = strrpos($path, '.') - $s) !== strlen($info['filename']))
  171. {
  172. $info['filename'] = substr($path, $s, $e);
  173. $info['basename'] = substr($path, $s);
  174. }
  175. $tmp_path = $info['dirname'].DIRECTORY_SEPARATOR.$info['basename'];
  176. $str=fix_filename($info['filename'],$transliteration);
  177. if($tmp_path!="")
  178. return $tmp_path.DIRECTORY_SEPARATOR.$str;
  179. else
  180. return $str;
  181. }
  182. function base_url(){
  183. return sprintf(
  184. "%s://%s",
  185. isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
  186. $_SERVER['HTTP_HOST']
  187. );
  188. }
  189. function config_loading($current_path,$fld){
  190. if(file_exists($current_path.$fld.".config")){
  191. require_once($current_path.$fld.".config");
  192. return true;
  193. }
  194. echo "!!!!".$parent=fix_dirname($fld);
  195. if($parent!="." && !empty($parent)){
  196. config_loading($current_path,$parent);
  197. }
  198. return false;
  199. }
  200. function image_check_memory_usage($img, $max_breedte, $max_hoogte){
  201. if(file_exists($img)){
  202. $K64 = 65536; // number of bytes in 64K
  203. $memory_usage = memory_get_usage();
  204. $memory_limit = abs(intval(str_replace('M','',ini_get('memory_limit'))*1024*1024));
  205. $image_properties = getimagesize($img);
  206. $image_width = $image_properties[0];
  207. $image_height = $image_properties[1];
  208. $image_bits = $image_properties['bits'];
  209. $image_memory_usage = $K64 + ($image_width * $image_height * ($image_bits ) * 2);
  210. $thumb_memory_usage = $K64 + ($max_breedte * $max_hoogte * ($image_bits ) * 2);
  211. $memory_needed = intval($memory_usage + $image_memory_usage + $thumb_memory_usage);
  212. if($memory_needed > $memory_limit){
  213. ini_set('memory_limit',(intval($memory_needed/1024/1024)+5) . 'M');
  214. if(ini_get('memory_limit') == (intval($memory_needed/1024/1024)+5) . 'M'){
  215. return true;
  216. }else{
  217. return false;
  218. }
  219. }else{
  220. return true;
  221. }
  222. }else{
  223. return false;
  224. }
  225. }
  226. function endsWith($haystack, $needle)
  227. {
  228. return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
  229. }
  230. function new_thumbnails_creation($targetPath,$targetFile,$name,$current_path,$relative_image_creation,$relative_path_from_current_pos,$relative_image_creation_name_to_prepend,$relative_image_creation_name_to_append,$relative_image_creation_width,$relative_image_creation_height,$fixed_image_creation,$fixed_path_from_filemanager,$fixed_image_creation_name_to_prepend,$fixed_image_creation_to_append,$fixed_image_creation_width,$fixed_image_creation_height){
  231. //create relative thumbs
  232. $all_ok=true;
  233. if($relative_image_creation){
  234. foreach($relative_path_from_current_pos as $k=>$path){
  235. if($path!="" && $path[strlen($path)-1]!="/") $path.="/";
  236. if (!file_exists($targetPath.$path)) create_folder($targetPath.$path,false);
  237. $info=pathinfo($name);
  238. if(!endsWith($targetPath,$path))
  239. if(!create_img($targetFile, $targetPath.$path.$relative_image_creation_name_to_prepend[$k].$info['filename'].$relative_image_creation_name_to_append[$k].".".$info['extension'], $relative_image_creation_width[$k], $relative_image_creation_height[$k]))
  240. $all_ok=false;
  241. }
  242. }
  243. //create fixed thumbs
  244. if($fixed_image_creation){
  245. foreach($fixed_path_from_filemanager as $k=>$path){
  246. if($path!="" && $path[strlen($path)-1]!="/") $path.="/";
  247. $base_dir=$path.substr_replace($targetPath, '', 0, strlen($current_path));
  248. if (!file_exists($base_dir)) create_folder($base_dir,false);
  249. $info=pathinfo($name);
  250. if(!create_img($targetFile, $base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension'], $fixed_image_creation_width[$k], $fixed_image_creation_height[$k]))
  251. $all_ok=false;
  252. }
  253. }
  254. return $all_ok;
  255. }
  256. // Get a remote file, using whichever mechanism is enabled
  257. function get_file_by_url($url) {
  258. if (ini_get('allow_url_fopen')) {
  259. return file_get_contents($url);
  260. }
  261. if (!function_exists('curl_version')) {
  262. return false;
  263. }
  264. $ch = curl_init();
  265. curl_setopt($ch, CURLOPT_HEADER, 0);
  266. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  267. curl_setopt($ch, CURLOPT_URL, $url);
  268. $data = curl_exec($ch);
  269. curl_close($ch);
  270. return $data;
  271. }
  272. ?>