PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/4.9/includes/captcha.php

http://miacms.googlecode.com/
PHP | 161 lines | 118 code | 25 blank | 18 comment | 9 complexity | 00954418cd569b101242c8050787aece MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. $c_name = 'mos_captcha';
  3. $c_width = '250';
  4. $c_height = '70';
  5. $c_imgtype = 'png';
  6. $c_codetype = 'true';
  7. /**
  8. * Spam Protection - Code Image Generator - 2006 Dominik Paulus
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  10. * @author: Dominik Paulus, [email]mail@dpaulus.de[/email]
  11. * @date: 06/24/06
  12. * @version: 3.4c
  13. *
  14. */
  15. // Imagecolors
  16. $colors = array(
  17. 255, 244, 234, // Background
  18. 255, 128, 0, // Code
  19. 255, 128, 0, // Vertical Lines
  20. 255, 128, 0 // Border (Last value without ',')
  21. );
  22. $x = $c_width;
  23. $y = $c_height;
  24. $y2 = $y/2; $x2 = $x/2;
  25. session_start();
  26. if (!isset($_SESSION['initiated'])) {
  27. session_regenerate_id(true);
  28. $_SESSION['initiated'] = true;
  29. }
  30. $_SESSION['captcha_img'] = "OK"; // debug
  31. mt_srand((double)microtime()*1000000);
  32. // Fontsetup
  33. $font = './captchaFonts/font1.ttf';
  34. // Numerical code
  35. if($c_codetype)
  36. $seccode = strval(mt_rand(10000, 99999));
  37. else {
  38. $string = "abcdefghjkmnpqrstuvwxyz0123456789";
  39. $stringlen = strlen($string);
  40. $seccode = "";
  41. for($i = 0; $i < 5; $i++)
  42. $seccode .= $string{mt_rand(0, $stringlen)};
  43. }
  44. $_SESSION['captcha_code'] = $seccode;
  45. $clen = strlen($seccode);
  46. // create image
  47. $im = ImageCreateTrueColor($x, $y) or die('ImageCreate error!');
  48. // Image colors
  49. $bgcolor = ImageColorAllocate($im, $colors[0], $colors[1], $colors[2]);
  50. $fontcolor = ImageColorAllocate($im, $colors[3], $colors[4], $colors[5]);
  51. $linecolor = ImageColorAllocate($im, $colors[6], $colors[7], $colors[8]);
  52. $bordercolor = ImageColorAllocate($im, $colors[9], $colors[10], $colors[11]);
  53. $alphacolor = ImageColorAllocate($im, 0, 255, 0);
  54. ImageFill($im, 0, 0, $bgcolor);
  55. // Code
  56. $xspace =70;
  57. $yspace = 60;
  58. $size = 25;
  59. $angle = 20;
  60. // Morph
  61. function morph($im, $sx, $sy, $w, $h) {
  62. $morphx = $h;
  63. $morphy = mt_rand(3.5,5.2);
  64. $mx = $sx;
  65. $my = $sy;
  66. $mvalues = array();
  67. for($i = 0; $i < $morphx/2; $i++) {
  68. $mvalues[] = $mx-(log($i+1)*$morphy);
  69. ImageCopyMerge($im, $im, $mvalues[$i], $my+$i, $mx, $my+$i, $w+20, 1, 0);
  70. }
  71. $mvalues = array_reverse($mvalues);
  72. $mvcount = count($mvalues);
  73. for($i = 0; $i < $mvcount; $i++) {
  74. ImageCopyMerge($im, $im, $mvalues[$i], $my+$i+$mvcount, $mx, $my+$i+$mvcount, $w+20, 1, 0);
  75. }
  76. }
  77. $ttfborders = array();
  78. for($i = 0; $i < $clen; $i++) {
  79. $tmp = ImageCreateTrueColor($xspace,$yspace);
  80. ImageFill($tmp, 0, 0, $bgcolor);
  81. $ttfborders[] = ImageTTFText($tmp, $size+mt_rand(0, 8), mt_rand(-$angle, $angle), 20,
  82. $yspace-10, $fontcolor, $font, $seccode{$i}
  83. );
  84. morph($tmp, 0, 0, 20, 20);
  85. ImageColorTransparent($tmp, $bgcolor);
  86. ImageCopyMerge($im, $tmp, ($i)*50, 2, 0, 0, $xspace, $yspace, 100);
  87. ImageDestroy($tmp);
  88. }
  89. // Wave
  90. ImageSetThickness($im, 3);
  91. $ux = $uy = 0;
  92. $vx = 0; //mt_rand(10,15);
  93. $vy = mt_rand($y2-3, $y2+3);
  94. for($i = 0; $i < 10; $i++) {
  95. $ux = $vx + mt_rand(20,30);
  96. $uy = mt_rand($y2-8,$y2+8);
  97. ImageSetThickness($im, mt_rand(1,2));
  98. ImageLine($im, $vx, $vy, $ux, $uy, $linecolor);
  99. $vx = $ux;
  100. $vy = $uy;
  101. }
  102. ImageLine($im, $vx, $vy, $x, $y2, $linecolor);
  103. // Triangle
  104. ImageSetThickness($im, 3);
  105. $ux = mt_rand($x2-10, $x2+10);
  106. $uy = mt_rand($y2-10, $y2-30);
  107. ImageLine($im, mt_rand(10,$x2-20), $y, $ux, $uy, $linecolor);
  108. ImageSetThickness($im, 1);
  109. ImageLine($im, mt_rand($x2+20,$x-10), $y, $ux, $uy, $linecolor);
  110. ImageSetThickness($im, 1);
  111. // Border
  112. ImageSetThickness($im, 1);
  113. ImageLine($im, 0, 0, 0, $y, $bordercolor); // left
  114. ImageLine($im, 0, 0, $x, 0, $bordercolor); // top
  115. ImageLine($im, 0, $y-1, $x, $y-1, $bordercolor); // bottom
  116. ImageLine($im, $x-1, 0, $x-1, $y-1, $bordercolor); // right
  117. for($i = $x/$clen; $i < $x; $i+=$x/$clen)
  118. ImageLine($im, $i, 0, $i, $y, $bordercolor);
  119. switch($c_imgtype) {
  120. case 'jpeg':
  121. Header("Content-Type: image/jpeg");
  122. ImageJPEG($im,"",75);
  123. break;
  124. case 'png':
  125. Header("Content-Type: image/png");
  126. ImagePNG($im);
  127. break;
  128. case 'gif':
  129. Header("Content-Type: image/gif");
  130. ImageGIF($im);
  131. break;
  132. default:
  133. die("Wrong \$type in captcha.php (should be jpeg, png or gif)\n");
  134. }
  135. ImageDestroy($im);
  136. ?>