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