/**
  * Initializes cookie-session
  */
 public function init_session()
 {
     if (is_admin()) {
         return;
     }
     if (isset($_COOKIE['mc_session_ids'])) {
         $this->session_ids = $_COOKIE['mc_session_ids'];
     } else {
         foreach (array('default', 'multi') as $place) {
             switch ($place) {
                 case 'multi':
                     for ($i = 0; $i < 5; $i++) {
                         $this->session_ids[$place][$i] = sha1($this->generate_password());
                     }
                     break;
                 case 'default':
                     $this->session_ids[$place] = sha1($this->generate_password());
                     break;
             }
         }
     }
     if (!isset($_COOKIE['mc_session_ids'])) {
         setcookie('mc_session_ids[default]', $this->session_ids['default'], current_time('timestamp', true) + apply_filters('math_captcha_time', Math_Captcha()->options['general']['time']), COOKIEPATH, COOKIE_DOMAIN, isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? true : false, true);
         for ($i = 0; $i < 5; $i++) {
             setcookie('mc_session_ids[multi][' . $i . ']', $this->session_ids['multi'][$i], current_time('timestamp', true) + apply_filters('math_captcha_time', Math_Captcha()->options['general']['time']), COOKIEPATH, COOKIE_DOMAIN);
         }
     }
 }
 /**
  *
  */
 public function check_update()
 {
     if (!is_admin() || !current_user_can('manage_options')) {
         return;
     }
     // gets current database version
     $current_db_version = get_option('math_captcha_version', '1.0.0');
     // new version?
     if (version_compare($current_db_version, Math_Captcha()->defaults['version'], '<')) {
         if (version_compare($current_db_version, '1.0.9', '<')) {
             update_option('math_captcha_options', Math_Captcha()->options['general']);
             delete_option('mc_options');
         }
         // updates plugin version
         update_option('math_captcha_version', Math_Captcha()->defaults['version']);
     }
 }
function wpcf7_mathcaptcha_messages($messages)
{
    return array_merge($messages, array('wrong_mathcaptcha' => array('description' => __('Invalid captcha value.', 'math-captcha'), 'default' => Math_Captcha()->core->error_messages['wrong']), 'fill_mathcaptcha' => array('description' => __('Please enter captcha value.', 'math-captcha'), 'default' => Math_Captcha()->core->error_messages['fill']), 'time_mathcaptcha' => array('description' => __('Captcha time expired.', 'math-captcha'), 'default' => Math_Captcha()->core->error_messages['time'])));
}
示例#4
0
 /**
  * Validates settings
  */
 public function validate_settings($input)
 {
     if (isset($_POST['save_mc_general'])) {
         // enable captcha forms
         $enable_for = array();
         if (empty($input['enable_for'])) {
             foreach (Math_Captcha()->defaults['general']['enable_for'] as $enable => $bool) {
                 $input['enable_for'][$enable] = false;
             }
         } else {
             foreach ($this->forms as $enable => $trans) {
                 $enable_for[$enable] = in_array($enable, $input['enable_for']) ? true : false;
             }
             $input['enable_for'] = $enable_for;
         }
         if (!class_exists('WPCF7_ContactForm') && Math_Captcha()->options['general']['enable_for']['contact_form_7']) {
             $input['enable_for']['contact_form_7'] = true;
         }
         if (!class_exists('bbPress') && Math_Captcha()->options['general']['enable_for']['bbpress']) {
             $input['enable_for']['bbpress'] = true;
         }
         // enable mathematical operations
         $mathematical_operations = array();
         if (empty($input['mathematical_operations'])) {
             add_settings_error('empty-operations', 'settings_updated', __('You need to check at least one mathematical operation. Defaults settings of this option restored.', 'math-captcha'), 'error');
             $input['mathematical_operations'] = Math_Captcha()->defaults['general']['mathematical_operations'];
         } else {
             foreach ($this->mathematical_operations as $operation => $trans) {
                 $mathematical_operations[$operation] = in_array($operation, $input['mathematical_operations']) ? true : false;
             }
             $input['mathematical_operations'] = $mathematical_operations;
         }
         // enable groups
         $groups = array();
         if (empty($input['groups'])) {
             add_settings_error('empty-groups', 'settings_updated', __('You need to check at least one group. Defaults settings of this option restored.', 'math-captcha'), 'error');
             $input['groups'] = Math_Captcha()->defaults['general']['groups'];
         } else {
             foreach ($this->groups as $group => $trans) {
                 $groups[$group] = in_array($group, $input['groups']) ? true : false;
             }
             $input['groups'] = $groups;
         }
         // hide for logged in users
         $input['hide_for_logged_users'] = isset($input['hide_for_logged_users']);
         // block direct comments access
         $input['block_direct_comments'] = isset($input['block_direct_comments']);
         // deactivation delete
         $input['deactivation_delete'] = isset($input['deactivation_delete']);
         // captcha title
         $input['title'] = trim($input['title']);
         // captcha time
         $time = (int) $input['time'];
         $input['time'] = $time < 0 ? Math_Captcha()->defaults['general']['time'] : $time;
         // flush rules
         $input['flush_rules'] = true;
     } elseif (isset($_POST['reset_mc_general'])) {
         $input = Math_Captcha()->defaults['general'];
         add_settings_error('settings', 'settings_reset', __('Settings restored to defaults.', 'math-captcha'), 'updated');
     }
     return $input;
 }
        }
        return $links;
    }
    /**
     * Add links to Settings page
     */
    function plugin_settings_link($links, $file)
    {
        if (!is_admin() || !current_user_can('manage_options')) {
            return $links;
        }
        static $plugin;
        $plugin = plugin_basename(__FILE__);
        if ($file == $plugin) {
            $settings_link = sprintf('<a href="%s">%s</a>', admin_url('options-general.php') . '?page=math-captcha', __('Settings', 'math-captcha'));
            array_unshift($links, $settings_link);
        }
        return $links;
    }
}
function Math_Captcha()
{
    static $instance;
    // first call to instance() initializes the plugin
    if ($instance === null || !$instance instanceof Math_Captcha) {
        $instance = Math_Captcha::instance();
    }
    return $instance;
}
Math_Captcha();
示例#6
0
    /**
     *
     */
    public function block_direct_comments($rules)
    {
        if (Math_Captcha()->options['general']['block_direct_comments']) {
            $new_rules = <<<EOT

# BEGIN Math Captcha
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post.php*
RewriteCond %{HTTP_REFERER} !.*{$_SERVER['HTTP_HOST']}.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^\$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/\$ [R=301,L]
</IfModule>
# END Math Captcha


EOT;
            return $new_rules . $rules;
        }
        return $rules;
    }