PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/extensions/elfinder/php/elFinderVolumeLocalFileSystem.class.php

https://bitbucket.org/rohitrox/hotc
PHP | 835 lines | 422 code | 104 blank | 309 comment | 123 complexity | 04c42e5d140ce51df7242daafcfdbfcb MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * elFinder driver for local filesystem.
  4. *
  5. * @author Dmitry (dio) Levashov
  6. * @author Troex Nevelin
  7. **/
  8. class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver {
  9. /**
  10. * Driver id
  11. * Must be started from letter and contains [a-z0-9]
  12. * Used as part of volume id
  13. *
  14. * @var string
  15. **/
  16. protected $driverId = 'l';
  17. /**
  18. * Required to count total archive files size
  19. *
  20. * @var int
  21. **/
  22. protected $archiveSize = 0;
  23. /**
  24. * Constructor
  25. * Extend options with required fields
  26. *
  27. * @return void
  28. * @author Dmitry (dio) Levashov
  29. **/
  30. public function __construct() {
  31. $this->options['alias'] = ''; // alias to replace root dir name
  32. $this->options['dirMode'] = 0755; // new dirs mode
  33. $this->options['fileMode'] = 0644; // new files mode
  34. $this->options['quarantine'] = '.quarantine'; // quarantine folder name - required to check archive (must be hidden)
  35. $this->options['maxArcFilesSize'] = 0; // max allowed archive files size (0 - no limit)
  36. }
  37. /*********************************************************************/
  38. /* INIT AND CONFIGURE */
  39. /*********************************************************************/
  40. /**
  41. * Configure after successfull mount.
  42. *
  43. * @return void
  44. * @author Dmitry (dio) Levashov
  45. **/
  46. protected function configure() {
  47. $this->aroot = realpath($this->root);
  48. $root = $this->stat($this->root);
  49. if ($this->options['quarantine']) {
  50. $this->attributes[] = array(
  51. 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR.$this->options['quarantine']).'$~',
  52. 'read' => false,
  53. 'write' => false,
  54. 'locked' => true,
  55. 'hidden' => true
  56. );
  57. }
  58. // chek thumbnails path
  59. if ($this->options['tmbPath']) {
  60. $this->options['tmbPath'] = strpos($this->options['tmbPath'], DIRECTORY_SEPARATOR) === false
  61. // tmb path set as dirname under root dir
  62. ? $this->root.DIRECTORY_SEPARATOR.$this->options['tmbPath']
  63. // tmb path as full path
  64. : $this->_normpath($this->options['tmbPath']);
  65. }
  66. parent::configure();
  67. // if no thumbnails url - try detect it
  68. if ($root['read'] && !$this->tmbURL && $this->URL) {
  69. if (strpos($this->tmbPath, $this->root) === 0) {
  70. $this->tmbURL = $this->URL.str_replace(DIRECTORY_SEPARATOR, '/', substr($this->tmbPath, strlen($this->root)+1));
  71. if (preg_match("|[^/?&=]$|", $this->tmbURL)) {
  72. $this->tmbURL .= '/';
  73. }
  74. }
  75. }
  76. // check quarantine dir
  77. if (!empty($this->options['quarantine'])) {
  78. $this->quarantine = $this->root.DIRECTORY_SEPARATOR.$this->options['quarantine'];
  79. if ((!is_dir($this->quarantine) && !$this->_mkdir($this->root, $this->options['quarantine'])) || !is_writable($this->quarantine)) {
  80. $this->archivers['extract'] = array();
  81. $this->disabled[] = 'extract';
  82. }
  83. } else {
  84. $this->archivers['extract'] = array();
  85. $this->disabled[] = 'extract';
  86. }
  87. }
  88. /*********************************************************************/
  89. /* FS API */
  90. /*********************************************************************/
  91. /*********************** paths/urls *************************/
  92. /**
  93. * Return parent directory path
  94. *
  95. * @param string $path file path
  96. * @return string
  97. * @author Dmitry (dio) Levashov
  98. **/
  99. protected function _dirname($path) {
  100. return dirname($path);
  101. }
  102. /**
  103. * Return file name
  104. *
  105. * @param string $path file path
  106. * @return string
  107. * @author Dmitry (dio) Levashov
  108. **/
  109. protected function _basename($path) {
  110. return basename($path);
  111. }
  112. /**
  113. * Join dir name and file name and retur full path
  114. *
  115. * @param string $dir
  116. * @param string $name
  117. * @return string
  118. * @author Dmitry (dio) Levashov
  119. **/
  120. protected function _joinPath($dir, $name) {
  121. return $dir.DIRECTORY_SEPARATOR.$name;
  122. }
  123. /**
  124. * Return normalized path, this works the same as os.path.normpath() in Python
  125. *
  126. * @param string $path path
  127. * @return string
  128. * @author Troex Nevelin
  129. **/
  130. protected function _normpath($path) {
  131. if (empty($path)) {
  132. return '.';
  133. }
  134. if (strpos($path, '/') === 0) {
  135. $initial_slashes = true;
  136. } else {
  137. $initial_slashes = false;
  138. }
  139. if (($initial_slashes)
  140. && (strpos($path, '//') === 0)
  141. && (strpos($path, '///') === false)) {
  142. $initial_slashes = 2;
  143. }
  144. $initial_slashes = (int) $initial_slashes;
  145. $comps = explode('/', $path);
  146. $new_comps = array();
  147. foreach ($comps as $comp) {
  148. if (in_array($comp, array('', '.'))) {
  149. continue;
  150. }
  151. if (($comp != '..')
  152. || (!$initial_slashes && !$new_comps)
  153. || ($new_comps && (end($new_comps) == '..'))) {
  154. array_push($new_comps, $comp);
  155. } elseif ($new_comps) {
  156. array_pop($new_comps);
  157. }
  158. }
  159. $comps = $new_comps;
  160. $path = implode('/', $comps);
  161. if ($initial_slashes) {
  162. $path = str_repeat('/', $initial_slashes) . $path;
  163. }
  164. return $path ? $path : '.';
  165. }
  166. /**
  167. * Return file path related to root dir
  168. *
  169. * @param string $path file path
  170. * @return string
  171. * @author Dmitry (dio) Levashov
  172. **/
  173. protected function _relpath($path) {
  174. return $path == $this->root ? '' : substr($path, strlen($this->root)+1);
  175. }
  176. /**
  177. * Convert path related to root dir into real path
  178. *
  179. * @param string $path file path
  180. * @return string
  181. * @author Dmitry (dio) Levashov
  182. **/
  183. protected function _abspath($path) {
  184. return $path == DIRECTORY_SEPARATOR ? $this->root : $this->root.DIRECTORY_SEPARATOR.$path;
  185. }
  186. /**
  187. * Return fake path started from root dir
  188. *
  189. * @param string $path file path
  190. * @return string
  191. * @author Dmitry (dio) Levashov
  192. **/
  193. protected function _path($path) {
  194. return $this->rootName.($path == $this->root ? '' : $this->separator.$this->_relpath($path));
  195. }
  196. /**
  197. * Return true if $path is children of $parent
  198. *
  199. * @param string $path path to check
  200. * @param string $parent parent path
  201. * @return bool
  202. * @author Dmitry (dio) Levashov
  203. **/
  204. protected function _inpath($path, $parent) {
  205. return $path == $parent || strpos($path, $parent.DIRECTORY_SEPARATOR) === 0;
  206. }
  207. /***************** file stat ********************/
  208. /**
  209. * Return stat for given path.
  210. * Stat contains following fields:
  211. * - (int) size file size in b. required
  212. * - (int) ts file modification time in unix time. required
  213. * - (string) mime mimetype. required for folders, others - optionally
  214. * - (bool) read read permissions. required
  215. * - (bool) write write permissions. required
  216. * - (bool) locked is object locked. optionally
  217. * - (bool) hidden is object hidden. optionally
  218. * - (string) alias for symlinks - link target path relative to root path. optionally
  219. * - (string) target for symlinks - link target path. optionally
  220. *
  221. * If file does not exists - returns empty array or false.
  222. *
  223. * @param string $path file path
  224. * @return array|false
  225. * @author Dmitry (dio) Levashov
  226. **/
  227. protected function _stat($path) {
  228. $stat = array();
  229. if (!file_exists($path)) {
  230. return $stat;
  231. }
  232. if ($path != $this->root && is_link($path)) {
  233. if (($target = $this->readlink($path)) == false
  234. || $target == $path) {
  235. $stat['mime'] = 'symlink-broken';
  236. $stat['read'] = false;
  237. $stat['write'] = false;
  238. $stat['size'] = 0;
  239. return $stat;
  240. }
  241. $stat['alias'] = $this->_path($target);
  242. $stat['target'] = $target;
  243. $path = $target;
  244. $lstat = lstat($path);
  245. $size = $lstat['size'];
  246. } else {
  247. $size = @filesize($path);
  248. }
  249. $dir = is_dir($path);
  250. $stat['mime'] = $dir ? 'directory' : $this->mimetype($path);
  251. $stat['ts'] = filemtime($path);
  252. $stat['read'] = is_readable($path);
  253. $stat['write'] = is_writable($path);
  254. if ($stat['read']) {
  255. $stat['size'] = $dir ? 0 : $size;
  256. }
  257. return $stat;
  258. }
  259. /**
  260. * Return true if path is dir and has at least one childs directory
  261. *
  262. * @param string $path dir path
  263. * @return bool
  264. * @author Dmitry (dio) Levashov
  265. **/
  266. protected function _subdirs($path) {
  267. if (($dir = dir($path))) {
  268. $dir = dir($path);
  269. while (($entry = $dir->read()) !== false) {
  270. $p = $dir->path.DIRECTORY_SEPARATOR.$entry;
  271. if ($entry != '.' && $entry != '..' && is_dir($p) && !$this->attr($p, 'hidden')) {
  272. $dir->close();
  273. return true;
  274. }
  275. }
  276. $dir->close();
  277. }
  278. return false;
  279. }
  280. /**
  281. * Return object width and height
  282. * Ususaly used for images, but can be realize for video etc...
  283. *
  284. * @param string $path file path
  285. * @param string $mime file mime type
  286. * @return string
  287. * @author Dmitry (dio) Levashov
  288. **/
  289. protected function _dimensions($path, $mime) {
  290. clearstatcache();
  291. return strpos($mime, 'image') === 0 && ($s = @getimagesize($path)) !== false
  292. ? $s[0].'x'.$s[1]
  293. : false;
  294. }
  295. /******************** file/dir content *********************/
  296. /**
  297. * Return symlink target file
  298. *
  299. * @param string $path link path
  300. * @return string
  301. * @author Dmitry (dio) Levashov
  302. **/
  303. protected function readlink($path) {
  304. if (!($target = @readlink($path))) {
  305. return false;
  306. }
  307. if (substr($target, 0, 1) != DIRECTORY_SEPARATOR) {
  308. $target = dirname($path).DIRECTORY_SEPARATOR.$target;
  309. }
  310. $atarget = realpath($target);
  311. if (!$atarget) {
  312. return false;
  313. }
  314. $root = $this->root;
  315. $aroot = $this->aroot;
  316. if ($this->_inpath($atarget, $this->aroot)) {
  317. return $this->_normpath($this->root.DIRECTORY_SEPARATOR.substr($atarget, strlen($this->aroot)+1));
  318. }
  319. return false;
  320. }
  321. /**
  322. * Return files list in directory.
  323. *
  324. * @param string $path dir path
  325. * @return array
  326. * @author Dmitry (dio) Levashov
  327. **/
  328. protected function _scandir($path) {
  329. $files = array();
  330. foreach (scandir($path) as $name) {
  331. if ($name != '.' && $name != '..') {
  332. $files[] = $path.DIRECTORY_SEPARATOR.$name;
  333. }
  334. }
  335. return $files;
  336. }
  337. /**
  338. * Open file and return file pointer
  339. *
  340. * @param string $path file path
  341. * @param bool $write open file for writing
  342. * @return resource|false
  343. * @author Dmitry (dio) Levashov
  344. **/
  345. protected function _fopen($path, $mode='rb') {
  346. return @fopen($path, 'r');
  347. }
  348. /**
  349. * Close opened file
  350. *
  351. * @param resource $fp file pointer
  352. * @return bool
  353. * @author Dmitry (dio) Levashov
  354. **/
  355. protected function _fclose($fp, $path='') {
  356. return @fclose($fp);
  357. }
  358. /******************** file/dir manipulations *************************/
  359. /**
  360. * Create dir and return created dir path or false on failed
  361. *
  362. * @param string $path parent dir path
  363. * @param string $name new directory name
  364. * @return string|bool
  365. * @author Dmitry (dio) Levashov
  366. **/
  367. protected function _mkdir($path, $name) {
  368. $path = $path.DIRECTORY_SEPARATOR.$name;
  369. if (@mkdir($path)) {
  370. @chmod($path, $this->options['dirMode']);
  371. return $path;
  372. }
  373. return false;
  374. }
  375. /**
  376. * Create file and return it's path or false on failed
  377. *
  378. * @param string $path parent dir path
  379. * @param string $name new file name
  380. * @return string|bool
  381. * @author Dmitry (dio) Levashov
  382. **/
  383. protected function _mkfile($path, $name) {
  384. $path = $path.DIRECTORY_SEPARATOR.$name;
  385. if (($fp = @fopen($path, 'w'))) {
  386. @fclose($fp);
  387. @chmod($path, $this->options['fileMode']);
  388. return $path;
  389. }
  390. return false;
  391. }
  392. /**
  393. * Create symlink
  394. *
  395. * @param string $source file to link to
  396. * @param string $targetDir folder to create link in
  397. * @param string $name symlink name
  398. * @return bool
  399. * @author Dmitry (dio) Levashov
  400. **/
  401. protected function _symlink($source, $targetDir, $name) {
  402. return @symlink($source, $targetDir.DIRECTORY_SEPARATOR.$name);
  403. }
  404. /**
  405. * Copy file into another file
  406. *
  407. * @param string $source source file path
  408. * @param string $targetDir target directory path
  409. * @param string $name new file name
  410. * @return bool
  411. * @author Dmitry (dio) Levashov
  412. **/
  413. protected function _copy($source, $targetDir, $name) {
  414. return copy($source, $targetDir.DIRECTORY_SEPARATOR.$name);
  415. }
  416. /**
  417. * Move file into another parent dir.
  418. * Return new file path or false.
  419. *
  420. * @param string $source source file path
  421. * @param string $target target dir path
  422. * @param string $name file name
  423. * @return string|bool
  424. * @author Dmitry (dio) Levashov
  425. **/
  426. protected function _move($source, $targetDir, $name) {
  427. $target = $targetDir.DIRECTORY_SEPARATOR.$name;
  428. return @rename($source, $target) ? $target : false;
  429. }
  430. /**
  431. * Remove file
  432. *
  433. * @param string $path file path
  434. * @return bool
  435. * @author Dmitry (dio) Levashov
  436. **/
  437. protected function _unlink($path) {
  438. return @unlink($path);
  439. }
  440. /**
  441. * Remove dir
  442. *
  443. * @param string $path dir path
  444. * @return bool
  445. * @author Dmitry (dio) Levashov
  446. **/
  447. protected function _rmdir($path) {
  448. return @rmdir($path);
  449. }
  450. /**
  451. * Create new file and write into it from file pointer.
  452. * Return new file path or false on error.
  453. *
  454. * @param resource $fp file pointer
  455. * @param string $dir target dir path
  456. * @param string $name file name
  457. * @return bool|string
  458. * @author Dmitry (dio) Levashov
  459. **/
  460. protected function _save($fp, $dir, $name, $mime, $w, $h) {
  461. $path = $dir.DIRECTORY_SEPARATOR.$name;
  462. if (!($target = @fopen($path, 'wb'))) {
  463. return false;
  464. }
  465. while (!feof($fp)) {
  466. fwrite($target, fread($fp, 8192));
  467. }
  468. fclose($target);
  469. @chmod($path, $this->options['fileMode']);
  470. clearstatcache();
  471. return $path;
  472. }
  473. /**
  474. * Get file contents
  475. *
  476. * @param string $path file path
  477. * @return string|false
  478. * @author Dmitry (dio) Levashov
  479. **/
  480. protected function _getContents($path) {
  481. return file_get_contents($path);
  482. }
  483. /**
  484. * Write a string to a file
  485. *
  486. * @param string $path file path
  487. * @param string $content new file content
  488. * @return bool
  489. * @author Dmitry (dio) Levashov
  490. **/
  491. protected function _filePutContents($path, $content) {
  492. if (@file_put_contents($path, $content, LOCK_EX) !== false) {
  493. clearstatcache();
  494. return true;
  495. }
  496. return false;
  497. }
  498. /**
  499. * Detect available archivers
  500. *
  501. * @return void
  502. **/
  503. protected function _checkArchivers() {
  504. if (!function_exists('exec')) {
  505. $this->options['archivers'] = $this->options['archive'] = array();
  506. return;
  507. }
  508. $arcs = array(
  509. 'create' => array(),
  510. 'extract' => array()
  511. );
  512. //exec('tar --version', $o, $ctar);
  513. $this->procExec('tar --version', $o, $ctar);
  514. if ($ctar == 0) {
  515. $arcs['create']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-cf', 'ext' => 'tar');
  516. $arcs['extract']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-xf', 'ext' => 'tar');
  517. //$test = exec('gzip --version', $o, $c);
  518. unset($o);
  519. $test = $this->procExec('gzip --version', $o, $c);
  520. if ($c == 0) {
  521. $arcs['create']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-czf', 'ext' => 'tgz');
  522. $arcs['extract']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-xzf', 'ext' => 'tgz');
  523. }
  524. unset($o);
  525. //$test = exec('bzip2 --version', $o, $c);
  526. $test = $this->procExec('bzip2 --version', $o, $c);
  527. if ($c == 0) {
  528. $arcs['create']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-cjf', 'ext' => 'tbz');
  529. $arcs['extract']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-xjf', 'ext' => 'tbz');
  530. }
  531. }
  532. unset($o);
  533. //exec('zip --version', $o, $c);
  534. $this->procExec('zip -v', $o, $c);
  535. if ($c == 0) {
  536. $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
  537. }
  538. unset($o);
  539. $this->procExec('unzip --help', $o, $c);
  540. if ($c == 0) {
  541. $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
  542. }
  543. unset($o);
  544. //exec('rar --version', $o, $c);
  545. $this->procExec('rar --version', $o, $c);
  546. if ($c == 0 || $c == 7) {
  547. $arcs['create']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'a -inul', 'ext' => 'rar');
  548. $arcs['extract']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'x -y', 'ext' => 'rar');
  549. } else {
  550. unset($o);
  551. //$test = exec('unrar', $o, $c);
  552. $test = $this->procExec('unrar', $o, $c);
  553. if ($c==0 || $c == 7) {
  554. $arcs['extract']['application/x-rar'] = array('cmd' => 'unrar', 'argc' => 'x -y', 'ext' => 'rar');
  555. }
  556. }
  557. unset($o);
  558. //exec('7za --help', $o, $c);
  559. $this->procExec('7za --help', $o, $c);
  560. if ($c == 0) {
  561. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'a', 'ext' => '7z');
  562. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'e -y', 'ext' => '7z');
  563. if (empty($arcs['create']['application/x-gzip'])) {
  564. $arcs['create']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'a -tgzip', 'ext' => 'tar.gz');
  565. }
  566. if (empty($arcs['extract']['application/x-gzip'])) {
  567. $arcs['extract']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'e -tgzip -y', 'ext' => 'tar.gz');
  568. }
  569. if (empty($arcs['create']['application/x-bzip2'])) {
  570. $arcs['create']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2', 'ext' => 'tar.bz');
  571. }
  572. if (empty($arcs['extract']['application/x-bzip2'])) {
  573. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2 -y', 'ext' => 'tar.bz');
  574. }
  575. if (empty($arcs['create']['application/zip'])) {
  576. $arcs['create']['application/zip'] = array('cmd' => '7za', 'argc' => 'a -tzip -l', 'ext' => 'zip');
  577. }
  578. if (empty($arcs['extract']['application/zip'])) {
  579. $arcs['extract']['application/zip'] = array('cmd' => '7za', 'argc' => 'e -tzip -y', 'ext' => 'zip');
  580. }
  581. if (empty($arcs['create']['application/x-tar'])) {
  582. $arcs['create']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'a -ttar -l', 'ext' => 'tar');
  583. }
  584. if (empty($arcs['extract']['application/x-tar'])) {
  585. $arcs['extract']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'e -ttar -y', 'ext' => 'tar');
  586. }
  587. }
  588. $this->archivers = $arcs;
  589. }
  590. /**
  591. * Unpack archive
  592. *
  593. * @param string $path archive path
  594. * @param array $arc archiver command and arguments (same as in $this->archivers)
  595. * @return void
  596. * @author Dmitry (dio) Levashov
  597. * @author Alexey Sukhotin
  598. **/
  599. protected function _unpack($path, $arc) {
  600. $cwd = getcwd();
  601. $dir = $this->_dirname($path);
  602. chdir($dir);
  603. $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($this->_basename($path));
  604. $this->procExec($cmd, $o, $c);
  605. chdir($cwd);
  606. }
  607. /**
  608. * Recursive symlinks search
  609. *
  610. * @param string $path file/dir path
  611. * @return bool
  612. * @author Dmitry (dio) Levashov
  613. **/
  614. protected function _findSymlinks($path) {
  615. if (is_link($path)) {
  616. return true;
  617. }
  618. if (is_dir($path)) {
  619. foreach (scandir($path) as $name) {
  620. if ($name != '.' && $name != '..') {
  621. $p = $path.DIRECTORY_SEPARATOR.$name;
  622. if (is_link($p)) {
  623. return true;
  624. }
  625. if (is_dir($p) && $this->_findSymlinks($p)) {
  626. return true;
  627. } elseif (is_file($p)) {
  628. $this->archiveSize += filesize($p);
  629. }
  630. }
  631. }
  632. } else {
  633. $this->archiveSize += filesize($path);
  634. }
  635. return false;
  636. }
  637. /**
  638. * Extract files from archive
  639. *
  640. * @param string $path archive path
  641. * @param array $arc archiver command and arguments (same as in $this->archivers)
  642. * @return true
  643. * @author Dmitry (dio) Levashov,
  644. * @author Alexey Sukhotin
  645. **/
  646. protected function _extract($path, $arc) {
  647. if ($this->quarantine) {
  648. $dir = $this->quarantine.DIRECTORY_SEPARATOR.str_replace(' ', '_', microtime()).basename($path);
  649. $archive = $dir.DIRECTORY_SEPARATOR.basename($path);
  650. if (!@mkdir($dir)) {
  651. return false;
  652. }
  653. chmod($dir, 0777);
  654. // copy in quarantine
  655. if (!copy($path, $archive)) {
  656. return false;
  657. }
  658. // extract in quarantine
  659. $this->_unpack($archive, $arc);
  660. @unlink($archive);
  661. // get files list
  662. $ls = array();
  663. foreach (scandir($dir) as $i => $name) {
  664. if ($name != '.' && $name != '..') {
  665. $ls[] = $name;
  666. }
  667. }
  668. // no files - extract error ?
  669. if (empty($ls)) {
  670. return false;
  671. }
  672. $this->archiveSize = 0;
  673. // find symlinks
  674. $symlinks = $this->_findSymlinks($dir);
  675. // remove arc copy
  676. $this->remove($dir);
  677. if ($symlinks) {
  678. return $this->setError(elFinder::ERROR_ARC_SYMLINKS);
  679. }
  680. // check max files size
  681. if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
  682. return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
  683. }
  684. // archive contains one item - extract in archive dir
  685. if (count($ls) == 1) {
  686. $this->_unpack($path, $arc);
  687. $result = dirname($path).DIRECTORY_SEPARATOR.$ls[0];
  688. } else {
  689. // for several files - create new directory
  690. // create unique name for directory
  691. $name = basename($path);
  692. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  693. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  694. }
  695. $test = dirname($path).DIRECTORY_SEPARATOR.$name;
  696. if (file_exists($test) || is_link($test)) {
  697. $name = $this->uniqueName(dirname($path), $name, '-', false);
  698. }
  699. $result = dirname($path).DIRECTORY_SEPARATOR.$name;
  700. $archive = $result.DIRECTORY_SEPARATOR.basename($path);
  701. if (!$this->_mkdir(dirname($path), $name) || !copy($path, $archive)) {
  702. return false;
  703. }
  704. $this->_unpack($archive, $arc);
  705. @unlink($archive);
  706. }
  707. return file_exists($result) ? $result : false;
  708. }
  709. }
  710. /**
  711. * Create archive and return its path
  712. *
  713. * @param string $dir target dir
  714. * @param array $files files names list
  715. * @param string $name archive name
  716. * @param array $arc archiver options
  717. * @return string|bool
  718. * @author Dmitry (dio) Levashov,
  719. * @author Alexey Sukhotin
  720. **/
  721. protected function _archive($dir, $files, $name, $arc) {
  722. $cwd = getcwd();
  723. chdir($dir);
  724. $files = array_map('escapeshellarg', $files);
  725. $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($name).' '.implode(' ', $files);
  726. $this->procExec($cmd, $o, $c);
  727. chdir($cwd);
  728. $path = $dir.DIRECTORY_SEPARATOR.$name;
  729. return file_exists($path) ? $path : false;
  730. }
  731. } // END class