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 {
29 public function __construct( $opt = '' ) {
30 $this->method = 'ftpsockets';
31 $this->errors = new WP_Error();
33 // Check if possible to use ftp functions.
34 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
37 $this->ftp = new ftp();
39 if ( empty($opt['port']) )
40 $this->options['port'] = 21;
42 $this->options['port'] = (int) $opt['port'];
44 if ( empty($opt['hostname']) )
45 $this->errors->add('empty_hostname', __('FTP hostname is required'));
47 $this->options['hostname'] = $opt['hostname'];
49 // Check if the options provided are OK.
50 if ( empty ($opt['username']) )
51 $this->errors->add('empty_username', __('FTP username is required'));
53 $this->options['username'] = $opt['username'];
55 if ( empty ($opt['password']) )
56 $this->errors->add('empty_password', __('FTP password is required'));
58 $this->options['password'] = $opt['password'];
66 public function connect() {
70 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
72 if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) {
73 $this->errors->add( 'connect',
74 /* translators: %s: hostname:port */
75 sprintf( __( 'Failed to connect to FTP Server %s' ),
76 $this->options['hostname'] . ':' . $this->options['port']
82 if ( ! $this->ftp->connect() ) {
83 $this->errors->add( 'connect',
84 /* translators: %s: hostname:port */
85 sprintf( __( 'Failed to connect to FTP Server %s' ),
86 $this->options['hostname'] . ':' . $this->options['port']
92 if ( ! $this->ftp->login( $this->options['username'], $this->options['password'] ) ) {
93 $this->errors->add( 'auth',
94 /* translators: %s: username */
95 sprintf( __( 'Username/Password incorrect for %s' ),
96 $this->options['username']
102 $this->ftp->SetType( FTP_BINARY );
103 $this->ftp->Passive( true );
104 $this->ftp->setTimeout( FS_TIMEOUT );
109 * Retrieves the file contents.
114 * @param string $file Filename.
115 * @return string|false File contents on success, false if no temp file could be opened,
116 * or if the file doesn't exist.
118 public function get_contents( $file ) {
119 if ( ! $this->exists($file) )
122 $temp = wp_tempnam( $file );
124 if ( ! $temphandle = fopen( $temp, 'w+' ) ) {
129 mbstring_binary_safe_encoding();
131 if ( ! $this->ftp->fget($temphandle, $file) ) {
135 reset_mbstring_encoding();
137 return ''; // Blank document, File does exist, It's just blank.
140 reset_mbstring_encoding();
142 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
145 while ( ! feof($temphandle) )
146 $contents .= fread($temphandle, 8192);
156 * @param string $file
159 public function get_contents_array($file) {
160 return explode("\n", $this->get_contents($file) );
166 * @param string $file
167 * @param string $contents
168 * @param int|bool $mode
171 public function put_contents($file, $contents, $mode = false ) {
172 $temp = wp_tempnam( $file );
173 if ( ! $temphandle = @fopen($temp, 'w+') ) {
178 // The FTP class uses string functions internally during file download/upload
179 mbstring_binary_safe_encoding();
181 $bytes_written = fwrite( $temphandle, $contents );
182 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
183 fclose( $temphandle );
186 reset_mbstring_encoding();
191 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
193 $ret = $this->ftp->fput($file, $temphandle);
195 reset_mbstring_encoding();
200 $this->chmod($file, $mode);
210 public function cwd() {
211 $cwd = $this->ftp->pwd();
213 $cwd = trailingslashit($cwd);
220 * @param string $file
223 public function chdir($file) {
224 return $this->ftp->chdir($file);
230 * @param string $file
231 * @param int|bool $mode
232 * @param bool $recursive
235 public function chmod($file, $mode = false, $recursive = false ) {
237 if ( $this->is_file($file) )
238 $mode = FS_CHMOD_FILE;
239 elseif ( $this->is_dir($file) )
240 $mode = FS_CHMOD_DIR;
245 // chmod any sub-objects if recursive.
246 if ( $recursive && $this->is_dir($file) ) {
247 $filelist = $this->dirlist($file);
248 foreach ( (array)$filelist as $filename => $filemeta )
249 $this->chmod($file . '/' . $filename, $mode, $recursive);
252 // chmod the file or directory
253 return $this->ftp->chmod($file, $mode);
259 * @param string $file
262 public function owner($file) {
263 $dir = $this->dirlist($file);
264 return $dir[$file]['owner'];
270 * @param string $file
273 public function getchmod($file) {
274 $dir = $this->dirlist($file);
275 return $dir[$file]['permsn'];
281 * @param string $file
284 public function group($file) {
285 $dir = $this->dirlist($file);
286 return $dir[$file]['group'];
292 * @param string $source
293 * @param string $destination
294 * @param bool $overwrite
295 * @param int|bool $mode
298 public function copy($source, $destination, $overwrite = false, $mode = false) {
299 if ( ! $overwrite && $this->exists($destination) )
302 $content = $this->get_contents($source);
303 if ( false === $content )
306 return $this->put_contents($destination, $content, $mode);
312 * @param string $source
313 * @param string $destination
314 * @param bool $overwrite
317 public function move($source, $destination, $overwrite = false ) {
318 return $this->ftp->rename($source, $destination);
324 * @param string $file
325 * @param bool $recursive
326 * @param string $type
329 public function delete($file, $recursive = false, $type = false) {
332 if ( 'f' == $type || $this->is_file($file) )
333 return $this->ftp->delete($file);
335 return $this->ftp->rmdir($file);
337 return $this->ftp->mdel($file);
343 * @param string $file
346 public function exists( $file ) {
347 $list = $this->ftp->nlist( $file );
349 if ( empty( $list ) && $this->is_dir( $file ) ) {
350 return true; // File is an empty directory.
353 return !empty( $list ); //empty list = no file, so invert.
354 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
360 * @param string $file
363 public function is_file($file) {
364 if ( $this->is_dir($file) )
366 if ( $this->exists($file) )
374 * @param string $path
377 public function is_dir($path) {
379 if ( $this->chdir($path) ) {
389 * @param string $file
392 public function is_readable($file) {
399 * @param string $file
402 public function is_writable($file) {
409 * @param string $file
412 public function atime($file) {
419 * @param string $file
422 public function mtime($file) {
423 return $this->ftp->mdtm($file);
427 * @param string $file
430 public function size($file) {
431 return $this->ftp->filesize($file);
437 * @param string $file
442 public function touch($file, $time = 0, $atime = 0 ) {
449 * @param string $path
450 * @param mixed $chmod
451 * @param mixed $chown
452 * @param mixed $chgrp
455 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
456 $path = untrailingslashit($path);
460 if ( ! $this->ftp->mkdir($path) )
463 $chmod = FS_CHMOD_DIR;
464 $this->chmod($path, $chmod);
471 * @param string $path
472 * @param bool $recursive
474 public function rmdir($path, $recursive = false ) {
475 $this->delete($path, $recursive);
481 * @param string $path
482 * @param bool $include_hidden
483 * @param bool $recursive
486 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
487 if ( $this->is_file($path) ) {
488 $limit_file = basename($path);
489 $path = dirname($path) . '/';
494 mbstring_binary_safe_encoding();
496 $list = $this->ftp->dirlist($path);
497 if ( empty( $list ) && ! $this->exists( $path ) ) {
499 reset_mbstring_encoding();
505 foreach ( $list as $struc ) {
507 if ( '.' == $struc['name'] || '..' == $struc['name'] )
510 if ( ! $include_hidden && '.' == $struc['name'][0] )
513 if ( $limit_file && $struc['name'] != $limit_file )
516 if ( 'd' == $struc['type'] ) {
518 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
520 $struc['files'] = array();
523 // Replace symlinks formatted as "source -> target" with just the source name
524 if ( $struc['islink'] )
525 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
527 // Add the Octal representation of the file permissions
528 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
530 $ret[ $struc['name'] ] = $struc;
533 reset_mbstring_encoding();
541 public function __destruct() {