PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/captcha/captcha.php

http://snowcms.googlecode.com/
PHP | 180 lines | 102 code | 26 blank | 52 comment | 16 complexity | abf5eff5665f6783be8bbc0826a4aff6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////
  3. // SnowCMS v2.0 //
  4. // By the SnowCMS Team //
  5. // www.snowcms.com //
  6. // Released under the Microsoft Reciprocal License //
  7. // www.opensource.org/licenses/ms-rl.html //
  8. ////////////////////////////////////////////////////////////////////////////
  9. // //
  10. // SnowCMS originally pawned by soren121 started in early 2008 //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////
  13. // //
  14. // SnowCMS v2.0 began in November 2009 //
  15. // //
  16. ////////////////////////////////////////////////////////////////////////////
  17. // File version: SnowCMS 2.0 //
  18. ////////////////////////////////////////////////////////////////////////////
  19. if(!defined('INSNOW'))
  20. {
  21. die('Nice try...');
  22. }
  23. // Title: Display CAPTCHA
  24. /*
  25. Function: captcha_display
  26. Renders a CAPTCHA image, for those oh-so-fun CAPTCHA tests.
  27. Parameters:
  28. none
  29. Returns:
  30. void - Nothing is returned by this function.
  31. Note:
  32. This function is overloadable.
  33. */
  34. function captcha_display()
  35. {
  36. global $api, $settings;
  37. $api->run_hooks('captcha_display');
  38. # Now, let's make sure that the server supports this, it uses GD...
  39. if(!function_exists('imagecreate'))
  40. {
  41. die(l('Your server configuration does not support the <a href="%s">GD extension</a>.', 'http://www.php.net/gd'));
  42. }
  43. # We need an identifier for this CAPTCHA image.
  44. elseif(empty($_GET['id']))
  45. {
  46. die(l('No CAPTCHA identifier was supplied, could not complete your request.'));
  47. }
  48. # Do you have GD2? Then we can use imagecreatetruecolor.
  49. $gd_info = gd_info();
  50. $gd_version = substr($gd_info['GD Version'], strpos($gd_info['GD Version'], '(') + 1, strpos($gd_info['GD Version'], ' ', strpos($gd_info['GD Version'], '(')) - (strpos($gd_info['GD Version'], '(') + 1));
  51. if(version_compare($gd_version, '2') >= 0)
  52. {
  53. $image = imagecreatetruecolor($settings->get('captcha_width', 'int'), $settings->get('captcha_height', 'int'));
  54. }
  55. else
  56. {
  57. $image = imagecreate($settings->get('captcha_width', 'int'), $settings->get('captcha_height', 'int'));
  58. }
  59. # Make our background color.
  60. $background = imagecolorallocate($image, $api->apply_filters('captcha_bg_red', 255), $api->apply_filters('captcha_bg_green', 255), $api->apply_filters('captcha_bg_blue', 255));
  61. # Apply the color.
  62. imagefill($image, 0, 0, $background);
  63. # Let's make some noise!
  64. # In order to do so, let's make some random colors, shall we?
  65. $colors = array();
  66. for($i = 0; $i < 32; $i++)
  67. {
  68. $colors[] = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  69. }
  70. # Some background noise.
  71. $num_ellipses = mt_rand(10, 15);
  72. for($i = 0; $i < $num_ellipses; $i++)
  73. {
  74. imageellipse($image, mt_rand(0, $settings->get('captcha_width', 'int')), mt_rand(0, $settings->get('captcha_height', 'int')), mt_rand(1, 50), mt_rand(1, 25), $colors[array_rand($colors)]);
  75. }
  76. # Now for the text, 5-6 is usually good.
  77. $num_chars = $settings->get('captcha_num_chars', 'int');
  78. # The characters which we will use to put into the CAPTCHA.
  79. $chars = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789');
  80. $rand_str = '';
  81. for($i = 0; $i < $num_chars; $i++)
  82. {
  83. $rand_str .= $chars[mt_rand(0, strlen($chars) - 1)];
  84. }
  85. # Save it to their session, otherwise, this does no good!!!
  86. if(!isset($_SESSION['captcha_text']) || !is_array($_SESSION['captcha_text']))
  87. {
  88. $_SESSION['captcha_text'] = array();
  89. }
  90. $_SESSION['captcha_text'][$_GET['id']] = $rand_str;
  91. # TrueType font support? If so you are AWESOME.
  92. if(function_exists('imagettftext'))
  93. {
  94. $fonts = scandir(dirname(__FILE__). '/ttf/');
  95. foreach($fonts as $key => $font)
  96. {
  97. if(substr($font, strlen($font) - 3, strlen($font)) != 'ttf')
  98. {
  99. unset($fonts[$key]);
  100. }
  101. }
  102. for($i = 0, $x = 10; $i < $num_chars; $i++, $x += 30)
  103. {
  104. $size = ceil($settings->get('captcha_height', 'int') / 2) + mt_rand(-2, 2);
  105. $angle = mt_rand(-30, 30);
  106. $y = ceil($settings->get('captcha_height', 'int') / 2) + mt_rand(0, floor($settings->get('captcha_height', 'int') / 2) - 10);
  107. $fontfile = dirname(__FILE__). '/ttf/'. $fonts[array_rand($fonts)];
  108. imagettftext($image, $size, $angle, $x - 2, $y - 2, $colors[array_rand($colors)], $fontfile, $rand_str[$i]);
  109. imagettftext($image, $size, $angle, $x, $y, $colors[array_rand($colors)], $fontfile, $rand_str[$i]);
  110. }
  111. }
  112. else
  113. {
  114. # Dang... No TrueType support? It won't be as good, but it is possible.
  115. for($i = 0, $x = 10; $i < $num_chars; $i ++, $x += 30)
  116. {
  117. # Get the character image.
  118. $char_image = imagecreatefrompng(dirname(__FILE__). '/chars/'. strtoupper($rand_str[$i]). '.png');
  119. # Rotate the image.
  120. $char_image = imagerotate($char_image, mt_rand(-30, 30), imagecolortransparent($char_image, imagecolorallocate($char_image, 255, 255, 255)));
  121. # Copy it over.
  122. imagecopy($image, $char_image, $x, mt_rand(0, floor($settings->get('captcha_height', 'int') / 2) - 10), 0, 0, imagesx($char_image), imagesy($char_image));
  123. imagedestroy($char_image);
  124. }
  125. }
  126. # Let's create a grid
  127. $vertical = $settings->get('captcha_width', 'int') / 20;
  128. for($i = 0; $i < $vertical; $i++)
  129. {
  130. imageline($image, (20 * $i) + mt_rand(-5, 5), 0, (20 * $i) + mt_rand(-5, 5), $settings->get('captcha_height', 'int'), $colors[array_rand($colors)]);
  131. }
  132. $horizontal = $settings->get('captcha_height', 'int') / 10;
  133. for($i = 0; $i < $horizontal; $i++)
  134. {
  135. imageline($image, 0, (10 * $i) + mt_rand(-5, 5), $settings->get('captcha_width', 'int'), (10 * $i) + mt_rand(-5, 5), $colors[array_rand($colors)]);
  136. }
  137. # Remove any previous headers so we can send out new ones!
  138. if(ob_get_length() > 0)
  139. {
  140. ob_clean();
  141. }
  142. header('Pragma: no-cache');
  143. header('Content-Type: image/png');
  144. # Display the image.
  145. imagepng($image);
  146. # Destroy! Destory! >:D
  147. imagedestroy($image);
  148. exit;
  149. }
  150. ?>