ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilWhiteListUrlValidator.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3declare(strict_types=1);
4
10{
12 protected $url = '';
13
15 protected $whitelist = [];
16
22 public function __construct(string $url, array $whitelist)
23 {
24 $this->url = $url;
25 $this->whitelist = array_filter(array_map(function (string $domain) {
26 return trim($domain); // Used for trimming and type validation (strict primitive type hint)
27 }, $whitelist));
28 }
29
34 private function isValidDomain(string $domain) : bool
35 {
36 foreach ($this->whitelist as $validDomain) {
37 if ($domain === $validDomain) {
38 return true;
39 }
40
41 $firstChar = $validDomain[0];
42 if ('.' !== $firstChar) {
43 $validDomain = '.' . $validDomain;
44 }
45
46 if (strlen($domain) > strlen($validDomain)) {
47 if (substr($domain, (0 - strlen($validDomain))) === $validDomain) {
48 return true;
49 }
50 }
51 }
52
53 return false;
54 }
55
59 public function isValid() : bool
60 {
61 $redirectDomain = parse_url($this->url, PHP_URL_HOST);
62 if (null === $redirectDomain) {
63 return false;
64 }
65
66 return $this->isValidDomain($redirectDomain);
67 }
68}
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
An exception for terminatinating execution or to throw for unit testing.
Class ilWhiteListUrlValidator.
__construct(string $url, array $whitelist)
ilWhiteListUrlValidator constructor.