PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/captcha.php

https://github.com/leviself/Feedmailer
PHP | 149 lines | 87 code | 24 blank | 38 comment | 6 complexity | 58c4d62f8060d21834beb3e4299cf3fc MD5 | raw file
  1. <?php
  2. session_start();
  3. /*
  4. * File: CaptchaSecurityImages.php
  5. * Author: Simon Jarvis
  6. * Copyright: 2006 Simon Jarvis
  7. * Date: 03/08/06
  8. * Updated: 07/02/07
  9. * Requirements: PHP 4/5 with GD and FreeType libraries
  10. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details:
  21. * http://www.gnu.org/licenses/gpl.html
  22. *
  23. */
  24. class CaptchaSecurityImages {
  25. /**
  26. * Name: Load the MySQL Connection
  27. * Usage: $this->db();
  28. * Usage: $this->mysql->function();
  29. **/
  30. public function db(){
  31. include dirname(__FILE__)."/inc/class.db.php";
  32. $this->mysql = new db();
  33. return $this->mysql->connect();
  34. }
  35. function vars(){
  36. $this->db();
  37. $var = array(
  38. "font" => $this->mysql->setting(captcha_font),
  39. "fontsize" => $this->mysql->setting(captcha_fontsize),
  40. "color" => array(
  41. "background" => $this->mysql->setting(captcha_color_background),
  42. "text" => $this->mysql->setting(captcha_color_text),
  43. "noise" => $this->mysql->setting(captcha_color_noise)),
  44. "lines" => $this->mysql->setting(captcha_lines),
  45. "dots" => $this->mysql->setting(captcha_dots),
  46. );
  47. return $this->var = $var;
  48. $this->mysql->disconnect();
  49. }
  50. function explodeColor($color){
  51. $values = explode(", ", $color);
  52. $colors = array(
  53. "r" => $values[0],
  54. "g" => $values[1],
  55. "b" => $values[2]
  56. );
  57. return $colors;
  58. }
  59. function generateCode($characters) {
  60. /* list all possible characters, similar looking characters and vowels have been removed */
  61. $possible = '23456789bcdfghjkmnpqrstvwxyz';
  62. $code = '';
  63. $i = 0;
  64. while ($i < $characters) {
  65. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  66. $i++;
  67. }
  68. return $code;
  69. }
  70. function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  71. // Load the variables
  72. $this->vars();
  73. // Generate the random code
  74. $code = $this->generateCode($characters);
  75. // Font name
  76. $font = "./".$this->var[font];
  77. /* font size will be 75% of the image height
  78. $font_size = $height * 0.60;*/
  79. $font_size = $this->var[fontsize];
  80. // Assign a variable for the image.
  81. $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  82. /* set the colours */
  83. $b_color = $this->explodeColor($this->var[color][background]);
  84. $t_color = $this->explodeColor($this->var[color][text]);
  85. $n_color = $this->explodeColor($this->var[color][noise]);
  86. $background_color = imagecolorallocate(
  87. $image,
  88. $b_color[r],
  89. $b_color[g],
  90. $b_color[b]);
  91. $text_color = imagecolorallocate(
  92. $image,
  93. $t_color[r],
  94. $t_color[g],
  95. $t_color[b]);
  96. $noise_color = imagecolorallocate(
  97. $image,
  98. $n_color[r],
  99. $n_color[g],
  100. $n_color[b]);
  101. /* generate random dots in background */
  102. for ($i=0; $i < $this->var[dots] ; $i++) {
  103. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  104. }
  105. /* generate random lines in background */
  106. for ($i=0; $i < $this->var[lines]; $i++) {
  107. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  108. }
  109. /* create textbox and add text */
  110. $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function');
  111. $x = ($width - $textbox[4])/2;
  112. $y = ($height - $textbox[5])/2;
  113. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function');
  114. /* output captcha image to browser */
  115. header('Content-Type: image/jpeg');
  116. imagejpeg($image);
  117. imagedestroy($image);
  118. $_SESSION['security_code'] = $code;
  119. }
  120. }
  121. $width = isset($_GET['width']) && $_GET['width'] < 600 ? $_GET['width'] : '120';
  122. $height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
  123. $characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';
  124. $captcha = new CaptchaSecurityImages($width,$height,$characters);
  125. ?>