3 * Backend for uploading files from a HTTP resource.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
25 use MediaWiki\User\UserIdentity
;
28 * Implements uploading from a HTTP resource.
31 * @author Bryan Tong Minh
32 * @author Michael Dale
34 class UploadFromUrl
extends UploadBase
{
37 protected $mTempPath, $mTmpHandle;
39 protected static $allowedUrls = [];
42 * Checks if the user is allowed to use the upload-by-URL feature. If the
43 * user is not allowed, return the name of the user right as a string. If
44 * the user is allowed, have the parent do further permissions checking.
46 * @param UserIdentity $user
50 public static function isAllowed( UserIdentity
$user ) {
51 if ( !MediaWikiServices
::getInstance()
52 ->getPermissionManager()
53 ->userHasRight( $user, 'upload_by_url' )
55 return 'upload_by_url';
58 return parent
::isAllowed( $user );
62 * Checks if the upload from URL feature is enabled
65 public static function isEnabled() {
66 global $wgAllowCopyUploads;
68 return $wgAllowCopyUploads && parent
::isEnabled();
72 * Checks whether the URL is for an allowed host
73 * The domains in the whitelist can include wildcard characters (*) in place
74 * of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
79 public static function isAllowedHost( $url ) {
80 global $wgCopyUploadsDomains;
81 if ( !count( $wgCopyUploadsDomains ) ) {
84 $parsedUrl = wfParseUrl( $url );
89 foreach ( $wgCopyUploadsDomains as $domain ) {
90 // See if the domain for the upload matches this whitelisted domain
91 $whitelistedDomainPieces = explode( '.', $domain );
92 $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
93 if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
95 // See if all the pieces match or not (excluding wildcards)
96 foreach ( $whitelistedDomainPieces as $index => $piece ) {
97 if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
102 // We found a match, so quit comparing against the list
107 if ( $parsedUrl['host'] === $domain ) {
118 * Checks whether the URL is not allowed.
123 public static function isAllowedUrl( $url ) {
124 if ( !isset( self
::$allowedUrls[$url] ) ) {
126 Hooks
::run( 'IsUploadAllowedFromUrl', [ $url, &$allowed ] );
127 self
::$allowedUrls[$url] = $allowed;
130 return self
::$allowedUrls[$url];
134 * Entry point for API upload
136 * @param string $name
138 * @throws MWException
140 public function initialize( $name, $url ) {
143 $tempPath = $this->makeTemporaryFile();
144 # File size and removeTempFile will be filled in later
145 $this->initializePathInfo( $name, $tempPath, 0, false );
149 * Entry point for SpecialUpload
150 * @param WebRequest &$request
152 public function initializeFromRequest( &$request ) {
153 $desiredDestName = $request->getText( 'wpDestFile' );
154 if ( !$desiredDestName ) {
155 $desiredDestName = $request->getText( 'wpUploadFileURL' );
159 trim( $request->getVal( 'wpUploadFileURL' ) )
164 * @param WebRequest $request
167 public static function isValidRequest( $request ) {
170 $url = $request->getVal( 'wpUploadFileURL' );
172 return !empty( $url )
173 && MediaWikiServices
::getInstance()
174 ->getPermissionManager()
175 ->userHasRight( $wgUser, 'upload_by_url' );
181 public function getSourceType() {
188 * @param array $httpOptions Array of options for MWHttpRequest.
189 * This could be used to override the timeout on the http request.
192 public function fetchFile( $httpOptions = [] ) {
193 if ( !Http
::isValidURI( $this->mUrl
) ) {
194 return Status
::newFatal( 'http-invalid-url', $this->mUrl
);
197 if ( !self
::isAllowedHost( $this->mUrl
) ) {
198 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
200 if ( !self
::isAllowedUrl( $this->mUrl
) ) {
201 return Status
::newFatal( 'upload-copy-upload-invalid-url' );
203 return $this->reallyFetchFile( $httpOptions );
207 * Create a new temporary file in the URL subdirectory of wfTempDir().
209 * @return string Path to the file
211 protected function makeTemporaryFile() {
212 $tmpFile = MediaWikiServices
::getInstance()->getTempFSFileFactory()
213 ->newTempFSFile( 'URL', 'urlupload_' );
214 $tmpFile->bind( $this );
216 return $tmpFile->getPath();
220 * Callback: save a chunk of the result of a HTTP request to the temporary file
223 * @param string $buffer
224 * @return int Number of bytes handled
226 public function saveTempFileChunk( $req, $buffer ) {
227 wfDebugLog( 'fileupload', 'Received chunk of ' . strlen( $buffer ) . ' bytes' );
228 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
230 if ( $nbytes == strlen( $buffer ) ) {
231 $this->mFileSize +
= $nbytes;
233 // Well... that's not good!
236 'Short write ' . $nbytes . '/' . strlen( $buffer ) .
237 ' bytes, aborting with ' . $this->mFileSize
. ' uploaded so far'
239 fclose( $this->mTmpHandle
);
240 $this->mTmpHandle
= false;
247 * Download the file, save it to the temporary file and update the file
248 * size and set $mRemoveTempFile to true.
250 * @param array $httpOptions Array of options for MWHttpRequest
253 protected function reallyFetchFile( $httpOptions = [] ) {
254 global $wgCopyUploadProxy, $wgCopyUploadTimeout;
255 if ( $this->mTempPath
=== false ) {
256 return Status
::newFatal( 'tmp-create-error' );
259 // Note the temporary file should already be created by makeTemporaryFile()
260 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
261 if ( !$this->mTmpHandle
) {
262 return Status
::newFatal( 'tmp-create-error' );
264 wfDebugLog( 'fileupload', 'Temporary file created "' . $this->mTempPath
. '"' );
266 $this->mRemoveTempFile
= true;
267 $this->mFileSize
= 0;
269 $options = $httpOptions +
[ 'followRedirects' => true ];
271 if ( $wgCopyUploadProxy !== false ) {
272 $options['proxy'] = $wgCopyUploadProxy;
275 if ( $wgCopyUploadTimeout && !isset( $options['timeout'] ) ) {
276 $options['timeout'] = $wgCopyUploadTimeout;
280 'Starting download from "' . $this->mUrl
. '" ' .
281 '<' . implode( ',', array_keys( array_filter( $options ) ) ) . '>'
283 $req = MWHttpRequest
::factory( $this->mUrl
, $options, __METHOD__
);
284 $req->setCallback( [ $this, 'saveTempFileChunk' ] );
285 $status = $req->execute();
287 if ( $this->mTmpHandle
) {
288 // File got written ok...
289 fclose( $this->mTmpHandle
);
290 $this->mTmpHandle
= null;
292 // We encountered a write error during the download...
293 return Status
::newFatal( 'tmp-write-error' );
296 wfDebugLog( 'fileupload', $status );
297 if ( $status->isOK() ) {
298 wfDebugLog( 'fileupload', 'Download by URL completed successfully.' );
302 'Download by URL completed with HTTP status ' . $req->getStatus()