]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/OEmbed.php
Merge pull request #4243 from MrPetovan/task/switch-to-array-new-style
[friendica.git] / src / Content / OEmbed.php
index ff025634577b67e0477797367ef8ba4fe0ddd2cd..a8780d4d51e225349cede4a654a495c8f2f00c54 100644 (file)
@@ -35,7 +35,7 @@ class OEmbed
        public static function replaceCallback($matches)
        {
                $embedurl = $matches[1];
-               $j = self::fetchURL($embedurl);
+               $j = self::fetchURL($embedurl, !self::isAllowedURL($embedurl));
                $s = self::formatObject($j);
 
                return $s;
@@ -67,7 +67,7 @@ class OEmbed
                // These media files should now be caught in bbcode.php
                // left here as a fallback in case this is called from another source
 
-               $noexts = array("mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm");
+               $noexts = ["mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm"];
                $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION);
 
 
@@ -125,20 +125,18 @@ class OEmbed
 
                // Always embed the SSL version
                if (isset($j->html)) {
-                       $j->html = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"), array("https://www.youtube.com/", "https://player.vimeo.com/"), $j->html);
+                       $j->html = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"], ["https://www.youtube.com/", "https://player.vimeo.com/"], $j->html);
                }
 
                $j->embedurl = $embedurl;
 
                // If fetching information doesn't work, then improve via internal functions
-               if (($j->type == "error") || ($no_rich_type && ($j->type == "rich"))) {
+               if ($no_rich_type && ($j->type == "rich")) {
                        $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
                        $j->type = $data["type"];
 
                        if ($j->type == "photo") {
                                $j->url = $data["url"];
-                               //$j->width = $data["images"][0]["width"];
-                               //$j->height = $data["images"][0]["height"];
                        }
 
                        if (isset($data["title"])) {
@@ -161,7 +159,7 @@ class OEmbed
                return $j;
        }
 
-       public static function formatObject($j)
+       private static function formatObject($j)
        {
                $embedurl = $j->embedurl;
                $jhtml = $j->html;
@@ -177,14 +175,14 @@ class OEmbed
                                        $th = 120;
                                        $tw = $th * $tr;
                                        $tpl = get_markup_template('oembed_video.tpl');
-                                       $ret .= replace_macros($tpl, array(
+                                       $ret .= replace_macros($tpl, [
                                                '$baseurl' => System::baseUrl(),
                                                '$embedurl' => $embedurl,
                                                '$escapedhtml' => base64_encode($jhtml),
                                                '$tw' => $tw,
                                                '$th' => $th,
                                                '$turl' => $j->thumbnail_url,
-                                       ));
+                                       ]);
                                } else {
                                        $ret = $jhtml;
                                }
@@ -195,33 +193,29 @@ class OEmbed
                        case "link":
                                break;
                        case "rich":
-                               if (self::isAllowedURL($embedurl)) {
-                                       $ret .= proxy_parse_html($jhtml);
-                               }
+                               $ret .= proxy_parse_html($jhtml);
                                break;
                }
 
-               $ret .= '</div>';
                // add link to source if not present in "rich" type
                if ($j->type != 'rich' || !strpos($j->html, $embedurl)) {
                        $ret .= '<h4>';
-                       if (isset($j->title)) {
-                               if (isset($j->provider_name)) {
+                       if (!empty($j->title)) {
+                               if (!empty($j->provider_name)) {
                                        $ret .= $j->provider_name . ": ";
                                }
 
-                               $embedlink = (isset($j->title)) ? $j->title : $embedurl;
-                               $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';
-                               if (isset($j->author_name)) {
+                               $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';
+                               if (!empty($j->author_name)) {
                                        $ret .= ' (' . $j->author_name . ')';
                                }
-                       } elseif (isset($j->provider_name) || isset($j->author_name)) {
+                       } elseif (!empty($j->provider_name) || !empty($j->author_name)) {
                                $embedlink = "";
-                               if (isset($j->provider_name)) {
+                               if (!empty($j->provider_name)) {
                                        $embedlink .= $j->provider_name;
                                }
 
-                               if (isset($j->author_name)) {
+                               if (!empty($j->author_name)) {
                                        if ($embedlink != "") {
                                                $embedlink .= ": ";
                                        }
@@ -233,6 +227,8 @@ class OEmbed
                                }
 
                                $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';
+                       } else {
+                               $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedurl . '</a>';
                        }
                        $ret .= "</h4>";
                } elseif (!strpos($j->html, $embedurl)) {
@@ -240,6 +236,8 @@ class OEmbed
                        $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';
                }
 
+               $ret .= '</div>';
+
                $ret = str_replace("\n", "", $ret);
                return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
        }
@@ -319,10 +317,10 @@ class OEmbed
        public static function getHTML($url, $title = null)
        {
                // Always embed the SSL version
-               $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
-                                       array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
+               $url = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"],
+                                       ["https://www.youtube.com/", "https://player.vimeo.com/"], $url);
 
-               $o = self::fetchURL($url);
+               $o = self::fetchURL($url, !self::isAllowedURL($url));
 
                if (!is_object($o) || $o->type == 'error') {
                        throw new Exception('OEmbed failed for URL: ' . $url);