Вход Регистрация
Файл: captcha/inc/myCaptcha.class.php
Строк: 360
<?php



            
/**************************************************************************************************
            | 
            | http://alternanetworks.com
            | ask@alternanetworks.com
            |
            |**************************************************************************************************
            |
            | By using this software you agree that you have read and acknowledged our End-User License 
            | Agreement available at http://envato.com/ and to be bound by it.
            |
            | Copyright (c) 2011 alternanetworks.com All rights reserved.
            |
            | http://codecanyon.net/user/AlternaNetworks 
            |**************************************************************************************************/

            //error_reporting( E_ALL );
            //ini_set( 'display_errors', '1' );

            
class myCaptcha
            
{
                 private 
$id = 'myCaptcha';
                 private 
$challenge;
                 private 
$path;
                 private 
$font = 'journal.ttf';
                 private 
$fontSize = 16;
                 private 
$fontColor = '00AAFF';
                 private 
$angle = 4;
                 private 
$fontWaveX = 10;
                 private 
$fontWaveY = 10;
                 private 
$lenght = 6;
                 private 
$width = 80;
                 private 
$height = 20;
                 private 
$useBackground = true;
                 private 
$background = 'bg00.png';
                 private 
$bgColor = 'F5F5F5';
                 private 
$useBgWave = true;
                 private 
$useFontWave = false;
                 private 
$useDictionary = false;
                 private 
$dictionary = 'en.dictionary.php';
                 private 
$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                 private 
$resources;
                 private 
$fonts_dir;
                 private 
$dictionary_dir;
                 private 
$bg_dir;
                 private 
$useBase64 = false;


                 
// constructor
                 
public function __construct( $id = null )
                 {
                      
$this->id = isset( $id ) && is_string( $id ) ? $this->generate_id( $id ) : $this->id;
                      
$this->session();
                      
$this->resources = DIRECTORY_SEPARATOR;
                      
$this->fonts_dir = 'fonts' . DIRECTORY_SEPARATOR;
                      
$this->dictionary_dir = 'dictionary' . DIRECTORY_SEPARATOR;
                      
$this->bg_dir = 'background' . DIRECTORY_SEPARATOR;
                      
$this->path = MYCAPTCHA_BASE_DIR;
                 }

                 
// generate id
                 
private function generate_id( $id )
                 {
                      return 
$id . '_myCaptcha';
                 }

                 
// check and start session
                 
private function session()
                 {
                      if ( 
session_id() == '' ) {
                           
session_start();
                      }
                      return 
true;
                 }

                 
// set font
                 
public function font( $font = null )
                 {
                      return 
$this->font = file_exists( $this->path . $this->resources . $this->fonts_dir . $font ) ? $font : $this->font;
                 }

                 
// set font size
                 
public function fontSize( $size = null )
                 {
                      return 
$this->fontSize = is_numeric( $size ) ? $size : $this->fontSize;
                 }

                 
// set angle
                 
public function fontAngle( $size = null )
                 {
                      return 
$this->angle = is_numeric( $size ) ? $size : $this->angle;
                 }

                 
// font Wave
                 
public function fontWaveX( $size = null )
                 {
                      return 
$this->fontWaveX = is_numeric( $size ) ? $size : $this->fontWaveX;
                 }

                 
// font Wave
                 
public function fontWaveY( $size = null )
                 {
                      return 
$this->fontWaveY = is_numeric( $size ) ? $size : $this->fontWaveY;
                 }

                 
// set color
                 
public function color( $color = null )
                 {
                      return 
$this->fontColor = isset( $color ) && self::hexcolor( $color ) ? strtoupper( str_replace( '#', '', $color ) ) : $this->fontColor;
                 }

                 
// set lenght of captcha
                 
public function lenght( $size = null )
                 {
                      
$size = isset( $size ) && $size >= 4 ? $size : $this->lenght;
                      
$size = $size >= 12 ? 12 : $size;
                      return 
$this->lenght = $size;
                 }

                 
// set width
                 
public function width( $size = null )
                 {
                      
$size = isset( $size ) && $size >= 40 ? $size : $this->width;
                      
$size = $size > 400 ? 400 : $size;
                      return 
$this->width = $size;
                 }

                 
// set width
                 
public function height( $size = null )
                 {
                      
$size = isset( $size ) && $size > 20 ? $size : $this->height;
                      
$size = $size > 400 ? 400 : $size;
                      return 
$this->height = $size;
                 }

                 
// set use background
                 
public function useBg( $var = null )
                 {
                      return 
$this->useBackground = is_bool( $var ) ? $var : $this->useBackground;
                 }

                 
// set bg
                 
public function bg( $bg = null )
                 {
                      return 
$this->background = file_exists( $this->path . $this->resources . $this->bg_dir . $bg ) ? $bg : $this->background;
                 }

                 
// set bgcolor
                 
public function bgColor( $color = null )
                 {
                      return 
$this->bgColor = isset( $color ) && self::hexcolor( $color ) ? strtoupper( str_replace( '#', '', $color ) ) : $this->bgColor;
                 }

                 
// set use bgwave
                 
public function useBgWave( $var = null )
                 {
                      return 
$this->useBgWave = isset( $var ) && is_bool( $var ) ? $var : $this->useBgWave;
                 }

                 
// set use fontwave
                 
public function useFontWave( $var = null )
                 {
                      return 
$this->useFontWave = is_bool( $var ) ? $var : $this->useFontWave;
                 }

                 
// set dictionary
                 
public function dictionary( $dictionary = null )
                 {
                      
$check = isset( $dictionary ) && file_exists( $this->path . $this->resources . $this->dictionary_dir . $dictionary ) && self::
                           
validateDictionary( $dictionary ) ? true : false;
                      return 
$this->dictionary = $check ? $dictionary : ( is_array( $dictionary ) ? array( $dictionary ) : $this->dictionary );
                 }

                 
// set use dictionary
                 
public function useDictionary( $var = null )
                 {
                      return 
$this->useDictionary = is_bool( $var ) ? $var : $this->useDictionary;
                 }


                 
// set pattern of captcha
                 
public function pattern( $pattern = '' )
                 {
                      return 
$this->pattern = $pattern;
                 }

                 
// validate dictionary file
                 
private function validateDictionary( $file )
                 {
                      return ( bool )
preg_match( '~^.*.dictionary.php$~', $file );
                 }

                 
// hex color validator
                 
private function hexcolor( $color )
                 {
                      return ( bool )
preg_match( "/^(#)?([0-9a-fA-F]{1,2}){6}$/i", $color );
                 }

                 
// set use base64
                 
public function base64( $var = null )
                 {
                      return 
$this->useBase64 = isset( $var ) && is_bool( $var ) ? $var : $this->useBase64;
                 }

                 
// decode hex colors
                 
private function hexcolorDecode( $hexcolor )
                 {
                      
$color = array();
                      
$color['r'] = hexdec( substr( $hexcolor, 0, 2 ) );
                      
$color['g'] = hexdec( substr( $hexcolor, 2, 2 ) );
                      
$color['b'] = hexdec( substr( $hexcolor, 4, 2 ) );
                      return 
$color;
                 }

                 
// make captcha
                 
private function make()
                 {
                      
$dictionary = $this->path . $this->resources . $this->dictionary_dir . $this->dictionary;
                      if ( 
$this->useDictionary && ( file_exists( $dictionary ) || is_array( $this->dictionary ) ) ) {
                           if ( 
is_array( $this->dictionary ) ) {
                                
$array = $this->dictionary;
                                
$words = array();
                                foreach ( 
$array[0] as $word ) {
                                     
$words[] = trim( $word );
                                }
                                
$word = array_unique( $words );
                           } else {
                                if ( !
defined( 'CAPTCHA_LOAD_DICTIONARY' ) )
                                     
define( 'CAPTCHA_LOAD_DICTIONARY', true );
                                include ( 
$dictionary );
                                
$words = json_decode( $words, true );
                           }
                           
$key = $words[mt_rand( 0, count( $words ) - 1 )];
                      } else {
                           
$pattern = $this->pattern;
                           
$key = '';
                           for ( 
$i = 0; $i < $this->lenght; $i++ ) {
                                
$key .= $pattern{mt_rand( 0, strlen( $pattern ) - 1 )};
                           }
                      }
                      
$_SESSION[$this->id] = $key;
                      return 
$this->challenge = $key;
                 }

                 
// show the image
                 
public function image()
                 {
                      
// start
                      
$this->make();

                      
// colors
                      
$txtColor = $this->hexcolorDecode( $this->fontColor );
                      
$bgcolor = $this->hexcolorDecode( $this->bgColor );
                      
$bgImage = $this->background;

                      
$bg_path = $this->path . $this->resources . $this->bg_dir;
                      
$font = $this->path . $this->resources . $this->fonts_dir . $this->font;
                      
$width = $this->width;
                      
$height = $this->height;
                      
$angle = $this->angle ? mt_rand( -$this->angle, $this->angle ) : 0;

                      
// create mage
                      
$imgBG = imagecreatetruecolor( $width, $height );
                      
$col = imagecolorallocate( $imgBG, $bgcolor['r'], $bgcolor['g'], $bgcolor['b'] );
                      
imagefilledrectangle( $imgBG, 0, 0, $width, $height, $col );
                      
imagealphablending( $imgBG, true );

                      
// background
                      
if ( $this->useBackground && file_exists( $bg_path . $bgImage ) ) {
                           
$file = pathinfo( $bg_path . $this->background );
                           
$ext = strtolower( $file['extension'] );
                           switch ( 
$ext ) {
                                case 
'jpg':
                                case 
'jpeg':
                                     
$bg = imagecreatefromjpeg( $bg_path . $bgImage );
                                     break;
                                case 
'gif':
                                     
$bg = imagecreatefromgif( $bg_path . $bgImage );
                                     break;
                                case 
'png':
                                default:
                                     
$bg = imagecreatefrompng( $bg_path . $bgImage );
                                     break;
                           }
                           
imagecopyresampled( $imgBG, $bg, 0, 0, 0, 0, $width, $height, $width, $height );
                           
$imgBG = $this->useBgWave ? $this->wave( $imgBG, $width, $height ) : $imgBG;
                           
imagedestroy( $bg );
                      }

                      
// text
                      
$imgTxt = imagecreatetruecolor( $width, $height );
                      
imagealphablending( $imgTxt, false );
                      
imagesavealpha( $imgTxt, true );

                      
$transparent = imagecolorallocatealpha( $imgTxt, 255, 255, 255, 127 );
                      
$color = imagecolorallocate( $imgTxt, $txtColor['r'], $txtColor['g'], $txtColor['b'] );
                      
imagefilledrectangle( $imgTxt, 0, 0, $width, $height, $transparent );

                      
$text_box = imagettfbbox( $this->fontSize, 0, $font, $this->challenge );
                      
$x = ceil( ( $width - $text_box[2] ) / 2 );
                      
$y = ceil( ( $height - $text_box[5] ) / 2 );

                      
imagealphablending( $imgTxt, true );
                      
imagettftext( $imgTxt, $this->fontSize, $angle, $x, $y, $color, $font, $this->challenge );


                      
// text wave
                      
$imgTxt = $this->useFontWave ? $this->wave( $imgTxt, $width, $height, $this->fontWaveX, $this->fontWaveY ) : $imgTxt;
                      
imagecopyresampled( $imgBG, $imgTxt, 0, 0, 0, 0, $width, $height, $width, $height );
                      
imagedestroy( $imgTxt );

                      
// ob start
                      
ob_start();
                      
imagepng( $imgBG );
                      
$image_data = ob_get_contents();
                      
ob_end_clean();
                      
imagedestroy( $imgBG );

                      
// base64 output
                      
if ( $this->useBase64 ) {
                           
$image_data = 'data:image/png;base64,' . base64_encode( $image_data );
                      } else {
                           
header( "Content-Type: image/png" );
                      }
                      return 
$image_data;
                 }

                 
// wave maker
                 
private function wave( $image, $width, $height, $periodX = 4, $periodY = 4 )
                 {
                      
$periodX = $periodX <= 0 ? 1 : $periodX;
                      
$periodY = $periodY <= 0 ? 1 : $periodY;
                      
$x_period = mt_rand( 1, $periodX );
                      
$y_period = mt_rand( 1, $periodY );
                      
$y_amplitude = 5;
                      
$x_amplitude = 5;
                      
$xp = $x_period * mt_rand( 1, 3 );
                      
$k = mt_rand( 0, 100 );
                      for ( 
$a = 0; $a < $width; $a++ )
                           
imagecopy( $image, $image, $a - 1, sin( $k + $a / $xp ) * $x_amplitude, $a, 0, 1, $height );
                      
$yp = $y_period * mt_rand( 1, 2 );
                      
$k = mt_rand( 0, 100 );
                      for ( 
$a = 0; $a < $height; $a++ )
                           
imagecopy( $image, $image, sin( $k + $a / $yp ) * $y_amplitude, $a - 1, 0, $a, $width, 1 );
                      return 
$image;
                 }

                 
// reset captcha
                 
private function _reset()
                 {
                      
$_SESSION[$this->id] = '';
                      unset( 
$_SESSION[$this->id] );
                      
$this->make();
                      return 
false;
                 }

                 
// get the challenge
                 
public function challenge( $id = null )
                 {
                      
$id = isset( $id ) && is_string( $id ) ? $id . '_myCaptcha' : 'myCaptcha';
                      return isset( 
$_SESSION[$id] ) ? $_SESSION[$id] : false;
                 }

                 
// qr code
                 
public function qr( $id = null, $width = null, $base64 = false )
                 {
                      
$id = isset( $id ) && is_string( $id ) ? $id . '_myCaptcha' : 'myCaptcha';
                      
$check = isset( $_SESSION[$id] ) ? $_SESSION[$id] : false;
                      if ( 
$check ) {
                           
$width = is_numeric( $width ) ? $width : $this->width;
                           require_once ( 
$this->path . $this->resources . 'inc' . DIRECTORY_SEPARATOR . 'myQrcode.class.php' );
                           
$qr = new myQrCode( false );
                           
$qr->text( $check );
                           
$im = imagecreatefromstring( $qr->draw( $width ) );
                           
// ob start
                           
ob_start();
                           
imagepng( $im );
                           
$image_data = ob_get_contents();
                           
ob_end_clean();
                           
imagedestroy( $im );
                           
// base64 output
                           
if ( $base64 ) {
                                
$image_data = 'data:image/png;base64,' . base64_encode( $image_data );
                           } else {
                                
header( "Content-Type: image/png" );
                           }
                           return 
$image_data;
                      }
                      return 
false;
                 }

                 
// reset class
                 
public function reset()
                 {
                      
$this->sendHeaders = false;
                      
$this->font = 'journal.ttf';
                      
$this->fontSize = 16;
                      
$fontColor = '00AAFF';
                      
$this->lenght = 6;
                      
$this->width = 80;
                      
$this->height = 20;
                      
$this->useBackgraound = true;
                      
$this->background = 'bg00.png';
                      
$this->bgColor = 'F5F5F5';
                      
$this->useBgWave = true;
                      
$this->useFontWave = false;
                      
$this->useDictionary = false;
                      
$this->dictionary = 'en.dictionary.php';
                      
$this->pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                      
$this->useBase64 = false;
                 }

            }
Онлайн: 1
Реклама