[OEmbed][Embed] Renamed OEmbed plugin to Embed

This commit is contained in:
Miguel Dantas 2019-07-06 04:31:02 +01:00 committed by Diogo Cordeiro
parent 1d41ff16d6
commit 52819d39d9
78 changed files with 103 additions and 99 deletions

View File

@ -349,7 +349,7 @@ $default =
'DirectMessage' => array(),
'EmailAuthentication' => array(),
'Event' => array(),
'Oembed' => array(),
'Embed' => array(),
'OpenID' => array(),
'OpportunisticQM' => array(),
'OStatus' => array(),

View File

@ -15,13 +15,14 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* OembedPlugin implementation for GNU social
* OEmbed and OpenGraph implementation for GNU social
*
* @package GNUsocial
* @author Stephen Paul Weber
* @author hannes
* @author Mikael Nordfeldth
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Miguel Dantas <biodantasgs@gmail.com>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@ -29,29 +30,31 @@
defined('GNUSOCIAL') || die();
/**
* Base class for the oEmbed plugin that does most of the heavy lifting to get
* Base class for the Embed plugin that does most of the heavy lifting to get
* and display representations for remote content.
*
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class OembedPlugin extends Plugin
class EmbedPlugin extends Plugin
{
const PLUGIN_VERSION = '2.0.1';
const PLUGIN_VERSION = '0.1.0';
// settings which can be set in config.php with addPlugin('Oembed', array('param'=>'value', ...));
// WARNING, these are _regexps_ (slashes added later). Always escape your dots and end your strings
public $domain_whitelist = array( // hostname => service provider
'^i\d*\.ytimg\.com$' => 'YouTube',
'^i\d*\.vimeocdn\.com$' => 'Vimeo',
);
// settings which can be set in config.php with addPlugin('Embed', array('param'=>'value', ...));
// WARNING, these are _regexps_ (slashes added later). Always escape your dots and end ('$') your strings
public $domain_whitelist = [
// hostname => service provider
'^i\d*\.ytimg\.com$' => 'YouTube',
'^i\d*\.vimeocdn\.com$' => 'Vimeo',
];
public $append_whitelist = array(); // fill this array as domain_whitelist to add more trusted sources
public $check_whitelist = false; // security/abuse precaution
protected $imgData = array();
/**
* Initialize the oEmbed plugin and set up the environment it needs for it.
* Initialize the Embed plugin and set up the environment it needs for it.
* Returns true if it initialized properly, the exception object if it
* doesn't.
*/
@ -77,19 +80,19 @@ class OembedPlugin extends Plugin
/**
* This code executes when GNU social creates the page routing, and we hook
* on this event to add our action handler for oEmbed.
* on this event to add our action handler for Embed.
*
* @param $m URLMapper the router that was initialized.
* @return bool true if successful, the exception object if it isn't.
*/
public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/oembed', ['action' => 'oembed']);
$m->connect('main/embed', ['action' => 'embed']);
}
/**
* This event executes when GNU social encounters a remote URL we then decide
* to interrogate for metadata. oEmbed gloms onto it to see if we have an
* to interrogate for metadata. Embed gloms onto it to see if we have an
* oEmbed endpoint or image to try to represent in the post.
*
* @param $url string the remote URL we're looking at
@ -218,7 +221,7 @@ class OembedPlugin extends Plugin
public function onEndShowStylesheets(Action $action)
{
$action->cssLink($this->path('css/oembed.css'));
$action->cssLink($this->path('css/embed.css'));
return true;
}
@ -233,9 +236,9 @@ class OembedPlugin extends Plugin
*/
public function onEndFileSaveNew(File $file)
{
$fo = File_oembed::getKV('file_id', $file->getID());
if ($fo instanceof File_oembed) {
common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file {$file->getID()}", __FILE__);
$fo = File_embed::getKV('file_id', $file->getID());
if ($fo instanceof File_embed) {
common_log(LOG_WARNING, "Strangely, a File_embed object exists for new file {$file->getID()}", __FILE__);
return true;
}
@ -243,51 +246,51 @@ class OembedPlugin extends Plugin
&& (('text/html' === substr($file->mimetype, 0, 9)
|| 'application/xhtml+xml' === substr($file->mimetype, 0, 21)))) {
try {
$oembed_data = File_oembed::_getOembed($file->url);
if ($oembed_data === false) {
throw new Exception('Did not get oEmbed data from URL');
$embed_data = File_embed::_getEmbed($file->url);
if ($embed_data === false) {
throw new Exception('Did not get embed data from URL');
}
$file->setTitle($oembed_data->title);
$file->setTitle($embed_data->title);
} catch (Exception $e) {
common_log(LOG_WARNING, sprintf(__METHOD__.': %s thrown when getting oEmbed data: %s', get_class($e), _ve($e->getMessage())));
common_log(LOG_WARNING, sprintf(__METHOD__.': %s thrown when getting embed data: %s', get_class($e), _ve($e->getMessage())));
return true;
}
File_oembed::saveNew($oembed_data, $file->getID());
File_embed::saveNew($embed_data, $file->getID());
}
return true;
}
public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
{
$oembed = File_oembed::getKV('file_id', $file->getID());
if (empty($oembed->author_name) && empty($oembed->provider)) {
$embed = File_embed::getKV('file_id', $file->getID());
if (empty($embed->author_name) && empty($embed->provider)) {
return true;
}
$out->elementStart('div', array('id'=>'oembed_info', 'class'=>'e-content'));
if (!empty($oembed->author_name)) {
if (!empty($embed->author_name)) {
$out->elementStart('div', 'fn vcard author');
if (empty($oembed->author_url)) {
$out->text($oembed->author_name);
if (empty($embed->author_url)) {
$out->text($embed->author_name);
} else {
$out->element(
'a',
array('href' => $oembed->author_url,
array('href' => $embed->author_url,
'class' => 'url'),
$oembed->author_name
$embed->author_name
);
}
}
if (!empty($oembed->provider)) {
if (!empty($embed->provider)) {
$out->elementStart('div', 'fn vcard');
if (empty($oembed->provider_url)) {
$out->text($oembed->provider);
if (empty($embed->provider_url)) {
$out->text($embed->provider);
} else {
$out->element(
'a',
array('href' => $oembed->provider_url,
array('href' => $embed->provider_url,
'class' => 'url'),
$oembed->provider
$embed->provider
);
}
}
@ -297,15 +300,15 @@ class OembedPlugin extends Plugin
public function onFileEnclosureMetadata(File $file, &$enclosure)
{
// Never treat generic HTML links as an enclosure type!
// But if we have oEmbed info, we'll consider it golden.
$oembed = File_oembed::getKV('file_id', $file->getID());
if (!$oembed instanceof File_oembed || !in_array($oembed->type, array('photo', 'video'))) {
// But if we have embed info, we'll consider it golden.
$embed = File_embed::getKV('file_id', $file->getID());
if (!$embed instanceof File_embed || !in_array($embed->type, array('photo', 'video'))) {
return true;
}
foreach (array('mimetype', 'url', 'title', 'modified', 'width', 'height') as $key) {
if (isset($oembed->{$key}) && !empty($oembed->{$key})) {
$enclosure->{$key} = $oembed->{$key};
if (isset($embed->{$key}) && !empty($embed->{$key})) {
$enclosure->{$key} = $embed->{$key};
}
}
return true;
@ -314,59 +317,59 @@ class OembedPlugin extends Plugin
public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file)
{
try {
$oembed = File_oembed::getByFile($file);
$embed = File_embed::getByFile($file);
} catch (NoResultException $e) {
return true;
}
// Show thumbnail as usual if it's a photo.
if ($oembed->type === 'photo') {
if ($embed->type === 'photo') {
return true;
}
$out->elementStart('article', ['class'=>'h-entry oembed']);
$out->elementStart('article', ['class'=>'h-entry embed']);
$out->elementStart('header');
try {
$thumb = $file->getThumbnail(128, 128);
$out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo oembed']));
$out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo embed']));
unset($thumb);
} catch (Exception $e) {
$out->element('div', ['class'=>'error'], $e->getMessage());
}
$out->elementStart('h5', ['class'=>'p-name oembed']);
$out->element('a', ['class'=>'u-url', 'href'=>$file->getUrl()], common_strip_html($oembed->title));
$out->elementStart('h5', ['class'=>'p-name embed']);
$out->element('a', ['class'=>'u-url', 'href'=>$file->getUrl()], common_strip_html($embed->title));
$out->elementEnd('h5');
$out->elementStart('div', ['class'=>'p-author oembed']);
if (!empty($oembed->author_name)) {
// TRANS: text before the author name of oEmbed attachment representation
$out->elementStart('div', ['class'=>'p-author embed']);
if (!empty($embed->author_name)) {
// TRANS: text before the author name of embed attachment representation
// FIXME: The whole "By x from y" should be i18n because of different language constructions.
$out->text(_('By '));
$attrs = ['class'=>'h-card p-author'];
if (!empty($oembed->author_url)) {
$attrs['href'] = $oembed->author_url;
if (!empty($embed->author_url)) {
$attrs['href'] = $embed->author_url;
$tag = 'a';
} else {
$tag = 'span';
}
$out->element($tag, $attrs, $oembed->author_name);
$out->element($tag, $attrs, $embed->author_name);
}
if (!empty($oembed->provider)) {
// TRANS: text between the oEmbed author name and provider url
if (!empty($embed->provider)) {
// TRANS: text between the embed author name and provider url
// FIXME: The whole "By x from y" should be i18n because of different language constructions.
$out->text(_(' from '));
$attrs = ['class'=>'h-card'];
if (!empty($oembed->provider_url)) {
$attrs['href'] = $oembed->provider_url;
if (!empty($embed->provider_url)) {
$attrs['href'] = $embed->provider_url;
$tag = 'a';
} else {
$tag = 'span';
}
$out->element($tag, $attrs, $oembed->provider);
$out->element($tag, $attrs, $embed->provider);
}
$out->elementEnd('div');
$out->elementEnd('header');
$out->elementStart('div', ['class'=>'p-summary oembed']);
$out->raw(common_purify($oembed->html));
$out->raw(common_purify($embed->html));
$out->elementEnd('div');
$out->elementStart('footer');
$out->elementEnd('footer');
@ -378,20 +381,21 @@ class OembedPlugin extends Plugin
public function onShowUnsupportedAttachmentRepresentation(HTMLOutputter $out, File $file)
{
try {
$oembed = File_oembed::getByFile($file);
$embed = File_embed::getByFile($file);
} catch (NoResultException $e) {
return true;
}
// the 'photo' type is shown through ordinary means, using StartShowAttachmentRepresentation!
switch ($oembed->type) {
switch ($embed->type) {
case 'video':
case 'link':
if (!empty($oembed->html)
if (!empty($embed->html)
&& (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
$purifier = new \HTMLPurifier();
require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();
// FIXME: do we allow <object> and <embed> here? we did that when we used htmLawed, but I'm not sure anymore...
$out->raw($purifier->purify($oembed->html));
$out->raw($purifier->purify($embed->html));
}
return false;
}
@ -401,7 +405,7 @@ class OembedPlugin extends Plugin
/**
* This event executes when GNU social is creating a file thumbnail entry in
* the database. We glom onto this to create proper information for oEmbed
* the database. We glom onto this to create proper information for Embed
* object thumbnails.
*
* @param $file File the file of the created thumbnail
@ -418,20 +422,20 @@ class OembedPlugin extends Plugin
return true;
}
// All our remote Oembed images lack a local filename property in the File object
// All our remote Embed images lack a local filename property in the File object
if (!is_null($file->filename)) {
common_debug(sprintf('Filename of file id==%d is not null (%s), so nothing oEmbed '.
common_debug(sprintf('Filename of file id==%d is not null (%s), so nothing Embed '.
'should handle.', $file->getID(), _ve($file->filename)));
return true;
}
try {
// If we have proper oEmbed data, there should be an entry in the File_oembed
// If we have proper Embed data, there should be an entry in the File_embed
// and File_thumbnail tables respectively. If not, we're not going to do anything.
$thumbnail = File_thumbnail::byFile($file);
} catch (NoResultException $e) {
// Not Oembed data, or at least nothing we either can or want to use.
common_debug('No oEmbed data found for file id=='.$file->getID());
// Not Embed data, or at least nothing we either can or want to use.
common_debug('No Embed data found for file id=='.$file->getID());
return true;
}
@ -440,7 +444,7 @@ class OembedPlugin extends Plugin
} catch (AlreadyFulfilledException $e) {
// aw yiss!
} catch (Exception $e) {
common_debug(sprintf('oEmbed encountered an exception (%s) for file id==%d: %s',
common_debug(sprintf('Embed encountered an exception (%s) for file id==%d: %s',
get_class($e), $file->getID(), _ve($e->getMessage())));
throw $e;
}
@ -512,11 +516,11 @@ class OembedPlugin extends Plugin
private function isRemoteImage($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
common_log(LOG_ERR, "Invalid URL in OEmbed::isRemoteImage()");
common_log(LOG_ERR, "Invalid URL in Embed::isRemoteImage()");
return false;
}
if ($url==null) {
common_log(LOG_ERR, "URL not specified in OEmbed::isRemoteImage()");
common_log(LOG_ERR, "URL not specified in Embed::isRemoteImage()");
return false;
}
try {
@ -585,7 +589,7 @@ class OembedPlugin extends Plugin
try {
// We'll trust sha256 (File::FILEHASH_ALG) not to have collision issues any time soon :)
$original_filename = bin2hex('oembed.' . $ext);
$original_filename = bin2hex('embed.' . $ext);
$filehash = hash(File::FILEHASH_ALG, $imgData);
$filename = "{$original_filename}-{$filehash}";
$fullpath = File_thumbnail::path($filename);
@ -594,7 +598,7 @@ class OembedPlugin extends Plugin
throw new ServerException(_('Could not write downloaded file to disk.'));
}
} catch (Exception $err) {
common_log(LOG_ERROR, "Went to write a thumbnail to disk in OembedPlugin::storeRemoteThumbnail " .
common_log(LOG_ERROR, "Went to write a thumbnail to disk in EmbedPlugin::storeRemoteThumbnail " .
"but encountered error: {$err}");
return $err;
} finally {
@ -611,7 +615,7 @@ class OembedPlugin extends Plugin
$thumbnail->updateWithKeys($orig);
} catch (exception $err) {
common_log(LOG_ERROR, "Went to write a thumbnail entry to the database in " .
"OembedPlugin::storeRemoteThumbnail but encountered error: ".$err);
"EmbedPlugin::storeRemoteThumbnail but encountered error: ".$err);
return $err;
}
return true;
@ -626,13 +630,13 @@ class OembedPlugin extends Plugin
*/
public function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Oembed',
$versions[] = array('name' => 'Embed',
'version' => self::PLUGIN_VERSION,
'author' => 'Mikael Nordfeldth',
'homepage' => 'http://gnu.io/social/',
'description' =>
// TRANS: Plugin description.
_m('Plugin for using and representing Oembed data.'));
_m('Plugin for using and representing oEmbed, OpenGraph and other data.'));
return true;
}
}

View File

@ -36,7 +36,7 @@ defined('GNUSOCIAL') || die();
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class OembedAction extends Action
class EmbedAction extends Action
{
protected function handle()
{

View File

@ -92,7 +92,7 @@ class File_oembed extends Managed_DataObject
*/
public static function getByFile(File $file)
{
$fo = new File_oembed();
$fo = new File_embed();
$fo->file_id = $file->id;
if (!$fo->find(true)) {
throw new NoResultException($fo);
@ -113,47 +113,47 @@ class File_oembed extends Managed_DataObject
*/
public static function saveNew($data, $file_id)
{
$file_oembed = new File_oembed;
$file_oembed->file_id = $file_id;
$file_embed = new File_embed;
$file_embed->file_id = $file_id;
if (!isset($data->version)) {
common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: '.var_export($data, true));
}
$file_oembed->version = $data->version;
$file_oembed->type = $data->type;
$file_embed->version = $data->version;
$file_embed->type = $data->type;
if (!empty($data->provider_name)) {
$file_oembed->provider = $data->provider_name;
$file_embed->provider = $data->provider_name;
}
if (!empty($data->provider)) {
$file_oembed->provider = $data->provider;
$file_embed->provider = $data->provider;
}
if (!empty($data->provider_url)) {
$file_oembed->provider_url = $data->provider_url;
$file_embed->provider_url = $data->provider_url;
}
if (!empty($data->width)) {
$file_oembed->width = intval($data->width);
$file_embed->width = intval($data->width);
}
if (!empty($data->height)) {
$file_oembed->height = intval($data->height);
$file_embed->height = intval($data->height);
}
if (!empty($data->html)) {
$file_oembed->html = $data->html;
$file_embed->html = $data->html;
}
if (!empty($data->title)) {
$file_oembed->title = $data->title;
$file_embed->title = $data->title;
}
if (!empty($data->author_name)) {
$file_oembed->author_name = $data->author_name;
$file_embed->author_name = $data->author_name;
}
if (!empty($data->author_url)) {
$file_oembed->author_url = $data->author_url;
$file_embed->author_url = $data->author_url;
}
if (!empty($data->url)) {
$file_oembed->url = $data->url;
$given_url = File_redirection::_canonUrl($file_oembed->url);
$file_embed->url = $data->url;
$given_url = File_redirection::_canonUrl($file_embed->url);
if (! empty($given_url)) {
try {
$file = File::getByUrl($given_url);
$file_oembed->mimetype = $file->mimetype;
$file_embed->mimetype = $file->mimetype;
} catch (NoResultException $e) {
// File_redirection::where argument 'discover' is false to avoid loops
$redir = File_redirection::where($given_url, false);
@ -163,9 +163,9 @@ class File_oembed extends Managed_DataObject
}
}
}
$result = $file_oembed->insert();
$result = $file_embed->insert();
if ($result === false) {
throw new ServerException('Failed to insert File_oembed data into database!');
throw new ServerException('Failed to insert File_embed data into database!');
}
if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
$ft = File_thumbnail::getKV('file_id', $file_id);