Elgg  Version 6.1
HMACCacheTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
8 
16 
17  use TimeUsing;
18 
22  public const TABLE_NAME = 'hmac_cache';
23 
27  protected int $ttl = 90000;
28 
34  public function __construct(protected Database $database) {
35  }
36 
42  public function __destruct() {
43  if ($this->getTTL() < 0) {
44  return;
45  }
46 
47  $expires = $this->getCurrentTime("-{$this->getTTL()} seconds");
48 
49  $delete = Delete::fromTable(self::TABLE_NAME);
50  $delete->where($delete->compare('ts', '<', $expires->getTimestamp(), ELGG_VALUE_TIMESTAMP));
51 
52  $this->database->deleteData($delete);
53  }
54 
63  public function setTTL(int $ttl = 0): void {
64  if ($ttl < -1) {
65  throw new RangeException(__METHOD__ . ': TTL needs to be greater than or equal to -1');
66  }
67 
68  $this->ttl = $ttl;
69  }
70 
76  public function getTTL(): int {
77  return $this->ttl;
78  }
79 
87  public function storeHMAC(string $hmac) {
88  $insert = Insert::intoTable(self::TABLE_NAME);
89  $insert->values([
90  'hmac' => $insert->param($hmac, ELGG_VALUE_STRING),
91  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
92  ]);
93 
94  return $this->database->insertData($insert);
95  }
96 
104  public function loadHMAC(string $hmac): ?string {
105  $select = Select::fromTable(self::TABLE_NAME);
106  $select->select('*');
107  $select->where($select->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
108 
109  $row = $this->database->getDataRow($select);
110  if (empty($row)) {
111  return null;
112  }
113 
114  return $row->hmac;
115  }
116 
124  public function deleteHMAC(string $hmac) : int {
125  $delete = Delete::fromTable(self::TABLE_NAME);
126  $delete->where($delete->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
127 
128  return $this->database->deleteData($delete);
129  }
130 }
Exception thrown to indicate range errors during program execution.
getTTL()
Get the configured Time-To-Live of the HMAC keys.
__construct(protected Database $database)
Create a new table handler.
The Elgg database.
Definition: Database.php:26
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$delete
deleteHMAC(string $hmac)
Delete a HMAC key from the database.
loadHMAC(string $hmac)
Load a HMAC key from the database.
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
setTTL(int $ttl=0)
Set the Time-To-Live of HMAC keys.
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
__destruct()
Cleanup expired HMAC keys.
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
storeHMAC(string $hmac)
Store a HMAC key for later use.
if(empty($entity_guid)||empty($recipient)||empty($muted_settings)||empty($hmac_token)) $hmac
Definition: mute.php:18
Manage the contents of the hmac_cache table.
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
$expires
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18