ElggHMACCache.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * ElggHMACCache
  4. * Store cached data in a temporary database, only used by the HMAC stuff.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage HMAC
  8. */
  9. class ElggHMACCache extends ElggCache {
  10. /**
  11. * Set the Elgg cache.
  12. *
  13. * @param int $max_age Maximum age in seconds, 0 if no limit.
  14. */
  15. function __construct($max_age = 0) {
  16. $this->setVariable("max_age", $max_age);
  17. }
  18. /**
  19. * Save a key
  20. *
  21. * @param string $key Name
  22. * @param string $data Value
  23. *
  24. * @return boolean
  25. */
  26. public function save($key, $data) {
  27. global $CONFIG;
  28. $key = sanitise_string($key);
  29. $time = time();
  30. $query = "INSERT into {$CONFIG->dbprefix}hmac_cache (hmac, ts) VALUES ('$key', '$time')";
  31. return insert_data($query);
  32. }
  33. /**
  34. * Load a key
  35. *
  36. * @param string $key Name
  37. * @param int $offset Offset
  38. * @param int $limit Limit
  39. *
  40. * @return string
  41. */
  42. public function load($key, $offset = 0, $limit = null) {
  43. global $CONFIG;
  44. $key = sanitise_string($key);
  45. $row = get_data_row("SELECT * from {$CONFIG->dbprefix}hmac_cache where hmac='$key'");
  46. if ($row) {
  47. return $row->hmac;
  48. }
  49. return false;
  50. }
  51. /**
  52. * Invalidate a given key.
  53. *
  54. * @param string $key Name
  55. *
  56. * @return bool
  57. */
  58. public function delete($key) {
  59. global $CONFIG;
  60. $key = sanitise_string($key);
  61. return delete_data("DELETE from {$CONFIG->dbprefix}hmac_cache where hmac='$key'");
  62. }
  63. /**
  64. * Clear out all the contents of the cache.
  65. *
  66. * Not currently implemented in this cache type.
  67. *
  68. * @return true
  69. */
  70. public function clear() {
  71. return true;
  72. }
  73. /**
  74. * Clean out old stuff.
  75. *
  76. */
  77. public function __destruct() {
  78. global $CONFIG;
  79. $time = time();
  80. $age = (int)$this->getVariable("max_age");
  81. $expires = $time - $age;
  82. delete_data("DELETE from {$CONFIG->dbprefix}hmac_cache where ts<$expires");
  83. }
  84. }