[FORMAT][Embed] Ran php-cs-fixer on Embed's files and small style fixes

This commit is contained in:
Miguel Dantas 2019-07-15 22:08:19 +01:00 committed by Diogo Cordeiro
parent 938d286fb6
commit ba15724a62
1 changed files with 50 additions and 33 deletions

View File

@ -42,7 +42,7 @@ class EmbedPlugin extends Plugin
{ {
const PLUGIN_VERSION = '0.1.0'; const PLUGIN_VERSION = '0.1.0';
// settings which can be set in config.php with addPlugin('Embed', array('param'=>'value', ...)); // settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
// WARNING, these are _regexps_ (slashes added later). Always escape your dots and end ('$') your strings // WARNING, these are _regexps_ (slashes added later). Always escape your dots and end ('$') your strings
public $domain_whitelist = [ public $domain_whitelist = [
@ -50,10 +50,10 @@ class EmbedPlugin extends Plugin
'^i\d*\.ytimg\.com$' => 'YouTube', '^i\d*\.ytimg\.com$' => 'YouTube',
'^i\d*\.vimeocdn\.com$' => 'Vimeo', '^i\d*\.vimeocdn\.com$' => 'Vimeo',
]; ];
public $append_whitelist = array(); // fill this array as domain_whitelist to add more trusted sources public $append_whitelist = []; // fill this array as domain_whitelist to add more trusted sources
public $check_whitelist = false; // security/abuse precaution public $check_whitelist = false; // security/abuse precaution
protected $imgData = array(); protected $imgData = [];
/** /**
* Initialize the Embed plugin and set up the environment it needs for it. * Initialize the Embed plugin and set up the environment it needs for it.
@ -113,7 +113,6 @@ class EmbedPlugin extends Plugin
*/ */
public function onGetRemoteUrlMetadataFromDom($url, DOMDocument $dom, stdClass &$metadata) public function onGetRemoteUrlMetadataFromDom($url, DOMDocument $dom, stdClass &$metadata)
{ {
try { try {
common_log(LOG_INFO, "Trying to find Embed data for {$url} with 'oscarotero/Embed'"); common_log(LOG_INFO, "Trying to find Embed data for {$url} with 'oscarotero/Embed'");
$info = Embed::create($url); $info = Embed::create($url);
@ -156,7 +155,7 @@ class EmbedPlugin extends Plugin
{ {
switch ($action->getActionName()) { switch ($action->getActionName()) {
case 'attachment': case 'attachment':
$url = common_local_url('attachment', array('attachment' => $action->attachment->getID())); $url = common_local_url('attachment', ['attachment' => $action->attachment->getID()]);
break; break;
case 'shownotice': case 'shownotice':
if (!$action->notice->isLocal()) { if (!$action->notice->isLocal()) {
@ -174,13 +173,17 @@ class EmbedPlugin extends Plugin
if (isset($url)) { if (isset($url)) {
foreach (['xml', 'json'] as $format) { foreach (['xml', 'json'] as $format) {
$action->element('link', $action->element(
['rel' =>'alternate', 'link',
['rel' =>'alternate',
'type' => "application/{$format}+oembed", 'type' => "application/{$format}+oembed",
'href' => common_local_url('oembed', 'href' => common_local_url(
[], 'oembed',
['format' => $format, 'url' => $url]), [],
'title' => 'oEmbed']); ['format' => $format, 'url' => $url]
),
'title' => 'oEmbed']
);
} }
} }
return true; return true;
@ -219,8 +222,11 @@ class EmbedPlugin extends Plugin
} }
$file->setTitle($embed_data->title); $file->setTitle($embed_data->title);
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_WARNING, sprintf(__METHOD__.': %s thrown when getting embed data: %s', common_log(LOG_WARNING, sprintf(
get_class($e), _ve($e->getMessage()))); __METHOD__.': %s thrown when getting embed data: %s',
get_class($e),
_ve($e->getMessage())
));
return true; return true;
} }
@ -235,7 +241,7 @@ class EmbedPlugin extends Plugin
if (empty($embed->author_name) && empty($embed->provider)) { if (empty($embed->author_name) && empty($embed->provider)) {
return true; return true;
} }
$out->elementStart('div', array('id'=>'oembed_info', 'class'=>'e-content')); $out->elementStart('div', ['id'=>'oembed_info', 'class'=>'e-content']);
foreach (['author_name' => ['class' => ' author', 'url' => 'author_url'], foreach (['author_name' => ['class' => ' author', 'url' => 'author_url'],
'provider' => ['class' => '', 'url' => 'provider_url']] 'provider' => ['class' => '', 'url' => 'provider_url']]
as $field => $options) { as $field => $options) {
@ -244,10 +250,11 @@ class EmbedPlugin extends Plugin
if (empty($embed->{$options['url']})) { if (empty($embed->{$options['url']})) {
$out->text($embed->{$field}); $out->text($embed->{$field});
} else { } else {
$out->element('a', $out->element(
array('href' => $embed->{$options['url']}, 'a',
'class' => 'url'), ['href' => $embed->{$options['url']},
$embed->{$field} 'class' => 'url'],
$embed->{$field}
); );
} }
} }
@ -260,7 +267,7 @@ class EmbedPlugin extends Plugin
// Never treat generic HTML links as an enclosure type! // Never treat generic HTML links as an enclosure type!
// But if we have embed info, we'll consider it golden. // But if we have embed info, we'll consider it golden.
$embed = File_embed::getKV('file_id', $file->getID()); $embed = File_embed::getKV('file_id', $file->getID());
if (!$embed instanceof File_embed || !in_array($embed->type, array('photo', 'video'))) { if (!$embed instanceof File_embed || !in_array($embed->type, ['photo', 'video'])) {
return true; return true;
} }
@ -291,7 +298,7 @@ class EmbedPlugin extends Plugin
$thumb = $file->getThumbnail(128, 128); $thumb = $file->getThumbnail(128, 128);
$out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo embed'])); $out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo embed']));
unset($thumb); unset($thumb);
} catch (FileNotFoundException $e){ } catch (FileNotFoundException $e) {
// Nothing to show // Nothing to show
} catch (Exception $e) { } catch (Exception $e) {
$out->element('div', ['class'=>'error'], $e->getMessage()); $out->element('div', ['class'=>'error'], $e->getMessage());
@ -405,8 +412,12 @@ class EmbedPlugin extends Plugin
} catch (AlreadyFulfilledException $e) { } catch (AlreadyFulfilledException $e) {
// aw yiss! // aw yiss!
} catch (Exception $e) { } catch (Exception $e) {
common_debug(sprintf('Embed encountered an exception (%s) for file id==%d: %s', common_debug(sprintf(
get_class($e), $file->getID(), _ve($e->getMessage()))); 'Embed encountered an exception (%s) for file id==%d: %s',
get_class($e),
$file->getID(),
_ve($e->getMessage())
));
throw $e; throw $e;
} }
@ -494,7 +505,8 @@ class EmbedPlugin extends Plugin
{ {
if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) { if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) {
throw new AlreadyFulfilledException( throw new AlreadyFulfilledException(
sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id)); sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id)
);
} }
$url = $thumbnail->getUrl(); $url = $thumbnail->getUrl();
@ -522,8 +534,11 @@ class EmbedPlugin extends Plugin
// First we download the file to memory and test whether it's actually an image file // First we download the file to memory and test whether it's actually an image file
// FIXME: To support remote video/whatever files, this needs reworking. // FIXME: To support remote video/whatever files, this needs reworking.
common_debug(sprintf('Downloading remote thumbnail for file id==%u with thumbnail URL: %s', common_debug(sprintf(
$thumbnail->file_id, $url)); 'Downloading remote thumbnail for file id==%u with thumbnail URL: %s',
$thumbnail->file_id,
$url
));
$imgData = HTTPClient::quickGet($url); $imgData = HTTPClient::quickGet($url);
$info = @getimagesizefromstring($imgData); $info = @getimagesizefromstring($imgData);
if ($info === false) { if ($info === false) {
@ -547,7 +562,9 @@ class EmbedPlugin extends Plugin
if (common_get_mime_media(MediaFile::getUploadedMimeType($fullpath)) !== 'image') { if (common_get_mime_media(MediaFile::getUploadedMimeType($fullpath)) !== 'image') {
@unlink($fullpath); @unlink($fullpath);
throw new UnsupportedMediaException( throw new UnsupportedMediaException(
_('Remote file format was not identified as an image.'), $url); _('Remote file format was not identified as an image.'),
$url
);
} }
} else { } else {
throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file with id==' . throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file with id==' .
@ -588,13 +605,13 @@ class EmbedPlugin extends Plugin
*/ */
public function onPluginVersion(array &$versions) public function onPluginVersion(array &$versions)
{ {
$versions[] = array('name' => 'Embed', $versions[] = ['name' => 'Embed',
'version' => self::PLUGIN_VERSION, 'version' => self::PLUGIN_VERSION,
'author' => 'Mikael Nordfeldth', 'author' => 'Mikael Nordfeldth',
'homepage' => 'http://gnu.io/social/', 'homepage' => 'http://gnu.io/social/',
'description' => 'description' =>
// TRANS: Plugin description. // TRANS: Plugin description.
_m('Plugin for using and representing oEmbed, OpenGraph and other data.')); _m('Plugin for using and representing oEmbed, OpenGraph and other data.')];
return true; return true;
} }
} }