Elgg  Version 5.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  const TABLE_NAME = 'hmac_cache';
23 
24  protected Database $database;
25 
29  protected int $ttl = 90000;
30 
38  public function __construct(Database $database) {
39  $this->database = $database;
40  }
41 
47  public function __destruct() {
48  if ($this->getTTL() < 0) {
49  return;
50  }
51 
52  $expires = $this->getCurrentTime("-{$this->getTTL()} seconds");
53 
54  $delete = Delete::fromTable(self::TABLE_NAME);
55  $delete->where($delete->compare('ts', '<', $expires->getTimestamp(), ELGG_VALUE_TIMESTAMP));
56 
57  $this->database->deleteData($delete);
58  }
59 
68  public function setTTL(int $ttl = 0): void {
69  if ($ttl < -1) {
70  throw new RangeException(__METHOD__ . ': TTL needs to be greater than or equal to -1');
71  }
72 
73  $this->ttl = $ttl;
74  }
75 
81  public function getTTL(): int {
82  return $this->ttl;
83  }
84 
92  public function storeHMAC(string $hmac) {
93  $insert = Insert::intoTable(self::TABLE_NAME);
94  $insert->values([
95  'hmac' => $insert->param($hmac, ELGG_VALUE_STRING),
96  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
97  ]);
98 
99  return $this->database->insertData($insert);
100  }
101 
109  public function loadHMAC(string $hmac): ?string {
110  $select = Select::fromTable(self::TABLE_NAME);
111  $select->select('*');
112  $select->where($select->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
113 
114  $row = $this->database->getDataRow($select);
115  if (empty($row)) {
116  return null;
117  }
118 
119  return $row->hmac;
120  }
121 
129  public function deleteHMAC(string $hmac) : int {
130  $delete = Delete::fromTable(self::TABLE_NAME);
131  $delete->where($delete->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
132 
133  return $this->database->deleteData($delete);
134  }
135 }
Exception thrown to indicate range errors during program execution.
getTTL()
Get the configured Time-To-Live of the HMAC keys.
The Elgg database.
Definition: Database.php:25
__construct(Database $database)
Create a new table handler.
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.
__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.
static intoTable($table)
{}
Definition: Insert.php:13
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.
$expires
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13