PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/elfinder/php/elFinderVolumeDriver.php

https://bitbucket.org/migelsabre/elfinderintegration
PHP | 3504 lines | 1753 code | 498 blank | 1253 comment | 524 complexity | 7a153c3021d6cc22424362c3f7521597 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. // library to crypt/uncrypt files names (not implemented)
  147. 'cryptLib' => '',
  148. // how to detect files mimetypes. (auto/internal/finfo/mime_content_type)
  149. 'mimeDetect' => 'auto',
  150. // mime.types file path (for mimeDetect==internal)
  151. 'mimefile' => '',
  152. // directory for thumbnails
  153. 'tmbPath' => '.tmb',
  154. // mode to create thumbnails dir
  155. 'tmbPathMode' => 0777,
  156. // thumbnails dir URL. Set it if store thumbnails outside root directory
  157. 'tmbURL' => '',
  158. // thumbnails size (px)
  159. 'tmbSize' => 48,
  160. // thumbnails crop (true - crop, false - scale image to fit thumbnail size)
  161. 'tmbCrop' => true,
  162. // thumbnails background color (hex #rrggbb or 'transparent')
  163. 'tmbBgColor' => '#ffffff',
  164. // image manipulations library
  165. 'imgLib' => 'auto',
  166. // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  167. 'copyOverwrite' => true,
  168. // if true - join new and old directories content on paste
  169. 'copyJoin' => true,
  170. // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
  171. 'uploadOverwrite' => true,
  172. // mimetypes allowed to upload
  173. 'uploadAllow' => array(),
  174. // mimetypes not allowed to upload
  175. 'uploadDeny' => array(),
  176. // order to proccess uploadAllow and uploadDeny options
  177. 'uploadOrder' => array('deny', 'allow'),
  178. // maximum upload file size. NOTE - this is size for every uploaded files
  179. 'uploadMaxSize' => 0,
  180. // files dates format
  181. 'dateFormat' => 'j M Y H:i',
  182. // files time format
  183. 'timeFormat' => 'H:i',
  184. // if true - every folder will be check for children folders, otherwise all folders will be marked as having subfolders
  185. 'checkSubfolders' => true,
  186. // allow to copy from this volume to other ones?
  187. 'copyFrom' => true,
  188. // allow to copy from other volumes to this one?
  189. 'copyTo' => true,
  190. // list of commands disabled on this root
  191. 'disabled' => array(),
  192. // regexp or function name to validate new file name
  193. 'acceptedName' => '/^[^\.].*/', //<-- DONT touch this! Use constructor options to overwrite it!
  194. // function/class method to control files permissions
  195. 'accessControl' => null,
  196. // some data required by access control
  197. 'accessControlData' => null,
  198. // default permissions. not set hidden/locked here - take no effect
  199. 'defaults' => array(
  200. 'read' => true,
  201. 'write' => true
  202. ),
  203. // files attributes
  204. 'attributes' => array(),
  205. // Allowed archive's mimetypes to create. Leave empty for all available types.
  206. 'archiveMimes' => array(),
  207. // Manual config for archivers. See example below. Leave empty for auto detect
  208. 'archivers' => array(),
  209. // required to fix bug on macos
  210. 'utf8fix' => false,
  211. // ? ? ? ? Ø Å
  212. 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"),
  213. 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5")
  214. );
  215. /**
  216. * Defaults permissions
  217. *
  218. * @var array
  219. **/
  220. protected $defaults = array(
  221. 'read' => true,
  222. 'write' => true,
  223. 'locked' => false,
  224. 'hidden' => false
  225. );
  226. /**
  227. * Access control function/class
  228. *
  229. * @var mixed
  230. **/
  231. protected $attributes = array();
  232. /**
  233. * Access control function/class
  234. *
  235. * @var mixed
  236. **/
  237. protected $access = null;
  238. /**
  239. * Mime types allowed to upload
  240. *
  241. * @var array
  242. **/
  243. protected $uploadAllow = array();
  244. /**
  245. * Mime types denied to upload
  246. *
  247. * @var array
  248. **/
  249. protected $uploadDeny = array();
  250. /**
  251. * Order to validate uploadAllow and uploadDeny
  252. *
  253. * @var array
  254. **/
  255. protected $uploadOrder = array();
  256. /**
  257. * Maximum allowed upload file size.
  258. * Set as number or string with unit - "10M", "500K", "1G"
  259. *
  260. * @var int|string
  261. **/
  262. protected $uploadMaxSize = 0;
  263. /**
  264. * Mimetype detect method
  265. *
  266. * @var string
  267. **/
  268. protected $mimeDetect = 'auto';
  269. /**
  270. * Flag - mimetypes from externail file was loaded
  271. *
  272. * @var bool
  273. **/
  274. private static $mimetypesLoaded = false;
  275. /**
  276. * Finfo object for mimeDetect == 'finfo'
  277. *
  278. * @var object
  279. **/
  280. protected $finfo = null;
  281. /**
  282. * List of disabled client's commands
  283. *
  284. * @var array
  285. **/
  286. protected $diabled = array();
  287. /**
  288. * default extensions/mimetypes for mimeDetect == 'internal'
  289. *
  290. * @var array
  291. **/
  292. protected static $mimetypes = array(
  293. // applications
  294. 'ai' => 'application/postscript',
  295. 'eps' => 'application/postscript',
  296. 'exe' => 'application/x-executable',
  297. 'doc' => 'application/vnd.ms-word',
  298. 'xls' => 'application/vnd.ms-excel',
  299. 'ppt' => 'application/vnd.ms-powerpoint',
  300. 'pps' => 'application/vnd.ms-powerpoint',
  301. 'pdf' => 'application/pdf',
  302. 'xml' => 'application/xml',
  303. 'swf' => 'application/x-shockwave-flash',
  304. 'torrent' => 'application/x-bittorrent',
  305. 'jar' => 'application/x-jar',
  306. // open office (finfo detect as application/zip)
  307. 'odt' => 'application/vnd.oasis.opendocument.text',
  308. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  309. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  310. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  311. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  312. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  313. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  314. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  315. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  316. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  317. 'odc' => 'application/vnd.oasis.opendocument.chart',
  318. 'odf' => 'application/vnd.oasis.opendocument.formula',
  319. 'odb' => 'application/vnd.oasis.opendocument.database',
  320. 'odi' => 'application/vnd.oasis.opendocument.image',
  321. 'oxt' => 'application/vnd.openofficeorg.extension',
  322. // MS office 2007 (finfo detect as application/zip)
  323. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  324. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  325. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  326. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  327. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  328. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  329. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  330. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  331. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  332. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  333. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  334. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  335. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  336. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  337. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  338. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  339. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  340. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  341. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  342. // archives
  343. 'gz' => 'application/x-gzip',
  344. 'tgz' => 'application/x-gzip',
  345. 'bz' => 'application/x-bzip2',
  346. 'bz2' => 'application/x-bzip2',
  347. 'tbz' => 'application/x-bzip2',
  348. 'zip' => 'application/zip',
  349. 'rar' => 'application/x-rar',
  350. 'tar' => 'application/x-tar',
  351. '7z' => 'application/x-7z-compressed',
  352. // texts
  353. 'txt' => 'text/plain',
  354. 'php' => 'text/x-php',
  355. 'html' => 'text/html',
  356. 'htm' => 'text/html',
  357. 'js' => 'text/javascript',
  358. 'css' => 'text/css',
  359. 'rtf' => 'text/rtf',
  360. 'rtfd' => 'text/rtfd',
  361. 'py' => 'text/x-python',
  362. 'java' => 'text/x-java-source',
  363. 'rb' => 'text/x-ruby',
  364. 'sh' => 'text/x-shellscript',
  365. 'pl' => 'text/x-perl',
  366. 'xml' => 'text/xml',
  367. 'sql' => 'text/x-sql',
  368. 'c' => 'text/x-csrc',
  369. 'h' => 'text/x-chdr',
  370. 'cpp' => 'text/x-c++src',
  371. 'hh' => 'text/x-c++hdr',
  372. 'log' => 'text/plain',
  373. 'csv' => 'text/x-comma-separated-values',
  374. // images
  375. 'bmp' => 'image/x-ms-bmp',
  376. 'jpg' => 'image/jpeg',
  377. 'jpeg' => 'image/jpeg',
  378. 'gif' => 'image/gif',
  379. 'png' => 'image/png',
  380. 'tif' => 'image/tiff',
  381. 'tiff' => 'image/tiff',
  382. 'tga' => 'image/x-targa',
  383. 'psd' => 'image/vnd.adobe.photoshop',
  384. 'ai' => 'image/vnd.adobe.photoshop',
  385. 'xbm' => 'image/xbm',
  386. 'pxm' => 'image/pxm',
  387. //audio
  388. 'mp3' => 'audio/mpeg',
  389. 'mid' => 'audio/midi',
  390. 'ogg' => 'audio/ogg',
  391. 'oga' => 'audio/ogg',
  392. 'm4a' => 'audio/x-m4a',
  393. 'wav' => 'audio/wav',
  394. 'wma' => 'audio/x-ms-wma',
  395. // video
  396. 'avi' => 'video/x-msvideo',
  397. 'dv' => 'video/x-dv',
  398. 'mp4' => 'video/mp4',
  399. 'mpeg' => 'video/mpeg',
  400. 'mpg' => 'video/mpeg',
  401. 'mov' => 'video/quicktime',
  402. 'wm' => 'video/x-ms-wmv',
  403. 'flv' => 'video/x-flv',
  404. 'mkv' => 'video/x-matroska',
  405. 'webm' => 'video/webm',
  406. 'ogv' => 'video/ogg',
  407. 'ogm' => 'video/ogg'
  408. );
  409. /**
  410. * Directory separator - required by client
  411. *
  412. * @var string
  413. **/
  414. protected $separator = DIRECTORY_SEPARATOR;
  415. /**
  416. * Mimetypes allowed to display
  417. *
  418. * @var array
  419. **/
  420. protected $onlyMimes = array();
  421. /**
  422. * Store files moved or overwrited files info
  423. *
  424. * @var array
  425. **/
  426. protected $removed = array();
  427. /**
  428. * Cache storage
  429. *
  430. * @var array
  431. **/
  432. protected $cache = array();
  433. /**
  434. * Cache by folders
  435. *
  436. * @var array
  437. **/
  438. protected $dirsCache = array();
  439. /*********************************************************************/
  440. /* INITIALIZATION */
  441. /*********************************************************************/
  442. /**
  443. * Prepare driver before mount volume.
  444. * Return true if volume is ready.
  445. *
  446. * @return bool
  447. * @author Dmitry (dio) Levashov
  448. **/
  449. protected function init() {
  450. return true;
  451. }
  452. /**
  453. * Configure after successfull mount.
  454. * By default set thumbnails path and image manipulation library.
  455. *
  456. * @return void
  457. * @author Dmitry (dio) Levashov
  458. **/
  459. protected function configure() {
  460. // set thumbnails path
  461. $path = $this->options['tmbPath'];
  462. if ($path) {
  463. if (!file_exists($path)) {
  464. if (@mkdir($path)) {
  465. chmod($path, $this->options['tmbPathMode']);
  466. } else {
  467. $path = '';
  468. }
  469. }
  470. if (is_dir($path) && is_readable($path)) {
  471. $this->tmbPath = $path;
  472. $this->tmbPathWritable = is_writable($path);
  473. }
  474. }
  475. // set image manipulation library
  476. $type = preg_match('/^(imagick|gd|auto)$/i', $this->options['imgLib'])
  477. ? strtolower($this->options['imgLib'])
  478. : 'auto';
  479. if (($type == 'imagick' || $type == 'auto') && extension_loaded('imagick')) {
  480. $this->imgLib = 'imagick';
  481. } else {
  482. $this->imgLib = function_exists('gd_info') ? 'gd' : '';
  483. }
  484. }
  485. /*********************************************************************/
  486. /* PUBLIC API */
  487. /*********************************************************************/
  488. /**
  489. * Return driver id. Used as a part of volume id.
  490. *
  491. * @return string
  492. * @author Dmitry (dio) Levashov
  493. **/
  494. public function driverId() {
  495. return $this->driverId;
  496. }
  497. /**
  498. * Return volume id
  499. *
  500. * @return string
  501. * @author Dmitry (dio) Levashov
  502. **/
  503. public function id() {
  504. return $this->id;
  505. }
  506. /**
  507. * Return debug info for client
  508. *
  509. * @return array
  510. * @author Dmitry (dio) Levashov
  511. **/
  512. public function debug() {
  513. return array(
  514. 'id' => $this->id(),
  515. 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
  516. 'mimeDetect' => $this->mimeDetect,
  517. 'imgLib' => $this->imgLib
  518. );
  519. }
  520. /**
  521. * "Mount" volume.
  522. * Return true if volume available for read or write,
  523. * false - otherwise
  524. *
  525. * @return bool
  526. * @author Dmitry (dio) Levashov
  527. * @author Alexey Sukhotin
  528. **/
  529. public function mount(array $opts) {
  530. if (!isset($opts['path']) || $opts['path'] === '') {
  531. return $this->setError('Path undefined.');;
  532. }
  533. $this->options = array_merge($this->options, $opts);
  534. $this->id = $this->driverId.(!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++).'_';
  535. $this->root = $this->_normpath($this->options['path']);
  536. $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR;
  537. // default file attribute
  538. $this->defaults = array(
  539. 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true,
  540. 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true,
  541. 'locked' => false,
  542. 'hidden' => false
  543. );
  544. // root attributes
  545. $this->attributes[] = array(
  546. 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR).'$~',
  547. 'locked' => true,
  548. 'hidden' => false
  549. );
  550. // set files attributes
  551. if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) {
  552. foreach ($this->options['attributes'] as $a) {
  553. // attributes must contain pattern and at least one rule
  554. if (!empty($a['pattern']) || count($a) > 1) {
  555. $this->attributes[] = $a;
  556. }
  557. }
  558. }
  559. if (!empty($this->options['accessControl'])) {
  560. if (is_string($this->options['accessControl'])
  561. && function_exists($this->options['accessControl'])) {
  562. $this->access = $this->options['accessControl'];
  563. } elseif (is_array($this->options['accessControl'])
  564. && count($this->options['accessControl']) > 1
  565. && is_object($this->options['accessControl'][0])
  566. && method_exists($this->options['accessControl'][0], $this->options['accessControl'][1])) {
  567. $this->access = array($this->options['accessControl'][0], $this->options['accessControl'][1]);
  568. }
  569. }
  570. $this->today = mktime(0,0,0, date('m'), date('d'), date('Y'));
  571. $this->yesterday = $this->today-86400;
  572. // debug($this->attributes);
  573. if (!$this->init()) {
  574. return false;
  575. }
  576. // check some options is arrays
  577. $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow'])
  578. ? $this->options['uploadAllow']
  579. : array();
  580. $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny'])
  581. ? $this->options['uploadDeny']
  582. : array();
  583. if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
  584. $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow');
  585. $this->uploadOrder = array(trim($parts[0]), trim($parts[1]));
  586. } else { // telephat_mode off
  587. $this->uploadOrder = $this->options['uploadOrder'];
  588. }
  589. if (!empty($this->options['uploadMaxSize'])) {
  590. $size = ''.$this->options['uploadMaxSize'];
  591. $unit = strtolower(substr($size, strlen($size) - 1));
  592. $n = 1;
  593. switch ($unit) {
  594. case 'k':
  595. $n = 1024;
  596. break;
  597. case 'm':
  598. $n = 1048576;
  599. break;
  600. case 'g':
  601. $n = 1073741824;
  602. }
  603. $this->uploadMaxSize = intval($size)*$n;
  604. }
  605. $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled'])
  606. ? $this->options['disabled']
  607. : array();
  608. $this->cryptLib = $this->options['cryptLib'];
  609. $this->mimeDetect = $this->options['mimeDetect'];
  610. // find available mimetype detect method
  611. $type = strtolower($this->options['mimeDetect']);
  612. $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ? $type : 'auto';
  613. $regexp = '/text\/x\-(php|c\+\+)/';
  614. if (($type == 'finfo' || $type == 'auto')
  615. && class_exists('finfo')) {
  616. $tmpFileInfo = @explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
  617. } else {
  618. $tmpFileInfo = false;
  619. }
  620. if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
  621. $type = 'finfo';
  622. $this->finfo = finfo_open(FILEINFO_MIME);
  623. } elseif (($type == 'mime_content_type' || $type == 'auto')
  624. && function_exists('mime_content_type')
  625. && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
  626. $type = 'mime_content_type';
  627. } else {
  628. $type = 'internal';
  629. }
  630. $this->mimeDetect = $type;
  631. // load mimes from external file for mimeDetect == 'internal'
  632. // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
  633. // file must be in file directory or in parent one
  634. if ($this->mimeDetect == 'internal' && !self::$mimetypesLoaded) {
  635. self::$mimetypesLoaded = true;
  636. $this->mimeDetect = 'internal';
  637. $file = false;
  638. if (!empty($this->options['mimefile']) && file_exists($this->options['mimefile'])) {
  639. $file = $this->options['mimefile'];
  640. } elseif (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types')) {
  641. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.'mime.types';
  642. } elseif (file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types')) {
  643. $file = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'mime.types';
  644. }
  645. if ($file && file_exists($file)) {
  646. $mimecf = file($file);
  647. foreach ($mimecf as $line_num => $line) {
  648. if (!preg_match('/^\s*#/', $line)) {
  649. $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
  650. for ($i = 1, $size = count($mime); $i < $size ; $i++) {
  651. if (!isset(self::$mimetypes[$mime[$i]])) {
  652. self::$mimetypes[$mime[$i]] = $mime[0];
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. $this->rootName = empty($this->options['alias']) ? $this->_basename($this->root) : $this->options['alias'];
  660. $root = $this->stat($this->root);
  661. if (!$root) {
  662. return $this->setError('Root folder does not exists.');
  663. }
  664. if (!$root['read'] && !$root['write']) {
  665. return $this->setError('Root folder has not read and write permissions.');
  666. }
  667. // debug($root);
  668. if ($root['read']) {
  669. // check startPath - path to open by default instead of root
  670. if ($this->options['startPath']) {
  671. $start = $this->stat($this->options['startPath']);
  672. if (!empty($start)
  673. && $start['mime'] == 'directory'
  674. && $start['read']
  675. && empty($start['hidden'])
  676. && $this->_inpath($this->options['startPath'], $this->root)) {
  677. $this->startPath = $this->options['startPath'];
  678. if (substr($this->startPath, -1, 1) == $this->options['separator']) {
  679. $this->startPath = substr($this->startPath, 0, -1);
  680. }
  681. }
  682. }
  683. } else {
  684. $this->options['URL'] = '';
  685. $this->options['tmbURL'] = '';
  686. $this->options['tmbPath'] = '';
  687. // read only volume
  688. array_unshift($this->attributes, array(
  689. 'pattern' => '/.*/',
  690. 'read' => false
  691. ));
  692. }
  693. $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1;
  694. $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48;
  695. $this->URL = $this->options['URL'];
  696. if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) {
  697. $this->URL .= '/';
  698. }
  699. $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : '';
  700. if ($this->tmbURL && preg_match("|[^/?&=]$|", $this->tmbURL)) {
  701. $this->tmbURL .= '/';
  702. }
  703. $this->nameValidator = is_string($this->options['acceptedName']) && !empty($this->options['acceptedName'])
  704. ? $this->options['acceptedName']
  705. : '';
  706. $this->_checkArchivers();
  707. // manual control archive types to create
  708. if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) {
  709. foreach ($this->archivers['create'] as $mime => $v) {
  710. if (!in_array($mime, $this->options['archiveMimes'])) {
  711. unset($this->archivers['create'][$mime]);
  712. }
  713. }
  714. }
  715. // manualy add archivers
  716. if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) {
  717. foreach ($this->options['archivers']['create'] as $mime => $conf) {
  718. if (strpos($mime, 'application/') === 0
  719. && !empty($conf['cmd'])
  720. && isset($conf['argc'])
  721. && !empty($conf['ext'])
  722. && !isset($this->archivers['create'][$mime])) {
  723. $this->archivers['create'][$mime] = $conf;
  724. }
  725. }
  726. }
  727. if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) {
  728. foreach ($this->options['archivers']['extract'] as $mime => $conf) {
  729. if (substr($mime, 'application/') === 0
  730. && !empty($cons['cmd'])
  731. && isset($conf['argc'])
  732. && !empty($conf['ext'])
  733. && !isset($this->archivers['extract'][$mime])) {
  734. $this->archivers['extract'][$mime] = $conf;
  735. }
  736. }
  737. }
  738. $this->configure();
  739. // echo $this->uploadMaxSize;
  740. // echo $this->options['uploadMaxSize'];
  741. return $this->mounted = true;
  742. }
  743. /**
  744. * Some "unmount" stuffs - may be required by virtual fs
  745. *
  746. * @return void
  747. * @author Dmitry (dio) Levashov
  748. **/
  749. public function umount() {
  750. }
  751. /**
  752. * Return error message from last failed action
  753. *
  754. * @return array
  755. * @author Dmitry (dio) Levashov
  756. **/
  757. public function error() {
  758. return $this->error;
  759. }
  760. /**
  761. * Set mimetypes allowed to display to client
  762. *
  763. * @param array $mimes
  764. * @return void
  765. * @author Dmitry (dio) Levashov
  766. **/
  767. public function setMimesFilter($mimes) {
  768. if (is_array($mimes)) {
  769. $this->onlyMimes = $mimes;
  770. }
  771. }
  772. /**
  773. * Return root folder hash
  774. *
  775. * @return string
  776. * @author Dmitry (dio) Levashov
  777. **/
  778. public function root() {
  779. return $this->encode($this->root);
  780. }
  781. /**
  782. * Return root or startPath hash
  783. *
  784. * @return string
  785. * @author Dmitry (dio) Levashov
  786. **/
  787. public function defaultPath() {
  788. return $this->encode($this->startPath ? $this->startPath : $this->root);
  789. }
  790. /**
  791. * Return volume options required by client:
  792. *
  793. * @return array
  794. * @author Dmitry (dio) Levashov
  795. **/
  796. public function options($hash) {
  797. return array(
  798. 'path' => $this->_path($this->decode($hash)),
  799. 'url' => $this->URL,
  800. 'tmbUrl' => $this->tmbURL,
  801. 'disabled' => $this->disabled,
  802. 'separator' => $this->separator,
  803. 'copyOverwrite' => intval($this->options['copyOverwrite']),
  804. 'archivers' => array(
  805. 'create' => array_keys($this->archivers['create']),
  806. 'extract' => array_keys($this->archivers['extract'])
  807. )
  808. );
  809. }
  810. /**
  811. * Return true if command disabled in options
  812. *
  813. * @param string $cmd command name
  814. * @return bool
  815. * @author Dmitry (dio) Levashov
  816. **/
  817. public function commandDisabled($cmd) {
  818. return in_array($cmd, $this->disabled);
  819. }
  820. /**
  821. * Return true if mime is required mimes list
  822. *
  823. * @param string $mime mime type to check
  824. * @param array $mimes allowed mime types list or not set to use client mimes list
  825. * @param bool|null $empty what to return on empty list
  826. * @return bool|null
  827. * @author Dmitry (dio) Levashov
  828. * @author Troex Nevelin
  829. **/
  830. public function mimeAccepted($mime, $mimes = array(), $empty = true) {
  831. $mimes = !empty($mimes) ? $mimes : $this->onlyMimes;
  832. if (empty($mimes)) {
  833. return $empty;
  834. }
  835. return $mime == 'directory'
  836. || in_array('all', $mimes)
  837. || in_array('All', $mimes)
  838. || in_array($mime, $mimes)
  839. || in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
  840. }
  841. /**
  842. * Return true if voume is readable.
  843. *
  844. * @return bool
  845. * @author Dmitry (dio) Levashov
  846. **/
  847. public function isReadable() {
  848. $stat = $this->stat($this->root);
  849. return $stat['read'];
  850. }
  851. /**
  852. * Return true if copy from this volume allowed
  853. *
  854. * @return bool
  855. * @author Dmitry (dio) Levashov
  856. **/
  857. public function copyFromAllowed() {
  858. return !!$this->options['copyFrom'];
  859. }
  860. /**
  861. * Return file path related to root
  862. *
  863. * @param string $hash file hash
  864. * @return string
  865. * @author Dmitry (dio) Levashov
  866. **/
  867. public function path($hash) {
  868. return $this->_path($this->decode($hash));
  869. }
  870. /**
  871. * Return file real path if file exists
  872. *
  873. * @param string $hash file hash
  874. * @return string
  875. * @author Dmitry (dio) Levashov
  876. **/
  877. public function realpath($hash) {
  878. $path = $this->decode($hash);
  879. return $this->stat($path) ? $path : false;
  880. }
  881. /**
  882. * Return list of moved/overwrited files
  883. *
  884. * @return array
  885. * @author Dmitry (dio) Levashov
  886. **/
  887. public function removed() {
  888. return $this->removed;
  889. }
  890. /**
  891. * Clean removed files list
  892. *
  893. * @return void
  894. * @author Dmitry (dio) Levashov
  895. **/
  896. public function resetRemoved() {
  897. $this->removed = array();
  898. }
  899. /**
  900. * Return file/dir hash or first founded child hash with required attr == $val
  901. *
  902. * @param string $hash file hash
  903. * @param string $attr attribute name
  904. * @param bool $val attribute value
  905. * @return string|false
  906. * @author Dmitry (dio) Levashov
  907. **/
  908. public function closest($hash, $attr, $val) {
  909. return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false;
  910. }
  911. /**
  912. * Return file info or false on error
  913. *
  914. * @param string $hash file hash
  915. * @param bool $realpath add realpath field to file info
  916. * @return array|false
  917. * @author Dmitry (dio) Levashov
  918. **/
  919. public function file($hash) {
  920. $path = $this->decode($hash);
  921. return ($file = $this->stat($path)) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  922. if (($file = $this->stat($path)) != false) {
  923. if ($realpath) {
  924. $file['realpath'] = $path;
  925. }
  926. return $file;
  927. }
  928. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  929. }
  930. /**
  931. * Return folder info
  932. *
  933. * @param string $hash folder hash
  934. * @param bool $hidden return hidden file info
  935. * @return array|false
  936. * @author Dmitry (dio) Levashov
  937. **/
  938. public function dir($hash, $resolveLink=false) {
  939. if (($dir = $this->file($hash)) == false) {
  940. return $this->setError(elFinder::ERROR_DIR_NOT_FOUND);
  941. }
  942. if ($resolveLink && !empty($dir['thash'])) {
  943. $dir = $this->file($dir['thash']);
  944. }
  945. return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
  946. ? $dir
  947. : $this->setError(elFinder::ERROR_NOT_DIR);
  948. }
  949. /**
  950. * Return directory content or false on error
  951. *
  952. * @param string $hash file hash
  953. * @return array|false
  954. * @author Dmitry (dio) Levashov
  955. **/
  956. public function scandir($hash) {
  957. if (($dir = $this->dir($hash)) == false) {
  958. return false;
  959. }
  960. return $dir['read']
  961. ? $this->getScandir($this->decode($hash))
  962. : $this->setError(elFinder::ERROR_PERM_DENIED);
  963. }
  964. /**
  965. * Return dir files names list
  966. *
  967. * @param string $hash file hash
  968. * @return array
  969. * @author Dmitry (dio) Levashov
  970. **/
  971. public function ls($hash) {
  972. if (($dir = $this->dir($hash)) == false || !$dir['read']) {
  973. return false;
  974. }
  975. $list = array();
  976. $path = $this->decode($hash);
  977. foreach ($this->getScandir($path) as $stat) {
  978. if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
  979. $list[] = $stat['name'];
  980. }
  981. }
  982. return $list;
  983. }
  984. /**
  985. * Return subfolders for required folder or false on error
  986. *
  987. * @param string $hash folder hash or empty string to get tree from root folder
  988. * @param int $deep subdir deep
  989. * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
  990. * @return array|false
  991. * @author Dmitry (dio) Levashov
  992. **/
  993. public function tree($hash='', $deep=0, $exclude='') {
  994. $path = $hash ? $this->decode($hash) : $this->root;
  995. if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') {
  996. return false;
  997. }
  998. $dirs = $this->gettree($path, $deep > 0 ? $deep -1 : $this->treeDeep-1, $exclude ? $this->decode($exclude) : null);
  999. array_unshift($dirs, $dir);
  1000. return $dirs;
  1001. }
  1002. /**
  1003. * Return part of dirs tree from required dir up to root dir
  1004. *
  1005. * @param string $hash directory hash
  1006. * @return array
  1007. * @author Dmitry (dio) Levashov
  1008. **/
  1009. public function parents($hash) {
  1010. if (($current = $this->dir($hash)) == false) {
  1011. return false;
  1012. }
  1013. $path = $this->decode($hash);
  1014. $tree = array();
  1015. while ($path && $path != $this->root) {
  1016. $path = $this->_dirname($path);
  1017. $stat = $this->stat($path);
  1018. if (!empty($stat['hidden']) || !$stat['read']) {
  1019. return false;
  1020. }
  1021. array_unshift($tree, $stat);
  1022. if ($path != $this->root) {
  1023. foreach ($this->gettree($path, 0) as $dir) {
  1024. if (!in_array($dir, $tree)) {
  1025. $tree[] = $dir;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. return $tree ? $tree : array($current);
  1031. }
  1032. /**
  1033. * Create thumbnail for required file and return its name of false on failed
  1034. *
  1035. * @return string|false
  1036. * @author Dmitry (dio) Levashov
  1037. **/
  1038. public function tmb($hash) {
  1039. $path = $this->decode($hash);
  1040. $stat = $this->stat($path);
  1041. if (isset($stat['tmb'])) {
  1042. return $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb'];
  1043. }
  1044. return false;
  1045. }
  1046. /**
  1047. * Return file size / total directory size
  1048. *
  1049. * @param string file hash
  1050. * @return int
  1051. * @author Dmitry (dio) Levashov
  1052. **/
  1053. public function size($hash) {
  1054. return $this->countSize($this->decode($hash));
  1055. }
  1056. /**
  1057. * Open file for reading and return file pointer
  1058. *
  1059. * @param string file hash
  1060. * @return Resource
  1061. * @author Dmitry (dio) Levashov
  1062. **/
  1063. public function open($hash) {
  1064. if (($file = $this->file($hash)) == false
  1065. || $file['mime'] == 'directory') {
  1066. return false;
  1067. }
  1068. return $this->_fopen($this->decode($hash), 'rb');
  1069. }
  1070. /**
  1071. * Close file pointer
  1072. *
  1073. * @param Resource $fp file pointer
  1074. * @param string $hash file hash
  1075. * @return void
  1076. * @author Dmitry (dio) Levashov
  1077. **/
  1078. public function close($fp, $hash) {
  1079. $this->_fclose($fp, $this->decode($hash));
  1080. }
  1081. /**
  1082. * Create directory and return dir info
  1083. *
  1084. * @param string $dst destination directory
  1085. * @param string $name directory name
  1086. * @return array|false
  1087. * @author Dmitry (dio) Levashov
  1088. **/
  1089. public function mkdir($dst, $name) {
  1090. if ($this->commandDisabled('mkdir')) {
  1091. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1092. }
  1093. if (!$this->nameAccepted($name)) {
  1094. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1095. }
  1096. if (($dir = $this->dir($dst)) == false) {
  1097. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1098. }
  1099. if (!$dir['write']) {
  1100. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1101. }
  1102. $path = $this->decode($dst);
  1103. $dst = $this->_joinPath($path, $name);
  1104. $stat = $this->stat($dst);
  1105. if (!empty($stat)) {
  1106. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1107. }
  1108. $this->clearcache();
  1109. return ($path = $this->_mkdir($path, $name)) ? $this->stat($path) : false;
  1110. }
  1111. /**
  1112. * Create empty file and return its info
  1113. *
  1114. * @param string $dst destination directory
  1115. * @param string $name file name
  1116. * @return array|false
  1117. * @author Dmitry (dio) Levashov
  1118. **/
  1119. public function mkfile($dst, $name) {
  1120. if ($this->commandDisabled('mkfile')) {
  1121. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1122. }
  1123. if (!$this->nameAccepted($name)) {
  1124. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1125. }
  1126. if (($dir = $this->dir($dst)) == false) {
  1127. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1128. }
  1129. $path = $this->decode($dst);
  1130. if (!$dir['write'] || !$this->allowCreate($path, $name)) {
  1131. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1132. }
  1133. if ($this->stat($this->_joinPath($path, $name))) {
  1134. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1135. }
  1136. $this->clearcache();
  1137. return ($path = $this->_mkfile($path, $name)) ? $this->stat($path) : false;
  1138. }
  1139. /**
  1140. * Rename file and return file info
  1141. *
  1142. * @param string $hash file hash
  1143. * @param string $name new file name
  1144. * @return array|false
  1145. * @author Dmitry (dio) Levashov
  1146. **/
  1147. public function rename($hash, $name) {
  1148. if ($this->commandDisabled('rename')) {
  1149. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1150. }
  1151. if (!$this->nameAccepted($name)) {
  1152. return $this->setError(elFinder::ERROR_INVALID_NAME, $name);
  1153. }
  1154. if (!($file = $this->file($hash))) {
  1155. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1156. }
  1157. if ($name == $file['name']) {
  1158. return $file;
  1159. }
  1160. if (!empty($file['locked'])) {
  1161. return $this->setError(elFinder::ERROR_LOCKED, $file['name']);
  1162. }
  1163. $path = $this->decode($hash);
  1164. $dir = $this->_dirname($path);
  1165. $stat = $this->stat($this->_joinPath($dir, $name));
  1166. if ($stat) {
  1167. return $this->setError(elFinder::ERROR_EXISTS, $name);
  1168. }
  1169. if (!$this->allowCreate($dir, $name)) {
  1170. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1171. }
  1172. $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move
  1173. if (($path = $this->_move($path, $dir, $name))) {
  1174. $this->clearcache();
  1175. return $this->stat($path);
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * Create file copy with suffix "copy number" and return its info
  1181. *
  1182. * @param string $hash file hash
  1183. * @param string $suffix suffix to add to file name
  1184. * @return array|false
  1185. * @author Dmitry (dio) Levashov
  1186. **/
  1187. public function duplicate($hash, $suffix='copy') {
  1188. if ($this->commandDisabled('duplicate')) {
  1189. return $this->setError(elFinder::ERROR_COPY, '#'.$hash, elFinder::ERROR_PERM_DENIED);
  1190. }
  1191. if (($file = $this->file($hash)) == false) {
  1192. return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND);
  1193. }
  1194. $path = $this->decode($hash);
  1195. $dir = $this->_dirname($path);
  1196. $name = $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' ');
  1197. if (!$this->allowCreate($dir, $name)) {
  1198. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1199. }
  1200. return ($path = $this->copy($path, $dir, $name)) == false
  1201. ? false
  1202. : $this->stat($path);
  1203. }
  1204. /**
  1205. * Save uploaded file.
  1206. * On success return array with new file stat and with removed file hash (if existed file was replaced)
  1207. *
  1208. * @param Resource $fp file pointer
  1209. * @param string $dst destination folder hash
  1210. * @param string $src file name
  1211. * @param string $tmpname file tmp name - required to detect mime type
  1212. * @return array|false
  1213. * @author Dmitry (dio) Levashov
  1214. **/
  1215. public function upload($fp, $dst, $name, $tmpname) {
  1216. if ($this->commandDisabled('upload')) {
  1217. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1218. }
  1219. if (($dir = $this->dir($dst)) == false) {
  1220. return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1221. }
  1222. if (!$dir['write']) {
  1223. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1224. }
  1225. if (!$this->nameAccepted($name)) {
  1226. return $this->setError(elFinder::ERROR_INVALID_NAME);
  1227. }
  1228. $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname, $name);
  1229. if ($mime == 'unknown' && $this->mimeDetect == 'internal') {
  1230. $mime = elFinderVolumeDriver::mimetypeInternalDetect($name);
  1231. }
  1232. // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
  1233. $allow = $this->mimeAccepted($mime, $this->uploadAllow, null);
  1234. $deny = $this->mimeAccepted($mime, $this->uploadDeny, null);
  1235. $upload = true; // default to allow
  1236. if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
  1237. $upload = false; // default is deny
  1238. if (!$deny && ($allow === true)) { // match only allow
  1239. $upload = true;
  1240. }// else (both match | no match | match only deny) { deny }
  1241. } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
  1242. $upload = true; // default is allow
  1243. if (($deny === true) && !$allow) { // match only deny
  1244. $upload = false;
  1245. } // else (both match | no match | match only allow) { allow }
  1246. }
  1247. if (!$upload) {
  1248. return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME);
  1249. }
  1250. if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) {
  1251. return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE);
  1252. }
  1253. $dstpath = $this->decode($dst);
  1254. $name = ElFinderIntegrationConnector::transliterate($name);
  1255. $test = $this->_joinPath($dstpath, $name);
  1256. $file = $this->stat($test);
  1257. $this->clearcache();
  1258. if ($file) { // file exists
  1259. if ($this->options['uploadOverwrite']) {
  1260. if (!$file['write']) {
  1261. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1262. } elseif ($file['mime'] == 'directory') {
  1263. return $this->setError(elFinder::ERROR_NOT_REPLACE, $name);
  1264. }
  1265. $this->remove($test);
  1266. } else {
  1267. $name = $this->uniqueName($dstpath, $name, '-', false);
  1268. }
  1269. }
  1270. $stat = array(
  1271. 'mime' => $mime,
  1272. 'width' => 0,
  1273. 'height' => 0,
  1274. 'size' => filesize($tmpname));
  1275. // $w = $h = 0;
  1276. if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
  1277. $stat['width'] = $s[0];
  1278. $stat['height'] = $s[1];
  1279. }
  1280. // $this->clearcache();
  1281. if (($path = $this->_save($fp, $dstpath, $name, $stat)) == false) {
  1282. return false;
  1283. }
  1284. return $this->stat($path);
  1285. }
  1286. /**
  1287. * Paste files
  1288. *
  1289. * @param Object $volume source volume
  1290. * @param string $source file hash
  1291. * @param string $dst destination dir hash
  1292. * @param bool $rmSrc remove source after copy?
  1293. * @return array|false
  1294. * @author Dmitry (dio) Levashov
  1295. **/
  1296. public function paste($volume, $src, $dst, $rmSrc = false) {
  1297. $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY;
  1298. if ($this->commandDisabled('paste')) {
  1299. return $this->setError($err, '#'.$src, elFinder::ERROR_PERM_DENIED);
  1300. }
  1301. if (($file = $volume->file($src, $rmSrc)) == false) {
  1302. return $this->setError($err, '#'.$src, elFinder::ERROR_FILE_NOT_FOUND);
  1303. }
  1304. $name = $file['name'];
  1305. $errpath = $volume->path($src);
  1306. if (($dir = $this->dir($dst)) == false) {
  1307. return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#'.$dst);
  1308. }
  1309. if (!$dir['write'] || !$file['read']) {
  1310. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1311. }
  1312. $destination = $this->decode($dst);
  1313. if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) {
  1314. return $rmSrc
  1315. ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test))
  1316. : $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1317. }
  1318. $test = $this->_joinPath($destination, $name);
  1319. $stat = $this->stat($test);
  1320. $this->clearcache();
  1321. if ($stat) {
  1322. if ($this->options['copyOverwrite']) {
  1323. // do not replace file with dir or dir with file
  1324. if (!$this->isSameType($file['mime'], $stat['mime'])) {
  1325. return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->_path($test));
  1326. }
  1327. // existed file is not writable
  1328. if (!$stat['write']) {
  1329. return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED);
  1330. }
  1331. // existed file locked or has locked child
  1332. if (($locked = $this->closestByAttr($test, 'locked', true))) {
  1333. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($locked));
  1334. }
  1335. // target is entity file of alias
  1336. if ($volume == $this && ($test == @$file['target'] || $test == $this->decode($src))) {
  1337. return $this->setError(elFinder::ERROR_REPLACE, $errpath);
  1338. }
  1339. // remove existed file
  1340. if (!$this->remove($test)) {
  1341. return $this->setError(elFinder::ERROR_REPLACE, $this->_path($test));
  1342. }
  1343. } else {
  1344. $name = $this->uniqueName($destination, $name, ' ', false);
  1345. }
  1346. }
  1347. // copy/move inside current volume
  1348. if ($volume == $this) {
  1349. $source = $this->decode($src);
  1350. // do not copy into itself
  1351. if ($this->_inpath($destination, $source)) {
  1352. return $this->setError(elFinder::ERROR_COPY_INTO_ITSELF, $errpath);
  1353. }
  1354. $method = $rmSrc ? 'move' : 'copy';
  1355. return ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false;
  1356. }
  1357. // copy/move from another volume
  1358. if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) {
  1359. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  1360. }
  1361. if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
  1362. return false;
  1363. }
  1364. if ($rmSrc) {
  1365. if ($volume->rm($src)) {
  1366. $this->removed[] = $file;
  1367. } else {
  1368. return $this->setError(elFinder::ERROR_MOVE, $errpath, elFinder::ERROR_RM_SRC);
  1369. }
  1370. }
  1371. return $this->stat($path);
  1372. }
  1373. /**
  1374. * Return file contents
  1375. *
  1376. * @param string $hash file hash
  1377. * @return string|false
  1378. * @author Dmitry (dio) Levashov
  1379. **/
  1380. public function getContents($hash) {
  1381. $file = $this->file($hash);
  1382. if (!$file) {
  1383. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1384. }
  1385. if ($file['mime'] == 'directory') {
  1386. return $this->setError(elFinder::ERROR_NOT_FILE);
  1387. }
  1388. if (!$file['read']) {
  1389. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1390. }
  1391. return $this->_getContents($this->decode($hash));
  1392. }
  1393. /**
  1394. * Put content in text file and return file info.
  1395. *
  1396. * @param string $hash file hash
  1397. * @param string $content new file content
  1398. * @return array
  1399. * @author Dmitry (dio) Levashov
  1400. **/
  1401. public function putContents($hash, $content) {
  1402. if ($this->commandDisabled('edit')) {
  1403. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1404. }
  1405. $path = $this->decode($hash);
  1406. if (!($file = $this->file($hash))) {
  1407. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1408. }
  1409. if (!$file['write']) {
  1410. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1411. }
  1412. $this->clearcache();
  1413. return $this->_filePutContents($path, $content) ? $this->stat($path) : false;
  1414. }
  1415. /**
  1416. * Extract files from archive
  1417. *
  1418. * @param string $hash archive hash
  1419. * @return array|bool
  1420. * @author Dmitry (dio) Levashov,
  1421. * @author Alexey Sukhotin
  1422. **/
  1423. public function extract($hash) {
  1424. if ($this->commandDisabled('extract')) {
  1425. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1426. }
  1427. if (($file = $this->file($hash)) == false) {
  1428. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1429. }
  1430. $archiver = isset($this->archivers['extract'][$file['mime']])
  1431. ? $this->archivers['extract'][$file['mime']]
  1432. : false;
  1433. if (!$archiver) {
  1434. return $this->setError(elFinder::ERROR_NOT_ARCHIVE);
  1435. }
  1436. $path = $this->decode($hash);
  1437. $parent = $this->stat($this->_dirname($path));
  1438. if (!$file['read'] || !$parent['write']) {
  1439. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1440. }
  1441. $this->clearcache();
  1442. return ($path = $this->_extract($path, $archiver)) ? $this->stat($path) : false;
  1443. }
  1444. /**
  1445. * Add files to archive
  1446. *
  1447. * @return void
  1448. **/
  1449. public function archive($hashes, $mime) {
  1450. if ($this->commandDisabled('archive')) {
  1451. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1452. }
  1453. $archiver = isset($this->archivers['create'][$mime])
  1454. ? $this->archivers['create'][$mime]
  1455. : false;
  1456. if (!$archiver) {
  1457. return $this->setError(elFinder::ERROR_ARCHIVE_TYPE);
  1458. }
  1459. $files = array();
  1460. foreach ($hashes as $hash) {
  1461. if (($file = $this->file($hash)) == false) {
  1462. return $this->error(elFinder::ERROR_FILE_NOT_FOUND, '#'+$hash);
  1463. }
  1464. if (!$file['read']) {
  1465. return $this->error(elFinder::ERROR_PERM_DENIED);
  1466. }
  1467. $path = $this->decode($hash);
  1468. if (!isset($dir)) {
  1469. $dir = $this->_dirname($path);
  1470. $stat = $this->stat($dir);
  1471. if (!$stat['write']) {
  1472. return $this->error(elFinder::ERROR_PERM_DENIED);
  1473. }
  1474. }
  1475. $files[] = $this->_basename($path);
  1476. }
  1477. $name = (count($files) == 1 ? $files[0] : 'Archive').'.'.$archiver['ext'];
  1478. $name = $this->uniqueName($dir, $name, '');
  1479. $this->clearcache();
  1480. return ($path = $this->_archive($dir, $files, $name, $archiver)) ? $this->stat($path) : false;
  1481. }
  1482. /**
  1483. * Resize image
  1484. *
  1485. * @param string $hash image file
  1486. * @param int $width new width
  1487. * @param int $height new height
  1488. * @param int $x X start poistion for crop
  1489. * @param int $y Y start poistion for crop
  1490. * @param string $mode action how to mainpulate image
  1491. * @return array|false
  1492. * @author Dmitry (dio) Levashov
  1493. * @author Alexey Sukhotin
  1494. * @author nao-pon
  1495. * @author Troex Nevelin
  1496. **/
  1497. public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
  1498. if ($this->commandDisabled('resize')) {
  1499. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1500. }
  1501. if (($file = $this->file($hash)) == false) {
  1502. return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
  1503. }
  1504. if (!$file['write'] || !$file['read']) {
  1505. return $this->setError(elFinder::ERROR_PERM_DENIED);
  1506. }
  1507. $path = $this->decode($hash);
  1508. if (!$this->canResize($path, $file)) {
  1509. return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
  1510. }
  1511. switch($mode) {
  1512. case 'propresize':
  1513. $result = $this->imgResize($path, $width, $height, true, true);
  1514. break;
  1515. case 'crop':
  1516. $result = $this->imgCrop($path, $width, $height, $x, $y);
  1517. break;
  1518. case 'fitsquare':
  1519. $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']));
  1520. break;
  1521. case 'rotate':
  1522. $result = $this->imgRotate($path, $degree, ($bg ? $bg : $this->options['tmbBgColor']));
  1523. break;
  1524. default:
  1525. $result = $this->imgResize($path, $width, $height, false, true);
  1526. break;
  1527. }
  1528. if ($result) {
  1529. $this->rmTmb($file);
  1530. $this->clearcache();
  1531. return $this->stat($path);
  1532. }
  1533. return false;
  1534. }
  1535. /**
  1536. * Remove file/dir
  1537. *
  1538. * @param string $hash file hash
  1539. * @return bool
  1540. * @author Dmitry (dio) Levashov
  1541. **/
  1542. public function rm($hash) {
  1543. return $this->commandDisabled('rm')
  1544. ? array(elFinder::ERROR_ACCESS_DENIED)
  1545. : $this->remove($this->decode($hash));
  1546. }
  1547. /**
  1548. * Search files
  1549. *
  1550. * @param string $q search string
  1551. * @param array $mimes
  1552. * @return array
  1553. * @author Dmitry (dio) Levashov
  1554. **/
  1555. public function search($q, $mimes) {
  1556. return $this->doSearch($this->root, $q, $mimes);
  1557. }
  1558. /**
  1559. * Return image dimensions
  1560. *
  1561. * @param string $hash file hash
  1562. * @return array
  1563. * @author Dmitry (dio) Levashov
  1564. **/
  1565. public function dimensions($hash) {
  1566. if (($file = $this->file($hash)) == false) {
  1567. return false;
  1568. }
  1569. return $this->_dimensions($this->decode($hash), $file['mime']);
  1570. }
  1571. /**
  1572. * Save error message
  1573. *
  1574. * @param array error
  1575. * @return false
  1576. * @author Dmitry(dio) Levashov
  1577. **/
  1578. protected function setError($error) {
  1579. $this->error = array();
  1580. foreach (func_get_args() as $err) {
  1581. if (is_array($err)) {
  1582. $this->error = array_merge($this->error, $err);
  1583. } else {
  1584. $this->error[] = $err;
  1585. }
  1586. }
  1587. // $this->error = is_array($error) ? $error : func_get_args();
  1588. return false;
  1589. }
  1590. /*********************************************************************/
  1591. /* FS API */
  1592. /*********************************************************************/
  1593. /***************** paths *******************/
  1594. /**
  1595. * Encode path into hash
  1596. *
  1597. * @param string file path
  1598. * @return string
  1599. * @author Dmitry (dio) Levashov
  1600. * @author Troex Nevelin
  1601. **/
  1602. protected function encode($path) {
  1603. if ($path !== '') {
  1604. // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
  1605. $p = $this->_relpath($path);
  1606. // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
  1607. if ($p === '') {
  1608. $p = DIRECTORY_SEPARATOR;
  1609. }
  1610. // TODO crypt path and return hash
  1611. $hash = $this->crypt($p);
  1612. // hash is used as id in HTML that means it must contain vaild chars
  1613. // make base64 html safe and append prefix in begining
  1614. $hash = strtr(base64_encode($hash), '+/=', '-_.');
  1615. // remove dots '.' at the end, before it was '=' in base64
  1616. $hash = rtrim($hash, '.');
  1617. // append volume id to make hash unique
  1618. return $this->id.$hash;
  1619. }
  1620. }
  1621. /**
  1622. * Decode path from hash
  1623. *
  1624. * @param string file hash
  1625. * @return string
  1626. * @author Dmitry (dio) Levashov
  1627. * @author Troex Nevelin
  1628. **/
  1629. protected function decode($hash) {
  1630. if (strpos($hash, $this->id) === 0) {
  1631. // cut volume id after it was prepended in encode
  1632. $h = substr($hash, strlen($this->id));
  1633. // replace HTML safe base64 to normal
  1634. $h = base64_decode(strtr($h, '-_.', '+/='));
  1635. // TODO uncrypt hash and return path
  1636. $path = $this->uncrypt($h);
  1637. // append ROOT to path after it was cut in encode
  1638. return $this->_abspath($path);//$this->root.($path == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR.$path);
  1639. }
  1640. }
  1641. /**
  1642. * Return crypted path
  1643. * Not implemented
  1644. *
  1645. * @param string path
  1646. * @return mixed
  1647. * @author Dmitry (dio) Levashov
  1648. **/
  1649. protected function crypt($path) {
  1650. return $path;
  1651. }
  1652. /**
  1653. * Return uncrypted path
  1654. * Not implemented
  1655. *
  1656. * @param mixed hash
  1657. * @return mixed
  1658. * @author Dmitry (dio) Levashov
  1659. **/
  1660. protected function uncrypt($hash) {
  1661. return $hash;
  1662. }
  1663. /**
  1664. * Validate file name based on $this->options['acceptedName'] regexp
  1665. *
  1666. * @param string $name file name
  1667. * @return bool
  1668. * @author Dmitry (dio) Levashov
  1669. **/
  1670. protected function nameAccepted($name) {
  1671. if ($this->nameValidator) {
  1672. if (function_exists($this->nameValidator)) {
  1673. $f = $this->nameValidator;
  1674. return $f($name);
  1675. }
  1676. return preg_match($this->nameValidator, $name);
  1677. }
  1678. return true;
  1679. }
  1680. /**
  1681. * Return new unique name based on file name and suffix
  1682. *
  1683. * @param string $path file path
  1684. * @param string $suffix suffix append to name
  1685. * @return string
  1686. * @author Dmitry (dio) Levashov
  1687. **/
  1688. public function uniqueName($dir, $name, $suffix = ' copy', $checkNum=true) {
  1689. $ext = '';
  1690. if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
  1691. $ext = '.'.$m[1];
  1692. $name = substr($name, 0, strlen($name)-strlen($m[0]));
  1693. }
  1694. if ($checkNum && preg_match('/('.$suffix.')(\d*)$/i', $name, $m)) {
  1695. $i = (int)$m[2];
  1696. $name = substr($name, 0, strlen($name)-strlen($m[2]));
  1697. } else {
  1698. $i = 1;
  1699. $name .= $suffix;
  1700. }
  1701. $max = $i+100000;
  1702. while ($i <= $max) {
  1703. $n = $name.($i > 0 ? $i : '').$ext;
  1704. if (!$this->stat($this->_joinPath($dir, $n))) {
  1705. $this->clearcache();
  1706. return $n;
  1707. }
  1708. $i++;
  1709. }
  1710. return $name.md5($dir).$ext;
  1711. }
  1712. /*********************** file stat *********************/
  1713. /**
  1714. * Check file attribute
  1715. *
  1716. * @param string $path file path
  1717. * @param string $name attribute name (read|write|locked|hidden)
  1718. * @param bool $val attribute value returned by file system
  1719. * @return bool
  1720. * @author Dmitry (dio) Levashov
  1721. **/
  1722. protected function attr($path, $name, $val=null) {
  1723. if (!isset($this->defaults[$name])) {
  1724. return false;
  1725. }
  1726. $perm = null;
  1727. if ($this->access) {
  1728. if (is_array($this->access)) {
  1729. $obj = $this->access[0];
  1730. $method = $this->access[1];
  1731. $perm = $obj->{$method}($name, $path, $this->options['accessControlData'], $this);
  1732. } else {
  1733. $func = $this->access;
  1734. $perm = $func($name, $path, $this->options['accessControlData'], $this);
  1735. }
  1736. if ($perm !== null) {
  1737. return !!$perm;
  1738. }
  1739. }
  1740. if ($this->separator != '/') {
  1741. $path = str_replace($this->separator, '/', $this->_relpath($path));
  1742. } else {
  1743. $path = $this->_relpath($path);
  1744. }
  1745. $path = '/'.$path;
  1746. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1747. $attrs = $this->attributes[$i];
  1748. if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $path)) {
  1749. $perm = $attrs[$name];
  1750. }
  1751. }
  1752. return $perm === null ? (is_null($val)? $this->defaults[$name] : $val) : !!$perm;
  1753. }
  1754. /**
  1755. * Return true if file with given name can be created in given folder.
  1756. *
  1757. * @param string $dir parent dir path
  1758. * @param string $name new file name
  1759. * @return bool
  1760. * @author Dmitry (dio) Levashov
  1761. **/
  1762. protected function allowCreate($dir, $name) {
  1763. $path = $this->_joinPath($dir, $name);
  1764. $perm = null;
  1765. if ($this->access) {
  1766. if (is_array($this->access)) {
  1767. $obj = $this->access[0];
  1768. $method = $this->access[1];
  1769. $perm = $obj->{$method}('write', $path, $this->options['accessControlData'], $this);
  1770. } else {
  1771. $func = $this->access;
  1772. $perm = $func('write', $path, $this->options['accessControlData'], $this);
  1773. }
  1774. if ($perm !== null) {
  1775. return !!$perm;
  1776. }
  1777. }
  1778. $testPath = $this->separator.$this->_relpath($path);
  1779. for ($i = 0, $c = count($this->attributes); $i < $c; $i++) {
  1780. $attrs = $this->attributes[$i];
  1781. if (isset($attrs['write']) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $testPath)) {
  1782. $perm = $attrs['write'];
  1783. }
  1784. }
  1785. return $perm === null ? true : $perm;
  1786. }
  1787. /**
  1788. * Return fileinfo
  1789. *
  1790. * @param string $path file cache
  1791. * @return array
  1792. * @author Dmitry (dio) Levashov
  1793. **/
  1794. protected function stat($path) {
  1795. if ($path === false) {
  1796. return false;
  1797. }
  1798. return isset($this->cache[$path])
  1799. ? $this->cache[$path]
  1800. : $this->updateCache($path, $this->_stat($path));
  1801. }
  1802. /**
  1803. * Put file stat in cache and return it
  1804. *
  1805. * @param string $path file path
  1806. * @param array $stat file stat
  1807. * @return array
  1808. * @author Dmitry (dio) Levashov
  1809. **/
  1810. protected function updateCache($path, $stat) {
  1811. if (empty($stat) || !is_array($stat)) {
  1812. return $this->cache[$path] = array();
  1813. }
  1814. $stat['hash'] = $this->encode($path);
  1815. $root = $path == $this->root;
  1816. if ($root) {
  1817. $stat['volumeid'] = $this->id;
  1818. if ($this->rootName) {
  1819. $stat['name'] = $this->rootName;
  1820. }
  1821. } else {
  1822. if (!isset($stat['name']) || !strlen($stat['name'])) {
  1823. $stat['name'] = $this->_basename($path);
  1824. }
  1825. if (empty($stat['phash'])) {
  1826. $stat['phash'] = $this->encode($this->_dirname($path));
  1827. }
  1828. }
  1829. // fix name if required
  1830. if ($this->options['utf8fix'] && $this->options['utf8patterns'] && $this->options['utf8replace']) {
  1831. $stat['name'] = json_decode(str_replace($this->options['utf8patterns'], $this->options['utf8replace'], json_encode($stat['name'])));
  1832. }
  1833. if (empty($stat['mime'])) {
  1834. $stat['mime'] = $this->mimetype($stat['name']);
  1835. }
  1836. // @todo move dateformat to client
  1837. // $stat['date'] = isset($stat['ts'])
  1838. // ? $this->formatDate($stat['ts'])
  1839. // : 'unknown';
  1840. if (!isset($stat['size'])) {
  1841. $stat['size'] = 'unknown';
  1842. }
  1843. $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ? !!$stat['read'] : null));
  1844. $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ? !!$stat['write'] : null));
  1845. if ($root) {
  1846. $stat['locked'] = 1;
  1847. } elseif ($this->attr($path, 'locked', !empty($stat['locked']))) {
  1848. $stat['locked'] = 1;
  1849. } else {
  1850. unset($stat['locked']);
  1851. }
  1852. if ($root) {
  1853. unset($stat['hidden']);
  1854. } elseif ($this->attr($path, 'hidden', !empty($stat['hidden']))
  1855. || !$this->mimeAccepted($stat['mime'])) {
  1856. $stat['hidden'] = $root ? 0 : 1;
  1857. } else {
  1858. unset($stat['hidden']);
  1859. }
  1860. if ($stat['read'] && empty($stat['hidden'])) {
  1861. if ($stat['mime'] == 'directory') {
  1862. // for dir - check for subdirs
  1863. if ($this->options['checkSubfolders']) {
  1864. if (isset($stat['dirs'])) {
  1865. if ($stat['dirs']) {
  1866. $stat['dirs'] = 1;
  1867. } else {
  1868. unset($stat['dirs']);
  1869. }
  1870. } elseif (!empty($stat['alias']) && !empty($stat['target'])) {
  1871. $stat['dirs'] = isset($this->cache[$stat['target']])
  1872. ? intval(isset($this->cache[$stat['target']]['dirs']))
  1873. : $this->_subdirs($stat['target']);
  1874. } elseif ($this->_subdirs($path)) {
  1875. $stat['dirs'] = 1;
  1876. }
  1877. } else {
  1878. $stat['dirs'] = 1;
  1879. }
  1880. } else {
  1881. // for files - check for thumbnails
  1882. $p = isset($stat['target']) ? $stat['target'] : $path;
  1883. if ($this->tmbURL && !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) {
  1884. $tmb = $this->gettmb($p, $stat);
  1885. $stat['tmb'] = $tmb ? $tmb : 1;
  1886. }
  1887. }
  1888. }
  1889. if (!empty($stat['alias']) && !empty($stat['target'])) {
  1890. $stat['thash'] = $this->encode($stat['target']);
  1891. unset($stat['target']);
  1892. }
  1893. return $this->cache[$path] = $stat;
  1894. }
  1895. /**
  1896. * Get stat for folder content and put in cache
  1897. *
  1898. * @param string $path
  1899. * @return void
  1900. * @author Dmitry (dio) Levashov
  1901. **/
  1902. protected function cacheDir($path) {
  1903. $this->dirsCache[$path] = array();
  1904. foreach ($this->_scandir($path) as $p) {
  1905. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  1906. $this->dirsCache[$path][] = $p;
  1907. }
  1908. }
  1909. }
  1910. /**
  1911. * Clean cache
  1912. *
  1913. * @return void
  1914. * @author Dmitry (dio) Levashov
  1915. **/
  1916. protected function clearcache() {
  1917. $this->cache = $this->dirsCache = array();
  1918. }
  1919. /**
  1920. * Return file mimetype
  1921. *
  1922. * @param string $path file path
  1923. * @return string
  1924. * @author Dmitry (dio) Levashov
  1925. **/
  1926. protected function mimetype($path, $name = '') {
  1927. $type = '';
  1928. if ($this->mimeDetect == 'finfo') {
  1929. if ($type = @finfo_file($this->finfo, $path)) {
  1930. if ($name === '') {
  1931. $name = $path;
  1932. }
  1933. $ext = (false === $pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1);
  1934. if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip)~', $type)) {
  1935. if (isset(elFinderVolumeDriver::$mimetypes[$ext])) $type = elFinderVolumeDriver::$mimetypes[$ext];
  1936. }
  1937. }
  1938. } elseif ($type == 'mime_content_type') {
  1939. $type = mime_content_type($path);
  1940. } else {
  1941. $type = elFinderVolumeDriver::mimetypeInternalDetect($path);
  1942. }
  1943. $type = explode(';', $type);
  1944. $type = trim($type[0]);
  1945. if (in_array($type, array('application/x-empty', 'inode/x-empty'))) {
  1946. // finfo return this mime for empty files
  1947. $type = 'text/plain';
  1948. } elseif ($type == 'application/x-zip') {
  1949. // http://elrte.org/redmine/issues/163
  1950. $type = 'application/zip';
  1951. }
  1952. return $type == 'unknown' && $this->mimeDetect != 'internal'
  1953. ? elFinderVolumeDriver::mimetypeInternalDetect($path)
  1954. : $type;
  1955. }
  1956. /**
  1957. * Detect file mimetype using "internal" method
  1958. *
  1959. * @param string $path file path
  1960. * @return string
  1961. * @author Dmitry (dio) Levashov
  1962. **/
  1963. static protected function mimetypeInternalDetect($path) {
  1964. $pinfo = pathinfo($path);
  1965. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  1966. return isset(elFinderVolumeDriver::$mimetypes[$ext]) ? elFinderVolumeDriver::$mimetypes[$ext] : 'unknown';
  1967. }
  1968. /**
  1969. * Return file/total directory size
  1970. *
  1971. * @param string $path file path
  1972. * @return int
  1973. * @author Dmitry (dio) Levashov
  1974. **/
  1975. protected function countSize($path) {
  1976. $stat = $this->stat($path);
  1977. if (empty($stat) || !$stat['read'] || !empty($stat['hidden'])) {
  1978. return 'unknown';
  1979. }
  1980. if ($stat['mime'] != 'directory') {
  1981. return $stat['size'];
  1982. }
  1983. $subdirs = $this->options['checkSubfolders'];
  1984. $this->options['checkSubfolders'] = true;
  1985. $result = 0;
  1986. foreach ($this->getScandir($path) as $stat) {
  1987. $size = $stat['mime'] == 'directory' && $stat['read']
  1988. ? $this->countSize($this->_joinPath($path, $stat['name']))
  1989. : (isset($stat['size']) ? intval($stat['size']) : 0);
  1990. if ($size > 0) {
  1991. $result += $size;
  1992. }
  1993. }
  1994. $this->options['checkSubfolders'] = $subdirs;
  1995. return $result;
  1996. }
  1997. /**
  1998. * Return true if all mimes is directory or files
  1999. *
  2000. * @param string $mime1 mimetype
  2001. * @param string $mime2 mimetype
  2002. * @return bool
  2003. * @author Dmitry (dio) Levashov
  2004. **/
  2005. protected function isSameType($mime1, $mime2) {
  2006. return ($mime1 == 'directory' && $mime1 == $mime2) || ($mime1 != 'directory' && $mime2 != 'directory');
  2007. }
  2008. /**
  2009. * If file has required attr == $val - return file path,
  2010. * If dir has child with has required attr == $val - return child path
  2011. *
  2012. * @param string $path file path
  2013. * @param string $attr attribute name
  2014. * @param bool $val attribute value
  2015. * @return string|false
  2016. * @author Dmitry (dio) Levashov
  2017. **/
  2018. protected function closestByAttr($path, $attr, $val) {
  2019. $stat = $this->stat($path);
  2020. if (empty($stat)) {
  2021. return false;
  2022. }
  2023. $v = isset($stat[$attr]) ? $stat[$attr] : false;
  2024. if ($v == $val) {
  2025. return $path;
  2026. }
  2027. return $stat['mime'] == 'directory'
  2028. ? $this->childsByAttr($path, $attr, $val)
  2029. : false;
  2030. }
  2031. /**
  2032. * Return first found children with required attr == $val
  2033. *
  2034. * @param string $path file path
  2035. * @param string $attr attribute name
  2036. * @param bool $val attribute value
  2037. * @return string|false
  2038. * @author Dmitry (dio) Levashov
  2039. **/
  2040. protected function childsByAttr($path, $attr, $val) {
  2041. foreach ($this->_scandir($path) as $p) {
  2042. if (($_p = $this->closestByAttr($p, $attr, $val)) != false) {
  2043. return $_p;
  2044. }
  2045. }
  2046. return false;
  2047. }
  2048. /***************** get content *******************/
  2049. /**
  2050. * Return required dir's files info.
  2051. * If onlyMimes is set - return only dirs and files of required mimes
  2052. *
  2053. * @param string $path dir path
  2054. * @return array
  2055. * @author Dmitry (dio) Levashov
  2056. **/
  2057. protected function getScandir($path) {
  2058. $files = array();
  2059. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2060. foreach ($this->dirsCache[$path] as $p) {
  2061. if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
  2062. $files[] = $stat;
  2063. }
  2064. }
  2065. return $files;
  2066. }
  2067. /**
  2068. * Return subdirs tree
  2069. *
  2070. * @param string $path parent dir path
  2071. * @param int $deep tree deep
  2072. * @return array
  2073. * @author Dmitry (dio) Levashov
  2074. **/
  2075. protected function gettree($path, $deep, $exclude='') {
  2076. $dirs = array();
  2077. !isset($this->dirsCache[$path]) && $this->cacheDir($path);
  2078. foreach ($this->dirsCache[$path] as $p) {
  2079. $stat = $this->stat($p);
  2080. if ($stat && empty($stat['hidden']) && $p != $exclude && $stat['mime'] == 'directory') {
  2081. $dirs[] = $stat;
  2082. if ($deep > 0 && !empty($stat['dirs'])) {
  2083. $dirs = array_merge($dirs, $this->gettree($p, $deep-1));
  2084. }
  2085. }
  2086. }
  2087. return $dirs;
  2088. }
  2089. /**
  2090. * Recursive files search
  2091. *
  2092. * @param string $path dir path
  2093. * @param string $q search string
  2094. * @param array $mimes
  2095. * @return array
  2096. * @author Dmitry (dio) Levashov
  2097. **/
  2098. protected function doSearch($path, $q, $mimes) {
  2099. $result = array();
  2100. foreach($this->_scandir($path) as $p) {
  2101. $stat = $this->stat($p);
  2102. if (!$stat) { // invalid links
  2103. continue;
  2104. }
  2105. if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'])) {
  2106. continue;
  2107. }
  2108. $name = $stat['name'];
  2109. if ($this->stripos($name, $q) !== false) {
  2110. $stat['path'] = $this->_path($p);
  2111. if ($this->URL && !isset($stat['url'])) {
  2112. $stat['url'] = $this->URL . str_replace($this->separator, '/', substr($p, strlen($this->root) + 1));
  2113. }
  2114. $result[] = $stat;
  2115. }
  2116. if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) {
  2117. $result = array_merge($result, $this->doSearch($p, $q, $mimes));
  2118. }
  2119. }
  2120. return $result;
  2121. }
  2122. /********************** manuipulations ******************/
  2123. /**
  2124. * Copy file/recursive copy dir only in current volume.
  2125. * Return new file path or false.
  2126. *
  2127. * @param string $src source path
  2128. * @param string $dst destination dir path
  2129. * @param string $name new file name (optionaly)
  2130. * @return string|false
  2131. * @author Dmitry (dio) Levashov
  2132. **/
  2133. protected function copy($src, $dst, $name) {
  2134. $srcStat = $this->stat($src);
  2135. $this->clearcache();
  2136. if (!empty($srcStat['thash'])) {
  2137. $target = $this->decode($srcStat['thash']);
  2138. $stat = $this->stat($target);
  2139. $this->clearcache();
  2140. return $stat && $this->_symlink($target, $dst, $name)
  2141. ? $this->_joinPath($dst, $name)
  2142. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2143. }
  2144. if ($srcStat['mime'] == 'directory') {
  2145. $test = $this->stat($this->_joinPath($dst, $name));
  2146. if (($test && $test['mime'] != 'directory') || !$this->_mkdir($dst, $name)) {
  2147. return $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2148. }
  2149. $dst = $this->_joinPath($dst, $name);
  2150. foreach ($this->getScandir($src) as $stat) {
  2151. if (empty($stat['hidden'])) {
  2152. $name = $stat['name'];
  2153. if (!$this->copy($this->_joinPath($src, $name), $dst, $name)) {
  2154. $this->remove($dst, true); // fall back
  2155. return false;
  2156. }
  2157. }
  2158. }
  2159. $this->clearcache();
  2160. return $dst;
  2161. }
  2162. return $this->_copy($src, $dst, $name)
  2163. ? $this->_joinPath($dst, $name)
  2164. : $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  2165. }
  2166. /**
  2167. * Move file
  2168. * Return new file path or false.
  2169. *
  2170. * @param string $src source path
  2171. * @param string $dst destination dir path
  2172. * @param string $name new file name
  2173. * @return string|false
  2174. * @author Dmitry (dio) Levashov
  2175. **/
  2176. protected function move($src, $dst, $name) {
  2177. $stat = $this->stat($src);
  2178. $stat['realpath'] = $src;
  2179. $this->rmTmb($stat); // can not do rmTmb() after _move()
  2180. $this->clearcache();
  2181. if ($this->_move($src, $dst, $name)) {
  2182. $this->removed[] = $stat;
  2183. return $this->_joinPath($dst, $name);
  2184. }
  2185. return $this->setError(elFinder::ERROR_MOVE, $this->_path($src));
  2186. }
  2187. /**
  2188. * Copy file from another volume.
  2189. * Return new file path or false.
  2190. *
  2191. * @param Object $volume source volume
  2192. * @param string $src source file hash
  2193. * @param string $destination destination dir path
  2194. * @param string $name file name
  2195. * @return string|false
  2196. * @author Dmitry (dio) Levashov
  2197. **/
  2198. protected function copyFrom($volume, $src, $destination, $name) {
  2199. if (($source = $volume->file($src)) == false) {
  2200. return $this->setError(elFinder::ERROR_COPY, '#'.$src, $volume->error());
  2201. }
  2202. $errpath = $volume->path($src);
  2203. if (!$this->nameAccepted($source['name'])) {
  2204. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
  2205. }
  2206. if (!$source['read']) {
  2207. return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
  2208. }
  2209. if ($source['mime'] == 'directory') {
  2210. $stat = $this->stat($this->_joinPath($destination, $name));
  2211. $this->clearcache();
  2212. if ((!$stat || $stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
  2213. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2214. }
  2215. $path = $this->_joinPath($destination, $name);
  2216. foreach ($volume->scandir($src) as $entr) {
  2217. if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
  2218. return false;
  2219. }
  2220. }
  2221. } else {
  2222. // $mime = $source['mime'];
  2223. // $w = $h = 0;
  2224. if (($dim = $volume->dimensions($src))) {
  2225. $s = explode('x', $dim);
  2226. $source['width'] = $s[0];
  2227. $source['height'] = $s[1];
  2228. }
  2229. if (($fp = $volume->open($src)) == false
  2230. || ($path = $this->_save($fp, $destination, $name, $source)) == false) {
  2231. $fp && $volume->close($fp, $src);
  2232. return $this->setError(elFinder::ERROR_COPY, $errpath);
  2233. }
  2234. $volume->close($fp, $src);
  2235. }
  2236. return $path;
  2237. }
  2238. /**
  2239. * Remove file/ recursive remove dir
  2240. *
  2241. * @param string $path file path
  2242. * @param bool $force try to remove even if file locked
  2243. * @return bool
  2244. * @author Dmitry (dio) Levashov
  2245. **/
  2246. protected function remove($path, $force = false) {
  2247. $stat = $this->stat($path);
  2248. $stat['realpath'] = $path;
  2249. $this->rmTmb($stat);
  2250. $this->clearcache();
  2251. if (empty($stat)) {
  2252. return $this->setError(elFinder::ERROR_RM, $this->_path($path), elFinder::ERROR_FILE_NOT_FOUND);
  2253. }
  2254. if (!$force && !empty($stat['locked'])) {
  2255. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($path));
  2256. }
  2257. if ($stat['mime'] == 'directory') {
  2258. foreach ($this->_scandir($path) as $p) {
  2259. $name = $this->_basename($p);
  2260. if ($name != '.' && $name != '..' && !$this->remove($p)) {
  2261. return false;
  2262. }
  2263. }
  2264. if (!$this->_rmdir($path)) {
  2265. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2266. }
  2267. } else {
  2268. if (!$this->_unlink($path)) {
  2269. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  2270. }
  2271. }
  2272. $this->removed[] = $stat;
  2273. return true;
  2274. }
  2275. /************************* thumbnails **************************/
  2276. /**
  2277. * Return thumbnail file name for required file
  2278. *
  2279. * @param array $stat file stat
  2280. * @return string
  2281. * @author Dmitry (dio) Levashov
  2282. **/
  2283. protected function tmbname($stat) {
  2284. return $stat['hash'].$stat['ts'].'.png';
  2285. }
  2286. /**
  2287. * Return thumnbnail name if exists
  2288. *
  2289. * @param string $path file path
  2290. * @param array $stat file stat
  2291. * @return string|false
  2292. * @author Dmitry (dio) Levashov
  2293. **/
  2294. protected function gettmb($path, $stat) {
  2295. if ($this->tmbURL && $this->tmbPath) {
  2296. // file itself thumnbnail
  2297. if (strpos($path, $this->tmbPath) === 0) {
  2298. return basename($path);
  2299. }
  2300. $name = $this->tmbname($stat);
  2301. if (file_exists($this->tmbPath.DIRECTORY_SEPARATOR.$name)) {
  2302. return $name;
  2303. }
  2304. }
  2305. return false;
  2306. }
  2307. /**
  2308. * Return true if thumnbnail for required file can be created
  2309. *
  2310. * @param string $path thumnbnail path
  2311. * @param array $stat file stat
  2312. * @return string|bool
  2313. * @author Dmitry (dio) Levashov
  2314. **/
  2315. protected function canCreateTmb($path, $stat) {
  2316. return $this->tmbPathWritable
  2317. && strpos($path, $this->tmbPath) === false // do not create thumnbnail for thumnbnail
  2318. && $this->imgLib
  2319. && strpos($stat['mime'], 'image') === 0
  2320. && ($this->imgLib == 'gd' ? $stat['mime'] == 'image/jpeg' || $stat['mime'] == 'image/png' || $stat['mime'] == 'image/gif' : true);
  2321. }
  2322. /**
  2323. * Return true if required file can be resized.
  2324. * By default - the same as canCreateTmb
  2325. *
  2326. * @param string $path thumnbnail path
  2327. * @param array $stat file stat
  2328. * @return string|bool
  2329. * @author Dmitry (dio) Levashov
  2330. **/
  2331. protected function canResize($path, $stat) {
  2332. return $this->canCreateTmb($path, $stat);
  2333. }
  2334. /**
  2335. * Create thumnbnail and return it's URL on success
  2336. *
  2337. * @param string $path file path
  2338. * @param string $mime file mime type
  2339. * @return string|false
  2340. * @author Dmitry (dio) Levashov
  2341. **/
  2342. protected function createTmb($path, $stat) {
  2343. if (!$stat || !$this->canCreateTmb($path, $stat)) {
  2344. return false;
  2345. }
  2346. $name = $this->tmbname($stat);
  2347. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$name;
  2348. // copy image into tmbPath so some drivers does not store files on local fs
  2349. if (($src = $this->_fopen($path, 'rb')) == false) {
  2350. return false;
  2351. }
  2352. if (($trg = fopen($tmb, 'wb')) == false) {
  2353. $this->_fclose($src, $path);
  2354. return false;
  2355. }
  2356. while (!feof($src)) {
  2357. fwrite($trg, fread($src, 8192));
  2358. }
  2359. $this->_fclose($src, $path);
  2360. fclose($trg);
  2361. $result = false;
  2362. $tmbSize = $this->tmbSize;
  2363. if (($s = getimagesize($tmb)) == false) {
  2364. return false;
  2365. }
  2366. /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  2367. if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
  2368. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2369. } else {
  2370. if ($this->options['tmbCrop']) {
  2371. /* Resize and crop if image bigger than thumbnail */
  2372. if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize) ) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
  2373. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
  2374. }
  2375. if (($s = getimagesize($tmb)) != false) {
  2376. $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize)/2) : 0;
  2377. $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize)/2) : 0;
  2378. $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
  2379. }
  2380. } else {
  2381. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
  2382. }
  2383. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png' );
  2384. }
  2385. if (!$result) {
  2386. unlink($tmb);
  2387. return false;
  2388. }
  2389. return $name;
  2390. }
  2391. /**
  2392. * Resize image
  2393. *
  2394. * @param string $path image file
  2395. * @param int $width new width
  2396. * @param int $height new height
  2397. * @param bool $keepProportions crop image
  2398. * @param bool $resizeByBiggerSide resize image based on bigger side if true
  2399. * @param string $destformat image destination format
  2400. * @return string|false
  2401. * @author Dmitry (dio) Levashov
  2402. * @author Alexey Sukhotin
  2403. **/
  2404. protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
  2405. if (($s = @getimagesize($path)) == false) {
  2406. return false;
  2407. }
  2408. $result = false;
  2409. list($size_w, $size_h) = array($width, $height);
  2410. if ($keepProportions == true) {
  2411. list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
  2412. /* Calculating image scale width and height */
  2413. $xscale = $orig_w / $new_w;
  2414. $yscale = $orig_h / $new_h;
  2415. /* Resizing by biggest side */
  2416. if ($resizeByBiggerSide) {
  2417. if ($orig_w > $orig_h) {
  2418. $size_h = $orig_h * $width / $orig_w;
  2419. $size_w = $width;
  2420. } else {
  2421. $size_w = $orig_w * $height / $orig_h;
  2422. $size_h = $height;
  2423. }
  2424. } else {
  2425. if ($orig_w > $orig_h) {
  2426. $size_w = $orig_w * $height / $orig_h;
  2427. $size_h = $height;
  2428. } else {
  2429. $size_h = $orig_h * $width / $orig_w;
  2430. $size_w = $width;
  2431. }
  2432. }
  2433. }
  2434. switch ($this->imgLib) {
  2435. case 'imagick':
  2436. try {
  2437. $img = new imagick($path);
  2438. } catch (Exception $e) {
  2439. return false;
  2440. }
  2441. $img->resizeImage($size_w, $size_h, Imagick::FILTER_LANCZOS, true);
  2442. $result = $img->writeImage($path);
  2443. return $result ? $path : false;
  2444. break;
  2445. case 'gd':
  2446. $img = self::gdImageCreate($path,$s['mime']);
  2447. if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
  2448. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2449. if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
  2450. return false;
  2451. }
  2452. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2453. imagedestroy($img);
  2454. imagedestroy($tmp);
  2455. return $result ? $path : false;
  2456. }
  2457. break;
  2458. }
  2459. return false;
  2460. }
  2461. /**
  2462. * Crop image
  2463. *
  2464. * @param string $path image file
  2465. * @param int $width crop width
  2466. * @param int $height crop height
  2467. * @param bool $x crop left offset
  2468. * @param bool $y crop top offset
  2469. * @param string $destformat image destination format
  2470. * @return string|false
  2471. * @author Dmitry (dio) Levashov
  2472. * @author Alexey Sukhotin
  2473. **/
  2474. protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
  2475. if (($s = @getimagesize($path)) == false) {
  2476. return false;
  2477. }
  2478. $result = false;
  2479. switch ($this->imgLib) {
  2480. case 'imagick':
  2481. try {
  2482. $img = new imagick($path);
  2483. } catch (Exception $e) {
  2484. return false;
  2485. }
  2486. $img->cropImage($width, $height, $x, $y);
  2487. $result = $img->writeImage($path);
  2488. return $result ? $path : false;
  2489. break;
  2490. case 'gd':
  2491. $img = self::gdImageCreate($path,$s['mime']);
  2492. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2493. self::gdImageBackground($tmp,$this->options['tmbBgColor']);
  2494. $size_w = $width;
  2495. $size_h = $height;
  2496. if ($s[0] < $width || $s[1] < $height) {
  2497. $size_w = $s[0];
  2498. $size_h = $s[1];
  2499. }
  2500. if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
  2501. return false;
  2502. }
  2503. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2504. imagedestroy($img);
  2505. imagedestroy($tmp);
  2506. return $result ? $path : false;
  2507. }
  2508. break;
  2509. }
  2510. return false;
  2511. }
  2512. /**
  2513. * Put image to square
  2514. *
  2515. * @param string $path image file
  2516. * @param int $width square width
  2517. * @param int $height square height
  2518. * @param int $align reserved
  2519. * @param int $valign reserved
  2520. * @param string $bgcolor square background color in #rrggbb format
  2521. * @param string $destformat image destination format
  2522. * @return string|false
  2523. * @author Dmitry (dio) Levashov
  2524. * @author Alexey Sukhotin
  2525. **/
  2526. protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null) {
  2527. if (($s = @getimagesize($path)) == false) {
  2528. return false;
  2529. }
  2530. $result = false;
  2531. /* Coordinates for image over square aligning */
  2532. $y = ceil(abs($height - $s[1]) / 2);
  2533. $x = ceil(abs($width - $s[0]) / 2);
  2534. switch ($this->imgLib) {
  2535. case 'imagick':
  2536. try {
  2537. $img = new imagick($path);
  2538. } catch (Exception $e) {
  2539. return false;
  2540. }
  2541. $img1 = new Imagick();
  2542. $img1->newImage($width, $height, new ImagickPixel($bgcolor));
  2543. $img1->setImageColorspace($img->getImageColorspace());
  2544. $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
  2545. $img1->compositeImage( $img, imagick::COMPOSITE_OVER, $x, $y );
  2546. $result = $img1->writeImage($path);
  2547. return $result ? $path : false;
  2548. break;
  2549. case 'gd':
  2550. $img = self::gdImageCreate($path,$s['mime']);
  2551. if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
  2552. self::gdImageBackground($tmp,$bgcolor);
  2553. if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
  2554. return false;
  2555. }
  2556. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2557. imagedestroy($img);
  2558. imagedestroy($tmp);
  2559. return $result ? $path : false;
  2560. }
  2561. break;
  2562. }
  2563. return false;
  2564. }
  2565. /**
  2566. * Rotate image
  2567. *
  2568. * @param string $path image file
  2569. * @param int $degree rotete degrees
  2570. * @param string $bgcolor square background color in #rrggbb format
  2571. * @param string $destformat image destination format
  2572. * @return string|false
  2573. * @author nao-pon
  2574. * @author Troex Nevelin
  2575. **/
  2576. protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) {
  2577. if (($s = @getimagesize($path)) == false) {
  2578. return false;
  2579. }
  2580. $result = false;
  2581. switch ($this->imgLib) {
  2582. case 'imagick':
  2583. try {
  2584. $img = new imagick($path);
  2585. } catch (Exception $e) {
  2586. return false;
  2587. }
  2588. $img->rotateImage(new ImagickPixel($bgcolor), $degree);
  2589. $result = $img->writeImage($path);
  2590. return $result ? $path : false;
  2591. break;
  2592. case 'gd':
  2593. $img = self::gdImageCreate($path,$s['mime']);
  2594. $degree = 360 - $degree;
  2595. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2596. $bgcolor = imagecolorallocate($img, $r, $g, $b);
  2597. $tmp = imageRotate($img, $degree, (int)$bgcolor);
  2598. $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
  2599. imageDestroy($img);
  2600. imageDestroy($tmp);
  2601. return $result ? $path : false;
  2602. break;
  2603. }
  2604. return false;
  2605. }
  2606. /**
  2607. * Execute shell command
  2608. *
  2609. * @param string $command command line
  2610. * @param array $output stdout strings
  2611. * @param array $return_var process exit code
  2612. * @param array $error_output stderr strings
  2613. * @return int exit code
  2614. * @author Alexey Sukhotin
  2615. **/
  2616. protected function procExec($command , array &$output = null, &$return_var = -1, array &$error_output = null) {
  2617. $descriptorspec = array(
  2618. 0 => array("pipe", "r"), // stdin
  2619. 1 => array("pipe", "w"), // stdout
  2620. 2 => array("pipe", "w") // stderr
  2621. );
  2622. $process = proc_open($command, $descriptorspec, $pipes, null, null);
  2623. if (is_resource($process)) {
  2624. fclose($pipes[0]);
  2625. $tmpout = '';
  2626. $tmperr = '';
  2627. $output = stream_get_contents($pipes[1]);
  2628. $error_output = stream_get_contents($pipes[2]);
  2629. fclose($pipes[1]);
  2630. fclose($pipes[2]);
  2631. $return_var = proc_close($process);
  2632. }
  2633. return $return_var;
  2634. }
  2635. /**
  2636. * Remove thumbnail, also remove recursively if stat is directory
  2637. *
  2638. * @param string $stat file stat
  2639. * @return void
  2640. * @author Dmitry (dio) Levashov
  2641. * @author Naoki Sawada
  2642. * @author Troex Nevelin
  2643. **/
  2644. protected function rmTmb($stat) {
  2645. if ($stat['mime'] === 'directory') {
  2646. foreach ($this->_scandir($this->decode($stat['hash'])) as $p) {
  2647. $name = $this->_basename($p);
  2648. $name != '.' && $name != '..' && $this->rmTmb($this->stat($p));
  2649. }
  2650. } else if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
  2651. $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$stat['tmb'];
  2652. file_exists($tmb) && @unlink($tmb);
  2653. clearstatcache();
  2654. }
  2655. }
  2656. /**
  2657. * Create an gd image according to the specified mime type
  2658. *
  2659. * @param string $path image file
  2660. * @param string $mime
  2661. * @return gd image resource identifier
  2662. */
  2663. protected function gdImageCreate($path,$mime){
  2664. switch($mime){
  2665. case 'image/jpeg':
  2666. return imagecreatefromjpeg($path);
  2667. case 'image/png':
  2668. return imagecreatefrompng($path);
  2669. case 'image/gif':
  2670. return imagecreatefromgif($path);
  2671. case 'image/xbm':
  2672. return imagecreatefromxbm($path);
  2673. }
  2674. return false;
  2675. }
  2676. /**
  2677. * Output gd image to file
  2678. *
  2679. * @param resource $image gd image resource
  2680. * @param string $filename The path to save the file to.
  2681. * @param string $destformat The Image type to use for $filename
  2682. * @param string $mime The original image mime type
  2683. */
  2684. protected function gdImage($image, $filename, $destformat, $mime ){
  2685. if ($destformat == 'jpg' || ($destformat == null && $mime == 'image/jpeg')) {
  2686. return imagejpeg($image, $filename, 100);
  2687. }
  2688. if ($destformat == 'gif' || ($destformat == null && $mime == 'image/gif')) {
  2689. return imagegif($image, $filename, 7);
  2690. }
  2691. return imagepng($image, $filename, 7);
  2692. }
  2693. /**
  2694. * Assign the proper background to a gd image
  2695. *
  2696. * @param resource $image gd image resource
  2697. * @param string $bgcolor background color in #rrggbb format
  2698. */
  2699. protected function gdImageBackground($image, $bgcolor){
  2700. if( $bgcolor == 'transparent' ){
  2701. imagesavealpha($image,true);
  2702. $bgcolor1 = imagecolorallocatealpha($image, 255, 255, 255, 127);
  2703. }else{
  2704. list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
  2705. $bgcolor1 = imagecolorallocate($image, $r, $g, $b);
  2706. }
  2707. imagefill($image, 0, 0, $bgcolor1);
  2708. }
  2709. /*********************** misc *************************/
  2710. /**
  2711. * Return smart formatted date
  2712. *
  2713. * @param int $ts file timestamp
  2714. * @return string
  2715. * @author Dmitry (dio) Levashov
  2716. **/
  2717. // protected function formatDate($ts) {
  2718. // if ($ts > $this->today) {
  2719. // return 'Today '.date($this->options['timeFormat'], $ts);
  2720. // }
  2721. //
  2722. // if ($ts > $this->yesterday) {
  2723. // return 'Yesterday '.date($this->options['timeFormat'], $ts);
  2724. // }
  2725. //
  2726. // return date($this->options['dateFormat'], $ts);
  2727. // }
  2728. /**
  2729. * Find position of first occurrence of string in a string with multibyte support
  2730. *
  2731. * @param string $haystack The string being checked.
  2732. * @param string $needle The string to find in haystack.
  2733. * @param int $offset The search offset. If it is not specified, 0 is used.
  2734. * @return int|bool
  2735. * @author Alexey Sukhotin
  2736. **/
  2737. protected function stripos($haystack , $needle , $offset = 0) {
  2738. if (function_exists('mb_stripos')) {
  2739. return mb_stripos($haystack , $needle , $offset);
  2740. } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) {
  2741. return mb_strpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);
  2742. }
  2743. return stripos($haystack , $needle , $offset);
  2744. }
  2745. /**==================================* abstract methods *====================================**/
  2746. /*********************** paths/urls *************************/
  2747. /**
  2748. * Return parent directory path
  2749. *
  2750. * @param string $path file path
  2751. * @return string
  2752. * @author Dmitry (dio) Levashov
  2753. **/
  2754. abstract protected function _dirname($path);
  2755. /**
  2756. * Return file name
  2757. *
  2758. * @param string $path file path
  2759. * @return string
  2760. * @author Dmitry (dio) Levashov
  2761. **/
  2762. abstract protected function _basename($path);
  2763. /**
  2764. * Join dir name and file name and return full path.
  2765. * Some drivers (db) use int as path - so we give to concat path to driver itself
  2766. *
  2767. * @param string $dir dir path
  2768. * @param string $name file name
  2769. * @return string
  2770. * @author Dmitry (dio) Levashov
  2771. **/
  2772. abstract protected function _joinPath($dir, $name);
  2773. /**
  2774. * Return normalized path
  2775. *
  2776. * @param string $path file path
  2777. * @return string
  2778. * @author Dmitry (dio) Levashov
  2779. **/
  2780. abstract protected function _normpath($path);
  2781. /**
  2782. * Return file path related to root dir
  2783. *
  2784. * @param string $path file path
  2785. * @return string
  2786. * @author Dmitry (dio) Levashov
  2787. **/
  2788. abstract protected function _relpath($path);
  2789. /**
  2790. * Convert path related to root dir into real path
  2791. *
  2792. * @param string $path rel file path
  2793. * @return string
  2794. * @author Dmitry (dio) Levashov
  2795. **/
  2796. abstract protected function _abspath($path);
  2797. /**
  2798. * Return fake path started from root dir.
  2799. * Required to show path on client side.
  2800. *
  2801. * @param string $path file path
  2802. * @return string
  2803. * @author Dmitry (dio) Levashov
  2804. **/
  2805. abstract protected function _path($path);
  2806. /**
  2807. * Return true if $path is children of $parent
  2808. *
  2809. * @param string $path path to check
  2810. * @param string $parent parent path
  2811. * @return bool
  2812. * @author Dmitry (dio) Levashov
  2813. **/
  2814. abstract protected function _inpath($path, $parent);
  2815. /**
  2816. * Return stat for given path.
  2817. * Stat contains following fields:
  2818. * - (int) size file size in b. required
  2819. * - (int) ts file modification time in unix time. required
  2820. * - (string) mime mimetype. required for folders, others - optionally
  2821. * - (bool) read read permissions. required
  2822. * - (bool) write write permissions. required
  2823. * - (bool) locked is object locked. optionally
  2824. * - (bool) hidden is object hidden. optionally
  2825. * - (string) alias for symlinks - link target path relative to root path. optionally
  2826. * - (string) target for symlinks - link target path. optionally
  2827. *
  2828. * If file does not exists - returns empty array or false.
  2829. *
  2830. * @param string $path file path
  2831. * @return array|false
  2832. * @author Dmitry (dio) Levashov
  2833. **/
  2834. abstract protected function _stat($path);
  2835. /***************** file stat ********************/
  2836. /**
  2837. * Return true if path is dir and has at least one childs directory
  2838. *
  2839. * @param string $path dir path
  2840. * @return bool
  2841. * @author Dmitry (dio) Levashov
  2842. **/
  2843. abstract protected function _subdirs($path);
  2844. /**
  2845. * Return object width and height
  2846. * Ususaly used for images, but can be realize for video etc...
  2847. *
  2848. * @param string $path file path
  2849. * @param string $mime file mime type
  2850. * @return string
  2851. * @author Dmitry (dio) Levashov
  2852. **/
  2853. abstract protected function _dimensions($path, $mime);
  2854. /******************** file/dir content *********************/
  2855. /**
  2856. * Return files list in directory
  2857. *
  2858. * @param string $path dir path
  2859. * @return array
  2860. * @author Dmitry (dio) Levashov
  2861. **/
  2862. abstract protected function _scandir($path);
  2863. /**
  2864. * Open file and return file pointer
  2865. *
  2866. * @param string $path file path
  2867. * @param bool $write open file for writing
  2868. * @return resource|false
  2869. * @author Dmitry (dio) Levashov
  2870. **/
  2871. abstract protected function _fopen($path, $mode="rb");
  2872. /**
  2873. * Close opened file
  2874. *
  2875. * @param resource $fp file pointer
  2876. * @param string $path file path
  2877. * @return bool
  2878. * @author Dmitry (dio) Levashov
  2879. **/
  2880. abstract protected function _fclose($fp, $path='');
  2881. /******************** file/dir manipulations *************************/
  2882. /**
  2883. * Create dir and return created dir path or false on failed
  2884. *
  2885. * @param string $path parent dir path
  2886. * @param string $name new directory name
  2887. * @return string|bool
  2888. * @author Dmitry (dio) Levashov
  2889. **/
  2890. abstract protected function _mkdir($path, $name);
  2891. /**
  2892. * Create file and return it's path or false on failed
  2893. *
  2894. * @param string $path parent dir path
  2895. * @param string $name new file name
  2896. * @return string|bool
  2897. * @author Dmitry (dio) Levashov
  2898. **/
  2899. abstract protected function _mkfile($path, $name);
  2900. /**
  2901. * Create symlink
  2902. *
  2903. * @param string $source file to link to
  2904. * @param string $targetDir folder to create link in
  2905. * @param string $name symlink name
  2906. * @return bool
  2907. * @author Dmitry (dio) Levashov
  2908. **/
  2909. abstract protected function _symlink($source, $targetDir, $name);
  2910. /**
  2911. * Copy file into another file (only inside one volume)
  2912. *
  2913. * @param string $source source file path
  2914. * @param string $target target dir path
  2915. * @param string $name file name
  2916. * @return bool
  2917. * @author Dmitry (dio) Levashov
  2918. **/
  2919. abstract protected function _copy($source, $targetDir, $name);
  2920. /**
  2921. * Move file into another parent dir.
  2922. * Return new file path or false.
  2923. *
  2924. * @param string $source source file path
  2925. * @param string $target target dir path
  2926. * @param string $name file name
  2927. * @return string|bool
  2928. * @author Dmitry (dio) Levashov
  2929. **/
  2930. abstract protected function _move($source, $targetDir, $name);
  2931. /**
  2932. * Remove file
  2933. *
  2934. * @param string $path file path
  2935. * @return bool
  2936. * @author Dmitry (dio) Levashov
  2937. **/
  2938. abstract protected function _unlink($path);
  2939. /**
  2940. * Remove dir
  2941. *
  2942. * @param string $path dir path
  2943. * @return bool
  2944. * @author Dmitry (dio) Levashov
  2945. **/
  2946. abstract protected function _rmdir($path);
  2947. /**
  2948. * Create new file and write into it from file pointer.
  2949. * Return new file path or false on error.
  2950. *
  2951. * @param resource $fp file pointer
  2952. * @param string $dir target dir path
  2953. * @param string $name file name
  2954. * @param array $stat file stat (required by some virtual fs)
  2955. * @return bool|string
  2956. * @author Dmitry (dio) Levashov
  2957. **/
  2958. abstract protected function _save($fp, $dir, $name, $stat);
  2959. /**
  2960. * Get file contents
  2961. *
  2962. * @param string $path file path
  2963. * @return string|false
  2964. * @author Dmitry (dio) Levashov
  2965. **/
  2966. abstract protected function _getContents($path);
  2967. /**
  2968. * Write a string to a file
  2969. *
  2970. * @param string $path file path
  2971. * @param string $content new file content
  2972. * @return bool
  2973. * @author Dmitry (dio) Levashov
  2974. **/
  2975. abstract protected function _filePutContents($path, $content);
  2976. /**
  2977. * Extract files from archive
  2978. *
  2979. * @param string $path file path
  2980. * @param array $arc archiver options
  2981. * @return bool
  2982. * @author Dmitry (dio) Levashov,
  2983. * @author Alexey Sukhotin
  2984. **/
  2985. abstract protected function _extract($path, $arc);
  2986. /**
  2987. * Create archive and return its path
  2988. *
  2989. * @param string $dir target dir
  2990. * @param array $files files names list
  2991. * @param string $name archive name
  2992. * @param array $arc archiver options
  2993. * @return string|bool
  2994. * @author Dmitry (dio) Levashov,
  2995. * @author Alexey Sukhotin
  2996. **/
  2997. abstract protected function _archive($dir, $files, $name, $arc);
  2998. /**
  2999. * Detect available archivers
  3000. *
  3001. * @return void
  3002. * @author Dmitry (dio) Levashov,
  3003. * @author Alexey Sukhotin
  3004. **/
  3005. abstract protected function _checkArchivers();
  3006. } // END class