BxDolKey.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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('BxDolKeyQuery');
  10. /**
  11. * @page objects
  12. * @section key Keys
  13. * @ref BxDolKey
  14. */
  15. /**
  16. * Key objects - automatically generate hashed keys.
  17. *
  18. * @section example Example of usage
  19. *
  20. * Generate new hashed key:
  21. *
  22. * @code
  23. * bx_import('BxDolKey'); // import class
  24. * $oKeys = BxDolKey::getInstance(); // get object instance
  25. * if ($oKeys) // check if object is available for using
  26. * echo $oKeys->getNewKey (3600); // get new hashed key, which will be automatically deleted after 1 hour
  27. * @endcode
  28. *
  29. * Check if hashed key exists:
  30. *
  31. * @code
  32. * bx_import('BxDolKey'); // import class
  33. * $oKeys = BxDolKey::getInstance(); // get object instance
  34. * if ($oKeys && $oKeys->isKeyExists ($sKey)) // check key exists
  35. * echo 'key exists';
  36. * else
  37. * echo 'key is invalid';
  38. * @endcode
  39. */
  40. class BxDolKey extends BxDol implements iBxDolSingleton {
  41. protected $_oQuery;
  42. /**
  43. * Constructor
  44. */
  45. public function __construct() {
  46. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  47. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  48. parent::__construct();
  49. $this->_oQuery = new BxDolKeyQuery();
  50. }
  51. /**
  52. * Prevent cloning the instance
  53. */
  54. public function __clone() {
  55. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  56. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  57. }
  58. /**
  59. * Get singleton instance of the class
  60. */
  61. public static function getInstance() {
  62. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  63. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolKey();
  64. return $GLOBALS['bxDolClasses'][__CLASS__];
  65. }
  66. /**
  67. * Get new key.
  68. * @param $iExpire - number of seconds to generated key after, by default - 1 week
  69. * @return newly generated key string
  70. */
  71. public function getNewKey ($aData = false, $iExpire = 604800) {
  72. $sKey = md5(time() . rand() . BX_DOL_SECRET);
  73. if ($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
  74. return $sKey;
  75. return false;
  76. }
  77. /**
  78. * Check if provided key exists.
  79. * @param $sKey - key string
  80. * @return true if key exists or false if key is missing
  81. */
  82. public function isKeyExists ($sKey) {
  83. return $this->_oQuery->get($sKey) ? true : false;
  84. }
  85. /**
  86. * Get key data.
  87. * @param $sKey - key string
  88. * @return true if key exists or false if key is missing
  89. */
  90. public function getKeyData ($sKey) {
  91. $sData = $this->_oQuery->getData($sKey);
  92. if ($sData)
  93. return unserialize($sData);
  94. return '';
  95. }
  96. /**
  97. * Delete provided key.
  98. * @param $sKey - key string
  99. * @return true if key was successfully found and delete if false otherwise
  100. */
  101. public function removeKey ($sKey) {
  102. return $this->_oQuery->remove($sKey);
  103. }
  104. /**
  105. * Delete expired keys.
  106. */
  107. public function prune () {
  108. return $this->_oQuery->prune();
  109. }
  110. }
  111. /** @} */