Merge "Adding wildcard support to $wgCopyUploadsDomains"
authorKaldari <rkaldari@wikimedia.org>
Fri, 28 Sep 2012 23:20:52 +0000 (23:20 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 28 Sep 2012 23:20:52 +0000 (23:20 +0000)
1  2 
includes/upload/UploadFromUrl.php

@@@ -61,6 -61,8 +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
                }
                $valid = false;
                foreach( $wgCopyUploadsDomains as $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;
        }
                $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();