X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=inline;f=src%2FContent%2FOEmbed.php;h=dd6ac7caa23109834e2959bb0035a680a755c9ce;hb=927e0ee0a25969e715113f2a57438116362f0316;hp=9f1df5174566bdca08e5be7493ad5d70fcb35c22;hpb=30c1cc0e8cec5438fd8fe36bd4ea00991dc01934;p=friendica.git diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index 9f1df51745..dd6ac7caa2 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -1,24 +1,27 @@ videowidth . ':' . $embedurl; + $condition = ['url' => normalise_link($embedurl), 'maxwidth' => $a->videowidth]; - $oembed = dba::selectFirst('oembed', ['content'], $condition); - if (DBM::is_result($oembed)) { - $txt = $oembed["content"]; + $oembed_record = DBA::selectFirst('oembed', ['content'], $condition); + if (DBA::isResult($oembed_record)) { + $json_string = $oembed_record['content']; } else { - $txt = Cache::get($a->videowidth . $embedurl); + $json_string = Cache::get($cache_key); } + // These media files should now be caught in bbcode.php // left here as a fallback in case this is called from another source - - $noexts = ["mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm"]; + $noexts = ['mp3', 'mp4', 'ogg', 'ogv', 'oga', 'ogm', 'webm']; $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION); + $oembed = new \Friendica\Object\OEmbed($embedurl); - if (is_null($txt)) { - $txt = ""; + if ($json_string) { + $oembed->parseJSON($json_string); + } else { + $json_string = ''; if (!in_array($ext, $noexts)) { // try oembed autodiscovery $redirects = 0; - $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*"); + $html_text = Network::fetchUrl($embedurl, false, $redirects, 15, 'text/*'); if ($html_text) { $dom = @DOMDocument::loadHTML($html_text); if ($dom) { $xpath = new DOMXPath($dom); $entries = $xpath->query("//link[@type='application/json+oembed']"); foreach ($entries as $e) { - $href = $e->getAttributeNode("href")->nodeValue; - $txt = fetch_url($href . '&maxwidth=' . $a->videowidth); + $href = $e->getAttributeNode('href')->nodeValue; + $json_string = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth); break; } + $entries = $xpath->query("//link[@type='text/json+oembed']"); foreach ($entries as $e) { - $href = $e->getAttributeNode("href")->nodeValue; - $txt = fetch_url($href . '&maxwidth=' . $a->videowidth); + $href = $e->getAttributeNode('href')->nodeValue; + $json_string = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth); break; } } } } - $txt = trim($txt); - - if (!$txt || $txt[0] != "{") { - $txt = '{"type":"error"}'; - } else { //save in cache - $j = json_decode($txt); - if ($j->type != "error") { - dba::insert('oembed', [ - 'url' => normalise_link($embedurl), - 'maxwidth' => $a->videowidth, - 'content' => $txt, - 'created' => datetime_convert() - ], true); - } + $json_string = trim($json_string); - Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY); + if (!$json_string || $json_string[0] != '{') { + $json_string = '{"type":"error"}'; } - } - $j = json_decode($txt); + $oembed->parseJSON($json_string); - if (!is_object($j)) { - return false; + if (!empty($oembed->type) && $oembed->type != 'error') { + DBA::insert('oembed', [ + 'url' => normalise_link($embedurl), + 'maxwidth' => $a->videowidth, + 'content' => $json_string, + 'created' => DateTimeFormat::utcNow() + ], true); + $cache_ttl = CACHE_DAY; + } else { + $cache_ttl = CACHE_FIVE_MINUTES; + } + + Cache::set($cache_key, $json_string, $cache_ttl); } - // Always embed the SSL version - if (isset($j->html)) { - $j->html = str_replace(["http://www.youtube.com/", "http://player.vimeo.com/"], ["https://www.youtube.com/", "https://player.vimeo.com/"], $j->html); + if ($oembed->type == 'error') { + return $oembed; } - $j->embedurl = $embedurl; + // Always embed the SSL version + $oembed->html = str_replace(['http://www.youtube.com/', 'http://player.vimeo.com/'], ['https://www.youtube.com/', 'https://player.vimeo.com/'], $oembed->html); // If fetching information doesn't work, then improve via internal functions - if ($no_rich_type && ($j->type == "rich")) { + if ($no_rich_type && ($oembed->type == 'rich')) { $data = ParseUrl::getSiteinfoCached($embedurl, true, false); - $j->type = $data["type"]; + $oembed->type = $data['type']; - if ($j->type == "photo") { - $j->url = $data["url"]; + if ($oembed->type == 'photo') { + $oembed->url = $data['url']; } - if (isset($data["title"])) { - $j->title = $data["title"]; + if (isset($data['title'])) { + $oembed->title = $data['title']; } - if (isset($data["text"])) { - $j->description = $data["text"]; + if (isset($data['text'])) { + $oembed->description = $data['text']; } - if (is_array($data["images"])) { - $j->thumbnail_url = $data["images"][0]["src"]; - $j->thumbnail_width = $data["images"][0]["width"]; - $j->thumbnail_height = $data["images"][0]["height"]; + if (!empty($data['images'])) { + $oembed->thumbnail_url = $data['images'][0]['src']; + $oembed->thumbnail_width = $data['images'][0]['width']; + $oembed->thumbnail_height = $data['images'][0]['height']; } } - Addon::callHooks('oembed_fetch_url', $embedurl, $j); + Addon::callHooks('oembed_fetch_url', $embedurl, $oembed); - return $j; + return $oembed; } - private static function formatObject($j) + private static function formatObject(\Friendica\Object\OEmbed $oembed) { - $embedurl = $j->embedurl; - $jhtml = $j->html; - $ret = '