PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/js/AjexFileManager/ajax/php/lib.php

https://github.com/PaulRover08/Active-Fusion
PHP | 442 lines | 346 code | 70 blank | 26 comment | 77 complexity | 29ec743d9d94ca34498d9e1518e68a1b MD5 | raw file
  1. <?php
  2. /**
  3. * Ajex.FileManager
  4. * http://demphest.ru/ajex-filemanager
  5. *
  6. * @version
  7. * 1.0.3
  8. *
  9. * @copyright
  10. * Copyright (C) 2009-2010 Demphest Gorphek
  11. *
  12. * @license
  13. * Dual licensed under the MIT and GPL licenses.
  14. * http://www.opensource.org/licenses/mit-license.php
  15. * http://www.gnu.org/licenses/gpl.html
  16. *
  17. * Ajex.FileManager is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Lesser General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This file is part of Ajex.FileManager.
  23. */
  24. isset($_GET['isWork'])? isWork() : null;
  25. if (isset($_GET['downloadFile'])) {
  26. $file = realpath($cfg['root'] . trim($_GET['downloadFile'], '/\\'));
  27. $file = str_replace('\\', DIR_SEP, $file);
  28. if (empty($file) || false === strpos($file, str_replace('\\', DIR_SEP, $cfg['root']))) {
  29. header("HTTP/1.0 404 Not Found");
  30. exit;
  31. }
  32. $inf = pathinfo($file);
  33. if (!in_array($inf['extension'], $cfg['allow']['file'])) {
  34. header("HTTP/1.0 404 Not Found");
  35. exit;
  36. }
  37. $inf['size'] = filesize($file);
  38. header("Pragma: public");
  39. header("Expires: 0");
  40. header("Cache-Control: private", false);
  41. header("Content-Disposition: attachment; filename=" . urlencode($inf['basename']));
  42. header("Content-Type: application/force-download");
  43. header("Content-Type: application/octet-stream");
  44. header("Content-Type: application/download");
  45. header("Content-Description: File Transfer");
  46. header("Content-Length: " . $inf['size']);
  47. if (50000000 > $inf['size']) {
  48. flush();
  49. $fp = fopen($file, "r");
  50. while (!feof($fp)) {
  51. echo fread($fp, 65536);
  52. flush();
  53. }
  54. fclose($fp);
  55. } else {
  56. readfile($file);
  57. }
  58. exit;
  59. }
  60. function listDirs($dir)
  61. {
  62. global $cfg;
  63. $list = array();
  64. $full = $cfg['root'] . trim($dir, '/\\') . DIR_SEP;
  65. $dirs = scandir($full);
  66. for ($i=-1, $iCount=count($dirs); ++$i<$iCount;) {
  67. if (is_dir($full . $dirs[$i]) && !in_array($dirs[$i], $cfg['hide']['folder'])) {
  68. $stats = getSize($full . $dirs[$i] . DIR_SEP, 'dir');
  69. $list[] = array(
  70. 'title' => $dirs[$i] . ' <i>' . $stats['_size'] . '</i>',
  71. 'key' => urlencode($dir . $dirs[$i]),
  72. 'isLazy' => true,
  73. 'isFolder' => true
  74. );
  75. }
  76. }
  77. return $list;
  78. }
  79. function listFiles($dir)
  80. {
  81. global $cfg;
  82. $list = array();
  83. $full = $cfg['root'] . trim($dir, '/\\') . DIR_SEP;
  84. $thumb = $cfg['root'] . $cfg['thumb']['dir'] . DIR_SEP . trim($dir, '/\\') . DIR_SEP;
  85. $files = scandir($full);
  86. natcasesort($files);
  87. for ($i=-1, $iCount=count($files); ++$i<$iCount;) {
  88. $ext = substr($files[$i], strrpos($files[$i], '.') + 1);
  89. $ext = strtolower($ext);
  90. if (!in_array($files[$i], $cfg['hide']['file']) && !in_array($ext, $cfg['deny'][$cfg['type']]) && in_array($ext, $cfg['allow'][$cfg['type']]) && is_file($full . $files[$i])) {
  91. $imgSize = array(0, 0);
  92. if (in_array($ext, $cfg['allow']['image'])) {
  93. if ($cfg['thumb']['auto'] && !file_exists($thumb . $files[$i])) {
  94. create_thumbnail($full . $files[$i], $thumb . $files[$i]);
  95. }
  96. $imgSize = getimagesize($full . $files[$i]);
  97. }
  98. $stats = getSize($full . $files[$i]);
  99. $list[] = array( 'name' => $files[$i],
  100. 'ext' => $ext,
  101. 'width' => $imgSize[0],
  102. 'height' => $imgSize[1],
  103. 'size' => $stats['_size'],
  104. 'date' => date($cfg['thumb']['date'], $stats['mtime']),
  105. 'r_size' => $stats['size'],
  106. 'mtime' => $stats['mtime'],
  107. 'thumb' => '/' . $cfg['url'] . '/' . $cfg['thumb']['dir'] . '/' . $dir . $files[$i]
  108. );
  109. }
  110. }
  111. switch($cfg['sort']) {
  112. case 'size':
  113. usort($list, 'sortSize');
  114. break;
  115. case 'date':
  116. usort($list, 'sortDate');
  117. break;
  118. default: //name
  119. break;
  120. }
  121. return $list;
  122. }
  123. function dirSize($dirFile)
  124. {
  125. $size = 0;
  126. $dir = opendir($dirFile);
  127. if (!$dir) return false;
  128. while (false !== ($f = readdir($dir))) {
  129. if ($f[0] == '.') continue;
  130. if (is_dir($dirFile . $f)) {
  131. $size += dirSize($dirFile . $f . DIR_SEP);
  132. } else {
  133. $size += filesize($dirFile . $f);
  134. }
  135. }
  136. closedir($dir);
  137. return $size;
  138. }
  139. function getSize($dirFile, $mode = 'file')
  140. {
  141. if ('file' == $mode) {
  142. $stats = stat($dirFile);
  143. } elseif ('dir' == $mode) {
  144. $stats['size'] = dirSize($dirFile);
  145. }
  146. if (empty($stats['size'])) {
  147. $stats['_size'] = '';
  148. } elseif ($stats['size'] < 1024) {
  149. $stats['_size'] .= ' B';
  150. } elseif ($stats['size'] < 1048576) {
  151. $stats['_size'] = round($stats['size'] / 1024) . ' KB';
  152. } else {
  153. $stats['_size'] = round($stats['size'] / 1048576, 2) . ' MB';
  154. }
  155. return $stats;
  156. }
  157. function create_thumbnail($orig_fname, $thum_fname, $thumb_width = null, $thumb_height = null, $quality = null, $do_cut = null, $uploadResize = false)
  158. {
  159. if (!mkdirs(dirname($thum_fname))) {
  160. return false;
  161. }
  162. $size = @getimagesize($orig_fname);
  163. if (false === $size) {
  164. return false;
  165. }
  166. $rgb = 0xFFFFFF;
  167. $src_x = $src_y = 0;
  168. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
  169. $icfunc = 'imagecreatefrom' . $format;
  170. if (!function_exists($icfunc)) {
  171. return false;
  172. }
  173. 0 === $thumb_width? $thumb_width = $size[0] : null;
  174. 0 === $thumb_height? $thumb_height = $size[1] : null;
  175. global $cfg;
  176. null === $thumb_width? $thumb_width = $cfg['thumb']['width'] : null;
  177. null === $thumb_height? $thumb_height = $cfg['thumb']['height']: null;
  178. null === $quality? $quality = $cfg['thumb']['quality'] : null;
  179. null === $do_cut? $do_cut = $cfg['thumb']['cut'] : null;
  180. $path = pathinfo($thum_fname);
  181. if (!is_dir($path['dirname'])) {
  182. $is = mkdir($path['dirname'], $cfg['chmod']['folder']);
  183. if (!$is) {
  184. return false;
  185. }
  186. }
  187. $orig_img = $icfunc($orig_fname);
  188. if (($size[0] <= $thumb_width) && ($size[1] <= $thumb_height)) {
  189. $width = $size[0];
  190. $height = $size[1];
  191. } else {
  192. $width = $thumb_width;
  193. $height = $thumb_height;
  194. $ratio_width = $size[0] / $thumb_width;
  195. $ratio_height = $size[1] / $thumb_height;
  196. if ($ratio_width < $ratio_height) {
  197. if ($do_cut) {
  198. $src_y = ($size[1] - $thumb_height * $ratio_width) / 2;
  199. $size[1] = $thumb_height * $ratio_width;
  200. } else {
  201. $width = $size[0] / $ratio_height;
  202. $height = $thumb_height;
  203. }
  204. } else {
  205. if ($do_cut) {
  206. $src_x = ($size[0] - $thumb_width * $ratio_height) / 2;
  207. $size[0] = $thumb_width * $ratio_height;
  208. } else {
  209. $width = $thumb_width;
  210. $height = $size[1] / $ratio_width;
  211. }
  212. }
  213. }
  214. $thum_img = imagecreatetruecolor($width, $height);
  215. imagefill($thum_img, 0, 0, $rgb);
  216. imagecopyresampled($thum_img, $orig_img, 0, 0, $src_x, $src_y, $width, $height, $size[0], $size[1]);
  217. //if (function_exists($image_ = 'image' . $format)) {
  218. //$image_($thum_img, $thum_fname, $quality);
  219. //} else {
  220. imagejpeg($thum_img, $thum_fname, $quality);
  221. //}
  222. flush();
  223. imagedestroy($orig_img);
  224. imagedestroy($thum_img);
  225. return true;
  226. }
  227. function getFreeFileName($fileName, $dir)
  228. {
  229. global $cfg;
  230. $dir = rtrim($dir, DIR_SEP) . DIR_SEP;
  231. $fileName = mb_strtolower($fileName, 'utf-8');
  232. $strlen = mb_strlen($fileName, 'utf-8');
  233. $dotPos= mb_strpos($fileName, '.', null, 'utf-8');
  234. $fname = mb_substr($fileName, 0, $dotPos, 'utf-8');
  235. $format = mb_substr($fileName, $dotPos+1, ($strlen-$dotPos), 'utf-8');
  236. if (file_exists($langPhp = dirname(__FILE__) . DIR_SEP . 'lang' . DIR_SEP . $cfg['lang'] . '.php')) {
  237. require_once $langPhp;
  238. }
  239. if (!function_exists('translit')) {
  240. function translit($str) {
  241. return $str;
  242. }
  243. }
  244. $fname = translit($fname);
  245. $f = $fname . '.' . $format;
  246. if (file_exists($dir . $f)) {
  247. if (false !== ($pos = strrpos($f, '_')) && !in_array($f{$pos+1}, array(0,1,2,3,4,5,6,7,8,9))) {
  248. $symname = substr($f, 0, $pos);
  249. } else {
  250. $symname = $fname;
  251. }
  252. $symname = $fname;
  253. $i = 0;
  254. $exist = true;
  255. while ($exist && ++$i < 777) {// :)
  256. $new_name = $symname . '_(' . $i . ').' . $format;
  257. if (!file_exists($dir . $new_name)) {
  258. $exist = false;
  259. $f = $new_name;
  260. }
  261. }
  262. }
  263. return $f;
  264. }
  265. function deleteDir($dirname)
  266. {
  267. global $cfg;
  268. if (!file_exists($dirname)) {
  269. return false;
  270. }
  271. if (is_file($dirname)) {
  272. return unlink($dirname);
  273. }
  274. $dir = dir($dirname);
  275. while (false !== $entry = $dir->read()) {
  276. if ('.' == $entry || '..' == $entry) {
  277. continue;
  278. }
  279. deleteDir($dirname . DIR_SEP . $entry);
  280. }
  281. $dir->close();
  282. return rmdir($dirname);
  283. }
  284. function mkdirs($dir, $mode=0777)
  285. {
  286. if (empty($dir)) {
  287. return false;
  288. }
  289. if (is_dir($dir) || '/' === $dir) {
  290. return true;
  291. }
  292. if (mkdirs(dirname($dir), $mode)) {
  293. $is = mkdir($dir, $mode);
  294. chmod($dir, $mode);
  295. return $is;
  296. }
  297. return false;
  298. }
  299. function sortDate($a, $b)
  300. {
  301. if ($a['mtime'] == $b['mtime']) return -1;
  302. if ($a['mtime'] < $b['mtime']) return 1;
  303. return 0;
  304. }
  305. function sortSize($a, $b)
  306. {
  307. if ($a['r_size'] == $b['r_size']) return -1;
  308. if ($a['r_size'] < $b['r_size']) return 1;
  309. return 0;
  310. }
  311. if (!function_exists('scandir')) {
  312. function scandir($dir)
  313. {
  314. $dh = opendir($dir);
  315. $files = array();
  316. while (false !== ($filename = readdir($dh))) {
  317. $files[] = $filename;
  318. }
  319. return $files;
  320. }
  321. }
  322. function isWork()
  323. {
  324. header('Content-Type: text/html; charset=utf-8');
  325. global $cfg;
  326. if (!is_dir($cfg['root'])) {
  327. echo 'Directory not found: ' . $cfg['root'], '<br />';
  328. if (mkdirs($cfg['root'], $cfg['chmod']['folder'])) {
  329. echo 'Successfully created';
  330. } else {
  331. echo 'Failed created, You need to create the folder manually, or set the right<br />Вам необходимо создать папку вручную, или выставить права';
  332. exit;
  333. }
  334. } elseif (!is_writable($cfg['root'])) {
  335. echo 'No write access to folder: ' . $cfg['root'], '<br />Нет доступа на запись.';
  336. } else {
  337. echo '<font color=green>Root directory in order<br />Корневая директория в порядке.</font>';
  338. }
  339. echo '<hr />';
  340. $type = array($cfg['thumb']['dir'], 'file', 'flash', 'image');
  341. for ($i=-1;++$i<4;) {
  342. $d = $cfg['root'] . $type[$i];
  343. if (!is_dir($d)) {
  344. echo 'Directory not found: ' . $d, '<br />';
  345. if (mkdirs($d, $cfg['chmod']['folder'])) {
  346. echo 'Successfully created';
  347. } else {
  348. echo 'Failed created, You need to create the folder manually, or set the right<br />Вам необходимо создать папку вручную, или выставить права';
  349. exit;
  350. }
  351. } elseif (!is_writable($d)) {
  352. echo 'No write access to folder: ' . $d, '<br />Нет доступа на запись.';
  353. } else {
  354. echo $type[$i] . ' - <font color=green>normal</font>';
  355. }
  356. echo '<br />';
  357. }
  358. echo '<hr />';
  359. echo 'Check available extensions: ';
  360. $ext = array('json_encode', 'mb_internal_encoding', 'mb_substr', 'mb_ereg_replace', 'mb_strlen', 'imagecreatetruecolor', 'imagefill', 'imagecopyresampled', 'imagejpeg', 'imagedestroy');
  361. for ($i=-1, $iCount=count($ext); ++$i<$iCount;) {
  362. echo '<br />';
  363. echo function_exists($ext[$i])? $ext[$i] . ' - <font color=green>yes</font> ' : $ext[$i] . ' - <font color=red>no</font>';
  364. }
  365. exit;
  366. }