51 if ( !$performer->
isAllowed(
'upload_by_url' )
53 return 'upload_by_url';
56 return parent::isAllowed( $performer );
89 $domainPieces = explode(
'.', $domain );
90 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
91 if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
94 foreach ( $domainPieces as $index => $piece ) {
95 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
122 if ( !isset( self::$allowedUrls[$url] ) ) {
124 Hooks::runner()->onIsUploadAllowedFromUrl( $url, $allowed );
125 self::$allowedUrls[$url] = $allowed;
128 return self::$allowedUrls[$url];
142 # File size and removeTempFile will be filled in later
151 $desiredDestName = $request->getText(
'wpDestFile' );
152 if ( !$desiredDestName ) {
153 $desiredDestName = $request->getText(
'wpUploadFileURL' );
157 trim( $request->getVal(
'wpUploadFileURL' ) )
166 $user = RequestContext::getMain()->getUser();
168 $url = $request->getVal(
'wpUploadFileURL' );
170 return !empty( $url )
171 && MediaWikiServices::getInstance()
172 ->getPermissionManager()
173 ->userHasRight( $user,
'upload_by_url' );
191 if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
192 return Status::newFatal(
'http-invalid-url', $this->mUrl );
195 if ( !self::isAllowedHost( $this->mUrl ) ) {
196 return Status::newFatal(
'upload-copy-upload-invalid-domain' );
198 if ( !self::isAllowedUrl( $this->mUrl ) ) {
199 return Status::newFatal(
'upload-copy-upload-invalid-url' );
210 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
211 ->newTempFSFile(
'URL',
'urlupload_' );
212 $tmpFile->bind( $this );
214 return $tmpFile->getPath();
225 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
226 $nbytes = fwrite( $this->mTmpHandle, $buffer );
228 if ( $nbytes == strlen( $buffer ) ) {
229 $this->mFileSize += $nbytes;
234 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
235 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
237 fclose( $this->mTmpHandle );
238 $this->mTmpHandle =
false;
253 if ( $this->mTempPath ===
false ) {
254 return Status::newFatal(
'tmp-create-error' );
258 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
259 if ( !$this->mTmpHandle ) {
260 return Status::newFatal(
'tmp-create-error' );
262 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
264 $this->mRemoveTempFile =
true;
265 $this->mFileSize = 0;
267 $options = $httpOptions + [
'followRedirects' => false ];
278 'Starting download from "' . $this->mUrl .
'" ' .
279 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
284 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
286 $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
287 while ( $attemptsLeft > 0 ) {
288 $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
289 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
290 $status = $req->execute();
291 if ( !$req->isRedirect() ) {
294 $targetUrl = $req->getFinalUrl();
296 ftruncate( $this->mTmpHandle, 0 );
297 rewind( $this->mTmpHandle );
301 if ( $attemptsLeft == 0 ) {
302 return Status::newFatal(
'upload-too-many-redirects' );
305 if ( $this->mTmpHandle ) {
307 fclose( $this->mTmpHandle );
308 $this->mTmpHandle =
null;
311 return Status::newFatal(
'tmp-write-error' );
315 if ( $status->isOK() ) {
316 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
320 'Download by URL completed with HTTP status ' . $req->getStatus()
int bool $wgCopyUploadTimeout
Different timeout for upload by url This could be useful since when fetching large files,...
$wgCopyUploadsDomains
A list of domains copy uploads can come from.
$wgCopyUploadProxy
Proxy to use for copy upload requests.
$wgAllowCopyUploads
Allow for upload to be copied from an URL.
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.
UploadBase and subclasses are the backend of MediaWiki's file uploads.
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
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.