PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_k2/lib/elfinder/elFinderVolumeDriver.class.php

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