/**
 * 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);
}
/**
 * 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);
}