public function send_logs()
 {
     global $ct_options, $ct_data;
     $ct_options = ct_get_options();
     $ct_data = ct_get_data();
     if (isset($ct_options['spam_firewall'])) {
         $value = @intval($ct_options['spam_firewall']);
     } else {
         $value = 0;
     }
     if ($value == 1 && isset($ct_data['sfw_log'])) {
         $sfw_log = $ct_data['sfw_log'];
         $data = array();
         foreach ($sfw_log as $key => $value) {
             $data[] = array($key, $value['all'], $value['allow'], $value['datetime']);
         }
         $qdata = array('data' => json_encode($data), 'rows' => count($data), 'timestamp' => time());
         if (!function_exists('sendRawRequest')) {
             require_once 'cleantalk.class.php';
         }
         $result = sendRawRequest('https://api.cleantalk.org/?method_name=sfw_logs&auth_key=' . $ct_options['apikey'], $qdata);
         $result = json_decode($result);
         if (isset($result->data) && isset($result->data->rows)) {
             if ($result->data->rows == count($data)) {
                 $ct_data['sfw_log'] = array();
                 update_option('cleantalk_data', $ct_data);
             }
         }
     }
 }
/**
 * Test S2member registration
 * @return array with errors 
 */
function ct_s2member_registration_test()
{
    global $ct_agent_version, $ct_post_data_label, $ct_post_data_authnet_label, $ct_formtime_label, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    if ($ct_options['registrations_test'] == 0) {
        return null;
    }
    $submit_time = submit_time_test();
    $checkjs = js_test('ct_checkjs', $_COOKIE, true);
    require_once 'cleantalk.class.php';
    $sender_info = get_sender_info();
    $sender_info = json_encode($sender_info);
    if ($sender_info === false) {
        $sender_info = '';
    }
    $sender_email = null;
    if (isset($_POST[$ct_post_data_label]['email'])) {
        $sender_email = $_POST[$ct_post_data_label]['email'];
    }
    if (isset($_POST[$ct_post_data_authnet_label]['email'])) {
        $sender_email = $_POST[$ct_post_data_authnet_label]['email'];
    }
    $sender_nickname = null;
    if (isset($_POST[$ct_post_data_label]['username'])) {
        $sender_nickname = $_POST[$ct_post_data_label]['username'];
    }
    if (isset($_POST[$ct_post_data_authnet_label]['username'])) {
        $sender_nickname = $_POST[$ct_post_data_authnet_label]['username'];
    }
    $config = get_option('cleantalk_server');
    $ct = new Cleantalk();
    $ct->work_url = $config['ct_work_url'];
    $ct->server_url = $ct_options['server'];
    $ct->server_ttl = $config['ct_server_ttl'];
    $ct->server_changed = $config['ct_server_changed'];
    $ct->ssl_on = $ct_options['ssl_on'];
    $ct_request = new CleantalkRequest();
    $ct_request->auth_key = $ct_options['apikey'];
    $ct_request->sender_email = $sender_email;
    $ct_request->sender_ip = $ct->ct_session_ip($_SERVER['REMOTE_ADDR']);
    $ct_request->sender_nickname = $sender_nickname;
    $ct_request->agent = $ct_agent_version;
    $ct_request->sender_info = $sender_info;
    $ct_request->js_on = $checkjs;
    $ct_request->submit_time = $submit_time;
    $ct_result = $ct->isAllowUser($ct_request);
    if ($ct->server_change) {
        update_option('cleantalk_server', array('ct_work_url' => $ct->work_url, 'ct_server_ttl' => $ct->server_ttl, 'ct_server_changed' => time()));
    }
    if ($ct_result->errno != 0) {
        return false;
    }
    // Restart submit form counter for failed requests
    if ($ct_result->allow == 0) {
        $_SESSION[$ct_formtime_label] = time();
    }
    if ($ct_result->allow == 0) {
        ct_die_extended($ct_result->comment);
    }
    return true;
}
function ct_inject_nocache_script($html)
{
    global $test_external_forms, $cleantalk_plugin_version, $ct_options;
    $ct_options = ct_get_options();
    if (substr($ct_options['apikey'], 0, 1) == 'y') {
        $ct_info_flag = "var ct_info_flag=true;\n";
    } else {
        $ct_info_flag = "var ct_info_flag=false;\n";
    }
    if (!is_admin() && stripos($html, "</body") !== false) {
        //$ct_replace.="\n<script type='text/javascript'>var ajaxurl = '".admin_url('admin-ajax.php')."';\n  $ct_info_flag </script>\n";
        $ct_replace = "<script async type='text/javascript' src='" . plugins_url('/inc/cleantalk_nocache.js', __FILE__) . "?random=" . $cleantalk_plugin_version . "'></script>\n";
        if ($test_external_forms) {
            $ct_replace .= "\n<script type='text/javascript'>var ct_blog_home = '" . get_home_url() . "';</script>\n";
            $ct_replace .= "<script async type='text/javascript' src='" . plugins_url('/inc/cleantalk_external.js', __FILE__) . "?random=" . $cleantalk_plugin_version . "'></script>\n";
        }
        //$html=str_ireplace("</body",$ct_replace."</body",$html);
        $html = substr_replace($html, $ct_replace . "</body", strripos($html, "</body"), 6);
    }
    if (!is_admin() && preg_match("#<head[^>]*>#i", $html) == 1) {
        $ct_replace = "\n<script type='text/javascript'>\nvar ct_ajaxurl = '" . admin_url('admin-ajax.php') . "';\n {$ct_info_flag} </script>\n";
        $html = preg_replace("(<head[^>]*>)", "\$0" . $ct_replace, $html, 1);
    }
    return $html;
}
/**
 * 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, '/');
    }
}
/**
 * On the scheduled action hook, run the function.
 */
