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

/elfinder/php/elFinderVolumeDriver.php

https://bitbucket.org/migelsabre/elfinderintegration
PHP | 3504 lines | 1753 code | 498 blank | 1253 comment | 524 complexity | 7a153c3021d6cc22424362c3f7521597 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'])) {
  560. if (is_string($this->options['accessControl'])
  561. && function_exists($this->options['accessControl'])) {
  562. $this->access = $this->options['accessControl'];
  563. } elseif (is_array($this->options['accessControl'])
  564. && count($this->options['accessControl']) > 1
  565. && is_object($this->options['accessControl'][0])
  566. && method_exists($this->options['accessControl'][0], $this->options['accessControl'][1])) {
  567. $this->access = array($this->options['accessControl'][0], $this->options['accessControl'][1]);
  568. }
  569. }
  570. $this->today = mktime(0,0,0, date('m'), date('d'), date('Y'));
  571. $this->yesterday = $this->today-86400;
  572. // debug($this->attributes);
  573. if (!$this->init()) {
  574. return false;
  575. }
  576. // check some options is arrays
  577. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  578. ? $this->options['uploadAllow']
  579. : array();
  580. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  581. ? $this->options['uploadDeny']
  582. : array();
  583. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  584. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  585. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  586. } else { // telephat_mode off
  587. $this->uploadOrder = $this->options['uploadOrder'];
  588. }
  589. if (!empty($this->options['uploadMaxSize'])) {
  590. $size = ''.$this->options['uploadMaxSize'];
  591. $unit = strtolower(substr($size, strlen($size) - 1));
  592. $n = 1;
  593. switch ($unit) {
  594. case 'k':
  595. $n = 1024;
  596. break;
  597. case 'm':
  598. $n = 1048576;
  599. break;
  600. case 'g':
  601. $n = 1073741824;
  602. }
  603. $this->uploadMaxSize = intval($size)*$n;
  604. }
  605. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  606. ? $this->options['disabled']
  607. : array();
  608. $this->cryptLib = $this->options['cryptLib'];
  609. $this->mimeDetect = $this->options['mimeDetect'];
  610. // find available mimetype detect method
  611. $type = strtolower($this->options['mimeDetect']);
  612. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  613. $regexp = '/text\/x\-(php|c\+\+)/';
  614. if (($type == 'finfo' || $type == 'auto')
  615. && class_exists('finfo')) {
  616. $tmpFileInfo = @explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
  617. } else {
  618. $tmpFileInfo = false;
  619. }
  620. if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
  621. $type = 'finfo';
  622. $this->finfo = finfo_open(FILEINFO_MIME);
  623. } elseif (($type == 'mime_content_type' || $type == 'auto')
  624. && function_exists('mime_content_type')
  625. && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
  626. $type = 'mime_content_type';
  627. } else {
  628. $type = 'internal';
  629. }
  630. $this->mimeDetect = $type;
  631. // load mimes from external file for mimeDetect == 'internal'
  632. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  633. // file must be in file directory or in parent one
  634. if ($this->mimeDetect == 'internal' && !self::$mimetypesLoaded) {
  635. self::$mimetypesLoaded = true;
  636. $this->mimeDetect = 'internal';
  637. $file = false;
  638. if (!empty($this->options['mimefile']) && file_exists($this->options['mimefile'])) {
  639. $file = $this->options['mimefile'];
  640. } elseif (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types')) {
  641. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types';
  642. } elseif (file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types')) {
  643. $file = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types';
  644. }
  645. if ($file && file_exists($file)) {
  646. $mimecf = file($file);
  647. foreach ($mimecf as $line_num => $line) {
  648. if (!preg_match('/^\s*#/', $line)) {
  649. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  650. for ($i = 1, $size = count($mime); $i < $size ; $i++) {
  651. if (!isset(self::$mimetypes[$mime[$i]])) {
  652. self::$mimetypes[$mime[$i]] = $mime[0];
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. $this->rootName = empty($this->options['alias']) ? $this->_basename($this->root) : $this->options['alias'];
  660. $root = $this->stat($this->root);
  661. if (!$root) {
  662. return $this->setError('Root folder does not exists.');
  663. }
  664. if (!$root['read'] && !$root['write']) {
  665. return $this->setError('Root folder has not read and write permissions.');
  666. }
  667. // debug($root);
  668. if ($root['read']) {
  669. // check startPath - path to open by default instead of root
  670. if ($this->options['startPath']) {
  671. $start = $this->stat($this->options['startPath']);
  672. if (!empty($start)
  673. && $start['mime'] == 'directory'
  674. && $start['read']
  675. && empty($start['hidden'])
  676. && $this->_inpath($this->options['startPath'], $this->root)) {
  677. $this->startPath = $this->options['startPath'];
  678. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  679. $this->startPath = substr($this->startPath, 0, -1);
  680. }
  681. }
  682. }
  683. } else {
  684. $this->options['URL'] = '';
  685. $this->options['tmbURL'] = '';
  686. $this->options['tmbPath'] = '';
  687. // read only volume
  688. array_unshift($this->attributes, array(
  689. 'pattern' => '/.*/',
  690. 'read' => false
  691. ));
  692. }
  693. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  694. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  695. $this->URL = $this->options['URL'];
  696. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  697. $this->URL .= '/';
  698. }
  699. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  700. if ($this->tmbURL && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  701. $this->tmbURL .= '/';
  702. }
  703. $this->nameValidator = is_string($this->options['acceptedName']) && !empty($this->options['acceptedName'])
  704. ? $this->options['acceptedName']
  705. : '';
  706. $this->_checkArchivers();
  707. // manual control archive types to create
  708. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  709. foreach ($this->archivers['create'] as $mime => $v) {
  710. if (!in_array($mime, $this->options['archiveMimes'])) {
  711. unset($this->archivers['create'][$mime]);
  712. }
  713. }
  714. }
  715. // manualy add archivers
  716. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  717. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  718. if (strpos($mime, 'application/') === 0
  719. && !empty($conf['cmd'])
  720. && isset($conf['argc'])
  721. && !empty($conf['ext'])
  722. && !isset($this->archivers['create'][$mime])) {
  723. $this->archivers['create'][$mime] = $conf;
  724. }
  725. }
  726. }
  727. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  728. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  729. if (substr($mime, 'application/') === 0
  730. && !empty($cons['cmd'])
  731. && isset($conf['argc'])
  732. && !empty($conf['ext'])
  733. && !isset($this->archivers['extract'][$mime])) {
  734. $this->archivers['extract'][$mime] = $conf;
  735. }
  736. }
  737. }
  738. $this->configure();
  739. // echo $this->uploadMaxSize;
  740. // echo $this->options['uploadMaxSize'];
  741. return $this->mounted = true;
  742. }
  743. /**
  744. * Some "unmount" stuffs - may be required by virtual fs
  745. *
  746. * @return void
  747. * @author Dmitry (dio) Levashov
  748. **/
  749. public function umount() {
  750. }
  751. /**
  752. * Return error message from last failed action
  753. *
  754. * @return array
  755. * @author Dmitry (dio) Levashov
  756. **/
  757. public function error() {
  758. return $this->error;
  759. }
  760. /**
  761. * Set mimetypes allowed to display to client
  762. *
  763. * @param array $mimes
  764. * @return void
  765. * @author Dmitry (dio) Levashov
  766. **/
  767. public function setMimesFilter($mimes) {
  768. if (is_array($mimes)) {
  769. $this->onlyMimes = $mimes;
  770. }
  771. }
  772. /**
  773. * Return root folder hash
  774. *
  775. * @return string
  776. * @author Dmitry (dio) Levashov
  777. **/
  778. public function root() {
  779. return $this->encode($this->root);
  780. }
  781. /**
  782. * Return root or startPath hash
  783. *
  784. * @return string
  785. * @author Dmitry (dio) Levashov
  786. **/
  787. public function defaultPath() {
  788. return $this->encode($this->startPath ? $this->startPath : $this->root);
  789. }
  790. /**
  791. * Return volume options required by client:
  792. *
  793. * @return array
  794. * @author Dmitry (dio) Levashov
  795. **/
  796. public function options($hash) {
  797. return array(
  798. 'path' => $this->_path($this->decode($hash)),
  799. 'url' => $this->URL,
  800. 'tmbUrl' => $this->tmbURL,
  801. 'disabled' => $this->disabled,
  802. 'separator' => $this->separator,
  803. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  804. 'archivers' => array(
  805. 'create' => array_keys($this->archivers['create']),
  806. 'extract' => array_keys($this->archivers['extract'])
  807. )
  808. );
  809. }
  810. /**
  811. * Return true if command disabled in options
  812. *
  813. * @param string $cmd command name
  814. * @return bool
  815. * @author Dmitry (dio) Levashov
  816. **/
  817. public function commandDisabled($cmd) {
  818. return in_array($cmd, $this->disabled);
  819. }
  820. /**
  821. * Return true if mime is required mimes list
  822. *
  823. * @param string $mime mime type to check
  824. * @param array $mimes allowed mime types list or not set to use client mimes list
  825. * @param bool|null $empty what to return on empty list
  826. * @return bool|null
  827. * @author Dmitry (dio) Levashov
  828. * @author Troex Nevelin
  829. **/
  830. public function mimeAccepted($mime, $mimes = array(), $empty = true) {
  831. $mimes = !empty($mimes) ? $mimes : $this->onlyMimes;
  832. if (empty($mimes)) {
  833. return $empty;
  834. }
  835. return $mime == 'directory'
  836. || in_array('all', $mimes)
  837. || in_array('All', $mimes)
  838. || in_array($mime, $mimes)
  839. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  840. }
  841. /**
  842. * Return true if voume is readable.
  843. *
  844. * @return bool
  845. * @author Dmitry (dio) Levashov
  846. **/
  847. public function isReadable() {
  848. $stat = $this->stat($this->root);
  849. return $stat['read'];
  850. }
  851. /**
  852. * Return true if copy from this volume allowed
  853. *
  854. * @return bool
  855. * @author Dmitry (dio) Levashov
  856. **/
  857. public function copyFromAllowed() {
  858. return !!$this->options['copyFrom'];
  859. }
  860. /**
  861. * Return file path related to root
  862. *
  863. * @param string $hash file hash
  864. * @return string
  865. * @author Dmitry (dio) Levashov
  866. **/
  867. public function path($hash) {
  868. return $this->_path($this->decode($hash));
  869. }
  870. /**
  871. * Return file real path if file exists
  872. *
  873. * @param string $hash file hash
  874. * @return string
  875. * @author Dmitry (dio) Levashov
  876. **/
  877. public function realpath($hash) {
  878. $path = $this->decode($hash);
  879. return $this->stat($path) ? $path : false;
  880. }
  881. /**
  882. * Return list of moved/overwrited files
  883. *
  884. * @return array
  885. * @author Dmitry (dio) Levashov
  886. **/
  887. public function removed() {
  888. return $this->removed;
  889. }
  890. /**
  891. * Clean removed files list
  892. *
  893. * @return void
  894. * @author Dmitry (dio) Levashov
  895. **/
  896. public function resetRemoved() {
  897. $this->removed = array();
  898. }
  899. /**
  900. * Return file/dir hash or first founded child hash with required attr == $val
  901. *
  902. * @param string $hash file hash
  903. * @param string $attr attribute name
  904. * @param bool $val attribute value
  905. * @return string|false
  906. * @author Dmitry (dio) Levashov
  907. **/
  908. public function closest($hash, $attr, $val) {
  909. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  910. }
  911. /**
  912. * Return file info or false on error
  913. *
  914. * @param string $hash file hash
  915. * @param bool $realpath add realpath field to file info
  916. * @return array|false
  917. * @author Dmitry (dio) Levashov
  918. **/
  919. public function file($hash) {
  920. $path = $this->decode($hash);
  921. return ($file = $this->stat($path)) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  922. if (($file = $this->stat($path)) != false) {
  923. if ($realpath) {
  924. $file['realpath'] = $path;
  925. }
  926. return $file;
  927. }
  928. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  929. }
  930. /**
  931. * Return folder info
  932. *
  933. * @param string $hash folder hash
  934. * @param bool $hidden return hidden file info
  935. * @return array|false
  936. * @author Dmitry (dio) Levashov
  937. **/
  938. public function dir($hash, $resolveLink=false) {
  939. if (($dir = $this->file($hash)) == false) {
  940. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  941. }
  942. if ($resolveLink && !empty($dir['thash'])) {
  943. $dir = $this->file($dir['thash']);
  944. }
  945. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  946. ? $dir
  947. : $this->setError(elFinder::ERROR_NOT_DIR);
  948. }
  949. /**
  950. * Return directory content or false on error
  951. *
  952. * @param string $hash file hash
  953. * @return array|false
  954. * @author Dmitry (dio) Levashov
  955. **/
  956. public function scandir($hash) {
  957. if (($dir = $this->dir($hash)) == false) {
  958. return false;
  959. }
  960. return $dir['read']
  961. ? $this->getScandir($this->decode($hash))
  962. : $this->setError(elFinder::ERROR_PERM_DENIED);
  963. }
  964. /**
  965. * Return dir files names list
  966. *
  967. * @param string $hash file hash
  968. * @return array
  969. * @author Dmitry (dio) Levashov
  970. **/
  971. public function ls($hash) {
  972. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  973. return false;
  974. }
  975. $list = array();
  976. $path = $this->decode($hash);
  977. foreach ($this->getScandir($path) as $stat) {
  978. if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
  979. $list[] = $stat['name'];
  980. }
  981. }
  982. return $list;
  983. }
  984. /**
  985. * Return subfolders for required folder or false on error
  986. *
  987. * @param string $hash folder hash or empty string to get tree from root folder
  988. * @param int $deep subdir deep
  989. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  990. * @return array|false
  991. * @author Dmitry (dio) Levashov
  992. **/
  993. public function tree($hash='', $deep=0, $exclude='') {
  994. $path = $hash ? $this->decode($hash) : $this->root;
  995. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  996. return false;
  997. }
  998. $dirs = $this->gettree($path, $deep > 0 ? $deep -1 : $this->treeDeep-1, $exclude ? $this->decode($exclude) : null);
  999. array_unshift($dirs, $dir);
  1000. return $dirs;
  1001. }
  1002. /**
  1003. * Return part of dirs tree from required dir up to root dir
  1004. *
  1005. * @param string $hash directory hash
  1006. * @return array
  1007. * @author Dmitry (dio) Levashov
  1008. **/
  1009. public function parents($hash) {
  1010. if (($current = $this->dir($hash)) == false) {
  1011. return false;
  1012. }
  1013. $path = $this->decode($hash);
  1014. $tree = array();
  1015. while ($path && $path != $this->root) {
  1016. $path = $this->_dirname($path);
  1017. $stat = $this->stat($path);
  1018. if (!empty($stat['hidden']) || !$stat['read']) {
  1019. return false;
  1020. }
  1021. array_unshift($tree, $stat);
  1022. if ($path != $this->root) {
  1023. foreach ($this->gettree($path, 0) as $dir) {
  1024. if (!in_array($dir, $tree)) {
  1025. $tree[] = $dir;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. return $tree ? $tree : array($current);
  1031. }
  1032. /**
  1033. * Create thumbnail for required file and return its name of false on failed
  1034. *
  1035. * @return string|false
  1036. * @author Dmitry (dio) Levashov
  1037. **/
  1038. public function tmb($hash) {
  1039. $path = $this->decode($hash);
  1040. $stat = $this->stat($path);
  1041. if (isset($stat['tmb'])) {
  1042. return $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1043. }
  1044. return false;
  1045. }
  1046. /**
  1047. * Return file size / total directory size
  1048. *
  1049. * @param string file hash
  1050. * @return int
  1051. * @author Dmitry (dio) Levashov
  1052. **/
  1053. public function size($hash) {
  1054. return $this->countSize($this->decode($hash));
  1055. }
  1056. /**
  1057. * Open file for reading and return file pointer
  1058. *
  1059. * @param string file hash
  1060. * @return Resource
  1061. * @author Dmitry (dio) Levashov
  1062. **/
  1063. public function open($hash) {
  1064. if (($file = $this->file($hash)) == false
  1065. || $file['mime'] == 'directory') {
  1066. return false;
  1067. }
  1068. return $this->_fopen($this->decode($hash), 'rb');
  1069. }
  1070. /**
  1071. * Close file pointer
  1072. *
  1073. * @param Resource $fp file pointer
  1074. * @param string $hash file hash
  1075. * @return void
  1076. * @author Dmitry (dio) Levashov
  1077. **/
  1078. public function close($fp, $hash) {
  1079. $this->_fclose($fp, $this->decode($hash));
  1080. }
  1081. /**
  1082. * Create directory and return dir info
  1083. *
  1084. * @param string $dst destination directory
  1085. * @param string $name directory name
  1086. * @return array|false
  1087. * @author Dmitry (dio) Levashov
  1088. **/
  1089. public function mkdir($dst, $name) {
  1090. if ($this->commandDisabled('mkdir')) {
  1091. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1092. }
  1093. if (!$this->nameAccepted($name)) {
  1094. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1095. }
  1096. if (($dir = $this->dir($dst)) == false) {
  1097. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1098. }
  1099. if (!$dir['write']) {
  1100. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1101. }
  1102. $path = $this->decode($dst);
  1103. $dst = $this->_joinPath($path, $name);
  1104. $stat = $this->stat($dst);
  1105. if (!empty($stat)) {
  1106. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1107. }
  1108. $this->clearcache();
  1109. return ($path = $this->_mkdir($path, $name)) ? $this->stat($path) : false;
  1110. }
  1111. /**
  1112. * Create empty file and return its info
  1113. *
  1114. * @param string $dst destination directory
  1115. * @param string $name file name
  1116. * @return array|false
  1117. * @author Dmitry (dio) Levashov
  1118. **/
  1119. public function mkfile($dst, $name) {
  1120. if ($this->commandDisabled('mkfile')) {
  1121. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1122. }
  1123. if (!$this->nameAccepted($name)) {
  1124. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1125. }
  1126. if (($dir = $this->dir($dst)) == false) {
  1127. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1128. }
  1129. $path = $this->decode($dst);
  1130. if (!$dir['write'] || !$this->allowCreate($path, $name)) {
  1131. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1132. }
  1133. if ($this->stat($this->_joinPath($path, $name))) {
  1134. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1135. }
  1136. $this->clearcache();
  1137. return ($path = $this->_mkfile($path, $name)) ? $this->stat($path) : false;
  1138. }
  1139. /**
  1140. * Rename file and return file info
  1141. *
  1142. * @param string $hash file hash
  1143. * @param string $name new file name
  1144. * @return array|false
  1145. * @author Dmitry (dio) Levashov
  1146. **/
  1147. public function rename($hash, $name) {
  1148. if ($this->commandDisabled('rename')) {
  1149. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1150. }
  1151. if (!$this->nameAccepted($name)) {
  1152. return $this->setError(elFinder::ERROR_INVALID_NAME, $name);
  1153. }
  1154. if (!($file = $this->file($hash))) {
  1155. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1156. }
  1157. if ($name == $file['name']) {
  1158. return $file;
  1159. }
  1160. if (!empty($file['locked'])) {
  1161. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  1162. }
  1163. $path = $this->decode($hash);
  1164. $dir = $this->_dirname($path);
  1165. $stat = $this->stat($this->_joinPath($dir, $name));
  1166. if ($stat) {
  1167. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1168. }
  1169. if (!$this->allowCreate($dir, $name)) {
  1170. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1171. }
  1172. $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move
  1173. if (($path = $this->_move($path, $dir, $name))) {
  1174. $this->clearcache();
  1175. return $this->stat($path);
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * Create file copy with suffix "copy number" and return its info
  1181. *
  1182. * @param string $hash file hash
  1183. * @param string $suffix suffix to add to file name
  1184. * @return array|false
  1185. * @author Dmitry (dio) Levashov
  1186. **/
  1187. public function duplicate($hash, $suffix='copy') {
  1188. if ($this->commandDisabled('duplicate')) {
  1189. return $this->setError(elFinder::ERROR_COPY, '#'.$hash, elFinder::ERROR_PERM_DENIED);
  1190. }
  1191. if (($file = $this->file($hash)) == false) {
  1192. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  1193. }
  1194. $path = $this->decode($hash);
  1195. $dir = $this->_dirname($path);
  1196. $name = $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' ');
  1197. if (!$this->allowCreate($dir, $name)) {
  1198. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1199. }
  1200. return ($path = $this->copy($path, $dir, $name)) == false
  1201. ? false
  1202. : $this->stat($path);
  1203. }
  1204. /**
  1205. * Save uploaded file.
  1206. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  1207. *
  1208. * @param Resource $fp file pointer
  1209. * @param string $dst destination folder hash
  1210. * @param string $src file name
  1211. * @param string $tmpname file tmp name - required to detect mime type
  1212. * @return array|false
  1213. * @author Dmitry (dio) Levashov
  1214. **/
  1215. public function upload($fp, $dst, $name, $tmpname) {
  1216. if ($this->commandDisabled('upload')) {
  1217. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1218. }
  1219. if (($dir = $this->dir($dst)) == false) {
  1220. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1221. }
  1222. if (!$dir['write']) {
  1223. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1224. }
  1225. if (!$this->nameAccepted($name)) {
  1226. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1227. }
  1228. $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname, $name);
  1229. if ($mime == 'unknown' && $this->mimeDetect == 'internal') {
  1230. $mime = elFinderVolumeDriver::mimetypeInternalDetect($name);
  1231. }
  1232. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  1233. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  1234. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  1235. $upload = true; // default to allow
  1236. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  1237. $upload = false; // default is deny
  1238. if (!$deny && ($allow === true)) { // match only allow
  1239. $upload = true;
  1240. }// else (both match | no match | match only deny) { deny }
  1241. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  1242. $upload = true; // default is allow
  1243. if (($deny === true) && !$allow) { // match only deny
  1244. $upload = false;
  1245. } // else (both match | no match | match only allow) { allow }
  1246. }
  1247. if (!$upload) {
  1248. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  1249. }
  1250. if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) {
  1251. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  1252. }
  1253. $dstpath = $this->decode($dst);
  1254. $name = ElFinderIntegrationConnector::transliterate($name);
  1255. $test = $this->_joinPath($dstpath, $name);
  1256. $file = $this->stat($test);
  1257. $this->clearcache();
  1258. if ($file) { // file exists
  1259. if ($this->options['uploadOverwrite']) {
  1260. if (!$file['write']) {
  1261. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1262. } elseif ($file['mime'] == 'directory') {
  1263. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  1264. }
  1265. $this->remove($test);
  1266. } else {
  1267. $name = $this->uniqueName($dstpath, $name, '-', false);
  1268. }
  1269. }
  1270. $stat = array(
  1271. 'mime' => $mime,
  1272. 'width' => 0,
  1273. 'height' => 0,
  1274. 'size' => filesize($tmpname));
  1275. // $w = $h = 0;
  1276. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  1277. $stat['width'] = $s[0];
  1278. $stat['height'] = $s[1];
  1279. }
  1280. // $this->clearcache();
  1281. if (($path = $this->_save($fp, $dstpath, $name, $stat)) == false) {
  1282. return false;
  1283. }
  1284. return $this->stat($path);
  1285. }
  1286. /**
  1287. * Paste files
  1288. *
  1289. * @param Object $volume source volume
  1290. * @param string $source file hash
  1291. * @param string $dst destination dir hash
  1292. * @param bool $rmSrc remove source after copy?
  1293. * @return array|false
  1294. * @author Dmitry (dio) Levashov
  1295. **/
  1296. public function paste($volume, $src, $dst, $rmSrc = false) {
  1297. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  1298. if ($this->commandDisabled('paste')) {
  1299. return $this->setError($err, '#'.$src, elFinder::ERROR_PERM_DENIED);
  1300. }
  1301. if (($file = $volume->file($src, $rmSrc)) == false) {
  1302. return $this->setError($err, '#'.$src, elFinder::ERROR_FILE_NOT_FOUND);
  1303. }
  1304. $name = $file['name'];
  1305. $errpath = $volume->path($src);
  1306. if (($dir = $this->dir($dst)) == false) {
  1307. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1308. }
  1309. if (!$dir['write'] || !$file['read']) {
  1310. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1311. }
  1312. $destination = $this->decode($dst);
  1313. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  1314. return $rmSrc
  1315. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  1316. : $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1317. }
  1318. $test = $this->_joinPath($destination, $name);
  1319. $stat = $this->stat($test);
  1320. $this->clearcache();
  1321. if ($stat) {
  1322. if ($this->options['copyOverwrite']) {
  1323. // do not replace file with dir or dir with file
  1324. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  1325. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->_path($test));
  1326. }
  1327. // existed file is not writable
  1328. if (!$stat['write']) {
  1329. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1330. }
  1331. // existed file locked or has locked child
  1332. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  1333. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($locked));
  1334. }
  1335. // target is entity file of alias
  1336. if ($volume == $this && ($test == @$file['target'] || $test == $this->decode($src))) {
  1337. return $this->setError(elFinder::ERROR_REPLACE, $errpath);
  1338. }
  1339. // remove existed file
  1340. if (!$this->remove($test)) {
  1341. return $this->setError(elFinder::ERROR_REPLACE, $this->_path($test));
  1342. }
  1343. } else {
  1344. $name = $this->uniqueName($destination, $name, ' ', false);
  1345. }
  1346. }
  1347. // copy/move inside current volume
  1348. if ($volume == $this) {
  1349. $source = $this->decode($src);
  1350. // do not copy into itself
  1351. if ($this->_inpath($destination, $source)) {
  1352. return $this->setError(elFinder::ERROR_COPY_INTO_ITSELF, $errpath);
  1353. }
  1354. $method = $rmSrc ? 'move' : 'copy';
  1355. return ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false;
  1356. }
  1357. // copy/move from another volume
  1358. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  1359. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  1360. }
  1361. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  1362. return false;
  1363. }
  1364. if ($rmSrc) {
  1365. if ($volume->rm($src)) {
  1366. $this->removed[] = $file;
  1367. } else {
  1368. return $this->setError(elFinder::ERROR_MOVE, $errpath, elFinder::ERROR_RM_SRC);
  1369. }
  1370. }
  1371. return $this->stat($path);
  1372. }
  1373. /**
  1374. * Return file contents
  1375. *
  1376. * @param string $hash file hash
  1377. * @return string|false
  1378. * @author Dmitry (dio) Levashov
  1379. **/
  1380. public function getContents($hash) {
  1381. $file = $this->file($hash);
  1382. if (!$file) {
  1383. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1384. }
  1385. if ($file['mime'] == 'directory') {
  1386. return $this->setError(elFinder::ERROR_NOT_FILE);
  1387. }
  1388. if (!$file['read']) {
  1389. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1390. }
  1391. return $this->_getContents($this->decode($hash));
  1392. }
  1393. /**
  1394. * Put content in text file and return file info.
  1395. *
  1396. * @param string $hash file hash
  1397. * @param string $content new file content
  1398. * @return array
  1399. * @author Dmitry (dio) Levashov
  1400. **/
  1401. public function putContents($hash, $content) {
  1402. if ($this->commandDisabled('edit')) {
  1403. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1404. }
  1405. $path = $this->decode($hash);
  1406. if (!($file = $this->file($hash))) {
  1407. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1408. }
  1409. if (!$file['write']) {
  1410. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1411. }
  1412. $this->clearcache();
  1413. return $this->_filePutContents($path, $content) ? $this->stat($path) : false;
  1414. }
  1415. /**
  1416. * Extract files from archive
  1417. *
  1418. * @param string $hash archive hash
  1419. * @return array|bool
  1420. * @author Dmitry (dio) Levashov,
  1421. * @author Alexey Sukhotin
  1422. **/
  1423. public function extract($hash) {
  1424. if ($this->commandDisabled('extract')) {
  1425. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1426. }
  1427. if (($file = $this->file($hash)) == false) {
  1428. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1429. }
  1430. $archiver = isset($this->archivers['extract'][$file['mime']])
  1431. ? $this->archivers['extract'][$file['mime']]
  1432. : false;
  1433. if (!$archiver) {
  1434. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  1435. }
  1436. $path = $this->decode($hash);
  1437. $parent = $this->stat($this->_dirname($path));
  1438. if (!$file['read'] || !$parent['write']) {
  1439. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1440. }
  1441. $this->clearcache();
  1442. return ($path = $this->_extract($path, $archiver)) ? $this->stat($path) : false;
  1443. }
  1444. /**
  1445. * Add files to archive
  1446. *
  1447. * @return void
  1448. **/
  1449. public function archive($hashes, $mime) {
  1450. if ($this->commandDisabled('archive')) {
  1451. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1452. }
  1453. $archiver = isset($this->archivers['create'][$mime])
  1454. ? $this->archivers['create'][$mime]
  1455. : false;
  1456. if (!$archiver) {
  1457. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  1458. }
  1459. $files = array();
  1460. foreach ($hashes as $hash) {
  1461. if (($file = $this->file($hash)) == false) {
  1462. return $this->error(elFinder::ERROR_FILE_NOT_FOUND, '#'+$hash);
  1463. }
  1464. if (!$file['read']) {
  1465. return $this->error(elFinder::ERROR_PERM_DENIED);
  1466. }
  1467. $path = $this->decode($hash);
  1468. if (!isset($dir)) {
  1469. $dir = $this->_dirname($path);
  1470. $stat = $this->stat($dir);
  1471. if (!$stat['write']) {
  1472. return $this->error(elFinder::ERROR_PERM_DENIED);
  1473. }
  1474. }
  1475. $files[] = $this->_basename($path);
  1476. }
  1477. $name = (count($files) == 1 ? $files[0] : 'Archive').'.'.$archiver['ext'];
  1478. $name = $this->uniqueName($dir, $name, '');
  1479. $this->clearcache();
  1480. return ($path = $this->_archive($dir, $files, $name, $archiver)) ? $this->stat($path) : false;
  1481. }
  1482. /**
  1483. * Resize image
  1484. *
  1485. * @param string $hash image file
  1486. * @param int $width new width
  1487. * @param int $height new height
  1488. * @param int $x X start poistion for crop
  1489. * @param int $y Y start poistion for crop
  1490. * @param string $mode action how to mainpulate image
  1491. * @return array|false
  1492. * @author Dmitry (dio) Levashov
  1493. * @author Alexey Sukhotin
  1494. * @author nao-pon
  1495. * @author Troex Nevelin
  1496. **/
  1497. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
  1498. if ($this->commandDisabled('resize')) {
  1499. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1500. }
  1501. if (($file = $this->file($hash)) == false) {
  1502. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1503. }
  1504. if (!$file['write'] || !$file['read']) {
  1505. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1506. }
  1507. $path = $this->decode($hash);
  1508. if (!$this->canResize($path, $file)) {
  1509. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  1510. }
  1511. switch($mode) {
  1512. case 'propresize':
  1513. $result = $this->imgResize($path, $width, $height, true, true);
  1514. break;
  1515. case 'crop':
  1516. $result = $this->imgCrop($path, $width, $height, $x, $y);
  1517. break;
  1518. case 'fitsquare':
  1519. $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']));
  1520. break;
  1521. case 'rotate':
  1522. $result = $this->imgRotate($path, $degree, ($bg ? $bg : $this->options['tmbBgColor']));
  1523. break;
  1524. default:
  1525. $result = $this->imgResize($path, $width, $height, false, true);
  1526. break;
  1527. }
  1528. if ($result) {
  1529. $this->rmTmb($file);
  1530. $this->clearcache();
  1531. return $this->stat($path);
  1532. }
  1533. return false;
  1534. }
  1535. /**
  1536. * Remove file/dir
  1537. *
  1538. * @param string $hash file hash
  1539. * @return bool
  1540. * @author Dmitry (dio) Levashov
  1541. **/
  1542. public function rm($hash) {
  1543. return $this->commandDisabled('rm')
  1544. ? array(elFinder::ERROR_ACCESS_DENIED)
  1545. : $this->remove($this->decode($hash));
  1546. }
  1547. /**
  1548. * Search files
  1549. *
  1550. * @param string $q search string
  1551. * @param array $mimes
  1552. * @return array
  1553. * @author Dmitry (dio) Levashov
  1554. **/
  1555. public function search($q, $mimes) {
  1556. return $this->doSearch($this->root, $q, $mimes);
  1557. }
  1558. /**
  1559. * Return image dimensions
  1560. *
  1561. * @param string $hash file hash
  1562. * @return array
  1563. * @author Dmitry (dio) Levashov
  1564. **/
  1565. public function dimensions($hash) {
  1566. if (($file = $this->file($hash)) == false) {
  1567. return false;
  1568. }
  1569. return $this->_dimensions($this->decode($hash), $file['mime']);
  1570. }
  1571. /**
  1572. * Save error message
  1573. *
  1574. * @param array error
  1575. * @return false
  1576. * @author Dmitry(dio) Levashov
  1577. **/
  1578. protected function setError($error) {
  1579. $this->error = array();
  1580. foreach (func_get_args() as $err) {
  1581. if (is_array($err)) {
  1582. $this->error = array_merge($this->error, $err);
  1583. } else {
  1584. $this->error[] = $err;
  1585. }
  1586. }
  1587. // $this->error = is_array($error) ? $error : func_get_args();
  1588. return false;
  1589. }
  1590. /*********************************************************************/
  1591. /* FS API */
  1592. /*********************************************************************/
  1593. /***************** paths *******************/
  1594. /**
  1595. * Encode path into hash
  1596. *
  1597. * @param string file path
  1598. * @return string
  1599. * @author Dmitry (dio) Levashov
  1600. * @author Troex Nevelin
  1601. **/
  1602. protected function encode($path) {
  1603. if ($path !== '') {
  1604. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1605. $p = $this->_relpath($path);
  1606. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1607. if ($p === '') {
  1608. $p = DIRECTORY_SEPARATOR;
  1609. }
  1610. // TODO crypt path and return hash
  1611. $hash = $this->crypt($p);
  1612. // hash is used as id in HTML that means it must contain vaild chars
  1613. // make base64 html safe and append prefix in begining
  1614. $hash = strtr(base64_encode($hash), '…

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