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

/src/elFinder/elFinderVolumeDriver.class.php

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

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