PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/drivers/Captcha.php

https://github.com/Toushi/flow
PHP | 227 lines | 109 code | 31 blank | 87 comment | 9 complexity | bd327e7c7cf47476ba75b2dd527b2c39 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Captcha driver class.
  4. *
  5. * $Id: Captcha.php 3237 2008-07-30 12:10:20Z Geert $
  6. *
  7. * @package Captcha
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. abstract class Captcha_Driver {
  13. // The correct Captcha challenge answer
  14. protected $response;
  15. // Image resource identifier and type ("png", "gif" or "jpeg")
  16. protected $image;
  17. protected $image_type = 'png';
  18. /**
  19. * Constructs a new challenge.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. // Generate a new challenge
  26. $this->response = $this->generate_challenge();
  27. // Store the correct Captcha response in a session
  28. Event::add('system.post_controller', array($this, 'update_response_session'));
  29. }
  30. /**
  31. * Generate a new Captcha challenge.
  32. *
  33. * @return string the challenge answer
  34. */
  35. abstract public function generate_challenge();
  36. /**
  37. * Output the Captcha challenge.
  38. *
  39. * @param boolean html output
  40. * @return mixed the rendered Captcha (e.g. an image, riddle, etc.)
  41. */
  42. abstract public function render($html);
  43. /**
  44. * Stores the response for the current Captcha challenge in a session so it is available
  45. * on the next page load for Captcha::valid(). This method is called after controller
  46. * execution (in the system.post_controller event) in order not to overwrite itself too soon.
  47. *
  48. * @return void
  49. */
  50. public function update_response_session()
  51. {
  52. Session::instance()->set('captcha_response', sha1(strtoupper($this->response)));
  53. }
  54. /**
  55. * Validates a Captcha response from a user.
  56. *
  57. * @param string captcha response
  58. * @return boolean
  59. */
  60. public function valid($response)
  61. {
  62. return (sha1(strtoupper($response)) === Session::instance()->get('captcha_response'));
  63. }
  64. /**
  65. * Returns the image type.
  66. *
  67. * @param string filename
  68. * @return string|FALSE image type ("png", "gif" or "jpeg")
  69. */
  70. public function image_type($filename)
  71. {
  72. switch (strtolower(substr(strrchr($filename, '.'), 1)))
  73. {
  74. case 'png':
  75. return 'png';
  76. case 'gif':
  77. return 'gif';
  78. case 'jpg':
  79. case 'jpeg':
  80. // Return "jpeg" and not "jpg" because of the GD2 function names
  81. return 'jpeg';
  82. default:
  83. return FALSE;
  84. }
  85. }
  86. /**
  87. * Creates an image resource with the dimensions specified in config.
  88. * If a background image is supplied, the image dimensions are used.
  89. *
  90. * @throws Kohana_Exception if no GD2 support
  91. * @param string path to the background image file
  92. * @return void
  93. */
  94. public function image_create($background = NULL)
  95. {
  96. // Check for GD2 support
  97. if ( ! function_exists('imagegd2'))
  98. throw new Kohana_Exception('captcha.requires_GD2');
  99. // Create a new image (black)
  100. $this->image = imagecreatetruecolor(Captcha::$config['width'], Captcha::$config['height']);
  101. // Use a background image
  102. if ( ! empty($background))
  103. {
  104. // Create the image using the right function for the filetype
  105. $function = 'imagecreatefrom'.$this->image_type($filename);
  106. $this->background_image = $function($background);
  107. // Resize the image if needed
  108. if (imagesx($this->background_image) !== Captcha::$config['width']
  109. OR imagesy($this->background_image) !== Captcha::$config['height'])
  110. {
  111. imagecopyresampled
  112. (
  113. $this->image, $this->background_image, 0, 0, 0, 0,
  114. Captcha::$config['width'], Captcha::$config['height'],
  115. imagesx($this->background_image), imagesy($this->background_image)
  116. );
  117. }
  118. // Free up resources
  119. imagedestroy($this->background_image);
  120. }
  121. }
  122. /**
  123. * Fills the background with a gradient.
  124. *
  125. * @param resource gd image color identifier for start color
  126. * @param resource gd image color identifier for end color
  127. * @param string direction: 'horizontal' or 'vertical', 'random' by default
  128. * @return void
  129. */
  130. public function image_gradient($color1, $color2, $direction = NULL)
  131. {
  132. $directions = array('horizontal', 'vertical');
  133. // Pick a random direction if needed
  134. if ( ! in_array($direction, $directions))
  135. {
  136. $direction = $directions[array_rand($directions)];
  137. // Switch colors
  138. if (mt_rand(0, 1) === 1)
  139. {
  140. $temp = $color1;
  141. $color1 = $color2;
  142. $color2 = $temp;
  143. }
  144. }
  145. // Extract RGB values
  146. $color1 = imagecolorsforindex($this->image, $color1);
  147. $color2 = imagecolorsforindex($this->image, $color2);
  148. // Preparations for the gradient loop
  149. $steps = ($direction === 'horizontal') ? Captcha::$config['width'] : Captcha::$config['height'];
  150. $r1 = ($color1['red'] - $color2['red']) / $steps;
  151. $g1 = ($color1['green'] - $color2['green']) / $steps;
  152. $b1 = ($color1['blue'] - $color2['blue']) / $steps;
  153. if ($direction === 'horizontal')
  154. {
  155. $x1 =& $i;
  156. $y1 = 0;
  157. $x2 =& $i;
  158. $y2 = Captcha::$config['height'];
  159. }
  160. else
  161. {
  162. $x1 = 0;
  163. $y1 =& $i;
  164. $x2 = Captcha::$config['width'];
  165. $y2 =& $i;
  166. }
  167. // Execute the gradient loop
  168. for ($i = 0; $i <= $steps; $i++)
  169. {
  170. $r2 = $color1['red'] - floor($i * $r1);
  171. $g2 = $color1['green'] - floor($i * $g1);
  172. $b2 = $color1['blue'] - floor($i * $b1);
  173. $color = imagecolorallocate($this->image, $r2, $g2, $b2);
  174. imageline($this->image, $x1, $y1, $x2, $y2, $color);
  175. }
  176. }
  177. /**
  178. * Returns the img html element or outputs the image to the browser.
  179. *
  180. * @param boolean html output
  181. * @return mixed html string or void
  182. */
  183. public function image_render($html)
  184. {
  185. // Output html element
  186. if ($html)
  187. return '<img alt="Captcha" src="'.url::site('captcha/'.Captcha::$config['group']).'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" />';
  188. // Send the correct HTTP header
  189. header('Content-Type: image/'.$this->image_type);
  190. // Pick the correct output function
  191. $function = 'image'.$this->image_type;
  192. $function($this->image);
  193. // Free up resources
  194. imagedestroy($this->image);
  195. }
  196. } // End Captcha Driver