[Embed] Fix use of undefined img_data

Got error 'PHP message: PHP Notice: Undefined variable: img_data in /var/www/html/plugins/Embed/EmbedPlugin.php on line 544'
This commit is contained in:
Miguel Dantas 2019-09-02 16:31:32 +01:00 committed by Diogo Peralta Cordeiro
parent 8f070f53ec
commit a99ae06573

View File

@ -132,8 +132,8 @@ class EmbedPlugin extends Plugin
if (substr($info->image, 0, 4) === 'data') { if (substr($info->image, 0, 4) === 'data') {
// Inline image // Inline image
$img_data = base64_decode(substr($info->image, stripos($info->image, 'base64,') + 7)); $imgData = base64_decode(substr($info->image, stripos($info->image, 'base64,') + 7));
list($filename, , ) = $this->validateAndWriteImage($img_data); list($filename, , ) = $this->validateAndWriteImage($imgData);
// Use a file URI for images, as file_embed can't store a filename // Use a file URI for images, as file_embed can't store a filename
$metadata->thumbnail_url = 'file://' . File_thumbnail::path($filename); $metadata->thumbnail_url = 'file://' . File_thumbnail::path($filename);
} else { } else {
@ -541,7 +541,7 @@ class EmbedPlugin extends Plugin
$fullpath = File_thumbnail::path($filename); $fullpath = File_thumbnail::path($filename);
// Write the file to disk. Throw Exception on failure // Write the file to disk. Throw Exception on failure
if (!file_exists($fullpath)) { if (!file_exists($fullpath)) {
if (strpos($fullpath, INSTALLDIR) !== 0 || file_put_contents($fullpath, $img_data) === false) { if (strpos($fullpath, INSTALLDIR) !== 0 || file_put_contents($fullpath, $imgData) === false) {
throw new ServerException(_('Could not write downloaded file to disk.')); throw new ServerException(_('Could not write downloaded file to disk.'));
} }
@ -575,7 +575,7 @@ class EmbedPlugin extends Plugin
"but encountered error: {$err}"); "but encountered error: {$err}");
throw $err; throw $err;
} finally { } finally {
unset($img_data); unset($imgData);
} }
return [$filename, $width, $height]; return [$filename, $width, $height];
@ -620,6 +620,8 @@ class EmbedPlugin extends Plugin
" but the upload limit is " . $max_size . " so we aborted."); " but the upload limit is " . $max_size . " so we aborted.");
return false; return false;
} }
} else {
return false;
} }
} catch (Exception $err) { } catch (Exception $err) {
common_debug("Could not determine size of remote image, aborted local storage."); common_debug("Could not determine size of remote image, aborted local storage.");
@ -633,10 +635,19 @@ class EmbedPlugin extends Plugin
$thumbnail->file_id, $thumbnail->file_id,
$url $url
)); ));
$imgData = HTTPClient::quickGet($url); try {
$imgData = HTTPClient::quickGet($url);
list($filename, $width, $height) = $this->validateAndWriteImage($imgData, $url, $headers, if (isset($imgData)) {
$thumbnail->file_id); list($filename, $width, $height) = $this->validateAndWriteImage($imgData, $url, $headers,
$thumbnail->file_id);
} else {
throw new UnsupportedMediaException('HTTPClient returned an empty result');
}
} catch(UnsupportedMediaException $e) {
// Couldn't find anything that looks like an image, nothing to do
common_debug("Embed was not able to find an image for URL `{$url}`: " . $e->getMessage());
return false;
}
} }
try { try {