PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/_file-manager/php/elFinderVolumeLocalFileSystem.class.php

https://github.com/MatchBlood/landingPage
PHP | 956 lines | 457 code | 117 blank | 382 comment | 131 complexity | af4651f001d19d4771623143d7ba92f4 MD5 | raw file
  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. $arcs = array(
  505. 'create' => array(),
  506. 'extract' => array()
  507. );
  508. //exec('tar --version', $o, $ctar);
  509. $this->procExec('tar --version', $o, $ctar);
  510. if ($ctar == 0) {
  511. //$arcs['create']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-cf', 'ext' => 'tar');
  512. //$arcs['extract']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-xf', 'ext' => 'tar');
  513. //$test = exec('gzip --version', $o, $c);
  514. unset($o);
  515. $test = $this->procExec('gzip --version', $o, $c);
  516. if ($c == 0) {
  517. //$arcs['create']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-czf', 'ext' => 'tgz');
  518. //$arcs['extract']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-xzf', 'ext' => 'tgz');
  519. }
  520. //unset($o);
  521. //$test = exec('bzip2 --version', $o, $c);
  522. $test = $this->procExec('bzip2 --version', $o, $c);
  523. if ($c == 0) {
  524. //$arcs['create']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-cjf', 'ext' => 'tbz');
  525. //$arcs['extract']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-xjf', 'ext' => 'tbz');
  526. }
  527. }
  528. //unset($o);
  529. //exec('zip --version', $o, $c);
  530. $this->procExec('zip -v', $o, $c);
  531. if ($c == 0) {
  532. $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
  533. }
  534. unset($o);
  535. $this->procExec('unzip --help', $o, $c);
  536. if ($c == 0) {
  537. $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
  538. }
  539. unset($o);
  540. //exec('rar --version', $o, $c);
  541. $this->procExec('rar --version', $o, $c);
  542. $c = 0;
  543. if ($c == 0 || $c == 7) {
  544. //$arcs['create']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'a -inul', 'ext' => 'rar');
  545. $arcs['extract']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'x -y', 'ext' => 'rar');
  546. } else {
  547. unset($o);
  548. //$test = exec('unrar', $o, $c);
  549. $test = $this->procExec('unrar', $o, $c);
  550. if ($c==0 || $c == 7) {
  551. $arcs['extract']['application/x-rar'] = array('cmd' => 'unrar', 'argc' => 'x -y', 'ext' => 'rar');
  552. }
  553. }
  554. unset($o);
  555. //exec('7za --help', $o, $c);
  556. /*$this->procExec('7za --help', $o, $c);
  557. if ($c == 0) {
  558. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'a', 'ext' => '7z');
  559. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'e -y', 'ext' => '7z');
  560. if (empty($arcs['create']['application/x-gzip'])) {
  561. $arcs['create']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'a -tgzip', 'ext' => 'tar.gz');
  562. }
  563. if (empty($arcs['extract']['application/x-gzip'])) {
  564. $arcs['extract']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'e -tgzip -y', 'ext' => 'tar.gz');
  565. }
  566. if (empty($arcs['create']['application/x-bzip2'])) {
  567. $arcs['create']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2', 'ext' => 'tar.bz');
  568. }
  569. if (empty($arcs['extract']['application/x-bzip2'])) {
  570. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2 -y', 'ext' => 'tar.bz');
  571. }
  572. if (empty($arcs['create']['application/zip'])) {
  573. $arcs['create']['application/zip'] = array('cmd' => '7za', 'argc' => 'a -tzip -l', 'ext' => 'zip');
  574. }
  575. if (empty($arcs['extract']['application/zip'])) {
  576. $arcs['extract']['application/zip'] = array('cmd' => '7za', 'argc' => 'e -tzip -y', 'ext' => 'zip');
  577. }
  578. if (empty($arcs['create']['application/x-tar'])) {
  579. $arcs['create']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'a -ttar -l', 'ext' => 'tar');
  580. }
  581. if (empty($arcs['extract']['application/x-tar'])) {
  582. $arcs['extract']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'e -ttar -y', 'ext' => 'tar');
  583. }
  584. }*/
  585. $this->archivers = $arcs;
  586. /*$zip = zip_open($cmd);
  587. if ($zip) {
  588. while ($zip_entry = zip_read($zip)) {
  589. echo "Name: " . zip_entry_name($zip_entry) . "\n";
  590. echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "\n";
  591. echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
  592. echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";
  593. if (zip_entry_open($zip, $zip_entry, "r")) {
  594. echo "File Contents:\n";
  595. $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  596. echo "$buf\n";
  597. zip_entry_close($zip_entry);
  598. }
  599. echo "\n";
  600. }
  601. zip_close($zip);
  602. }*/
  603. }
  604. /**
  605. * Unpack archive
  606. *
  607. * @param string $path archive path
  608. * @param array $arc archiver command and arguments (same as in $this->archivers)
  609. * @return void
  610. * @author Dmitry (dio) Levashov
  611. * @author Alexey Sukhotin
  612. **/
  613. protected function _unpack($path, $arc) {
  614. $cwd = getcwd();
  615. $dir = $this->_dirname($path);
  616. chdir($dir);
  617. //$cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($this->_basename($path));
  618. $change = array(".zip",".rar");
  619. $with = array("","");
  620. $newFolderName = str_replace($change, $with, $this->_basename($path));
  621. $newFolderName2 = $newFolderName;
  622. for($fld=1; $fld<100; $fld++) {
  623. if(file_exists($newFolderName)) {
  624. $newFolderName = $newFolderName2 . '-' . $fld;
  625. }
  626. else { continue; }
  627. }
  628. /* FIX 2012.12.11 */
  629. if(preg_match('/\.zip$/i', $this->_basename($path)))
  630. {
  631. $zip = new ZipArchive;
  632. if ($zip->open($this->_basename($path)) === TRUE) { /* $this->_basename($path) - failo vardas */
  633. $zip->extractTo('./'.$newFolderName); //'.'.$this->_basename($path).'/'
  634. $zip->close();
  635. } else {}
  636. //$this->procExec($cmd, $o, $c);
  637. }
  638. elseif(preg_match('/\.rar$/i', $this->_basename($path)))
  639. {
  640. $rar_file = rar_open($this->_basename($path)) or die();
  641. $entries = rar_list($rar_file);
  642. foreach ($entries as $entry) {
  643. $entry->extract('./'.$newFolderName);
  644. }
  645. rar_close($rar_file);
  646. }
  647. chdir($cwd);
  648. }
  649. /**
  650. * Recursive symlinks search
  651. *
  652. * @param string $path file/dir path
  653. * @return bool
  654. * @author Dmitry (dio) Levashov
  655. **/
  656. protected function _findSymlinks($path) {
  657. if (is_link($path)) {
  658. return true;
  659. }
  660. if (is_dir($path)) {
  661. foreach (scandir($path) as $name) {
  662. if ($name != '.' && $name != '..') {
  663. $p = $path.DIRECTORY_SEPARATOR.$name;
  664. if (is_link($p)) {
  665. return true;
  666. }
  667. if (is_dir($p) && $this->_findSymlinks($p)) {
  668. return true;
  669. } elseif (is_file($p)) {
  670. $this->archiveSize += filesize($p);
  671. }
  672. }
  673. }
  674. } else {
  675. $this->archiveSize += filesize($path);
  676. }
  677. return false;
  678. }
  679. /**
  680. * Extract files from archive
  681. *
  682. * @param string $path archive path
  683. * @param array $arc archiver command and arguments (same as in $this->archivers)
  684. * @return true
  685. * @author Dmitry (dio) Levashov,
  686. * @author Alexey Sukhotin
  687. **/
  688. protected function _extract($path, $arc) {
  689. if ($this->quarantine) {
  690. $dir = $this->quarantine.DIRECTORY_SEPARATOR.str_replace(' ', '_', microtime()).basename($path);
  691. $archive = $dir.DIRECTORY_SEPARATOR.basename($path);
  692. if (!@mkdir($dir)) {
  693. return false;
  694. }
  695. chmod($dir, 0777);
  696. // copy in quarantine
  697. if (!copy($path, $archive)) {
  698. return false;
  699. }
  700. // extract in quarantine
  701. $this->_unpack($archive, $arc);
  702. @unlink($archive);
  703. // get files list
  704. $ls = array();
  705. foreach (scandir($dir) as $i => $name) {
  706. if ($name != '.' && $name != '..') {
  707. $ls[] = $name;
  708. }
  709. }
  710. // no files - extract error ?
  711. if (empty($ls)) {
  712. //return false;
  713. }
  714. $this->archiveSize = 0;
  715. // find symlinks
  716. $symlinks = $this->_findSymlinks($dir);
  717. // remove arc copy
  718. $this->remove($dir);
  719. if ($symlinks) {
  720. return $this->setError(elFinder::ERROR_ARC_SYMLINKS);
  721. }
  722. // check max files size
  723. if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
  724. return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
  725. }
  726. // archive contains one item - extract in archive dir
  727. if (count($ls) == 1) {
  728. $this->_unpack($path, $arc);
  729. $result = dirname($path).DIRECTORY_SEPARATOR.$ls[0];
  730. } else {
  731. // for several files - create new directory
  732. // create unique name for directory
  733. $name = basename($path);
  734. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  735. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  736. }
  737. $test = dirname($path).DIRECTORY_SEPARATOR.$name;
  738. if (file_exists($test) || is_link($test)) {
  739. $name = $this->uniqueName(dirname($path), $name, '-', false);
  740. }
  741. $result = dirname($path).DIRECTORY_SEPARATOR.$name;
  742. $archive = $result.DIRECTORY_SEPARATOR.basename($path);
  743. if (!$this->_mkdir(dirname($path), $name) || !copy($path, $archive)) {
  744. return false;
  745. }
  746. $this->_unpack($archive, $arc);
  747. @unlink($archive);
  748. }
  749. return file_exists($result) ? $result : false;
  750. }
  751. }
  752. /**
  753. * Create archive and return its path
  754. *
  755. * @param string $dir target dir
  756. * @param array $files files names list
  757. * @param string $name archive name
  758. * @param array $arc archiver options
  759. * @return string|bool
  760. * @author Dmitry (dio) Levashov,
  761. * @author Alexey Sukhotin
  762. * c = failo name
  763. * o = komandines eilutes dalis
  764. **/
  765. protected function _archive($dir, $files, $name, $arc) {
  766. $cwd = getcwd();
  767. chdir($dir);
  768. $files = array_map('escapeshellarg', $files);
  769. function ZipIt($source, $destination, $files)
  770. {
  771. if (!extension_loaded('zip') || !file_exists($source)) {
  772. return false;
  773. }
  774. $zip = new ZipArchive();
  775. if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
  776. return false;
  777. }
  778. $source = str_replace('\\', '/', realpath($source));
  779. //echo $source ."\n";
  780. if (is_dir($source) === true)
  781. {
  782. //$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
  783. foreach ($files as $file)
  784. {
  785. $file = str_replace('\\', '/', $file);
  786. $file = substr($file, 0, -1);
  787. $file = substr($file, 1 );
  788. // Ignore "." and ".." folders
  789. if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
  790. continue;
  791. $file = realpath($file);
  792. if (is_dir($file) === true)
  793. {
  794. $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
  795. /* FIX 2012.12.12 */
  796. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(str_replace($source . '/', '', $file . '/')));
  797. //echo '.'.str_replace($source . '/', '', $file . '/');
  798. foreach ($iterator as $key=>$value) {
  799. if( in_array(substr($key, strrpos($key, '/')+1), array('.', '..')) )
  800. continue;
  801. if (is_dir($key) === true)
  802. {
  803. $zip->addEmptyDir($key); //GOOD
  804. }
  805. else if (is_file($key) === true)
  806. {
  807. $zip->addFile($key, $key) or die ("ERROR: Could not add file: $key"); //GOOD
  808. }
  809. }
  810. }
  811. else if (is_file($file) === true)
  812. {
  813. $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
  814. }
  815. }
  816. }
  817. else if (is_file($source) === true)
  818. {
  819. $zip->addFromString(basename($source), file_get_contents($source));
  820. }
  821. return $zip->close();
  822. }
  823. ZipIt('.', './'.$name.'', $files);
  824. chdir($cwd);
  825. $path = $dir.DIRECTORY_SEPARATOR.$name;
  826. return file_exists($path) ? $path : false;
  827. }
  828. } // END class