PageRenderTime 92ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/class-wp-filesystem-ftpext.php

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