BxDolCaptcha.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  50. * Constructor
  51. * @param $aObject array of captcha options
  52. */
  53. protected function __construct($aObject, $oTemplate)
  54. {
  55. parent::__construct();
  56. $this->_sObject = $aObject['object'];
  57. $this->_aObject = $aObject;
  58. $this->_sKeyPublic = getParam('sys_recaptcha_key_public');
  59. $this->_sKeyPrivate = getParam('sys_recaptcha_key_private');
  60. if ($oTemplate)
  61. $this->_oTemplate = $oTemplate;
  62. else
  63. $this->_oTemplate = BxDolTemplate::getInstance();
  64. }
  65. /**
  66. * Get captcha object instance by object name
  67. * @param $sObject object name
  68. * @return object instance or false on error
  69. */
  70. static public function getObjectInstance($sObject = false)
  71. {
  72. if (!$sObject)
  73. $sObject = getParam('sys_captcha_default');
  74. if (isset($GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject]))
  75. return $GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject];
  76. $aObject = BxDolCaptchaQuery::getCaptchaObject($sObject);
  77. if (!$aObject || !is_array($aObject))
  78. return false;
  79. if (empty($aObject['override_class_name']))
  80. return false;
  81. $sClass = $aObject['override_class_name'];
  82. if (!empty($aObject['override_class_file']))
  83. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  84. $o = new $sClass($aObject);
  85. if (!$o->isAvailable())
  86. return false;
  87. return ($GLOBALS['bxDolClasses']['BxDolCaptcha!'.$sObject] = $o);
  88. }
  89. /**
  90. * Display captcha.
  91. * @param $bDynamicMode - is AJAX mode or not, if true then HTML code with captcha is loaded dynamically.
  92. */
  93. public function display ($bDynamicMode = false)
  94. {
  95. // override this function in particular class
  96. }
  97. /**
  98. * Check captcha.
  99. */
  100. public function check ()
  101. {
  102. // override this function in particular class
  103. }
  104. /**
  105. * Return text entered by user
  106. */
  107. public function getUserResponse ()
  108. {
  109. // override this function in particular class
  110. }
  111. /**
  112. * Check if captcha is available, like all API keys are specified.
  113. */
  114. public function isAvailable ()
  115. {
  116. return !empty($this->_sKeyPublic) && !empty($this->_sKeyPrivate);
  117. }
  118. }
  119. /** @} */