1
0

BxBaseCaptchaReCAPTCHAInvisible.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * reCAPTCHA representation.
  11. * @see BxDolCaptcha
  12. */
  13. class BxBaseCaptchaReCAPTCHAInvisible extends BxTemplCaptchaReCAPTCHA
  14. {
  15. protected $sApiUrl;
  16. protected $sVerifyUrl;
  17. public function __construct ($aObject, $oTemplate)
  18. {
  19. parent::__construct ($aObject, $oTemplate);
  20. $this->sApiUrl = 'https://www.google.com/recaptcha/api.js';
  21. $this->sVerifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
  22. }
  23. /**
  24. * Display captcha.
  25. */
  26. public function display ($bDynamicMode = false)
  27. {
  28. $sCode = '';
  29. $sId = 'sys-captcha-' . $this->_sObject;
  30. $sCode .= '<div id="' . $sId . '"></div>';
  31. $sPostfix = bx_gen_method_name($this->_sObject);
  32. $sOnLoadFunction = 'onLoadCallback' . $sPostfix;
  33. $sOnSubmitFunction = 'onSubmitCallback' . $sPostfix;
  34. $sOnLoadCode = "
  35. var " . $sOnLoadFunction . " = function() {
  36. grecaptcha.render('" . $sId . "', {
  37. 'sitekey': '" . $this->_sKeyPublic . "',
  38. 'size': 'invisible',
  39. 'badge': 'inline'
  40. });
  41. grecaptcha.execute();
  42. };
  43. ";
  44. $sCode .= $this->_oTemplate->_wrapInTagJsCode($sOnLoadCode);
  45. $sCode .= $this->_oTemplate->addJs(bx_append_url_params($this->sApiUrl, array(
  46. 'onload' => $sOnLoadFunction,
  47. 'render' => 'explicit',
  48. 'hl' => BxDolLanguages::getInstance()->getCurrentLanguage()
  49. )), true);
  50. return $sCode;
  51. }
  52. /**
  53. * Check captcha.
  54. */
  55. public function check ()
  56. {
  57. $mixedResponce = bx_file_get_contents($this->sVerifyUrl, array(
  58. 'secret' => $this->_sKeyPrivate,
  59. 'response' => bx_process_input(bx_get('g-recaptcha-response')),
  60. 'remoteip' => getVisitorIP()
  61. ));
  62. if($mixedResponce === false)
  63. return false;
  64. $aResponce = json_decode($mixedResponce, true);
  65. if(isset($aResponce['success']) && $aResponce['success'] === true)
  66. return true;
  67. if(!empty($aResponce['error-codes']))
  68. $this->_error = $aResponce['error-codes'];
  69. return false;
  70. }
  71. }
  72. /** @} */