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 {
27 public function __construct( $opt = '' ) {
28 $this->method = 'ftpsockets';
29 $this->errors = new WP_Error();
31 // Check if possible to use ftp functions.
32 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
35 $this->ftp = new ftp();
37 if ( empty($opt['port']) )
38 $this->options['port'] = 21;
40 $this->options['port'] = $opt['port'];
42 if ( empty($opt['hostname']) )
43 $this->errors->add('empty_hostname', __('FTP hostname is required'));
45 $this->options['hostname'] = $opt['hostname'];
47 // Check if the options provided are OK.
48 if ( empty ($opt['username']) )
49 $this->errors->add('empty_username', __('FTP username is required'));
51 $this->options['username'] = $opt['username'];
53 if ( empty ($opt['password']) )
54 $this->errors->add('empty_password', __('FTP password is required'));
56 $this->options['password'] = $opt['password'];
63 public function connect() {
67 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
69 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
70 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
74 if ( ! $this->ftp->connect() ) {
75 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
79 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
80 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
84 $this->ftp->SetType( FTP_BINARY );
85 $this->ftp->Passive( true );
86 $this->ftp->setTimeout( FS_TIMEOUT );
92 * @return false|string
94 public function get_contents( $file ) {
95 if ( ! $this->exists($file) )
98 $temp = wp_tempnam( $file );
100 if ( ! $temphandle = fopen($temp, 'w+') )
103 mbstring_binary_safe_encoding();
105 if ( ! $this->ftp->fget($temphandle, $file) ) {
109 reset_mbstring_encoding();
111 return ''; // Blank document, File does exist, It's just blank.
114 reset_mbstring_encoding();
116 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
119 while ( ! feof($temphandle) )
120 $contents .= fread($temphandle, 8192);
127 * @param string $file
130 public function get_contents_array($file) {
131 return explode("\n", $this->get_contents($file) );
135 * @param string $file
136 * @param string $contents
137 * @param int|bool $mode
140 public function put_contents($file, $contents, $mode = false ) {
141 $temp = wp_tempnam( $file );
142 if ( ! $temphandle = @fopen($temp, 'w+') ) {
147 // The FTP class uses string functions internally during file download/upload
148 mbstring_binary_safe_encoding();
150 $bytes_written = fwrite( $temphandle, $contents );
151 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
152 fclose( $temphandle );
155 reset_mbstring_encoding();
160 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
162 $ret = $this->ftp->fput($file, $temphandle);
164 reset_mbstring_encoding();
169 $this->chmod($file, $mode);
178 public function cwd() {
179 $cwd = $this->ftp->pwd();
181 $cwd = trailingslashit($cwd);
187 * @param string $file
190 public function chdir($file) {
191 return $this->ftp->chdir($file);
195 * @param string $file
196 * @param int|bool $mode
197 * @param bool $recursive
200 public function chmod($file, $mode = false, $recursive = false ) {
202 if ( $this->is_file($file) )
203 $mode = FS_CHMOD_FILE;
204 elseif ( $this->is_dir($file) )
205 $mode = FS_CHMOD_DIR;
210 // chmod any sub-objects if recursive.
211 if ( $recursive && $this->is_dir($file) ) {
212 $filelist = $this->dirlist($file);
213 foreach ( (array)$filelist as $filename => $filemeta )
214 $this->chmod($file . '/' . $filename, $mode, $recursive);
217 // chmod the file or directory
218 return $this->ftp->chmod($file, $mode);
222 * @param string $file
225 public function owner($file) {
226 $dir = $this->dirlist($file);
227 return $dir[$file]['owner'];
230 * @param string $file
233 public function getchmod($file) {
234 $dir = $this->dirlist($file);
235 return $dir[$file]['permsn'];
238 * @param string $file
241 public function group($file) {
242 $dir = $this->dirlist($file);
243 return $dir[$file]['group'];
246 * @param string $source
247 * @param string $destination
248 * @param bool $overwrite
249 * @param int|bool $mode
252 public function copy($source, $destination, $overwrite = false, $mode = false) {
253 if ( ! $overwrite && $this->exists($destination) )
256 $content = $this->get_contents($source);
257 if ( false === $content )
260 return $this->put_contents($destination, $content, $mode);
263 * @param string $source
264 * @param string $destination
265 * @param bool $overwrite
268 public function move($source, $destination, $overwrite = false ) {
269 return $this->ftp->rename($source, $destination);
272 * @param string $file
273 * @param bool $recursive
274 * @param string $type
277 public function delete($file, $recursive = false, $type = false) {
280 if ( 'f' == $type || $this->is_file($file) )
281 return $this->ftp->delete($file);
283 return $this->ftp->rmdir($file);
285 return $this->ftp->mdel($file);
289 * @param string $file
292 public function exists( $file ) {
293 $list = $this->ftp->nlist( $file );
295 if ( empty( $list ) && $this->is_dir( $file ) ) {
296 return true; // File is an empty directory.
299 return !empty( $list ); //empty list = no file, so invert.
300 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
304 * @param string $file
307 public function is_file($file) {
308 if ( $this->is_dir($file) )
310 if ( $this->exists($file) )
316 * @param string $path
319 public function is_dir($path) {
321 if ( $this->chdir($path) ) {
329 * @param string $file
332 public function is_readable($file) {
337 * @param string $file
340 public function is_writable($file) {
345 * @param string $file
348 public function atime($file) {
353 * @param string $file
356 public function mtime($file) {
357 return $this->ftp->mdtm($file);
361 * @param string $file
364 public function size($file) {
365 return $this->ftp->filesize($file);
368 * @param string $file
373 public function touch($file, $time = 0, $atime = 0 ) {
378 * @param string $path
379 * @param mixed $chmod
380 * @param mixed $chown
381 * @param mixed $chgrp
384 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
385 $path = untrailingslashit($path);
389 if ( ! $this->ftp->mkdir($path) )
392 $chmod = FS_CHMOD_DIR;
393 $this->chmod($path, $chmod);
399 * @param bool $recursive
401 public function rmdir($path, $recursive = false ) {
402 $this->delete($path, $recursive);
406 * @param string $path
407 * @param bool $include_hidden
408 * @param bool $recursive
411 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
412 if ( $this->is_file($path) ) {
413 $limit_file = basename($path);
414 $path = dirname($path) . '/';
419 mbstring_binary_safe_encoding();
421 $list = $this->ftp->dirlist($path);
422 if ( empty( $list ) && ! $this->exists( $path ) ) {
424 reset_mbstring_encoding();
430 foreach ( $list as $struc ) {
432 if ( '.' == $struc['name'] || '..' == $struc['name'] )
435 if ( ! $include_hidden && '.' == $struc['name'][0] )
438 if ( $limit_file && $struc['name'] != $limit_file )
441 if ( 'd' == $struc['type'] ) {
443 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
445 $struc['files'] = array();
448 // Replace symlinks formatted as "source -> target" with just the source name
449 if ( $struc['islink'] )
450 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
452 // Add the Octal representation of the file permissions
453 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
455 $ret[ $struc['name'] ] = $struc;
458 reset_mbstring_encoding();
466 public function __destruct() {