]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Network.php
Merge pull request #11515 from annando/issue-11492
[friendica.git] / src / Util / Network.php
index 492d0ecac7293c604f5a908298ddd5b15835fa4f..af4d18b4ab037301ea3bc5692519cb8334977dae 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\Core\Logger;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Network\HTTPException\NotModifiedException;
+use GuzzleHttp\Psr7\Uri;
 
 class Network
 {
@@ -436,7 +437,7 @@ class Network
         * @param array $parsed URL parts
         *
         * @return string The glued URL.
-        * @deprecated since version 2021.12, use a UriInterface object like GuzzleHttp\Psr7\Uri instead
+        * @deprecated since version 2021.12, use GuzzleHttp\Psr7\Uri::fromParts($parts) instead
         */
        public static function unparseURL(array $parsed)
        {
@@ -462,6 +463,29 @@ class Network
                        (strlen($fragment) ? "#".$fragment : '');
        }
 
+       /**
+        * Convert an URI to an IDN compatible URI
+        *
+        * @param string $uri
+        * @return string
+        */
+       public static function convertToIdn(string $uri): string
+       {
+               $parts = parse_url($uri);
+               if (!empty($parts['scheme']) && !empty($parts['host'])) {
+                       $parts['host'] = idn_to_ascii($parts['host']);
+                       $uri = Uri::fromParts($parts);
+               } else {
+                       $parts = explode('@', $uri);
+                       if (count($parts) == 2) {
+                               $uri = $parts[0] . '@' . idn_to_ascii($parts[1]);
+                       } else {
+                               $uri = idn_to_ascii($uri);
+                       }
+               }
+
+               return $uri;
+       }
 
        /**
         * Switch the scheme of an url between http and https
@@ -569,6 +593,7 @@ class Network
         */
        public static function isValidHttpUrl(string $url)
        {
-               return in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https']) && parse_url($url, PHP_URL_HOST);
+               $scheme = parse_url($url, PHP_URL_SCHEME);
+               return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
        }
 }