PageRenderTime 40ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 8ms app.codeStats 0ms

/classes/captcha.class.php

https://github.com/gesf/captcha.class.php
PHP | 270 lines | 122 code | 21 blank | 127 comment | 13 complexity | a288e1f44433d68d1f8cf67e9c6f47cc MD5 | raw file
  1<?php
  2
  3/* Prevent unauthorized access */
  4if (! defined ( "INSITE" ))die ( "No direct access allowed!" );
  5
  6/**
  7 * Class Captcha
  8 *
  9 * Defines the Captcha class - generate CAPTCHAs
 10 * to create a safe Captcha image
 11 *
 12 * @package PHP Captcha Class
 13 * @author  Gonรงalo Fontoura gesf
 14 * @copyright 2005 Gonรงalo "gesf" Fontoura.
 15 * @license Licensed under the GNU L-GPL
 16 * @link https://github.com/gesf/captcha.class.php
 17 * @version 1.1 
 18 */
 19
 20/* Class Start */
 21class Captcha {
 22	
 23	/**
 24	 * Set Captcha Mode
 25	 *
 26	 * @var number
 27	 * @name $_capMode
 28	 * 2 : normal
 29	 * 4 : medium
 30	 * 6 : strong
 31	 */
 32	var $_capMode = 4;
 33	
 34	/**
 35	 * Set Captcha Lenght
 36	 *
 37	 * @var number
 38	 * @name $_capLength
 39	 */
 40	var $_capLength = 6;
 41	
 42	/**
 43	 * Store Captcha String
 44	 *
 45	 * @var string
 46	 * @name $_capString
 47	 */
 48	var $_capString;
 49	
 50	/**
 51	 * Captcha Image Type
 52	 *
 53	 * @var string
 54	 * @name $_capImageType
 55	 */
 56	var $_capImageType = 'png';
 57	
 58	/**
 59	 * Captcha Image Fonts
 60	 *
 61	 * @var string
 62	 * @name $_capFont
 63	 */
 64	var $_capFont = 'fonts/captcha.ttf';
 65	
 66	/**
 67	 * Captcha Default Character Width
 68	 *
 69	 * @var number
 70	 * @name $_capCharWidth
 71	 */
 72	var $_capCharWidth = 25;
 73	
 74	/**
 75	 * Captcha Default Text Color
 76	 *
 77	 * @var string
 78	 * @name $_capTextColor
 79	 */
 80	var $_capTextColor = 'FFFFFF';
 81	
 82	/**
 83	 * Captcha Default Background Color
 84	 *
 85	 * @var string
 86	 * @name $_capBgColor
 87	 */
 88	var $_capBgColor = '0070C2';
 89	
 90	/**
 91	 * To Store the Captcha String Type
 92	 *
 93	 * @var number
 94	 * @name $_capCase
 95	 */
 96	var $_capCase = 5;
 97	
 98	/**
 99	 * Stores the Captcha Image Height
100	 *
101	 * @var number
102	 * @name $_capimage_height
103	 */
104	var $_capimage_height = 40;
105	
106	/**
107	 * The Captcha Text Padding
108	 *
109	 * @var number
110	 * @name $_capimage_padding
111	 */
112	var $_capimage_padding = 0;
113	
114	/**
115	 * Class Constructor
116	 *
117	 * Call needed methods and gerenate CAPTCHA right away .
118	 *
119	 * @param string $letter        	
120	 * @param number $case        	
121	 */
122	public function Captcha($letter = '', $case = 5) {
123		$this->_capCase = $case;
124		if (empty ( $letter )) {
125			$this->StringGen ();
126		} else {
127			$this->_capLength = strlen ( $letter );
128			$this->_capString = substr ( $letter, 0, $this->_capLength );
129		}
130		@session_start ();
131		$_SESSION ["CAPTCHA_HASH"] = sha1 ( $this->_capString );
132		$this->SendHeader ();
133		$this->MakeCaptcha ();
134	}
135	
136	/**
137	 * Generate CAPTCHA string
138	 *
139	 * String Type:
140	 * 0 : Lowercase Letters (a-z).
141	 * 1 : Uppercase Letters (A-Z).
142	 * 2 : Numbers Only (0-9).
143	 * 3 : Letters Only (upper and lower case).
144	 * 4 : Lowercase Letters and Numbers.
145	 * 5 : Uppercase Letters and Numbers.
146	 * 6 : All together
147	 */
148	public function StringGen() {
149		$uppercase = range ( 'A', 'Z' );
150		$lowercase = range ( 'a', 'z' );
151		$numeric = range ( 0, 9 );
152		$char_pool = array ();
153		switch ($this->_capCase) {
154			case 0 :
155				$char_pool = $lowercase;
156				break;
157			case 1 :
158				$char_pool = $uppercase;
159				break;
160			case 2 :
161				$char_pool = $numeric;
162				break;
163			case 3 :
164				$char_pool = array_merge ( $uppercase, $lowercase );
165				break;
166			case 4 :
167				$char_pool = array_merge ( $lowercase, $numeric );
168				break;
169			case 5 :
170				$char_pool = array_merge ( $uppercase, $numeric );
171				break;
172			case 6 :
173				$char_pool = array_merge ( $uppercase, $lowercase, $numeric );
174				break;
175			default :
176				$char_pool = array_merge ( $uppercase, $numeric );
177		}
178		$pool_length = count ( $char_pool ) - 1;
179		for($i = 0; $i < $this->_capLength; $i ++) {
180			$this->_capString .= $char_pool [mt_rand ( 0, $pool_length )];
181		}
182	}
183	
184	/**
185	 * Captcha Header Setting
186	 * 
187	 * Sends the proper Content-type
188	 */
189	public function SendHeader() {
190		switch ($this->_capImageType) {
191			case 'jpeg' :
192				header ( 'Content-type: image/jpeg' );
193				break;
194			case 'png' :
195				header ( 'Content-type: image/png' );
196				break;
197			case 'gif' :
198				header ( 'Content-type: image/gif' );
199				break;
200			default :
201				header ( 'Content-type: image/png' );
202				break;
203		}
204	}
205	
206	/**
207	 * Create Captcha
208	 * 
209	 * Generate the image based on all the settings
210	 * @version 1.1 
211	 */
212	public function MakeCaptcha() {
213		$imagelength = $this->_capLength * $this->_capCharWidth + $this->_capimage_padding;
214		$image = imagecreate ( $imagelength, $this->_capimage_height );
215		$bgcolor = imagecolorallocate ( $image, hexdec ( substr ( $this->_capBgColor, 0, 2 ) ), hexdec ( substr ( $this->_capBgColor, 2, 2 ) ), hexdec ( substr ( $this->_capBgColor, 4, 2 ) ) );
216		$stringcolor = imagecolorallocate ( $image, hexdec ( substr ( $this->_capTextColor, 0, 2 ) ), hexdec ( substr ( $this->_capTextColor, 2, 2 ) ), hexdec ( substr ( $this->_capTextColor, 4, 2 ) ) );
217		$linecolor = imagecolorallocate ( $image, 0, 0, 0 );		
218		for ($i = 0; $i <= 2; $i++) {
219			$captcha_image_lcolor[] = imagecolorallocate($image, hexdec ( substr ( $this->_capTextColor, 0, 2 ) ), hexdec ( substr ( $this->_capTextColor, 2, 2 ) ), hexdec ( substr ( $this->_capTextColor, 4, 2 ) ));
220		}       
221		for($j = 0; $j <= $this->_capMode; $j ++) {
222			if ($this->_capMode % ($j+1) === 0) {
223				for($i = 0; $i <= 10; $i ++) {
224					imageline ( $image, $i * 20 + mt_rand ( 4, 26 ), 0, $i * 20 - mt_rand ( 4, 26 ), 39, $captcha_image_lcolor [mt_rand ( 0, 2 )] );
225				}
226			} else {
227				for($i = 0; $i <= 10; $i ++) {
228					imageline ( $image, $i * 20 + mt_rand ( 4, 26 ), 39, $i * 20 - mt_rand ( 4, 26 ), 0, $captcha_image_lcolor [mt_rand ( 0, 2 )] );
229				}
230				
231			}
232		}		
233		imagettftext ( $image, $this->_capCharWidth, 4, 18, 34, $stringcolor, $this->_capFont, $this->_capString );
234		switch ($this->_capImageType) {
235			case 'jpeg' :
236				imagejpeg ( $image );
237				break;
238			case 'png' :
239				imagepng ( $image );
240				break;
241			case 'gif' :
242				imagegif ( $image );
243				break;
244			default :
245				imagepng ( $image );
246				break;
247		}
248		imagedestroy ( $image );
249	}
250	
251	/**
252	 * Some additional methods you might want to use
253	 *
254	 * Returns the CAPTCHA string as it is
255	 *
256	 * @return string
257	 */
258	public function GetCaptchaString() {
259		return $this->_capString;
260	}
261	
262	/**
263	 * Returns the CAPTCHA hash
264	 *
265	 * @return string
266	 */
267	public function GetCaptchaHash() {
268		return $_SESSION ["CAPTCHA_HASH"];
269	}
270}/* End Class Start */