ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilWACToken.php
Go to the documentation of this file.
1<?php
2
20{
24 private const SALT_FILE_PATH = __DIR__ . '/../../../../public/data/wacsalt.php';
25 protected static string $SALT = '';
26 protected string $session_id = '';
27 protected int $timestamp = 0;
28 protected string $ip = '';
29 protected string $token = '';
30 protected string $raw_token = '';
31 protected string $path = '';
32 protected string $id = '';
33 protected string $client = '';
34 protected int $ttl = 0;
35
36
40 public function __construct(
41 string $path,
42 string $client,
43 int $timestamp = 0,
44 int $ttl = 0
45 ) {
46 $this->setClient($client);
47 $this->setPath($path);
48 $session_id = session_id();
49 $this->setSessionId($session_id ?: '-');
50 if (isset($_SERVER['REMOTE_ADDR'])) {
51 $this->setIp($_SERVER['REMOTE_ADDR']);
52 }
53 $this->setTimestamp($timestamp !== 0 ? $timestamp : time());
55 $this->setTTL($ttl); // since we do not know the type at this poit we choose the shorter duration for security reasons
56 $this->generateToken();
57 $this->setId($this->getPath());
58 }
59
60
61 public function generateToken(): void
62 {
63 $this->initSalt();
64 $token = implode('-', [
65 self::getSALT(),
66 $this->getClient(),
67 $this->getTimestamp(),
68 $this->getTTL(),
69 ]);
70 $this->setRawToken($token);
71 $token = sha1($token);
72 $this->setToken($token);
73 }
74
75
76 protected function initSalt(): void
77 {
78 if (self::getSALT() !== '' && self::getSALT() !== '0') {
79 return;
80 }
81 $salt = '';
82 if (is_file(self::SALT_FILE_PATH)) {
85 self::setSALT($salt);
86 }
87
88 if (strcmp($salt, '') === 0) {
89 $this->generateSaltFile();
90 $this->initSalt();
91 }
92 }
93
94
98 protected function generateSaltFile(): void
99 {
100 if (is_file(self::SALT_FILE_PATH)) {
101 unlink(self::SALT_FILE_PATH);
102 }
103 $template = file_get_contents(__DIR__ . '/../wacsalt.php.template');
104 $random = new \Random\Randomizer();
105 $salt = md5(time() * $random->getInt(1000, 9999) . self::SALT_FILE_PATH);
106 self::setSALT($salt);
107 $template = str_replace('INSERT_SALT', $salt, $template);
108 if (is_writable(dirname(self::SALT_FILE_PATH))) {
109 file_put_contents(self::SALT_FILE_PATH, $template);
110 } else {
111 throw new ilWACException(ilWACException::DATA_DIR_NON_WRITEABLE, self::SALT_FILE_PATH);
112 }
113 }
114
115
116 public function getSessionId(): string
117 {
118 return $this->session_id;
119 }
120
121
122 public function setSessionId(string $session_id): void
123 {
124 $this->session_id = $session_id;
125 }
126
127
128 public function getTimestamp(): int
129 {
130 return $this->timestamp;
131 }
132
133
134 public function setTimestamp(int $timestamp): void
135 {
136 $this->timestamp = $timestamp;
137 }
138
139
140 public function getIp(): string
141 {
142 return $this->ip;
143 }
144
145
146 public function setIp(string $ip): void
147 {
148 $this->ip = $ip;
149 }
150
151
152 public function getToken(): string
153 {
154 return $this->token;
155 }
156
157
158 public function setToken(string $token): void
159 {
160 $this->token = $token;
161 }
162
163
164 public function getPath(): string
165 {
166 return $this->path;
167 }
168
169
170 public function setPath(string $path): void
171 {
172 $this->path = $path;
173 }
174
175
176 public function getId(): string
177 {
178 return $this->id;
179 }
180
181
182 public function getHashedId(): string
183 {
184 return md5($this->id);
185 }
186
187
188 public function setId(string $id): void
189 {
190 $this->id = $id;
191 }
192
193
194 public static function getSALT(): string
195 {
196 return self::$SALT;
197 }
198
199
200 public static function setSALT(string $salt): void
201 {
202 self::$SALT = $salt;
203 }
204
205
206 public function getClient(): string
207 {
208 return $this->client;
209 }
210
211
212 public function setClient(string $client): void
213 {
214 $this->client = $client;
215 }
216
217
218 public function getTTL(): int
219 {
220 return $this->ttl;
221 }
222
223
224 public function setTTL(int $ttl): void
225 {
226 $this->ttl = $ttl;
227 }
228
229
230 public function getRawToken(): string
231 {
232 return $this->raw_token;
233 }
234
235
236 public function setRawToken(string $raw_token): void
237 {
238 $this->raw_token = $raw_token;
239 }
240}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getTokenMaxLifetimeInSeconds()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getSALT()
string $session_id
__construct(string $path, string $client, int $timestamp=0, int $ttl=0)
ilWACToken constructor.
static string $SALT
setPath(string $path)
setToken(string $token)
setIp(string $ip)
setRawToken(string $raw_token)
const SALT_FILE_PATH
static setSALT(string $salt)
setSessionId(string $session_id)
setId(string $id)
setTimestamp(int $timestamp)
setTTL(int $ttl)
setClient(string $client)
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26