BxDolKey.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 with params.
  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 getNewKeyExt ($aData = false, $iExpire = 604800, $aParams = [])
  82. {
  83. $iLenth = !empty($aParams['length']) ? (int)$aParams['length'] : 32;
  84. $sKey = !empty($aParams['key']) ? $aParams['key'] : genRndPwd($iLenth, false);
  85. while(true) {
  86. if(!$this->isKeyExists($sKey))
  87. break;
  88. $sKey = genRndPwd($iLenth, false);
  89. }
  90. if($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
  91. return $sKey;
  92. return false;
  93. }
  94. /**
  95. * Get new key (numeric).
  96. * @param $aData - some data to associate with the key
  97. * @param $iExpire - number of seconds to generated key after, by default - 1 week
  98. * @return newly generated key string
  99. */
  100. public function getNewKeyNumeric ($aData = false, $iExpire = 604800)
  101. {
  102. $sKey = '';
  103. while(true) {
  104. $sKey = mt_rand(100000, 999999);
  105. if(!$this->isKeyExists($sKey))
  106. break;
  107. }
  108. if($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
  109. return $sKey;
  110. return false;
  111. }
  112. /**
  113. * Check if provided key exists.
  114. * @param $sKey - key string
  115. * @return true if key exists or false if key is missing
  116. */
  117. public function isKeyExists ($sKey, $sSalt = '')
  118. {
  119. return $this->_oQuery->get($sKey, $sSalt) ? true : false;
  120. }
  121. /**
  122. * Get key data.
  123. * @param $sKey - key string
  124. * @return true if key exists or false if key is missing
  125. */
  126. public function getKeyData ($sKey, $sSalt = '')
  127. {
  128. $sData = $this->_oQuery->getData($sKey, $sSalt);
  129. if ($sData)
  130. return unserialize($sData);
  131. return '';
  132. }
  133. /**
  134. * Delete provided key.
  135. * @param $sKey - key string
  136. * @return true if key was successfully found and delete if false otherwise
  137. */
  138. public function removeKey ($sKey)
  139. {
  140. return $this->_oQuery->remove($sKey);
  141. }
  142. /**
  143. * Delete expired keys.
  144. */
  145. public function prune ()
  146. {
  147. return $this->_oQuery->prune();
  148. }
  149. }
  150. /** @} */