web/wp-admin/includes/class-wp-filesystem-ssh2.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    46 	var $sftp_link = false;
    46 	var $sftp_link = false;
    47 	var $keys = false;
    47 	var $keys = false;
    48 	var $errors = array();
    48 	var $errors = array();
    49 	var $options = array();
    49 	var $options = array();
    50 
    50 
    51 	function WP_Filesystem_SSH2($opt='') {
    51 	function __construct($opt='') {
    52 		$this->method = 'ssh2';
    52 		$this->method = 'ssh2';
    53 		$this->errors = new WP_Error();
    53 		$this->errors = new WP_Error();
    54 
    54 
    55 		//Check if possible to use ssh2 functions.
    55 		//Check if possible to use ssh2 functions.
    56 		if ( ! extension_loaded('ssh2') ) {
    56 		if ( ! extension_loaded('ssh2') ) {
    71 		if ( empty($opt['hostname']) )
    71 		if ( empty($opt['hostname']) )
    72 			$this->errors->add('empty_hostname', __('SSH2 hostname is required'));
    72 			$this->errors->add('empty_hostname', __('SSH2 hostname is required'));
    73 		else
    73 		else
    74 			$this->options['hostname'] = $opt['hostname'];
    74 			$this->options['hostname'] = $opt['hostname'];
    75 
    75 
    76 		if ( isset($opt['base']) && ! empty($opt['base']) )
    76 		if ( ! empty($opt['base']) )
    77 			$this->wp_base = $opt['base'];
    77 			$this->wp_base = $opt['base'];
    78 
    78 
    79 		// Check if the options provided are OK.
    79 		// Check if the options provided are OK.
    80 		if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
    80 		if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
    81 			$this->options['public_key'] = $opt['public_key'];
    81 			$this->options['public_key'] = $opt['public_key'];
   158 	function get_contents_array($file) {
   158 	function get_contents_array($file) {
   159 		$file = ltrim($file, '/');
   159 		$file = ltrim($file, '/');
   160 		return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   160 		return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   161 	}
   161 	}
   162 
   162 
   163 	function put_contents($file, $contents, $type = '' ) {
   163 	function put_contents($file, $contents, $mode = false ) {
   164 		$file = ltrim($file, '/');
   164 		$file = ltrim($file, '/');
   165 		return false !== file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
   165 		$ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
       
   166 
       
   167 		$this->chmod($file, $mode);
       
   168 
       
   169 		return false !== $ret;
   166 	}
   170 	}
   167 
   171 
   168 	function cwd() {
   172 	function cwd() {
   169 		$cwd = $this->run_command('pwd');
   173 		$cwd = $this->run_command('pwd');
   170 		if( $cwd )
   174 		if ( $cwd )
   171 			$cwd = trailingslashit($cwd);
   175 			$cwd = trailingslashit($cwd);
   172 		return $cwd;
   176 		return $cwd;
   173 	}
   177 	}
   174 
   178 
   175 	function chdir($dir) {
   179 	function chdir($dir) {
   232 			return $gid;
   236 			return $gid;
   233 		$grouparray = posix_getgrgid($gid);
   237 		$grouparray = posix_getgrgid($gid);
   234 		return $grouparray['name'];
   238 		return $grouparray['name'];
   235 	}
   239 	}
   236 
   240 
   237 	function copy($source, $destination, $overwrite = false ) {
   241 	function copy($source, $destination, $overwrite = false, $mode = false) {
   238 		if( ! $overwrite && $this->exists($destination) )
   242 		if ( ! $overwrite && $this->exists($destination) )
   239 			return false;
   243 			return false;
   240 		$content = $this->get_contents($source);
   244 		$content = $this->get_contents($source);
   241 		if( false === $content)
   245 		if ( false === $content)
   242 			return false;
   246 			return false;
   243 		return $this->put_contents($destination, $content);
   247 		return $this->put_contents($destination, $content, $mode);
   244 	}
   248 	}
   245 
   249 
   246 	function move($source, $destination, $overwrite = false) {
   250 	function move($source, $destination, $overwrite = false) {
   247 		return @ssh2_sftp_rename($this->link, $source, $destination);
   251 		return @ssh2_sftp_rename($this->link, $source, $destination);
   248 	}
   252 	}
   249 
   253 
   250 	function delete($file, $recursive = false) {
   254 	function delete($file, $recursive = false, $type = false) {
   251 		if ( $this->is_file($file) )
   255 		if ( 'f' == $type || $this->is_file($file) )
   252 			return ssh2_sftp_unlink($this->sftp_link, $file);
   256 			return ssh2_sftp_unlink($this->sftp_link, $file);
   253 		if ( ! $recursive )
   257 		if ( ! $recursive )
   254 			 return ssh2_sftp_rmdir($this->sftp_link, $file);
   258 			 return ssh2_sftp_rmdir($this->sftp_link, $file);
   255 		$filelist = $this->dirlist($file);
   259 		$filelist = $this->dirlist($file);
   256 		if ( is_array($filelist) ) {
   260 		if ( is_array($filelist) ) {
   257 			foreach ( $filelist as $filename => $fileinfo) {
   261 			foreach ( $filelist as $filename => $fileinfo) {
   258 				$this->delete($file . '/' . $filename, $recursive);
   262 				$this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
   259 			}
   263 			}
   260 		}
   264 		}
   261 		return ssh2_sftp_rmdir($this->sftp_link, $file);
   265 		return ssh2_sftp_rmdir($this->sftp_link, $file);
   262 	}
   266 	}
   263 
   267 
   300 		$file = ltrim($file, '/');
   304 		$file = ltrim($file, '/');
   301 		return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   305 		return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   302 	}
   306 	}
   303 
   307 
   304 	function touch($file, $time = 0, $atime = 0) {
   308 	function touch($file, $time = 0, $atime = 0) {
   305 		//Not implmented.
   309 		//Not implemented.
   306 	}
   310 	}
   307 
   311 
   308 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
   312 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
   309 		$path = untrailingslashit($path);
   313 		$path = untrailingslashit($path);
       
   314 		if ( empty($path) )
       
   315 			return false;
       
   316 
   310 		if ( ! $chmod )
   317 		if ( ! $chmod )
   311 			$chmod = FS_CHMOD_DIR;
   318 			$chmod = FS_CHMOD_DIR;
   312 		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
   319 		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
   313 			return false;
   320 			return false;
   314 		if ( $chown )
   321 		if ( $chown )