2006-08-17 01:05:35 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates an IPv4 address
|
|
|
|
* @author Feyd @ forums.devnetwork.net (public domain)
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_URI_IPv4 extends HTMLPurifier_AttrDef
|
2006-08-17 01:05:35 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* IPv4 regex, protected so that IPv6 can reuse it.
|
|
|
|
* @type string
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $ip4;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param string $aIP
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public function validate($aIP, $config, $context)
|
|
|
|
{
|
|
|
|
if (!$this->ip4) {
|
|
|
|
$this->_loadRegex();
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
if (preg_match('#^' . $this->ip4 . '$#s', $aIP)) {
|
|
|
|
return $aIP;
|
|
|
|
}
|
2006-08-17 01:05:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
/**
|
|
|
|
* Lazy load function to prevent regex from being stuffed in
|
|
|
|
* cache.
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
protected function _loadRegex()
|
|
|
|
{
|
2007-05-29 18:19:42 +00:00
|
|
|
$oct = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])'; // 0-255
|
|
|
|
$this->ip4 = "(?:{$oct}\\.{$oct}\\.{$oct}\\.{$oct})";
|
|
|
|
}
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|