/** * Normalizes domain name and punycode's it * @param string $url URL * @return mixed string with normalized domain on success, boolean false otherwise */ public static function normalizeDomain($url) { $url = self::fix($url); if ($url) { $domain = parse_url($url, PHP_URL_HOST); if (strpos($domain, self::PUNYCODE_PREFIX) !== 0) { $idn = new \Net_IDNA2(); $domain = $idn->encode($domain); } return $domain; } return false; }
/** * @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; }
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 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; }
$request_params = array('ListType' => "COMPLETED", 'PageSize' => 100, 'SortBy' => "DOMAINNAME"); if (!empty($params['PromotionCode'])) { $request_params['PromotionCode'] = $params['PromotionCode']; } $api = new NamecheapRegistrarApi($username, $password, $testmode); $response = $api->request("namecheap.domains.transfer.getList", $request_params); $result = $api->parseResponse($response); $transfers = namecheap_parseResult($result['TransferGetListResult']['Transfer']); } catch (Exception $e) { $report .= $e->getMessage() . "\n"; } if (!$transfers) { $report .= "No registrar completed transfers found\n"; } else { while (($row = mysql_fetch_assoc($dbresult)) !== false) { $row['domain'] = $idna2->encode($row['domain']); $report .= "Domain " . $row['domain']; if (isset($transfers[$row['domain']]) && "COMPLETED" == $transfers[$row['domain']]['Status']) { $t = $transfers[$row['domain']]; $update = array('status' => "Active"); update_query("tbldomains", $update, array('id' => $row['id'])); $report .= " transfer status updated\n"; } else { $report .= " skipped\n"; } } } } /** * Active domains expiration and next due date dates synchronization */
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; }
/** * 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 encoding domain name in punycode * * @param string $url encode url to Punycode * @return string */ public function encodePunycode($url) { $parsedUrl = parse_url($url); if (!$this->_isPunycode($parsedUrl['host'])) { if (function_exists('idn_to_ascii')) { $host = idn_to_ascii($parsedUrl['host']); } else { $idn = new Net_IDNA2(); $host = $idn->encode($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; }
/** * Encodes to punycode * @param string $host ONLY the host name, e.g. "испытание.рф" * @return string */ function encode_host($host) { if (!preg_match("/[^\\w\\-\\.]/", $host)) { return $host; } require_once 'Net/IDNA2.php'; // netcat/require/lib $encoder = new Net_IDNA2(); try { $host = $encoder->encode(strtolower($host)); } catch (Net_IDNA2_Exception $e) { trigger_error("Cannot convert host name '{$host}' to punycode: {$e->getMessage()}", E_USER_WARNING); return $host; } return $host; }
public function __construct($sld, $tld) { $this->_sld = $sld; $this->_tld = $tld; if (in_array($tld, self::$_tldList)) { $this->_tldInList = true; $idna2 = new Net_IDNA2(); try { $this->_encodedSld = $idna2->encode($sld); } catch (Exception $e) { $this->_encodedSld = $sld; } if ($sld != $this->_encodedSld) { $this->_sldWasEncoded = true; } } else { $this->_encodedSld = $sld; } return $this; }
/** * 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']; }
/** * 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; }