/** * 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_utf8($domain) { static $idn, $loaded; if (!$loaded) { $idn = new Net_IDNA2(); $loaded = true; } if ($idn && $domain && preg_match('/(^|\\.)xn--/i', $domain)) { try { $domain = $idn->decode($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; }
/** * 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; } }
/** * 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; }
/** * Decodes a String from IDNA format to UTF8 * * @param $input * @return string */ function decode_idna($input) { if (function_exists('idn_to_utf8')) { // return idn_to_utf8($input, IDNA_USE_STD3_RULES); return idn_to_utf8($input); } else { $IDNA = new Net_IDNA2(); $output = $IDNA->decode($input); return $output == false ? $input : $output; } }
/** * 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'] . (empty($components['port']) ? '' : ':' . $components['port']); if (self::isPunycoded($host)) { try { $idn = new \Net_IDNA2(); $host = $idn->decode($host); } catch (\InvalidArgumentException $e) { } } $path = !empty($components['path']) ? $components['path'] : ''; return $host . rtrim($path, '/'); } 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; }