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 {
20 var $options = array();
22 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' )
29 $this->ftp = new ftp();
31 if ( empty($opt['port']) )
32 $this->options['port'] = 21;
34 $this->options['port'] = $opt['port'];
36 if ( empty($opt['hostname']) )
37 $this->errors->add('empty_hostname', __('FTP hostname is required'));
39 $this->options['hostname'] = $opt['hostname'];
41 if ( ! empty($opt['base']) )
42 $this->wp_base = $opt['base'];
44 // Check if the options provided are OK.
45 if ( empty ($opt['username']) )
46 $this->errors->add('empty_username', __('FTP username is required'));
48 $this->options['username'] = $opt['username'];
50 if ( empty ($opt['password']) )
51 $this->errors->add('empty_password', __('FTP password is required'));
53 $this->options['password'] = $opt['password'];
60 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
62 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
63 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
67 if ( ! $this->ftp->connect() ) {
68 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
72 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
73 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
77 $this->ftp->SetType( FTP_BINARY );
78 $this->ftp->Passive( true );
79 $this->ftp->setTimeout( FS_TIMEOUT );
83 function get_contents( $file ) {
84 if ( ! $this->exists($file) )
87 $temp = wp_tempnam( $file );
89 if ( ! $temphandle = fopen($temp, 'w+') )
92 mbstring_binary_safe_encoding();
94 if ( ! $this->ftp->fget($temphandle, $file) ) {
98 reset_mbstring_encoding();
100 return ''; // Blank document, File does exist, It's just blank.
103 reset_mbstring_encoding();
105 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
108 while ( ! feof($temphandle) )
109 $contents .= fread($temphandle, 8192);
116 function get_contents_array($file) {
117 return explode("\n", $this->get_contents($file) );
120 function put_contents($file, $contents, $mode = false ) {
121 $temp = wp_tempnam( $file );
122 if ( ! $temphandle = @fopen($temp, 'w+') ) {
127 // The FTP class uses string functions internally during file download/upload
128 mbstring_binary_safe_encoding();
130 $bytes_written = fwrite( $temphandle, $contents );
131 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
132 fclose( $temphandle );
135 reset_mbstring_encoding();
140 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
142 $ret = $this->ftp->fput($file, $temphandle);
144 reset_mbstring_encoding();
149 $this->chmod($file, $mode);
155 $cwd = $this->ftp->pwd();
157 $cwd = trailingslashit($cwd);
161 function chdir($file) {
162 return $this->ftp->chdir($file);
165 function chgrp($file, $group, $recursive = false ) {
169 function chmod($file, $mode = false, $recursive = false ) {
171 if ( $this->is_file($file) )
172 $mode = FS_CHMOD_FILE;
173 elseif ( $this->is_dir($file) )
174 $mode = FS_CHMOD_DIR;
179 // chmod any sub-objects if recursive.
180 if ( $recursive && $this->is_dir($file) ) {
181 $filelist = $this->dirlist($file);
182 foreach ( (array)$filelist as $filename => $filemeta )
183 $this->chmod($file . '/' . $filename, $mode, $recursive);
186 // chmod the file or directory
187 return $this->ftp->chmod($file, $mode);
190 function owner($file) {
191 $dir = $this->dirlist($file);
192 return $dir[$file]['owner'];
195 function getchmod($file) {
196 $dir = $this->dirlist($file);
197 return $dir[$file]['permsn'];
200 function group($file) {
201 $dir = $this->dirlist($file);
202 return $dir[$file]['group'];
205 function copy($source, $destination, $overwrite = false, $mode = false) {
206 if ( ! $overwrite && $this->exists($destination) )
209 $content = $this->get_contents($source);
210 if ( false === $content )
213 return $this->put_contents($destination, $content, $mode);
216 function move($source, $destination, $overwrite = false ) {
217 return $this->ftp->rename($source, $destination);
220 function delete($file, $recursive = false, $type = false) {
223 if ( 'f' == $type || $this->is_file($file) )
224 return $this->ftp->delete($file);
226 return $this->ftp->rmdir($file);
228 return $this->ftp->mdel($file);
231 function exists( $file ) {
232 $list = $this->ftp->nlist( $file );
233 return !empty( $list ); //empty list = no file, so invert.
234 // return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server
237 function is_file($file) {
238 if ( $this->is_dir($file) )
240 if ( $this->exists($file) )
245 function is_dir($path) {
247 if ( $this->chdir($path) ) {
254 function is_readable($file) {
258 function is_writable($file) {
262 function atime($file) {
266 function mtime($file) {
267 return $this->ftp->mdtm($file);
270 function size($file) {
271 return $this->ftp->filesize($file);
274 function touch($file, $time = 0, $atime = 0 ) {
278 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
279 $path = untrailingslashit($path);
283 if ( ! $this->ftp->mkdir($path) )
286 $chmod = FS_CHMOD_DIR;
287 $this->chmod($path, $chmod);
289 $this->chown($path, $chown);
291 $this->chgrp($path, $chgrp);
295 function rmdir($path, $recursive = false ) {
296 $this->delete($path, $recursive);
299 function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
300 if ( $this->is_file($path) ) {
301 $limit_file = basename($path);
302 $path = dirname($path) . '/';
307 mbstring_binary_safe_encoding();
309 $list = $this->ftp->dirlist($path);
310 if ( empty( $list ) && ! $this->exists( $path ) ) {
312 reset_mbstring_encoding();
318 foreach ( $list as $struc ) {
320 if ( '.' == $struc['name'] || '..' == $struc['name'] )
323 if ( ! $include_hidden && '.' == $struc['name'][0] )
326 if ( $limit_file && $struc['name'] != $limit_file )
329 if ( 'd' == $struc['type'] ) {
331 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
333 $struc['files'] = array();
336 // Replace symlinks formatted as "source -> target" with just the source name
337 if ( $struc['islink'] )
338 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
340 $ret[ $struc['name'] ] = $struc;
343 reset_mbstring_encoding();
348 function __destruct() {