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

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

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