/components/com_rwcards/captcha/class.captcha.php

https://github.com/MaBelleEcole/Main · PHP · 150 lines · 85 code · 23 blank · 42 comment · 10 complexity · 324f71274c01269c68456cdef08951ff MD5 · raw file

  1. <?php
  2. /*
  3. Jax Captcha Class v1.o1 - Copyright (c) 2005, Andreas John aka Jack (tR)
  4. This program and it's moduls are Open Source in terms of General Public License (GPL) v2.0
  5. class.captcha.php (captcha class module)
  6. Last modification: 2005-09-05
  7. */
  8. class captcha
  9. {
  10. var $session_key = null;
  11. var $temp_dir = null;
  12. var $width = 150;
  13. var $height = 60;
  14. var $jpg_quality = 100;
  15. /**
  16. * Constructor - Initializes Captcha class!
  17. *
  18. * @param string $session_key
  19. * @param string $temp_dir
  20. * @return captcha
  21. */
  22. function captcha( $session_key, $temp_dir )
  23. {
  24. $this->session_key = $session_key;
  25. $this->temp_dir = $temp_dir;
  26. }
  27. /**
  28. * Generates Image file for captcha
  29. *
  30. * @param string $location
  31. * @param string $char_seq
  32. * @return unknown
  33. */
  34. function _generate_image( $location, $char_seq )
  35. {
  36. $num_chars = strlen($char_seq);
  37. $img = imagecreatetruecolor( $this->width, $this->height );
  38. imagealphablending($img, 1);
  39. imagecolortransparent( $img );
  40. // generate background of randomly built ellipses
  41. for ($i=1; $i<=200; $i++)
  42. {
  43. $r = round( rand( 0, 100 ) );
  44. $g = round( rand( 0, 100 ) );
  45. $b = round( rand( 0, 100 ) );
  46. $color = imagecolorallocate( $img, $r, $g, $b );
  47. imagefilledellipse( $img,round(rand(0,$this->width)), round(rand(0,$this->height)), round(rand(0,$this->width/16)), round(rand(0,$this->height/4)), $color );
  48. }
  49. $start_x = round($this->width / $num_chars);
  50. $max_font_size = $start_x;
  51. $start_x = round(0.5*$start_x);
  52. $max_x_ofs = round($max_font_size*0.9);
  53. // set each letter with random angle, size and color
  54. for ($i=0;$i<=$num_chars;$i++)
  55. {
  56. $r = round( rand( 127, 255 ) );
  57. $g = round( rand( 127, 255 ) );
  58. $b = round( rand( 127, 255 ) );
  59. $y_pos = ($this->height/2)+round( rand( 5, 20 ) );
  60. $fontsize = round( rand( 18, $max_font_size) );
  61. $color = imagecolorallocate( $img, $r, $g, $b);
  62. $presign = round( rand( 0, 1 ) );
  63. $angle = round( rand( 0, 25 ) );
  64. if ($presign==true) $angle = -1*$angle;
  65. ImageTTFText( $img, $fontsize, $angle, $start_x+$i*$max_x_ofs, $y_pos, $color, './components/com_rwcards/captcha/arial.ttf', substr($char_seq,$i,1) );
  66. }
  67. // create image file
  68. imagejpeg( $img, $location, $this->jpg_quality );
  69. //flush();
  70. imagedestroy( $img );
  71. return true;
  72. }
  73. /**
  74. * Returns name of the new generated captcha image file
  75. *
  76. * @param unknown_type $num_chars
  77. * @return unknown
  78. */
  79. function get_pic( $num_chars=8 )
  80. {
  81. // define characters of which the captcha can consist
  82. $alphabet = array(
  83. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  84. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  85. '1','2','3','4','5','6','7','8','9','0' );
  86. $max = sizeof( $alphabet );
  87. // generate random string
  88. $captcha_str = '';
  89. for ($i=1;$i<=$num_chars;$i++) // from 1..$num_chars
  90. {
  91. // choose randomly a character from alphabet and append it to string
  92. $chosen = rand( 1, $max );
  93. $captcha_str .= $alphabet[$chosen-1];
  94. }
  95. // generate a picture file that displays the random string
  96. if ( $this->_generate_image( $this->temp_dir.'/'.'cap_'.md5( strtolower( $captcha_str )).'.jpg' , $captcha_str ) )
  97. {
  98. $fh = fopen( $this->temp_dir.'/'.'cap_'.$this->session_key.'.txt', "w" );
  99. fputs( $fh, md5( strtolower( $captcha_str ) ) );
  100. return( md5( strtolower( $captcha_str ) ) );
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. /**
  108. * check hash of password against hash of searched characters
  109. *
  110. * @param string $char_seq
  111. * @return boolean
  112. */
  113. function verify( $char_seq )
  114. {
  115. $file = $this->temp_dir.'/'.'cap_'.$this->session_key.'.txt';
  116. if (! file_exists( $file ) ) return false;
  117. $fh = fopen( $file, "r" );
  118. $hash = fgets( $fh );
  119. if (md5(strtolower($char_seq)) == $hash)
  120. return true;
  121. else
  122. return false;
  123. }
  124. }
  125. ?>