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

/lib/util/Captcha.php

https://github.com/jcorbinredtree/framework
PHP | 106 lines | 55 code | 16 blank | 35 comment | 2 complexity | c41c24462baea8a8998f57b1625f12d6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Captcha class definition
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: The contents of this file are subject to the Mozilla Public License Version 1.1
  8. * (the "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  13. * the specific language governing rights and limitations under the License.
  14. *
  15. * The Original Code is Red Tree Systems Code.
  16. *
  17. * The Initial Developer of the Original Code is Red Tree Systems, LLC. All Rights Reserved.
  18. *
  19. * @author Red Tree Systems, LLC <support@redtreesystems.com>
  20. * @copyright 2007 Red Tree Systems, LLC
  21. * @license MPL 1.1
  22. * @version 1.0
  23. * @link http://framework.redtreesystems.com
  24. */
  25. /**
  26. * Simple CAPTCHA class
  27. *
  28. * @static
  29. * @package Utils
  30. */
  31. class Captcha
  32. {
  33. const WIDTH = 150;
  34. const HEIGHT = 20;
  35. private function __construct()
  36. {
  37. }
  38. static private function GetCaptchaString()
  39. {
  40. return
  41. preg_replace_callback('/[^a-z]/i',
  42. create_function('', 'return chr(mt_rand(65, 90));'),
  43. substr(
  44. md5(
  45. mt_rand()
  46. ),
  47. 0,
  48. mt_rand(3, 5)
  49. )
  50. );
  51. }
  52. static public function Display($key)
  53. {
  54. $captcha = Captcha::GetCaptchaString();
  55. $_SESSION[$key] = $captcha;
  56. $im = @imagecreate(Captcha::WIDTH, Captcha::HEIGHT) or die("CAPTCHA Unsupported");
  57. $background_color = imagecolorallocate($im, 255, 255, 255);
  58. $chars = str_split($captcha);
  59. $halfWidth = (Captcha::WIDTH / 2);
  60. $halfHeight = (Captcha::HEIGHT / 2);
  61. /*
  62. * text
  63. */
  64. $i = 0;
  65. foreach($chars as $char){
  66. $text_color = imagecolorallocate($im, mt_rand(0, 125), mt_rand(0, 125), mt_rand(0, 125));
  67. imagechar($im, mt_rand(4, 5), mt_rand($i, $i+5), mt_rand(0, 7), $char, $text_color);
  68. $i += mt_rand(15, 40);
  69. }
  70. /*
  71. * noise
  72. */
  73. for ($numlines = mt_rand(2, 4); $numlines > 0; $numlines--) {
  74. $line_color = imagecolorallocate($im, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
  75. $xStart = mt_rand(0, $halfWidth);
  76. $yStart = mt_rand(0, $halfHeight);
  77. $xEnd = mt_rand($xStart, Captcha::WIDTH);
  78. $yEnd = mt_rand($yStart, Captcha::HEIGHT);
  79. imageline($im, $xStart, $yStart, $xEnd, $yEnd, $line_color);
  80. }
  81. for ($pixels = Captcha::WIDTH + Captcha::HEIGHT; $pixels > 0; $pixels--) {
  82. $color = imagecolorallocate($im, mt_rand(125, 255), mt_rand(125, 255), mt_rand(125, 255));
  83. imagesetpixel($im, mt_rand(0, Captcha::WIDTH), mt_rand(0, Captcha::HEIGHT), $color);
  84. }
  85. header("Content-type: image/jpeg");
  86. imagejpeg($im, null, 100);
  87. imagedestroy($im);
  88. }
  89. }
  90. ?>