mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress FTP Filesystem.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Filesystem
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * WordPress Filesystem Class for implementing FTP.
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since 2.5
mp-wp_genesis 13 * @package WordPress
mp-wp_genesis 14 * @subpackage Filesystem
mp-wp_genesis 15 * @uses WP_Filesystem_Base Extends class
mp-wp_genesis 16 */
mp-wp_genesis 17 class WP_Filesystem_FTPext extends WP_Filesystem_Base {
mp-wp_genesis 18 var $link;
mp-wp_genesis 19 var $timeout = 5;
mp-wp_genesis 20 var $errors = array();
mp-wp_genesis 21 var $options = array();
mp-wp_genesis 22
mp-wp_genesis 23 var $permission = null;
mp-wp_genesis 24
mp-wp_genesis 25 function WP_Filesystem_FTPext($opt='') {
mp-wp_genesis 26 $this->method = 'ftpext';
mp-wp_genesis 27 $this->errors = new WP_Error();
mp-wp_genesis 28
mp-wp_genesis 29 //Check if possible to use ftp functions.
mp-wp_genesis 30 if ( ! extension_loaded('ftp') ) {
mp-wp_genesis 31 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
mp-wp_genesis 32 return false;
mp-wp_genesis 33 }
mp-wp_genesis 34
mp-wp_genesis 35 // Set defaults:
mp-wp_genesis 36 if ( empty($opt['port']) )
mp-wp_genesis 37 $this->options['port'] = 21;
mp-wp_genesis 38 else
mp-wp_genesis 39 $this->options['port'] = $opt['port'];
mp-wp_genesis 40
mp-wp_genesis 41 if ( empty($opt['hostname']) )
mp-wp_genesis 42 $this->errors->add('empty_hostname', __('FTP hostname is required'));
mp-wp_genesis 43 else
mp-wp_genesis 44 $this->options['hostname'] = $opt['hostname'];
mp-wp_genesis 45
mp-wp_genesis 46 if ( isset($opt['base']) && ! empty($opt['base']) )
mp-wp_genesis 47 $this->wp_base = $opt['base'];
mp-wp_genesis 48
mp-wp_genesis 49 // Check if the options provided are OK.
mp-wp_genesis 50 if ( empty ($opt['username']) )
mp-wp_genesis 51 $this->errors->add('empty_username', __('FTP username is required'));
mp-wp_genesis 52 else
mp-wp_genesis 53 $this->options['username'] = $opt['username'];
mp-wp_genesis 54
mp-wp_genesis 55 if ( empty ($opt['password']) )
mp-wp_genesis 56 $this->errors->add('empty_password', __('FTP password is required'));
mp-wp_genesis 57 else
mp-wp_genesis 58 $this->options['password'] = $opt['password'];
mp-wp_genesis 59
mp-wp_genesis 60 $this->options['ssl'] = false;
mp-wp_genesis 61 if ( isset($opt['ssl']) )
mp-wp_genesis 62 $this->options['ssl'] = ( !empty($opt['ssl']) );
mp-wp_genesis 63 elseif ( isset( $opt['connection_type']) )
mp-wp_genesis 64 $this->options['ssl'] = ( 'ftps' == $opt['connection_type'] );
mp-wp_genesis 65 }
mp-wp_genesis 66
mp-wp_genesis 67 function connect() {
mp-wp_genesis 68 if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
mp-wp_genesis 69 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
mp-wp_genesis 70 else
mp-wp_genesis 71 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
mp-wp_genesis 72
mp-wp_genesis 73 if ( ! $this->link ) {
mp-wp_genesis 74 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
mp-wp_genesis 75 return false;
mp-wp_genesis 76 }
mp-wp_genesis 77
mp-wp_genesis 78 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
mp-wp_genesis 79 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
mp-wp_genesis 80 return false;
mp-wp_genesis 81 }
mp-wp_genesis 82
mp-wp_genesis 83 //Set the Connection to use Passive FTP
mp-wp_genesis 84 @ftp_pasv( $this->link, true );
mp-wp_genesis 85
mp-wp_genesis 86 return true;
mp-wp_genesis 87 }
mp-wp_genesis 88
mp-wp_genesis 89 function setDefaultPermissions($perm) {
mp-wp_genesis 90 $this->permission = $perm;
mp-wp_genesis 91 }
mp-wp_genesis 92
mp-wp_genesis 93 function get_contents($file, $type = '', $resumepos = 0 ){
mp-wp_genesis 94 if( empty($type) )
mp-wp_genesis 95 $type = FTP_BINARY;
mp-wp_genesis 96
mp-wp_genesis 97 $temp = tmpfile();
mp-wp_genesis 98 if ( ! $temp )
mp-wp_genesis 99 return false;
mp-wp_genesis 100
mp-wp_genesis 101 if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
mp-wp_genesis 102 return false;
mp-wp_genesis 103
mp-wp_genesis 104 fseek($temp, 0); //Skip back to the start of the file being written to
mp-wp_genesis 105 $contents = '';
mp-wp_genesis 106
mp-wp_genesis 107 while ( ! feof($temp) )
mp-wp_genesis 108 $contents .= fread($temp, 8192);
mp-wp_genesis 109
mp-wp_genesis 110 fclose($temp);
mp-wp_genesis 111 return $contents;
mp-wp_genesis 112 }
mp-wp_genesis 113 function get_contents_array($file) {
mp-wp_genesis 114 return explode("\n", $this->get_contents($file));
mp-wp_genesis 115 }
mp-wp_genesis 116 function put_contents($file, $contents, $type = '' ) {
mp-wp_genesis 117 if( empty($type) )
mp-wp_genesis 118 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
mp-wp_genesis 119
mp-wp_genesis 120 $temp = tmpfile();
mp-wp_genesis 121 if ( ! $temp )
mp-wp_genesis 122 return false;
mp-wp_genesis 123
mp-wp_genesis 124 fwrite($temp, $contents);
mp-wp_genesis 125 fseek($temp, 0); //Skip back to the start of the file being written to
mp-wp_genesis 126
mp-wp_genesis 127 $ret = @ftp_fput($this->link, $file, $temp, $type);
mp-wp_genesis 128
mp-wp_genesis 129 fclose($temp);
mp-wp_genesis 130 return $ret;
mp-wp_genesis 131 }
mp-wp_genesis 132 function cwd() {
mp-wp_genesis 133 $cwd = @ftp_pwd($this->link);
mp-wp_genesis 134 if( $cwd )
mp-wp_genesis 135 $cwd = trailingslashit($cwd);
mp-wp_genesis 136 return $cwd;
mp-wp_genesis 137 }
mp-wp_genesis 138 function chdir($dir) {
mp-wp_genesis 139 return @ftp_chdir($dir);
mp-wp_genesis 140 }
mp-wp_genesis 141 function chgrp($file, $group, $recursive = false ) {
mp-wp_genesis 142 return false;
mp-wp_genesis 143 }
mp-wp_genesis 144 function chmod($file, $mode = false, $recursive = false) {
mp-wp_genesis 145 if( ! $mode )
mp-wp_genesis 146 $mode = $this->permission;
mp-wp_genesis 147 if( ! $mode )
mp-wp_genesis 148 return false;
mp-wp_genesis 149 if ( ! $this->exists($file) )
mp-wp_genesis 150 return false;
mp-wp_genesis 151 if ( ! $recursive || ! $this->is_dir($file) ) {
mp-wp_genesis 152 if ( ! function_exists('ftp_chmod') )
mp-wp_genesis 153 return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
mp-wp_genesis 154 return @ftp_chmod($this->link, $mode, $file);
mp-wp_genesis 155 }
mp-wp_genesis 156 //Is a directory, and we want recursive
mp-wp_genesis 157 $filelist = $this->dirlist($file);
mp-wp_genesis 158 foreach($filelist as $filename){
mp-wp_genesis 159 $this->chmod($file . '/' . $filename, $mode, $recursive);
mp-wp_genesis 160 }
mp-wp_genesis 161 return true;
mp-wp_genesis 162 }
mp-wp_genesis 163 function chown($file, $owner, $recursive = false ) {
mp-wp_genesis 164 return false;
mp-wp_genesis 165 }
mp-wp_genesis 166 function owner($file) {
mp-wp_genesis 167 $dir = $this->dirlist($file);
mp-wp_genesis 168 return $dir[$file]['owner'];
mp-wp_genesis 169 }
mp-wp_genesis 170 function getchmod($file) {
mp-wp_genesis 171 $dir = $this->dirlist($file);
mp-wp_genesis 172 return $dir[$file]['permsn'];
mp-wp_genesis 173 }
mp-wp_genesis 174 function group($file) {
mp-wp_genesis 175 $dir = $this->dirlist($file);
mp-wp_genesis 176 return $dir[$file]['group'];
mp-wp_genesis 177 }
mp-wp_genesis 178 function copy($source, $destination, $overwrite = false ) {
mp-wp_genesis 179 if( ! $overwrite && $this->exists($destination) )
mp-wp_genesis 180 return false;
mp-wp_genesis 181 $content = $this->get_contents($source);
mp-wp_genesis 182 if( false === $content)
mp-wp_genesis 183 return false;
mp-wp_genesis 184 return $this->put_contents($destination, $content);
mp-wp_genesis 185 }
mp-wp_genesis 186 function move($source, $destination, $overwrite = false) {
mp-wp_genesis 187 return ftp_rename($this->link, $source, $destination);
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 function delete($file,$recursive=false) {
mp-wp_genesis 191 if ( $this->is_file($file) )
mp-wp_genesis 192 return @ftp_delete($this->link, $file);
mp-wp_genesis 193 if ( !$recursive )
mp-wp_genesis 194 return @ftp_rmdir($this->link, $file);
mp-wp_genesis 195 $filelist = $this->dirlist($file);
mp-wp_genesis 196 foreach ((array) $filelist as $filename => $fileinfo) {
mp-wp_genesis 197 $this->delete($file . '/' . $filename, $recursive);
mp-wp_genesis 198 }
mp-wp_genesis 199 return @ftp_rmdir($this->link, $file);
mp-wp_genesis 200 }
mp-wp_genesis 201
mp-wp_genesis 202 function exists($file) {
mp-wp_genesis 203 $list = ftp_rawlist($this->link, $file, false);
mp-wp_genesis 204 if( ! $list )
mp-wp_genesis 205 return false;
mp-wp_genesis 206 return count($list) == 1 ? true : false;
mp-wp_genesis 207 }
mp-wp_genesis 208 function is_file($file) {
mp-wp_genesis 209 return $this->is_dir($file) ? false : true;
mp-wp_genesis 210 }
mp-wp_genesis 211 function is_dir($path) {
mp-wp_genesis 212 $cwd = $this->cwd();
mp-wp_genesis 213 $result = @ftp_chdir($this->link, $path);
mp-wp_genesis 214 if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
mp-wp_genesis 215 @ftp_chdir($this->link, $cwd);
mp-wp_genesis 216 return true;
mp-wp_genesis 217 }
mp-wp_genesis 218 return false;
mp-wp_genesis 219 }
mp-wp_genesis 220 function is_readable($file) {
mp-wp_genesis 221 //Get dir list, Check if the file is writable by the current user??
mp-wp_genesis 222 return true;
mp-wp_genesis 223 }
mp-wp_genesis 224 function is_writable($file) {
mp-wp_genesis 225 //Get dir list, Check if the file is writable by the current user??
mp-wp_genesis 226 return true;
mp-wp_genesis 227 }
mp-wp_genesis 228 function atime($file) {
mp-wp_genesis 229 return false;
mp-wp_genesis 230 }
mp-wp_genesis 231 function mtime($file) {
mp-wp_genesis 232 return ftp_mdtm($this->link, $file);
mp-wp_genesis 233 }
mp-wp_genesis 234 function size($file) {
mp-wp_genesis 235 return ftp_size($this->link, $file);
mp-wp_genesis 236 }
mp-wp_genesis 237 function touch($file, $time = 0, $atime = 0) {
mp-wp_genesis 238 return false;
mp-wp_genesis 239 }
mp-wp_genesis 240 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
mp-wp_genesis 241 if( !@ftp_mkdir($this->link, $path) )
mp-wp_genesis 242 return false;
mp-wp_genesis 243 if( $chmod )
mp-wp_genesis 244 $this->chmod($path, $chmod);
mp-wp_genesis 245 if( $chown )
mp-wp_genesis 246 $this->chown($path, $chown);
mp-wp_genesis 247 if( $chgrp )
mp-wp_genesis 248 $this->chgrp($path, $chgrp);
mp-wp_genesis 249 return true;
mp-wp_genesis 250 }
mp-wp_genesis 251 function rmdir($path, $recursive = false) {
mp-wp_genesis 252 if( ! $recursive )
mp-wp_genesis 253 return @ftp_rmdir($this->link, $path);
mp-wp_genesis 254
mp-wp_genesis 255 //TODO: Recursive Directory delete, Have to delete files from the folder first.
mp-wp_genesis 256 //$dir = $this->dirlist($path);
mp-wp_genesis 257 //foreach($dir as $file)
mp-wp_genesis 258
mp-wp_genesis 259 }
mp-wp_genesis 260
mp-wp_genesis 261 function parselisting($line) {
mp-wp_genesis 262 $is_windows = ($this->OS_remote == FTP_OS_Windows);
mp-wp_genesis 263 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)) {
mp-wp_genesis 264 $b = array();
mp-wp_genesis 265 if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
mp-wp_genesis 266 $b['isdir'] = ($lucifer[7]=="<DIR>");
mp-wp_genesis 267 if ( $b['isdir'] )
mp-wp_genesis 268 $b['type'] = 'd';
mp-wp_genesis 269 else
mp-wp_genesis 270 $b['type'] = 'f';
mp-wp_genesis 271 $b['size'] = $lucifer[7];
mp-wp_genesis 272 $b['month'] = $lucifer[1];
mp-wp_genesis 273 $b['day'] = $lucifer[2];
mp-wp_genesis 274 $b['year'] = $lucifer[3];
mp-wp_genesis 275 $b['hour'] = $lucifer[4];
mp-wp_genesis 276 $b['minute'] = $lucifer[5];
mp-wp_genesis 277 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
mp-wp_genesis 278 $b['am/pm'] = $lucifer[6];
mp-wp_genesis 279 $b['name'] = $lucifer[8];
mp-wp_genesis 280 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
mp-wp_genesis 281 //echo $line."\n";
mp-wp_genesis 282 $lcount=count($lucifer);
mp-wp_genesis 283 if ($lcount<8) return '';
mp-wp_genesis 284 $b = array();
mp-wp_genesis 285 $b['isdir'] = $lucifer[0]{0} === "d";
mp-wp_genesis 286 $b['islink'] = $lucifer[0]{0} === "l";
mp-wp_genesis 287 if ( $b['isdir'] )
mp-wp_genesis 288 $b['type'] = 'd';
mp-wp_genesis 289 elseif ( $b['islink'] )
mp-wp_genesis 290 $b['type'] = 'l';
mp-wp_genesis 291 else
mp-wp_genesis 292 $b['type'] = 'f';
mp-wp_genesis 293 $b['perms'] = $lucifer[0];
mp-wp_genesis 294 $b['number'] = $lucifer[1];
mp-wp_genesis 295 $b['owner'] = $lucifer[2];
mp-wp_genesis 296 $b['group'] = $lucifer[3];
mp-wp_genesis 297 $b['size'] = $lucifer[4];
mp-wp_genesis 298 if ($lcount==8) {
mp-wp_genesis 299 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
mp-wp_genesis 300 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
mp-wp_genesis 301 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
mp-wp_genesis 302 $b['name'] = $lucifer[7];
mp-wp_genesis 303 } else {
mp-wp_genesis 304 $b['month'] = $lucifer[5];
mp-wp_genesis 305 $b['day'] = $lucifer[6];
mp-wp_genesis 306 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
mp-wp_genesis 307 $b['year'] = date("Y");
mp-wp_genesis 308 $b['hour'] = $l2[1];
mp-wp_genesis 309 $b['minute'] = $l2[2];
mp-wp_genesis 310 } else {
mp-wp_genesis 311 $b['year'] = $lucifer[7];
mp-wp_genesis 312 $b['hour'] = 0;
mp-wp_genesis 313 $b['minute'] = 0;
mp-wp_genesis 314 }
mp-wp_genesis 315 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
mp-wp_genesis 316 $b['name'] = $lucifer[8];
mp-wp_genesis 317 }
mp-wp_genesis 318 }
mp-wp_genesis 319
mp-wp_genesis 320 return $b;
mp-wp_genesis 321 }
mp-wp_genesis 322
mp-wp_genesis 323 function dirlist($path = '.', $incdot = false, $recursive = false) {
mp-wp_genesis 324 if( $this->is_file($path) ) {
mp-wp_genesis 325 $limitFile = basename($path);
mp-wp_genesis 326 $path = dirname($path) . '/';
mp-wp_genesis 327 } else {
mp-wp_genesis 328 $limitFile = false;
mp-wp_genesis 329 }
mp-wp_genesis 330
mp-wp_genesis 331 $list = @ftp_rawlist($this->link, '-a ' . $path, false);
mp-wp_genesis 332
mp-wp_genesis 333 if ( $list === false )
mp-wp_genesis 334 return false;
mp-wp_genesis 335
mp-wp_genesis 336 $dirlist = array();
mp-wp_genesis 337 foreach ( $list as $k => $v ) {
mp-wp_genesis 338 $entry = $this->parselisting($v);
mp-wp_genesis 339 if ( empty($entry) )
mp-wp_genesis 340 continue;
mp-wp_genesis 341
mp-wp_genesis 342 if ( '.' == $entry["name"] || '..' == $entry["name"] )
mp-wp_genesis 343 continue;
mp-wp_genesis 344
mp-wp_genesis 345 $dirlist[ $entry['name'] ] = $entry;
mp-wp_genesis 346 }
mp-wp_genesis 347
mp-wp_genesis 348 if ( ! $dirlist )
mp-wp_genesis 349 return false;
mp-wp_genesis 350 if ( empty($dirlist) )
mp-wp_genesis 351 return array();
mp-wp_genesis 352
mp-wp_genesis 353 $ret = array();
mp-wp_genesis 354 foreach ( $dirlist as $struc ) {
mp-wp_genesis 355
mp-wp_genesis 356 if ( 'd' == $struc['type'] ) {
mp-wp_genesis 357 $struc['files'] = array();
mp-wp_genesis 358
mp-wp_genesis 359 if ( $incdot ){
mp-wp_genesis 360 //We're including the doted starts
mp-wp_genesis 361 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
mp-wp_genesis 362 if ($recursive)
mp-wp_genesis 363 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
mp-wp_genesis 364 }
mp-wp_genesis 365 } else { //No dots
mp-wp_genesis 366 if ($recursive)
mp-wp_genesis 367 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
mp-wp_genesis 368 }
mp-wp_genesis 369 }
mp-wp_genesis 370 //File
mp-wp_genesis 371 $ret[$struc['name']] = $struc;
mp-wp_genesis 372 }
mp-wp_genesis 373 return $ret;
mp-wp_genesis 374 }
mp-wp_genesis 375
mp-wp_genesis 376 function __destruct(){
mp-wp_genesis 377 if( $this->link )
mp-wp_genesis 378 ftp_close($this->link);
mp-wp_genesis 379 }
mp-wp_genesis 380 }
mp-wp_genesis 381
mp-wp_genesis 382 ?>