BxDolCaptcha.php 3.8 KB

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