| 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 |
if (!class_exists('UpdraftPlus_Remote_Communications')) include_once($this->get_host_dir().'/vendor/team-updraft/common-libs/src/updraft-rpc/class-udrpc.php'); |
| 185 |
$ud_rpc = new UpdraftPlus_Remote_Communications($indicator_name); |
| 186 |
$ud_rpc->set_can_generate(true); |
| 187 |
return $ud_rpc; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Noop method. |
| 192 |
* Depending on the host plugin this method may or may not be used. |
| 193 |
* |
| 194 |
* N.B. The UpdraftPlus plugin is using and overriding this method in its host file. |
| 195 |
* |
| 196 |
* @param boolean $register Indicate whether to add or remote filter hooks |
| 197 |
* @ignore |
| 198 |
*/ |
| 199 |
// @codingStandardsIgnoreLine |
| 200 |
public function register_wp_http_option_hooks($register = true) {} |
| 201 |
|
| 202 |
/** |
| 203 |
* Remove slashes from a string or array of strings. |
| 204 |
* |
| 205 |
* The function wp_unslash() is WP 3.6+, so therefore we have a compatibility method here |
| 206 |
* |
| 207 |
* @param String|Array $value String or array of strings to unslash. |
| 208 |
* @return String|Array Unslashed $value |
| 209 |
*/ |
| 210 |
public function wp_unslash($value) { |
| 211 |
return function_exists('wp_unslash') ? wp_unslash($value) : stripslashes_deep($value); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Generate a log line based from the PHP error information |
| 216 |
* |
| 217 |
* @param Integer $errno Error number |
| 218 |
* @param String $errstr Error string |
| 219 |
* @param String $errfile Error file |
| 220 |
* @param String $errline Line number where the error occurred |
| 221 |
* |
| 222 |
* @return string|bool |
| 223 |
*/ |
| 224 |
public function php_error_to_logline($errno, $errstr, $errfile, $errline) { |
| 225 |
switch ($errno) { |
| 226 |
case 1: |
| 227 |
$e_type = 'E_ERROR'; |
| 228 |
break; |
| 229 |
case 2: |
| 230 |
$e_type = 'E_WARNING'; |
| 231 |
break; |
| 232 |
case 4: |
| 233 |
$e_type = 'E_PARSE'; |
| 234 |
break; |
| 235 |
case 8: |
| 236 |
$e_type = 'E_NOTICE'; |
| 237 |
break; |
| 238 |
case 16: |
| 239 |
$e_type = 'E_CORE_ERROR'; |
| 240 |
break; |
| 241 |
case 32: |
| 242 |
$e_type = 'E_CORE_WARNING'; |
| 243 |
break; |
| 244 |
case 64: |
| 245 |
$e_type = 'E_COMPILE_ERROR'; |
| 246 |
break; |
| 247 |
case 128: |
| 248 |
$e_type = 'E_COMPILE_WARNING'; |
| 249 |
break; |
| 250 |
case 256: |
| 251 |
$e_type = 'E_USER_ERROR'; |
| 252 |
break; |
| 253 |
case 512: |
| 254 |
$e_type = 'E_USER_WARNING'; |
| 255 |
break; |
| 256 |
case 1024: |
| 257 |
$e_type = 'E_USER_NOTICE'; |
| 258 |
break; |
| 259 |
case 2048: |
| 260 |
$e_type = 'E_STRICT'; |
| 261 |
break; |
| 262 |
case 4096: |
| 263 |
$e_type = 'E_RECOVERABLE_ERROR'; |
| 264 |
break; |
| 265 |
case 8192: |
| 266 |
$e_type = 'E_DEPRECATED'; |
| 267 |
break; |
| 268 |
case 16384: |
| 269 |
$e_type = 'E_USER_DEPRECATED'; |
| 270 |
break; |
| 271 |
case 30719: |
| 272 |
$e_type = 'E_ALL'; |
| 273 |
break; |
| 274 |
default: |
| 275 |
$e_type = "E_UNKNOWN ($errno)"; |
| 276 |
break; |
| 277 |
} |
| 278 |
|
| 279 |
if (false !== stripos($errstr, 'table which is not valid in this version of Gravity Forms')) return false; |
| 280 |
|
| 281 |
if (!is_string($errstr)) $errstr = serialize($errstr); |
| 282 |
|
| 283 |
if (0 === strpos($errfile, ABSPATH)) $errfile = substr($errfile, strlen(ABSPATH)); |
| 284 |
|
| 285 |
if ('E_DEPRECATED' == $e_type && !empty($this->no_deprecation_warnings)) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
return "PHP event: code $e_type: $errstr (line $errline, $errfile)"; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* PHP error handler |
| 294 |
* |
| 295 |
* @param Integer $errno Error number |
| 296 |
* @param String $errstr Error string |
| 297 |
* @param String $errfile Error file |
| 298 |
* @param String $errline Line number where the error occurred |
| 299 |
* |
| 300 |
* @return bool |
| 301 |
*/ |
| 302 |
public function php_error($errno, $errstr, $errfile, $errline) { |
| 303 |
if (0 == error_reporting()) return true; |
| 304 |
$logline = $this->php_error_to_logline($errno, $errstr, $errfile, $errline); |
| 305 |
if (false !== $logline) $this->log($logline, 'notice', 'php_event'); |
| 306 |
// Pass it up the chain |
| 307 |
return $this->error_reporting_stop_when_logged; |
| 308 |
} |
| 309 |
} |
| 310 |
|