PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/updraftplus/central/updraftplus-commands.php

https://gitlab.com/meixnertobias/thaimaidaiproductionwp
PHP | 339 lines | 209 code | 117 blank | 13 comment | 66 complexity | c83c98615e576543dc65e360951fe69f MD5 | raw file
  1. <?php
  2. if (!defined('UPDRAFTPLUS_DIR')) die('No access.');
  3. /*
  4. - A container for all the RPC commands implemented. Commands map exactly onto method names (and hence this class should not implement anything else, beyond the constructor, and private methods)
  5. - Return format is array('response' => (string - a code), 'data' => (mixed));
  6. RPC commands are not allowed to begin with an underscore. So, any private methods can be prefixed with an underscore.
  7. */
  8. if (!class_exists('UpdraftCentral_Commands')) require_once('commands.php');
  9. class UpdraftPlus_RemoteControl_Commands extends UpdraftCentral_Commands {
  10. public function get_download_status($items) {
  11. if (false === ($updraftplus_admin = $this->_load_ud_admin())) return $this->_generic_error_response('no_updraftplus');
  12. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  13. if (!is_array($items)) $items = array();
  14. return $this->_response($updraftplus_admin->get_download_statuses($items));
  15. }
  16. public function downloader($downloader_params) {
  17. if (false === ($updraftplus_admin = $this->_load_ud_admin())) return $this->_generic_error_response('no_updraftplus');
  18. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  19. $findex = $downloader_params['findex'];
  20. $type = $downloader_params['type'];
  21. $timestamp = $downloader_params['timestamp'];
  22. // Valid stages: 2='spool the data'|'delete'='delete local copy'|anything else='make sure it is present'
  23. $stage = empty($downloader_params['stage']) ? false : $downloader_params['stage'];
  24. // This may, or may not, return, depending upon whether the files are already downloaded
  25. // The response is usually an array with key 'result', and values deleted|downloaded|needs_download|download_failed
  26. $response = $updraftplus_admin->do_updraft_download_backup($findex, $type, $timestamp, $stage, array($this, '_updraftplus_background_operation_started'));
  27. if (is_array($response)) {
  28. $response['request'] = $downloader_params;
  29. }
  30. return $this->_response($response);
  31. }
  32. public function delete_downloaded($set_info) {
  33. $set_info['stage'] = 'delete';
  34. return $this->downloader($set_info);
  35. }
  36. public function backup_progress($params) {
  37. if (false === ($updraftplus_admin = $this->_load_ud_admin())) return $this->_generic_error_response('no_updraftplus');
  38. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  39. $request = array(
  40. 'thisjobonly' => $params['job_id']
  41. );
  42. $activejobs_list = $updraftplus_admin->get_activejobs_list($request);
  43. return $this->_response($activejobs_list);
  44. }
  45. public function backupnow($params) {
  46. if (false === ($updraftplus_admin = $this->_load_ud_admin())) return $this->_generic_error_response('no_updraftplus');
  47. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  48. $updraftplus_admin->request_backupnow($params, array($this, '_updraftplus_background_operation_started'));
  49. // Control returns when the backup finished; but, the browser connection should have been closed before
  50. die;
  51. }
  52. public function _updraftplus_background_operation_started($msg) {
  53. // Under-the-hood hackery to allow the browser connection to be closed, and the backup/download to continue
  54. $rpc_response = $this->rc->return_rpc_message($this->_response($msg));
  55. $data = isset($rpc_response['data']) ? $rpc_response['data'] : null;
  56. $ud_rpc = $this->rc->get_current_udrpc();
  57. $encoded = json_encode($ud_rpc->create_message($rpc_response['response'], $data, true));
  58. $this->_load_ud()->close_browser_connection($encoded);
  59. }
  60. private function _load_ud() {
  61. global $updraftplus;
  62. return is_a($updraftplus, 'UpdraftPlus') ? $updraftplus : false;
  63. }
  64. private function _load_ud_admin() {
  65. if (!defined('UPDRAFTPLUS_DIR') || !is_file(UPDRAFTPLUS_DIR.'/admin.php')) return false;
  66. require_once(UPDRAFTPLUS_DIR.'/admin.php');
  67. global $updraftplus_admin;
  68. return $updraftplus_admin;
  69. }
  70. public function get_log($job_id) {
  71. if (false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  72. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  73. if (!preg_match("/^[0-9a-f]{12}$/", $job_id)) return $this->_generic_error_response('updraftplus_permission_invalid_jobid');
  74. $updraft_dir = $updraftplus->backups_dir_location();
  75. $log_file = $updraft_dir.'/log.'.$job_id.'.txt';
  76. if (is_readable($log_file)) {
  77. return $this->_response(array('log' => file_get_contents($log_file)));
  78. } else {
  79. return $this->_generic_error_response('updraftplus_unreadable_log');
  80. }
  81. }
  82. public function activejobs_delete($job_id) {
  83. if (false === ($updraftplus_admin = $this->_load_ud_admin())) return $this->_generic_error_response('no_updraftplus');
  84. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  85. $delete = $updraftplus_admin->activejobs_delete((string)$job_id);
  86. return $this->_response($delete);
  87. }
  88. public function deleteset($what) {
  89. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  90. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  91. $results = $updraftplus_admin->delete_set($what);
  92. $get_history_opts = isset($what['get_history_opts']) ? $what['get_history_opts'] : array();
  93. $history = $updraftplus_admin->settings_downloading_and_restoring(UpdraftPlus_Options::get_updraft_option('updraft_backup_history'), true, $get_history_opts);
  94. $results['history'] = $history;
  95. return $this->_response($results);
  96. }
  97. public function rescan($what) {
  98. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  99. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  100. $remotescan = ('remotescan' == $what);
  101. $rescan = ($remotescan || 'rescan' == $what);
  102. $history_status = $updraftplus_admin->get_history_status($rescan, $remotescan);
  103. return $this->_response($history_status);
  104. }
  105. public function get_settings($options) {
  106. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  107. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  108. ob_start();
  109. $updraftplus_admin->settings_formcontents($options);
  110. $output = ob_get_contents();
  111. ob_end_clean();
  112. return $this->_response(array(
  113. 'settings' => $output,
  114. 'meta' => apply_filters('updraftplus_get_settings_meta', array()),
  115. ));
  116. }
  117. public function test_storage_settings($test_data) {
  118. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  119. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  120. ob_start();
  121. $updraftplus_admin->do_credentials_test($test_data);
  122. $output = ob_get_contents();
  123. ob_end_clean();
  124. return $this->_response(array(
  125. 'output' => $output,
  126. ));
  127. }
  128. public function extradb_testconnection($info) {
  129. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  130. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  131. $results = apply_filters('updraft_extradb_testconnection_go', array(), $info);
  132. return $this->_response($results);
  133. }
  134. private function _get_vault() {
  135. require_once(UPDRAFTPLUS_DIR.'/methods/updraftvault.php');
  136. $vault = new UpdraftPlus_BackupModule_updraftvault();
  137. return $vault;
  138. }
  139. public function vault_connect($credentials) {
  140. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  141. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  142. $results = $this->_get_vault()->ajax_vault_connect(false, $credentials);
  143. return $this->_response($results);
  144. }
  145. public function vault_disconnect() {
  146. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  147. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  148. $results = (array)$this->_get_vault()->ajax_vault_disconnect(false);
  149. return $this->_response($results);
  150. }
  151. public function vault_recount_quota() {
  152. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  153. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  154. $results = $this->_get_vault()->ajax_vault_recountquota(false);
  155. return $this->_response($results);
  156. }
  157. public function save_settings($settings) {
  158. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  159. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  160. if (empty($settings) || !is_string($settings)) return $this->_generic_error_response('invalid_settings');
  161. parse_str($settings, $settings_as_array);
  162. $results = $updraftplus_admin->save_settings($settings_as_array);
  163. return $this->_response($results);
  164. }
  165. public function s3_newuser($data) {
  166. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  167. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  168. $results = apply_filters('updraft_s3_newuser_go', array(), $data);
  169. return $this->_response($results);
  170. }
  171. public function get_fragment($fragment) {
  172. if (false === ($updraftplus_admin = $this->_load_ud_admin()) || false === ($updraftplus = $this->_load_ud())) return $this->_generic_error_response('no_updraftplus');
  173. if (!UpdraftPlus_Options::user_can_manage()) return $this->_generic_error_response('updraftplus_permission_denied');
  174. if (is_array($fragment)) {
  175. $data = $fragment['data'];
  176. $fragment = $fragment['fragment'];
  177. }
  178. $error = false;
  179. switch ($fragment) {
  180. case 's3_new_api_user_form':
  181. ob_start();
  182. do_action('updraft_s3_print_new_api_user_form', false);
  183. $output = ob_get_contents();
  184. ob_end_clean();
  185. break;
  186. case 'backupnow_modal_contents':
  187. $updraft_dir = $updraftplus->backups_dir_location();
  188. if (!$updraftplus->really_is_writable($updraft_dir)) {
  189. $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'));
  190. } else {
  191. $output = array('html' => $updraftplus_admin->backupnow_modal_contents());
  192. }
  193. break;
  194. case 'panel_download_and_restore':
  195. $backup_history = UpdraftPlus_Options::get_updraft_option('updraft_backup_history');
  196. if (empty($backup_history)) {
  197. $updraftplus->rebuild_backup_history();
  198. $backup_history = UpdraftPlus_Options::get_updraft_option('updraft_backup_history');
  199. }
  200. $backup_history = is_array($backup_history) ? $backup_history : array();
  201. $output = $updraftplus_admin->settings_downloading_and_restoring($backup_history, true, $data);
  202. break;
  203. case 'disk_usage':
  204. $output = $updraftplus_admin->get_disk_space_used($data);
  205. break;
  206. default:
  207. // We just return a code - translation is done on the other side
  208. $output = 'ud_get_fragment_could_not_return';
  209. $error = true;
  210. break;
  211. }
  212. if (empty($error)) {
  213. return $this->_response(array(
  214. 'output' => $output,
  215. ));
  216. } else {
  217. return $this->_generic_error_response('get_fragment_error', $output);
  218. }
  219. }
  220. }