MediaWiki  master
UploadBase.php
Go to the documentation of this file.
00001 <?php
00040 abstract class UploadBase {
00041         protected $mTempPath;
00042         protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
00043         protected $mTitle = false, $mTitleError = 0;
00044         protected $mFilteredName, $mFinalExtension;
00045         protected $mLocalFile, $mFileSize, $mFileProps;
00046         protected $mBlackListedExtensions;
00047         protected $mJavaDetected;
00048 
00049         const SUCCESS = 0;
00050         const OK = 0;
00051         const EMPTY_FILE = 3;
00052         const MIN_LENGTH_PARTNAME = 4;
00053         const ILLEGAL_FILENAME = 5;
00054         const OVERWRITE_EXISTING_FILE = 7; # Not used anymore; handled by verifyTitlePermissions()
00055         const FILETYPE_MISSING = 8;
00056         const FILETYPE_BADTYPE = 9;
00057         const VERIFICATION_ERROR = 10;
00058 
00059         # HOOK_ABORTED is the new name of UPLOAD_VERIFICATION_ERROR
00060         const UPLOAD_VERIFICATION_ERROR = 11;
00061         const HOOK_ABORTED = 11;
00062         const FILE_TOO_LARGE = 12;
00063         const WINDOWS_NONASCII_FILENAME = 13;
00064         const FILENAME_TOO_LONG = 14;
00065 
00070         public function getVerificationErrorCode( $error ) {
00071                 $code_to_status = array(self::EMPTY_FILE => 'empty-file',
00072                                                                 self::FILE_TOO_LARGE => 'file-too-large',
00073                                                                 self::FILETYPE_MISSING => 'filetype-missing',
00074                                                                 self::FILETYPE_BADTYPE => 'filetype-banned',
00075                                                                 self::MIN_LENGTH_PARTNAME => 'filename-tooshort',
00076                                                                 self::ILLEGAL_FILENAME => 'illegal-filename',
00077                                                                 self::OVERWRITE_EXISTING_FILE => 'overwrite',
00078                                                                 self::VERIFICATION_ERROR => 'verification-error',
00079                                                                 self::HOOK_ABORTED =>  'hookaborted',
00080                                                                 self::WINDOWS_NONASCII_FILENAME => 'windows-nonascii-filename',
00081                                                                 self::FILENAME_TOO_LONG => 'filename-toolong',
00082                 );
00083                 if( isset( $code_to_status[$error] ) ) {
00084                         return $code_to_status[$error];
00085                 }
00086 
00087                 return 'unknown-error';
00088         }
00089 
00095         public static function isEnabled() {
00096                 global $wgEnableUploads;
00097 
00098                 if ( !$wgEnableUploads ) {
00099                         return false;
00100                 }
00101 
00102                 # Check php's file_uploads setting
00103                 return wfIsHipHop() || wfIniGetBool( 'file_uploads' );
00104         }
00105 
00114         public static function isAllowed( $user ) {
00115                 foreach ( array( 'upload', 'edit' ) as $permission ) {
00116                         if ( !$user->isAllowed( $permission ) ) {
00117                                 return $permission;
00118                         }
00119                 }
00120                 return true;
00121         }
00122 
00123         // Upload handlers. Should probably just be a global.
00124         static $uploadHandlers = array( 'Stash', 'File', 'Url' );
00125 
00133         public static function createFromRequest( &$request, $type = null ) {
00134                 $type = $type ? $type : $request->getVal( 'wpSourceType', 'File' );
00135 
00136                 if( !$type ) {
00137                         return null;
00138                 }
00139 
00140                 // Get the upload class
00141                 $type = ucfirst( $type );
00142 
00143                 // Give hooks the chance to handle this request
00144                 $className = null;
00145                 wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
00146                 if ( is_null( $className ) ) {
00147                         $className = 'UploadFrom' . $type;
00148                         wfDebug( __METHOD__ . ": class name: $className\n" );
00149                         if( !in_array( $type, self::$uploadHandlers ) ) {
00150                                 return null;
00151                         }
00152                 }
00153 
00154                 // Check whether this upload class is enabled
00155                 if( !call_user_func( array( $className, 'isEnabled' ) ) ) {
00156                         return null;
00157                 }
00158 
00159                 // Check whether the request is valid
00160                 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) ) {
00161                         return null;
00162                 }
00163 
00164                 $handler = new $className;
00165 
00166                 $handler->initializeFromRequest( $request );
00167                 return $handler;
00168         }
00169 
00175         public static function isValidRequest( $request ) {
00176                 return false;
00177         }
00178 
00179         public function __construct() {}
00180 
00187         public function getSourceType() { return null; }
00188 
00197         public function initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile = false ) {
00198                 $this->mDesiredDestName = $name;
00199                 if ( FileBackend::isStoragePath( $tempPath ) ) {
00200                         throw new MWException( __METHOD__ . " given storage path `$tempPath`." );
00201                 }
00202                 $this->mTempPath = $tempPath;
00203                 $this->mFileSize = $fileSize;
00204                 $this->mRemoveTempFile = $removeTempFile;
00205         }
00206 
00210         public abstract function initializeFromRequest( &$request );
00211 
00216         public function fetchFile() {
00217                 return Status::newGood();
00218         }
00219 
00224         public function isEmptyFile() {
00225                 return empty( $this->mFileSize );
00226         }
00227 
00232         public function getFileSize() {
00233                 return $this->mFileSize;
00234         }
00235 
00240         function getRealPath( $srcPath ) {
00241                 wfProfileIn( __METHOD__ );
00242                 $repo = RepoGroup::singleton()->getLocalRepo();
00243                 if ( $repo->isVirtualUrl( $srcPath ) ) {
00244                         // @TODO: just make uploads work with storage paths
00245                         // UploadFromStash loads files via virtuals URLs
00246                         $tmpFile = $repo->getLocalCopy( $srcPath );
00247                         $tmpFile->bind( $this ); // keep alive with $thumb
00248                         wfProfileOut( __METHOD__ );
00249                         return $tmpFile->getPath();
00250                 }
00251                 wfProfileOut( __METHOD__ );
00252                 return $srcPath;
00253         }
00254 
00259         public function verifyUpload() {
00260                 wfProfileIn( __METHOD__ );
00261 
00265                 if( $this->isEmptyFile() ) {
00266                         wfProfileOut( __METHOD__ );
00267                         return array( 'status' => self::EMPTY_FILE );
00268                 }
00269 
00273                 $maxSize = self::getMaxUploadSize( $this->getSourceType() );
00274                 if( $this->mFileSize > $maxSize ) {
00275                         wfProfileOut( __METHOD__ );
00276                         return array(
00277                                 'status' => self::FILE_TOO_LARGE,
00278                                 'max' => $maxSize,
00279                         );
00280                 }
00281 
00287                 $verification = $this->verifyFile();
00288                 if( $verification !== true ) {
00289                         wfProfileOut( __METHOD__ );
00290                         return array(
00291                                 'status' => self::VERIFICATION_ERROR,
00292                                 'details' => $verification
00293                         );
00294                 }
00295 
00299                 $result = $this->validateName();
00300                 if( $result !== true ) {
00301                         wfProfileOut( __METHOD__ );
00302                         return $result;
00303                 }
00304 
00305                 $error = '';
00306                 if( !wfRunHooks( 'UploadVerification',
00307                         array( $this->mDestName, $this->mTempPath, &$error ) ) )
00308                 {
00309                         wfProfileOut( __METHOD__ );
00310                         return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
00311                 }
00312 
00313                 wfProfileOut( __METHOD__ );
00314                 return array( 'status' => self::OK );
00315         }
00316 
00323         protected function validateName() {
00324                 $nt = $this->getTitle();
00325                 if( is_null( $nt ) ) {
00326                         $result = array( 'status' => $this->mTitleError );
00327                         if( $this->mTitleError == self::ILLEGAL_FILENAME ) {
00328                                 $result['filtered'] = $this->mFilteredName;
00329                         }
00330                         if ( $this->mTitleError == self::FILETYPE_BADTYPE ) {
00331                                 $result['finalExt'] = $this->mFinalExtension;
00332                                 if ( count( $this->mBlackListedExtensions ) ) {
00333                                         $result['blacklistedExt'] = $this->mBlackListedExtensions;
00334                                 }
00335                         }
00336                         return $result;
00337                 }
00338                 $this->mDestName = $this->getLocalFile()->getName();
00339 
00340                 return true;
00341         }
00342 
00349         protected function verifyMimeType( $mime ) {
00350                 global $wgVerifyMimeType;
00351                 wfProfileIn( __METHOD__ );
00352                 if ( $wgVerifyMimeType ) {
00353                         wfDebug ( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n");
00354                         global $wgMimeTypeBlacklist;
00355                         if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
00356                                 wfProfileOut( __METHOD__ );
00357                                 return array( 'filetype-badmime', $mime );
00358                         }
00359 
00360                         # XXX: Missing extension will be caught by validateName() via getTitle()
00361                         if ( $this->mFinalExtension != '' && !$this->verifyExtension( $mime, $this->mFinalExtension ) ) {
00362                                 wfProfileOut( __METHOD__ );
00363                                 return array( 'filetype-mime-mismatch', $this->mFinalExtension, $mime );
00364                         }
00365 
00366                         # Check IE type
00367                         $fp = fopen( $this->mTempPath, 'rb' );
00368                         $chunk = fread( $fp, 256 );
00369                         fclose( $fp );
00370 
00371                         $magic = MimeMagic::singleton();
00372                         $extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
00373                         $ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
00374                         foreach ( $ieTypes as $ieType ) {
00375                                 if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
00376                                         wfProfileOut( __METHOD__ );
00377                                         return array( 'filetype-bad-ie-mime', $ieType );
00378                                 }
00379                         }
00380                 }
00381 
00382                 wfProfileOut( __METHOD__ );
00383                 return true;
00384         }
00385 
00391         protected function verifyFile() {
00392                 global $wgAllowJavaUploads, $wgDisableUploadScriptChecks;
00393                 wfProfileIn( __METHOD__ );
00394 
00395                 # get the title, even though we are doing nothing with it, because
00396                 # we need to populate mFinalExtension
00397                 $this->getTitle();
00398 
00399                 $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
00400 
00401                 # check mime type, if desired
00402                 $mime = $this->mFileProps[ 'file-mime' ];
00403                 $status = $this->verifyMimeType( $mime );
00404                 if ( $status !== true ) {
00405                         wfProfileOut( __METHOD__ );
00406                         return $status;
00407                 }
00408 
00409                 # check for htmlish code and javascript
00410                 if ( !$wgDisableUploadScriptChecks ) {
00411                         if( self::detectScript( $this->mTempPath, $mime, $this->mFinalExtension ) ) {
00412                                 wfProfileOut( __METHOD__ );
00413                                 return array( 'uploadscripted' );
00414                         }
00415                         if( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
00416                                 if( $this->detectScriptInSvg( $this->mTempPath ) ) {
00417                                         wfProfileOut( __METHOD__ );
00418                                         return array( 'uploadscripted' );
00419                                 }
00420                         }
00421                 }
00422 
00423                 # Check for Java applets, which if uploaded can bypass cross-site
00424                 # restrictions.
00425                 if ( !$wgAllowJavaUploads ) {
00426                         $this->mJavaDetected = false;
00427                         $zipStatus = ZipDirectoryReader::read( $this->mTempPath,
00428                                 array( $this, 'zipEntryCallback' ) );
00429                         if ( !$zipStatus->isOK() ) {
00430                                 $errors = $zipStatus->getErrorsArray();
00431                                 $error = reset( $errors );
00432                                 if ( $error[0] !== 'zip-wrong-format' ) {
00433                                         wfProfileOut( __METHOD__ );
00434                                         return $error;
00435                                 }
00436                         }
00437                         if ( $this->mJavaDetected ) {
00438                                 wfProfileOut( __METHOD__ );
00439                                 return array( 'uploadjava' );
00440                         }
00441                 }
00442 
00443                 # Scan the uploaded file for viruses
00444                 $virus = $this->detectVirus( $this->mTempPath );
00445                 if ( $virus ) {
00446                         wfProfileOut( __METHOD__ );
00447                         return array( 'uploadvirus', $virus );
00448                 }
00449 
00450                 $handler = MediaHandler::getHandler( $mime );
00451                 if ( $handler ) {
00452                         $handlerStatus = $handler->verifyUpload( $this->mTempPath );
00453                         if ( !$handlerStatus->isOK() ) {
00454                                 $errors = $handlerStatus->getErrorsArray();
00455                                 wfProfileOut( __METHOD__ );
00456                                 return reset( $errors );
00457                         }
00458                 }
00459 
00460                 wfRunHooks( 'UploadVerifyFile', array( $this, $mime, &$status ) );
00461                 if ( $status !== true ) {
00462                         wfProfileOut( __METHOD__ );
00463                         return $status;
00464                 }
00465 
00466                 wfDebug( __METHOD__ . ": all clear; passing.\n" );
00467                 wfProfileOut( __METHOD__ );
00468                 return true;
00469         }
00470 
00474         function zipEntryCallback( $entry ) {
00475                 $names = array( $entry['name'] );
00476 
00477                 // If there is a null character, cut off the name at it, because JDK's
00478                 // ZIP_GetEntry() uses strcmp() if the name hashes match. If a file name
00479                 // were constructed which had ".class\0" followed by a string chosen to
00480                 // make the hash collide with the truncated name, that file could be
00481                 // returned in response to a request for the .class file.
00482                 $nullPos = strpos( $entry['name'], "\000" );
00483                 if ( $nullPos !== false ) {
00484                         $names[] = substr( $entry['name'], 0, $nullPos );
00485                 }
00486 
00487                 // If there is a trailing slash in the file name, we have to strip it,
00488                 // because that's what ZIP_GetEntry() does.
00489                 if ( preg_grep( '!\.class/?$!', $names ) ) {
00490                         $this->mJavaDetected = true;
00491                 }
00492         }
00493 
00501         public function verifyPermissions( $user ) {
00502                 return $this->verifyTitlePermissions( $user );
00503         }
00504 
00516         public function verifyTitlePermissions( $user ) {
00521                 $nt = $this->getTitle();
00522                 if( is_null( $nt ) ) {
00523                         return true;
00524                 }
00525                 $permErrors = $nt->getUserPermissionsErrors( 'edit', $user );
00526                 $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $user );
00527                 if ( !$nt->exists() ) {
00528                         $permErrorsCreate = $nt->getUserPermissionsErrors( 'create', $user );
00529                 } else {
00530                         $permErrorsCreate = array();
00531                 }
00532                 if( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
00533                         $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
00534                         $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
00535                         return $permErrors;
00536                 }
00537 
00538                 $overwriteError = $this->checkOverwrite( $user );
00539                 if ( $overwriteError !== true ) {
00540                         return array( $overwriteError );
00541                 }
00542 
00543                 return true;
00544         }
00545 
00551         public function checkWarnings() {
00552                 global $wgLang;
00553                 wfProfileIn( __METHOD__ );
00554 
00555                 $warnings = array();
00556 
00557                 $localFile = $this->getLocalFile();
00558                 $filename = $localFile->getName();
00559 
00564                 $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
00565                 $comparableName = Title::capitalize( $comparableName, NS_FILE );
00566 
00567                 if( $this->mDesiredDestName != $filename && $comparableName != $filename ) {
00568                         $warnings['badfilename'] = $filename;
00569                 }
00570 
00571                 // Check whether the file extension is on the unwanted list
00572                 global $wgCheckFileExtensions, $wgFileExtensions;
00573                 if ( $wgCheckFileExtensions ) {
00574                         if ( !$this->checkFileExtension( $this->mFinalExtension, $wgFileExtensions ) ) {
00575                                 $warnings['filetype-unwanted-type'] = array( $this->mFinalExtension,
00576                                         $wgLang->commaList( $wgFileExtensions ), count( $wgFileExtensions ) );
00577                         }
00578                 }
00579 
00580                 global $wgUploadSizeWarning;
00581                 if ( $wgUploadSizeWarning && ( $this->mFileSize > $wgUploadSizeWarning ) ) {
00582                         $warnings['large-file'] = $wgUploadSizeWarning;
00583                 }
00584 
00585                 if ( $this->mFileSize == 0 ) {
00586                         $warnings['emptyfile'] = true;
00587                 }
00588 
00589                 $exists = self::getExistsWarning( $localFile );
00590                 if( $exists !== false ) {
00591                         $warnings['exists'] = $exists;
00592                 }
00593 
00594                 // Check dupes against existing files
00595                 $hash = FSFile::getSha1Base36FromPath( $this->mTempPath );
00596                 $dupes = RepoGroup::singleton()->findBySha1( $hash );
00597                 $title = $this->getTitle();
00598                 // Remove all matches against self
00599                 foreach ( $dupes as $key => $dupe ) {
00600                         if( $title->equals( $dupe->getTitle() ) ) {
00601                                 unset( $dupes[$key] );
00602                         }
00603                 }
00604                 if( $dupes ) {
00605                         $warnings['duplicate'] = $dupes;
00606                 }
00607 
00608                 // Check dupes against archives
00609                 $archivedImage = new ArchivedFile( null, 0, "{$hash}.{$this->mFinalExtension}" );
00610                 if ( $archivedImage->getID() > 0 ) {
00611                         $warnings['duplicate-archive'] = $archivedImage->getName();
00612                 }
00613 
00614                 wfProfileOut( __METHOD__ );
00615                 return $warnings;
00616         }
00617 
00629         public function performUpload( $comment, $pageText, $watch, $user ) {
00630                 wfProfileIn( __METHOD__ );
00631 
00632                 $status = $this->getLocalFile()->upload(
00633                         $this->mTempPath,
00634                         $comment,
00635                         $pageText,
00636                         File::DELETE_SOURCE,
00637                         $this->mFileProps,
00638                         false,
00639                         $user
00640                 );
00641 
00642                 if( $status->isGood() ) {
00643                         if ( $watch ) {
00644                                 $user->addWatch( $this->getLocalFile()->getTitle() );
00645                         }
00646                         wfRunHooks( 'UploadComplete', array( &$this ) );
00647                 }
00648 
00649                 wfProfileOut( __METHOD__ );
00650                 return $status;
00651         }
00652 
00659         public function getTitle() {
00660                 if ( $this->mTitle !== false ) {
00661                         return $this->mTitle;
00662                 }
00663 
00664                 /* Assume that if a user specified File:Something.jpg, this is an error
00665                  * and that the namespace prefix needs to be stripped of.
00666                  */
00667                 $title = Title::newFromText( $this->mDesiredDestName );
00668                 if ( $title && $title->getNamespace() == NS_FILE ) {
00669                         $this->mFilteredName = $title->getDBkey();
00670                 } else {
00671                         $this->mFilteredName = $this->mDesiredDestName;
00672                 }
00673 
00674                 # oi_archive_name is max 255 bytes, which include a timestamp and an
00675                 # exclamation mark, so restrict file name to 240 bytes.
00676                 if ( strlen( $this->mFilteredName ) > 240 ) {
00677                         $this->mTitleError = self::FILENAME_TOO_LONG;
00678                         return $this->mTitle = null;
00679                 }
00680 
00686                 $this->mFilteredName = wfStripIllegalFilenameChars( $this->mFilteredName );
00687                 /* Normalize to title form before we do any further processing */
00688                 $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00689                 if( is_null( $nt ) ) {
00690                         $this->mTitleError = self::ILLEGAL_FILENAME;
00691                         return $this->mTitle = null;
00692                 }
00693                 $this->mFilteredName = $nt->getDBkey();
00694 
00695 
00696 
00701                 list( $partname, $ext ) = $this->splitExtensions( $this->mFilteredName );
00702 
00703                 if( count( $ext ) ) {
00704                         $this->mFinalExtension = trim( $ext[count( $ext ) - 1] );
00705                 } else {
00706                         $this->mFinalExtension = '';
00707 
00708                         # No extension, try guessing one
00709                         $magic = MimeMagic::singleton();
00710                         $mime = $magic->guessMimeType( $this->mTempPath );
00711                         if ( $mime !== 'unknown/unknown' ) {
00712                                 # Get a space separated list of extensions
00713                                 $extList = $magic->getExtensionsForType( $mime );
00714                                 if ( $extList ) {
00715                                         # Set the extension to the canonical extension
00716                                         $this->mFinalExtension = strtok( $extList, ' ' );
00717 
00718                                         # Fix up the other variables
00719                                         $this->mFilteredName .= ".{$this->mFinalExtension}";
00720                                         $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00721                                         $ext = array( $this->mFinalExtension );
00722                                 }
00723                         }
00724 
00725                 }
00726 
00727                 /* Don't allow users to override the blacklist (check file extension) */
00728                 global $wgCheckFileExtensions, $wgStrictFileExtensions;
00729                 global $wgFileExtensions, $wgFileBlacklist;
00730 
00731                 $blackListedExtensions = $this->checkFileExtensionList( $ext, $wgFileBlacklist );
00732 
00733                 if ( $this->mFinalExtension == '' ) {
00734                         $this->mTitleError = self::FILETYPE_MISSING;
00735                         return $this->mTitle = null;
00736                 } elseif ( $blackListedExtensions ||
00737                                 ( $wgCheckFileExtensions && $wgStrictFileExtensions &&
00738                                         !$this->checkFileExtensionList( $ext, $wgFileExtensions ) ) ) {
00739                         $this->mBlackListedExtensions = $blackListedExtensions;
00740                         $this->mTitleError = self::FILETYPE_BADTYPE;
00741                         return $this->mTitle = null;
00742                 }
00743 
00744                 // Windows may be broken with special characters, see bug XXX
00745                 if ( wfIsWindows() && !preg_match( '/^[\x0-\x7f]*$/', $nt->getText() ) ) {
00746                         $this->mTitleError = self::WINDOWS_NONASCII_FILENAME;
00747                         return $this->mTitle = null;
00748                 }
00749 
00750                 # If there was more than one "extension", reassemble the base
00751                 # filename to prevent bogus complaints about length
00752                 if( count( $ext ) > 1 ) {
00753                         for( $i = 0; $i < count( $ext ) - 1; $i++ ) {
00754                                 $partname .= '.' . $ext[$i];
00755                         }
00756                 }
00757 
00758                 if( strlen( $partname ) < 1 ) {
00759                         $this->mTitleError =  self::MIN_LENGTH_PARTNAME;
00760                         return $this->mTitle = null;
00761                 }
00762 
00763                 return $this->mTitle = $nt;
00764         }
00765 
00771         public function getLocalFile() {
00772                 if( is_null( $this->mLocalFile ) ) {
00773                         $nt = $this->getTitle();
00774                         $this->mLocalFile = is_null( $nt ) ? null : wfLocalFile( $nt );
00775                 }
00776                 return $this->mLocalFile;
00777         }
00778 
00790         public function stashFile() {
00791                 // was stashSessionFile
00792                 wfProfileIn( __METHOD__ );
00793 
00794                 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
00795                 $file = $stash->stashFile( $this->mTempPath, $this->getSourceType() );
00796                 $this->mLocalFile = $file;
00797 
00798                 wfProfileOut( __METHOD__ );
00799                 return $file;
00800         }
00801 
00807         public function stashFileGetKey() {
00808                 return $this->stashFile()->getFileKey();
00809         }
00810 
00816         public function stashSession() {
00817                 return $this->stashFileGetKey();
00818         }
00819 
00824         public function cleanupTempFile() {
00825                 if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
00826                         wfDebug( __METHOD__ . ": Removing temporary file {$this->mTempPath}\n" );
00827                         unlink( $this->mTempPath );
00828                 }
00829         }
00830 
00831         public function getTempPath() {
00832                 return $this->mTempPath;
00833         }
00834 
00844         public static function splitExtensions( $filename ) {
00845                 $bits = explode( '.', $filename );
00846                 $basename = array_shift( $bits );
00847                 return array( $basename, $bits );
00848         }
00849 
00858         public static function checkFileExtension( $ext, $list ) {
00859                 return in_array( strtolower( $ext ), $list );
00860         }
00861 
00870         public static function checkFileExtensionList( $ext, $list ) {
00871                 return array_intersect( array_map( 'strtolower', $ext ), $list );
00872         }
00873 
00881         public static function verifyExtension( $mime, $extension ) {
00882                 $magic = MimeMagic::singleton();
00883 
00884                 if ( !$mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
00885                         if ( !$magic->isRecognizableExtension( $extension ) ) {
00886                                 wfDebug( __METHOD__ . ": passing file with unknown detected mime type; " .
00887                                         "unrecognized extension '$extension', can't verify\n" );
00888                                 return true;
00889                         } else {
00890                                 wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; ".
00891                                         "recognized extension '$extension', so probably invalid file\n" );
00892                                 return false;
00893                         }
00894 
00895                 $match = $magic->isMatchingExtension( $extension, $mime );
00896 
00897                 if ( $match === null ) {
00898                         wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
00899                         return true;
00900                 } elseif( $match === true ) {
00901                         wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file\n" );
00902 
00903                         #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
00904                         return true;
00905 
00906                 } else {
00907                         wfDebug( __METHOD__ . ": mime type $mime mismatches file extension $extension, rejecting file\n" );
00908                         return false;
00909                 }
00910         }
00911 
00923         public static function detectScript( $file, $mime, $extension ) {
00924                 global $wgAllowTitlesInSVG;
00925                 wfProfileIn( __METHOD__ );
00926 
00927                 # ugly hack: for text files, always look at the entire file.
00928                 # For binary field, just check the first K.
00929 
00930                 if( strpos( $mime,'text/' ) === 0 ) {
00931                         $chunk = file_get_contents( $file );
00932                 } else {
00933                         $fp = fopen( $file, 'rb' );
00934                         $chunk = fread( $fp, 1024 );
00935                         fclose( $fp );
00936                 }
00937 
00938                 $chunk = strtolower( $chunk );
00939 
00940                 if( !$chunk ) {
00941                         wfProfileOut( __METHOD__ );
00942                         return false;
00943                 }
00944 
00945                 # decode from UTF-16 if needed (could be used for obfuscation).
00946                 if( substr( $chunk, 0, 2 ) == "\xfe\xff" ) {
00947                         $enc = 'UTF-16BE';
00948                 } elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" ) {
00949                         $enc = 'UTF-16LE';
00950                 } else {
00951                         $enc = null;
00952                 }
00953 
00954                 if( $enc ) {
00955                         $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
00956                 }
00957 
00958                 $chunk = trim( $chunk );
00959 
00960                 # @todo FIXME: Convert from UTF-16 if necessarry!
00961                 wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff\n" );
00962 
00963                 # check for HTML doctype
00964                 if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) {
00965                         wfProfileOut( __METHOD__ );
00966                         return true;
00967                 }
00968 
00984                 $tags = array(
00985                         '<a href',
00986                         '<body',
00987                         '<head',
00988                         '<html',   #also in safari
00989                         '<img',
00990                         '<pre',
00991                         '<script', #also in safari
00992                         '<table'
00993                 );
00994 
00995                 if( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
00996                         $tags[] = '<title';
00997                 }
00998 
00999                 foreach( $tags as $tag ) {
01000                         if( false !== strpos( $chunk, $tag ) ) {
01001                                 wfDebug( __METHOD__ . ": found something that may make it be mistaken for html: $tag\n" );
01002                                 wfProfileOut( __METHOD__ );
01003                                 return true;
01004                         }
01005                 }
01006 
01007                 /*
01008                  * look for JavaScript
01009                  */
01010 
01011                 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
01012                 $chunk = Sanitizer::decodeCharReferences( $chunk );
01013 
01014                 # look for script-types
01015                 if( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
01016                         wfDebug( __METHOD__ . ": found script types\n" );
01017                         wfProfileOut( __METHOD__ );
01018                         return true;
01019                 }
01020 
01021                 # look for html-style script-urls
01022                 if( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
01023                         wfDebug( __METHOD__ . ": found html-style script urls\n" );
01024                         wfProfileOut( __METHOD__ );
01025                         return true;
01026                 }
01027 
01028                 # look for css-style script-urls
01029                 if( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
01030                         wfDebug( __METHOD__ . ": found css-style script urls\n" );
01031                         wfProfileOut( __METHOD__ );
01032                         return true;
01033                 }
01034 
01035                 wfDebug( __METHOD__ . ": no scripts found\n" );
01036                 wfProfileOut( __METHOD__ );
01037                 return false;
01038         }
01039 
01044         protected function detectScriptInSvg( $filename ) {
01045                 $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
01046                 return $check->filterMatch;
01047         }
01048 
01055         public function checkSvgScriptCallback( $element, $attribs ) {
01056                 $strippedElement = $this->stripXmlNamespace( $element );
01057 
01058                 /*
01059                  * check for elements that can contain javascript
01060                  */
01061                 if( $strippedElement == 'script' ) {
01062                         wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file.\n" );
01063                         return true;
01064                 }
01065 
01066                 # e.g., <svg xmlns="http://www.w3.org/2000/svg"> <handler xmlns:ev="http://www.w3.org/2001/xml-events" ev:event="load">alert(1)</handler> </svg>
01067                 if( $strippedElement == 'handler' ) {
01068                         wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file.\n" );
01069                         return true;
01070                 }
01071 
01072                 # SVG reported in Feb '12 that used xml:stylesheet to generate javascript block
01073                 if( $strippedElement == 'stylesheet' ) {
01074                         wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file.\n" );
01075                         return true;
01076                 }
01077 
01078                 foreach( $attribs as $attrib => $value ) {
01079                         $stripped = $this->stripXmlNamespace( $attrib );
01080                         $value = strtolower($value);
01081 
01082                         if( substr( $stripped, 0, 2 ) == 'on' ) {
01083                                 wfDebug( __METHOD__ . ": Found event-handler attribute '$attrib'='$value' in uploaded file.\n" );
01084                                 return true;
01085                         }
01086 
01087                         # href with javascript target
01088                         if( $stripped == 'href' && strpos( strtolower( $value ), 'javascript:' ) !== false ) {
01089                                 wfDebug( __METHOD__ . ": Found script in href attribute '$attrib'='$value' in uploaded file.\n" );
01090                                 return true;
01091                         }
01092 
01093                         # href with embeded svg as target
01094                         if( $stripped == 'href' && preg_match( '!data:[^,]*image/svg[^,]*,!sim', $value ) ) {
01095                                 wfDebug( __METHOD__ . ": Found href to embedded svg \"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
01096                                 return true;
01097                         }
01098 
01099                         # href with embeded (text/xml) svg as target
01100                         if( $stripped == 'href' && preg_match( '!data:[^,]*text/xml[^,]*,!sim', $value ) ) {
01101                                 wfDebug( __METHOD__ . ": Found href to embedded svg \"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
01102                                 return true;
01103                         }
01104 
01105                         # use set/animate to add event-handler attribute to parent
01106                         if( ( $strippedElement == 'set' || $strippedElement == 'animate' ) && $stripped == 'attributename' && substr( $value, 0, 2 ) == 'on' ) {
01107                                 wfDebug( __METHOD__ . ": Found svg setting event-handler attribute with \"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
01108                                 return true;
01109                         }
01110 
01111                         # use set to add href attribute to parent element
01112                         if( $strippedElement == 'set' && $stripped == 'attributename' && strpos( $value, 'href' ) !== false ) {
01113                                 wfDebug( __METHOD__ . ": Found svg setting href attibute '$value' in uploaded file.\n" );
01114                                 return true;
01115                         }
01116 
01117                         # use set to add a remote / data / script target to an element
01118                         if( $strippedElement == 'set' && $stripped == 'to' &&  preg_match( '!(http|https|data|script):!sim', $value ) ) {
01119                                 wfDebug( __METHOD__ . ": Found svg setting attibute to '$value' in uploaded file.\n" );
01120                                 return true;
01121                         }
01122 
01123 
01124                         # use handler attribute with remote / data / script
01125                         if( $stripped == 'handler' &&  preg_match( '!(http|https|data|script):!sim', $value ) ) {
01126                                 wfDebug( __METHOD__ . ": Found svg setting handler with remote/data/script '$attrib'='$value' in uploaded file.\n" );
01127                                 return true;
01128                         }
01129 
01130                         # use CSS styles to bring in remote code
01131                         # catch url("http:..., url('http:..., url(http:..., but not url("#..., url('#..., url(#....
01132                         if( $stripped == 'style' && preg_match_all( '!((?:font|clip-path|fill|filter|marker|marker-end|marker-mid|marker-start|mask|stroke)\s*:\s*url\s*\(\s*["\']?\s*[^#]+.*?\))!sim', $value, $matches ) ) {
01133                                 foreach ($matches[1] as $match) {
01134                                         if (!preg_match( '!(?:font|clip-path|fill|filter|marker|marker-end|marker-mid|marker-start|mask|stroke)\s*:\s*url\s*\(\s*(#|\'#|"#)!sim', $match ) ) {
01135                                                 wfDebug( __METHOD__ . ": Found svg setting a style with remote url '$attrib'='$value' in uploaded file.\n" );
01136                                                 return true;
01137                                         }
01138                                 }
01139                         }
01140 
01141                         # image filters can pull in url, which could be svg that executes scripts
01142                         if( $strippedElement == 'image' && $stripped == 'filter' && preg_match( '!url\s*\(!sim', $value ) ) {
01143                                 wfDebug( __METHOD__ . ": Found image filter with url: \"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
01144                                 return true;
01145                         }
01146 
01147                 }
01148 
01149                 return false; //No scripts detected
01150         }
01151 
01156         private function stripXmlNamespace( $name ) {
01157                 // 'http://www.w3.org/2000/svg:script' -> 'script'
01158                 $parts = explode( ':', strtolower( $name ) );
01159                 return array_pop( $parts );
01160         }
01161 
01172         public static function detectVirus( $file ) {
01173                 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
01174                 wfProfileIn( __METHOD__ );
01175 
01176                 if ( !$wgAntivirus ) {
01177                         wfDebug( __METHOD__ . ": virus scanner disabled\n" );
01178                         wfProfileOut( __METHOD__ );
01179                         return null;
01180                 }
01181 
01182                 if ( !$wgAntivirusSetup[$wgAntivirus] ) {
01183                         wfDebug( __METHOD__ . ": unknown virus scanner: $wgAntivirus\n" );
01184                         $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>",
01185                                 array( 'virus-badscanner', $wgAntivirus ) );
01186                         wfProfileOut( __METHOD__ );
01187                         return wfMessage( 'virus-unknownscanner' )->text() . " $wgAntivirus";
01188                 }
01189 
01190                 # look up scanner configuration
01191                 $command = $wgAntivirusSetup[$wgAntivirus]['command'];
01192                 $exitCodeMap = $wgAntivirusSetup[$wgAntivirus]['codemap'];
01193                 $msgPattern = isset( $wgAntivirusSetup[$wgAntivirus]['messagepattern'] ) ?
01194                         $wgAntivirusSetup[$wgAntivirus]['messagepattern'] : null;
01195 
01196                 if ( strpos( $command, "%f" ) === false ) {
01197                         # simple pattern: append file to scan
01198                         $command .= " " . wfEscapeShellArg( $file );
01199                 } else {
01200                         # complex pattern: replace "%f" with file to scan
01201                         $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
01202                 }
01203 
01204                 wfDebug( __METHOD__ . ": running virus scan: $command \n" );
01205 
01206                 # execute virus scanner
01207                 $exitCode = false;
01208 
01209                 # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
01210                 #      that does not seem to be worth the pain.
01211                 #      Ask me (Duesentrieb) about it if it's ever needed.
01212                 $output = wfShellExec( "$command 2>&1", $exitCode );
01213 
01214                 # map exit code to AV_xxx constants.
01215                 $mappedCode = $exitCode;
01216                 if ( $exitCodeMap ) {
01217                         if ( isset( $exitCodeMap[$exitCode] ) ) {
01218                                 $mappedCode = $exitCodeMap[$exitCode];
01219                         } elseif ( isset( $exitCodeMap["*"] ) ) {
01220                                 $mappedCode = $exitCodeMap["*"];
01221                         }
01222                 }
01223 
01224                 if ( $mappedCode === AV_SCAN_FAILED ) {
01225                         # scan failed (code was mapped to false by $exitCodeMap)
01226                         wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
01227 
01228                         if ( $wgAntivirusRequired ) {
01229                                 wfProfileOut( __METHOD__ );
01230                                 return wfMessage( 'virus-scanfailed', array( $exitCode ) )->text();
01231                         } else {
01232                                 wfProfileOut( __METHOD__ );
01233                                 return null;
01234                         }
01235                 } elseif ( $mappedCode === AV_SCAN_ABORTED ) {
01236                         # scan failed because filetype is unknown (probably imune)
01237                         wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
01238                         wfProfileOut( __METHOD__ );
01239                         return null;
01240                 } elseif ( $mappedCode === AV_NO_VIRUS ) {
01241                         # no virus found
01242                         wfDebug( __METHOD__ . ": file passed virus scan.\n" );
01243                         wfProfileOut( __METHOD__ );
01244                         return false;
01245                 } else {
01246                         $output = trim( $output );
01247 
01248                         if ( !$output ) {
01249                                 $output = true; #if there's no output, return true
01250                         } elseif ( $msgPattern ) {
01251                                 $groups = array();
01252                                 if ( preg_match( $msgPattern, $output, $groups ) ) {
01253                                         if ( $groups[1] ) {
01254                                                 $output = $groups[1];
01255                                         }
01256                                 }
01257                         }
01258 
01259                         wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
01260                         wfProfileOut( __METHOD__ );
01261                         return $output;
01262                 }
01263         }
01264 
01273         private function checkOverwrite( $user ) {
01274                 // First check whether the local file can be overwritten
01275                 $file = $this->getLocalFile();
01276                 if( $file->exists() ) {
01277                         if( !self::userCanReUpload( $user, $file ) ) {
01278                                 return array( 'fileexists-forbidden', $file->getName() );
01279                         } else {
01280                                 return true;
01281                         }
01282                 }
01283 
01284                 /* Check shared conflicts: if the local file does not exist, but
01285                  * wfFindFile finds a file, it exists in a shared repository.
01286                  */
01287                 $file = wfFindFile( $this->getTitle() );
01288                 if ( $file && !$user->isAllowed( 'reupload-shared' ) ) {
01289                         return array( 'fileexists-shared-forbidden', $file->getName() );
01290                 }
01291 
01292                 return true;
01293         }
01294 
01302         public static function userCanReUpload( User $user, $img ) {
01303                 if( $user->isAllowed( 'reupload' ) ) {
01304                         return true; // non-conditional
01305                 }
01306                 if( !$user->isAllowed( 'reupload-own' ) ) {
01307                         return false;
01308                 }
01309                 if( is_string( $img ) ) {
01310                         $img = wfLocalFile( $img );
01311                 }
01312                 if ( !( $img instanceof LocalFile ) ) {
01313                         return false;
01314                 }
01315 
01316                 return $user->getId() == $img->getUser( 'id' );
01317         }
01318 
01330         public static function getExistsWarning( $file ) {
01331                 if( $file->exists() ) {
01332                         return array( 'warning' => 'exists', 'file' => $file );
01333                 }
01334 
01335                 if( $file->getTitle()->getArticleID() ) {
01336                         return array( 'warning' => 'page-exists', 'file' => $file );
01337                 }
01338 
01339                 if ( $file->wasDeleted() && !$file->exists() ) {
01340                         return array( 'warning' => 'was-deleted', 'file' => $file );
01341                 }
01342 
01343                 if( strpos( $file->getName(), '.' ) == false ) {
01344                         $partname = $file->getName();
01345                         $extension = '';
01346                 } else {
01347                         $n = strrpos( $file->getName(), '.' );
01348                         $extension = substr( $file->getName(), $n + 1 );
01349                         $partname = substr( $file->getName(), 0, $n );
01350                 }
01351                 $normalizedExtension = File::normalizeExtension( $extension );
01352 
01353                 if ( $normalizedExtension != $extension ) {
01354                         // We're not using the normalized form of the extension.
01355                         // Normal form is lowercase, using most common of alternate
01356                         // extensions (eg 'jpg' rather than 'JPEG').
01357                         //
01358                         // Check for another file using the normalized form...
01359                         $nt_lc = Title::makeTitle( NS_FILE, "{$partname}.{$normalizedExtension}" );
01360                         $file_lc = wfLocalFile( $nt_lc );
01361 
01362                         if( $file_lc->exists() ) {
01363                                 return array(
01364                                         'warning' => 'exists-normalized',
01365                                         'file' => $file,
01366                                         'normalizedFile' => $file_lc
01367                                 );
01368                         }
01369                 }
01370 
01371                 if ( self::isThumbName( $file->getName() ) ) {
01372                         # Check for filenames like 50px- or 180px-, these are mostly thumbnails
01373                         $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $extension, NS_FILE );
01374                         $file_thb = wfLocalFile( $nt_thb );
01375                         if( $file_thb->exists() ) {
01376                                 return array(
01377                                         'warning' => 'thumb',
01378                                         'file' => $file,
01379                                         'thumbFile' => $file_thb
01380                                 );
01381                         } else {
01382                                 // File does not exist, but we just don't like the name
01383                                 return array(
01384                                         'warning' => 'thumb-name',
01385                                         'file' => $file,
01386                                         'thumbFile' => $file_thb
01387                                 );
01388                         }
01389                 }
01390 
01391 
01392                 foreach( self::getFilenamePrefixBlacklist() as $prefix ) {
01393                         if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
01394                                 return array(
01395                                         'warning' => 'bad-prefix',
01396                                         'file' => $file,
01397                                         'prefix' => $prefix
01398                                 );
01399                         }
01400                 }
01401 
01402                 return false;
01403         }
01404 
01410         public static function isThumbName( $filename ) {
01411                 $n = strrpos( $filename, '.' );
01412                 $partname = $n ? substr( $filename, 0, $n ) : $filename;
01413                 return (
01414                                         substr( $partname , 3, 3 ) == 'px-' ||
01415                                         substr( $partname , 2, 3 ) == 'px-'
01416                                 ) &&
01417                                 preg_match( "/[0-9]{2}/" , substr( $partname , 0, 2 ) );
01418         }
01419 
01425         public static function getFilenamePrefixBlacklist() {
01426                 $blacklist = array();
01427                 $message = wfMessage( 'filename-prefix-blacklist' )->inContentLanguage();
01428                 if( !$message->isDisabled() ) {
01429                         $lines = explode( "\n", $message->plain() );
01430                         foreach( $lines as $line ) {
01431                                 // Remove comment lines
01432                                 $comment = substr( trim( $line ), 0, 1 );
01433                                 if ( $comment == '#' || $comment == '' ) {
01434                                         continue;
01435                                 }
01436                                 // Remove additional comments after a prefix
01437                                 $comment = strpos( $line, '#' );
01438                                 if ( $comment > 0 ) {
01439                                         $line = substr( $line, 0, $comment-1 );
01440                                 }
01441                                 $blacklist[] = trim( $line );
01442                         }
01443                 }
01444                 return $blacklist;
01445         }
01446 
01457         public function getImageInfo( $result ) {
01458                 $file = $this->getLocalFile();
01459                 // TODO This cries out for refactoring. We really want to say $file->getAllInfo(); here.
01460                 // Perhaps "info" methods should be moved into files, and the API should just wrap them in queries.
01461                 if ( $file instanceof UploadStashFile ) {
01462                         $imParam = ApiQueryStashImageInfo::getPropertyNames();
01463                         $info = ApiQueryStashImageInfo::getInfo( $file, array_flip( $imParam ), $result );
01464                 } else {
01465                         $imParam = ApiQueryImageInfo::getPropertyNames();
01466                         $info = ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
01467                 }
01468                 return $info;
01469         }
01470 
01475         public function convertVerifyErrorToStatus( $error ) {
01476                 $code = $error['status'];
01477                 unset( $code['status'] );
01478                 return Status::newFatal( $this->getVerificationErrorCode( $code ), $error );
01479         }
01480 
01485         public static function getMaxUploadSize( $forType = null ) {
01486                 global $wgMaxUploadSize;
01487 
01488                 if ( is_array( $wgMaxUploadSize ) ) {
01489                         if ( !is_null( $forType ) && isset( $wgMaxUploadSize[$forType] ) ) {
01490                                 return $wgMaxUploadSize[$forType];
01491                         } else {
01492                                 return $wgMaxUploadSize['*'];
01493                         }
01494                 } else {
01495                         return intval( $wgMaxUploadSize );
01496                 }
01497 
01498         }
01499 }