123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace Plugin\StoreRemoteMedia;
- use App\Core\Modules\Plugin;
- class StoreRemoteMedia extends Plugin
- {
- const PLUGIN_VERSION = '3.0.0';
-
-
- public $domain_whitelist = [
-
- '^i\d*\.ytimg\.com$' => 'YouTube',
- '^i\d*\.vimeocdn\.com$' => 'Vimeo',
- ];
- public $append_whitelist = [];
- public $check_whitelist = false;
- public $store_original = false;
- public $thumbnail_width;
- public $thumbnail_height;
- public $crop;
- public $max_size;
-
- public function initialize()
- {
- parent::initialize();
- $this->domain_whitelist = array_merge($this->domain_whitelist, $this->append_whitelist);
-
- $this->thumbnail_width = $this->thumbnail_width ?? common_config('thumbnail', 'width');
- $this->thumbnail_height = $this->thumbnail_height ?? common_config('thumbnail', 'height');
- $this->max_size = $this->max_size ?? common_config('attachments', 'file_quota');
- $this->crop = $this->crop ?? common_config('thumbnail', 'crop');
- }
-
- public function onCreateFileImageThumbnailSource(File $file, ?string &$imgPath = null, ?string $media = null): bool
- {
-
-
- if (common_config('site', 'private')) {
- return true;
- }
-
- if (!$file->isStoredRemotely()) {
- common_debug(sprintf('File id==%d isn\'t a non-fetched remote file (%s), so nothing StoreRemoteMedia ' .
- 'should handle.', $file->getID(), _ve($file->filename)));
- return true;
- }
- try {
- File_thumbnail::byFile($file);
-
- return true;
- } catch (NoResultException $e) {
-
- }
- $url = $file->getUrl(false);
- if (substr($url, 0, 7) == 'file://') {
- $filename = substr($url, 7);
- $info = getimagesize($filename);
- $filename = basename($filename);
- $width = $info[0];
- $height = $info[1];
- } else {
- $this->checkWhitelist($url);
- $head = (new HTTPClient())->head($url);
- $headers = $head->getHeader();
- $headers = array_change_key_case($headers, CASE_LOWER);
- try {
- $is_image = $this->isRemoteImage($url, $headers);
- if ($is_image == true) {
- $file_size = $this->getRemoteFileSize($url, $headers);
- if (($file_size != false) && ($file_size > $this->max_size)) {
- common_debug('Went to store remote thumbnail of size ' . $file_size .
- ' but the upload limit is ' . $this->max_size . ' so we aborted.');
- return false;
- }
- } else {
- return false;
- }
- } catch (Exception $err) {
- common_debug('Could not determine size of remote image, aborted local storage.');
- throw $err;
- }
-
-
- common_debug(sprintf(
- 'Downloading remote image for file id==%u with URL: %s',
- $file->getID(),
- $url
- ));
- try {
- $imgData = HTTPClient::quickGet($url);
- if (isset($imgData)) {
- list($filename, $filehash, $width, $height) = $this->validateAndWriteImage(
- $imgData,
- $url,
- $headers,
- $file->getID()
- );
- } else {
- throw new UnsupportedMediaException('HTTPClient returned an empty result');
- }
- } catch (UnsupportedMediaException $e) {
-
- common_debug("StoreRemoteMedia was not able to find an image for URL `{$url}`: " . $e->getMessage());
- return false;
- }
- }
- $ft = null;
- if ($this->store_original) {
- try {
-
- $orig = clone $file;
- $file->filename = $filename;
- $file->filehash = $filehash;
- $file->width = $width;
- $file->height = $height;
-
- $file->updateWithKeys($orig);
- } catch (Exception $err) {
- common_log(LOG_ERR, 'Went to update a file entry on the database in ' .
- 'StoreRemoteMediaPlugin::storeRemoteThumbnail but encountered error: ' . $err);
- throw $err;
- }
- } else {
- try {
-
- $data = new stdClass();
- $data->thumbnail_url = $url;
- $data->thumbnail_width = $width;
- $data->thumbnail_height = $height;
- File_thumbnail::saveNew($data, $file->getID());
- $ft = File_thumbnail::byFile($file);
- $orig = clone $ft;
- $ft->filename = $filename;
- $ft->updateWithKeys($orig);
- } catch (Exception $err) {
- common_log(LOG_ERR, 'Went to write a thumbnail entry to the database in ' .
- 'StoreRemoteMediaPlugin::storeRemoteThumbnail but encountered error: ' . $err);
- throw $err;
- }
- }
-
- try {
- $imgPath = $file->getFileOrThumbnailPath($ft);
- return !file_exists($imgPath);
- } catch (Exception $e) {
- return true;
- }
- }
-
- private function getRemoteFileSize($url, $headers = null)
- {
- try {
- if ($headers === null) {
- if (!common_valid_http_url($url)) {
- common_log(LOG_ERR, 'Invalid URL in StoreRemoteMedia::getRemoteFileSize()');
- return false;
- }
- $head = (new HTTPClient())->head($url);
- $headers = $head->getHeader();
- $headers = array_change_key_case($headers, CASE_LOWER);
- }
- return $headers['content-length'] ?? false;
- } catch (Exception $err) {
- common_log(LOG_ERR, __CLASS__ . ': getRemoteFileSize on URL : ' . _ve($url) .
- ' threw exception: ' . $err->getMessage());
- return false;
- }
- }
-
- private function isRemoteImage($url, $headers = null): bool
- {
- if (empty($headers)) {
- if (!common_valid_http_url($url)) {
- common_log(LOG_ERR, 'Invalid URL in StoreRemoteMedia::isRemoteImage()');
- return false;
- }
- $head = (new HTTPClient())->head($url);
- $headers = $head->getHeader();
- $headers = array_change_key_case($headers, CASE_LOWER);
- }
- return !empty($headers['content-type']) && common_get_mime_media($headers['content-type']) === 'image';
- }
-
- protected function validateAndWriteImage(&$imgData, ?string $url = null, ?array $headers = null, ?int $file_id = null): array
- {
- $info = @getimagesizefromstring($imgData);
-
-
- if ($info === false) {
- throw new UnsupportedMediaException(_m('Remote file format was not identified as an image.'), $url);
- } elseif (!$info[0] || !$info[1]) {
- throw new UnsupportedMediaException(_m('Image file had impossible geometry (0 width or height)'));
- }
- $width = min($info[0], $this->thumbnail_width);
- $height = min($info[1], $this->thumbnail_height);
- $filehash = hash(File::FILEHASH_ALG, $imgData);
- try {
- if (!empty($url)) {
- $original_name = HTTPClient::get_filename($url, $headers);
- }
- $filename = MediaFile::encodeFilename($original_name ?? _m('Untitled attachment'), $filehash);
- } catch (Exception $err) {
- common_log(LOG_ERR, 'Went to write a thumbnail to disk in StoreRemoteMediaPlugin::storeRemoteThumbnail ' .
- "but encountered error: {$err}");
- throw $err;
- }
- try {
- $fullpath = $this->store_original ? File::path($filename) : File_thumbnail::path($filename);
-
- if (!file_exists($fullpath)) {
- if (strpos($fullpath, INSTALLDIR) !== 0 || file_put_contents($fullpath, $imgData) === false) {
- throw new ServerException(_m('Could not write downloaded file to disk.'));
- }
- if (common_get_mime_media(MediaFile::getUploadedMimeType($fullpath)) !== 'image') {
- @unlink($fullpath);
- throw new UnsupportedMediaException(
- _m('Remote file format was not identified as an image.'),
- $url
- );
- }
-
- if (!$this->store_original && $this->crop && ($info[0] > $this->thumbnail_width || $info[1] > $this->thumbnail_height)) {
- try {
-
- $img = new ImageFile(-1, $fullpath);
- list($width, $height, $x, $y, $w, $h) = $img->scaleToFit($this->thumbnail_width, $this->thumbnail_height, $this->crop);
-
- $box = [
- 'width' => $width, 'height' => $height,
- 'x' => $x, 'y' => $y,
- 'w' => $w, 'h' => $h,
- ];
- $width = $box['width'];
- $height = $box['height'];
- $img->resizeTo($fullpath, $box);
- } catch (\Intervention\Image\Exception\NotReadableException $e) {
- common_log(LOG_ERR, "StoreRemoteMediaPlugin::storeRemoteThumbnail was unable to decode image with Intervention: {$e}");
-
- }
- }
- } else {
- throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file' .
- ($file_id ? 'with id==' . $file_id : '') . ' at path ' . $fullpath);
- }
- } catch (AlreadyFulfilledException $e) {
-
- } catch (Exception $err) {
- common_log(LOG_ERR, 'Went to write a thumbnail to disk in StoreRemoteMediaPlugin::storeRemoteThumbnail ' .
- "but encountered error: {$err}");
- throw $err;
- } finally {
- unset($imgData);
- }
- return [$filename, $filehash, $width, $height];
- }
-
- protected function checkWhitelist($url)
- {
- if (!$this->check_whitelist) {
- return false;
- }
- $host = parse_url($url, PHP_URL_HOST);
- foreach ($this->domain_whitelist as $regex => $provider) {
- if (preg_match("/{$regex}/", $host)) {
- return $provider;
- }
- }
- throw new ServerException(sprintf(_m('Domain not in remote thumbnail source whitelist: %s'), $host));
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = ['name' => 'StoreRemoteMedia',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Mikael Nordfeldth, Diogo Peralta Cordeiro',
- 'homepage' => GNUSOCIAL_ENGINE_URL,
- 'description' =>
- _m('Plugin for downloading remotely attached files to local server.'), ];
- return true;
- }
- }
|