4 * @file src/Content/OEmbed.php
\r
7 namespace Friendica\Content;
\r
9 use Friendica\Core\Cache;
\r
10 use Friendica\Core\System;
\r
11 use Friendica\Core\Config;
\r
12 use Friendica\Database\DBM;
\r
13 use Friendica\Util\ParseUrl;
\r
20 require_once 'include/dba.php';
\r
21 require_once 'mod/proxy.php';
\r
24 * Handles all OEmbed content fetching and replacement
\r
26 * OEmbed is a standard used to allow an embedded representation of a URL on
\r
29 * @see https://oembed.com
\r
31 * @author Hypolite Petovan <mrpetovan@gmail.com>
\r
35 public static function replaceCallback($matches)
\r
37 $embedurl = $matches[1];
\r
38 $j = self::fetchURL($embedurl);
\r
39 $s = self::formatObject($j);
\r
45 * @brief Get data from an URL to embed its content.
\r
47 * @param string $embedurl The URL from which the data should be fetched.
\r
48 * @param bool $no_rich_type If set to true rich type content won't be fetched.
\r
50 * @return bool|object Returns object with embed content or false if no embedable
\r
53 public static function fetchURL($embedurl, $no_rich_type = false)
\r
55 $embedurl = trim($embedurl, "'");
\r
56 $embedurl = trim($embedurl, '"');
\r
60 $condition = array('url' => normalise_link($embedurl));
\r
61 $r = dba::select('oembed', array('content'), $condition, array('limit' => 1));
\r
63 if (DBM::is_result($r)) {
\r
64 $txt = $r["content"];
\r
66 $txt = Cache::get($a->videowidth . $embedurl);
\r
68 // These media files should now be caught in bbcode.php
\r
69 // left here as a fallback in case this is called from another source
\r
71 $noexts = array("mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm");
\r
72 $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION);
\r
75 if (is_null($txt)) {
\r
78 if (!in_array($ext, $noexts)) {
\r
79 // try oembed autodiscovery
\r
81 $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");
\r
83 $dom = @DOMDocument::loadHTML($html_text);
\r
85 $xpath = new DOMXPath($dom);
\r
86 $entries = $xpath->query("//link[@type='application/json+oembed']");
\r
87 foreach ($entries as $e) {
\r
88 $href = $e->getAttributeNode("href")->nodeValue;
\r
89 $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
\r
92 $entries = $xpath->query("//link[@type='text/json+oembed']");
\r
93 foreach ($entries as $e) {
\r
94 $href = $e->getAttributeNode("href")->nodeValue;
\r
95 $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
\r
104 if (!$txt || $txt[0] != "{") {
\r
105 $txt = '{"type":"error"}';
\r
106 } else { //save in cache
\r
107 $j = json_decode($txt);
\r
108 if ($j->type != "error") {
\r
109 dba::insert('oembed', array('url' => normalise_link($embedurl),
\r
110 'content' => $txt, 'created' => datetime_convert()), true);
\r
113 Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY);
\r
117 $j = json_decode($txt);
\r
119 if (!is_object($j)) {
\r
123 // Always embed the SSL version
\r
124 if (isset($j->html)) {
\r
125 $j->html = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"), array("https://www.youtube.com/", "https://player.vimeo.com/"), $j->html);
\r
128 $j->embedurl = $embedurl;
\r
130 // If fetching information doesn't work, then improve via internal functions
\r
131 if (($j->type == "error") || ($no_rich_type && ($j->type == "rich"))) {
\r
132 $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
\r
133 $j->type = $data["type"];
\r
135 if ($j->type == "photo") {
\r
136 $j->url = $data["url"];
\r
137 //$j->width = $data["images"][0]["width"];
\r
138 //$j->height = $data["images"][0]["height"];
\r
141 if (isset($data["title"])) {
\r
142 $j->title = $data["title"];
\r
145 if (isset($data["text"])) {
\r
146 $j->description = $data["text"];
\r
149 if (is_array($data["images"])) {
\r
150 $j->thumbnail_url = $data["images"][0]["src"];
\r
151 $j->thumbnail_width = $data["images"][0]["width"];
\r
152 $j->thumbnail_height = $data["images"][0]["height"];
\r
156 call_hooks('oembed_fetch_url', $embedurl, $j);
\r
161 public static function formatObject($j)
\r
163 $embedurl = $j->embedurl;
\r
165 $ret = '<div class="oembed ' . $j->type . '">';
\r
166 switch ($j->type) {
\r
168 if (isset($j->thumbnail_url)) {
\r
169 $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width : 200;
\r
170 $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height : 180;
\r
171 // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
\r
172 $tr = (($th) ? $tw / $th : 1);
\r
176 $tpl = get_markup_template('oembed_video.tpl');
\r
177 $ret .= replace_macros($tpl, array(
\r
178 '$baseurl' => System::baseUrl(),
\r
179 '$embedurl' => $embedurl,
\r
180 '$escapedhtml' => base64_encode($jhtml),
\r
183 '$turl' => $j->thumbnail_url,
\r
190 $ret .= '<img width="' . $j->width . '" src="' . proxy_url($j->url) . '">';
\r
195 if (self::isAllowedURL($embedurl)) {
\r
196 $ret .= proxy_parse_html($jhtml);
\r
202 // add link to source if not present in "rich" type
\r
203 if ($j->type != 'rich' || !strpos($j->html, $embedurl)) {
\r
205 if (isset($j->title)) {
\r
206 if (isset($j->provider_name)) {
\r
207 $ret .= $j->provider_name . ": ";
\r
210 $embedlink = (isset($j->title)) ? $j->title : $embedurl;
\r
211 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';
\r
212 if (isset($j->author_name)) {
\r
213 $ret .= ' (' . $j->author_name . ')';
\r
215 } elseif (isset($j->provider_name) || isset($j->author_name)) {
\r
217 if (isset($j->provider_name)) {
\r
218 $embedlink .= $j->provider_name;
\r
221 if (isset($j->author_name)) {
\r
222 if ($embedlink != "") {
\r
223 $embedlink .= ": ";
\r
226 $embedlink .= $j->author_name;
\r
228 if (trim($embedlink) == "") {
\r
229 $embedlink = $embedurl;
\r
232 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';
\r
235 } elseif (!strpos($j->html, $embedurl)) {
\r
236 // add <a> for html2bbcode conversion
\r
237 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';
\r
240 $ret = str_replace("\n", "", $ret);
\r
241 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
\r
244 public static function BBCode2HTML($text)
\r
246 $stopoembed = Config::get("system", "no_oembed");
\r
247 if ($stopoembed == true) {
\r
248 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>" . t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
\r
250 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", ['self', 'replaceCallback'], $text);
\r
254 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
\r
255 * and replace it with [embed]url[/embed]
\r
257 public static function HTML2BBCode($text)
\r
259 // start parser only if 'oembed' is in text
\r
260 if (strpos($text, "oembed")) {
\r
262 // convert non ascii chars to html entities
\r
263 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
\r
265 // If it doesn't parse at all, just return the text.
\r
266 $dom = @DOMDocument::loadHTML($html_text);
\r
270 $xpath = new DOMXPath($dom);
\r
272 $xattr = self::buildXPath("class", "oembed");
\r
273 $entries = $xpath->query("//div[$xattr]");
\r
275 $xattr = "@rel='oembed'"; //oe_build_xpath("rel","oembed");
\r
276 foreach ($entries as $e) {
\r
277 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
\r
278 if (!is_null($href)) {
\r
279 $e->parentNode->replaceChild(new DOMText("[embed]" . $href . "[/embed]"), $e);
\r
282 return self::getInnerHTML($dom->getElementsByTagName("body")->item(0));
\r
289 * Determines if rich content OEmbed is allowed for the provided URL
\r
291 * @brief Determines if rich content OEmbed is allowed for the provided URL
\r
292 * @param string $url
\r
295 public static function isAllowedURL($url)
\r
297 if (!Config::get('system', 'no_oembed_rich_content')) {
\r
301 $domain = parse_url($url, PHP_URL_HOST);
\r
306 $str_allowed = Config::get('system', 'allowed_oembed', '');
\r
307 if (!x($str_allowed)) {
\r
311 $allowed = explode(',', $str_allowed);
\r
313 return allowed_domain($domain, $allowed);
\r
316 public static function getHTML($url, $title = null)
\r
318 // Always embed the SSL version
\r
319 $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
\r
320 array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
\r
322 $o = OEmbed::fetchURL($url);
\r
324 if (!is_object($o) || $o->type == 'error') {
\r
325 throw new Exception('OEmbed failed for URL: ' . $url);
\r
329 $o->title = $title;
\r
332 $html = OEmbed::formatObject($o);
\r
338 * @brief Generates the iframe HTML for an oembed attachment.
\r
340 * Width and height are given by the remote, and are regularly too small for
\r
341 * the generated iframe.
\r
343 * The width is entirely discarded for the actual width of the post, while fixed
\r
344 * height is used as a starting point before the inevitable resizing.
\r
346 * Since the iframe is automatically resized on load, there are no need for ugly
\r
347 * and impractical scrollbars.
\r
349 * @todo This function is currently unused until someoneā¢ adds support for a separate OEmbed domain
\r
351 * @param string $src Original remote URL to embed
\r
352 * @param string $width
\r
353 * @param string $height
\r
354 * @return string formatted HTML
\r
356 * @see oembed_format_object()
\r
358 private static function iframe($src, $width, $height)
\r
362 if (!$height || strstr($height, '%')) {
\r
367 $src = System::baseUrl() . '/oembed/' . base64url_encode($src);
\r
368 return '<iframe onload="resizeIframe(this);" class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $src . '" allowfullscreen scrolling="no" frameborder="no">' . t('Embedded content') . '</iframe>';
\r
372 * Generates an XPath query to select elements whose provided attribute contains
\r
373 * the provided value in a space-separated list.
\r
375 * @brief Generates attribute search XPath string
\r
377 * @param string $attr Name of the attribute to seach
\r
378 * @param string $value Value to search in a space-separated list
\r
381 private static function buildXPath($attr, $value)
\r
383 // https://www.westhoffswelt.de/blog/2009/6/9/select-html-elements-with-more-than-one-css-class-using-xpath
\r
384 return "contains(normalize-space(@$attr), ' $value ') or substring(normalize-space(@$attr), 1, string-length('$value') + 1) = '$value ' or substring(normalize-space(@$attr), string-length(@$attr) - string-length('$value')) = ' $value' or @$attr = '$value'";
\r
388 * Returns the inner XML string of a provided DOMNode
\r
390 * @brief Returns the inner XML string of a provided DOMNode
\r
392 * @param DOMNode $node
\r
395 private static function getInnerHTML(DOMNode $node)
\r
398 $children = $node->childNodes;
\r
399 foreach ($children as $child) {
\r
400 $innerHTML .= $child->ownerDocument->saveXML($child);
\r