| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Context\RequestContext; |
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\User\UserIdentity; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
class UploadStash { |
| 59 |
|
| 60 |
public const KEY_FORMAT_REGEX = '/^[\w\-\.]+\.\w*$/'; |
| 61 |
private const MAX_US_PROPS_SIZE = 65535; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public $repo; |
| 70 |
|
| 71 |
|
| 72 |
protected $files = []; |
| 73 |
|
| 74 |
|
| 75 |
protected $fileMetadata = []; |
| 76 |
|
| 77 |
|
| 78 |
protected $fileProps = []; |
| 79 |
|
| 80 |
|
| 81 |
private $user; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function __construct( FileRepo $repo, ?UserIdentity $user = null ) { |
| 92 |
|
| 93 |
$this->repo = $repo; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
$this->user = $user ?? RequestContext::getMain()->getUser(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function getFile( $key, $noAuth = false ) { |
| 114 |
if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) { |
| 115 |
throw new UploadStashBadPathException( |
| 116 |
wfMessage( 'uploadstash-bad-path-bad-format', $key ) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
if ( !$noAuth && !$this->user->isRegistered() ) { |
| 121 |
throw new UploadStashNotLoggedInException( |
| 122 |
wfMessage( 'uploadstash-not-logged-in' ) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
if ( !isset( $this->fileMetadata[$key] ) ) { |
| 127 |
if ( !$this->fetchFileMetadata( $key ) ) { |
| 128 |
|
| 129 |
|
| 130 |
$this->fetchFileMetadata( $key, DB_PRIMARY ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( !isset( $this->fileMetadata[$key] ) ) { |
| 134 |
throw new UploadStashFileNotFoundException( |
| 135 |
wfMessage( 'uploadstash-file-not-found', $key ) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
$this->initFile( $key ); |
| 141 |
|
| 142 |
|
| 143 |
if ( |
| 144 |
isset( $this->fileMetadata[$key]['us_props'] ) && strlen( $this->fileMetadata[$key]['us_props'] ) |
| 145 |
) { |
| 146 |
$this->fileProps[$key] = unserialize( $this->fileMetadata[$key]['us_props'] ); |
| 147 |
} else { |
| 148 |
wfDebug( __METHOD__ . " fetched props for $key from file" ); |
| 149 |
$path = $this->fileMetadata[$key]['us_path']; |
| 150 |
$this->fileProps[$key] = $this->repo->getFileProps( $path ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if ( !$this->files[$key]->exists() ) { |
| 155 |
wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist" ); |
| 156 |
|
| 157 |
throw new UploadStashBadPathException( |
| 158 |
wfMessage( 'uploadstash-bad-path' ) |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
if ( !$noAuth && $this->fileMetadata[$key]['us_user'] != $this->user->getId() ) { |
| 163 |
throw new UploadStashWrongOwnerException( |
| 164 |
wfMessage( 'uploadstash-wrong-owner', $key ) |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
return $this->files[$key]; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function getMetadata( $key ) { |
| 178 |
$this->getFile( $key ); |
| 179 |
|
| 180 |
return $this->fileMetadata[$key]; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getFileProps( $key ) { |
| 190 |
$this->getFile( $key ); |
| 191 |
|
| 192 |
return $this->fileProps[$key]; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function stashFile( $path, $sourceType = null, $fileProps = null ) { |
| 209 |
if ( !is_file( $path ) ) { |
| 210 |
wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist" ); |
| 211 |
throw new UploadStashBadPathException( |
| 212 |
wfMessage( 'uploadstash-bad-path' ) |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if ( !$fileProps ) { |
| 218 |
$mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() ); |
| 219 |
$fileProps = $mwProps->getPropsFromPath( $path, true ); |
| 220 |
} |
| 221 |
wfDebug( __METHOD__ . " stashing file at '$path'" ); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$extension = self::getExtensionForPath( $path ); |
| 226 |
if ( !preg_match( "/\\.\\Q$extension\\E$/", $path ) ) { |
| 227 |
$pathWithGoodExtension = "$path.$extension"; |
| 228 |
} else { |
| 229 |
$pathWithGoodExtension = $path; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
[ $usec, $sec ] = explode( ' ', microtime() ); |
| 238 |
$usec = substr( $usec, 2 ); |
| 239 |
$key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) . '.' . |
| 240 |
Wikimedia\base_convert( (string)mt_rand(), 10, 36 ) . '.' . |
| 241 |
$this->user->getId() . '.' . |
| 242 |
$extension; |
| 243 |
|
| 244 |
$this->fileProps[$key] = $fileProps; |
| 245 |
|
| 246 |
if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) { |
| 247 |
throw new UploadStashBadPathException( |
| 248 |
wfMessage( 'uploadstash-bad-path-bad-format', $key ) |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
wfDebug( __METHOD__ . " key for '$path': $key" ); |
| 253 |
|
| 254 |
|
| 255 |
$storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ), $path ); |
| 256 |
|
| 257 |
if ( !$storeStatus->isOK() ) { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
foreach ( $storeStatus->getMessages( 'error' ) as $msg ) { |
| 267 |
throw new UploadStashFileException( $msg ); |
| 268 |
} |
| 269 |
foreach ( $storeStatus->getMessages( 'warning' ) as $msg ) { |
| 270 |
throw new UploadStashFileException( $msg ); |
| 271 |
} |
| 272 |
|
| 273 |
throw new UploadStashFileException( [ 'unknown', 'no error recorded' ] ); |
| 274 |
} |
| 275 |
$stashPath = $storeStatus->value; |
| 276 |
|
| 277 |
|
| 278 |
if ( !$this->user->isRegistered() ) { |
| 279 |
throw new UploadStashNotLoggedInException( |
| 280 |
wfMessage( 'uploadstash-not-logged-in' ) |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
wfDebug( __METHOD__ . " inserting $stashPath under $key" ); |
| 286 |
$dbw = $this->repo->getPrimaryDB(); |
| 287 |
|
| 288 |
$serializedFileProps = serialize( $fileProps ); |
| 289 |
if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
$fileProps['metadata'] = []; |
| 294 |
$serializedFileProps = serialize( $fileProps ); |
| 295 |
} |
| 296 |
|
| 297 |
$insertRow = [ |
| 298 |
'us_user' => $this->user->getId(), |
| 299 |
'us_key' => $key, |
| 300 |
'us_orig_path' => $path, |
| 301 |
'us_path' => $stashPath, |
| 302 |
'us_props' => $dbw->encodeBlob( $serializedFileProps ), |
| 303 |
'us_size' => $fileProps['size'], |
| 304 |
'us_sha1' => $fileProps['sha1'], |
| 305 |
'us_mime' => $fileProps['mime'], |
| 306 |
'us_media_type' => $fileProps['media_type'], |
| 307 |
'us_image_width' => $fileProps['width'], |
| 308 |
'us_image_height' => $fileProps['height'], |
| 309 |
'us_image_bits' => $fileProps['bits'], |
| 310 |
'us_source_type' => $sourceType, |
| 311 |
'us_timestamp' => $dbw->timestamp(), |
| 312 |
'us_status' => 'finished' |
| 313 |
]; |
| 314 |
|
| 315 |
$dbw->newInsertQueryBuilder() |
| 316 |
->insertInto( 'uploadstash' ) |
| 317 |
->row( $insertRow ) |
| 318 |
->caller( __METHOD__ )->execute(); |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
$insertRow['us_id'] = $dbw->insertId(); |
| 323 |
|
| 324 |
$this->fileMetadata[$key] = $insertRow; |
| 325 |
|
| 326 |
|
| 327 |
$this->initFile( $key ); |
| 328 |
|
| 329 |
return $this->getFile( $key ); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function clear() { |
| 340 |
if ( !$this->user->isRegistered() ) { |
| 341 |
throw new UploadStashNotLoggedInException( |
| 342 |
wfMessage( 'uploadstash-not-logged-in' ) |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
wfDebug( __METHOD__ . ' clearing all rows for user ' . $this->user->getId() ); |
| 347 |
$dbw = $this->repo->getPrimaryDB(); |
| 348 |
$dbw->newDeleteQueryBuilder() |
| 349 |
->deleteFrom( 'uploadstash' ) |
| 350 |
->where( [ 'us_user' => $this->user->getId() ] ) |
| 351 |
->caller( __METHOD__ )->execute(); |
| 352 |
|
| 353 |
|
| 354 |
$this->files = []; |
| 355 |
$this->fileMetadata = []; |
| 356 |
|
| 357 |
return true; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function removeFile( $key ) { |
| 369 |
if ( !$this->user->isRegistered() ) { |
| 370 |
throw new UploadStashNotLoggedInException( |
| 371 |
wfMessage( 'uploadstash-not-logged-in' ) |
| 372 |
); |
| 373 |
} |
| 374 |
|
| 375 |
$dbw = $this->repo->getPrimaryDB(); |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
$row = $dbw->newSelectQueryBuilder() |
| 380 |
->select( 'us_user' ) |
| 381 |
->from( 'uploadstash' ) |
| 382 |
->where( [ 'us_key' => $key ] ) |
| 383 |
->caller( __METHOD__ )->fetchRow(); |
| 384 |
|
| 385 |
if ( !$row ) { |
| 386 |
throw new UploadStashNoSuchKeyException( |
| 387 |
wfMessage( 'uploadstash-no-such-key', $key ) |
| 388 |
); |
| 389 |
} |
| 390 |
|
| 391 |
if ( $row->us_user != $this->user->getId() ) { |
| 392 |
throw new UploadStashWrongOwnerException( |
| 393 |
wfMessage( 'uploadstash-wrong-owner', $key ) |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
return $this->removeFileNoAuth( $key ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public function removeFileNoAuth( $key ) { |
| 407 |
wfDebug( __METHOD__ . " clearing row $key" ); |
| 408 |
|
| 409 |
|
| 410 |
$this->getFile( $key, true ); |
| 411 |
|
| 412 |
$dbw = $this->repo->getPrimaryDB(); |
| 413 |
|
| 414 |
$dbw->newDeleteQueryBuilder() |
| 415 |
->deleteFrom( 'uploadstash' ) |
| 416 |
->where( [ 'us_key' => $key ] ) |
| 417 |
->caller( __METHOD__ )->execute(); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
$this->files[$key]->remove(); |
| 423 |
|
| 424 |
unset( $this->files[$key] ); |
| 425 |
unset( $this->fileMetadata[$key] ); |
| 426 |
|
| 427 |
return true; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function listFiles() { |
| 437 |
if ( !$this->user->isRegistered() ) { |
| 438 |
throw new UploadStashNotLoggedInException( |
| 439 |
wfMessage( 'uploadstash-not-logged-in' ) |
| 440 |
); |
| 441 |
} |
| 442 |
|
| 443 |
$res = $this->repo->getReplicaDB()->newSelectQueryBuilder() |
| 444 |
->select( 'us_key' ) |
| 445 |
->from( 'uploadstash' ) |
| 446 |
->where( [ 'us_user' => $this->user->getId() ] ) |
| 447 |
->caller( __METHOD__ )->fetchResultSet(); |
| 448 |
|
| 449 |
if ( $res->numRows() == 0 ) { |
| 450 |
|
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
$keys = []; |
| 456 |
foreach ( $res as $row ) { |
| 457 |
$keys[] = $row->us_key; |
| 458 |
} |
| 459 |
|
| 460 |
return $keys; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public static function getExtensionForPath( $path ) { |
| 473 |
$prohibitedFileExtensions = MediaWikiServices::getInstance() |
| 474 |
->getMainConfig()->get( MainConfigNames::ProhibitedFileExtensions ); |
| 475 |
|
| 476 |
$n = strrpos( $path, '.' ); |
| 477 |
|
| 478 |
if ( $n !== false ) { |
| 479 |
$extension = $n ? substr( $path, $n + 1 ) : ''; |
| 480 |
} else { |
| 481 |
|
| 482 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 483 |
$mimeType = $magic->guessMimeType( $path ); |
| 484 |
$extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType ) ?? ''; |
| 485 |
} |
| 486 |
|
| 487 |
$extension = File::normalizeExtension( $extension ); |
| 488 |
if ( in_array( $extension, $prohibitedFileExtensions ) ) { |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
return ''; |
| 494 |
} |
| 495 |
|
| 496 |
return $extension; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
protected function fetchFileMetadata( $key, $readFromDB = DB_REPLICA ) { |
| 507 |
|
| 508 |
if ( $readFromDB === DB_PRIMARY ) { |
| 509 |
|
| 510 |
$dbr = $this->repo->getPrimaryDB(); |
| 511 |
} else { |
| 512 |
$dbr = $this->repo->getReplicaDB(); |
| 513 |
} |
| 514 |
|
| 515 |
$row = $dbr->newSelectQueryBuilder() |
| 516 |
->select( [ |
| 517 |
'us_user', 'us_key', 'us_orig_path', 'us_path', 'us_props', |
| 518 |
'us_size', 'us_sha1', 'us_mime', 'us_media_type', |
| 519 |
'us_image_width', 'us_image_height', 'us_image_bits', |
| 520 |
'us_source_type', 'us_timestamp', 'us_status', |
| 521 |
] ) |
| 522 |
->from( 'uploadstash' ) |
| 523 |
->where( [ 'us_key' => $key ] ) |
| 524 |
->caller( __METHOD__ )->fetchRow(); |
| 525 |
|
| 526 |
if ( !is_object( $row ) ) { |
| 527 |
|
| 528 |
return false; |
| 529 |
} |
| 530 |
|
| 531 |
$this->fileMetadata[$key] = (array)$row; |
| 532 |
$this->fileMetadata[$key]['us_props'] = $dbr->decodeBlob( $row->us_props ); |
| 533 |
|
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
protected function initFile( $key ) { |
| 545 |
$file = new UploadStashFile( |
| 546 |
$this->repo, |
| 547 |
$this->fileMetadata[$key]['us_path'], |
| 548 |
$key, |
| 549 |
$this->fileMetadata[$key]['us_sha1'] |
| 550 |
); |
| 551 |
if ( $file->getSize() === 0 ) { |
| 552 |
throw new UploadStashZeroLengthFileException( |
| 553 |
wfMessage( 'uploadstash-zero-length' ) |
| 554 |
); |
| 555 |
} |
| 556 |
$this->files[$key] = $file; |
| 557 |
|
| 558 |
return true; |
| 559 |
} |
| 560 |
} |