/**
 * Check messages for external plugins
 * @return array with checking result;
 */
function ct_test_message($nickname, $email, $ip, $text)
{
    $checkjs = js_test_plugin('ct_checkjs', $_COOKIE, true);
    $post_info['comment_type'] = 'feedback_plugin_check';
    $post_info = json_encode($post_info);
    $ct_base_call_result = ct_base_call(array('message' => $text, 'example' => null, 'sender_email' => $email, 'sender_nickname' => $nickname, 'post_info' => $post_info, 'sender_info' => get_sender_info(), 'checkjs' => $checkjs));
    $ct_result = $ct_base_call_result['ct_result'];
    $result = array('allow' => $ct_result->allow, 'comment' => $ct_result->comment);
    return $result;
}
/**
 * General test for any post data
 */
function ct_contact_form_validate_postdata()
{
    global $pagenow, $cleantalk_executed, $cleantalk_url_exclusions;
    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);
    if ($_SERVER['REQUEST_METHOD'] != 'POST' || isset($_POST['log']) && isset($_POST['pwd']) && isset($pagenow) && $pagenow == 'wp-login.php' || isset($pagenow) && $pagenow == 'wp-login.php' && isset($_GET['action']) && $_GET['action'] == 'lostpassword' || ct_check_array_keys($_POST) || (isset($pagenow) && $pagenow == 'wp-cron.php' || 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 || check_url_exclusions()) {
        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;
}
/**
 * Inner function - Common part of request sending
 * @param array Array of parameters:
 *  'message' - string
 *  'example' - string
 *  'checkjs' - int
 *  'sender_email' - string
 *  'sender_nickname' - string
 *  'sender_info' - array
 *  'post_info' - string
 * @return array array('ct'=> Cleantalk, 'ct_result' => CleantalkResponse)
 */
function ct_base_call($params = array())
{
    global $wpdb, $ct_agent_version, $ct_formtime_label, $ct_options, $ct_data;
    require_once 'cleantalk.class.php';
    $submit_time = submit_time_test();
    $sender_info = get_sender_info();
    if (array_key_exists('sender_info', $params)) {
        $sender_info = array_merge($sender_info, (array) $params['sender_info']);
    }
    $sender_info = json_encode($sender_info);
    if ($sender_info === false) {
        $sender_info = '';
    }
    $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->message = $params['message'];
    $ct_request->example = $params['example'];
    $ct_request->sender_email = $params['sender_email'];
    $ct_request->sender_nickname = $params['sender_nickname'];
    $ct_request->sender_ip = $ct->ct_session_ip($_SERVER['REMOTE_ADDR']);
    $ct_request->agent = $ct_agent_version;
    $ct_request->sender_info = $sender_info;
    $ct_request->js_on = $params['checkjs'];
    $ct_request->submit_time = $submit_time;
    $ct_request->post_info = $params['post_info'];
    if (isset($ct_data['last_error_no'])) {
        $ct_request->last_error_no = $ct_data['last_error_no'];
        $ct_request->last_error_time = $ct_data['last_error_time'];
        $ct_request->last_error_text = $ct_data['last_error_text'];
    }
    $ct_result = $ct->isAllowMessage($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()));
    }
    // Restart submit form counter for failed requests
    if ($ct_result->allow == 0) {
        $_SESSION[$ct_formtime_label] = time();
        ct_add_event('no');
    } else {
        if (isset($ct_result->errno)) {
            if ($ct_result->errno == 1 || $ct_result->errno == 0) {
                $ct_data['last_error_no'] = $ct_result->errno;
                $ct_data['last_error_time'] = time();
                $ct_data['last_error_text'] = $ct_result->errstr;
                update_option('cleantalk_data', $ct_data);
            }
        }
        ct_add_event('yes');
    }
    return array('ct' => $ct, 'ct_result' => $ct_result);
}
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_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;
        }
    }
}
/**
 * Inner function - Common part of request sending
 * @param array Array of parameters:
 *  'message' - string
 *  'example' - string
 *  'checkjs' - int
 *  'sender_email' - string
 *  'sender_nickname' - string
 *  'sender_info' - array
 *  'post_info' - string
 * @return array array('ct'=> Cleantalk, 'ct_result' => CleantalkResponse)
 */
function ct_base_call($params = array())
{
    global $wpdb, $ct_agent_version, $ct_formtime_label, $ct_options, $ct_data;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    require_once 'cleantalk.class.php';
    $submit_time = submit_time_test();
    $sender_info = get_sender_info();
    if (array_key_exists('sender_info', $params)) {
        $sender_info = array_merge($sender_info, (array) $params['sender_info']);
    }
    $sender_info = json_encode($sender_info);
    if ($sender_info === false) {
        $sender_info = '';
    }
    $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->message = $params['message'];
    $ct_request->example = $params['example'];
    $ct_request->sender_email = $params['sender_email'];
    $ct_request->sender_nickname = $params['sender_nickname'];
    $ct_request->sender_ip = $ct->ct_session_ip($_SERVER['REMOTE_ADDR']);
    $ct_request->agent = $ct_agent_version;
    $ct_request->sender_info = $sender_info;
    $ct_request->js_on = $params['checkjs'];
    $ct_request->submit_time = $submit_time;
    $ct_request->post_info = $params['post_info'];
    if (isset($ct_data['last_error_no'])) {
        $ct_request->last_error_no = $ct_data['last_error_no'];
        $ct_request->last_error_time = $ct_data['last_error_time'];
        $ct_request->last_error_text = $ct_data['last_error_text'];
    }
    $ct_result = $ct->isAllowMessage($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 (@intval($ct_result->errno) != 0) {
        if ($params['checkjs'] != 1) {
            $ct_result->allow = 0;
            $ct_result->spam = 1;
            $ct_result->comment = __('Forbidden. Please, enable Javascript.', 'cleantalk');
        } else {
            $ct_result->allow = 1;
            $ct_result->comment = __('Allow', 'cleantalk');
        }
    }
    // Restart submit form counter for failed requests
    if ($ct_result->allow == 0) {
        ct_init_session();
        $_SESSION[$ct_formtime_label] = time();
        ct_add_event('no');
    } else {
        ct_add_event('yes');
        /*if($is_logged_in)
         	{
         		$user_cnt++;
         		update_user_meta( $user_id, 'cleantalk_messages_number', $user_cnt);
         	}*/
    }
    return array('ct' => $ct, 'ct_result' => $ct_result);
}