From da9523ed23c8758ae9aa159f5d7f776f867dcf44 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 8 Jan 2018 23:40:10 -0500 Subject: [PATCH 1/4] Add maxwidth column to oembed table - Match Caching key for OEmbed queries --- boot.php | 2 +- src/Content/OEmbed.php | 16 ++++++++++------ src/Database/DBStructure.php | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/boot.php b/boot.php index 48eaeb6f3b..b10ba7a63f 100644 --- a/boot.php +++ b/boot.php @@ -43,7 +43,7 @@ define('FRIENDICA_PLATFORM', 'Friendica'); define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_VERSION', '3.6-dev'); define('DFRN_PROTOCOL_VERSION', '2.23'); -define('DB_UPDATE_VERSION', 1239); +define('DB_UPDATE_VERSION', 1240); define('NEW_UPDATE_ROUTINE_VERSION', 1170); /** diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index 5a3f07b536..364648d071 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -47,8 +47,8 @@ class OEmbed * @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 embedable - * content exists + * @return bool|object Returns object with embed content or false if no embeddable + * content exists */ public static function fetchURL($embedurl, $no_rich_type = false) { @@ -57,7 +57,7 @@ class OEmbed $a = get_app(); - $condition = ['url' => normalise_link($embedurl)]; + $condition = ['url' => normalise_link($embedurl), 'maxwidth' => $a->videowidth]; $r = dba::selectFirst('oembed', ['content'], $condition); if (DBM::is_result($r)) { $txt = $r["content"]; @@ -105,8 +105,12 @@ class OEmbed } else { //save in cache $j = json_decode($txt); if ($j->type != "error") { - dba::insert('oembed', array('url' => normalise_link($embedurl), - 'content' => $txt, 'created' => datetime_convert()), true); + dba::insert('oembed', [ + 'url' => normalise_link($embedurl), + 'maxwidth' => $a->videowidth, + 'content' => $txt, + 'created' => datetime_convert() + ], true); } Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY); @@ -306,7 +310,7 @@ class OEmbed if (!x($str_allowed)) { return false; } - + $allowed = explode(',', $str_allowed); return allowed_domain($domain, $allowed); diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index b33f023450..11bf40aec4 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -1277,11 +1277,12 @@ class DBStructure { $database["oembed"] = array( "fields" => array( "url" => array("type" => "varbinary(255)", "not null" => "1", "primary" => "1"), + "maxwidth" => array("type" => "int(11)", "not null" => "1", "primary" => "1"), "content" => array("type" => "mediumtext"), "created" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE), ), "indexes" => array( - "PRIMARY" => array("url"), + "PRIMARY" => array("url", "maxwidth"), "created" => array("created"), ) ); From 569156422037859d13ae75cea3d4bd57651d8bb4 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 8 Jan 2018 23:52:26 -0500 Subject: [PATCH 2/4] Trust OEmbed HTML when it's available - Reworked bb_attachment() to trust OEmbed content when it's successfully pulled - Fix coding standard by renaming parameter and variables --- include/bbcode.php | 61 ++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index fed535db57..08bfab3ed7 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -46,16 +46,16 @@ function bb_map_location($match) { * Note: Can produce a [bookmark] tag in the returned string * * @brief Processes [attachment] tags - * @param string $Text + * @param string $return * @param bool|int $simplehtml * @param bool $tryoembed * @return string */ -function bb_attachment($Text, $simplehtml = false, $tryoembed = true) +function bb_attachment($return, $simplehtml = false, $tryoembed = true) { - $data = get_attachment_data($Text); + $data = get_attachment_data($return); if (!$data) { - return $Text; + return $return; } if (isset($data["title"])) { @@ -68,49 +68,46 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) $data["image"] = ""; } + $return = ''; if ($simplehtml == 7) { - $text = style_url_for_mastodon($data["url"]); + $return = style_url_for_mastodon($data["url"]); } elseif (($simplehtml != 4) && ($simplehtml != 0)) { - $text = sprintf('%s
', $data["url"], $data["title"]); + $return = sprintf('%s
', $data["url"], $data["title"]); } else { - if ($simplehtml != 4) { - $text = sprintf('', $data["type"]); - } - - $oembed = sprintf('[bookmark=%s]%s[/bookmark]', $data['url'], $data['title']); - if ($tryoembed) { - try { - $oembed = OEmbed::getHTML($data['url'], $data['title']); - } catch (Exception $e) { - // $oembed isn't modified + try { + if ($tryoembed) { + $return = OEmbed::getHTML($data['url'], $data['title']); + } else { + throw new Exception('OEmbed is disabled for this attachment.'); + } + } catch (Exception $e) { + if ($simplehtml != 4) { + $return = sprintf('', $data["type"]); } - } - if (stripos($oembed, "