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 protected $imgData = array();
20 // these should be declared protected everywhere
21 public function initialize()
25 $this->domain_whitelist = array_merge($this->domain_whitelist, $this->append_whitelist);
29 * Save embedding information for a File, if applicable.
31 * Normally this event is called through File::saveNew()
33 * @param File $file The abount-to-be-inserted File object.
35 * @return boolean success
37 public function onStartFileSaveNew(File &$file)
39 // save given URL as title if it's a media file this plugin understands
40 // which will make it shown in the AttachmentList widgets
42 if (isset($file->title) && strlen($file->title)>0) {
43 // Title is already set
46 if (!isset($file->mimetype)) {
47 // Unknown mimetype, it's not our job to figure out what it is.
50 switch (common_get_mime_media($file->mimetype)) {
52 // Just to set something for now at least...
53 $file->title = $file->mimetype;
60 public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
62 // If we are on a private node, we won't do any remote calls (just as a precaution until
63 // we can configure this from config.php for the private nodes)
64 if (common_config('site', 'private')) {
68 if ($media !== 'image') {
72 // If there is a local filename, it is either a local file already or has already been downloaded.
73 if (!empty($file->filename)) {
77 $this->checkWhitelist($file->getUrl());
79 // First we download the file to memory and test whether it's actually an image file
80 common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->getID(), _ve($file->getUrl())));
82 $imgData = HTTPClient::quickGet($file->getUrl());
83 } catch (HTTP_Request2_ConnectionException $e) {
84 common_log(LOG_ERR, __CLASS__.': quickGet on URL: '._ve($file->getUrl()).' threw exception: '.$e->getMessage());
87 $info = @getimagesizefromstring($imgData);
88 if ($info === false) {
89 throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $file->getUrl());
90 } elseif (!$info[0] || !$info[1]) {
91 throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)'));
94 $filehash = hash(File::FILEHASH_ALG, $imgData);
96 // Exception will be thrown before $file is set to anything, so old $file value will be kept
97 $file = File::getByHash($filehash);
99 //FIXME: Add some code so we don't have to store duplicate File rows for same hash files.
100 } catch (NoResultException $e) {
101 $filename = $filehash . '.' . common_supported_mime_to_ext($info['mime']);
102 $fullpath = File::path($filename);
104 // Write the file to disk if it doesn't exist yet. Throw Exception on failure.
105 if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) {
106 throw new ServerException(_('Could not write downloaded file to disk.'));
109 // Updated our database for the file record
110 $orig = clone($file);
111 $file->filehash = $filehash;
112 $file->filename = $filename;
113 $file->width = $info[0]; // array indexes documented on php.net:
114 $file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
115 // Throws exception on failure.
116 $file->updateWithKeys($orig);
118 // Get rid of the file from memory
121 $imgPath = $file->getPath();
127 * @return boolean false on no check made, provider name on success
128 * @throws ServerException if check is made but fails
130 protected function checkWhitelist($url)
132 if (!$this->check_whitelist) {
133 return false; // indicates "no check made"
136 $host = parse_url($url, PHP_URL_HOST);
137 foreach ($this->domain_whitelist as $regex => $provider) {
138 if (preg_match("/$regex/", $host)) {
139 return $provider; // we trust this source, return provider name
143 throw new ServerException(sprintf(_('Domain not in remote source whitelist: %s'), $host));
146 public function onPluginVersion(array &$versions)
148 $versions[] = array('name' => 'StoreRemoteMedia',
149 'version' => GNUSOCIAL_VERSION,
150 'author' => 'Mikael Nordfeldth',
151 'homepage' => 'https://gnu.io/',
153 // TRANS: Plugin description.
154 _m('Plugin for downloading remotely attached files to local server.'));