ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
WhiteListUrlValidator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\AuthApache;
22
23final readonly class WhiteListUrlValidator
24{
26 private array $whitelist;
27
31 public function __construct(private string $url, array $whitelist)
32 {
33 $this->whitelist = array_filter(array_map(static function (string $domain): string {
34 return trim($domain);
35 }, $whitelist));
36 }
37
38 private function isValidHost(string $host): bool
39 {
40 foreach ($this->whitelist as $valid_host) {
41 if ($host === $valid_host) {
42 return true;
43 }
44
45 if (!str_starts_with($valid_host, '.')) {
46 $valid_host = '.' . $valid_host;
47 }
48
49 if ((\strlen($host) > \strlen($valid_host)) && substr(
50 $host,
51 (0 - \strlen($valid_host))
52 ) === $valid_host) {
53 return true;
54 }
55 }
56
57 return false;
58 }
59
60 public function isValid(): bool
61 {
62 $host = parse_url($this->url, PHP_URL_HOST);
63 if ($host === null) {
64 return false;
65 }
66
67 return $this->isValidHost($host);
68 }
69}
__construct(private string $url, array $whitelist)
$url
Definition: shib_logout.php:68