PageRenderTime 71ms CodeModel.GetById 21ms 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
  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'];
  1607. }
  1608. /**
  1609. * Return temp path
  1610. *
  1611. * @return string
  1612. * @author Naoki Sawada
  1613. */
  1614. public function getTempPath() {
  1615. if (@ $this->tmpPath) {
  1616. return $this->tmpPath;
  1617. } else if (@ $this->tmp) {
  1618. return $this->tmp;
  1619. } else if (@ $this->tmbPath) {
  1620. return $this->tmbPath;
  1621. } else {
  1622. return null;
  1623. }
  1624. }
  1625. /**
  1626. * (Make &) Get upload taget dirctory hash
  1627. *
  1628. * @param string $baseTargetHash
  1629. * @param string $path
  1630. * @param array $result
  1631. * @return boolean|string
  1632. * @author Naoki Sawada
  1633. */
  1634. public function getUploadTaget($baseTargetHash, $path, & $result) {
  1635. $base = $this->decode($baseTargetHash);
  1636. $targetHash = $baseTargetHash;
  1637. $path = ltrim($path, '/');
  1638. $dirs = explode('/', $path);
  1639. array_pop($dirs);
  1640. foreach($dirs as $dir) {
  1641. $targetPath = $this->_joinPath($base, $dir);
  1642. if (! $_realpath = $this->realpath($this->encode($targetPath))) {
  1643. if ($stat = $this->mkdir($targetHash, $dir)) {
  1644. $result['added'][] = $stat;
  1645. $targetHash = $stat['hash'];
  1646. $base = $this->decode($targetHash);
  1647. } else {
  1648. return false;
  1649. }
  1650. } else {
  1651. $targetHash = $this->encode($_realpath);
  1652. if ($this->dir($targetHash)) {
  1653. $base = $this->decode($targetHash);
  1654. } else {
  1655. return false;
  1656. }
  1657. }
  1658. }
  1659. return $targetHash;
  1660. }
  1661. /**
  1662. * Save error message
  1663. *
  1664. * @param array error
  1665. * @return false
  1666. * @author Dmitry(dio) Levashov
  1667. **/
  1668. protected function setError($error) {
  1669. $this->error = array();
  1670. foreach (func_get_args() as $err) {
  1671. if (is_array($err)) {
  1672. $this->error = array_merge($this->error, $err);
  1673. } else {
  1674. $this->error[] = $err;
  1675. }
  1676. }
  1677. // $this->error = is_array($error) ? $error : func_get_args();
  1678. return false;
  1679. }
  1680. /*********************************************************************/
  1681. /* FS API */
  1682. /*********************************************************************/
  1683. /***************** paths *******************/
  1684. /**
  1685. * Encode path into hash
  1686. *
  1687. * @param string file path
  1688. * @return string
  1689. * @author Dmitry (dio) Levashov
  1690. * @author Troex Nevelin
  1691. **/
  1692. protected function encode($path) {
  1693. if ($path !== '') {
  1694. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1695. $p = $this->_relpath($path);
  1696. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1697. if ($p === '') {
  1698. $p = DIRECTORY_SEPARATOR;
  1699. }
  1700. // TODO crypt path and return hash
  1701. $hash = $this->crypt($p);
  1702. // hash is used as id in HTML that means it must contain vaild chars
  1703. // make base64 html safe and append prefix in begining
  1704. $hash = strtr(base64_encode($hash), '+/=', '-_.');
  1705. // remove dots '.' at the end, before it was '=' in base64
  1706. $hash = rtrim($hash, '.');
  1707. // append volume id to make hash unique
  1708. return $this->id.$hash;
  1709. }
  1710. }
  1711. /**
  1712. * Decode path from hash
  1713. *
  1714. * @param string file hash
  1715. * @return string
  1716. * @author Dmitry (dio) Levashov
  1717. * @author Troex Nevelin
  1718. **/
  1719. protected function decode($hash) {
  1720. if (strpos($hash, $this->id) === 0) {
  1721. // cut volume id after it was prepended in encode
  1722. $h = substr($hash, strlen($this->id));
  1723. // replace HTML safe base64 to normal
  1724. $h = base64_decode(strtr($h, '-_.', '+/='));
  1725. // TODO uncrypt hash and return path
  1726. $path = $this->uncrypt($h);
  1727. // append ROOT to path after it was cut in encode
  1728. return $this->_abspath($path);//$this->root.($path == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR.$path);
  1729. }
  1730. }
  1731. /**
  1732. * Return crypted path
  1733. * Not implemented
  1734. *
  1735. * @param string path
  1736. * @return mixed
  1737. * @author Dmitry (dio) Levashov
  1738. **/
  1739. protected function crypt($path) {
  1740. return $path;
  1741. }
  1742. /**
  1743. * Return uncrypted path
  1744. * Not implemented
  1745. *
  1746. * @param mixed hash
  1747. * @return mixed
  1748. * @author Dmitry (dio) Levashov
  1749. **/
  1750. protected function uncrypt($hash) {
  1751. return $hash;
  1752. }
  1753. /**
  1754. * Validate file name based on $this->options['acceptedName'] regexp
  1755. *
  1756. * @param string $name file name
  1757. * @return bool
  1758. * @author Dmitry (dio) Levashov
  1759. **/
  1760. protected function nameAccepted($name) {
  1761. if ($this->nameValidator) {
  1762. if (function_exists($this->nameValidator)) {
  1763. $f = $this->nameValidator;
  1764. return $f($name);
  1765. }
  1766. return preg_match($this->nameValidator, $name);
  1767. }
  1768. return true;
  1769. }
  1770. /**
  1771. * Return new unique name based on file name and suffix
  1772. *
  1773. * @param string $path file path
  1774. * @param string $suffix suffix append to name
  1775. * @return string
  1776. * @author Dmitry (dio) Levashov
  1777. **/
  1778. public function uniqueName($dir, $name, $suffix = ' copy', $checkNum=true) {
  1779. $ext = '';
  1780. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  1781. $ext = '.'.$m[1];
  1782. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  1783. }
  1784. if ($checkNum && preg_match('/('.$suffix.')(\d*)$/i', $name, $m)) {
  1785. $i = (int)$m[2];
  1786. $name = substr($name, 0, strlen($name)-strlen($m[2]));
  1787. } else {
  1788. $i = 1;
  1789. $name .= $suffix;
  1790. }
  1791. $max = $i+100000;
  1792. while ($i <= $max) {
  1793. $n = $name.($i > 0 ? $i : '').$ext;
  1794. if (!$this->stat($this->_joinPath($dir, $n))) {
  1795. $this->clearcache();
  1796. return $n;
  1797. }
  1798. $i++;
  1799. }
  1800. return $name.md5($dir).$ext;
  1801. }
  1802. /*********************** file stat *********************/
  1803. /**
  1804. * Check file attribute
  1805. *
  1806. * @param string $path file path
  1807. * @param string $name attribute name (read|write|locked|hidden)
  1808. * @param bool $val attribute value returned by file system
  1809. * @param bool $isDir path is directory (true: directory, false: file)
  1810. * @return bool
  1811. * @author Dmitry (dio) Levashov
  1812. **/
  1813. protected function attr($path, $name, $val=null, $isDir=null) {
  1814. if (!isset($this->defaults[$name])) {
  1815. return false;
  1816. }
  1817. $perm = null;
  1818. if ($this->access) {
  1819. $perm = call_user_func($this->access, $name, $path, $this->options['accessControlData'], $this, $isDir);
  1820. if ($perm !== null) {
  1821. return !!$perm;
  1822. }
  1823. }
  1824. if ($this->separator != '/') {
  1825. $path = str_replace($this->separator, '/', $this->_relpath($path));
  1826. } else {
  1827. $path = $this->_relpath($path);
  1828. }
  1829. $path = '/'.$path;
  1830. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1831. $attrs = $this->attributes[$i];
  1832. if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $path)) {
  1833. $perm = $attrs[$name];
  1834. }
  1835. }
  1836. return $perm === null ? (is_null($val)? $this->defaults[$name] : $val) : !!$perm;
  1837. }
  1838. /**
  1839. * Return true if file with given name can be created in given folder.
  1840. *
  1841. * @param string $dir parent dir path
  1842. * @param string $name new file name
  1843. * @return bool
  1844. * @author Dmitry (dio) Levashov
  1845. **/
  1846. protected function allowCreate($dir, $name, $isDir = null) {
  1847. $path = $this->_joinPath($dir, $name);
  1848. $perm = null;
  1849. if ($this->access) {
  1850. $perm = call_user_func($this->access, 'write', $path, $this->options['accessControlData'], $this, $isDir);
  1851. if ($perm !== null) {
  1852. return !!$perm;
  1853. }
  1854. }
  1855. $testPath = $this->separator.$this->_relpath($path);
  1856. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1857. $attrs = $this->attributes[$i];
  1858. if (isset($attrs['write']) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $testPath)) {
  1859. $perm = $attrs['write'];
  1860. }
  1861. }
  1862. return $perm === null ? true : $perm;
  1863. }
  1864. /**
  1865. * Return fileinfo
  1866. *
  1867. * @param string $path file cache
  1868. * @return array
  1869. * @author Dmitry (dio) Levashov
  1870. **/
  1871. protected function stat($path) {
  1872. if ($path === false) {
  1873. return false;
  1874. }
  1875. return isset($this->cache[$path])
  1876. ? $this->cache[$path]
  1877. : $this->updateCache($path, $this->_stat($path));
  1878. }
  1879. /**
  1880. * Put file stat in cache and return it
  1881. *
  1882. * @param string $path file path
  1883. * @param array $stat file stat
  1884. * @return array
  1885. * @author Dmitry (dio) Levashov
  1886. **/
  1887. protected function updateCache($path, $stat) {
  1888. if (empty($stat) || !is_array($stat)) {
  1889. return $this->cache[$path] = array();
  1890. }
  1891. $stat['hash'] = $this->encode($path);
  1892. $root = $path == $this->root;
  1893. if ($root) {
  1894. $stat['volumeid'] = $this->id;
  1895. if ($this->rootName) {
  1896. $stat['name'] = $this->rootName;
  1897. }
  1898. if (! empty($this->options['icon'])) {
  1899. $stat['icon'] = $this->options['icon'];
  1900. }
  1901. } else {
  1902. if (!isset($stat['name']) || !strlen($stat['name'])) {
  1903. $stat['name'] = $this->_basename($path);
  1904. }
  1905. if (empty($stat['phash'])) {
  1906. $stat['phash'] = $this->encode($this->_dirname($path));
  1907. }
  1908. }
  1909. // fix name if required
  1910. if ($this->options['utf8fix'] && $this->options['utf8patterns'] && $this->options['utf8replace']) {
  1911. $stat['name'] = json_decode(str_replace($this->options['utf8patterns'], $this->options['utf8replace'], json_encode($stat['name'])));
  1912. }
  1913. if (empty($stat['mime'])) {
  1914. $stat['mime'] = $this->mimetype($stat['name']);
  1915. }
  1916. // @todo move dateformat to client
  1917. // $stat['date'] = isset($stat['ts'])
  1918. // ? $this->formatDate($stat['ts'])
  1919. // : 'unknown';
  1920. if (!isset($stat['size'])) {
  1921. $stat['size'] = 'unknown';
  1922. }
  1923. $isDir = ($stat['mime'] === 'directory');
  1924. $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ? !!$stat['read'] : null, $isDir));
  1925. $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ? !!$stat['write'] : null, $isDir));
  1926. if ($root) {
  1927. $stat['locked'] = 1;
  1928. } elseif ($this->attr($path, 'locked', !empty($stat['locked']), $isDir)) {
  1929. $stat['locked'] = 1;
  1930. } else {
  1931. unset($stat['locked']);
  1932. }
  1933. if ($root) {
  1934. unset($stat['hidden']);
  1935. } elseif ($this->attr($path, 'hidden', !empty($stat['hidden']), $isDir)
  1936. || !$this->mimeAccepted($stat['mime'])) {
  1937. $stat['hidden'] = $root ? 0 : 1;
  1938. } else {
  1939. unset($stat['hidden']);
  1940. }
  1941. if ($stat['read'] && empty($stat['hidden'])) {
  1942. if ($isDir) {
  1943. // for dir - check for subdirs
  1944. if ($this->options['checkSubfolders']) {
  1945. if (isset($stat['dirs'])) {
  1946. if ($stat['dirs']) {
  1947. $stat['dirs'] = 1;
  1948. } else {
  1949. unset($stat['dirs']);
  1950. }
  1951. } elseif (!empty($stat['alias']) && !empty($stat['target'])) {
  1952. $stat['dirs'] = isset($this->cache[$stat['target']])
  1953. ? intval(isset($this->cache[$stat['target']]['dirs']))
  1954. : $this->_subdirs($stat['target']);
  1955. } elseif ($this->_subdirs($path)) {
  1956. $stat['dirs'] = 1;
  1957. }
  1958. } else {
  1959. $stat['dirs'] = 1;
  1960. }
  1961. } else {
  1962. // for files - check for thumbnails
  1963. $p = isset($stat['target']) ? $stat['target'] : $path;
  1964. if ($this->tmbURL && !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) {
  1965. $tmb = $this->gettmb($p, $stat);
  1966. $stat['tmb'] = $tmb ? $tmb : 1;
  1967. }
  1968. }
  1969. }
  1970. if (!empty($stat['alias']) && !empty($stat['target'])) {
  1971. $stat['thash'] = $this->encode($stat['target']);
  1972. unset($stat['target']);
  1973. }
  1974. if (isset($this->options['netkey']) && $path === $this->root) {
  1975. $stat['netkey'] = $this->options['netkey'];
  1976. }
  1977. return $this->cache[$path] = $stat;
  1978. }
  1979. /**
  1980. * Get stat for folder content and put in cache
  1981. *
  1982. * @param string $path
  1983. * @return void
  1984. * @author Dmitry (dio) Levashov
  1985. **/
  1986. protected function cacheDir($path) {
  1987. $this->dirsCache[$path] = array();
  1988. foreach ($this->_scandir($path) as $p) {
  1989. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  1990. $this->dirsCache[$path][] = $p;
  1991. }
  1992. }
  1993. }
  1994. /**
  1995. * Clean cache
  1996. *
  1997. * @return void
  1998. * @author Dmitry (dio) Levashov
  1999. **/
  2000. protected function clearcache() {
  2001. $this->cache = $this->dirsCache = array();
  2002. }
  2003. /**
  2004. * Return file mimetype
  2005. *
  2006. * @param string $path file path
  2007. * @return string
  2008. * @author Dmitry (dio) Levashov
  2009. **/
  2010. protected function mimetype($path, $name = '') {
  2011. $type = '';
  2012. if ($this->mimeDetect == 'finfo') {
  2013. if ($type = @finfo_file($this->finfo, $path)) {
  2014. if ($name === '') {
  2015. $name = $path;
  2016. }
  2017. $ext = (false === $pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1);
  2018. if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip)~', $type)) {
  2019. if (isset(elFinderVolumeDriver::$mimetypes[$ext])) $type = elFinderVolumeDriver::$mimetypes[$ext];
  2020. }
  2021. }
  2022. } elseif ($type == 'mime_content_type') {
  2023. $type = mime_content_type($path);
  2024. } else {
  2025. $type = elFinderVolumeDriver::mimetypeInternalDetect($path);
  2026. }
  2027. $type = explode(';', $type);
  2028. $type = trim($type[0]);
  2029. if (in_array($type, array('application/x-empty', 'inode/x-empty'))) {
  2030. // finfo return this mime for empty files
  2031. $type = 'text/plain';
  2032. } elseif ($type == 'application/x-zip') {
  2033. // http://elrte.org/redmine/issues/163
  2034. $type = 'application/zip';
  2035. }
  2036. return $type == 'unknown' && $this->mimeDetect != 'internal'
  2037. ? elFinderVolumeDriver::mimetypeInternalDetect($path)
  2038. : $type;
  2039. }
  2040. /**
  2041. * Detect file mimetype using "internal" method
  2042. *
  2043. * @param string $path file path
  2044. * @return string
  2045. * @author Dmitry (dio) Levashov
  2046. **/
  2047. static protected function mimetypeInternalDetect($path) {
  2048. $pinfo = pathinfo($path);
  2049. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  2050. return isset(elFinderVolumeDriver::$mimetypes[$ext]) ? elFinderVolumeDriver::$mimetypes[$ext] : 'unknown';
  2051. }
  2052. /**
  2053. * Return file/total directory size
  2054. *
  2055. * @param string $path file path
  2056. * @return int
  2057. * @author Dmitry (dio) Levashov
  2058. **/
  2059. protected function countSize($path) {
  2060. $stat = $this->stat($path);
  2061. if (empty($stat) || !$stat['read'] || !empty($stat['hidden'])) {
  2062. return 'unknown';
  2063. }
  2064. if ($stat['mime'] != 'directory') {
  2065. return $stat['size'];
  2066. }
  2067. $subdirs = $this->options['checkSubfolders'];
  2068. $this->options['checkSubfolders'] = true;
  2069. $result = 0;
  2070. foreach ($this->getScandir($path) as $stat) {
  2071. $size = $stat['mime'] == 'directory' && $stat['read']
  2072. ? $this->countSize($this->_joinPath($path, $stat['name']))
  2073. : (isset($stat['size']) ? intval($stat['size']) : 0);
  2074. if ($size > 0) {
  2075. $result += $size;
  2076. }
  2077. }
  2078. $this->options['checkSubfolders'] = $subdirs;
  2079. return $result;
  2080. }
  2081. /**
  2082. * Return true if all mimes is directory or files
  2083. *
  2084. * @param string $mime1 mimetype
  2085. * @param string $mime2 mimetype
  2086. * @return bool
  2087. * @author Dmitry (dio) Levashov
  2088. **/
  2089. protected function isSameType($mime1, $mime2) {
  2090. return ($mime1 == 'directory' && $mime1 == $mime2) || ($mime1 != 'directory' && $mime2 != 'directory');
  2091. }
  2092. /**
  2093. * If file has required attr == $val - return file path,
  2094. * If dir has child with has required attr == $val - return child path
  2095. *
  2096. * @param string $path file path
  2097. * @param string $attr attribute name
  2098. * @param bool $val attribute value
  2099. * @return string|false
  2100. * @author Dmitry (dio) Levashov
  2101. **/
  2102. protected function closestByAttr($path, $attr, $val) {
  2103. $stat = $this->stat($path);
  2104. if (empty($stat)) {
  2105. return false;
  2106. }
  2107. $v = isset($stat[$attr]) ? $stat[$attr] : false;
  2108. if ($v == $val) {
  2109. return $path;
  2110. }
  2111. return $stat['mime'] == 'directory'
  2112. ? $this->childsByAttr($path, $attr, $val)
  2113. : false;
  2114. }
  2115. /**
  2116. * Return first found children with required attr == $val
  2117. *
  2118. * @param string $path file path
  2119. * @param string $attr attribute name
  2120. * @param bool $val attribute value
  2121. * @return string|false
  2122. * @author Dmitry (dio) Levashov
  2123. **/
  2124. protected function childsByAttr($path, $attr, $val) {
  2125. foreach ($this->_scandir($path) as $p) {
  2126. if (($_p = $this->closestByAttr($p, $attr, $val)) != false) {
  2127. return $_p;
  2128. }
  2129. }
  2130. return false;
  2131. }
  2132. /***************** get content *******************/
  2133. /**
  2134. * Return required dir's files info.
  2135. * If onlyMimes is set - return only dirs and files of required mimes
  2136. *
  2137. * @param string $path dir path
  2138. * @return array
  2139. * @author Dmitry (dio) Levashov
  2140. **/
  2141. protected function getScandir($path) {
  2142. $files = array();
  2143. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2144. foreach ($this->dirsCache[$path] as $p) {
  2145. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  2146. $files[] = $stat;
  2147. }
  2148. }
  2149. return $files;
  2150. }
  2151. /**
  2152. * Return subdirs tree
  2153. *
  2154. * @param string $path parent dir path
  2155. * @param int $deep tree deep
  2156. * @return array
  2157. * @author Dmitry (dio) Levashov
  2158. **/
  2159. protected function gettree($path, $deep, $exclude='') {
  2160. $dirs = array();
  2161. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2162. foreach ($this->dirsCache[$path] as $p) {
  2163. $stat = $this->stat($p);
  2164. if ($stat && empty($stat['hidden']) && $p != $exclude && $stat['mime'] == 'directory') {
  2165. $dirs[] = $stat;
  2166. if ($deep > 0 && !empty($stat['dirs'])) {
  2167. $dirs = array_merge($dirs, $this->gettree($p, $deep-1));
  2168. }
  2169. }
  2170. }
  2171. return $dirs;
  2172. }
  2173. /**
  2174. * Recursive files search
  2175. *
  2176. * @param string $path dir path
  2177. * @param string $q search string
  2178. * @param array $mimes
  2179. * @return array
  2180. * @author Dmitry (dio) Levashov
  2181. **/
  2182. protected function doSearch($path, $q, $mimes) {
  2183. $result = array();
  2184. foreach($this->_scandir($path) as $p) {
  2185. $stat = $this->stat($p);
  2186. if (!$stat) { // invalid links
  2187. continue;
  2188. }
  2189. if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'])) {
  2190. continue;
  2191. }
  2192. $name = $stat['name'];
  2193. if ($this->stripos($name, $q) !== false) {
  2194. $stat['path'] = $this->_path($p);
  2195. if ($this->URL && !isset($stat['url'])) {
  2196. $stat['url'] = $this->URL . str_replace($this->separator, '/', substr($p, strlen($this->root) + 1));
  2197. }
  2198. $result[] = $stat;
  2199. }
  2200. if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) {
  2201. $result = array_merge($result, $this->doSearch($p, $q, $mimes));
  2202. }
  2203. }
  2204. return $result;
  2205. }
  2206. /********************** manuipulations ******************/
  2207. /**
  2208. * Copy file/recursive copy dir only in current volume.
  2209. * Return new file path or false.
  2210. *
  2211. * @param string $src source path
  2212. * @param string $dst destination dir path
  2213. * @param string $name new file name (optionaly)
  2214. * @return string|false
  2215. * @author Dmitry (dio) Levashov
  2216. **/
  2217. protected function copy($src, $dst, $name) {
  2218. $srcStat = $this->stat($src);
  2219. $this->clearcache();
  2220. if (!empty($srcStat['thash'])) {
  2221. $target = $this->decode($srcStat['thash']);
  2222. $stat = $this->stat($target);
  2223. $this->clearcache();
  2224. return $stat && $this->_symlink($target, $dst, $name)
  2225. ? $this->_joinPath($dst, $name)
  2226. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2227. }
  2228. if ($srcStat['mime'] == 'directory') {
  2229. $test = $this->stat($this->_joinPath($dst, $name));
  2230. if (($test && $test['mime'] != 'directory') || !$this->_mkdir($dst, $name)) {
  2231. return $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2232. }
  2233. $dst = $this->_joinPath($dst, $name);
  2234. foreach ($this->getScandir($src) as $stat) {
  2235. if (empty($stat['hidden'])) {
  2236. $name = $stat['name'];
  2237. if (!$this->copy($this->_joinPath($src, $name), $dst, $name)) {
  2238. $this->remove($dst, true); // fall back
  2239. return false;
  2240. }
  2241. }
  2242. }
  2243. $this->clearcache();
  2244. return $dst;
  2245. }
  2246. return $this->_copy($src, $dst, $name)
  2247. ? $this->_joinPath($dst, $name)
  2248. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2249. }
  2250. /**
  2251. * Move file
  2252. * Return new file path or false.
  2253. *
  2254. * @param string $src source path
  2255. * @param string $dst destination dir path
  2256. * @param string $name new file name
  2257. * @return string|false
  2258. * @author Dmitry (dio) Levashov
  2259. **/
  2260. protected function move($src, $dst, $name) {
  2261. $stat = $this->stat($src);
  2262. $stat['realpath'] = $src;
  2263. $this->rmTmb($stat); // can not do rmTmb() after _move()
  2264. $this->clearcache();
  2265. if ($this->_move($src, $dst, $name)) {
  2266. $this->removed[] = $stat;
  2267. return $this->_joinPath($dst, $name);
  2268. }
  2269. return $this->setError(elFinder::ERROR_MOVE, $this->_path($src));
  2270. }
  2271. /**
  2272. * Copy file from another volume.
  2273. * Return new file path or false.
  2274. *
  2275. * @param Object $volume source volume
  2276. * @param string $src source file hash
  2277. * @param string $destination destination dir path
  2278. * @param string $name file name
  2279. * @return string|false
  2280. * @author Dmitry (dio) Levashov
  2281. **/
  2282. protected function copyFrom($volume, $src, $destination, $name) {
  2283. if (($source = $volume->file($src)) == false) {
  2284. return $this->setError(elFinder::ERROR_COPY, '#'.$src, $volume->error());
  2285. }
  2286. $errpath = $volume->path($src);
  2287. if (!$this->nameAccepted($source['name'])) {
  2288. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
  2289. }
  2290. if (!$source['read']) {
  2291. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  2292. }
  2293. if ($source['mime'] == 'directory') {
  2294. $stat = $this->stat($this->_joinPath($destination, $name));
  2295. $this->clearcache();
  2296. if ((!$stat || $stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
  2297. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2298. }
  2299. $path = $this->_joinPath($destination, $name);
  2300. foreach ($volume->scandir($src) as $entr) {
  2301. if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
  2302. return false;
  2303. }
  2304. }
  2305. } else {
  2306. // $mime = $source['mime'];
  2307. // $w = $h = 0;
  2308. if (($dim = $volume->dimensions($src))) {
  2309. $s = explode('x', $dim);
  2310. $source['width'] = $s[0];
  2311. $source['height'] = $s[1];
  2312. }
  2313. if (($fp = $volume->open($src)) == false
  2314. || ($path = $this->_save($fp, $destination, $name, $source)) == false) {
  2315. $fp && $volume->close($fp, $src);
  2316. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2317. }
  2318. $volume->close($fp, $src);
  2319. }
  2320. return $path;
  2321. }
  2322. /**
  2323. * Remove file/ recursive remove dir
  2324. *
  2325. * @param string $path file path
  2326. * @param bool $force try to remove even if file locked
  2327. * @return bool
  2328. * @author Dmitry (dio) Levashov
  2329. **/
  2330. protected function remove($path, $force = false) {
  2331. $stat = $this->stat($path);
  2332. if (empty($stat)) {
  2333. return $this->setError(elFinder::ERROR_RM, $this->_path($path), elFinder::ERROR_FILE_NOT_FOUND);
  2334. }
  2335. $stat['realpath'] = $path;
  2336. $this->rmTmb($stat);
  2337. $this->clearcache();
  2338. if (!$force && !empty($stat['locked'])) {
  2339. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($path));
  2340. }
  2341. if ($stat['mime'] == 'directory') {
  2342. foreach ($this->_scandir($path) as $p) {
  2343. $name = $this->_basename($p);
  2344. if ($name != '.' && $name != '..' && !$this->remove($p)) {
  2345. return false;
  2346. }
  2347. }
  2348. if (!$this->_rmdir($path)) {
  2349. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2350. }
  2351. } else {
  2352. if (!$this->_unlink($path)) {
  2353. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2354. }
  2355. }
  2356. $this->removed[] = $stat;
  2357. return true;
  2358. }
  2359. /************************* thumbnails **************************/
  2360. /**
  2361. * Return thumbnail file name for required file
  2362. *
  2363. * @param array $stat file stat
  2364. * @return string
  2365. * @author Dmitry (dio) Levashov
  2366. **/
  2367. protected function tmbname($stat) {
  2368. return $stat['hash'].$stat['ts'].'.png';
  2369. }
  2370. /**
  2371. * Return thumnbnail name if exists
  2372. *
  2373. * @param string $path file path
  2374. * @param array $stat file stat
  2375. * @return string|false
  2376. * @author Dmitry (dio) Levashov
  2377. **/
  2378. protected function gettmb($path, $stat) {
  2379. if ($this->tmbURL && $this->tmbPath) {
  2380. // file itself thumnbnail
  2381. if (strpos($path, $this->tmbPath) === 0) {
  2382. return basename($path);
  2383. }
  2384. $name = $this->tmbname($stat);
  2385. if (file_exists($this->tmbPath.DIRECTORY_SEPARATOR.$name)) {
  2386. return $name;
  2387. }
  2388. }
  2389. return false;
  2390. }
  2391. /**
  2392. * Return true if thumnbnail for required file can be created
  2393. *
  2394. * @param string $path thumnbnail path
  2395. * @param array $stat file stat
  2396. * @return string|bool
  2397. * @author Dmitry (dio) Levashov
  2398. **/
  2399. protected function canCreateTmb($path, $stat) {
  2400. return $this->tmbPathWritable
  2401. && strpos($path, $this->tmbPath) === false // do not create thumnbnail for thumnbnail
  2402. && $this->imgLib
  2403. && strpos($stat['mime'], 'image') === 0
  2404. && ($this->imgLib == 'gd' ? $stat['mime'] == 'image/jpeg' || $stat['mime'] == 'image/png' || $stat['mime'] == 'image/gif' : true);
  2405. }
  2406. /**
  2407. * Return true if required file can be resized.
  2408. * By default - the same as canCreateTmb
  2409. *
  2410. * @param string $path thumnbnail path
  2411. * @param array $stat file stat
  2412. * @return string|bool
  2413. * @author Dmitry (dio) Levashov
  2414. **/
  2415. protected function canResize($path, $stat) {
  2416. return $this->canCreateTmb($path, $stat);
  2417. }
  2418. /**
  2419. * Create thumnbnail and return it's URL on success
  2420. *
  2421. * @param string $path file path
  2422. * @param string $mime file mime type
  2423. * @return string|false
  2424. * @author Dmitry (dio) Levashov
  2425. **/
  2426. protected function createTmb($path, $stat) {
  2427. if (!$stat || !$this->canCreateTmb($path, $stat)) {
  2428. return false;
  2429. }
  2430. $name = $this->tmbname($stat);
  2431. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$name;
  2432. // copy image into tmbPath so some drivers does not store files on local fs
  2433. if (($src = $this->_fopen($path, 'rb')) == false) {
  2434. return false;
  2435. }
  2436. if (($trg = fopen($tmb, 'wb')) == false) {
  2437. $this->_fclose($src, $path);
  2438. return false;
  2439. }
  2440. while (!feof($src)) {
  2441. fwrite($trg, fread($src, 8192));
  2442. }
  2443. $this->_fclose($src, $path);
  2444. fclose($trg);
  2445. $result = false;
  2446. $tmbSize = $this->tmbSize;
  2447. if (($s = getimagesize($tmb)) == false) {
  2448. return false;
  2449. }
  2450. /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  2451. if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
  2452. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2453. } else {
  2454. if ($this->options['tmbCrop']) {
  2455. /* Resize and crop if image bigger than thumbnail */
  2456. if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize) ) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
  2457. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
  2458. }
  2459. if (($s = getimagesize($tmb)) != false) {
  2460. $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize)/2) : 0;
  2461. $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize)/2) : 0;
  2462. $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
  2463. }
  2464. } else {
  2465. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
  2466. }
  2467. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2468. }
  2469. if (!$result) {
  2470. unlink($tmb);
  2471. return false;
  2472. }
  2473. return $name;
  2474. }
  2475. /**
  2476. * Resize image
  2477. *
  2478. * @param string $path image file
  2479. * @param int $width new width
  2480. * @param int $height new height
  2481. * @param bool $keepProportions crop image
  2482. * @param bool $resizeByBiggerSide resize image based on bigger side if true
  2483. * @param string $destformat image destination format
  2484. * @return string|false
  2485. * @author Dmitry (dio) Levashov
  2486. * @author Alexey Sukhotin
  2487. **/
  2488. protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
  2489. if (($s = @getimagesize($path)) == false) {
  2490. return false;
  2491. }
  2492. $result = false;
  2493. list($size_w, $size_h) = array($width, $height);
  2494. if ($keepProportions == true) {
  2495. list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
  2496. /* Calculating image scale width and height */
  2497. $xscale = $orig_w / $new_w;
  2498. $yscale = $orig_h / $new_h;
  2499. /* Resizing by biggest side */
  2500. if ($resizeByBiggerSide) {
  2501. if ($orig_w > $orig_h) {
  2502. $size_h = $orig_h * $width / $orig_w;
  2503. $size_w = $width;
  2504. } else {
  2505. $size_w = $orig_w * $height / $orig_h;
  2506. $size_h = $height;
  2507. }
  2508. } else {
  2509. if ($orig_w > $orig_h) {
  2510. $size_w = $orig_w * $height / $orig_h;
  2511. $size_h = $height;
  2512. } else {
  2513. $size_h = $orig_h * $width / $orig_w;
  2514. $size_w = $width;
  2515. }
  2516. }
  2517. }
  2518. switch ($this->imgLib) {
  2519. case 'imagick':
  2520. try {
  2521. $img = new imagick($path);
  2522. } catch (Exception $e) {
  2523. return false;
  2524. }
  2525. // Imagick::FILTER_BOX faster than FILTER_LANCZOS so use for createTmb
  2526. // resize bench: http://app-mgng.rhcloud.com/9
  2527. // resize sample: http://www.dylanbeattie.net/magick/filters/result.html
  2528. $filter = ($destformat === 'png' /* createTmb */)? Imagick::FILTER_BOX : Imagick::FILTER_LANCZOS;
  2529. $img->resizeImage($size_w, $size_h, $filter, 1);
  2530. $result = $img->writeImage($path);
  2531. $img->destroy();
  2532. return $result ? $path : false;
  2533. break;
  2534. case 'gd':
  2535. $img = self::gdImageCreate($path,$s['mime']);
  2536. if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
  2537. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2538. if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
  2539. return false;
  2540. }
  2541. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2542. imagedestroy($img);
  2543. imagedestroy($tmp);
  2544. return $result ? $path : false;
  2545. }
  2546. break;
  2547. }
  2548. return false;
  2549. }
  2550. /**
  2551. * Crop image
  2552. *
  2553. * @param string $path image file
  2554. * @param int $width crop width
  2555. * @param int $height crop height
  2556. * @param bool $x crop left offset
  2557. * @param bool $y crop top offset
  2558. * @param string $destformat image destination format
  2559. * @return string|false
  2560. * @author Dmitry (dio) Levashov
  2561. * @author Alexey Sukhotin
  2562. **/
  2563. protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
  2564. if (($s = @getimagesize($path)) == false) {
  2565. return false;
  2566. }
  2567. $result = false;
  2568. switch ($this->imgLib) {
  2569. case 'imagick':
  2570. try {
  2571. $img = new imagick($path);
  2572. } catch (Exception $e) {
  2573. return false;
  2574. }
  2575. $img->cropImage($width, $height, $x, $y);
  2576. $result = $img->writeImage($path);
  2577. $img->destroy();
  2578. return $result ? $path : false;
  2579. break;
  2580. case 'gd':
  2581. $img = self::gdImageCreate($path,$s['mime']);
  2582. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2583. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2584. $size_w = $width;
  2585. $size_h = $height;
  2586. if ($s[0] < $width || $s[1] < $height) {
  2587. $size_w = $s[0];
  2588. $size_h = $s[1];
  2589. }
  2590. if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
  2591. return false;
  2592. }
  2593. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2594. imagedestroy($img);
  2595. imagedestroy($tmp);
  2596. return $result ? $path : false;
  2597. }
  2598. break;
  2599. }
  2600. return false;
  2601. }
  2602. /**
  2603. * Put image to square
  2604. *
  2605. * @param string $path image file
  2606. * @param int $width square width
  2607. * @param int $height square height
  2608. * @param int $align reserved
  2609. * @param int $valign reserved
  2610. * @param string $bgcolor square background color in #rrggbb format
  2611. * @param string $destformat image destination format
  2612. * @return string|false
  2613. * @author Dmitry (dio) Levashov
  2614. * @author Alexey Sukhotin
  2615. **/
  2616. protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null) {
  2617. if (($s = @getimagesize($path)) == false) {
  2618. return false;
  2619. }
  2620. $result = false;
  2621. /* Coordinates for image over square aligning */
  2622. $y = ceil(abs($height - $s[1]) / 2);
  2623. $x = ceil(abs($width - $s[0]) / 2);
  2624. switch ($this->imgLib) {
  2625. case 'imagick':
  2626. try {
  2627. $img = new imagick($path);
  2628. } catch (Exception $e) {
  2629. return false;
  2630. }
  2631. $img1 = new Imagick();
  2632. $img1->newImage($width, $height, new ImagickPixel($bgcolor));
  2633. $img1->setImageColorspace($img->getImageColorspace());
  2634. $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
  2635. $img1->compositeImage( $img, imagick::COMPOSITE_OVER, $x, $y );
  2636. $result = $img1->writeImage($path);
  2637. $img->destroy();
  2638. return $result ? $path : false;
  2639. break;
  2640. case 'gd':
  2641. $img = self::gdImageCreate($path,$s['mime']);
  2642. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2643. self::gdImageBackground($tmp,$bgcolor);
  2644. if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
  2645. return false;
  2646. }
  2647. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2648. imagedestroy($img);
  2649. imagedestroy($tmp);
  2650. return $result ? $path : false;
  2651. }
  2652. break;
  2653. }
  2654. return false;
  2655. }
  2656. /**
  2657. * Rotate image
  2658. *
  2659. * @param string $path image file
  2660. * @param int $degree rotete degrees
  2661. * @param string $bgcolor square background color in #rrggbb format
  2662. * @param string $destformat image destination format
  2663. * @return string|false
  2664. * @author nao-pon
  2665. * @author Troex Nevelin
  2666. **/
  2667. protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) {
  2668. if (($s = @getimagesize($path)) == false) {
  2669. return false;
  2670. }
  2671. $result = false;
  2672. switch ($this->imgLib) {
  2673. case 'imagick':
  2674. try {
  2675. $img = new imagick($path);
  2676. } catch (Exception $e) {
  2677. return false;
  2678. }
  2679. $img->rotateImage(new ImagickPixel($bgcolor), $degree);
  2680. $result = $img->writeImage($path);
  2681. $img->destroy();
  2682. return $result ? $path : false;
  2683. break;
  2684. case 'gd':
  2685. $img = self::gdImageCreate($path,$s['mime']);
  2686. $degree = 360 - $degree;
  2687. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2688. $bgcolor = imagecolorallocate($img, $r, $g, $b);
  2689. $tmp = imageRotate($img, $degree, (int)$bgcolor);
  2690. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2691. imageDestroy($img);
  2692. imageDestroy($tmp);
  2693. return $result ? $path : false;
  2694. break;
  2695. }
  2696. return false;
  2697. }
  2698. /**
  2699. * Execute shell command
  2700. *
  2701. * @param string $command command line
  2702. * @param array $output stdout strings
  2703. * @param array $return_var process exit code
  2704. * @param array $error_output stderr strings
  2705. * @return int exit code
  2706. * @author Alexey Sukhotin
  2707. **/
  2708. protected function procExec($command , array &$output = null, &$return_var = -1, array &$error_output = null) {
  2709. $descriptorspec = array(
  2710. 0 => array("pipe", "r"), // stdin
  2711. 1 => array("pipe", "w"), // stdout
  2712. 2 => array("pipe", "w") // stderr
  2713. );
  2714. $process = proc_open($command, $descriptorspec, $pipes, null, null);
  2715. if (is_resource($process)) {
  2716. fclose($pipes[0]);
  2717. $tmpout = '';
  2718. $tmperr = '';
  2719. $output = stream_get_contents($pipes[1]);
  2720. $error_output = stream_get_contents($pipes[2]);
  2721. fclose($pipes[1]);
  2722. fclose($pipes[2]);
  2723. $return_var = proc_close($process);
  2724. }
  2725. return $return_var;
  2726. }
  2727. /**
  2728. * Remove thumbnail, also remove recursively if stat is directory
  2729. *
  2730. * @param string $stat file stat
  2731. * @return void
  2732. * @author Dmitry (dio) Levashov
  2733. * @author Naoki Sawada
  2734. * @author Troex Nevelin
  2735. **/
  2736. protected function rmTmb($stat) {
  2737. if ($stat['mime'] === 'directory') {
  2738. foreach ($this->_scandir($this->decode($stat['hash'])) as $p) {
  2739. $name = $this->_basename($p);
  2740. $name != '.' && $name != '..' && $this->rmTmb($this->stat($p));
  2741. }
  2742. } else if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
  2743. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$stat['tmb'];
  2744. file_exists($tmb) && @unlink($tmb);
  2745. clearstatcache();
  2746. }
  2747. }
  2748. /**
  2749. * Create an gd image according to the specified mime type
  2750. *
  2751. * @param string $path image file
  2752. * @param string $mime
  2753. * @return gd image resource identifier
  2754. */
  2755. protected function gdImageCreate($path,$mime){
  2756. switch($mime){
  2757. case 'image/jpeg':
  2758. return imagecreatefromjpeg($path);
  2759. case 'image/png':
  2760. return imagecreatefrompng($path);
  2761. case 'image/gif':
  2762. return imagecreatefromgif($path);
  2763. case 'image/xbm':
  2764. return imagecreatefromxbm($path);
  2765. }
  2766. return false;
  2767. }
  2768. /**
  2769. * Output gd image to file
  2770. *
  2771. * @param resource $image gd image resource
  2772. * @param string $filename The path to save the file to.
  2773. * @param string $destformat The Image type to use for $filename
  2774. * @param string $mime The original image mime type
  2775. */
  2776. protected function gdImage($image, $filename, $destformat, $mime ){
  2777. if ($destformat == 'jpg' || ($destformat == null && $mime == 'image/jpeg')) {
  2778. return imagejpeg($image, $filename, 100);
  2779. }
  2780. if ($destformat == 'gif' || ($destformat == null && $mime == 'image/gif')) {
  2781. return imagegif($image, $filename, 7);
  2782. }
  2783. return imagepng($image, $filename, 7);
  2784. }
  2785. /**
  2786. * Assign the proper background to a gd image
  2787. *
  2788. * @param resource $image gd image resource
  2789. * @param string $bgcolor background color in #rrggbb format
  2790. */
  2791. protected function gdImageBackground($image, $bgcolor){
  2792. if( $bgcolor == 'transparent' ){
  2793. imagesavealpha($image,true);
  2794. $bgcolor1 = imagecolorallocatealpha($image, 255, 255, 255, 127);
  2795. }else{
  2796. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2797. $bgcolor1 = imagecolorallocate($image, $r, $g, $b);
  2798. }
  2799. imagefill($image, 0, 0, $bgcolor1);
  2800. }
  2801. /*********************** misc *************************/
  2802. /**
  2803. * Return smart formatted date
  2804. *
  2805. * @param int $ts file timestamp
  2806. * @return string
  2807. * @author Dmitry (dio) Levashov
  2808. **/
  2809. // protected function formatDate($ts) {
  2810. // if ($ts > $this->today) {
  2811. // return 'Today '.date($this->options['timeFormat'], $ts);
  2812. // }
  2813. //
  2814. // if ($ts > $this->yesterday) {
  2815. // return 'Yesterday '.date($this->options['timeFormat'], $ts);
  2816. // }
  2817. //
  2818. // return date($this->options['dateFormat'], $ts);
  2819. // }
  2820. /**
  2821. * Find position of first occurrence of string in a string with multibyte support
  2822. *
  2823. * @param string $haystack The string being checked.
  2824. * @param string $needle The string to find in haystack.
  2825. * @param int $offset The search offset. If it is not specified, 0 is used.
  2826. * @return int|bool
  2827. * @author Alexey Sukhotin
  2828. **/
  2829. protected function stripos($haystack , $needle , $offset = 0) {
  2830. if (function_exists('mb_stripos')) {
  2831. return mb_stripos($haystack , $needle , $offset);
  2832. } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) {
  2833. return mb_strpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);
  2834. }
  2835. return stripos($haystack , $needle , $offset);
  2836. }
  2837. /**
  2838. * Get server side available archivers
  2839. *
  2840. * @param bool $use_cache
  2841. * @return array
  2842. */
  2843. protected function getArchivers($use_cache = true) {
  2844. if (!function_exists('proc_open')) {
  2845. return array();
  2846. }
  2847. if ($use_cache && isset($_SESSION['ELFINDER_ARCHIVERS_CACHE']) && is_array($_SESSION['ELFINDER_ARCHIVERS_CACHE'])) {
  2848. return $_SESSION['ELFINDER_ARCHIVERS_CACHE'];
  2849. }
  2850. $arcs = array(
  2851. 'create' => array(),
  2852. 'extract' => array()
  2853. );
  2854. $this->procExec('tar --version', $o, $ctar);
  2855. if ($ctar == 0) {
  2856. $arcs['create']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-cf', 'ext' => 'tar');
  2857. $arcs['extract']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-xf', 'ext' => 'tar');
  2858. unset($o);
  2859. $test = $this->procExec('gzip --version', $o, $c);
  2860. if ($c == 0) {
  2861. $arcs['create']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-czf', 'ext' => 'tgz');
  2862. $arcs['extract']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-xzf', 'ext' => 'tgz');
  2863. }
  2864. unset($o);
  2865. $test = $this->procExec('bzip2 --version', $o, $c);
  2866. if ($c == 0) {
  2867. $arcs['create']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-cjf', 'ext' => 'tbz');
  2868. $arcs['extract']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-xjf', 'ext' => 'tbz');
  2869. }
  2870. }
  2871. unset($o);
  2872. $this->procExec('zip -v', $o, $c);
  2873. if ($c == 0) {
  2874. $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
  2875. }
  2876. unset($o);
  2877. $this->procExec('unzip --help', $o, $c);
  2878. if ($c == 0) {
  2879. $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
  2880. }
  2881. unset($o);
  2882. $this->procExec('rar --version', $o, $c);
  2883. if ($c == 0 || $c == 7) {
  2884. $arcs['create']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'a -inul', 'ext' => 'rar');
  2885. $arcs['extract']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'x -y', 'ext' => 'rar');
  2886. } else {
  2887. unset($o);
  2888. $test = $this->procExec('unrar', $o, $c);
  2889. if ($c==0 || $c == 7) {
  2890. $arcs['extract']['application/x-rar'] = array('cmd' => 'unrar', 'argc' => 'x -y', 'ext' => 'rar');
  2891. }
  2892. }
  2893. unset($o);
  2894. $this->procExec('7za --help', $o, $c);
  2895. if ($c == 0) {
  2896. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'a', 'ext' => '7z');
  2897. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'e -y', 'ext' => '7z');
  2898. if (empty($arcs['create']['application/x-gzip'])) {
  2899. $arcs['create']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'a -tgzip', 'ext' => 'tar.gz');
  2900. }
  2901. if (empty($arcs['extract']['application/x-gzip'])) {
  2902. $arcs['extract']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'e -tgzip -y', 'ext' => 'tar.gz');
  2903. }
  2904. if (empty($arcs['create']['application/x-bzip2'])) {
  2905. $arcs['create']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2', 'ext' => 'tar.bz');
  2906. }
  2907. if (empty($arcs['extract']['application/x-bzip2'])) {
  2908. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2 -y', 'ext' => 'tar.bz');
  2909. }
  2910. if (empty($arcs['create']['application/zip'])) {
  2911. $arcs['create']['application/zip'] = array('cmd' => '7za', 'argc' => 'a -tzip -l', 'ext' => 'zip');
  2912. }
  2913. if (empty($arcs['extract']['application/zip'])) {
  2914. $arcs['extract']['application/zip'] = array('cmd' => '7za', 'argc' => 'e -tzip -y', 'ext' => 'zip');
  2915. }
  2916. if (empty($arcs['create']['application/x-tar'])) {
  2917. $arcs['create']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'a -ttar -l', 'ext' => 'tar');
  2918. }
  2919. if (empty($arcs['extract']['application/x-tar'])) {
  2920. $arcs['extract']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'e -ttar -y', 'ext' => 'tar');
  2921. }
  2922. } else if (substr(PHP_OS,0,3) === 'WIN') {
  2923. // check `7z` for Windows server.
  2924. unset($o);
  2925. $this->procExec('7z', $o, $c);
  2926. if ($c == 0) {
  2927. $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7z', 'argc' => 'a -mx0', 'ext' => '7z');
  2928. $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7z', 'argc' => 'x -y', 'ext' => '7z');
  2929. if (empty($arcs['create']['application/x-gzip'])) {
  2930. $arcs['create']['application/x-gzip'] = array('cmd' => '7z', 'argc' => 'a -tgzip -mx0', 'ext' => 'tar.gz');
  2931. }
  2932. if (empty($arcs['extract']['application/x-gzip'])) {
  2933. $arcs['extract']['application/x-gzip'] = array('cmd' => '7z', 'argc' => 'x -tgzip -y', 'ext' => 'tar.gz');
  2934. }
  2935. if (empty($arcs['create']['application/x-bzip2'])) {
  2936. $arcs['create']['application/x-bzip2'] = array('cmd' => '7z', 'argc' => 'a -tbzip2 -mx0', 'ext' => 'tar.bz');
  2937. }
  2938. if (empty($arcs['extract']['application/x-bzip2'])) {
  2939. $arcs['extract']['application/x-bzip2'] = array('cmd' => '7z', 'argc' => 'x -tbzip2 -y', 'ext' => 'tar.bz');
  2940. }
  2941. if (empty($arcs['create']['application/zip'])) {
  2942. $arcs['create']['application/zip'] = array('cmd' => '7z', 'argc' => 'a -tzip -l -mx0', 'ext' => 'zip');
  2943. }
  2944. if (empty($arcs['extract']['application/zip'])) {
  2945. $arcs['extract']['application/zip'] = array('cmd' => '7z', 'argc' => 'x -tzip -y', 'ext' => 'zip');
  2946. }
  2947. if (empty($arcs['create']['application/x-tar'])) {
  2948. $arcs['create']['application/x-tar'] = array('cmd' => '7z', 'argc' => 'a -ttar -l -mx0', 'ext' => 'tar');
  2949. }
  2950. if (empty($arcs['extract']['application/x-tar'])) {
  2951. $arcs['extract']['application/x-tar'] = array('cmd' => '7z', 'argc' => 'x -ttar -y', 'ext' => 'tar');
  2952. }
  2953. if (empty($arcs['create']['application/x-rar'])) {
  2954. $arcs['create']['application/x-rar'] = array('cmd' => '7z', 'argc' => 'a -trar -l -mx0', 'ext' => 'rar');
  2955. }
  2956. if (empty($arcs['extract']['application/x-rar'])) {
  2957. $arcs['extract']['application/x-rar'] = array('cmd' => '7z', 'argc' => 'x -trar -y', 'ext' => 'rar');
  2958. }
  2959. }
  2960. }
  2961. $_SESSION['ELFINDER_ARCHIVERS_CACHE'] = $arcs;
  2962. return $arcs;
  2963. }
  2964. /**==================================* abstract methods *====================================**/
  2965. /*********************** paths/urls *************************/
  2966. /**
  2967. * Return parent directory path
  2968. *
  2969. * @param string $path file path
  2970. * @return string
  2971. * @author Dmitry (dio) Levashov
  2972. **/
  2973. abstract protected function _dirname($path);
  2974. /**
  2975. * Return file name
  2976. *
  2977. * @param string $path file path
  2978. * @return string
  2979. * @author Dmitry (dio) Levashov
  2980. **/
  2981. abstract protected function _basename($path);
  2982. /**
  2983. * Join dir name and file name and return full path.
  2984. * Some drivers (db) use int as path - so we give to concat path to driver itself
  2985. *
  2986. * @param string $dir dir path
  2987. * @param string $name file name
  2988. * @return string
  2989. * @author Dmitry (dio) Levashov
  2990. **/
  2991. abstract protected function _joinPath($dir, $name);
  2992. /**
  2993. * Return normalized path
  2994. *
  2995. * @param string $path file path
  2996. * @return string
  2997. * @author Dmitry (dio) Levashov
  2998. **/
  2999. abstract protected function _normpath($path);
  3000. /**
  3001. * Return file path related to root dir
  3002. *
  3003. * @param string $path file path
  3004. * @return string
  3005. * @author Dmitry (dio) Levashov
  3006. **/
  3007. abstract protected function _relpath($path);
  3008. /**
  3009. * Convert path related to root dir into real path
  3010. *
  3011. * @param string $path rel file path
  3012. * @return string
  3013. * @author Dmitry (dio) Levashov
  3014. **/
  3015. abstract protected function _abspath($path);
  3016. /**
  3017. * Return fake path started from root dir.
  3018. * Required to show path on client side.
  3019. *
  3020. * @param string $path file path
  3021. * @return string
  3022. * @author Dmitry (dio) Levashov
  3023. **/
  3024. abstract protected function _path($path);
  3025. /**
  3026. * Return true if $path is children of $parent
  3027. *
  3028. * @param string $path path to check
  3029. * @param string $parent parent path
  3030. * @return bool
  3031. * @author Dmitry (dio) Levashov
  3032. **/
  3033. abstract protected function _inpath($path, $parent);
  3034. /**
  3035. * Return stat for given path.
  3036. * Stat contains following fields:
  3037. * - (int) size file size in b. required
  3038. * - (int) ts file modification time in unix time. required
  3039. * - (string) mime mimetype. required for folders, others - optionally
  3040. * - (bool) read read permissions. required
  3041. * - (bool) write write permissions. required
  3042. * - (bool) locked is object locked. optionally
  3043. * - (bool) hidden is object hidden. optionally
  3044. * - (string) alias for symlinks - link target path relative to root path. optionally
  3045. * - (string) target for symlinks - link target path. optionally
  3046. *
  3047. * If file does not exists - returns empty array or false.
  3048. *
  3049. * @param string $path file path
  3050. * @return array|false
  3051. * @author Dmitry (dio) Levashov
  3052. **/
  3053. abstract protected function _stat($path);
  3054. /***************** file stat ********************/
  3055. /**
  3056. * Return true if path is dir and has at least one childs directory
  3057. *
  3058. * @param string $path dir path
  3059. * @return bool
  3060. * @author Dmitry (dio) Levashov
  3061. **/
  3062. abstract protected function _subdirs($path);
  3063. /**
  3064. * Return object width and height
  3065. * Ususaly used for images, but can be realize for video etc...
  3066. *
  3067. * @param string $path file path
  3068. * @param string $mime file mime type
  3069. * @return string
  3070. * @author Dmitry (dio) Levashov
  3071. **/
  3072. abstract protected function _dimensions($path, $mime);
  3073. /******************** file/dir content *********************/
  3074. /**
  3075. * Return files list in directory
  3076. *
  3077. * @param string $path dir path
  3078. * @return array
  3079. * @author Dmitry (dio) Levashov
  3080. **/
  3081. abstract protected function _scandir($path);
  3082. /**
  3083. * Open file and return file pointer
  3084. *
  3085. * @param string $path file path
  3086. * @param bool $write open file for writing
  3087. * @return resource|false
  3088. * @author Dmitry (dio) Levashov
  3089. **/
  3090. abstract protected function _fopen($path, $mode="rb");
  3091. /**
  3092. * Close opened file
  3093. *
  3094. * @param resource $fp file pointer
  3095. * @param string $path file path
  3096. * @return bool
  3097. * @author Dmitry (dio) Levashov
  3098. **/
  3099. abstract protected function _fclose($fp, $path='');
  3100. /******************** file/dir manipulations *************************/
  3101. /**
  3102. * Create dir and return created dir path or false on failed
  3103. *
  3104. * @param string $path parent dir path
  3105. * @param string $name new directory name
  3106. * @return string|bool
  3107. * @author Dmitry (dio) Levashov
  3108. **/
  3109. abstract protected function _mkdir($path, $name);
  3110. /**
  3111. * Create file and return it's path or false on failed
  3112. *
  3113. * @param string $path parent dir path
  3114. * @param string $name new file name
  3115. * @return string|bool
  3116. * @author Dmitry (dio) Levashov
  3117. **/
  3118. abstract protected function _mkfile($path, $name);
  3119. /**
  3120. * Create symlink
  3121. *
  3122. * @param string $source file to link to
  3123. * @param string $targetDir folder to create link in
  3124. * @param string $name symlink name
  3125. * @return bool
  3126. * @author Dmitry (dio) Levashov
  3127. **/
  3128. abstract protected function _symlink($source, $targetDir, $name);
  3129. /**
  3130. * Copy file into another file (only inside one volume)
  3131. *
  3132. * @param string $source source file path
  3133. * @param string $target target dir path
  3134. * @param string $name file name
  3135. * @return bool
  3136. * @author Dmitry (dio) Levashov
  3137. **/
  3138. abstract protected function _copy($source, $targetDir, $name);
  3139. /**
  3140. * Move file into another parent dir.
  3141. * Return new file path or false.
  3142. *
  3143. * @param string $source source file path
  3144. * @param string $target target dir path
  3145. * @param string $name file name
  3146. * @return string|bool
  3147. * @author Dmitry (dio) Levashov
  3148. **/
  3149. abstract protected function _move($source, $targetDir, $name);
  3150. /**
  3151. * Remove file
  3152. *
  3153. * @param string $path file path
  3154. * @return bool
  3155. * @author Dmitry (dio) Levashov
  3156. **/
  3157. abstract protected function _unlink($path);
  3158. /**
  3159. * Remove dir
  3160. *
  3161. * @param string $path dir path
  3162. * @return bool
  3163. * @author Dmitry (dio) Levashov
  3164. **/
  3165. abstract protected function _rmdir($path);
  3166. /**
  3167. * Create new file and write into it from file pointer.
  3168. * Return new file path or false on error.
  3169. *
  3170. * @param resource $fp file pointer
  3171. * @param string $dir target dir path
  3172. * @param string $name file name
  3173. * @param array $stat file stat (required by some virtual fs)
  3174. * @return bool|string
  3175. * @author Dmitry (dio) Levashov
  3176. **/
  3177. abstract protected function _save($fp, $dir, $name, $stat);
  3178. /**
  3179. * Get file contents
  3180. *
  3181. * @param string $path file path
  3182. * @return string|false
  3183. * @author Dmitry (dio) Levashov
  3184. **/
  3185. abstract protected function _getContents($path);
  3186. /**
  3187. * Write a string to a file
  3188. *
  3189. * @param string $path file path
  3190. * @param string $content new file content
  3191. * @return bool
  3192. * @author Dmitry (dio) Levashov
  3193. **/
  3194. abstract protected function _filePutContents($path, $content);
  3195. /**
  3196. * Extract files from archive
  3197. *
  3198. * @param string $path file path
  3199. * @param array $arc archiver options
  3200. * @return bool
  3201. * @author Dmitry (dio) Levashov,
  3202. * @author Alexey Sukhotin
  3203. **/
  3204. abstract protected function _extract($path, $arc);
  3205. /**
  3206. * Create archive and return its path
  3207. *
  3208. * @param string $dir target dir
  3209. * @param array $files files names list
  3210. * @param string $name archive name
  3211. * @param array $arc archiver options
  3212. * @return string|bool
  3213. * @author Dmitry (dio) Levashov,
  3214. * @author Alexey Sukhotin
  3215. **/
  3216. abstract protected function _archive($dir, $files, $name, $arc);
  3217. /**
  3218. * Detect available archivers
  3219. *
  3220. * @return void
  3221. * @author Dmitry (dio) Levashov,
  3222. * @author Alexey Sukhotin
  3223. **/
  3224. abstract protected function _checkArchivers();
  3225. } // END class