Ejemplo n.º 1
0
 /**
  * Constructs a new Captcha object.
  *
  * @throws Kohana_Exception
  * @param string Config group name
  * @return void
  */
 public function __construct($group = NULL)
 {
     // Create a singleton instance once
     empty(Captcha::$instance) and Captcha::$instance = $this;
     // No config group name given
     if (!is_string($group)) {
         $group = 'default';
     }
     // Load and validate config group
     if (!is_array($config = captcha_config($group))) {
         throw new Kohana_Exception('Captcha group not defined in :group configuration', array(':group' => $group));
     }
     // All captcha config groups inherit default config group
     if ($group !== 'default') {
         // Load and validate default config group
         if (!is_array($default = captcha_config('default'))) {
             throw new Kohana_Exception('Captcha group not defined in :group configuration', array(':group' => 'default'));
         }
         // Merge config group with default config group
         $config += $default;
     }
     // Assign config values to the object
     foreach ($config as $key => $value) {
         if (array_key_exists($key, Captcha::$config)) {
             Captcha::$config[$key] = $value;
         }
     }
     // Store the config group name as well, so the drivers can access it
     Captcha::$config['group'] = $group;
     // If using a background image, check if it exists
     if (!empty($config['background'])) {
         Captcha::$config['background'] = str_replace('\\', '/', realpath($config['background']));
         if (!is_file(Captcha::$config['background'])) {
             throw new Kohana_Exception('The specified file, :file, was not found.', array(':file' => Captcha::$config['background']));
         }
     }
     // If using any fonts, check if they exist
     if (!empty($config['fonts'])) {
         Captcha::$config['fontpath'] = str_replace('\\', '/', realpath($config['fontpath'])) . '/';
         foreach ($config['fonts'] as $font) {
             if (!is_file(Captcha::$config['fontpath'] . $font)) {
                 throw new Kohana_Exception('The specified file, :file, was not found.', array(':file' => Captcha::$config['fontpath'] . $font));
             }
         }
     }
     // Generate a new challenge
     $this->response = $this->generate_challenge();
 }
Ejemplo n.º 2
0
 function captcha($config = array())
 {
     // Check for GD library
     if (!function_exists('gd_info')) {
         throw new Exception('Required GD library is missing');
     }
     $captcha_config = captcha_config();
     /*
     	// Overwrite defaults with custom config values
     	if( is_array($config) ) {
     		foreach( $config as $key => $value ) $captcha_config[$key] = $value;
     	}
     	
     	// Restrict certain values
     	if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1;
     	if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0;
     	if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10;
     	if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min'];
     	if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10;
     	if( $captcha_config['max_font_size'] < $captcha_config['min_font_size'] ) $captcha_config['max_font_size'] = $captcha_config['min_font_size'];
     */
     // Use milliseconds instead of seconds
     srand(microtime() * 100);
     // Generate CAPTCHA code if not set by user
     if (empty($captcha_config['code'])) {
         $captcha_config['code'] = '';
         $length = rand($captcha_config['min_length'], $captcha_config['max_length']);
         while (strlen($captcha_config['code']) < $length) {
             $captcha_config['code'] .= substr($captcha_config['characters'], rand() % strlen($captcha_config['characters']), 1);
         }
     }
     // Generate image src
     /*
     		$image_src = substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT'])) . '?_CAPTCHA&amp;t=' . urlencode(microtime());
     		$image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/');
     */
     $image_src = admin_url('admin-ajax.php') . '?action=sfw2l_get_captcha&_CAPTCHA=true&amp;t=' . urlencode(microtime());
     //plugins_url( 'captcha.php', __FILE__ ) . '?_CAPTCHA&amp;t=' . urlencode(microtime());
     // direct access version, requires wp-load
     //$image_src = plugins_url( 'captcha.php', __FILE__ ) . '?_CAPTCHA&amp;t=' . urlencode(microtime());
     //$_SESSION['_CAPTCHA']['config'] = serialize($captcha_config);
     return array('code' => $captcha_config['code'], 'image_src' => $image_src);
 }