]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php
Merge branch 'master' of git.gnu.io:gnu/gnu-social into mmn_fixes
[quix0rs-gnu-social.git] / plugins / StoreRemoteMedia / StoreRemoteMediaPlugin.php
index 798d7bc79f0a70887002bf70b2e4de35f79c2348..e171218f79ffc6868abfe82e91f8e1d471c9e147 100644 (file)
@@ -15,6 +15,11 @@ class StoreRemoteMediaPlugin extends Plugin
     public $append_whitelist = array(); // fill this array as domain_whitelist to add more trusted sources
     public $check_whitelist  = false;    // security/abuse precaution
 
+    public $domain_blacklist = array();
+    public $check_blacklist = false;
+
+    public $max_image_bytes = 10485760;  // 10MiB max image size by default
+
     protected $imgData = array();
 
     // these should be declared protected everywhere
@@ -30,29 +35,27 @@ class StoreRemoteMediaPlugin extends Plugin
      *
      * Normally this event is called through File::saveNew()
      *
-     * @param File   $file       The newly inserted File object.
-     * @param array  $redir_data lookup data eg from File_redirection::where()
-     * @param string $given_url
+     * @param File   $file       The abount-to-be-inserted File object.
      *
      * @return boolean success
      */
-    public function onStartFileSaveNew(array &$redir_data, $given_url)
+    public function onStartFileSaveNew(File &$file)
     {
         // save given URL as title if it's a media file this plugin understands
         // which will make it shown in the AttachmentList widgets
 
-        if (isset($redir_data['title']) && strlen($redir_data['title']>0)) {
+        if (isset($file->title) && strlen($file->title)>0) {
             // Title is already set
             return true;
         }
-        if (!isset($redir_data['type'])) {
+        if (!isset($file->mimetype)) {
             // Unknown mimetype, it's not our job to figure out what it is.
             return true;
         }
-        switch (common_get_mime_media($redir_data['type'])) {
+        switch (common_get_mime_media($file->mimetype)) {
         case 'image':
             // Just to set something for now at least...
-            $redir_data['title'] = $given_url;
+            //$file->title = $file->mimetype;
             break;
         }
         
@@ -76,14 +79,53 @@ class StoreRemoteMediaPlugin extends Plugin
             return true;
         }
 
-        $this->checkWhitelist($file->getUrl());
+        $remoteUrl = $file->getUrl();
+
+        if (!$this->checkWhiteList($remoteUrl) ||
+            !$this->checkBlackList($remoteUrl)) {
+                   return true;
+        }
+
+        try {
+            /*
+            $http = new HTTPClient();
+            common_debug(sprintf('Performing HEAD request for remote file id==%u to avoid unnecessarily downloading too large files. URL: %s', $file->getID(), $remoteUrl));
+            $head = $http->head($remoteUrl);
+            $remoteUrl = $head->getEffectiveUrl();   // to avoid going through redirects again
+            if (!$this->checkBlackList($remoteUrl)) {
+                common_log(LOG_WARN, sprintf('%s: Non-blacklisted URL %s redirected to blacklisted URL %s', __CLASS__, $file->getUrl(), $remoteUrl));
+                return true;
+            }
 
-        // First we download the file to memory and test whether it's actually an image file
-        $imgData = HTTPClient::quickGet($file->getUrl());
-        common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->id, $file->url));
+            $headers = $head->getHeader();
+            $filesize = isset($headers['content-length']) ? $headers['content-length'] : null;
+            */
+            $filesize = $file->getSize();
+            if (empty($filesize)) {
+                // file size not specified on remote server
+                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) {
+                //FIXME: When we perhaps start fetching videos etc. we'll need to differentiate max_image_bytes from that...
+                // file too big according to plugin configuration
+                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')) {
+                // file too big according to site configuration
+                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;
+            }
+
+            // Then we download the file to memory and test whether it's actually an image file
+            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.'), $url);
+            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)'));
         }
@@ -110,7 +152,7 @@ class StoreRemoteMediaPlugin extends Plugin
             $file->width = $info[0];    // array indexes documented on php.net:
             $file->height = $info[1];   // https://php.net/manual/en/function.getimagesize.php
             // Throws exception on failure.
-            $file->updateWithKeys($orig, 'id');
+            $file->updateWithKeys($orig);
         }
         // Get rid of the file from memory
         unset($imgData);
@@ -121,23 +163,39 @@ class StoreRemoteMediaPlugin extends Plugin
     }
 
     /**
-     * @return boolean          false on no check made, provider name on success
-     * @throws ServerException  if check is made but fails
+     * @return boolean          true if given url passes blacklist check
      */
-    protected function checkWhitelist($url)
+    protected function checkBlackList($url)
     {
-        if (!$this->check_whitelist) {
-            return false;   // indicates "no check made"
+        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;
+    }
 
+    /***
+     * @return boolean          true if given url passes whitelist check
+     */
+    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 $provider;    // we trust this source, return provider name
+                return true;
             }
         }
 
-        throw new ServerException(sprintf(_('Domain not in remote source whitelist: %s'), $host));
+        return false;
     }
 
     public function onPluginVersion(array &$versions)