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 );
88 public function get_contents( $file ) {
89 if ( ! $this->exists($file) )
92 $temp = wp_tempnam( $file );
94 if ( ! $temphandle = fopen($temp, 'w+') )
97 mbstring_binary_safe_encoding();
99 if ( ! $this->ftp->fget($temphandle, $file) ) {
103 reset_mbstring_encoding();
105 return ''; // Blank document, File does exist, It's just blank.
108 reset_mbstring_encoding();
110 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
113 while ( ! feof($temphandle) )
114 $contents .= fread($temphandle, 8192);
121 * @param string $file
124 public function get_contents_array($file) {
125 return explode("\n", $this->get_contents($file) );
129 * @param string $file
130 * @param string $contents
131 * @param int|bool $mode
134 public function put_contents($file, $contents, $mode = false ) {
135 $temp = wp_tempnam( $file );
136 if ( ! $temphandle = @fopen($temp, 'w+') ) {
141 // The FTP class uses string functions internally during file download/upload
142 mbstring_binary_safe_encoding();
144 $bytes_written = fwrite( $temphandle, $contents );
145 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
146 fclose( $temphandle );
149 reset_mbstring_encoding();
154 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
156 $ret = $this->ftp->fput($file, $temphandle);
158 reset_mbstring_encoding();
163 $this->chmod($file, $mode);
168 public function cwd() {
169 $cwd = $this->ftp->pwd();
171 $cwd = trailingslashit($cwd);
175 public function chdir($file) {
176 return $this->ftp->chdir($file);
180 * @param string $file
182 * @param bool $recursive
184 public function chgrp($file, $group, $recursive = false ) {
189 * @param string $file
190 * @param int|bool $mode
191 * @param bool $recursive
194 public function chmod($file, $mode = false, $recursive = false ) {
196 if ( $this->is_file($file) )
197 $mode = FS_CHMOD_FILE;
198 elseif ( $this->is_dir($file) )
199 $mode = FS_CHMOD_DIR;
204 // chmod any sub-objects if recursive.
205 if ( $recursive && $this->is_dir($file) ) {
206 $filelist = $this->dirlist($file);
207 foreach ( (array)$filelist as $filename => $filemeta )
208 $this->chmod($file . '/' . $filename, $mode, $recursive);
211 // chmod the file or directory
212 return $this->ftp->chmod($file, $mode);
216 * @param string $file
219 public function owner($file) {
220 $dir = $this->dirlist($file);
221 return $dir[$file]['owner'];
224 * @param string $file
227 public function getchmod($file) {
228 $dir = $this->dirlist($file);
229 return $dir[$file]['permsn'];
232 * @param string $file
235 public function group($file) {
236 $dir = $this->dirlist($file);
237 return $dir[$file]['group'];
240 * @param string $source
241 * @param string $destination
242 * @param bool $overwrite
243 * @param int|bool $mode
246 public function copy($source, $destination, $overwrite = false, $mode = false) {
247 if ( ! $overwrite && $this->exists($destination) )
250 $content = $this->get_contents($source);
251 if ( false === $content )
254 return $this->put_contents($destination, $content, $mode);
257 * @param string $source
258 * @param string $destination
259 * @param bool $overwrite
262 public function move($source, $destination, $overwrite = false ) {
263 return $this->ftp->rename($source, $destination);
266 * @param string $file
267 * @param bool $recursive
268 * @param string $type
271 public function delete($file, $recursive = false, $type = false) {
274 if ( 'f' == $type || $this->is_file($file) )
275 return $this->ftp->delete($file);
277 return $this->ftp->rmdir($file);
279 return $this->ftp->mdel($file);
283 * @param string $file
286 public function exists( $file ) {
287 $list = $this->ftp->nlist( $file );
288 return !empty( $list ); //empty list = no file, so invert.
289 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
293 * @param string $file
296 public function is_file($file) {
297 if ( $this->is_dir($file) )
299 if ( $this->exists($file) )
305 * @param string $path
308 public function is_dir($path) {
310 if ( $this->chdir($path) ) {
318 * @param string $file
321 public function is_readable($file) {
326 * @param string $file
329 public function is_writable($file) {
334 * @param string $file
337 public function atime($file) {
342 * @param string $file
345 public function mtime($file) {
346 return $this->ftp->mdtm($file);
350 * @param string $file
353 public function size($file) {
354 return $this->ftp->filesize($file);
357 * @param string $file
362 public function touch($file, $time = 0, $atime = 0 ) {
367 * @param string $path
368 * @param mixed $chmod
369 * @param mixed $chown
370 * @param mixed $chgrp
373 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
374 $path = untrailingslashit($path);
378 if ( ! $this->ftp->mkdir($path) )
381 $chmod = FS_CHMOD_DIR;
382 $this->chmod($path, $chmod);
384 $this->chown($path, $chown);
386 $this->chgrp($path, $chgrp);
392 * @param bool $recursive
394 public function rmdir($path, $recursive = false ) {
395 $this->delete($path, $recursive);
399 * @param string $path
400 * @param bool $include_hidden
401 * @param bool $recursive
404 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
405 if ( $this->is_file($path) ) {
406 $limit_file = basename($path);
407 $path = dirname($path) . '/';
412 mbstring_binary_safe_encoding();
414 $list = $this->ftp->dirlist($path);
415 if ( empty( $list ) && ! $this->exists( $path ) ) {
417 reset_mbstring_encoding();
423 foreach ( $list as $struc ) {
425 if ( '.' == $struc['name'] || '..' == $struc['name'] )
428 if ( ! $include_hidden && '.' == $struc['name'][0] )
431 if ( $limit_file && $struc['name'] != $limit_file )
434 if ( 'd' == $struc['type'] ) {
436 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
438 $struc['files'] = array();
441 // Replace symlinks formatted as "source -> target" with just the source name
442 if ( $struc['islink'] )
443 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
445 $ret[ $struc['name'] ] = $struc;
448 reset_mbstring_encoding();
453 public function __destruct() {