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 {
23 public function __construct($opt = '') {
24 $this->method = 'ftpsockets';
25 $this->errors = new WP_Error();
27 // Check if possible to use ftp functions.
28 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
31 $this->ftp = new ftp();
33 if ( empty($opt['port']) )
34 $this->options['port'] = 21;
36 $this->options['port'] = $opt['port'];
38 if ( empty($opt['hostname']) )
39 $this->errors->add('empty_hostname', __('FTP hostname is required'));
41 $this->options['hostname'] = $opt['hostname'];
43 // Check if the options provided are OK.
44 if ( empty ($opt['username']) )
45 $this->errors->add('empty_username', __('FTP username is required'));
47 $this->options['username'] = $opt['username'];
49 if ( empty ($opt['password']) )
50 $this->errors->add('empty_password', __('FTP password is required'));
52 $this->options['password'] = $opt['password'];
55 public function connect() {
59 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
61 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
62 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
66 if ( ! $this->ftp->connect() ) {
67 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
71 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
72 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
76 $this->ftp->SetType( FTP_BINARY );
77 $this->ftp->Passive( true );
78 $this->ftp->setTimeout( FS_TIMEOUT );
84 * @return false|string
86 public function get_contents( $file ) {
87 if ( ! $this->exists($file) )
90 $temp = wp_tempnam( $file );
92 if ( ! $temphandle = fopen($temp, 'w+') )
95 mbstring_binary_safe_encoding();
97 if ( ! $this->ftp->fget($temphandle, $file) ) {
101 reset_mbstring_encoding();
103 return ''; // Blank document, File does exist, It's just blank.
106 reset_mbstring_encoding();
108 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
111 while ( ! feof($temphandle) )
112 $contents .= fread($temphandle, 8192);
119 * @param string $file
122 public function get_contents_array($file) {
123 return explode("\n", $this->get_contents($file) );
127 * @param string $file
128 * @param string $contents
129 * @param int|bool $mode
132 public function put_contents($file, $contents, $mode = false ) {
133 $temp = wp_tempnam( $file );
134 if ( ! $temphandle = @fopen($temp, 'w+') ) {
139 // The FTP class uses string functions internally during file download/upload
140 mbstring_binary_safe_encoding();
142 $bytes_written = fwrite( $temphandle, $contents );
143 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
144 fclose( $temphandle );
147 reset_mbstring_encoding();
152 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
154 $ret = $this->ftp->fput($file, $temphandle);
156 reset_mbstring_encoding();
161 $this->chmod($file, $mode);
166 public function cwd() {
167 $cwd = $this->ftp->pwd();
169 $cwd = trailingslashit($cwd);
173 public function chdir($file) {
174 return $this->ftp->chdir($file);
178 * @param string $file
179 * @param int|bool $mode
180 * @param bool $recursive
183 public function chmod($file, $mode = false, $recursive = false ) {
185 if ( $this->is_file($file) )
186 $mode = FS_CHMOD_FILE;
187 elseif ( $this->is_dir($file) )
188 $mode = FS_CHMOD_DIR;
193 // chmod any sub-objects if recursive.
194 if ( $recursive && $this->is_dir($file) ) {
195 $filelist = $this->dirlist($file);
196 foreach ( (array)$filelist as $filename => $filemeta )
197 $this->chmod($file . '/' . $filename, $mode, $recursive);
200 // chmod the file or directory
201 return $this->ftp->chmod($file, $mode);
205 * @param string $file
208 public function owner($file) {
209 $dir = $this->dirlist($file);
210 return $dir[$file]['owner'];
213 * @param string $file
216 public function getchmod($file) {
217 $dir = $this->dirlist($file);
218 return $dir[$file]['permsn'];
221 * @param string $file
224 public function group($file) {
225 $dir = $this->dirlist($file);
226 return $dir[$file]['group'];
229 * @param string $source
230 * @param string $destination
231 * @param bool $overwrite
232 * @param int|bool $mode
235 public function copy($source, $destination, $overwrite = false, $mode = false) {
236 if ( ! $overwrite && $this->exists($destination) )
239 $content = $this->get_contents($source);
240 if ( false === $content )
243 return $this->put_contents($destination, $content, $mode);
246 * @param string $source
247 * @param string $destination
248 * @param bool $overwrite
251 public function move($source, $destination, $overwrite = false ) {
252 return $this->ftp->rename($source, $destination);
255 * @param string $file
256 * @param bool $recursive
257 * @param string $type
260 public function delete($file, $recursive = false, $type = false) {
263 if ( 'f' == $type || $this->is_file($file) )
264 return $this->ftp->delete($file);
266 return $this->ftp->rmdir($file);
268 return $this->ftp->mdel($file);
272 * @param string $file
275 public function exists( $file ) {
276 $list = $this->ftp->nlist( $file );
278 if ( empty( $list ) && $this->is_dir( $file ) ) {
279 return true; // File is an empty directory.
282 return !empty( $list ); //empty list = no file, so invert.
283 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
287 * @param string $file
290 public function is_file($file) {
291 if ( $this->is_dir($file) )
293 if ( $this->exists($file) )
299 * @param string $path
302 public function is_dir($path) {
304 if ( $this->chdir($path) ) {
312 * @param string $file
315 public function is_readable($file) {
320 * @param string $file
323 public function is_writable($file) {
328 * @param string $file
331 public function atime($file) {
336 * @param string $file
339 public function mtime($file) {
340 return $this->ftp->mdtm($file);
344 * @param string $file
347 public function size($file) {
348 return $this->ftp->filesize($file);
351 * @param string $file
356 public function touch($file, $time = 0, $atime = 0 ) {
361 * @param string $path
362 * @param mixed $chmod
363 * @param mixed $chown
364 * @param mixed $chgrp
367 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
368 $path = untrailingslashit($path);
372 if ( ! $this->ftp->mkdir($path) )
375 $chmod = FS_CHMOD_DIR;
376 $this->chmod($path, $chmod);
382 * @param bool $recursive
384 public function rmdir($path, $recursive = false ) {
385 $this->delete($path, $recursive);
389 * @param string $path
390 * @param bool $include_hidden
391 * @param bool $recursive
394 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
395 if ( $this->is_file($path) ) {
396 $limit_file = basename($path);
397 $path = dirname($path) . '/';
402 mbstring_binary_safe_encoding();
404 $list = $this->ftp->dirlist($path);
405 if ( empty( $list ) && ! $this->exists( $path ) ) {
407 reset_mbstring_encoding();
413 foreach ( $list as $struc ) {
415 if ( '.' == $struc['name'] || '..' == $struc['name'] )
418 if ( ! $include_hidden && '.' == $struc['name'][0] )
421 if ( $limit_file && $struc['name'] != $limit_file )
424 if ( 'd' == $struc['type'] ) {
426 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
428 $struc['files'] = array();
431 // Replace symlinks formatted as "source -> target" with just the source name
432 if ( $struc['islink'] )
433 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
435 $ret[ $struc['name'] ] = $struc;
438 reset_mbstring_encoding();
443 public function __destruct() {