55 if ( !$performer->
isAllowed(
'upload_by_url' ) ) {
56 return 'upload_by_url';
59 return parent::isAllowed( $performer );
67 $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
69 return $allowCopyUploads && parent::isEnabled();
81 $domains = self::getAllowedHosts();
82 if ( !count( $domains ) ) {
90 foreach ( $domains as $domain ) {
92 $domainPieces = explode(
'.', $domain );
93 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
94 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
97 foreach ( $domainPieces as $index => $piece ) {
98 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
121 private static function getAllowedHosts(): array {
123 $domains = $config->get( MainConfigNames::CopyUploadsDomains );
125 if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
126 $page =
wfMessage(
'copyupload-allowed-domains' )->inContentLanguage()->plain();
128 foreach ( explode(
"\n", $page ) as $line ) {
130 $line = preg_replace(
"/^\\s*([^#]*)\\s*((.*)?)$/",
"\\1", $line );
132 $line = trim( $line );
134 if ( $line !==
'' ) {
150 if ( !isset( self::$allowedUrls[$url] ) ) {
152 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
153 ->onIsUploadAllowedFromUrl( $url, $allowed );
154 self::$allowedUrls[$url] = $allowed;
157 return self::$allowedUrls[$url];
170 $tempPath = $this->makeTemporaryFile();
171 # File size and removeTempFile will be filled in later
172 $this->initializePathInfo( $name, $tempPath, 0,
false );
180 $desiredDestName = $request->getText(
'wpDestFile' );
181 if ( !$desiredDestName ) {
182 $desiredDestName = $request->getText(
'wpUploadFileURL' );
186 trim( $request->getVal(
'wpUploadFileURL' ) )
195 $user = RequestContext::getMain()->getUser();
197 $url = $request->getVal(
'wpUploadFileURL' );
200 && MediaWikiServices::getInstance()
201 ->getPermissionManager()
202 ->userHasRight( $user,
'upload_by_url' );
220 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
221 return Status::newFatal(
'http-invalid-url', $this->mUrl );
224 if ( !self::isAllowedHost( $this->mUrl ) ) {
225 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
227 if ( !self::isAllowedUrl( $this->mUrl ) ) {
228 return Status::newFatal(
'upload-copy-upload-invalid-url' );
230 return $this->reallyFetchFile( $httpOptions );
239 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
240 ->newTempFSFile(
'URL',
'urlupload_' );
241 $tmpFile->bind( $this );
243 return $tmpFile->getPath();
254 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
255 $nbytes = fwrite( $this->mTmpHandle, $buffer );
257 if ( $nbytes == strlen( $buffer ) ) {
258 $this->mFileSize += $nbytes;
263 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
264 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
266 fclose( $this->mTmpHandle );
267 $this->mTmpHandle =
false;
281 $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
282 $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
283 ->get( MainConfigNames::CopyUploadTimeout );
284 if ( $this->mTempPath ===
false ) {
285 return Status::newFatal(
'tmp-create-error' );
289 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
290 if ( !$this->mTmpHandle ) {
291 return Status::newFatal(
'tmp-create-error' );
293 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
295 $this->mRemoveTempFile =
true;
296 $this->mFileSize = 0;
298 $options = $httpOptions + [
'followRedirects' => false ];
300 if ( $copyUploadProxy !==
false ) {
301 $options[
'proxy'] = $copyUploadProxy;
304 if ( $copyUploadTimeout && !isset( $options[
'timeout'] ) ) {
305 $options[
'timeout'] = $copyUploadTimeout;
309 'Starting download from "' . $this->mUrl .
'" ' .
310 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
315 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
316 $targetUrl = $this->mUrl;
317 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
318 while ( $attemptsLeft > 0 ) {
319 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
320 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
321 $status = $req->execute();
322 if ( !$req->isRedirect() ) {
325 $targetUrl = $req->getFinalUrl();
327 ftruncate( $this->mTmpHandle, 0 );
328 rewind( $this->mTmpHandle );
332 if ( $attemptsLeft == 0 ) {
333 return Status::newFatal(
'upload-too-many-redirects' );
336 if ( $this->mTmpHandle ) {
338 fclose( $this->mTmpHandle );
339 $this->mTmpHandle =
null;
342 return Status::newFatal(
'tmp-write-error' );
346 if ( $status->isOK() ) {
347 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
350 wfDebugLog(
'fileupload', $status->getWikiText(
false,
false,
'en' ) );
354 '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.
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.
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.