2006-08-17 01:05:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/AttrDef.php';
|
2007-02-14 20:38:51 +00:00
|
|
|
require_once 'HTMLPurifier/AttrDef/URI/IPv4.php';
|
|
|
|
require_once 'HTMLPurifier/AttrDef/URI/IPv6.php';
|
2006-08-17 01:05:35 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
2006-11-12 19:26:49 +00:00
|
|
|
* Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_URI_Host extends HTMLPurifier_AttrDef
|
2006-08-17 01:05:35 +00:00
|
|
|
{
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
2007-02-14 20:38:51 +00:00
|
|
|
* Instance of HTMLPurifier_AttrDef_URI_IPv4 sub-validator
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $ipv4;
|
2006-09-23 00:43:21 +00:00
|
|
|
|
|
|
|
/**
|
2007-02-14 20:38:51 +00:00
|
|
|
* Instance of HTMLPurifier_AttrDef_URI_IPv6 sub-validator
|
2006-09-23 00:43:21 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $ipv6;
|
2006-08-17 01:05:35 +00:00
|
|
|
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct() {
|
2007-02-14 20:38:51 +00:00
|
|
|
$this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
|
|
|
|
$this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($string, $config, $context) {
|
2006-08-17 01:05:35 +00:00
|
|
|
$length = strlen($string);
|
|
|
|
if ($string === '') return '';
|
|
|
|
if ($length > 1 && $string[0] === '[' && $string[$length-1] === ']') {
|
|
|
|
//IPv6
|
|
|
|
$ip = substr($string, 1, $length - 2);
|
|
|
|
$valid = $this->ipv6->validate($ip, $config, $context);
|
|
|
|
if ($valid === false) return false;
|
|
|
|
return '['. $valid . ']';
|
|
|
|
}
|
2006-11-12 19:26:49 +00:00
|
|
|
|
|
|
|
// need to do checks on unusual encodings too
|
2006-08-17 01:05:35 +00:00
|
|
|
$ipv4 = $this->ipv4->validate($string, $config, $context);
|
|
|
|
if ($ipv4 !== false) return $ipv4;
|
|
|
|
|
|
|
|
// validate a domain name here, do filtering, etc etc etc
|
|
|
|
|
|
|
|
// We could use this, but it would break I18N domain names
|
|
|
|
//$match = preg_match('/^[a-z0-9][\w\-\.]*[a-z0-9]$/i', $string);
|
|
|
|
//if (!$match) return false;
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|