PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/ts.php

https://bitbucket.org/jonarano/joneame
PHP | 134 lines | 84 code | 16 blank | 34 comment | 18 complexity | f232036c2e3385fa68175d19b0521f70 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. // Modified and adapted by Ricardo Galli from:
  3. /*
  4. * File: CaptchaSecurityImages.php
  5. * Author: Simon Jarvis
  6. * Copyright: 2006 Simon Jarvis
  7. * Date: 03/08/06
  8. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details:
  19. * http://www.gnu.org/licenses/gpl.html
  20. *
  21. */
  22. if (empty($globals['recaptcha_public_key']) || empty($globals['recaptcha_private_key'])) {
  23. session_cache_expire(15);
  24. session_name('mnm_captcha');
  25. session_start();
  26. } else {
  27. require_once(mnminclude.'recaptchalib.php');
  28. }
  29. class CaptchaSecurityImages {
  30. var $font = 'adler.ttf';
  31. function generateCode($characters) {
  32. /* list all possible characters, similar looking characters and vowels have been removed */
  33. $possible = '23456789bcdfghjkmnpqrstvwxyz';
  34. $code = '';
  35. $i = 0;
  36. while ($i < $characters) {
  37. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  38. $i++;
  39. }
  40. return $code;
  41. }
  42. function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  43. $code = $this->generateCode($characters);
  44. /* font size will be 75% of the image height */
  45. // changed to .55 for Adler
  46. $font_size = $height * 0.55;
  47. $image = @imagecreate($width, $height) or die('Cannot Initialize new GD image stream');
  48. /* set the colours */
  49. $background_color = imagecolorallocate($image, 255, 255, 255);
  50. //$text_color = imagecolorallocate($image, 20, 40, 100);
  51. $text_color = imagecolorallocate($image, mt_rand(140,148),mt_rand(72,80),mt_rand(0,5));
  52. //$noise_color = imagecolorallocate($image, 100, 120, 180);
  53. $noise_color = imagecolorallocate($image, mt_rand(250,255), mt_rand(95,105), mt_rand(0,5));
  54. /* generate random dots in background */
  55. for( $i=0; $i<($width*$height)/3; $i++ ) {
  56. $x1 = mt_rand(0,$width);
  57. $y1 = mt_rand(0,$height);
  58. imagefilledrectangle($image, $x1, $y1, $x1, $y1, $noise_color);
  59. }
  60. /* generate random lines in background */
  61. for( $i=0; $i<($width*$height)/150; $i++ ) {
  62. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  63. }
  64. /* create textbox and add text */
  65. $textbox = imagettfbbox($font_size, 0, $this->font, $code);
  66. $x = ($width - $textbox[4])/2;
  67. // Changed to 2.3 for adler
  68. $y = ($height - $textbox[5])/2.3;
  69. imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code);
  70. /* output captcha image to browser */
  71. imagejpeg($image);
  72. imagedestroy($image);
  73. $_SESSION['security_code'] = $code;
  74. }
  75. }
  76. function ts_gfx() {
  77. // Hack to avoid problems with monofont.ttf
  78. putenv('GDFONTPATH=' . mnminclude);
  79. header('Content-Type: image/jpeg');
  80. $captcha = new CaptchaSecurityImages(155,45,5);
  81. }
  82. function ts_is_human() {
  83. global $globals;
  84. if (empty($globals['recaptcha_public_key']) || empty($globals['recaptcha_private_key'])) {
  85. $result = !empty($_SESSION['security_code']) && $_SESSION['security_code'] == $_POST['security_code'];
  86. if ($result) {
  87. $_SESSION['security_code'] = '';
  88. return true;
  89. }
  90. return false;
  91. } else {
  92. if ($_POST["recaptcha_response_field"]) {
  93. $resp = recaptcha_check_answer ($globals['recaptcha_private_key'],
  94. $_SERVER["REMOTE_ADDR"],
  95. $_POST["recaptcha_challenge_field"],
  96. $_POST["recaptcha_response_field"]);
  97. if ($resp->is_valid) {
  98. return true;
  99. } else {
  100. # set the error code so that we can display it
  101. $globals['error'] = $resp->error;
  102. }
  103. }
  104. return false;
  105. }
  106. }
  107. function ts_print_form() {
  108. global $globals;
  109. if (empty($globals['recaptcha_public_key']) || empty($globals['recaptcha_private_key'])) {
  110. echo _("introduce el texto de la imagen:")."<br/>\n";
  111. echo '<div class="tc"><img src="ts_image.php" alt="code number"/></div>';
  112. echo '<input type="text" size="20" name="security_code" /><br/>'."\n";
  113. } else {
  114. // reCaptcha
  115. echo recaptcha_get_html($globals['recaptcha_public_key'],null);
  116. }
  117. }
  118. ?>