(bug 23464) File: prefixes are now chopped off during uploading.
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * UploadBase and subclasses are the backend of MediaWiki's file uploads.
7 * The frontends are formed by ApiUpload and SpecialUpload.
8 *
9 * See also includes/docs/upload.txt
10 *
11 * @author Brion Vibber
12 * @author Bryan Tong Minh
13 * @author Michael Dale
14 */
15
16 abstract class UploadBase {
17 protected $mTempPath;
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;
24
25 const SUCCESS = 0;
26 const OK = 0;
27 const EMPTY_FILE = 3;
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;
34
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;
40
41 const SESSION_VERSION = 2;
42 const SESSION_KEYNAME = 'wsUploadData';
43
44 static public function getSessionKeyname() {
45 return self::SESSION_KEYNAME;
46 }
47
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',
59 );
60 if( isset( $code_to_status[$error] ) ) {
61 return $code_to_status[$error];
62 }
63
64 return 'unknown-error';
65 }
66
67 /**
68 * Returns true if uploads are enabled.
69 * Can be override by subclasses.
70 */
71 public static function isEnabled() {
72 global $wgEnableUploads;
73 if ( !$wgEnableUploads ) {
74 return false;
75 }
76
77 # Check php's file_uploads setting
78 if( !wfIniGetBool( 'file_uploads' ) ) {
79 return false;
80 }
81 return true;
82 }
83
84 /**
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.
88 *
89 * @param $user User
90 */
91 public static function isAllowed( $user ) {
92 foreach ( array( 'upload', 'edit' ) as $permission ) {
93 if ( !$user->isAllowed( $permission ) ) {
94 return $permission;
95 }
96 }
97 return true;
98 }
99
100 // Upload handlers. Should probably just be a global.
101 static $uploadHandlers = array( 'Stash', 'File', 'Url' );
102
103 /**
104 * Create a form of UploadBase depending on wpSourceType and initializes it
105 *
106 * @param $request WebRequest
107 */
108 public static function createFromRequest( &$request, $type = null ) {
109 $type = $type ? $type : $request->getVal( 'wpSourceType', 'File' );
110
111 if( !$type ) {
112 return null;
113 }
114
115 // Get the upload class
116 $type = ucfirst( $type );
117
118 // Give hooks the chance to handle this request
119 $className = null;
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 ) ) {
125 return null;
126 }
127 }
128
129 // Check whether this upload class is enabled
130 if( !call_user_func( array( $className, 'isEnabled' ) ) ) {
131 return null;
132 }
133
134 // Check whether the request is valid
135 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) ) {
136 return null;
137 }
138
139 $handler = new $className;
140
141 $handler->initializeFromRequest( $request );
142 return $handler;
143 }
144
145 /**
146 * Check whether a request if valid for this handler
147 */
148 public static function isValidRequest( $request ) {
149 return false;
150 }
151
152 public function __construct() {}
153
154 /**
155 * Returns the upload type. Should be overridden by child classes
156 *
157 * @since 1.18
158 * @return string
159 */
160 public function getSourceType() { return null; }
161
162 /**
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?
168 * @return null
169 */
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;
175 }
176
177 /**
178 * Initialize from a WebRequest. Override this in a subclass.
179 */
180 public abstract function initializeFromRequest( &$request );
181
182 /**
183 * Fetch the file. Usually a no-op
184 */
185 public function fetchFile() {
186 return Status::newGood();
187 }
188
189 /**
190 * Return true if the file is empty
191 * @return bool
192 */
193 public function isEmptyFile() {
194 return empty( $this->mFileSize );
195 }
196
197 /**
198 * Return the file size
199 * @return integer
200 */
201 public function getFileSize() {
202 return $this->mFileSize;
203 }
204
205 /**
206 * Append a file to the Repo file
207 *
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
211 */
212 protected function appendToUploadFile( $srcPath, $toAppendPath ) {
213 $repo = RepoGroup::singleton()->getLocalRepo();
214 $status = $repo->append( $srcPath, $toAppendPath );
215 return $status;
216 }
217
218 /**
219 * @param $srcPath String: the source path
220 * @return the real path if it was a virtual URL
221 */
222 function getRealPath( $srcPath ) {
223 $repo = RepoGroup::singleton()->getLocalRepo();
224 if ( $repo->isVirtualUrl( $srcPath ) ) {
225 return $repo->resolveVirtualUrl( $srcPath );
226 }
227 return $srcPath;
228 }
229
230 /**
231 * Verify whether the upload is sane.
232 * @return mixed self::OK or else an array with error information
233 */
234 public function verifyUpload() {
235 /**
236 * If there was no filename or a zero size given, give up quick.
237 */
238 if( $this->isEmptyFile() ) {
239 return array( 'status' => self::EMPTY_FILE );
240 }
241
242 /**
243 * Honor $wgMaxUploadSize
244 */
245 $maxSize = self::getMaxUploadSize( $this->getSourceType() );
246 if( $this->mFileSize > $maxSize ) {
247 return array(
248 'status' => self::FILE_TOO_LARGE,
249 'max' => $maxSize,
250 );
251 }
252
253 /**
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.
257 */
258 $verification = $this->verifyFile();
259 if( $verification !== true ) {
260 return array(
261 'status' => self::VERIFICATION_ERROR,
262 'details' => $verification
263 );
264 }
265
266 /**
267 * Make sure this file can be created
268 */
269 $result = $this->validateName();
270 if( $result !== true ) {
271 return $result;
272 }
273
274 $error = '';
275 if( !wfRunHooks( 'UploadVerification',
276 array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
277 return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
278 }
279
280 return array( 'status' => self::OK );
281 }
282
283 /**
284 * Verify that the name is valid and, if necessary, that we can overwrite
285 *
286 * @return mixed true if valid, otherwise and array with 'status'
287 * and other keys
288 **/
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;
295 }
296 if ( $this->mTitleError == self::FILETYPE_BADTYPE ) {
297 $result['finalExt'] = $this->mFinalExtension;
298 if ( count( $this->mBlackListedExtensions ) ) {
299 $result['blacklistedExt'] = $this->mBlackListedExtensions;
300 }
301 }
302 return $result;
303 }
304 $this->mDestName = $this->getLocalFile()->getName();
305
306 return true;
307 }
308
309 /**
310 * Verify the mime type
311 *
312 * @param $mime string representing the mime
313 * @return mixed true if the file is verified, an array otherwise
314 */
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 );
322 }
323
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 );
327 }
328
329 # Check IE type
330 $fp = fopen( $this->mTempPath, 'rb' );
331 $chunk = fread( $fp, 256 );
332 fclose( $fp );
333
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 );
340 }
341 }
342 }
343
344 return true;
345 }
346
347 /**
348 * Verifies that it's ok to include the uploaded file
349 *
350 * @return mixed true of the file is verified, array otherwise.
351 */
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
356 $this->getTitle();
357
358 $this->mFileProps = File::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
359 $this->checkMacBinary();
360
361 # check mime type, if desired
362 $mime = $this->mFileProps[ 'file-mime' ];
363 $status = $this->verifyMimeType( $mime );
364 if ( $status !== true ) {
365 return $status;
366 }
367
368 # check for htmlish code and javascript
369 if( self::detectScript( $this->mTempPath, $mime, $this->mFinalExtension ) ) {
370 return array( 'uploadscripted' );
371 }
372 if( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
373 if( $this->detectScriptInSvg( $this->mTempPath ) ) {
374 return array( 'uploadscripted' );
375 }
376 }
377
378 # Check for Java applets, which if uploaded can bypass cross-site
379 # restrictions.
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' ) {
388 return $error;
389 }
390 }
391 if ( $this->mJavaDetected ) {
392 return array( 'uploadjava' );
393 }
394 }
395
396 # Scan the uploaded file for viruses
397 $virus = $this->detectVirus( $this->mTempPath );
398 if ( $virus ) {
399 return array( 'uploadvirus', $virus );
400 }
401
402 $handler = MediaHandler::getHandler( $mime );
403 if ( $handler ) {
404 $handlerStatus = $handler->verifyUpload( $this->mTempPath );
405 if ( !$handlerStatus->isOK() ) {
406 $errors = $handlerStatus->getErrorsArray();
407 return reset( $errors );
408 }
409 }
410
411 wfRunHooks( 'UploadVerifyFile', array( $this, $mime, &$status ) );
412 if ( $status !== true ) {
413 return $status;
414 }
415
416 wfDebug( __METHOD__ . ": all clear; passing.\n" );
417 return true;
418 }
419
420 /**
421 * Callback for ZipDirectoryReader to detect Java class files.
422 */
423 function zipEntryCallback( $entry ) {
424 $names = array( $entry['name'] );
425
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 );
434 }
435
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;
440 }
441 }
442
443 /**
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.
449 */
450 public function verifyPermissions( $user ) {
451 return $this->verifyTitlePermissions( $user );
452 }
453
454 /**
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.
460 *
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.
464 */
465 public function verifyTitlePermissions( $user ) {
466 /**
467 * If the image is protected, non-sysop users won't be able
468 * to modify it by uploading a new revision.
469 */
470 $nt = $this->getTitle();
471 if( is_null( $nt ) ) {
472 return true;
473 }
474 $permErrors = $nt->getUserPermissionsErrors( 'edit', $user );
475 $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $user );
476 if ( !$nt->exists() ) {
477 $permErrorsCreate = $nt->getUserPermissionsErrors( 'createpage', $user );
478 } else {
479 $permErrorsCreate = array();
480 }
481 if( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
482 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
483 $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
484 return $permErrors;
485 }
486
487 $overwriteError = $this->checkOverwrite( $user );
488 if ( $overwriteError !== true ) {
489 return array( $overwriteError );
490 }
491
492 return true;
493 }
494
495 /**
496 * Check for non fatal problems with the file
497 *
498 * @return Array of warnings
499 */
500 public function checkWarnings() {
501 $warnings = array();
502
503 $localFile = $this->getLocalFile();
504 $filename = $localFile->getName();
505
506 /**
507 * Check whether the resulting filename is different from the desired one,
508 * but ignore things like ucfirst() and spaces/underscore things
509 */
510 $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
511 $comparableName = Title::capitalize( $comparableName, NS_FILE );
512
513 if( $this->mDesiredDestName != $filename && $comparableName != $filename ) {
514 $warnings['badfilename'] = $filename;
515 }
516
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;
522 }
523 }
524
525 global $wgUploadSizeWarning;
526 if ( $wgUploadSizeWarning && ( $this->mFileSize > $wgUploadSizeWarning ) ) {
527 $warnings['large-file'] = $wgUploadSizeWarning;
528 }
529
530 if ( $this->mFileSize == 0 ) {
531 $warnings['emptyfile'] = true;
532 }
533
534 $exists = self::getExistsWarning( $localFile );
535 if( $exists !== false ) {
536 $warnings['exists'] = $exists;
537 }
538
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] );
547 }
548 }
549 if( $dupes ) {
550 $warnings['duplicate'] = $dupes;
551 }
552
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();
557 }
558
559 return $warnings;
560 }
561
562 /**
563 * Really perform the upload. Stores the file in the local repo, watches
564 * if necessary and runs the UploadComplete hook.
565 *
566 * @param $user User
567 *
568 * @return Status indicating the whether the upload succeeded.
569 */
570 public function performUpload( $comment, $pageText, $watch, $user ) {
571 $status = $this->getLocalFile()->upload(
572 $this->mTempPath,
573 $comment,
574 $pageText,
575 File::DELETE_SOURCE,
576 $this->mFileProps,
577 false,
578 $user
579 );
580
581 if( $status->isGood() ) {
582 if ( $watch ) {
583 $user->addWatch( $this->getLocalFile()->getTitle() );
584 }
585
586 wfRunHooks( 'UploadComplete', array( &$this ) );
587 }
588
589 return $status;
590 }
591
592 /**
593 * Returns the title of the file to be uploaded. Sets mTitleError in case
594 * the name was illegal.
595 *
596 * @return Title The title of the file or null in case the name was illegal
597 */
598 public function getTitle() {
599 if ( $this->mTitle !== false ) {
600 return $this->mTitle;
601 }
602
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.
605 */
606 $title = Title::newFromText( $this->mDesiredDestName );
607 if ( $title->getNamespace() == NS_FILE ) {
608 $this->mFilteredName = $title->getDBkey();
609 } else {
610 $this->mFilteredName = $this->mDesiredDestName;
611 }
612
613 /**
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.
617 */
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;
624 }
625 $this->mFilteredName = $nt->getDBkey();
626
627 /**
628 * We'll want to blacklist against *any* 'extension', and use
629 * only the final one for the whitelist.
630 */
631 list( $partname, $ext ) = $this->splitExtensions( $this->mFilteredName );
632
633 if( count( $ext ) ) {
634 $this->mFinalExtension = trim( $ext[count( $ext ) - 1] );
635 } else {
636 $this->mFinalExtension = '';
637
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 );
644 if ( $extList ) {
645 # Set the extension to the canonical extension
646 $this->mFinalExtension = strtok( $extList, ' ' );
647
648 # Fix up the other variables
649 $this->mFilteredName .= ".{$this->mFinalExtension}";
650 $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
651 $ext = array( $this->mFinalExtension );
652 }
653 }
654
655 }
656
657 /* Don't allow users to override the blacklist (check file extension) */
658 global $wgCheckFileExtensions, $wgStrictFileExtensions;
659 global $wgFileExtensions, $wgFileBlacklist;
660
661 $blackListedExtensions = $this->checkFileExtensionList( $ext, $wgFileBlacklist );
662
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;
672 }
673
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;
678 }
679
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];
685 }
686 }
687
688 if( strlen( $partname ) < 1 ) {
689 $this->mTitleError = self::MIN_LENGTH_PARTNAME;
690 return $this->mTitle = null;
691 }
692
693 return $this->mTitle = $nt;
694 }
695
696 /**
697 * Return the local file and initializes if necessary.
698 *
699 * @return LocalFile
700 */
701 public function getLocalFile() {
702 if( is_null( $this->mLocalFile ) ) {
703 $nt = $this->getTitle();
704 $this->mLocalFile = is_null( $nt ) ? null : wfLocalFile( $nt );
705 }
706 return $this->mLocalFile;
707 }
708
709 /**
710 * NOTE: Probably should be deprecated in favor of UploadStash, but this is sometimes
711 * called outside that context.
712 *
713 * Stash a file in a temporary directory for later processing
714 * after the user has confirmed it.
715 *
716 * If the user doesn't explicitly cancel or accept, these files
717 * can accumulate in the temp directory.
718 *
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
722 */
723 protected function saveTempUploadedFile( $saveName, $tempSrc ) {
724 $repo = RepoGroup::singleton()->getLocalRepo();
725 $status = $repo->storeTemp( $saveName, $tempSrc );
726 return $status;
727 }
728
729 /**
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.
732 *
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.
737 *
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
740 */
741 public function stashSessionFile( $key = null ) {
742 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
743 $data = array(
744 'mFileProps' => $this->mFileProps,
745 'mSourceType' => $this->getSourceType(),
746 );
747 $file = $stash->stashFile( $this->mTempPath, $data, $key );
748 $this->mLocalFile = $file;
749 return $file;
750 }
751
752 /**
753 * Stash a file in a temporary directory, returning a key which can be used to find the file again. See stashSessionFile().
754 *
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
757 */
758 public function stashSession( $key = null ) {
759 return $this->stashSessionFile( $key )->getSessionKey();
760 }
761
762 /**
763 * If we've modified the upload file we need to manually remove it
764 * on exit to clean up.
765 */
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 );
770 }
771 }
772
773 public function getTempPath() {
774 return $this->mTempPath;
775 }
776
777 /**
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.
782 *
783 * @return array
784 */
785 public static function splitExtensions( $filename ) {
786 $bits = explode( '.', $filename );
787 $basename = array_shift( $bits );
788 return array( $basename, $bits );
789 }
790
791 /**
792 * Perform case-insensitive match against a list of file extensions.
793 * Returns true if the extension is in the list.
794 *
795 * @param $ext String
796 * @param $list Array
797 * @return Boolean
798 */
799 public static function checkFileExtension( $ext, $list ) {
800 return in_array( strtolower( $ext ), $list );
801 }
802
803 /**
804 * Perform case-insensitive match against a list of file extensions.
805 * Returns an array of matching extensions.
806 *
807 * @param $ext Array
808 * @param $list Array
809 * @return Boolean
810 */
811 public static function checkFileExtensionList( $ext, $list ) {
812 return array_intersect( array_map( 'strtolower', $ext ), $list );
813 }
814
815 /**
816 * Checks if the mime type of the uploaded file matches the file extension.
817 *
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
820 * @return Boolean
821 */
822 public static function verifyExtension( $mime, $extension ) {
823 $magic = MimeMagic::singleton();
824
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" );
829 return true;
830 } else {
831 wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; ".
832 "recognized extension '$extension', so probably invalid file\n" );
833 return false;
834 }
835
836 $match = $magic->isMatchingExtension( $extension, $mime );
837
838 if ( $match === null ) {
839 wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
840 return true;
841 } elseif( $match === true ) {
842 wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file\n" );
843
844 #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
845 return true;
846
847 } else {
848 wfDebug( __METHOD__ . ": mime type $mime mismatches file extension $extension, rejecting file\n" );
849 return false;
850 }
851 }
852
853 /**
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.
858 *
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
863 */
864 public static function detectScript( $file, $mime, $extension ) {
865 global $wgAllowTitlesInSVG;
866
867 # ugly hack: for text files, always look at the entire file.
868 # For binary field, just check the first K.
869
870 if( strpos( $mime,'text/' ) === 0 ) {
871 $chunk = file_get_contents( $file );
872 } else {
873 $fp = fopen( $file, 'rb' );
874 $chunk = fread( $fp, 1024 );
875 fclose( $fp );
876 }
877
878 $chunk = strtolower( $chunk );
879
880 if( !$chunk ) {
881 return false;
882 }
883
884 # decode from UTF-16 if needed (could be used for obfuscation).
885 if( substr( $chunk, 0, 2 ) == "\xfe\xff" ) {
886 $enc = 'UTF-16BE';
887 } elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" ) {
888 $enc = 'UTF-16LE';
889 } else {
890 $enc = null;
891 }
892
893 if( $enc ) {
894 $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
895 }
896
897 $chunk = trim( $chunk );
898
899 # FIXME: convert from UTF-16 if necessarry!
900 wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff\n" );
901
902 # check for HTML doctype
903 if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) {
904 return true;
905 }
906
907 /**
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
911 * attack vectors.
912 *
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!)
917 *
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.
921 */
922 $tags = array(
923 '<a href',
924 '<body',
925 '<head',
926 '<html', #also in safari
927 '<img',
928 '<pre',
929 '<script', #also in safari
930 '<table'
931 );
932
933 if( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
934 $tags[] = '<title';
935 }
936
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" );
940 return true;
941 }
942 }
943
944 /*
945 * look for JavaScript
946 */
947
948 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
949 $chunk = Sanitizer::decodeCharReferences( $chunk );
950
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" );
954 return true;
955 }
956
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" );
960 return true;
961 }
962
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" );
966 return true;
967 }
968
969 wfDebug( __METHOD__ . ": no scripts found\n" );
970 return false;
971 }
972
973 protected function detectScriptInSvg( $filename ) {
974 $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
975 return $check->filterMatch;
976 }
977
978 /**
979 * @todo Replace this with a whitelist filter!
980 */
981 public function checkSvgScriptCallback( $element, $attribs ) {
982 $stripped = $this->stripXmlNamespace( $element );
983
984 if( $stripped == 'script' ) {
985 wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file.\n" );
986 return true;
987 }
988
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" );
993 return true;
994 }
995 if( $stripped == 'href' && strpos( strtolower( $value ), 'javascript:' ) !== false ) {
996 wfDebug( __METHOD__ . ": Found script href attribute '$attrib'='$value' in uploaded file.\n" );
997 return true;
998 }
999 }
1000 }
1001
1002 private function stripXmlNamespace( $name ) {
1003 // 'http://www.w3.org/2000/svg:script' -> 'script'
1004 $parts = explode( ':', strtolower( $name ) );
1005 return array_pop( $parts );
1006 }
1007
1008 /**
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.
1012 *
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.
1017 */
1018 public static function detectVirus( $file ) {
1019 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
1020
1021 if ( !$wgAntivirus ) {
1022 wfDebug( __METHOD__ . ": virus scanner disabled\n" );
1023 return null;
1024 }
1025
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";
1031 }
1032
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;
1038
1039 if ( strpos( $command, "%f" ) === false ) {
1040 # simple pattern: append file to scan
1041 $command .= " " . wfEscapeShellArg( $file );
1042 } else {
1043 # complex pattern: replace "%f" with file to scan
1044 $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
1045 }
1046
1047 wfDebug( __METHOD__ . ": running virus scan: $command \n" );
1048
1049 # execute virus scanner
1050 $exitCode = false;
1051
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 );
1056
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["*"];
1064 }
1065 }
1066
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" );
1070
1071 if ( $wgAntivirusRequired ) {
1072 return wfMsg( 'virus-scanfailed', array( $exitCode ) );
1073 } else {
1074 return null;
1075 }
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" );
1079 return null;
1080 } elseif ( $mappedCode === AV_NO_VIRUS ) {
1081 # no virus found
1082 wfDebug( __METHOD__ . ": file passed virus scan.\n" );
1083 return false;
1084 } else {
1085 $output = trim( $output );
1086
1087 if ( !$output ) {
1088 $output = true; #if there's no output, return true
1089 } elseif ( $msgPattern ) {
1090 $groups = array();
1091 if ( preg_match( $msgPattern, $output, $groups ) ) {
1092 if ( $groups[1] ) {
1093 $output = $groups[1];
1094 }
1095 }
1096 }
1097
1098 wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
1099 return $output;
1100 }
1101 }
1102
1103 /**
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.
1108 */
1109 private function checkMacBinary() {
1110 $macbin = new MacBinary( $this->mTempPath );
1111 if( $macbin->isValid() ) {
1112 $dataFile = tempnam( wfTempDir(), 'WikiMacBinary' );
1113 $dataHandle = fopen( $dataFile, 'wb' );
1114
1115 wfDebug( __METHOD__ . ": Extracting MacBinary data fork to $dataFile\n" );
1116 $macbin->extractData( $dataHandle );
1117
1118 $this->mTempPath = $dataFile;
1119 $this->mFileSize = $macbin->dataForkLength();
1120
1121 // We'll have to manually remove the new file if it's not kept.
1122 $this->mRemoveTempFile = true;
1123 }
1124 $macbin->close();
1125 }
1126
1127 /**
1128 * Check if there's an overwrite conflict and, if so, if restrictions
1129 * forbid this user from performing the upload.
1130 *
1131 * @param $user User
1132 *
1133 * @return mixed true on success, array on failure
1134 */
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() );
1141 } else {
1142 return true;
1143 }
1144 }
1145
1146 /* Check shared conflicts: if the local file does not exist, but
1147 * wfFindFile finds a file, it exists in a shared repository.
1148 */
1149 $file = wfFindFile( $this->getTitle() );
1150 if ( $file && !$user->isAllowed( 'reupload-shared' ) ) {
1151 return array( 'fileexists-shared-forbidden', $file->getName() );
1152 }
1153
1154 return true;
1155 }
1156
1157 /**
1158 * Check if a user is the last uploader
1159 *
1160 * @param $user User object
1161 * @param $img String: image name
1162 * @return Boolean
1163 */
1164 public static function userCanReUpload( User $user, $img ) {
1165 if( $user->isAllowed( 'reupload' ) ) {
1166 return true; // non-conditional
1167 }
1168 if( !$user->isAllowed( 'reupload-own' ) ) {
1169 return false;
1170 }
1171 if( is_string( $img ) ) {
1172 $img = wfLocalFile( $img );
1173 }
1174 if ( !( $img instanceof LocalFile ) ) {
1175 return false;
1176 }
1177
1178 return $user->getId() == $img->getUser( 'id' );
1179 }
1180
1181 /**
1182 * Helper function that does various existence checks for a file.
1183 * The following checks are performed:
1184 * - The file exists
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
1188 *
1189 * @param $file File The File object to check
1190 * @return mixed False if the file does not exists, else an array
1191 */
1192 public static function getExistsWarning( $file ) {
1193 if( $file->exists() ) {
1194 return array( 'warning' => 'exists', 'file' => $file );
1195 }
1196
1197 if( $file->getTitle()->getArticleID() ) {
1198 return array( 'warning' => 'page-exists', 'file' => $file );
1199 }
1200
1201 if ( $file->wasDeleted() && !$file->exists() ) {
1202 return array( 'warning' => 'was-deleted', 'file' => $file );
1203 }
1204
1205 if( strpos( $file->getName(), '.' ) == false ) {
1206 $partname = $file->getName();
1207 $extension = '';
1208 } else {
1209 $n = strrpos( $file->getName(), '.' );
1210 $extension = substr( $file->getName(), $n + 1 );
1211 $partname = substr( $file->getName(), 0, $n );
1212 }
1213 $normalizedExtension = File::normalizeExtension( $extension );
1214
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').
1219 //
1220 // Check for another file using the normalized form...
1221 $nt_lc = Title::makeTitle( NS_FILE, "{$partname}.{$normalizedExtension}" );
1222 $file_lc = wfLocalFile( $nt_lc );
1223
1224 if( $file_lc->exists() ) {
1225 return array(
1226 'warning' => 'exists-normalized',
1227 'file' => $file,
1228 'normalizedFile' => $file_lc
1229 );
1230 }
1231 }
1232
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() ) {
1238 return array(
1239 'warning' => 'thumb',
1240 'file' => $file,
1241 'thumbFile' => $file_thb
1242 );
1243 } else {
1244 // File does not exist, but we just don't like the name
1245 return array(
1246 'warning' => 'thumb-name',
1247 'file' => $file,
1248 'thumbFile' => $file_thb
1249 );
1250 }
1251 }
1252
1253
1254 foreach( self::getFilenamePrefixBlacklist() as $prefix ) {
1255 if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
1256 return array(
1257 'warning' => 'bad-prefix',
1258 'file' => $file,
1259 'prefix' => $prefix
1260 );
1261 }
1262 }
1263
1264 return false;
1265 }
1266
1267 /**
1268 * Helper function that checks whether the filename looks like a thumbnail
1269 */
1270 public static function isThumbName( $filename ) {
1271 $n = strrpos( $filename, '.' );
1272 $partname = $n ? substr( $filename, 0, $n ) : $filename;
1273 return (
1274 substr( $partname , 3, 3 ) == 'px-' ||
1275 substr( $partname , 2, 3 ) == 'px-'
1276 ) &&
1277 preg_match( "/[0-9]{2}/" , substr( $partname , 0, 2 ) );
1278 }
1279
1280 /**
1281 * Get a list of blacklisted filename prefixes from [[MediaWiki:Filename-prefix-blacklist]]
1282 *
1283 * @return array list of prefixes
1284 */
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 == '' ) {
1294 continue;
1295 }
1296 // Remove additional comments after a prefix
1297 $comment = strpos( $line, '#' );
1298 if ( $comment > 0 ) {
1299 $line = substr( $line, 0, $comment-1 );
1300 }
1301 $blacklist[] = trim( $line );
1302 }
1303 }
1304 return $blacklist;
1305 }
1306
1307 /**
1308 * Gets image info about the file just uploaded.
1309 *
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.
1313 *
1314 * @param $result ApiResult:
1315 * @return Array: image info
1316 */
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 );
1324 } else {
1325 $imParam = ApiQueryImageInfo::getPropertyNames();
1326 $info = ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
1327 }
1328 return $info;
1329 }
1330
1331
1332 public function convertVerifyErrorToStatus( $error ) {
1333 $code = $error['status'];
1334 unset( $code['status'] );
1335 return Status::newFatal( $this->getVerificationErrorCode( $code ), $error );
1336 }
1337
1338 public static function getMaxUploadSize( $forType = null ) {
1339 global $wgMaxUploadSize;
1340
1341 if ( is_array( $wgMaxUploadSize ) ) {
1342 if ( !is_null( $forType ) && isset( $wgMaxUploadSize[$forType] ) ) {
1343 return $wgMaxUploadSize[$forType];
1344 } else {
1345 return $wgMaxUploadSize['*'];
1346 }
1347 } else {
1348 return intval( $wgMaxUploadSize );
1349 }
1350
1351 }
1352 }