Merge "Adding wildcard support to $wgCopyUploadsDomains"
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
index fdd2b1a..7f430c5 100644 (file)
@@ -61,6 +61,8 @@ class UploadFromUrl extends UploadBase {
 
        /**
         * Checks whether the URL is for an allowed host
+        * The domains in the whitelist can include wildcard characters (*) in place
+        * of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
         *
         * @param $url string
         * @return bool
@@ -70,17 +72,34 @@ class UploadFromUrl extends UploadBase {
                if ( !count( $wgCopyUploadsDomains ) ) {
                        return true;
                }
-               $uri = new Uri( $url );
-               $parsedDomain = $uri->getHost();
-               if ( $parsedDomain === null ) {
+               $parsedUrl = wfParseUrl( $url );
+               if ( !$parsedUrl ) {
                        return false;
                }
                $valid = false;
                foreach( $wgCopyUploadsDomains as $domain ) {
-                       if ( $parsedDomain === $domain ) {
+                       // See if the domain for the upload matches this whitelisted domain
+                       $whitelistedDomainPieces = explode( '.', $domain );
+                       $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
+                       if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
+                               $valid = true;
+                               // See if all the pieces match or not (excluding wildcards)
+                               foreach ( $whitelistedDomainPieces as $index => $piece ) {
+                                       if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
+                                               $valid = false;
+                                       }
+                               }
+                               if ( $valid ) {
+                                       // We found a match, so quit comparing against the list
+                                       break;
+                               }
+                       }
+                       /* Non-wildcard test
+                       if ( $parsedUrl['host'] === $domain ) {
                                $valid = true;
                                break;
                        }
+                       */
                }
                return $valid;
        }
@@ -208,9 +227,14 @@ class UploadFromUrl extends UploadBase {
                $this->mRemoveTempFile = true;
                $this->mFileSize = 0;
 
-               $req = MWHttpRequest::factory( $this->mUrl, array(
+               $options = array(
                        'followRedirects' => true
-               ) );
+               );
+               global $wgCopyUploadProxy;
+               if ( $wgCopyUploadProxy !== false ) {
+                       $options['proxy'] = $wgCopyUploadProxy;
+               }
+               $req = MWHttpRequest::factory( $this->mUrl, $options );
                $req->setCallback( array( $this, 'saveTempFileChunk' ) );
                $status = $req->execute();