BxBaseCaptchaReCAPTCHANew.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 BxBaseCaptchaReCAPTCHANew 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. $aApiParams = array();
  30. if($bDynamicMode) {
  31. $sPostfix = $this->_sObject;
  32. $sId = 'sys-captcha-' . $sPostfix;
  33. $sOnLoadFunction = 'onLoadCallback' . $sPostfix;
  34. $sOnLoadCode = "
  35. var " . $sOnLoadFunction . " = function() {
  36. grecaptcha.render('" . $sId . "', {
  37. 'sitekey': '" . $this->_sKeyPublic . "',
  38. 'theme': '" . $this->_sSkin . "'
  39. });
  40. };
  41. ";
  42. $aApiParams = array(
  43. 'onload' => $sOnLoadFunction,
  44. 'render' => 'explicit'
  45. );
  46. $sCode .= $this->_oTemplate->_wrapInTagJsCode($sOnLoadCode);
  47. $sCode .= '<div id="' . $sId . '">';
  48. }
  49. else {
  50. $aApiParams = array(
  51. 'render' => 'onload'
  52. );
  53. $sCode .= '<div class="g-recaptcha" data-sitekey="' . $this->_sKeyPublic . '" data-theme="' . $this->_sSkin . '"></div>';
  54. }
  55. $aApiParams['hl'] = BxDolLanguages::getInstance()->getCurrentLanguage();
  56. $sCodeJs = $this->_oTemplate->addJs(bx_append_url_params($this->sApiUrl, $aApiParams), $bDynamicMode);
  57. return ($bDynamicMode ? $sCodeJs : '') . $sCode;
  58. }
  59. /**
  60. * Check captcha.
  61. */
  62. public function check ()
  63. {
  64. $mixedResponce = bx_file_get_contents($this->sVerifyUrl, array(
  65. 'secret' => $this->_sKeyPrivate,
  66. 'response' => bx_process_input(bx_get('g-recaptcha-response')),
  67. 'remoteip' => getVisitorIP()
  68. ));
  69. if($mixedResponce === false)
  70. return false;
  71. $aResponce = json_decode($mixedResponce, true);
  72. if(isset($aResponce['success']) && $aResponce['success'] === true)
  73. return true;
  74. if(!empty($aResponce['error-codes']))
  75. $this->_error = $aResponce['error-codes'];
  76. return false;
  77. }
  78. }
  79. /** @} */