PageRenderTime 79ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/public/js/kcfinder/core/class/browser.php

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