PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.8.13
UpdraftPlus: WP Backup & Migration Plugin v1.8.13
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / methods / ftp.php

ftp.php in UpdraftPlus: WP Backup & Migration Plugin 1.8.13, at methods/ftp.php

303 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class UpdraftPlus_BackupModule_ftp {
4
5 // Get FTP object with parameters set
6 private 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 public function backup($backup_array) {
28
29 global $updraftplus, $updraftplus_backup;
30
31 $server = $updraftplus->get_job_option('updraft_server_address');
32 $user = $updraftplus->get_job_option('updraft_ftp_login');
33
34 $ftp = $this->getFTP(
35 $server,
36 $user,
37 $updraftplus->get_job_option('updraft_ftp_pass'), $updraftplus->get_job_option('updraft_ssl_nossl'),
38 $updraftplus->get_job_option('updraft_ssl_disableverify'),
39 $updraftplus->get_job_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->get_job_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 # Note :Setting $resume to true unnecessarily is not meant to be a problem. Only ever (Feb 2014) seen one weird FTP server where calling SIZE on a non-existent file did create a problem. So, this code just helps that case. (the check for non-empty upload_status[p] is being cautious.
59 $upload_status = $updraftplus->jobdata_get('uploading_substatus');
60 if (0 == $updraftplus->current_resumption|| (is_array($upload_status) && !empty($upload_status['p']) && $upload_status['p'] == 0)) {
61 $resume = false;
62 } else {
63 $resume = true;
64 }
65
66 if ($ftp->put($fullpath, $ftp_remote_path.$file, FTP_BINARY, $resume, $updraftplus)) {
67 $updraftplus->log("FTP upload attempt successful (".$size_k."Kb in ".(round(microtime(true)-$timer_start,2)).'s)');
68 $updraftplus->uploaded_file($file);
69 } else {
70 $updraftplus->log("ERROR: FTP upload failed" );
71 $updraftplus->log(sprintf(__("%s upload failed",'updraftplus'), 'FTP'), 'error');
72 }
73 }
74
75 return array('ftp_object' => $ftp, 'ftp_remote_path' => $ftp_remote_path);
76 }
77
78 public function delete($files, $ftparr = array()) {
79
80 global $updraftplus;
81 if (is_string($files)) $files=array($files);
82
83 if (isset($ftparr['ftp_object'])) {
84 $ftp = $ftparr['ftp_object'];
85 } else {
86
87 $server = $updraftplus->get_job_option('updraft_server_address');
88 $user = $updraftplus->get_job_option('updraft_ftp_login');
89
90 $ftp = $this->getFTP(
91 $server,
92 $user,
93 $updraftplus->get_job_option('updraft_ftp_pass'), $updraftplus->get_job_option('updraft_ssl_nossl'),
94 $updraftplus->get_job_option('updraft_ssl_disableverify'),
95 $updraftplus->get_job_option('updraft_ssl_useservercerts')
96 );
97
98 if (!$ftp->connect()) {
99 $updraftplus->log("FTP Failure: we did not successfully log in with those credentials.");
100 return false;
101 }
102
103 }
104
105 $ftp_remote_path = isset($ftparr['ftp_remote_path']) ? $ftparr['ftp_remote_path'] : trailingslashit($updraftplus->get_job_option('updraft_ftp_remote_path'));
106
107 $ret = true;
108 foreach ($files as $file) {
109 if (@$ftp->delete($ftp_remote_path.$file)) {
110 $updraftplus->log("FTP delete: succeeded (${ftp_remote_path}${file})");
111 } else {
112 $updraftplus->log("FTP delete: failed (${ftp_remote_path}${file})");
113 $ret = false;
114 }
115 }
116 return $ret;
117
118 }
119
120 public function download($file) {
121 if( !class_exists('UpdraftPlus_ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php');
122
123 global $updraftplus;
124
125 $ftp = $this->getFTP(
126 $updraftplus->get_job_option('updraft_server_address'),
127 $updraftplus->get_job_option('updraft_ftp_login'),
128 $updraftplus->get_job_option('updraft_ftp_pass'), $updraftplus->get_job_option('updraft_ssl_nossl'),
129 $updraftplus->get_job_option('updraft_ssl_disableverify'),
130 $updraftplus->get_job_option('updraft_ssl_useservercerts')
131 );
132
133 if (!$ftp->connect()) {
134 $updraftplus->log("FTP Failure: we did not successfully log in with those credentials.");
135 $updraftplus->log(sprintf(__("%s login failure",'updraftplus'), 'FTP'), 'error');
136 return false;
137 }
138
139 //$ftp->make_dir(); we may need to recursively create dirs? TODO
140
141 $ftp_remote_path = trailingslashit($updraftplus->get_job_option('updraft_ftp_remote_path'));
142 $fullpath = $updraftplus->backups_dir_location().'/'.$file;
143
144 $resume = false;
145 if (file_exists($fullpath)) {
146 $resume = true;
147 $updraftplus->log("File already exists locally; will resume: size: ".filesize($fullpath));
148 }
149
150 return $ftp->get($fullpath, $ftp_remote_path.$file, FTP_BINARY, $resume, $updraftplus);
151 }
152
153 public function config_print_javascript_onready() {
154 ?>
155 jQuery('#updraft-ftp-test').click(function(){
156 jQuery('#updraft-ftp-test').html('<?php echo esc_js(sprintf(__('Testing %s Settings...', 'updraftplus'),'FTP')); ?>');
157 var data = {
158 action: 'updraft_ajax',
159 subaction: 'credentials_test',
160 method: 'ftp',
161 nonce: '<?php echo wp_create_nonce('updraftplus-credentialtest-nonce'); ?>',
162 server: jQuery('#updraft_server_address').val(),
163 login: jQuery('#updraft_ftp_login').val(),
164 pass: jQuery('#updraft_ftp_pass').val(),
165 path: jQuery('#updraft_ftp_remote_path').val(),
166 disableverify: (jQuery('#updraft_ssl_disableverify').is(':checked')) ? 1 : 0,
167 useservercerts: (jQuery('#updraft_ssl_useservercerts').is(':checked')) ? 1 : 0,
168 nossl: (jQuery('#updraft_ssl_nossl').is(':checked')) ? 1 : 0,
169 };
170 jQuery.post(ajaxurl, data, function(response) {
171 jQuery('#updraft-ftp-test').html('<?php echo esc_js(sprintf(__('Test %s Settings', 'updraftplus'),'FTP')); ?>');
172 alert('<?php echo esc_js(sprintf(__('%s settings test result:', 'updraftplus'), 'FTP'));?> ' + response);
173
174 });
175 });
176 <?php
177 }
178
179 private function ftp_possible() {
180 $funcs_disabled = array();
181 foreach (array('ftp_connect', 'ftp_login', 'ftp_nb_fput') as $func) {
182 if (!function_exists($func)) $funcs_disabled['ftp'][] = $func;
183 }
184 $funcs_disabled = apply_filters('updraftplus_ftp_possible', $funcs_disabled);
185 return (0 == count($funcs_disabled)) ? true : $funcs_disabled;
186 }
187
188 public function config_print() {
189 global $updraftplus;
190
191 $possible = $this->ftp_possible();
192 if (is_array($possible)) {
193 ?>
194 <tr class="updraftplusmethod ftp">
195 <th></th>
196 <td>
197 <?php
198 // Check requirements.
199 global $updraftplus_admin;
200 $trans = array(
201 'ftp' => __('regular non-encrypted FTP', 'updraftplus'),
202 'ftpsslimplicit' => __('encrypted FTP (implicit encryption)', 'updraftplus'),
203 'ftpsslexplicit' => __('encrypted FTP (explicit encryption)', 'updraftplus')
204 );
205 foreach ($possible as $type => $missing) {
206 $updraftplus_admin->show_double_warning('<strong>'.__('Warning','updraftplus').':</strong> '. sprintf(__("Your web server's PHP installation has these functions disabled: %s.", 'updraftplus'), implode(', ', $missing)).' '.sprintf(__('Your hosting company must enable these functions before %s can work.', 'updraftplus'), $trans[$type]), 'ftp');
207 }
208 ?>
209 </td>
210 </tr>
211 <?php
212 }
213
214 ?>
215
216 <tr class="updraftplusmethod ftp">
217 <td></td>
218 <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>
219 </tr>
220
221 <tr class="updraftplusmethod ftp">
222 <th></th>
223 <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>
224 </tr>
225
226 <tr class="updraftplusmethod ftp">
227 <th><?php _e('FTP Server','updraftplus');?>:</th>
228 <td><input type="text" size="40" id="updraft_server_address" name="updraft_server_address" value="<?php echo htmlspecialchars($updraftplus->get_job_option('updraft_server_address')); ?>" /></td>
229 </tr>
230 <tr class="updraftplusmethod ftp">
231 <th><?php _e('FTP Login','updraftplus');?>:</th>
232 <td><input type="text" size="40" id="updraft_ftp_login" name="updraft_ftp_login" value="<?php echo htmlspecialchars($updraftplus->get_job_option('updraft_ftp_login')) ?>" /></td>
233 </tr>
234 <tr class="updraftplusmethod ftp">
235 <th><?php _e('FTP Password','updraftplus');?>:</th>
236 <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->get_job_option('updraft_ftp_pass')); ?>" /></td>
237 </tr>
238 <tr class="updraftplusmethod ftp">
239 <th><?php _e('Remote Path','updraftplus');?>:</th>
240 <td><input type="text" size="64" id="updraft_ftp_remote_path" name="updraft_ftp_remote_path" value="<?php echo htmlspecialchars($updraftplus->get_job_option('updraft_ftp_remote_path')); ?>" /> <em><?php _e('Needs to already exist','updraftplus');?></em></td>
241 </tr>
242 <tr class="updraftplusmethod ftp">
243 <th></th>
244 <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>
245 </tr>
246 <?php
247 }
248
249 public function get_credentials() {
250 return array('updraft_server_address', 'updraft_ftp_login', 'updraft_ftp_pass', 'updraft_ftp_remote_path', 'updraft_ssl_disableverify', 'updraft_ssl_nossl', 'updraft_ssl_useservercerts');
251 }
252
253 public function credentials_test() {
254
255 $server = $_POST['server'];
256 $login = stripslashes($_POST['login']);
257 $pass = stripslashes($_POST['pass']);
258 $path = $_POST['path'];
259 $nossl = $_POST['nossl'];
260
261 $disable_verify = $_POST['disableverify'];
262 $use_server_certs = $_POST['useservercerts'];
263
264 if (empty($server)) {
265 _e("Failure: No server details were given.",'updraftplus');
266 return;
267 }
268 if (empty($login)) {
269 printf(__("Failure: No %s was given.",'updraftplus'),'login');
270 return;
271 }
272 if (empty($pass)) {
273 printf(__("Failure: No %s was given.",'updraftplus'),'password');
274 return;
275 }
276
277 $ftp = $this->getFTP($server, $login, $pass, $nossl, $disable_verify, $use_server_certs);
278
279 if (!$ftp->connect()) {
280 _e('Failure: we did not successfully log in with those credentials.', 'updraftplus');
281 return;
282 }
283 //$ftp->make_dir(); we may need to recursively create dirs? TODO
284
285 $file = md5(rand(0,99999999)).'.tmp';
286 $fullpath = trailingslashit($path).$file;
287 if (!file_exists(ABSPATH.'wp-includes/version.php')) {
288 _e("Failure: an unexpected internal UpdraftPlus error occurred when testing the credentials - please contact the developer");
289 return;
290 }
291 if ($ftp->put(ABSPATH.'wp-includes/version.php', $fullpath, FTP_BINARY, false, true)) {
292 echo __("Success: we successfully logged in, and confirmed our ability to create a file in the given directory (login type:",'updraftplus')." ".$ftp->login_type.')';
293 @$ftp->delete($fullpath);
294 } else {
295 _e('Failure: we successfully logged in, but were not able to create a file in the given directory.');
296 }
297
298 }
299
300 }
301
302 ?>
303