PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ajexfilemanager/ajax/php/lib.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 441 lines | 345 code | 70 blank | 26 comment | 77 complexity | 9667e6a773ce783466c49d9f5ff059c8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Ajex.FileManager
  4. * http://demphest.ru/ajex-filemanager
  5. *
  6. * @version
  7. * 1.0.2 (12 March 2010)
  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. 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])) {
  90. $imgSize = array(0, 0);
  91. if (in_array($ext, $cfg['allow']['image'])) {
  92. if ($cfg['thumb']['auto'] && !file_exists($thumb . $files[$i])) {
  93. create_thumbnail($full . $files[$i], $thumb . $files[$i]);
  94. }
  95. $imgSize = getimagesize($full . $files[$i]);
  96. }
  97. $stats = getSize($full . $files[$i]);
  98. $list[] = array( 'name' => $files[$i],
  99. 'ext' => $ext,
  100. 'width' => $imgSize[0],
  101. 'height' => $imgSize[1],
  102. 'size' => $stats['_size'],
  103. 'date' => date($cfg['thumb']['date'], $stats['mtime']),
  104. 'r_size' => $stats['size'],
  105. 'mtime' => $stats['mtime'],
  106. 'thumb' => '/' . $cfg['url'] . '/' . $cfg['thumb']['dir'] . '/' . $dir . $files[$i]
  107. );
  108. }
  109. }
  110. switch($cfg['sort']) {
  111. case 'size':
  112. usort($list, 'sortSize');
  113. break;
  114. case 'date':
  115. usort($list, 'sortDate');
  116. break;
  117. default: //name
  118. break;
  119. }
  120. return $list;
  121. }
  122. function dirSize($dirFile)
  123. {
  124. $size = 0;
  125. $dir = opendir($dirFile);
  126. if (!$dir) return false;
  127. while (false !== ($f = readdir($dir))) {
  128. if ($f[0] == '.') continue;
  129. if (is_dir($dirFile . $f)) {
  130. $size += dirSize($dirFile . $f . DIR_SEP);
  131. } else {
  132. $size += filesize($dirFile . $f);
  133. }
  134. }
  135. closedir($dir);
  136. return $size;
  137. }
  138. function getSize($dirFile, $mode = 'file')
  139. {
  140. if ('file' == $mode) {
  141. $stats = stat($dirFile);
  142. } elseif ('dir' == $mode) {
  143. $stats['size'] = dirSize($dirFile);
  144. }
  145. if (empty($stats['size'])) {
  146. $stats['_size'] = '';
  147. } elseif ($stats['size'] < 1024) {
  148. $stats['_size'] .= ' B';
  149. } elseif ($stats['size'] < 1048576) {
  150. $stats['_size'] = round($stats['size'] / 1024) . ' KB';
  151. } else {
  152. $stats['_size'] = round($stats['size'] / 1048576, 2) . ' MB';
  153. }
  154. return $stats;
  155. }
  156. function create_thumbnail($orig_fname, $thum_fname, $thumb_width = null, $thumb_height = null, $quality = null, $do_cut = null, $uploadResize = false)
  157. {
  158. if (!mkdirs(dirname($thum_fname))) {
  159. return false;
  160. }
  161. $size = @getimagesize($orig_fname);
  162. if (false === $size) {
  163. return false;
  164. }
  165. $rgb = 0xFFFFFF;
  166. $src_x = $src_y = 0;
  167. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
  168. $icfunc = 'imagecreatefrom' . $format;
  169. if (!function_exists($icfunc)) {
  170. return false;
  171. }
  172. 0 === $thumb_width? $thumb_width = $size[0] : null;
  173. 0 === $thumb_height? $thumb_height = $size[1] : null;
  174. global $cfg;
  175. null === $thumb_width? $thumb_width = $cfg['thumb']['width'] : null;
  176. null === $thumb_height? $thumb_height = $cfg['thumb']['height']: null;
  177. null === $quality? $quality = $cfg['thumb']['quality'] : null;
  178. null === $do_cut? $do_cut = $cfg['thumb']['cut'] : null;
  179. $path = pathinfo($thum_fname);
  180. if (!is_dir($path['dirname'])) {
  181. $is = mkdir($path['dirname'], $cfg['chmod']['folder']);
  182. if (!$is) {
  183. return false;
  184. }
  185. }
  186. $orig_img = $icfunc($orig_fname);
  187. if (($size[0] <= $thumb_width) && ($size[1] <= $thumb_height)) {
  188. $width = $size[0];
  189. $height = $size[1];
  190. } else {
  191. $width = $thumb_width;
  192. $height = $thumb_height;
  193. $ratio_width = $size[0] / $thumb_width;
  194. $ratio_height = $size[1] / $thumb_height;
  195. if ($ratio_width < $ratio_height) {
  196. if ($do_cut) {
  197. $src_y = ($size[1] - $thumb_height * $ratio_width) / 2;
  198. $size[1] = $thumb_height * $ratio_width;
  199. } else {
  200. $width = $size[0] / $ratio_height;
  201. $height = $thumb_height;
  202. }
  203. } else {
  204. if ($do_cut) {
  205. $src_x = ($size[0] - $thumb_width * $ratio_height) / 2;
  206. $size[0] = $thumb_width * $ratio_height;
  207. } else {
  208. $width = $thumb_width;
  209. $height = $size[1] / $ratio_width;
  210. }
  211. }
  212. }
  213. $thum_img = imagecreatetruecolor($width, $height);
  214. imagefill($thum_img, 0, 0, $rgb);
  215. imagecopyresampled($thum_img, $orig_img, 0, 0, $src_x, $src_y, $width, $height, $size[0], $size[1]);
  216. //if (function_exists($image_ = 'image' . $format)) {
  217. //$image_($thum_img, $thum_fname, $quality);
  218. //} else {
  219. imagejpeg($thum_img, $thum_fname, $quality);
  220. //}
  221. flush();
  222. imagedestroy($orig_img);
  223. imagedestroy($thum_img);
  224. return true;
  225. }
  226. function getFreeFileName($fileName, $dir)
  227. {
  228. global $cfg;
  229. $dir = rtrim($dir, DIR_SEP) . DIR_SEP;
  230. $fileName = mb_strtolower($fileName, 'utf-8');
  231. $strlen = mb_strlen($fileName, 'utf-8');
  232. $dotPos= mb_strrpos($fileName, '.', null, 'utf-8');
  233. $fname = mb_substr($fileName, 0, mb_strrpos($fileName, '.', null, 'utf-8'), 'utf-8');
  234. $format = mb_substr($fileName, $dotPos+1, ($strlen-$dotPos), 'utf-8');
  235. if (file_exists($langPhp = dirname(__FILE__) . DIR_SEP . 'lang' . DIR_SEP . $cfg['lang'] . '.php')) {
  236. require_once $langPhp;
  237. }
  238. if (!function_exists('translit')) {
  239. function translit($str) {
  240. return $str;
  241. }
  242. }
  243. $fname = translit($fname);
  244. $f = $fname . '.' . $format;
  245. if (file_exists($dir . $f)) {
  246. if (false !== ($pos = strrpos($f, '_')) && !in_array($f{$pos+1}, array(0,1,2,3,4,5,6,7,8,9))) {
  247. $symname = substr($f, 0, $pos);
  248. } else {
  249. $symname = $fname;
  250. }
  251. $symname = $fname;
  252. $i = 0;
  253. $exist = true;
  254. while ($exist && ++$i < 777) {// :)
  255. $new_name = $symname . '_(' . $i . ').' . $format;
  256. if (!file_exists($dir . $new_name)) {
  257. $exist = false;
  258. $f = $new_name;
  259. }
  260. }
  261. }
  262. return $f;
  263. }
  264. function deleteDir($dirname)
  265. {
  266. global $cfg;
  267. if (!file_exists($dirname)) {
  268. return false;
  269. }
  270. if (is_file($dirname)) {
  271. return unlink($dirname);
  272. }
  273. $dir = dir($dirname);
  274. while (false !== $entry = $dir->read()) {
  275. if ('.' == $entry || '..' == $entry) {
  276. continue;
  277. }
  278. deleteDir($dirname . DIR_SEP . $entry);
  279. }
  280. $dir->close();
  281. return rmdir($dirname);
  282. }
  283. function mkdirs($dir, $mode=0777)
  284. {
  285. if (empty($dir)) {
  286. return false;
  287. }
  288. if (is_dir($dir) || '/' === $dir) {
  289. return true;
  290. }
  291. if (mkdirs(dirname($dir), $mode)) {
  292. $is = mkdir($dir, $mode);
  293. chmod($dir, $mode);
  294. return $is;
  295. }
  296. return false;
  297. }
  298. function sortDate($a, $b)
  299. {
  300. if ($a['mtime'] == $b['mtime']) return -1;
  301. if ($a['mtime'] < $b['mtime']) return 1;
  302. return 0;
  303. }
  304. function sortSize($a, $b)
  305. {
  306. if ($a['r_size'] == $b['r_size']) return -1;
  307. if ($a['r_size'] < $b['r_size']) return 1;
  308. return 0;
  309. }
  310. if (!function_exists('scandir')) {
  311. function scandir($dir)
  312. {
  313. $dh = opendir($dir);
  314. $files = array();
  315. while (false !== ($filename = readdir($dh))) {
  316. $files[] = $filename;
  317. }
  318. return $files;
  319. }
  320. }
  321. function isWork()
  322. {
  323. header('Content-Type: text/html; charset=utf-8');
  324. global $cfg;
  325. if (!is_dir($cfg['root'])) {
  326. echo 'Directory not found: ' . $cfg['root'], '<br />';
  327. if (mkdirs($cfg['root'], $cfg['chmod']['folder'])) {
  328. echo 'Successfully created';
  329. } else {
  330. echo 'Failed created, You need to create the folder manually, or set the right<br />Вам необходимо создать папку вручную, или выставить права';
  331. exit;
  332. }
  333. } elseif (!is_writable($cfg['root'])) {
  334. echo 'No write access to folder: ' . $cfg['root'], '<br />Нет доступа на запись.';
  335. } else {
  336. echo '<font color=green>Root directory in order<br />Корневая директория в порядке.</font>';
  337. }
  338. echo '<hr />';
  339. $type = array($cfg['thumb']['dir'], 'file', 'flash', 'image');
  340. for ($i=-1;++$i<4;) {
  341. $d = $cfg['root'] . $type[$i];
  342. if (!is_dir($d)) {
  343. echo 'Directory not found: ' . $d, '<br />';
  344. if (mkdirs($d, $cfg['chmod']['folder'])) {
  345. echo 'Successfully created';
  346. } else {
  347. echo 'Failed created, You need to create the folder manually, or set the right<br />Вам необходимо создать папку вручную, или выставить права';
  348. exit;
  349. }
  350. } elseif (!is_writable($d)) {
  351. echo 'No write access to folder: ' . $d, '<br />Нет доступа на запись.';
  352. } else {
  353. echo $type[$i] . ' - <font color=green>normal</font>';
  354. }
  355. echo '<br />';
  356. }
  357. echo '<hr />';
  358. echo 'Check available extensions: ';
  359. $ext = array('json_encode', 'mb_internal_encoding', 'mb_substr', 'mb_ereg_replace', 'mb_strlen', 'imagecreatetruecolor', 'imagefill', 'imagecopyresampled', 'imagejpeg', 'imagedestroy');
  360. for ($i=-1, $iCount=count($ext); ++$i<$iCount;) {
  361. echo '<br />';
  362. echo function_exists($ext[$i])? $ext[$i] . ' - <font color=green>yes</font> ' : $ext[$i] . ' - <font color=red>no</font>';
  363. }
  364. exit;
  365. }