60 if ( !$performer->
isAllowed(
'upload_by_url' ) ) {
61 return 'upload_by_url';
64 return parent::isAllowed( $performer );
72 $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
74 return $allowCopyUploads && parent::isEnabled();
86 $domains = self::getAllowedHosts();
87 if ( !count( $domains ) ) {
95 foreach ( $domains as $domain ) {
97 $domainPieces = explode(
'.', $domain );
98 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
99 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
102 foreach ( $domainPieces as $index => $piece ) {
103 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
133 if ( !isset(
$params[
'filename'] ) || !isset(
$params[
'url'] ) ) {
139 return sha1( sprintf(
"%s|||%s",
$params[
'filename'],
$params[
'url'] ) );
150 $uploadCacheKey = $request->getText(
'wpCacheKey', $request->getText(
'key',
'' ) );
151 if ( $uploadCacheKey !==
'' ) {
152 return $uploadCacheKey;
154 $desiredDestName = $request->getText(
'wpDestFile' );
155 if ( !$desiredDestName ) {
156 $desiredDestName = $request->getText(
'wpUploadFileURL' );
160 'filename' => $desiredDestName,
161 'url' => trim( $request->getVal(
'wpUploadFileURL' ) )
169 private static function getAllowedHosts(): array {
171 $domains = $config->get( MainConfigNames::CopyUploadsDomains );
173 if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
174 $page =
wfMessage(
'copyupload-allowed-domains' )->inContentLanguage()->plain();
176 foreach ( explode(
"\n", $page ) as $line ) {
178 $line = preg_replace(
"/^\\s*([^#]*)\\s*((.*)?)$/",
"\\1", $line );
180 $line = trim( $line );
182 if ( $line !==
'' ) {
198 if ( !isset( self::$allowedUrls[
$url] ) ) {
200 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
201 ->onIsUploadAllowedFromUrl(
$url, $allowed );
202 self::$allowedUrls[
$url] = $allowed;
205 return self::$allowedUrls[
$url];
225 $tempPath = $this->makeTemporaryFile();
226 # File size and removeTempFile will be filled in later
227 $this->initializePathInfo( $name, $tempPath, 0,
false );
235 $desiredDestName = $request->getText(
'wpDestFile' );
236 if ( !$desiredDestName ) {
237 $desiredDestName = $request->getText(
'wpUploadFileURL' );
241 trim( $request->getVal(
'wpUploadFileURL' ) )
250 $user = RequestContext::getMain()->getUser();
252 $url = $request->getVal(
'wpUploadFileURL' );
255 && MediaWikiServices::getInstance()
256 ->getPermissionManager()
257 ->userHasRight( $user,
'upload_by_url' );
275 $status = $this->canFetchFile();
276 if ( !$status->isGood() ) {
279 return $this->reallyFetchFile( $httpOptions );
288 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
289 return Status::newFatal(
'http-invalid-url', $this->mUrl );
292 if ( !self::isAllowedHost( $this->mUrl ) ) {
293 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
295 if ( !self::isAllowedUrl( $this->mUrl ) ) {
296 return Status::newFatal(
'upload-copy-upload-invalid-url' );
298 return Status::newGood();
307 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
308 ->newTempFSFile(
'URL',
'urlupload_' );
309 $tmpFile->bind( $this );
311 return $tmpFile->getPath();
322 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
323 $nbytes = fwrite( $this->mTmpHandle, $buffer );
325 if ( $nbytes == strlen( $buffer ) ) {
326 $this->mFileSize += $nbytes;
331 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
332 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
334 fclose( $this->mTmpHandle );
335 $this->mTmpHandle =
false;
349 $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
350 $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
351 ->get( MainConfigNames::CopyUploadTimeout );
352 if ( $this->mTempPath ===
false ) {
353 return Status::newFatal(
'tmp-create-error' );
357 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
358 if ( !$this->mTmpHandle ) {
359 return Status::newFatal(
'tmp-create-error' );
361 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
363 $this->mRemoveTempFile =
true;
364 $this->mFileSize = 0;
366 $options = $httpOptions + [
'followRedirects' => false ];
368 if ( $copyUploadProxy !==
false ) {
369 $options[
'proxy'] = $copyUploadProxy;
372 if ( $copyUploadTimeout && !isset( $options[
'timeout'] ) ) {
373 $options[
'timeout'] = $copyUploadTimeout;
377 'Starting download from "' . $this->mUrl .
'" ' .
378 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
383 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
384 $targetUrl = $this->mUrl;
385 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
386 while ( $attemptsLeft > 0 ) {
387 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
388 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
389 $status = $req->execute();
390 if ( !$req->isRedirect() ) {
393 $targetUrl = $req->getFinalUrl();
395 ftruncate( $this->mTmpHandle, 0 );
396 rewind( $this->mTmpHandle );
400 if ( $attemptsLeft == 0 ) {
401 return Status::newFatal(
'upload-too-many-redirects' );
404 if ( $this->mTmpHandle ) {
406 fclose( $this->mTmpHandle );
407 $this->mTmpHandle =
null;
410 return Status::newFatal(
'tmp-write-error' );
414 if ( $status->isOK() ) {
415 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
418 wfDebugLog(
'fileupload', $status->getWikiText(
false,
false,
'en' ) );
422 '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
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.