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