BxBaseCaptchaHCaptcha.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 BxBaseCaptchaHCaptcha extends BxDolCaptcha
  14. {
  15. protected $_sSkin;
  16. protected $sApiUrl;
  17. protected $sVerifyUrl;
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. parent::__construct ($aObject, $oTemplate);
  21. $this->_sSkin = 'light';
  22. $this->sApiUrl = 'https://js.hcaptcha.com/1/api.js';
  23. $this->sVerifyUrl = 'https://hcaptcha.com/siteverify';
  24. }
  25. /**
  26. * Display captcha.
  27. */
  28. public function display ($bDynamicMode = false)
  29. {
  30. $sCode = '';
  31. $aApiParams = array();
  32. if($bDynamicMode) {
  33. $sPostfix = $this->_sObject;
  34. $sId = 'sys-captcha-' . $sPostfix;
  35. $sOnLoadFunction = 'onLoadCallback' . $sPostfix;
  36. $sOnLoadCode = "
  37. var " . $sOnLoadFunction . " = function() {
  38. hcaptcha.render('" . $sId . "', {
  39. 'sitekey': '" . $this->_sKeyPublic . "',
  40. 'theme': '" . $this->_sSkin . "'
  41. });
  42. };
  43. ";
  44. $aApiParams = array(
  45. 'onload' => $sOnLoadFunction,
  46. 'render' => 'explicit'
  47. );
  48. $sCode .= $this->_oTemplate->_wrapInTagJsCode($sOnLoadCode);
  49. $sCode .= '<div id="' . $sId . '"></div>';
  50. }
  51. else {
  52. $aApiParams = array(
  53. 'render' => 'onload'
  54. );
  55. $sCode .= '<div class="h-captcha" data-sitekey="' . $this->_sKeyPublic . '" data-theme="' . $this->_sSkin . '"></div>';
  56. }
  57. $aApiParams['hl'] = BxDolLanguages::getInstance()->getCurrentLanguage();
  58. $sCodeJs = $this->_oTemplate->addJs(bx_append_url_params($this->sApiUrl, $aApiParams), true);
  59. return $sCodeJs . $sCode;
  60. }
  61. /**
  62. * Check captcha.
  63. */
  64. public function check ()
  65. {
  66. $mixedResponce = bx_file_get_contents($this->sVerifyUrl, array(
  67. 'secret' => $this->_sKeyPrivate,
  68. 'response' => bx_process_input(bx_get('h-captcha-response'))
  69. ), 'post');
  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. /** @} */