wp/wp-admin/includes/class-wp-filesystem-ssh2.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress Filesystem Class for implementing SSH2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * To use this class you must follow these steps for PHP 5.2.6+
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now, But many users have found the latest versions work)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * cd /usr/src
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * tar -zxvf libssh2-0.14.tar.gz
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * cd libssh2-0.14/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * ./configure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * make all install
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * Note: Do not leave the directory yet!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * Enter: pecl install -f ssh2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * Copy the ssh.so file it creates to your PHP Module Directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * Open up your PHP.INI file and look for where extensions are placed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * Add in your PHP.ini file: extension=ssh2.so
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * Restart Apache!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * @subpackage Filesystem
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	public $link = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
	 * @var resource
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	public $sftp_link;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	public $keys = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	public function __construct($opt='') {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		$this->method = 'ssh2';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		$this->errors = new WP_Error();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		//Check if possible to use ssh2 functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		if ( ! extension_loaded('ssh2') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
			$this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
			return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		if ( !function_exists('stream_get_contents') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
			$this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
			return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		// Set defaults:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		if ( empty($opt['port']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
			$this->options['port'] = 22;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			$this->options['port'] = $opt['port'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		if ( empty($opt['hostname']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			$this->errors->add('empty_hostname', __('SSH2 hostname is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			$this->options['hostname'] = $opt['hostname'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		// Check if the options provided are OK.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			$this->options['public_key'] = $opt['public_key'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			$this->options['private_key'] = $opt['private_key'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
			$this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
			$this->keys = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		} elseif ( empty ($opt['username']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
			$this->errors->add('empty_username', __('SSH2 username is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		if ( !empty($opt['username']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			$this->options['username'] = $opt['username'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		if ( empty ($opt['password']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
			// Password can be blank if we are using keys.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
			if ( !$this->keys )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				$this->errors->add('empty_password', __('SSH2 password is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			$this->options['password'] = $opt['password'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
	public function connect() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		if ( ! $this->keys ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			$this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
			$this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		if ( ! $this->link ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			$this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		if ( !$this->keys ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
			if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
				$this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
			if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
				$this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		$this->sftp_link = ssh2_sftp($this->link);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
	 * @param string $command
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
	 * @param bool $returnbool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   127
	 * @return bool|string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
	public function run_command( $command, $returnbool = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		if ( ! $this->link )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			$this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
			stream_set_blocking( $stream, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
			stream_set_timeout( $stream, FS_TIMEOUT );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			$data = stream_get_contents( $stream );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			fclose( $stream );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			if ( $returnbool )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
				return ( $data === false ) ? false : '' != trim($data);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
				return $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
	 * @return string|false
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
	public function get_contents( $file ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
	 * @return array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
	public function get_contents_array($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
	 * @param string $contents
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
	 * @param bool|int $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
	public function put_contents($file, $contents, $mode = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		$ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		if ( $ret !== strlen( $contents ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		$this->chmod($file, $mode);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
	public function cwd() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		$cwd = $this->run_command('pwd');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
		if ( $cwd ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
			$cwd = trailingslashit( trim( $cwd ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   189
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
		return $cwd;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	 * @param string $dir
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
	 * @return bool|string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
	public function chdir($dir) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
		return $this->run_command('cd ' . $dir, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
	 * @param string $group
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
	public function chgrp($file, $group, $recursive = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		if ( ! $this->exists($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		if ( ! $recursive || ! $this->is_dir($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
			return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
	 * @param int $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
	 * @return bool|string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
	public function chmod($file, $mode = false, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		if ( ! $this->exists($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		if ( ! $mode ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
			if ( $this->is_file($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
				$mode = FS_CHMOD_FILE;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			elseif ( $this->is_dir($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
				$mode = FS_CHMOD_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		if ( ! $recursive || ! $this->is_dir($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	 * Change the ownership of a file / folder.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	 * @since Unknown
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
	 * @param string     $file    Path to the file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	 * @param string|int $owner   A user name or number.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	 * @param bool       $recursive Optional. If set True changes file owner recursivly. Defaults to False.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	 * @return bool|string Returns true on success or false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
	public function chown( $file, $owner, $recursive = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		if ( ! $this->exists($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		if ( ! $recursive || ! $this->is_dir($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
			return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	 * @return string|false
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
	public function owner($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		$owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
		if ( ! $owneruid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
		if ( ! function_exists('posix_getpwuid') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
			return $owneruid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		$ownerarray = posix_getpwuid($owneruid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		return $ownerarray['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
	 * @return string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
	public function getchmod($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
		return substr( decoct( @fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ) ) ), -3 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	 * @return string|false
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
	public function group($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
		$gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		if ( ! $gid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
		if ( ! function_exists('posix_getgrgid') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
			return $gid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
		$grouparray = posix_getgrgid($gid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
		return $grouparray['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	 * @param string $source
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
	 * @param string $destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
	 * @param bool $overwrite
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
	 * @param int|bool $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
	public function copy($source, $destination, $overwrite = false, $mode = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		if ( ! $overwrite && $this->exists($destination) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
		$content = $this->get_contents($source);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		if ( false === $content)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
		return $this->put_contents($destination, $content, $mode);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	 * @param string $source
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
	 * @param string $destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	 * @param bool $overwrite
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
	public function move($source, $destination, $overwrite = false) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
		return @ssh2_sftp_rename( $this->sftp_link, $source, $destination );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	 * @param string|bool $type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
	public function delete($file, $recursive = false, $type = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		if ( 'f' == $type || $this->is_file($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			return ssh2_sftp_unlink($this->sftp_link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		if ( ! $recursive )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
			 return ssh2_sftp_rmdir($this->sftp_link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		$filelist = $this->dirlist($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
		if ( is_array($filelist) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
			foreach ( $filelist as $filename => $fileinfo) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
				$this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		return ssh2_sftp_rmdir($this->sftp_link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
	public function exists($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
		return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	public function is_file($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
		return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
	public function is_dir($path) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		$path = ltrim($path, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
	public function is_readable($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
	public function is_writable($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
		return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @return int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
	public function atime($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
	 * @return int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
	public function mtime($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
	 * @return int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
	public function size($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
		$file = ltrim($file, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   405
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   406
	 * @param int $time
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   407
	 * @param int $atime
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   408
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   409
	public function touch($file, $time = 0, $atime = 0) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		//Not implemented.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   413
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   414
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   415
	 * @param mixed $chmod
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
	 * @param mixed $chown
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
	 * @param mixed $chgrp
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   418
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   420
	public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		$path = untrailingslashit($path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
		if ( empty($path) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
		if ( ! $chmod )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
			$chmod = FS_CHMOD_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
		if ( $chown )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
			$this->chown($path, $chown);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		if ( $chgrp )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			$this->chgrp($path, $chgrp);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   438
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   440
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
	public function rmdir($path, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
		return $this->delete($path, $recursive);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   447
	 * @param bool $include_hidden
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   448
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   449
	 * @return bool|array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   450
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
	public function dirlist($path, $include_hidden = true, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
		if ( $this->is_file($path) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
			$limit_file = basename($path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
			$path = dirname($path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
			$limit_file = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
		if ( ! $this->is_dir($path) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		$ret = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
		$dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		if ( ! $dir )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		while (false !== ($entry = $dir->read()) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
			$struc = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
			$struc['name'] = $entry;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
			if ( '.' == $struc['name'] || '..' == $struc['name'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
				continue; //Do not care about these folders.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
			if ( ! $include_hidden && '.' == $struc['name'][0] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
			if ( $limit_file && $struc['name'] != $limit_file )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
			$struc['perms'] 	= $this->gethchmod($path.'/'.$entry);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
			$struc['permsn']	= $this->getnumchmodfromh($struc['perms']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			$struc['number'] 	= false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
			$struc['owner']    	= $this->owner($path.'/'.$entry);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
			$struc['group']    	= $this->group($path.'/'.$entry);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
			$struc['size']    	= $this->size($path.'/'.$entry);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
			$struc['lastmodunix']= $this->mtime($path.'/'.$entry);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
			$struc['lastmod']   = date('M j',$struc['lastmodunix']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
			if ( 'd' == $struc['type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
				if ( $recursive )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
					$struc['files'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
			$ret[ $struc['name'] ] = $struc;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
		$dir->close();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
		unset($dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
		return $ret;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
}