Elgg  Version 4.3
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  protected $database;
23 
27  protected $ttl = 90000;
28 
32  protected $table = 'hmac_cache';
33 
41  public function __construct(Database $database) {
42  $this->database = $database;
43  }
44 
50  public function __destruct() {
51  if ($this->getTTL() < 0) {
52  return;
53  }
54 
55  $expires = $this->getCurrentTime("-{$this->getTTL()} seconds");
56 
57  $delete = Delete::fromTable($this->table);
58  $delete->where($delete->compare('ts', '<', $expires->getTimestamp(), ELGG_VALUE_TIMESTAMP));
59 
60  $this->database->deleteData($delete);
61  }
62 
71  public function setTTL(int $ttl = 0) {
72  if ($ttl < -1) {
73  throw new InvalidArgumentException(__METHOD__ . ': TTL needs to be greater than or equal to -1');
74  }
75 
76  $this->ttl = $ttl;
77  }
78 
84  public function getTTL() : int {
85  return $this->ttl;
86  }
87 
95  public function storeHMAC(string $hmac) {
96  $insert = Insert::intoTable($this->table);
97  $insert->values([
98  'hmac' => $insert->param($hmac, ELGG_VALUE_STRING),
99  'ts' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
100  ]);
101 
102  return $this->database->insertData($insert);
103  }
104 
112  public function loadHMAC(string $hmac) : ?string {
113  $select = Select::fromTable($this->table);
114  $select->select('*');
115  $select->where($select->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
116 
117  $row = $this->database->getDataRow($select);
118  if (empty($row)) {
119  return null;
120  }
121 
122  return $row->hmac;
123  }
124 
132  public function deleteHMAC(string $hmac) : int {
133  $delete = Delete::fromTable($this->table);
134  $delete->where($delete->compare('hmac', '=', $hmac, ELGG_VALUE_STRING));
135 
136  return $this->database->deleteData($delete);
137  }
138 }
Exception thrown if an argument is not of the expected type.
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.
$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.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13