BxDolKey.php 3.2 KB

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