0 || sizeof($TBU['queue']) > 0)) { // } add_action('admin_footer', [&$this, 'keepAliveJS']); } /** * initializeOfflineAjax - Initialized Offline handlers for Ajax * * @return void */ public function initializeOfflineAjax() { // Check if the request comes from a logged-in admin (Browser context) // OR from the Ping Server (M2M context) $is_admin = current_user_can('manage_options') && check_ajax_referer('backup-migration-ajax', 'nonce', false); $is_ping_server = $this->verify_ping_server_request(); if (!$is_admin && !$is_ping_server) { wp_send_json_error('Unauthorized access', 403); return; } // if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { // Extend execution time if (BMP::isFunctionEnabled('headers_sent') && BMP::isFunctionEnabled('session_status')) { if (!headers_sent() && session_status() === PHP_SESSION_DISABLED) { if (BMP::isFunctionEnabled('ignore_user_abort')) @ignore_user_abort(true); if (BMP::isFunctionEnabled('set_time_limit')) @set_time_limit(16000); if (BMP::isFunctionEnabled('ini_set')) { @ini_set('max_execution_time', '259200'); @ini_set('max_input_time', '259200'); } } } if ((isset($_GET['token']) && ($_GET['token'] == 'bmip' || $_GET['token'] == 'bmi') && isset($_GET['f']))) { if (empty($_GET)) return; // Sanitize User Input $post = BMP::sanitize($_GET); } else if ((isset($_POST['token']) && ($_POST['token'] == 'bmip' || $_POST['token'] == 'bmi') && isset($_POST['f']))) { if (empty($_POST)) return; // Sanitize User Input $post = BMP::sanitize($_POST); } if (!empty($post)) { do_action("bmi_ajax_offline", $post); } // Execution error due to time limit // register_shutdown_function([$this, 'execution_shutdown']); // } } /** * Verifies the Ping Server handshake. * @return bool true if the request is verified, otherwise it sends a JSON error response and exits. */ private function verify_ping_server_request() { $stored_sk = get_option('bmi_sk_keepalive'); if (!isset($_SERVER['CONTENT_TYPE']) || stripos($_SERVER['CONTENT_TYPE'], 'application/json') === false) { return false; } $raw = file_get_contents('php://input'); $data = json_decode($raw, true); if (json_last_error() !== JSON_ERROR_NONE) { return false; } $request_sk = sanitize_text_field( wp_unslash( isset($data['sk']) ? $data['sk'] : '' ) ); if (empty($stored_sk) || empty($request_sk)) { return false; } // Constant-Time Comparison (Prevents Timing Attacks) when possible if (!hash_equals($stored_sk, $request_sk)) { return false; } update_option('bmi_cron_last_ping_time', current_time('timestamp')); return true; } public function keepAliveJS() { if ($this->ajaxInserted) return; ?> ajaxInserted = true; } function bmip_handle_handshake_request() { $incoming_sk = isset($_POST['sk']) ? sanitize_text_field($_POST['sk']) : ''; $challenge = isset($_POST['challenge']) ? sanitize_text_field($_POST['challenge']) : ''; $stored_sk = get_option('bmi_sk_keepalive'); if ( ! empty($stored_sk) && ! empty($incoming_sk) && hash_equals($stored_sk, $incoming_sk) ) { header('Content-Type: text/plain'); echo esc_html( $challenge ); exit; } else { header('HTTP/1.0 403 Forbidden'); echo 'Invalid Handshake'; exit; } } }