Validate.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_ReCaptcha
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\ReCaptcha\Model;
  21. use MSP\ReCaptcha\Api\ValidateInterface;
  22. use ReCaptcha\ReCaptcha;
  23. class Validate implements ValidateInterface
  24. {
  25. /**
  26. * @var Config
  27. */
  28. private $config;
  29. /**
  30. * Validate constructor.
  31. * @param Config $config
  32. */
  33. public function __construct(
  34. Config $config
  35. ) {
  36. $this->config = $config;
  37. }
  38. /**
  39. * Return true if reCaptcha validation has passed
  40. * @param string $reCaptchaResponse
  41. * @param string $remoteIp
  42. * @return bool
  43. */
  44. public function validate($reCaptchaResponse, $remoteIp)
  45. {
  46. $secret = $this->config->getPrivateKey();
  47. if ($reCaptchaResponse) {
  48. // @codingStandardsIgnoreStart
  49. $reCaptcha = new ReCaptcha($secret);
  50. // @codingStandardsIgnoreEmd
  51. $res = $reCaptcha->verify($reCaptchaResponse, $remoteIp);
  52. if ($res->isSuccess()) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. }