]> git.mxchange.org Git - friendica.git/blob - src/Content/OEmbed.php
Update function
[friendica.git] / src / Content / OEmbed.php
1 <?php\r
2 \r
3 /**\r
4  * @file src/Content/OEmbed.php\r
5  */\r
6 \r
7 namespace Friendica\Content;\r
8 \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
14 use dba;\r
15 use DOMDocument;\r
16 use DOMXPath;\r
17 use DOMNode;\r
18 use Exception;\r
19 \r
20 require_once 'include/dba.php';\r
21 require_once 'mod/proxy.php';\r
22 \r
23 /**\r
24  * Handles all OEmbed content fetching and replacement\r
25  *\r
26  * OEmbed is a standard used to allow an embedded representation of a URL on\r
27  * third party sites\r
28  *\r
29  * @see https://oembed.com\r
30  *\r
31  * @author Hypolite Petovan <mrpetovan@gmail.com>\r
32  */\r
33 class OEmbed\r
34 {\r
35         public static function replaceCallback($matches)\r
36         {\r
37                 $embedurl = $matches[1];\r
38                 $j = self::fetchURL($embedurl);\r
39                 $s = self::formatObject($j);\r
40 \r
41                 return $s;\r
42         }\r
43 \r
44         /**\r
45          * @brief Get data from an URL to embed its content.\r
46          *\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
49          *\r
50          * @return bool|object Returns object with embed content or false if no embedable\r
51          *       content exists\r
52          */\r
53         public static function fetchURL($embedurl, $no_rich_type = false)\r
54         {\r
55                 $embedurl = trim($embedurl, "'");\r
56                 $embedurl = trim($embedurl, '"');\r
57 \r
58                 $a = get_app();\r
59 \r
60                 $condition = array('url' => normalise_link($embedurl));\r
61                 $r = dba::select('oembed', array('content'), $condition, array('limit' => 1));\r
62 \r
63                 if (DBM::is_result($r)) {\r
64                         $txt = $r["content"];\r
65                 } else {\r
66                         $txt = Cache::get($a->videowidth . $embedurl);\r
67                 }\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
70 \r
71                 $noexts = array("mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm");\r
72                 $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION);\r
73 \r
74 \r
75                 if (is_null($txt)) {\r
76                         $txt = "";\r
77 \r
78                         if (!in_array($ext, $noexts)) {\r
79                                 // try oembed autodiscovery\r
80                                 $redirects = 0;\r
81                                 $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");\r
82                                 if ($html_text) {\r
83                                         $dom = @DOMDocument::loadHTML($html_text);\r
84                                         if ($dom) {\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
90                                                         break;\r
91                                                 }\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
96                                                         break;\r
97                                                 }\r
98                                         }\r
99                                 }\r
100                         }\r
101 \r
102                         $txt = trim($txt);\r
103 \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
111                                 }\r
112 \r
113                                 Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY);\r
114                         }\r
115                 }\r
116 \r
117                 $j = json_decode($txt);\r
118 \r
119                 if (!is_object($j)) {\r
120                         return false;\r
121                 }\r
122 \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
126                 }\r
127 \r
128                 $j->embedurl = $embedurl;\r
129 \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
134 \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
139                         }\r
140 \r
141                         if (isset($data["title"])) {\r
142                                 $j->title = $data["title"];\r
143                         }\r
144 \r
145                         if (isset($data["text"])) {\r
146                                 $j->description = $data["text"];\r
147                         }\r
148 \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
153                         }\r
154                 }\r
155 \r
156                 call_hooks('oembed_fetch_url', $embedurl, $j);\r
157 \r
158                 return $j;\r
159         }\r
160 \r
161         public static function formatObject($j)\r
162         {\r
163                 $embedurl = $j->embedurl;\r
164                 $jhtml = $j->html;\r
165                 $ret = '<div class="oembed ' . $j->type . '">';\r
166                 switch ($j->type) {\r
167                         case "video":\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
173 \r
174                                         $th = 120;\r
175                                         $tw = $th * $tr;\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
181                                                 '$tw' => $tw,\r
182                                                 '$th' => $th,\r
183                                                 '$turl' => $j->thumbnail_url,\r
184                                         ));\r
185                                 } else {\r
186                                         $ret = $jhtml;\r
187                                 }\r
188                                 break;\r
189                         case "photo":\r
190                                 $ret .= '<img width="' . $j->width . '" src="' . proxy_url($j->url) . '">';\r
191                                 break;\r
192                         case "link":\r
193                                 break;\r
194                         case "rich":\r
195                                 if (self::isAllowedURL($embedurl)) {\r
196                                         $ret .= proxy_parse_html($jhtml);\r
197                                 }\r
198                                 break;\r
199                 }\r
200 \r
201                 $ret .= '</div>';\r
202                 // add link to source if not present in "rich" type\r
203                 if ($j->type != 'rich' || !strpos($j->html, $embedurl)) {\r
204                         $ret .= '<h4>';\r
205                         if (isset($j->title)) {\r
206                                 if (isset($j->provider_name)) {\r
207                                         $ret .= $j->provider_name . ": ";\r
208                                 }\r
209 \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
214                                 }\r
215                         } elseif (isset($j->provider_name) || isset($j->author_name)) {\r
216                                 $embedlink = "";\r
217                                 if (isset($j->provider_name)) {\r
218                                         $embedlink .= $j->provider_name;\r
219                                 }\r
220 \r
221                                 if (isset($j->author_name)) {\r
222                                         if ($embedlink != "") {\r
223                                                 $embedlink .= ": ";\r
224                                         }\r
225 \r
226                                         $embedlink .= $j->author_name;\r
227                                 }\r
228                                 if (trim($embedlink) == "") {\r
229                                         $embedlink = $embedurl;\r
230                                 }\r
231 \r
232                                 $ret .= '<a href="' . $embedurl . '" rel="oembed">' . $embedlink . '</a>';\r
233                         }\r
234                         $ret .= "</h4>";\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
238                 }\r
239 \r
240                 $ret = str_replace("\n", "", $ret);\r
241                 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));\r
242         }\r
243 \r
244         public static function BBCode2HTML($text)\r
245         {\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
249                 }\r
250                 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", ['self', 'replaceCallback'], $text);\r
251         }\r
252 \r
253         /**\r
254          * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>\r
255          * and replace it with [embed]url[/embed]\r
256          */\r
257         public static function HTML2BBCode($text)\r
258         {\r
259                 // start parser only if 'oembed' is in text\r
260                 if (strpos($text, "oembed")) {\r
261 \r
262                         // convert non ascii chars to html entities\r
263                         $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));\r
264 \r
265                         // If it doesn't parse at all, just return the text.\r
266                         $dom = @DOMDocument::loadHTML($html_text);\r
267                         if (!$dom) {\r
268                                 return $text;\r
269                         }\r
270                         $xpath = new DOMXPath($dom);\r
271 \r
272                         $xattr = self::buildXPath("class", "oembed");\r
273                         $entries = $xpath->query("//div[$xattr]");\r
274 \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
280                                 }\r
281                         }\r
282                         return self::getInnerHTML($dom->getElementsByTagName("body")->item(0));\r
283                 } else {\r
284                         return $text;\r
285                 }\r
286         }\r
287 \r
288         /**\r
289          * Determines if rich content OEmbed is allowed for the provided URL\r
290          *\r
291          * @brief Determines if rich content OEmbed is allowed for the provided URL\r
292          * @param string $url\r
293          * @return boolean\r
294          */\r
295         public static function isAllowedURL($url)\r
296         {\r
297                 if (!Config::get('system', 'no_oembed_rich_content')) {\r
298                         return true;\r
299                 }\r
300 \r
301                 $domain = parse_url($url, PHP_URL_HOST);\r
302                 if (!x($domain)) {\r
303                         return false;\r
304                 }\r
305 \r
306                 $str_allowed = Config::get('system', 'allowed_oembed', '');\r
307                 if (!x($str_allowed)) {\r
308                         return false;\r
309                 }\r
310                 \r
311                 $allowed = explode(',', $str_allowed);\r
312 \r
313                 return allowed_domain($domain, $allowed);\r
314         }\r
315 \r
316         public static function getHTML($url, $title = null)\r
317         {\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
321 \r
322                 $o = OEmbed::fetchURL($url);\r
323 \r
324                 if (!is_object($o) || $o->type == 'error') {\r
325                         throw new Exception('OEmbed failed for URL: ' . $url);\r
326                 }\r
327 \r
328                 if (x($title)) {\r
329                         $o->title = $title;\r
330                 }\r
331 \r
332                 $html = OEmbed::formatObject($o);\r
333 \r
334                 return $html;\r
335         }\r
336 \r
337         /**\r
338          * @brief Generates the iframe HTML for an oembed attachment.\r
339          *\r
340          * Width and height are given by the remote, and are regularly too small for\r
341          * the generated iframe.\r
342          *\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
345          *\r
346          * Since the iframe is automatically resized on load, there are no need for ugly\r
347          * and impractical scrollbars.\r
348          *\r
349          * @todo This function is currently unused until someoneā„¢ adds support for a separate OEmbed domain\r
350          *\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
355          *\r
356          * @see oembed_format_object()\r
357          */\r
358         private static function iframe($src, $width, $height)\r
359         {\r
360                 $a = get_app();\r
361 \r
362                 if (!$height || strstr($height, '%')) {\r
363                         $height = '200';\r
364                 }\r
365                 $width = '100%';\r
366 \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
369         }\r
370 \r
371         /**\r
372          * Generates an XPath query to select elements whose provided attribute contains\r
373          * the provided value in a space-separated list.\r
374          *\r
375          * @brief Generates attribute search XPath string\r
376          *\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
379          * @return string\r
380          */\r
381         private static function buildXPath($attr, $value)\r
382         {\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
385         }\r
386 \r
387         /**\r
388          * Returns the inner XML string of a provided DOMNode\r
389          *\r
390          * @brief Returns the inner XML string of a provided DOMNode\r
391          *\r
392          * @param DOMNode $node\r
393          * @return string\r
394          */\r
395         private static function getInnerHTML(DOMNode $node)\r
396         {\r
397                 $innerHTML = '';\r
398                 $children = $node->childNodes;\r
399                 foreach ($children as $child) {\r
400                         $innerHTML .= $child->ownerDocument->saveXML($child);\r
401                 }\r
402                 return $innerHTML;\r
403         }\r
404 \r
405 }\r