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

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

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