PageRenderTime 77ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/php/elFinderVolumeDriver.class.php

https://bitbucket.org/dschmid/html5blog
PHP | 3370 lines | 1697 code | 472 blank | 1201 comment | 564 complexity | 7f1ed785f6a470b5c39e5839de212944 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Base class for elFinder volume.
  4. * Provide 2 layers:
  5. * 1. Public API (commands)
  6. * 2. abstract fs API
  7. *
  8. * All abstract methods begin with "_"
  9. *
  10. * @author Dmitry (dio) Levashov
  11. * @author Troex Nevelin
  12. * @author Alexey Sukhotin
  13. **/
  14. abstract class elFinderVolumeDriver {
  15. /**
  16. * Driver id
  17. * Must be started from letter and contains [a-z0-9]
  18. * Used as part of volume id
  19. *
  20. * @var string
  21. **/
  22. protected $driverId = 'a';
  23. /**
  24. * Volume id - used as prefix for files hashes
  25. *
  26. * @var string
  27. **/
  28. protected $id = '';
  29. /**
  30. * Flag - volume "mounted" and available
  31. *
  32. * @var bool
  33. **/
  34. protected $mounted = false;
  35. /**
  36. * Root directory path
  37. *
  38. * @var string
  39. **/
  40. protected $root = '';
  41. /**
  42. * Root basename | alias
  43. *
  44. * @var string
  45. **/
  46. protected $rootName = '';
  47. /**
  48. * Default directory to open
  49. *
  50. * @var string
  51. **/
  52. protected $startPath = '';
  53. /**
  54. * Base URL
  55. *
  56. * @var string
  57. **/
  58. protected $URL = '';
  59. /**
  60. * Thumbnails dir path
  61. *
  62. * @var string
  63. **/
  64. protected $tmbPath = '';
  65. /**
  66. * Is thumbnails dir writable
  67. *
  68. * @var bool
  69. **/
  70. protected $tmbPathWritable = false;
  71. /**
  72. * Thumbnails base URL
  73. *
  74. * @var string
  75. **/
  76. protected $tmbURL = '';
  77. /**
  78. * Thumbnails size in px
  79. *
  80. * @var int
  81. **/
  82. protected $tmbSize = 48;
  83. /**
  84. * Image manipulation lib name
  85. * auto|imagick|mogtify|gd
  86. *
  87. * @var string
  88. **/
  89. protected $imgLib = 'auto';
  90. /**
  91. * Library to crypt files name
  92. *
  93. * @var string
  94. **/
  95. protected $cryptLib = '';
  96. /**
  97. * Archivers config
  98. *
  99. * @var array
  100. **/
  101. protected $archivers = array(
  102. 'create' => array(),
  103. 'extract' => array()
  104. );
  105. /**
  106. * How many subdirs levels return for tree
  107. *
  108. * @var int
  109. **/
  110. protected $treeDeep = 1;
  111. /**
  112. * Errors from last failed action
  113. *
  114. * @var array
  115. **/
  116. protected $error = array();
  117. /**
  118. * Today 24:00 timestamp
  119. *
  120. * @var int
  121. **/
  122. protected $today = 0;
  123. /**
  124. * Yesterday 24:00 timestamp
  125. *
  126. * @var int
  127. **/
  128. protected $yesterday = 0;
  129. /**
  130. * Object configuration
  131. *
  132. * @var array
  133. **/
  134. protected $options = array(
  135. 'id' => '',
  136. // root directory path
  137. 'path' => '',
  138. // open this path on initial request instead of root path
  139. 'startPath' => '',
  140. // how many subdirs levels return per request
  141. 'treeDeep' => 1,
  142. // root url, not set to disable sending URL to client (replacement for old "fileURL" option)
  143. 'URL' => '',
  144. // directory separator. required by client to show paths correctly
  145. 'separator' => DIRECTORY_SEPARATOR,
  146. // library to crypt/uncrypt files names (not implemented)
  147. 'cryptLib' => '',
  148. // how to detect files mimetypes. (auto/internal/finfo/mime_content_type)
  149. 'mimeDetect' => 'auto',
  150. // mime.types file path (for mimeDetect==internal)
  151. 'mimefile' => '',
  152. // directory for thumbnails
  153. 'tmbPath' => '.tmb',
  154. // mode to create thumbnails dir
  155. 'tmbPathMode' => 0777,
  156. // thumbnails dir URL. Set it if store thumbnails outside root directory
  157. 'tmbURL' => '',
  158. // thumbnails size (px)
  159. 'tmbSize' => 48,
  160. // thumbnails crop (true - crop, false - scale image to fit thumbnail size)
  161. 'tmbCrop' => true,
  162. // thumbnails background color (hex #rrggbb or 'transparent')
  163. 'tmbBgColor' => '#ffffff',
  164. // image manipulations library
  165. 'imgLib' => 'auto',
  166. // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  167. 'copyOverwrite' => true,
  168. // if true - join new and old directories content on paste
  169. 'copyJoin' => true,
  170. // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  171. 'uploadOverwrite' => true,
  172. // mimetypes allowed to upload
  173. 'uploadAllow' => array(),
  174. // mimetypes not allowed to upload
  175. 'uploadDeny' => array(),
  176. // order to proccess uploadAllow and uploadDeny options
  177. 'uploadOrder' => array('deny', 'allow'),
  178. // maximum upload file size. NOTE - this is size for every uploaded files
  179. 'uploadMaxSize' => 0,
  180. // files dates format
  181. 'dateFormat' => 'j M Y H:i',
  182. // files time format
  183. 'timeFormat' => 'H:i',
  184. // if true - every folder will be check for children folders, otherwise all folders will be marked as having subfolders
  185. 'checkSubfolders' => true,
  186. // allow to copy from this volume to other ones?
  187. 'copyFrom' => true,
  188. // allow to copy from other volumes to this one?
  189. 'copyTo' => true,
  190. // list of commands disabled on this root
  191. 'disabled' => array(),
  192. // regexp or function name to validate new file name
  193. 'acceptedName' => '/^\w[\w\s\.\%\-\(\)\[\]]*$/u',
  194. // function/class method to control files permissions
  195. 'accessControl' => null,
  196. // some data required by access control
  197. 'accessControlData' => null,
  198. // default permissions. not set hidden/locked here - take no effect
  199. 'defaults' => array(
  200. 'read' => true,
  201. 'write' => true
  202. ),
  203. // files attributes
  204. 'attributes' => array(),
  205. // Allowed archive's mimetypes to create. Leave empty for all available types.
  206. 'archiveMimes' => array(),
  207. // Manual config for archivers. See example below. Leave empty for auto detect
  208. 'archivers' => array(),
  209. // required to fix bug on macos
  210. 'utf8fix' => false,
  211. // ? ? ? ? Ø Å
  212. 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"),
  213. 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5")
  214. );
  215. /**
  216. * Defaults permissions
  217. *
  218. * @var array
  219. **/
  220. protected $defaults = array(
  221. 'read' => true,
  222. 'write' => true,
  223. 'locked' => false,
  224. 'hidden' => false
  225. );
  226. /**
  227. * Access control function/class
  228. *
  229. * @var mixed
  230. **/
  231. protected $attributes = array();
  232. /**
  233. * Access control function/class
  234. *
  235. * @var mixed
  236. **/
  237. protected $access = null;
  238. /**
  239. * Mime types allowed to upload
  240. *
  241. * @var array
  242. **/
  243. protected $uploadAllow = array();
  244. /**
  245. * Mime types denied to upload
  246. *
  247. * @var array
  248. **/
  249. protected $uploadDeny = array();
  250. /**
  251. * Order to validate uploadAllow and uploadDeny
  252. *
  253. * @var array
  254. **/
  255. protected $uploadOrder = array();
  256. /**
  257. * Maximum allowed upload file size.
  258. * Set as number or string with unit - "10M", "500K", "1G"
  259. *
  260. * @var int|string
  261. **/
  262. protected $uploadMaxSize = 0;
  263. /**
  264. * Mimetype detect method
  265. *
  266. * @var string
  267. **/
  268. protected $mimeDetect = 'auto';
  269. /**
  270. * Flag - mimetypes from externail file was loaded
  271. *
  272. * @var bool
  273. **/
  274. private static $mimetypesLoaded = false;
  275. /**
  276. * Finfo object for mimeDetect == 'finfo'
  277. *
  278. * @var object
  279. **/
  280. protected $finfo = null;
  281. /**
  282. * List of disabled client's commands
  283. *
  284. * @var array
  285. **/
  286. protected $diabled = array();
  287. /**
  288. * default extensions/mimetypes for mimeDetect == 'internal'
  289. *
  290. * @var array
  291. **/
  292. protected static $mimetypes = array(
  293. // applications
  294. 'ai' => 'application/postscript',
  295. 'eps' => 'application/postscript',
  296. 'exe' => 'application/x-executable',
  297. 'doc' => 'application/vnd.ms-word',
  298. 'xls' => 'application/vnd.ms-excel',
  299. 'ppt' => 'application/vnd.ms-powerpoint',
  300. 'pps' => 'application/vnd.ms-powerpoint',
  301. 'pdf' => 'application/pdf',
  302. 'xml' => 'application/xml',
  303. 'odt' => 'application/vnd.oasis.opendocument.text',
  304. 'swf' => 'application/x-shockwave-flash',
  305. 'torrent' => 'application/x-bittorrent',
  306. 'jar' => 'application/x-jar',
  307. // archives
  308. 'gz' => 'application/x-gzip',
  309. 'tgz' => 'application/x-gzip',
  310. 'bz' => 'application/x-bzip2',
  311. 'bz2' => 'application/x-bzip2',
  312. 'tbz' => 'application/x-bzip2',
  313. 'zip' => 'application/zip',
  314. 'rar' => 'application/x-rar',
  315. 'tar' => 'application/x-tar',
  316. '7z' => 'application/x-7z-compressed',
  317. // texts
  318. 'txt' => 'text/plain',
  319. 'php' => 'text/x-php',
  320. 'html' => 'text/html',
  321. 'htm' => 'text/html',
  322. 'js' => 'text/javascript',
  323. 'css' => 'text/css',
  324. 'rtf' => 'text/rtf',
  325. 'rtfd' => 'text/rtfd',
  326. 'py' => 'text/x-python',
  327. 'java' => 'text/x-java-source',
  328. 'rb' => 'text/x-ruby',
  329. 'sh' => 'text/x-shellscript',
  330. 'pl' => 'text/x-perl',
  331. 'xml' => 'text/xml',
  332. 'sql' => 'text/x-sql',
  333. 'c' => 'text/x-csrc',
  334. 'h' => 'text/x-chdr',
  335. 'cpp' => 'text/x-c++src',
  336. 'hh' => 'text/x-c++hdr',
  337. 'log' => 'text/plain',
  338. 'csv' => 'text/x-comma-separated-values',
  339. // images
  340. 'bmp' => 'image/x-ms-bmp',
  341. 'jpg' => 'image/jpeg',
  342. 'jpeg' => 'image/jpeg',
  343. 'gif' => 'image/gif',
  344. 'png' => 'image/png',
  345. 'tif' => 'image/tiff',
  346. 'tiff' => 'image/tiff',
  347. 'tga' => 'image/x-targa',
  348. 'psd' => 'image/vnd.adobe.photoshop',
  349. 'ai' => 'image/vnd.adobe.photoshop',
  350. 'xbm' => 'image/xbm',
  351. 'pxm' => 'image/pxm',
  352. //audio
  353. 'mp3' => 'audio/mpeg',
  354. 'mid' => 'audio/midi',
  355. 'ogg' => 'audio/ogg',
  356. 'oga' => 'audio/ogg',
  357. 'm4a' => 'audio/x-m4a',
  358. 'wav' => 'audio/wav',
  359. 'wma' => 'audio/x-ms-wma',
  360. // video
  361. 'avi' => 'video/x-msvideo',
  362. 'dv' => 'video/x-dv',
  363. 'mp4' => 'video/mp4',
  364. 'mpeg' => 'video/mpeg',
  365. 'mpg' => 'video/mpeg',
  366. 'mov' => 'video/quicktime',
  367. 'wm' => 'video/x-ms-wmv',
  368. 'flv' => 'video/x-flv',
  369. 'mkv' => 'video/x-matroska',
  370. 'webm' => 'video/webm',
  371. 'ogv' => 'video/ogg',
  372. 'ogm' => 'video/ogg'
  373. );
  374. /**
  375. * Directory separator - required by client
  376. *
  377. * @var string
  378. **/
  379. protected $separator = DIRECTORY_SEPARATOR;
  380. /**
  381. * Mimetypes allowed to display
  382. *
  383. * @var array
  384. **/
  385. protected $onlyMimes = array();
  386. /**
  387. * Store files moved or overwrited files info
  388. *
  389. * @var array
  390. **/
  391. protected $removed = array();
  392. /**
  393. * Cache storage
  394. *
  395. * @var array
  396. **/
  397. protected $cache = array();
  398. /**
  399. * Cache by folders
  400. *
  401. * @var array
  402. **/
  403. protected $dirsCache = array();
  404. /*********************************************************************/
  405. /* INITIALIZATION */
  406. /*********************************************************************/
  407. /**
  408. * Prepare driver before mount volume.
  409. * Return true if volume is ready.
  410. *
  411. * @return bool
  412. * @author Dmitry (dio) Levashov
  413. **/
  414. protected function init() {
  415. return true;
  416. }
  417. /**
  418. * Configure after successfull mount.
  419. * By default set thumbnails path and image manipulation library.
  420. *
  421. * @return void
  422. * @author Dmitry (dio) Levashov
  423. **/
  424. protected function configure() {
  425. // set thumbnails path
  426. $path = $this->options['tmbPath'];
  427. if ($path) {
  428. if (!file_exists($path)) {
  429. if (@mkdir($path)) {
  430. chmod($path, $this->options['tmbPathMode']);
  431. } else {
  432. $path = '';
  433. }
  434. }
  435. if (is_dir($path) && is_readable($path)) {
  436. $this->tmbPath = $path;
  437. $this->tmbPathWritable = is_writable($path);
  438. }
  439. }
  440. // set image manipulation library
  441. $type = preg_match('/^(imagick|gd|auto)$/i', $this->options['imgLib'])
  442. ? strtolower($this->options['imgLib'])
  443. : 'auto';
  444. if (($type == 'imagick' || $type == 'auto') && extension_loaded('imagick')) {
  445. $this->imgLib = 'imagick';
  446. } else {
  447. $this->imgLib = function_exists('gd_info') ? 'gd' : '';
  448. }
  449. }
  450. /*********************************************************************/
  451. /* PUBLIC API */
  452. /*********************************************************************/
  453. /**
  454. * Return driver id. Used as a part of volume id.
  455. *
  456. * @return string
  457. * @author Dmitry (dio) Levashov
  458. **/
  459. public function driverId() {
  460. return $this->driverId;
  461. }
  462. /**
  463. * Return volume id
  464. *
  465. * @return string
  466. * @author Dmitry (dio) Levashov
  467. **/
  468. public function id() {
  469. return $this->id;
  470. }
  471. /**
  472. * Return debug info for client
  473. *
  474. * @return array
  475. * @author Dmitry (dio) Levashov
  476. **/
  477. public function debug() {
  478. return array(
  479. 'id' => $this->id(),
  480. 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
  481. 'mimeDetect' => $this->mimeDetect,
  482. 'imgLib' => $this->imgLib
  483. );
  484. }
  485. /**
  486. * "Mount" volume.
  487. * Return true if volume available for read or write,
  488. * false - otherwise
  489. *
  490. * @return bool
  491. * @author Dmitry (dio) Levashov
  492. * @author Alexey Sukhotin
  493. **/
  494. public function mount(array $opts) {
  495. if (!isset($opts['path']) || $opts['path'] === '') {
  496. return false;
  497. }
  498. $this->options = array_merge($this->options, $opts);
  499. $this->id = $this->driverId.(!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++).'_';
  500. $this->root = $this->_normpath($this->options['path']);
  501. $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR;
  502. // default file attribute
  503. $this->defaults = array(
  504. 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true,
  505. 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true,
  506. 'locked' => false,
  507. 'hidden' => false
  508. );
  509. // root attributes
  510. $this->attributes[] = array(
  511. 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR).'$~',
  512. 'locked' => true,
  513. 'hidden' => false
  514. );
  515. // set files attributes
  516. if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) {
  517. foreach ($this->options['attributes'] as $a) {
  518. // attributes must contain pattern and at least one rule
  519. if (!empty($a['pattern']) || count($a) > 1) {
  520. $this->attributes[] = $a;
  521. }
  522. }
  523. }
  524. if (!empty($this->options['accessControl'])) {
  525. if (is_string($this->options['accessControl'])
  526. && function_exists($this->options['accessControl'])) {
  527. $this->access = $this->options['accessControl'];
  528. } elseif (is_array($this->options['accessControl'])
  529. && count($this->options['accessControl']) > 1
  530. && is_object($this->options['accessControl'][0])
  531. && method_exists($this->options['accessControl'][0], $this->options['accessControl'][1])) {
  532. $this->access = array($this->options['accessControl'][0], $this->options['accessControl'][1]);
  533. }
  534. }
  535. $this->today = mktime(0,0,0, date('m'), date('d'), date('Y'));
  536. $this->yesterday = $this->today-86400;
  537. // debug($this->attributes);
  538. if (!$this->init()) {
  539. return false;
  540. }
  541. // check some options is arrays
  542. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  543. ? $this->options['uploadAllow']
  544. : array();
  545. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  546. ? $this->options['uploadDeny']
  547. : array();
  548. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  549. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  550. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  551. } else { // telephat_mode off
  552. $this->uploadOrder = $this->options['uploadOrder'];
  553. }
  554. if (!empty($this->options['uploadMaxSize'])) {
  555. $size = ''.$this->options['uploadMaxSize'];
  556. $unit = strtolower(substr($size, strlen($size) - 1));
  557. $n = 1;
  558. switch ($unit) {
  559. case 'k':
  560. $n = 1024;
  561. break;
  562. case 'm':
  563. $n = 1048576;
  564. break;
  565. case 'g':
  566. $n = 1073741824;
  567. }
  568. $this->uploadMaxSize = intval($size)*$n;
  569. }
  570. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  571. ? $this->options['disabled']
  572. : array();
  573. $this->cryptLib = $this->options['cryptLib'];
  574. $this->mimeDetect = $this->options['mimeDetect'];
  575. // find available mimetype detect method
  576. $type = strtolower($this->options['mimeDetect']);
  577. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  578. $regexp = '/text\/x\-(php|c\+\+)/';
  579. if (($type == 'finfo' || $type == 'auto')
  580. && class_exists('finfo')
  581. && preg_match($regexp, array_shift(explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__))))) {
  582. $type = 'finfo';
  583. $this->finfo = finfo_open(FILEINFO_MIME);
  584. } elseif (($type == 'mime_content_type' || $type == 'auto')
  585. && function_exists('mime_content_type')
  586. && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
  587. $type = 'mime_content_type';
  588. } else {
  589. $type = 'internal';
  590. }
  591. $this->mimeDetect = $type;
  592. // load mimes from external file for mimeDetect == 'internal'
  593. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  594. // file must be in file directory or in parent one
  595. if ($this->mimeDetect == 'internal' && !self::$mimetypesLoaded) {
  596. self::$mimetypesLoaded = true;
  597. $this->mimeDetect = 'internal';
  598. $file = false;
  599. if (!empty($this->options['mimefile']) && file_exists($this->options['mimefile'])) {
  600. $file = $this->options['mimefile'];
  601. } elseif (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types')) {
  602. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types';
  603. } elseif (file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types')) {
  604. $file = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types';
  605. }
  606. if ($file && file_exists($file)) {
  607. $mimecf = file($file);
  608. foreach ($mimecf as $line_num => $line) {
  609. if (!preg_match('/^\s*#/', $line)) {
  610. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  611. for ($i = 1, $size = count($mime); $i < $size ; $i++) {
  612. if (!isset(self::$mimetypes[$mime[$i]])) {
  613. self::$mimetypes[$mime[$i]] = $mime[0];
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. $this->rootName = empty($this->options['alias']) ? $this->_basename($this->root) : $this->options['alias'];
  621. $root = $this->stat($this->root);
  622. if (!$root) {
  623. return $this->setError('Root folder does not exists.');
  624. }
  625. if (!$root['read'] && !$root['write']) {
  626. return $this->setError('Root folder has not read and write permissions.');
  627. }
  628. // debug($root);
  629. if ($root['read']) {
  630. // check startPath - path to open by default instead of root
  631. if ($this->options['startPath']) {
  632. $start = $this->stat($this->options['startPath']);
  633. if (!empty($start)
  634. && $start['mime'] == 'directory'
  635. && $start['read']
  636. && empty($start['hidden'])
  637. && $this->_inpath($this->options['startPath'], $this->root)) {
  638. $this->startPath = $this->options['startPath'];
  639. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  640. $this->startPath = substr($this->startPath, 0, -1);
  641. }
  642. }
  643. }
  644. } else {
  645. $this->options['URL'] = '';
  646. $this->options['tmbURL'] = '';
  647. $this->options['tmbPath'] = '';
  648. // read only volume
  649. array_unshift($this->attributes, array(
  650. 'pattern' => '/.*/',
  651. 'read' => false
  652. ));
  653. }
  654. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  655. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  656. $this->URL = $this->options['URL'];
  657. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  658. $this->URL .= '/';
  659. }
  660. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  661. if ($this->tmbURL && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  662. $this->tmbURL .= '/';
  663. }
  664. $this->nameValidator = is_string($this->options['acceptedName']) && !empty($this->options['acceptedName'])
  665. ? $this->options['acceptedName']
  666. : '';
  667. $this->_checkArchivers();
  668. // manual control archive types to create
  669. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  670. foreach ($this->archivers['create'] as $mime => $v) {
  671. if (!in_array($mime, $this->options['archiveMimes'])) {
  672. unset($this->archivers['create'][$mime]);
  673. }
  674. }
  675. }
  676. // manualy add archivers
  677. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  678. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  679. if (strpos($mime, 'application/') === 0
  680. && !empty($conf['cmd'])
  681. && isset($conf['argc'])
  682. && !empty($conf['ext'])
  683. && !isset($this->archivers['create'][$mime])) {
  684. $this->archivers['create'][$mime] = $conf;
  685. }
  686. }
  687. }
  688. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  689. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  690. if (substr($mime, 'application/') === 0
  691. && !empty($cons['cmd'])
  692. && isset($conf['argc'])
  693. && !empty($conf['ext'])
  694. && !isset($this->archivers['extract'][$mime])) {
  695. $this->archivers['extract'][$mime] = $conf;
  696. }
  697. }
  698. }
  699. $this->configure();
  700. // echo $this->uploadMaxSize;
  701. // echo $this->options['uploadMaxSize'];
  702. return $this->mounted = true;
  703. }
  704. /**
  705. * Some "unmount" stuffs - may be required by virtual fs
  706. *
  707. * @return void
  708. * @author Dmitry (dio) Levashov
  709. **/
  710. public function umount() {
  711. }
  712. /**
  713. * Return error message from last failed action
  714. *
  715. * @return array
  716. * @author Dmitry (dio) Levashov
  717. **/
  718. public function error() {
  719. return $this->error;
  720. }
  721. /**
  722. * Set mimetypes allowed to display to client
  723. *
  724. * @param array $mimes
  725. * @return void
  726. * @author Dmitry (dio) Levashov
  727. **/
  728. public function setMimesFilter($mimes) {
  729. if (is_array($mimes)) {
  730. $this->onlyMimes = $mimes;
  731. }
  732. }
  733. /**
  734. * Return root folder hash
  735. *
  736. * @return string
  737. * @author Dmitry (dio) Levashov
  738. **/
  739. public function root() {
  740. return $this->encode($this->root);
  741. }
  742. /**
  743. * Return root or startPath hash
  744. *
  745. * @return string
  746. * @author Dmitry (dio) Levashov
  747. **/
  748. public function defaultPath() {
  749. return $this->encode($this->startPath ? $this->startPath : $this->root);
  750. }
  751. /**
  752. * Return volume options required by client:
  753. *
  754. * @return array
  755. * @author Dmitry (dio) Levashov
  756. **/
  757. public function options($hash) {
  758. return array(
  759. 'path' => $this->_path($this->decode($hash)),
  760. 'url' => $this->URL,
  761. 'tmbUrl' => $this->tmbURL,
  762. 'disabled' => $this->disabled,
  763. 'separator' => $this->separator,
  764. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  765. 'archivers' => array(
  766. 'create' => array_keys($this->archivers['create']),
  767. 'extract' => array_keys($this->archivers['extract'])
  768. )
  769. );
  770. }
  771. /**
  772. * Return true if command disabled in options
  773. *
  774. * @param string $cmd command name
  775. * @return bool
  776. * @author Dmitry (dio) Levashov
  777. **/
  778. public function commandDisabled($cmd) {
  779. return in_array($cmd, $this->disabled);
  780. }
  781. /**
  782. * Return true if mime is required mimes list
  783. *
  784. * @param string $mime mime type to check
  785. * @param array $mimes allowed mime types list or not set to use client mimes list
  786. * @param bool|null $empty what to return on empty list
  787. * @return bool|null
  788. * @author Dmitry (dio) Levashov
  789. * @author Troex Nevelin
  790. **/
  791. public function mimeAccepted($mime, $mimes = array(), $empty = true) {
  792. $mimes = !empty($mimes) ? $mimes : $this->onlyMimes;
  793. if (empty($mimes)) {
  794. return $empty;
  795. }
  796. return $mime == 'directory'
  797. || in_array('all', $mimes)
  798. || in_array('All', $mimes)
  799. || in_array($mime, $mimes)
  800. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  801. }
  802. /**
  803. * Return true if voume is readable.
  804. *
  805. * @return bool
  806. * @author Dmitry (dio) Levashov
  807. **/
  808. public function isReadable() {
  809. $stat = $this->stat($this->root);
  810. return $stat['read'];
  811. }
  812. /**
  813. * Return true if copy from this volume allowed
  814. *
  815. * @return bool
  816. * @author Dmitry (dio) Levashov
  817. **/
  818. public function copyFromAllowed() {
  819. return !!$this->options['copyFrom'];
  820. }
  821. /**
  822. * Return file path related to root
  823. *
  824. * @param string $hash file hash
  825. * @return string
  826. * @author Dmitry (dio) Levashov
  827. **/
  828. public function path($hash) {
  829. return $this->_path($this->decode($hash));
  830. }
  831. /**
  832. * Return file real path if file exists
  833. *
  834. * @param string $hash file hash
  835. * @return string
  836. * @author Dmitry (dio) Levashov
  837. **/
  838. public function realpath($hash) {
  839. $path = $this->decode($hash);
  840. return $this->stat($path) ? $path : false;
  841. }
  842. /**
  843. * Return list of moved/overwrited files
  844. *
  845. * @return array
  846. * @author Dmitry (dio) Levashov
  847. **/
  848. public function removed() {
  849. return $this->removed;
  850. }
  851. /**
  852. * Clean removed files list
  853. *
  854. * @return void
  855. * @author Dmitry (dio) Levashov
  856. **/
  857. public function resetRemoved() {
  858. $this->removed = array();
  859. }
  860. /**
  861. * Return file/dir hash or first founded child hash with required attr == $val
  862. *
  863. * @param string $hash file hash
  864. * @param string $attr attribute name
  865. * @param bool $val attribute value
  866. * @return string|false
  867. * @author Dmitry (dio) Levashov
  868. **/
  869. public function closest($hash, $attr, $val) {
  870. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  871. }
  872. /**
  873. * Return file info or false on error
  874. *
  875. * @param string $hash file hash
  876. * @param bool $realpath add realpath field to file info
  877. * @return array|false
  878. * @author Dmitry (dio) Levashov
  879. **/
  880. public function file($hash) {
  881. $path = $this->decode($hash);
  882. return ($file = $this->stat($path)) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  883. if (($file = $this->stat($path)) != false) {
  884. if ($realpath) {
  885. $file['realpath'] = $path;
  886. }
  887. return $file;
  888. }
  889. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  890. }
  891. /**
  892. * Return folder info
  893. *
  894. * @param string $hash folder hash
  895. * @param bool $hidden return hidden file info
  896. * @return array|false
  897. * @author Dmitry (dio) Levashov
  898. **/
  899. public function dir($hash, $resolveLink=false) {
  900. if (($dir = $this->file($hash)) == false) {
  901. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  902. }
  903. if ($resolveLink && !empty($dir['thash'])) {
  904. $dir = $this->file($dir['thash']);
  905. }
  906. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  907. ? $dir
  908. : $this->setError(elFinder::ERROR_NOT_DIR);
  909. }
  910. /**
  911. * Return directory content or false on error
  912. *
  913. * @param string $hash file hash
  914. * @return array|false
  915. * @author Dmitry (dio) Levashov
  916. **/
  917. public function scandir($hash) {
  918. if (($dir = $this->dir($hash)) == false) {
  919. return false;
  920. }
  921. return $dir['read']
  922. ? $this->getScandir($this->decode($hash))
  923. : $this->setError(elFinder::ERROR_PERM_DENIED);
  924. }
  925. /**
  926. * Return dir files names list
  927. *
  928. * @param string $hash file hash
  929. * @return array
  930. * @author Dmitry (dio) Levashov
  931. **/
  932. public function ls($hash) {
  933. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  934. return false;
  935. }
  936. $list = array();
  937. $path = $this->decode($hash);
  938. foreach ($this->getScandir($path) as $stat) {
  939. if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
  940. $list[] = $stat['name'];
  941. }
  942. }
  943. return $list;
  944. }
  945. /**
  946. * Return subfolders for required folder or false on error
  947. *
  948. * @param string $hash folder hash or empty string to get tree from root folder
  949. * @param int $deep subdir deep
  950. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  951. * @return array|false
  952. * @author Dmitry (dio) Levashov
  953. **/
  954. public function tree($hash='', $deep=0, $exclude='') {
  955. $path = $hash ? $this->decode($hash) : $this->root;
  956. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  957. return false;
  958. }
  959. $dirs = $this->gettree($path, $deep > 0 ? $deep -1 : $this->treeDeep-1, $this->decode($exclude));
  960. array_unshift($dirs, $dir);
  961. return $dirs;
  962. }
  963. /**
  964. * Return part of dirs tree from required dir up to root dir
  965. *
  966. * @param string $hash directory hash
  967. * @return array
  968. * @author Dmitry (dio) Levashov
  969. **/
  970. public function parents($hash) {
  971. if (($current = $this->dir($hash)) == false) {
  972. return false;
  973. }
  974. $path = $this->decode($hash);
  975. $tree = array();
  976. while ($path && $path != $this->root) {
  977. $path = $this->_dirname($path);
  978. $stat = $this->stat($path);
  979. if (!empty($stat['hidden']) || !$stat['read']) {
  980. return false;
  981. }
  982. array_unshift($tree, $stat);
  983. if ($path != $this->root) {
  984. foreach ($this->gettree($path, 0) as $dir) {
  985. if (!in_array($dir, $tree)) {
  986. $tree[] = $dir;
  987. }
  988. }
  989. }
  990. }
  991. return $tree ? $tree : array($current);
  992. }
  993. /**
  994. * Create thumbnail for required file and return its name of false on failed
  995. *
  996. * @return string|false
  997. * @author Dmitry (dio) Levashov
  998. **/
  999. public function tmb($hash) {
  1000. $path = $this->decode($hash);
  1001. $stat = $this->stat($path);
  1002. if (isset($stat['tmb'])) {
  1003. return $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1004. }
  1005. return false;
  1006. }
  1007. /**
  1008. * Return file size / total directory size
  1009. *
  1010. * @param string file hash
  1011. * @return int
  1012. * @author Dmitry (dio) Levashov
  1013. **/
  1014. public function size($hash) {
  1015. return $this->countSize($this->decode($hash));
  1016. }
  1017. /**
  1018. * Open file for reading and return file pointer
  1019. *
  1020. * @param string file hash
  1021. * @return Resource
  1022. * @author Dmitry (dio) Levashov
  1023. **/
  1024. public function open($hash) {
  1025. if (($file = $this->file($hash)) == false
  1026. || $file['mime'] == 'directory') {
  1027. return false;
  1028. }
  1029. return $this->_fopen($this->decode($hash), 'rb');
  1030. }
  1031. /**
  1032. * Close file pointer
  1033. *
  1034. * @param Resource $fp file pointer
  1035. * @param string $hash file hash
  1036. * @return void
  1037. * @author Dmitry (dio) Levashov
  1038. **/
  1039. public function close($fp, $hash) {
  1040. $this->_fclose($fp, $this->decode($hash));
  1041. }
  1042. /**
  1043. * Create directory and return dir info
  1044. *
  1045. * @param string $dst destination directory
  1046. * @param string $name directory name
  1047. * @return array|false
  1048. * @author Dmitry (dio) Levashov
  1049. **/
  1050. public function mkdir($dst, $name) {
  1051. if ($this->commandDisabled('mkdir')) {
  1052. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1053. }
  1054. if (!$this->nameAccepted($name)) {
  1055. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1056. }
  1057. if (($dir = $this->dir($dst)) == false) {
  1058. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1059. }
  1060. if (!$dir['write']) {
  1061. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1062. }
  1063. $path = $this->decode($dst);
  1064. $dst = $this->_joinPath($path, $name);
  1065. $stat = $this->stat($dst);
  1066. if (!empty($stat)) {
  1067. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1068. }
  1069. $this->clearcache();
  1070. return ($path = $this->_mkdir($path, $name)) ? $this->stat($path) : false;
  1071. }
  1072. /**
  1073. * Create empty file and return its info
  1074. *
  1075. * @param string $dst destination directory
  1076. * @param string $name file name
  1077. * @return array|false
  1078. * @author Dmitry (dio) Levashov
  1079. **/
  1080. public function mkfile($dst, $name) {
  1081. if ($this->commandDisabled('mkfile')) {
  1082. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1083. }
  1084. if (!$this->nameAccepted($name)) {
  1085. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1086. }
  1087. if (($dir = $this->dir($dst)) == false) {
  1088. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1089. }
  1090. if (!$dir['write']) {
  1091. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1092. }
  1093. $path = $this->decode($dst);
  1094. if ($this->stat($this->_joinPath($path, $name))) {
  1095. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1096. }
  1097. $this->clearcache();
  1098. return ($path = $this->_mkfile($path, $name)) ? $this->stat($path) : false;
  1099. }
  1100. /**
  1101. * Rename file and return file info
  1102. *
  1103. * @param string $hash file hash
  1104. * @param string $name new file name
  1105. * @return array|false
  1106. * @author Dmitry (dio) Levashov
  1107. **/
  1108. public function rename($hash, $name) {
  1109. if ($this->commandDisabled('rename')) {
  1110. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1111. }
  1112. if (!$this->nameAccepted($name)) {
  1113. return $this->setError(elFinder::ERROR_INVALID_NAME, $name);
  1114. }
  1115. if (!($file = $this->file($hash))) {
  1116. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1117. }
  1118. if ($name == $file['name']) {
  1119. return $file;
  1120. }
  1121. if (!empty($file['locked'])) {
  1122. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  1123. }
  1124. $path = $this->decode($hash);
  1125. $dir = $this->_dirname($path);
  1126. $stat = $this->stat($this->_joinPath($dir, $name));
  1127. if ($stat) {
  1128. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1129. }
  1130. if (!$this->_move($path, $dir, $name)) {
  1131. return false;
  1132. }
  1133. if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
  1134. $this->rmTmb($stat['tmb']);
  1135. }
  1136. $path = $this->_joinPath($dir, $name);
  1137. $this->clearcache();
  1138. return $this->stat($path);
  1139. }
  1140. /**
  1141. * Create file copy with suffix "copy number" and return its info
  1142. *
  1143. * @param string $hash file hash
  1144. * @param string $suffix suffix to add to file name
  1145. * @return array|false
  1146. * @author Dmitry (dio) Levashov
  1147. **/
  1148. public function duplicate($hash, $suffix='copy') {
  1149. if ($this->commandDisabled('duplicate')) {
  1150. return $this->setError(elFinder::ERROR_COPY, '#'.$hash, elFinder::ERROR_PERM_DENIED);
  1151. }
  1152. if (($file = $this->file($hash)) == false) {
  1153. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  1154. }
  1155. $path = $this->decode($hash);
  1156. $dir = $this->_dirname($path);
  1157. return ($path = $this->copy($path, $dir, $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' '))) == false
  1158. ? false
  1159. : $this->stat($path);
  1160. }
  1161. /**
  1162. * Save uploaded file.
  1163. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  1164. *
  1165. * @param Resource $fp file pointer
  1166. * @param string $dst destination folder hash
  1167. * @param string $src file name
  1168. * @param string $tmpname file tmp name - required to detect mime type
  1169. * @return array|false
  1170. * @author Dmitry (dio) Levashov
  1171. **/
  1172. public function upload($fp, $dst, $name, $tmpname) {
  1173. if ($this->commandDisabled('upload')) {
  1174. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1175. }
  1176. if (($dir = $this->dir($dst)) == false) {
  1177. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1178. }
  1179. if (!$dir['write']) {
  1180. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1181. }
  1182. if (!$this->nameAccepted($name)) {
  1183. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1184. }
  1185. $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname);
  1186. if ($mime == 'unknown' && $this->mimeDetect == 'internal') {
  1187. $mime = elFinderVolumeDriver::mimetypeInternalDetect($name);
  1188. }
  1189. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  1190. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  1191. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  1192. $upload = true; // default to allow
  1193. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  1194. $upload = false; // default is deny
  1195. if (!$deny && ($allow === true)) { // match only allow
  1196. $upload = true;
  1197. }// else (both match | no match | match only deny) { deny }
  1198. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  1199. $upload = true; // default is allow
  1200. if (($deny === true) && !$allow) { // match only deny
  1201. $upload = false;
  1202. } // else (both match | no match | match only allow) { allow }
  1203. }
  1204. if (!$upload) {
  1205. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  1206. }
  1207. if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) {
  1208. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  1209. }
  1210. $dstpath = $this->decode($dst);
  1211. $test = $this->_joinPath($dstpath, $name);
  1212. $file = $this->stat($test);
  1213. $this->clearcache();
  1214. if ($file) { // file exists
  1215. if ($this->options['uploadOverwrite']) {
  1216. if (!$file['write']) {
  1217. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1218. } elseif ($file['mime'] == 'directory') {
  1219. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  1220. }
  1221. $this->remove($file);
  1222. } else {
  1223. $name = $this->uniqueName($dstpath, $name, '-', false);
  1224. }
  1225. }
  1226. $w = $h = 0;
  1227. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  1228. $w = $s[0];
  1229. $h = $s[1];
  1230. }
  1231. // $this->clearcache();
  1232. if (($path = $this->_save($fp, $dstpath, $name, $mime, $w, $h)) == false) {
  1233. return false;
  1234. }
  1235. return $this->stat($path);
  1236. }
  1237. /**
  1238. * Paste files
  1239. *
  1240. * @param Object $volume source volume
  1241. * @param string $source file hash
  1242. * @param string $dst destination dir hash
  1243. * @param bool $rmSrc remove source after copy?
  1244. * @return array|false
  1245. * @author Dmitry (dio) Levashov
  1246. **/
  1247. public function paste($volume, $src, $dst, $rmSrc = false) {
  1248. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  1249. if ($this->commandDisabled('paste')) {
  1250. return $this->setError($err, '#'.$src, elFinder::ERROR_PERM_DENIED);
  1251. }
  1252. if (($file = $volume->file($src, $rmSrc)) == false) {
  1253. return $this->setError($err, '#'.$src, elFinder::ERROR_FILE_NOT_FOUND);
  1254. }
  1255. $name = $file['name'];
  1256. $errpath = $volume->path($src);
  1257. if (($dir = $this->dir($dst)) == false) {
  1258. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1259. }
  1260. if (!$dir['write'] || !$file['read']) {
  1261. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1262. }
  1263. $destination = $this->decode($dst);
  1264. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  1265. return $rmSrc
  1266. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  1267. : $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1268. }
  1269. $test = $this->_joinPath($destination, $name);
  1270. $stat = $this->stat($test);
  1271. $this->clearcache();
  1272. if ($stat) {
  1273. if ($this->options['copyOverwrite']) {
  1274. // do not replace file with dir or dir with file
  1275. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  1276. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->_path($test));
  1277. }
  1278. // existed file is not writable
  1279. if (!$stat['write']) {
  1280. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1281. }
  1282. // existed file locked or has locked child
  1283. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  1284. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($locked));
  1285. }
  1286. // remove existed file
  1287. if (!$this->remove($test)) {
  1288. return $this->setError(elFinder::ERROR_REPLACE, $this->_path($test));
  1289. }
  1290. } else {
  1291. $name = $this->uniqueName($destination, $name, ' ', false);
  1292. }
  1293. }
  1294. // copy/move inside current volume
  1295. if ($volume == $this) {
  1296. $source = $this->decode($src);
  1297. // do not copy into itself
  1298. if ($this->_inpath($destination, $source)) {
  1299. return $this->setError(elFinder::ERROR_COPY_INTO_ITSELF, $path);
  1300. }
  1301. $method = $rmSrc ? 'move' : 'copy';
  1302. return ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false;
  1303. }
  1304. // copy/move from another volume
  1305. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  1306. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  1307. }
  1308. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  1309. return false;
  1310. }
  1311. if ($rmSrc) {
  1312. if ($volume->rm($src)) {
  1313. $this->removed[] = $file;
  1314. } else {
  1315. return $this->setError(elFinder::ERROR_MOVE, $errpath, elFinder::ERROR_RM_SRC);
  1316. }
  1317. }
  1318. return $this->stat($path);
  1319. }
  1320. /**
  1321. * Return file contents
  1322. *
  1323. * @param string $hash file hash
  1324. * @return string|false
  1325. * @author Dmitry (dio) Levashov
  1326. **/
  1327. public function getContents($hash) {
  1328. $file = $this->file($hash);
  1329. if (!$file) {
  1330. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1331. }
  1332. if ($file['mime'] == 'directory') {
  1333. return $this->setError(elFinder::ERROR_NOT_FILE);
  1334. }
  1335. if (!$file['read']) {
  1336. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1337. }
  1338. return $this->_getContents($this->decode($hash));
  1339. }
  1340. /**
  1341. * Put content in text file and return file info.
  1342. *
  1343. * @param string $hash file hash
  1344. * @param string $content new file content
  1345. * @return array
  1346. * @author Dmitry (dio) Levashov
  1347. **/
  1348. public function putContents($hash, $content) {
  1349. if ($this->commandDisabled('edit')) {
  1350. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1351. }
  1352. $path = $this->decode($hash);
  1353. if (!($file = $this->file($hash))) {
  1354. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1355. }
  1356. if (!$file['write']) {
  1357. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1358. }
  1359. $this->clearcache();
  1360. return $this->_filePutContents($path, $content) ? $this->stat($path) : false;
  1361. }
  1362. /**
  1363. * Extract files from archive
  1364. *
  1365. * @param string $hash archive hash
  1366. * @return array|bool
  1367. * @author Dmitry (dio) Levashov,
  1368. * @author Alexey Sukhotin
  1369. **/
  1370. public function extract($hash) {
  1371. if ($this->commandDisabled('extract')) {
  1372. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1373. }
  1374. if (($file = $this->file($hash)) == false) {
  1375. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1376. }
  1377. $archiver = isset($this->archivers['extract'][$file['mime']])
  1378. ? $this->archivers['extract'][$file['mime']]
  1379. : false;
  1380. if (!$archiver) {
  1381. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  1382. }
  1383. $path = $this->decode($hash);
  1384. $parent = $this->stat($this->_dirname($path));
  1385. if (!$file['read'] || !$parent['write']) {
  1386. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1387. }
  1388. $this->clearcache();
  1389. return ($path = $this->_extract($path, $archiver)) ? $this->stat($path) : false;
  1390. }
  1391. /**
  1392. * Add files to archive
  1393. *
  1394. * @return void
  1395. **/
  1396. public function archive($hashes, $mime) {
  1397. if ($this->commandDisabled('archive')) {
  1398. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1399. }
  1400. $archiver = isset($this->archivers['create'][$mime])
  1401. ? $this->archivers['create'][$mime]
  1402. : false;
  1403. if (!$archiver) {
  1404. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  1405. }
  1406. $files = array();
  1407. foreach ($hashes as $hash) {
  1408. if (($file = $this->file($hash)) == false) {
  1409. return $this->error(elFinder::ERROR_FILE_NOT_FOUND, '#'+$hash);
  1410. }
  1411. if (!$file['read']) {
  1412. return $this->error(elFinder::ERROR_PERM_DENIED);
  1413. }
  1414. $path = $this->decode($hash);
  1415. if (!isset($dir)) {
  1416. $dir = $this->_dirname($path);
  1417. $stat = $this->stat($dir);
  1418. if (!$stat['write']) {
  1419. return $this->error(elFinder::ERROR_PERM_DENIED);
  1420. }
  1421. }
  1422. $files[] = $this->_basename($path);
  1423. }
  1424. $name = (count($files) == 1 ? $files[0] : 'Archive').'.'.$archiver['ext'];
  1425. $name = $this->uniqueName($dir, $name, '');
  1426. $this->clearcache();
  1427. return ($path = $this->_archive($dir, $files, $name, $archiver)) ? $this->stat($path) : false;
  1428. }
  1429. /**
  1430. * Resize image
  1431. *
  1432. * @param string $hash image file
  1433. * @param int $width new width
  1434. * @param int $height new height
  1435. * @param int $x X start poistion for crop
  1436. * @param int $y Y start poistion for crop
  1437. * @param string $mode action how to mainpulate image
  1438. * @return array|false
  1439. * @author Dmitry (dio) Levashov
  1440. * @author Alexey Sukhotin
  1441. * @author nao-pon
  1442. * @author Troex Nevelin
  1443. **/
  1444. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
  1445. if ($this->commandDisabled('resize')) {
  1446. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1447. }
  1448. if (($file = $this->file($hash)) == false) {
  1449. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1450. }
  1451. if (!$file['write'] || !$file['read']) {
  1452. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1453. }
  1454. $path = $this->decode($hash);
  1455. if (!$this->canResize($path, $file)) {
  1456. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  1457. }
  1458. switch($mode) {
  1459. case 'propresize':
  1460. $result = $this->imgResize($path, $width, $height, true, true);
  1461. break;
  1462. case 'crop':
  1463. $result = $this->imgCrop($path, $width, $height, $x, $y);
  1464. break;
  1465. case 'fitsquare':
  1466. $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']));
  1467. break;
  1468. case 'rotate':
  1469. $result = $this->imgRotate($path, $degree, ($bg ? $bg : $this->options['tmbBgColor']));
  1470. break;
  1471. default:
  1472. $result = $this->imgResize($path, $width, $height, false, true);
  1473. break;
  1474. }
  1475. if ($result) {
  1476. if (!empty($file['tmb']) && $file['tmb'] != "1") {
  1477. $this->rmTmb($file['tmb']);
  1478. }
  1479. $this->clearcache();
  1480. return $this->stat($path);
  1481. }
  1482. return false;
  1483. }
  1484. /**
  1485. * Remove file/dir
  1486. *
  1487. * @param string $hash file hash
  1488. * @return bool
  1489. * @author Dmitry (dio) Levashov
  1490. **/
  1491. public function rm($hash) {
  1492. return $this->commandDisabled('rm')
  1493. ? array(elFinder::ERROR_ACCESS_DENIED)
  1494. : $this->remove($this->decode($hash));
  1495. }
  1496. /**
  1497. * Search files
  1498. *
  1499. * @param string $q search string
  1500. * @param array $mimes
  1501. * @return array
  1502. * @author Dmitry (dio) Levashov
  1503. **/
  1504. public function search($q, $mimes) {
  1505. return $this->doSearch($this->root, $q, $mimes);
  1506. }
  1507. /**
  1508. * Return image dimensions
  1509. *
  1510. * @param string $hash file hash
  1511. * @return array
  1512. * @author Dmitry (dio) Levashov
  1513. **/
  1514. public function dimensions($hash) {
  1515. if (($file = $this->file($hash)) == false) {
  1516. return false;
  1517. }
  1518. return $this->_dimensions($this->decode($hash), $file['mime']);
  1519. }
  1520. /**
  1521. * Save error message
  1522. *
  1523. * @param array error
  1524. * @return false
  1525. * @author Dmitry(dio) Levashov
  1526. **/
  1527. protected function setError($error) {
  1528. $this->error = array();
  1529. foreach (func_get_args() as $err) {
  1530. if (is_array($err)) {
  1531. $this->error = array_merge($this->error, $err);
  1532. } else {
  1533. $this->error[] = $err;
  1534. }
  1535. }
  1536. // $this->error = is_array($error) ? $error : func_get_args();
  1537. return false;
  1538. }
  1539. /*********************************************************************/
  1540. /* FS API */
  1541. /*********************************************************************/
  1542. /***************** paths *******************/
  1543. /**
  1544. * Encode path into hash
  1545. *
  1546. * @param string file path
  1547. * @return string
  1548. * @author Dmitry (dio) Levashov
  1549. * @author Troex Nevelin
  1550. **/
  1551. protected function encode($path) {
  1552. if ($path !== '') {
  1553. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1554. $p = $this->_relpath($path);
  1555. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1556. if ($p === '') {
  1557. $p = DIRECTORY_SEPARATOR;
  1558. }
  1559. // TODO crypt path and return hash
  1560. $hash = $this->crypt($p);
  1561. // hash is used as id in HTML that means it must contain vaild chars
  1562. // make base64 html safe and append prefix in begining
  1563. $hash = strtr(base64_encode($hash), '+/=', '-_.');
  1564. // remove dots '.' at the end, before it was '=' in base64
  1565. $hash = rtrim($hash, '.');
  1566. // append volume id to make hash unique
  1567. return $this->id.$hash;
  1568. }
  1569. }
  1570. /**
  1571. * Decode path from hash
  1572. *
  1573. * @param string file hash
  1574. * @return string
  1575. * @author Dmitry (dio) Levashov
  1576. * @author Troex Nevelin
  1577. **/
  1578. protected function decode($hash) {
  1579. if (strpos($hash, $this->id) === 0) {
  1580. // cut volume id after it was prepended in encode
  1581. $h = substr($hash, strlen($this->id));
  1582. // replace HTML safe base64 to normal
  1583. $h = base64_decode(strtr($h, '-_.', '+/='));
  1584. // TODO uncrypt hash and return path
  1585. $path = $this->uncrypt($h);
  1586. // append ROOT to path after it was cut in encode
  1587. return $this->_abspath($path);//$this->root.($path == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR.$path);
  1588. }
  1589. }
  1590. /**
  1591. * Return crypted path
  1592. * Not implemented
  1593. *
  1594. * @param string path
  1595. * @return mixed
  1596. * @author Dmitry (dio) Levashov
  1597. **/
  1598. protected function crypt($path) {
  1599. return $path;
  1600. }
  1601. /**
  1602. * Return uncrypted path
  1603. * Not implemented
  1604. *
  1605. * @param mixed hash
  1606. * @return mixed
  1607. * @author Dmitry (dio) Levashov
  1608. **/
  1609. protected function uncrypt($hash) {
  1610. return $hash;
  1611. }
  1612. /**
  1613. * Validate file name based on $this->options['acceptedName'] regexp
  1614. *
  1615. * @param string $name file name
  1616. * @return bool
  1617. * @author Dmitry (dio) Levashov
  1618. **/
  1619. protected function nameAccepted($name) {
  1620. if ($this->nameValidator) {
  1621. if (function_exists($this->nameValidator)) {
  1622. $f = $this->nameValidator;
  1623. return $f($name);
  1624. }
  1625. return preg_match($this->nameValidator, $name);
  1626. }
  1627. return true;
  1628. }
  1629. /**
  1630. * Return new unique name based on file name and suffix
  1631. *
  1632. * @param string $path file path
  1633. * @param string $suffix suffix append to name
  1634. * @return string
  1635. * @author Dmitry (dio) Levashov
  1636. **/
  1637. public function uniqueName($dir, $name, $suffix = ' copy', $checkNum=true) {
  1638. $ext = '';
  1639. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  1640. $ext = '.'.$m[1];
  1641. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  1642. }
  1643. if ($checkNum && preg_match('/('.$suffix.')(\d*)$/i', $name, $m)) {
  1644. $i = (int)$m[2];
  1645. $name = substr($name, 0, strlen($name)-strlen($m[2]));
  1646. } else {
  1647. $i = 1;
  1648. $name .= $suffix;
  1649. }
  1650. $max = $i+100000;
  1651. while ($i <= $max) {
  1652. $n = $name.($i > 0 ? $i : '').$ext;
  1653. if (!$this->stat($this->_joinPath($dir, $n))) {
  1654. $this->clearcache();
  1655. return $n;
  1656. }
  1657. $i++;
  1658. }
  1659. return $name.md5($dir).$ext;
  1660. }
  1661. /*********************** file stat *********************/
  1662. /**
  1663. * Check file attribute
  1664. *
  1665. * @param string $path file path
  1666. * @param string $name attribute name (read|write|locked|hidden)
  1667. * @param bool $val attribute value returned by f…

Large files files are truncated, but you can click here to view the full file