/** * Decodes punycoded'd URL * @param string $url URL * @return mixed string with decoded URL on success, boolean false otherwise */ public static function decode($url) { $url = self::fix($url); if ($url) { $components = parse_url($url); $host = $components['host']; if (strpos($host, self::PUNYCODE_PREFIX) !== false) { $idn = new \Net_IDNA2(); $host = $idn->decode($host); } $path = !empty($components['path']) ? $components['path'] : ''; return $host . rtrim($path, '/'); } return false; }
function idn_to_ascii($domain, $options = 0, $variant = INTL_IDNA_VARIANT_2003, &$idna_info = null) { if ($domain && preg_match('/[^\\x20-\\x7E]/', $domain)) { if ($variant == INTL_IDNA_VARIANT_2003) { $idn = Net_IDNA2::singleton(array('encoding' => 'utf8', 'version' => '2003', 'overlong' => false)); } else { $idn = Net_IDNA2::singleton(array('encoding' => 'utf8', 'version' => '2008', 'overlong' => false)); } try { $domain = $idn->encode($domain); } catch (Exception $e) { } } return $domain; }
/** * Converts given IDN to the punycode. * @param string $value IDN to be converted. * @return string resulting punycode. * @since 1.1.13 */ private function encodeIDN($value) { if (preg_match_all('/^(.*)@(.*)$/', $value, $matches)) { if (function_exists('idn_to_ascii')) { $value = $matches[1][0] . '@' . idn_to_ascii($matches[2][0]); } else { require_once Yii::getPathOfAlias('system.vendors.Net_IDNA2.Net') . DIRECTORY_SEPARATOR . 'IDNA2.php'; $idna = new Net_IDNA2(); $value = $matches[1][0] . '@' . @$idna->encode($matches[2][0]); } } return $value; }
<?php // Platform's core initialization. require dirname(__FILE__) . '/../config.php'; require $PLATFORMCREATE; ci()->load->helper('url')->library('template'); header('Content-Type: text/html; charset=UTF-8'); //require 'Net/IDNA2.php'; $idn = Net_IDNA2::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 = ''; }
/** * 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 Net_IDNA2 * @access public */ function singleton($params = array()) { static $instances; if (!isset($instances)) { $instances = array(); } $signature = serialize($params); if (!isset($instances[$signature])) { $instances[$signature] = Net_IDNA2::getInstance($params); } return $instances[$signature]; }
if (file_exists(dirname(__FILE__) . "/../../../init.php")) { require_once dirname(__FILE__) . "/../../../init.php"; } else { require_once dirname(__FILE__) . "/../../../dbconnect.php"; } require_once dirname(__FILE__) . "/../../../includes/functions.php"; require_once dirname(__FILE__) . "/../../../includes/registrarfunctions.php"; require_once dirname(__FILE__) . "/namecheapapi.php"; $registrar = "namecheap"; $params = getregistrarconfigoptions($registrar); $testmode = (bool) $params['TestMode']; $username = $testmode ? $params['SandboxUsername'] : $params['Username']; $password = $testmode ? $params['SandboxPassword'] : $params['Password']; $sync_next_due_date = (bool) $params['SyncNextDueDate']; $idna2 = new Net_IDNA2(); $report = "Namecheap Domain Sync Report\n" . "-----------------------------------------------------------------------------------------------------\n\n"; /** * Transfers */ $report .= "Processing transfers:\n"; $dbresult = select_query("tbldomains", "id, LOWER(domain) AS domain", array('registrar' => $registrar, 'status' => "Pending Transfer")); if (!$dbresult || !mysql_num_rows($dbresult)) { $report .= "No domains with status 'Pending Transfer' found\n"; } else { $transfers = array(); try { $request_params = array('ListType' => "COMPLETED", 'PageSize' => 100, 'SortBy' => "DOMAINNAME"); if (!empty($params['PromotionCode'])) { $request_params['PromotionCode'] = $params['PromotionCode']; }
public function validate($string, $config, $context) { $length = strlen($string); // empty hostname is OK; it's usually semantically equivalent: // the default host as defined by a URI scheme is used: // // If the URI scheme defines a default for host, then that // default applies when the host subcomponent is undefined // or when the registered name is empty (zero length). 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 . ']'; } // need to do checks on unusual encodings too $ipv4 = $this->ipv4->validate($string, $config, $context); if ($ipv4 !== false) { return $ipv4; } // A regular domain name. // This doesn't match I18N domain names, but we don't have proper IRI support, // so force users to insert Punycode. // The productions describing this are: $a = '[a-z]'; // alpha $an = '[a-z0-9]'; // alphanum $and = '[a-z0-9-]'; // alphanum | "-" // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum $domainlabel = "{$an}({$and}*{$an})?"; // toplabel = alpha | alpha *( alphanum | "-" ) alphanum $toplabel = "{$a}({$and}*{$an})?"; // hostname = *( domainlabel "." ) toplabel [ "." ] if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } // If we have Net_IDNA2 support, we can support IRIs by // punycoding them. (This is the most portable thing to do, // since otherwise we have to assume browsers support if ($config->get('Core.EnableIDNA')) { $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true)); // we need to encode each period separately $parts = explode('.', $string); try { $new_parts = array(); foreach ($parts as $part) { $encodable = false; for ($i = 0, $c = strlen($part); $i < $c; $i++) { if (ord($part[$i]) > 0x7a) { $encodable = true; break; } } if (!$encodable) { $new_parts[] = $part; } else { $new_parts[] = $idna->encode($part); } } $string = implode('.', $new_parts); if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } } catch (Exception $e) { // XXX error reporting } } return false; }
/** * Check valid url * * @param string $url * @return bool */ public static function isValid($url) { if (filter_var($url, FILTER_VALIDATE_URL) !== false) { return true; } try { $idn = new \Net_IDNA2(); $url = $idn->encode($url); if (filter_var($url, FILTER_VALIDATE_URL) !== false) { return true; } } catch (\InvalidArgumentException $e) { } return false; }
/** * Converts given IDN to the punycode. * * @param string $value IDN to be converted. * * @return string resulting punycode. * @since 1.1.13 */ private function encodeIDN($value) { if (preg_match_all('/^(.*)@(.*)$/', $value, $matches)) { if (function_exists('idn_to_ascii')) { $value = $matches[1][0] . '@' . idn_to_ascii($matches[2][0]); } else { require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Net_IDNA2' . DIRECTORY_SEPARATOR . 'IDNA2.php'; $idna = new Net_IDNA2(); $value = $matches[1][0] . '@' . @$idna->encode($matches[2][0]); } } return $value; }
/** * Retrieve decoding domain name from punycode * * @param string $url decode url from Punycode * @return string */ public function decodePunycode($url) { $parsedUrl = parse_url($url); if ($this->_isPunycode($parsedUrl['host'])) { if (function_exists('idn_to_utf8')) { $host = idn_to_utf8($parsedUrl['host']); } else { $idn = new Net_IDNA2(); $host = $idn->decode($parsedUrl['host']); } return str_replace($parsedUrl['host'], $host, $url); } else { return $url; } }
public function validate($string, $config, $context) { $length = strlen($string); if ($string === '') { return ''; } if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') { $ip = substr($string, 1, $length - 2); $valid = $this->ipv6->validate($ip, $config, $context); if ($valid === false) { return false; } return '[' . $valid . ']'; } $ipv4 = $this->ipv4->validate($string, $config, $context); if ($ipv4 !== false) { return $ipv4; } $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : ''; $a = '[a-z]'; $an = '[a-z0-9]'; $and = "[a-z0-9-{$underscore}]"; $domainlabel = "{$an}({$and}*{$an})?"; $toplabel = "{$a}({$and}*{$an})?"; if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } if ($config->get('Core.EnableIDNA')) { $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true)); $parts = explode('.', $string); try { $new_parts = array(); foreach ($parts as $part) { $encodable = false; for ($i = 0, $c = strlen($part); $i < $c; $i++) { if (ord($part[$i]) > 0x7a) { $encodable = true; break; } } if (!$encodable) { $new_parts[] = $part; } else { $new_parts[] = $idna->encode($part); } } $string = implode('.', $new_parts); if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } } catch (Exception $e) { } } return false; }
/** * Decodes from punycode * @param string $host ONLY the host name, e.g. "XN----7SBCPNF2DL2EYA.XN--P1AI" * @return string */ function decode_host($host) { if (stripos($host, "xn--") === false) { return $host; } require_once 'Net/IDNA2.php'; // netcat/require/lib $decoder = new Net_IDNA2(); try { $host = $decoder->decode(strtolower($host)); } catch (Net_IDNA2_Exception $e) { trigger_error("Cannot convert host name '{$host}' from punycode: {$e->getMessage()}", E_USER_WARNING); return $host; } return $host; }
/** * Encodes a String from IDNA format ASCII * * @param $input * @return string */ function encode_idna($input) { if (function_exists('idn_to_ascii')) { return idn_to_ascii($input); } else { $IDNA = new Net_IDNA2(); $output = $IDNA->encode($input); return $output; } }
/** * Does a bunch of additional validation on the email address parts contained in $emailAddress * Then adds it to $emailAdddresses. */ private function _addAddress(&$emailAddresses, &$emailAddress, $encoding) { if (!$emailAddress['invalid']) { if ($emailAddress['address_temp'] || $emailAddress['quote_temp']) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = "Incomplete address"; $this->log('error', "Email\\Parse->_addAddress - corruption during parsing - leftovers:\n\$i: {$i}\n\$emailAddress['address_temp'] : {$emailAddress['address_temp']}\n\$emailAddress['quote_temp']: {$emailAddress['quote_temp']}\n"); } elseif ($emailAddress['ip'] && $emailAddress['domain']) { // Error - this should never occur $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = "Confusion during parsing"; $this->log('error', "Email\\Parse->_addAddress - both an IP address '{$emailAddress['ip']}' and a domain '{$emailAddress['domain']}' found for the email address '{$emailAddress['original_address']}'\n"); } elseif ($emailAddress['ip'] || $emailAddress['domain'] && preg_match('/\\d+\\.\\d+\\.\\d+\\.\\d+/', $emailAddress['domain'])) { if ($emailAddress['domain']) { $emailAddress['ip'] = $emailAddress['domain']; $emailAddress['domain'] = null; } if (!$this->ipValidator) { $this->ipValidator = new Ip(); } try { if (!$this->ipValidator->isValid($emailAddress['ip'])) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'IP address invalid: \'' . $emailAddress['ip'] . '\' does not appear to be a valid IP address'; } elseif (preg_match('/192\\.168\\.\\d+\\.\\d+/', $emailAddress['ip']) || preg_match('/172\\.(1[6-9]|2[0-9]|3[0-2])\\.\\d+\\.\\d+/', $emailAddress['ip']) || preg_match('/10\\.\\d+\\.\\d+\\.\\d+/', $emailAddress['ip'])) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'IP address invalid (private): ' . $emailAddress['ip']; } elseif (preg_match('/169\\.254\\.\\d+\\.\\d+/', $emailAddress['ip'])) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'IP address invalid (APIPA): ' . $emailAddress['ip']; } } catch (Exception $e) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'IP address invalid: ' . $emailAddress['ip']; } } elseif ($emailAddress['domain']) { // Check for IDNA if (max(array_keys(count_chars($emailAddress['domain'], 1))) > 127) { $idna = new \Net_IDNA2(); try { $emailAddress['domain'] = $idna->encode($emailAddress['domain']); } catch (Exception $e) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = "Can't convert domain {$emailAddress['domain']} to punycode"; } } $result = $this->validateDomainName($emailAddress['domain']); if (!$result['valid']) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = isset($result['reason']) ? 'Domain invalid: ' . $result['reason'] : 'Domain invalid for some unknown reason'; } } } // Prepare some of the fields needed $emailAddress['name_parsed'] = rtrim($emailAddress['name_parsed']); $emailAddress['original_address'] = rtrim($emailAddress['original_address']); $name = $emailAddress['name_quoted'] ? "\"{$emailAddress['name_parsed']}\"" : $emailAddress['name_parsed']; $localPart = $emailAddress['local_part_quoted'] ? "\"{$emailAddress['local_part_parsed']}\"" : $emailAddress['local_part_parsed']; $domainPart = $emailAddress['ip'] ? '[' . $emailAddress['ip'] . ']' : $emailAddress['domain']; if (!$emailAddress['invalid']) { if (mb_strlen($domainPart, $encoding) == 0) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'Email address needs a domain after the \'@\''; } elseif (mb_strlen($localPart, $encoding) > 63) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'Email address before the \'@\' can not be greater than 63 characters'; } elseif (mb_strlen($localPart, $encoding) + mb_strlen($domainPart, $encoding) + 1 > 254) { $emailAddress['invalid'] = true; $emailAddress['invalid_reason'] = 'Email addresses can not be greater than 254 characters'; } } // Build the email address hash $emailAddrDef = array('address' => '', 'simple_address' => '', 'original_address' => rtrim($emailAddress['original_address']), 'name' => $name, 'name_parsed' => $emailAddress['name_parsed'], 'local_part' => $localPart, 'local_part_parsed' => $emailAddress['local_part_parsed'], 'domain_part' => $domainPart, 'domain' => $emailAddress['domain'], 'ip' => $emailAddress['ip'], 'invalid' => $emailAddress['invalid'], 'invalid_reason' => $emailAddress['invalid_reason']); // Build the proper address by hand (has comments stripped out and should have quotes in the proper places) if (!$emailAddrDef['invalid']) { $emailAddrDef['simple_address'] = "{$emailAddrDef['local_part']}@{$emailAddrDef['domain_part']}"; $properAddress = $emailAddrDef['name'] ? "{$emailAddrDef['name']} <{$emailAddrDef['local_part']}@{$emailAddrDef['domain_part']}>" : $emailAddrDef['simple_address']; $emailAddrDef['address'] = $properAddress; } $emailAddresses[] = $emailAddrDef; return $emailAddrDef['invalid']; }
function idn_to_ascii($domain) { static $idn, $loaded; if (!$loaded) { $idn = new Net_IDNA2(); $loaded = true; } if ($idn && $domain && preg_match('/[^\\x20-\\x7E]/', $domain)) { try { $domain = $idn->encode($domain); } catch (Exception $e) { } } return $domain; }
/** * Converts given punycode to the IDN. * @param string $value punycode to be converted. * @return string resulting IDN. * @since 1.1.13 */ private function decodeIDN($value) { if (preg_match_all('/^(.*):\\/\\/([^\\/]+)(.*)$/', $value, $matches)) { if (function_exists('idn_to_utf8')) { $value = $matches[1][0] . '://' . idn_to_utf8($matches[2][0]) . $matches[3][0]; } else { require_once Yii::getPathOfAlias('system.vendors.Net_IDNA2.Net') . DIRECTORY_SEPARATOR . 'IDNA2.php'; $idna = new Net_IDNA2(); $value = $matches[1][0] . '://' . @$idna->decode($matches[2][0]) . $matches[3][0]; } } return $value; }
/** * @param string $string * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @return bool|string */ public function validate($string, $config, $context) { $length = strlen($string); // empty hostname is OK; it's usually semantically equivalent: // the default host as defined by a URI scheme is used: // // If the URI scheme defines a default for host, then that // default applies when the host subcomponent is undefined // or when the registered name is empty (zero length). 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 . ']'; } // need to do checks on unusual encodings too $ipv4 = $this->ipv4->validate($string, $config, $context); if ($ipv4 !== false) { return $ipv4; } // A regular domain name. // This doesn't match I18N domain names, but we don't have proper IRI support, // so force users to insert Punycode. // There is not a good sense in which underscores should be // allowed, since it's technically not! (And if you go as // far to allow everything as specified by the DNS spec... // well, that's literally everything, modulo some space limits // for the components and the overall name (which, by the way, // we are NOT checking!). So we (arbitrarily) decide this: // let's allow underscores wherever we would have allowed // hyphens, if they are enabled. This is a pretty good match // for browser behavior, for example, a large number of browsers // cannot handle foo_.example.com, but foo_bar.example.com is // fairly well supported. $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : ''; // The productions describing this are: $a = '[a-z]'; // alpha $an = '[a-z0-9]'; // alphanum $and = "[a-z0-9-{$underscore}]"; // alphanum | "-" // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum $domainlabel = "{$an}({$and}*{$an})?"; // toplabel = alpha | alpha *( alphanum | "-" ) alphanum $toplabel = "{$a}({$and}*{$an})?"; // hostname = *( domainlabel "." ) toplabel [ "." ] if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } // If we have Net_IDNA2 support, we can support IRIs by // punycoding them. (This is the most portable thing to do, // since otherwise we have to assume browsers support if ($config->get('Core.EnableIDNA')) { $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true)); // we need to encode each period separately $parts = explode('.', $string); try { $new_parts = array(); foreach ($parts as $part) { $encodable = false; for ($i = 0, $c = strlen($part); $i < $c; $i++) { if (ord($part[$i]) > 0x7a) { $encodable = true; break; } } if (!$encodable) { $new_parts[] = $part; } else { $new_parts[] = $idna->encode($part); } } $string = implode('.', $new_parts); if (preg_match("/^({$domainlabel}\\.)*{$toplabel}\\.?\$/i", $string)) { return $string; } } catch (Exception $e) { // XXX error reporting } } return false; }
/** * Converts given punycode to the IDN. * * @param string $value punycode to be converted. * * @return string resulting IDN. * @since 1.1.13 */ private function decodeIDN($value) { if (preg_match_all('/^(.*):\\/\\/([^\\/]+)(.*)$/', $value, $matches)) { if (function_exists('idn_to_utf8')) { $value = $matches[1][0] . '://' . idn_to_utf8($matches[2][0]) . $matches[3][0]; } else { require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Net_IDNA2' . DIRECTORY_SEPARATOR . 'IDNA2.php'; $idna = new Net_IDNA2(); $value = $matches[1][0] . '://' . @$idna->decode($matches[2][0]) . $matches[3][0]; } } return $value; }