MediaWiki  REL1_23
UploadBase.php
Go to the documentation of this file.
00001 <?php
00038 abstract class UploadBase {
00039     protected $mTempPath;
00040     protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
00041     protected $mTitle = false, $mTitleError = 0;
00042     protected $mFilteredName, $mFinalExtension;
00043     protected $mLocalFile, $mFileSize, $mFileProps;
00044     protected $mBlackListedExtensions;
00045     protected $mJavaDetected, $mSVGNSError;
00046 
00047     protected static $safeXmlEncodings = array( 'UTF-8', 'ISO-8859-1', 'ISO-8859-2', 'UTF-16', 'UTF-32' );
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 
00066     const SESSION_STATUS_KEY = 'wsUploadStatusData';
00067 
00072     public function getVerificationErrorCode( $error ) {
00073         $code_to_status = array(
00074             self::EMPTY_FILE => 'empty-file',
00075             self::FILE_TOO_LARGE => 'file-too-large',
00076             self::FILETYPE_MISSING => 'filetype-missing',
00077             self::FILETYPE_BADTYPE => 'filetype-banned',
00078             self::MIN_LENGTH_PARTNAME => 'filename-tooshort',
00079             self::ILLEGAL_FILENAME => 'illegal-filename',
00080             self::OVERWRITE_EXISTING_FILE => 'overwrite',
00081             self::VERIFICATION_ERROR => 'verification-error',
00082             self::HOOK_ABORTED => 'hookaborted',
00083             self::WINDOWS_NONASCII_FILENAME => 'windows-nonascii-filename',
00084             self::FILENAME_TOO_LONG => 'filename-toolong',
00085         );
00086         if ( isset( $code_to_status[$error] ) ) {
00087             return $code_to_status[$error];
00088         }
00089 
00090         return 'unknown-error';
00091     }
00092 
00098     public static function isEnabled() {
00099         global $wgEnableUploads;
00100 
00101         if ( !$wgEnableUploads ) {
00102             return false;
00103         }
00104 
00105         # Check php's file_uploads setting
00106         return wfIsHHVM() || wfIniGetBool( 'file_uploads' );
00107     }
00108 
00117     public static function isAllowed( $user ) {
00118         foreach ( array( 'upload', 'edit' ) as $permission ) {
00119             if ( !$user->isAllowed( $permission ) ) {
00120                 return $permission;
00121             }
00122         }
00123         return true;
00124     }
00125 
00126     // Upload handlers. Should probably just be a global.
00127     static $uploadHandlers = array( 'Stash', 'File', 'Url' );
00128 
00136     public static function createFromRequest( &$request, $type = null ) {
00137         $type = $type ? $type : $request->getVal( 'wpSourceType', 'File' );
00138 
00139         if ( !$type ) {
00140             return null;
00141         }
00142 
00143         // Get the upload class
00144         $type = ucfirst( $type );
00145 
00146         // Give hooks the chance to handle this request
00147         $className = null;
00148         wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
00149         if ( is_null( $className ) ) {
00150             $className = 'UploadFrom' . $type;
00151             wfDebug( __METHOD__ . ": class name: $className\n" );
00152             if ( !in_array( $type, self::$uploadHandlers ) ) {
00153                 return null;
00154             }
00155         }
00156 
00157         // Check whether this upload class is enabled
00158         if ( !call_user_func( array( $className, 'isEnabled' ) ) ) {
00159             return null;
00160         }
00161 
00162         // Check whether the request is valid
00163         if ( !call_user_func( array( $className, 'isValidRequest' ), $request ) ) {
00164             return null;
00165         }
00166 
00167         $handler = new $className;
00168 
00169         $handler->initializeFromRequest( $request );
00170         return $handler;
00171     }
00172 
00178     public static function isValidRequest( $request ) {
00179         return false;
00180     }
00181 
00182     public function __construct() {}
00183 
00190     public function getSourceType() {
00191         return null;
00192     }
00193 
00202     public function initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile = false ) {
00203         $this->mDesiredDestName = $name;
00204         if ( FileBackend::isStoragePath( $tempPath ) ) {
00205             throw new MWException( __METHOD__ . " given storage path `$tempPath`." );
00206         }
00207         $this->mTempPath = $tempPath;
00208         $this->mFileSize = $fileSize;
00209         $this->mRemoveTempFile = $removeTempFile;
00210     }
00211 
00215     abstract public function initializeFromRequest( &$request );
00216 
00221     public function fetchFile() {
00222         return Status::newGood();
00223     }
00224 
00229     public function isEmptyFile() {
00230         return empty( $this->mFileSize );
00231     }
00232 
00237     public function getFileSize() {
00238         return $this->mFileSize;
00239     }
00240 
00245     public function getTempFileSha1Base36() {
00246         return FSFile::getSha1Base36FromPath( $this->mTempPath );
00247     }
00248 
00253     function getRealPath( $srcPath ) {
00254         wfProfileIn( __METHOD__ );
00255         $repo = RepoGroup::singleton()->getLocalRepo();
00256         if ( $repo->isVirtualUrl( $srcPath ) ) {
00257             // @todo just make uploads work with storage paths
00258             // UploadFromStash loads files via virtual URLs
00259             $tmpFile = $repo->getLocalCopy( $srcPath );
00260             if ( $tmpFile ) {
00261                 $tmpFile->bind( $this ); // keep alive with $this
00262             }
00263             $path = $tmpFile ? $tmpFile->getPath() : false;
00264         } else {
00265             $path = $srcPath;
00266         }
00267         wfProfileOut( __METHOD__ );
00268         return $path;
00269     }
00270 
00275     public function verifyUpload() {
00276         wfProfileIn( __METHOD__ );
00277 
00281         if ( $this->isEmptyFile() ) {
00282             wfProfileOut( __METHOD__ );
00283             return array( 'status' => self::EMPTY_FILE );
00284         }
00285 
00289         $maxSize = self::getMaxUploadSize( $this->getSourceType() );
00290         if ( $this->mFileSize > $maxSize ) {
00291             wfProfileOut( __METHOD__ );
00292             return array(
00293                 'status' => self::FILE_TOO_LARGE,
00294                 'max' => $maxSize,
00295             );
00296         }
00297 
00303         $verification = $this->verifyFile();
00304         if ( $verification !== true ) {
00305             wfProfileOut( __METHOD__ );
00306             return array(
00307                 'status' => self::VERIFICATION_ERROR,
00308                 'details' => $verification
00309             );
00310         }
00311 
00315         $result = $this->validateName();
00316         if ( $result !== true ) {
00317             wfProfileOut( __METHOD__ );
00318             return $result;
00319         }
00320 
00321         $error = '';
00322         if ( !wfRunHooks( 'UploadVerification',
00323             array( $this->mDestName, $this->mTempPath, &$error ) )
00324         ) {
00325             wfProfileOut( __METHOD__ );
00326             return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
00327         }
00328 
00329         wfProfileOut( __METHOD__ );
00330         return array( 'status' => self::OK );
00331     }
00332 
00339     public function validateName() {
00340         $nt = $this->getTitle();
00341         if ( is_null( $nt ) ) {
00342             $result = array( 'status' => $this->mTitleError );
00343             if ( $this->mTitleError == self::ILLEGAL_FILENAME ) {
00344                 $result['filtered'] = $this->mFilteredName;
00345             }
00346             if ( $this->mTitleError == self::FILETYPE_BADTYPE ) {
00347                 $result['finalExt'] = $this->mFinalExtension;
00348                 if ( count( $this->mBlackListedExtensions ) ) {
00349                     $result['blacklistedExt'] = $this->mBlackListedExtensions;
00350                 }
00351             }
00352             return $result;
00353         }
00354         $this->mDestName = $this->getLocalFile()->getName();
00355 
00356         return true;
00357     }
00358 
00367     protected function verifyMimeType( $mime ) {
00368         global $wgVerifyMimeType;
00369         wfProfileIn( __METHOD__ );
00370         if ( $wgVerifyMimeType ) {
00371             wfDebug( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n" );
00372             global $wgMimeTypeBlacklist;
00373             if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
00374                 wfProfileOut( __METHOD__ );
00375                 return array( 'filetype-badmime', $mime );
00376             }
00377 
00378             # Check IE type
00379             $fp = fopen( $this->mTempPath, 'rb' );
00380             $chunk = fread( $fp, 256 );
00381             fclose( $fp );
00382 
00383             $magic = MimeMagic::singleton();
00384             $extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
00385             $ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
00386             foreach ( $ieTypes as $ieType ) {
00387                 if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
00388                     wfProfileOut( __METHOD__ );
00389                     return array( 'filetype-bad-ie-mime', $ieType );
00390                 }
00391             }
00392         }
00393 
00394         wfProfileOut( __METHOD__ );
00395         return true;
00396     }
00397 
00403     protected function verifyFile() {
00404         global $wgVerifyMimeType;
00405         wfProfileIn( __METHOD__ );
00406 
00407         $status = $this->verifyPartialFile();
00408         if ( $status !== true ) {
00409             wfProfileOut( __METHOD__ );
00410             return $status;
00411         }
00412 
00413         $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
00414         $mime = $this->mFileProps['file-mime'];
00415 
00416         if ( $wgVerifyMimeType ) {
00417             # XXX: Missing extension will be caught by validateName() via getTitle()
00418             if ( $this->mFinalExtension != '' && !$this->verifyExtension( $mime, $this->mFinalExtension ) ) {
00419                 wfProfileOut( __METHOD__ );
00420                 return array( 'filetype-mime-mismatch', $this->mFinalExtension, $mime );
00421             }
00422         }
00423 
00424         $handler = MediaHandler::getHandler( $mime );
00425         if ( $handler ) {
00426             $handlerStatus = $handler->verifyUpload( $this->mTempPath );
00427             if ( !$handlerStatus->isOK() ) {
00428                 $errors = $handlerStatus->getErrorsArray();
00429                 wfProfileOut( __METHOD__ );
00430                 return reset( $errors );
00431             }
00432         }
00433 
00434         wfRunHooks( 'UploadVerifyFile', array( $this, $mime, &$status ) );
00435         if ( $status !== true ) {
00436             wfProfileOut( __METHOD__ );
00437             return $status;
00438         }
00439 
00440         wfDebug( __METHOD__ . ": all clear; passing.\n" );
00441         wfProfileOut( __METHOD__ );
00442         return true;
00443     }
00444 
00453     protected function verifyPartialFile() {
00454         global $wgAllowJavaUploads, $wgDisableUploadScriptChecks;
00455         wfProfileIn( __METHOD__ );
00456 
00457         # getTitle() sets some internal parameters like $this->mFinalExtension
00458         $this->getTitle();
00459 
00460         $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
00461 
00462         # check mime type, if desired
00463         $mime = $this->mFileProps['file-mime'];
00464         $status = $this->verifyMimeType( $mime );
00465         if ( $status !== true ) {
00466             wfProfileOut( __METHOD__ );
00467             return $status;
00468         }
00469 
00470         # check for htmlish code and javascript
00471         if ( !$wgDisableUploadScriptChecks ) {
00472             if ( self::detectScript( $this->mTempPath, $mime, $this->mFinalExtension ) ) {
00473                 wfProfileOut( __METHOD__ );
00474                 return array( 'uploadscripted' );
00475             }
00476             if ( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
00477                 $svgStatus = $this->detectScriptInSvg( $this->mTempPath );
00478                 if ( $svgStatus !== false ) {
00479                     wfProfileOut( __METHOD__ );
00480                     return $svgStatus;
00481                 }
00482             }
00483         }
00484 
00485         # Check for Java applets, which if uploaded can bypass cross-site
00486         # restrictions.
00487         if ( !$wgAllowJavaUploads ) {
00488             $this->mJavaDetected = false;
00489             $zipStatus = ZipDirectoryReader::read( $this->mTempPath,
00490                 array( $this, 'zipEntryCallback' ) );
00491             if ( !$zipStatus->isOK() ) {
00492                 $errors = $zipStatus->getErrorsArray();
00493                 $error = reset( $errors );
00494                 if ( $error[0] !== 'zip-wrong-format' ) {
00495                     wfProfileOut( __METHOD__ );
00496                     return $error;
00497                 }
00498             }
00499             if ( $this->mJavaDetected ) {
00500                 wfProfileOut( __METHOD__ );
00501                 return array( 'uploadjava' );
00502             }
00503         }
00504 
00505         # Scan the uploaded file for viruses
00506         $virus = $this->detectVirus( $this->mTempPath );
00507         if ( $virus ) {
00508             wfProfileOut( __METHOD__ );
00509             return array( 'uploadvirus', $virus );
00510         }
00511 
00512         wfProfileOut( __METHOD__ );
00513         return true;
00514     }
00515 
00519     function zipEntryCallback( $entry ) {
00520         $names = array( $entry['name'] );
00521 
00522         // If there is a null character, cut off the name at it, because JDK's
00523         // ZIP_GetEntry() uses strcmp() if the name hashes match. If a file name
00524         // were constructed which had ".class\0" followed by a string chosen to
00525         // make the hash collide with the truncated name, that file could be
00526         // returned in response to a request for the .class file.
00527         $nullPos = strpos( $entry['name'], "\000" );
00528         if ( $nullPos !== false ) {
00529             $names[] = substr( $entry['name'], 0, $nullPos );
00530         }
00531 
00532         // If there is a trailing slash in the file name, we have to strip it,
00533         // because that's what ZIP_GetEntry() does.
00534         if ( preg_grep( '!\.class/?$!', $names ) ) {
00535             $this->mJavaDetected = true;
00536         }
00537     }
00538 
00546     public function verifyPermissions( $user ) {
00547         return $this->verifyTitlePermissions( $user );
00548     }
00549 
00561     public function verifyTitlePermissions( $user ) {
00566         $nt = $this->getTitle();
00567         if ( is_null( $nt ) ) {
00568             return true;
00569         }
00570         $permErrors = $nt->getUserPermissionsErrors( 'edit', $user );
00571         $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $user );
00572         if ( !$nt->exists() ) {
00573             $permErrorsCreate = $nt->getUserPermissionsErrors( 'create', $user );
00574         } else {
00575             $permErrorsCreate = array();
00576         }
00577         if ( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
00578             $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
00579             $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
00580             return $permErrors;
00581         }
00582 
00583         $overwriteError = $this->checkOverwrite( $user );
00584         if ( $overwriteError !== true ) {
00585             return array( $overwriteError );
00586         }
00587 
00588         return true;
00589     }
00590 
00598     public function checkWarnings() {
00599         global $wgLang;
00600         wfProfileIn( __METHOD__ );
00601 
00602         $warnings = array();
00603 
00604         $localFile = $this->getLocalFile();
00605         $filename = $localFile->getName();
00606 
00611         $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
00612         $comparableName = Title::capitalize( $comparableName, NS_FILE );
00613 
00614         if ( $this->mDesiredDestName != $filename && $comparableName != $filename ) {
00615             $warnings['badfilename'] = $filename;
00616             // Debugging for bug 62241
00617             wfDebugLog( 'upload', "Filename: '$filename', mDesiredDestName: '$this->mDesiredDestName', comparableName: '$comparableName'" );
00618         }
00619 
00620         // Check whether the file extension is on the unwanted list
00621         global $wgCheckFileExtensions, $wgFileExtensions;
00622         if ( $wgCheckFileExtensions ) {
00623             $extensions = array_unique( $wgFileExtensions );
00624             if ( !$this->checkFileExtension( $this->mFinalExtension, $extensions ) ) {
00625                 $warnings['filetype-unwanted-type'] = array( $this->mFinalExtension,
00626                     $wgLang->commaList( $extensions ), count( $extensions ) );
00627             }
00628         }
00629 
00630         global $wgUploadSizeWarning;
00631         if ( $wgUploadSizeWarning && ( $this->mFileSize > $wgUploadSizeWarning ) ) {
00632             $warnings['large-file'] = array( $wgUploadSizeWarning, $this->mFileSize );
00633         }
00634 
00635         if ( $this->mFileSize == 0 ) {
00636             $warnings['emptyfile'] = true;
00637         }
00638 
00639         $exists = self::getExistsWarning( $localFile );
00640         if ( $exists !== false ) {
00641             $warnings['exists'] = $exists;
00642         }
00643 
00644         // Check dupes against existing files
00645         $hash = $this->getTempFileSha1Base36();
00646         $dupes = RepoGroup::singleton()->findBySha1( $hash );
00647         $title = $this->getTitle();
00648         // Remove all matches against self
00649         foreach ( $dupes as $key => $dupe ) {
00650             if ( $title->equals( $dupe->getTitle() ) ) {
00651                 unset( $dupes[$key] );
00652             }
00653         }
00654         if ( $dupes ) {
00655             $warnings['duplicate'] = $dupes;
00656         }
00657 
00658         // Check dupes against archives
00659         $archivedImage = new ArchivedFile( null, 0, "{$hash}.{$this->mFinalExtension}" );
00660         if ( $archivedImage->getID() > 0 ) {
00661             if ( $archivedImage->userCan( File::DELETED_FILE ) ) {
00662                 $warnings['duplicate-archive'] = $archivedImage->getName();
00663             } else {
00664                 $warnings['duplicate-archive'] = '';
00665             }
00666         }
00667 
00668         wfProfileOut( __METHOD__ );
00669         return $warnings;
00670     }
00671 
00683     public function performUpload( $comment, $pageText, $watch, $user ) {
00684         wfProfileIn( __METHOD__ );
00685 
00686         $status = $this->getLocalFile()->upload(
00687             $this->mTempPath,
00688             $comment,
00689             $pageText,
00690             File::DELETE_SOURCE,
00691             $this->mFileProps,
00692             false,
00693             $user
00694         );
00695 
00696         if ( $status->isGood() ) {
00697             if ( $watch ) {
00698                 WatchAction::doWatch( $this->getLocalFile()->getTitle(), $user, WatchedItem::IGNORE_USER_RIGHTS );
00699             }
00700             wfRunHooks( 'UploadComplete', array( &$this ) );
00701         }
00702 
00703         wfProfileOut( __METHOD__ );
00704         return $status;
00705     }
00706 
00713     public function getTitle() {
00714         if ( $this->mTitle !== false ) {
00715             return $this->mTitle;
00716         }
00717         /* Assume that if a user specified File:Something.jpg, this is an error
00718          * and that the namespace prefix needs to be stripped of.
00719          */
00720         $title = Title::newFromText( $this->mDesiredDestName );
00721         if ( $title && $title->getNamespace() == NS_FILE ) {
00722             $this->mFilteredName = $title->getDBkey();
00723         } else {
00724             $this->mFilteredName = $this->mDesiredDestName;
00725         }
00726 
00727         # oi_archive_name is max 255 bytes, which include a timestamp and an
00728         # exclamation mark, so restrict file name to 240 bytes.
00729         if ( strlen( $this->mFilteredName ) > 240 ) {
00730             $this->mTitleError = self::FILENAME_TOO_LONG;
00731             $this->mTitle = null;
00732             return $this->mTitle;
00733         }
00734 
00740         $this->mFilteredName = wfStripIllegalFilenameChars( $this->mFilteredName );
00741         /* Normalize to title form before we do any further processing */
00742         $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00743         if ( is_null( $nt ) ) {
00744             $this->mTitleError = self::ILLEGAL_FILENAME;
00745             $this->mTitle = null;
00746             return $this->mTitle;
00747         }
00748         $this->mFilteredName = $nt->getDBkey();
00749 
00754         list( $partname, $ext ) = $this->splitExtensions( $this->mFilteredName );
00755 
00756         if ( count( $ext ) ) {
00757             $this->mFinalExtension = trim( $ext[count( $ext ) - 1] );
00758         } else {
00759             $this->mFinalExtension = '';
00760 
00761             # No extension, try guessing one
00762             $magic = MimeMagic::singleton();
00763             $mime = $magic->guessMimeType( $this->mTempPath );
00764             if ( $mime !== 'unknown/unknown' ) {
00765                 # Get a space separated list of extensions
00766                 $extList = $magic->getExtensionsForType( $mime );
00767                 if ( $extList ) {
00768                     # Set the extension to the canonical extension
00769                     $this->mFinalExtension = strtok( $extList, ' ' );
00770 
00771                     # Fix up the other variables
00772                     $this->mFilteredName .= ".{$this->mFinalExtension}";
00773                     $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00774                     $ext = array( $this->mFinalExtension );
00775                 }
00776             }
00777         }
00778 
00779         /* Don't allow users to override the blacklist (check file extension) */
00780         global $wgCheckFileExtensions, $wgStrictFileExtensions;
00781         global $wgFileExtensions, $wgFileBlacklist;
00782 
00783         $blackListedExtensions = $this->checkFileExtensionList( $ext, $wgFileBlacklist );
00784 
00785         if ( $this->mFinalExtension == '' ) {
00786             $this->mTitleError = self::FILETYPE_MISSING;
00787             $this->mTitle = null;
00788             return $this->mTitle;
00789         } elseif ( $blackListedExtensions ||
00790                 ( $wgCheckFileExtensions && $wgStrictFileExtensions &&
00791                     !$this->checkFileExtension( $this->mFinalExtension, $wgFileExtensions ) ) ) {
00792             $this->mBlackListedExtensions = $blackListedExtensions;
00793             $this->mTitleError = self::FILETYPE_BADTYPE;
00794             $this->mTitle = null;
00795             return $this->mTitle;
00796         }
00797 
00798         // Windows may be broken with special characters, see bug XXX
00799         if ( wfIsWindows() && !preg_match( '/^[\x0-\x7f]*$/', $nt->getText() ) ) {
00800             $this->mTitleError = self::WINDOWS_NONASCII_FILENAME;
00801             $this->mTitle = null;
00802             return $this->mTitle;
00803         }
00804 
00805         # If there was more than one "extension", reassemble the base
00806         # filename to prevent bogus complaints about length
00807         if ( count( $ext ) > 1 ) {
00808             for ( $i = 0; $i < count( $ext ) - 1; $i++ ) {
00809                 $partname .= '.' . $ext[$i];
00810             }
00811         }
00812 
00813         if ( strlen( $partname ) < 1 ) {
00814             $this->mTitleError = self::MIN_LENGTH_PARTNAME;
00815             $this->mTitle = null;
00816             return $this->mTitle;
00817         }
00818 
00819         $this->mTitle = $nt;
00820         return $this->mTitle;
00821     }
00822 
00828     public function getLocalFile() {
00829         if ( is_null( $this->mLocalFile ) ) {
00830             $nt = $this->getTitle();
00831             $this->mLocalFile = is_null( $nt ) ? null : wfLocalFile( $nt );
00832         }
00833         return $this->mLocalFile;
00834     }
00835 
00848     public function stashFile( User $user = null ) {
00849         // was stashSessionFile
00850         wfProfileIn( __METHOD__ );
00851 
00852         $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $user );
00853         $file = $stash->stashFile( $this->mTempPath, $this->getSourceType() );
00854         $this->mLocalFile = $file;
00855 
00856         wfProfileOut( __METHOD__ );
00857         return $file;
00858     }
00859 
00865     public function stashFileGetKey() {
00866         return $this->stashFile()->getFileKey();
00867     }
00868 
00874     public function stashSession() {
00875         return $this->stashFileGetKey();
00876     }
00877 
00882     public function cleanupTempFile() {
00883         if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
00884             wfDebug( __METHOD__ . ": Removing temporary file {$this->mTempPath}\n" );
00885             unlink( $this->mTempPath );
00886         }
00887     }
00888 
00889     public function getTempPath() {
00890         return $this->mTempPath;
00891     }
00892 
00902     public static function splitExtensions( $filename ) {
00903         $bits = explode( '.', $filename );
00904         $basename = array_shift( $bits );
00905         return array( $basename, $bits );
00906     }
00907 
00916     public static function checkFileExtension( $ext, $list ) {
00917         return in_array( strtolower( $ext ), $list );
00918     }
00919 
00928     public static function checkFileExtensionList( $ext, $list ) {
00929         return array_intersect( array_map( 'strtolower', $ext ), $list );
00930     }
00931 
00939     public static function verifyExtension( $mime, $extension ) {
00940         $magic = MimeMagic::singleton();
00941 
00942         if ( !$mime || $mime == 'unknown' || $mime == 'unknown/unknown' ) {
00943             if ( !$magic->isRecognizableExtension( $extension ) ) {
00944                 wfDebug( __METHOD__ . ": passing file with unknown detected mime type; " .
00945                     "unrecognized extension '$extension', can't verify\n" );
00946                 return true;
00947             } else {
00948                 wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; " .
00949                     "recognized extension '$extension', so probably invalid file\n" );
00950                 return false;
00951             }
00952         }
00953 
00954         $match = $magic->isMatchingExtension( $extension, $mime );
00955 
00956         if ( $match === null ) {
00957             if ( $magic->getTypesForExtension( $extension ) !== null ) {
00958                 wfDebug( __METHOD__ . ": No extension known for $mime, but we know a mime for $extension\n" );
00959                 return false;
00960             } else {
00961                 wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
00962                 return true;
00963             }
00964         } elseif ( $match === true ) {
00965             wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file\n" );
00966 
00967             #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
00968             return true;
00969 
00970         } else {
00971             wfDebug( __METHOD__ . ": mime type $mime mismatches file extension $extension, rejecting file\n" );
00972             return false;
00973         }
00974     }
00975 
00987     public static function detectScript( $file, $mime, $extension ) {
00988         global $wgAllowTitlesInSVG;
00989         wfProfileIn( __METHOD__ );
00990 
00991         # ugly hack: for text files, always look at the entire file.
00992         # For binary field, just check the first K.
00993 
00994         if ( strpos( $mime, 'text/' ) === 0 ) {
00995             $chunk = file_get_contents( $file );
00996         } else {
00997             $fp = fopen( $file, 'rb' );
00998             $chunk = fread( $fp, 1024 );
00999             fclose( $fp );
01000         }
01001 
01002         $chunk = strtolower( $chunk );
01003 
01004         if ( !$chunk ) {
01005             wfProfileOut( __METHOD__ );
01006             return false;
01007         }
01008 
01009         # decode from UTF-16 if needed (could be used for obfuscation).
01010         if ( substr( $chunk, 0, 2 ) == "\xfe\xff" ) {
01011             $enc = 'UTF-16BE';
01012         } elseif ( substr( $chunk, 0, 2 ) == "\xff\xfe" ) {
01013             $enc = 'UTF-16LE';
01014         } else {
01015             $enc = null;
01016         }
01017 
01018         if ( $enc ) {
01019             $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
01020         }
01021 
01022         $chunk = trim( $chunk );
01023 
01024         # @todo FIXME: Convert from UTF-16 if necessary!
01025         wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff\n" );
01026 
01027         # check for HTML doctype
01028         if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) {
01029             wfProfileOut( __METHOD__ );
01030             return true;
01031         }
01032 
01033         // Some browsers will interpret obscure xml encodings as UTF-8, while
01034         // PHP/expat will interpret the given encoding in the xml declaration (bug 47304)
01035         if ( $extension == 'svg' || strpos( $mime, 'image/svg' ) === 0 ) {
01036             if ( self::checkXMLEncodingMissmatch( $file ) ) {
01037                 wfProfileOut( __METHOD__ );
01038                 return true;
01039             }
01040         }
01041 
01057         $tags = array(
01058             '<a href',
01059             '<body',
01060             '<head',
01061             '<html',   #also in safari
01062             '<img',
01063             '<pre',
01064             '<script', #also in safari
01065             '<table'
01066         );
01067 
01068         if ( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
01069             $tags[] = '<title';
01070         }
01071 
01072         foreach ( $tags as $tag ) {
01073             if ( false !== strpos( $chunk, $tag ) ) {
01074                 wfDebug( __METHOD__ . ": found something that may make it be mistaken for html: $tag\n" );
01075                 wfProfileOut( __METHOD__ );
01076                 return true;
01077             }
01078         }
01079 
01080         /*
01081          * look for JavaScript
01082          */
01083 
01084         # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
01085         $chunk = Sanitizer::decodeCharReferences( $chunk );
01086 
01087         # look for script-types
01088         if ( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
01089             wfDebug( __METHOD__ . ": found script types\n" );
01090             wfProfileOut( __METHOD__ );
01091             return true;
01092         }
01093 
01094         # look for html-style script-urls
01095         if ( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
01096             wfDebug( __METHOD__ . ": found html-style script urls\n" );
01097             wfProfileOut( __METHOD__ );
01098             return true;
01099         }
01100 
01101         # look for css-style script-urls
01102         if ( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
01103             wfDebug( __METHOD__ . ": found css-style script urls\n" );
01104             wfProfileOut( __METHOD__ );
01105             return true;
01106         }
01107 
01108         wfDebug( __METHOD__ . ": no scripts found\n" );
01109         wfProfileOut( __METHOD__ );
01110         return false;
01111     }
01112 
01120     public static function checkXMLEncodingMissmatch( $file ) {
01121         global $wgSVGMetadataCutoff;
01122         $contents = file_get_contents( $file, false, null, -1, $wgSVGMetadataCutoff );
01123         $encodingRegex = '!encoding[ \t\n\r]*=[ \t\n\r]*[\'"](.*?)[\'"]!si';
01124 
01125         if ( preg_match( "!<\?xml\b(.*?)\?>!si", $contents, $matches ) ) {
01126             if ( preg_match( $encodingRegex, $matches[1], $encMatch )
01127                 && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
01128             ) {
01129                 wfDebug( __METHOD__ . ": Found unsafe XML encoding '{$encMatch[1]}'\n" );
01130                 return true;
01131             }
01132         } elseif ( preg_match( "!<\?xml\b!si", $contents ) ) {
01133             // Start of XML declaration without an end in the first $wgSVGMetadataCutoff
01134             // bytes. There shouldn't be a legitimate reason for this to happen.
01135             wfDebug( __METHOD__ . ": Unmatched XML declaration start\n" );
01136             return true;
01137         } elseif ( substr( $contents, 0, 4 ) == "\x4C\x6F\xA7\x94" ) {
01138             // EBCDIC encoded XML
01139             wfDebug( __METHOD__ . ": EBCDIC Encoded XML\n" );
01140             return true;
01141         }
01142 
01143         // It's possible the file is encoded with multi-byte encoding, so re-encode attempt to
01144         // detect the encoding in case is specifies an encoding not whitelisted in self::$safeXmlEncodings
01145         $attemptEncodings = array( 'UTF-16', 'UTF-16BE', 'UTF-32', 'UTF-32BE' );
01146         foreach ( $attemptEncodings as $encoding ) {
01147             wfSuppressWarnings();
01148             $str = iconv( $encoding, 'UTF-8', $contents );
01149             wfRestoreWarnings();
01150             if ( $str != '' && preg_match( "!<\?xml\b(.*?)\?>!si", $str, $matches ) ) {
01151                 if ( preg_match( $encodingRegex, $matches[1], $encMatch )
01152                     && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
01153                 ) {
01154                     wfDebug( __METHOD__ . ": Found unsafe XML encoding '{$encMatch[1]}'\n" );
01155                     return true;
01156                 }
01157             } elseif ( $str != '' && preg_match( "!<\?xml\b!si", $str ) ) {
01158                 // Start of XML declaration without an end in the first $wgSVGMetadataCutoff
01159                 // bytes. There shouldn't be a legitimate reason for this to happen.
01160                 wfDebug( __METHOD__ . ": Unmatched XML declaration start\n" );
01161                 return true;
01162             }
01163         }
01164 
01165         return false;
01166     }
01167 
01172     protected function detectScriptInSvg( $filename ) {
01173         $this->mSVGNSError = false;
01174         $check = new XmlTypeCheck(
01175             $filename,
01176             array( $this, 'checkSvgScriptCallback' ),
01177             true,
01178             array( 'processing_instruction_handler' => 'UploadBase::checkSvgPICallback' )
01179         );
01180         if ( $check->wellFormed !== true ) {
01181             // Invalid xml (bug 58553)
01182             return array( 'uploadinvalidxml' );
01183         } elseif ( $check->filterMatch ) {
01184             if ( $this->mSVGNSError ) {
01185                 return array( 'uploadscriptednamespace', $this->mSVGNSError );
01186             }
01187             return array( 'uploadscripted' );
01188         }
01189         return false;
01190     }
01191 
01198     public static function checkSvgPICallback( $target, $data ) {
01199         // Don't allow external stylesheets (bug 57550)
01200         if ( preg_match( '/xml-stylesheet/i', $target ) ) {
01201             return true;
01202         }
01203         return false;
01204     }
01205 
01212     public function checkSvgScriptCallback( $element, $attribs, $data = null ) {
01213 
01214         list( $namespace, $strippedElement ) = $this->splitXmlNamespace( $element );
01215 
01216         static $validNamespaces = array(
01217             '',
01218             'adobe:ns:meta/',
01219             'http://creativecommons.org/ns#',
01220             'http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd',
01221             'http://ns.adobe.com/adobeillustrator/10.0/',
01222             'http://ns.adobe.com/adobesvgviewerextensions/3.0/',
01223             'http://ns.adobe.com/extensibility/1.0/',
01224             'http://ns.adobe.com/flows/1.0/',
01225             'http://ns.adobe.com/illustrator/1.0/',
01226             'http://ns.adobe.com/imagereplacement/1.0/',
01227             'http://ns.adobe.com/pdf/1.3/',
01228             'http://ns.adobe.com/photoshop/1.0/',
01229             'http://ns.adobe.com/saveforweb/1.0/',
01230             'http://ns.adobe.com/variables/1.0/',
01231             'http://ns.adobe.com/xap/1.0/',
01232             'http://ns.adobe.com/xap/1.0/g/',
01233             'http://ns.adobe.com/xap/1.0/g/img/',
01234             'http://ns.adobe.com/xap/1.0/mm/',
01235             'http://ns.adobe.com/xap/1.0/rights/',
01236             'http://ns.adobe.com/xap/1.0/stype/dimensions#',
01237             'http://ns.adobe.com/xap/1.0/stype/font#',
01238             'http://ns.adobe.com/xap/1.0/stype/manifestitem#',
01239             'http://ns.adobe.com/xap/1.0/stype/resourceevent#',
01240             'http://ns.adobe.com/xap/1.0/stype/resourceref#',
01241             'http://ns.adobe.com/xap/1.0/t/pg/',
01242             'http://purl.org/dc/elements/1.1/',
01243             'http://purl.org/dc/elements/1.1',
01244             'http://schemas.microsoft.com/visio/2003/svgextensions/',
01245             'http://sodipodi.sourceforge.net/dtd/sodipodi-0.dtd',
01246             'http://web.resource.org/cc/',
01247             'http://www.freesoftware.fsf.org/bkchem/cdml',
01248             'http://www.inkscape.org/namespaces/inkscape',
01249             'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
01250             'http://www.w3.org/2000/svg',
01251         );
01252 
01253         if ( !in_array( $namespace, $validNamespaces ) ) {
01254             wfDebug( __METHOD__ . ": Non-svg namespace '$namespace' in uploaded file.\n" );
01255             // @TODO return a status object to a closure in XmlTypeCheck, for MW1.21+
01256             $this->mSVGNSError = $namespace;
01257             return true;
01258         }
01259 
01260         /*
01261          * check for elements that can contain javascript
01262          */
01263         if ( $strippedElement == 'script' ) {
01264             wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file.\n" );
01265             return true;
01266         }
01267 
01268         # 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>
01269         if ( $strippedElement == 'handler' ) {
01270             wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file.\n" );
01271             return true;
01272         }
01273 
01274         # SVG reported in Feb '12 that used xml:stylesheet to generate javascript block
01275         if ( $strippedElement == 'stylesheet' ) {
01276             wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file.\n" );
01277             return true;
01278         }
01279 
01280         # Block iframes, in case they pass the namespace check
01281         if ( $strippedElement == 'iframe' ) {
01282             wfDebug( __METHOD__ . ": iframe in uploaded file.\n" );
01283             return true;
01284         }
01285 
01286         # Check <style> css
01287         if ( $strippedElement == 'style'
01288             && self::checkCssFragment( Sanitizer::normalizeCss( $data ) )
01289         ) {
01290             wfDebug( __METHOD__ . ": hostile css in style element.\n" );
01291             return true;
01292         }
01293 
01294         foreach ( $attribs as $attrib => $value ) {
01295             $stripped = $this->stripXmlNamespace( $attrib );
01296             $value = strtolower( $value );
01297 
01298             if ( substr( $stripped, 0, 2 ) == 'on' ) {
01299                 wfDebug( __METHOD__ . ": Found event-handler attribute '$attrib'='$value' in uploaded file.\n" );
01300                 return true;
01301             }
01302 
01303             # href with non-local target (don't allow http://, javascript:, etc)
01304             if ( $stripped == 'href'
01305                 && strpos( $value, 'data:' ) !== 0
01306                 && strpos( $value, '#' ) !== 0
01307             ) {
01308                 if ( !( $strippedElement === 'a'
01309                     && preg_match( '!^https?://!im', $value ) )
01310                 ) {
01311                     wfDebug( __METHOD__ . ": Found href attribute <$strippedElement "
01312                         . "'$attrib'='$value' in uploaded file.\n" );
01313 
01314                     return true;
01315                 }
01316             }
01317 
01318             # href with embedded svg as target
01319             if ( $stripped == 'href' && preg_match( '!data:[^,]*image/svg[^,]*,!sim', $value ) ) {
01320                 wfDebug( __METHOD__ . ": Found href to embedded svg \"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
01321                 return true;
01322             }
01323 
01324             # href with embedded (text/xml) svg as target
01325             if ( $stripped == 'href' && preg_match( '!data:[^,]*text/xml[^,]*,!sim', $value ) ) {
01326                 wfDebug( __METHOD__ . ": Found href to embedded svg \"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
01327                 return true;
01328             }
01329 
01330             # Change href with animate from (http://html5sec.org/#137). This doesn't seem
01331             # possible without embedding the svg, but filter here in case.
01332             if ( $stripped == 'from'
01333                 && $strippedElement === 'animate'
01334                 && !preg_match( '!^https?://!im', $value )
01335             ) {
01336                 wfDebug( __METHOD__ . ": Found animate that might be changing href using from "
01337                     . "\"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
01338 
01339                 return true;
01340             }
01341 
01342             # use set/animate to add event-handler attribute to parent
01343             if ( ( $strippedElement == 'set' || $strippedElement == 'animate' ) && $stripped == 'attributename' && substr( $value, 0, 2 ) == 'on' ) {
01344                 wfDebug( __METHOD__ . ": Found svg setting event-handler attribute with \"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
01345                 return true;
01346             }
01347 
01348             # use set to add href attribute to parent element
01349             if ( $strippedElement == 'set' && $stripped == 'attributename' && strpos( $value, 'href' ) !== false ) {
01350                 wfDebug( __METHOD__ . ": Found svg setting href attribute '$value' in uploaded file.\n" );
01351                 return true;
01352             }
01353 
01354             # use set to add a remote / data / script target to an element
01355             if ( $strippedElement == 'set' && $stripped == 'to' && preg_match( '!(http|https|data|script):!sim', $value ) ) {
01356                 wfDebug( __METHOD__ . ": Found svg setting attribute to '$value' in uploaded file.\n" );
01357                 return true;
01358             }
01359 
01360             # use handler attribute with remote / data / script
01361             if ( $stripped == 'handler' && preg_match( '!(http|https|data|script):!sim', $value ) ) {
01362                 wfDebug( __METHOD__ . ": Found svg setting handler with remote/data/script '$attrib'='$value' in uploaded file.\n" );
01363                 return true;
01364             }
01365 
01366             # use CSS styles to bring in remote code
01367             if ( $stripped == 'style'
01368                 && self::checkCssFragment( Sanitizer::normalizeCss( $value ) )
01369             ) {
01370                 wfDebug( __METHOD__ . ": Found svg setting a style with "
01371                     . "remote url '$attrib'='$value' in uploaded file.\n" );
01372                 return true;
01373             }
01374 
01375             # Several attributes can include css, css character escaping isn't allowed
01376             $cssAttrs = array( 'font', 'clip-path', 'fill', 'filter', 'marker',
01377                 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke' );
01378             if ( in_array( $stripped, $cssAttrs )
01379                 && self::checkCssFragment( $value )
01380             ) {
01381                 wfDebug( __METHOD__ . ": Found svg setting a style with "
01382                     . "remote url '$attrib'='$value' in uploaded file.\n" );
01383                 return true;
01384             }
01385 
01386             # image filters can pull in url, which could be svg that executes scripts
01387             if ( $strippedElement == 'image' && $stripped == 'filter' && preg_match( '!url\s*\(!sim', $value ) ) {
01388                 wfDebug( __METHOD__ . ": Found image filter with url: \"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
01389                 return true;
01390             }
01391 
01392         }
01393 
01394         return false; //No scripts detected
01395     }
01396 
01404     private static function checkCssFragment( $value ) {
01405 
01406         # Forbid external stylesheets, for both reliability and to protect viewer's privacy
01407         if ( strpos( $value, '@import' ) !== false ) {
01408             return true;
01409         }
01410 
01411         # We allow @font-face to embed fonts with data: urls, so we snip the string
01412         # 'url' out so this case won't match when we check for urls below
01413         $pattern = '!(@font-face\s*{[^}]*src:)url(\("data:;base64,)!im';
01414         $value = preg_replace( $pattern, '$1$2', $value );
01415 
01416         # Check for remote and executable CSS. Unlike in Sanitizer::checkCss, the CSS
01417         # properties filter and accelerator don't seem to be useful for xss in SVG files.
01418         # Expression and -o-link don't seem to work either, but filtering them here in case.
01419         # Additionally, we catch remote urls like url("http:..., url('http:..., url(http:...,
01420         # but not local ones such as url("#..., url('#..., url(#....
01421         if ( preg_match( '!expression
01422                 | -o-link\s*:
01423                 | -o-link-source\s*:
01424                 | -o-replace\s*:!imx', $value ) ) {
01425             return true;
01426         }
01427 
01428         if ( preg_match_all(
01429                 "!(\s*(url|image|image-set)\s*\(\s*[\"']?\s*[^#]+.*?\))!sim",
01430                 $value,
01431                 $matches
01432             ) !== 0
01433         ) {
01434             # TODO: redo this in one regex. Until then, url("#whatever") matches the first
01435             foreach ( $matches[1] as $match ) {
01436                 if ( !preg_match( "!\s*(url|image|image-set)\s*\(\s*(#|'#|\"#)!im", $match ) ) {
01437                     return true;
01438                 }
01439             }
01440         }
01441 
01442         if ( preg_match( '/[\000-\010\013\016-\037\177]/', $value ) ) {
01443             return true;
01444         }
01445 
01446         return false;
01447     }
01448 
01454     private static function splitXmlNamespace( $element ) {
01455         // 'http://www.w3.org/2000/svg:script' -> array( 'http://www.w3.org/2000/svg', 'script' )
01456         $parts = explode( ':', strtolower( $element ) );
01457         $name = array_pop( $parts );
01458         $ns = implode( ':', $parts );
01459         return array( $ns, $name );
01460     }
01461 
01466     private function stripXmlNamespace( $name ) {
01467         // 'http://www.w3.org/2000/svg:script' -> 'script'
01468         $parts = explode( ':', strtolower( $name ) );
01469         return array_pop( $parts );
01470     }
01471 
01482     public static function detectVirus( $file ) {
01483         global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
01484         wfProfileIn( __METHOD__ );
01485 
01486         if ( !$wgAntivirus ) {
01487             wfDebug( __METHOD__ . ": virus scanner disabled\n" );
01488             wfProfileOut( __METHOD__ );
01489             return null;
01490         }
01491 
01492         if ( !$wgAntivirusSetup[$wgAntivirus] ) {
01493             wfDebug( __METHOD__ . ": unknown virus scanner: $wgAntivirus\n" );
01494             $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>",
01495                 array( 'virus-badscanner', $wgAntivirus ) );
01496             wfProfileOut( __METHOD__ );
01497             return wfMessage( 'virus-unknownscanner' )->text() . " $wgAntivirus";
01498         }
01499 
01500         # look up scanner configuration
01501         $command = $wgAntivirusSetup[$wgAntivirus]['command'];
01502         $exitCodeMap = $wgAntivirusSetup[$wgAntivirus]['codemap'];
01503         $msgPattern = isset( $wgAntivirusSetup[$wgAntivirus]['messagepattern'] ) ?
01504             $wgAntivirusSetup[$wgAntivirus]['messagepattern'] : null;
01505 
01506         if ( strpos( $command, "%f" ) === false ) {
01507             # simple pattern: append file to scan
01508             $command .= " " . wfEscapeShellArg( $file );
01509         } else {
01510             # complex pattern: replace "%f" with file to scan
01511             $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
01512         }
01513 
01514         wfDebug( __METHOD__ . ": running virus scan: $command \n" );
01515 
01516         # execute virus scanner
01517         $exitCode = false;
01518 
01519         # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
01520         #      that does not seem to be worth the pain.
01521         #      Ask me (Duesentrieb) about it if it's ever needed.
01522         $output = wfShellExecWithStderr( $command, $exitCode );
01523 
01524         # map exit code to AV_xxx constants.
01525         $mappedCode = $exitCode;
01526         if ( $exitCodeMap ) {
01527             if ( isset( $exitCodeMap[$exitCode] ) ) {
01528                 $mappedCode = $exitCodeMap[$exitCode];
01529             } elseif ( isset( $exitCodeMap["*"] ) ) {
01530                 $mappedCode = $exitCodeMap["*"];
01531             }
01532         }
01533 
01534         /* NB: AV_NO_VIRUS is 0 but AV_SCAN_FAILED is false,
01535          * so we need the strict equalities === and thus can't use a switch here
01536          */
01537         if ( $mappedCode === AV_SCAN_FAILED ) {
01538             # scan failed (code was mapped to false by $exitCodeMap)
01539             wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
01540 
01541             $output = $wgAntivirusRequired ? wfMessage( 'virus-scanfailed', array( $exitCode ) )->text() : null;
01542         } elseif ( $mappedCode === AV_SCAN_ABORTED ) {
01543             # scan failed because filetype is unknown (probably imune)
01544             wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
01545             $output = null;
01546         } elseif ( $mappedCode === AV_NO_VIRUS ) {
01547             # no virus found
01548             wfDebug( __METHOD__ . ": file passed virus scan.\n" );
01549             $output = false;
01550         } else {
01551             $output = trim( $output );
01552 
01553             if ( !$output ) {
01554                 $output = true; #if there's no output, return true
01555             } elseif ( $msgPattern ) {
01556                 $groups = array();
01557                 if ( preg_match( $msgPattern, $output, $groups ) ) {
01558                     if ( $groups[1] ) {
01559                         $output = $groups[1];
01560                     }
01561                 }
01562             }
01563 
01564             wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
01565         }
01566 
01567         wfProfileOut( __METHOD__ );
01568         return $output;
01569     }
01570 
01579     private function checkOverwrite( $user ) {
01580         // First check whether the local file can be overwritten
01581         $file = $this->getLocalFile();
01582         if ( $file->exists() ) {
01583             if ( !self::userCanReUpload( $user, $file ) ) {
01584                 return array( 'fileexists-forbidden', $file->getName() );
01585             } else {
01586                 return true;
01587             }
01588         }
01589 
01590         /* Check shared conflicts: if the local file does not exist, but
01591          * wfFindFile finds a file, it exists in a shared repository.
01592          */
01593         $file = wfFindFile( $this->getTitle() );
01594         if ( $file && !$user->isAllowed( 'reupload-shared' ) ) {
01595             return array( 'fileexists-shared-forbidden', $file->getName() );
01596         }
01597 
01598         return true;
01599     }
01600 
01608     public static function userCanReUpload( User $user, $img ) {
01609         if ( $user->isAllowed( 'reupload' ) ) {
01610             return true; // non-conditional
01611         }
01612         if ( !$user->isAllowed( 'reupload-own' ) ) {
01613             return false;
01614         }
01615         if ( is_string( $img ) ) {
01616             $img = wfLocalFile( $img );
01617         }
01618         if ( !( $img instanceof LocalFile ) ) {
01619             return false;
01620         }
01621 
01622         return $user->getId() == $img->getUser( 'id' );
01623     }
01624 
01636     public static function getExistsWarning( $file ) {
01637         if ( $file->exists() ) {
01638             return array( 'warning' => 'exists', 'file' => $file );
01639         }
01640 
01641         if ( $file->getTitle()->getArticleID() ) {
01642             return array( 'warning' => 'page-exists', 'file' => $file );
01643         }
01644 
01645         if ( $file->wasDeleted() && !$file->exists() ) {
01646             return array( 'warning' => 'was-deleted', 'file' => $file );
01647         }
01648 
01649         if ( strpos( $file->getName(), '.' ) == false ) {
01650             $partname = $file->getName();
01651             $extension = '';
01652         } else {
01653             $n = strrpos( $file->getName(), '.' );
01654             $extension = substr( $file->getName(), $n + 1 );
01655             $partname = substr( $file->getName(), 0, $n );
01656         }
01657         $normalizedExtension = File::normalizeExtension( $extension );
01658 
01659         if ( $normalizedExtension != $extension ) {
01660             // We're not using the normalized form of the extension.
01661             // Normal form is lowercase, using most common of alternate
01662             // extensions (eg 'jpg' rather than 'JPEG').
01663             //
01664             // Check for another file using the normalized form...
01665             $nt_lc = Title::makeTitle( NS_FILE, "{$partname}.{$normalizedExtension}" );
01666             $file_lc = wfLocalFile( $nt_lc );
01667 
01668             if ( $file_lc->exists() ) {
01669                 return array(
01670                     'warning' => 'exists-normalized',
01671                     'file' => $file,
01672                     'normalizedFile' => $file_lc
01673                 );
01674             }
01675         }
01676 
01677         // Check for files with the same name but a different extension
01678         $similarFiles = RepoGroup::singleton()->getLocalRepo()->findFilesByPrefix(
01679                 "{$partname}.", 1 );
01680         if ( count( $similarFiles ) ) {
01681             return array(
01682                 'warning' => 'exists-normalized',
01683                 'file' => $file,
01684                 'normalizedFile' => $similarFiles[0],
01685             );
01686         }
01687 
01688         if ( self::isThumbName( $file->getName() ) ) {
01689             # Check for filenames like 50px- or 180px-, these are mostly thumbnails
01690             $nt_thb = Title::newFromText( substr( $partname, strpos( $partname, '-' ) + 1 ) . '.' . $extension, NS_FILE );
01691             $file_thb = wfLocalFile( $nt_thb );
01692             if ( $file_thb->exists() ) {
01693                 return array(
01694                     'warning' => 'thumb',
01695                     'file' => $file,
01696                     'thumbFile' => $file_thb
01697                 );
01698             } else {
01699                 // File does not exist, but we just don't like the name
01700                 return array(
01701                     'warning' => 'thumb-name',
01702                     'file' => $file,
01703                     'thumbFile' => $file_thb
01704                 );
01705             }
01706         }
01707 
01708         foreach ( self::getFilenamePrefixBlacklist() as $prefix ) {
01709             if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
01710                 return array(
01711                     'warning' => 'bad-prefix',
01712                     'file' => $file,
01713                     'prefix' => $prefix
01714                 );
01715             }
01716         }
01717 
01718         return false;
01719     }
01720 
01726     public static function isThumbName( $filename ) {
01727         $n = strrpos( $filename, '.' );
01728         $partname = $n ? substr( $filename, 0, $n ) : $filename;
01729         return (
01730                     substr( $partname, 3, 3 ) == 'px-' ||
01731                     substr( $partname, 2, 3 ) == 'px-'
01732                 ) &&
01733                 preg_match( "/[0-9]{2}/", substr( $partname, 0, 2 ) );
01734     }
01735 
01741     public static function getFilenamePrefixBlacklist() {
01742         $blacklist = array();
01743         $message = wfMessage( 'filename-prefix-blacklist' )->inContentLanguage();
01744         if ( !$message->isDisabled() ) {
01745             $lines = explode( "\n", $message->plain() );
01746             foreach ( $lines as $line ) {
01747                 // Remove comment lines
01748                 $comment = substr( trim( $line ), 0, 1 );
01749                 if ( $comment == '#' || $comment == '' ) {
01750                     continue;
01751                 }
01752                 // Remove additional comments after a prefix
01753                 $comment = strpos( $line, '#' );
01754                 if ( $comment > 0 ) {
01755                     $line = substr( $line, 0, $comment - 1 );
01756                 }
01757                 $blacklist[] = trim( $line );
01758             }
01759         }
01760         return $blacklist;
01761     }
01762 
01773     public function getImageInfo( $result ) {
01774         $file = $this->getLocalFile();
01775         // TODO This cries out for refactoring. We really want to say $file->getAllInfo(); here.
01776         // Perhaps "info" methods should be moved into files, and the API should just wrap them in queries.
01777         if ( $file instanceof UploadStashFile ) {
01778             $imParam = ApiQueryStashImageInfo::getPropertyNames();
01779             $info = ApiQueryStashImageInfo::getInfo( $file, array_flip( $imParam ), $result );
01780         } else {
01781             $imParam = ApiQueryImageInfo::getPropertyNames();
01782             $info = ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
01783         }
01784         return $info;
01785     }
01786 
01791     public function convertVerifyErrorToStatus( $error ) {
01792         $code = $error['status'];
01793         unset( $code['status'] );
01794         return Status::newFatal( $this->getVerificationErrorCode( $code ), $error );
01795     }
01796 
01801     public static function getMaxUploadSize( $forType = null ) {
01802         global $wgMaxUploadSize;
01803 
01804         if ( is_array( $wgMaxUploadSize ) ) {
01805             if ( !is_null( $forType ) && isset( $wgMaxUploadSize[$forType] ) ) {
01806                 return $wgMaxUploadSize[$forType];
01807             } else {
01808                 return $wgMaxUploadSize['*'];
01809             }
01810         } else {
01811             return intval( $wgMaxUploadSize );
01812         }
01813     }
01814 
01821     public static function getSessionStatus( $statusKey ) {
01822         return isset( $_SESSION[self::SESSION_STATUS_KEY][$statusKey] )
01823             ? $_SESSION[self::SESSION_STATUS_KEY][$statusKey]
01824             : false;
01825     }
01826 
01834     public static function setSessionStatus( $statusKey, $value ) {
01835         if ( $value === false ) {
01836             unset( $_SESSION[self::SESSION_STATUS_KEY][$statusKey] );
01837         } else {
01838             $_SESSION[self::SESSION_STATUS_KEY][$statusKey] = $value;
01839         }
01840     }
01841 }