PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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