]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ParseUrl.php
Quickfix to avoid bad looking incoming tweets
[friendica.git] / src / Util / ParseUrl.php
index 1810293b71ac25268d0c75c319c0cd83200b35ac..9ce9b0e9f9510cac664df737d795a0990897c16d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -214,7 +214,7 @@ class ParseUrl
                }
 
                $curlResult = DI::httpRequest()->get($url);
-               if (!$curlResult->isSuccess()) {
+               if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return $siteinfo;
                }
 
@@ -349,6 +349,9 @@ class ParseUrl
                        $siteinfo['title'] = trim($list->item(0)->nodeValue);
                }
 
+               $twitter_card = false;
+               $twitter_image = false;
+
                $list = $xpath->query('//meta[@name]');
                foreach ($list as $node) {
                        $meta_tag = [];
@@ -376,6 +379,7 @@ class ParseUrl
                                        break;
                                case 'twitter:image':
                                        $siteinfo['image'] = $meta_tag['content'];
+                                       $twitter_image = true;
                                        break;
                                case 'twitter:image:src':
                                        $siteinfo['image'] = $meta_tag['content'];
@@ -383,7 +387,7 @@ class ParseUrl
                                case 'twitter:card':
                                        // Detect photo pages
                                        if ($meta_tag['content'] == 'summary_large_image') {
-                                               $siteinfo['type'] = 'photo';
+                                               $twitter_card = true;
                                        }
                                        break;
                                case 'twitter:description':
@@ -458,6 +462,7 @@ class ParseUrl
                                                break;
                                        case 'twitter:image':
                                                $siteinfo['image'] = $meta_tag['content'];
+                                               $twitter_image = true;
                                                break;
                                }
                        }
@@ -472,10 +477,11 @@ class ParseUrl
                        }
                }
 
+// Currently deactivated, see https://github.com/friendica/friendica/pull/10148#issuecomment-821512658         
                // Prevent to have a photo type without an image
-               if ((empty($siteinfo['image']) || !empty($siteinfo['text'])) && ($siteinfo['type'] == 'photo')) {
-                       $siteinfo['type'] = 'link';
-               }
+//             if ($twitter_card && $twitter_image && !empty($siteinfo['image'])) {
+//                     $siteinfo['type'] = 'photo';
+//             }
 
                if (!empty($siteinfo['image'])) {
                        $siteinfo['images'] = $siteinfo['images'] ?? [];
@@ -512,7 +518,6 @@ class ParseUrl
        {
                if (!empty($siteinfo['images'])) {
                        array_walk($siteinfo['images'], function (&$image) use ($page_url) {
-                               $image = [];
                                // According to the specifications someone could place a picture url into the content field as well.
                                // But this doesn't seem to happen in the wild, so we don't cover it here.
                                if (!empty($image['url'])) {
@@ -525,7 +530,11 @@ class ParseUrl
                                                $image['contenttype'] = $photodata['mime'];
                                                unset($image['url']);
                                                ksort($image);
+                                       } else {
+                                               $image = [];
                                        }
+                               } else {
+                                       $image = [];
                                }
                        });
 
@@ -711,7 +720,7 @@ class ParseUrl
 
                array_walk_recursive($siteinfo, function (&$element) {
                        if (is_string($element)) {
-                               $element = html_entity_decode($element, ENT_COMPAT, 'UTF-8');
+                               $element = trim(strip_tags(html_entity_decode($element, ENT_COMPAT, 'UTF-8')));
                        }
                });
 
@@ -942,7 +951,7 @@ class ParseUrl
                                        $siteinfo['keywords'][] = trim($keyword);
                                }
                        }
-               } else {
+               } elseif (!empty($jsonld['keywords'])) {
                        $content = JsonLD::fetchElementArray($jsonld, 'keywords');
                        if (!empty($content) && is_array($content)) {
                                $jsonldinfo['keywords'] = $content;