/** * Generate host (FQDN, IPv4, ...) regex * 'localhost' : Matches with 'localhost' only * 'example.org' : Matches with 'example.org' only (See host_normalize() about 'www') * '.example.org' : Matches with ALL FQDN ended with '.example.org' * '*.example.org' : Almost the same of '.example.org' except 'www.example.org' * '10.20.30.40' : Matches with IPv4 address '10.20.30.40' only * [TODO] '192.' : Matches with all IPv4 hosts started with '192.' * TODO: IPv4, CIDR?, IPv6 */ private static function generate_host_regex($string = '', $divider = '/') { if (!is_string($string)) { return ''; } if (mb_strpos($string, '.') === FALSE || SpamPickup::is_ip($string)) { // "localhost", IPv4, etc return self::generate_glob_regex($string, $divider); } // FQDN or something $part = explode('.', $string, 2); if ($part[0] == '') { // ".example.org" $part[0] = '(?:.*\\.)?'; } else { if ($part[0] == '*') { // "*.example.org" $part[0] = '.*\\.'; } else { // example.org, etc return self::generate_glob_regex($string, $divider); } } $part[1] = self::generate_glob_regex($part[1], $divider); return implode('', $part); }
/** * Check responsibility-root of the FQDN * 'foo.bar.example.com' => 'example.com' (.com has the last whois for it) * 'foo.bar.example.au' => 'example.au' (.au has the last whois for it) * 'foo.bar.example.edu.au' => 'example.edu.au' (.edu.au has the last whois for it) * 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it) * @staticvar type $domain * @param type $fqdn * @param type $parent * @param type $implicit * @return string */ static function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE) { static $domain; if ($fqdn === NULL) { $domain = NULL; // Unset return ''; } if (!is_string($fqdn)) { return ''; } if (SpamPickup::is_ip($fqdn)) { return $fqdn; } if (!isset($domain)) { $domain = array(); if (file_exists(DOMAIN_INI_FILE)) { include DOMAIN_INI_FILE; // Set } } $result = array(); $dcursor =& $domain; $array = array_reverse(explode('.', $fqdn)); $i = 0; while (TRUE) { if (!isset($array[$i])) { break; } $acursor = $array[$i]; if (is_array($dcursor) && isset($dcursor[$acursor])) { $result[] =& $array[$i]; $dcursor =& $dcursor[$acursor]; } else { if (!$parent && isset($acursor)) { $result[] =& $array[$i]; // Whois servers must know this subdomain } break; } ++$i; } // Implicit responsibility: Top-Level-Domains must not be yours // 'bar.foo.something' => 'foo.something' if ($implicit && count($result) == 1 && count($array) > 1) { $result[] =& $array[1]; } return $result ? implode('.', array_reverse($result)) : ''; }