BxDolKey.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. {
  42. protected $_oQuery;
  43. /**
  44. * Constructor
  45. */
  46. public function __construct()
  47. {
  48. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  49. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  50. parent::__construct();
  51. $this->_oQuery = new BxDolKeyQuery();
  52. }
  53. /**
  54. * Prevent cloning the instance
  55. */
  56. public function __clone()
  57. {
  58. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  59. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  60. }
  61. /**
  62. * Get singleton instance of the class
  63. */
  64. public static function getInstance()
  65. {
  66. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  67. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolKey();
  68. return $GLOBALS['bxDolClasses'][__CLASS__];
  69. }
  70. /**
  71. * Get new key.
  72. * @param $iExpire - number of seconds to generated key after, by default - 1 week
  73. * @return newly generated key string
  74. */
  75. public function getNewKey ($aData = false, $iExpire = 604800)
  76. {
  77. $sKey = md5(time() . rand() . BX_DOL_SECRET);
  78. if ($this->_oQuery->insert($sKey, $aData ? serialize($aData) : '', (int)$iExpire));
  79. return $sKey;
  80. return false;
  81. }
  82. /**
  83. * Check if provided key exists.
  84. * @param $sKey - key string
  85. * @return true if key exists or false if key is missing
  86. */
  87. public function isKeyExists ($sKey)
  88. {
  89. return $this->_oQuery->get($sKey) ? true : false;
  90. }
  91. /**
  92. * Get key data.
  93. * @param $sKey - key string
  94. * @return true if key exists or false if key is missing
  95. */
  96. public function getKeyData ($sKey)
  97. {
  98. $sData = $this->_oQuery->getData($sKey);
  99. if ($sData)
  100. return unserialize($sData);
  101. return '';
  102. }
  103. /**
  104. * Delete provided key.
  105. * @param $sKey - key string
  106. * @return true if key was successfully found and delete if false otherwise
  107. */
  108. public function removeKey ($sKey)
  109. {
  110. return $this->_oQuery->remove($sKey);
  111. }
  112. /**
  113. * Delete expired keys.
  114. */
  115. public function prune ()
  116. {
  117. return $this->_oQuery->prune();
  118. }
  119. }
  120. /** @} */