BxBaseCaptchaReCAPTCHA.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /**
  10. * reCAPTCHA representation.
  11. * @see BxDolCaptcha
  12. */
  13. class BxBaseCaptchaReCAPTCHA extends BxDolCaptcha
  14. {
  15. protected $_bJsCssAdded = false;
  16. protected $_oTemplate;
  17. protected $_sSkin = 'white';
  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. $sInit = "
  39. Recaptcha.create('" . $this->_sKeyPublic . "', '" . $sId . "', {
  40. lang: '" . BxDolLanguages::getInstance()->getCurrentLanguage() . "',
  41. theme: '" . $this->_sSkin . "',
  42. callback: Recaptcha.focus_response_field
  43. });
  44. ";
  45. if ($bDynamicMode) {
  46. $sCode = "
  47. <script>
  48. if ('undefined' == typeof(window.Recaptcha)) {
  49. $.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js', function(data, textStatus, jqxhr) {
  50. $sInit
  51. });
  52. } else {
  53. $sInit
  54. }
  55. </script>";
  56. } else {
  57. $sCode = "
  58. <script>
  59. $(document).ready(function () {
  60. $sInit
  61. });
  62. </script>";
  63. }
  64. return $this->_addJsCss($bDynamicMode) . '<div id="' . $sId . '"></div>' . $sCode;
  65. }
  66. /**
  67. * Check captcha.
  68. */
  69. public function check ()
  70. {
  71. require_once(BX_DIRECTORY_PATH_PLUGINS . 'recaptcha/recaptchalib.php');
  72. $oResp = recaptcha_check_answer(
  73. $this->_sKeyPrivate,
  74. $_SERVER["REMOTE_ADDR"],
  75. bx_process_input(bx_get('recaptcha_challenge_field')),
  76. bx_process_input(bx_get('recaptcha_response_field'))
  77. );
  78. if (!$oResp->is_valid) {
  79. $this->_error = $oResp->error;
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Check if captcha is available, like all API keys are specified.
  86. */
  87. public function isAvailable ()
  88. {
  89. return !empty($this->_sKeyPublic) && !empty($this->_sKeyPrivate);
  90. }
  91. /**
  92. * Add css/js files which are needed for display and functionality.
  93. */
  94. protected function _addJsCss($bDynamicMode = false)
  95. {
  96. if ($bDynamicMode)
  97. return '';
  98. if ($this->_bJsCssAdded)
  99. return '';
  100. $this->_oTemplate->addJs('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
  101. $this->_bJsCssAdded = true;
  102. return '';
  103. }
  104. }
  105. /** @} */