PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.16.5
UpdraftPlus: WP Backup & Migration Plugin v1.16.5
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 / includes / class-commands.php

class-commands.php in UpdraftPlus: WP Backup & Migration Plugin 1.16.5, at includes/class-commands.php

983 lines 36.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UPDRAFTPLUS_DIR')) die('No access.');
4
5 /*
6 - A container for all the remote commands implemented. Commands map exactly onto method names (and hence this class should not implement anything else, beyond the constructor, and private methods)
7 - Return format is either to return data (boolean, string, array), or an WP_Error object
8 Commands are not allowed to begin with an underscore. So, any private methods can be prefixed with an underscore.
9 TODO: Many of these just verify input, and then call back into a relevant method in UpdraftPlus_Admin. Once all commands have been ported over to go via this class, those methods in UpdraftPlus_Admin can generally be folded into the relevant method in here, and removed from UpdraftPlus_Admin. (Since this class is intended to become the official way of performing actions). As a bonus, we then won't need so much _load_ud(_admin) boilerplate.
10 */
11
12 if (class_exists('UpdraftPlus_Commands')) return;
13
14 class UpdraftPlus_Commands {
15
16 private $_uc_helper;
17
18 /**
19 * Constructor
20 *
21 * @param Class $uc_helper The 'helper' needs to provide the method _updraftplus_background_operation_started
22 */
23 public function __construct($uc_helper) {
24 $this->_uc_helper = $uc_helper;
25 }
26
27 /**
28 * Get the Advanced Tools HTMl and return to Central
29 *
30 * @param string $options Options for advanced settings
31 * @return string
32 */
33 public function get_advanced_settings($options) {
34 // load global updraftplus and admin
35 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
36 if (false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
37
38 $html = $updraftplus_admin->settings_advanced_tools(true, array('options' => $options));
39
40 return $html;
41 }
42
43 public function get_download_status($items) {
44 // load global updraftplus and admin
45 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
46
47 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
48
49 if (!is_array($items)) $items = array();
50
51 return $updraftplus_admin->get_download_statuses($items);
52
53 }
54
55 /**
56 * Begin a download process
57 *
58 * @param Array $downloader_params - download parameters (findex, type, timestamp, stage)
59 *
60 * @return Array - as from UpdraftPlus_Admin::do_updraft_download_backup() (with 'request' key added, with value $downloader_params)
61 */
62 public function downloader($downloader_params) {
63
64 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
65
66 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
67
68 $findex = $downloader_params['findex'];
69 $type = $downloader_params['type'];
70 $timestamp = $downloader_params['timestamp'];
71 // Valid stages: 2='spool the data'|'delete'='delete local copy'|anything else='make sure it is present'
72 $stage = empty($downloader_params['stage']) ? false : $downloader_params['stage'];
73
74 // This may, or may not, return, depending upon whether the files are already downloaded
75 // The response is usually an array with key 'result', and values deleted|downloaded|needs_download|download_failed
76 $response = $updraftplus_admin->do_updraft_download_backup($findex, $type, $timestamp, $stage, array($this->_uc_helper, '_updraftplus_background_operation_started'));
77
78 if (is_array($response)) {
79 $response['request'] = $downloader_params;
80 }
81
82 return $response;
83 }
84
85 public function delete_downloaded($set_info) {
86 $set_info['stage'] = 'delete';
87 return $this->downloader($set_info);
88 }
89
90 /**
91 * Get backup progress (as HTML) for a particular backup
92 *
93 * @param Array $params - should have a key 'job_id' with corresponding value
94 *
95 * @return String - the HTML
96 */
97 public function backup_progress($params) {
98
99 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
100
101 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
102
103 $request = array(
104 'thisjobonly' => $params['job_id']
105 );
106 $activejobs_list = $updraftplus_admin->get_activejobs_list($request);
107
108 return $activejobs_list;
109
110 }
111
112 public function backupnow($params) {
113
114 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
115
116 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
117
118 if (!empty($params['updraftplus_clone_backup'])) {
119 add_filter('updraft_backupnow_options', array($updraftplus, 'updraftplus_clone_backup_options'), 10, 2);
120 add_filter('updraftplus_initial_jobdata', array($updraftplus, 'updraftplus_clone_backup_jobdata'), 10, 3);
121 }
122
123 $background_operation_started_method_name = empty($params['background_operation_started_method_name']) ? '_updraftplus_background_operation_started' : $params['background_operation_started_method_name'];
124 $updraftplus_admin->request_backupnow($params, array($this->_uc_helper, $background_operation_started_method_name));
125
126 // Control returns when the backup finished; but, the browser connection should have been closed before
127 die;
128 }
129
130 /**
131 * Mark a backup as "do not delete"
132 *
133 * @param array $params this is an array of parameters sent via ajax it can include the following:
134 * backup_key - Integer - backup timestamp
135 * always_keep - Boolean - "Always keep" value
136 * @return array which contains rawbackup html
137 */
138 public function always_keep_this_backup($params) {
139 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
140 $backup_key = $params['backup_key'];
141 $backup_history = UpdraftPlus_Backup_History::get_history();
142 if (empty($params['always_keep'])) {
143 unset($backup_history[$backup_key]['always_keep']);
144 } else {
145 $backup_history[$backup_key]['always_keep'] = true;
146 }
147 UpdraftPlus_Backup_History::save_history($backup_history);
148 $nonce = $backup_history[$backup_key]['nonce'];
149 $rawbackup = $updraftplus_admin->raw_backup_info($backup_history, $backup_key, $nonce);
150 return array(
151 'rawbackup' => html_entity_decode($rawbackup),
152 );
153 }
154
155 private function _load_ud() {
156 global $updraftplus;
157 return is_a($updraftplus, 'UpdraftPlus') ? $updraftplus : false;
158 }
159
160 private function _load_ud_admin() {
161 if (!defined('UPDRAFTPLUS_DIR') || !is_file(UPDRAFTPLUS_DIR.'/admin.php')) return false;
162 include_once(UPDRAFTPLUS_DIR.'/admin.php');
163 global $updraftplus_admin;
164 return $updraftplus_admin;
165 }
166
167 public function get_log($job_id = '') {
168
169 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
170
171 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
172
173 if ('' != $job_id && !preg_match("/^[0-9a-f]{12}$/", $job_id)) return new WP_Error('updraftplus_permission_invalid_jobid');
174
175 return $updraftplus_admin->fetch_log($job_id);
176
177 }
178
179 public function activejobs_delete($job_id) {
180
181 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
182
183 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
184
185 return $updraftplus_admin->activejobs_delete((string) $job_id);
186
187 }
188
189 public function deleteset($what) {
190
191 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
192
193 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
194
195 $results = $updraftplus_admin->delete_set($what);
196
197 $get_history_opts = isset($what['get_history_opts']) ? $what['get_history_opts'] : array();
198
199 $backup_history = UpdraftPlus_Backup_History::get_history();
200
201 $results['history'] = $updraftplus_admin->settings_downloading_and_restoring($backup_history, true, $get_history_opts);
202
203 $results['backupnow_file_entities'] = apply_filters('updraftplus_backupnow_file_entities', array());
204 $results['modal_afterfileoptions'] = apply_filters('updraft_backupnow_modal_afterfileoptions', '', '');
205
206 $results['count_backups'] = count($backup_history);
207
208 return $results;
209
210 }
211
212 /**
213 * Slightly misnamed - this doesn't always rescan, but it does always return the history status (possibly after a rescan)
214 *
215 * @param Array|String $data - with keys 'operation' and 'debug'; or, if a string (backwards compatibility), just the value of the 'operation' key (with debug assumed as 0)
216 *
217 * @return Array - returns an array of history statuses
218 */
219 public function rescan($data) {
220
221 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
222
223 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
224
225 if (is_array($data)) {
226 $operation = empty($data['operation']) ? '' : $data['operation'];
227 $debug = !empty($data['debug']);
228 } else {
229 $operation = $data;
230 $debug = false;
231 }
232
233 $remotescan = ('remotescan' == $operation);
234 $rescan = ($remotescan || 'rescan' == $operation);
235
236 $history_status = $updraftplus_admin->get_history_status($rescan, $remotescan, $debug);
237 $history_status['backupnow_file_entities'] = apply_filters('updraftplus_backupnow_file_entities', array());
238 $history_status['modal_afterfileoptions'] = apply_filters('updraft_backupnow_modal_afterfileoptions', '', '');
239
240 return $history_status;
241
242 }
243
244 public function get_settings($options) {
245 global $updraftplus;
246 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
247
248 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
249
250 ob_start();
251 $updraftplus_admin->settings_formcontents($options);
252 $output = ob_get_contents();
253 ob_end_clean();
254
255 $remote_storage_options_and_templates = UpdraftPlus_Storage_Methods_Interface::get_remote_storage_options_and_templates();
256
257 return array(
258 'settings' => $output,
259 'remote_storage_options' => $remote_storage_options_and_templates['options'],
260 'remote_storage_templates' => $remote_storage_options_and_templates['templates'],
261 'meta' => apply_filters('updraftplus_get_settings_meta', array()),
262 'updraftplus_version' => $updraftplus->version,
263 );
264
265 }
266
267 /**
268 * Run a credentials test
269 *
270 * @param Array $test_data - test configuration
271 *
272 * @return WP_Error|Array - test results (keys: results, (optional)data), or an error
273 */
274 public function test_storage_settings($test_data) {
275
276 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
277
278 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
279
280 $results = $updraftplus_admin->do_credentials_test($test_data, true);
281
282 return $results;
283
284 }
285
286 /**
287 * Perform a connection test on a database
288 *
289 * @param Array $info - test parameters
290 *
291 * @return Array - test results
292 */
293 public function extradb_testconnection($info) {
294
295 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
296
297 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
298
299 $results = apply_filters('updraft_extradb_testconnection_go', array(), $info);
300
301 return $results;
302
303 }
304
305 /**
306 * This method will make a call to the methods responsible for recounting the quota in the UpdraftVault account
307 *
308 * @param array $params - an array of parameters such as a instance_id
309 * @return string - the result of the call
310 */
311 public function vault_recountquota($params = array()) {
312 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
313
314 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
315
316 $instance_id = empty($params['instance_id']) ? '' : $params['instance_id'];
317
318 $vault = $updraftplus_admin->get_updraftvault($instance_id);
319
320 return $vault->ajax_vault_recountquota(false);
321 }
322
323 /**
324 * This method will make a call to the methods responsible for creating a connection to UpdraftVault
325 *
326 * @param array $credentials - an array of parameters such as the user credentials and instance_id
327 * @return string - the result of the call
328 */
329 public function vault_connect($credentials) {
330
331 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
332
333 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
334
335 $instance_id = empty($credentials['instance_id']) ? '' : $credentials['instance_id'];
336
337 return $updraftplus_admin->get_updraftvault($instance_id)->ajax_vault_connect(false, $credentials);
338
339 }
340
341 /**
342 * This method will make a call to the methods responsible for removing a connection to UpdraftVault
343 *
344 * @param array $params - an array of parameters such as a instance_id
345 * @return string - the result of the call
346 */
347 public function vault_disconnect($params = array()) {
348
349 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
350
351 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
352
353 $echo_results = empty($params['immediate_echo']) ? false : true;
354
355 $instance_id = empty($params['instance_id']) ? '' : $params['instance_id'];
356
357 $results = (array) $updraftplus_admin->get_updraftvault($instance_id)->ajax_vault_disconnect($echo_results);
358
359 return $results;
360
361 }
362
363 /**
364 * A handler method to call the UpdraftPlus admin save settings method. It will check if the settings passed to it are in the format of a string if so it converts it to an array otherwise just pass the array
365 *
366 * @param String/Array $settings Settings to be saved to UpdraftPlus either in the form of a string ready to be converted to an array or already an array ready to be passed to the save settings function in UpdraftPlus.
367 * @return Array An Array response to be sent back
368 */
369 public function save_settings($settings) {
370
371 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
372
373 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
374
375 if (!empty($settings)) {
376
377 if (is_string($settings)) {
378 parse_str($settings, $settings_as_array);
379 } elseif (is_array($settings)) {
380 $settings_as_array = $settings;
381 } else {
382 return new WP_Error('invalid_settings');
383 }
384 }
385
386 $results = $updraftplus_admin->save_settings($settings_as_array);
387
388 return $results;
389
390 }
391
392 public function s3_newuser($data) {
393 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
394
395 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
396 $results = apply_filters('updraft_s3_newuser_go', array(), $data);
397
398 return $results;
399 }
400
401 public function cloudfiles_newuser($data) {
402
403 global $updraftplus_addon_cloudfilesenhanced;
404 if (!is_a($updraftplus_addon_cloudfilesenhanced, 'UpdraftPlus_Addon_CloudFilesEnhanced')) {
405 $data = array('e' => 1, 'm' => sprintf(__('%s add-on not found', 'updraftplus'), 'Rackspace Cloud Files'));
406 } else {
407 $data = $updraftplus_addon_cloudfilesenhanced->create_api_user($data);
408 }
409
410 if (0 === $data['e']) {
411 return $data;
412 } else {
413 return new WP_Error('error', '', $data);
414 }
415 }
416
417 /**
418 * Get an HTML fragment
419 *
420 * @param String|Array $fragment - what fragment to fetch. If an array, the fragment identifier is in 'fragment' (and 'data' is associated data)
421 *
422 * @return Array|WP_Error
423 */
424 public function get_fragment($fragment) {
425
426 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
427
428 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
429
430 if (is_array($fragment)) {
431 $data = $fragment['data'];
432 $fragment = $fragment['fragment'];
433 }
434
435 $error = false;
436
437 switch ($fragment) {
438
439 case 'last_backup_html':
440 $output = $updraftplus_admin->last_backup_html();
441 break;
442
443 case 's3_new_api_user_form':
444 ob_start();
445 do_action('updraft_s3_print_new_api_user_form', false);
446 $output = ob_get_contents();
447 ob_end_clean();
448 break;
449
450 case 'cloudfiles_new_api_user_form':
451 global $updraftplus_addon_cloudfilesenhanced;
452 if (!is_a($updraftplus_addon_cloudfilesenhanced, 'UpdraftPlus_Addon_CloudFilesEnhanced')) {
453 $error = true;
454 $output = 'cloudfiles_addon_not_found';
455 } else {
456 $output = array(
457 'accounts' => $updraftplus_addon_cloudfilesenhanced->account_options(),
458 'regions' => $updraftplus_addon_cloudfilesenhanced->region_options(),
459 );
460 }
461 break;
462
463 case 'backupnow_modal_contents':
464 $updraft_dir = $updraftplus->backups_dir_location();
465 if (!UpdraftPlus_Filesystem_Functions::really_is_writable($updraft_dir)) {
466 $output = array('error' => true, 'html' => __("The 'Backup Now' button is disabled as your backup directory is not writable (go to the 'Settings' tab and find the relevant option).", 'updraftplus'));
467 } else {
468 $output = array('html' => $updraftplus_admin->backupnow_modal_contents());
469 }
470 break;
471
472 case 'panel_download_and_restore':
473 $backup_history = UpdraftPlus_Backup_History::get_history();
474 $output = $updraftplus_admin->settings_downloading_and_restoring($backup_history, true, $data);
475 break;
476
477 case 'disk_usage':
478 $output = UpdraftPlus_Filesystem_Functions::get_disk_space_used($data);
479 break;
480 default:
481 // We just return a code - translation is done on the other side
482 $output = 'ud_get_fragment_could_not_return';
483 $error = true;
484 break;
485 }
486
487 if (!$error) {
488 return array(
489 'output' => $output,
490 );
491 } else {
492 return new WP_Error('get_fragment_error', '', $output);
493 }
494
495 }
496
497 /**
498 * This gets the http_get function from admin to grab information on a url
499 *
500 * @param string $uri URL to be used
501 * @return array returns response from specific URL
502 */
503 public function http_get($uri) {
504 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
505
506 if (empty($uri)) {
507 return new WP_Error('error', '', 'no_uri');
508 }
509
510 $response = $updraftplus_admin->http_get($uri, false);
511 $response_decode = json_decode($response);
512
513 if (isset($response_decode->e)) {
514 return new WP_Error('error', '', htmlspecialchars($response_decode->e));
515 }
516
517 return array('status' => $response_decode->code, 'response' => $response_decode->html_response);
518 }
519
520 /**
521 * This gets the http_get function from admin to grab cURL information on a url
522 *
523 * @param string $uri URL to be used
524 * @return array
525 */
526 public function http_get_curl($uri) {
527 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
528
529 if (empty($uri)) {
530 return new WP_Error('error', '', 'no_uri');
531 }
532
533 if (!function_exists('curl_exec')) {
534 return new WP_Error('error', '', 'no_curl');
535 }
536
537 $response_encode = $updraftplus_admin->http_get($uri, true);
538 $response_decode = json_decode($response_encode);
539
540 $response = 'Curl Info: ' . $response_decode->verb
541 .'Response: ' . $response_decode->response;
542
543 if (false === $response_decode->response) {
544 return new WP_Error('error', '', array(
545 'error' => htmlspecialchars($response_decode->e),
546 "status" => $response_decode->status,
547 "log" => htmlspecialchars($response_decode->verb)
548 ));
549 }
550
551 return array(
552 'response'=> htmlspecialchars(substr($response, 0, 2048)),
553 'status'=> $response_decode->status,
554 'log'=> htmlspecialchars($response_decode->verb)
555 );
556 }
557
558 /**
559 * Display raw backup and file list
560 *
561 * @return string
562 */
563 public function show_raw_backup_and_file_list() {
564 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
565
566 /*
567 Need to remove the pre tags as the modal assumes a <pre> is for a new box.
568 This cause issues specifically with fetch log events. Do this by passing true
569 to the method show_raw_backups
570 */
571
572 $response = $updraftplus_admin->show_raw_backups(true);
573
574 return $response['html'];
575 }
576
577 public function reset_site_id() {
578 if (false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
579 delete_site_option('updraftplus-addons_siteid');
580 return $updraftplus->siteid();
581 }
582
583 public function search_replace($query) {
584
585 if (!class_exists('UpdraftPlus_Addons_Migrator')) {
586 return new WP_Error('error', '', 'no_class_found');
587 }
588
589 global $updraftplus_addons_migrator;
590
591 if (!is_a($updraftplus_addons_migrator, 'UpdraftPlus_Addons_Migrator')) {
592 return new WP_Error('error', 'no_object_found');
593 }
594
595 $_POST = $query;
596
597 ob_start();
598
599 do_action('updraftplus_adminaction_searchreplace', $query);
600
601 $response = array('log' => ob_get_clean());
602
603 return $response;
604 }
605
606 public function change_lock_settings($data) {
607 global $updraftplus_addon_lockadmin;
608
609 if (!class_exists('UpdraftPlus_Addon_LockAdmin')) {
610 return new WP_Error('error', '', 'no_class_found');
611 }
612
613 if (!is_a($updraftplus_addon_lockadmin, "UpdraftPlus_Addon_LockAdmin")) {
614 return new WP_Error('error', '', 'no_object_found');
615 }
616
617 $session_length = empty($data["session_length"]) ? '' : $data["session_length"];
618 $password = empty($data["password"]) ? '' : $data["password"];
619 $old_password = empty($data["old_password"]) ? '' : $data["old_password"];
620 $support_url = $data["support_url"];
621
622 $user = wp_get_current_user();
623 if (0 == $user->ID) {
624 return new WP_Error('no_user_found');
625 }
626
627 $options = $updraftplus_addon_lockadmin->return_opts();
628
629 if ($old_password == $options['password']) {
630
631 $options['password'] = (string) $password;
632 $options['support_url'] = (string) $support_url;
633 $options['session_length'] = (int) $session_length;
634 UpdraftPlus_Options::update_updraft_option('updraft_adminlocking', $options);
635
636 return "lock_changed";
637 } else {
638 return new WP_Error('error', '', 'wrong_old_password');
639 }
640 }
641
642 public function delete_key($key_id) {
643 global $updraftplus_updraftcentral_main;
644
645 if (!is_a($updraftplus_updraftcentral_main, 'UpdraftPlus_UpdraftCentral_Main')) {
646 return new WP_Error('error', '', 'UpdraftPlus_UpdraftCentral_Main object not found');
647 }
648
649 $response = $updraftplus_updraftcentral_main->delete_key($key_id);
650 return $response;
651
652 }
653
654 public function create_key($data) {
655 global $updraftplus_updraftcentral_main;
656
657 if (!is_a($updraftplus_updraftcentral_main, 'UpdraftPlus_UpdraftCentral_Main')) {
658 return new WP_Error('error', '', 'UpdraftPlus_UpdraftCentral_Main object not found');
659 }
660
661 $response = call_user_func(array($updraftplus_updraftcentral_main, 'create_key'), $data);
662
663 return $response;
664 }
665
666 public function fetch_log($data) {
667 global $updraftplus_updraftcentral_main;
668
669 if (!is_a($updraftplus_updraftcentral_main, 'UpdraftPlus_UpdraftCentral_Main')) {
670 return new WP_Error('error', '', 'UpdraftPlus_UpdraftCentral_Main object not found');
671 }
672
673 $response = call_user_func(array($updraftplus_updraftcentral_main, 'get_log'), $data);
674 return $response;
675 }
676
677 /**
678 * A handler method to call the UpdraftPlus admin auth_remote_method
679 *
680 * @param Array - $data It consists of below key elements:
681 * $remote_method - Remote storage service
682 * $instance_id - Remote storage instance id
683 * @return Array An Array response to be sent back
684 */
685 public function auth_remote_method($data) {
686 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
687 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
688 $response = $updraftplus_admin->auth_remote_method($data);
689 return $response;
690 }
691
692 /**
693 * A handler method to call the UpdraftPlus admin deauth_remote_method
694 *
695 * @param Array - $data It consists of below key elements:
696 * $remote_method - Remote storage service
697 * $instance_id - Remote storage instance id
698 * @return Array An Array response to be sent back
699 */
700 public function deauth_remote_method($data) {
701 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
702 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
703 $response = $updraftplus_admin->deauth_remote_method($data);
704 return $response;
705 }
706
707 /**
708 * A handler method to call the UpdraftPlus admin wipe settings method
709 *
710 * @return Array An Array response to be sent back
711 */
712 public function wipe_settings() {
713 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
714
715 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
716
717 // pass false to this method so that it does not remove the UpdraftCentral key
718 $response = $updraftplus_admin->updraft_wipe_settings(false);
719
720 return $response;
721 }
722
723 /**
724 * Retrieves backup information (next scheduled backups, last backup jobs and last log message)
725 * for UpdraftCentral consumption
726 *
727 * @return Array An array containing the results of the backup information retrieval
728 */
729 public function get_backup_info() {
730 try {
731
732 // load global updraftplus admin
733 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
734
735 ob_start();
736 $updraftplus_admin->next_scheduled_backups_output();
737 $next_scheduled_backups = ob_get_clean();
738
739 $response = array(
740 'next_scheduled_backups' => $next_scheduled_backups,
741 'last_backup_job' => $updraftplus_admin->last_backup_html(),
742 'last_log_message' => UpdraftPlus_Options::get_updraft_lastmessage()
743 );
744
745 $updraft_last_backup = UpdraftPlus_Options::get_updraft_option('updraft_last_backup', false);
746 $backup_history = UpdraftPlus_Backup_History::get_history();
747
748 if (false !== $updraft_last_backup && !empty($backup_history)) {
749 $backup_nonce = $updraft_last_backup['backup_nonce'];
750
751 $response['backup_nonce'] = $backup_nonce;
752 $response['log'] = $this->get_log($backup_nonce);
753 }
754
755 } catch (Exception $e) {
756 $response = array('error' => true, 'message' => $e->getMessage());
757 }
758
759 return $response;
760 }
761
762 /**
763 * This method will check the connection status to UpdraftPlus.com using the submitted credentials and return the result of that check.
764 *
765 * @param array $data - an array that contains the users UpdraftPlus.com credentials
766 *
767 * @return array - an array with the result of the connection status
768 */
769 public function updraftplus_com_login_submit($data) {
770 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
771
772 global $updraftplus_addons2;
773
774 $options = $updraftplus_addons2->get_option(UDADDONS2_SLUG.'_options');
775 $new_options = $data['data'];
776
777 // Check if we can make a connection if we can then we don't want to reset the options in the case where the user has removed their password from the form
778 $result = !empty($options['email']) ? $updraftplus_addons2->connection_status() : false;
779
780 if (true !== $result) {
781 // We failed to make a connection so try the new options
782 $updraftplus_addons2->update_option(UDADDONS2_SLUG.'_options', $new_options);
783 $result = $updraftplus_addons2->connection_status();
784 }
785
786 if (true !== $result) {
787 if (is_wp_error($result)) {
788 $connection_errors = array();
789 foreach ($result->get_error_messages() as $key => $msg) {
790 $connection_errors[] = $msg;
791 }
792 } else {
793 if (!empty($options['email']) && !empty($options['password'])) $connection_errors = array(__('An unknown error occurred when trying to connect to UpdraftPlus.Com', 'updraftplus'));
794 }
795 $result = false;
796 }
797 if ($result && isset($new_options['auto_update'])) {
798 if (1 == $new_options['auto_update']) {
799 UpdraftPlus_Options::update_updraft_option('updraft_auto_updates', 1);
800 } else {
801 UpdraftPlus_Options::update_updraft_option('updraft_auto_updates', 0);
802 }
803 }
804
805 if ($result) {
806 return array(
807 'success' => true
808 );
809 } else {
810 // There was an error reset the options so that we don't get unwanted notices on the dashboard.
811 $updraftplus_addons2->update_option(UDADDONS2_SLUG.'_options', array('email' => '', 'password' => ''));
812
813 return array(
814 'error' => true,
815 'message' => $connection_errors
816 );
817 }
818 }
819
820 /**
821 * This function will add some needed filters in order to be able to send a local backup to remote storage it will then boot the backup process.
822 *
823 * @param array $data - data sent from the front end, it includes the backup timestamp and nonce
824 *
825 * @return array - the response to be sent back to the front end
826 */
827 public function upload_local_backup($data) {
828 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
829
830 add_filter('updraftplus_initial_jobdata', array($updraftplus_admin, 'upload_local_backup_jobdata'), 10, 3);
831 add_filter('updraftplus_get_backup_file_basename_from_time', array($updraftplus_admin, 'upload_local_backup_name'), 10, 3);
832
833 $background_operation_started_method_name = empty($data['background_operation_started_method_name']) ? '_updraftplus_background_operation_started' : $data['background_operation_started_method_name'];
834
835 $msg = array(
836 'nonce' => $data['use_nonce'],
837 'm' => apply_filters('updraftplus_backupnow_start_message', '<strong>'.__('Start backup', 'updraftplus').':</strong> '.htmlspecialchars(__('OK. You should soon see activity in the "Last log message" field below.', 'updraftplus')), $data['use_nonce'])
838 );
839
840 $close_connection_callable = array($this->_uc_helper, $background_operation_started_method_name);
841
842 if (is_callable($close_connection_callable)) {
843 call_user_func($close_connection_callable, $msg);
844 } else {
845 $updraftplus->close_browser_connection(json_encode($msg));
846 }
847
848 do_action('updraft_backupnow_backup_all', apply_filters('updraft_backupnow_options', $data, array()));
849
850 // Control returns when the backup finished; but, the browser connection should have been closed before
851 die;
852 }
853
854 /**
855 * Pre-check before sending request and delegates login request to the appropriate service
856 *
857 * @param array $params - The submitted form data
858 * @return string - the result of the call
859 */
860 public function process_updraftcentral_login($params) {
861 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
862 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
863
864 return $updraftplus_admin->get_updraftcentral_cloud()->ajax_process_login($params);
865 }
866
867 /**
868 * Pre-check before sending request and delegates registration request to the appropriate service
869 *
870 * @param array $params - The submitted form data
871 * @return string - the result of the call
872 */
873 public function process_updraftcentral_registration($params) {
874 if (false === ($updraftplus_admin = $this->_load_ud_admin())) return new WP_Error('no_updraftplus');
875 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
876
877 return $updraftplus_admin->get_updraftcentral_cloud()->ajax_process_registration($params);
878 }
879
880 /**
881 * Pre-check before sending request and delegates login request to the appropriate service
882 *
883 * @param array $params - The submitted form data
884 * @return string - the result of the call
885 */
886 public function process_updraftplus_clone_login($params) {
887 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
888 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
889
890 $response = $updraftplus->get_updraftplus_clone()->ajax_process_login($params, false);
891
892 if (isset($response['status']) && 'authenticated' == $response['status']) {
893 $tokens = isset($response['tokens']) ? $response['tokens'] : 0;
894 $content = '<div class="updraftclone-main-row">';
895 $content .= '<div class="updraftclone-tokens">';
896 $content .= '<p>' . __("Available temporary clone tokens:", "updraftplus") . ' <span class="tokens-number">' . esc_html($tokens) . '</span></p>';
897 $content .= '<p><a href="'.$updraftplus->get_url('buy-tokens').'">'.__('You can buy more temporary clone tokens here.', 'updraftplus').'</a></p>';
898 $content .= '</div>';
899
900 if (0 != $response['tokens']) {
901 $content .= '<div class="updraftclone_action_box">';
902 $content .= $updraftplus_admin->updraftplus_clone_versions();
903 $content .= '<p class="updraftplus_clone_status"></p>';
904 $content .= '<button id="updraft_migrate_createclone" class="button button-primary button-hero" data-clone_id="'.$response['clone_info']['id'].'" data-secret_token="'.$response['clone_info']['secret_token'].'">'. __('Create clone', 'updraftplus') . '</button>';
905 $content .= '<span class="updraftplus_spinner spinner">' . __('Processing', 'updraftplus') . '...</span>';
906 $content .= '</div>';
907 }
908 $content .= '</div>'; // end .updraftclone-main-row
909
910 $content .= isset($response['clone_list']) ? '<div class="clone-list"><h3>'.__('Current clones', 'updraftplus').' - <a target="_blank" href="https://updraftplus.com/my-account/clones/">'.__('manage', 'updraftplus').'</a></h3>'.$response['clone_list'].'</div>' : '';
911
912 $response['html'] = $content;
913 }
914
915 return $response;
916 }
917
918 /**
919 * This function sends the request to create the clone
920 *
921 * @param array $params - The submitted data
922 * @return string - the result of the call
923 */
924 public function process_updraftplus_clone_create($params) {
925 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
926 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
927
928 $response = $updraftplus->get_updraftplus_clone()->ajax_process_clone($params);
929
930 if (!isset($response['status']) && 'success' != $response['status']) return $response;
931
932 $content = '';
933
934 if (isset($response['data'])) {
935 $tokens = isset($response['data']['tokens']) ? $response['data']['tokens'] : 0;
936 $url = isset($response['data']['url']) ? $response['data']['url'] : '';
937
938 $content .= '<div class="updraftclone-main-row">';
939
940 $content .= '<div class="updraftclone-tokens">';
941 $content .= '<p>' . __("Available temporary clone tokens:", "updraftplus") . ' <span class="tokens-number">' . esc_html($tokens) . '</span></p>';
942 $content .= '</div>';
943
944 $content .= '<div class="updraftclone_action_box">';
945
946 $content .= $updraftplus_admin->updraftplus_clone_info($url);
947
948 $content .= '</div>';
949
950 $content .= '</div>'; // end .updraftclone-main-row
951 }
952
953 $content .= '<p id="updraft_clone_progress">' . __('The creation of your data for creating the clone should now begin. N.B. You will be charged one token once the clone is ready. If the clone fails to boot, then no token will be taken.', 'updraftplus') . '<span class="updraftplus_spinner spinner">' . __('Processing', 'updraftplus') . '...</span></p>';
954 $content .= '<div id="updraft_clone_activejobsrow" style="display:none;"></div>';
955
956 $response['html'] = $content;
957 $response['url'] = $url;
958 $response['key'] = '';
959
960 return $response;
961 }
962
963 /**
964 * This function will get the clone netowrk info HTML for the passed in clone URL
965 *
966 * @param array $params - the parameters for the call
967 *
968 * @return array - the response array that includes the network HTML
969 */
970 public function get_clone_network_info($params) {
971 if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return new WP_Error('no_updraftplus');
972 if (!UpdraftPlus_Options::user_can_manage()) return new WP_Error('updraftplus_permission_denied');
973
974 $url = empty($params['clone_url']) ? '' : $params['clone_url'];
975
976 $response = array();
977
978 $response['html'] = $updraftplus_admin->updraftplus_clone_info($url);
979
980 return $response;
981 }
982 }
983