elFinderVolumeFTP.class.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. <?php
  2. /**
  3. * Simple elFinder driver for FTP
  4. *
  5. * @author Dmitry (dio) Levashov
  6. * @author Cem (discofever)
  7. **/
  8. class elFinderVolumeFTP extends elFinderVolumeDriver {
  9. /**
  10. * Driver id
  11. * Must be started from letter and contains [a-z0-9]
  12. * Used as part of volume id
  13. *
  14. * @var string
  15. **/
  16. protected $driverId = 'f';
  17. /**
  18. * FTP Connection Instance
  19. *
  20. * @var ftp
  21. **/
  22. protected $connect = null;
  23. /**
  24. * Directory for tmp files
  25. * If not set driver will try to use tmbDir as tmpDir
  26. *
  27. * @var string
  28. **/
  29. protected $tmpPath = '';
  30. /**
  31. * Last FTP error message
  32. *
  33. * @var string
  34. **/
  35. protected $ftpError = '';
  36. /**
  37. * FTP server output list as ftp on linux
  38. *
  39. * @var bool
  40. **/
  41. protected $ftpOsUnix;
  42. /**
  43. * FTP LIST command option
  44. *
  45. * @var string
  46. */
  47. protected $ftpListOption = '-al';
  48. /**
  49. * Is connected server Pure FTPd?
  50. *
  51. * @var bool
  52. */
  53. protected $isPureFtpd = false;
  54. /**
  55. * Is connected server with FTPS?
  56. *
  57. * @var bool
  58. */
  59. protected $isFTPS = false;
  60. /**
  61. * Tmp folder path
  62. *
  63. * @var string
  64. **/
  65. protected $tmp = '';
  66. /**
  67. * FTP command `MLST` support
  68. *
  69. * @var bool
  70. */
  71. private $MLSTsupprt = false;
  72. /**
  73. * Calling cacheDir() target path with non-MLST
  74. *
  75. * @var string
  76. */
  77. private $cacheDirTarget = '';
  78. /**
  79. * Constructor
  80. * Extend options with required fields
  81. *
  82. * @author Dmitry (dio) Levashov
  83. * @author Cem (DiscoFever)
  84. */
  85. public function __construct() {
  86. $opts = array(
  87. 'host' => 'localhost',
  88. 'user' => '',
  89. 'pass' => '',
  90. 'port' => 21,
  91. 'mode' => 'passive',
  92. 'ssl' => false,
  93. 'path' => '/',
  94. 'timeout' => 20,
  95. 'owner' => true,
  96. 'tmbPath' => '',
  97. 'tmpPath' => '',
  98. 'separator' => '/',
  99. 'dirMode' => 0755,
  100. 'fileMode' => 0644,
  101. 'rootCssClass' => 'elfinder-navbar-root-ftp',
  102. 'ftpListOption' => '-al',
  103. );
  104. $this->options = array_merge($this->options, $opts);
  105. $this->options['mimeDetect'] = 'internal';
  106. }
  107. /**
  108. * Prepare
  109. * Call from elFinder::netmout() before volume->mount()
  110. *
  111. * @param $options
  112. * @return Array
  113. * @author Naoki Sawada
  114. */
  115. public function netmountPrepare($options) {
  116. if (!empty($_REQUEST['encoding']) && iconv('UTF-8', $_REQUEST['encoding'], '') !== false) {
  117. $options['encoding'] = $_REQUEST['encoding'];
  118. if (!empty($_REQUEST['locale']) && setlocale(LC_ALL, $_REQUEST['locale'])) {
  119. setlocale(LC_ALL, elFinder::$locale);
  120. $options['locale'] = $_REQUEST['locale'];
  121. }
  122. }
  123. if (!empty($_REQUEST['FTPS'])) {
  124. $options['ssl'] = true;
  125. }
  126. $options['statOwner'] = true;
  127. $options['allowChmodReadOnly'] = true;
  128. $options['acceptedName'] = '#^[^/\\?*:|"<>]*[^./\\?*:|"<>]$#';
  129. return $options;
  130. }
  131. /*********************************************************************/
  132. /* INIT AND CONFIGURE */
  133. /*********************************************************************/
  134. /**
  135. * Prepare FTP connection
  136. * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn
  137. *
  138. * @return bool
  139. * @author Dmitry (dio) Levashov
  140. * @author Cem (DiscoFever)
  141. **/
  142. protected function init() {
  143. if (!$this->options['host']
  144. || !$this->options['port']) {
  145. return $this->setError('Required options undefined.');
  146. }
  147. if (!$this->options['user']) {
  148. $this->options['user'] = 'anonymous';
  149. $this->options['pass'] = '';
  150. }
  151. if (!$this->options['path']) {
  152. $this->options['path'] = '/';
  153. }
  154. // make ney mount key
  155. $this->netMountKey = md5(join('-', array('ftp', $this->options['host'], $this->options['port'], $this->options['path'], $this->options['user'])));
  156. if (!function_exists('ftp_connect')) {
  157. return $this->setError('FTP extension not loaded.');
  158. }
  159. // remove protocol from host
  160. $scheme = parse_url($this->options['host'], PHP_URL_SCHEME);
  161. if ($scheme) {
  162. $this->options['host'] = substr($this->options['host'], strlen($scheme)+3);
  163. }
  164. // normalize root path
  165. $this->root = $this->options['path'] = $this->_normpath($this->options['path']);
  166. if (empty($this->options['alias'])) {
  167. $this->options['alias'] = $this->options['user'].'@'.$this->options['host'];
  168. // $num = elFinder::$volumesCnt-1;
  169. // $this->options['alias'] = $this->root == '/' || $this->root == '.' ? 'FTP folder '.$num : basename($this->root);
  170. }
  171. $this->rootName = $this->options['alias'];
  172. $this->options['separator'] = '/';
  173. if (is_null($this->options['syncChkAsTs'])) {
  174. $this->options['syncChkAsTs'] = true;
  175. }
  176. if(isset($this->options['ftpListOption'])) {
  177. $this->ftpListOption = $this->options['ftpListOption'];
  178. }
  179. return $this->connect();
  180. }
  181. /**
  182. * Configure after successfull mount.
  183. *
  184. * @return void
  185. * @author Dmitry (dio) Levashov
  186. **/
  187. protected function configure() {
  188. parent::configure();
  189. if (!empty($this->options['tmpPath'])) {
  190. if ((is_dir($this->options['tmpPath']) || mkdir($this->options['tmpPath'], 0755, true)) && is_writable($this->options['tmpPath'])) {
  191. $this->tmp = $this->options['tmpPath'];
  192. }
  193. }
  194. if (!$this->tmp && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
  195. $this->tmp = $tmp;
  196. }
  197. // fallback of $this->tmp
  198. if (!$this->tmp && $this->tmbPathWritable) {
  199. $this->tmp = $this->tmbPath;
  200. }
  201. if (!$this->tmp) {
  202. $this->disabled[] = 'mkfile';
  203. $this->disabled[] = 'paste';
  204. $this->disabled[] = 'duplicate';
  205. $this->disabled[] = 'upload';
  206. $this->disabled[] = 'edit';
  207. $this->disabled[] = 'archive';
  208. $this->disabled[] = 'extract';
  209. }
  210. // echo $this->tmp;
  211. }
  212. /**
  213. * Connect to ftp server
  214. *
  215. * @return bool
  216. * @author Dmitry (dio) Levashov
  217. **/
  218. protected function connect() {
  219. $withSSL = empty($this->options['ssl'])? '' : ' with SSL';
  220. if ($withSSL) {
  221. if (!function_exists('ftp_ssl_connect') || !($this->connect = ftp_ssl_connect($this->options['host'], $this->options['port'], $this->options['timeout']))) {
  222. return $this->setError('Unable to connect to FTP server '.$this->options['host'].$withSSL);
  223. }
  224. $this->isFTPS = true;
  225. } else {
  226. if (!($this->connect = ftp_connect($this->options['host'], $this->options['port'], $this->options['timeout']))) {
  227. return $this->setError('Unable to connect to FTP server '.$this->options['host']);
  228. }
  229. }
  230. if (!ftp_login($this->connect, $this->options['user'], $this->options['pass'])) {
  231. $this->umount();
  232. return $this->setError('Unable to login into '.$this->options['host'].$withSSL);
  233. }
  234. // try switch utf8 mode
  235. if ($this->encoding) {
  236. ftp_raw($this->connect, 'OPTS UTF8 OFF');
  237. } else {
  238. ftp_raw($this->connect, 'OPTS UTF8 ON' );
  239. }
  240. $help = ftp_raw($this->connect, 'HELP');
  241. $this->isPureFtpd = stripos(implode(' ', $help), 'Pure-FTPd') !== false;
  242. if(! $this->isPureFtpd){
  243. // switch off extended passive mode - may be usefull for some servers
  244. // this command, for pure-ftpd, doesn't work and takes a timeout in some pure-ftpd versions
  245. ftp_raw($this->connect, 'epsv4 off' );
  246. }
  247. // enter passive mode if required
  248. $pasv = ($this->options['mode'] == 'passive');
  249. if (! ftp_pasv($this->connect, $pasv)) {
  250. if ($pasv) {
  251. $this->options['mode'] = 'active';
  252. }
  253. }
  254. // enter root folder
  255. if (! ftp_chdir($this->connect, $this->root)
  256. || $this->root != ftp_pwd($this->connect)) {
  257. $this->umount();
  258. return $this->setError('Unable to open root folder.');
  259. }
  260. // check for MLST support
  261. $features = ftp_raw($this->connect, 'FEAT');
  262. if (!is_array($features)) {
  263. $this->umount();
  264. return $this->setError('Server does not support command FEAT.');
  265. }
  266. foreach ($features as $feat) {
  267. if (strpos(trim($feat), 'MLST') === 0) {
  268. $this->MLSTsupprt = true;
  269. break;
  270. }
  271. }
  272. return true;
  273. }
  274. /**
  275. * Call ftp_rawlist with option prefix
  276. *
  277. * @param string $path
  278. * @return array
  279. */
  280. protected function ftpRawList($path) {
  281. if ($this->isPureFtpd) {
  282. $path = str_replace(' ', '\ ', $path);
  283. }
  284. if ($this->ftpListOption) {
  285. $path = $this->ftpListOption . ' ' . $path;
  286. }
  287. $res = ftp_rawlist($this->connect, $path);
  288. if ($res === false) {
  289. $res = array();
  290. }
  291. return $res;
  292. }
  293. /*********************************************************************/
  294. /* FS API */
  295. /*********************************************************************/
  296. /**
  297. * Close opened connection
  298. *
  299. * @return void
  300. * @author Dmitry (dio) Levashov
  301. **/
  302. public function umount() {
  303. $this->connect && ftp_close($this->connect);
  304. }
  305. /**
  306. * Parse line from ftp_rawlist() output and return file stat (array)
  307. *
  308. * @param string $raw line from ftp_rawlist() output
  309. * @param $base
  310. * @param bool $nameOnly
  311. * @return array
  312. * @author Dmitry Levashov
  313. */
  314. protected function parseRaw($raw, $base, $nameOnly = false) {
  315. static $now;
  316. static $lastyear;
  317. if (! $now) {
  318. $now = time();
  319. $lastyear = date('Y') - 1;
  320. }
  321. $info = preg_split("/\s+/", $raw, 9);
  322. $stat = array();
  323. if (!isset($this->ftpOsUnix)) {
  324. $this->ftpOsUnix = !preg_match('/\d/', substr($info[0], 0, 1));
  325. }
  326. if (!$this->ftpOsUnix) {
  327. $info = $this->normalizeRawWindows($raw);
  328. }
  329. if (count($info) < 9 || $info[8] == '.' || $info[8] == '..') {
  330. return false;
  331. }
  332. $name = $info[8];
  333. if (preg_match('|(.+)\-\>(.+)|', $name, $m)) {
  334. $name = trim($m[1]);
  335. // check recursive processing
  336. if ($this->cacheDirTarget && $this->_joinPath($base, $name) !== $this->cacheDirTarget) {
  337. return array();
  338. }
  339. if (!$nameOnly) {
  340. $target = trim($m[2]);
  341. if (substr($target, 0, 1) !== $this->separator) {
  342. $target = $this->getFullPath($target, $base);
  343. }
  344. $target = $this->_normpath($target);
  345. $stat['name'] = $name;
  346. $stat['target'] = $target;
  347. return $stat;
  348. }
  349. }
  350. if ($nameOnly) {
  351. return array('name' => $name);
  352. }
  353. if (is_numeric($info[5]) && !$info[6] && !$info[7]) {
  354. // by normalizeRawWindows()
  355. $stat['ts'] = $info[5];
  356. } else {
  357. $stat['ts'] = strtotime($info[5].' '.$info[6].' '.$info[7]);
  358. if ($stat['ts'] && $stat['ts'] > $now && strpos($info[7], ':') !== false) {
  359. $stat['ts'] = strtotime($info[5].' '.$info[6].' '.$lastyear.' '.$info[7]);
  360. }
  361. if (empty($stat['ts'])) {
  362. $stat['ts'] = strtotime($info[6].' '.$info[5].' '.$info[7]);
  363. if ($stat['ts'] && $stat['ts'] > $now && strpos($info[7], ':') !== false) {
  364. $stat['ts'] = strtotime($info[6].' '.$info[5].' '.$lastyear.' '.$info[7]);
  365. }
  366. }
  367. }
  368. if ($this->options['statOwner']) {
  369. $stat['owner'] = $info[2];
  370. $stat['group'] = $info[3];
  371. $stat['perm'] = substr($info[0], 1);
  372. //
  373. // if not exists owner in LS ftp ==> isowner = true
  374. // if is defined as option : 'owner' => true isowner = true
  375. //
  376. // if exist owner in LS ftp and 'owner' => False isowner = result of owner(file) == user(logged with ftp)
  377. //
  378. $stat['isowner'] = isset($stat['owner']) ? ($this->options['owner'] ? true : ($stat['owner'] == $this->options['user'])) : true;
  379. }
  380. $owner_computed = isset($stat['isowner'])? $stat['isowner'] : $this->options['owner'] ;
  381. $perm = $this->parsePermissions($info[0], $owner_computed );
  382. $stat['name'] = $name;
  383. $stat['mime'] = substr(strtolower($info[0]), 0, 1) == 'd' ? 'directory' : $this->mimetype($stat['name'], true);
  384. $stat['size'] = $stat['mime'] == 'directory' ? 0 : $info[4];
  385. $stat['read'] = $perm['read'];
  386. $stat['write'] = $perm['write'];
  387. return $stat;
  388. }
  389. /**
  390. * Normalize MS-DOS style FTP LIST Raw line
  391. *
  392. * @param string $raw line from FTP LIST (MS-DOS style)
  393. * @return array
  394. * @author Naoki Sawada
  395. **/
  396. protected function normalizeRawWindows($raw) {
  397. $info = array_pad(array(), 9, '');
  398. $item = preg_replace('#\s+#', ' ', trim($raw), 3);
  399. list($date, $time, $size, $name) = explode(' ', $item, 4);
  400. $format = strlen($date) === 8 ? 'm-d-yH:iA' : 'Y-m-dH:i';
  401. $dateObj = DateTime::createFromFormat($format, $date.$time);
  402. $info[5] = strtotime($dateObj->format('Y-m-d H:i'));
  403. $info[8] = $name;
  404. if ($size === '<DIR>') {
  405. $info[4] = 0;
  406. $info[0] = 'drwxr-xr-x';
  407. } else {
  408. $info[4] = (int)$size;
  409. $info[0] = '-rw-r--r--';
  410. }
  411. return $info;
  412. }
  413. /**
  414. * Parse permissions string. Return array(read => true/false, write => true/false)
  415. *
  416. * @param string $perm permissions string 'rwx' + 'rwx' + 'rwx'
  417. * ^ ^ ^
  418. * | | +-> others
  419. * | +---------> group
  420. * +-----------------> owner
  421. * The isowner parameter is computed by the caller.
  422. * If the owner parameter in the options is true, the user is the actual owner of all objects even if che user used in the ftp Login
  423. * is different from the file owner id.
  424. * If the owner parameter is false to understand if the user is the file owner we compare the ftp user with the file owner id.
  425. * @param Boolean $isowner. Tell if the current user is the owner of the object.
  426. * @return string
  427. * @author Dmitry (dio) Levashov
  428. * @author Ugo Vierucci
  429. */
  430. protected function parsePermissions($perm, $isowner = true) {
  431. $res = array();
  432. $parts = array();
  433. for ($i = 0, $l = strlen($perm); $i < $l; $i++) {
  434. $parts[] = substr($perm, $i, 1);
  435. }
  436. $read = ($isowner && $parts[1] == 'r') || $parts[4] == 'r' || $parts[7] == 'r';
  437. return array(
  438. 'read' => $parts[0] == 'd' ? $read && (($isowner && $parts[3] == 'x') || $parts[6] == 'x' || $parts[9] == 'x') : $read,
  439. 'write' => ($isowner && $parts[2] == 'w') || $parts[5] == 'w' || $parts[8] == 'w'
  440. );
  441. }
  442. /**
  443. * Cache dir contents
  444. *
  445. * @param string $path dir path
  446. * @return void
  447. * @author Dmitry Levashov
  448. **/
  449. protected function cacheDir($path) {
  450. $this->dirsCache[$path] = array();
  451. $hasDir = false;
  452. $list = array();
  453. $encPath = $this->convEncIn($path);
  454. foreach ($this->ftpRawList($encPath) as $raw) {
  455. if (($stat = $this->parseRaw($raw, $encPath))) {
  456. $list[] = $stat;
  457. }
  458. }
  459. $list = $this->convEncOut($list);
  460. $prefix = ($path === $this->separator)? $this->separator : $path . $this->separator;
  461. $targets = array();
  462. foreach($list as $stat) {
  463. $p = $prefix . $stat['name'];
  464. if (isset($stat['target'])) {
  465. // stat later
  466. $targets[$stat['name']] = $stat['target'];
  467. } else {
  468. $stat = $this->updateCache($p, $stat);
  469. if (empty($stat['hidden'])) {
  470. if (! $hasDir && $stat['mime'] === 'directory') {
  471. $hasDir = true;
  472. }
  473. $this->dirsCache[$path][] = $p;
  474. }
  475. }
  476. }
  477. // stat link targets
  478. foreach($targets as $name => $target) {
  479. $stat = array();
  480. $stat['name'] = $name;
  481. $p = $prefix . $name;
  482. $cacheDirTarget = $this->cacheDirTarget;
  483. $this->cacheDirTarget = $this->convEncIn($target, true);
  484. if ($tstat = $this->stat($target)) {
  485. $stat['size'] = $tstat['size'];
  486. $stat['alias'] = $target;
  487. $stat['thash'] = $tstat['hash'];
  488. $stat['mime'] = $tstat['mime'];
  489. $stat['read'] = $tstat['read'];
  490. $stat['write'] = $tstat['write'];
  491. if (isset($tstat['ts'])) { $stat['ts'] = $tstat['ts']; }
  492. if (isset($tstat['owner'])) { $stat['owner'] = $tstat['owner']; }
  493. if (isset($tstat['group'])) { $stat['group'] = $tstat['group']; }
  494. if (isset($tstat['perm'])) { $stat['perm'] = $tstat['perm']; }
  495. if (isset($tstat['isowner'])) { $stat['isowner'] = $tstat['isowner']; }
  496. } else {
  497. $stat['mime'] = 'symlink-broken';
  498. $stat['read'] = false;
  499. $stat['write'] = false;
  500. $stat['size'] = 0;
  501. }
  502. $this->cacheDirTarget = $cacheDirTarget;
  503. $stat = $this->updateCache($p, $stat);
  504. if (empty($stat['hidden'])) {
  505. if (! $hasDir && $stat['mime'] === 'directory') {
  506. $hasDir = true;
  507. }
  508. $this->dirsCache[$path][] = $p;
  509. }
  510. }
  511. if (isset($this->sessionCache['subdirs'])) {
  512. $this->sessionCache['subdirs'][$path] = $hasDir;
  513. }
  514. }
  515. /**
  516. * Return ftp transfer mode for file
  517. *
  518. * @param string $path file path
  519. * @return string
  520. * @author Dmitry (dio) Levashov
  521. **/
  522. protected function ftpMode($path) {
  523. return strpos($this->mimetype($path), 'text/') === 0 ? FTP_ASCII : FTP_BINARY;
  524. }
  525. /*********************** paths/urls *************************/
  526. /**
  527. * Return parent directory path
  528. *
  529. * @param string $path file path
  530. * @return string
  531. * @author Naoki Sawada
  532. **/
  533. protected function _dirname($path) {
  534. $parts = explode($this->separator, trim($path, $this->separator));
  535. array_pop($parts);
  536. return $this->separator . join($this->separator, $parts);
  537. }
  538. /**
  539. * Return file name
  540. *
  541. * @param string $path file path
  542. * @return string
  543. * @author Naoki Sawada
  544. **/
  545. protected function _basename($path) {
  546. $parts = explode($this->separator, trim($path, $this->separator));
  547. return array_pop($parts);
  548. }
  549. /**
  550. * Join dir name and file name and retur full path
  551. *
  552. * @param string $dir
  553. * @param string $name
  554. * @return string
  555. * @author Dmitry (dio) Levashov
  556. **/
  557. protected function _joinPath($dir, $name) {
  558. return rtrim($dir, $this->separator).$this->separator.$name;
  559. }
  560. /**
  561. * Return normalized path, this works the same as os.path.normpath() in Python
  562. *
  563. * @param string $path path
  564. * @return string
  565. * @author Troex Nevelin
  566. **/
  567. protected function _normpath($path) {
  568. if (empty($path)) {
  569. $path = '.';
  570. }
  571. // path must be start with /
  572. $path = preg_replace('|^\.\/?|', $this->separator, $path);
  573. $path = preg_replace('/^([^\/])/', "/$1", $path);
  574. if ($path[0] === $this->separator) {
  575. $initial_slashes = true;
  576. } else {
  577. $initial_slashes = false;
  578. }
  579. if (($initial_slashes)
  580. && (strpos($path, '//') === 0)
  581. && (strpos($path, '///') === false)) {
  582. $initial_slashes = 2;
  583. }
  584. $initial_slashes = (int) $initial_slashes;
  585. $comps = explode($this->separator, $path);
  586. $new_comps = array();
  587. foreach ($comps as $comp) {
  588. if (in_array($comp, array('', '.'))) {
  589. continue;
  590. }
  591. if (($comp != '..')
  592. || (!$initial_slashes && !$new_comps)
  593. || ($new_comps && (end($new_comps) == '..'))) {
  594. array_push($new_comps, $comp);
  595. } elseif ($new_comps) {
  596. array_pop($new_comps);
  597. }
  598. }
  599. $comps = $new_comps;
  600. $path = implode($this->separator, $comps);
  601. if ($initial_slashes) {
  602. $path = str_repeat($this->separator, $initial_slashes) . $path;
  603. }
  604. return $path ? $path : '.';
  605. }
  606. /**
  607. * Return file path related to root dir
  608. *
  609. * @param string $path file path
  610. * @return string
  611. * @author Dmitry (dio) Levashov
  612. **/
  613. protected function _relpath($path) {
  614. if ($path === $this->root) {
  615. return '';
  616. } else {
  617. if (strpos($path, $this->root) === 0) {
  618. return ltrim(substr($path, strlen($this->root)), $this->separator);
  619. } else {
  620. // for link
  621. return $path;
  622. }
  623. }
  624. }
  625. /**
  626. * Convert path related to root dir into real path
  627. *
  628. * @param string $path file path
  629. * @return string
  630. * @author Dmitry (dio) Levashov
  631. **/
  632. protected function _abspath($path) {
  633. if ($path === $this->separator) {
  634. return $this->root;
  635. } else {
  636. if ($path[0] === $this->separator) {
  637. // for link
  638. return $path;
  639. } else {
  640. return $this->_joinPath($this->root, $path);
  641. }
  642. }
  643. }
  644. /**
  645. * Return fake path started from root dir
  646. *
  647. * @param string $path file path
  648. * @return string
  649. * @author Dmitry (dio) Levashov
  650. **/
  651. protected function _path($path) {
  652. return $this->rootName.($path == $this->root ? '' : $this->separator.$this->_relpath($path));
  653. }
  654. /**
  655. * Return true if $path is children of $parent
  656. *
  657. * @param string $path path to check
  658. * @param string $parent parent path
  659. * @return bool
  660. * @author Dmitry (dio) Levashov
  661. **/
  662. protected function _inpath($path, $parent) {
  663. return $path == $parent || strpos($path, rtrim($parent, $this->separator) . $this->separator) === 0;
  664. }
  665. /***************** file stat ********************/
  666. /**
  667. * Return stat for given path.
  668. * Stat contains following fields:
  669. * - (int) size file size in b. required
  670. * - (int) ts file modification time in unix time. required
  671. * - (string) mime mimetype. required for folders, others - optionally
  672. * - (bool) read read permissions. required
  673. * - (bool) write write permissions. required
  674. * - (bool) locked is object locked. optionally
  675. * - (bool) hidden is object hidden. optionally
  676. * - (string) alias for symlinks - link target path relative to root path. optionally
  677. * - (string) target for symlinks - link target path. optionally
  678. *
  679. * If file does not exists - returns empty array or false.
  680. *
  681. * @param string $path file path
  682. * @return array|false
  683. * @author Dmitry (dio) Levashov
  684. **/
  685. protected function _stat($path) {
  686. $outPath = $this->convEncOut($path);
  687. if (isset($this->cache[$outPath])) {
  688. return $this->convEncIn($this->cache[$outPath]);
  689. } else {
  690. $this->convEncIn();
  691. }
  692. if (! $this->MLSTsupprt) {
  693. if ($path === $this->root) {
  694. $res = array(
  695. 'name' => $this->root,
  696. 'mime' => 'directory',
  697. 'dirs' => $this->_subdirs($path)
  698. );
  699. if ($this->isMyReload()) {
  700. $ts = 0;
  701. foreach ($this->ftpRawList($path) as $str) {
  702. if (($stat = $this->parseRaw($str, $path))) {
  703. if (! empty($stat['ts'])) {
  704. $ts = max($ts, $stat['ts']);
  705. }
  706. }
  707. }
  708. if ($ts) {
  709. $res['ts'] = $ts;
  710. }
  711. }
  712. return $res;
  713. }
  714. // stat of system root
  715. if ($path === $this->separator) {
  716. $res = array(
  717. 'name' => $this->separator,
  718. 'mime' => 'directory',
  719. 'dirs' => 1
  720. );
  721. $this->cache[$outPath] = $res;
  722. return $res;
  723. }
  724. $pPath = $this->_dirname($path);
  725. if ($this->_inPath($pPath, $this->root)) {
  726. $outPPpath = $this->convEncOut($pPath);
  727. if (! isset($this->dirsCache[$outPPpath])) {
  728. $parentSubdirs = null;
  729. if (isset($this->sessionCache['subdirs']) && isset($this->sessionCache['subdirs'][$outPPpath])) {
  730. $parentSubdirs = $this->sessionCache['subdirs'][$outPPpath ];
  731. }
  732. $this->cacheDir($outPPpath);
  733. if ($parentSubdirs) {
  734. $this->sessionCache['subdirs'][$outPPpath] = $parentSubdirs;
  735. }
  736. }
  737. }
  738. $stat = $this->convEncIn(isset($this->cache[$outPath])? $this->cache[$outPath] : array());
  739. if (! $this->mounted) {
  740. // dispose incomplete cache made by calling `stat` by 'startPath' option
  741. $this->cache = array();
  742. }
  743. return $stat;
  744. }
  745. $raw = ftp_raw($this->connect, 'MLST ' . $path);
  746. if (is_array($raw) && count($raw) > 1 && substr(trim($raw[0]), 0, 1) == 2) {
  747. $parts = explode(';', trim($raw[1]));
  748. array_pop($parts);
  749. $parts = array_map('strtolower', $parts);
  750. $stat = array();
  751. $mode = '';
  752. foreach ($parts as $part) {
  753. list($key, $val) = explode('=', $part, 2);
  754. switch ($key) {
  755. case 'type':
  756. if (strpos($val, 'dir') !== false) {
  757. $stat['mime'] = 'directory';
  758. } else if (strpos($val, 'link') !== false) {
  759. $stat['mime'] = 'symlink';
  760. break(2);
  761. } else {
  762. $stat['mime'] = $this->mimetype($path);
  763. }
  764. break;
  765. case 'size':
  766. $stat['size'] = $val;
  767. break;
  768. case 'modify':
  769. $ts = mktime(intval(substr($val, 8, 2)), intval(substr($val, 10, 2)), intval(substr($val, 12, 2)), intval(substr($val, 4, 2)), intval(substr($val, 6, 2)), substr($val, 0, 4));
  770. $stat['ts'] = $ts;
  771. break;
  772. case 'unix.mode':
  773. $mode = strval($val);
  774. break;
  775. case 'unix.uid':
  776. $stat['owner'] = $val;
  777. break;
  778. case 'unix.gid':
  779. $stat['group'] = $val;
  780. break;
  781. case 'perm':
  782. $val = strtolower($val);
  783. $stat['read'] = (int)preg_match('/e|l|r/', $val);
  784. $stat['write'] = (int)preg_match('/w|m|c/', $val);
  785. if (!preg_match('/f|d/', $val)) {
  786. $stat['locked'] = 1;
  787. }
  788. break;
  789. }
  790. }
  791. if (empty($stat['mime'])) {
  792. return array();
  793. }
  794. // do not use MLST to get stat of symlink
  795. if ($stat['mime'] === 'symlink') {
  796. $this->MLSTsupprt = false;
  797. $res = $this->_stat($path);
  798. $this->MLSTsupprt = true;
  799. return $res;
  800. }
  801. if ($stat['mime'] === 'directory') {
  802. $stat['size'] = 0;
  803. }
  804. if ($mode) {
  805. $stat['perm'] = '';
  806. if ($mode[0] === '0') {
  807. $mode = substr($mode, 1);
  808. }
  809. $perm = array();
  810. for ($i = 0; $i <= 2; $i++) {
  811. $perm[$i] = array(false, false, false);
  812. $n = isset($mode[$i]) ? $mode[$i] : 0;
  813. if ($n - 4 >= 0) {
  814. $perm[$i][0] = true;
  815. $n = $n - 4;
  816. $stat['perm'] .= 'r';
  817. } else {
  818. $stat['perm'] .= '-';
  819. }
  820. if ($n - 2 >= 0) {
  821. $perm[$i][1] = true;
  822. $n = $n - 2;
  823. $stat['perm'] .= 'w';
  824. } else {
  825. $stat['perm'] .= '-';
  826. }
  827. if ($n - 1 == 0) {
  828. $perm[$i][2] = true;
  829. $stat['perm'] .= 'x';
  830. } else {
  831. $stat['perm'] .= '-';
  832. }
  833. }
  834. $stat['perm'] = trim($stat['perm']);
  835. //
  836. // if not exists owner in LS ftp ==> isowner = true
  837. // if is defined as option : 'owner' => true isowner = true
  838. //
  839. // if exist owner in LS ftp and 'owner' => False isowner = result of owner(file) == user(logged with ftp)
  840. $owner_computed = isset($stat['owner']) ? ($this->options['owner'] ? true : ($stat['owner'] == $this->options['user'])) : true;
  841. $read = ($owner_computed && $perm[0][0]) || $perm[1][0] || $perm[2][0];
  842. $stat['read'] = $stat['mime'] == 'directory' ? $read && (($owner_computed && $perm[0][2]) || $perm[1][2] || $perm[2][2]) : $read;
  843. $stat['write'] = ($owner_computed && $perm[0][1]) || $perm[1][1] || $perm[2][1];
  844. if ($this->options['statOwner']) {
  845. $stat['isowner'] = $owner_computed;
  846. } else {
  847. unset($stat['owner'], $stat['group'], $stat['perm']);
  848. }
  849. }
  850. return $stat;
  851. }
  852. return array();
  853. }
  854. /**
  855. * Return true if path is dir and has at least one childs directory
  856. *
  857. * @param string $path dir path
  858. * @return bool
  859. * @author Dmitry (dio) Levashov
  860. **/
  861. protected function _subdirs($path) {
  862. foreach ($this->ftpRawList($path) as $str) {
  863. $info = preg_split('/\s+/', $str, 9);
  864. if (!isset($this->ftpOsUnix)) {
  865. $this->ftpOsUnix = !preg_match('/\d/', substr($info[0], 0, 1));
  866. }
  867. if (!$this->ftpOsUnix) {
  868. $info = $this->normalizeRawWindows($str);
  869. }
  870. $name = isset($info[8])? trim($info[8]) : '';
  871. if ($name && $name !== '.' && $name !== '..' && substr(strtolower($info[0]), 0, 1) === 'd') {
  872. return true;
  873. }
  874. }
  875. return false;
  876. }
  877. /**
  878. * Return object width and height
  879. * Ususaly used for images, but can be realize for video etc...
  880. *
  881. * @param string $path file path
  882. * @param string $mime file mime type
  883. * @return string|false
  884. * @author Dmitry (dio) Levashov
  885. **/
  886. protected function _dimensions($path, $mime) {
  887. $ret = false;
  888. if ($imgsize = $this->getImageSize($path, $mime)) {
  889. $ret = array('dim' => $imgsize['dimensions']);
  890. if (!empty($imgsize['url'])) {
  891. $ret['url'] = $imgsize['url'];
  892. }
  893. }
  894. return $ret;
  895. }
  896. /******************** file/dir content *********************/
  897. /**
  898. * Return files list in directory.
  899. *
  900. * @param string $path dir path
  901. * @return array
  902. * @author Dmitry (dio) Levashov
  903. * @author Cem (DiscoFever)
  904. **/
  905. protected function _scandir($path) {
  906. $files = array();
  907. foreach ($this->ftpRawList($path) as $str) {
  908. if (($stat = $this->parseRaw($str, $path, true))) {
  909. $files[] = $this->_joinPath($path, $stat['name']);
  910. }
  911. }
  912. return $files;
  913. }
  914. /**
  915. * Open file and return file pointer
  916. *
  917. * @param string $path file path
  918. * @param string $mode
  919. * @return false|resource
  920. * @internal param bool $write open file for writing
  921. * @author Dmitry (dio) Levashov
  922. */
  923. protected function _fopen($path, $mode='rb') {
  924. // try ftp stream wrapper
  925. if ($this->options['mode'] === 'passive' && ini_get('allow_url_fopen')) {
  926. $url = ($this->isFTPS? 'ftps' : 'ftp').'://'.$this->options['user'].':'.$this->options['pass'].'@'.$this->options['host'].':'.$this->options['port'].$path;
  927. if (strtolower($mode[0]) === 'w') {
  928. $context = stream_context_create(array('ftp' => array('overwrite' => true)));
  929. $fp = fopen($url, $mode, false, $context);
  930. } else {
  931. $fp = fopen($url, $mode);
  932. }
  933. if ($fp) {
  934. return $fp;
  935. }
  936. }
  937. if ($this->tmp) {
  938. $local = $this->getTempFile($path);
  939. $fp = fopen($local, 'wb');
  940. $ret = ftp_nb_fget($this->connect, $fp, $path, FTP_BINARY);
  941. while ($ret === FTP_MOREDATA) {
  942. elFinder::extendTimeLimit();
  943. $ret = ftp_nb_continue($this->connect);
  944. }
  945. if ($ret === FTP_FINISHED) {
  946. fclose($fp);
  947. $fp = fopen($local, $mode);
  948. return $fp;
  949. }
  950. fclose($fp);
  951. is_file($local) && unlink($local);
  952. }
  953. return false;
  954. }
  955. /**
  956. * Close opened file
  957. *
  958. * @param resource $fp file pointer
  959. * @param string $path
  960. * @return bool
  961. * @author Dmitry (dio) Levashov
  962. */
  963. protected function _fclose($fp, $path='') {
  964. is_resource($fp) && fclose($fp);
  965. if ($path) {
  966. unlink($this->getTempFile($path));
  967. }
  968. }
  969. /******************** file/dir manipulations *************************/
  970. /**
  971. * Create dir and return created dir path or false on failed
  972. *
  973. * @param string $path parent dir path
  974. * @param string $name new directory name
  975. * @return string|bool
  976. * @author Dmitry (dio) Levashov
  977. **/
  978. protected function _mkdir($path, $name) {
  979. $path = $this->_joinPath($path, $name);
  980. if (ftp_mkdir($this->connect, $path) === false) {
  981. return false;
  982. }
  983. $this->options['dirMode'] && ftp_chmod($this->connect, $this->options['dirMode'], $path);
  984. return $path;
  985. }
  986. /**
  987. * Create file and return it's path or false on failed
  988. *
  989. * @param string $path parent dir path
  990. * @param string $name new file name
  991. * @return string|bool
  992. * @author Dmitry (dio) Levashov
  993. **/
  994. protected function _mkfile($path, $name) {
  995. if ($this->tmp) {
  996. $path = $this->_joinPath($path, $name);
  997. $local = $this->getTempFile();
  998. $res = touch($local) && ftp_put($this->connect, $path, $local, FTP_ASCII);
  999. unlink($local);
  1000. return $res ? $path : false;
  1001. }
  1002. return false;
  1003. }
  1004. /**
  1005. * Create symlink. FTP driver does not support symlinks.
  1006. *
  1007. * @param string $target link target
  1008. * @param string $path symlink path
  1009. * @param string $name
  1010. * @return bool
  1011. * @author Dmitry (dio) Levashov
  1012. */
  1013. protected function _symlink($target, $path, $name) {
  1014. return false;
  1015. }
  1016. /**
  1017. * Copy file into another file
  1018. *
  1019. * @param string $source source file path
  1020. * @param string $targetDir target directory path
  1021. * @param string $name new file name
  1022. * @return bool
  1023. * @author Dmitry (dio) Levashov
  1024. **/
  1025. protected function _copy($source, $targetDir, $name) {
  1026. $res = false;
  1027. if ($this->tmp) {
  1028. $local = $this->getTempFile();
  1029. $target = $this->_joinPath($targetDir, $name);
  1030. if (ftp_get($this->connect, $local, $source, FTP_BINARY)
  1031. && ftp_put($this->connect, $target, $local, $this->ftpMode($target))) {
  1032. $res = $target;
  1033. }
  1034. unlink($local);
  1035. }
  1036. return $res;
  1037. }
  1038. /**
  1039. * Move file into another parent dir.
  1040. * Return new file path or false.
  1041. *
  1042. * @param string $source source file path
  1043. * @param $targetDir
  1044. * @param string $name file name
  1045. * @return bool|string
  1046. * @internal param string $target target dir path
  1047. * @author Dmitry (dio) Levashov
  1048. */
  1049. protected function _move($source, $targetDir, $name) {
  1050. $target = $this->_joinPath($targetDir, $name);
  1051. return ftp_rename($this->connect, $source, $target) ? $target : false;
  1052. }
  1053. /**
  1054. * Remove file
  1055. *
  1056. * @param string $path file path
  1057. * @return bool
  1058. * @author Dmitry (dio) Levashov
  1059. **/
  1060. protected function _unlink($path) {
  1061. return ftp_delete($this->connect, $path);
  1062. }
  1063. /**
  1064. * Remove dir
  1065. *
  1066. * @param string $path dir path
  1067. * @return bool
  1068. * @author Dmitry (dio) Levashov
  1069. **/
  1070. protected function _rmdir($path) {
  1071. return ftp_rmdir($this->connect, $path);
  1072. }
  1073. /**
  1074. * Create new file and write into it from file pointer.
  1075. * Return new file path or false on error.
  1076. *
  1077. * @param resource $fp file pointer
  1078. * @param string $dir target dir path
  1079. * @param string $name file name
  1080. * @param array $stat file stat (required by some virtual fs)
  1081. * @return bool|string
  1082. * @author Dmitry (dio) Levashov
  1083. **/
  1084. protected function _save($fp, $dir, $name, $stat) {
  1085. $path = $this->_joinPath($dir, $name);
  1086. return ftp_fput($this->connect, $path, $fp, $this->ftpMode($path))
  1087. ? $path
  1088. : false;
  1089. }
  1090. /**
  1091. * Get file contents
  1092. *
  1093. * @param string $path file path
  1094. * @return string|false
  1095. * @author Dmitry (dio) Levashov
  1096. **/
  1097. protected function _getContents($path) {
  1098. $contents = '';
  1099. if (($fp = $this->_fopen($path))) {
  1100. while (!feof($fp)) {
  1101. $contents .= fread($fp, 8192);
  1102. }
  1103. $this->_fclose($fp, $path);
  1104. return $contents;
  1105. }
  1106. return false;
  1107. }
  1108. /**
  1109. * Write a string to a file
  1110. *
  1111. * @param string $path file path
  1112. * @param string $content new file content
  1113. * @return bool
  1114. * @author Dmitry (dio) Levashov
  1115. **/
  1116. protected function _filePutContents($path, $content) {
  1117. $res = false;
  1118. if ($this->tmp) {
  1119. $local = $this->getTempFile();
  1120. if (file_put_contents($local, $content, LOCK_EX) !== false
  1121. && ($fp = fopen($local, 'rb'))) {
  1122. $file = $this->stat($this->convEncOut($path, false));
  1123. if (! empty($file['thash'])) {
  1124. $path = $this->decode($file['thash']);
  1125. }
  1126. clearstatcache();
  1127. $res = ftp_fput($this->connect, $path, $fp, $this->ftpMode($path));
  1128. fclose($fp);
  1129. }
  1130. file_exists($local) && unlink($local);
  1131. }
  1132. return $res;
  1133. }
  1134. /**
  1135. * Detect available archivers
  1136. *
  1137. * @return void
  1138. **/
  1139. protected function _checkArchivers() {
  1140. $this->archivers = $this->getArchivers();
  1141. return;
  1142. }
  1143. /**
  1144. * chmod availability
  1145. *
  1146. * @param string $path
  1147. * @param string $mode
  1148. * @return bool
  1149. */
  1150. protected function _chmod($path, $mode) {
  1151. $modeOct = is_string($mode) ? octdec($mode) : octdec(sprintf("%04o",$mode));
  1152. return ftp_chmod($this->connect, $modeOct, $path);
  1153. }
  1154. /**
  1155. * Recursive symlinks search
  1156. *
  1157. * @param string $path file/dir path
  1158. * @return bool
  1159. * @author Dmitry (dio) Levashov
  1160. **/
  1161. protected function _findSymlinks($path) {
  1162. die('Not yet implemented. (_findSymlinks)');
  1163. }
  1164. /**
  1165. * Extract files from archive
  1166. *
  1167. * @param string $path archive path
  1168. * @param array $arc archiver command and arguments (same as in $this->archivers)
  1169. * @return true
  1170. * @author Dmitry (dio) Levashov,
  1171. * @author Alexey Sukhotin
  1172. **/
  1173. protected function _extract($path, $arc)
  1174. {
  1175. $dir = $this->tempDir();
  1176. if (!$dir) {
  1177. return false;
  1178. }
  1179. $basename = $this->_basename($path);
  1180. $localPath = $dir . DIRECTORY_SEPARATOR . $basename;
  1181. if (!ftp_get($this->connect, $localPath, $path, FTP_BINARY)) {
  1182. //cleanup
  1183. $this->rmdirRecursive($dir);
  1184. return false;
  1185. }
  1186. $this->unpackArchive($localPath, $arc);
  1187. $this->archiveSize = 0;
  1188. // find symlinks and check extracted items
  1189. $checkRes = $this->checkExtractItems($dir);
  1190. if ($checkRes['symlinks']) {
  1191. $this->rmdirRecursive($dir);
  1192. return $this->setError(array_merge($this->error, array(elFinder::ERROR_ARC_SYMLINKS)));
  1193. }
  1194. $this->archiveSize = $checkRes['totalSize'];
  1195. if ($checkRes['rmNames']) {
  1196. foreach($checkRes['rmNames'] as $name) {
  1197. $this->addError(elFinder::ERROR_SAVE, $name);
  1198. }
  1199. }
  1200. $filesToProcess = self::listFilesInDirectory($dir, true);
  1201. // no files - extract error ?
  1202. if (empty($filesToProcess)) {
  1203. $this->rmdirRecursive($dir);
  1204. return false;
  1205. }
  1206. // check max files size
  1207. if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
  1208. $this->rmdirRecursive($dir);
  1209. return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
  1210. }
  1211. $extractTo = $this->extractToNewdir; // 'auto', ture or false
  1212. // archive contains one item - extract in archive dir
  1213. $name = '';
  1214. $src = $dir . DIRECTORY_SEPARATOR . $filesToProcess[0];
  1215. if (($extractTo === 'auto' || !$extractTo) && count($filesToProcess) === 1 && is_file($src)) {
  1216. $name = $filesToProcess[0];
  1217. } else if ($extractTo === 'auto' || $extractTo) {
  1218. // for several files - create new directory
  1219. // create unique name for directory
  1220. $src = $dir;
  1221. $splits = elFinder::splitFileExtention(basename($path));
  1222. $name = $splits[0];
  1223. $test = $this->_joinPath(dirname($path), $name);
  1224. if ($this->stat($test)) {
  1225. $name = $this->uniqueName(dirname($path), $name, '-', false);
  1226. }
  1227. }
  1228. if ($name !== '' && is_file($src)) {
  1229. $result = $this->_joinPath(dirname($path), $name);
  1230. if (! ftp_put($this->connect, $result, $src, FTP_BINARY)) {
  1231. $this->rmdirRecursive($dir);
  1232. return false;
  1233. }
  1234. } else {
  1235. $dstDir = $this->_dirname($path);
  1236. $result = array();
  1237. if (is_dir($src) && $name) {
  1238. $target = $this->_joinPath($dstDir, $name);
  1239. $_stat = $this->_stat($target);
  1240. if ($_stat) {
  1241. if (! $this->options['copyJoin']) {
  1242. if ($_stat['mime'] === 'directory') {
  1243. $this->delTree($target);
  1244. } else {
  1245. $this->_unlink($target);
  1246. }
  1247. $_stat = false;
  1248. } else {
  1249. $dstDir = $target;
  1250. }
  1251. }
  1252. if (! $_stat && (!$dstDir = $this->_mkdir($dstDir, $name))) {
  1253. $this->rmdirRecursive($dir);
  1254. return false;
  1255. }
  1256. $result[] = $dstDir;
  1257. }
  1258. foreach($filesToProcess as $name) {
  1259. $name = rtrim($name, DIRECTORY_SEPARATOR);
  1260. $src = $dir . DIRECTORY_SEPARATOR . $name;
  1261. if (is_dir($src)) {
  1262. $p = dirname($name);
  1263. if ($p === '.') {
  1264. $p = '';
  1265. }
  1266. $name = basename($name);
  1267. $target = $this->_joinPath($this->_joinPath($dstDir, $p), $name);
  1268. $_stat = $this->_stat($target);
  1269. if ($_stat) {
  1270. if (! $this->options['copyJoin']) {
  1271. if ($_stat['mime'] === 'directory') {
  1272. $this->delTree($target);
  1273. } else {
  1274. $this->_unlink($target);
  1275. }
  1276. $_stat = false;
  1277. }
  1278. }
  1279. if (! $_stat && (!$target = $this->_mkdir($this->_joinPath($dstDir, $p), $name))) {
  1280. $this->rmdirRecursive($dir);
  1281. return false;
  1282. }
  1283. } else {
  1284. $target = $this->_joinPath($dstDir, $name);
  1285. if (! ftp_put($this->connect, $target, $src, FTP_BINARY)) {
  1286. $this->rmdirRecursive($dir);
  1287. return false;
  1288. }
  1289. }
  1290. $result[] = $target;
  1291. }
  1292. if (!$result) {
  1293. $this->rmdirRecursive($dir);
  1294. return false;
  1295. }
  1296. }
  1297. is_dir($dir) && $this->rmdirRecursive($dir);
  1298. $this->clearcache();
  1299. return $result? $result : false;
  1300. }
  1301. /**
  1302. * Create archive and return its path
  1303. *
  1304. * @param string $dir target dir
  1305. * @param array $files files names list
  1306. * @param string $name archive name
  1307. * @param array $arc archiver options
  1308. * @return string|bool
  1309. * @author Dmitry (dio) Levashov,
  1310. * @author Alexey Sukhotin
  1311. **/
  1312. protected function _archive($dir, $files, $name, $arc)
  1313. {
  1314. // get current directory
  1315. $cwd = getcwd();
  1316. $tmpDir = $this->tempDir();
  1317. if (!$tmpDir) {
  1318. return false;
  1319. }
  1320. //download data
  1321. if (!$this->ftp_download_files($dir, $files, $tmpDir)) {
  1322. //cleanup
  1323. $this->rmdirRecursive($tmpDir);
  1324. return false;
  1325. }
  1326. $remoteArchiveFile = false;
  1327. if ($path = $this->makeArchive($tmpDir, $files, $name, $arc)) {
  1328. $remoteArchiveFile = $this->_joinPath($dir, $name);
  1329. if (!ftp_put($this->connect, $remoteArchiveFile, $path, FTP_BINARY)) {
  1330. $remoteArchiveFile = false;
  1331. }
  1332. }
  1333. //cleanup
  1334. if(!$this->rmdirRecursive($tmpDir)) {
  1335. return false;
  1336. }
  1337. return $remoteArchiveFile;
  1338. }
  1339. /**
  1340. * Create writable temporary directory and return path to it.
  1341. * @return string path to the new temporary directory or false in case of error.
  1342. */
  1343. private function tempDir()
  1344. {
  1345. $tempPath = tempnam($this->tmp, 'elFinder');
  1346. if (!$tempPath) {
  1347. $this->setError(elFinder::ERROR_CREATING_TEMP_DIR, $this->tmp);
  1348. return false;
  1349. }
  1350. $success = unlink($tempPath);
  1351. if (!$success) {
  1352. $this->setError(elFinder::ERROR_CREATING_TEMP_DIR, $this->tmp);
  1353. return false;
  1354. }
  1355. $success = mkdir($tempPath, 0700, true);
  1356. if (!$success) {
  1357. $this->setError(elFinder::ERROR_CREATING_TEMP_DIR, $this->tmp);
  1358. return false;
  1359. }
  1360. return $tempPath;
  1361. }
  1362. /**
  1363. * Gets an array of absolute remote FTP paths of files and
  1364. * folders in $remote_directory omitting symbolic links.
  1365. *
  1366. * @param $remote_directory string remote FTP path to scan for file and folders recursively
  1367. * @param $targets array Array of target item. `null` is to get all of items
  1368. * @return array of elements each of which is an array of two elements:
  1369. * <ul>
  1370. * <li>$item['path'] - absolute remote FTP path</li>
  1371. * <li>$item['type'] - either 'f' for file or 'd' for directory</li>
  1372. * </ul>
  1373. */
  1374. protected function ftp_scan_dir($remote_directory, $targets = null)
  1375. {
  1376. $buff = $this->ftpRawList($remote_directory);
  1377. $items = array();
  1378. if ($targets && is_array($targets)) {
  1379. $targets = array_flip($targets);
  1380. } else {
  1381. $targets = false;
  1382. }
  1383. foreach ($buff as $str) {
  1384. $info = preg_split("/\s+/", $str, 9);
  1385. if (!isset($this->ftpOsUnix)) {
  1386. $this->ftpOsUnix = !preg_match('/\d/', substr($info[0], 0, 1));
  1387. }
  1388. if (!$this->ftpOsUnix) {
  1389. $info = $this->normalizeRawWindows($str);
  1390. }
  1391. $type = substr($info[0], 0, 1);
  1392. $name = trim($info[8]);
  1393. if ($name !== '.' && $name !== '..' && (!$targets || isset($targets[$name]))) {
  1394. switch ($type) {
  1395. case 'l' : //omit symbolic links
  1396. case 'd' :
  1397. $remote_file_path = $this->_joinPath($remote_directory, $name);
  1398. $item = array();
  1399. $item['path'] = $remote_file_path;
  1400. $item['type'] = 'd'; // normal file
  1401. $items[] = $item;
  1402. $items = array_merge($items, $this->ftp_scan_dir($remote_file_path));
  1403. break;
  1404. default:
  1405. $remote_file_path = $this->_joinPath($remote_directory, $name);
  1406. $item = array();
  1407. $item['path'] = $remote_file_path;
  1408. $item['type'] = 'f'; // normal file
  1409. $items[] = $item;
  1410. }
  1411. }
  1412. }
  1413. return $items;
  1414. }
  1415. /**
  1416. * Downloads specified files from remote directory
  1417. * if there is a directory among files it is downloaded recursively (omitting symbolic links).
  1418. *
  1419. * @param $remote_directory string remote FTP path to a source directory to download from.
  1420. * @param array $files list of files to download from remote directory.
  1421. * @param $dest_local_directory string destination folder to store downloaded files.
  1422. * @return bool true on success and false on failure.
  1423. */
  1424. private function ftp_download_files($remote_directory, array $files, $dest_local_directory)
  1425. {
  1426. $contents = $this->ftp_scan_dir($remote_directory, $files);
  1427. if (!isset($contents)) {
  1428. $this->setError(elFinder::ERROR_FTP_DOWNLOAD_FILE, $remote_directory);
  1429. return false;
  1430. }
  1431. $remoteDirLen = strlen($remote_directory);
  1432. foreach ($contents as $item) {
  1433. $relative_path = substr($item['path'], $remoteDirLen);
  1434. $local_path = $dest_local_directory . DIRECTORY_SEPARATOR . $relative_path;
  1435. switch ($item['type']) {
  1436. case 'd':
  1437. $success = mkdir($local_path);
  1438. break;
  1439. case 'f':
  1440. $success = ftp_get($this->connect, $local_path, $item['path'], FTP_BINARY);
  1441. break;
  1442. default:
  1443. $success = true;
  1444. }
  1445. if (!$success) {
  1446. $this->setError(elFinder::ERROR_FTP_DOWNLOAD_FILE, $remote_directory);
  1447. return false;
  1448. }
  1449. }
  1450. return true;
  1451. }
  1452. /**
  1453. * Delete local directory recursively.
  1454. * @param $dirPath string to directory to be erased.
  1455. * @return bool true on success and false on failure.
  1456. */
  1457. private function deleteDir($dirPath)
  1458. {
  1459. if (!is_dir($dirPath)) {
  1460. $success = unlink($dirPath);
  1461. } else {
  1462. $success = true;
  1463. foreach (array_reverse(elFinderVolumeFTP::listFilesInDirectory($dirPath, false)) as $path) {
  1464. $path = $dirPath . DIRECTORY_SEPARATOR . $path;
  1465. if(is_link($path)) {
  1466. unlink($path);
  1467. } else if (is_dir($path)) {
  1468. $success = rmdir($path);
  1469. } else {
  1470. $success = unlink($path);
  1471. }
  1472. if (!$success) {
  1473. break;
  1474. }
  1475. }
  1476. if($success) {
  1477. $success = rmdir($dirPath);
  1478. }
  1479. }
  1480. if(!$success) {
  1481. $this->setError(elFinder::ERROR_RM, $dirPath);
  1482. return false;
  1483. }
  1484. return $success;
  1485. }
  1486. /**
  1487. * Returns array of strings containing all files and folders in the specified local directory.
  1488. * @param $dir
  1489. * @param $omitSymlinks
  1490. * @param string $prefix
  1491. * @return array array of files and folders names relative to the $path
  1492. * or an empty array if the directory $path is empty,
  1493. * <br />
  1494. * false if $path is not a directory or does not exist.
  1495. * @throws Exception
  1496. * @internal param string $path path to directory to scan.
  1497. */
  1498. private static function listFilesInDirectory($dir, $omitSymlinks, $prefix = '')
  1499. {
  1500. if (!is_dir($dir)) {
  1501. return false;
  1502. }
  1503. $excludes = array(".","..");
  1504. $result = array();
  1505. $files = self::localScandir($dir);
  1506. if(!$files) {
  1507. return array();
  1508. }
  1509. foreach($files as $file) {
  1510. if(!in_array($file, $excludes)) {
  1511. $path = $dir.DIRECTORY_SEPARATOR.$file;
  1512. if(is_link($path)) {
  1513. if($omitSymlinks) {
  1514. continue;
  1515. } else {
  1516. $result[] = $prefix.$file;
  1517. }
  1518. } else if(is_dir($path)) {
  1519. $result[] = $prefix.$file.DIRECTORY_SEPARATOR;
  1520. $subs = elFinderVolumeFTP::listFilesInDirectory($path, $omitSymlinks, $prefix.$file.DIRECTORY_SEPARATOR);
  1521. if($subs) {
  1522. $result = array_merge($result, $subs);
  1523. }
  1524. } else {
  1525. $result[] = $prefix.$file;
  1526. }
  1527. }
  1528. }
  1529. return $result;
  1530. }
  1531. } // END class