ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
HTMLPurifier_AttrDef_URI_Host Class Reference

Validates a host according to the IPv4, IPv6 and DNS (future) specifications. More...

+ Inheritance diagram for HTMLPurifier_AttrDef_URI_Host:
+ Collaboration diagram for HTMLPurifier_AttrDef_URI_Host:

Public Member Functions

 __construct ()
 
 validate ($string, $config, $context)
 
- Public Member Functions inherited from HTMLPurifier_AttrDef
 validate ($string, $config, $context)
 Validates and cleans passed string according to a definition. More...
 
 parseCDATA ($string)
 Convenience method that parses a string as if it were CDATA. More...
 
 make ($string)
 Factory method for creating this class from a string. More...
 

Protected Attributes

 $ipv4
 IPv4 sub-validator. More...
 
 $ipv6
 IPv6 sub-validator. More...
 

Additional Inherited Members

- Data Fields inherited from HTMLPurifier_AttrDef
 $minimized = false
 Tells us whether or not an HTML attribute is minimized. More...
 
 $required = false
 Tells us whether or not an HTML attribute is required. More...
 
- Protected Member Functions inherited from HTMLPurifier_AttrDef
 mungeRgb ($string)
 Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work properly. More...
 
 expandCSSEscape ($string)
 Parses a possibly escaped CSS string and returns the "pure" version of it. More...
 

Detailed Description

Validates a host according to the IPv4, IPv6 and DNS (future) specifications.

Definition at line 6 of file Host.php.

Constructor & Destructor Documentation

◆ __construct()

HTMLPurifier_AttrDef_URI_Host::__construct ( )

Definition at line 21 of file Host.php.

22  {
23  $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
24  $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
25  }
Validates an IPv6 address.
Definition: IPv6.php:9
Validates an IPv4 address.
Definition: IPv4.php:7

Member Function Documentation

◆ validate()

HTMLPurifier_AttrDef_URI_Host::validate (   $string,
  $config,
  $context 
)
Parameters
string$string
HTMLPurifier_Config$config
HTMLPurifier_Context$context
Returns
bool|string

Definition at line 33 of file Host.php.

References $ipv4, and $valid.

34  {
35  $length = strlen($string);
36  // empty hostname is OK; it's usually semantically equivalent:
37  // the default host as defined by a URI scheme is used:
38  //
39  // If the URI scheme defines a default for host, then that
40  // default applies when the host subcomponent is undefined
41  // or when the registered name is empty (zero length).
42  if ($string === '') {
43  return '';
44  }
45  if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') {
46  //IPv6
47  $ip = substr($string, 1, $length - 2);
48  $valid = $this->ipv6->validate($ip, $config, $context);
49  if ($valid === false) {
50  return false;
51  }
52  return '[' . $valid . ']';
53  }
54 
55  // need to do checks on unusual encodings too
56  $ipv4 = $this->ipv4->validate($string, $config, $context);
57  if ($ipv4 !== false) {
58  return $ipv4;
59  }
60 
61  // A regular domain name.
62 
63  // This doesn't match I18N domain names, but we don't have proper IRI support,
64  // so force users to insert Punycode.
65 
66  // There is not a good sense in which underscores should be
67  // allowed, since it's technically not! (And if you go as
68  // far to allow everything as specified by the DNS spec...
69  // well, that's literally everything, modulo some space limits
70  // for the components and the overall name (which, by the way,
71  // we are NOT checking!). So we (arbitrarily) decide this:
72  // let's allow underscores wherever we would have allowed
73  // hyphens, if they are enabled. This is a pretty good match
74  // for browser behavior, for example, a large number of browsers
75  // cannot handle foo_.example.com, but foo_bar.example.com is
76  // fairly well supported.
77  $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : '';
78 
79  // The productions describing this are:
80  $a = '[a-z]'; // alpha
81  $an = '[a-z0-9]'; // alphanum
82  $and = "[a-z0-9-$underscore]"; // alphanum | "-"
83  // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
84  $domainlabel = "$an($and*$an)?";
85  // toplabel = alpha | alpha *( alphanum | "-" ) alphanum
86  $toplabel = "$a($and*$an)?";
87  // hostname = *( domainlabel "." ) toplabel [ "." ]
88  if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
89  return $string;
90  }
91 
92  // If we have Net_IDNA2 support, we can support IRIs by
93  // punycoding them. (This is the most portable thing to do,
94  // since otherwise we have to assume browsers support
95  // mjansen-patch: begin
96  if (function_exists('idn_to_ascii')) {
97  return idn_to_ascii($string);
98  // If we have Net_IDNA2 support, we can support IRIs by
99  // punycoding them. (This is the most portable thing to do,
100  // since otherwise we have to assume browsers support
101  } elseif ($config->get('Core.EnableIDNA')) {
102  // mjansen-patch: end
103  $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
104  // we need to encode each period separately
105  $parts = explode('.', $string);
106  try {
107  $new_parts = array();
108  foreach ($parts as $part) {
109  $encodable = false;
110  for ($i = 0, $c = strlen($part); $i < $c; $i++) {
111  if (ord($part[$i]) > 0x7a) {
112  $encodable = true;
113  break;
114  }
115  }
116  if (!$encodable) {
117  $new_parts[] = $part;
118  } else {
119  $new_parts[] = $idna->encode($part);
120  }
121  }
122  $string = implode('.', $new_parts);
123  if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
124  return $string;
125  }
126  } catch (Exception $e) {
127  // XXX error reporting
128  }
129  }
130  return false;
131  }
$valid
$ipv4
IPv4 sub-validator.
Definition: Host.php:13

Field Documentation

◆ $ipv4

HTMLPurifier_AttrDef_URI_Host::$ipv4
protected

IPv4 sub-validator.

HTMLPurifier_AttrDef_URI_IPv4

Definition at line 13 of file Host.php.

Referenced by validate().

◆ $ipv6

HTMLPurifier_AttrDef_URI_Host::$ipv6
protected

IPv6 sub-validator.

HTMLPurifier_AttrDef_URI_IPv6

Definition at line 19 of file Host.php.


The documentation for this class was generated from the following file: