/**
 * Init functions 
 * @return 	mixed[] Array of options
 */
function ct_init()
{
    global $ct_wplp_result_label, $ct_jp_comments, $ct_post_data_label, $ct_post_data_authnet_label, $ct_formtime_label, $ct_direct_post, $ct_options, $ct_data, $ct_check_post_result, $test_external_forms;
    $ct_options = ct_get_options();
    $ct_data = ct_get_data();
    //fix for EPM registration form
    if (isset($_POST) && isset($_POST['reg_email']) && shortcode_exists('epm_registration_form')) {
        unset($_POST['ct_checkjs_register_form']);
    }
    ct_init_session();
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (is_array($_SESSION) && !array_key_exists($ct_formtime_label, $_SESSION) && session_id() != '') {
            $ct_direct_post = 1;
        }
    } else {
        /*if(isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'],'preview')!==false)
         	{
         		//do nothing
         	}
         	else
         	{*/
        $_SESSION[$ct_formtime_label] = time();
        //}
    }
    if ($test_external_forms && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['cleantalk_hidden_method']) && isset($_POST['cleantalk_hidden_action'])) {
        $action = htmlspecialchars($_POST['cleantalk_hidden_action']);
        $method = htmlspecialchars($_POST['cleantalk_hidden_method']);
        unset($_POST['cleantalk_hidden_action']);
        unset($_POST['cleantalk_hidden_method']);
        ct_contact_form_validate();
        print "<html><body><form method='{$method}' action='{$action}'>";
        ct_print_form($_POST, '');
        print "</form><center>Redirecting to " . $action . "... Anti-spam by CleanTalk.</center></body></html>";
        print "<script>document.forms[0].submit();</script>";
        die;
    }
    if (isset($ct_options['general_postdata_test'])) {
        $ct_general_postdata_test = @intval($ct_options['general_postdata_test']);
        //hook for Anonymous Post
        add_action('template_redirect', 'ct_contact_form_validate_postdata', 1);
    } else {
        $ct_general_postdata_test = 0;
    }
    if (isset($ct_options['general_contact_forms_test']) && $ct_options['general_contact_forms_test'] == 1) {
        add_action('CMA_custom_post_type_nav', 'ct_contact_form_validate_postdata', 1);
        add_action('template_redirect', 'ct_contact_form_validate', 1);
        /*if(isset($_GET['ait-action'])&&$_GET['ait-action']=='register')
        		{
        			$tmp=$_POST['redirect_to'];
        			unset($_POST['redirect_to']);
        			ct_contact_form_validate();
        			$_POST['redirect_to']=$tmp;
        		}*/
    }
    if ($ct_general_postdata_test == 1) {
        add_action('CMA_custom_post_type_nav', 'ct_contact_form_validate_postdata', 1);
    }
    //add_action('wp_footer','ct_ajaxurl');
    // Fast Secure contact form
    if (defined('FSCF_VERSION')) {
        add_filter('si_contact_display_after_fields', 'ct_si_contact_display_after_fields');
        add_filter('si_contact_form_validate', 'ct_si_contact_form_validate');
    }
    // WooCoomerse signups
    if (class_exists('WooCommerce')) {
        add_filter('woocommerce_register_post', 'ct_register_post', 1, 3);
    }
    // JetPack Contact form
    $jetpack_active_modules = false;
    if (defined('JETPACK__VERSION')) {
        add_filter('grunion_contact_form_field_html', 'ct_grunion_contact_form_field_html', 10, 2);
        if (JETPACK__VERSION == '3.4-beta') {
            add_filter('contact_form_is_spam', 'ct_contact_form_is_spam');
        } else {
            if (JETPACK__VERSION == '3.4-beta2' || JETPACK__VERSION >= '3.4') {
                add_filter('jetpack_contact_form_is_spam', 'ct_contact_form_is_spam_jetpack', 1, 2);
            } else {
                add_filter('contact_form_is_spam', 'ct_contact_form_is_spam');
            }
        }
        $jetpack_active_modules = get_option('jetpack_active_modules');
        if (class_exists('Jetpack', false) && $jetpack_active_modules && in_array('comments', $jetpack_active_modules)) {
            $ct_jp_comments = true;
        }
    }
    // Contact Form7
    if (defined('WPCF7_VERSION')) {
        add_filter('wpcf7_form_elements', 'ct_wpcf7_form_elements');
        if (WPCF7_VERSION >= '3.0.0') {
            add_filter('wpcf7_spam', 'ct_wpcf7_spam');
        } else {
            add_filter('wpcf7_acceptance', 'ct_wpcf7_spam');
        }
    }
    // Formidable
    if (class_exists('FrmSettings')) {
        add_action('frm_validate_entry', 'ct_frm_validate_entry', 20, 2);
        add_action('frm_entries_footer_scripts', 'ct_frm_entries_footer_scripts', 20, 2);
    }
    // BuddyPress
    if (class_exists('BuddyPress')) {
        add_action('bp_before_registration_submit_buttons', 'ct_register_form', 1);
        add_filter('bp_signup_validate', 'ct_registration_errors', 1);
    }
    // bbPress
    if (class_exists('bbPress')) {
        add_filter('bbp_new_topic_pre_content', 'ct_bbp_new_pre_content', 1);
        add_filter('bbp_new_reply_pre_content', 'ct_bbp_new_pre_content', 1);
        add_action('bbp_theme_before_topic_form_content', 'ct_comment_form');
        add_action('bbp_theme_before_reply_form_content', 'ct_comment_form');
    }
    add_action('comment_form', 'ct_comment_form');
    //intercept WordPress Landing Pages POST
    if (defined('LANDINGPAGES_CURRENT_VERSION') && !empty($_POST)) {
        if (array_key_exists('action', $_POST) && $_POST['action'] === 'inbound_store_lead') {
            // AJAX action(s)
            ct_check_wplp();
        } else {
            if (array_key_exists('inbound_submitted', $_POST) && $_POST['inbound_submitted'] == '1') {
                // Final submit
                ct_check_wplp();
            }
        }
    }
    // intercept S2member POST
    if (defined('WS_PLUGIN__S2MEMBER_PRO_VERSION') && (isset($_POST[$ct_post_data_label]['email']) || isset($_POST[$ct_post_data_authnet_label]['email']))) {
        ct_s2member_registration_test();
    }
    //
    // New user approve hack
    // https://wordpress.org/plugins/new-user-approve/
    //
    if (ct_plugin_active('new-user-approve/new-user-approve.php')) {
        add_action('register_post', 'ct_register_post', 1, 3);
    }
    //
    // Load JS code to website footer
    //
    if (!(defined('DOING_AJAX') && DOING_AJAX)) {
        add_action('wp_footer', 'ct_footer_add_cookie', 1);
    }
    if (ct_is_user_enable()) {
        ct_cookies_test();
        if (isset($ct_options['general_contact_forms_test']) && $ct_options['general_contact_forms_test'] == 1 && !isset($_POST['comment_post_ID']) && !isset($_GET['for'])) {
            $ct_check_post_result = false;
            ct_contact_form_validate();
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $ct_general_postdata_test == 1 && !is_admin()) {
            $ct_check_post_result = false;
            ct_contact_form_validate_postdata();
        }
    }
}
        $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;
}
if (is_admin()) {
    require_once CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-comments.php';
    require_once CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-users.php';
}
if (isset($_GET['ait-action']) && $_GET['ait-action'] == 'register') {
    $tmp = $_POST['redirect_to'];
    unset($_POST['redirect_to']);
    ct_contact_form_validate();
    $_POST['redirect_to'] = $tmp;
}
function ct_show_comment_link()
{
    print "<div style='font-size:10pt;'><a href='https://cleantalk.org/wordpress-anti-spam-plugin' target='_blank'>" . __('WordPress spam', 'cleantalk') . "</a> " . __('blocked by', 'cleantalk') . " CleanTalk.</div>";
}
add_action('right_now_content_table_end', 'my_add_counts_to_dashboard');
function cleantalk_update_sfw()
{
    global $wpdb;
    $wpdb->query("drop table if exists `" . $wpdb->base_prefix . "cleantalk_sfw`;");
    $wpdb->query("CREATE TABLE IF NOT EXISTS `" . $wpdb->base_prefix . "cleantalk_sfw` (\n`network` int(11) unsigned NOT NULL,\n`mask` int(11) unsigned NOT NULL,\nINDEX (  `network` ,  `mask` )\n) ENGINE = MYISAM ;");
    if (!function_exists('sendRawRequest')) {
        require_once 'inc/cleantalk.class.php';
    }