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