]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Network.php
Merge pull request #4858 from annando/issue-4655
[friendica.git] / src / Util / Network.php
index 1f9184ae968f178dc22ff8029ac56d1776de0d4f..4a11f9259579e31272b57fdfbe9a1c6d363a8212 100644 (file)
@@ -15,8 +15,6 @@ use Friendica\Util\XML;
 use DOMDocument;
 use DomXPath;
 
-require_once 'library/slinky.php';
-
 class Network
 {
        /**
@@ -81,8 +79,20 @@ class Network
 
                $a = get_app();
 
+               $parts = parse_url($url);
+               $path_parts = explode('/', $parts['path']);
+               foreach ($path_parts as $part) {
+                       if (strlen($part) <> mb_strlen($part)) {
+                               $parts2[] = rawurlencode($part);
+                       } else {
+                               $parts2[] = $part;
+                       }
+               }
+               $parts['path'] =  implode('/', $parts2);
+               $url = self::unparseURL($parts);
+
                if (self::isUrlBlocked($url)) {
-                       logger('z_fetch_url: domain of ' . $url . ' is blocked', LOGGER_DATA);
+                       logger('domain of ' . $url . ' is blocked', LOGGER_DATA);
                        return $ret;
                }
 
@@ -187,7 +197,7 @@ class Network
                }
 
                if (curl_errno($ch) !== CURLE_OK) {
-                       logger('fetch_url error fetching ' . $url . ': ' . curl_error($ch), LOGGER_NORMAL);
+                       logger('error fetching ' . $url . ': ' . curl_error($ch), LOGGER_NORMAL);
                }
 
                $ret['errno'] = curl_errno($ch);
@@ -197,7 +207,7 @@ class Network
 
                $http_code = $curl_info['http_code'];
 
-               logger('fetch_url ' . $url . ': ' . $http_code . " " . $s, LOGGER_DATA);
+               logger($url . ': ' . $http_code . " " . $s, LOGGER_DATA);
                $header = '';
 
                // Pull out multiple headers, e.g. proxy and continuation headers
@@ -219,7 +229,7 @@ class Network
 
                        $newurl = $curl_info['redirect_url'];
 
-                       if (($new_location_info['path'] == '') && ( $new_location_info['host'] != '')) {
+                       if (($new_location_info['path'] == '') && ($new_location_info['host'] != '')) {
                                $newurl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
                        }
 
@@ -231,6 +241,11 @@ class Network
                        if (strpos($newurl, '/') === 0) {
                                $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
                        }
+                       $old_location_query = @parse_url($url, PHP_URL_QUERY);
+
+                       if ($old_location_query != '') {
+                               $newurl .= '?' . $old_location_query;
+                       }
 
                        if (filter_var($newurl, FILTER_VALIDATE_URL)) {
                                $redirects++;
@@ -250,8 +265,8 @@ class Network
                if (!$ret['success']) {
                        $ret['error'] = curl_error($ch);
                        $ret['debug'] = $curl_info;
-                       logger('z_fetch_url: error: '.$url.': '.$ret['return_code'].' - '.$ret['error'], LOGGER_DEBUG);
-                       logger('z_fetch_url: debug: '.print_r($curl_info, true), LOGGER_DATA);
+                       logger('error: '.$url.': '.$ret['return_code'].' - '.$ret['error'], LOGGER_DEBUG);
+                       logger('debug: '.print_r($curl_info, true), LOGGER_DATA);
                }
 
                $ret['body'] = substr($s, strlen($header));
@@ -372,6 +387,9 @@ class Network
 
                if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
                        $matches = [];
+                       $new_location_info = @parse_url($curl_info['redirect_url']);
+                       $old_location_info = @parse_url($curl_info['url']);
+       
                        preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
                        $newurl = trim(array_pop($matches));
 
@@ -428,7 +446,7 @@ class Network
                /// @TODO Really suppress function outcomes? Why not find them + debug them?
                $h = @parse_url($url);
 
-               if ((is_array($h)) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
+               if ((is_array($h)) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
                        return $url;
                }
 
@@ -453,7 +471,7 @@ class Network
 
                $h = substr($addr, strpos($addr, '@') + 1);
 
-               if (($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP) )) {
+               if (($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP) )) {
                        return true;
                }
                return false;
@@ -773,29 +791,6 @@ class Network
                return $url;
        }
 
-       public static function shortenUrl($url)
-       {
-               $slinky = new Slinky($url);
-               $yourls_url = Config::get('yourls', 'url1');
-               if ($yourls_url) {
-                       $yourls_username = Config::get('yourls', 'username1');
-                       $yourls_password = Config::get('yourls', 'password1');
-                       $yourls_ssl = Config::get('yourls', 'ssl1');
-                       $yourls = new Slinky_YourLS();
-                       $yourls->set('username', $yourls_username);
-                       $yourls->set('password', $yourls_password);
-                       $yourls->set('ssl', $yourls_ssl);
-                       $yourls->set('yourls-url', $yourls_url);
-                       $slinky->set_cascade([$yourls, new Slinky_Ur1ca(), new Slinky_TinyURL()]);
-               } else {
-                       // setup a cascade of shortening services
-                       // try to get a short link from these services
-                       // in the order ur1.ca, tinyurl
-                       $slinky->set_cascade([new Slinky_Ur1ca(), new Slinky_TinyURL()]);
-               }
-               return $slinky->short();
-       }
-
        /**
         * @brief Find the matching part between two url
         *