BxDolCaptcha.php 3.8 KB

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