class-wp-filesystem-ftpext.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * WordPress FTP Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for implementing FTP.
  10. *
  11. * @since 2.5.0
  12. *
  13. * @see WP_Filesystem_Base
  14. */
  15. class WP_Filesystem_FTPext extends WP_Filesystem_Base {
  16. public $link;
  17. /**
  18. *
  19. * @param array $opt
  20. */
  21. public function __construct( $opt = '' ) {
  22. $this->method = 'ftpext';
  23. $this->errors = new WP_Error();
  24. // Check if possible to use ftp functions.
  25. if ( ! extension_loaded('ftp') ) {
  26. $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
  27. return;
  28. }
  29. // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
  30. if ( ! defined('FS_TIMEOUT') )
  31. define('FS_TIMEOUT', 240);
  32. if ( empty($opt['port']) )
  33. $this->options['port'] = 21;
  34. else
  35. $this->options['port'] = $opt['port'];
  36. if ( empty($opt['hostname']) )
  37. $this->errors->add('empty_hostname', __('FTP hostname is required'));
  38. else
  39. $this->options['hostname'] = $opt['hostname'];
  40. // Check if the options provided are OK.
  41. if ( empty($opt['username']) )
  42. $this->errors->add('empty_username', __('FTP username is required'));
  43. else
  44. $this->options['username'] = $opt['username'];
  45. if ( empty($opt['password']) )
  46. $this->errors->add('empty_password', __('FTP password is required'));
  47. else
  48. $this->options['password'] = $opt['password'];
  49. $this->options['ssl'] = false;
  50. if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
  51. $this->options['ssl'] = true;
  52. }
  53. /**
  54. *
  55. * @return bool
  56. */
  57. public function connect() {
  58. if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
  59. $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
  60. else
  61. $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
  62. if ( ! $this->link ) {
  63. $this->errors->add( 'connect',
  64. /* translators: %s: hostname:port */
  65. sprintf( __( 'Failed to connect to FTP Server %s' ),
  66. $this->options['hostname'] . ':' . $this->options['port']
  67. )
  68. );
  69. return false;
  70. }
  71. if ( ! @ftp_login( $this->link,$this->options['username'], $this->options['password'] ) ) {
  72. $this->errors->add( 'auth',
  73. /* translators: %s: username */
  74. sprintf( __( 'Username/Password incorrect for %s' ),
  75. $this->options['username']
  76. )
  77. );
  78. return false;
  79. }
  80. // Set the Connection to use Passive FTP
  81. @ftp_pasv( $this->link, true );
  82. if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
  83. @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
  84. return true;
  85. }
  86. /**
  87. * Retrieves the file contents.
  88. *
  89. * @since 2.5.0
  90. *
  91. * @param string $file Filename.
  92. * @return string|false File contents on success, false if no temp file could be opened,
  93. * or if the file couldn't be retrieved.
  94. */
  95. public function get_contents( $file ) {
  96. $tempfile = wp_tempnam($file);
  97. $temp = fopen($tempfile, 'w+');
  98. if ( ! $temp ) {
  99. unlink( $tempfile );
  100. return false;
  101. }
  102. if ( ! @ftp_fget( $this->link, $temp, $file, FTP_BINARY ) ) {
  103. fclose( $temp );
  104. unlink( $tempfile );
  105. return false;
  106. }
  107. fseek( $temp, 0 ); // Skip back to the start of the file being written to
  108. $contents = '';
  109. while ( ! feof($temp) )
  110. $contents .= fread($temp, 8192);
  111. fclose($temp);
  112. unlink($tempfile);
  113. return $contents;
  114. }
  115. /**
  116. *
  117. * @param string $file
  118. * @return array
  119. */
  120. public function get_contents_array($file) {
  121. return explode("\n", $this->get_contents($file));
  122. }
  123. /**
  124. *
  125. * @param string $file
  126. * @param string $contents
  127. * @param bool|int $mode
  128. * @return bool
  129. */
  130. public function put_contents($file, $contents, $mode = false ) {
  131. $tempfile = wp_tempnam($file);
  132. $temp = fopen( $tempfile, 'wb+' );
  133. if ( ! $temp ) {
  134. unlink( $tempfile );
  135. return false;
  136. }
  137. mbstring_binary_safe_encoding();
  138. $data_length = strlen( $contents );
  139. $bytes_written = fwrite( $temp, $contents );
  140. reset_mbstring_encoding();
  141. if ( $data_length !== $bytes_written ) {
  142. fclose( $temp );
  143. unlink( $tempfile );
  144. return false;
  145. }
  146. fseek( $temp, 0 ); // Skip back to the start of the file being written to
  147. $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
  148. fclose($temp);
  149. unlink($tempfile);
  150. $this->chmod($file, $mode);
  151. return $ret;
  152. }
  153. /**
  154. *
  155. * @return string
  156. */
  157. public function cwd() {
  158. $cwd = @ftp_pwd($this->link);
  159. if ( $cwd )
  160. $cwd = trailingslashit($cwd);
  161. return $cwd;
  162. }
  163. /**
  164. *
  165. * @param string $dir
  166. * @return bool
  167. */
  168. public function chdir($dir) {
  169. return @ftp_chdir($this->link, $dir);
  170. }
  171. /**
  172. *
  173. * @param string $file
  174. * @param int $mode
  175. * @param bool $recursive
  176. * @return bool
  177. */
  178. public function chmod($file, $mode = false, $recursive = false) {
  179. if ( ! $mode ) {
  180. if ( $this->is_file($file) )
  181. $mode = FS_CHMOD_FILE;
  182. elseif ( $this->is_dir($file) )
  183. $mode = FS_CHMOD_DIR;
  184. else
  185. return false;
  186. }
  187. // chmod any sub-objects if recursive.
  188. if ( $recursive && $this->is_dir($file) ) {
  189. $filelist = $this->dirlist($file);
  190. foreach ( (array)$filelist as $filename => $filemeta )
  191. $this->chmod($file . '/' . $filename, $mode, $recursive);
  192. }
  193. // chmod the file or directory
  194. if ( ! function_exists('ftp_chmod') )
  195. return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
  196. return (bool)@ftp_chmod($this->link, $mode, $file);
  197. }
  198. /**
  199. *
  200. * @param string $file
  201. * @return string
  202. */
  203. public function owner($file) {
  204. $dir = $this->dirlist($file);
  205. return $dir[$file]['owner'];
  206. }
  207. /**
  208. *
  209. * @param string $file
  210. * @return string
  211. */
  212. public function getchmod($file) {
  213. $dir = $this->dirlist($file);
  214. return $dir[$file]['permsn'];
  215. }
  216. /**
  217. *
  218. * @param string $file
  219. * @return string
  220. */
  221. public function group($file) {
  222. $dir = $this->dirlist($file);
  223. return $dir[$file]['group'];
  224. }
  225. /**
  226. *
  227. * @param string $source
  228. * @param string $destination
  229. * @param bool $overwrite
  230. * @param string|bool $mode
  231. * @return bool
  232. */
  233. public function copy($source, $destination, $overwrite = false, $mode = false) {
  234. if ( ! $overwrite && $this->exists($destination) )
  235. return false;
  236. $content = $this->get_contents($source);
  237. if ( false === $content )
  238. return false;
  239. return $this->put_contents($destination, $content, $mode);
  240. }
  241. /**
  242. *
  243. * @param string $source
  244. * @param string $destination
  245. * @param bool $overwrite
  246. * @return bool
  247. */
  248. public function move($source, $destination, $overwrite = false) {
  249. return ftp_rename($this->link, $source, $destination);
  250. }
  251. /**
  252. *
  253. * @param string $file
  254. * @param bool $recursive
  255. * @param string $type
  256. * @return bool
  257. */
  258. public function delete($file, $recursive = false, $type = false) {
  259. if ( empty($file) )
  260. return false;
  261. if ( 'f' == $type || $this->is_file($file) )
  262. return @ftp_delete($this->link, $file);
  263. if ( !$recursive )
  264. return @ftp_rmdir($this->link, $file);
  265. $filelist = $this->dirlist( trailingslashit($file) );
  266. if ( !empty($filelist) )
  267. foreach ( $filelist as $delete_file )
  268. $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
  269. return @ftp_rmdir($this->link, $file);
  270. }
  271. /**
  272. *
  273. * @param string $file
  274. * @return bool
  275. */
  276. public function exists($file) {
  277. $list = @ftp_nlist($this->link, $file);
  278. if ( empty( $list ) && $this->is_dir( $file ) ) {
  279. return true; // File is an empty directory.
  280. }
  281. return !empty($list); //empty list = no file, so invert.
  282. }
  283. /**
  284. *
  285. * @param string $file
  286. * @return bool
  287. */
  288. public function is_file($file) {
  289. return $this->exists($file) && !$this->is_dir($file);
  290. }
  291. /**
  292. *
  293. * @param string $path
  294. * @return bool
  295. */
  296. public function is_dir($path) {
  297. $cwd = $this->cwd();
  298. $result = @ftp_chdir($this->link, trailingslashit($path) );
  299. if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
  300. @ftp_chdir($this->link, $cwd);
  301. return true;
  302. }
  303. return false;
  304. }
  305. /**
  306. *
  307. * @param string $file
  308. * @return bool
  309. */
  310. public function is_readable($file) {
  311. return true;
  312. }
  313. /**
  314. *
  315. * @param string $file
  316. * @return bool
  317. */
  318. public function is_writable($file) {
  319. return true;
  320. }
  321. /**
  322. *
  323. * @param string $file
  324. * @return bool
  325. */
  326. public function atime($file) {
  327. return false;
  328. }
  329. /**
  330. *
  331. * @param string $file
  332. * @return int
  333. */
  334. public function mtime($file) {
  335. return ftp_mdtm($this->link, $file);
  336. }
  337. /**
  338. *
  339. * @param string $file
  340. * @return int
  341. */
  342. public function size($file) {
  343. return ftp_size($this->link, $file);
  344. }
  345. /**
  346. *
  347. * @param string $file
  348. * @return bool
  349. */
  350. public function touch($file, $time = 0, $atime = 0) {
  351. return false;
  352. }
  353. /**
  354. *
  355. * @param string $path
  356. * @param mixed $chmod
  357. * @param mixed $chown
  358. * @param mixed $chgrp
  359. * @return bool
  360. */
  361. public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  362. $path = untrailingslashit($path);
  363. if ( empty($path) )
  364. return false;
  365. if ( !@ftp_mkdir($this->link, $path) )
  366. return false;
  367. $this->chmod($path, $chmod);
  368. return true;
  369. }
  370. /**
  371. *
  372. * @param string $path
  373. * @param bool $recursive
  374. * @return bool
  375. */
  376. public function rmdir($path, $recursive = false) {
  377. return $this->delete($path, $recursive);
  378. }
  379. /**
  380. *
  381. * @staticvar bool $is_windows
  382. * @param string $line
  383. * @return array
  384. */
  385. public function parselisting($line) {
  386. static $is_windows = null;
  387. if ( is_null($is_windows) )
  388. $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
  389. if ( $is_windows && preg_match('/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer) ) {
  390. $b = array();
  391. if ( $lucifer[3] < 70 )
  392. $lucifer[3] +=2000;
  393. else
  394. $lucifer[3] += 1900; // 4digit year fix
  395. $b['isdir'] = ( $lucifer[7] == '<DIR>');
  396. if ( $b['isdir'] )
  397. $b['type'] = 'd';
  398. else
  399. $b['type'] = 'f';
  400. $b['size'] = $lucifer[7];
  401. $b['month'] = $lucifer[1];
  402. $b['day'] = $lucifer[2];
  403. $b['year'] = $lucifer[3];
  404. $b['hour'] = $lucifer[4];
  405. $b['minute'] = $lucifer[5];
  406. $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
  407. $b['am/pm'] = $lucifer[6];
  408. $b['name'] = $lucifer[8];
  409. } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
  410. //echo $line."\n";
  411. $lcount = count($lucifer);
  412. if ( $lcount < 8 )
  413. return '';
  414. $b = array();
  415. $b['isdir'] = $lucifer[0]{0} === 'd';
  416. $b['islink'] = $lucifer[0]{0} === 'l';
  417. if ( $b['isdir'] )
  418. $b['type'] = 'd';
  419. elseif ( $b['islink'] )
  420. $b['type'] = 'l';
  421. else
  422. $b['type'] = 'f';
  423. $b['perms'] = $lucifer[0];
  424. $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
  425. $b['number'] = $lucifer[1];
  426. $b['owner'] = $lucifer[2];
  427. $b['group'] = $lucifer[3];
  428. $b['size'] = $lucifer[4];
  429. if ( $lcount == 8 ) {
  430. sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
  431. sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
  432. $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
  433. $b['name'] = $lucifer[7];
  434. } else {
  435. $b['month'] = $lucifer[5];
  436. $b['day'] = $lucifer[6];
  437. if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
  438. $b['year'] = date("Y");
  439. $b['hour'] = $l2[1];
  440. $b['minute'] = $l2[2];
  441. } else {
  442. $b['year'] = $lucifer[7];
  443. $b['hour'] = 0;
  444. $b['minute'] = 0;
  445. }
  446. $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
  447. $b['name'] = $lucifer[8];
  448. }
  449. }
  450. // Replace symlinks formatted as "source -> target" with just the source name
  451. if ( isset( $b['islink'] ) && $b['islink'] ) {
  452. $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
  453. }
  454. return $b;
  455. }
  456. /**
  457. *
  458. * @param string $path
  459. * @param bool $include_hidden
  460. * @param bool $recursive
  461. * @return bool|array
  462. */
  463. public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
  464. if ( $this->is_file($path) ) {
  465. $limit_file = basename($path);
  466. $path = dirname($path) . '/';
  467. } else {
  468. $limit_file = false;
  469. }
  470. $pwd = @ftp_pwd($this->link);
  471. if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
  472. return false;
  473. $list = @ftp_rawlist($this->link, '-a', false);
  474. @ftp_chdir($this->link, $pwd);
  475. if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
  476. return false;
  477. $dirlist = array();
  478. foreach ( $list as $k => $v ) {
  479. $entry = $this->parselisting($v);
  480. if ( empty($entry) )
  481. continue;
  482. if ( '.' == $entry['name'] || '..' == $entry['name'] )
  483. continue;
  484. if ( ! $include_hidden && '.' == $entry['name'][0] )
  485. continue;
  486. if ( $limit_file && $entry['name'] != $limit_file)
  487. continue;
  488. $dirlist[ $entry['name'] ] = $entry;
  489. }
  490. $ret = array();
  491. foreach ( (array)$dirlist as $struc ) {
  492. if ( 'd' == $struc['type'] ) {
  493. if ( $recursive )
  494. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  495. else
  496. $struc['files'] = array();
  497. }
  498. $ret[ $struc['name'] ] = $struc;
  499. }
  500. return $ret;
  501. }
  502. /**
  503. */
  504. public function __destruct() {
  505. if ( $this->link )
  506. ftp_close($this->link);
  507. }
  508. }