]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/OEmbed.php
Update function
[friendica.git] / src / Content / OEmbed.php
index 51c987755e1b6f2211d06cfda02ba3be0356e878..07c36685c3516f3afeb969e124d704bd8df37d02 100644 (file)
@@ -15,6 +15,7 @@ use dba;
 use DOMDocument;\r
 use DOMXPath;\r
 use DOMNode;\r
+use Exception;\r
 \r
 require_once 'include/dba.php';\r
 require_once 'mod/proxy.php';\r
@@ -160,8 +161,8 @@ class OEmbed
        public static function formatObject($j)\r
        {\r
                $embedurl = $j->embedurl;\r
-               $jhtml = self::iframe($j->embedurl, (isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null));\r
-               $ret = "<span class='oembed " . $j->type . "'>";\r
+               $jhtml = $j->html;\r
+               $ret = '<div class="oembed ' . $j->type . '">';\r
                switch ($j->type) {\r
                        case "video":\r
                                if (isset($j->thumbnail_url)) {\r
@@ -173,7 +174,7 @@ class OEmbed
                                        $th = 120;\r
                                        $tw = $th * $tr;\r
                                        $tpl = get_markup_template('oembed_video.tpl');\r
-                                       $ret.=replace_macros($tpl, array(\r
+                                       $ret .= replace_macros($tpl, array(\r
                                                '$baseurl' => System::baseUrl(),\r
                                                '$embedurl' => $embedurl,\r
                                                '$escapedhtml' => base64_encode($jhtml),\r
@@ -184,33 +185,32 @@ class OEmbed
                                } else {\r
                                        $ret = $jhtml;\r
                                }\r
-                               //$ret.="<br>";\r
                                break;\r
                        case "photo":\r
-                               $ret.= "<img width='" . $j->width . "' src='" . proxy_url($j->url) . "'>";\r
+                               $ret .= '<img width="' . $j->width . '" src="' . proxy_url($j->url) . '">';\r
                                break;\r
                        case "link":\r
                                break;\r
                        case "rich":\r
-                               // not so safe..\r
                                if (self::isAllowedURL($embedurl)) {\r
                                        $ret .= proxy_parse_html($jhtml);\r
                                }\r
                                break;\r
                }\r
 \r
+               $ret .= '</div>';\r
                // add link to source if not present in "rich" type\r
                if ($j->type != 'rich' || !strpos($j->html, $embedurl)) {\r
-                       $ret .= "<h4>";\r
+                       $ret .= '<h4>';\r
                        if (isset($j->title)) {\r
                                if (isset($j->provider_name)) {\r
                                        $ret .= $j->provider_name . ": ";\r
                                }\r
 \r
                                $embedlink = (isset($j->title)) ? $j->title : $embedurl;\r
-                               $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";\r
+                               $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';\r
                                if (isset($j->author_name)) {\r
-                                       $ret.=" (" . $j->author_name . ")";\r
+                                       $ret .= ' (' . $j->author_name . ')';\r
                                }\r
                        } elseif (isset($j->provider_name) || isset($j->author_name)) {\r
                                $embedlink = "";\r
@@ -229,16 +229,14 @@ class OEmbed
                                        $embedlink = $embedurl;\r
                                }\r
 \r
-                               $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";\r
+                               $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';\r
                        }\r
-                       //if (isset($j->author_name)) $ret.=" by ".$j->author_name;\r
-                       //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;\r
                        $ret .= "</h4>";\r
-               } else {\r
+               } elseif (!strpos($j->html, $embedurl)) {\r
                        // add <a> for html2bbcode conversion\r
-                       $ret .= "<a href='$embedurl' rel='oembed'>$embedurl</a>";\r
+                       $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';\r
                }\r
-               $ret.="</span>";\r
+\r
                $ret = str_replace("\n", "", $ret);\r
                return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));\r
        }\r
@@ -272,7 +270,7 @@ class OEmbed
                        $xpath = new DOMXPath($dom);\r
 \r
                        $xattr = self::buildXPath("class", "oembed");\r
-                       $entries = $xpath->query("//span[$xattr]");\r
+                       $entries = $xpath->query("//div[$xattr]");\r
 \r
                        $xattr = "@rel='oembed'"; //oe_build_xpath("rel","oembed");\r
                        foreach ($entries as $e) {\r
@@ -287,6 +285,55 @@ class OEmbed
                }\r
        }\r
 \r
