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 ( ! 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, $mode = false ) {
119 $temp = wp_tempnam( $file );
120 if ( ! $temphandle = @fopen($temp, 'w+') ) {
125 fwrite($temphandle, $contents);
126 fseek($temphandle, 0); //Skip back to the start of the file being written to
128 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
129 $this->ftp->SetType($type);
131 $ret = $this->ftp->fput($file, $temphandle);
136 $this->chmod($file, $mode);
142 $cwd = $this->ftp->pwd();
144 $cwd = trailingslashit($cwd);
148 function chdir($file) {
149 return $this->ftp->chdir($file);
152 function chgrp($file, $group, $recursive = false ) {
156 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 // chmod any sub-objects if recursive.
167 if ( $recursive && $this->is_dir($file) ) {
168 $filelist = $this->dirlist($file);
169 foreach ( (array)$filelist as $filename => $filemeta )
170 $this->chmod($file . '/' . $filename, $mode, $recursive);
173 // chmod the file or directory
174 return $this->ftp->chmod($file, $mode);
177 function chown($file, $owner, $recursive = false ) {
181 function owner($file) {
182 $dir = $this->dirlist($file);
183 return $dir[$file]['owner'];
186 function getchmod($file) {
187 $dir = $this->dirlist($file);
188 return $dir[$file]['permsn'];
191 function group($file) {
192 $dir = $this->dirlist($file);
193 return $dir[$file]['group'];
196 function copy($source, $destination, $overwrite = false ) {
197 if ( ! $overwrite && $this->exists($destination) )
200 $content = $this->get_contents($source);
201 if ( false === $content )
204 return $this->put_contents($destination, $content);
207 function move($source, $destination, $overwrite = false ) {
208 return $this->ftp->rename($source, $destination);
211 function delete($file, $recursive = false ) {
214 if ( $this->is_file($file) )
215 return $this->ftp->delete($file);
217 return $this->ftp->rmdir($file);
219 return $this->ftp->mdel($file);
222 function exists($file) {
223 return $this->ftp->is_exists($file);
226 function is_file($file) {
227 if ( $this->is_dir($file) )
229 if ( $this->exists($file) )
234 function is_dir($path) {
236 if ( $this->chdir($path) ) {
243 function is_readable($file) {
244 //Get dir list, Check if the file is writable by the current user??
248 function is_writable($file) {
249 //Get dir list, Check if the file is writable by the current user??
253 function atime($file) {
257 function mtime($file) {
258 return $this->ftp->mdtm($file);
261 function size($file) {
262 return $this->ftp->filesize($file);
265 function touch($file, $time = 0, $atime = 0 ) {
269 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
270 if ( ! $this->ftp->mkdir($path) )
273 $chmod = FS_CHMOD_DIR;
274 $this->chmod($path, $chmod);
276 $this->chown($path, $chown);
278 $this->chgrp($path, $chgrp);
282 function rmdir($path, $recursive = false ) {
283 $this->delete($path, $recursive);
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);
295 if ( empty($list) && !$this->exists($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() {