web/wp-admin/includes/class-wp-filesystem-ftpsockets.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * WordPress FTP Sockets Filesystem.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Filesystem
       
     7  */
       
     8 
       
     9 /**
       
    10  * WordPress Filesystem Class for implementing FTP Sockets.
       
    11  *
       
    12  * @since 2.5
       
    13  * @package WordPress
       
    14  * @subpackage Filesystem
       
    15  * @uses WP_Filesystem_Base Extends class
       
    16  */
       
    17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
       
    18 	var $ftp = false;
       
    19 	var $timeout = 5;
       
    20 	var $errors = null;
       
    21 	var $options = array();
       
    22 
       
    23 	var $permission = null;
       
    24 
       
    25 	function WP_Filesystem_ftpsockets($opt = '') {
       
    26 		$this->method = 'ftpsockets';
       
    27 		$this->errors = new WP_Error();
       
    28 
       
    29 		//Check if possible to use ftp functions.
       
    30 		if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
       
    31 				return false;
       
    32 		$this->ftp = new ftp();
       
    33 
       
    34 		//Set defaults:
       
    35 		if ( empty($opt['port']) )
       
    36 			$this->options['port'] = 21;
       
    37 		else
       
    38 			$this->options['port'] = $opt['port'];
       
    39 
       
    40 		if ( empty($opt['hostname']) )
       
    41 			$this->errors->add('empty_hostname', __('FTP hostname is required'));
       
    42 		else
       
    43 			$this->options['hostname'] = $opt['hostname'];
       
    44 
       
    45 		if ( isset($opt['base']) && ! empty($opt['base']) )
       
    46 			$this->wp_base = $opt['base'];
       
    47 
       
    48 		// Check if the options provided are OK.
       
    49 		if ( empty ($opt['username']) )
       
    50 			$this->errors->add('empty_username', __('FTP username is required'));
       
    51 		else
       
    52 			$this->options['username'] = $opt['username'];
       
    53 
       
    54 		if ( empty ($opt['password']) )
       
    55 			$this->errors->add('empty_password', __('FTP password is required'));
       
    56 		else
       
    57 			$this->options['password'] = $opt['password'];
       
    58 	}
       
    59 
       
    60 	function connect() {
       
    61 		if ( ! $this->ftp )
       
    62 			return false;
       
    63 
       
    64 		//$this->ftp->Verbose = true;
       
    65 
       
    66 		if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
       
    67 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
       
    68 			return false;
       
    69 		}
       
    70 		if ( ! $this->ftp->connect() ) {
       
    71 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
       
    72 			return false;
       
    73 		}
       
    74 
       
    75 		if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
       
    76 			$this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
       
    77 			return false;
       
    78 		}
       
    79 
       
    80 		$this->ftp->SetType(FTP_AUTOASCII);
       
    81 		$this->ftp->Passive(true);
       
    82 		return true;
       
    83 	}
       
    84 
       
    85 	function setDefaultPermissions($perm) {
       
    86 		$this->permission = $perm;
       
    87 	}
       
    88 
       
    89 	function get_contents($file, $type = '', $resumepos = 0) {
       
    90 		if( ! $this->exists($file) )
       
    91 			return false;
       
    92 
       
    93 		if( empty($type) )
       
    94 			$type = FTP_AUTOASCII;
       
    95 		$this->ftp->SetType($type);
       
    96 
       
    97 		$temp = wp_tempnam( $file );
       
    98 
       
    99 		if ( ! $temphandle = fopen($temp, 'w+') )
       
   100 			return false;
       
   101 
       
   102 		if ( ! $this->ftp->fget($temphandle, $file) ) {
       
   103 			fclose($temphandle);
       
   104 			unlink($temp);
       
   105 			return ''; //Blank document, File does exist, Its just blank.
       
   106 		}
       
   107 
       
   108 		fseek($temphandle, 0); //Skip back to the start of the file being written to
       
   109 		$contents = '';
       
   110 
       
   111 		while ( ! feof($temphandle) )
       
   112 			$contents .= fread($temphandle, 8192);
       
   113 
       
   114 		fclose($temphandle);
       
   115 		unlink($temp);
       
   116 		return $contents;
       
   117 	}
       
   118 
       
   119 	function get_contents_array($file) {
       
   120 		return explode("\n", $this->get_contents($file) );
       
   121 	}
       
   122 
       
   123 	function put_contents($file, $contents, $type = '' ) {
       
   124 		if( empty($type) )
       
   125 			$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
       
   126 
       
   127 		$this->ftp->SetType($type);
       
   128 
       
   129 		$temp = wp_tempnam( $file );
       
   130 		if ( ! $temphandle = fopen($temp, 'w+') ){
       
   131 			unlink($temp);
       
   132 			return false;
       
   133 		}
       
   134 
       
   135 		fwrite($temphandle, $contents);
       
   136 		fseek($temphandle, 0); //Skip back to the start of the file being written to
       
   137 
       
   138 		$ret = $this->ftp->fput($file, $temphandle);
       
   139 
       
   140 		fclose($temphandle);
       
   141 		unlink($temp);
       
   142 		return $ret;
       
   143 	}
       
   144 
       
   145 	function cwd() {
       
   146 		$cwd = $this->ftp->pwd();
       
   147 		if( $cwd )
       
   148 			$cwd = trailingslashit($cwd);
       
   149 		return $cwd;
       
   150 	}
       
   151 
       
   152 	function chdir($file) {
       
   153 		return $this->ftp->chdir($file);
       
   154 	}
       
   155 
       
   156 	function chgrp($file, $group, $recursive = false ) {
       
   157 		return false;
       
   158 	}
       
   159 
       
   160 	function chmod($file, $mode = false, $recursive = false ) {
       
   161 		if( ! $mode )
       
   162 			$mode = $this->permission;
       
   163 		if( ! $mode )
       
   164 			return false;
       
   165 		//if( ! $this->exists($file) )
       
   166 		//	return false;
       
   167 		if( ! $recursive || ! $this->is_dir($file) ) {
       
   168 			return $this->ftp->chmod($file,$mode);
       
   169 		}
       
   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);
       
   174 		}
       
   175 		return true;
       
   176 	}
       
   177 
       
   178 	function chown($file, $owner, $recursive = false ) {
       
   179 		return false;
       
   180 	}
       
   181 
       
   182 	function owner($file) {
       
   183 		$dir = $this->dirlist($file);
       
   184 		return $dir[$file]['owner'];
       
   185 	}
       
   186 
       
   187 	function getchmod($file) {
       
   188 		$dir = $this->dirlist($file);
       
   189 		return $dir[$file]['permsn'];
       
   190 	}
       
   191 
       
   192 	function group($file) {
       
   193 		$dir = $this->dirlist($file);
       
   194 		return $dir[$file]['group'];
       
   195 	}
       
   196 
       
   197 	function copy($source, $destination, $overwrite = false ) {
       
   198 		if( ! $overwrite && $this->exists($destination) )
       
   199 			return false;
       
   200 
       
   201 		$content = $this->get_contents($source);
       
   202 		if ( false === $content )
       
   203 			return false;
       
   204 
       
   205 		return $this->put_contents($destination, $content);
       
   206 	}
       
   207 
       
   208 	function move($source, $destination, $overwrite = false ) {
       
   209 		return $this->ftp->rename($source, $destination);
       
   210 	}
       
   211 
       
   212 	function delete($file, $recursive = false ) {
       
   213 		if ( empty($file) )
       
   214 			return false;
       
   215 		if ( $this->is_file($file) )
       
   216 			return $this->ftp->delete($file);
       
   217 		if ( !$recursive )
       
   218 			return $this->ftp->rmdir($file);
       
   219 
       
   220 		return $this->ftp->mdel($file);
       
   221 	}
       
   222 
       
   223 	function exists($file) {
       
   224 		return $this->ftp->is_exists($file);
       
   225 	}
       
   226 
       
   227 	function is_file($file) {
       
   228 		return $this->is_dir($file) ? false : true;
       
   229 	}
       
   230 
       
   231 	function is_dir($path) {
       
   232 		$cwd = $this->cwd();
       
   233 		if ( $this->chdir($path) ) {
       
   234 			$this->chdir($cwd);
       
   235 			return true;
       
   236 		}
       
   237 		return false;
       
   238 	}
       
   239 
       
   240 	function is_readable($file) {
       
   241 		//Get dir list, Check if the file is writable by the current user??
       
   242 		return true;
       
   243 	}
       
   244 
       
   245 	function is_writable($file) {
       
   246 		//Get dir list, Check if the file is writable by the current user??
       
   247 		return true;
       
   248 	}
       
   249 
       
   250 	function atime($file) {
       
   251 		return false;
       
   252 	}
       
   253 
       
   254 	function mtime($file) {
       
   255 		return $this->ftp->mdtm($file);
       
   256 	}
       
   257 
       
   258 	function size($file) {
       
   259 		return $this->ftp->filesize($file);
       
   260 	}
       
   261 
       
   262 	function touch($file, $time = 0, $atime = 0 ) {
       
   263 		return false;
       
   264 	}
       
   265 
       
   266 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
       
   267 		if( ! $this->ftp->mkdir($path) )
       
   268 			return false;
       
   269 		if( $chmod )
       
   270 			$this->chmod($path, $chmod);
       
   271 		if( $chown )
       
   272 			$this->chown($path, $chown);
       
   273 		if( $chgrp )
       
   274 			$this->chgrp($path, $chgrp);
       
   275 		return true;
       
   276 	}
       
   277 
       
   278 	function rmdir($path, $recursive = false ) {
       
   279 		if( ! $recursive )
       
   280 			return $this->ftp->rmdir($path);
       
   281 
       
   282 		return $this->ftp->mdel($path);
       
   283 	}
       
   284 
       
   285 	function dirlist($path = '.', $incdot = false, $recursive = false ) {
       
   286 		if( $this->is_file($path) ) {
       
   287 			$limitFile = basename($path);
       
   288 			$path = dirname($path) . '/';
       
   289 		} else {
       
   290 			$limitFile = false;
       
   291 		}
       
   292 
       
   293 		$list = $this->ftp->dirlist($path);
       
   294 		if( ! $list )
       
   295 			return false;
       
   296 		if( empty($list) )
       
   297 			return array();
       
   298 
       
   299 		$ret = array();
       
   300 		foreach ( $list as $struc ) {
       
   301 
       
   302 			if ( 'd' == $struc['type'] ) {
       
   303 				$struc['files'] = array();
       
   304 
       
   305 				if ( $incdot ){
       
   306 					//We're including the doted starts
       
   307 					if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
       
   308 						if ($recursive)
       
   309 							$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
       
   310 					}
       
   311 				} else { //No dots
       
   312 					if ($recursive)
       
   313 						$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
       
   314 				}
       
   315 			}
       
   316 			//File
       
   317 			$ret[$struc['name']] = $struc;
       
   318 		}
       
   319 		return $ret;
       
   320 	}
       
   321 
       
   322 	function __destruct() {
       
   323 		$this->ftp->quit();
       
   324 	}
       
   325 }
       
   326 
       
   327 ?>