PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/sfFormExtraPlugin/lib/validator/sfValidatorReCaptcha.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 134 lines | 60 code | 12 blank | 62 comment | 6 complexity | 71d741ed1a332e2c3c5e5eba43061123 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorReCaptcha validates a ReCaptcha.
  11. *
  12. * This validator uses ReCaptcha: http://recaptcha.net/
  13. *
  14. * The ReCaptcha API documentation can be found at http://recaptcha.net/apidocs/captcha/
  15. *
  16. * To be able to use this validator, you need an API key: http://recaptcha.net/api/getkey
  17. *
  18. * To create a captcha validator:
  19. *
  20. * $captcha = new sfValidatorReCaptcha(array('private_key' => RECAPTCHA_PRIVATE_KEY));
  21. *
  22. * where RECAPTCHA_PRIVATE_KEY is the ReCaptcha private key.
  23. *
  24. * @package symfony
  25. * @subpackage validator
  26. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  27. * @version SVN: $Id: sfValidatorReCaptcha.class.php 7903 2008-03-15 13:17:41Z fabien $
  28. */
  29. class sfValidatorReCaptcha extends sfValidatorBase
  30. {
  31. /**
  32. * Configures the current validator.
  33. *
  34. * Available options:
  35. *
  36. * * private_key: The ReCaptcha private key (required)
  37. * * remote_addr: The remote address of the user
  38. * * server_host: The ReCaptcha server host
  39. * * server_port: The ReCaptcha server port
  40. * * server_path: The ReCatpcha server path
  41. * * server_timeout: The timeout to use when contacting the ReCaptcha server
  42. *
  43. * Available error codes:
  44. *
  45. * * captcha
  46. * * server_problem
  47. *
  48. * @see sfValidatorBase
  49. */
  50. protected function configure($options = array(), $messages = array())
  51. {
  52. $this->addRequiredOption('private_key');
  53. $this->addOption('remote_addr');
  54. $this->addOption('server_host', 'api-verify.recaptcha.net');
  55. $this->addOption('server_port', 80);
  56. $this->addOption('server_path', '/verify');
  57. $this->addOption('server_timeout', 10);
  58. $this->addMessage('captcha', 'The captcha is not valid (%error%).');
  59. $this->addMessage('server_problem', 'Unable to check the captcha from the server (%error%).');
  60. }
  61. /**
  62. * Cleans the input value.
  63. *
  64. * The input value must be an array with 2 required keys: recaptcha_challenge_field and recaptcha_response_field.
  65. *
  66. * It always returns null.
  67. *
  68. * @see sfValidatorBase
  69. */
  70. protected function doClean($value)
  71. {
  72. $challenge = isset($value['recaptcha_challenge_field']) ? $value['recaptcha_challenge_field'] : null;
  73. $response = isset($value['recaptcha_response_field']) ? $value['recaptcha_response_field'] : null;
  74. if (empty($challenge) || empty($response))
  75. {
  76. throw new sfValidatorError($this, 'captcha', array('error' => 'invalid captcha'));
  77. }
  78. if (true !== ($answer = $this->check(array(
  79. 'privatekey' => $this->getOption('private_key'),
  80. 'remoteip' => $this->getOption('remote_addr') ? $this->getOption('remote_addr') : $_SERVER['REMOTE_ADDR'],
  81. 'challenge' => $challenge,
  82. 'response' => $response,
  83. ))))
  84. {
  85. throw new sfValidatorError($this, 'captcha', array('error' => $answer));
  86. }
  87. return null;
  88. }
  89. /**
  90. * Returns true if the captcha is valid.
  91. *
  92. * @param array An array of request parameters
  93. *
  94. * @return Boolean true if the captcha is valid, false otherwise
  95. */
  96. protected function check($parameters)
  97. {
  98. if (false === ($fs = @fsockopen($this->getOption('server_host'), $this->getOption('server_port'), $errno, $errstr, $this->getOption('server_timeout'))))
  99. {
  100. throw new sfValidatorError($this, 'server_problem', array('error' => $errstr));
  101. }
  102. $query = http_build_query($parameters, null, '&');
  103. fwrite($fs, sprintf(
  104. "POST %s HTTP/1.0\r\n".
  105. "Host: %s\r\n".
  106. "Content-Type: application/x-www-form-urlencoded\r\n".
  107. "Content-Length: %d\r\n".
  108. "User-Agent: reCAPTCHA/PHP/symfony\r\n".
  109. "\r\n%s",
  110. $this->getOption('server_path'), $this->getOption('server_host'), strlen($query), $query)
  111. );
  112. $response = '';
  113. while (!feof($fs))
  114. {
  115. $response .= fgets($fs, 1160);
  116. }
  117. fclose($fs);
  118. $response = explode("\r\n\r\n", $response, 2);
  119. $answers = explode("\n", $response[1]);
  120. return 'true' == trim($answers[0]) ? true : $answers[1];
  121. }
  122. }