ILIAS  release_8 Revision v8.23
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 {
25  protected string $url = '';
27  protected array $whitelist = [];
28 
34  public function __construct(string $url, array $whitelist)
35  {
36  $this->url = $url;
37  $this->whitelist = array_filter(array_map(static function (string $domain) {
38  return trim($domain); // Used for trimming and type validation (strict primitive type hint)
39  }, $whitelist));
40  }
41 
42  private function isValidDomain(string $domain): bool
43  {
44  foreach ($this->whitelist as $validDomain) {
45  if ($domain === $validDomain) {
46  return true;
47  }
48 
49  $firstChar = $validDomain[0];
50  if ('.' !== $firstChar) {
51  $validDomain = '.' . $validDomain;
52  }
53 
54  if ((strlen($domain) > strlen($validDomain)) && substr(
55  $domain,
56  (0 - strlen($validDomain))
57  ) === $validDomain) {
58  return true;
59  }
60  }
61 
62  return false;
63  }
64 
65  public function isValid(): bool
66  {
67  $redirectDomain = parse_url($this->url, PHP_URL_HOST);
68  if (null === $redirectDomain) {
69  return false;
70  }
71 
72  return $this->isValidDomain($redirectDomain);
73  }
74 }
Class ilWhiteListUrlValidator.
__construct(string $url, array $whitelist)
ilWhiteListUrlValidator constructor.