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

/captcha.php

https://github.com/stormeus/Kusaba-Z
PHP | 133 lines | 58 code | 14 blank | 61 comment | 8 complexity | a6ec0e470a5d9d9acc681b2310f804ba MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of kusaba.
  4. *
  5. * kusaba is free software; you can redistribute it and/or modify it under the
  6. * terms of the GNU General Public License as published by the Free Software
  7. * Foundation; either version 2 of the License, or (at your option) any later
  8. * version.
  9. *
  10. * kusaba is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * kusaba; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * Captcha display
  20. *
  21. * Generates a random word and stores it in a session variable, which is later used as a verification that the poster is in fact a human
  22. *
  23. * @package kusaba
  24. */
  25. /**
  26. * Start the session
  27. */
  28. session_start();
  29. $preconfig_db_unnecessary = true;
  30. /**
  31. * Require the configuration file
  32. */
  33. require 'config.php';
  34. /*
  35. * File: CaptchaSecurityImages.php
  36. * Author: Simon Jarvis
  37. * Copyright: 2006 Simon Jarvis
  38. * Date: 03/08/06
  39. * Updated: 07/02/07
  40. * Requirements: PHP 4/5 with GD and FreeType libraries
  41. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  42. *
  43. * This program is free software; you can redistribute it and/or
  44. * modify it under the terms of the GNU General Public License
  45. * as published by the Free Software Foundation; either version 2
  46. * of the License, or (at your option) any later version.
  47. *
  48. * This program is distributed in the hope that it will be useful,
  49. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  51. * GNU General Public License for more details:
  52. * http://www.gnu.org/licenses/gpl.html
  53. *
  54. */
  55. /**
  56. * Captcha image class
  57. *
  58. * @package kusaba
  59. */
  60. class CaptchaSecurityImages {
  61. function CaptchaSecurityImages($width='120',$height='40',$characters='6',$font) {
  62. global $font,$font_ballback;
  63. require_once KU_ROOTDIR . 'inc/classes/randword.class.php';
  64. $randword_class = new Rand_Word;
  65. $code = $randword_class->rand_word($characters);
  66. /* font size will be 75% of the image height */
  67. $font_size = $height * 0.85;
  68. $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  69. /* set the colours */
  70. $background_color = imagecolorallocate($image, 255, 255, 255);
  71. $text_color = imagecolorallocate($image, 35, 45, 100);
  72. $noise_color = imagecolorallocate($image, 100, 120, 180);
  73. /* generate random dots in background */
  74. if (@imagettfbbox($font_size, 0, $font, $code)) {
  75. $ttf_supported = true;
  76. for( $i=0; $i<($width*$height)/3; $i++ ) {
  77. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  78. }
  79. } else {
  80. $ttf_supported = false;
  81. for( $i=0; $i<($width*$height)/12; $i++ ) {
  82. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  83. }
  84. }
  85. /* generate random lines in background */
  86. if ($ttf_supported) {
  87. for( $i=0; $i<($width*$height)/150; $i++ ) {
  88. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  89. }
  90. }
  91. if ($ttf_supported) {
  92. $textbox = imagettfbbox($font_size, 0, $font, $code);
  93. $x = ($width - $textbox[4])/2;
  94. $y = ($height - $textbox[5])/2;
  95. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code);
  96. } else {
  97. $x = 4;
  98. $y = 5;
  99. imagestring($image, $font_ballback, $x, $y, $code, $text_color);
  100. }
  101. /* create textbox and add text*/
  102. $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function');
  103. $x = ($width - $textbox[4])/2;
  104. $y = ($height - $textbox[5])/2;
  105. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function');
  106. /*output captcha image to browser */
  107. header('Content-Type: image/jpeg');
  108. imagejpeg($image);
  109. imagedestroy($image);
  110. $_SESSION['security_code'] = $code;
  111. }
  112. }
  113. $width = 90;
  114. $height = 30;
  115. $characters = 6;
  116. $font = KU_ROOTDIR . 'lib/fonts/monofont.ttf';
  117. $font_fallback = imageloadfont(KU_ROOTDIR . 'lib/fonts/captchafont.gdf');
  118. $captcha = new CaptchaSecurityImages($width,$height,$characters,$font);
  119. ?>