function ct_do_this_hourly()
{
    global $ct_options, $ct_data;
    // do something every hour
    if (!isset($ct_options)) {
        $ct_options = ct_get_options();
    }
    if (!isset($ct_data)) {
        $ct_data = ct_get_data();
    }
    delete_spam_comments();
    ct_send_feedback();
}
function ct_ajax_check_comments()
{
    check_ajax_referer('ct_secret_nonce', 'security');
    global $ct_options;
    $ct_options = ct_get_options();
    $args_unchecked = array('meta_query' => array(array('key' => 'ct_checked', 'value' => '1', 'compare' => 'NOT EXISTS')), 'number' => 500, 'status' => 'all');
    $u = get_comments($args_unchecked);
    $u = array_slice($u, 0, 500);
    $u = array_values($u);
    if (sizeof($u) > 0) {
        //print_r($u);
        //die();
        $data = array();
        for ($i = 0; $i < sizeof($u); $i++) {
            $data[] = $u[$i]->comment_author_IP;
            $data[] = $u[$i]->comment_author_email;
        }
        $data = implode(',', $data);
        $request = "data={$data}";
        $opts = array('http' => array('method' => "POST", 'content' => $request));
        $context = stream_context_create($opts);
        $result = @file_get_contents("https://api.cleantalk.org/?method_name=spam_check&auth_key=" . $ct_options['apikey'], 0, $context);
        $result = json_decode($result);
        if (isset($result->error_message)) {
            print $result->error_message;
        } else {
            for ($i = 0; $i < sizeof($u); $i++) {
                add_comment_meta($u[$i]->comment_ID, 'ct_checked', date("Y-m-d H:m:s"), true);
                $uip = $u[$i]->comment_author_IP;
                if (empty($uip)) {
                    continue;
                }
                $uim = $u[$i]->comment_author_email;
                if (empty($uim)) {
                    continue;
                }
                if (isset($result->data->{$uip}) && $result->data->{$uip}->appears == 1 || isset($result->data->{$uim}) && $result->data->{$uim}->appears == 1) {
                    add_comment_meta($u[$i]->comment_ID, 'ct_marked_as_spam', '1', true);
                }
            }
            print 1;
        }
    } else {
        print 0;
    }
    die;
}
function ct_ajax_hook()
{
    require_once CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-public.php';
    global $ct_agent_version, $ct_checkjs_register_form, $ct_session_request_id_label, $ct_session_register_ok_label, $bp, $ct_signup_done, $ct_formtime_label, $ct_negative_comment, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    $sender_email = null;
    $message = '';
    $nickname = null;
    if (isset($_POST['user_login'])) {
        $nickname = $_POST['user_login'];
    } else {
        $nickname = '';
    }
    if (isset($_POST['cscf']['confirm-email'])) {
        $tmp = $_POST['cscf']['confirm-email'];
        $_POST['cscf']['confirm-email'] = 1;
    }
    if (($_POST['action'] == 'request_appointment' || $_POST['action'] == 'send_message') && isset($_POST['target'])) {
        $tmp = $_POST['target'];
        $_POST['target'] = 1;
    }
    ct_get_fields($sender_email, $message, $_POST);
    if (isset($_POST['cscf']['confirm-email'])) {
        $_POST['cscf']['confirm-email'] = $tmp;
    }
    if (($_POST['action'] == 'request_appointment' || $_POST['action'] == 'send_message') && isset($_POST['target'])) {
        $_POST['target'] = $tmp;
    }
    if ($sender_email != null) {
        $checkjs = js_test('ct_checkjs', $_COOKIE, true);
        $submit_time = submit_time_test();
        $sender_info = get_sender_info();
        $sender_info['post_checkjs_passed'] = $checkjs;
        $sender_info = json_encode($sender_info);
        if ($sender_info === false) {
            $sender_info = '';
        }
        $ct_base_call_result = ct_base_call(array('message' => $message, 'example' => null, 'sender_email' => $sender_email, 'sender_nickname' => $nickname, 'sender_info' => $sender_info, 'post_info' => null, 'checkjs' => $checkjs));
        $ct = $ct_base_call_result['ct'];
        $ct_result = $ct_base_call_result['ct_result'];
        if ($ct_result->allow == 0) {
            if ($_POST['action'] == 'wpuf_submit_register') {
                $result = array('success' => false, 'error' => $ct_result->comment);
                @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
                print json_encode($result);
                die;
            } else {
                if ($_POST['action'] == 'mymail_form_submit') {
                    $result = array('success' => false, 'html' => $ct_result->comment);
                    @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
                    print json_encode($result);
                    die;
                } else {
                    if ($_POST['action'] == 'wysija_ajax' && $_POST['task'] != 'send_preview') {
                        $result = array('result' => false, 'msgs' => array('updated' => array($ct_result->comment)));
                        //@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
                        print $_GET['callback'] . '(' . json_encode($result) . ');';
                        die;
                    } else {
                        if ($_POST['action'] == 'cs_registration_validation') {
                            $result = array("type" => "error", "message" => $ct_result->comment);
                            print json_encode($result);
                            die;
                        } else {
                            if ($_POST['action'] == 'request_appointment' || $_POST['action'] == 'send_message') {
                                print $ct_result->comment;
                                die;
                            } else {
                                if ($_POST['action'] == 'zn_do_login') {
                                    print '<div id="login_error">' . $ct_result->comment . '</div>';
                                    die;
                                } else {
                                    if ($_POST['action'] == 'vfb_submit') {
                                        $result = array('result' => false, 'message' => $ct_result->comment);
                                        @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
                                        print json_encode($result);
                                        die;
                                    } else {
                                        if ($_POST['action'] == 'cscf-submitform') {
                                            $result = array('sent' => true, 'valid' => false, 'errorlist' => array('name' => $ct_result->comment));
                                            print json_encode($result);
                                            die;
                                        } else {
                                            if ($_POST['action'] == 'woocommerce_checkout') {
                                                print $ct_result->comment;
                                                die;
                                            } else {
                                                if ($_POST['action'] == 'frm_entries_create') {
                                                    $result = array('112' => $ct_result->comment);
                                                    print json_encode($result);
                                                    die;
                                                } else {
                                                    if (isset($_POST['cma-action']) && $_POST['cma-action'] == 'add') {
                                                        $result = array('success' => 0, 'thread_id' => null, 'messages' => array($ct_result->comment));
                                                        print json_encode($result);
                                                        die;
                                                    } else {
                                                        if ($_POST['action'] == 'td_mod_register') {
                                                            print json_encode(array('register', 0, $ct_result->comment));
                                                            die;
                                                        } else {
                                                            if ($_POST['action'] == 'tmpl_ajax_check_user_email') {
                                                                print "17,email";
                                                                die;
                                                            } else {
                                                                if ($_POST['action'] == 'tevolution_submit_from_preview' || $_POST['action'] == 'submit_form_recaptcha_validation') {
                                                                    print $ct_result->comment;
                                                                    die;
                                                                } else {
                                                                    if ($_POST['action'] == 'ninja_forms_ajax_submit') {
                                                                        print '{"form_id":' . $_POST['_form_id'] . ',"errors":false,"success":{"success_msg-Success":"' . $ct_result->comment . '"}}';
                                                                        die;
                                                                    } else {
                                                                        print $ct_result->comment;
                                                                        die;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
function ct_ajax_check_users()
{
    check_ajax_referer('ct_secret_nonce', 'security');
    global $ct_options;
    $ct_options = ct_get_options();
    $args_unchecked = array('meta_query' => array('relation' => 'AND', array('key' => 'ct_checked', 'value' => '1', 'compare' => 'NOT EXISTS'), array('key' => 'ct_hash', 'value' => '1', 'compare' => 'NOT EXISTS')), 'number' => 500);
    $u = get_users($args_unchecked);
    //$u=array_slice($u,0,10);
    if (sizeof($u) > 0) {
        $data = array();
        for ($i = 0; $i < sizeof($u); $i++) {
            $user_meta = get_user_meta($u[$i]->ID, 'session_tokens', true);
            if (is_array($user_meta)) {
                $user_meta = array_values($user_meta);
            }
            if (@isset($user_meta[0]['ip'])) {
                $data[] = $user_meta[0]['ip'];
            } else {
                $data[] = '127.0.0.1';
            }
            $data[] = $u[$i]->data->user_email;
        }
        $data = implode(',', $data);
        $request = "data={$data}";
        $opts = array('http' => array('method' => "POST", 'content' => $request));
        $context = stream_context_create($opts);
        $result = @file_get_contents("https://api.cleantalk.org/?method_name=spam_check&auth_key=" . $ct_options['apikey'], 0, $context);
        $result = json_decode($result);
        if (isset($result->error_message)) {
            print $result->error_message;
        } else {
            for ($i = 0; $i < sizeof($u); $i++) {
                update_user_meta($u[$i]->ID, 'ct_checked', date("Y-m-d H:m:s"), true);
                $user_meta = get_user_meta($u[$i]->ID, 'session_tokens', true);
                if (is_array($user_meta)) {
                    $user_meta = array_values($user_meta);
                }
                if (@isset($user_meta[0]['ip'])) {
                    $uip = $user_meta[0]['ip'];
                } else {
                    $uip = '127.0.0.1';
                }
                //if($uip=='127.0.0.1')continue;
                $uim = $u[$i]->data->user_email;
                if (empty($uim)) {
                    continue;
                }
                //print "uip: $uip, uim: $uim\n";
                if ($result->data->{$uip}->appears == 1 || $result->data->{$uim}->appears == 1) {
                    update_user_meta($u[$i]->ID, 'ct_marked_as_spam', '1', true);
                }
            }
            print 1;
        }
    } else {
        print 0;
    }
    die;
}
/**
 * Delete old spam comments 
 * @return null 
 */
function delete_spam_comments()
{
    global $pagenow, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    if ($ct_options['remove_old_spam'] == 1) {
        $last_comments = get_comments(array('status' => 'spam', 'number' => 1000, 'order' => 'ASC'));
        foreach ($last_comments as $c) {
            if (time() - strtotime($c->comment_date_gmt) > 86400 * $ct_options['spam_store_days']) {
                // Force deletion old spam comments
                wp_delete_comment($c->comment_ID, true);
            }
        }
    }
    return null;
}
function ct_theme_submit()
{
    require_once CLEANTALK_PLUGIN_DIR . 'cleantalk-public.php';
    global $ct_agent_version, $ct_checkjs_register_form, $ct_session_request_id_label, $ct_session_register_ok_label, $bp, $ct_signup_done, $ct_formtime_label, $ct_negative_comment, $ct_options, $ct_data;
    $ct_data = ct_get_data();
    $ct_options = ct_get_options();
    $sender_email = null;
    $message = '';
    if (isset($_POST['cscf']['confirm-email'])) {
        $tmp = $_POST['cscf']['confirm-email'];
        $_POST['cscf']['confirm-email'] = 1;
    }
    ct_get_fields($sender_email, $message, $_POST);
    if (isset($_POST['cscf']['confirm-email'])) {
        $_POST['cscf']['confirm-email'] = $tmp;
    }
    if ($sender_email != null) {
        $checkjs = js_test('ct_checkjs', $_COOKIE, true);
        $submit_time = submit_time_test();
        $sender_info = get_sender_info();
        $sender_info['post_checkjs_passed'] = $checkjs;
        $sender_info = json_encode($sender_info);
        if ($sender_info === false) {
            $sender_info = '';
        }
        $ct_base_call_result = ct_base_call(array('message' => $message, 'example' => null, 'sender_email' => $sender_email, 'sender_nickname' => null, 'sender_info' => $sender_info, 'post_info' => null, 'checkjs' => $checkjs));
        $ct = $ct_base_call_result['ct'];
        $ct_result = $ct_base_call_result['ct_result'];
        if ($ct_result->allow == 0) {
            $result = array('sent' => true, 'valid' => false, 'errorlist' => array('name' => $ct_result->comment));
            print json_encode($result);
            die;
        }
    }
}
/**
 * Admin callback function - Displays inputs of 'apikey' plugin parameter
 */
function ct_input_apikey()
{
    global $ct_options, $ct_data, $ct_notice_online_label;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    echo "<script src='" . plugins_url('cleantalk-admin.js', __FILE__) . "'></script>\n";
    $value = $ct_options['apikey'];
    $def_value = '';
    echo "<input id='cleantalk_apikey' name='cleantalk_settings[apikey]' size='20' type='text' value='{$value}' style=\"font-size: 14pt;\"/>";
    if (ct_valid_key($value) === false) {
        echo "<a target='__blank' style='margin-left: 10px' href='https://cleantalk.org/register?platform=wordpress&email=" . urlencode(get_option('admin_email')) . "&website=" . urlencode(parse_url(get_option('siteurl'), PHP_URL_HOST)) . "'>" . __('Click here to get access key manually', 'cleantalk') . "</a>";
        if (function_exists('curl_init') && function_exists('json_decode')) {
            echo '<br /><br /><input name="get_apikey_auto" type="submit" value="' . __('Get access key automatically', 'cleantalk') . '"  />';
            admin_addDescriptionsFields(sprintf(__('Admin e-mail (%s) will be used for registration', 'cleantalk'), get_option('admin_email')));
            admin_addDescriptionsFields(sprintf('<a target="__blank" style="color:#BBB;" href="https://cleantalk.org/publicoffer">%s</a>', __('License agreement', 'cleantalk')));
        }
    } else {
        if (isset($_COOKIE[$ct_notice_online_label]) && $_COOKIE[$ct_notice_online_label] > 0) {
            echo '&nbsp;&nbsp;<span style="text-decoration: underline;">The key accepted!</span>&nbsp;';
        }
        echo "<br /><br /><a target='__blank' href='https://cleantalk.org/my?user_token=" . @$ct_data['user_token'] . "'>" . __('Click here to get anti-spam statistics', 'cleantalk') . "</a>";
    }
}
/**
 * Admin action 'admin_init' - Add the admin settings and such
 */
function ct_admin_init()
{
    global $show_ct_notice_trial, $ct_notice_trial_label, $show_ct_notice_online, $ct_notice_online_label, $trial_notice_showtime, $ct_plugin_name, $ct_options, $trial_notice_check_timeout, $account_notice_check_timeout, $ct_user_token_label, $ct_account_status_check;
    $ct_options = ct_get_options();
    $show_ct_notice_trial = false;
    if (isset($_COOKIE[$ct_notice_trial_label])) {
        if ($_COOKIE[$ct_notice_trial_label] == 1) {
            $show_ct_notice_trial = true;
        }
    }
    if (time() > $ct_options['next_account_status_check']) {
        $result = false;
        if (function_exists('curl_init') && function_exists('json_decode') && ct_valid_key($ct_options['apikey'])) {
            $url = 'https://cleantalk.org/app_notice';
            $server_timeout = 2;
            $data['auth_key'] = $ct_options['apikey'];
            $data['param'] = 'notice_paid_till';
            $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);
            $notice_check_timeout = $trial_notice_check_timeout;
            if ($result) {
                $result = json_decode($result, true);
                if (isset($result['show_notice'])) {
                    if ($result['show_notice'] == 1 && isset($result['trial']) && $result['trial'] == 1) {
                        $show_ct_notice_trial = true;
                    }
                    if ($result['show_notice'] == 0) {
                        $notice_check_timeout = $account_notice_check_timeout;
                    }
                }
                if (isset($result['user_token'])) {
                    $ct_options['user_token'] = $result['user_token'];
                }
            }
            // Save next status request time
            $ct_options['next_account_status_check'] = strtotime("+{$notice_check_timeout} hours", time());
            $ct_account_status_check = time();
            update_option('cleantalk_settings', $ct_options);
        }
        if ($result) {
            setcookie($ct_notice_trial_label, (string) $show_ct_notice_trial, strtotime("+{$trial_notice_showtime} minutes"), '/');
        }
    }
    $show_ct_notice_online = '';
    if (isset($_COOKIE[$ct_notice_online_label])) {
        if ($_COOKIE[$ct_notice_online_label] === 'BAD_KEY') {
            $show_ct_notice_online = 'N';
        } else {
            if (time() - $_COOKIE[$ct_notice_online_label] <= 5) {
                $show_ct_notice_online = 'Y';
            }
        }
    }
    ct_init_session();
    register_setting('cleantalk_settings', 'cleantalk_settings', 'ct_settings_validate');
    add_settings_section('cleantalk_settings_main', __($ct_plugin_name, 'cleantalk'), 'ct_section_settings_main', 'cleantalk');
    add_settings_section('cleantalk_settings_anti_spam', __('Anti-spam settings', 'cleantalk'), 'ct_section_settings_anti_spam', 'cleantalk');
    add_settings_field('cleantalk_apikey', __('Access key', 'cleantalk'), 'ct_input_apikey', 'cleantalk', 'cleantalk_settings_main');
    add_settings_field('cleantalk_remove_old_spam', __('Automatically delete spam comments', 'cleantalk'), 'ct_input_remove_old_spam', 'cleantalk', 'cleantalk_settings_main');
    add_settings_field('cleantalk_registrations_test', __('Registration forms', 'cleantalk'), 'ct_input_registrations_test', 'cleantalk', 'cleantalk_settings_anti_spam');
    add_settings_field('cleantalk_comments_test', __('Comments form', 'cleantalk'), 'ct_input_comments_test', 'cleantalk', 'cleantalk_settings_anti_spam');
    add_settings_field('cleantalk_contact_forms_test', __('Contact forms', 'cleantalk'), 'ct_input_contact_forms_test', 'cleantalk', 'cleantalk_settings_anti_spam');
    add_settings_field('cleantalk_general_contact_forms_test', __('Custom contact forms', 'cleantalk'), 'ct_input_general_contact_forms_test', 'cleantalk', 'cleantalk_settings_anti_spam');
}
/**
 * General test for any post data
 */
function ct_contact_form_validate_postdata()
{
    global $pagenow, $cleantalk_executed, $cleantalk_url_exclusions, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    if ($cleantalk_executed) {
        return null;
    }
    if (defined('DOING_AJAX') && DOING_AJAX) {
        return null;
    }
    if (isset($cleantalk_url_exclusions)) {
        $ct_cnt = sizeof($cleantalk_url_exclusions);
    } else {
        $ct_cnt = 0;
    }
    //@header("CtExclusions: ".$ct_cnt);
    cleantalk_debug("CtExclusions", $ct_cnt);
    if (@sizeof($_POST) == 0 || isset($_POST['signup_username']) && isset($_POST['signup_email']) && isset($_POST['signup_password']) || isset($pagenow) && $pagenow == 'wp-login.php' || isset($pagenow) && $pagenow == 'wp-login.php' && isset($_GET['action']) && $_GET['action'] == 'lostpassword' || strpos($_SERVER['REQUEST_URI'], '/checkout/') !== false || strpos($_SERVER['REQUEST_URI'], '/wp-admin/') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-comments-post.php') !== false || @strpos($_SERVER['HTTP_REFERER'], '/wp-admin/') !== false || strpos($_SERVER['REQUEST_URI'], '/login/') !== false || isset($_GET['ptype']) && $_GET['ptype'] == 'login' || check_url_exclusions() || ct_check_array_keys($_POST) || isset($_POST['ct_checkjs_register_form']) || isset($_POST['signup_username']) && isset($_POST['signup_password_confirm']) && isset($_POST['signup_submit']) || @intval($ct_options['general_contact_forms_test']) == 0 || isset($_POST['bbp_topic_content']) || isset($_POST['bbp_reply_content']) || isset($_POST['fscf_submitted']) || strpos($_SERVER['REQUEST_URI'], '/wc-api/') !== false) {
        return null;
    }
    $_POST = ct_filter_array($_POST);
    $checkjs = js_test('ct_checkjs', $_COOKIE, true);
    $post_info['comment_type'] = 'feedback_general_postdata';
    $post_info = json_encode($post_info);
    if ($post_info === false) {
        $post_info = '';
    }
    $message = '';
    @ct_get_fields_any_postdata($message, $_POST);
    if (strlen(trim($message)) < 10) {
        return null;
    }
    $skip_params = array('ipn_track_id', 'txn_type', 'payment_status');
    foreach ($skip_params as $key => $value) {
        if (@array_key_exists($value, $_GET) || @array_key_exists($value, $_POST)) {
            return null;
        }
    }
    $ct_base_call_result = ct_base_call(array('message' => $message, 'example' => null, 'sender_email' => '', 'sender_nickname' => '', 'post_info' => $post_info, 'sender_info' => get_sender_info(), 'checkjs' => $checkjs));
    $cleantalk_executed = true;
    $ct = $ct_base_call_result['ct'];
    $ct_result = $ct_base_call_result['ct_result'];
    if ($ct_result->allow == 0) {
        if (!(defined('DOING_AJAX') && DOING_AJAX)) {
            global $ct_comment;
            $ct_comment = $ct_result->comment;
            if (isset($_POST['cma-action']) && $_POST['cma-action'] == 'add') {
                $result = array('success' => 0, 'thread_id' => null, 'messages' => array($ct_result->comment));
                header("Content-Type: application/json");
                print json_encode($result);
                die;
            } else {
                ct_die(null, null);
            }
        } else {
            echo $ct_result->comment;
        }
        exit;
    }
    return null;
}
/**
 * Test users registration
 * @return array with errors 
 */
function ct_registration_errors($errors, $sanitized_user_login = null, $user_email = null)
{
    global $ct_agent_version, $ct_checkjs_register_form, $ct_session_request_id_label, $ct_session_register_ok_label, $bp, $ct_signup_done, $ct_formtime_label, $ct_negative_comment, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    // Go out if a registrered user action
    if (ct_is_user_enable() === false) {
        return $errors;
    }
    if ($ct_options['registrations_test'] == 0) {
        return $errors;
    }
    //
    // The function already executed
    // It happens when used ct_register_post();
    //
    if ($ct_signup_done && is_object($errors) && count($errors->errors) > 0) {
        return $errors;
    }
    //
    // BuddyPress actions
    //
    $buddypress = false;
    if ($sanitized_user_login === null && isset($_POST['signup_username'])) {
        $sanitized_user_login = $_POST['signup_username'];
        $buddypress = true;
    }
    if ($user_email === null && isset($_POST['signup_email'])) {
        $user_email = $_POST['signup_email'];
        $buddypress = true;
    }
    //
    // Break tests because we already have servers response
    //
    if ($buddypress && $ct_signup_done) {
        if ($ct_negative_comment) {
            $bp->signup->errors['signup_username'] = $ct_negative_comment;
        }
        return $errors;
    }
    $submit_time = submit_time_test();
    $sender_info = get_sender_info();
    $checkjs = 0;
    $checkjs = js_test($ct_checkjs_register_form, $_POST, true);
    $sender_info['post_checkjs_passed'] = $checkjs;
    //
    // This hack can be helpfull when plugin uses with untested themes&signups plugins.
    //
    if ($checkjs == 0) {
        $checkjs = js_test('ct_checkjs', $_COOKIE, true);
        $sender_info['cookie_checkjs_passed'] = $checkjs;
    }
    $sender_info = json_encode($sender_info);
    if ($sender_info === false) {
        $sender_info = '';
    }
    require_once 'cleantalk.class.php';
    $config = get_option('cleantalk_server');
    $ct = new Cleantalk();
    $ct->work_url = $config['ct_work_url'];
    $ct->server_url = $ct_options['server'];
    $ct->server_ttl = $config['ct_server_ttl'];
    $ct->server_changed = $config['ct_server_changed'];
    $ct->ssl_on = $ct_options['ssl_on'];
    $ct_request = new CleantalkRequest();
    $ct_request->auth_key = $ct_options['apikey'];
    $ct_request->sender_email = $user_email;
    $ct_request->sender_ip = $ct->ct_session_ip($_SERVER['REMOTE_ADDR']);
    $ct_request->sender_nickname = $sanitized_user_login;
    $ct_request->agent = $ct_agent_version;
    $ct_request->sender_info = $sender_info;
    $ct_request->js_on = $checkjs;
    $ct_request->submit_time = $submit_time;
    $ct_result = $ct->isAllowUser($ct_request);
    if ($ct->server_change) {
        update_option('cleantalk_server', array('ct_work_url' => $ct->work_url, 'ct_server_ttl' => $ct->server_ttl, 'ct_server_changed' => time()));
    }
    $ct_signup_done = true;
    if ($ct_result->errno != 0 && $ct_options['notice_api_errors']) {
        ct_send_error_notice($ct_result->comment);
        return $errors;
    }
    if ($ct_result->errno != 0 && $checkjs == 0) {
        $ct_result->allow = 0;
    }
    if ($ct_result->inactive != 0) {
        ct_send_error_notice($ct_result->comment);
        return $errors;
    }
    if ($ct_result->allow == 0) {
        // Restart submit form counter for failed requests
        $_SESSION[$ct_formtime_label] = time();
        if ($buddypress === true) {
            $bp->signup->errors['signup_username'] = $ct_result->comment;
        } else {
            if (is_wp_error($errors)) {
                $errors->add('ct_error', $ct_result->comment);
            }
            $ct_negative_comment = $ct_result->comment;
        }
    } else {
        if ($ct_result->id !== null) {
            $_SESSION[$ct_session_request_id_label] = $ct_result->id;
            $_SESSION[$ct_session_register_ok_label] = $ct_result->id;
        }
    }
    return $errors;
}
$cleantalk_executed = false;
if (defined('CLEANTALK_AJAX_USE_BUFFER')) {
    $cleantalk_use_buffer = CLEANTALK_AJAX_USE_BUFFER;
} else {
    $cleantalk_use_buffer = true;
}
if (defined('CLEANTALK_AJAX_USE_FOOTER_HEADER')) {
    $cleantalk_use_footer_header = CLEANTALK_AJAX_USE_FOOTER_HEADER;
} else {
    $cleantalk_use_footer_header = true;
}
if (!defined('CLEANTALK_PLUGIN_DIR')) {
    define('CLEANTALK_PLUGIN_DIR', plugin_dir_path(__FILE__));
    global $ct_options, $ct_data;
    require_once CLEANTALK_PLUGIN_DIR . 'cleantalk-common.php';
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    $jigo_version = @get_option('jigoshop_db_version', '');
    if ($jigo_version == '') {
        ct_init_session();
    }
    if (!isset($_SESSION['ct_redirects'])) {
        $_SESSION['ct_redirects'] = 0;
    }
    if (isset($ct_options['check_external'])) {
        if (@intval($ct_options['check_external']) == 1) {
            $test_external_forms = true;
        } else {
            $test_external_forms = false;
        }
    } else {