56 if ( !$performer->
isAllowed(
'upload_by_url' ) ) {
57 return 'upload_by_url';
60 return parent::isAllowed( $performer );
68 $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
70 return $allowCopyUploads && parent::isEnabled();
82 $domains = self::getAllowedHosts();
83 if ( !count( $domains ) ) {
91 foreach ( $domains as $domain ) {
93 $domainPieces = explode(
'.', $domain );
94 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
95 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
98 foreach ( $domainPieces as $index => $piece ) {
99 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
129 if ( !isset(
$params[
'filename'] ) || !isset(
$params[
'url'] ) ) {
135 return sha1( sprintf(
"%s|||%s",
$params[
'filename'],
$params[
'url'] ) );
142 private static function getAllowedHosts(): array {
144 $domains = $config->get( MainConfigNames::CopyUploadsDomains );
146 if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
147 $page =
wfMessage(
'copyupload-allowed-domains' )->inContentLanguage()->plain();
149 foreach ( explode(
"\n", $page ) as $line ) {
151 $line = preg_replace(
"/^\\s*([^#]*)\\s*((.*)?)$/",
"\\1", $line );
153 $line = trim( $line );
155 if ( $line !==
'' ) {
171 if ( !isset( self::$allowedUrls[$url] ) ) {
173 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
174 ->onIsUploadAllowedFromUrl( $url, $allowed );
175 self::$allowedUrls[$url] = $allowed;
178 return self::$allowedUrls[$url];
190 $tempPath = $this->makeTemporaryFile();
191 # File size and removeTempFile will be filled in later
192 $this->initializePathInfo( $name, $tempPath, 0,
false );
200 $desiredDestName = $request->getText(
'wpDestFile' );
201 if ( !$desiredDestName ) {
202 $desiredDestName = $request->getText(
'wpUploadFileURL' );
206 trim( $request->getVal(
'wpUploadFileURL' ) )
215 $user = RequestContext::getMain()->getUser();
217 $url = $request->getVal(
'wpUploadFileURL' );
220 && MediaWikiServices::getInstance()
221 ->getPermissionManager()
222 ->userHasRight( $user,
'upload_by_url' );
240 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
241 return Status::newFatal(
'http-invalid-url', $this->mUrl );
244 if ( !self::isAllowedHost( $this->mUrl ) ) {
245 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
247 if ( !self::isAllowedUrl( $this->mUrl ) ) {
248 return Status::newFatal(
'upload-copy-upload-invalid-url' );
250 return $this->reallyFetchFile( $httpOptions );
259 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
260 ->newTempFSFile(
'URL',
'urlupload_' );
261 $tmpFile->bind( $this );
263 return $tmpFile->getPath();
274 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
275 $nbytes = fwrite( $this->mTmpHandle, $buffer );
277 if ( $nbytes == strlen( $buffer ) ) {
278 $this->mFileSize += $nbytes;
283 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
284 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
286 fclose( $this->mTmpHandle );
287 $this->mTmpHandle =
false;
301 $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
302 $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
303 ->get( MainConfigNames::CopyUploadTimeout );
304 if ( $this->mTempPath ===
false ) {
305 return Status::newFatal(
'tmp-create-error' );
309 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
310 if ( !$this->mTmpHandle ) {
311 return Status::newFatal(
'tmp-create-error' );
313 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
315 $this->mRemoveTempFile =
true;
316 $this->mFileSize = 0;
318 $options = $httpOptions + [
'followRedirects' => false ];
320 if ( $copyUploadProxy !==
false ) {
321 $options[
'proxy'] = $copyUploadProxy;
324 if ( $copyUploadTimeout && !isset( $options[
'timeout'] ) ) {
325 $options[
'timeout'] = $copyUploadTimeout;
329 'Starting download from "' . $this->mUrl .
'" ' .
330 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
335 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
336 $targetUrl = $this->mUrl;
337 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
338 while ( $attemptsLeft > 0 ) {
339 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
340 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
341 $status = $req->execute();
342 if ( !$req->isRedirect() ) {
345 $targetUrl = $req->getFinalUrl();
347 ftruncate( $this->mTmpHandle, 0 );
348 rewind( $this->mTmpHandle );
352 if ( $attemptsLeft == 0 ) {
353 return Status::newFatal(
'upload-too-many-redirects' );
356 if ( $this->mTmpHandle ) {
358 fclose( $this->mTmpHandle );
359 $this->mTmpHandle =
null;
362 return Status::newFatal(
'tmp-write-error' );
366 if ( $status->isOK() ) {
367 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
370 wfDebugLog(
'fileupload', $status->getWikiText(
false,
false,
'en' ) );
374 '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.
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.
makeTemporaryFile()
Create a new temporary file in the URL subdirectory of wfTempDir().
static isValidRequest( $request)
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.
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.