ILIAS  release_4-4 Revision
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
 Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator. More...
 
 $ipv6
 Instance of HTMLPurifier_AttrDef_URI_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 19 of file Host.php.

19  {
20  $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
21  $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
22  }
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 
)

Definition at line 24 of file Host.php.

References $ipv4, and $valid.

24  {
25  $length = strlen($string);
26  // empty hostname is OK; it's usually semantically equivalent:
27  // the default host as defined by a URI scheme is used:
28  //
29  // If the URI scheme defines a default for host, then that
30  // default applies when the host subcomponent is undefined
31  // or when the registered name is empty (zero length).
32  if ($string === '') return '';
33  if ($length > 1 && $string[0] === '[' && $string[$length-1] === ']') {
34  //IPv6
35  $ip = substr($string, 1, $length - 2);
36  $valid = $this->ipv6->validate($ip, $config, $context);
37  if ($valid === false) return false;
38  return '['. $valid . ']';
39  }
40 
41  // need to do checks on unusual encodings too
42  $ipv4 = $this->ipv4->validate($string, $config, $context);
43  if ($ipv4 !== false) return $ipv4;
44 
45  // A regular domain name.
46 
47  // This doesn't match I18N domain names, but we don't have proper IRI support,
48  // so force users to insert Punycode.
49 
50  // The productions describing this are:
51  $a = '[a-z]'; // alpha
52  $an = '[a-z0-9]'; // alphanum
53  $and = '[a-z0-9-]'; // alphanum | "-"
54  // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
55  $domainlabel = "$an($and*$an)?";
56  // toplabel = alpha | alpha *( alphanum | "-" ) alphanum
57  $toplabel = "$a($and*$an)?";
58  // hostname = *( domainlabel "." ) toplabel [ "." ]
59  if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
60  return $string;
61  }
62 
63  // If we have Net_IDNA2 support, we can support IRIs by
64  // punycoding them. (This is the most portable thing to do,
65  // since otherwise we have to assume browsers support
66 
67  if ($config->get('Core.EnableIDNA')) {
68  $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
69  // we need to encode each period separately
70  $parts = explode('.', $string);
71  try {
72  $new_parts = array();
73  foreach ($parts as $part) {
74  $encodable = false;
75  for ($i = 0, $c = strlen($part); $i < $c; $i++) {
76  if (ord($part[$i]) > 0x7a) {
77  $encodable = true;
78  break;
79  }
80  }
81  if (!$encodable) {
82  $new_parts[] = $part;
83  } else {
84  $new_parts[] = $idna->encode($part);
85  }
86  }
87  $string = implode('.', $new_parts);
88  if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
89  return $string;
90  }
91  } catch (Exception $e) {
92  // XXX error reporting
93  }
94  }
95 
96  return false;
97  }
$valid
$ipv4
Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator.
Definition: Host.php:12

Field Documentation

◆ $ipv4

HTMLPurifier_AttrDef_URI_Host::$ipv4
protected

Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator.

Definition at line 12 of file Host.php.

Referenced by validate().

◆ $ipv6

HTMLPurifier_AttrDef_URI_Host::$ipv6
protected

Instance of HTMLPurifier_AttrDef_URI_IPv6 sub-validator.

Definition at line 17 of file Host.php.


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