WP_Filesystem_SSH2
- Since
- 2.7.0
- Source
wp-admin/includes/class-wp-filesystem-ssh2.php:36
Description
To use this class you must follow these steps for PHP 5.2.6+
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)
cd /usr/src wget https://www.libssh2.org/download/libssh2-0.14.tar.gz tar -zxvf libssh2-0.14.tar.gz cd libssh2-0.14/ ./configure make all install
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
Restart Apache! 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().
Compatibility
- WordPress
- since 2.7.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Properties · 3
$linkresourcepublic$sftp_linkresourcepublic$keysboolpublic
Methods · 30
- __construct()Constructor.
- connect()Connects filesystem.
- sftp_path()Gets the ssh2.sftp PHP stream wrapper path to open for the given file.
- run_command()
- get_contents()Reads entire file into a string.
- get_contents_array()Reads entire file into an array.
- put_contents()Writes a string to a file.
- cwd()Gets the current working directory.
- chdir()Changes current directory.
- chgrp()Changes the file group.
- chmod()Changes filesystem permissions.
- chown()Changes the owner of a file or directory.
- owner()Gets the file owner.
- getchmod()Gets the permissions of the specified file or filepath in their octal format.
- group()Gets the file's group.
- copy()Copies a file.
- move()Moves a file or directory.
- delete()Deletes a file or directory.
- exists()Checks if a file or directory exists.
- is_file()Checks if resource is a file.
- is_dir()Checks if resource is a directory.
- is_readable()Checks if a file is readable.
- is_writable()Checks if a file or directory is writable.
- atime()Gets the file's last access time.
- mtime()Gets the file modification time.
- size()Gets the file size (in bytes).
- touch()Sets the access and modification times of a file.
- mkdir()Creates a directory.
- rmdir()Deletes a directory.
- dirlist()Gets details for files in a directory or a specific file.
Source code
class WP_Filesystem_SSH2 extends WP_Filesystem_Base { /** * @since 2.7.0 * @var resource */ public $link = false; /** * @since 2.7.0 * @var resource */ public $sftp_link; /** * @since 2.7.0 * @var bool */ public $keys = false; /** * Constructor. * * @since 2.7.0 * * @param array $opt */ public function __construct( $opt = '' ) { $this->method = 'ssh2'; $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' ) ); return; } // Set defaults: if ( empty( $opt['port'] ) ) { $this->options['port'] = 22; } else { $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; } 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. if ( ! $this->keys ) { $this->errors->add( 'empty_password', __( 'SSH2 password is required' ) ); } else { $this->options['password'] = null; } } else { $this->options['password'] = $opt['password']; } } /** * Connects filesystem.Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-admin/includes/class-wp-filesystem-ssh2.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.