X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FOEmbed.php;h=dd6ac7caa23109834e2959bb0035a680a755c9ce;hb=4d39164c1ee6de2b1a09c2a1a8a4c89531d47ee5;hp=a8780d4d51e225349cede4a654a495c8f2f00c54;hpb=e36f2bb1fb3439e9993c7568e57140c4f954b772;p=friendica.git diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index a8780d4d51..dd6ac7caa2 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -3,22 +3,25 @@ /** * @file src/Content/OEmbed.php */ - namespace Friendica\Content; -use Friendica\Core\Cache; -use Friendica\Core\System; -use Friendica\Core\Config; -use Friendica\Database\DBM; -use Friendica\Util\ParseUrl; -use dba; use DOMDocument; -use DOMXPath; use DOMNode; +use DOMText; +use DOMXPath; use Exception; +use Friendica\Core\Addon; +use Friendica\Core\Cache; +use Friendica\Core\Config; +use Friendica\Core\L10n; +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Network; +use Friendica\Util\ParseUrl; +use Friendica\Util\Proxy as ProxyUtils; require_once 'include/dba.php'; -require_once 'mod/proxy.php'; /** * Handles all OEmbed content fetching and replacement @@ -44,131 +47,132 @@ class OEmbed /** * @brief Get data from an URL to embed its content. * - * @param string $embedurl The URL from which the data should be fetched. - * @param bool $no_rich_type If set to true rich type content won't be fetched. + * @param string $embedurl The URL from which the data should be fetched. + * @param bool $no_rich_type If set to true rich type content won't be fetched. * - * @return bool|object Returns object with embed content or false if no embeddable - * content exists + * @return \Friendica\Object\OEmbed */ public static function fetchURL($embedurl, $no_rich_type = false) { - $embedurl = trim($embedurl, "'"); - $embedurl = trim($embedurl, '"'); + $embedurl = trim($embedurl, '\'"'); $a = get_app(); + $cache_key = 'oembed:' . $a->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']; } } - call_hooks('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 = '
'; - switch ($j->type) { + $ret = '
'; + + switch ($oembed->type) { case "video": - if (isset($j->thumbnail_url)) { - $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width : 200; - $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height : 180; + if ($oembed->thumbnail_url) { + $tw = (isset($oembed->thumbnail_width) && intval($oembed->thumbnail_width)) ? $oembed->thumbnail_width : 200; + $th = (isset($oembed->thumbnail_height) && intval($oembed->thumbnail_height)) ? $oembed->thumbnail_height : 180; // make sure we don't attempt divide by zero, fallback is a 1:1 ratio $tr = (($th) ? $tw / $th : 1); @@ -177,63 +181,66 @@ class OEmbed $tpl = get_markup_template('oembed_video.tpl'); $ret .= replace_macros($tpl, [ '$baseurl' => System::baseUrl(), - '$embedurl' => $embedurl, - '$escapedhtml' => base64_encode($jhtml), + '$embedurl' => $oembed->embed_url, + '$escapedhtml' => base64_encode($oembed->html), '$tw' => $tw, '$th' => $th, - '$turl' => $j->thumbnail_url, + '$turl' => $oembed->thumbnail_url, ]); } else { - $ret = $jhtml; + $ret = $oembed->html; } break; + case "photo": - $ret .= ''; + $ret .= ''; break; + case "link": break; + case "rich": - $ret .= proxy_parse_html($jhtml); + $ret .= ProxyUtils::proxifyHtml($oembed->html); break; } // add link to source if not present in "rich" type - if ($j->type != 'rich' || !strpos($j->html, $embedurl)) { + if ($oembed->type != 'rich' || !strpos($oembed->html, $oembed->embed_url)) { $ret .= '

'; - if (!empty($j->title)) { - if (!empty($j->provider_name)) { - $ret .= $j->provider_name . ": "; + if (!empty($oembed->title)) { + if (!empty($oembed->provider_name)) { + $ret .= $oembed->provider_name . ": "; } - $ret .= '' . $j->title . ''; - if (!empty($j->author_name)) { - $ret .= ' (' . $j->author_name . ')'; + $ret .= '' . $oembed->title . ''; + if (!empty($oembed->author_name)) { + $ret .= ' (' . $oembed->author_name . ')'; } - } elseif (!empty($j->provider_name) || !empty($j->author_name)) { + } elseif (!empty($oembed->provider_name) || !empty($oembed->author_name)) { $embedlink = ""; - if (!empty($j->provider_name)) { - $embedlink .= $j->provider_name; + if (!empty($oembed->provider_name)) { + $embedlink .= $oembed->provider_name; } - if (!empty($j->author_name)) { + if (!empty($oembed->author_name)) { if ($embedlink != "") { $embedlink .= ": "; } - $embedlink .= $j->author_name; + $embedlink .= $oembed->author_name; } if (trim($embedlink) == "") { - $embedlink = $embedurl; + $embedlink = $oembed->embed_url; } - $ret .= '' . $embedlink . ''; + $ret .= '' . $embedlink . ''; } else { - $ret .= '' . $embedurl . ''; + $ret .= '' . $oembed->embed_url . ''; } $ret .= "

"; - } elseif (!strpos($j->html, $embedurl)) { + } elseif (!strpos($oembed->html, $oembed->embed_url)) { // add for html2bbcode conversion - $ret .= '' . $j->title . ''; + $ret .= '' . $oembed->title . ''; } $ret .= '
'; @@ -246,7 +253,7 @@ class OEmbed { $stopoembed = Config::get("system", "no_oembed"); if ($stopoembed == true) { - return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "" . t('Embedding disabled') . " : $1", $text); + return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "" . L10n::t('Embedding disabled') . " : $1", $text); } return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", ['self', 'replaceCallback'], $text); } @@ -311,7 +318,7 @@ class OEmbed $allowed = explode(',', $str_allowed); - return allowed_domain($domain, $allowed); + return Network::isDomainAllowed($domain, $allowed); } public static function getHTML($url, $title = null) @@ -322,7 +329,7 @@ class OEmbed $o = self::fetchURL($url, !self::isAllowedURL($url)); - if (!is_object($o) || $o->type == 'error') { + if (!is_object($o) || property_exists($o, 'type') && $o->type == 'error') { throw new Exception('OEmbed failed for URL: ' . $url); } @@ -366,7 +373,7 @@ class OEmbed $width = '100%'; $src = System::baseUrl() . '/oembed/' . base64url_encode($src); - return ''; + return ''; } /**