CaptchaModel.php (1285B)
1 <?php 2 3 /** 4 * Class CaptchaModel 5 * 6 * This model class handles all the captcha stuff. 7 * Currently this uses the excellent Captcha generator lib from https://github.com/Gregwar/Captcha 8 * Have a look there for more options etc. 9 */ 10 class CaptchaModel 11 { 12 /** 13 * Generates the captcha, "returns" a real image, this is why there is header('Content-type: image/jpeg') 14 * Note: This is a very special method, as this is echoes out binary data. 15 */ 16 public static function generateAndShowCaptcha() 17 { 18 // create a captcha with the CaptchaBuilder lib (loaded via Composer) 19 $captcha = new Gregwar\Captcha\CaptchaBuilder; 20 $captcha->build( 21 Config::get('CAPTCHA_WIDTH'), 22 Config::get('CAPTCHA_HEIGHT') 23 ); 24 25 // write the captcha character into session 26 Session::set('captcha', $captcha->getPhrase()); 27 28 // render an image showing the characters (=the captcha) 29 header('Content-type: image/jpeg'); 30 $captcha->output(); 31 } 32 33 /** 34 * Checks if the entered captcha is the same like the one from the rendered image which has been saved in session 35 * @param $captcha string The captcha characters 36 * @return bool success of captcha check 37 */ 38 public static function checkCaptcha($captcha) 39 { 40 if ($captcha == Session::get('captcha')) { 41 return true; 42 } 43 44 return false; 45 } 46 }