ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilWhiteListUrlValidator.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
11  protected $url = '';
12 
14  protected $whitelist = [];
15 
21  public function __construct(string $url, array $whitelist)
22  {
23  $this->url = $url;
24  $this->whitelist = array_filter(array_map(function (string $domain) {
25  return trim($domain); // Used for trimming and type validation (strict primitive type hint)
26  }, $whitelist));
27  }
28 
33  private function isValidDomain(string $domain) : bool
34  {
35  foreach ($this->whitelist as $validDomain) {
36  if ($domain === $validDomain) {
37  return true;
38  }
39 
40  $firstChar = $validDomain[0];
41  if ('.' !== $firstChar) {
42  $validDomain = '.' . $validDomain;
43  }
44 
45  if (strlen($domain) > strlen($validDomain)) {
46  if (substr($domain, (0 - strlen($validDomain))) === $validDomain) {
47  return true;
48  }
49  }
50  }
51 
52  return false;
53  }
54 
58  public function isValid() : bool
59  {
60  $redirectDomain = parse_url($this->url, PHP_URL_HOST);
61  if (null === $redirectDomain) {
62  return false;
63  }
64 
65  return $this->isValidDomain($redirectDomain);
66  }
67 }
Class ilWhiteListUrlValidator.
__construct(string $url, array $whitelist)
ilWhiteListUrlValidator constructor.