]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ParseUrl.php
Honor item delivery data legacy fields
[friendica.git] / src / Util / ParseUrl.php
index 2c134542a0392e279dd6f64f4008ec7cf8e52c74..70c5279cc84a42ce9314a202375cb2f1382c6fd5 100644 (file)
@@ -9,10 +9,10 @@ use DOMDocument;
 use DOMXPath;
 use Friendica\Content\OEmbed;
 use Friendica\Core\Addon;
+use Friendica\Core\Logger;
 use Friendica\Database\DBA;
 use Friendica\Object\Image;
-
-require_once 'include/dba.php';
+use Friendica\Util\Strings;
 
 /**
  * @brief Class with methods for extracting certain content from an url
@@ -48,7 +48,7 @@ class ParseUrl
                }
 
                $parsed_url = DBA::selectFirst('parsed_url', ['content'],
-                       ['url' => normalise_link($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed]
+                       ['url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing, 'oembed' => $do_oembed]
                );
                if (!empty($parsed_url['content'])) {
                        $data = unserialize($parsed_url['content']);
@@ -60,7 +60,7 @@ class ParseUrl
                DBA::insert(
                        'parsed_url',
                        [
-                               'url' => normalise_link($url), 'guessing' => !$no_guessing,
+                               'url' => Strings::normaliseLink($url), 'guessing' => !$no_guessing,
                                'oembed' => $do_oembed, 'content' => serialize($data),
                                'created' => DateTimeFormat::utcNow()
                        ],
@@ -111,7 +111,7 @@ class ParseUrl
         */
        public static function getSiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1)
        {
-               $a = get_app();
+               $a = \get_app();
 
                $siteinfo = [];
 
@@ -123,7 +123,7 @@ class ParseUrl
                }
 
                if ($count > 10) {
-                       logger('Endless loop detected for ' . $url, LOGGER_DEBUG);
+                       Logger::log('Endless loop detected for ' . $url, Logger::DEBUG);
                        return $siteinfo;
                }
 
@@ -135,23 +135,23 @@ class ParseUrl
                $siteinfo['url'] = $url;
                $siteinfo['type'] = 'link';
 
-               $data = Network::curl($url);
-               if (!$data['success']) {
+               $curlResult = Network::curl($url);
+               if (!$curlResult->isSuccess()) {
                        return $siteinfo;
                }
 
                // If the file is too large then exit
-               if ($data['info']['download_content_length'] > 1000000) {
+               if (defaults($curlResult->getInfo(), 'download_content_length', 0) > 1000000) {
                        return $siteinfo;
                }
 
                // If it isn't a HTML file then exit
-               if (($data['info']['content_type'] != '') && !strstr(strtolower($data['info']['content_type']), 'html')) {
+               if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
                        return $siteinfo;
                }
 
-               $header = $data['header'];
-               $body = $data['body'];
+               $header = $curlResult->getHeader();
+               $body = $curlResult->getBody();
 
                if ($do_oembed) {
                        $oembed_data = OEmbed::fetchURL($url);
@@ -161,7 +161,8 @@ class ParseUrl
                                        $siteinfo['type'] = $oembed_data->type;
                                }
 
-                               if (($oembed_data->type == 'link') && ($siteinfo['type'] != 'photo')) {
+                               // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178
+                               if ($siteinfo['type'] != 'photo') {
                                        if (isset($oembed_data->title)) {
                                                $siteinfo['title'] = trim($oembed_data->title);
                                        }
@@ -181,12 +182,11 @@ class ParseUrl
                        $charset = trim(trim(trim(array_pop($matches)), ';,'));
                }
 
-               if ($charset == '') {
-                       $charset = 'utf-8';
-               }
+               if ($charset && strtoupper($charset) != 'UTF-8') {
+                       // See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211
+                       $charset = str_ireplace('latin-1', 'latin1', $charset);
 
-               if (($charset != '') && (strtoupper($charset) != 'UTF-8')) {
-                       logger('detected charset ' . $charset, LOGGER_DEBUG);
+                       Logger::log('detected charset ' . $charset, Logger::DEBUG);
                        $body = iconv($charset, 'UTF-8//TRANSLIT', $body);
                }
 
@@ -271,11 +271,9 @@ class ParseUrl
                                        $siteinfo['image'] = $meta_tag['content'];
                                        break;
                                case 'twitter:card':
-                                       // Obsolete card type
-                                       if ($meta_tag['content'] == 'photo') {
-                                               $siteinfo['type'] = 'summary_large_image';
-                                       } else {
-                                               $siteinfo['type'] = $meta_tag['content'];
+                                       // Detect photo pages
+                                       if ($meta_tag['content'] == 'summary_large_image') {
+                                               $siteinfo['type'] = 'photo';
                                        }
                                        break;
                                case 'twitter:description':
@@ -299,10 +297,6 @@ class ParseUrl
                        }
                }
 
-               if ($siteinfo['type'] == 'summary' || $siteinfo['type'] == 'summary_large_image') {
-                       $siteinfo['type'] = 'link';
-               }
-
                if (isset($keywords)) {
                        $siteinfo['keywords'] = [];
                        foreach ($keywords as $keyword) {
@@ -338,7 +332,12 @@ class ParseUrl
                        }
                }
 
-               if ((@$siteinfo['image'] == '') && !$no_guessing) {
+               // Prevent to have a photo type without an image
+               if ((empty($siteinfo['image']) || !empty($siteinfo['text'])) && ($siteinfo['type'] == 'photo')) {
+                       $siteinfo['type'] = 'link';
+               }
+
+               if (empty($siteinfo['image']) && !$no_guessing) {
                        $list = $xpath->query('//img[@src]');
                        foreach ($list as $node) {
                                $img_tag = [];
@@ -421,7 +420,7 @@ class ParseUrl
                        }
                }
 
-               logger('Siteinfo for ' . $url . ' ' . print_r($siteinfo, true), LOGGER_DEBUG);
+               Logger::log('Siteinfo for ' . $url . ' ' . print_r($siteinfo, true), Logger::DEBUG);
 
                Addon::callHooks('getsiteinfo', $siteinfo);
 
@@ -487,21 +486,23 @@ class ParseUrl
 
                $complete = $schemearr["scheme"]."://".$schemearr["host"];
 
-               if (@$schemearr["port"] != "") {
+               if (!empty($schemearr["port"])) {
                        $complete .= ":".$schemearr["port"];
                }
 
-               if (strpos($urlarr["path"], "/") !== 0) {
-                       $complete .= "/";
-               }
+               if (!empty($urlarr["path"])) {
+                       if (strpos($urlarr["path"], "/") !== 0) {
+                               $complete .= "/";
+                       }
 
-               $complete .= $urlarr["path"];
+                       $complete .= $urlarr["path"];
+               }
 
-               if (@$urlarr["query"] != "") {
+               if (!empty($urlarr["query"])) {
                        $complete .= "?".$urlarr["query"];
                }
 
-               if (@$urlarr["fragment"] != "") {
+               if (!empty($urlarr["fragment"])) {
                        $complete .= "#".$urlarr["fragment"];
                }