| 1 |
<?php |
| 2 |
|
| 3 |
class UpdraftPlus_BackupModule_ftp { |
| 4 |
|
| 5 |
// Get FTP object with parameters set |
| 6 |
function getFTP($server, $user, $pass, $disable_ssl = false, $disable_verify = true, $use_server_certs = false, $passive = true) { |
| 7 |
|
| 8 |
if( !class_exists('UpdraftPlus_ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php'); |
| 9 |
|
| 10 |
$port = 21; |
| 11 |
if (preg_match('/^(.*):(\d+)$/', $server, $matches)) { |
| 12 |
$server = $matches[1]; |
| 13 |
$port = $matches[2]; |
| 14 |
} |
| 15 |
|
| 16 |
$ftp = new UpdraftPlus_ftp_wrapper($server, $user, $pass, $port); |
| 17 |
|
| 18 |
if ($disable_ssl) $ftp->ssl = false; |
| 19 |
$ftp->use_server_certs = $use_server_certs; |
| 20 |
$ftp->disable_verify = $disable_verify; |
| 21 |
if ($passive) $ftp->passive = true; |
| 22 |
|
| 23 |
return $ftp; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
function backup($backup_array) { |
| 28 |
|
| 29 |
global $updraftplus, $updraftplus_backup; |
| 30 |
|
| 31 |
$server = UpdraftPlus_Options::get_updraft_option('updraft_server_address'); |
| 32 |
$user = UpdraftPlus_Options::get_updraft_option('updraft_ftp_login'); |
| 33 |
|
| 34 |
$ftp = $this->getFTP( |
| 35 |
$server, |
| 36 |
$user, |
| 37 |
UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass'), UpdraftPlus_Options::get_updraft_option('updraft_ssl_nossl'), |
| 38 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify'), |
| 39 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_useservercerts') |
| 40 |
); |
| 41 |
|
| 42 |
if (!$ftp->connect()) { |
| 43 |
$updraftplus->log("FTP Failure: we did not successfully log in with those credentials."); |
| 44 |
$updraftplus->log(sprintf(__("%s login failure",'updraftplus'), 'FTP'), 'error'); |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 49 |
|
| 50 |
$updraft_dir = $updraftplus->backups_dir_location().'/'; |
| 51 |
|
| 52 |
$ftp_remote_path = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); |
| 53 |
foreach($backup_array as $file) { |
| 54 |
$fullpath = $updraft_dir.$file; |
| 55 |
$updraftplus->log("FTP upload attempt: $file -> ftp://$user@$server/${ftp_remote_path}${file}"); |
| 56 |
$timer_start = microtime(true); |
| 57 |
$size_k = round(filesize($fullpath)/1024,1); |
| 58 |
if ($ftp->put($fullpath, $ftp_remote_path.$file, FTP_BINARY, true, $updraftplus)) { |
| 59 |
$updraftplus->log("FTP upload attempt successful (".$size_k."Kb in ".(round(microtime(true)-$timer_start,2)).'s)'); |
| 60 |
$updraftplus->uploaded_file($file); |
| 61 |
} else { |
| 62 |
$updraftplus->log("ERROR: FTP upload failed" ); |
| 63 |
$updraftplus->log(sprintf(__("%s upload failed",'updraftplus'), 'FTP'), 'error'); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return array('ftp_object' => $ftp, 'ftp_remote_path' => $ftp_remote_path); |
| 68 |
} |
| 69 |
|
| 70 |
function delete($files, $ftparr = array()) { |
| 71 |
|
| 72 |
global $updraftplus; |
| 73 |
if (is_string($files)) $files=array($files); |
| 74 |
|
| 75 |
if (isset($ftparr['ftp_object'])) { |
| 76 |
$ftp = $ftparr['ftp_object']; |
| 77 |
} else { |
| 78 |
|
| 79 |
$server = UpdraftPlus_Options::get_updraft_option('updraft_server_address'); |
| 80 |
$user = UpdraftPlus_Options::get_updraft_option('updraft_ftp_login'); |
| 81 |
|
| 82 |
$ftp = $this->getFTP( |
| 83 |
$server, |
| 84 |
$user, |
| 85 |
UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass'), UpdraftPlus_Options::get_updraft_option('updraft_ssl_nossl'), |
| 86 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify'), |
| 87 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_useservercerts') |
| 88 |
); |
| 89 |
|
| 90 |
if (!$ftp->connect()) { |
| 91 |
$updraftplus->log("FTP Failure: we did not successfully log in with those credentials."); |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
$ftp_remote_path = isset($ftparr['ftp_remote_path']) ? $ftparr['ftp_remote_path'] : trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); |
| 98 |
|
| 99 |
$ret = true; |
| 100 |
foreach ($files as $file) { |
| 101 |
if (@$ftp->delete($ftp_remote_path.$file)) { |
| 102 |
$updraftplus->log("FTP delete: succeeded (${ftp_remote_path}${file})"); |
| 103 |
} else { |
| 104 |
$updraftplus->log("FTP delete: failed (${ftp_remote_path}${file})"); |
| 105 |
$ret = false; |
| 106 |
} |
| 107 |
} |
| 108 |
return $ret; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
function download($file) { |
| 113 |
if( !class_exists('UpdraftPlus_ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php'); |
| 114 |
|
| 115 |
global $updraftplus; |
| 116 |
|
| 117 |
$ftp = $this->getFTP( |
| 118 |
UpdraftPlus_Options::get_updraft_option('updraft_server_address'), |
| 119 |
UpdraftPlus_Options::get_updraft_option('updraft_ftp_login'), |
| 120 |
UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass'), UpdraftPlus_Options::get_updraft_option('updraft_ssl_nossl'), |
| 121 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify'), |
| 122 |
UpdraftPlus_Options::get_updraft_option('updraft_ssl_useservercerts') |
| 123 |
); |
| 124 |
|
| 125 |
if (!$ftp->connect()) { |
| 126 |
$updraftplus->log("FTP Failure: we did not successfully log in with those credentials."); |
| 127 |
$updraftplus->log(sprintf(__("%s login failure",'updraftplus'), 'FTP'), 'error'); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 132 |
|
| 133 |
$ftp_remote_path = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); |
| 134 |
$fullpath = $updraftplus->backups_dir_location().'/'.$file; |
| 135 |
|
| 136 |
$resume = false; |
| 137 |
if (file_exists($fullpath)) { |
| 138 |
$resume = true; |
| 139 |
$updraftplus->log("File already exists locally; will resume: size: ".filesize($fullpath)); |
| 140 |
} |
| 141 |
|
| 142 |
$ftp->get($fullpath, $ftp_remote_path.$file, FTP_BINARY, $resume, $updraftplus); |
| 143 |
} |
| 144 |
|
| 145 |
public static function config_print_javascript_onready() { |
| 146 |
?> |
| 147 |
jQuery('#updraft-ftp-test').click(function(){ |
| 148 |
jQuery('#updraft-ftp-test').html('<?php echo esc_js(sprintf(__('Testing %s Settings...', 'updraftplus'),'FTP')); ?>'); |
| 149 |
var data = { |
| 150 |
action: 'updraft_ajax', |
| 151 |
subaction: 'credentials_test', |
| 152 |
method: 'ftp', |
| 153 |
nonce: '<?php echo wp_create_nonce('updraftplus-credentialtest-nonce'); ?>', |
| 154 |
server: jQuery('#updraft_server_address').val(), |
| 155 |
login: jQuery('#updraft_ftp_login').val(), |
| 156 |
pass: jQuery('#updraft_ftp_pass').val(), |
| 157 |
path: jQuery('#updraft_ftp_remote_path').val(), |
| 158 |
disableverify: (jQuery('#updraft_ssl_disableverify').is(':checked')) ? 1 : 0, |
| 159 |
useservercerts: (jQuery('#updraft_ssl_useservercerts').is(':checked')) ? 1 : 0, |
| 160 |
nossl: (jQuery('#updraft_ssl_nossl').is(':checked')) ? 1 : 0, |
| 161 |
}; |
| 162 |
jQuery.post(ajaxurl, data, function(response) { |
| 163 |
jQuery('#updraft-ftp-test').html('<?php echo esc_js(sprintf(__('Test %s Settings', 'updraftplus'),'FTP')); ?>'); |
| 164 |
alert('<?php echo esc_js(sprintf(__('%s settings test result:', 'updraftplus'), 'FTP'));?> ' + response); |
| 165 |
|
| 166 |
}); |
| 167 |
}); |
| 168 |
<?php |
| 169 |
} |
| 170 |
|
| 171 |
public static function config_print() { |
| 172 |
?> |
| 173 |
|
| 174 |
<tr class="updraftplusmethod ftp"> |
| 175 |
<td></td> |
| 176 |
<td><p><em><?php printf(__('%s is a great choice, because UpdraftPlus supports chunked uploads - no matter how big your site is, UpdraftPlus can upload it a little at a time, and not get thwarted by timeouts.','updraftplus'),'FTP');?></em></p></td> |
| 177 |
</tr> |
| 178 |
|
| 179 |
<tr class="updraftplusmethod ftp"> |
| 180 |
<th></th> |
| 181 |
<td><em><?php echo apply_filters('updraft_sftp_ftps_notice', '<strong>'.htmlspecialchars(__('Only non-encrypted FTP is supported by regular UpdraftPlus.')).'</strong> <a href="http://updraftplus.com/shop/sftp/">'.__('If you want encryption (e.g. you are storing sensitive business data), then an add-on is available.','updraftplus')).'</a>'; ?></em></td> |
| 182 |
</tr> |
| 183 |
|
| 184 |
<tr class="updraftplusmethod ftp"> |
| 185 |
<th><?php _e('FTP Server','updraftplus');?>:</th> |
| 186 |
<td><input type="text" size="40" id="updraft_server_address" name="updraft_server_address" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_server_address')); ?>" /></td> |
| 187 |
</tr> |
| 188 |
<tr class="updraftplusmethod ftp"> |
| 189 |
<th><?php _e('FTP Login','updraftplus');?>:</th> |
| 190 |
<td><input type="text" size="40" id="updraft_ftp_login" name="updraft_ftp_login" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_login')) ?>" /></td> |
| 191 |
</tr> |
| 192 |
<tr class="updraftplusmethod ftp"> |
| 193 |
<th><?php _e('FTP Password','updraftplus');?>:</th> |
| 194 |
<td><input type="<?php echo apply_filters('updraftplus_admin_secret_field_type', 'text'); ?>" size="40" id="updraft_ftp_pass" name="updraft_ftp_pass" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass')); ?>" /></td> |
| 195 |
</tr> |
| 196 |
<tr class="updraftplusmethod ftp"> |
| 197 |
<th><?php _e('Remote Path','updraftplus');?>:</th> |
| 198 |
<td><input type="text" size="64" id="updraft_ftp_remote_path" name="updraft_ftp_remote_path" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); ?>" /> <em><?php _e('Needs to already exist','updraftplus');?></em></td> |
| 199 |
</tr> |
| 200 |
<tr class="updraftplusmethod ftp"> |
| 201 |
<th></th> |
| 202 |
<td><p><button id="updraft-ftp-test" type="button" class="button-primary" style="font-size:18px !important"><?php echo sprintf(__('Test %s Settings','updraftplus'),'FTP');?></button></p></td> |
| 203 |
</tr> |
| 204 |
<?php |
| 205 |
} |
| 206 |
|
| 207 |
public static function credentials_test() { |
| 208 |
|
| 209 |
$server = $_POST['server']; |
| 210 |
$login = stripslashes($_POST['login']); |
| 211 |
$pass = stripslashes($_POST['pass']); |
| 212 |
$path = $_POST['path']; |
| 213 |
$nossl = $_POST['nossl']; |
| 214 |
|
| 215 |
$disable_verify = $_POST['disableverify']; |
| 216 |
$use_server_certs = $_POST['useservercerts']; |
| 217 |
|
| 218 |
if (empty($server)) { |
| 219 |
_e("Failure: No server details were given.",'updraftplus'); |
| 220 |
return; |
| 221 |
} |
| 222 |
if (empty($login)) { |
| 223 |
printf(__("Failure: No %s was given.",'updraftplus'),'login'); |
| 224 |
return; |
| 225 |
} |
| 226 |
if (empty($pass)) { |
| 227 |
printf(__("Failure: No %s was given.",'updraftplus'),'password'); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$ftp = self::getFTP($server, $login, $pass, $nossl, $disable_verify, $use_server_certs); |
| 232 |
|
| 233 |
if (!$ftp->connect()) { |
| 234 |
_e('Failure: we did not successfully log in with those credentials.', 'updraftplus'); |
| 235 |
return; |
| 236 |
} |
| 237 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 238 |
|
| 239 |
$file = md5(rand(0,99999999)).'.tmp'; |
| 240 |
$fullpath = trailingslashit($path).$file; |
| 241 |
if (!file_exists(ABSPATH.'wp-includes/version.php')) { |
| 242 |
_e("Failure: an unexpected internal UpdraftPlus error occurred when testing the credentials - please contact the developer"); |
| 243 |
return; |
| 244 |
} |
| 245 |
if ($ftp->put(ABSPATH.'wp-includes/version.php', $fullpath, FTP_BINARY, false, true)) { |
| 246 |
echo __("Success: we successfully logged in, and confirmed our ability to create a file in the given directory (login type:",'updraftplus')." ".$ftp->login_type.')'; |
| 247 |
@$ftp->delete($fullpath); |
| 248 |
} else { |
| 249 |
_e('Failure: we successfully logged in, but were not able to create a file in the given directory.'); |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
?> |