123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class StoreRemoteMediaPlugin extends Plugin
- {
-
-
- public $domain_whitelist = array(
- '^i\d*\.ytimg\.com$' => 'YouTube',
- '^i\d*\.vimeocdn\.com$' => 'Vimeo',
- );
- public $append_whitelist = array();
- public $check_whitelist = false;
- 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':
-
- $file->title = $file->mimetype;
- 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;
- }
- $this->checkWhitelist($file->getUrl());
-
- common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->getID(), _ve($file->getUrl())));
- try {
- $imgData = HTTPClient::quickGet($file->getUrl());
- } catch (HTTP_Request2_ConnectionException $e) {
- common_log(LOG_ERR, __CLASS__.': quickGet 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.'), $file->getUrl());
- } 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 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(_('Domain not in remote source whitelist: %s'), $host));
- }
- public function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'StoreRemoteMedia',
- 'version' => GNUSOCIAL_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => 'https://gnu.io/',
- 'description' =>
-
- _m('Plugin for downloading remotely attached files to local server.'));
- return true;
- }
- }
|