BxBaseCaptchaReCAPTCHA.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolCaptcha');
  10. /**
  11. * reCAPTCHA representation.
  12. * @see BxDolCaptcha
  13. */
  14. class BxBaseCaptchaReCAPTCHA extends BxDolCaptcha {
  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. parent::__construct ($aObject);
  23. if ($oTemplate)
  24. $this->_oTemplate = $oTemplate;
  25. else
  26. $this->_oTemplate = BxDolTemplate::getInstance();
  27. $this->_sKeyPublic = getParam('sys_recaptcha_key_public');
  28. $this->_sKeyPrivate = getParam('sys_recaptcha_key_private');
  29. }
  30. /**
  31. * Display captcha.
  32. */
  33. public function display ($bDynamicMode = false) {
  34. // TODO: captcha don't display error in javascript mode, try to find the way on how to pass error code in this mode
  35. bx_import('BxDolLanguages');
  36. $sId = 'sys-captcha-' . time() . rand(0, PHP_INT_MAX);
  37. $sInit = "
  38. Recaptcha.create('" . $this->_sKeyPublic . "', '" . $sId . "', {
  39. lang: '" . BxDolLanguages::getInstance()->getCurrentLanguage() . "',
  40. theme: '" . $this->_sSkin . "',
  41. callback: Recaptcha.focus_response_field
  42. });
  43. ";
  44. if ($bDynamicMode) {
  45. $sCode = "
  46. <script>
  47. if ('undefined' == typeof(window.Recaptcha)) {
  48. $.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js', function(data, textStatus, jqxhr) {
  49. $sInit
  50. });
  51. } else {
  52. $sInit
  53. }
  54. </script>";
  55. } else {
  56. $sCode = "
  57. <script>
  58. $(document).ready(function () {
  59. $sInit
  60. });
  61. </script>";
  62. }
  63. return $this->_addJsCss($bDynamicMode) . '<div id="' . $sId . '"></div>' . $sCode;
  64. }
  65. /**
  66. * Check captcha.
  67. */
  68. public function check () {
  69. require_once(BX_DIRECTORY_PATH_PLUGINS . 'recaptcha/recaptchalib.php');
  70. $oResp = recaptcha_check_answer(
  71. $this->_sKeyPrivate,
  72. $_SERVER["REMOTE_ADDR"],
  73. bx_process_input(bx_get('recaptcha_challenge_field')),
  74. bx_process_input(bx_get('recaptcha_response_field'))
  75. );
  76. if (!$oResp->is_valid) {
  77. $this->_error = $oResp->error;
  78. return false;
  79. }
  80. return true;
  81. }
  82. /**
  83. * Check if captcha is available, like all API keys are specified.
  84. */
  85. public function isAvailable () {
  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. if ($bDynamicMode)
  93. return '';
  94. if ($this->_bJsCssAdded)
  95. return '';
  96. $this->_oTemplate->addJs('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
  97. $this->_bJsCssAdded = true;
  98. return '';
  99. }
  100. }
  101. /** @} */