BxBaseCaptchaReCAPTCHA.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 UnaCoreBaseRepresentation UNA Core Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * reCAPTCHA representation.
  11. * @see BxDolCaptcha
  12. */
  13. class BxBaseCaptchaReCAPTCHA extends BxDolCaptcha
  14. {
  15. protected $_bJsCssAdded = false;
  16. protected $_oTemplate;
  17. protected $_sSkin = 'custom';
  18. protected $_error = null;
  19. protected $_sKeyPublic;
  20. protected $_sKeyPrivate;
  21. public function __construct ($aObject, $oTemplate)
  22. {
  23. parent::__construct ($aObject);
  24. if ($oTemplate)
  25. $this->_oTemplate = $oTemplate;
  26. else
  27. $this->_oTemplate = BxDolTemplate::getInstance();
  28. $this->_sKeyPublic = getParam('sys_recaptcha_key_public');
  29. $this->_sKeyPrivate = getParam('sys_recaptcha_key_private');
  30. }
  31. /**
  32. * Display captcha.
  33. */
  34. public function display ($bDynamicMode = false)
  35. {
  36. // TODO: captcha don't display error in javascript mode, try to find the way on how to pass error code in this mode
  37. $sId = 'sys-captcha-' . time() . rand(0, PHP_INT_MAX);
  38. $sJsObject = 'Recaptcha';
  39. $sInit = $sJsObject . ".create('" . $this->_sKeyPublic . "', '" . $sId . "', {
  40. lang: '" . BxDolLanguages::getInstance()->getCurrentLanguage() . "',
  41. theme: '" . $this->_sSkin . "',
  42. custom_theme_widget: '" . $sId . "',
  43. callback: " . $sJsObject . ".focus_response_field
  44. });";
  45. return $this->_addJsCss($bDynamicMode) . $this->_oTemplate->parseHtmlByName('reCaptcha.html', array(
  46. 'js_object' => $sJsObject,
  47. 'id' => $sId,
  48. 'bx_if:show_common' => array(
  49. 'condition' => !$bDynamicMode,
  50. 'content' => array(
  51. 'js_init' => $sInit,
  52. )
  53. ),
  54. 'bx_if:show_dynamic' => array(
  55. 'condition' => $bDynamicMode,
  56. 'content' => array(
  57. 'js_object' => $sJsObject,
  58. 'js_init' => $sInit,
  59. )
  60. ),
  61. ));
  62. }
  63. /**
  64. * Check captcha.
  65. */
  66. public function check ()
  67. {
  68. require_once(BX_DIRECTORY_PATH_PLUGINS . 'recaptcha/recaptchalib.php');
  69. $oResp = recaptcha_check_answer(
  70. $this->_sKeyPrivate,
  71. $_SERVER["REMOTE_ADDR"],
  72. bx_process_input(bx_get('recaptcha_challenge_field')),
  73. bx_process_input(bx_get('recaptcha_response_field'))
  74. );
  75. if (!$oResp->is_valid) {
  76. $this->_error = $oResp->error;
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * Check if captcha is available, like all API keys are specified.
  83. */
  84. public function isAvailable ()
  85. {
  86. return !empty($this->_sKeyPublic) && !empty($this->_sKeyPrivate);
  87. }
  88. /**
  89. * Add css/js files which are needed for display and functionality.
  90. */
  91. protected function _addJsCss($bDynamicMode = false)
  92. {
  93. if ($bDynamicMode)
  94. return '';
  95. if ($this->_bJsCssAdded)
  96. return '';
  97. $this->_oTemplate->addJs('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
  98. $this->_bJsCssAdded = true;
  99. return '';
  100. }
  101. }
  102. /** @} */