ultimix
captcha.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
16  /*
17  * w3captcha - php-скрипт для генерации изображений CAPTCHA
18  * версия: 1.0 от 01.02.2008
19  * разработчики: http://w3box.ru
20  * тип лицензии: freeware
21  * w3box.ru © 2008
22  */
23 
35 
46  var $Security = false;
47 
58  var $Chars = false;
59 
70  function __construct()
71  {
72  try
73  {
74  $this->Security = get_package( 'security' , 'last' , __FILE__ );
75  $this->Chars = '0123456789';
76  }
77  catch( Exception $e )
78  {
79  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
80  }
81  }
82 
101  function prepare_image()
102  {
103  try
104  {
105  $Width = 100; /* ширина картинки */
106  $Height = 30; /* высота картинки */
107  $Image = imagecreatetruecolor( $Width , $Height );
108 
109  $BackgroundColor = imagecolorallocate( $Image , 255 , 255 , 255 ); /* rbg-цвет фона */
110 
111  imagefill( $Image , 0 , 0 , $BackgroundColor);
112 
113  return( $Image );
114  }
115  catch( Exception $e )
116  {
117  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
118  }
119  }
120 
143  function get_font_settings( $Image )
144  {
145  try
146  {
147  $FontFile = dirname( __FILE__ ).'/res/font/albonic.ttf'; /* путь к файлу относительно w3captcha.php */
148  $CharAlign = 22; /* выравнивание символа по-вертикали */
149  $Start = 5; /* позиция первого символа по-горизонтали */
150  $Interval = 16; /* интервал между началами символов */
151  $Color = imagecolorallocate( $Image , 255 , 0 , 0 ); /* rbg-цвет тени */
152  $NumChars = strlen( $this->Chars );
153 
154  return( array( $FontFile , $CharAlign , $Start , $Interval , $Color , $NumChars ) );
155  }
156  catch( Exception $e )
157  {
158  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
159  }
160  }
161 
184  function create_captcha( &$Options )
185  {
186  try
187  {
188  $Image = $this->prepare_image();
189  $Str = "";
190 
191  list( $FontFile , $Align , $Start , $Interval , $Color , $Count ) = $this->get_font_settings( $Image );
192 
193  for( $i = 0 ; $i < 5 ; $i++ )
194  {
195  $Char = $this->Chars[ rand( 0 , $Count - 1 ) ];
196  $FontSize = rand( 15 , 25 );
197  $CharAngle = rand( -10 , 10 );
198  imagettftext(
199  $Image , $FontSize , $CharAngle , $Start , $Align , $Color , $FontFile , $Char
200  );
201  $Start += $Interval;
202  $Str .= $Char;
203  }
204 
205  return( array( $Image , $Str ) );
206  }
207  catch( Exception $e )
208  {
209  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
210  }
211  }
212 
235  function get_sesion_name( &$Options )
236  {
237  try
238  {
239  $SessionName = 'captcha';
240 
241  if( $Options->get_setting( 'captcha_name' , false ) !== false )
242  {
243  if( $Options->get_setting( 'captcha_name' ) == 'auto' )
244  {
245  $SessionName = $Options->get_setting( 'captcha_name' );
246  }
247  else
248  {
249  $SessionName = md5(
250  $this->Security->get_srv( 'SCRIPT_NAME' , 'string' ).
251  $this->Security->get_srv( 'QUERY_STRING' , 'string' )
252  );
253  }
254  }
255 
256  return( $SessionName );
257  }
258  catch( Exception $e )
259  {
260  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
261  }
262  }
263 
290  function validate_captcha( $InputCaptcha , $Options )
291  {
292  try
293  {
294  return( @$_SESSION[ $this->get_sesion_name( $Options ) ] == $InputCaptcha );
295  }
296  catch( Exception $e )
297  {
298  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
299  }
300  }
301 
320  function output_image( &$Image )
321  {
322  try
323  {
324  if( function_exists( "imagepng" ) )
325  {
326  header( "Content-type: image/png" );
327  imagepng( $Image );
328  }
329  elseif( function_exists( "imagegif" ) )
330  {
331  header( "Content-type: image/gif" );
332  imagegif( $Image );
333  }
334  elseif( function_exists( "imagejpeg" ) )
335  {
336  header( "Content-type: image/jpeg" );
337  imagejpeg( $Image );
338  }
339 
340  imagedestroy( $Image );
341  }
342  catch( Exception $e )
343  {
344  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
345  }
346  }
347 
370  function view( $Options )
371  {
372  try
373  {
374  @session_start();
375 
376  list( $Image , $Str ) = $this->create_captcha( $Options );
377 
378  $_SESSION[ $this->get_sesion_name( $Options ) ] = $Str;
379 
380  $this->output_image( $Image );
381 
382  return( '' );
383  }
384  catch( Exception $e )
385  {
386  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
387  }
388  }
389  }
390 
391 ?>