/** * Encode an internationalized domain name * * @param string $strDomain The domain name * * @return string The encoded domain name * * @deprecated Use Idna::encode() instead */ protected function idnaEncode($strDomain) { return \Idna::encode($strDomain); }
/** * Tries to parse a string and to get the domain name, tld and idn * converted domain name. * * If given string is not a domain name, it will add a default tld. * * Also skips given string if it is longer than 63 characters. * * @throws instance of AbstractException if throwExceptions = true * @param string $unparsedString * @param string $defaultTld * @return void */ public function parse($unparsedString, $defaultTld = 'com') { try { if ($this->loaded === false) { $this->load(); } $matchedDomain = ''; $matchedDomainIdn = ''; $matchedTld = ''; $matchedTldIdn = ''; $validHostname = true; $IdnaConverter = new Idna(array('idn_version' => 2008)); preg_match('/^((http|https|ftp|ftps|news|ssh|sftp|gopher):[\\/]{2,})?([^\\/]+)/', mb_strtolower(trim($unparsedString), $this->encoding), $matches); $parsedString = end($matches); foreach ($this->tldList['content'] as $tld) { if (preg_match('/\\.' . $tld . '$/', $parsedString, $trash)) { $matchedTld = $tld; $matchedTldIdn = $IdnaConverter->encode($tld); $matchedDomain = str_replace('.' . $matchedTld, '', $parsedString); $matchedDomain = rtrim($matchedDomain, '.'); $matchedDomain = ltrim($matchedDomain, '.'); if ($matchedTld != 'name' && strpos($matchedDomain, '.')) { $matchedDomain = str_replace('.', '', strrchr($matchedDomain, '.')); } if (strpos($matchedDomain, ' ')) { $matchedDomain = explode(' ', $matchedDomain); $matchedDomain = end($matchedDomain); } $matchedDomainIdn = $IdnaConverter->encode($matchedDomain); break; } if ($tld == $parsedString) { $matchedTld = $tld; $matchedTldIdn = $IdnaConverter->encode($tld); break; } } if ($matchedDomain == '' && strlen($matchedDomainIdn) <= 63 && $matchedTld == '') { $matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\\-\\.]/', function ($match) use(&$validHostname) { $validHostname = false; }, $IdnaConverter->encode($parsedString))); $matchedDomainIdn = $IdnaConverter->encode($matchedDomain); $matchedTld = $matchedTldIdn = $defaultTld; } elseif ($matchedDomain != '' && strlen($matchedDomainIdn) <= 63 && $matchedTld != '') { $matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\\-\\.]/', function ($match) use(&$validHostname) { $validHostname = false; }, $IdnaConverter->encode($matchedDomain))); $matchedDomainIdn = $IdnaConverter->encode($matchedDomain); } elseif ($matchedDomain == '' && $matchedTld != '') { $validHostname = false; } else { throw \DomainParser\AbstractException::factory('UnparsableString', 'Unparsable domain name.'); } $Result = new Result($matchedDomain, $matchedDomainIdn, $matchedTld, $matchedTldIdn, $validHostname); } catch (\DomainParser\AbstractException $e) { if ($this->throwExceptions) { throw $e; } $Result = new Result(); $Result->error = $e->getMessage(); } return $Result->get($this->format); }
/** * Catch list from server and parse them to array. * * It only uses the official ICANN domain names and adds private * domains and missing official third-levels by using an additional hash. * * The manual added list is not complete. * * @throws ConnectErrorException * @see Novutec\Additional.php $additional * @param boolean $existFile * @return void */ private function catchTlds($existFile) { $content = @file_get_contents($this->tldUrl); if ($content === false) { if (!$existFile) { throw \Novutec\DomainParser\AbstractException::factory('Connect', 'Could not catch file from server.'); } return; } $IdnaConverter = new Idna(array('idn_version' => 2008)); // only match official ICANN domain tlds if (preg_match('/\\/\\/ ===BEGIN ICANN DOMAINS===(.*)(?=\\/\\/ ===END ICANN DOMAINS===)/s', $content, $matches) !== 1) { throw \Novutec\DomainParser\AbstractException::factory('UnparsableString', 'Could not fetch ICANN Domains of Mozilla TLD File.'); } $tlds = array(); $list_str = $matches[1]; foreach (explode("\n", $list_str) as $line) { $line = trim($line); // skip empty or comment lines if ($line == '' || $line[0] == '/' || strpos($line, '!') !== false) { continue; } // reformat prefixed wildcards if ($line[0] == '*') { $line = substr($line, 2); } // convert to xn-- format $tld = $IdnaConverter->encode($line); // validate if toplevel domain $pos = strrpos($tld, '.'); if ($pos === false) { $match = $tld; } else { $match = substr($tld, $pos + 1); } if (!isset($tlds[$match])) { $tlds[$match] = array(); } $tlds[$match][] = $tld; } // load additional to add to list require_once 'Additional.php'; // merge list and sort tlds by length within its group $this->tldList['content'] = array_merge_recursive($tlds, $additional); foreach ($this->tldList['content'] as $tldGroup => $tld) { usort($tld, function ($a, $b) { return strlen($b) - strlen($a); }); $this->tldList['content'][$tldGroup] = $tld; } $this->tldList['timestamp'] = time(); }
public function test_encode() { foreach ($this->arrIdnaTests as $strDefaultDomain => $strEncodedDomain) { $this->assertEquals(Idna::encode($strDefaultDomain), $strEncodedDomain); } }