BxDolKey.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, $sSalt = '')
  69. {
  70. $sKey = md5(time() . rand() . BX_DOL_SECRET . $sSalt);
  71. if ($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire, $sSalt));
  72. return $sKey;
  73. return false;
  74. }
  75. /**
  76. * Get new key (numeric).
  77. * @param $aData - some data to associate with the key
  78. * @param $iExpire - number of seconds to generated key after, by default - 1 week
  79. * @return newly generated key string
  80. */
  81. public function getNewKeyNumeric ($aData = false, $iExpire = 604800)
  82. {
  83. $sKey = '';
  84. while(true) {
  85. $sKey = mt_rand(100000, 999999);
  86. if(!$this->isKeyExists($sKey))
  87. break;
  88. }
  89. if($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
  90. return $sKey;
  91. return false;
  92. }
  93. /**
  94. * Check if provided key exists.
  95. * @param $sKey - key string
  96. * @return true if key exists or false if key is missing
  97. */
  98. public function isKeyExists ($sKey, $sSalt = '')
  99. {
  100. return $this->_oQuery->get($sKey, $sSalt) ? true : false;
  101. }
  102. /**
  103. * Get key data.
  104. * @param $sKey - key string
  105. * @return true if key exists or false if key is missing
  106. */
  107. public function getKeyData ($sKey, $sSalt = '')
  108. {
  109. $sData = $this->_oQuery->getData($sKey, $sSalt);
  110. if ($sData)
  111. return unserialize($sData);
  112. return '';
  113. }
  114. /**
  115. * Delete provided key.
  116. * @param $sKey - key string
  117. * @return true if key was successfully found and delete if false otherwise
  118. */
  119. public function removeKey ($sKey)
  120. {
  121. return $this->_oQuery->remove($sKey);
  122. }
  123. /**
  124. * Delete expired keys.
  125. */
  126. public function prune ()
  127. {
  128. return $this->_oQuery->prune();
  129. }
  130. }
  131. /** @} */