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 {
20 var $options = array();
22 function WP_Filesystem_ftpsockets($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' )
29 $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 ( isset($opt['base']) && ! 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'];
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_AUTOASCII);
79 $this->ftp->Passive(true);
80 $this->ftp->setTimeout(FS_TIMEOUT);
84 function get_contents($file, $type = '', $resumepos = 0) {
85 if ( ! $this->exists($file) )
89 $type = FTP_AUTOASCII;
90 $this->ftp->SetType($type);
92 $temp = wp_tempnam( $file );
94 if ( ! $temphandle = fopen($temp, 'w+') )
97 if ( ! $this->ftp->fget($temphandle, $file) ) {
100 return ''; //Blank document, File does exist, Its just blank.
103 fseek($temphandle, 0); //Skip back to the start of the file being written to
106 while ( ! feof($temphandle) )
107 $contents .= fread($temphandle, 8192);
114 function get_contents_array($file) {
115 return explode("\n", $this->get_contents($file) );
118 function put_contents($file, $contents, $type = '' ) {
120 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
122 $this->ftp->SetType($type);
124 $temp = wp_tempnam( $file );
125 if ( ! $temphandle = fopen($temp, 'w+') ) {
130 fwrite($temphandle, $contents);
131 fseek($temphandle, 0); //Skip back to the start of the file being written to
133 $ret = $this->ftp->fput($file, $temphandle);
141 $cwd = $this->ftp->pwd();
143 $cwd = trailingslashit($cwd);
147 function chdir($file) {
148 return $this->ftp->chdir($file);
151 function chgrp($file, $group, $recursive = false ) {
155 function chmod($file, $mode = false, $recursive = false ) {
158 if ( $this->is_file($file) )
159 $mode = FS_CHMOD_FILE;
160 elseif ( $this->is_dir($file) )
161 $mode = FS_CHMOD_DIR;
166 if ( ! $recursive || ! $this->is_dir($file) ) {
167 return $this->ftp->chmod($file, $mode);
170 //Is a directory, and we want recursive
171 $filelist = $this->dirlist($file);
172 foreach ( $filelist as $filename )
173 $this->chmod($file . '/' . $filename, $mode, $recursive);
178 function chown($file, $owner, $recursive = false ) {
182 function owner($file) {
183 $dir = $this->dirlist($file);
184 return $dir[$file]['owner'];
187 function getchmod($file) {
188 $dir = $this->dirlist($file);
189 return $dir[$file]['permsn'];
192 function group($file) {
193 $dir = $this->dirlist($file);
194 return $dir[$file]['group'];
197 function copy($source, $destination, $overwrite = false ) {
198 if ( ! $overwrite && $this->exists($destination) )
201 $content = $this->get_contents($source);
202 if ( false === $content )
205 return $this->put_contents($destination, $content);
208 function move($source, $destination, $overwrite = false ) {
209 return $this->ftp->rename($source, $destination);
212 function delete($file, $recursive = false ) {
215 if ( $this->is_file($file) )
216 return $this->ftp->delete($file);
218 return $this->ftp->rmdir($file);
220 return $this->ftp->mdel($file);
223 function exists($file) {
224 return $this->ftp->is_exists($file);
227 function is_file($file) {
228 return $this->is_dir($file) ? false : true;
231 function is_dir($path) {
233 if ( $this->chdir($path) ) {
240 function is_readable($file) {
241 //Get dir list, Check if the file is writable by the current user??
245 function is_writable($file) {
246 //Get dir list, Check if the file is writable by the current user??
250 function atime($file) {
254 function mtime($file) {
255 return $this->ftp->mdtm($file);
258 function size($file) {
259 return $this->ftp->filesize($file);
262 function touch($file, $time = 0, $atime = 0 ) {
266 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
267 if ( ! $this->ftp->mkdir($path) )
270 $chmod = FS_CHMOD_DIR;
271 $this->chmod($path, $chmod);
273 $this->chown($path, $chown);
275 $this->chgrp($path, $chgrp);
279 function rmdir($path, $recursive = false ) {
281 return $this->ftp->rmdir($path);
283 return $this->ftp->mdel($path);
286 function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
287 if ( $this->is_file($path) ) {
288 $limit_file = basename($path);
289 $path = dirname($path) . '/';
294 $list = $this->ftp->dirlist($path);
299 foreach ( $list as $struc ) {
301 if ( '.' == $struc['name'] || '..' == $struc['name'] )
304 if ( ! $include_hidden && '.' == $struc['name'][0] )
307 if ( $limit_file && $struc['name'] != $limit_file )
310 if ( 'd' == $struc['type'] ) {
312 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
314 $struc['files'] = array();
317 $ret[ $struc['name'] ] = $struc;
322 function __destruct() {