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