BxDolKey.php 3.3 KB

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