/**
 * After options update
 * @return array
*/
function ct_update_option($option_name)
{
    global $show_ct_notice_online, $ct_notice_online_label, $ct_notice_trial_label, $trial_notice_showtime, $ct_options, $ct_data, $ct_server_timeout;
    $ct_options = ct_get_options(true);
    $ct_data = ct_get_data(true);
    if ($option_name !== 'cleantalk_settings') {
        return;
    }
    $api_key = $ct_options['apikey'];
    if (isset($_POST['cleantalk_settings']['apikey'])) {
        $api_key = trim($_POST['cleantalk_settings']['apikey']);
        $ct_options['apikey'] = $api_key;
    }
    if (@isset($_POST['cleantalk_settings']['spam_firewall']) && $_POST['cleantalk_settings']['spam_firewall'] == 1) {
        cleantalk_update_sfw();
    }
    if (!ct_valid_key($api_key)) {
        return;
    }
    /*$ct_base_call_result = ct_base_call(array(
    		'message' => 'CleanTalk setup test',
    		'example' => null,
    		'sender_email' => '*****@*****.**',
    		'sender_nickname' => 'CleanTalk',
    		'post_info' => '',
    		'checkjs' => 1
    	));*/
    $key_valid = true;
    $app_server_error = false;
    $ct_data['testing_failed'] = 0;
    if (!function_exists('sendRawRequest')) {
        require_once 'cleantalk.class.php';
    }
    $request = array();
    $request['method_name'] = 'notice_validate_key';
    $request['auth_key'] = $api_key;
    $url = 'https://api.cleantalk.org';
    if (!function_exists('sendRawRequest')) {
        require_once 'cleantalk.class.php';
    }
    $result = sendRawRequest($url, $request);
    if ($result) {
        $result = json_decode($result, true);
        if (isset($result['valid']) && $result['valid'] == 0) {
            $key_valid = false;
            $ct_data['testing_failed'] = 1;
        }
    }
    if (!$result || !isset($result['valid'])) {
        $app_server_error = true;
        $ct_data['testing_failed'] = 1;
    }
    update_option('cleantalk_data', $ct_data);
    if ($key_valid) {
        // Removes cookie for server errors
        if ($app_server_error) {
            setcookie($ct_notice_online_label, '', 1, '/');
            // time 1 is exactly in past even clients time() is wrong
            unset($_COOKIE[$ct_notice_online_label]);
        } else {
            setcookie($ct_notice_online_label, (string) time(), strtotime("+14 days"), '/');
        }
        setcookie($ct_notice_trial_label, '0', strtotime("+{$trial_notice_showtime} minutes"), '/');
    } else {
        setcookie($ct_notice_online_label, 'BAD_KEY', 0, '/');
    }
}
/**
 * After options update
 * @return array
*/
function ct_update_option($option_name)
{
    global $show_ct_notice_online, $ct_notice_online_label, $ct_notice_trial_label, $trial_notice_showtime, $ct_account_status_check, $ct_options, $ct_server_timeout;
    if ($option_name !== 'cleantalk_settings') {
        return;
    }
    // Skip test call if the function executet during account status check
    if ($ct_account_status_check > 0 && time() - $ct_account_status_check < 5) {
        return;
    }
    $key_valid = true;
    $app_server_error = false;
    if (function_exists('curl_init') && function_exists('json_decode')) {
        $api_key = $ct_options['apikey'];
        if (isset($_POST['cleantalk_settings']['apikey'])) {
            $api_key = trim($_POST['cleantalk_settings']['apikey']);
        }
        if (!ct_valid_key($api_key)) {
            return null;
        }
        $url = 'https://cleantalk.org/app_notice';
        $server_timeout = $ct_server_timeout;
        $data['auth_key'] = $api_key;
        $data['param'] = 'notice_validate_key';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $server_timeout);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        // receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // resolve 'Expect: 100-continue' issue
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        if ($result) {
            $result = json_decode($result, true);
            if (isset($result['valid']) && $result['valid'] == 0) {
                $key_valid = false;
            }
        }
        if (!$result || !isset($result['valid'])) {
            $app_server_error = true;
        }
    }
    if ($key_valid) {
        // Removes cookie for server errors
        if ($app_server_error) {
            setcookie($ct_notice_online_label, '', 1, '/');
            // time 1 is exactly in past even clients time() is wrong
            unset($_COOKIE[$ct_notice_online_label]);
        } else {
            setcookie($ct_notice_online_label, (string) time(), strtotime("+14 days"), '/');
        }
        setcookie($ct_notice_trial_label, '0', strtotime("+{$trial_notice_showtime} minutes"), '/');
    } else {
        setcookie($ct_notice_online_label, 'BAD_KEY', 0, '/');
    }
}