BxDolCaptcha.php 3.6 KB

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