elFinderVolumeFTP.class.php 44 KB

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