Captcha.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Mb+q3NGklhDEfLjdI7KJHpjTTrjH4c3asnT1dGhtIZU=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * creates a captcha element
  14. *
  15. * TODO - refactor after creating thumbnail class
  16. */
  17. namespace Cube\Form\Element;
  18. use Cube\Form\Element;
  19. class Captcha extends Element
  20. {
  21. /**
  22. *
  23. * type of element - override the variable from the parent class
  24. *
  25. * @var string
  26. */
  27. protected $_element = 'captcha';
  28. /**
  29. *
  30. * the font that will be used
  31. *
  32. * @var string
  33. */
  34. protected $_font;
  35. /**
  36. *
  37. * the width of the image
  38. *
  39. * @var integer
  40. */
  41. protected $_width = 220;
  42. /**
  43. *
  44. * the height of the image
  45. *
  46. * @var integer
  47. */
  48. protected $_height = 50;
  49. /**
  50. *
  51. * the length of the captcha code
  52. *
  53. * @var integer
  54. */
  55. protected $_length = 5;
  56. /**
  57. *
  58. * the size of the font
  59. *
  60. * @var integer
  61. */
  62. protected $_fontSize = 32;
  63. /**
  64. *
  65. * the number of pixels added as noise on the image
  66. *
  67. * @var integer
  68. */
  69. protected $_pixels = 50;
  70. /**
  71. *
  72. * the number of circles added as noise on the image
  73. *
  74. * @var integer
  75. */
  76. protected $_circles = 5;
  77. /**
  78. *
  79. * the captcha code generated
  80. *
  81. * @var string
  82. */
  83. protected $_code;
  84. /**
  85. *
  86. * the image that will be output
  87. *
  88. * @var resource
  89. */
  90. protected $_image;
  91. /**
  92. *
  93. * the url of the image that will be generated
  94. *
  95. * @var string
  96. */
  97. public $_captchaImage;
  98. /**
  99. *
  100. * class constructor
  101. *
  102. * @param string $name the name of the form field
  103. * @param int $length the length of the captcha in characters
  104. * @param int $width the width of the captcha image
  105. * @param int $height the height of the captcha image
  106. */
  107. public function __construct($name = 'captcha', $length = null, $width = null, $height = null)
  108. {
  109. parent::__construct($this->_type, $name);
  110. $this->_captchaImage = 'thumbnail.php?option=captcha&name=' . $this->_name;
  111. $this->addValidator('Captcha');
  112. if ($length !== null) {
  113. $this->_length = intval($length);
  114. }
  115. if ($width !== null) {
  116. $this->_width = intval($width);
  117. }
  118. if ($height !== null) {
  119. $this->_height = intval($height);
  120. }
  121. $this->_font = __DIR__ . '/../../../../fonts/Candal.ttf';
  122. }
  123. /**
  124. *
  125. * create the captcha code and save it in a session variable
  126. *
  127. * @return string
  128. */
  129. public function setCode()
  130. {
  131. srand();
  132. $this->_code = substr(md5(rand(2, 9999999999)), 0, $this->_length);
  133. $session = new PPS_Session();
  134. $session->set($this->_name, $this->_code);
  135. return $this->_code;
  136. }
  137. /**
  138. *
  139. * get the captcha code
  140. *
  141. * @return string
  142. */
  143. public function getCode()
  144. {
  145. if (!$this->_code) {
  146. $this->setCode();
  147. }
  148. return $this->_code;
  149. }
  150. /**
  151. *
  152. * create the captcha image
  153. */
  154. public function createImage()
  155. {
  156. $this->_image = imagecreate($this->_width, $this->_height);
  157. $white = imagecolorallocate($this->_image, 255, 255, 255);
  158. $grey = imagecolorallocate($this->_image, 180, 180, 180);
  159. $black = imagecolorallocate($this->_image, 0, 0, 0);
  160. $code = $this->getCode();
  161. $height = $this->_height - (($this->_height - $this->_fontSize) / 2);
  162. // Add some shadow to the text
  163. imagettftext($this->_image, $this->_fontSize, 0, 41, ($height - 1), $grey, $this->_font, $this->_code);
  164. // Add the text
  165. imagettftext($this->_image, $this->_fontSize, 0, 40, $height, $black, $this->_font, $this->_code);
  166. srand();
  167. // add noise (pixels)
  168. for ($i = 1; $i <= $this->_pixels; $i++) {
  169. imagesetpixel($this->_image, rand(0, $this->_width), rand(0, $this->_height), $black);
  170. }
  171. // add noise (circles)
  172. for ($i = 1; $i <= $this->_circles; $i++) {
  173. imageellipse($this->_image, rand(0, $this->_width), rand(0, $this->_height), rand(60, 150), rand(60, 150), $black);
  174. }
  175. header("Content-Type: image/png");
  176. imagepng($this->_image);
  177. imagedestroy($this->_image);
  178. }
  179. public function render()
  180. {
  181. return '<input type="text" name="' . $this->_name . '" '
  182. . $this->renderAttributes()
  183. . $this->_endTag;
  184. }
  185. }