/** * Constructs a new challenge. * * @return void */ public function __construct() { Captcha::$config = array_merge(Kohana::config('captcha/' . Captcha::$config['style']), Kohana::config('captcha.' . Captcha::$config['group']), Captcha::$config); if (!empty($_POST) && !empty(Captcha::$config['field']) && !empty($_POST['g-recaptcha-response'])) { $_POST[Captcha::$config['field']] = $_POST['g-recaptcha-response']; } }
public static function instance($group = 'alpha') { if (!isset(Captcha::$instance)) { $config = Kohana::config('captcha'); Captcha::$config = $config; //$style = $config['style']; $class = 'Captcha_' . ucfirst($group); Captcha::$instance = new $class($config); } return Captcha::$instance; }
/** * Constructs a new Captcha object. * * @throws KoException * @param string config group name * @return void */ public function __construct($config = NULL) { if (empty($config)) { $config = Ko::config('captcha.default'); } elseif (is_string($config)) { if (($config = Ko::config('captcha.' . $config)) === NULL) { throw new KoException('captcha.undefined_group :group', array(':group' => $config)); } } $config_default = Ko::config('captcha.default'); // Merge the default config with the passed config self::$config = array_merge($config_default, $config); // If using a background image, check if it exists if (!empty($config['background'])) { self::$config['background'] = str_replace('\\', '/', realpath($config['background'])); if (!is_file(self::$config['background'])) { throw new KoException('captcha.file_not_found :background', array(':background' => self::$config['background'])); } } // If using any fonts, check if they exist if (!empty($config['fonts'])) { self::$config['fontpath'] = str_replace('\\', '/', realpath($config['fontpath'])) . '/'; foreach ($config['fonts'] as $font) { if (!is_file(self::$config['fontpath'] . $font)) { throw new KoException('captcha.file_not_found :font', array(':font' => self::$config['fontpath'] . $font)); } } } // Set driver name $driver = 'Captcha_' . ucfirst($config['style']); // Load the driver if (!Ko::autoload($driver)) { throw new KoException('core.driver_not_found :style', array(':style' => $driver)); } $this->driver = new $driver(); // Validate the driver if (!$this->driver instanceof Captcha_Driver) { throw new KoException('core.driver_implements :driver', array(':driver' => $config['style'])); } }
/** * render image * * @param array $config * @return image */ public static function render($config = false) { if (is_array($config)) { Captcha::$config = array_merge(Captcha::$config, $config); } if (empty(Captcha::$response)) { Captcha::generate_challenge(); } // Creates Captcha::$image Captcha::image_create(Captcha::$config['background']); // Add a random gradient if (empty(Captcha::$config['background'])) { $color1 = imagecolorallocate(Captcha::$image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $color2 = imagecolorallocate(Captcha::$image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); Captcha::image_gradient($color1, $color2); } // Add a few random circles for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++) { $color = imagecolorallocatealpha(Captcha::$image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120)); $size = mt_rand(5, Captcha::$config['height'] / 3); imagefilledellipse(Captcha::$image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color); } // Calculate character font-size and spacing $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen(Captcha::$response); $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$response)); // Background alphabetic character attributes $color_limit = mt_rand(96, 160); $chars = 'ABEFGJKLPQRTVY'; // Draw each captcha character with varying attributes for ($i = 0, $strlen = strlen(Captcha::$response); $i < $strlen; $i++) { // Use different fonts if available $font = Core::find_file('data', Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])], false); $angle = mt_rand(-40, 20); // Scale the character size on image height $size = $default_size / 10 * mt_rand(8, 12); if (!function_exists('imageftbbox')) { Core::show_500(__('function imageftbbox not exist.')); } $box = imageftbbox($size, $angle, $font, Captcha::$response[$i]); // Calculate character starting coordinates $x = $spacing / 4 + $i * $spacing; $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4; // Draw captcha text character // Allocate random color, size and rotation attributes to text $color = imagecolorallocate(Captcha::$image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255)); // Write text character to image imagefttext(Captcha::$image, $size, $angle, $x, $y, $color, $font, Captcha::$response[$i]); // Draw "ghost" alphabetic character $text_color = imagecolorallocatealpha(Captcha::$image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120)); $char = substr($chars, mt_rand(0, 14), 1); imagettftext(Captcha::$image, $size * 1.4, mt_rand(-45, 45), $x - mt_rand(5, 10), $y + mt_rand(5, 10), $text_color, $font, $char); } // Output return Captcha::image_render(); }