PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/application/protected/extensions/widgets/editor/source/kcfinder/core/browser.php

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