Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../includes
File: ftp.class.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.');
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Adapted from http://www.solutionbot.com/2009/01/02/php-ftp-class/
[5] Fix | Delete
*/
[6] Fix | Delete
class UpdraftPlus_ftp_wrapper {
[7] Fix | Delete
[8] Fix | Delete
private $conn_id;
[9] Fix | Delete
[10] Fix | Delete
private $host;
[11] Fix | Delete
[12] Fix | Delete
private $username;
[13] Fix | Delete
[14] Fix | Delete
private $password;
[15] Fix | Delete
[16] Fix | Delete
private $port;
[17] Fix | Delete
[18] Fix | Delete
public $timeout = 60;
[19] Fix | Delete
[20] Fix | Delete
public $passive = true;
[21] Fix | Delete
[22] Fix | Delete
public $system_type = '';
[23] Fix | Delete
[24] Fix | Delete
public $ssl = false;
[25] Fix | Delete
[26] Fix | Delete
public $use_server_certs = false;
[27] Fix | Delete
[28] Fix | Delete
public $disable_verify = true;
[29] Fix | Delete
[30] Fix | Delete
public $login_type = 'non-encrypted';
[31] Fix | Delete
[32] Fix | Delete
public function __construct($host, $username, $password, $port = 21) {
[33] Fix | Delete
$this->host = $host;
[34] Fix | Delete
$this->username = $username;
[35] Fix | Delete
$this->password = $password;
[36] Fix | Delete
$this->port = $port;
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
public function connect() {
[40] Fix | Delete
[41] Fix | Delete
$time_start = time();
[42] Fix | Delete
$this->conn_id = ftp_connect($this->host, $this->port, 20);
[43] Fix | Delete
[44] Fix | Delete
if ($this->conn_id) $result = ftp_login($this->conn_id, $this->username, $this->password);
[45] Fix | Delete
[46] Fix | Delete
if (!empty($result)) {
[47] Fix | Delete
ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout);
[48] Fix | Delete
ftp_pasv($this->conn_id, $this->passive);
[49] Fix | Delete
$this->system_type = ftp_systype($this->conn_id);
[50] Fix | Delete
return true;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
if (time() - $time_start > 19) {
[54] Fix | Delete
global $updraftplus_admin;
[55] Fix | Delete
if (isset($updraftplus_admin->logged) && is_array($updraftplus_admin->logged)) {
[56] Fix | Delete
$updraftplus_admin->logged[] = 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.', 'updraftplus'), 'FTP');
[57] Fix | Delete
} else {
[58] Fix | Delete
global $updraftplus;
[59] Fix | Delete
$updraftplus->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.', 'updraftplus'), 'FTP'), 'error');
[60] Fix | Delete
}
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
return false;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
public function put($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) {
[67] Fix | Delete
[68] Fix | Delete
$file_size = filesize($local_file_path);
[69] Fix | Delete
[70] Fix | Delete
$existing_size = 0;
[71] Fix | Delete
if ($resume) {
[72] Fix | Delete
$existing_size = ftp_size($this->conn_id, $remote_file_path);
[73] Fix | Delete
if ($existing_size <=0) {
[74] Fix | Delete
$resume = false;
[75] Fix | Delete
$existing_size = 0;
[76] Fix | Delete
} else {
[77] Fix | Delete
if (is_a($updraftplus, 'UpdraftPlus')) $updraftplus->log("File already exists at remote site: size $existing_size. Will attempt resumption.");
[78] Fix | Delete
if ($existing_size >= $file_size) {
[79] Fix | Delete
if (is_a($updraftplus, 'UpdraftPlus')) $updraftplus->log("File is apparently already completely uploaded");
[80] Fix | Delete
return true;
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
// From here on, $file_size is only used for logging calculations. We want to avoid divsion by zero.
[86] Fix | Delete
$file_size = max($file_size, 1);
[87] Fix | Delete
[88] Fix | Delete
if (!$fh = fopen($local_file_path, 'rb')) return false;
[89] Fix | Delete
if ($existing_size) fseek($fh, $existing_size);
[90] Fix | Delete
[91] Fix | Delete
$ret = ftp_nb_fput($this->conn_id, $remote_file_path, $fh, $mode, $existing_size);
[92] Fix | Delete
[93] Fix | Delete
// $existing_size can now be re-purposed
[94] Fix | Delete
[95] Fix | Delete
while (FTP_MOREDATA == $ret) {
[96] Fix | Delete
if (is_a($updraftplus, 'UpdraftPlus')) {
[97] Fix | Delete
$new_size = ftell($fh);
[98] Fix | Delete
$record_after = 524288;
[99] Fix | Delete
if ($existing_size > 2097152) {
[100] Fix | Delete
$record_after = ($existing_size > 4194304) ? 2097152 : 1048576;
[101] Fix | Delete
}
[102] Fix | Delete
if ($new_size - $existing_size > $record_after) {
[103] Fix | Delete
$existing_size = $new_size;
[104] Fix | Delete
$percent = round(100*$new_size/$file_size, 1);
[105] Fix | Delete
$updraftplus->record_uploaded_chunk($percent, '', $local_file_path);
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
// Continue upload
[109] Fix | Delete
$ret = ftp_nb_continue($this->conn_id);
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
fclose($fh);
[113] Fix | Delete
[114] Fix | Delete
if (FTP_FINISHED != $ret) {
[115] Fix | Delete
if (is_a($updraftplus, 'UpdraftPlus')) $updraftplus->log("FTP upload: error ($ret)");
[116] Fix | Delete
return false;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
return true;
[120] Fix | Delete
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
public function get($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) {
[124] Fix | Delete
[125] Fix | Delete
$file_last_size = 0;
[126] Fix | Delete
[127] Fix | Delete
if ($resume) {
[128] Fix | Delete
if (!$fh = fopen($local_file_path, 'ab')) return false;
[129] Fix | Delete
clearstatcache($local_file_path);// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.clearstatcache_clear_realpath_cacheFound -- The function clearstatcache() does not have a parameter "clear_realpath_cache" in PHP version 5.2 or earlier
[130] Fix | Delete
$file_last_size = filesize($local_file_path);
[131] Fix | Delete
} else {
[132] Fix | Delete
if (!$fh = fopen($local_file_path, 'wb')) return false;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
$ret = ftp_nb_fget($this->conn_id, $fh, $remote_file_path, $mode, $file_last_size);
[136] Fix | Delete
[137] Fix | Delete
if (false == $ret) return false;
[138] Fix | Delete
[139] Fix | Delete
while (FTP_MOREDATA == $ret) {
[140] Fix | Delete
[141] Fix | Delete
if ($updraftplus) {
[142] Fix | Delete
$file_now_size = filesize($local_file_path);
[143] Fix | Delete
if ($file_now_size - $file_last_size > 524288) {
[144] Fix | Delete
$updraftplus->log("FTP fetch: file size is now: ".sprintf("%0.2f", filesize($local_file_path)/1048576)." Mb");
[145] Fix | Delete
$file_last_size = $file_now_size;
[146] Fix | Delete
}
[147] Fix | Delete
clearstatcache($local_file_path);// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.clearstatcache_clear_realpath_cacheFound -- The function clearstatcache() does not have a parameter "clear_realpath_cache" in PHP version 5.2 or earlier
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
$ret = ftp_nb_continue($this->conn_id);
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
fclose($fh);
[154] Fix | Delete
[155] Fix | Delete
if (FTP_FINISHED == $ret) {
[156] Fix | Delete
if ($updraftplus) $updraftplus->log("FTP fetch: fetch complete");
[157] Fix | Delete
return true;
[158] Fix | Delete
} else {
[159] Fix | Delete
if ($updraftplus) $updraftplus->log("FTP fetch: fetch failed");
[160] Fix | Delete
return false;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
public function chmod($permissions, $remote_filename) {
[166] Fix | Delete
if ($this->is_octal($permissions)) {
[167] Fix | Delete
$result = ftp_chmod($this->conn_id, $permissions, $remote_filename);
[168] Fix | Delete
if ($result) {
[169] Fix | Delete
return true;
[170] Fix | Delete
} else {
[171] Fix | Delete
return false;
[172] Fix | Delete
}
[173] Fix | Delete
} else {
[174] Fix | Delete
throw new Exception('$permissions must be an octal number');
[175] Fix | Delete
}
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
public function chdir($directory) {
[179] Fix | Delete
ftp_chdir($this->conn_id, $directory);
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
public function delete($remote_file_path) {
[183] Fix | Delete
if (ftp_delete($this->conn_id, $remote_file_path)) {
[184] Fix | Delete
return true;
[185] Fix | Delete
} else {
[186] Fix | Delete
return false;
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
public function make_dir($directory) {
[191] Fix | Delete
if (ftp_mkdir($this->conn_id, $directory)) {
[192] Fix | Delete
return true;
[193] Fix | Delete
} else {
[194] Fix | Delete
return false;
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
public function rename($old_name, $new_name) {
[199] Fix | Delete
if (ftp_rename($this->conn_id, $old_name, $new_name)) {
[200] Fix | Delete
return true;
[201] Fix | Delete
} else {
[202] Fix | Delete
return false;
[203] Fix | Delete
}
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
public function remove_dir($directory) {
[207] Fix | Delete
if (ftp_rmdir($this->conn_id, $directory)) {
[208] Fix | Delete
return true;
[209] Fix | Delete
} else {
[210] Fix | Delete
return false;
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
public function dir_list($directory) {
[215] Fix | Delete
return ftp_nlist($this->conn_id, $directory);
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
public function cdup() {
[219] Fix | Delete
ftp_cdup($this->conn_id);
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
public function size($f) {
[223] Fix | Delete
return ftp_size($this->conn_id, $f);
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
public function current_dir() {
[227] Fix | Delete
return ftp_pwd($this->conn_id);
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
private function is_octal($i) {
[231] Fix | Delete
return decoct(octdec($i)) == $i;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
public function __destruct() {
[235] Fix | Delete
if ($this->conn_id) ftp_close($this->conn_id);
[236] Fix | Delete
}
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function