BxBaseCaptchaReCAPTCHANew.php 2.5 KB

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