123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class StoreRemoteMediaPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
-
-
- public $domain_whitelist = array(
- '^i\d*\.ytimg\.com$' => 'YouTube',
- '^i\d*\.vimeocdn\.com$' => 'Vimeo',
- );
- public $append_whitelist = array();
- public $check_whitelist = false;
- public $domain_blacklist = array();
- public $check_blacklist = false;
- public $max_image_bytes = 10485760;
- protected $imgData = array();
-
- public function initialize()
- {
- parent::initialize();
- $this->domain_whitelist = array_merge($this->domain_whitelist, $this->append_whitelist);
- }
-
- public function onStartFileSaveNew(File &$file)
- {
-
-
- if (isset($file->title) && strlen($file->title)>0) {
-
- return true;
- }
- if (!isset($file->mimetype)) {
-
- return true;
- }
- switch (common_get_mime_media($file->mimetype)) {
- case 'image':
-
-
- break;
- }
-
- return true;
- }
- public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
- {
-
-
- if (common_config('site', 'private')) {
- return true;
- }
- if ($media !== 'image') {
- return true;
- }
-
- if (!empty($file->filename)) {
- return true;
- }
- $remoteUrl = $file->getUrl();
- if (!$this->checkWhiteList($remoteUrl) ||
- !$this->checkBlackList($remoteUrl)) {
- return true;
- }
- try {
-
- $filesize = $file->getSize();
- if (empty($filesize)) {
-
- common_debug(sprintf('%s: Ignoring remote media because we did not get a content length for file id==%u', __CLASS__, $file->getID()));
- return true;
- } elseif ($filesize > $this->max_image_bytes) {
-
-
- common_debug(sprintf('%s: Skipping remote media because content length (%u) is larger than plugin configured max_image_bytes (%u) for file id==%u', __CLASS__, intval($filesize), $this->max_image_bytes, $file->getID()));
- return true;
- } elseif ($filesize > common_config('attachments', 'file_quota')) {
-
- common_debug(sprintf('%s: Skipping remote media because content length (%u) is larger than file_quota (%u) for file id==%u', __CLASS__, intval($filesize), common_config('attachments', 'file_quota'), $file->getID()));
- return true;
- }
-
- common_debug(sprintf('Downloading remote file id==%u (should be size %u) with effective URL: %s', $file->getID(), $filesize, _ve($remoteUrl)));
- $imgData = HTTPClient::quickGet($remoteUrl);
- } catch (HTTP_Request2_ConnectionException $e) {
- common_log(LOG_ERR, __CLASS__.': '._ve(get_class($e)).' on URL: '._ve($file->getUrl()).' threw exception: '.$e->getMessage());
- return true;
- }
- $info = @getimagesizefromstring($imgData);
- if ($info === false) {
- throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $remoteUrl);
- } elseif (!$info[0] || !$info[1]) {
- throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)'));
- }
- $filehash = hash(File::FILEHASH_ALG, $imgData);
- try {
-
- $file = File::getByHash($filehash);
-
- } catch (NoResultException $e) {
- $filename = $filehash . '.' . common_supported_mime_to_ext($info['mime']);
- $fullpath = File::path($filename);
-
- if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) {
- throw new ServerException(_('Could not write downloaded file to disk.'));
- }
-
- $orig = clone($file);
- $file->filehash = $filehash;
- $file->filename = $filename;
- $file->width = $info[0];
- $file->height = $info[1];
-
- $file->updateWithKeys($orig);
- }
-
- unset($imgData);
- $imgPath = $file->getPath();
- return false;
- }
-
- protected function checkBlackList($url)
- {
- if (!$this->check_blacklist) {
- return true;
- }
- $host = parse_url($url, PHP_URL_HOST);
- foreach ($this->domain_blacklist as $regex => $provider) {
- if (preg_match("/$regex/", $host)) {
- return false;
- }
- }
- return true;
- }
-
- protected function checkWhiteList($url)
- {
- if (!$this->check_whitelist) {
- return true;
- }
- $host = parse_url($url, PHP_URL_HOST);
- foreach ($this->domain_whitelist as $regex => $provider) {
- if (preg_match("/$regex/", $host)) {
- return true;
- }
- }
- return false;
- }
- public function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'StoreRemoteMedia',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => 'https://gnu.io/',
- 'description' =>
-
- _m('Plugin for downloading remotely attached files to local server.'));
- return true;
- }
- }
|