4 * @file src/Content/OEmbed.php
6 namespace Friendica\Content;
13 use Friendica\Core\Cache;
14 use Friendica\Core\Config;
15 use Friendica\Core\Hook;
16 use Friendica\Core\L10n;
17 use Friendica\Core\Renderer;
18 use Friendica\Core\System;
19 use Friendica\Database\DBA;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Network;
22 use Friendica\Util\ParseUrl;
23 use Friendica\Util\Proxy as ProxyUtils;
24 use Friendica\Util\Strings;
27 * Handles all OEmbed content fetching and replacement
29 * OEmbed is a standard used to allow an embedded representation of a URL on
32 * @see https://oembed.com
34 * @author Hypolite Petovan <hypolite@mrpetovan.com>
38 public static function replaceCallback($matches)
40 $embedurl = $matches[1];
41 $j = self::fetchURL($embedurl, !self::isAllowedURL($embedurl));
42 $s = self::formatObject($j);
48 * @brief Get data from an URL to embed its content.
50 * @param string $embedurl The URL from which the data should be fetched.
51 * @param bool $no_rich_type If set to true rich type content won't be fetched.
53 * @return \Friendica\Object\OEmbed
54 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56 public static function fetchURL($embedurl, $no_rich_type = false)
58 $embedurl = trim($embedurl, '\'"');
62 $cache_key = 'oembed:' . $a->videowidth . ':' . $embedurl;
64 $condition = ['url' => Strings::normaliseLink($embedurl), 'maxwidth' => $a->videowidth];
65 $oembed_record = DBA::selectFirst('oembed', ['content'], $condition);
66 if (DBA::isResult($oembed_record)) {
67 $json_string = $oembed_record['content'];
69 $json_string = Cache::get($cache_key);
72 // These media files should now be caught in bbcode.php
73 // left here as a fallback in case this is called from another source
74 $noexts = ['mp3', 'mp4', 'ogg', 'ogv', 'oga', 'ogm', 'webm'];
75 $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION);
77 $oembed = new \Friendica\Object\OEmbed($embedurl);
80 $oembed->parseJSON($json_string);
84 if (!in_array($ext, $noexts)) {
85 // try oembed autodiscovery
87 $html_text = Network::fetchUrl($embedurl, false, $redirects, 15, 'text/*');
89 $dom = new DOMDocument();
90 $dom->loadHTML($html_text);
92 $xpath = new DOMXPath($dom);
93 $entries = $xpath->query("//link[@type='application/json+oembed']");
94 foreach ($entries as $e) {
95 $href = $e->getAttributeNode('href')->nodeValue;
96 $json_string = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
100 $entries = $xpath->query("//link[@type='text/json+oembed']");
101 foreach ($entries as $e) {
102 $href = $e->getAttributeNode('href')->nodeValue;
103 $json_string = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
110 $json_string = trim($json_string);
112 if (!$json_string || $json_string[0] != '{') {
113 $json_string = '{"type":"error"}';
116 $oembed->parseJSON($json_string);
118 if (!empty($oembed->type) && $oembed->type != 'error') {
119 DBA::insert('oembed', [
120 'url' => Strings::normaliseLink($embedurl),
121 'maxwidth' => $a->videowidth,
122 'content' => $json_string,
123 'created' => DateTimeFormat::utcNow()
125 $cache_ttl = Cache::DAY;
127 $cache_ttl = Cache::FIVE_MINUTES;
130 Cache::set($cache_key, $json_string, $cache_ttl);
133 if ($oembed->type == 'error') {
137 // Always embed the SSL version
138 $oembed->html = str_replace(['http://www.youtube.com/', 'http://player.vimeo.com/'], ['https://www.youtube.com/', 'https://player.vimeo.com/'], $oembed->html);
140 // If fetching information doesn't work, then improve via internal functions
141 if ($no_rich_type && ($oembed->type == 'rich')) {
142 $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
143 $oembed->type = $data['type'];
145 if ($oembed->type == 'photo') {
146 $oembed->url = $data['url'];
149 if (isset($data['title'])) {
150 $oembed->title = $data['title'];
153 if (isset($data['text'])) {
154 $oembed->description = $data['text'];
157 if (!empty($data['images'])) {
158 $oembed->thumbnail_url = $data['images'][0]['src'];
159 $oembed->thumbnail_width = $data['images'][0]['width'];
160 $oembed->thumbnail_height = $data['images'][0]['height'];
164 Hook::callAll('oembed_fetch_url', $embedurl, $oembed);
169 private static function formatObject(\Friendica\Object\OEmbed $oembed)
171 $ret = '<div class="oembed ' . $oembed->type . '">';
173 switch ($oembed->type) {
175 if ($oembed->thumbnail_url) {
176 $tw = (isset($oembed->thumbnail_width) && intval($oembed->thumbnail_width)) ? $oembed->thumbnail_width : 200;
177 $th = (isset($oembed->thumbnail_height) && intval($oembed->thumbnail_height)) ? $oembed->thumbnail_height : 180;
178 // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
179 $tr = (($th) ? $tw / $th : 1);
183 $tpl = Renderer::getMarkupTemplate('oembed_video.tpl');
184 $ret .= Renderer::replaceMacros($tpl, [
185 '$baseurl' => System::baseUrl(),
186 '$embedurl' => $oembed->embed_url,
187 '$escapedhtml' => base64_encode($oembed->html),
190 '$turl' => $oembed->thumbnail_url,
193 $ret = $oembed->html;
198 $ret .= '<img width="' . $oembed->width . '" src="' . ProxyUtils::proxifyUrl($oembed->url) . '">';
205 $ret .= ProxyUtils::proxifyHtml($oembed->html);
209 // add link to source if not present in "rich" type
210 if ($oembed->type != 'rich' || !strpos($oembed->html, $oembed->embed_url)) {
212 if (!empty($oembed->title)) {
213 if (!empty($oembed->provider_name)) {
214 $ret .= $oembed->provider_name . ": ";
217 $ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
218 if (!empty($oembed->author_name)) {
219 $ret .= ' (' . $oembed->author_name . ')';
221 } elseif (!empty($oembed->provider_name) || !empty($oembed->author_name)) {
223 if (!empty($oembed->provider_name)) {
224 $embedlink .= $oembed->provider_name;
227 if (!empty($oembed->author_name)) {
228 if ($embedlink != "") {
232 $embedlink .= $oembed->author_name;
234 if (trim($embedlink) == "") {
235 $embedlink = $oembed->embed_url;
238 $ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $embedlink . '</a>';
240 $ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->embed_url . '</a>';
243 } elseif (!strpos($oembed->html, $oembed->embed_url)) {
244 // add <a> for html2bbcode conversion
245 $ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
250 return str_replace("\n", "", $ret);
253 public static function BBCode2HTML($text)
255 $stopoembed = Config::get("system", "no_oembed");
256 if ($stopoembed == true) {
257 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>" . L10n::t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
259 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", ['self', 'replaceCallback'], $text);
263 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
264 * and replace it with [embed]url[/embed]
269 public static function HTML2BBCode($text)
271 // start parser only if 'oembed' is in text
272 if (strpos($text, "oembed")) {
274 // convert non ascii chars to html entities
275 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
277 // If it doesn't parse at all, just return the text.
278 $dom = new DOMDocument();
279 $dom->loadHTML($html_text);
283 $xpath = new DOMXPath($dom);
285 $xattr = self::buildXPath("class", "oembed");
286 $entries = $xpath->query("//div[$xattr]");
288 $xattr = "@rel='oembed'"; //oe_build_xpath("rel","oembed");
289 foreach ($entries as $e) {
290 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
291 if (!is_null($href)) {
292 $e->parentNode->replaceChild(new DOMText("[embed]" . $href . "[/embed]"), $e);
295 return self::getInnerHTML($dom->getElementsByTagName("body")->item(0));
302 * Determines if rich content OEmbed is allowed for the provided URL
304 * @brief Determines if rich content OEmbed is allowed for the provided URL
307 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
309 public static function isAllowedURL($url)
311 if (!Config::get('system', 'no_oembed_rich_content')) {
315 $domain = parse_url($url, PHP_URL_HOST);
316 if (empty($domain)) {
320 $str_allowed = Config::get('system', 'allowed_oembed', '');
321 if (empty($str_allowed)) {
325 $allowed = explode(',', $str_allowed);
327 return Network::isDomainAllowed($domain, $allowed);
330 public static function getHTML($url, $title = null)
332 // Always embed the SSL version
333 $url = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"],
334 ["https://www.youtube.com/", "https://player.vimeo.com/"], $url);
336 $o = self::fetchURL($url, !self::isAllowedURL($url));
338 if (!is_object($o) || property_exists($o, 'type') && $o->type == 'error') {
339 throw new Exception('OEmbed failed for URL: ' . $url);
342 if (!empty($title)) {
346 $html = self::formatObject($o);
352 * @brief Generates the iframe HTML for an oembed attachment.
354 * Width and height are given by the remote, and are regularly too small for
355 * the generated iframe.
357 * The width is entirely discarded for the actual width of the post, while fixed
358 * height is used as a starting point before the inevitable resizing.
360 * Since the iframe is automatically resized on load, there are no need for ugly
361 * and impractical scrollbars.
363 * @todo This function is currently unused until someoneā¢ adds support for a separate OEmbed domain
365 * @param string $src Original remote URL to embed
366 * @param string $width
367 * @param string $height
368 * @return string formatted HTML
370 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
371 * @see oembed_format_object()
373 private static function iframe($src, $width, $height)
375 if (!$height || strstr($height, '%')) {
380 $src = System::baseUrl() . '/oembed/' . Strings::base64UrlEncode($src);
381 return '<iframe onload="resizeIframe(this);" class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $src . '" allowfullscreen scrolling="no" frameborder="no">' . L10n::t('Embedded content') . '</iframe>';
385 * Generates an XPath query to select elements whose provided attribute contains
386 * the provided value in a space-separated list.
388 * @brief Generates attribute search XPath string
390 * @param string $attr Name of the attribute to seach
391 * @param string $value Value to search in a space-separated list
394 private static function buildXPath($attr, $value)
396 // https://www.westhoffswelt.de/blog/2009/6/9/select-html-elements-with-more-than-one-css-class-using-xpath
397 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'";
401 * Returns the inner XML string of a provided DOMNode
403 * @brief Returns the inner XML string of a provided DOMNode
405 * @param DOMNode $node
408 private static function getInnerHTML(DOMNode $node)
411 $children = $node->childNodes;
412 foreach ($children as $child) {
413 $innerHTML .= $child->ownerDocument->saveXML($child);