2 class WP_Filesystem_FTPext extends WP_Filesystem_Base{
6 var $options = array();
8 var $permission = null;
10 var $filetypes = array(
25 function WP_Filesystem_FTPext($opt='') {
26 $this->method = 'ftpext';
27 $this->errors = new WP_Error();
29 //Check if possible to use ftp functions.
30 if ( ! extension_loaded('ftp') ) {
31 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
36 if ( empty($opt['port']) )
37 $this->options['port'] = 21;
39 $this->options['port'] = $opt['port'];
41 if ( empty($opt['hostname']) )
42 $this->errors->add('empty_hostname', __('FTP hostname is required'));
44 $this->options['hostname'] = $opt['hostname'];
46 if ( isset($opt['base']) && ! empty($opt['base']) )
47 $this->wp_base = $opt['base'];
49 // Check if the options provided are OK.
50 if ( empty ($opt['username']) )
51 $this->errors->add('empty_username', __('FTP username is required'));
53 $this->options['username'] = $opt['username'];
55 if ( empty ($opt['password']) )
56 $this->errors->add('empty_password', __('FTP password is required'));
58 $this->options['password'] = $opt['password'];
60 $this->options['ssl'] = ( !empty($opt['ssl']) );
64 if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
65 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
67 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
69 if ( ! $this->link ) {
70 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
74 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
75 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
82 function setDefaultPermissions($perm) {
83 $this->permission = $perm;
86 function get_contents($file, $type = '', $resumepos = 0 ){
88 $extension = substr(strrchr($file, "."), 1);
89 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
94 if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
96 fseek($temp, 0); //Skip back to the start of the file being written to
98 while (!feof($temp)) {
99 $contents .= fread($temp, 8192);
104 function get_contents_array($file) {
105 return explode("\n", $this->get_contents($file));
107 function put_contents($file, $contents, $type = '' ) {
109 $extension = substr(strrchr($file, "."), 1);
110 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
115 fwrite($temp, $contents);
116 fseek($temp, 0); //Skip back to the start of the file being written to
117 $ret = @ftp_fput($this->link, $file, $temp, $type);
122 $cwd = ftp_pwd($this->link);
124 $cwd = trailingslashit($cwd);
127 function chdir($dir) {
128 return @ftp_chdir($dir);
130 function chgrp($file, $group, $recursive = false ) {
133 function chmod($file, $mode = false, $recursive = false) {
135 $mode = $this->permission;
138 if ( ! $this->exists($file) )
140 if ( ! $recursive || ! $this->is_dir($file) ) {
141 if ( ! function_exists('ftp_chmod') )
142 return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
143 return @ftp_chmod($this->link, $mode, $file);
145 //Is a directory, and we want recursive
146 $filelist = $this->dirlist($file);
147 foreach($filelist as $filename){
148 $this->chmod($file . '/' . $filename, $mode, $recursive);
152 function chown($file, $owner, $recursive = false ) {
155 function owner($file) {
156 $dir = $this->dirlist($file);
157 return $dir[$file]['owner'];
159 function getchmod($file) {
160 $dir = $this->dirlist($file);
161 return $dir[$file]['permsn'];
163 function group($file) {
164 $dir = $this->dirlist($file);
165 return $dir[$file]['group'];
167 function copy($source, $destination, $overwrite = false ) {
168 if( ! $overwrite && $this->exists($destination) )
170 $content = $this->get_contents($source);
171 if( false === $content)
173 return $this->put_contents($destination, $content);
175 function move($source, $destination, $overwrite = false) {
176 return ftp_rename($this->link, $source, $destination);
179 function delete($file,$recursive=false) {
180 if ( $this->is_file($file) )
181 return @ftp_delete($this->link, $file);
183 return @ftp_rmdir($this->link, $file);
184 $filelist = $this->dirlist($file);
185 foreach ((array) $filelist as $filename => $fileinfo) {
186 $this->delete($file . '/' . $filename, $recursive);
188 return @ftp_rmdir($this->link, $file);
191 function exists($file) {
192 $list = ftp_rawlist($this->link, $file, false);
195 return count($list) == 1 ? true : false;
197 function is_file($file) {
198 return $this->is_dir($file) ? false : true;
200 function is_dir($path) {
202 $result = @ftp_chdir($this->link, $path);
203 if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
204 @ftp_chdir($this->link, $cwd);
209 function is_readable($file) {
210 //Get dir list, Check if the file is writable by the current user??
213 function is_writable($file) {
214 //Get dir list, Check if the file is writable by the current user??
217 function atime($file) {
220 function mtime($file) {
221 return ftp_mdtm($this->link, $file);
223 function size($file) {
224 return ftp_size($this->link, $file);
226 function touch($file, $time = 0, $atime = 0) {
229 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
230 if( !@ftp_mkdir($this->link, $path) )
233 $this->chmod($path, $chmod);
235 $this->chown($path, $chown);
237 $this->chgrp($path, $chgrp);
240 function rmdir($path, $recursive = false) {
242 return @ftp_rmdir($this->link, $path);
244 //TODO: Recursive Directory delete, Have to delete files from the folder first.
245 //$dir = $this->dirlist($path);
246 //foreach($dir as $file)
250 function parselisting($line) {
251 $is_windows = ($this->OS_remote == FTP_OS_Windows);
252 if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
254 if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
255 $b['isdir'] = ($lucifer[7]=="<DIR>");
260 $b['size'] = $lucifer[7];
261 $b['month'] = $lucifer[1];
262 $b['day'] = $lucifer[2];
263 $b['year'] = $lucifer[3];
264 $b['hour'] = $lucifer[4];
265 $b['minute'] = $lucifer[5];
266 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
267 $b['am/pm'] = $lucifer[6];
268 $b['name'] = $lucifer[8];
269 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
271 $lcount=count($lucifer);
272 if ($lcount<8) return '';
274 $b['isdir'] = $lucifer[0]{0} === "d";
275 $b['islink'] = $lucifer[0]{0} === "l";
278 elseif ( $b['islink'] )
282 $b['perms'] = $lucifer[0];
283 $b['number'] = $lucifer[1];
284 $b['owner'] = $lucifer[2];
285 $b['group'] = $lucifer[3];
286 $b['size'] = $lucifer[4];
288 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
289 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
290 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
291 $b['name'] = $lucifer[7];
293 $b['month'] = $lucifer[5];
294 $b['day'] = $lucifer[6];
295 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
296 $b['year'] = date("Y");
298 $b['minute'] = $l2[2];
300 $b['year'] = $lucifer[7];
304 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
305 $b['name'] = $lucifer[8];
312 function dirlist($path = '.', $incdot = false, $recursive = false) {
313 if( $this->is_file($path) ) {
314 $limitFile = basename($path);
315 $path = dirname($path) . '/';
320 $list = @ftp_rawlist($this->link, '-a ' . $path, false);
322 if ( $list === false )
326 foreach ( $list as $k => $v ) {
327 $entry = $this->parselisting($v);
331 if ( '.' == $entry["name"] || '..' == $entry["name"] )
334 $dirlist[ $entry['name'] ] = $entry;
339 if ( empty($dirlist) )
343 foreach ( $dirlist as $struc ) {
345 if ( 'd' == $struc['type'] ) {
346 $struc['files'] = array();
349 //We're including the doted starts
350 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
352 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
356 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
360 $ret[$struc['name']] = $struc;
365 function __destruct(){
367 ftp_close($this->link);