PageRenderTime 52ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/php/elFinderVolumeDriver.class.php

https://github.com/dyjo/elFinder
PHP | 3482 lines | 1730 code | 497 blank | 1255 comment | 516 complexity | 597f85ac3fbdfa7feec4ea086772fd10 MD5 | raw file

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' => '/^[^\.].*/', //<-- DONT touch this! Use constructor options to overwrite it!
  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. 'swf' => 'application/x-shockwave-flash',
  304. 'torrent' => 'application/x-bittorrent',
  305. 'jar' => 'application/x-jar',
  306. // open office (finfo detect as application/zip)
  307. 'odt' => 'application/vnd.oasis.opendocument.text',
  308. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  309. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  310. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  311. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  312. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  313. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  314. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  315. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  316. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  317. 'odc' => 'application/vnd.oasis.opendocument.chart',
  318. 'odf' => 'application/vnd.oasis.opendocument.formula',
  319. 'odb' => 'application/vnd.oasis.opendocument.database',
  320. 'odi' => 'application/vnd.oasis.opendocument.image',
  321. 'oxt' => 'application/vnd.openofficeorg.extension',
  322. // MS office 2007 (finfo detect as application/zip)
  323. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  324. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  325. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  326. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  327. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  328. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  329. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  330. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  331. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  332. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  333. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  334. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  335. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  336. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  337. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  338. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  339. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  340. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  341. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  342. // archives
  343. 'gz' => 'application/x-gzip',
  344. 'tgz' => 'application/x-gzip',
  345. 'bz' => 'application/x-bzip2',
  346. 'bz2' => 'application/x-bzip2',
  347. 'tbz' => 'application/x-bzip2',
  348. 'zip' => 'application/zip',
  349. 'rar' => 'application/x-rar',
  350. 'tar' => 'application/x-tar',
  351. '7z' => 'application/x-7z-compressed',
  352. // texts
  353. 'txt' => 'text/plain',
  354. 'php' => 'text/x-php',
  355. 'html' => 'text/html',
  356. 'htm' => 'text/html',
  357. 'js' => 'text/javascript',
  358. 'css' => 'text/css',
  359. 'rtf' => 'text/rtf',
  360. 'rtfd' => 'text/rtfd',
  361. 'py' => 'text/x-python',
  362. 'java' => 'text/x-java-source',
  363. 'rb' => 'text/x-ruby',
  364. 'sh' => 'text/x-shellscript',
  365. 'pl' => 'text/x-perl',
  366. 'xml' => 'text/xml',
  367. 'sql' => 'text/x-sql',
  368. 'c' => 'text/x-csrc',
  369. 'h' => 'text/x-chdr',
  370. 'cpp' => 'text/x-c++src',
  371. 'hh' => 'text/x-c++hdr',
  372. 'log' => 'text/plain',
  373. 'csv' => 'text/x-comma-separated-values',
  374. // images
  375. 'bmp' => 'image/x-ms-bmp',
  376. 'jpg' => 'image/jpeg',
  377. 'jpeg' => 'image/jpeg',
  378. 'gif' => 'image/gif',
  379. 'png' => 'image/png',
  380. 'tif' => 'image/tiff',
  381. 'tiff' => 'image/tiff',
  382. 'tga' => 'image/x-targa',
  383. 'psd' => 'image/vnd.adobe.photoshop',
  384. 'ai' => 'image/vnd.adobe.photoshop',
  385. 'xbm' => 'image/xbm',
  386. 'pxm' => 'image/pxm',
  387. //audio
  388. 'mp3' => 'audio/mpeg',
  389. 'mid' => 'audio/midi',
  390. 'ogg' => 'audio/ogg',
  391. 'oga' => 'audio/ogg',
  392. 'm4a' => 'audio/x-m4a',
  393. 'wav' => 'audio/wav',
  394. 'wma' => 'audio/x-ms-wma',
  395. // video
  396. 'avi' => 'video/x-msvideo',
  397. 'dv' => 'video/x-dv',
  398. 'mp4' => 'video/mp4',
  399. 'mpeg' => 'video/mpeg',
  400. 'mpg' => 'video/mpeg',
  401. 'mov' => 'video/quicktime',
  402. 'wm' => 'video/x-ms-wmv',
  403. 'flv' => 'video/x-flv',
  404. 'mkv' => 'video/x-matroska',
  405. 'webm' => 'video/webm',
  406. 'ogv' => 'video/ogg',
  407. 'ogm' => 'video/ogg'
  408. );
  409. /**
  410. * Directory separator - required by client
  411. *
  412. * @var string
  413. **/
  414. protected $separator = DIRECTORY_SEPARATOR;
  415. /**
  416. * Mimetypes allowed to display
  417. *
  418. * @var array
  419. **/
  420. protected $onlyMimes = array();
  421. /**
  422. * Store files moved or overwrited files info
  423. *
  424. * @var array
  425. **/
  426. protected $removed = array();
  427. /**
  428. * Cache storage
  429. *
  430. * @var array
  431. **/
  432. protected $cache = array();
  433. /**
  434. * Cache by folders
  435. *
  436. * @var array
  437. **/
  438. protected $dirsCache = array();
  439. /*********************************************************************/
  440. /* INITIALIZATION */
  441. /*********************************************************************/
  442. /**
  443. * Prepare driver before mount volume.
  444. * Return true if volume is ready.
  445. *
  446. * @return bool
  447. * @author Dmitry (dio) Levashov
  448. **/
  449. protected function init() {
  450. return true;
  451. }
  452. /**
  453. * Configure after successfull mount.
  454. * By default set thumbnails path and image manipulation library.
  455. *
  456. * @return void
  457. * @author Dmitry (dio) Levashov
  458. **/
  459. protected function configure() {
  460. // set thumbnails path
  461. $path = $this->options['tmbPath'];
  462. if ($path) {
  463. if (!file_exists($path)) {
  464. if (@mkdir($path)) {
  465. chmod($path, $this->options['tmbPathMode']);
  466. } else {
  467. $path = '';
  468. }
  469. }
  470. if (is_dir($path) && is_readable($path)) {
  471. $this->tmbPath = $path;
  472. $this->tmbPathWritable = is_writable($path);
  473. }
  474. }
  475. // set image manipulation library
  476. $type = preg_match('/^(imagick|gd|auto)$/i', $this->options['imgLib'])
  477. ? strtolower($this->options['imgLib'])
  478. : 'auto';
  479. if (($type == 'imagick' || $type == 'auto') && extension_loaded('imagick')) {
  480. $this->imgLib = 'imagick';
  481. } else {
  482. $this->imgLib = function_exists('gd_info') ? 'gd' : '';
  483. }
  484. }
  485. /*********************************************************************/
  486. /* PUBLIC API */
  487. /*********************************************************************/
  488. /**
  489. * Return driver id. Used as a part of volume id.
  490. *
  491. * @return string
  492. * @author Dmitry (dio) Levashov
  493. **/
  494. public function driverId() {
  495. return $this->driverId;
  496. }
  497. /**
  498. * Return volume id
  499. *
  500. * @return string
  501. * @author Dmitry (dio) Levashov
  502. **/
  503. public function id() {
  504. return $this->id;
  505. }
  506. /**
  507. * Return debug info for client
  508. *
  509. * @return array
  510. * @author Dmitry (dio) Levashov
  511. **/
  512. public function debug() {
  513. return array(
  514. 'id' => $this->id(),
  515. 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
  516. 'mimeDetect' => $this->mimeDetect,
  517. 'imgLib' => $this->imgLib
  518. );
  519. }
  520. /**
  521. * "Mount" volume.
  522. * Return true if volume available for read or write,
  523. * false - otherwise
  524. *
  525. * @return bool
  526. * @author Dmitry (dio) Levashov
  527. * @author Alexey Sukhotin
  528. **/
  529. public function mount(array $opts) {
  530. if (!isset($opts['path']) || $opts['path'] === '') {
  531. return $this->setError('Path undefined.');;
  532. }
  533. $this->options = array_merge($this->options, $opts);
  534. $this->id = $this->driverId.(!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++).'_';
  535. $this->root = $this->_normpath($this->options['path']);
  536. $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR;
  537. // default file attribute
  538. $this->defaults = array(
  539. 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true,
  540. 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true,
  541. 'locked' => false,
  542. 'hidden' => false
  543. );
  544. // root attributes
  545. $this->attributes[] = array(
  546. 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR).'$~',
  547. 'locked' => true,
  548. 'hidden' => false
  549. );
  550. // set files attributes
  551. if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) {
  552. foreach ($this->options['attributes'] as $a) {
  553. // attributes must contain pattern and at least one rule
  554. if (!empty($a['pattern']) || count($a) > 1) {
  555. $this->attributes[] = $a;
  556. }
  557. }
  558. }
  559. if (!empty($this->options['accessControl']) && is_callable($this->options['accessControl'])) {
  560. $this->access = $this->options['accessControl'];
  561. }
  562. $this->today = mktime(0,0,0, date('m'), date('d'), date('Y'));
  563. $this->yesterday = $this->today-86400;
  564. // debug($this->attributes);
  565. if (!$this->init()) {
  566. return false;
  567. }
  568. // check some options is arrays
  569. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  570. ? $this->options['uploadAllow']
  571. : array();
  572. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  573. ? $this->options['uploadDeny']
  574. : array();
  575. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  576. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  577. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  578. } else { // telephat_mode off
  579. $this->uploadOrder = $this->options['uploadOrder'];
  580. }
  581. if (!empty($this->options['uploadMaxSize'])) {
  582. $size = ''.$this->options['uploadMaxSize'];
  583. $unit = strtolower(substr($size, strlen($size) - 1));
  584. $n = 1;
  585. switch ($unit) {
  586. case 'k':
  587. $n = 1024;
  588. break;
  589. case 'm':
  590. $n = 1048576;
  591. break;
  592. case 'g':
  593. $n = 1073741824;
  594. }
  595. $this->uploadMaxSize = intval($size)*$n;
  596. }
  597. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  598. ? $this->options['disabled']
  599. : array();
  600. $this->cryptLib = $this->options['cryptLib'];
  601. $this->mimeDetect = $this->options['mimeDetect'];
  602. // find available mimetype detect method
  603. $type = strtolower($this->options['mimeDetect']);
  604. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  605. $regexp = '/text\/x\-(php|c\+\+)/';
  606. if (($type == 'finfo' || $type == 'auto')
  607. && class_exists('finfo')) {
  608. $tmpFileInfo = @explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
  609. } else {
  610. $tmpFileInfo = false;
  611. }
  612. if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
  613. $type = 'finfo';
  614. $this->finfo = finfo_open(FILEINFO_MIME);
  615. } elseif (($type == 'mime_content_type' || $type == 'auto')
  616. && function_exists('mime_content_type')
  617. && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
  618. $type = 'mime_content_type';
  619. } else {
  620. $type = 'internal';
  621. }
  622. $this->mimeDetect = $type;
  623. // load mimes from external file for mimeDetect == 'internal'
  624. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  625. // file must be in file directory or in parent one
  626. if ($this->mimeDetect == 'internal' && !self::$mimetypesLoaded) {
  627. self::$mimetypesLoaded = true;
  628. $this->mimeDetect = 'internal';
  629. $file = false;
  630. if (!empty($this->options['mimefile']) && file_exists($this->options['mimefile'])) {
  631. $file = $this->options['mimefile'];
  632. } elseif (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types')) {
  633. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types';
  634. } elseif (file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types')) {
  635. $file = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types';
  636. }
  637. if ($file && file_exists($file)) {
  638. $mimecf = file($file);
  639. foreach ($mimecf as $line_num => $line) {
  640. if (!preg_match('/^\s*#/', $line)) {
  641. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  642. for ($i = 1, $size = count($mime); $i < $size ; $i++) {
  643. if (!isset(self::$mimetypes[$mime[$i]])) {
  644. self::$mimetypes[$mime[$i]] = $mime[0];
  645. }
  646. }
  647. }
  648. }
  649. }
  650. }
  651. $this->rootName = empty($this->options['alias']) ? $this->_basename($this->root) : $this->options['alias'];
  652. $root = $this->stat($this->root);
  653. if (!$root) {
  654. return $this->setError('Root folder does not exists.');
  655. }
  656. if (!$root['read'] && !$root['write']) {
  657. return $this->setError('Root folder has not read and write permissions.');
  658. }
  659. // debug($root);
  660. if ($root['read']) {
  661. // check startPath - path to open by default instead of root
  662. if ($this->options['startPath']) {
  663. $start = $this->stat($this->options['startPath']);
  664. if (!empty($start)
  665. && $start['mime'] == 'directory'
  666. && $start['read']
  667. && empty($start['hidden'])
  668. && $this->_inpath($this->options['startPath'], $this->root)) {
  669. $this->startPath = $this->options['startPath'];
  670. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  671. $this->startPath = substr($this->startPath, 0, -1);
  672. }
  673. }
  674. }
  675. } else {
  676. $this->options['URL'] = '';
  677. $this->options['tmbURL'] = '';
  678. $this->options['tmbPath'] = '';
  679. // read only volume
  680. array_unshift($this->attributes, array(
  681. 'pattern' => '/.*/',
  682. 'read' => false
  683. ));
  684. }
  685. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  686. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  687. $this->URL = $this->options['URL'];
  688. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  689. $this->URL .= '/';
  690. }
  691. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  692. if ($this->tmbURL && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  693. $this->tmbURL .= '/';
  694. }
  695. $this->nameValidator = is_string($this->options['acceptedName']) && !empty($this->options['acceptedName'])
  696. ? $this->options['acceptedName']
  697. : '';
  698. $this->_checkArchivers();
  699. // manual control archive types to create
  700. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  701. foreach ($this->archivers['create'] as $mime => $v) {
  702. if (!in_array($mime, $this->options['archiveMimes'])) {
  703. unset($this->archivers['create'][$mime]);
  704. }
  705. }
  706. }
  707. // manualy add archivers
  708. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  709. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  710. if (strpos($mime, 'application/') === 0
  711. && !empty($conf['cmd'])
  712. && isset($conf['argc'])
  713. && !empty($conf['ext'])
  714. && !isset($this->archivers['create'][$mime])) {
  715. $this->archivers['create'][$mime] = $conf;
  716. }
  717. }
  718. }
  719. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  720. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  721. if (substr($mime, 'application/') === 0
  722. && !empty($cons['cmd'])
  723. && isset($conf['argc'])
  724. && !empty($conf['ext'])
  725. && !isset($this->archivers['extract'][$mime])) {
  726. $this->archivers['extract'][$mime] = $conf;
  727. }
  728. }
  729. }
  730. $this->configure();
  731. // echo $this->uploadMaxSize;
  732. // echo $this->options['uploadMaxSize'];
  733. return $this->mounted = true;
  734. }
  735. /**
  736. * Some "unmount" stuffs - may be required by virtual fs
  737. *
  738. * @return void
  739. * @author Dmitry (dio) Levashov
  740. **/
  741. public function umount() {
  742. }
  743. /**
  744. * Return error message from last failed action
  745. *
  746. * @return array
  747. * @author Dmitry (dio) Levashov
  748. **/
  749. public function error() {
  750. return $this->error;
  751. }
  752. /**
  753. * Set mimetypes allowed to display to client
  754. *
  755. * @param array $mimes
  756. * @return void
  757. * @author Dmitry (dio) Levashov
  758. **/
  759. public function setMimesFilter($mimes) {
  760. if (is_array($mimes)) {
  761. $this->onlyMimes = $mimes;
  762. }
  763. }
  764. /**
  765. * Return root folder hash
  766. *
  767. * @return string
  768. * @author Dmitry (dio) Levashov
  769. **/
  770. public function root() {
  771. return $this->encode($this->root);
  772. }
  773. /**
  774. * Return root or startPath hash
  775. *
  776. * @return string
  777. * @author Dmitry (dio) Levashov
  778. **/
  779. public function defaultPath() {
  780. return $this->encode($this->startPath ? $this->startPath : $this->root);
  781. }
  782. /**
  783. * Return volume options required by client:
  784. *
  785. * @return array
  786. * @author Dmitry (dio) Levashov
  787. **/
  788. public function options($hash) {
  789. return array(
  790. 'path' => $this->_path($this->decode($hash)),
  791. 'url' => $this->URL,
  792. 'tmbUrl' => $this->tmbURL,
  793. 'disabled' => $this->disabled,
  794. 'separator' => $this->separator,
  795. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  796. 'archivers' => array(
  797. // 'create' => array_keys($this->archivers['create']),
  798. // 'extract' => array_keys($this->archivers['extract']),
  799. 'create' => is_array($this->archivers['create']) ? array_keys($this->archivers['create']) : array(),
  800. 'extract' => is_array($this->archivers['extract']) ? array_keys($this->archivers['extract']) : array()
  801. )
  802. );
  803. }
  804. /**
  805. * Return true if command disabled in options
  806. *
  807. * @param string $cmd command name
  808. * @return bool
  809. * @author Dmitry (dio) Levashov
  810. **/
  811. public function commandDisabled($cmd) {
  812. return in_array($cmd, $this->disabled);
  813. }
  814. /**
  815. * Return true if mime is required mimes list
  816. *
  817. * @param string $mime mime type to check
  818. * @param array $mimes allowed mime types list or not set to use client mimes list
  819. * @param bool|null $empty what to return on empty list
  820. * @return bool|null
  821. * @author Dmitry (dio) Levashov
  822. * @author Troex Nevelin
  823. **/
  824. public function mimeAccepted($mime, $mimes = array(), $empty = true) {
  825. $mimes = !empty($mimes) ? $mimes : $this->onlyMimes;
  826. if (empty($mimes)) {
  827. return $empty;
  828. }
  829. return $mime == 'directory'
  830. || in_array('all', $mimes)
  831. || in_array('All', $mimes)
  832. || in_array($mime, $mimes)
  833. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  834. }
  835. /**
  836. * Return true if voume is readable.
  837. *
  838. * @return bool
  839. * @author Dmitry (dio) Levashov
  840. **/
  841. public function isReadable() {
  842. $stat = $this->stat($this->root);
  843. return $stat['read'];
  844. }
  845. /**
  846. * Return true if copy from this volume allowed
  847. *
  848. * @return bool
  849. * @author Dmitry (dio) Levashov
  850. **/
  851. public function copyFromAllowed() {
  852. return !!$this->options['copyFrom'];
  853. }
  854. /**
  855. * Return file path related to root
  856. *
  857. * @param string $hash file hash
  858. * @return string
  859. * @author Dmitry (dio) Levashov
  860. **/
  861. public function path($hash) {
  862. return $this->_path($this->decode($hash));
  863. }
  864. /**
  865. * Return file real path if file exists
  866. *
  867. * @param string $hash file hash
  868. * @return string
  869. * @author Dmitry (dio) Levashov
  870. **/
  871. public function realpath($hash) {
  872. $path = $this->decode($hash);
  873. return $this->stat($path) ? $path : false;
  874. }
  875. /**
  876. * Return list of moved/overwrited files
  877. *
  878. * @return array
  879. * @author Dmitry (dio) Levashov
  880. **/
  881. public function removed() {
  882. return $this->removed;
  883. }
  884. /**
  885. * Clean removed files list
  886. *
  887. * @return void
  888. * @author Dmitry (dio) Levashov
  889. **/
  890. public function resetRemoved() {
  891. $this->removed = array();
  892. }
  893. /**
  894. * Return file/dir hash or first founded child hash with required attr == $val
  895. *
  896. * @param string $hash file hash
  897. * @param string $attr attribute name
  898. * @param bool $val attribute value
  899. * @return string|false
  900. * @author Dmitry (dio) Levashov
  901. **/
  902. public function closest($hash, $attr, $val) {
  903. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  904. }
  905. /**
  906. * Return file info or false on error
  907. *
  908. * @param string $hash file hash
  909. * @param bool $realpath add realpath field to file info
  910. * @return array|false
  911. * @author Dmitry (dio) Levashov
  912. **/
  913. public function file($hash) {
  914. $path = $this->decode($hash);
  915. return ($file = $this->stat($path)) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  916. if (($file = $this->stat($path)) != false) {
  917. if ($realpath) {
  918. $file['realpath'] = $path;
  919. }
  920. return $file;
  921. }
  922. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  923. }
  924. /**
  925. * Return folder info
  926. *
  927. * @param string $hash folder hash
  928. * @param bool $hidden return hidden file info
  929. * @return array|false
  930. * @author Dmitry (dio) Levashov
  931. **/
  932. public function dir($hash, $resolveLink=false) {
  933. if (($dir = $this->file($hash)) == false) {
  934. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  935. }
  936. if ($resolveLink && !empty($dir['thash'])) {
  937. $dir = $this->file($dir['thash']);
  938. }
  939. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  940. ? $dir
  941. : $this->setError(elFinder::ERROR_NOT_DIR);
  942. }
  943. /**
  944. * Return directory content or false on error
  945. *
  946. * @param string $hash file hash
  947. * @return array|false
  948. * @author Dmitry (dio) Levashov
  949. **/
  950. public function scandir($hash) {
  951. if (($dir = $this->dir($hash)) == false) {
  952. return false;
  953. }
  954. return $dir['read']
  955. ? $this->getScandir($this->decode($hash))
  956. : $this->setError(elFinder::ERROR_PERM_DENIED);
  957. }
  958. /**
  959. * Return dir files names list
  960. *
  961. * @param string $hash file hash
  962. * @return array
  963. * @author Dmitry (dio) Levashov
  964. **/
  965. public function ls($hash) {
  966. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  967. return false;
  968. }
  969. $list = array();
  970. $path = $this->decode($hash);
  971. foreach ($this->getScandir($path) as $stat) {
  972. if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
  973. $list[] = $stat['name'];
  974. }
  975. }
  976. return $list;
  977. }
  978. /**
  979. * Return subfolders for required folder or false on error
  980. *
  981. * @param string $hash folder hash or empty string to get tree from root folder
  982. * @param int $deep subdir deep
  983. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  984. * @return array|false
  985. * @author Dmitry (dio) Levashov
  986. **/
  987. public function tree($hash='', $deep=0, $exclude='') {
  988. $path = $hash ? $this->decode($hash) : $this->root;
  989. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  990. return false;
  991. }
  992. $dirs = $this->gettree($path, $deep > 0 ? $deep -1 : $this->treeDeep-1, $exclude ? $this->decode($exclude) : null);
  993. array_unshift($dirs, $dir);
  994. return $dirs;
  995. }
  996. /**
  997. * Return part of dirs tree from required dir up to root dir
  998. *
  999. * @param string $hash directory hash
  1000. * @return array
  1001. * @author Dmitry (dio) Levashov
  1002. **/
  1003. public function parents($hash) {
  1004. if (($current = $this->dir($hash)) == false) {
  1005. return false;
  1006. }
  1007. $path = $this->decode($hash);
  1008. $tree = array();
  1009. while ($path && $path != $this->root) {
  1010. $path = $this->_dirname($path);
  1011. $stat = $this->stat($path);
  1012. if (!empty($stat['hidden']) || !$stat['read']) {
  1013. return false;
  1014. }
  1015. array_unshift($tree, $stat);
  1016. if ($path != $this->root) {
  1017. foreach ($this->gettree($path, 0) as $dir) {
  1018. if (!in_array($dir, $tree)) {
  1019. $tree[] = $dir;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. return $tree ? $tree : array($current);
  1025. }
  1026. /**
  1027. * Create thumbnail for required file and return its name of false on failed
  1028. *
  1029. * @return string|false
  1030. * @author Dmitry (dio) Levashov
  1031. **/
  1032. public function tmb($hash) {
  1033. $path = $this->decode($hash);
  1034. $stat = $this->stat($path);
  1035. if (isset($stat['tmb'])) {
  1036. return $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1037. }
  1038. return false;
  1039. }
  1040. /**
  1041. * Return file size / total directory size
  1042. *
  1043. * @param string file hash
  1044. * @return int
  1045. * @author Dmitry (dio) Levashov
  1046. **/
  1047. public function size($hash) {
  1048. return $this->countSize($this->decode($hash));
  1049. }
  1050. /**
  1051. * Open file for reading and return file pointer
  1052. *
  1053. * @param string file hash
  1054. * @return Resource
  1055. * @author Dmitry (dio) Levashov
  1056. **/
  1057. public function open($hash) {
  1058. if (($file = $this->file($hash)) == false
  1059. || $file['mime'] == 'directory') {
  1060. return false;
  1061. }
  1062. return $this->_fopen($this->decode($hash), 'rb');
  1063. }
  1064. /**
  1065. * Close file pointer
  1066. *
  1067. * @param Resource $fp file pointer
  1068. * @param string $hash file hash
  1069. * @return void
  1070. * @author Dmitry (dio) Levashov
  1071. **/
  1072. public function close($fp, $hash) {
  1073. $this->_fclose($fp, $this->decode($hash));
  1074. }
  1075. /**
  1076. * Create directory and return dir info
  1077. *
  1078. * @param string $dst destination directory
  1079. * @param string $name directory name
  1080. * @return array|false
  1081. * @author Dmitry (dio) Levashov
  1082. **/
  1083. public function mkdir($dst, $name) {
  1084. if ($this->commandDisabled('mkdir')) {
  1085. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1086. }
  1087. if (!$this->nameAccepted($name)) {
  1088. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1089. }
  1090. if (($dir = $this->dir($dst)) == false) {
  1091. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1092. }
  1093. if (!$dir['write']) {
  1094. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1095. }
  1096. $path = $this->decode($dst);
  1097. $dst = $this->_joinPath($path, $name);
  1098. $stat = $this->stat($dst);
  1099. if (!empty($stat)) {
  1100. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1101. }
  1102. $this->clearcache();
  1103. return ($path = $this->_mkdir($path, $name)) ? $this->stat($path) : false;
  1104. }
  1105. /**
  1106. * Create empty file and return its info
  1107. *
  1108. * @param string $dst destination directory
  1109. * @param string $name file name
  1110. * @return array|false
  1111. * @author Dmitry (dio) Levashov
  1112. **/
  1113. public function mkfile($dst, $name) {
  1114. if ($this->commandDisabled('mkfile')) {
  1115. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1116. }
  1117. if (!$this->nameAccepted($name)) {
  1118. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1119. }
  1120. if (($dir = $this->dir($dst)) == false) {
  1121. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1122. }
  1123. $path = $this->decode($dst);
  1124. if (!$dir['write'] || !$this->allowCreate($path, $name)) {
  1125. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1126. }
  1127. if ($this->stat($this->_joinPath($path, $name))) {
  1128. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1129. }
  1130. $this->clearcache();
  1131. return ($path = $this->_mkfile($path, $name)) ? $this->stat($path) : false;
  1132. }
  1133. /**
  1134. * Rename file and return file info
  1135. *
  1136. * @param string $hash file hash
  1137. * @param string $name new file name
  1138. * @return array|false
  1139. * @author Dmitry (dio) Levashov
  1140. **/
  1141. public function rename($hash, $name) {
  1142. if ($this->commandDisabled('rename')) {
  1143. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1144. }
  1145. if (!$this->nameAccepted($name)) {
  1146. return $this->setError(elFinder::ERROR_INVALID_NAME, $name);
  1147. }
  1148. if (!($file = $this->file($hash))) {
  1149. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1150. }
  1151. if ($name == $file['name']) {
  1152. return $file;
  1153. }
  1154. if (!empty($file['locked'])) {
  1155. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  1156. }
  1157. $path = $this->decode($hash);
  1158. $dir = $this->_dirname($path);
  1159. $stat = $this->stat($this->_joinPath($dir, $name));
  1160. if ($stat) {
  1161. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1162. }
  1163. if (!$this->allowCreate($dir, $name)) {
  1164. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1165. }
  1166. $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move
  1167. if (($path = $this->_move($path, $dir, $name))) {
  1168. $this->clearcache();
  1169. return $this->stat($path);
  1170. }
  1171. return false;
  1172. }
  1173. /**
  1174. * Create file copy with suffix "copy number" and return its info
  1175. *
  1176. * @param string $hash file hash
  1177. * @param string $suffix suffix to add to file name
  1178. * @return array|false
  1179. * @author Dmitry (dio) Levashov
  1180. **/
  1181. public function duplicate($hash, $suffix='copy') {
  1182. if ($this->commandDisabled('duplicate')) {
  1183. return $this->setError(elFinder::ERROR_COPY, '#'.$hash, elFinder::ERROR_PERM_DENIED);
  1184. }
  1185. if (($file = $this->file($hash)) == false) {
  1186. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  1187. }
  1188. $path = $this->decode($hash);
  1189. $dir = $this->_dirname($path);
  1190. $name = $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' ');
  1191. if (!$this->allowCreate($dir, $name)) {
  1192. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1193. }
  1194. return ($path = $this->copy($path, $dir, $name)) == false
  1195. ? false
  1196. : $this->stat($path);
  1197. }
  1198. /**
  1199. * Save uploaded file.
  1200. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  1201. *
  1202. * @param Resource $fp file pointer
  1203. * @param string $dst destination folder hash
  1204. * @param string $src file name
  1205. * @param string $tmpname file tmp name - required to detect mime type
  1206. * @return array|false
  1207. * @author Dmitry (dio) Levashov
  1208. **/
  1209. public function upload($fp, $dst, $name, $tmpname) {
  1210. if ($this->commandDisabled('upload')) {
  1211. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1212. }
  1213. if (($dir = $this->dir($dst)) == false) {
  1214. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1215. }
  1216. if (!$dir['write']) {
  1217. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1218. }
  1219. if (!$this->nameAccepted($name)) {
  1220. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1221. }
  1222. $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname, $name);
  1223. if ($mime == 'unknown' && $this->mimeDetect == 'internal') {
  1224. $mime = elFinderVolumeDriver::mimetypeInternalDetect($name);
  1225. }
  1226. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  1227. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  1228. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  1229. $upload = true; // default to allow
  1230. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  1231. $upload = false; // default is deny
  1232. if (!$deny && ($allow === true)) { // match only allow
  1233. $upload = true;
  1234. }// else (both match | no match | match only deny) { deny }
  1235. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  1236. $upload = true; // default is allow
  1237. if (($deny === true) && !$allow) { // match only deny
  1238. $upload = false;
  1239. } // else (both match | no match | match only allow) { allow }
  1240. }
  1241. if (!$upload) {
  1242. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  1243. }
  1244. if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) {
  1245. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  1246. }
  1247. $dstpath = $this->decode($dst);
  1248. $test = $this->_joinPath($dstpath, $name);
  1249. $file = $this->stat($test);
  1250. $this->clearcache();
  1251. if ($file) { // file exists
  1252. if ($this->options['uploadOverwrite']) {
  1253. if (!$file['write']) {
  1254. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1255. } elseif ($file['mime'] == 'directory') {
  1256. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  1257. }
  1258. $this->remove($test);
  1259. } else {
  1260. $name = $this->uniqueName($dstpath, $name, '-', false);
  1261. }
  1262. }
  1263. $stat = array(
  1264. 'mime' => $mime,
  1265. 'width' => 0,
  1266. 'height' => 0,
  1267. 'size' => filesize($tmpname));
  1268. // $w = $h = 0;
  1269. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  1270. $stat['width'] = $s[0];
  1271. $stat['height'] = $s[1];
  1272. }
  1273. // $this->clearcache();
  1274. if (($path = $this->_save($fp, $dstpath, $name, $stat)) == false) {
  1275. return false;
  1276. }
  1277. return $this->stat($path);
  1278. }
  1279. /**
  1280. * Paste files
  1281. *
  1282. * @param Object $volume source volume
  1283. * @param string $source file hash
  1284. * @param string $dst destination dir hash
  1285. * @param bool $rmSrc remove source after copy?
  1286. * @return array|false
  1287. * @author Dmitry (dio) Levashov
  1288. **/
  1289. public function paste($volume, $src, $dst, $rmSrc = false) {
  1290. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  1291. if ($this->commandDisabled('paste')) {
  1292. return $this->setError($err, '#'.$src, elFinder::ERROR_PERM_DENIED);
  1293. }
  1294. if (($file = $volume->file($src, $rmSrc)) == false) {
  1295. return $this->setError($err, '#'.$src, elFinder::ERROR_FILE_NOT_FOUND);
  1296. }
  1297. $name = $file['name'];
  1298. $errpath = $volume->path($src);
  1299. if (($dir = $this->dir($dst)) == false) {
  1300. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1301. }
  1302. if (!$dir['write'] || !$file['read']) {
  1303. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1304. }
  1305. $destination = $this->decode($dst);
  1306. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  1307. return $rmSrc
  1308. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  1309. : $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1310. }
  1311. $test = $this->_joinPath($destination, $name);
  1312. $stat = $this->stat($test);
  1313. $this->clearcache();
  1314. if ($stat) {
  1315. if ($this->options['copyOverwrite']) {
  1316. // do not replace file with dir or dir with file
  1317. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  1318. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->_path($test));
  1319. }
  1320. // existed file is not writable
  1321. if (!$stat['write']) {
  1322. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1323. }
  1324. // existed file locked or has locked child
  1325. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  1326. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($locked));
  1327. }
  1328. // target is entity file of alias
  1329. if ($volume == $this && ($test == @$file['target'] || $test == $this->decode($src))) {
  1330. return $this->setError(elFinder::ERROR_REPLACE, $errpath);
  1331. }
  1332. // remove existed file
  1333. if (!$this->remove($test)) {
  1334. return $this->setError(elFinder::ERROR_REPLACE, $this->_path($test));
  1335. }
  1336. } else {
  1337. $name = $this->uniqueName($destination, $name, ' ', false);
  1338. }
  1339. }
  1340. // copy/move inside current volume
  1341. if ($volume == $this) {
  1342. $source = $this->decode($src);
  1343. // do not copy into itself
  1344. if ($this->_inpath($destination, $source)) {
  1345. return $this->setError(elFinder::ERROR_COPY_INTO_ITSELF, $errpath);
  1346. }
  1347. $method = $rmSrc ? 'move' : 'copy';
  1348. return ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false;
  1349. }
  1350. // copy/move from another volume
  1351. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  1352. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  1353. }
  1354. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  1355. return false;
  1356. }
  1357. if ($rmSrc) {
  1358. if ($volume->rm($src)) {
  1359. $this->removed[] = $file;
  1360. } else {
  1361. return $this->setError(elFinder::ERROR_MOVE, $errpath, elFinder::ERROR_RM_SRC);
  1362. }
  1363. }
  1364. return $this->stat($path);
  1365. }
  1366. /**
  1367. * Return file contents
  1368. *
  1369. * @param string $hash file hash
  1370. * @return string|false
  1371. * @author Dmitry (dio) Levashov
  1372. **/
  1373. public function getContents($hash) {
  1374. $file = $this->file($hash);
  1375. if (!$file) {
  1376. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1377. }
  1378. if ($file['mime'] == 'directory') {
  1379. return $this->setError(elFinder::ERROR_NOT_FILE);
  1380. }
  1381. if (!$file['read']) {
  1382. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1383. }
  1384. return $this->_getContents($this->decode($hash));
  1385. }
  1386. /**
  1387. * Put content in text file and return file info.
  1388. *
  1389. * @param string $hash file hash
  1390. * @param string $content new file content
  1391. * @return array
  1392. * @author Dmitry (dio) Levashov
  1393. **/
  1394. public function putContents($hash, $content) {
  1395. if ($this->commandDisabled('edit')) {
  1396. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1397. }
  1398. $path = $this->decode($hash);
  1399. if (!($file = $this->file($hash))) {
  1400. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1401. }
  1402. if (!$file['write']) {
  1403. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1404. }
  1405. $this->clearcache();
  1406. return $this->_filePutContents($path, $content) ? $this->stat($path) : false;
  1407. }
  1408. /**
  1409. * Extract files from archive
  1410. *
  1411. * @param string $hash archive hash
  1412. * @return array|bool
  1413. * @author Dmitry (dio) Levashov,
  1414. * @author Alexey Sukhotin
  1415. **/
  1416. public function extract($hash) {
  1417. if ($this->commandDisabled('extract')) {
  1418. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1419. }
  1420. if (($file = $this->file($hash)) == false) {
  1421. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1422. }
  1423. $archiver = isset($this->archivers['extract'][$file['mime']])
  1424. ? $this->archivers['extract'][$file['mime']]
  1425. : false;
  1426. if (!$archiver) {
  1427. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  1428. }
  1429. $path = $this->decode($hash);
  1430. $parent = $this->stat($this->_dirname($path));
  1431. if (!$file['read'] || !$parent['write']) {
  1432. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1433. }
  1434. $this->clearcache();
  1435. return ($path = $this->_extract($path, $archiver)) ? $this->stat($path) : false;
  1436. }
  1437. /**
  1438. * Add files to archive
  1439. *
  1440. * @return void
  1441. **/
  1442. public function archive($hashes, $mime) {
  1443. if ($this->commandDisabled('archive')) {
  1444. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1445. }
  1446. $archiver = isset($this->archivers['create'][$mime])
  1447. ? $this->archivers['create'][$mime]
  1448. : false;
  1449. if (!$archiver) {
  1450. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  1451. }
  1452. $files = array();
  1453. foreach ($hashes as $hash) {
  1454. if (($file = $this->file($hash)) == false) {
  1455. return $this->error(elFinder::ERROR_FILE_NOT_FOUND, '#'+$hash);
  1456. }
  1457. if (!$file['read']) {
  1458. return $this->error(elFinder::ERROR_PERM_DENIED);
  1459. }
  1460. $path = $this->decode($hash);
  1461. if (!isset($dir)) {
  1462. $dir = $this->_dirname($path);
  1463. $stat = $this->stat($dir);
  1464. if (!$stat['write']) {
  1465. return $this->error(elFinder::ERROR_PERM_DENIED);
  1466. }
  1467. }
  1468. $files[] = $this->_basename($path);
  1469. }
  1470. $name = (count($files) == 1 ? $files[0] : 'Archive').'.'.$archiver['ext'];
  1471. $name = $this->uniqueName($dir, $name, '');
  1472. $this->clearcache();
  1473. return ($path = $this->_archive($dir, $files, $name, $archiver)) ? $this->stat($path) : false;
  1474. }
  1475. /**
  1476. * Resize image
  1477. *
  1478. * @param string $hash image file
  1479. * @param int $width new width
  1480. * @param int $height new height
  1481. * @param int $x X start poistion for crop
  1482. * @param int $y Y start poistion for crop
  1483. * @param string $mode action how to mainpulate image
  1484. * @return array|false
  1485. * @author Dmitry (dio) Levashov
  1486. * @author Alexey Sukhotin
  1487. * @author nao-pon
  1488. * @author Troex Nevelin
  1489. **/
  1490. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
  1491. if ($this->commandDisabled('resize')) {
  1492. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1493. }
  1494. if (($file = $this->file($hash)) == false) {
  1495. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1496. }
  1497. if (!$file['write'] || !$file['read']) {
  1498. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1499. }
  1500. $path = $this->decode($hash);
  1501. if (!$this->canResize($path, $file)) {
  1502. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  1503. }
  1504. switch($mode) {
  1505. case 'propresize':
  1506. $result = $this->imgResize($path, $width, $height, true, true);
  1507. break;
  1508. case 'crop':
  1509. $result = $this->imgCrop($path, $width, $height, $x, $y);
  1510. break;
  1511. case 'fitsquare':
  1512. $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']));
  1513. break;
  1514. case 'rotate':
  1515. $result = $this->imgRotate($path, $degree, ($bg ? $bg : $this->options['tmbBgColor']));
  1516. break;
  1517. default:
  1518. $result = $this->imgResize($path, $width, $height, false, true);
  1519. break;
  1520. }
  1521. if ($result) {
  1522. $this->rmTmb($file);
  1523. $this->clearcache();
  1524. return $this->stat($path);
  1525. }
  1526. return false;
  1527. }
  1528. /**
  1529. * Remove file/dir
  1530. *
  1531. * @param string $hash file hash
  1532. * @return bool
  1533. * @author Dmitry (dio) Levashov
  1534. **/
  1535. public function rm($hash) {
  1536. return $this->commandDisabled('rm')
  1537. ? array(elFinder::ERROR_ACCESS_DENIED)
  1538. : $this->remove($this->decode($hash));
  1539. }
  1540. /**
  1541. * Search files
  1542. *
  1543. * @param string $q search string
  1544. * @param array $mimes
  1545. * @return array
  1546. * @author Dmitry (dio) Levashov
  1547. **/
  1548. public function search($q, $mimes) {
  1549. return $this->doSearch($this->root, $q, $mimes);
  1550. }
  1551. /**
  1552. * Return image dimensions
  1553. *
  1554. * @param string $hash file hash
  1555. * @return array
  1556. * @author Dmitry (dio) Levashov
  1557. **/
  1558. public function dimensions($hash) {
  1559. if (($file = $this->file($hash)) == false) {
  1560. return false;
  1561. }
  1562. return $this->_dimensions($this->decode($hash), $file['mime']);
  1563. }
  1564. /**
  1565. * Save error message
  1566. *
  1567. * @param array error
  1568. * @return false
  1569. * @author Dmitry(dio) Levashov
  1570. **/
  1571. protected function setError($error) {
  1572. $this->error = array();
  1573. foreach (func_get_args() as $err) {
  1574. if (is_array($err)) {
  1575. $this->error = array_merge($this->error, $err);
  1576. } else {
  1577. $this->error[] = $err;
  1578. }
  1579. }
  1580. // $this->error = is_array($error) ? $error : func_get_args();
  1581. return false;
  1582. }
  1583. /*********************************************************************/
  1584. /* FS API */
  1585. /*********************************************************************/
  1586. /***************** paths *******************/
  1587. /**
  1588. * Encode path into hash
  1589. *
  1590. * @param string file path
  1591. * @return string
  1592. * @author Dmitry (dio) Levashov
  1593. * @author Troex Nevelin
  1594. **/
  1595. protected function encode($path) {
  1596. if ($path !== '') {
  1597. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1598. $p = $this->_relpath($path);
  1599. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1600. if ($p === '') {
  1601. $p = DIRECTORY_SEPARATOR;
  1602. }
  1603. // TODO crypt path and return hash
  1604. $hash = $this->crypt($p);
  1605. // hash is used as id in HTML that means it must contain vaild chars
  1606. // make base64 html safe and append prefi…

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