00001 <?php
00016 abstract class UploadBase {
00017 protected $mTempPath;
00018 protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
00019 protected $mTitle = false, $mTitleError = 0;
00020 protected $mFilteredName, $mFinalExtension;
00021 protected $mLocalFile;
00022
00023 const SUCCESS = 0;
00024 const OK = 0;
00025 const EMPTY_FILE = 3;
00026 const MIN_LENGTH_PARTNAME = 4;
00027 const ILLEGAL_FILENAME = 5;
00028 const OVERWRITE_EXISTING_FILE = 7;
00029 const FILETYPE_MISSING = 8;
00030 const FILETYPE_BADTYPE = 9;
00031 const VERIFICATION_ERROR = 10;
00032 const UPLOAD_VERIFICATION_ERROR = 11;
00033 const HOOK_ABORTED = 11;
00034
00035 const SESSION_VERSION = 2;
00036
00041 public static function isEnabled() {
00042 global $wgEnableUploads;
00043 if ( !$wgEnableUploads ) {
00044 return false;
00045 }
00046
00047 # Check php's file_uploads setting
00048 if( !wfIniGetBool( 'file_uploads' ) ) {
00049 return false;
00050 }
00051 return true;
00052 }
00053
00059 public static function isAllowed( $user ) {
00060 if( !$user->isAllowed( 'upload' ) ) {
00061 return 'upload';
00062 }
00063 return true;
00064 }
00065
00066
00067 static $uploadHandlers = array( 'Stash', 'File', 'Url' );
00068
00072 public static function createFromRequest( &$request, $type = null ) {
00073 $type = $type ? $type : $request->getVal( 'wpSourceType', 'File' );
00074
00075 if( !$type ) {
00076 return null;
00077 }
00078
00079
00080 $type = ucfirst( $type );
00081
00082
00083 $className = null;
00084 wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
00085 if ( is_null( $className ) ) {
00086 $className = 'UploadFrom' . $type;
00087 wfDebug( __METHOD__ . ": class name: $className\n" );
00088 if( !in_array( $type, self::$uploadHandlers ) ) {
00089 return null;
00090 }
00091 }
00092
00093
00094 if( !call_user_func( array( $className, 'isEnabled' ) ) ) {
00095 return null;
00096 }
00097
00098
00099 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) ) {
00100 return null;
00101 }
00102
00103 $handler = new $className;
00104
00105 $handler->initializeFromRequest( $request );
00106 return $handler;
00107 }
00108
00112 public static function isValidRequest( $request ) {
00113 return false;
00114 }
00115
00116 public function __construct() {}
00117
00126 public function initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile = false ) {
00127 $this->mDesiredDestName = $name;
00128 $this->mTempPath = $tempPath;
00129 $this->mFileSize = $fileSize;
00130 $this->mRemoveTempFile = $removeTempFile;
00131 }
00132
00136 public abstract function initializeFromRequest( &$request );
00137
00141 public function fetchFile() {
00142 return Status::newGood();
00143 }
00144
00148 public function isEmptyFile() {
00149 return empty( $this->mFileSize );
00150 }
00151
00156 function getRealPath( $srcPath ) {
00157 $repo = RepoGroup::singleton()->getLocalRepo();
00158 if ( $repo->isVirtualUrl( $srcPath ) ) {
00159 return $repo->resolveVirtualUrl( $srcPath );
00160 }
00161 return $srcPath;
00162 }
00163
00168 public function verifyUpload() {
00172 if( $this->isEmptyFile() ) {
00173 return array( 'status' => self::EMPTY_FILE );
00174 }
00175
00181 $verification = $this->verifyFile();
00182 if( $verification !== true ) {
00183 if( !is_array( $verification ) ) {
00184 $verification = array( $verification );
00185 }
00186 return array(
00187 'status' => self::VERIFICATION_ERROR,
00188 'details' => $verification
00189 );
00190 }
00191
00192 $nt = $this->getTitle();
00193 if( is_null( $nt ) ) {
00194 $result = array( 'status' => $this->mTitleError );
00195 if( $this->mTitleError == self::ILLEGAL_FILENAME ) {
00196 $result['filtered'] = $this->mFilteredName;
00197 }
00198 if ( $this->mTitleError == self::FILETYPE_BADTYPE ) {
00199 $result['finalExt'] = $this->mFinalExtension;
00200 }
00201 return $result;
00202 }
00203 $this->mDestName = $this->getLocalFile()->getName();
00204
00208 $overwrite = $this->checkOverwrite();
00209 if( $overwrite !== true ) {
00210 return array(
00211 'status' => self::OVERWRITE_EXISTING_FILE,
00212 'overwrite' => $overwrite
00213 );
00214 }
00215
00216 $error = '';
00217 if( !wfRunHooks( 'UploadVerification',
00218 array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
00219
00220 return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
00221 }
00222
00223 return array( 'status' => self::OK );
00224 }
00225
00231 protected function verifyFile() {
00232 $this->mFileProps = File::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
00233 $this->checkMacBinary();
00234
00235 # magically determine mime type
00236 $magic = MimeMagic::singleton();
00237 $mime = $magic->guessMimeType( $this->mTempPath, false );
00238
00239 # check mime type, if desired
00240 global $wgVerifyMimeType;
00241 if ( $wgVerifyMimeType ) {
00242 wfDebug ( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n");
00243 if ( !$this->verifyExtension( $mime, $this->mFinalExtension ) ) {
00244 return array( 'filetype-mime-mismatch' );
00245 }
00246
00247 global $wgMimeTypeBlacklist;
00248 if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
00249 return array( 'filetype-badmime', $mime );
00250 }
00251
00252 # Check IE type
00253 $fp = fopen( $this->mTempPath, 'rb' );
00254 $chunk = fread( $fp, 256 );
00255 fclose( $fp );
00256 $extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
00257 $ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
00258 foreach ( $ieTypes as $ieType ) {
00259 if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
00260 return array( 'filetype-bad-ie-mime', $ieType );
00261 }
00262 }
00263 }
00264
00265 # check for htmlish code and javascript
00266 if( self::detectScript( $this->mTempPath, $mime, $this->mFinalExtension ) ) {
00267 return 'uploadscripted';
00268 }
00269 if( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
00270 if( self::detectScriptInSvg( $this->mTempPath ) ) {
00271 return 'uploadscripted';
00272 }
00273 }
00274
00278 $virus = $this->detectVirus( $this->mTempPath );
00279 if ( $virus ) {
00280 return array( 'uploadvirus', $virus );
00281 }
00282 wfDebug( __METHOD__ . ": all clear; passing.\n" );
00283 return true;
00284 }
00285
00293 public function verifyPermissions( $user ) {
00298 $nt = $this->getTitle();
00299 if( is_null( $nt ) ) {
00300 return true;
00301 }
00302 $permErrors = $nt->getUserPermissionsErrors( 'edit', $user );
00303 $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $user );
00304 $permErrorsCreate = ( $nt->exists() ? array() : $nt->getUserPermissionsErrors( 'create', $user ) );
00305 if( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
00306 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
00307 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
00308 return $permErrors;
00309 }
00310 return true;
00311 }
00312
00318 public function checkWarnings() {
00319 $warnings = array();
00320
00321 $localFile = $this->getLocalFile();
00322 $filename = $localFile->getName();
00323 $n = strrpos( $filename, '.' );
00324 $partname = $n ? substr( $filename, 0, $n ) : $filename;
00325
00330 $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
00331 $comparableName = Title::capitalize( $comparableName, NS_FILE );
00332
00333 if( $this->mDesiredDestName != $filename && $comparableName != $filename ) {
00334 $warnings['badfilename'] = $filename;
00335 }
00336
00337
00338 global $wgCheckFileExtensions, $wgFileExtensions;
00339 if ( $wgCheckFileExtensions ) {
00340 if ( !$this->checkFileExtension( $this->mFinalExtension, $wgFileExtensions ) ) {
00341 $warnings['filetype-unwanted-type'] = $this->mFinalExtension;
00342 }
00343 }
00344
00345 global $wgUploadSizeWarning;
00346 if ( $wgUploadSizeWarning && ( $this->mFileSize > $wgUploadSizeWarning ) ) {
00347 $warnings['large-file'] = $wgUploadSizeWarning;
00348 }
00349
00350 if ( $this->mFileSize == 0 ) {
00351 $warnings['emptyfile'] = true;
00352 }
00353
00354 $exists = self::getExistsWarning( $localFile );
00355 if( $exists !== false ) {
00356 $warnings['exists'] = $exists;
00357 }
00358
00359
00360 $hash = File::sha1Base36( $this->mTempPath );
00361 $dupes = RepoGroup::singleton()->findBySha1( $hash );
00362 $title = $this->getTitle();
00363
00364 foreach ( $dupes as $key => $dupe ) {
00365 if( $title->equals( $dupe->getTitle() ) ) {
00366 unset( $dupes[$key] );
00367 }
00368 }
00369 if( $dupes ) {
00370 $warnings['duplicate'] = $dupes;
00371 }
00372
00373
00374 $archivedImage = new ArchivedFile( null, 0, "{$hash}.{$this->mFinalExtension}" );
00375 if ( $archivedImage->getID() > 0 ) {
00376 $warnings['duplicate-archive'] = $archivedImage->getName();
00377 }
00378
00379 return $warnings;
00380 }
00381
00388 public function performUpload( $comment, $pageText, $watch, $user ) {
00389 wfDebug( "\n\n\performUpload: sum:" . $comment . ' c: ' . $pageText . ' w:' . $watch );
00390 $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
00391 File::DELETE_SOURCE, $this->mFileProps, false, $user );
00392
00393 if( $status->isGood() && $watch ) {
00394 $user->addWatch( $this->getLocalFile()->getTitle() );
00395 }
00396
00397 if( $status->isGood() ) {
00398 wfRunHooks( 'UploadComplete', array( &$this ) );
00399 }
00400
00401 return $status;
00402 }
00403
00410 public function getTitle() {
00411 if ( $this->mTitle !== false ) {
00412 return $this->mTitle;
00413 }
00414
00420 $basename = $this->mDesiredDestName;
00421
00422 $this->mFilteredName = wfStripIllegalFilenameChars( $basename );
00423
00424 $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00425 if( is_null( $nt ) ) {
00426 $this->mTitleError = self::ILLEGAL_FILENAME;
00427 return $this->mTitle = null;
00428 }
00429 $this->mFilteredName = $nt->getDBkey();
00430
00435 list( $partname, $ext ) = $this->splitExtensions( $this->mFilteredName );
00436
00437 if( count( $ext ) ) {
00438 $this->mFinalExtension = trim( $ext[count( $ext ) - 1] );
00439 } else {
00440 $this->mFinalExtension = '';
00441 }
00442
00443
00444 global $wgCheckFileExtensions, $wgStrictFileExtensions;
00445 global $wgFileExtensions, $wgFileBlacklist;
00446 if ( $this->mFinalExtension == '' ) {
00447 $this->mTitleError = self::FILETYPE_MISSING;
00448 return $this->mTitle = null;
00449 } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) ||
00450 ( $wgCheckFileExtensions && $wgStrictFileExtensions &&
00451 !$this->checkFileExtension( $this->mFinalExtension, $wgFileExtensions ) ) ) {
00452 $this->mTitleError = self::FILETYPE_BADTYPE;
00453 return $this->mTitle = null;
00454 }
00455
00456 # If there was more than one "extension", reassemble the base
00457 # filename to prevent bogus complaints about length
00458 if( count( $ext ) > 1 ) {
00459 for( $i = 0; $i < count( $ext ) - 1; $i++ ) {
00460 $partname .= '.' . $ext[$i];
00461 }
00462 }
00463
00464 if( strlen( $partname ) < 1 ) {
00465 $this->mTitleError = self::MIN_LENGTH_PARTNAME;
00466 return $this->mTitle = null;
00467 }
00468
00469 $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
00470 if( is_null( $nt ) ) {
00471 $this->mTitleError = self::ILLEGAL_FILENAME;
00472 return $this->mTitle = null;
00473 }
00474 return $this->mTitle = $nt;
00475 }
00476
00480 public function getLocalFile() {
00481 if( is_null( $this->mLocalFile ) ) {
00482 $nt = $this->getTitle();
00483 $this->mLocalFile = is_null( $nt ) ? null : wfLocalFile( $nt );
00484 }
00485 return $this->mLocalFile;
00486 }
00487
00499 protected function saveTempUploadedFile( $saveName, $tempSrc ) {
00500 $repo = RepoGroup::singleton()->getLocalRepo();
00501 $status = $repo->storeTemp( $saveName, $tempSrc );
00502 return $status;
00503 }
00504
00513 public function stashSession() {
00514 $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
00515 if( !$status->isOK() ) {
00516 # Couldn't save the file.
00517 return false;
00518 }
00519 if( !isset( $_SESSION ) ) {
00520 session_start();
00521 }
00522 $key = $this->getSessionKey();
00523 $_SESSION['wsUploadData'][$key] = array(
00524 'mTempPath' => $status->value,
00525 'mFileSize' => $this->mFileSize,
00526 'mFileProps' => $this->mFileProps,
00527 'version' => self::SESSION_VERSION,
00528 );
00529 return $key;
00530 }
00531
00535 protected function getSessionKey() {
00536 $key = mt_rand( 0, 0x7fffffff );
00537 $_SESSION['wsUploadData'][$key] = array();
00538 return $key;
00539 }
00540
00545 public function cleanupTempFile() {
00546 if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
00547 wfDebug( __METHOD__ . ": Removing temporary file {$this->mTempPath}\n" );
00548 unlink( $this->mTempPath );
00549 }
00550 }
00551
00552 public function getTempPath() {
00553 return $this->mTempPath;
00554 }
00555
00564 public static function splitExtensions( $filename ) {
00565 $bits = explode( '.', $filename );
00566 $basename = array_shift( $bits );
00567 return array( $basename, $bits );
00568 }
00569
00578 public static function checkFileExtension( $ext, $list ) {
00579 return in_array( strtolower( $ext ), $list );
00580 }
00581
00590 public static function checkFileExtensionList( $ext, $list ) {
00591 foreach( $ext as $e ) {
00592 if( in_array( strtolower( $e ), $list ) ) {
00593 return true;
00594 }
00595 }
00596 return false;
00597 }
00598
00606 public static function verifyExtension( $mime, $extension ) {
00607 $magic = MimeMagic::singleton();
00608
00609 if ( !$mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
00610 if ( !$magic->isRecognizableExtension( $extension ) ) {
00611 wfDebug( __METHOD__ . ": passing file with unknown detected mime type; " .
00612 "unrecognized extension '$extension', can't verify\n" );
00613 return true;
00614 } else {
00615 wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; ".
00616 "recognized extension '$extension', so probably invalid file\n" );
00617 return false;
00618 }
00619
00620 $match = $magic->isMatchingExtension( $extension, $mime );
00621
00622 if ( $match === null ) {
00623 wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
00624 return true;
00625 } elseif( $match === true ) {
00626 wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file\n" );
00627
00628 #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
00629 return true;
00630
00631 } else {
00632 wfDebug( __METHOD__ . ": mime type $mime mismatches file extension $extension, rejecting file\n" );
00633 return false;
00634 }
00635 }
00636
00648 public static function detectScript( $file, $mime, $extension ) {
00649 global $wgAllowTitlesInSVG;
00650
00651 # ugly hack: for text files, always look at the entire file.
00652 # For binary field, just check the first K.
00653
00654 if( strpos( $mime,'text/' ) === 0 ) {
00655 $chunk = file_get_contents( $file );
00656 } else {
00657 $fp = fopen( $file, 'rb' );
00658 $chunk = fread( $fp, 1024 );
00659 fclose( $fp );
00660 }
00661
00662 $chunk = strtolower( $chunk );
00663
00664 if( !$chunk ) {
00665 return false;
00666 }
00667
00668 # decode from UTF-16 if needed (could be used for obfuscation).
00669 if( substr( $chunk, 0, 2 ) == "\xfe\xff" ) {
00670 $enc = 'UTF-16BE';
00671 } elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" ) {
00672 $enc = 'UTF-16LE';
00673 } else {
00674 $enc = null;
00675 }
00676
00677 if( $enc ) {
00678 $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
00679 }
00680
00681 $chunk = trim( $chunk );
00682
00683 # FIXME: convert from UTF-16 if necessarry!
00684 wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff\n" );
00685
00686 # check for HTML doctype
00687 if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) {
00688 return true;
00689 }
00690
00706 $tags = array(
00707 '<a href',
00708 '<body',
00709 '<head',
00710 '<html', #also in safari
00711 '<img',
00712 '<pre',
00713 '<script', #also in safari
00714 '<table'
00715 );
00716
00717 if( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
00718 $tags[] = '<title';
00719 }
00720
00721 foreach( $tags as $tag ) {
00722 if( false !== strpos( $chunk, $tag ) ) {
00723 return true;
00724 }
00725 }
00726
00727
00728
00729
00730
00731 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
00732 $chunk = Sanitizer::decodeCharReferences( $chunk );
00733
00734 # look for script-types
00735 if( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
00736 return true;
00737 }
00738
00739 # look for html-style script-urls
00740 if( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
00741 return true;
00742 }
00743
00744 # look for css-style script-urls
00745 if( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
00746 return true;
00747 }
00748
00749 wfDebug( __METHOD__ . ": no scripts found\n" );
00750 return false;
00751 }
00752
00753 protected function detectScriptInSvg( $filename ) {
00754 $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
00755 return $check->filterMatch;
00756 }
00757
00761 public function checkSvgScriptCallback( $element, $attribs ) {
00762 $stripped = $this->stripXmlNamespace( $element );
00763
00764 if( $stripped == 'script' ) {
00765 wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file.\n" );
00766 return true;
00767 }
00768
00769 foreach( $attribs as $attrib => $value ) {
00770 $stripped = $this->stripXmlNamespace( $attrib );
00771 if( substr( $stripped, 0, 2 ) == 'on' ) {
00772 wfDebug( __METHOD__ . ": Found script attribute '$attrib'='value' in uploaded file.\n" );
00773 return true;
00774 }
00775 if( $stripped == 'href' && strpos( strtolower( $value ), 'javascript:' ) !== false ) {
00776 wfDebug( __METHOD__ . ": Found script href attribute '$attrib'='$value' in uploaded file.\n" );
00777 return true;
00778 }
00779 }
00780 }
00781
00782 private function stripXmlNamespace( $name ) {
00783
00784 $parts = explode( ':', strtolower( $name ) );
00785 return array_pop( $parts );
00786 }
00787
00798 public static function detectVirus( $file ) {
00799 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
00800
00801 if ( !$wgAntivirus ) {
00802 wfDebug( __METHOD__ . ": virus scanner disabled\n" );
00803 return null;
00804 }
00805
00806 if ( !$wgAntivirusSetup[$wgAntivirus] ) {
00807 wfDebug( __METHOD__ . ": unknown virus scanner: $wgAntivirus\n" );
00808 $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1</div>", array( 'virus-badscanner', $wgAntivirus ) );
00809 return wfMsg( 'virus-unknownscanner' ) . " $wgAntivirus";
00810 }
00811
00812 # look up scanner configuration
00813 $command = $wgAntivirusSetup[$wgAntivirus]['command'];
00814 $exitCodeMap = $wgAntivirusSetup[$wgAntivirus]['codemap'];
00815 $msgPattern = isset( $wgAntivirusSetup[$wgAntivirus]['messagepattern'] ) ?
00816 $wgAntivirusSetup[$wgAntivirus]['messagepattern'] : null;
00817
00818 if ( strpos( $command, "%f" ) === false ) {
00819 # simple pattern: append file to scan
00820 $command .= " " . wfEscapeShellArg( $file );
00821 } else {
00822 # complex pattern: replace "%f" with file to scan
00823 $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
00824 }
00825
00826 wfDebug( __METHOD__ . ": running virus scan: $command \n" );
00827
00828 # execute virus scanner
00829 $exitCode = false;
00830
00831 # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
00832 # that does not seem to be worth the pain.
00833 # Ask me (Duesentrieb) about it if it's ever needed.
00834 $output = wfShellExec( "$command 2>&1", $exitCode );
00835
00836 # map exit code to AV_xxx constants.
00837 $mappedCode = $exitCode;
00838 if ( $exitCodeMap ) {
00839 if ( isset( $exitCodeMap[$exitCode] ) ) {
00840 $mappedCode = $exitCodeMap[$exitCode];
00841 } elseif ( isset( $exitCodeMap["*"] ) ) {
00842 $mappedCode = $exitCodeMap["*"];
00843 }
00844 }
00845
00846 if ( $mappedCode === AV_SCAN_FAILED ) {
00847 # scan failed (code was mapped to false by $exitCodeMap)
00848 wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
00849
00850 if ( $wgAntivirusRequired ) {
00851 return wfMsg( 'virus-scanfailed', array( $exitCode ) );
00852 } else {
00853 return null;
00854 }
00855 } elseif ( $mappedCode === AV_SCAN_ABORTED ) {
00856 # scan failed because filetype is unknown (probably imune)
00857 wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
00858 return null;
00859 } elseif ( $mappedCode === AV_NO_VIRUS ) {
00860 # no virus found
00861 wfDebug( __METHOD__ . ": file passed virus scan.\n" );
00862 return false;
00863 } else {
00864 $output = trim( $output );
00865
00866 if ( !$output ) {
00867 $output = true; #if there's no output, return true
00868 } elseif ( $msgPattern ) {
00869 $groups = array();
00870 if ( preg_match( $msgPattern, $output, $groups ) ) {
00871 if ( $groups[1] ) {
00872 $output = $groups[1];
00873 }
00874 }
00875 }
00876
00877 wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
00878 return $output;
00879 }
00880 }
00881
00888 private function checkMacBinary() {
00889 $macbin = new MacBinary( $this->mTempPath );
00890 if( $macbin->isValid() ) {
00891 $dataFile = tempnam( wfTempDir(), 'WikiMacBinary' );
00892 $dataHandle = fopen( $dataFile, 'wb' );
00893
00894 wfDebug( __METHOD__ . ": Extracting MacBinary data fork to $dataFile\n" );
00895 $macbin->extractData( $dataHandle );
00896
00897 $this->mTempPath = $dataFile;
00898 $this->mFileSize = $macbin->dataForkLength();
00899
00900 // We'll have to manually remove the new file if it's not kept.
00901 $this->mRemoveTempFile = true;
00902 }
00903 $macbin->close();
00904 }
00905
00912 private function checkOverwrite() {
00913 global $wgUser;
00914 // First check whether the local file can be overwritten
00915 $file = $this->getLocalFile();
00916 if( $file->exists() ) {
00917 if( !self::userCanReUpload( $wgUser, $file ) ) {
00918 return 'fileexists-forbidden';
00919 } else {
00920 return true;
00921 }
00922 }
00923
00924 /* Check shared conflicts: if the local file does not exist, but
00925 * wfFindFile finds a file, it exists in a shared repository.
00926 */
00927 $file = wfFindFile( $this->getTitle() );
00928 if ( $file && !$wgUser->isAllowed( 'reupload-shared' ) ) {
00929 return 'fileexists-shared-forbidden';
00930 }
00931
00932 return true;
00933 }
00934
00942 public static function userCanReUpload( User $user, $img ) {
00943 if( $user->isAllowed( 'reupload' ) ) {
00944 return true; // non-conditional
00945 }
00946 if( !$user->isAllowed( 'reupload-own' ) ) {
00947 return false;
00948 }
00949 if( is_string( $img ) ) {
00950 $img = wfLocalFile( $img );
00951 }
00952 if ( !( $img instanceof LocalFile ) ) {
00953 return false;
00954 }
00955
00956 return $user->getId() == $img->getUser( 'id' );
00957 }
00958
00970 public static function getExistsWarning( $file ) {
00971 if( $file->exists() ) {
00972 return array( 'warning' => 'exists', 'file' => $file );
00973 }
00974
00975 if( $file->getTitle()->getArticleID() ) {
00976 return array( 'warning' => 'page-exists', 'file' => $file );
00977 }
00978
00979 if ( $file->wasDeleted() && !$file->exists() ) {
00980 return array( 'warning' => 'was-deleted', 'file' => $file );
00981 }
00982
00983 if( strpos( $file->getName(), '.' ) == false ) {
00984 $partname = $file->getName();
00985 $extension = '';
00986 } else {
00987 $n = strrpos( $file->getName(), '.' );
00988 $extension = substr( $file->getName(), $n + 1 );
00989 $partname = substr( $file->getName(), 0, $n );
00990 }
00991 $normalizedExtension = File::normalizeExtension( $extension );
00992
00993 if ( $normalizedExtension != $extension ) {
00994 // We're not using the normalized form of the extension.
00995
00996
00997
00998
00999 $nt_lc = Title::makeTitle( NS_FILE, "{$partname}.{$normalizedExtension}" );
01000 $file_lc = wfLocalFile( $nt_lc );
01001
01002 if( $file_lc->exists() ) {
01003 return array(
01004 'warning' => 'exists-normalized',
01005 'file' => $file,
01006 'normalizedFile' => $file_lc
01007 );
01008 }
01009 }
01010
01011 if ( self::isThumbName( $file->getName() ) ) {
01012 # Check for filenames like 50px- or 180px-, these are mostly thumbnails
01013 $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $extension, NS_FILE );
01014 $file_thb = wfLocalFile( $nt_thb );
01015 if( $file_thb->exists() ) {
01016 return array(
01017 'warning' => 'thumb',
01018 'file' => $file,
01019 'thumbFile' => $file_thb
01020 );
01021 } else {
01022
01023 return array(
01024 'warning' => 'thumb-name',
01025 'file' => $file,
01026 'thumbFile' => $file_thb
01027 );
01028 }
01029 }
01030
01031
01032 foreach( self::getFilenamePrefixBlacklist() as $prefix ) {
01033 if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
01034 return array(
01035 'warning' => 'bad-prefix',
01036 'file' => $file,
01037 'prefix' => $prefix
01038 );
01039 }
01040 }
01041
01042 return false;
01043 }
01044
01048 public static function isThumbName( $filename ) {
01049 $n = strrpos( $filename, '.' );
01050 $partname = $n ? substr( $filename, 0, $n ) : $filename;
01051 return (
01052 substr( $partname , 3, 3 ) == 'px-' ||
01053 substr( $partname , 2, 3 ) == 'px-'
01054 ) &&
01055 preg_match( "/[0-9]{2}/" , substr( $partname , 0, 2 ) );
01056 }
01057
01063 public static function getFilenamePrefixBlacklist() {
01064 $blacklist = array();
01065 $message = wfMsgForContent( 'filename-prefix-blacklist' );
01066 if( $message && !( wfEmptyMsg( 'filename-prefix-blacklist', $message ) || $message == '-' ) ) {
01067 $lines = explode( "\n", $message );
01068 foreach( $lines as $line ) {
01069
01070 $comment = substr( trim( $line ), 0, 1 );
01071 if ( $comment == '#' || $comment == '' ) {
01072 continue;
01073 }
01074
01075 $comment = strpos( $line, '#' );
01076 if ( $comment > 0 ) {
01077 $line = substr( $line, 0, $comment-1 );
01078 }
01079 $blacklist[] = trim( $line );
01080 }
01081 }
01082 return $blacklist;
01083 }
01084
01085 public function getImageInfo( $result ) {
01086 $file = $this->getLocalFile();
01087 $imParam = ApiQueryImageInfo::getPropertyNames();
01088 return ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
01089 }
01090
01091 }