47 protected static $allowedUrls = [];
59 if ( !$performer->
isAllowed(
'upload_by_url' ) ) {
60 return 'upload_by_url';
63 return parent::isAllowed( $performer );
71 $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
73 return $allowCopyUploads && parent::isEnabled();
85 $domains = self::getAllowedHosts();
86 if ( !count( $domains ) ) {
94 foreach ( $domains as $domain ) {
96 $domainPieces = explode(
'.', $domain );
97 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
98 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
101 foreach ( $domainPieces as $index => $piece ) {
102 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
132 if ( !isset(
$params[
'filename'] ) || !isset(
$params[
'url'] ) ) {
138 return sha1( sprintf(
"%s|||%s",
$params[
'filename'],
$params[
'url'] ) );
149 $uploadCacheKey = $request->getText(
'wpCacheKey', $request->getText(
'key',
'' ) );
150 if ( $uploadCacheKey !==
'' ) {
151 return $uploadCacheKey;
153 $desiredDestName = $request->getText(
'wpDestFile' );
154 if ( !$desiredDestName ) {
155 $desiredDestName = $request->getText(
'wpUploadFileURL' );
159 'filename' => $desiredDestName,
160 'url' => trim( $request->getVal(
'wpUploadFileURL' ) )
168 private static function getAllowedHosts(): array {
170 $domains = $config->get( MainConfigNames::CopyUploadsDomains );
172 if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
173 $page =
wfMessage(
'copyupload-allowed-domains' )->inContentLanguage()->plain();
175 foreach ( explode(
"\n", $page ) as $line ) {
177 $line = preg_replace(
"/^\\s*([^#]*)\\s*((.*)?)$/",
"\\1", $line );
179 $line = trim( $line );
181 if ( $line !==
'' ) {
197 if ( !isset( self::$allowedUrls[
$url] ) ) {
199 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
200 ->onIsUploadAllowedFromUrl(
$url, $allowed );
201 self::$allowedUrls[
$url] = $allowed;
204 return self::$allowedUrls[
$url];
224 $tempPath = $this->makeTemporaryFile();
225 # File size and removeTempFile will be filled in later
226 $this->initializePathInfo( $name, $tempPath, 0,
false );
234 $desiredDestName = $request->getText(
'wpDestFile' );
235 if ( !$desiredDestName ) {
236 $desiredDestName = $request->getText(
'wpUploadFileURL' );
240 trim( $request->getVal(
'wpUploadFileURL' ) )
249 $user = RequestContext::getMain()->getUser();
251 $url = $request->getVal(
'wpUploadFileURL' );
254 && MediaWikiServices::getInstance()
255 ->getPermissionManager()
256 ->userHasRight( $user,
'upload_by_url' );
274 $status = $this->canFetchFile();
275 if ( !$status->isGood() ) {
278 return $this->reallyFetchFile( $httpOptions );
287 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
288 return Status::newFatal(
'http-invalid-url', $this->mUrl );
291 if ( !self::isAllowedHost( $this->mUrl ) ) {
292 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
294 if ( !self::isAllowedUrl( $this->mUrl ) ) {
295 return Status::newFatal(
'upload-copy-upload-invalid-url' );
297 return Status::newGood();
306 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
307 ->newTempFSFile(
'URL',
'urlupload_' );
308 $tmpFile->bind( $this );
310 return $tmpFile->getPath();
321 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
322 $nbytes = fwrite( $this->mTmpHandle, $buffer );
324 if ( $nbytes == strlen( $buffer ) ) {
325 $this->mFileSize += $nbytes;
330 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
331 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
333 fclose( $this->mTmpHandle );
334 $this->mTmpHandle =
false;
348 $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
349 $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
350 ->get( MainConfigNames::CopyUploadTimeout );
353 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
354 if ( !$this->mTmpHandle ) {
355 return Status::newFatal(
'tmp-create-error' );
357 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
359 $this->mRemoveTempFile =
true;
360 $this->mFileSize = 0;
362 $options = $httpOptions + [
'followRedirects' => false ];
364 if ( $copyUploadProxy !==
false ) {
365 $options[
'proxy'] = $copyUploadProxy;
368 if ( $copyUploadTimeout && !isset( $options[
'timeout'] ) ) {
369 $options[
'timeout'] = $copyUploadTimeout;
373 'Starting download from "' . $this->mUrl .
'" ' .
374 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
379 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
380 $targetUrl = $this->mUrl;
381 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
382 while ( $attemptsLeft > 0 ) {
383 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
384 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
385 $status = $req->execute();
386 if ( !$req->isRedirect() ) {
389 $targetUrl = $req->getFinalUrl();
391 ftruncate( $this->mTmpHandle, 0 );
392 rewind( $this->mTmpHandle );
396 if ( $attemptsLeft == 0 ) {
397 return Status::newFatal(
'upload-too-many-redirects' );
400 if ( $this->mTmpHandle ) {
402 fclose( $this->mTmpHandle );
403 $this->mTmpHandle =
null;
406 return Status::newFatal(
'tmp-write-error' );
410 if ( $status->isOK() ) {
411 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
414 wfDebugLog(
'fileupload', $status->getWikiText(
false,
false,
'en' ) );
418 'Download by URL completed with HTTP status ' . $req->getStatus()
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
static array< string, bool > $allowedUrls
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...
resource null false $mTmpHandle
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.