PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/kcfinder/core/class/browser.php

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