ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
inc.pwassist_session_handler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 function db_pwassist_create_id(): string
31 {
32  global $DIC;
33 
34  $ilDB = $DIC->database();
35 
36  do {
37  $hash = bin2hex(ilPasswordUtils::getBytes(32));
38 
39  $exists = (
40  (int) ($ilDB->fetchAssoc(
41  $ilDB->query(
42  "SELECT EXISTS(" .
43  "SELECT 1 FROM usr_pwassist WHERE pwassist_id = " . $ilDB->quote($hash, ilDBConstants::T_TEXT) .
44  ") AS hit"
45  )
46  )['hit'] ?? 0) === 1
47  );
48  } while ($exists);
49 
50  return $hash;
51 }
52 
56 function db_pwassist_session_read(string $pwassist_id): ?array
57 {
58  global $DIC;
59 
60  $ilDB = $DIC->database();
61 
62  $q = 'SELECT * FROM usr_pwassist WHERE pwassist_id = ' . $ilDB->quote($pwassist_id, ilDBConstants::T_TEXT);
63  $r = $ilDB->query($q);
64 
65  return $ilDB->fetchAssoc($r);
66 }
67 
71 function db_pwassist_session_find(int $user_id): ?array
72 {
73  global $DIC;
74 
75  $ilDB = $DIC->database();
76 
77  $q = 'SELECT * FROM usr_pwassist WHERE user_id = ' . $ilDB->quote($user_id, ilDBConstants::T_INTEGER);
78  $r = $ilDB->query($q);
79 
80  return $ilDB->fetchAssoc($r);
81 }
82 
83 function db_pwassist_session_write(string $pwassist_id, int $maxlifetime, int $user_id): void
84 {
85  global $DIC;
86 
87  $ilDB = $DIC->database();
88 
89  $q = 'DELETE FROM usr_pwassist ' .
90  'WHERE pwassist_id = ' . $ilDB->quote($pwassist_id, ilDBConstants::T_TEXT) . ' ' .
91  'OR user_id = ' . $ilDB->quote($user_id, ilDBConstants::T_INTEGER);
92  $ilDB->manipulate($q);
93 
94  $ctime = time();
95  $expires = $ctime + $maxlifetime;
96  $ilDB->manipulateF(
97  'INSERT INTO usr_pwassist (pwassist_id, expires, user_id, ctime) VALUES (%s, %s, %s, %s)',
99  [$pwassist_id, $expires, $user_id, $ctime]
100  );
101 }
102 
103 function db_pwassist_session_destroy(string $pwassist_id): void
104 {
105  global $DIC;
106 
107  $ilDB = $DIC->database();
108 
109  $q = 'DELETE FROM usr_pwassist WHERE pwassist_id = ' . $ilDB->quote($pwassist_id, ilDBConstants::T_TEXT);
110  $ilDB->manipulate($q);
111 }
112 
113 
114 function db_pwassist_session_gc(): void
115 {
116  global $DIC;
117 
118  $ilDB = $DIC->database();
119 
120  $q = 'DELETE FROM usr_pwassist WHERE expires < ' . $ilDB->quote(time(), ilDBConstants::T_INTEGER);
121  $ilDB->manipulate($q);
122 }
db_pwassist_session_destroy(string $pwassist_id)
db_pwassist_create_id()
Creates a new secure id.
global $DIC
Definition: feed.php:28
db_pwassist_session_read(string $pwassist_id)
db_pwassist_session_write(string $pwassist_id, int $maxlifetime, int $user_id)
static getBytes(int $length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.
$q
Definition: shib_logout.php:21
db_pwassist_session_find(int $user_id)
$r