51 if ( !MediaWikiServices::getInstance()
52 ->getPermissionManager()
53 ->userHasRight( $user,
'upload_by_url' )
55 return 'upload_by_url';
58 return parent::isAllowed( $user );
91 $whitelistedDomainPieces = explode(
'.', $domain );
92 $uploadDomainPieces = explode(
'.', $parsedUrl[
'host'] );
93 if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
96 foreach ( $whitelistedDomainPieces as $index => $piece ) {
97 if ( $piece !==
'*' && $piece !== $uploadDomainPieces[$index] ) {
124 if ( !isset( self::$allowedUrls[$url] ) ) {
127 self::$allowedUrls[$url] = $allowed;
130 return self::$allowedUrls[$url];
144 # File size and removeTempFile will be filled in later
153 $desiredDestName = $request->getText(
'wpDestFile' );
154 if ( !$desiredDestName ) {
155 $desiredDestName = $request->getText(
'wpUploadFileURL' );
159 trim( $request->getVal(
'wpUploadFileURL' ) )
170 $url = $request->getVal(
'wpUploadFileURL' );
172 return !empty( $url )
173 && MediaWikiServices::getInstance()
174 ->getPermissionManager()
175 ->userHasRight( $wgUser,
'upload_by_url' );
197 if ( !self::isAllowedHost( $this->mUrl ) ) {
200 if ( !self::isAllowedUrl( $this->mUrl ) ) {
212 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
213 ->newTempFSFile(
'URL',
'urlupload_' );
214 $tmpFile->bind( $this );
216 return $tmpFile->getPath();
227 wfDebugLog(
'fileupload',
'Received chunk of ' . strlen( $buffer ) .
' bytes' );
228 $nbytes = fwrite( $this->mTmpHandle, $buffer );
230 if ( $nbytes == strlen( $buffer ) ) {
231 $this->mFileSize += $nbytes;
236 'Short write ' . $nbytes .
'/' . strlen( $buffer ) .
237 ' bytes, aborting with ' . $this->mFileSize .
' uploaded so far'
239 fclose( $this->mTmpHandle );
240 $this->mTmpHandle =
false;
255 if ( $this->mTempPath ===
false ) {
260 $this->mTmpHandle = fopen( $this->mTempPath,
'wb' );
261 if ( !$this->mTmpHandle ) {
264 wfDebugLog(
'fileupload',
'Temporary file created "' . $this->mTempPath .
'"' );
266 $this->mRemoveTempFile =
true;
267 $this->mFileSize = 0;
269 $options = $httpOptions + [
'followRedirects' => false ];
280 'Starting download from "' . $this->mUrl .
'" ' .
281 '<' . implode(
',', array_keys( array_filter( $options ) ) ) .
'>'
286 $attemptsLeft = $options[
'maxRedirects'] ?? 5;
288 while ( $attemptsLeft > 0 ) {
290 $req->setCallback( [ $this,
'saveTempFileChunk' ] );
291 $status = $req->execute();
292 if ( !$req->isRedirect() ) {
295 $targetUrl = $req->getFinalUrl();
297 ftruncate( $this->mTmpHandle, 0 );
298 rewind( $this->mTmpHandle );
302 if ( $attemptsLeft == 0 ) {
306 if ( $this->mTmpHandle ) {
308 fclose( $this->mTmpHandle );
309 $this->mTmpHandle =
null;
316 if ( $status->isOK() ) {
317 wfDebugLog(
'fileupload',
'Download by URL completed successfully.' );
321 'Download by URL completed with HTTP status ' . $req->getStatus()