3 if (!defined('GNUSOCIAL')) { exit(1); }
5 // FIXME: To support remote video/whatever files, this plugin needs reworking.
7 class StoreRemoteMediaPlugin extends Plugin
9 // settings which can be set in config.php with addPlugin('Oembed', array('param'=>'value', ...));
10 // WARNING, these are _regexps_ (slashes added later). Always escape your dots and end your strings
11 public $domain_whitelist = array( // hostname => service provider
12 '^i\d*\.ytimg\.com$' => 'YouTube',
13 '^i\d*\.vimeocdn\.com$' => 'Vimeo',
15 public $append_whitelist = array(); // fill this array as domain_whitelist to add more trusted sources
16 public $check_whitelist = false; // security/abuse precaution
18 public $domain_blacklist = array();
19 public $check_blacklist = false;
21 protected $imgData = array();
23 // these should be declared protected everywhere
24 public function initialize()
28 $this->domain_whitelist = array_merge($this->domain_whitelist, $this->append_whitelist);
32 * Save embedding information for a File, if applicable.
34 * Normally this event is called through File::saveNew()
36 * @param File $file The abount-to-be-inserted File object.
38 * @return boolean success
40 public function onStartFileSaveNew(File &$file)
42 // save given URL as title if it's a media file this plugin understands
43 // which will make it shown in the AttachmentList widgets
45 if (isset($file->title) && strlen($file->title)>0) {
46 // Title is already set
49 if (!isset($file->mimetype)) {
50 // Unknown mimetype, it's not our job to figure out what it is.
53 switch (common_get_mime_media($file->mimetype)) {
55 // Just to set something for now at least...
56 $file->title = $file->mimetype;
63 public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
65 // If we are on a private node, we won't do any remote calls (just as a precaution until
66 // we can configure this from config.php for the private nodes)
67 if (common_config('site', 'private')) {
71 if ($media !== 'image') {
75 // If there is a local filename, it is either a local file already or has already been downloaded.
76 if (!empty($file->filename)) {
80 if (!$this->checkWhiteList($file->getUrl()) ||
81 !$this->checkBlackList($file->getUrl())) {
85 // First we download the file to memory and test whether it's actually an image file
86 common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->getID(), _ve($file->getUrl())));
88 $imgData = HTTPClient::quickGet($file->getUrl());
89 } catch (HTTP_Request2_ConnectionException $e) {
90 common_log(LOG_ERR, __CLASS__.': quickGet on URL: '._ve($file->getUrl()).' threw exception: '.$e->getMessage());
93 $info = @getimagesizefromstring($imgData);
94 if ($info === false) {
95 throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $file->getUrl());
96 } elseif (!$info[0] || !$info[1]) {
97 throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)'));
100 $filehash = hash(File::FILEHASH_ALG, $imgData);
102 // Exception will be thrown before $file is set to anything, so old $file value will be kept
103 $file = File::getByHash($filehash);
105 //FIXME: Add some code so we don't have to store duplicate File rows for same hash files.
106 } catch (NoResultException $e) {
107 $filename = $filehash . '.' . common_supported_mime_to_ext($info['mime']);
108 $fullpath = File::path($filename);
110 // Write the file to disk if it doesn't exist yet. Throw Exception on failure.
111 if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) {
112 throw new ServerException(_('Could not write downloaded file to disk.'));
115 // Updated our database for the file record
116 $orig = clone($file);
117 $file->filehash = $filehash;
118 $file->filename = $filename;
119 $file->width = $info[0]; // array indexes documented on php.net:
120 $file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
121 // Throws exception on failure.
122 $file->updateWithKeys($orig);
124 // Get rid of the file from memory
127 $imgPath = $file->getPath();
133 * @return boolean true if given url passes blacklist check
135 protected function checkBlackList($url)
137 if (!$this->check_blacklist) {
140 $host = parse_url($url, PHP_URL_HOST);
141 foreach ($this->domain_blacklist as $regex => $provider) {
142 if (preg_match("/$regex/", $host)) {
151 * @return boolean true if given url passes whitelist check
153 protected function checkWhiteList($url)
155 if (!$this->check_whitelist) {
158 $host = parse_url($url, PHP_URL_HOST);
159 foreach ($this->domain_whitelist as $regex => $provider) {
160 if (preg_match("/$regex/", $host)) {
168 public function onPluginVersion(array &$versions)
170 $versions[] = array('name' => 'StoreRemoteMedia',
171 'version' => GNUSOCIAL_VERSION,
172 'author' => 'Mikael Nordfeldth',
173 'homepage' => 'https://gnu.io/',
175 // TRANS: Plugin description.
176 _m('Plugin for downloading remotely attached files to local server.'));