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