wp/wp-admin/includes/class-wp-filesystem-ftpext.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 11:14:17 +0000
changeset 6 490d5cc509ed
parent 5 5e2f62d02dcd
child 7 cf61fcea0001
permissions -rw-r--r--
update portfolio
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 FTP Filesystem.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Filesystem
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * WordPress Filesystem Class for implementing FTP.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * @subpackage Filesystem
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * @uses WP_Filesystem_Base Extends class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
class WP_Filesystem_FTPext extends WP_Filesystem_Base {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    18
	public $link;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
	public function __construct($opt='') {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
		$this->method = 'ftpext';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		$this->errors = new WP_Error();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		// Check if possible to use ftp functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		if ( ! extension_loaded('ftp') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			$this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
			return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		// This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		if ( ! defined('FS_TIMEOUT') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
			define('FS_TIMEOUT', 240);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		if ( empty($opt['port']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			$this->options['port'] = 21;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			$this->options['port'] = $opt['port'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		if ( empty($opt['hostname']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
			$this->errors->add('empty_hostname', __('FTP hostname is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			$this->options['hostname'] = $opt['hostname'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		// Check if the options provided are OK.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		if ( empty($opt['username']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
			$this->errors->add('empty_username', __('FTP username is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
			$this->options['username'] = $opt['username'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		if ( empty($opt['password']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			$this->errors->add('empty_password', __('FTP password is required'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
			$this->options['password'] = $opt['password'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		$this->options['ssl'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
			$this->options['ssl'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	public function connect() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			$this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
			$this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		if ( ! $this->link ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			$this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		// Set the Connection to use Passive FTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		@ftp_pasv( $this->link, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
			@ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
	 * @return false|string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
	public function get_contents( $file ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
		$tempfile = wp_tempnam($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		$temp = fopen($tempfile, 'w+');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		if ( ! $temp )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		fseek( $temp, 0 ); // Skip back to the start of the file being written to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		$contents = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		while ( ! feof($temp) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			$contents .= fread($temp, 8192);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		fclose($temp);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		unlink($tempfile);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		return $contents;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
	 * @return array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
	public function get_contents_array($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		return explode("\n", $this->get_contents($file));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
	 * @param string $contents
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
	 * @param bool|int $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
	public function put_contents($file, $contents, $mode = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		$tempfile = wp_tempnam($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		$temp = fopen( $tempfile, 'wb+' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		if ( ! $temp )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		mbstring_binary_safe_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
		$data_length = strlen( $contents );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		$bytes_written = fwrite( $temp, $contents );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		reset_mbstring_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		if ( $data_length !== $bytes_written ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
			fclose( $temp );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			unlink( $tempfile );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		fseek( $temp, 0 ); // Skip back to the start of the file being written to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		$ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		fclose($temp);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		unlink($tempfile);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$this->chmod($file, $mode);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		return $ret;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
	 * @return string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
	public function cwd() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
		$cwd = @ftp_pwd($this->link);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		if ( $cwd )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
			$cwd = trailingslashit($cwd);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		return $cwd;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
	 * @param string $dir
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
	 * @return bool
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
	public function chdir($dir) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		return @ftp_chdir($this->link, $dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
5
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
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
	 * @param int $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	public function chmod($file, $mode = false, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		if ( ! $mode ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
			if ( $this->is_file($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
				$mode = FS_CHMOD_FILE;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
			elseif ( $this->is_dir($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
				$mode = FS_CHMOD_DIR;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		// chmod any sub-objects if recursive.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
		if ( $recursive && $this->is_dir($file) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
			$filelist = $this->dirlist($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
			foreach ( (array)$filelist as $filename => $filemeta )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
				$this->chmod($file . '/' . $filename, $mode, $recursive);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		// chmod the file or directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		if ( ! function_exists('ftp_chmod') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
		return (bool)@ftp_chmod($this->link, $mode, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
	 * @return string
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 owner($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		$dir = $this->dirlist($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		return $dir[$file]['owner'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   211
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
	 * @return string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
	public function getchmod($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		$dir = $this->dirlist($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		return $dir[$file]['permsn'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
	 * @return string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
	public function group($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		$dir = $this->dirlist($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		return $dir[$file]['group'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
	 * @param string $source
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	 * @param string $destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
	 * @param bool   $overwrite
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   232
	 * @param string|bool $mode
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
	public function copy($source, $destination, $overwrite = false, $mode = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		if ( ! $overwrite && $this->exists($destination) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		$content = $this->get_contents($source);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
		if ( false === $content )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		return $this->put_contents($destination, $content, $mode);
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
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	 * @param string $source
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	 * @param string $destination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	 * @param bool $overwrite
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   247
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
	public function move($source, $destination, $overwrite = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		return ftp_rename($this->link, $source, $destination);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
	 * @param string $type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	public function delete($file, $recursive = false, $type = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		if ( empty($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		if ( 'f' == $type || $this->is_file($file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
			return @ftp_delete($this->link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
		if ( !$recursive )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
			return @ftp_rmdir($this->link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		$filelist = $this->dirlist( trailingslashit($file) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		if ( !empty($filelist) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
			foreach ( $filelist as $delete_file )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
				$this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
		return @ftp_rmdir($this->link, $file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
	}
5
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
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
	public function exists($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
		$list = @ftp_nlist($this->link, $file);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
		if ( empty( $list ) && $this->is_dir( $file ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
			return true; // File is an empty directory.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		return !empty($list); //empty list = no file, so invert.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
	public function is_file($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
		return $this->exists($file) && !$this->is_dir($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
	public function is_dir($path) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		$cwd = $this->cwd();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
		$result = @ftp_chdir($this->link, trailingslashit($path) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
			@ftp_chdir($this->link, $cwd);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		}
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
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	public function is_readable($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
		return true;
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
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	public function is_writable($file) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
	public function atime($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   325
		return false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
	 * @return int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	public function mtime($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
		return ftp_mdtm($this->link, $file);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
	 * @return int
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
	public function size($file) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
		return ftp_size($this->link, $file);
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
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	 * @param string $file
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	public function touch($file, $time = 0, $atime = 0) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
	 * @param mixed $chmod
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
	 * @param mixed $chown
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
	 * @param mixed $chgrp
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
	public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
		$path = untrailingslashit($path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		if ( empty($path) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
		if ( !@ftp_mkdir($this->link, $path) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
		$this->chmod($path, $chmod);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	public function rmdir($path, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		return $this->delete($path, $recursive);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 * @staticvar bool $is_windows
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 * @param string $line
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @return string
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 parselisting($line) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
		static $is_windows;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		if ( is_null($is_windows) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
			$is_windows = stripos( ftp_systype($this->link), 'win') !== false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		if ( $is_windows && preg_match('/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
			$b = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
			if ( $lucifer[3] < 70 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
				$lucifer[3] +=2000;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
				$lucifer[3] += 1900; // 4digit year fix
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
			$b['isdir'] = ( $lucifer[7] == '<DIR>');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			if ( $b['isdir'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
				$b['type'] = 'd';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
				$b['type'] = 'f';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
			$b['size'] = $lucifer[7];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
			$b['month'] = $lucifer[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
			$b['day'] = $lucifer[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			$b['year'] = $lucifer[3];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
			$b['hour'] = $lucifer[4];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
			$b['minute'] = $lucifer[5];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
			$b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			$b['am/pm'] = $lucifer[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
			$b['name'] = $lucifer[8];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
		} elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			//echo $line."\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
			$lcount = count($lucifer);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			if ( $lcount < 8 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
				return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
			$b = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
			$b['isdir'] = $lucifer[0]{0} === 'd';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			$b['islink'] = $lucifer[0]{0} === 'l';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
			if ( $b['isdir'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
				$b['type'] = 'd';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
			elseif ( $b['islink'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
				$b['type'] = 'l';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
				$b['type'] = 'f';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			$b['perms'] = $lucifer[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
			$b['number'] = $lucifer[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
			$b['owner'] = $lucifer[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
			$b['group'] = $lucifer[3];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
			$b['size'] = $lucifer[4];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
			if ( $lcount == 8 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
				sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
				sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
				$b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
				$b['name'] = $lucifer[7];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
				$b['month'] = $lucifer[5];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
				$b['day'] = $lucifer[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
				if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
					$b['year'] = date("Y");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
					$b['hour'] = $l2[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
					$b['minute'] = $l2[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
					$b['year'] = $lucifer[7];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
					$b['hour'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
					$b['minute'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
				$b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
				$b['name'] = $lucifer[8];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		// Replace symlinks formatted as "source -> target" with just the source name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		if ( $b['islink'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
			$b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
		return $b;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   454
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   455
	 * @param string $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
	 * @param bool $include_hidden
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
	 * @param bool $recursive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
	 * @return bool|array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
	public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
		if ( $this->is_file($path) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
			$limit_file = basename($path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
			$path = dirname($path) . '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
			$limit_file = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		$pwd = @ftp_pwd($this->link);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
		if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		$list = @ftp_rawlist($this->link, '-a', false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
		@ftp_chdir($this->link, $pwd);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
		if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
		$dirlist = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		foreach ( $list as $k => $v ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
			$entry = $this->parselisting($v);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
			if ( empty($entry) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			if ( '.' == $entry['name'] || '..' == $entry['name'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
			if ( ! $include_hidden && '.' == $entry['name'][0] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
			if ( $limit_file && $entry['name'] != $limit_file)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
			$dirlist[ $entry['name'] ] = $entry;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
		$ret = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
		foreach ( (array)$dirlist as $struc ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
			if ( 'd' == $struc['type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
				if ( $recursive )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
					$struc['files'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
			$ret[ $struc['name'] ] = $struc;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
		return $ret;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
	public function __destruct() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
		if ( $this->link )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
			ftp_close($this->link);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
}