3 * WordPress FTP Sockets Filesystem.
6 * @subpackage Filesystem
10 * WordPress Filesystem Class for implementing FTP Sockets.
14 * @subpackage Filesystem
15 * @uses WP_Filesystem_Base Extends class
17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
19 public $errors = null;
20 public $options = array();
22 public function __construct($opt = '') {
23 $this->method = 'ftpsockets';
24 $this->errors = new WP_Error();
26 // Check if possible to use ftp functions.
27 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
30 $this->ftp = new ftp();
32 if ( empty($opt['port']) )
33 $this->options['port'] = 21;
35 $this->options['port'] = $opt['port'];
37 if ( empty($opt['hostname']) )
38 $this->errors->add('empty_hostname', __('FTP hostname is required'));
40 $this->options['hostname'] = $opt['hostname'];
42 if ( ! empty($opt['base']) )
43 $this->wp_base = $opt['base'];
45 // Check if the options provided are OK.
46 if ( empty ($opt['username']) )
47 $this->errors->add('empty_username', __('FTP username is required'));
49 $this->options['username'] = $opt['username'];
51 if ( empty ($opt['password']) )
52 $this->errors->add('empty_password', __('FTP password is required'));
54 $this->options['password'] = $opt['password'];
57 public function connect() {
61 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
63 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
64 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
68 if ( ! $this->ftp->connect() ) {
69 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
73 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
74 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
78 $this->ftp->SetType( FTP_BINARY );
79 $this->ftp->Passive( true );
80 $this->ftp->setTimeout( FS_TIMEOUT );
84 public function get_contents( $file ) {
85 if ( ! $this->exists($file) )
88 $temp = wp_tempnam( $file );
90 if ( ! $temphandle = fopen($temp, 'w+') )
93 mbstring_binary_safe_encoding();
95 if ( ! $this->ftp->fget($temphandle, $file) ) {
99 reset_mbstring_encoding();
101 return ''; // Blank document, File does exist, It's just blank.
104 reset_mbstring_encoding();
106 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
109 while ( ! feof($temphandle) )
110 $contents .= fread($temphandle, 8192);
117 public function get_contents_array($file) {
118 return explode("\n", $this->get_contents($file) );
121 public function put_contents($file, $contents, $mode = false ) {
122 $temp = wp_tempnam( $file );
123 if ( ! $temphandle = @fopen($temp, 'w+') ) {
128 // The FTP class uses string functions internally during file download/upload
129 mbstring_binary_safe_encoding();
131 $bytes_written = fwrite( $temphandle, $contents );
132 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
133 fclose( $temphandle );
136 reset_mbstring_encoding();
141 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
143 $ret = $this->ftp->fput($file, $temphandle);
145 reset_mbstring_encoding();
150 $this->chmod($file, $mode);
155 public function cwd() {
156 $cwd = $this->ftp->pwd();
158 $cwd = trailingslashit($cwd);
162 public function chdir($file) {
163 return $this->ftp->chdir($file);
166 public function chgrp($file, $group, $recursive = false ) {
170 public function chmod($file, $mode = false, $recursive = false ) {
172 if ( $this->is_file($file) )
173 $mode = FS_CHMOD_FILE;
174 elseif ( $this->is_dir($file) )
175 $mode = FS_CHMOD_DIR;
180 // chmod any sub-objects if recursive.
181 if ( $recursive && $this->is_dir($file) ) {
182 $filelist = $this->dirlist($file);
183 foreach ( (array)$filelist as $filename => $filemeta )
184 $this->chmod($file . '/' . $filename, $mode, $recursive);
187 // chmod the file or directory
188 return $this->ftp->chmod($file, $mode);
191 public function owner($file) {
192 $dir = $this->dirlist($file);
193 return $dir[$file]['owner'];
196 public function getchmod($file) {
197 $dir = $this->dirlist($file);
198 return $dir[$file]['permsn'];
201 public function group($file) {
202 $dir = $this->dirlist($file);
203 return $dir[$file]['group'];
206 public function copy($source, $destination, $overwrite = false, $mode = false) {
207 if ( ! $overwrite && $this->exists($destination) )
210 $content = $this->get_contents($source);
211 if ( false === $content )
214 return $this->put_contents($destination, $content, $mode);
217 public function move($source, $destination, $overwrite = false ) {
218 return $this->ftp->rename($source, $destination);
221 public function delete($file, $recursive = false, $type = false) {
224 if ( 'f' == $type || $this->is_file($file) )
225 return $this->ftp->delete($file);
227 return $this->ftp->rmdir($file);
229 return $this->ftp->mdel($file);
232 public function exists( $file ) {
233 $list = $this->ftp->nlist( $file );
234 return !empty( $list ); //empty list = no file, so invert.
235 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
238 public function is_file($file) {
239 if ( $this->is_dir($file) )
241 if ( $this->exists($file) )
246 public function is_dir($path) {
248 if ( $this->chdir($path) ) {
255 public function is_readable($file) {
259 public function is_writable($file) {
263 public function atime($file) {
267 public function mtime($file) {
268 return $this->ftp->mdtm($file);
271 public function size($file) {
272 return $this->ftp->filesize($file);
275 public function touch($file, $time = 0, $atime = 0 ) {
279 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
280 $path = untrailingslashit($path);
284 if ( ! $this->ftp->mkdir($path) )
287 $chmod = FS_CHMOD_DIR;
288 $this->chmod($path, $chmod);
290 $this->chown($path, $chown);
292 $this->chgrp($path, $chgrp);
296 public function rmdir($path, $recursive = false ) {
297 $this->delete($path, $recursive);
300 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
301 if ( $this->is_file($path) ) {
302 $limit_file = basename($path);
303 $path = dirname($path) . '/';
308 mbstring_binary_safe_encoding();
310 $list = $this->ftp->dirlist($path);
311 if ( empty( $list ) && ! $this->exists( $path ) ) {
313 reset_mbstring_encoding();
319 foreach ( $list as $struc ) {
321 if ( '.' == $struc['name'] || '..' == $struc['name'] )
324 if ( ! $include_hidden && '.' == $struc['name'][0] )
327 if ( $limit_file && $struc['name'] != $limit_file )
330 if ( 'd' == $struc['type'] ) {
332 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
334 $struc['files'] = array();
337 // Replace symlinks formatted as "source -> target" with just the source name
338 if ( $struc['islink'] )
339 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
341 $ret[ $struc['name'] ] = $struc;
344 reset_mbstring_encoding();
349 public function __destruct() {