recaptcha.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace Web\Google;
  16. //! Google ReCAPTCHA v2 plug-in
  17. class Recaptcha {
  18. const
  19. //! API URL
  20. URL_Recaptcha='https://www.google.com/recaptcha/api/siteverify';
  21. /**
  22. * Verify reCAPTCHA response
  23. * @param string $secret
  24. * @param string $response
  25. * @return bool
  26. **/
  27. static function verify($secret,$response=NULL) {
  28. $fw=\Base::instance();
  29. if (!isset($response))
  30. $response=$fw->{'POST.g-recaptcha-response'};
  31. if (!$response)
  32. return FALSE;
  33. $web=\Web::instance();
  34. $out=$web->request(self::URL_Recaptcha,[
  35. 'method'=>'POST',
  36. 'content'=>http_build_query([
  37. 'secret'=>$secret,
  38. 'response'=>$response,
  39. 'remoteip'=>$fw->IP
  40. ]),
  41. ]);
  42. return isset($out['body']) &&
  43. ($json=json_decode($out['body'],TRUE)) &&
  44. isset($json['success']) && $json['success'];
  45. }
  46. }