57 if ( !$performer->
isAllowed(
'upload_by_url' ) ) {
58 return 'upload_by_url';
61 return parent::isAllowed( $performer );
69 $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
71 return $allowCopyUploads && parent::isEnabled();
83 $domains = self::getAllowedHosts();
84 if ( !count( $domains ) ) {
92 foreach ( $domains as $domain ) {
94 $domainPieces = explode(
'.', $domain );
95 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
96 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
99 foreach ( $domainPieces as $index => $piece ) {
100 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
130 if ( !isset(
$params[
'filename'] ) || !isset(
$params[
'url'] ) ) {
136 return sha1( sprintf(
"%s|||%s",
$params[
'filename'],
$params[
'url'] ) );
147 $uploadCacheKey = $request->getText(
'wpCacheKey', $request->getText(
'key',
'' ) );
148 if ( $uploadCacheKey !==
'' ) {
149 return $uploadCacheKey;
151 $desiredDestName = $request->getText(
'wpDestFile' );
152 if ( !$desiredDestName ) {
153 $desiredDestName = $request->getText(
'wpUploadFileURL' );
157 'filename' => $desiredDestName,
158 'url' => trim( $request->getVal(
'wpUploadFileURL' ) )
166 private static function getAllowedHosts(): array {
168 $domains = $config->get( MainConfigNames::CopyUploadsDomains );
170 if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
171 $page =
wfMessage(
'copyupload-allowed-domains' )->inContentLanguage()->plain();
173 foreach ( explode(
"\n", $page ) as $line ) {
175 $line = preg_replace(
"/^\\s*([^#]*)\\s*((.*)?)$/",
"\\1", $line );
177 $line = trim( $line );
179 if ( $line !==
'' ) {
195 if ( !isset( self::$allowedUrls[$url] ) ) {
197 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
198 ->onIsUploadAllowedFromUrl( $url, $allowed );
199 self::$allowedUrls[$url] = $allowed;
202 return self::$allowedUrls[$url];
222 $tempPath = $this->makeTemporaryFile();
223 # File size and removeTempFile will be filled in later
224 $this->initializePathInfo( $name, $tempPath, 0,
false );
232 $desiredDestName = $request->getText(
'wpDestFile' );
233 if ( !$desiredDestName ) {
234 $desiredDestName = $request->getText(
'wpUploadFileURL' );
238 trim( $request->getVal(
'wpUploadFileURL' ) )
247 $user = RequestContext::getMain()->getUser();
249 $url = $request->getVal(
'wpUploadFileURL' );
252 && MediaWikiServices::getInstance()
253 ->getPermissionManager()
254 ->userHasRight( $user,
'upload_by_url' );
272 $status = $this->canFetchFile();
273 if ( !$status->isGood() ) {
276 return $this->reallyFetchFile( $httpOptions );
285 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
286 return Status::newFatal(
'http-invalid-url', $this->mUrl );
289 if ( !self::isAllowedHost( $this->mUrl ) ) {
290 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
292 if ( !self::isAllowedUrl( $this->mUrl ) ) {
293 return Status::newFatal(
'upload-copy-upload-invalid-url' );
295 return Status::newGood();
304 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
305 ->newTempFSFile(
'URL',
'urlupload_' );
306 $tmpFile->bind( $this );
308 return $tmpFile->getPath();
319 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
320 $nbytes = fwrite( $this->mTmpHandle, $buffer );
322 if ( $nbytes == strlen( $buffer ) ) {
323 $this->mFileSize += $nbytes;
328 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
329 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
331 fclose( $this->mTmpHandle );
332 $this->mTmpHandle =
false;
346 $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
347 $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
348 ->get( MainConfigNames::CopyUploadTimeout );
349 if ( $this->mTempPath ===
false ) {
350 return Status::newFatal(
'tmp-create-error' );
354 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
355 if ( !$this->mTmpHandle ) {
356 return Status::newFatal(
'tmp-create-error' );
358 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
360 $this->mRemoveTempFile =
true;
361 $this->mFileSize = 0;
363 $options = $httpOptions + [
'followRedirects' => false ];
365 if ( $copyUploadProxy !==
false ) {
366 $options[
'proxy'] = $copyUploadProxy;
369 if ( $copyUploadTimeout && !isset( $options[
'timeout'] ) ) {
370 $options[
'timeout'] = $copyUploadTimeout;
374 'Starting download from "' . $this->mUrl .
'" ' .
375 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
380 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
381 $targetUrl = $this->mUrl;
382 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
383 while ( $attemptsLeft > 0 ) {
384 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
385 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
386 $status = $req->execute();
387 if ( !$req->isRedirect() ) {
390 $targetUrl = $req->getFinalUrl();
392 ftruncate( $this->mTmpHandle, 0 );
393 rewind( $this->mTmpHandle );
397 if ( $attemptsLeft == 0 ) {
398 return Status::newFatal(
'upload-too-many-redirects' );
401 if ( $this->mTmpHandle ) {
403 fclose( $this->mTmpHandle );
404 $this->mTmpHandle =
null;
407 return Status::newFatal(
'tmp-write-error' );
411 if ( $status->isOK() ) {
412 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
415 wfDebugLog(
'fileupload', $status->getWikiText(
false,
false,
'en' ) );
419 'Download by URL completed with HTTP status ' . $req->getStatus()
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
getCacheKey()
Get the cache key used to store status.
Group all the pieces relevant to the context of a request into one instance.
A class containing constants representing the names of configuration variables.
UploadBase and subclasses are the backend of MediaWiki's file uploads.
Implements uploading from a HTTP resource.
canFetchFile()
verify we can actually download the file
makeTemporaryFile()
Create a new temporary file in the URL subdirectory of wfTempDir().
static isValidRequest( $request)
getUrl()
Get the URL of the file to be uploaded.
static isAllowed(Authority $performer)
Checks if the user is allowed to use the upload-by-URL feature.
initializeFromRequest(&$request)
Entry point for SpecialUpload.
reallyFetchFile( $httpOptions=[])
Download the file, save it to the temporary file and update the file size and set $mRemoveTempFile to...
initialize( $name, $url)
Entry point for API upload.
static getCacheKeyFromRequest(&$request)
Get the caching key from a web request.
fetchFile( $httpOptions=[])
Download the file.
static getCacheKey( $params)
Provides a caching key for an upload from url set of parameters Used to set the status of an async jo...
saveTempFileChunk( $req, $buffer)
Callback: save a chunk of the result of a HTTP request to the temporary file.
static isAllowedHost( $url)
Checks whether the URL is for an allowed host The domains in the allowlist can include wildcard chara...
static isAllowedUrl( $url)
Checks whether the URL is not allowed.
static isEnabled()
Checks if the upload from URL feature is enabled.