web/wp-admin/includes/class-wp-filesystem-ssh2.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    43 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
    43 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
    44 
    44 
    45 	var $link = false;
    45 	var $link = false;
    46 	var $sftp_link = false;
    46 	var $sftp_link = false;
    47 	var $keys = false;
    47 	var $keys = false;
    48 	/*
       
    49 	 * This is the timeout value for ssh results.
       
    50 	 * Slower servers might need this incressed, but this number otherwise should not change.
       
    51 	 *
       
    52 	 * @parm $timeout int
       
    53 	 *
       
    54 	 */
       
    55 	var $timeout = 15;
       
    56 	var $errors = array();
    48 	var $errors = array();
    57 	var $options = array();
    49 	var $options = array();
    58 
       
    59 	var $permission = 0644;
       
    60 
    50 
    61 	function WP_Filesystem_SSH2($opt='') {
    51 	function WP_Filesystem_SSH2($opt='') {
    62 		$this->method = 'ssh2';
    52 		$this->method = 'ssh2';
    63 		$this->errors = new WP_Error();
    53 		$this->errors = new WP_Error();
    64 
    54 
   146 
   136 
   147 		if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
   137 		if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
   148 			$this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
   138 			$this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
   149 		} else {
   139 		} else {
   150 			stream_set_blocking( $stream, true );
   140 			stream_set_blocking( $stream, true );
   151 			stream_set_timeout( $stream, $this->timeout );
   141 			stream_set_timeout( $stream, FS_TIMEOUT );
   152 			$data = stream_get_contents( $stream );
   142 			$data = stream_get_contents( $stream );
   153 			fclose( $stream );
   143 			fclose( $stream );
   154 
   144 
   155 			if ( $returnbool )
   145 			if ( $returnbool )
   156 				return ( $data === false ) ? false : '' != trim($data);
   146 				return ( $data === false ) ? false : '' != trim($data);
   158 				return $data;
   148 				return $data;
   159 		}
   149 		}
   160 		return false;
   150 		return false;
   161 	}
   151 	}
   162 
   152 
   163 	function setDefaultPermissions($perm) {
       
   164 		$this->debug("setDefaultPermissions();");
       
   165 		if ( $perm )
       
   166 			$this->permission = $perm;
       
   167 	}
       
   168 
       
   169 	function get_contents($file, $type = '', $resumepos = 0 ) {
   153 	function get_contents($file, $type = '', $resumepos = 0 ) {
   170 		$file = ltrim($file, '/');
   154 		$file = ltrim($file, '/');
   171 		return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   155 		return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   172 	}
   156 	}
   173 
   157 
   176 		return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   160 		return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
   177 	}
   161 	}
   178 
   162 
   179 	function put_contents($file, $contents, $type = '' ) {
   163 	function put_contents($file, $contents, $type = '' ) {
   180 		$file = ltrim($file, '/');
   164 		$file = ltrim($file, '/');
   181 		return file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
   165 		return false !== file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
   182 	}
   166 	}
   183 
   167 
   184 	function cwd() {
   168 	function cwd() {
   185 		$cwd = $this->run_command('pwd');
   169 		$cwd = $this->run_command('pwd');
   186 		if( $cwd )
   170 		if( $cwd )
   199 			return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
   183 			return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
   200 		return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
   184 		return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
   201 	}
   185 	}
   202 
   186 
   203 	function chmod($file, $mode = false, $recursive = false) {
   187 	function chmod($file, $mode = false, $recursive = false) {
   204 		if( ! $mode )
       
   205 			$mode = $this->permission;
       
   206 		if( ! $mode )
       
   207 			return false;
       
   208 		if ( ! $this->exists($file) )
   188 		if ( ! $this->exists($file) )
   209 			return false;
   189 			return false;
       
   190 
       
   191 		if ( ! $mode ) {
       
   192 			if ( $this->is_file($file) )
       
   193 				$mode = FS_CHMOD_FILE;
       
   194 			elseif ( $this->is_dir($file) )
       
   195 				$mode = FS_CHMOD_DIR;
       
   196 			else
       
   197 				return false;
       
   198 		}
       
   199 
   210 		if ( ! $recursive || ! $this->is_dir($file) )
   200 		if ( ! $recursive || ! $this->is_dir($file) )
   211 			return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
   201 			return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
   212 		return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
   202 		return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
   213 	}
   203 	}
   214 
   204 
   313 
   303 
   314 	function touch($file, $time = 0, $atime = 0) {
   304 	function touch($file, $time = 0, $atime = 0) {
   315 		//Not implmented.
   305 		//Not implmented.
   316 	}
   306 	}
   317 
   307 
   318 	function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
   308 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
   319 		$path = untrailingslashit($path);
   309 		$path = untrailingslashit($path);
   320 		$chmod = !empty($chmod) ? $chmod : $this->permission;
   310 		if ( ! $chmod )
       
   311 			$chmod = FS_CHMOD_DIR;
   321 		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
   312 		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
   322 			return false;
   313 			return false;
   323 		if ( $chown )
   314 		if ( $chown )
   324 			$this->chown($path, $chown);
   315 			$this->chown($path, $chown);
   325 		if ( $chgrp )
   316 		if ( $chgrp )
   329 
   320 
   330 	function rmdir($path, $recursive = false) {
   321 	function rmdir($path, $recursive = false) {
   331 		return $this->delete($path, $recursive);
   322 		return $this->delete($path, $recursive);
   332 	}
   323 	}
   333 
   324 
   334 	function dirlist($path, $incdot = false, $recursive = false) {
   325 	function dirlist($path, $include_hidden = true, $recursive = false) {
   335 		if ( $this->is_file($path) ) {
   326 		if ( $this->is_file($path) ) {
   336 			$limitFile = basename($path);
   327 			$limit_file = basename($path);
   337 			$path = dirname($path);
   328 			$path = dirname($path);
   338 		} else {
   329 		} else {
   339 			$limitFile = false;
   330 			$limit_file = false;
   340 		}
   331 		}
       
   332 
   341 		if ( ! $this->is_dir($path) )
   333 		if ( ! $this->is_dir($path) )
   342 			return false;
   334 			return false;
   343 
   335 
   344 		$ret = array();
   336 		$ret = array();
   345 		$dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
   337 		$dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
       
   338 
   346 		if ( ! $dir )
   339 		if ( ! $dir )
   347 			return false;
   340 			return false;
       
   341 
   348 		while (false !== ($entry = $dir->read()) ) {
   342 		while (false !== ($entry = $dir->read()) ) {
   349 			$struc = array();
   343 			$struc = array();
   350 			$struc['name'] = $entry;
   344 			$struc['name'] = $entry;
   351 
   345 
   352 			if ( '.' == $struc['name'] || '..' == $struc['name'] )
   346 			if ( '.' == $struc['name'] || '..' == $struc['name'] )
   353 				continue; //Do not care about these folders.
   347 				continue; //Do not care about these folders.
   354 			if ( '.' == $struc['name'][0] && !$incdot)
   348 
       
   349 			if ( ! $include_hidden && '.' == $struc['name'][0] )
   355 				continue;
   350 				continue;
   356 			if ( $limitFile && $struc['name'] != $limitFile)
   351 
       
   352 			if ( $limit_file && $struc['name'] != $limit_file )
   357 				continue;
   353 				continue;
   358 
   354 
   359 			$struc['perms'] 	= $this->gethchmod($path.'/'.$entry);
   355 			$struc['perms'] 	= $this->gethchmod($path.'/'.$entry);
   360 			$struc['permsn']	= $this->getnumchmodfromh($struc['perms']);
   356 			$struc['permsn']	= $this->getnumchmodfromh($struc['perms']);
   361 			$struc['number'] 	= false;
   357 			$struc['number'] 	= false;
   367 			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
   363 			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
   368 			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
   364 			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
   369 
   365 
   370 			if ( 'd' == $struc['type'] ) {
   366 			if ( 'd' == $struc['type'] ) {
   371 				if ( $recursive )
   367 				if ( $recursive )
   372 					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
   368 					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
   373 				else
   369 				else
   374 					$struc['files'] = array();
   370 					$struc['files'] = array();
   375 			}
   371 			}
   376 
   372 
   377 			$ret[ $struc['name'] ] = $struc;
   373 			$ret[ $struc['name'] ] = $struc;