WP_Filesystem_SSH2::__constructpublicWP 2.7.0

Constructor.

Method of the class: WP_Filesystem_SSH2{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WP_Filesystem_SSH2 = new WP_Filesystem_SSH2();
$WP_Filesystem_SSH2->__construct( $opt );
$opt(array)

Array of connection options.

Default: null

  • hostname(string)
    Required. SSH server hostname.

  • username(string)
    Required. SSH username.

  • port(int)
    Optional. SSH server port.
    Default: 22

  • password(string)
    Optional. SSH password. May be empty when using keys.

  • public_key(string)
    Optional. Path to public key file for publickey authentication.

  • private_key(string)
    Optional. Path to private key file for publickey authentication.

Changelog

Since 2.7.0 Introduced.

WP_Filesystem_SSH2::__construct() code WP 7.1

public function __construct( $opt = null ) {
	$this->method  = 'ssh2';
	$this->errors  = new WP_Error();
	$this->options = array(
		'port'     => 22,
		'hostname' => '',
		'username' => '',
		'password' => null,
	);

	// Check if possible to use ssh2 functions.
	if ( ! extension_loaded( 'ssh2' ) ) {
		$this->errors->add( 'no_ssh2_ext', __( 'The ssh2 PHP extension is not available' ) );
		return;
	}

	if ( ! is_array( $opt ) ) {
		$opt = array();
	}

	// Set defaults:
	if ( ! empty( $opt['port'] ) ) {
		$this->options['port'] = $opt['port'];
	}

	if ( empty( $opt['hostname'] ) ) {
		$this->errors->add( 'empty_hostname', __( 'SSH2 hostname is required' ) );
	} else {
		$this->options['hostname'] = $opt['hostname'];
	}

	// Check if the options provided are OK.
	if ( ! empty( $opt['public_key'] ) && ! empty( $opt['private_key'] ) ) {
		$this->options['public_key']  = $opt['public_key'];
		$this->options['private_key'] = $opt['private_key'];

		$this->options['hostkey'] = array( 'hostkey' => 'ssh-rsa,ssh-ed25519' );

		$this->keys = true;
	}

	// A username is always required, whether authenticating with a password or with keys.
	if ( empty( $opt['username'] ) ) {
		$this->errors->add( 'empty_username', __( 'SSH2 username is required' ) );
	} else {
		$this->options['username'] = $opt['username'];
	}

	if ( ! empty( $opt['password'] ) ) {
		$this->options['password'] = $opt['password'];
	} elseif ( ! $this->keys ) {
		// Password can be blank if we are using keys.
		$this->errors->add( 'empty_password', __( 'SSH2 password is required' ) );
	}
}