| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined('ABSPATH') ) |
| 4 |
die(); |
| 5 |
|
| 6 |
/* Adapted from http://www.solutionbot.com/2009/01/02/php-ftp-class/ */ |
| 7 |
class IWP_MMB_ftp_wrapper { |
| 8 |
private $conn_id; |
| 9 |
private $host; |
| 10 |
private $username; |
| 11 |
private $password; |
| 12 |
private $port; |
| 13 |
public $timeout = 60; |
| 14 |
public $passive = true; |
| 15 |
public $system_type = ''; |
| 16 |
public $ssl = true; |
| 17 |
public $use_server_certs = false; |
| 18 |
public $disable_verify = true; |
| 19 |
public $login_type = 'non-encrypted'; |
| 20 |
|
| 21 |
public function __construct($host, $username, $password, $port = 21) { |
| 22 |
$this->host = $host; |
| 23 |
$this->username = $username; |
| 24 |
$this->password = $password; |
| 25 |
$this->port = $port; |
| 26 |
} |
| 27 |
|
| 28 |
public function connect() { |
| 29 |
|
| 30 |
$time_start = time(); |
| 31 |
if (function_exists('ftp_ssl_connect') && false !== $this->ssl) { |
| 32 |
$this->conn_id = ftp_ssl_connect($this->host, $this->port, 15); |
| 33 |
$attempting_ssl = true; |
| 34 |
} |
| 35 |
|
| 36 |
if ($this->conn_id) { |
| 37 |
$this->login_type = 'encrypted'; |
| 38 |
$this->ssl = true; |
| 39 |
} else { |
| 40 |
$this->conn_id = ftp_connect($this->host, $this->port, 15); |
| 41 |
} |
| 42 |
|
| 43 |
if ($this->conn_id) $result = ftp_login($this->conn_id, $this->username, $this->password); |
| 44 |
|
| 45 |
if (!empty($result)) { |
| 46 |
ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout); |
| 47 |
ftp_pasv($this->conn_id, $this->passive); |
| 48 |
$this->system_type = ftp_systype($this->conn_id); |
| 49 |
return true; |
| 50 |
} elseif (!empty($attempting_ssl)) { |
| 51 |
$this->ssl = false; |
| 52 |
$this->login_type = 'non-encrypted'; |
| 53 |
$time_start = time(); |
| 54 |
$this->conn_id = ftp_connect($this->host, $this->port, 15); |
| 55 |
if ($this->conn_id) $result = ftp_login($this->conn_id, $this->username, $this->password); |
| 56 |
if (!empty($result)) { |
| 57 |
ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout); |
| 58 |
ftp_pasv($this->conn_id, $this->passive); |
| 59 |
$this->system_type = ftp_systype($this->conn_id); |
| 60 |
return true; |
| 61 |
} else { |
| 62 |
// Add back the previous PHP messages |
| 63 |
|
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// If we got here, then we failed |
| 68 |
if (time() - $time_start > 19) { |
| 69 |
global $iwp_backup_core; |
| 70 |
$iwp_backup_core->log(sprintf(__('The %s connection timed out; if you entered the server correctly, then this is usually caused by a firewall blocking the connection - you should check with your web hosting company.', 'InfiniteWP'), 'FTP'), 'error'); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
public function put($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $iwp_backup_core = false, $ftp_remote_path = false) { |
| 78 |
if ($iwp_backup_core == false) { |
| 79 |
global $iwp_backup_core; |
| 80 |
} |
| 81 |
$file_size = filesize($local_file_path); |
| 82 |
|
| 83 |
$existing_size = 0; |
| 84 |
if ($resume) { |
| 85 |
$existing_size = ftp_size($this->conn_id, $remote_file_path); |
| 86 |
if ($existing_size <=0) { |
| 87 |
$resume = false; $existing_size = 0; |
| 88 |
} else { |
| 89 |
if (is_a($iwp_backup_core, 'IWP_MMB_Backup_Core')) $iwp_backup_core->log("File already exists at remote site: size $existing_size. Will attempt resumption."); |
| 90 |
if ($existing_size >= $file_size) { |
| 91 |
if (is_a($iwp_backup_core, 'IWP_MMB_Backup_Core')) $iwp_backup_core->log("File is apparently already completely uploaded"); |
| 92 |
return true; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// From here on, $file_size is only used for logging calculations. We want to avoid divsion by zero. |
| 98 |
$file_size = max($file_size, 1); |
| 99 |
|
| 100 |
if (!$fh = fopen($local_file_path, 'rb')) return false; |
| 101 |
if ($existing_size) fseek($fh, $existing_size); |
| 102 |
@ftp_mkdir($this->conn_id, $ftp_remote_path); |
| 103 |
$ret = ftp_nb_fput($this->conn_id, $remote_file_path, $fh, FTP_BINARY, $existing_size); |
| 104 |
|
| 105 |
// $existing_size can now be re-purposed |
| 106 |
|
| 107 |
while ($ret == FTP_MOREDATA) { |
| 108 |
if (is_a($iwp_backup_core, 'IWP_MMB_Backup_Core')) { |
| 109 |
$new_size = ftell($fh); |
| 110 |
$record_after = 524288; |
| 111 |
if ($existing_size > 2097152) { |
| 112 |
$record_after = ($existing_size > 4194304) ? 2097152 : 1048576; |
| 113 |
} |
| 114 |
if ($new_size - $existing_size > $record_after) { |
| 115 |
$existing_size = $new_size; |
| 116 |
$percent = round(100*$new_size/$file_size,1); |
| 117 |
$iwp_backup_core->record_uploaded_chunk($percent, '', $local_file_path); |
| 118 |
} |
| 119 |
} |
| 120 |
// Continue upload |
| 121 |
$ret = ftp_nb_continue($this->conn_id); |
| 122 |
} |
| 123 |
|
| 124 |
fclose($fh); |
| 125 |
|
| 126 |
if ($ret != FTP_FINISHED) { |
| 127 |
if (is_a($iwp_backup_core, 'IWP_MMB_Backup_Core')) $iwp_backup_core->log("FTP upload: error ($ret)"); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
public function get($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $iwp_backup_core = false) { |
| 136 |
if ($iwp_backup_core == false) { |
| 137 |
global $iwp_backup_core; |
| 138 |
} |
| 139 |
$file_last_size = 0; |
| 140 |
|
| 141 |
if ($resume) { |
| 142 |
if (!$fh = fopen($local_file_path, 'ab')) return false; |
| 143 |
clearstatcache($local_file_path); |
| 144 |
$file_last_size = filesize($local_file_path); |
| 145 |
} else { |
| 146 |
if (!$fh = fopen($local_file_path, 'wb')) return false; |
| 147 |
} |
| 148 |
|
| 149 |
$ret = ftp_nb_fget($this->conn_id, $fh, $remote_file_path, $mode, $file_last_size); |
| 150 |
|
| 151 |
if (false == $ret) return false; |
| 152 |
|
| 153 |
while ($ret == FTP_MOREDATA) { |
| 154 |
|
| 155 |
if($iwp_backup_core->restore_loop_break()){ |
| 156 |
return 'partial'; |
| 157 |
} |
| 158 |
|
| 159 |
if ($iwp_backup_core) { |
| 160 |
$file_now_size = filesize($local_file_path); |
| 161 |
if ($file_now_size - $file_last_size > 524288) { |
| 162 |
$iwp_backup_core->log("FTP fetch: file size is now: ".sprintf("%0.2f",filesize($local_file_path)/1048576)." Mb"); |
| 163 |
$file_last_size = $file_now_size; |
| 164 |
} |
| 165 |
clearstatcache($local_file_path); |
| 166 |
} |
| 167 |
|
| 168 |
$ret = ftp_nb_continue($this->conn_id); |
| 169 |
} |
| 170 |
|
| 171 |
fclose($fh); |
| 172 |
|
| 173 |
if ($ret == FTP_FINISHED) { |
| 174 |
if ($iwp_backup_core) $iwp_backup_core->log("FTP fetch: fetch complete"); |
| 175 |
return true; |
| 176 |
} else { |
| 177 |
if ($iwp_backup_core) $iwp_backup_core->log("FTP fetch: fetch failed"); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
public function chmod($permissions, $remote_filename) { |
| 184 |
if ($this->is_octal($permissions)) { |
| 185 |
$result = ftp_chmod($this->conn_id, $permissions, $remote_filename); |
| 186 |
if ($result) { |
| 187 |
return true; |
| 188 |
} else { |
| 189 |
return false; |
| 190 |
} |
| 191 |
} else { |
| 192 |
throw new Exception('$permissions must be an octal number'); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public function chdir($directory) { |
| 197 |
ftp_chdir($this->conn_id, $directory); |
| 198 |
} |
| 199 |
|
| 200 |
public function delete($remote_file_path) { |
| 201 |
if (ftp_delete($this->conn_id, $remote_file_path)) { |
| 202 |
return true; |
| 203 |
} else { |
| 204 |
return false; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public function make_dir($directory) { |
| 209 |
if (ftp_mkdir($this->conn_id, $directory)) { |
| 210 |
return true; |
| 211 |
} else { |
| 212 |
return false; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
public function rename($old_name, $new_name) { |
| 217 |
if (ftp_rename($this->conn_id, $old_name, $new_name)) |
| 218 |
{ |
| 219 |
return true; |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
return false; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public function remove_dir($directory) { |
| 228 |
if (ftp_rmdir($this->conn_id, $directory)) |
| 229 |
{ |
| 230 |
return true; |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
return false; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
public function dir_list($directory) { |
| 239 |
return ftp_nlist($this->conn_id, $directory); |
| 240 |
} |
| 241 |
|
| 242 |
public function cdup() { |
| 243 |
ftp_cdup($this->conn_id); |
| 244 |
} |
| 245 |
|
| 246 |
public function size($f) { |
| 247 |
return ftp_size($this->conn_id, $f); |
| 248 |
} |
| 249 |
|
| 250 |
public function current_dir() { |
| 251 |
return ftp_pwd($this->conn_id); |
| 252 |
} |
| 253 |
|
| 254 |
private function is_octal($i) { |
| 255 |
return decoct(octdec($i)) == $i; |
| 256 |
} |
| 257 |
|
| 258 |
public function __destruct() { |
| 259 |
if ($this->conn_id) ftp_close($this->conn_id); |
| 260 |
} |
| 261 |
} |
| 262 |
?> |
| 263 |
|