BxDolCaptcha.php 3.7 KB

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