MediaWiki  master
UploadFromUrl.php
Go to the documentation of this file.
1 <?php
27 
35 class UploadFromUrl extends UploadBase {
36  protected $mUrl;
37 
38  protected $mTempPath, $mTmpHandle;
39 
40  protected static $allowedUrls = [];
41 
51  public static function isAllowed( Authority $performer ) {
52  if ( !$performer->isAllowed( 'upload_by_url' )
53  ) {
54  return 'upload_by_url';
55  }
56 
57  return parent::isAllowed( $performer );
58  }
59 
64  public static function isEnabled() {
65  $allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads );
66 
67  return $allowCopyUploads && parent::isEnabled();
68  }
69 
78  public static function isAllowedHost( $url ) {
79  $domains = self::getAllowedHosts();
80  if ( !count( $domains ) ) {
81  return true;
82  }
83  $parsedUrl = wfParseUrl( $url );
84  if ( !$parsedUrl ) {
85  return false;
86  }
87  $valid = false;
88  foreach ( $domains as $domain ) {
89  // See if the domain for the upload matches this allowed domain
90  $domainPieces = explode( '.', $domain );
91  $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
92  if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) {
93  $valid = true;
94  // See if all the pieces match or not (excluding wildcards)
95  foreach ( $domainPieces as $index => $piece ) {
96  if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
97  $valid = false;
98  }
99  }
100  if ( $valid ) {
101  // We found a match, so quit comparing against the list
102  break;
103  }
104  }
105  /* Non-wildcard test
106  if ( $parsedUrl['host'] === $domain ) {
107  $valid = true;
108  break;
109  }
110  */
111  }
112 
113  return $valid;
114  }
115 
119  private static function getAllowedHosts(): array {
120  $config = MediaWikiServices::getInstance()->getMainConfig();
121  $domains = $config->get( MainConfigNames::CopyUploadsDomains );
122 
123  if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) {
124  $page = wfMessage( 'copyupload-allowed-domains' )->inContentLanguage()->plain();
125 
126  foreach ( explode( "\n", $page ) as $line ) {
127  // Strip comments
128  $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
129  // Trim whitespace
130  $line = trim( $line );
131 
132  if ( $line !== '' ) {
133  $domains[] = $line;
134  }
135  }
136  }
137 
138  return $domains;
139  }
140 
147  public static function isAllowedUrl( $url ) {
148  if ( !isset( self::$allowedUrls[$url] ) ) {
149  $allowed = true;
150  Hooks::runner()->onIsUploadAllowedFromUrl( $url, $allowed );
151  self::$allowedUrls[$url] = $allowed;
152  }
153 
154  return self::$allowedUrls[$url];
155  }
156 
164  public function initialize( $name, $url ) {
165  $this->mUrl = $url;
166 
167  $tempPath = $this->makeTemporaryFile();
168  # File size and removeTempFile will be filled in later
169  $this->initializePathInfo( $name, $tempPath, 0, false );
170  }
171 
176  public function initializeFromRequest( &$request ) {
177  $desiredDestName = $request->getText( 'wpDestFile' );
178  if ( !$desiredDestName ) {
179  $desiredDestName = $request->getText( 'wpUploadFileURL' );
180  }
181  $this->initialize(
182  $desiredDestName,
183  trim( $request->getVal( 'wpUploadFileURL' ) )
184  );
185  }
186 
191  public static function isValidRequest( $request ) {
192  $user = RequestContext::getMain()->getUser();
193 
194  $url = $request->getVal( 'wpUploadFileURL' );
195 
196  return !empty( $url )
197  && MediaWikiServices::getInstance()
198  ->getPermissionManager()
199  ->userHasRight( $user, 'upload_by_url' );
200  }
201 
205  public function getSourceType() {
206  return 'url';
207  }
208 
216  public function fetchFile( $httpOptions = [] ) {
217  if ( !MWHttpRequest::isValidURI( $this->mUrl ) ) {
218  return Status::newFatal( 'http-invalid-url', $this->mUrl );
219  }
220 
221  if ( !self::isAllowedHost( $this->mUrl ) ) {
222  return Status::newFatal( 'upload-copy-upload-invalid-domain' );
223  }
224  if ( !self::isAllowedUrl( $this->mUrl ) ) {
225  return Status::newFatal( 'upload-copy-upload-invalid-url' );
226  }
227  return $this->reallyFetchFile( $httpOptions );
228  }
229 
235  protected function makeTemporaryFile() {
236  $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
237  ->newTempFSFile( 'URL', 'urlupload_' );
238  $tmpFile->bind( $this );
239 
240  return $tmpFile->getPath();
241  }
242 
250  public function saveTempFileChunk( $req, $buffer ) {
251  wfDebugLog( 'fileupload', 'Received chunk of ' . strlen( $buffer ) . ' bytes' );
252  $nbytes = fwrite( $this->mTmpHandle, $buffer );
253 
254  if ( $nbytes == strlen( $buffer ) ) {
255  $this->mFileSize += $nbytes;
256  } else {
257  // Well... that's not good!
258  wfDebugLog(
259  'fileupload',
260  'Short write ' . $nbytes . '/' . strlen( $buffer ) .
261  ' bytes, aborting with ' . $this->mFileSize . ' uploaded so far'
262  );
263  fclose( $this->mTmpHandle );
264  $this->mTmpHandle = false;
265  }
266 
267  return $nbytes;
268  }
269 
277  protected function reallyFetchFile( $httpOptions = [] ) {
278  $copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy );
279  $copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig()
280  ->get( MainConfigNames::CopyUploadTimeout );
281  if ( $this->mTempPath === false ) {
282  return Status::newFatal( 'tmp-create-error' );
283  }
284 
285  // Note the temporary file should already be created by makeTemporaryFile()
286  $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
287  if ( !$this->mTmpHandle ) {
288  return Status::newFatal( 'tmp-create-error' );
289  }
290  wfDebugLog( 'fileupload', 'Temporary file created "' . $this->mTempPath . '"' );
291 
292  $this->mRemoveTempFile = true;
293  $this->mFileSize = 0;
294 
295  $options = $httpOptions + [ 'followRedirects' => false ];
296 
297  if ( $copyUploadProxy !== false ) {
298  $options['proxy'] = $copyUploadProxy;
299  }
300 
301  if ( $copyUploadTimeout && !isset( $options['timeout'] ) ) {
302  $options['timeout'] = $copyUploadTimeout;
303  }
304  wfDebugLog(
305  'fileupload',
306  'Starting download from "' . $this->mUrl . '" ' .
307  '<' . implode( ',', array_keys( array_filter( $options ) ) ) . '>'
308  );
309 
310  // Manually follow any redirects up to the limit and reset the output file before each new request to prevent
311  // capturing the redirect response as part of the file.
312  $attemptsLeft = $options['maxRedirects'] ?? 5;
313  $targetUrl = $this->mUrl;
314  $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
315  while ( $attemptsLeft > 0 ) {
316  $req = $requestFactory->create( $targetUrl, $options, __METHOD__ );
317  $req->setCallback( [ $this, 'saveTempFileChunk' ] );
318  $status = $req->execute();
319  if ( !$req->isRedirect() ) {
320  break;
321  }
322  $targetUrl = $req->getFinalUrl();
323  // Remove redirect response content from file.
324  ftruncate( $this->mTmpHandle, 0 );
325  rewind( $this->mTmpHandle );
326  $attemptsLeft--;
327  }
328 
329  if ( $attemptsLeft == 0 ) {
330  return Status::newFatal( 'upload-too-many-redirects' );
331  }
332 
333  if ( $this->mTmpHandle ) {
334  // File got written ok...
335  fclose( $this->mTmpHandle );
336  $this->mTmpHandle = null;
337  } else {
338  // We encountered a write error during the download...
339  return Status::newFatal( 'tmp-write-error' );
340  }
341 
342  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable Always set after loop
343  if ( $status->isOK() ) {
344  wfDebugLog( 'fileupload', 'Download by URL completed successfully.' );
345  } else {
346  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable Always set after loop
347  wfDebugLog( 'fileupload', $status->getWikitext( false, false, 'en' ) );
348  wfDebugLog(
349  'fileupload',
350  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable Always set after loop
351  'Download by URL completed with HTTP status ' . $req->getStatus()
352  );
353  }
354 
355  // @phan-suppress-next-line PhanTypeMismatchReturnNullable,PhanPossiblyUndeclaredVariable Always set after loop
356  return $status;
357  }
358 }
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.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
static isValidURI( $uri)
Check that the given URI is a valid one.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getMain()
Get the RequestContext object associated with the main request.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
UploadBase and subclasses are the backend of MediaWiki's file uploads.
Definition: UploadBase.php:49
Implements uploading from a HTTP resource.
makeTemporaryFile()
Create a new temporary file in the URL subdirectory of wfTempDir().
static $allowedUrls
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.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
isAllowed(string $permission)
Checks whether this authority has the given permission in general.
$line
Definition: mcc.php:119