BxDolCaptcha.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * CAPTCHA objects.
  11. *
  12. * Default captcha is stored in 'sys_captcha_default' setting option.
  13. *
  14. * @section captcha_create Creating the Captcha object:
  15. *
  16. *
  17. * Add record to 'sys_objects_captcha' table:
  18. *
  19. * - object: name of the captcha object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_blogs - custom captcha in blogs module.
  20. * - title: captcha title.
  21. * - override_class_name: user defined class name which is derived from one of base captcha classes.
  22. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  23. *
  24. *
  25. * @section example Example of usage
  26. *
  27. * Display captcha:
  28. *
  29. * @code
  30. * $oCaptcha = BxDolCaptcha::getObjectInstance(); // get default captcha object instance
  31. * if ($oCaptcha) // check if captcha is available for using
  32. * echo $oCaptcha->display (); // output HTML which will automatically show captcha
  33. * @endcode
  34. *
  35. * Check captcha:
  36. *
  37. * @code
  38. * $oCaptcha = BxDolCaptcha::getObjectInstance(); // get default captcha object instance
  39. * if ($oCaptcha && $oCaptcha->check ()) // check if captcha is correct
  40. * echo 'captcha is OK';
  41. * else
  42. * echo 'captcha is incorrect'; //
  43. * @endcode
  44. */
  45. class BxDolCaptcha extends BxDolFactory implements iBxDolFactoryObject
  46. {
  47. protected $_sObject;
  48. protected $_aObject;
  49. protected $_sKeyPublic;
  50. protected $_sKeyPrivate;
  51. protected $_oTemplate;
  52. /**
  53. * Constructor
  54. * @param $aObject array of captcha options
  55. */
  56. protected function __construct($aObject, $oTemplate)
  57. {
  58. parent::__construct();
  59. $this->_sObject = $aObject['object'];
  60. $this->_aObject = $aObject;
  61. $this->_sKeyPublic = getParam('sys_recaptcha_key_public');
  62. $this->_sKeyPrivate = getParam('sys_recaptcha_key_private');
  63. if ($oTemplate)
  64. $this->_oTemplate = $oTemplate;
  65. else
  66. $this->_oTemplate = BxDolTemplate::getInstance();
  67. }
  68. /**
  69. * Get captcha object instance by object name
  70. * @param $sObject object name
  71. * @return object instance or false on error
  72. */
  73. static public function getObjectInstance($sObject = false)
  74. {
  75. if (!$sObject)
  76. $sObject = getParam('sys_captcha_default');
  77. if (isset($GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject]))
  78. return $GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject];
  79. $aObject = BxDolCaptchaQuery::getCaptchaObject($sObject);
  80. if (!$aObject || !is_array($aObject))
  81. return false;
  82. if (empty($aObject['override_class_name']))
  83. return false;
  84. $sClass = $aObject['override_class_name'];
  85. if (!empty($aObject['override_class_file']))
  86. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  87. $o = new $sClass($aObject);
  88. if (!$o->isAvailable())
  89. return false;
  90. return ($GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject] = $o);
  91. }
  92. /**
  93. * Display captcha.
  94. * @param $bDynamicMode - is AJAX mode or not, if true then HTML code with captcha is loaded dynamically.
  95. */
  96. public function display ($bDynamicMode = false)
  97. {
  98. // override this function in particular class
  99. }
  100. /**
  101. * Check captcha.
  102. */
  103. public function check ()
  104. {
  105. // override this function in particular class
  106. }
  107. /**
  108. * Return text entered by user
  109. */
  110. public function getUserResponse ()
  111. {
  112. // override this function in particular class
  113. }
  114. /**
  115. * Check if captcha is available, like all API keys are specified.
  116. */
  117. public function isAvailable ()
  118. {
  119. return !empty($this->_sKeyPublic) && !empty($this->_sKeyPrivate);
  120. }
  121. }
  122. /** @} */