ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilCmiXapiAuthToken.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
16{
17 const DB_TABLE_NAME = 'cmix_token';
18
19 const OPENSSL_ENCRYPTION_METHOD = 'aes128';
20
21 const OPENSSL_IV = '1234567890123456';
22
26 protected $ref_id;
27
31 protected $obj_id;
32
36 protected $usr_id;
37
41 protected $token;
42
46 protected $valid_until;
47
51 protected $lrs_type_id;
52
56 protected $cmi5_session;
57
62
67
71 public function getRefId() : int
72 {
73 return $this->ref_id;
74 }
75
79 public function setRefId(int $ref_id)
80 {
81 $this->ref_id = $ref_id;
82 }
83
87 public function getObjId() : int
88 {
89 return $this->obj_id;
90 }
91
95 public function setObjId(int $obj_id)
96 {
97 $this->obj_id = $obj_id;
98 }
99
103 public function getUsrId() : int
104 {
105 return $this->usr_id;
106 }
107
111 public function setUsrId(int $usr_id)
112 {
113 $this->usr_id = $usr_id;
114 }
115
119 public function getToken() : string
120 {
121 return $this->token;
122 }
123
127 public function setToken(string $token)
128 {
129 $this->token = $token;
130 }
131
135 public function getValidUntil() : string
136 {
137 return $this->valid_until;
138 }
139
143 public function setValidUntil(string $valid_until)
144 {
145 $this->valid_until = $valid_until;
146 }
147
151 public function getLrsTypeId()
152 {
153 return $this->lrs_type_id;
154 }
155
159 public function setLrsTypeId($lrs_type_id)
160 {
161 $this->lrs_type_id = $lrs_type_id;
162 }
163
167 public function getCmi5Session()
168 {
169 return $this->cmi5_session;
170 }
171
176 {
177 $this->cmi5_session = $cmi5_session;
178 }
179
183 public function getCmi5SessionData()
184 {
186 }
187
192 {
193 $this->cmi5_session_data = $cmi5_session_data;
194 }
195
200 {
202 }
203
208 {
209 $this->returned_for_cmi5_session = $returned_for_cmi5_session;
210 }
211
212 public function update()
213 {
214 global $DIC; /* @var \ILIAS\DI\Container $DIC */
215 $DIC->database()->update(
216 self::DB_TABLE_NAME,
217 [
218 'valid_until' => array('timestamp', $this->getValidUntil()),
219 'ref_id' => array('integer', $this->getRefId()),
220 'obj_id' => array('integer', $this->getObjId()),
221 'usr_id' => array('integer', $this->getUsrId()),
222 'lrs_type_id' => array('integer', $this->getLrsTypeId()),
223 'cmi5_session' => array('text', $this->getCmi5Session()),
224 'returned_for_cmi5_session' => array('text', $this->getReturnedForCmi5Session()),
225 'cmi5_session_data' => array('clob', $this->getCmi5SessionData())
226 ],
227 [
228 'token' => array('text', $this->getToken()),
229 ]
230 );
231 }
232
233 public static function insertToken($usrId, $refId, $objId, $lrsTypeId, $a_token, $a_time)
234 {
235 global $DIC; /* @var \ILIAS\DI\Container $DIC */
236 $ilDB = $DIC->database();
237
238 $ilDB->insert(
239 self::DB_TABLE_NAME,
240 array(
241 'token' => array('text', $a_token),
242 'valid_until' => array('timestamp', $a_time),
243 'ref_id' => array('integer', $refId),
244 'obj_id' => array('integer', $objId),
245 'usr_id' => array('integer', $usrId),
246 'lrs_type_id' => array('integer', $lrsTypeId)
247 )
248 );
249 // 'cmi5_session' defaults always to '' by inserting
250 // 'returned_for_cmi5_session' defaults always to '' by inserting
251 }
252
253 public static function deleteTokenByObjIdAndUsrId($objId, $usrId)
254 {
255 global $DIC; /* @var \ILIAS\DI\Container $DIC */
256 $ilDB = $DIC->database();
257
258 $query = "
259 DELETE FROM " . self::DB_TABLE_NAME . "
260 WHERE obj_id = %s AND usr_id = %s
261 ";
262
263 $ilDB->manipulateF($query, array('integer', 'integer'), array($objId, $usrId));
264 }
265
266 public static function deleteTokenByObjIdAndRefIdAndUsrId($objId, $refId, $usrId)
267 {
268 global $DIC; /* @var \ILIAS\DI\Container $DIC */
269 $ilDB = $DIC->database();
270
271 $query = "
272 DELETE FROM " . self::DB_TABLE_NAME . "
273 WHERE obj_id = %s AND ref_id = %s AND usr_id = %s
274 ";
275
276 $ilDB->manipulateF($query, array('integer', 'integer', 'integer'), array($objId, $refId, $usrId));
277 }
278
279 public function delete()
280 {
281 global $DIC; /* @var \ILIAS\DI\Container $DIC */
282 $ilDB = $DIC->database();
283
284 $query = "
285 DELETE FROM " . self::DB_TABLE_NAME . "
286 WHERE obj_id = %s AND ref_id = %s AND usr_id = %s
287 ";
288
289 $ilDB->manipulateF($query, array('integer', 'integer', 'integer'), array($this->getObjId(), $this->getRefId(), $this->getUsrId()));
290 }
291
292 public static function deleteExpiredTokens()
293 {
294 global $DIC; /* @var \ILIAS\DI\Container $DIC */
295 $ilDB = $DIC->database();
296
297 $query = "DELETE FROM " . self::DB_TABLE_NAME . " WHERE valid_until < CURRENT_TIMESTAMP";
298 $ilDB->manipulate($query);
299 }
300
301
302 public static function selectCurrentTimestamp()
303 {
304 global $DIC; /* @var \ILIAS\DI\Container $DIC */
305 $ilDB = $DIC->database();
306
307 $query = "SELECT CURRENT_TIMESTAMP";
308 $result = $ilDB->query($query);
309 $row = $ilDB->fetchAssoc($result);
310
311 return $row['CURRENT_TIMESTAMP'];
312 }
313
314 public static function createToken()
315 {
316 return (new \Ramsey\Uuid\UuidFactory())->uuid4()->toString();
317 }
318
319 public static function fillToken($usrId, $refId, $objId, $lrsTypeId = 0)
320 {
321 //$seconds = $this->getTimeToDelete();
322 $seconds = 86400; // TODO: invalidation interval
323
324 $nowTimeDT = self::selectCurrentTimestamp();
325
326 $nowTime = new ilDateTime($nowTimeDT, IL_CAL_DATETIME);
327
328 $nowTimeTS = $nowTime->get(IL_CAL_UNIX);
329 $newTimeTS = $nowTimeTS + $seconds;
330
331 $newTime = new ilDateTime($newTimeTS, IL_CAL_UNIX);
332
333 //self::deleteTokenByObjIdAndUsrId($object->getId(), $usrId);
334
335 try {
336 $tokenObject = self::getInstanceByObjIdAndRefIdAndUsrId($objId, $refId, $usrId, false);
337 $tokenObject->setValidUntil($newTime->get(IL_CAL_DATETIME));
338 $tokenObject->update();
339
340 $token = $tokenObject->getToken();
341 } catch (ilCmiXapiException $e) {
343 self::insertToken($usrId, $refId, $objId, $lrsTypeId, $token, $newTime->get(IL_CAL_DATETIME));
344 }
345
346 // TODO: move to cronjob ;-)
347 // TODO: check cmi5 sessions of token and if not terminated -> abandoned statement
349
350 return $token;
351 }
352
358 public static function getInstanceByToken($token)
359 {
360 global $DIC; /* @var \ILIAS\DI\Container $DIC */
361
362 $query = "
363 SELECT * FROM " . self::DB_TABLE_NAME . "
364 WHERE token = %s AND valid_until > CURRENT_TIMESTAMP
365 ";
366
367 $res = $DIC->database()->queryF($query, array('text'), array($token));
368
369 while ($row = $DIC->database()->fetchAssoc($res)) {
370 $tokenObject = new self();
371 $tokenObject->setToken($token);
372 $tokenObject->setValidUntil($row['valid_until']);
373 $tokenObject->setUsrId($row['usr_id']);
374 $tokenObject->setObjId($row['obj_id']);
375 $tokenObject->setRefId($row['ref_id']);
376 $tokenObject->setLrsTypeId($row['lrs_type_id']);
377 $tokenObject->setCmi5Session($row['cmi5_session']);
378 $tokenObject->setReturnedForCmi5Session($row['returned_for_cmi5_session']);
379 $tokenObject->setCmi5SessionData($row['cmi5_session_data']);
380
381 return $tokenObject;
382 }
383
384 throw new ilCmiXapiException('no valid token found for: ' . $token);
385 }
386
393 public static function getInstanceByObjIdAndUsrId($objId, $usrId, $checkValid = true)
394 {
395 global $DIC; /* @var \ILIAS\DI\Container $DIC */
396 $ilDB = $DIC->database();
397
398 $query = "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s";
399
400 if ($checkValid) {
401 $query .= " AND valid_until > CURRENT_TIMESTAMP";
402 }
403
404 $result = $ilDB->queryF($query, array('integer', 'integer'), array($objId, $usrId));
405
406 $row = $ilDB->fetchAssoc($result);
407
408 if ($row) {
409 $tokenObject = new self();
410 $tokenObject->setToken($row['token']);
411 $tokenObject->setValidUntil($row['valid_until']);
412 $tokenObject->setUsrId($row['usr_id']);
413 $tokenObject->setObjId($row['obj_id']);
414 $tokenObject->setRefId($row['ref_id']);
415 $tokenObject->setLrsTypeId($row['lrs_type_id']);
416 $tokenObject->setCmi5Session($row['cmi5_session']);
417 $tokenObject->setReturnedForCmi5Session($row['returned_for_cmi5_session']);
418 $tokenObject->setCmi5SessionData($row['cmi5_session_data']);
419
420 return $tokenObject;
421 }
422
423 throw new ilCmiXapiException('no valid token found for: ' . $objId . '/' . $usrId);
424 }
425
433 public static function getInstanceByObjIdAndRefIdAndUsrId($objId, $refId, $usrId, $checkValid = true)
434 {
435 global $DIC; /* @var \ILIAS\DI\Container $DIC */
436 $ilDB = $DIC->database();
437
438 $query = "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND ref_id = %s AND usr_id = %s";
439
440 if ($checkValid) {
441 $query .= " AND valid_until > CURRENT_TIMESTAMP";
442 }
443
444 $result = $ilDB->queryF($query, array('integer', 'integer', 'integer'), array($objId, $refId, $usrId));
445
446 $row = $ilDB->fetchAssoc($result);
447
448 if ($row) {
449 $tokenObject = new self();
450 $tokenObject->setToken($row['token']);
451 $tokenObject->setValidUntil($row['valid_until']);
452 $tokenObject->setUsrId($row['usr_id']);
453 $tokenObject->setObjId($row['obj_id']);
454 $tokenObject->setRefId($row['ref_id']);
455 $tokenObject->setLrsTypeId($row['lrs_type_id']);
456 $tokenObject->setCmi5Session($row['cmi5_session']);
457 $tokenObject->setReturnedForCmi5Session($row['returned_for_cmi5_session']);
458 $tokenObject->setCmi5SessionData($row['cmi5_session_data']);
459
460 return $tokenObject;
461 }
462
463 throw new ilCmiXapiException('no valid token found for: ' . $objId . '/' . $usrId);
464 }
465
466 /*
467 public static function bindCmi5Session(string $token, string $cmi5_session)
468 {
469 global $DIC;
470 $ilDB = $DIC->database();
471 $ilDB->manipulate("UPDATE " . self::DB_TABLE_NAME . " SET cmi5_session = " . $ilDB->quote($cmi5_session, 'text') . " WHERE token = " . $ilDB->quote($token, 'text'));
472 }
473 */
474
483 public static function getCmi5SessionByUsrIdAndObjIdAndRefId(int $usrId, int $objId, $refId = null)
484 {
485 global $DIC;
486 $ilDB = $DIC->database();
487 if (empty($refId)) {
488 $query = "SELECT cmi5_session FROM " . self::DB_TABLE_NAME . " WHERE usr_id = %s AND obj_id = %s";
489 $result = $ilDB->queryF($query, array('integer', 'integer'), array($usrId, $objId));
490 } else {
491 $query = "SELECT cmi5_session FROM " . self::DB_TABLE_NAME . " WHERE usr_id = %s AND obj_id = %s AND ref_id = %s";
492 $result = $ilDB->queryF($query, array('integer', 'integer', 'integer'), array($usrId, $objId, $refId));
493 }
494
495 $row = $ilDB->fetchAssoc($result);
496
497 if ($row && $row['cmi5_session'] != '') {
498 return $row['cmi5_session'];
499 }
500 throw new ilCmiXapiException('no valid cmi5_session found for: ' . $objId . '/' . $usrId);
501 }
502
507 public static function getWacSalt()
508 {
509 include 'data/wacsalt.php';
510
511 if (isset($salt)) {
512 return $salt;
513 }
514
515 throw new ilCmiXapiException('no salt for encryption provided');
516 }
517}
$result
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_DATETIME
static deleteTokenByObjIdAndUsrId($objId, $usrId)
static getInstanceByObjIdAndUsrId($objId, $usrId, $checkValid=true)
static fillToken($usrId, $refId, $objId, $lrsTypeId=0)
setCmi5SessionData($cmi5_session_data)
static getInstanceByObjIdAndRefIdAndUsrId($objId, $refId, $usrId, $checkValid=true)
static getCmi5SessionByUsrIdAndObjIdAndRefId(int $usrId, int $objId, $refId=null)
setReturnedForCmi5Session($returned_for_cmi5_session)
static insertToken($usrId, $refId, $objId, $lrsTypeId, $a_token, $a_time)
setValidUntil(string $valid_until)
static deleteTokenByObjIdAndRefIdAndUsrId($objId, $refId, $usrId)
@classDescription Date and time handling
global $DIC
Definition: goto.php:24
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$objId
Definition: xapitoken.php:39
$refId
Definition: xapitoken.php:40