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+') )
127 mbstring_binary_safe_encoding();
129 if ( ! $this->ftp->fget($temphandle, $file) ) {
133 reset_mbstring_encoding();
135 return ''; // Blank document, File does exist, It's just blank.
138 reset_mbstring_encoding();
140 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
143 while ( ! feof($temphandle) )
144 $contents .= fread($temphandle, 8192);
154 * @param string $file
157 public function get_contents_array($file) {
158 return explode("\n", $this->get_contents($file) );
164 * @param string $file
165 * @param string $contents
166 * @param int|bool $mode
169 public function put_contents($file, $contents, $mode = false ) {
170 $temp = wp_tempnam( $file );
171 if ( ! $temphandle = @fopen($temp, 'w+') ) {
176 // The FTP class uses string functions internally during file download/upload
177 mbstring_binary_safe_encoding();
179 $bytes_written = fwrite( $temphandle, $contents );
180 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
181 fclose( $temphandle );
184 reset_mbstring_encoding();
189 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
191 $ret = $this->ftp->fput($file, $temphandle);
193 reset_mbstring_encoding();
198 $this->chmod($file, $mode);
208 public function cwd() {
209 $cwd = $this->ftp->pwd();
211 $cwd = trailingslashit($cwd);
218 * @param string $file
221 public function chdir($file) {
222 return $this->ftp->chdir($file);
228 * @param string $file
229 * @param int|bool $mode
230 * @param bool $recursive
233 public function chmod($file, $mode = false, $recursive = false ) {
235 if ( $this->is_file($file) )
236 $mode = FS_CHMOD_FILE;
237 elseif ( $this->is_dir($file) )
238 $mode = FS_CHMOD_DIR;
243 // chmod any sub-objects if recursive.
244 if ( $recursive && $this->is_dir($file) ) {
245 $filelist = $this->dirlist($file);
246 foreach ( (array)$filelist as $filename => $filemeta )
247 $this->chmod($file . '/' . $filename, $mode, $recursive);
250 // chmod the file or directory
251 return $this->ftp->chmod($file, $mode);
257 * @param string $file
260 public function owner($file) {
261 $dir = $this->dirlist($file);
262 return $dir[$file]['owner'];
268 * @param string $file
271 public function getchmod($file) {
272 $dir = $this->dirlist($file);
273 return $dir[$file]['permsn'];
279 * @param string $file
282 public function group($file) {
283 $dir = $this->dirlist($file);
284 return $dir[$file]['group'];
290 * @param string $source
291 * @param string $destination
292 * @param bool $overwrite
293 * @param int|bool $mode
296 public function copy($source, $destination, $overwrite = false, $mode = false) {
297 if ( ! $overwrite && $this->exists($destination) )
300 $content = $this->get_contents($source);
301 if ( false === $content )
304 return $this->put_contents($destination, $content, $mode);
310 * @param string $source
311 * @param string $destination
312 * @param bool $overwrite
315 public function move($source, $destination, $overwrite = false ) {
316 return $this->ftp->rename($source, $destination);
322 * @param string $file
323 * @param bool $recursive
324 * @param string $type
327 public function delete($file, $recursive = false, $type = false) {
330 if ( 'f' == $type || $this->is_file($file) )
331 return $this->ftp->delete($file);
333 return $this->ftp->rmdir($file);
335 return $this->ftp->mdel($file);
341 * @param string $file
344 public function exists( $file ) {
345 $list = $this->ftp->nlist( $file );
347 if ( empty( $list ) && $this->is_dir( $file ) ) {
348 return true; // File is an empty directory.
351 return !empty( $list ); //empty list = no file, so invert.
352 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
358 * @param string $file
361 public function is_file($file) {
362 if ( $this->is_dir($file) )
364 if ( $this->exists($file) )
372 * @param string $path
375 public function is_dir($path) {
377 if ( $this->chdir($path) ) {
387 * @param string $file
390 public function is_readable($file) {
397 * @param string $file
400 public function is_writable($file) {
407 * @param string $file
410 public function atime($file) {
417 * @param string $file
420 public function mtime($file) {
421 return $this->ftp->mdtm($file);
425 * @param string $file
428 public function size($file) {
429 return $this->ftp->filesize($file);
435 * @param string $file
440 public function touch($file, $time = 0, $atime = 0 ) {
447 * @param string $path
448 * @param mixed $chmod
449 * @param mixed $chown
450 * @param mixed $chgrp
453 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
454 $path = untrailingslashit($path);
458 if ( ! $this->ftp->mkdir($path) )
461 $chmod = FS_CHMOD_DIR;
462 $this->chmod($path, $chmod);
469 * @param string $path
470 * @param bool $recursive
472 public function rmdir($path, $recursive = false ) {
473 $this->delete($path, $recursive);
479 * @param string $path
480 * @param bool $include_hidden
481 * @param bool $recursive
484 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
485 if ( $this->is_file($path) ) {
486 $limit_file = basename($path);
487 $path = dirname($path) . '/';
492 mbstring_binary_safe_encoding();
494 $list = $this->ftp->dirlist($path);
495 if ( empty( $list ) && ! $this->exists( $path ) ) {
497 reset_mbstring_encoding();
503 foreach ( $list as $struc ) {
505 if ( '.' == $struc['name'] || '..' == $struc['name'] )
508 if ( ! $include_hidden && '.' == $struc['name'][0] )
511 if ( $limit_file && $struc['name'] != $limit_file )
514 if ( 'd' == $struc['type'] ) {
516 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
518 $struc['files'] = array();
521 // Replace symlinks formatted as "source -> target" with just the source name
522 if ( $struc['islink'] )
523 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
525 // Add the Octal representation of the file permissions
526 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
528 $ret[ $struc['name'] ] = $struc;
531 reset_mbstring_encoding();
539 public function __destruct() {