PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / lib-central / central / host.php

host.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.7.0, at vendor/team-updraft/lib-central/central/host.php

312 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access.');
4
5 /**
6 * The template definition for UpdraftCentral host
7 */
8 abstract class UpdraftCentral_Host {
9
10 public $plugin_name;
11
12 public $translations;
13
14 public $error_reporting_stop_when_logged = false;
15
16 public $no_deprecation_warnings = false;
17
18 private $jobdata;
19
20 abstract protected function load_updraftcentral();
21 abstract protected function is_host_dir_set();
22 abstract protected function get_host_dir();
23 abstract protected function get_version();
24 abstract public function log($line, $level = 'notice', $uniq_id = false);
25
26 /**
27 * Class constructor
28 */
29 public function __construct() {
30 add_action('wp_ajax_updraft_central_ajax', array($this, 'updraft_central_ajax_handler'));
31 }
32
33 /**
34 * Returns the plugin name associated with this host class
35 *
36 * @return string
37 */
38 public function get_plugin_name() {
39 return $this->plugin_name;
40 }
41
42 /**
43 * Retrieves or shows a message from the translations collection based on its identifier key
44 *
45 * @param string $key The ID of the the message
46 * @param bool $echo Indicate whether the message is to be shown directly (echoed) or just for retrieval
47 *
48 * @return string/void
49 */
50 public function retrieve_show_message($key, $echo = false) {
51 if (empty($key) || !isset($this->translations[$key])) return '';
52
53 if ($echo) {
54 echo $this->translations[$key];
55 return;
56 }
57
58 return $this->translations[$key];
59 }
60
61 /**
62 * Adds a section to a designated area primarily used for generating UpdraftCentral keys
63 *
64 * @return void
65 */
66 public function debugtools_dashboard() {
67 if (!current_user_can('manage_options')) return;
68
69 global $updraftcentral_main;
70
71 if (!class_exists('UpdraftCentral_Main')) {
72 if (defined('UPDRAFTCENTRAL_CLIENT_DIR') && file_exists(UPDRAFTCENTRAL_CLIENT_DIR.'/bootstrap.php')) {
73 include_once(UPDRAFTCENTRAL_CLIENT_DIR.'/bootstrap.php');
74 $updraftcentral_main = new UpdraftCentral_Main();
75 }
76 }
77
78 if ($updraftcentral_main) {
79 $updraftcentral_main->debugtools_dashboard();
80 }
81 }
82
83 /**
84 * Whether the current user can perform key control AJAX actions
85 *
86 * @return Boolean
87 */
88 public function current_user_can_ajax() {
89 return current_user_can('manage_options');
90 }
91
92 /**
93 * Handles ajax requests coming from the section or area generated by the
94 * "debugtools_dashboard" method (below)
95 *
96 * @return void
97 */
98 public function updraft_central_ajax_handler() {
99 global $updraftcentral_main;
100
101 $nonce = empty($_REQUEST['nonce']) ? '' : $_REQUEST['nonce'];
102 if (empty($nonce) || !wp_verify_nonce($nonce, 'updraftcentral-request-nonce') || !$this->current_user_can_ajax() || empty($_REQUEST['subaction'])) die('Security check');
103
104 if (is_a($updraftcentral_main, 'UpdraftCentral_Main')) {
105
106 $subaction = $_REQUEST['subaction'];
107 if ($this->is_action_whitelisted($subaction) && is_callable(array($updraftcentral_main, $subaction))) {
108
109 // Undo WP's slashing of POST data
110 $data = $this->wp_unslash($_POST);
111
112 // TODO: Once all commands come through here and through updraft_send_command(), the data should always come from this attribute (once updraft_send_command() is modified appropriately).
113 if (isset($data['action_data'])) $data = $data['action_data'];
114 try {
115
116 $results = call_user_func(array($updraftcentral_main, $subaction), $data);
117 } catch (Exception $e) {
118 $log_message = 'PHP Fatal Exception error ('.get_class($e).') has occurred during '.$subaction.' subaction. Error Message: '.$e->getMessage().' (Code: '.$e->getCode().', line '.$e->getLine().' in '.$e->getFile().')';
119 error_log($log_message);
120 echo json_encode(array(
121 'fatal_error' => true,
122 'fatal_error_message' => $log_message
123 ));
124 die;
125 // @codingStandardsIgnoreLine
126 } catch (Error $e) {
127 $log_message = 'PHP Fatal error ('.get_class($e).') has occurred during '.$subaction.' subaction. Error Message: '.$e->getMessage().' (Code: '.$e->getCode().', line '.$e->getLine().' in '.$e->getFile().')';
128 error_log($log_message);
129 echo json_encode(array(
130 'fatal_error' => true,
131 'fatal_error_message' => $log_message
132 ));
133 die;
134 }
135 if (is_wp_error($results)) {
136 $results = array(
137 'result' => false,
138 'error_code' => $results->get_error_code(),
139 'error_message' => $results->get_error_message(),
140 'error_data' => $results->get_error_data(),
141 );
142 }
143
144 if (is_string($results)) {
145 // A handful of legacy methods, and some which are directly the source for iframes, for which JSON is not appropriate.
146 echo $results;
147 } else {
148 echo json_encode($results);
149 }
150 die;
151 }
152 }
153 die;
154 }
155
156 /**
157 * Verifies whether the submitted action is valid and allowed for execution over AJAX
158 *
159 * @param string $action The action to execute
160 *
161 * @return bool
162 */
163 private function is_action_whitelisted($action) {
164 $allowed_actions = array('delete_key', 'get_log', 'create_key');
165 return in_array($action, $allowed_actions);
166 }
167
168 /**
169 * Retrieves the filter used by UpdraftCentral to log errors or certain events
170 *
171 * @return string
172 */
173 public function get_logline_filter() {
174 return 'updraftcentral_logline';
175 }
176
177 /**
178 * Gets an RPC object, and sets some defaults on it that we always want
179 *
180 * @param string $indicator_name indicator name
181 * @return array
182 */
183 public function get_udrpc($indicator_name = 'migrator.updraftplus.com') {
184 global $updraftplus;
185 $updraftplus->ensure_phpseclib();
186 if (!class_exists('UpdraftPlus_Remote_Communications_V2')) include_once($this->get_host_dir().'/vendor/team-updraft/common-libs/src/updraft-rpc/class-udrpc2.php');
187 $ud_rpc = new UpdraftPlus_Remote_Communications_V2($indicator_name);
188 $ud_rpc->set_can_generate(true);
189 return $ud_rpc;
190 }
191
192 /**
193 * Noop method.
194 * Depending on the host plugin this method may or may not be used.
195 *
196 * N.B. The UpdraftPlus plugin is using and overriding this method in its host file.
197 *
198 * @param boolean $register Indicate whether to add or remote filter hooks
199 * @ignore
200 */
201 // @codingStandardsIgnoreLine
202 public function register_wp_http_option_hooks($register = true) {}
203
204 /**
205 * Remove slashes from a string or array of strings.
206 *
207 * The function wp_unslash() is WP 3.6+, so therefore we have a compatibility method here
208 *
209 * @param String|Array $value String or array of strings to unslash.
210 * @return String|Array Unslashed $value
211 */
212 public function wp_unslash($value) {
213 return function_exists('wp_unslash') ? wp_unslash($value) : stripslashes_deep($value);
214 }
215
216 /**
217 * Generate a log line based from the PHP error information
218 *
219 * @param Integer $errno Error number
220 * @param String $errstr Error string
221 * @param String $errfile Error file
222 * @param String $errline Line number where the error occurred
223 *
224 * @return string|bool
225 */
226 public function php_error_to_logline($errno, $errstr, $errfile, $errline) {
227 switch ($errno) {
228 case 1:
229 $e_type = 'E_ERROR';
230 break;
231 case 2:
232 $e_type = 'E_WARNING';
233 break;
234 case 4:
235 $e_type = 'E_PARSE';
236 break;
237 case 8:
238 $e_type = 'E_NOTICE';
239 break;
240 case 16:
241 $e_type = 'E_CORE_ERROR';
242 break;
243 case 32:
244 $e_type = 'E_CORE_WARNING';
245 break;
246 case 64:
247 $e_type = 'E_COMPILE_ERROR';
248 break;
249 case 128:
250 $e_type = 'E_COMPILE_WARNING';
251 break;
252 case 256:
253 $e_type = 'E_USER_ERROR';
254 break;
255 case 512:
256 $e_type = 'E_USER_WARNING';
257 break;
258 case 1024:
259 $e_type = 'E_USER_NOTICE';
260 break;
261 case 2048:
262 $e_type = 'E_STRICT';
263 break;
264 case 4096:
265 $e_type = 'E_RECOVERABLE_ERROR';
266 break;
267 case 8192:
268 $e_type = 'E_DEPRECATED';
269 break;
270 case 16384:
271 $e_type = 'E_USER_DEPRECATED';
272 break;
273 case 30719:
274 $e_type = 'E_ALL';
275 break;
276 default:
277 $e_type = "E_UNKNOWN ($errno)";
278 break;
279 }
280
281 if (false !== stripos($errstr, 'table which is not valid in this version of Gravity Forms')) return false;
282
283 if (!is_string($errstr)) $errstr = serialize($errstr);
284
285 if (0 === strpos($errfile, ABSPATH)) $errfile = substr($errfile, strlen(ABSPATH));
286
287 if ('E_DEPRECATED' == $e_type && !empty($this->no_deprecation_warnings)) {
288 return false;
289 }
290
291 return "PHP event: code $e_type: $errstr (line $errline, $errfile)";
292 }
293
294 /**
295 * PHP error handler
296 *
297 * @param Integer $errno Error number
298 * @param String $errstr Error string
299 * @param String $errfile Error file
300 * @param String $errline Line number where the error occurred
301 *
302 * @return bool
303 */
304 public function php_error($errno, $errstr, $errfile, $errline) {
305 if (0 == error_reporting()) return true;
306 $logline = $this->php_error_to_logline($errno, $errstr, $errfile, $errline);
307 if (false !== $logline) $this->log($logline, 'notice', 'php_event');
308 // Pass it up the chain
309 return $this->error_reporting_stop_when_logged;
310 }
311 }
312