hook to intercept upload_by_url
authornischayn22 <nischayn22@gmail.com>
Thu, 23 May 2013 10:16:38 +0000 (15:46 +0530)
committerMatthias Mullie <git@mullie.eu>
Wed, 24 Jul 2013 17:11:33 +0000 (19:11 +0200)
This is planned to be used for intercepting by UW for Flickr blacklisting.

Bug: 42307
Change-Id: Ia3daaeba1ce5e69e751ffc2ae5afd5e449cf4aa7

RELEASE-NOTES-1.22
docs/hooks.txt
includes/api/ApiBase.php
includes/api/ApiUpload.php
includes/upload/UploadFromUrl.php

index f6c304e..0b4a6c5 100644 (file)
@@ -149,6 +149,8 @@ production.
   right?" check is used to avoid more expensive checks.
 * Display "(No difference)" instead of an empty diff (when comparing revisions
   in the history or when previewing changes while editing).
+* New hook 'IsUploadAllowedFromUrl' is added which can be used to intercept uploads by
+  URL, useful for blacklisting specific URLs
 
 === Bug fixes in 1.22 ===
 * Disable Special:PasswordReset when $wgEnableEmail is false. Previously one
index 9967666..84cc820 100644 (file)
@@ -1360,6 +1360,10 @@ $article: article (object) being checked
 $ip: IP being check
 $result: Change this value to override the result of wfIsTrustedProxy()
 
+'IsUploadAllowedFromUrl': Override the result of UploadFromUrl::isAllowedUrl()
+$url: URL used to upload from
+&$allowed: Boolean indicating if uploading is allowed for given URL
+
 'isValidEmailAddr': Override the result of User::isValidEmailAddr(), for
 instance to return false if the domain name doesn't match your organization.
 $addr: The e-mail address entered by the user
index 8f5185a..2e6846d 100644 (file)
@@ -1404,6 +1404,7 @@ abstract class ApiBase extends ContextSource {
                'uploaddisabled' => array( 'code' => 'uploaddisabled', 'info' => 'Uploads are not enabled. Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true' ),
                'copyuploaddisabled' => array( 'code' => 'copyuploaddisabled', 'info' => 'Uploads by URL is not enabled. Make sure $wgAllowCopyUploads is set to true in LocalSettings.php.' ),
                'copyuploadbaddomain' => array( 'code' => 'copyuploadbaddomain', 'info' => 'Uploads by URL are not allowed from this domain.' ),
+               'copyuploadbadurl' => array( 'code' => 'copyuploadbadurl', 'info' => 'Upload not allowed from this URL.' ),
 
                'filename-tooshort' => array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
                'filename-toolong' => array( 'code' => 'filename-toolong', 'info' => 'The filename is too long' ),
index 34741b5..b903a62 100644 (file)
@@ -410,6 +410,10 @@ class ApiUpload extends ApiBase {
                                $this->dieUsageMsg( 'copyuploadbaddomain' );
                        }
 
+                       if ( !UploadFromUrl::isAllowedUrl( $this->mParams['url'] ) ) {
+                               $this->dieUsageMsg( 'copyuploadbadurl' );
+                       }
+
                        $async = false;
                        if ( $this->mParams['asyncdownload'] ) {
                                $this->checkAsyncDownloadEnabled();
index c99feef..a4374f3 100644 (file)
@@ -34,6 +34,8 @@ class UploadFromUrl extends UploadBase {
 
        protected $mTempPath, $mTmpHandle;
 
+       protected static $allowedUrls = array();
+
        /**
         * Checks if the user is allowed to use the upload-by-URL feature. If the
         * user is not allowed, return the name of the user right as a string. If
@@ -104,6 +106,21 @@ class UploadFromUrl extends UploadBase {
                return $valid;
        }
 
+       /**
+        * Checks whether the URL is not allowed.
+        *
+        * @param $url string
+        * @return bool
+        */
+       public static function isAllowedUrl( $url ) {
+               if ( !isset( self::$allowedUrls[$url] ) ) {
+                       $allowed = true;
+                       wfRunHooks( 'IsUploadAllowedFromUrl', array( $url, &$allowed ) );
+                       self::$allowedUrls[$url] = $allowed;
+               }
+               return self::$allowedUrls[$url];
+       }
+
        /**
         * Entry point for API upload
         *
@@ -175,6 +192,9 @@ class UploadFromUrl extends UploadBase {
                if ( !self::isAllowedHost( $this->mUrl ) ) {
                        return Status::newFatal( 'upload-copy-upload-invalid-domain' );
                }
+               if ( !self::isAllowedUrl( $this->mUrl ) ) {
+                       return Status::newFatal( 'upload-copy-upload-invalid-url' );
+               }
                if ( !$this->mAsync ) {
                        return $this->reallyFetchFile();
                }