Elgg  Version master
HMACCacheTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 use Elgg\Traits\TimeUsing;
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 }
if(empty($entity_guid)||empty($recipient)||empty($muted_settings)||empty($hmac_token)) $hmac
Definition: mute.php:18
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$delete
Query builder for updating data in the database.
Definition: Delete.php:8
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
Manage the contents of the hmac_cache table.
deleteHMAC(string $hmac)
Delete a HMAC key from the database.
__construct(protected Database $database)
Create a new table handler.
getTTL()
Get the configured Time-To-Live of the HMAC keys.
storeHMAC(string $hmac)
Store a HMAC key for later use.
loadHMAC(string $hmac)
Load a HMAC key from the database.
setTTL(int $ttl=0)
Set the Time-To-Live of HMAC keys.
__destruct()
Cleanup expired HMAC keys.
Query builder for fetching data from the database.
Definition: Select.php:8
The Elgg database.
Definition: Database.php:26
Exception thrown to indicate range errors during program execution.
const ELGG_VALUE_STRING
Definition: constants.php:112
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$expires