ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilWhiteListUrlValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /******************************************************************************
6  *
7  * This file is part of ILIAS, a powerful learning management system.
8  *
9  * ILIAS is licensed with the GPL-3.0, you should have received a copy
10  * of said license along with the source code.
11  *
12  * If this is not the case or you just want to try ILIAS, you'll find
13  * us at:
14  * https://www.ilias.de
15  * https://github.com/ILIAS-eLearning
16  *
17  *****************************************************************************/
18 
24 {
26  private array $whitelist;
27 
32  public function __construct(private string $url, array $whitelist)
33  {
34  $this->whitelist = array_filter(array_map(static function (string $domain): string {
35  return trim($domain); // Used for trimming and type validation (strict primitive type hint)
36  }, $whitelist));
37  }
38 
39  private function isValidDomain(string $domain): bool
40  {
41  foreach ($this->whitelist as $validDomain) {
42  if ($domain === $validDomain) {
43  return true;
44  }
45 
46  $firstChar = $validDomain[0];
47  if ('.' !== $firstChar) {
48  $validDomain = '.' . $validDomain;
49  }
50 
51  if ((strlen($domain) > strlen($validDomain)) && substr(
52  $domain,
53  (0 - strlen($validDomain))
54  ) === $validDomain) {
55  return true;
56  }
57  }
58 
59  return false;
60  }
61 
62  public function isValid(): bool
63  {
64  $redirectDomain = parse_url($this->url, PHP_URL_HOST);
65  if (null === $redirectDomain) {
66  return false;
67  }
68 
69  return $this->isValidDomain($redirectDomain);
70  }
71 }
$url
Definition: ltiregstart.php:35
__construct(private string $url, array $whitelist)
ilWhiteListUrlValidator constructor.
Class ilWhiteListUrlValidator.