]> git.mxchange.org Git - friendica.git/blob - src/Content/OEmbed.php
360a0dc4899439949d7addf0de0b99bcf088d469
[friendica.git] / src / Content / OEmbed.php
1 <?php
2
3 /**
4  * @file src/Content/OEmbed.php
5  */
6 namespace Friendica\Content;
7
8 use Friendica\Core\Addon;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Util\Network;
16 use Friendica\Util\ParseUrl;
17 use dba;
18 use DOMDocument;
19 use DOMNode;
20 use DOMText;
21 use DOMXPath;
22 use Exception;
23
24 require_once 'include/dba.php';
25 require_once 'mod/proxy.php';
26
27 /**
28  * Handles all OEmbed content fetching and replacement
29  *
30  * OEmbed is a standard used to allow an embedded representation of a URL on
31  * third party sites
32  *
33  * @see https://oembed.com
34  *
35  * @author Hypolite Petovan <mrpetovan@gmail.com>
36  */
37 class OEmbed
38 {
39         public static function replaceCallback($matches)
40         {
41                 $embedurl = $matches[1];
42                 $j = self::fetchURL($embedurl, !self::isAllowedURL($embedurl));
43                 $s = self::formatObject($j);
44
45                 return $s;
46         }
47
48         /**
49          * @brief Get data from an URL to embed its content.
50          *
51          * @param string $embedurl The URL from which the data should be fetched.
52          * @param bool $no_rich_type If set to true rich type content won't be fetched.
53          *
54          * @return bool|object Returns object with embed content or false if no embeddable
55          *                         content exists
56          */
57         public static function fetchURL($embedurl, $no_rich_type = false)
58         {
59                 $embedurl = trim($embedurl, "'");
60                 $embedurl = trim($embedurl, '"');
61
62                 $a = get_app();
63
64                 $condition = ['url' => normalise_link($embedurl), 'maxwidth' => $a->videowidth];
65                 $oembed = dba::selectFirst('oembed', ['content'], $condition);
66                 if (DBM::is_result($oembed)) {
67                         $txt = $oembed["content"];
68                 } else {
69                         $txt = Cache::get($a->videowidth . $embedurl);
70                 }
71                 // These media files should now be caught in bbcode.php
72                 // left here as a fallback in case this is called from another source
73
74                 $noexts = ["mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm"];
75                 $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION);
76
77
78                 if (is_null($txt)) {
79                         $txt = "";
80
81                         if (!in_array($ext, $noexts)) {
82                                 // try oembed autodiscovery
83                                 $redirects = 0;
84                                 $html_text = Network::fetchUrl($embedurl, false, $redirects, 15, "text/*");
85                                 if ($html_text) {
86                                         $dom = @DOMDocument::loadHTML($html_text);
87                                         if ($dom) {
88                                                 $xpath = new DOMXPath($dom);
89                                                 $entries = $xpath->query("//link[@type='application/json+oembed']");
90                                                 foreach ($entries as $e) {
91                                                         $href = $e->getAttributeNode("href")->nodeValue;
92                                                         $txt = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
93                                                         break;
94                                                 }
95                                                 $entries = $xpath->query("//link[@type='text/json+oembed']");
96                                                 foreach ($entries as $e) {
97                                                         $href = $e->getAttributeNode("href")->nodeValue;
98                                                         $txt = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
99                                                         break;
100                                                 }
101                                         }
102                                 }
103                         }
104
105                         $txt = trim($txt);
106
107                         if (!$txt || $txt[0] != "{") {
108                                 $txt = '{"type":"error"}';
109                         } else { //save in cache
110                                 $j = json_decode($txt);
111                                 if (!empty($j->type) && $j->type != "error") {
112                                         dba::insert('oembed', [
113                                                 'url' => normalise_link($embedurl),
114                                                 'maxwidth' => $a->videowidth,
115                                                 'content' => $txt,
116                                                 'created' => DateTimeFormat::utcNow()
117                                         ], true);
118                                 }
119
120                                 Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY);
121                         }
122                 }
123
124                 $j = json_decode($txt);
125
126                 if (!is_object($j)) {
127                         return false;
128                 }
129
130                 // Always embed the SSL version
131                 if (isset($j->html)) {
132                         $j->html = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"], ["https://www.youtube.com/", "https://player.vimeo.com/"], $j->html);
133                 }
134
135                 $j->embedurl = $embedurl;
136
137                 // If fetching information doesn't work, then improve via internal functions
138                 if ($no_rich_type && ($j->type == "rich")) {
139                         $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
140                         $j->type = $data["type"];
141
142                         if ($j->type == "photo") {
143                                 $j->url = $data["url"];
144                         }
145
146                         if (isset($data["title"])) {
147                                 $j->title = $data["title"];
148                         }
149
150                         if (isset($data["text"])) {
151                                 $j->description = $data["text"];
152                         }
153
154                         if (is_array($data["images"])) {
155                                 $j->thumbnail_url = $data["images"][0]["src"];
156                                 $j->thumbnail_width = $data["images"][0]["width"];
157                                 $j->thumbnail_height = $data["images"][0]["height"];
158                         }
159                 }
160
161                 Addon::callHooks('oembed_fetch_url', $embedurl, $j);
162
163                 return $j;
164         }
165
166         private static function formatObject($j)
167         {
168                 $embedurl = $j->embedurl;
169                 $jhtml = $j->html;
170                 $ret = '<div class="oembed ' . $j->type . '">';
171                 switch ($j->type) {
172                         case "video":
173                                 if (isset($j->thumbnail_url)) {
174                                         $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width : 200;
175                                         $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height : 180;
176                                         // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
177                                         $tr = (($th) ? $tw / $th : 1);
178
179                                         $th = 120;
180                                         $tw = $th * $tr;
181                                         $tpl = get_markup_template('oembed_video.tpl');
182                                         $ret .= replace_macros($tpl, [
183                                                 '$baseurl' => System::baseUrl(),
184                                                 '$embedurl' => $embedurl,
185                                                 '$escapedhtml' => base64_encode($jhtml),
186                                                 '$tw' => $tw,
187                                                 '$th' => $th,
188                                                 '$turl' => $j->thumbnail_url,
189                                         ]);
190                                 } else {
191                                         $ret = $jhtml;
192                                 }
193                                 break;
194                         case "photo":
195                                 $ret .= '<img width="' . $j->width . '" src="' . proxy_url($j->url) . '">';
196                                 break;
197                         case "link":
198                                 break;
199                         case "rich":
200                                 $ret .= proxy_parse_html($jhtml);
201                                 break;
202                 }
203
204                 // add link to source if not present in "rich" type
205                 if ($j->type != 'rich' || !strpos($j->html, $embedurl)) {
206                         $ret .= '<h4>';
207                         if (!empty($j->title)) {
208                                 if (!empty($j->provider_name)) {
209                                         $ret .= $j->provider_name . ": ";
210                                 }
211
212                                 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';
213                                 if (!empty($j->author_name)) {
214                                         $ret .= ' (' . $j->author_name . ')';
215                                 }
216                         } elseif (!empty($j->provider_name) || !empty($j->author_name)) {
217                                 $embedlink = "";
218                                 if (!empty($j->provider_name)) {
219                                         $embedlink .= $j->provider_name;
220                                 }
221
222                                 if (!empty($j->author_name)) {
223                                         if ($embedlink != "") {
224                                                 $embedlink .= ": ";
225                                         }
226
227                                         $embedlink .= $j->author_name;
228                                 }
229                                 if (trim($embedlink) == "") {
230                                         $embedlink = $embedurl;
231                                 }
232
233                                 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';
234                         } else {
235                                 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedurl . '</a>';
236                         }
237                         $ret .= "</h4>";
238                 } elseif (!strpos($j->html, $embedurl)) {
239                         // add <a> for html2bbcode conversion
240                         $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $j->title . '</a>';
241                 }
242
243                 $ret .= '</div>';
244
245                 $ret = str_replace("\n", "", $ret);
246                 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
247         }
248
249         public static function BBCode2HTML($text)
250         {
251                 $stopoembed = Config::get("system", "no_oembed");
252                 if ($stopoembed == true) {
253                         return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>" . L10n::t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
254                 }
255                 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", ['self', 'replaceCallback'], $text);
256         }
257
258         /**
259          * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
260          * and replace it with [embed]url[/embed]
261          */
262         public static function HTML2BBCode($text)
263         {
264                 // start parser only if 'oembed' is in text
265                 if (strpos($text, "oembed")) {
266
267                         // convert non ascii chars to html entities
268                         $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
269
270                         // If it doesn't parse at all, just return the text.
271                         $dom = @DOMDocument::loadHTML($html_text);
272                         if (!$dom) {
273                                 return $text;
274                         }
275                         $xpath = new DOMXPath($dom);
276
277                         $xattr = self::buildXPath("class", "oembed");
278                         $entries = $xpath->query("//div[$xattr]");
279
280                         $xattr = "@rel='oembed'"; //oe_build_xpath("rel","oembed");
281                         foreach ($entries as $e) {
282                                 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
283                                 if (!is_null($href)) {
284                                         $e->parentNode->replaceChild(new DOMText("[embed]" . $href . "[/embed]"), $e);
285                                 }
286                         }
287                         return self::getInnerHTML($dom->getElementsByTagName("body")->item(0));
288                 } else {
289                         return $text;
290                 }
291         }
292
293         /**
294          * Determines if rich content OEmbed is allowed for the provided URL
295          *
296          * @brief Determines if rich content OEmbed is allowed for the provided URL
297          * @param string $url
298          * @return boolean
299          */
300         public static function isAllowedURL($url)
301         {
302                 if (!Config::get('system', 'no_oembed_rich_content')) {
303                         return true;
304                 }
305
306                 $domain = parse_url($url, PHP_URL_HOST);
307                 if (!x($domain)) {
308                         return false;
309                 }
310
311                 $str_allowed = Config::get('system', 'allowed_oembed', '');
312                 if (!x($str_allowed)) {
313                         return false;
314                 }
315
316                 $allowed = explode(',', $str_allowed);
317
318                 return Network::isDomainAllowed($domain, $allowed);
319         }
320
321         public static function getHTML($url, $title = null)
322         {
323                 // Always embed the SSL version
324                 $url = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"],
325                                         ["https://www.youtube.com/", "https://player.vimeo.com/"], $url);
326
327                 $o = self::fetchURL($url, !self::isAllowedURL($url));
328
329                 if (!is_object($o) || $o->type == 'error') {
330                         throw new Exception('OEmbed failed for URL: ' . $url);
331                 }
332
333                 if (x($title)) {
334                         $o->title = $title;
335                 }
336
337                 $html = self::formatObject($o);
338
339                 return $html;
340         }
341
342         /**
343          * @brief Generates the iframe HTML for an oembed attachment.
344          *
345          * Width and height are given by the remote, and are regularly too small for
346          * the generated iframe.
347          *
348          * The width is entirely discarded for the actual width of the post, while fixed
349          * height is used as a starting point before the inevitable resizing.
350          *
351          * Since the iframe is automatically resized on load, there are no need for ugly
352          * and impractical scrollbars.
353          *
354          * @todo This function is currently unused until someoneā„¢ adds support for a separate OEmbed domain
355          *
356          * @param string $src Original remote URL to embed
357          * @param string $width
358          * @param string $height
359          * @return string formatted HTML
360          *
361          * @see oembed_format_object()
362          */
363         private static function iframe($src, $width, $height)
364         {
365                 $a = get_app();
366
367                 if (!$height || strstr($height, '%')) {
368                         $height = '200';
369                 }
370                 $width = '100%';
371
372                 $src = System::baseUrl() . '/oembed/' . base64url_encode($src);
373                 return '<iframe onload="resizeIframe(this);" class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $src . '" allowfullscreen scrolling="no" frameborder="no">' . L10n::t('Embedded content') . '</iframe>';
374         }
375
376         /**
377          * Generates an XPath query to select elements whose provided attribute contains
378          * the provided value in a space-separated list.
379          *
380          * @brief Generates attribute search XPath string
381          *
382          * @param string $attr Name of the attribute to seach
383          * @param string $value Value to search in a space-separated list
384          * @return string
385          */
386         private static function buildXPath($attr, $value)
387         {
388                 // https://www.westhoffswelt.de/blog/2009/6/9/select-html-elements-with-more-than-one-css-class-using-xpath
389                 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'";
390         }
391
392         /**
393          * Returns the inner XML string of a provided DOMNode
394          *
395          * @brief Returns the inner XML string of a provided DOMNode
396          *
397          * @param DOMNode $node
398          * @return string
399          */
400         private static function getInnerHTML(DOMNode $node)
401         {
402                 $innerHTML = '';
403                 $children = $node->childNodes;
404                 foreach ($children as $child) {
405                         $innerHTML .= $child->ownerDocument->saveXML($child);
406                 }
407                 return $innerHTML;
408         }
409
410 }