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

/library/captcha.php

http://github.com/pateketrueke/tetlphp
PHP | 167 lines | 92 code | 43 blank | 32 comment | 16 complexity | f20a26cf65a70e477139c10e302567d1 MD5 | raw file
  1. <?php
  2. /**
  3. * Basic captcha library
  4. */
  5. // TODO: wrap in a class too?
  6. /**
  7. * Retrieve/set unique identifier
  8. *
  9. * @param mixed Code length|File
  10. * @param string Unique key
  11. * @staticvar string Charset
  12. * @return string
  13. */
  14. function captcha_id($test = 6, $key = 0) {
  15. static $chars = 'jklmn-JKLMN#rstuvwx@RSTUVWX+56789=ABCDEFG';
  16. if ( ! is_num($test)) {
  17. if ( ! is_file($test)) {
  18. return FALSE;
  19. }
  20. $test = preg_split('/\b|[\s\t]+/', read($test));
  21. $test = array_filter(array_unique($test));
  22. $hash = $test[array_rand($test, 1)];
  23. } else {
  24. $hash = '';
  25. $max = strlen($chars) - 1;
  26. if ($test > 10) {
  27. $test = 10;
  28. }
  29. while(strlen($hash) < $test) {
  30. $old = substr($chars, mt_rand(0, $max), 1);
  31. if ( ! is_false(strpos($hash, $old))) {
  32. continue;
  33. }
  34. $hash .= $old;
  35. }
  36. }
  37. session("--captcha-id$$key", $hash);
  38. return $hash;
  39. }
  40. /**
  41. * Generate the captcha image
  42. *
  43. * @param string Hash
  44. * @param integer Width
  45. * @param integer Height
  46. * @param string Fontfile
  47. * @param boolean Invert color?
  48. * @staticvar mixed Function callback
  49. * @return string
  50. */
  51. function captcha_src($hash, $width = 120, $height = 24, $font = '', $invert = FALSE) {
  52. static $negative = NULL;
  53. if (is_null($negative)) {
  54. $negative = function ($num) {
  55. return $num * -1;
  56. };
  57. }
  58. $length = strlen($hash);
  59. $resource = imagecreatetruecolor($width, $height);
  60. imagesavealpha($resource, TRUE);
  61. imagealphablending($resource, TRUE);
  62. $dark = array(mt_rand(0, 96), mt_rand(0, 96), mt_rand(0, 96));
  63. $light = array(mt_rand(127, 240), mt_rand(127, 240), mt_rand(127, 240));
  64. if (is_true($invert)) {
  65. $bgcolor = imagecolorallocatealpha($resource, 0, 0, 0, 0);
  66. $light = array_map($negative, $light);
  67. $dark = array_map($negative, $dark);
  68. } else {
  69. $bgcolor = imagecolorallocatealpha($resource, 255, 255, 255, 127);
  70. }
  71. imagefill($resource, 0, 0, $bgcolor);
  72. $bg = imagecolorallocatealpha($resource, $light[0], $light[1], $light[2], mt_rand(45, 85));
  73. $fg = imagecolorallocatealpha($resource, $dark[0], $dark[1], $dark[2], mt_rand(33, 52));
  74. $left = substr($hash, 0, ceil($length / 2));
  75. $right = substr($hash, strlen($left));
  76. $factor = floor($width / $length);
  77. $font = realpath($font);
  78. if ( ! is_file($font)) {
  79. $top = ($width * $height) / 3.3;
  80. for ($i = 0; $i < $top; $i += 1) {
  81. imagefilledellipse($resource, mt_rand(0, $width), mt_rand(0, $height), 1, mt_rand(1, 3), $bg);
  82. }
  83. for ($i = 0; $i < $length; $i += 1) {
  84. imagestring($resource, 5, ($factor / 3) + $i * $factor, mt_rand(0, $height - mt_rand(13, 20)), substr($hash, $i, 1), $fg);
  85. if (mt_rand(0, 75) % 9) {
  86. $img = imagerotate($resource, is_odd($i) ? - .8 : .7, $bgcolor);
  87. }
  88. }
  89. } else {
  90. $box = imageftbbox($factor, 0, $font, 'Q');
  91. $w = max(abs($box[2]) - abs($box[0]), 1);
  92. $h = max(abs($box[7]) - abs($box[6]), 1);
  93. imagettftext($resource, $factor, mt_rand(-4, 5), $x = mt_rand($w / strlen($left), $w * 1.3), mt_rand($h, $h * 1.33), $fg, $font, $left);
  94. for ($i = 2; $i < $factor; $i += 1) {
  95. imagettftext($resource, 16, mt_rand(20, 33), ($i - 1.5) * $factor, $i * 15, $bg, $font, str_shuffle($hash));
  96. }
  97. $a = $x + ($w * strlen($left));
  98. $b = $width - ($w * strlen($right)) - $w;
  99. imagettftext($resource, $factor, mt_rand(-3, 4), mt_rand(min($a, $b), max($a, $b)), $h * 1.20, ($fg * 1.3) / 96, $font, $right);
  100. }
  101. header('Content-Type: image/png');
  102. imagepng($resource);
  103. imagedestroy($resource);
  104. exit;
  105. }
  106. /**
  107. * Captcha code validation
  108. *
  109. * @param string Received code
  110. * @param string Unique key
  111. * @param boolean Strict?
  112. * @return boolean
  113. */
  114. function captcha_valid($test, $key = 0, $strict = FALSE) {
  115. if ( ! empty($test)) {
  116. $old = session("--captcha-id$$key");
  117. if (is_true($strict)) {
  118. return ! strcmp($old, $test);
  119. }
  120. return strtolower($old) === strtolower($test);
  121. }
  122. return FALSE;
  123. }
  124. /* EOF: ./library/captcha.php */