| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage captcha | 
					
						
							|  |  |  |  * @since 2010-04-24 | 
					
						
							|  |  |  |  * @version SVN: $Id$ | 
					
						
							|  |  |  |  * @filesource $URL$ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Captcha | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     protected $font_size = 38; | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected $width = 200; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $height = 70; | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getImage($token) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $font = dirname(__FILE__) . '/poh.ttf'; | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |         $image = imagecreatetruecolor($this->width, $this->height); | 
					
						
							|  |  |  |         // белый фон
 | 
					
						
							|  |  |  |         $background = imagecolorallocate($image, 255, 255, 255); | 
					
						
							|  |  |  |         imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background); | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |         // основная магия тут
 | 
					
						
							|  |  |  |         if (Session::get('_ctoken') == $token && Session::get('_ccode')) { | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             $code = Session::get('_ccode'); | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             $color = imagecolorallocate($image, 81, 81, 81); | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             for ($j = 0; $j < 5; $j++) { | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  |                 imageellipse($image, rand(-$this->width, $this->width * 2), | 
					
						
							|  |  |  |                              rand(-$this->height, $this->height * 2), | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |                              $this->width, $this->width, $color); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             $letters = preg_split("//u", strtoupper($code)); | 
					
						
							|  |  |  |             $length = count($letters); | 
					
						
							|  |  |  |             $step = floor($this->width / $length); | 
					
						
							|  |  |  |             $i = 2; | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             foreach ($letters as $key => $letter) { | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  |                 imagettftext($image, $this->font_size, 0, | 
					
						
							|  |  |  |                              rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2, | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |                              $color, $font, $letter); | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  |                 $i = $i + $step; | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |         ob_start(); | 
					
						
							|  |  |  |         header("Cache-Control: no-store, no-cache, must-revalidate"); | 
					
						
							|  |  |  |         header("Content-Type: image/jpg"); | 
					
						
							|  |  |  |         imagejpeg($image, '', 100); | 
					
						
							|  |  |  |         imagedestroy($image); | 
					
						
							|  |  |  |         return ob_get_clean(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     public function getToken() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $token = md5(microtime() . uniqid()); | 
					
						
							|  |  |  |         $code = strtoupper(substr(str_ireplace(array('0', 'O'), '', md5(time() . 'captcha' . rand(0, 1000))), 1, 5)); | 
					
						
							|  |  |  |         Session::set('_ctoken', $token); | 
					
						
							|  |  |  |         Session::set('_ccode', $code); | 
					
						
							|  |  |  |         return $token; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |     public function checkCode($token, $code) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-13 14:55:06 +04:00
										 |  |  |         if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)) { | 
					
						
							| 
									
										
										
										
											2010-04-27 17:28:43 +00:00
										 |  |  |             // Housekeeping
 | 
					
						
							|  |  |  |             Session::del('_ccode'); | 
					
						
							|  |  |  |             Session::del('_ctoken'); | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |