ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
URLBuilderToken.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\UI;
22 
30 {
31  public const TOKEN_LENGTH = 24;
32  private array $namespace;
33  private string $name;
34  private string $token;
35 
36  public function __construct(array $namespace, string $name)
37  {
38  $this->namespace = $namespace;
39  $this->name = $name;
40  $this->token = $this->createToken();
41  }
42 
46  public function getToken(): string
47  {
48  return $this->token;
49  }
50 
55  public function getName(): string
56  {
57  return implode(URLBuilder::SEPARATOR, $this->namespace) . URLBuilder::SEPARATOR . $this->name;
58  }
59 
60  public function __toString(): string
61  {
62  return $this->getName();
63  }
64 
70  private function createToken(): string
71  {
72  try {
73  $token = bin2hex(random_bytes(self::TOKEN_LENGTH / 2));
74  } catch (\Throwable $t) {
75  $token = md5(uniqid((string) time(), true));
76  $token = substr($token, 0, self::TOKEN_LENGTH);
77  }
78  return $token;
79  }
80 
85  public function render(): string
86  {
87  $namespace = [];
88  foreach ($this->namespace as $name) {
89  $namespace[] = '"' . $name . '"';
90  }
91  $output = 'new il.UI.core.URLBuilderToken([' . implode(',', $namespace) . '], "' . $this->name . '", "' . $this->token . '")';
92  return $output;
93  }
94 }
getName()
Get the full name of the token including its namespace.
const SEPARATOR
Separator for parts of a parameter&#39;s namespace.
Definition: URLBuilder.php:49
getToken()
Get the token hash value.
render()
Output the JS equivalent of the token as a string.
createToken()
Creates a randomized string of length URLBuilderToken::TOKEN_LENGTH generated by the random_bytes() f...
__construct(array $namespace, string $name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...