PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/media/browser/mcpuk/core/browser.php

https://github.com/64j/modx.evo.custom
PHP | 807 lines | 720 code | 59 blank | 28 comment | 128 complexity | ea356f795ebbd42cdda36f20bde4e158 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Browser actions class
  5. * @package KCFinder
  6. * @version 2.51
  7. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  8. * @copyright 2010, 2011 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 browser extends uploader {
  14. protected $action;
  15. protected $thumbsDir;
  16. protected $thumbsTypeDir;
  17. public function __construct() {
  18. parent::__construct();
  19. if (isset($this->post['dir'])) {
  20. $dir = $this->checkInputDir($this->post['dir'], true, false);
  21. if ($dir === false) unset($this->post['dir']);
  22. $this->post['dir'] = $dir;
  23. }
  24. if (isset($this->get['dir'])) {
  25. $dir = $this->checkInputDir($this->get['dir'], true, false);
  26. if ($dir === false) unset($this->get['dir']);
  27. $this->get['dir'] = $dir;
  28. }
  29. $thumbsDir = $this->config['uploadDir'] . "/" . $this->config['thumbsDir'];
  30. if ((
  31. !is_dir($thumbsDir) &&
  32. !@mkdir($thumbsDir, $this->config['dirPerms'])
  33. ) ||
  34. !is_readable($thumbsDir) ||
  35. !dir::isWritable($thumbsDir) ||
  36. (
  37. !is_dir("$thumbsDir/{$this->type}") &&
  38. !@mkdir("$thumbsDir/{$this->type}", $this->config['dirPerms'])
  39. )
  40. )
  41. $this->errorMsg("Cannot access or create thumbnails folder.");
  42. $this->thumbsDir = $thumbsDir;
  43. $this->thumbsTypeDir = "$thumbsDir/{$this->type}";
  44. // Remove temporary zip downloads if exists
  45. $files = dir::content($this->config['uploadDir'], array(
  46. 'types' => "file",
  47. 'pattern' => '/^.*\.zip$/i'
  48. ));
  49. if (is_array($files) && count($files)) {
  50. $time = time();
  51. foreach ($files as $file)
  52. if (is_file($file) && ($time - filemtime($file) > 3600))
  53. unlink($file);
  54. }
  55. if (isset($this->get['theme']) &&
  56. ($this->get['theme'] == basename($this->get['theme'])) &&
  57. is_dir("themes/{$this->get['theme']}")
  58. )
  59. $this->config['theme'] = $this->get['theme'];
  60. }
  61. public function action() {
  62. $act = isset($this->get['act']) ? $this->get['act'] : "browser";
  63. if(!preg_match('@^[0-9a-zA-Z_]+$@', $act)) $this->errorMsg("Unknown error.");
  64. if (!method_exists($this, "act_$act"))
  65. $act = "browser";
  66. $this->action = $act;
  67. $method = "act_$act";
  68. if ($this->config['disabled']) {
  69. $message = $this->label("You don't have permissions to browse server.");
  70. if (in_array($act, array("browser", "upload")) ||
  71. (substr($act, 0, 8) == "download")
  72. )
  73. $this->backMsg($message);
  74. else {
  75. header("Content-Type: text/plain; charset={$this->charset}");
  76. die(json_encode(array('error' => $message)));
  77. }
  78. }
  79. if (!isset($this->session['dir']))
  80. $this->session['dir'] = $this->type;
  81. else {
  82. $type = $this->getTypeFromPath($this->session['dir']);
  83. $dir = $this->config['uploadDir'] . "/" . $this->session['dir'];
  84. if (($type != $this->type) || !is_dir($dir) || !is_readable($dir))
  85. $this->session['dir'] = $this->type;
  86. }
  87. $this->session['dir'] = path::normalize($this->session['dir']);
  88. if ($act == "browser") {
  89. header("X-UA-Compatible: chrome=1");
  90. header("Content-Type: text/html; charset={$this->charset}");
  91. } elseif (
  92. (substr($act, 0, 8) != "download") &&
  93. !in_array($act, array("thumb", "upload"))
  94. )
  95. header("Content-Type: text/plain; charset={$this->charset}");
  96. $return = $this->$method();
  97. echo ($return === true)
  98. ? '{}'
  99. : $return;
  100. }
  101. protected function act_browser() {
  102. if (isset($this->get['dir']) &&
  103. is_dir("{$this->typeDir}/{$this->get['dir']}") &&
  104. is_readable("{$this->typeDir}/{$this->get['dir']}")
  105. )
  106. $this->session['dir'] = path::normalize("{$this->type}/{$this->get['dir']}");
  107. return $this->output();
  108. }
  109. protected function act_init() {
  110. $tree = $this->getDirInfo($this->typeDir);
  111. $tree['dirs'] = $this->getTree($this->session['dir']);
  112. if (!is_array($tree['dirs']) || !count($tree['dirs']))
  113. unset($tree['dirs']);
  114. $files = $this->getFiles($this->session['dir']);
  115. $dirWritable = dir::isWritable("{$this->config['uploadDir']}/{$this->session['dir']}");
  116. $data = array(
  117. 'tree' => &$tree,
  118. 'files' => &$files,
  119. 'dirWritable' => $dirWritable
  120. );
  121. return json_encode($data);
  122. }
  123. protected function act_thumb() {
  124. $this->getDir($this->get['dir'], true);
  125. if (!isset($this->get['file']) || !isset($this->get['dir']))
  126. $this->sendDefaultThumb();
  127. $file = $this->get['file'];
  128. if (basename($file) != $file)
  129. $this->sendDefaultThumb();
  130. $file = "{$this->thumbsDir}/{$this->type}/{$this->get['dir']}/$file";
  131. if (!is_file($file) || !is_readable($file)) {
  132. $file = "{$this->config['uploadDir']}/{$this->type}/{$this->get['dir']}/" . basename($file);
  133. if (!is_file($file) || !is_readable($file))
  134. $this->sendDefaultThumb($file);
  135. $image = new gd($file);
  136. if ($image->init_error)
  137. $this->sendDefaultThumb($file);
  138. $browsable = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
  139. if (in_array($image->type, $browsable) &&
  140. ($image->get_width() <= $this->config['thumbWidth']) &&
  141. ($image->get_height() <= $this->config['thumbHeight'])
  142. ) {
  143. $type =
  144. ($image->type == IMAGETYPE_GIF) ? "gif" : (
  145. ($image->type == IMAGETYPE_PNG) ? "png" : "jpeg");
  146. $type = "image/$type";
  147. httpCache::file($file, $type);
  148. } else
  149. $this->sendDefaultThumb($file);
  150. }
  151. httpCache::file($file, "image/jpeg");
  152. }
  153. protected function act_expand() {
  154. return json_encode(array('dirs' => $this->getDirs($this->postDir())));
  155. }
  156. protected function act_chDir() {
  157. $this->postDir(); // Just for existing check
  158. $this->session['dir'] = $this->type . "/" . $this->post['dir'];
  159. $dirWritable = dir::isWritable("{$this->config['uploadDir']}/{$this->session['dir']}");
  160. return json_encode(array(
  161. 'files' => $this->getFiles($this->session['dir']),
  162. 'dirWritable' => $dirWritable
  163. ));
  164. }
  165. protected function act_newDir() {
  166. if (!$this->config['access']['dirs']['create'] ||
  167. !isset($this->post['dir']) ||
  168. !isset($this->post['newDir'])
  169. )
  170. $this->errorMsg("Unknown error.");
  171. $dir = $this->postDir();
  172. $newDir = $this->normalizeDirname(trim($this->post['newDir']));
  173. if (!strlen($newDir))
  174. $this->errorMsg("Please enter new folder name.");
  175. if (preg_match('/[\/\\\\]/s', $newDir))
  176. $this->errorMsg("Unallowable characters in folder name.");
  177. if (substr($newDir, 0, 1) == ".")
  178. $this->errorMsg("Folder name shouldn't begins with '.'");
  179. if (file_exists("$dir/$newDir"))
  180. $this->errorMsg("A file or folder with that name already exists.");
  181. if (!@mkdir("$dir/$newDir", $this->config['dirPerms']))
  182. $this->errorMsg("Cannot create {dir} folder.", array('dir' => $newDir));
  183. return true;
  184. }
  185. protected function act_renameDir() {
  186. if (!$this->config['access']['dirs']['rename'] ||
  187. !isset($this->post['dir']) ||
  188. !isset($this->post['newName'])
  189. )
  190. $this->errorMsg("Unknown error.");
  191. $dir = $this->postDir();
  192. $newName = $this->normalizeDirname(trim($this->post['newName']));
  193. if (!strlen($newName))
  194. $this->errorMsg("Please enter new folder name.");
  195. if (preg_match('/[\/\\\\]/s', $newName))
  196. $this->errorMsg("Unallowable characters in folder name.");
  197. if (substr($newName, 0, 1) == ".")
  198. $this->errorMsg("Folder name shouldn't begins with '.'");
  199. if (!@rename($dir, dirname($dir) . "/$newName"))
  200. $this->errorMsg("Cannot rename the folder.");
  201. $thumbDir = "$this->thumbsTypeDir/{$this->post['dir']}";
  202. if (is_dir($thumbDir))
  203. @rename($thumbDir, dirname($thumbDir) . "/$newName");
  204. return json_encode(array('name' => $newName));
  205. }
  206. protected function act_deleteDir() {
  207. if (!$this->config['access']['dirs']['delete'] ||
  208. !isset($this->post['dir']) ||
  209. !strlen(trim($this->post['dir']))
  210. )
  211. $this->errorMsg("Unknown error.");
  212. $dir = $this->postDir();
  213. if (!dir::isWritable($dir))
  214. $this->errorMsg("Cannot delete the folder.");
  215. $result = !dir::prune($dir, false);
  216. if (is_array($result) && count($result))
  217. $this->errorMsg("Failed to delete {count} files/folders.",
  218. array('count' => count($result)));
  219. $thumbDir = "$this->thumbsTypeDir/{$this->post['dir']}";
  220. if (is_dir($thumbDir)) dir::prune($thumbDir);
  221. return true;
  222. }
  223. protected function act_upload() {
  224. if (!$this->config['access']['files']['upload'] ||
  225. !isset($this->post['dir'])
  226. )
  227. $this->errorMsg("Unknown error.");
  228. $dir = $this->postDir();
  229. if (!dir::isWritable($dir))
  230. $this->errorMsg("Cannot access or write to upload folder.");
  231. if (is_array($this->file['name'])) {
  232. $return = array();
  233. foreach ($this->file['name'] as $i => $name) {
  234. $return[] = $this->moveUploadFile(array(
  235. 'name' => $name,
  236. 'tmp_name' => $this->file['tmp_name'][$i],
  237. 'error' => $this->file['error'][$i]
  238. ), $dir);
  239. }
  240. return implode("\n", $return);
  241. } else
  242. return $this->moveUploadFile($this->file, $dir);
  243. }
  244. protected function act_download() {
  245. $dir = $this->postDir();
  246. if (!isset($this->post['dir']) ||
  247. !isset($this->post['file']) ||
  248. strpos($this->post['file'],'../')!==false ||
  249. (false === ($file = "$dir/{$this->post['file']}")) ||
  250. !file_exists($file) || !is_readable($file)
  251. )
  252. $this->errorMsg("Unknown error.");
  253. header("Pragma: public");
  254. header("Expires: 0");
  255. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  256. header("Cache-Control: private", false);
  257. header("Content-Type: application/octet-stream");
  258. header('Content-Disposition: attachment; filename="' . str_replace('"', "_", $this->post['file']) . '"');
  259. header("Content-Transfer-Encoding:­ binary");
  260. header("Content-Length: " . filesize($file));
  261. readfile($file);
  262. die;
  263. }
  264. protected function act_rename() {
  265. $dir = $this->postDir();
  266. if (!$this->config['access']['files']['rename'] ||
  267. !isset($this->post['dir']) ||
  268. !isset($this->post['file']) ||
  269. strpos($this->post['file'],'../')!==false ||
  270. !isset($this->post['newName']) ||
  271. (false === ($file = "$dir/{$this->post['file']}")) ||
  272. !file_exists($file) || !is_readable($file) || !file::isWritable($file)
  273. )
  274. $this->errorMsg("Unknown error.");
  275. if (isset($this->config['denyExtensionRename']) &&
  276. $this->config['denyExtensionRename'] &&
  277. (file::getExtension($this->post['file'], true) !==
  278. file::getExtension($this->post['newName'], true)
  279. )
  280. )
  281. $this->errorMsg("You cannot rename the extension of files!");
  282. $newName = $this->normalizeFilename(trim($this->post['newName']));
  283. if (!strlen($newName))
  284. $this->errorMsg("Please enter new file name.");
  285. if (preg_match('/[\/\\\\]/s', $newName))
  286. $this->errorMsg("Unallowable characters in file name.");
  287. if (substr($newName, 0, 1) == ".")
  288. $this->errorMsg("File name shouldn't begins with '.'");
  289. $newName = "$dir/$newName";
  290. if (file_exists($newName))
  291. $this->errorMsg("A file or folder with that name already exists.");
  292. $ext = file::getExtension($newName);
  293. if (!$this->validateExtension($ext, $this->type))
  294. $this->errorMsg("Denied file extension.");
  295. if (!@rename($file, $newName))
  296. $this->errorMsg("Unknown error.");
  297. $thumbDir = "{$this->thumbsTypeDir}/{$this->post['dir']}";
  298. $thumbFile = "$thumbDir/{$this->post['file']}";
  299. if (file_exists($thumbFile))
  300. @rename($thumbFile, "$thumbDir/" . basename($newName));
  301. return true;
  302. }
  303. protected function act_delete() {
  304. $dir = $this->postDir();
  305. if (!$this->config['access']['files']['delete'] ||
  306. !isset($this->post['dir']) ||
  307. !isset($this->post['file']) ||
  308. strpos($this->post['file'],'../')!==false ||
  309. (false === ($file = "$dir/{$this->post['file']}")) ||
  310. !file_exists($file) || !is_readable($file) || !file::isWritable($file) ||
  311. !@unlink($file)
  312. )
  313. $this->errorMsg("Unknown error.");
  314. $thumb = "{$this->thumbsTypeDir}/{$this->post['dir']}/{$this->post['file']}";
  315. if (file_exists($thumb)) @unlink($thumb);
  316. return true;
  317. }
  318. protected function act_cp_cbd() {
  319. $dir = $this->postDir();
  320. if (!$this->config['access']['files']['copy'] ||
  321. !isset($this->post['dir']) ||
  322. !is_dir($dir) || !is_readable($dir) || !dir::isWritable($dir) ||
  323. !isset($this->post['files']) || !is_array($this->post['files']) ||
  324. !count($this->post['files'])
  325. )
  326. $this->errorMsg("Unknown error.");
  327. $error = array();
  328. foreach($this->post['files'] as $file) {
  329. $file = path::normalize($file);
  330. if (substr($file, 0, 1) == ".") continue;
  331. $type = explode("/", $file);
  332. $type = $type[0];
  333. if ($type != $this->type) continue;
  334. $path = "{$this->config['uploadDir']}/$file";
  335. $base = basename($file);
  336. $replace = array('file' => $base);
  337. $ext = file::getExtension($base);
  338. if (!file_exists($path))
  339. $error[] = $this->label("The file '{file}' does not exist.", $replace);
  340. elseif (substr($base, 0, 1) == ".")
  341. $error[] = "$base: " . $this->label("File name shouldn't begins with '.'");
  342. elseif (!$this->validateExtension($ext, $type))
  343. $error[] = "$base: " . $this->label("Denied file extension.");
  344. elseif (file_exists("$dir/$base"))
  345. $error[] = "$base: " . $this->label("A file or folder with that name already exists.");
  346. elseif (!is_readable($path) || !is_file($path))
  347. $error[] = $this->label("Cannot read '{file}'.", $replace);
  348. elseif (!@copy($path, "$dir/$base"))
  349. $error[] = $this->label("Cannot copy '{file}'.", $replace);
  350. else {
  351. if (function_exists("chmod"))
  352. @chmod("$dir/$base", $this->config['filePerms']);
  353. $fromThumb = "{$this->thumbsDir}/$file";
  354. if (is_file($fromThumb) && is_readable($fromThumb)) {
  355. $toThumb = "{$this->thumbsTypeDir}/{$this->post['dir']}";
  356. if (!is_dir($toThumb))
  357. @mkdir($toThumb, $this->config['dirPerms'], true);
  358. $toThumb .= "/$base";
  359. @copy($fromThumb, $toThumb);
  360. }
  361. }
  362. }
  363. if (count($error))
  364. return json_encode(array('error' => $error));
  365. return true;
  366. }
  367. protected function act_mv_cbd() {
  368. $dir = $this->postDir();
  369. if (!$this->config['access']['files']['move'] ||
  370. !isset($this->post['dir']) ||
  371. !is_dir($dir) || !is_readable($dir) || !dir::isWritable($dir) ||
  372. !isset($this->post['files']) || !is_array($this->post['files']) ||
  373. !count($this->post['files'])
  374. )
  375. $this->errorMsg("Unknown error.");
  376. $error = array();
  377. foreach($this->post['files'] as $file) {
  378. $file = path::normalize($file);
  379. if (substr($file, 0, 1) == ".") continue;
  380. $type = explode("/", $file);
  381. $type = $type[0];
  382. if ($type != $this->type) continue;
  383. $path = "{$this->config['uploadDir']}/$file";
  384. $base = basename($file);
  385. $replace = array('file' => $base);
  386. $ext = file::getExtension($base);
  387. if (!file_exists($path))
  388. $error[] = $this->label("The file '{file}' does not exist.", $replace);
  389. elseif (substr($base, 0, 1) == ".")
  390. $error[] = "$base: " . $this->label("File name shouldn't begins with '.'");
  391. elseif (!$this->validateExtension($ext, $type))
  392. $error[] = "$base: " . $this->label("Denied file extension.");
  393. elseif (file_exists("$dir/$base"))
  394. $error[] = "$base: " . $this->label("A file or folder with that name already exists.");
  395. elseif (!is_readable($path) || !is_file($path))
  396. $error[] = $this->label("Cannot read '{file}'.", $replace);
  397. elseif (!file::isWritable($path) || !@rename($path, "$dir/$base"))
  398. $error[] = $this->label("Cannot move '{file}'.", $replace);
  399. else {
  400. if (function_exists("chmod"))
  401. @chmod("$dir/$base", $this->config['filePerms']);
  402. $fromThumb = "{$this->thumbsDir}/$file";
  403. if (is_file($fromThumb) && is_readable($fromThumb)) {
  404. $toThumb = "{$this->thumbsTypeDir}/{$this->post['dir']}";
  405. if (!is_dir($toThumb))
  406. @mkdir($toThumb, $this->config['dirPerms'], true);
  407. $toThumb .= "/$base";
  408. @rename($fromThumb, $toThumb);
  409. }
  410. }
  411. }
  412. if (count($error))
  413. return json_encode(array('error' => $error));
  414. return true;
  415. }
  416. protected function act_rm_cbd() {
  417. if (!$this->config['access']['files']['delete'] ||
  418. !isset($this->post['files']) ||
  419. !is_array($this->post['files']) ||
  420. !count($this->post['files'])
  421. )
  422. $this->errorMsg("Unknown error.");
  423. $error = array();
  424. foreach($this->post['files'] as $file) {
  425. $file = path::normalize($file);
  426. if (substr($file, 0, 1) == ".") continue;
  427. $type = explode("/", $file);
  428. $type = $type[0];
  429. if ($type != $this->type) continue;
  430. $path = "{$this->config['uploadDir']}/$file";
  431. $base = basename($file);
  432. $replace = array('file' => $base);
  433. if (!is_file($path))
  434. $error[] = $this->label("The file '{file}' does not exist.", $replace);
  435. elseif (!@unlink($path))
  436. $error[] = $this->label("Cannot delete '{file}'.", $replace);
  437. else {
  438. $thumb = "{$this->thumbsDir}/$file";
  439. if (is_file($thumb)) @unlink($thumb);
  440. }
  441. }
  442. if (count($error))
  443. return json_encode(array('error' => $error));
  444. return true;
  445. }
  446. protected function act_downloadDir() {
  447. $dir = $this->postDir();
  448. if (!isset($this->post['dir']) || $this->config['denyZipDownload'])
  449. $this->errorMsg("Unknown error.");
  450. $filename = basename($dir) . ".zip";
  451. do {
  452. $file = md5(time() . session_id());
  453. $file = "{$this->config['uploadDir']}/$file.zip";
  454. } while (file_exists($file));
  455. new zipFolder($file, $dir);
  456. header("Content-Type: application/x-zip");
  457. header('Content-Disposition: attachment; filename="' . str_replace('"', "_", $filename) . '"');
  458. header("Content-Length: " . filesize($file));
  459. readfile($file);
  460. unlink($file);
  461. die;
  462. }
  463. protected function act_downloadSelected() {
  464. $dir = $this->postDir();
  465. if (!isset($this->post['dir']) ||
  466. !isset($this->post['files']) ||
  467. !is_array($this->post['files']) ||
  468. $this->config['denyZipDownload']
  469. )
  470. $this->errorMsg("Unknown error.");
  471. $zipFiles = array();
  472. foreach ($this->post['files'] as $file) {
  473. $file = path::normalize($file);
  474. if ((substr($file, 0, 1) == ".") || (strpos($file, '/') !== false))
  475. continue;
  476. $file = "$dir/$file";
  477. if (!is_file($file) || !is_readable($file))
  478. continue;
  479. $zipFiles[] = $file;
  480. }
  481. do {
  482. $file = md5(time() . session_id());
  483. $file = "{$this->config['uploadDir']}/$file.zip";
  484. } while (file_exists($file));
  485. $zip = new ZipArchive();
  486. $res = $zip->open($file, ZipArchive::CREATE);
  487. if ($res === TRUE) {
  488. foreach ($zipFiles as $cfile)
  489. $zip->addFile($cfile, basename($cfile));
  490. $zip->close();
  491. }
  492. header("Content-Type: application/x-zip");
  493. header('Content-Disposition: attachment; filename="selected_files_' . basename($file) . '"');
  494. header("Content-Length: " . filesize($file));
  495. readfile($file);
  496. unlink($file);
  497. die;
  498. }
  499. protected function act_downloadClipboard() {
  500. if (!isset($this->post['files']) ||
  501. !is_array($this->post['files']) ||
  502. $this->config['denyZipDownload']
  503. )
  504. $this->errorMsg("Unknown error.");
  505. $zipFiles = array();
  506. foreach ($this->post['files'] as $file) {
  507. $file = path::normalize($file);
  508. if ((substr($file, 0, 1) == "."))
  509. continue;
  510. $type = explode("/", $file);
  511. $type = $type[0];
  512. if ($type != $this->type)
  513. continue;
  514. $file = $this->config['uploadDir'] . "/$file";
  515. if (!is_file($file) || !is_readable($file))
  516. continue;
  517. $zipFiles[] = $file;
  518. }
  519. do {
  520. $file = md5(time() . session_id());
  521. $file = "{$this->config['uploadDir']}/$file.zip";
  522. } while (file_exists($file));
  523. $zip = new ZipArchive();
  524. $res = $zip->open($file, ZipArchive::CREATE);
  525. if ($res === TRUE) {
  526. foreach ($zipFiles as $cfile)
  527. $zip->addFile($cfile, basename($cfile));
  528. $zip->close();
  529. }
  530. header("Content-Type: application/x-zip");
  531. header('Content-Disposition: attachment; filename="clipboard_' . basename($file) . '"');
  532. header("Content-Length: " . filesize($file));
  533. readfile($file);
  534. unlink($file);
  535. die;
  536. }
  537. protected function moveUploadFile($file, $dir) {
  538. $message = $this->checkUploadedFile($file);
  539. if ($message !== true) {
  540. if (isset($file['tmp_name']))
  541. @unlink($file['tmp_name']);
  542. return "{$file['name']}: $message";
  543. }
  544. $filename = $this->normalizeFilename($file['name']);
  545. $target = "$dir/" . file::getInexistantFilename($filename, $dir);
  546. if (!@move_uploaded_file($file['tmp_name'], $target) &&
  547. !@rename($file['tmp_name'], $target) &&
  548. !@copy($file['tmp_name'], $target)
  549. ) {
  550. @unlink($file['tmp_name']);
  551. return "{$file['name']}: " . $this->label("Cannot move uploaded file to target folder.");
  552. } elseif (function_exists('chmod'))
  553. chmod($target, $this->config['filePerms']);
  554. $this->makeThumb($target);
  555. return "/" . basename($target);
  556. }
  557. protected function sendDefaultThumb($file=null) {
  558. if ($file !== null) {
  559. $ext = file::getExtension($file);
  560. $thumb = "themes/{$this->config['theme']}/img/files/big/$ext.png";
  561. }
  562. if (!isset($thumb) || !file_exists($thumb))
  563. $thumb = "themes/{$this->config['theme']}/img/files/big/..png";
  564. header("Content-Type: image/png");
  565. readfile($thumb);
  566. die;
  567. }
  568. protected function getFiles($dir) {
  569. $thumbDir = "{$this->config['uploadDir']}/{$this->config['thumbsDir']}/$dir";
  570. $dir = "{$this->config['uploadDir']}/$dir";
  571. $return = array();
  572. $files = dir::content($dir, array('types' => "file"));
  573. if ($files === false)
  574. return $return;
  575. foreach ($files as $file) {
  576. $ext = file::getExtension($file);
  577. $smallThumb = false;
  578. if (in_array(strtolower($ext), array('png', 'jpg', 'gif', 'jpeg' )) ) {
  579. $size = @getimagesize($file);
  580. if (is_array($size) && count($size)) {
  581. $thumb_file = "$thumbDir/" . basename($file);
  582. if (!is_file($thumb_file))
  583. $this->makeThumb($file, false);
  584. $smallThumb =
  585. ($size[0] <= $this->config['thumbWidth']) &&
  586. ($size[1] <= $this->config['thumbHeight']) &&
  587. in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG));
  588. }
  589. }
  590. $stat = stat($file);
  591. if ($stat === false) continue;
  592. $name = basename($file);
  593. if (substr($name,0,1) == '.' && !$this->config['showHiddenFiles']) continue;
  594. $bigIcon = file_exists("themes/{$this->config['theme']}/img/files/big/$ext.png");
  595. $smallIcon = file_exists("themes/{$this->config['theme']}/img/files/small/$ext.png");
  596. $thumb = file_exists("$thumbDir/$name");
  597. $return[] = array(
  598. 'name' => stripcslashes($name),
  599. 'size' => $stat['size'],
  600. 'mtime' => $stat['mtime'],
  601. 'date' => @strftime($this->dateTimeSmall, $stat['mtime']),
  602. 'readable' => is_readable($file),
  603. 'writable' => file::isWritable($file),
  604. 'bigIcon' => $bigIcon,
  605. 'smallIcon' => $smallIcon,
  606. 'thumb' => $thumb,
  607. 'smallThumb' => $smallThumb
  608. );
  609. }
  610. return $return;
  611. }
  612. protected function getTree($dir, $index=0) {
  613. $path = explode("/", $dir);
  614. $pdir = "";
  615. for ($i = 0; ($i <= $index && $i < count($path)); $i++)
  616. $pdir .= "/{$path[$i]}";
  617. if (strlen($pdir))
  618. $pdir = substr($pdir, 1);
  619. $fdir = "{$this->config['uploadDir']}/$pdir";
  620. $dirs = $this->getDirs($fdir);
  621. if (is_array($dirs) && count($dirs) && ($index <= count($path) - 1)) {
  622. /* Теперь собирается только первый уровень. Остальные подгружаются по мере их просмотра. Спасибо Rekill
  623. foreach ($dirs as $i => $cdir) {
  624. if ($cdir['hasDirs'] &&
  625. (
  626. ($index == count($path) - 1) ||
  627. ($cdir['name'] == $path[$index + 1])
  628. )
  629. ) {
  630. $dirs[$i]['dirs'] = $this->getTree($dir, $index + 1);
  631. if (!is_array($dirs[$i]['dirs']) || !count($dirs[$i]['dirs'])) {
  632. unset($dirs[$i]['dirs']);
  633. continue;
  634. }
  635. }
  636. }
  637. */
  638. } else
  639. return false;
  640. return $dirs;
  641. }
  642. protected function postDir($existent=true) {
  643. $dir = $this->typeDir;
  644. if (isset($this->post['dir']))
  645. $dir .= "/" . $this->post['dir'];
  646. if ($existent && (!is_dir($dir) || !is_readable($dir)))
  647. $this->errorMsg("Inexistant or inaccessible folder.");
  648. return $dir;
  649. }
  650. protected function getDir($existent=true) {
  651. $dir = $this->typeDir;
  652. if (isset($this->get['dir']))
  653. $dir .= "/" . $this->get['dir'];
  654. if ($existent && (!is_dir($dir) || !is_readable($dir)))
  655. $this->errorMsg("Inexistant or inaccessible folder.");
  656. return $dir;
  657. }
  658. protected function getDirs($dir) {
  659. $dirs = dir::content($dir, array('types' => "dir"));
  660. $return = array();
  661. if (is_array($dirs)) {
  662. $writable = dir::isWritable($dir);
  663. foreach ($dirs as $cdir) {
  664. $info = $this->getDirInfo($cdir);
  665. if ($info === false) continue;
  666. $info['removable'] = $writable && $info['writable'];
  667. $return[] = $info;
  668. }
  669. }
  670. return $return;
  671. }
  672. protected function getDirInfo($dir, $removable=false) {
  673. if ((substr(basename($dir), 0, 1) == ".") || !is_dir($dir) || !is_readable($dir))
  674. return false;
  675. $dirs = dir::content($dir, array('types' => "dir"));
  676. if (is_array($dirs)) {
  677. foreach ($dirs as $key => $cdir)
  678. if (substr(basename($cdir), 0, 1) == ".")
  679. unset($dirs[$key]);
  680. $hasDirs = count($dirs) ? true : false;
  681. } else
  682. $hasDirs = false;
  683. $writable = dir::isWritable($dir);
  684. $info = array(
  685. 'name' => stripslashes(basename($dir)),
  686. 'readable' => is_readable($dir),
  687. 'writable' => $writable,
  688. 'removable' => $removable && $writable && dir::isWritable(dirname($dir)),
  689. 'hasDirs' => $hasDirs
  690. );
  691. if ($dir == "{$this->config['uploadDir']}/{$this->session['dir']}")
  692. $info['current'] = true;
  693. return $info;
  694. }
  695. protected function output($data=null, $template=null) {
  696. if (!is_array($data)) $data = array();
  697. if ($template === null)
  698. $template = $this->action;
  699. if (file_exists("tpl/tpl_$template.php")) {
  700. ob_start();
  701. $eval = "unset(\$data);unset(\$template);unset(\$eval);";
  702. $_ = $data;
  703. foreach (array_keys($data) as $key)
  704. if (preg_match('/^[a-z\d_]+$/i', $key))
  705. $eval .= "\$$key=\$_['$key'];";
  706. $eval .= "unset(\$_);require \"tpl/tpl_$template.php\";";
  707. eval($eval);
  708. return ob_get_clean();
  709. }
  710. return "";
  711. }
  712. protected function errorMsg($message, array $data=null) {
  713. if (in_array($this->action, array("thumb", "upload", "download", "downloadDir")))
  714. die($this->label($message, $data));
  715. if (($this->action === null) || ($this->action == "browser"))
  716. $this->backMsg($message, $data);
  717. else {
  718. $message = $this->label($message, $data);
  719. die(json_encode(array('error' => $message)));
  720. }
  721. }
  722. }
  723. ?>