ReCaptcha.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. * - Documentation and latest version
  5. * http://recaptcha.net/plugins/php/
  6. * - Get a reCAPTCHA API Key
  7. * https://www.google.com/recaptcha/admin/create
  8. * - Discussion group
  9. * http://groups.google.com/group/recaptcha
  10. *
  11. * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
  12. * AUTHORS:
  13. * Mike Crawford
  14. * Ben Maurer
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. */
  34. /**
  35. *
  36. * PHP Pro Bid $Id$ t5ph3lW4L/rvh8eZc2pHTNSEkY3fi1pk9c9K1mixMow=
  37. *
  38. * @link http://www.phpprobid.com
  39. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  40. * @license http://www.phpprobid.com/license Commercial License
  41. *
  42. * @version 7.5
  43. */
  44. /**
  45. * reCAPTCHA 2 validator for PHP Pro Bid V7
  46. *
  47. */
  48. namespace Ppb\Validate;
  49. use Cube\Validate\AbstractValidate,
  50. Cube\Controller\Front;
  51. class ReCaptcha extends AbstractValidate
  52. {
  53. /**
  54. * reCAPTCHA verify server
  55. *
  56. * @var string
  57. */
  58. const VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify';
  59. /**
  60. *
  61. * validation error message
  62. *
  63. * @var string
  64. */
  65. protected $_message = "The reCAPTCHA code is incorrect.";
  66. /**
  67. *
  68. * Calls an HTTP POST function to verify if the user's guess was correct
  69. *
  70. * @return bool return true if the validation is successful
  71. */
  72. public function isValid()
  73. {
  74. $settings = Front::getInstance()->getBootstrap()->getResource('settings');
  75. $request = Front::getInstance()->getRequest();
  76. $response = $request->getParam('g-recaptcha-response');
  77. if ($response == null || strlen($response) == 0) {
  78. return false;
  79. }
  80. $json = $this->_post(array(
  81. 'secret' => $settings['recaptcha_private_key'],
  82. 'remoteip' => $_SERVER['REMOTE_ADDR'],
  83. 'response' => $response
  84. ));
  85. $result = json_decode($json, true);
  86. if (isset($result['success']) && $result['success'] == true) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * Submits an HTTP POST to a reCAPTCHA server
  93. *
  94. * @param array $data
  95. *
  96. * @return array response
  97. */
  98. protected function _post(array $data)
  99. {
  100. $peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
  101. $options = array(
  102. 'http' => array(
  103. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  104. 'method' => 'POST',
  105. 'content' => http_build_query($data, '', '&'),
  106. // Force the peer to validate (not needed in 5.6.0+, but still works
  107. 'verify_peer' => true,
  108. // Force the peer validation to use www.google.com
  109. $peerKey => 'www.google.com',
  110. ),
  111. );
  112. $context = stream_context_create($options);
  113. return file_get_contents(self::VERIFY_SERVER, false, $context);
  114. }
  115. }