/** * Attempts to return a concrete IDNA instance for either php4 or php5, * only creating a new instance if no IDNA instance with the same * parameters currently exists. * * @param array $params Set of paramaters * @return object IDNA The newly created concrete Log instance, or an * false on an error. * @access public */ function &singleton($params = array()) { static $instances; if (!isset($instances)) { $instances = array(); } $signature = serialize($params); if (!isset($instances[$signature])) { $instances[$signature] =& Net_IDNA::getInstance($params); } return $instances[$signature]; }
/** * Determine if given domain or address literal is valid * eg for use in JIDs and URLs. Does not check if the domain * exists! * * @param string $domain * @return boolean valid or not */ function common_valid_domain($domain) { $octet = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])"; $ipv4 = "(?:{$octet}(?:\\.{$octet}){3})"; if (preg_match("/^{$ipv4}\$/u", $domain)) { return true; } $group = "(?:[0-9a-f]{1,4})"; $ipv6 = "(?:\\[({$group}(?::{$group}){0,7})?(::)?({$group}(?::{$group}){0,7})?\\])"; // http://tools.ietf.org/html/rfc3513#section-2.2 if (preg_match("/^{$ipv6}\$/ui", $domain, $matches)) { $before = explode(":", $matches[1]); $zeroes = $matches[2]; $after = explode(":", $matches[3]); if ($zeroes) { $min = 0; $max = 7; } else { $min = 1; $max = 8; } $explicit = count($before) + count($after); if ($explicit < $min || $explicit > $max) { return false; } return true; } try { require_once "Net/IDNA.php"; $idn = Net_IDNA::getInstance(); $domain = $idn->encode($domain); } catch (Exception $e) { return false; } $subdomain = "(?:[a-z0-9][a-z0-9-]*)"; // @fixme $fqdn = "(?:{$subdomain}(?:\\.{$subdomain})*\\.?)"; return preg_match("/^{$fqdn}\$/ui", $domain); }
<?php header('Content-Type: text/html; charset=UTF-8'); require 'Net/IDNA.php'; $idn = Net_IDNA::getInstance(); if (isset($_REQUEST['encode'])) { $decoded = isset($_REQUEST['decoded']) ? $_REQUEST['decoded'] : ''; try { $encoded = $idn->encode($decoded); } catch (Exception $e) { /* just swallow */ } } if (isset($_REQUEST['decode'])) { $encoded = isset($_REQUEST['encoded']) ? $_REQUEST['encoded'] : ''; try { $decoded = $idn->decode($encoded); } catch (Exception $e) { /* just swallow */ } } if (!isset($encoded)) { $encoded = ''; } if (!isset($decoded)) { $decoded = ''; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>