| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Context\RequestContext; |
| 25 |
use MediaWiki\HookContainer\HookRunner; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use MediaWiki\Permissions\Authority; |
| 29 |
use MediaWiki\Request\WebRequest; |
| 30 |
use MediaWiki\Status\Status; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class UploadFromUrl extends UploadBase { |
| 40 |
|
| 41 |
protected $mUrl; |
| 42 |
|
| 43 |
|
| 44 |
protected $mTmpHandle; |
| 45 |
|
| 46 |
|
| 47 |
protected static $allowedUrls = []; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static function isAllowed( Authority $performer ) { |
| 59 |
if ( !$performer->isAllowed( 'upload_by_url' ) ) { |
| 60 |
return 'upload_by_url'; |
| 61 |
} |
| 62 |
|
| 63 |
return parent::isAllowed( $performer ); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static function isEnabled() { |
| 71 |
$allowCopyUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::AllowCopyUploads ); |
| 72 |
|
| 73 |
return $allowCopyUploads && parent::isEnabled(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function isAllowedHost( $url ) { |
| 85 |
$domains = self::getAllowedHosts(); |
| 86 |
if ( !count( $domains ) ) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
$parsedUrl = wfGetUrlUtils()->parse( $url ); |
| 90 |
if ( !$parsedUrl ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
$valid = false; |
| 94 |
foreach ( $domains as $domain ) { |
| 95 |
|
| 96 |
$domainPieces = explode( '.', $domain ); |
| 97 |
$uploadDomainPieces = explode( '.', $parsedUrl['host'] ); |
| 98 |
if ( count( $domainPieces ) === count( $uploadDomainPieces ) ) { |
| 99 |
$valid = true; |
| 100 |
|
| 101 |
foreach ( $domainPieces as $index => $piece ) { |
| 102 |
if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) { |
| 103 |
$valid = false; |
| 104 |
} |
| 105 |
} |
| 106 |
if ( $valid ) { |
| 107 |
|
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
return $valid; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static function getCacheKey( $params ) { |
| 132 |
if ( !isset( $params['filename'] ) || !isset( $params['url'] ) ) { |
| 133 |
return ""; |
| 134 |
} else { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
return sha1( sprintf( "%s|||%s", $params['filename'], $params['url'] ) ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static function getCacheKeyFromRequest( &$request ) { |
| 149 |
$uploadCacheKey = $request->getText( 'wpCacheKey', $request->getText( 'key', '' ) ); |
| 150 |
if ( $uploadCacheKey !== '' ) { |
| 151 |
return $uploadCacheKey; |
| 152 |
} |
| 153 |
$desiredDestName = $request->getText( 'wpDestFile' ); |
| 154 |
if ( !$desiredDestName ) { |
| 155 |
$desiredDestName = $request->getText( 'wpUploadFileURL' ); |
| 156 |
} |
| 157 |
return self::getCacheKey( |
| 158 |
[ |
| 159 |
'filename' => $desiredDestName, |
| 160 |
'url' => trim( $request->getVal( 'wpUploadFileURL' ) ) |
| 161 |
] |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private static function getAllowedHosts(): array { |
| 169 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 170 |
$domains = $config->get( MainConfigNames::CopyUploadsDomains ); |
| 171 |
|
| 172 |
if ( $config->get( MainConfigNames::CopyUploadAllowOnWikiDomainConfig ) ) { |
| 173 |
$page = wfMessage( 'copyupload-allowed-domains' )->inContentLanguage()->plain(); |
| 174 |
|
| 175 |
foreach ( explode( "\n", $page ) as $line ) { |
| 176 |
|
| 177 |
$line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line ); |
| 178 |
|
| 179 |
$line = trim( $line ); |
| 180 |
|
| 181 |
if ( $line !== '' ) { |
| 182 |
$domains[] = $line; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $domains; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static function isAllowedUrl( $url ) { |
| 197 |
if ( !isset( self::$allowedUrls[$url] ) ) { |
| 198 |
$allowed = true; |
| 199 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 200 |
->onIsUploadAllowedFromUrl( $url, $allowed ); |
| 201 |
self::$allowedUrls[$url] = $allowed; |
| 202 |
} |
| 203 |
|
| 204 |
return self::$allowedUrls[$url]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function getUrl() { |
| 212 |
return $this->mUrl; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function initialize( $name, $url ) { |
| 222 |
$this->mUrl = $url; |
| 223 |
|
| 224 |
$tempPath = $this->makeTemporaryFile(); |
| 225 |
|
| 226 |
$this->initializePathInfo( $name, $tempPath, 0, false ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function initializeFromRequest( &$request ) { |
| 234 |
$desiredDestName = $request->getText( 'wpDestFile' ); |
| 235 |
if ( !$desiredDestName ) { |
| 236 |
$desiredDestName = $request->getText( 'wpUploadFileURL' ); |
| 237 |
} |
| 238 |
$this->initialize( |
| 239 |
$desiredDestName, |
| 240 |
trim( $request->getVal( 'wpUploadFileURL' ) ) |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static function isValidRequest( $request ) { |
| 249 |
$user = RequestContext::getMain()->getUser(); |
| 250 |
|
| 251 |
$url = $request->getVal( 'wpUploadFileURL' ); |
| 252 |
|
| 253 |
return $url |
| 254 |
&& MediaWikiServices::getInstance() |
| 255 |
->getPermissionManager() |
| 256 |
->userHasRight( $user, 'upload_by_url' ); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function getSourceType() { |
| 263 |
return 'url'; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public function fetchFile( $httpOptions = [] ) { |
| 274 |
$status = $this->canFetchFile(); |
| 275 |
if ( !$status->isGood() ) { |
| 276 |
return $status; |
| 277 |
} |
| 278 |
return $this->reallyFetchFile( $httpOptions ); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function canFetchFile() { |
| 287 |
return $this->canFetchFileFromUrl( $this->mUrl ); |
| 288 |
} |
| 289 |
|
| 290 |
private function canFetchFileFromUrl( string $url ): Status { |
| 291 |
if ( !MWHttpRequest::isValidURI( $url ) ) { |
| 292 |
return Status::newFatal( 'http-invalid-url', $url ); |
| 293 |
} |
| 294 |
if ( !self::isAllowedHost( $url ) ) { |
| 295 |
return Status::newFatal( 'upload-copy-upload-invalid-domain' ); |
| 296 |
} |
| 297 |
if ( !self::isAllowedUrl( $url ) ) { |
| 298 |
return Status::newFatal( 'upload-copy-upload-invalid-url' ); |
| 299 |
} |
| 300 |
return Status::newGood(); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
protected function makeTemporaryFile() { |
| 309 |
$tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory() |
| 310 |
->newTempFSFile( 'URL', 'urlupload_' ); |
| 311 |
$tmpFile->bind( $this ); |
| 312 |
|
| 313 |
return $tmpFile->getPath(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public function saveTempFileChunk( $req, $buffer ) { |
| 324 |
wfDebugLog( 'fileupload', 'Received chunk of ' . strlen( $buffer ) . ' bytes' ); |
| 325 |
$nbytes = fwrite( $this->mTmpHandle, $buffer ); |
| 326 |
|
| 327 |
if ( $nbytes == strlen( $buffer ) ) { |
| 328 |
$this->mFileSize += $nbytes; |
| 329 |
} else { |
| 330 |
|
| 331 |
wfDebugLog( |
| 332 |
'fileupload', |
| 333 |
'Short write ' . $nbytes . '/' . strlen( $buffer ) . |
| 334 |
' bytes, aborting with ' . $this->mFileSize . ' uploaded so far' |
| 335 |
); |
| 336 |
fclose( $this->mTmpHandle ); |
| 337 |
$this->mTmpHandle = false; |
| 338 |
} |
| 339 |
|
| 340 |
return $nbytes; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
protected function reallyFetchFile( $httpOptions = [] ) { |
| 351 |
$copyUploadProxy = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CopyUploadProxy ); |
| 352 |
$copyUploadTimeout = MediaWikiServices::getInstance()->getMainConfig() |
| 353 |
->get( MainConfigNames::CopyUploadTimeout ); |
| 354 |
|
| 355 |
|
| 356 |
$this->mTmpHandle = fopen( $this->mTempPath, 'wb' ); |
| 357 |
if ( !$this->mTmpHandle ) { |
| 358 |
return Status::newFatal( 'tmp-create-error' ); |
| 359 |
} |
| 360 |
wfDebugLog( 'fileupload', 'Temporary file created "' . $this->mTempPath . '"' ); |
| 361 |
|
| 362 |
$this->mRemoveTempFile = true; |
| 363 |
$this->mFileSize = 0; |
| 364 |
|
| 365 |
$options = $httpOptions + [ 'followRedirects' => false ]; |
| 366 |
|
| 367 |
if ( $copyUploadProxy !== false ) { |
| 368 |
$options['proxy'] = $copyUploadProxy; |
| 369 |
} |
| 370 |
|
| 371 |
if ( $copyUploadTimeout && !isset( $options['timeout'] ) ) { |
| 372 |
$options['timeout'] = $copyUploadTimeout; |
| 373 |
} |
| 374 |
wfDebugLog( |
| 375 |
'fileupload', |
| 376 |
'Starting download from "' . $this->mUrl . '" ' . |
| 377 |
'<' . implode( ',', array_keys( array_filter( $options ) ) ) . '>' |
| 378 |
); |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
$attemptsLeft = $options['maxRedirects'] ?? 5; |
| 383 |
$targetUrl = $this->mUrl; |
| 384 |
$requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory(); |
| 385 |
while ( $attemptsLeft > 0 ) { |
| 386 |
$req = $requestFactory->create( $targetUrl, $options, __METHOD__ ); |
| 387 |
$req->setCallback( [ $this, 'saveTempFileChunk' ] ); |
| 388 |
$status = $req->execute(); |
| 389 |
if ( !$req->isRedirect() ) { |
| 390 |
break; |
| 391 |
} |
| 392 |
$targetUrl = $req->getFinalUrl(); |
| 393 |
$redirectedUrlFetchable = $this->canFetchFileFromUrl( $targetUrl ); |
| 394 |
if ( !$redirectedUrlFetchable->isGood() ) { |
| 395 |
return $redirectedUrlFetchable; |
| 396 |
} |
| 397 |
|
| 398 |
ftruncate( $this->mTmpHandle, 0 ); |
| 399 |
rewind( $this->mTmpHandle ); |
| 400 |
$attemptsLeft--; |
| 401 |
} |
| 402 |
|
| 403 |
if ( $attemptsLeft == 0 ) { |
| 404 |
return Status::newFatal( 'upload-too-many-redirects' ); |
| 405 |
} |
| 406 |
|
| 407 |
if ( $this->mTmpHandle ) { |
| 408 |
|
| 409 |
fclose( $this->mTmpHandle ); |
| 410 |
$this->mTmpHandle = null; |
| 411 |
} else { |
| 412 |
|
| 413 |
return Status::newFatal( 'tmp-write-error' ); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
if ( $status->isOK() ) { |
| 418 |
wfDebugLog( 'fileupload', 'Download by URL completed successfully.' ); |
| 419 |
} else { |
| 420 |
|
| 421 |
wfDebugLog( 'fileupload', $status->getWikiText( false, false, 'en' ) ); |
| 422 |
wfDebugLog( |
| 423 |
'fileupload', |
| 424 |
|
| 425 |
'Download by URL completed with HTTP status ' . $req->getStatus() |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
return $status; |
| 431 |
} |
| 432 |
} |