PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/sondage/admin/scripts/kcfinder/core/browser.php

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