6 * UploadBase and subclasses are the backend of MediaWiki's file uploads.
7 * The frontends are formed by ApiUpload and SpecialUpload.
9 * See also includes/docs/upload.txt
11 * @author Brion Vibber
12 * @author Bryan Tong Minh
13 * @author Michael Dale
16 abstract class UploadBase
{
18 protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
19 protected $mTitle = false, $mTitleError = 0;
20 protected $mFilteredName, $mFinalExtension;
21 protected $mLocalFile, $mFileSize, $mFileProps;
22 protected $mBlackListedExtensions;
23 protected $mJavaDetected;
28 const MIN_LENGTH_PARTNAME
= 4;
29 const ILLEGAL_FILENAME
= 5;
30 const OVERWRITE_EXISTING_FILE
= 7; # Not used anymore; handled by verifyTitlePermissions()
31 const FILETYPE_MISSING
= 8;
32 const FILETYPE_BADTYPE
= 9;
33 const VERIFICATION_ERROR
= 10;
35 # HOOK_ABORTED is the new name of UPLOAD_VERIFICATION_ERROR
36 const UPLOAD_VERIFICATION_ERROR
= 11;
37 const HOOK_ABORTED
= 11;
38 const FILE_TOO_LARGE
= 12;
39 const WINDOWS_NONASCII_FILENAME
= 13;
41 const SESSION_VERSION
= 2;
42 const SESSION_KEYNAME
= 'wsUploadData';
44 static public function getSessionKeyname() {
45 return self
::SESSION_KEYNAME
;
48 public function getVerificationErrorCode( $error ) {
49 $code_to_status = array(self
::EMPTY_FILE
=> 'empty-file',
50 self
::FILE_TOO_LARGE
=> 'file-too-large',
51 self
::FILETYPE_MISSING
=> 'filetype-missing',
52 self
::FILETYPE_BADTYPE
=> 'filetype-banned',
53 self
::MIN_LENGTH_PARTNAME
=> 'filename-tooshort',
54 self
::ILLEGAL_FILENAME
=> 'illegal-filename',
55 self
::OVERWRITE_EXISTING_FILE
=> 'overwrite',
56 self
::VERIFICATION_ERROR
=> 'verification-error',
57 self
::HOOK_ABORTED
=> 'hookaborted',
58 self
::WINDOWS_NONASCII_FILENAME
=> 'windows-nonascii-filename',
60 if( isset( $code_to_status[$error] ) ) {
61 return $code_to_status[$error];
64 return 'unknown-error';
68 * Returns true if uploads are enabled.
69 * Can be override by subclasses.
71 public static function isEnabled() {
72 global $wgEnableUploads;
73 if ( !$wgEnableUploads ) {
77 # Check php's file_uploads setting
78 if( !wfIniGetBool( 'file_uploads' ) ) {
85 * Returns true if the user can use this upload module or else a string
86 * identifying the missing permission.
87 * Can be overriden by subclasses.
91 public static function isAllowed( $user ) {
92 foreach ( array( 'upload', 'edit' ) as $permission ) {
93 if ( !$user->isAllowed( $permission ) ) {
100 // Upload handlers. Should probably just be a global.
101 static $uploadHandlers = array( 'Stash', 'File', 'Url' );
104 * Create a form of UploadBase depending on wpSourceType and initializes it
106 * @param $request WebRequest
108 public static function createFromRequest( &$request, $type = null ) {
109 $type = $type ?
$type : $request->getVal( 'wpSourceType', 'File' );
115 // Get the upload class
116 $type = ucfirst( $type );
118 // Give hooks the chance to handle this request
120 wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
121 if ( is_null( $className ) ) {
122 $className = 'UploadFrom' . $type;
123 wfDebug( __METHOD__
. ": class name: $className\n" );
124 if( !in_array( $type, self
::$uploadHandlers ) ) {
129 // Check whether this upload class is enabled
130 if( !call_user_func( array( $className, 'isEnabled' ) ) ) {
134 // Check whether the request is valid
135 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) ) {
139 $handler = new $className;
141 $handler->initializeFromRequest( $request );
146 * Check whether a request if valid for this handler
148 public static function isValidRequest( $request ) {
152 public function __construct() {}
155 * Returns the upload type. Should be overridden by child classes
160 public function getSourceType() { return null; }
163 * Initialize the path information
164 * @param $name string the desired destination name
165 * @param $tempPath string the temporary path
166 * @param $fileSize int the file size
167 * @param $removeTempFile bool (false) remove the temporary file?
170 public function initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile = false ) {
171 $this->mDesiredDestName
= $name;
172 $this->mTempPath
= $tempPath;
173 $this->mFileSize
= $fileSize;
174 $this->mRemoveTempFile
= $removeTempFile;
178 * Initialize from a WebRequest. Override this in a subclass.
180 public abstract function initializeFromRequest( &$request );
183 * Fetch the file. Usually a no-op
185 public function fetchFile() {
186 return Status
::newGood();
190 * Return true if the file is empty
193 public function isEmptyFile() {
194 return empty( $this->mFileSize
);
198 * Return the file size
201 public function getFileSize() {
202 return $this->mFileSize
;
206 * Append a file to the Repo file
208 * @param $srcPath String: path to source file
209 * @param $toAppendPath String: path to the Repo file that will be appended to.
210 * @return Status Status
212 protected function appendToUploadFile( $srcPath, $toAppendPath ) {
213 $repo = RepoGroup
::singleton()->getLocalRepo();
214 $status = $repo->append( $srcPath, $toAppendPath );
219 * @param $srcPath String: the source path
220 * @return the real path if it was a virtual URL
222 function getRealPath( $srcPath ) {
223 $repo = RepoGroup
::singleton()->getLocalRepo();
224 if ( $repo->isVirtualUrl( $srcPath ) ) {
225 return $repo->resolveVirtualUrl( $srcPath );
231 * Verify whether the upload is sane.
232 * @return mixed self::OK or else an array with error information
234 public function verifyUpload() {
236 * If there was no filename or a zero size given, give up quick.
238 if( $this->isEmptyFile() ) {
239 return array( 'status' => self
::EMPTY_FILE
);
243 * Honor $wgMaxUploadSize
245 $maxSize = self
::getMaxUploadSize( $this->getSourceType() );
246 if( $this->mFileSize
> $maxSize ) {
248 'status' => self
::FILE_TOO_LARGE
,
254 * Look at the contents of the file; if we can recognize the
255 * type but it's corrupt or data of the wrong type, we should
256 * probably not accept it.
258 $verification = $this->verifyFile();
259 if( $verification !== true ) {
261 'status' => self
::VERIFICATION_ERROR
,
262 'details' => $verification
267 * Make sure this file can be created
269 $result = $this->validateName();
270 if( $result !== true ) {
275 if( !wfRunHooks( 'UploadVerification',
276 array( $this->mDestName
, $this->mTempPath
, &$error ) ) ) {
277 return array( 'status' => self
::HOOK_ABORTED
, 'error' => $error );
280 return array( 'status' => self
::OK
);
284 * Verify that the name is valid and, if necessary, that we can overwrite
286 * @return mixed true if valid, otherwise and array with 'status'
289 protected function validateName() {
290 $nt = $this->getTitle();
291 if( is_null( $nt ) ) {
292 $result = array( 'status' => $this->mTitleError
);
293 if( $this->mTitleError
== self
::ILLEGAL_FILENAME
) {
294 $result['filtered'] = $this->mFilteredName
;
296 if ( $this->mTitleError
== self
::FILETYPE_BADTYPE
) {
297 $result['finalExt'] = $this->mFinalExtension
;
298 if ( count( $this->mBlackListedExtensions
) ) {
299 $result['blacklistedExt'] = $this->mBlackListedExtensions
;
304 $this->mDestName
= $this->getLocalFile()->getName();
310 * Verify the mime type
312 * @param $mime string representing the mime
313 * @return mixed true if the file is verified, an array otherwise
315 protected function verifyMimeType( $mime ) {
316 global $wgVerifyMimeType;
317 if ( $wgVerifyMimeType ) {
318 wfDebug ( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n");
319 global $wgMimeTypeBlacklist;
320 if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
321 return array( 'filetype-badmime', $mime );
324 # XXX: Missing extension will be caught by validateName() via getTitle()
325 if ( $this->mFinalExtension
!= '' && !$this->verifyExtension( $mime, $this->mFinalExtension
) ) {
326 return array( 'filetype-mime-mismatch', $this->mFinalExtension
, $mime );
330 $fp = fopen( $this->mTempPath
, 'rb' );
331 $chunk = fread( $fp, 256 );
334 $magic = MimeMagic
::singleton();
335 $extMime = $magic->guessTypesForExtension( $this->mFinalExtension
);
336 $ieTypes = $magic->getIEMimeTypes( $this->mTempPath
, $chunk, $extMime );
337 foreach ( $ieTypes as $ieType ) {
338 if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
339 return array( 'filetype-bad-ie-mime', $ieType );
348 * Verifies that it's ok to include the uploaded file
350 * @return mixed true of the file is verified, array otherwise.
352 protected function verifyFile() {
353 global $wgAllowJavaUploads;
354 # get the title, even though we are doing nothing with it, because
355 # we need to populate mFinalExtension
358 $this->mFileProps
= File
::getPropsFromPath( $this->mTempPath
, $this->mFinalExtension
);
359 $this->checkMacBinary();
361 # check mime type, if desired
362 $mime = $this->mFileProps
[ 'file-mime' ];
363 $status = $this->verifyMimeType( $mime );
364 if ( $status !== true ) {
368 # check for htmlish code and javascript
369 if( self
::detectScript( $this->mTempPath
, $mime, $this->mFinalExtension
) ) {
370 return array( 'uploadscripted' );
372 if( $this->mFinalExtension
== 'svg' ||
$mime == 'image/svg+xml' ) {
373 if( $this->detectScriptInSvg( $this->mTempPath
) ) {
374 return array( 'uploadscripted' );
378 # Check for Java applets, which if uploaded can bypass cross-site
380 if ( !$wgAllowJavaUploads ) {
381 $this->mJavaDetected
= false;
382 $zipStatus = ZipDirectoryReader
::read( $this->mTempPath
,
383 array( $this, 'zipEntryCallback' ) );
384 if ( !$zipStatus->isOK() ) {
385 $errors = $zipStatus->getErrorsArray();
386 $error = reset( $errors );
387 if ( $error[0] !== 'zip-wrong-format' ) {
391 if ( $this->mJavaDetected
) {
392 return array( 'uploadjava' );
396 # Scan the uploaded file for viruses
397 $virus = $this->detectVirus( $this->mTempPath
);
399 return array( 'uploadvirus', $virus );
402 $handler = MediaHandler
::getHandler( $mime );
404 $handlerStatus = $handler->verifyUpload( $this->mTempPath
);
405 if ( !$handlerStatus->isOK() ) {
406 $errors = $handlerStatus->getErrorsArray();
407 return reset( $errors );
411 wfRunHooks( 'UploadVerifyFile', array( $this, $mime, &$status ) );
412 if ( $status !== true ) {
416 wfDebug( __METHOD__
. ": all clear; passing.\n" );
421 * Callback for ZipDirectoryReader to detect Java class files.
423 function zipEntryCallback( $entry ) {
424 $names = array( $entry['name'] );
426 // If there is a null character, cut off the name at it, because JDK's
427 // ZIP_GetEntry() uses strcmp() if the name hashes match. If a file name
428 // were constructed which had ".class\0" followed by a string chosen to
429 // make the hash collide with the truncated name, that file could be
430 // returned in response to a request for the .class file.
431 $nullPos = strpos( $entry['name'], "\000" );
432 if ( $nullPos !== false ) {
433 $names[] = substr( $entry['name'], 0, $nullPos );
436 // If there is a trailing slash in the file name, we have to strip it,
437 // because that's what ZIP_GetEntry() does.
438 if ( preg_grep( '!\.class/?$!', $names ) ) {
439 $this->mJavaDetected
= true;
444 * Alias for verifyTitlePermissions. The function was originally 'verifyPermissions'
445 * but that suggests it's checking the user, when it's really checking the title + user combination.
446 * @param $user User object to verify the permissions against
447 * @return mixed An array as returned by getUserPermissionsErrors or true
448 * in case the user has proper permissions.
450 public function verifyPermissions( $user ) {
451 return $this->verifyTitlePermissions( $user );
455 * Check whether the user can edit, upload and create the image. This
456 * checks only against the current title; if it returns errors, it may
457 * very well be that another title will not give errors. Therefore
458 * isAllowed() should be called as well for generic is-user-blocked or
459 * can-user-upload checking.
461 * @param $user User object to verify the permissions against
462 * @return mixed An array as returned by getUserPermissionsErrors or true
463 * in case the user has proper permissions.
465 public function verifyTitlePermissions( $user ) {
467 * If the image is protected, non-sysop users won't be able
468 * to modify it by uploading a new revision.
470 $nt = $this->getTitle();
471 if( is_null( $nt ) ) {
474 $permErrors = $nt->getUserPermissionsErrors( 'edit', $user );
475 $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $user );
476 if ( !$nt->exists() ) {
477 $permErrorsCreate = $nt->getUserPermissionsErrors( 'createpage', $user );
479 $permErrorsCreate = array();
481 if( $permErrors ||
$permErrorsUpload ||
$permErrorsCreate ) {
482 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
483 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
487 $overwriteError = $this->checkOverwrite( $user );
488 if ( $overwriteError !== true ) {
489 return array( $overwriteError );
496 * Check for non fatal problems with the file
498 * @return Array of warnings
500 public function checkWarnings() {
503 $localFile = $this->getLocalFile();
504 $filename = $localFile->getName();
507 * Check whether the resulting filename is different from the desired one,
508 * but ignore things like ucfirst() and spaces/underscore things
510 $comparableName = str_replace( ' ', '_', $this->mDesiredDestName
);
511 $comparableName = Title
::capitalize( $comparableName, NS_FILE
);
513 if( $this->mDesiredDestName
!= $filename && $comparableName != $filename ) {
514 $warnings['badfilename'] = $filename;
517 // Check whether the file extension is on the unwanted list
518 global $wgCheckFileExtensions, $wgFileExtensions;
519 if ( $wgCheckFileExtensions ) {
520 if ( !$this->checkFileExtension( $this->mFinalExtension
, $wgFileExtensions ) ) {
521 $warnings['filetype-unwanted-type'] = $this->mFinalExtension
;
525 global $wgUploadSizeWarning;
526 if ( $wgUploadSizeWarning && ( $this->mFileSize
> $wgUploadSizeWarning ) ) {
527 $warnings['large-file'] = $wgUploadSizeWarning;
530 if ( $this->mFileSize
== 0 ) {
531 $warnings['emptyfile'] = true;
534 $exists = self
::getExistsWarning( $localFile );
535 if( $exists !== false ) {
536 $warnings['exists'] = $exists;
539 // Check dupes against existing files
540 $hash = File
::sha1Base36( $this->mTempPath
);
541 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
542 $title = $this->getTitle();
543 // Remove all matches against self
544 foreach ( $dupes as $key => $dupe ) {
545 if( $title->equals( $dupe->getTitle() ) ) {
546 unset( $dupes[$key] );
550 $warnings['duplicate'] = $dupes;
553 // Check dupes against archives
554 $archivedImage = new ArchivedFile( null, 0, "{$hash}.{$this->mFinalExtension}" );
555 if ( $archivedImage->getID() > 0 ) {
556 $warnings['duplicate-archive'] = $archivedImage->getName();
563 * Really perform the upload. Stores the file in the local repo, watches
564 * if necessary and runs the UploadComplete hook.
568 * @return Status indicating the whether the upload succeeded.
570 public function performUpload( $comment, $pageText, $watch, $user ) {
571 $status = $this->getLocalFile()->upload(
581 if( $status->isGood() ) {
583 $user->addWatch( $this->getLocalFile()->getTitle() );
586 wfRunHooks( 'UploadComplete', array( &$this ) );
593 * Returns the title of the file to be uploaded. Sets mTitleError in case
594 * the name was illegal.
596 * @return Title The title of the file or null in case the name was illegal
598 public function getTitle() {
599 if ( $this->mTitle
!== false ) {
600 return $this->mTitle
;
603 /* Assume that if a user specified File:Something.jpg, this is an error
604 * and that the namespace prefix needs to be stripped of.
606 $title = Title
::newFromText( $this->mDesiredDestName
);
607 if ( $title->getNamespace() == NS_FILE
) {
608 $this->mFilteredName
= $title->getDBkey();
610 $this->mFilteredName
= $this->mDesiredDestName
;
614 * Chop off any directories in the given filename. Then
615 * filter out illegal characters, and try to make a legible name
616 * out of it. We'll strip some silently that Title would die on.
618 $this->mFilteredName
= wfStripIllegalFilenameChars( $this->mFilteredName
);
619 /* Normalize to title form before we do any further processing */
620 $nt = Title
::makeTitleSafe( NS_FILE
, $this->mFilteredName
);
621 if( is_null( $nt ) ) {
622 $this->mTitleError
= self
::ILLEGAL_FILENAME
;
623 return $this->mTitle
= null;
625 $this->mFilteredName
= $nt->getDBkey();
628 * We'll want to blacklist against *any* 'extension', and use
629 * only the final one for the whitelist.
631 list( $partname, $ext ) = $this->splitExtensions( $this->mFilteredName
);
633 if( count( $ext ) ) {
634 $this->mFinalExtension
= trim( $ext[count( $ext ) - 1] );
636 $this->mFinalExtension
= '';
638 # No extension, try guessing one
639 $magic = MimeMagic
::singleton();
640 $mime = $magic->guessMimeType( $this->mTempPath
);
641 if ( $mime !== 'unknown/unknown' ) {
642 # Get a space separated list of extensions
643 $extList = $magic->getExtensionsForType( $mime );
645 # Set the extension to the canonical extension
646 $this->mFinalExtension
= strtok( $extList, ' ' );
648 # Fix up the other variables
649 $this->mFilteredName
.= ".{$this->mFinalExtension}";
650 $nt = Title
::makeTitleSafe( NS_FILE
, $this->mFilteredName
);
651 $ext = array( $this->mFinalExtension
);
657 /* Don't allow users to override the blacklist (check file extension) */
658 global $wgCheckFileExtensions, $wgStrictFileExtensions;
659 global $wgFileExtensions, $wgFileBlacklist;
661 $blackListedExtensions = $this->checkFileExtensionList( $ext, $wgFileBlacklist );
663 if ( $this->mFinalExtension
== '' ) {
664 $this->mTitleError
= self
::FILETYPE_MISSING
;
665 return $this->mTitle
= null;
666 } elseif ( $blackListedExtensions ||
667 ( $wgCheckFileExtensions && $wgStrictFileExtensions &&
668 !$this->checkFileExtension( $this->mFinalExtension
, $wgFileExtensions ) ) ) {
669 $this->mBlackListedExtensions
= $blackListedExtensions;
670 $this->mTitleError
= self
::FILETYPE_BADTYPE
;
671 return $this->mTitle
= null;
674 // Windows may be broken with special characters, see bug XXX
675 if ( wfIsWindows() && !preg_match( '/^[\x0-\x7f]*$/', $nt->getText() ) ) {
676 $this->mTitleError
= self
::WINDOWS_NONASCII_FILENAME
;
677 return $this->mTitle
= null;
680 # If there was more than one "extension", reassemble the base
681 # filename to prevent bogus complaints about length
682 if( count( $ext ) > 1 ) {
683 for( $i = 0; $i < count( $ext ) - 1; $i++
) {
684 $partname .= '.' . $ext[$i];
688 if( strlen( $partname ) < 1 ) {
689 $this->mTitleError
= self
::MIN_LENGTH_PARTNAME
;
690 return $this->mTitle
= null;
693 return $this->mTitle
= $nt;
697 * Return the local file and initializes if necessary.
701 public function getLocalFile() {
702 if( is_null( $this->mLocalFile
) ) {
703 $nt = $this->getTitle();
704 $this->mLocalFile
= is_null( $nt ) ?
null : wfLocalFile( $nt );
706 return $this->mLocalFile
;
710 * NOTE: Probably should be deprecated in favor of UploadStash, but this is sometimes
711 * called outside that context.
713 * Stash a file in a temporary directory for later processing
714 * after the user has confirmed it.
716 * If the user doesn't explicitly cancel or accept, these files
717 * can accumulate in the temp directory.
719 * @param $saveName String: the destination filename
720 * @param $tempSrc String: the source temporary file to save
721 * @return String: full path the stashed file, or false on failure
723 protected function saveTempUploadedFile( $saveName, $tempSrc ) {
724 $repo = RepoGroup
::singleton()->getLocalRepo();
725 $status = $repo->storeTemp( $saveName, $tempSrc );
730 * If the user does not supply all necessary information in the first upload form submission (either by accident or
731 * by design) then we may want to stash the file temporarily, get more information, and publish the file later.
733 * This method will stash a file in a temporary directory for later processing, and save the necessary descriptive info
734 * into the user's session.
735 * This method returns the file object, which also has a 'sessionKey' property which can be passed through a form or
736 * API request to find this stashed file again.
738 * @param $key String: (optional) the session key used to find the file info again. If not supplied, a key will be autogenerated.
739 * @return UploadStashFile stashed file
741 public function stashSessionFile( $key = null ) {
742 $stash = RepoGroup
::singleton()->getLocalRepo()->getUploadStash();
744 'mFileProps' => $this->mFileProps
,
745 'mSourceType' => $this->getSourceType(),
747 $file = $stash->stashFile( $this->mTempPath
, $data, $key );
748 $this->mLocalFile
= $file;
753 * Stash a file in a temporary directory, returning a key which can be used to find the file again. See stashSessionFile().
755 * @param $key String: (optional) the session key used to find the file info again. If not supplied, a key will be autogenerated.
756 * @return String: session key
758 public function stashSession( $key = null ) {
759 return $this->stashSessionFile( $key )->getSessionKey();
763 * If we've modified the upload file we need to manually remove it
764 * on exit to clean up.
766 public function cleanupTempFile() {
767 if ( $this->mRemoveTempFile
&& $this->mTempPath
&& file_exists( $this->mTempPath
) ) {
768 wfDebug( __METHOD__
. ": Removing temporary file {$this->mTempPath}\n" );
769 unlink( $this->mTempPath
);
773 public function getTempPath() {
774 return $this->mTempPath
;
778 * Split a file into a base name and all dot-delimited 'extensions'
779 * on the end. Some web server configurations will fall back to
780 * earlier pseudo-'extensions' to determine type and execute
781 * scripts, so the blacklist needs to check them all.
785 public static function splitExtensions( $filename ) {
786 $bits = explode( '.', $filename );
787 $basename = array_shift( $bits );
788 return array( $basename, $bits );
792 * Perform case-insensitive match against a list of file extensions.
793 * Returns true if the extension is in the list.
799 public static function checkFileExtension( $ext, $list ) {
800 return in_array( strtolower( $ext ), $list );
804 * Perform case-insensitive match against a list of file extensions.
805 * Returns an array of matching extensions.
811 public static function checkFileExtensionList( $ext, $list ) {
812 return array_intersect( array_map( 'strtolower', $ext ), $list );
816 * Checks if the mime type of the uploaded file matches the file extension.
818 * @param $mime String: the mime type of the uploaded file
819 * @param $extension String: the filename extension that the file is to be served with
822 public static function verifyExtension( $mime, $extension ) {
823 $magic = MimeMagic
::singleton();
825 if ( !$mime ||
$mime == 'unknown' ||
$mime == 'unknown/unknown' )
826 if ( !$magic->isRecognizableExtension( $extension ) ) {
827 wfDebug( __METHOD__
. ": passing file with unknown detected mime type; " .
828 "unrecognized extension '$extension', can't verify\n" );
831 wfDebug( __METHOD__
. ": rejecting file with unknown detected mime type; ".
832 "recognized extension '$extension', so probably invalid file\n" );
836 $match = $magic->isMatchingExtension( $extension, $mime );
838 if ( $match === null ) {
839 wfDebug( __METHOD__
. ": no file extension known for mime type $mime, passing file\n" );
841 } elseif( $match === true ) {
842 wfDebug( __METHOD__
. ": mime type $mime matches extension $extension, passing file\n" );
844 #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
848 wfDebug( __METHOD__
. ": mime type $mime mismatches file extension $extension, rejecting file\n" );
854 * Heuristic for detecting files that *could* contain JavaScript instructions or
855 * things that may look like HTML to a browser and are thus
856 * potentially harmful. The present implementation will produce false
857 * positives in some situations.
859 * @param $file String: pathname to the temporary upload file
860 * @param $mime String: the mime type of the file
861 * @param $extension String: the extension of the file
862 * @return Boolean: true if the file contains something looking like embedded scripts
864 public static function detectScript( $file, $mime, $extension ) {
865 global $wgAllowTitlesInSVG;
867 # ugly hack: for text files, always look at the entire file.
868 # For binary field, just check the first K.
870 if( strpos( $mime,'text/' ) === 0 ) {
871 $chunk = file_get_contents( $file );
873 $fp = fopen( $file, 'rb' );
874 $chunk = fread( $fp, 1024 );
878 $chunk = strtolower( $chunk );
884 # decode from UTF-16 if needed (could be used for obfuscation).
885 if( substr( $chunk, 0, 2 ) == "\xfe\xff" ) {
887 } elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" ) {
894 $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
897 $chunk = trim( $chunk );
899 # FIXME: convert from UTF-16 if necessarry!
900 wfDebug( __METHOD__
. ": checking for embedded scripts and HTML stuff\n" );
902 # check for HTML doctype
903 if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) {
908 * Internet Explorer for Windows performs some really stupid file type
909 * autodetection which can cause it to interpret valid image files as HTML
910 * and potentially execute JavaScript, creating a cross-site scripting
913 * Apple's Safari browser also performs some unsafe file type autodetection
914 * which can cause legitimate files to be interpreted as HTML if the
915 * web server is not correctly configured to send the right content-type
916 * (or if you're really uploading plain text and octet streams!)
918 * Returns true if IE is likely to mistake the given file for HTML.
919 * Also returns true if Safari would mistake the given file for HTML
920 * when served with a generic content-type.
926 '<html', #also in safari
929 '<script', #also in safari
933 if( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
937 foreach( $tags as $tag ) {
938 if( false !== strpos( $chunk, $tag ) ) {
939 wfDebug( __METHOD__
. ": found something that may make it be mistaken for html: $tag\n" );
945 * look for JavaScript
948 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
949 $chunk = Sanitizer
::decodeCharReferences( $chunk );
951 # look for script-types
952 if( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
953 wfDebug( __METHOD__
. ": found script types\n" );
957 # look for html-style script-urls
958 if( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
959 wfDebug( __METHOD__
. ": found html-style script urls\n" );
963 # look for css-style script-urls
964 if( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
965 wfDebug( __METHOD__
. ": found css-style script urls\n" );
969 wfDebug( __METHOD__
. ": no scripts found\n" );
973 protected function detectScriptInSvg( $filename ) {
974 $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
975 return $check->filterMatch
;
979 * @todo Replace this with a whitelist filter!
981 public function checkSvgScriptCallback( $element, $attribs ) {
982 $stripped = $this->stripXmlNamespace( $element );
984 if( $stripped == 'script' ) {
985 wfDebug( __METHOD__
. ": Found script element '$element' in uploaded file.\n" );
989 foreach( $attribs as $attrib => $value ) {
990 $stripped = $this->stripXmlNamespace( $attrib );
991 if( substr( $stripped, 0, 2 ) == 'on' ) {
992 wfDebug( __METHOD__
. ": Found script attribute '$attrib'='value' in uploaded file.\n" );
995 if( $stripped == 'href' && strpos( strtolower( $value ), 'javascript:' ) !== false ) {
996 wfDebug( __METHOD__
. ": Found script href attribute '$attrib'='$value' in uploaded file.\n" );
1002 private function stripXmlNamespace( $name ) {
1003 // 'http://www.w3.org/2000/svg:script' -> 'script'
1004 $parts = explode( ':', strtolower( $name ) );
1005 return array_pop( $parts );
1009 * Generic wrapper function for a virus scanner program.
1010 * This relies on the $wgAntivirus and $wgAntivirusSetup variables.
1011 * $wgAntivirusRequired may be used to deny upload if the scan fails.
1013 * @param $file String: pathname to the temporary upload file
1014 * @return mixed false if not virus is found, NULL if the scan fails or is disabled,
1015 * or a string containing feedback from the virus scanner if a virus was found.
1016 * If textual feedback is missing but a virus was found, this function returns true.
1018 public static function detectVirus( $file ) {
1019 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
1021 if ( !$wgAntivirus ) {
1022 wfDebug( __METHOD__
. ": virus scanner disabled\n" );
1026 if ( !$wgAntivirusSetup[$wgAntivirus] ) {
1027 wfDebug( __METHOD__
. ": unknown virus scanner: $wgAntivirus\n" );
1028 $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>",
1029 array( 'virus-badscanner', $wgAntivirus ) );
1030 return wfMsg( 'virus-unknownscanner' ) . " $wgAntivirus";
1033 # look up scanner configuration
1034 $command = $wgAntivirusSetup[$wgAntivirus]['command'];
1035 $exitCodeMap = $wgAntivirusSetup[$wgAntivirus]['codemap'];
1036 $msgPattern = isset( $wgAntivirusSetup[$wgAntivirus]['messagepattern'] ) ?
1037 $wgAntivirusSetup[$wgAntivirus]['messagepattern'] : null;
1039 if ( strpos( $command, "%f" ) === false ) {
1040 # simple pattern: append file to scan
1041 $command .= " " . wfEscapeShellArg( $file );
1043 # complex pattern: replace "%f" with file to scan
1044 $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
1047 wfDebug( __METHOD__
. ": running virus scan: $command \n" );
1049 # execute virus scanner
1052 # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
1053 # that does not seem to be worth the pain.
1054 # Ask me (Duesentrieb) about it if it's ever needed.
1055 $output = wfShellExec( "$command 2>&1", $exitCode );
1057 # map exit code to AV_xxx constants.
1058 $mappedCode = $exitCode;
1059 if ( $exitCodeMap ) {
1060 if ( isset( $exitCodeMap[$exitCode] ) ) {
1061 $mappedCode = $exitCodeMap[$exitCode];
1062 } elseif ( isset( $exitCodeMap["*"] ) ) {
1063 $mappedCode = $exitCodeMap["*"];
1067 if ( $mappedCode === AV_SCAN_FAILED
) {
1068 # scan failed (code was mapped to false by $exitCodeMap)
1069 wfDebug( __METHOD__
. ": failed to scan $file (code $exitCode).\n" );
1071 if ( $wgAntivirusRequired ) {
1072 return wfMsg( 'virus-scanfailed', array( $exitCode ) );
1076 } elseif ( $mappedCode === AV_SCAN_ABORTED
) {
1077 # scan failed because filetype is unknown (probably imune)
1078 wfDebug( __METHOD__
. ": unsupported file type $file (code $exitCode).\n" );
1080 } elseif ( $mappedCode === AV_NO_VIRUS
) {
1082 wfDebug( __METHOD__
. ": file passed virus scan.\n" );
1085 $output = trim( $output );
1088 $output = true; #if there's no output, return true
1089 } elseif ( $msgPattern ) {
1091 if ( preg_match( $msgPattern, $output, $groups ) ) {
1093 $output = $groups[1];
1098 wfDebug( __METHOD__
. ": FOUND VIRUS! scanner feedback: $output \n" );
1104 * Check if the temporary file is MacBinary-encoded, as some uploads
1105 * from Internet Explorer on Mac OS Classic and Mac OS X will be.
1106 * If so, the data fork will be extracted to a second temporary file,
1107 * which will then be checked for validity and either kept or discarded.
1109 private function checkMacBinary() {
1110 $macbin = new MacBinary( $this->mTempPath
);
1111 if( $macbin->isValid() ) {
1112 $dataFile = tempnam( wfTempDir(), 'WikiMacBinary' );
1113 $dataHandle = fopen( $dataFile, 'wb' );
1115 wfDebug( __METHOD__
. ": Extracting MacBinary data fork to $dataFile\n" );
1116 $macbin->extractData( $dataHandle );
1118 $this->mTempPath
= $dataFile;
1119 $this->mFileSize
= $macbin->dataForkLength();
1121 // We'll have to manually remove the new file if it's not kept.
1122 $this->mRemoveTempFile
= true;
1128 * Check if there's an overwrite conflict and, if so, if restrictions
1129 * forbid this user from performing the upload.
1133 * @return mixed true on success, array on failure
1135 private function checkOverwrite( $user ) {
1136 // First check whether the local file can be overwritten
1137 $file = $this->getLocalFile();
1138 if( $file->exists() ) {
1139 if( !self
::userCanReUpload( $user, $file ) ) {
1140 return array( 'fileexists-forbidden', $file->getName() );
1146 /* Check shared conflicts: if the local file does not exist, but
1147 * wfFindFile finds a file, it exists in a shared repository.
1149 $file = wfFindFile( $this->getTitle() );
1150 if ( $file && !$user->isAllowed( 'reupload-shared' ) ) {
1151 return array( 'fileexists-shared-forbidden', $file->getName() );
1158 * Check if a user is the last uploader
1160 * @param $user User object
1161 * @param $img String: image name
1164 public static function userCanReUpload( User
$user, $img ) {
1165 if( $user->isAllowed( 'reupload' ) ) {
1166 return true; // non-conditional
1168 if( !$user->isAllowed( 'reupload-own' ) ) {
1171 if( is_string( $img ) ) {
1172 $img = wfLocalFile( $img );
1174 if ( !( $img instanceof LocalFile
) ) {
1178 return $user->getId() == $img->getUser( 'id' );
1182 * Helper function that does various existence checks for a file.
1183 * The following checks are performed:
1185 * - Article with the same name as the file exists
1186 * - File exists with normalized extension
1187 * - The file looks like a thumbnail and the original exists
1189 * @param $file File The File object to check
1190 * @return mixed False if the file does not exists, else an array
1192 public static function getExistsWarning( $file ) {
1193 if( $file->exists() ) {
1194 return array( 'warning' => 'exists', 'file' => $file );
1197 if( $file->getTitle()->getArticleID() ) {
1198 return array( 'warning' => 'page-exists', 'file' => $file );
1201 if ( $file->wasDeleted() && !$file->exists() ) {
1202 return array( 'warning' => 'was-deleted', 'file' => $file );
1205 if( strpos( $file->getName(), '.' ) == false ) {
1206 $partname = $file->getName();
1209 $n = strrpos( $file->getName(), '.' );
1210 $extension = substr( $file->getName(), $n +
1 );
1211 $partname = substr( $file->getName(), 0, $n );
1213 $normalizedExtension = File
::normalizeExtension( $extension );
1215 if ( $normalizedExtension != $extension ) {
1216 // We're not using the normalized form of the extension.
1217 // Normal form is lowercase, using most common of alternate
1218 // extensions (eg 'jpg' rather than 'JPEG').
1220 // Check for another file using the normalized form...
1221 $nt_lc = Title
::makeTitle( NS_FILE
, "{$partname}.{$normalizedExtension}" );
1222 $file_lc = wfLocalFile( $nt_lc );
1224 if( $file_lc->exists() ) {
1226 'warning' => 'exists-normalized',
1228 'normalizedFile' => $file_lc
1233 if ( self
::isThumbName( $file->getName() ) ) {
1234 # Check for filenames like 50px- or 180px-, these are mostly thumbnails
1235 $nt_thb = Title
::newFromText( substr( $partname , strpos( $partname , '-' ) +
1 ) . '.' . $extension, NS_FILE
);
1236 $file_thb = wfLocalFile( $nt_thb );
1237 if( $file_thb->exists() ) {
1239 'warning' => 'thumb',
1241 'thumbFile' => $file_thb
1244 // File does not exist, but we just don't like the name
1246 'warning' => 'thumb-name',
1248 'thumbFile' => $file_thb
1254 foreach( self
::getFilenamePrefixBlacklist() as $prefix ) {
1255 if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
1257 'warning' => 'bad-prefix',
1268 * Helper function that checks whether the filename looks like a thumbnail
1270 public static function isThumbName( $filename ) {
1271 $n = strrpos( $filename, '.' );
1272 $partname = $n ?
substr( $filename, 0, $n ) : $filename;
1274 substr( $partname , 3, 3 ) == 'px-' ||
1275 substr( $partname , 2, 3 ) == 'px-'
1277 preg_match( "/[0-9]{2}/" , substr( $partname , 0, 2 ) );
1281 * Get a list of blacklisted filename prefixes from [[MediaWiki:Filename-prefix-blacklist]]
1283 * @return array list of prefixes
1285 public static function getFilenamePrefixBlacklist() {
1286 $blacklist = array();
1287 $message = wfMessage( 'filename-prefix-blacklist' )->inContentLanguage();
1288 if( !$message->isDisabled() ) {
1289 $lines = explode( "\n", $message->plain() );
1290 foreach( $lines as $line ) {
1291 // Remove comment lines
1292 $comment = substr( trim( $line ), 0, 1 );
1293 if ( $comment == '#' ||
$comment == '' ) {
1296 // Remove additional comments after a prefix
1297 $comment = strpos( $line, '#' );
1298 if ( $comment > 0 ) {
1299 $line = substr( $line, 0, $comment-1 );
1301 $blacklist[] = trim( $line );
1308 * Gets image info about the file just uploaded.
1310 * Also has the effect of setting metadata to be an 'indexed tag name' in returned API result if
1311 * 'metadata' was requested. Oddly, we have to pass the "result" object down just so it can do that
1312 * with the appropriate format, presumably.
1314 * @param $result ApiResult:
1315 * @return Array: image info
1317 public function getImageInfo( $result ) {
1318 $file = $this->getLocalFile();
1319 // TODO This cries out for refactoring. We really want to say $file->getAllInfo(); here.
1320 // Perhaps "info" methods should be moved into files, and the API should just wrap them in queries.
1321 if ( $file instanceof UploadStashFile
) {
1322 $imParam = ApiQueryStashImageInfo
::getPropertyNames();
1323 $info = ApiQueryStashImageInfo
::getInfo( $file, array_flip( $imParam ), $result );
1325 $imParam = ApiQueryImageInfo
::getPropertyNames();
1326 $info = ApiQueryImageInfo
::getInfo( $file, array_flip( $imParam ), $result );
1332 public function convertVerifyErrorToStatus( $error ) {
1333 $code = $error['status'];
1334 unset( $code['status'] );
1335 return Status
::newFatal( $this->getVerificationErrorCode( $code ), $error );
1338 public static function getMaxUploadSize( $forType = null ) {
1339 global $wgMaxUploadSize;
1341 if ( is_array( $wgMaxUploadSize ) ) {
1342 if ( !is_null( $forType ) && isset( $wgMaxUploadSize[$forType] ) ) {
1343 return $wgMaxUploadSize[$forType];
1345 return $wgMaxUploadSize['*'];
1348 return intval( $wgMaxUploadSize );