PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 415 lines | 320 code | 73 blank | 22 comment | 81 complexity | 6764f43b78e8bbe008f06ad3b1a1f5aa MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. var $link;
  18. var $errors = null;
  19. var $options = array();
  20. 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 false;
  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. 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. function get_contents( $file ) {
  74. $tempfile = wp_tempnam($file);
  75. $temp = fopen($tempfile, 'w+');
  76. if ( ! $temp )
  77. return false;
  78. if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
  79. return false;
  80. fseek( $temp, 0 ); // Skip back to the start of the file being written to
  81. $contents = '';
  82. while ( ! feof($temp) )
  83. $contents .= fread($temp, 8192);
  84. fclose($temp);
  85. unlink($tempfile);
  86. return $contents;
  87. }
  88. function get_contents_array($file) {
  89. return explode("\n", $this->get_contents($file));
  90. }
  91. function put_contents($file, $contents, $mode = false ) {
  92. $tempfile = wp_tempnam($file);
  93. $temp = fopen( $tempfile, 'wb+' );
  94. if ( ! $temp )
  95. return false;
  96. mbstring_binary_safe_encoding();
  97. $data_length = strlen( $contents );
  98. $bytes_written = fwrite( $temp, $contents );
  99. reset_mbstring_encoding();
  100. if ( $data_length !== $bytes_written ) {
  101. fclose( $temp );
  102. unlink( $tempfile );
  103. return false;
  104. }
  105. fseek( $temp, 0 ); // Skip back to the start of the file being written to
  106. $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
  107. fclose($temp);
  108. unlink($tempfile);
  109. $this->chmod($file, $mode);
  110. return $ret;
  111. }
  112. function cwd() {
  113. $cwd = @ftp_pwd($this->link);
  114. if ( $cwd )
  115. $cwd = trailingslashit($cwd);
  116. return $cwd;
  117. }
  118. function chdir($dir) {
  119. return @ftp_chdir($this->link, $dir);
  120. }
  121. function chgrp($file, $group, $recursive = false ) {
  122. return false;
  123. }
  124. function chmod($file, $mode = false, $recursive = false) {
  125. if ( ! $mode ) {
  126. if ( $this->is_file($file) )
  127. $mode = FS_CHMOD_FILE;
  128. elseif ( $this->is_dir($file) )
  129. $mode = FS_CHMOD_DIR;
  130. else
  131. return false;
  132. }
  133. // chmod any sub-objects if recursive.
  134. if ( $recursive && $this->is_dir($file) ) {
  135. $filelist = $this->dirlist($file);
  136. foreach ( (array)$filelist as $filename => $filemeta )
  137. $this->chmod($file . '/' . $filename, $mode, $recursive);
  138. }
  139. // chmod the file or directory
  140. if ( ! function_exists('ftp_chmod') )
  141. return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
  142. return (bool)@ftp_chmod($this->link, $mode, $file);
  143. }
  144. function owner($file) {
  145. $dir = $this->dirlist($file);
  146. return $dir[$file]['owner'];
  147. }
  148. function getchmod($file) {
  149. $dir = $this->dirlist($file);
  150. return $dir[$file]['permsn'];
  151. }
  152. function group($file) {
  153. $dir = $this->dirlist($file);
  154. return $dir[$file]['group'];
  155. }
  156. function copy($source, $destination, $overwrite = false, $mode = false) {
  157. if ( ! $overwrite && $this->exists($destination) )
  158. return false;
  159. $content = $this->get_contents($source);
  160. if ( false === $content )
  161. return false;
  162. return $this->put_contents($destination, $content, $mode);
  163. }
  164. function move($source, $destination, $overwrite = false) {
  165. return ftp_rename($this->link, $source, $destination);
  166. }
  167. function delete($file, $recursive = false, $type = false) {
  168. if ( empty($file) )
  169. return false;
  170. if ( 'f' == $type || $this->is_file($file) )
  171. return @ftp_delete($this->link, $file);
  172. if ( !$recursive )
  173. return @ftp_rmdir($this->link, $file);
  174. $filelist = $this->dirlist( trailingslashit($file) );
  175. if ( !empty($filelist) )
  176. foreach ( $filelist as $delete_file )
  177. $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
  178. return @ftp_rmdir($this->link, $file);
  179. }
  180. function exists($file) {
  181. $list = @ftp_nlist($this->link, $file);
  182. return !empty($list); //empty list = no file, so invert.
  183. }
  184. function is_file($file) {
  185. return $this->exists($file) && !$this->is_dir($file);
  186. }
  187. function is_dir($path) {
  188. $cwd = $this->cwd();
  189. $result = @ftp_chdir($this->link, trailingslashit($path) );
  190. if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
  191. @ftp_chdir($this->link, $cwd);
  192. return true;
  193. }
  194. return false;
  195. }
  196. function is_readable($file) {
  197. return true;
  198. }
  199. function is_writable($file) {
  200. return true;
  201. }
  202. function atime($file) {
  203. return false;
  204. }
  205. function mtime($file) {
  206. return ftp_mdtm($this->link, $file);
  207. }
  208. function size($file) {
  209. return ftp_size($this->link, $file);
  210. }
  211. function touch($file, $time = 0, $atime = 0) {
  212. return false;
  213. }
  214. function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  215. $path = untrailingslashit($path);
  216. if ( empty($path) )
  217. return false;
  218. if ( !@ftp_mkdir($this->link, $path) )
  219. return false;
  220. $this->chmod($path, $chmod);
  221. if ( $chown )
  222. $this->chown($path, $chown);
  223. if ( $chgrp )
  224. $this->chgrp($path, $chgrp);
  225. return true;
  226. }
  227. function rmdir($path, $recursive = false) {
  228. return $this->delete($path, $recursive);
  229. }
  230. function parselisting($line) {
  231. static $is_windows;
  232. if ( is_null($is_windows) )
  233. $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
  234. 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) ) {
  235. $b = array();
  236. if ( $lucifer[3] < 70 )
  237. $lucifer[3] +=2000;
  238. else
  239. $lucifer[3] += 1900; // 4digit year fix
  240. $b['isdir'] = ( $lucifer[7] == '<DIR>');
  241. if ( $b['isdir'] )
  242. $b['type'] = 'd';
  243. else
  244. $b['type'] = 'f';
  245. $b['size'] = $lucifer[7];
  246. $b['month'] = $lucifer[1];
  247. $b['day'] = $lucifer[2];
  248. $b['year'] = $lucifer[3];
  249. $b['hour'] = $lucifer[4];
  250. $b['minute'] = $lucifer[5];
  251. $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
  252. $b['am/pm'] = $lucifer[6];
  253. $b['name'] = $lucifer[8];
  254. } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
  255. //echo $line."\n";
  256. $lcount = count($lucifer);
  257. if ( $lcount < 8 )
  258. return '';
  259. $b = array();
  260. $b['isdir'] = $lucifer[0]{0} === 'd';
  261. $b['islink'] = $lucifer[0]{0} === 'l';
  262. if ( $b['isdir'] )
  263. $b['type'] = 'd';
  264. elseif ( $b['islink'] )
  265. $b['type'] = 'l';
  266. else
  267. $b['type'] = 'f';
  268. $b['perms'] = $lucifer[0];
  269. $b['number'] = $lucifer[1];
  270. $b['owner'] = $lucifer[2];
  271. $b['group'] = $lucifer[3];
  272. $b['size'] = $lucifer[4];
  273. if ( $lcount == 8 ) {
  274. sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
  275. sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
  276. $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
  277. $b['name'] = $lucifer[7];
  278. } else {
  279. $b['month'] = $lucifer[5];
  280. $b['day'] = $lucifer[6];
  281. if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
  282. $b['year'] = date("Y");
  283. $b['hour'] = $l2[1];
  284. $b['minute'] = $l2[2];
  285. } else {
  286. $b['year'] = $lucifer[7];
  287. $b['hour'] = 0;
  288. $b['minute'] = 0;
  289. }
  290. $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
  291. $b['name'] = $lucifer[8];
  292. }
  293. }
  294. // Replace symlinks formatted as "source -> target" with just the source name
  295. if ( $b['islink'] )
  296. $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
  297. return $b;
  298. }
  299. function dirlist($path = '.', $include_hidden = true, $recursive = false) {
  300. if ( $this->is_file($path) ) {
  301. $limit_file = basename($path);
  302. $path = dirname($path) . '/';
  303. } else {
  304. $limit_file = false;
  305. }
  306. $pwd = @ftp_pwd($this->link);
  307. if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
  308. return false;
  309. $list = @ftp_rawlist($this->link, '-a', false);
  310. @ftp_chdir($this->link, $pwd);
  311. if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
  312. return false;
  313. $dirlist = array();
  314. foreach ( $list as $k => $v ) {
  315. $entry = $this->parselisting($v);
  316. if ( empty($entry) )
  317. continue;
  318. if ( '.' == $entry['name'] || '..' == $entry['name'] )
  319. continue;
  320. if ( ! $include_hidden && '.' == $entry['name'][0] )
  321. continue;
  322. if ( $limit_file && $entry['name'] != $limit_file)
  323. continue;
  324. $dirlist[ $entry['name'] ] = $entry;
  325. }
  326. $ret = array();
  327. foreach ( (array)$dirlist as $struc ) {
  328. if ( 'd' == $struc['type'] ) {
  329. if ( $recursive )
  330. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  331. else
  332. $struc['files'] = array();
  333. }
  334. $ret[ $struc['name'] ] = $struc;
  335. }
  336. return $ret;
  337. }
  338. function __destruct() {
  339. if ( $this->link )
  340. ftp_close($this->link);
  341. }
  342. }