+       /**\r
+        * Determines if rich content OEmbed is allowed for the provided URL\r
+        *\r
+        * @brief Determines if rich content OEmbed is allowed for the provided URL\r
+        * @param string $url\r
+        * @return boolean\r
+        */\r
+       public static function isAllowedURL($url)\r
+       {\r
+               if (!Config::get('system', 'no_oembed_rich_content')) {\r
+                       return true;\r
+               }\r
+\r
+               $domain = parse_url($url, PHP_URL_HOST);\r
+               if (!x($domain)) {\r
+                       return false;\r
+               }\r
+\r
+               $str_allowed = Config::get('system', 'allowed_oembed', '');\r
+               if (!x($str_allowed)) {\r
+                       return false;\r
+               }\r
+               \r
+               $allowed = explode(',', $str_allowed);\r
+\r
+               return allowed_domain($domain, $allowed);\r
+       }\r
+\r
+       public static function getHTML($url, $title = null)\r
+       {\r
+               // Always embed the SSL version\r
+               $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),\r
+                                       array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);\r
+\r
+               $o = OEmbed::fetchURL($url);\r
+\r
+               if (!is_object($o) || $o->type == 'error') {\r
+                       throw new Exception('OEmbed failed for URL: ' . $url);\r
+               }\r
+\r
+               if (x($title)) {\r
+                       $o->title = $title;\r
+               }\r
+\r
+               $html = OEmbed::formatObject($o);\r
+\r
+               return $html;\r
+       }\r
+\r
        /**\r
         * @brief Generates the iframe HTML for an oembed attachment.\r
         *\r
@@ -299,6 +346,8 @@ class OEmbed
         * Since the iframe is automatically resized on load, there are no need for ugly\r
         * and impractical scrollbars.\r
         *\r
+        * @todo This function is currently unused until someoneā„¢ adds support for a separate OEmbed domain\r
+        *\r
         * @param string $src Original remote URL to embed\r
         * @param string $width\r
         * @param string $height\r
@@ -315,11 +364,8 @@ class OEmbed
                }\r
                $width = '100%';\r
 \r
-               // Only proxy OEmbed URLs to avoid mixed-content errors\r
-               if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL && parse_url($src, PHP_URL_SCHEME) !== 'https') {\r
-                       $src = System::baseUrl() . '/oembed/' . base64url_encode($src);\r
-               }\r
-               return '<iframe onload="resizeIframe(this);" class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $s . '" allowfullscreen scrolling="no" frameborder="no">' . t('Embedded content') . '</iframe>';\r
+               $src = System::baseUrl() . '/oembed/' . base64url_encode($src);\r
+               return '<iframe onload="resizeIframe(this);" class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $src . '" allowfullscreen scrolling="no" frameborder="no">' . t('Embedded content') . '</iframe>';\r
        }\r
 \r
        /**\r
@@ -356,24 +402,4 @@ class OEmbed
                return $innerHTML;\r
        }\r
 \r
-       /**\r
-        * Determines if rich content OEmbed is allowed for the provided URL\r
-        *\r
-        * @brief Determines if rich content OEmbed is allowed for the provided URL\r
-        * @param string $url\r
-        * @return boolean\r
-        */\r
-       private static function isAllowedURL($url)\r
-       {\r
-               if (!Config::get('system', 'no_oembed_rich_content')) {\r
-                       return true;\r
-               }\r
-\r
-               $domain = parse_url($url, PHP_URL_HOST);\r
-\r
-               $str_allowed = Config::get('system', 'allowed_oembed', '');\r
-               $allowed = explode(',', $str_allowed);\r
-\r
-               return allowed_domain($domain, $allowed, true);\r
-       }\r
 }\r