* WordPress Filesystem Class for implementing SSH2
* To use this class you must follow these steps for PHP 5.2.6+
* {@link http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes}
* Compile libssh2 (Note: Only 0.14 is officially working with PHP 5.2.6+ right now, But many users have found the latest versions work)
* wget https://www.libssh2.org/download/libssh2-0.14.tar.gz
* tar -zxvf libssh2-0.14.tar.gz
* Note: Do not leave the directory yet!
* Enter: pecl install -f ssh2
* Copy the ssh.so file it creates to your PHP Module Directory.
* Open up your PHP.INI file and look for where extensions are placed.
* Add in your PHP.ini file: extension=ssh2.so
* Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
* Note: As of WordPress 2.8, this utilizes the PHP5+ function `stream_get_contents()`.
class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
public function __construct( $opt = '' ) {
$this->errors = new WP_Error();
// Check if possible to use ssh2 functions.
if ( ! extension_loaded( 'ssh2' ) ) {
$this->errors->add( 'no_ssh2_ext', __( 'The ssh2 PHP extension is not available' ) );
if ( empty( $opt['port'] ) ) {
$this->options['port'] = 22;
$this->options['port'] = $opt['port'];
if ( empty( $opt['hostname'] ) ) {
$this->errors->add( 'empty_hostname', __( 'SSH2 hostname is required' ) );
$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' );
} elseif ( empty( $opt['username'] ) ) {
$this->errors->add( 'empty_username', __( 'SSH2 username is required' ) );
if ( ! empty( $opt['username'] ) ) {
$this->options['username'] = $opt['username'];
if ( empty( $opt['password'] ) ) {
// Password can be blank if we are using keys.
$this->errors->add( 'empty_password', __( 'SSH2 password is required' ) );
$this->options['password'] = null;
$this->options['password'] = $opt['password'];
* @return bool True on success, false on failure.
public function connect() {
$this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'] );
$this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'], $this->options['hostkey'] );
/* translators: %s: hostname:port */
__( 'Failed to connect to SSH2 Server %s' ),
$this->options['hostname'] . ':' . $this->options['port']
if ( ! @ssh2_auth_password( $this->link, $this->options['username'], $this->options['password'] ) ) {
/* translators: %s: Username. */
__( 'Username/Password incorrect for %s' ),
$this->options['username']
if ( ! @ssh2_auth_pubkey_file( $this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
/* translators: %s: Username. */
__( 'Public and Private keys incorrect for %s' ),
$this->options['username']
$this->sftp_link = ssh2_sftp( $this->link );
if ( ! $this->sftp_link ) {
/* translators: %s: hostname:port */
__( 'Failed to initialize a SFTP subsystem session with the SSH2 Server %s' ),
$this->options['hostname'] . ':' . $this->options['port']
* Gets the ssh2.sftp PHP stream wrapper path to open for the given file.
* This method also works around a PHP bug where the root directory (/) cannot
* be opened by PHP functions, causing a false failure. In order to work around
* this, the path is converted to /./ which is semantically the same as /
* See https://bugs.php.net/bug.php?id=64169 for more details.
* @param string $path The File/Directory path on the remote server to return
* @return string The ssh2.sftp:// wrapped path to use.
public function sftp_path( $path ) {
return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' );
* @param bool $returnbool
* @return bool|string True on success, false on failure. String if the command was executed, `$returnbool`
* is false (default), and data from the resulting stream was retrieved.
public function run_command( $command, $returnbool = false ) {
$stream = ssh2_exec( $this->link, $command );
/* translators: %s: Command. */
__( 'Unable to perform command: %s' ),
stream_set_blocking( $stream, true );
stream_set_timeout( $stream, FS_TIMEOUT );
$data = stream_get_contents( $stream );
return ( false === $data ) ? false : '' !== trim( $data );
* Reads entire file into a string.
* @param string $file Name of the file to read.
* @return string|false Read data on success, false if no temporary file could be opened,
* or if the file couldn't be retrieved.
public function get_contents( $file ) {
return file_get_contents( $this->sftp_path( $file ) );
* Reads entire file into an array.
* @param string $file Path to the file.
* @return array|false File contents in an array on success, false on failure.
public function get_contents_array( $file ) {
return file( $this->sftp_path( $file ) );
* Writes a string to a file.
* @param string $file Remote path to the file where to write the data.
* @param string $contents The data to write.
* @param int|false $mode Optional. The file permissions as octal number, usually 0644.
* @return bool True on success, false on failure.
public function put_contents( $file, $contents, $mode = false ) {
$ret = file_put_contents( $this->sftp_path( $file ), $contents );
if ( strlen( $contents ) !== $ret ) {
$this->chmod( $file, $mode );
* Gets the current working directory.
* @return string|false The current working directory on success, false on failure.
$cwd = ssh2_sftp_realpath( $this->sftp_link, '.' );
$cwd = trailingslashit( trim( $cwd ) );
* Changes current directory.
* @param string $dir The new current directory.
* @return bool True on success, false on failure.
public function chdir( $dir ) {
return $this->run_command( 'cd ' . $dir, true );
* Changes the file group.
* @param string $file Path to the file.
* @param string|int $group A group name or number.
* @param bool $recursive Optional. If set to true, changes file group recursively.
* @return bool True on success, false on failure.
public function chgrp( $file, $group, $recursive = false ) {
if ( ! $this->exists( $file ) ) {
if ( ! $recursive || ! $this->is_dir( $file ) ) {
return $this->run_command( sprintf( 'chgrp %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
return $this->run_command( sprintf( 'chgrp -R %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
* Changes filesystem permissions.
* @param string $file Path to the file.
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
* 0755 for directories. Default false.
* @param bool $recursive Optional. If set to true, changes file permissions recursively.
* @return bool True on success, false on failure.
public function chmod( $file, $mode = false, $recursive = false ) {
if ( ! $this->exists( $file ) ) {
if ( $this->is_file( $file ) ) {
} elseif ( $this->is_dir( $file ) ) {
if ( ! $recursive || ! $this->is_dir( $file ) ) {
return $this->run_command( sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ), true );
return $this->run_command( sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ), true );
* Changes the owner of a file or directory.
* @param string $file Path to the file or directory.
* @param string|int $owner A user name or number.
* @param bool $recursive Optional. If set to true, changes file owner recursively.
* @return bool True on success, false on failure.
public function chown( $file, $owner, $recursive = false ) {
if ( ! $this->exists( $file ) ) {
if ( ! $recursive || ! $this->is_dir( $file ) ) {
return $this->run_command( sprintf( 'chown %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
return $this->run_command( sprintf( 'chown -R %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
* @param string $file Path to the file.
* @return string|false Username of the owner on success, false on failure.
public function owner( $file ) {
$owneruid = @fileowner( $this->sftp_path( $file ) );
if ( ! function_exists( 'posix_getpwuid' ) ) {
$ownerarray = posix_getpwuid( $owneruid );
return $ownerarray['name'];
* Gets the permissions of the specified file or filepath in their octal format.
* @param string $file Path to the file.
* @return string Mode of the file (the last 3 digits).
public function getchmod( $file ) {
return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 );
* @param string $file Path to the file.
* @return string|false The group on success, false on failure.
public function group( $file ) {
$gid = @filegroup( $this->sftp_path( $file ) );
if ( ! function_exists( 'posix_getgrgid' ) ) {
$grouparray = posix_getgrgid( $gid );
return $grouparray['name'];
* @param string $source Path to the source file.
* @param string $destination Path to the destination file.
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
* 0755 for dirs. Default false.
* @return bool True on success, false on failure.
public function copy( $source, $destination, $overwrite = false, $mode = false ) {
if ( ! $overwrite && $this->exists( $destination ) ) {
$content = $this->get_contents( $source );
if ( false === $content ) {
return $this->put_contents( $destination, $content, $mode );