Source for file Host.php

Documentation is available at Host.php

  1. <?php
  2.  
  3. /**
  4.  * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
  5.  */
  6. {
  7.     
  8.     /**
  9.      * Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator
  10.      */
  11.     protected $ipv4;
  12.     
  13.     /**
  14.      * Instance of HTMLPurifier_AttrDef_URI_IPv6 sub-validator
  15.      */
  16.     protected $ipv6;
  17.     
  18.     public function __construct({
  19.         $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
  20.         $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
  21.     }
  22.     
  23.     public function validate($string$config$context{
  24.         $length strlen($string);
  25.         if ($string === ''return '';
  26.         if ($length && $string[0=== '[' && $string[$length-1=== ']'{
  27.             //IPv6
  28.             $ip substr($string1$length 2);
  29.             $valid $this->ipv6->validate($ip$config$context);
  30.             if ($valid === falsereturn false;
  31.             return '['$valid ']';
  32.         }
  33.         
  34.         // need to do checks on unusual encodings too
  35.         $ipv4 $this->ipv4->validate($string$config$context);
  36.         if ($ipv4 !== falsereturn $ipv4;
  37.         
  38.         // A regular domain name.
  39.         
  40.         // This breaks I18N domain names, but we don't have proper IRI support,
  41.         // so force users to insert Punycode. If there's complaining we'll 
  42.         // try to fix things into an international friendly form.
  43.         
  44.         // The productions describing this are:
  45.         $a   '[a-z]';     // alpha
  46.         $an  '[a-z0-9]';  // alphanum
  47.         $and '[a-z0-9-]'// alphanum | "-"
  48.         // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
  49.         $domainlabel   "$an($and*$an)?";
  50.         // toplabel    = alpha | alpha *( alphanum | "-" ) alphanum
  51.         $toplabel      "$a($and*$an)?";
  52.         // hostname    = *( domainlabel "." ) toplabel [ "." ]
  53.         $match preg_match("/^($domainlabel\.)*$toplabel\.?$/i"$string);
  54.         if (!$matchreturn false;
  55.         
  56.         return $string;
  57.     }
  58.     
  59. }

Documentation generated on Thu, 19 Jun 2008 18:49:17 -0400 by phpDocumentor 1.4.2