Example #1
0
/**
 * Process the requests sent by the form submissions originated in the monitoring
 * page, all forms must have a nonce field that will be checked against the one
 * generated in the template render function.
 *
 * @return void
 */
function sucuriscan_monitoring_form_submissions()
{
    if (SucuriScanInterface::check_nonce()) {
        // Add and/or Update the Sucuri WAF API Key (do it before anything else).
        $option_name = ':cloudproxy_apikey';
        $api_key = SucuriScanRequest::post($option_name);
        if ($api_key !== false) {
            if (SucuriScanAPI::is_valid_cloudproxy_key($api_key)) {
                SucuriScanOption::update_option($option_name, $api_key);
                SucuriScanOption::update_option(':revproxy', 'enabled');
                SucuriScanInterface::info('CloudProxy API key saved successfully');
            } elseif (empty($api_key)) {
                SucuriScanOption::delete_option($option_name);
                SucuriScanOption::update_option(':revproxy', 'disabled');
                SucuriScanInterface::info('CloudProxy API key removed successfully');
            } else {
                SucuriScanInterface::error('Invalid CloudProxy API key, check your settings and try again.');
            }
        }
        // Flush the cache of the site(s) associated with the API key.
        if (SucuriScanRequest::post(':clear_cache', '1')) {
            $clear_cache_resp = SucuriScanAPI::clear_cloudproxy_cache();
            if ($clear_cache_resp) {
                if (isset($clear_cache_resp->messages[0])) {
                    // Clear W3 Total Cache if it is installed.
                    if (function_exists('w3tc_flush_all')) {
                        w3tc_flush_all();
                    }
                    SucuriScanInterface::info($clear_cache_resp->messages[0]);
                } else {
                    SucuriScanInterface::error('Could not clear the cache of your site, try later again.');
                }
            } else {
                SucuriScanInterface::error('CloudProxy is not enabled on your site, or your API key is invalid.');
            }
        }
    }
}