BxBaseCaptchaReCAPTCHA.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. bx_import('BxDolCaptcha');
  10. /**
  11. * reCAPTCHA representation.
  12. * @see BxDolCaptcha
  13. */
  14. class BxBaseCaptchaReCAPTCHA extends BxDolCaptcha
  15. {
  16. protected $_bJsCssAdded = false;
  17. protected $_oTemplate;
  18. protected $_sSkin = 'white';
  19. protected $_error = null;
  20. protected $_sKeyPublic;
  21. protected $_sKeyPrivate;
  22. public function __construct ($aObject, $oTemplate)
  23. {
  24. parent::__construct ($aObject);
  25. if ($oTemplate)
  26. $this->_oTemplate = $oTemplate;
  27. else
  28. $this->_oTemplate = BxDolTemplate::getInstance();
  29. $this->_sKeyPublic = getParam('sys_recaptcha_key_public');
  30. $this->_sKeyPrivate = getParam('sys_recaptcha_key_private');
  31. }
  32. /**
  33. * Display captcha.
  34. */
  35. public function display ($bDynamicMode = false)
  36. {
  37. // TODO: captcha don't display error in javascript mode, try to find the way on how to pass error code in this mode
  38. bx_import('BxDolLanguages');
  39. $sId = 'sys-captcha-' . time() . rand(0, PHP_INT_MAX);
  40. $sInit = "
  41. Recaptcha.create('" . $this->_sKeyPublic . "', '" . $sId . "', {
  42. lang: '" . BxDolLanguages::getInstance()->getCurrentLanguage() . "',
  43. theme: '" . $this->_sSkin . "',
  44. callback: Recaptcha.focus_response_field
  45. });
  46. ";
  47. if ($bDynamicMode) {
  48. $sCode = "
  49. <script>
  50. if ('undefined' == typeof(window.Recaptcha)) {
  51. $.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js', function(data, textStatus, jqxhr) {
  52. $sInit
  53. });
  54. } else {
  55. $sInit
  56. }
  57. </script>";
  58. } else {
  59. $sCode = "
  60. <script>
  61. $(document).ready(function () {
  62. $sInit
  63. });
  64. </script>";
  65. }
  66. return $this->_addJsCss($bDynamicMode) . '<div id="' . $sId . '"></div>' . $sCode;
  67. }
  68. /**
  69. * Check captcha.
  70. */
  71. public function check ()
  72. {
  73. require_once(BX_DIRECTORY_PATH_PLUGINS . 'recaptcha/recaptchalib.php');
  74. $oResp = recaptcha_check_answer(
  75. $this->_sKeyPrivate,
  76. $_SERVER["REMOTE_ADDR"],
  77. bx_process_input(bx_get('recaptcha_challenge_field')),
  78. bx_process_input(bx_get('recaptcha_response_field'))
  79. );
  80. if (!$oResp->is_valid) {
  81. $this->_error = $oResp->error;
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Check if captcha is available, like all API keys are specified.
  88. */
  89. public function isAvailable ()
  90. {
  91. return !empty($this->_sKeyPublic) && !empty($this->_sKeyPrivate);
  92. }
  93. /**
  94. * Add css/js files which are needed for display and functionality.
  95. */
  96. protected function _addJsCss($bDynamicMode = false)
  97. {
  98. if ($bDynamicMode)
  99. return '';
  100. if ($this->_bJsCssAdded)
  101. return '';
  102. $this->_oTemplate->addJs('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
  103. $this->_bJsCssAdded = true;
  104. return '';
  105. }
  106. }
  107. /** @} */