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

/admin/php/elFinderVolumeDriver.class.php

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