<?php
/**
- * @file
+ * @file
* @ingroup upload
- *
+ *
* UploadBase and subclasses are the backend of MediaWiki's file uploads.
* The frontends are formed by ApiUpload and SpecialUpload.
- *
+ *
* See also includes/docs/upload.txt
- *
+ *
* @author Brion Vibber
* @author Bryan Tong Minh
* @author Michael Dale
*/
-
+
abstract class UploadBase {
protected $mTempPath;
protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
const SUCCESS = 0;
const OK = 0;
- const BEFORE_PROCESSING = 1;
- const LARGE_FILE_SERVER = 2;
const EMPTY_FILE = 3;
const MIN_LENGTH_PARTNAME = 4;
const ILLEGAL_FILENAME = 5;
- const PROTECTED_PAGE = 6;
const OVERWRITE_EXISTING_FILE = 7;
const FILETYPE_MISSING = 8;
const FILETYPE_BADTYPE = 9;
const VERIFICATION_ERROR = 10;
const UPLOAD_VERIFICATION_ERROR = 11;
- const UPLOAD_WARNING = 12;
- const INTERNAL_ERROR = 13;
+ const HOOK_ABORTED = 11;
const MIN_LENGHT_PARTNAME = 14;
const SESSION_VERSION = 2;
// Get the upload class
$type = ucfirst( $type );
- $className = 'UploadFrom' . $type;
- wfDebug( __METHOD__ . ": class name: $className\n" );
- if( !in_array( $type, self::$uploadHandlers ) )
- return null;
+
+ // Give hooks the chance to handle this request
+ $className = null;
+ wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
+ if ( is_null( $className ) ) {
+ $className = 'UploadFrom' . $type;
+ wfDebug( __METHOD__ . ": class name: $className\n" );
+ if( !in_array( $type, self::$uploadHandlers ) )
+ return null;
+ }
// Check whether this upload class is enabled
if( !call_user_func( array( $className, 'isEnabled' ) ) )
$this->mFileSize = $fileSize;
$this->mRemoveTempFile = $removeTempFile;
}
-
+
/**
* Initialize from a WebRequest. Override this in a subclass.
*/
public function isEmptyFile(){
return empty( $this->mFileSize );
}
-
- /*
- * getRealPath
- * @param string $srcPath the source path
- * @returns the real path if it was a virtual url
- */
+
+ /**
+ * getRealPath
+ * @param string $srcPath the source path
+ * @returns the real path if it was a virtual url
+ */
function getRealPath( $srcPath ){
$repo = RepoGroup::singleton()->getLocalRepo();
if ( $repo->isVirtualUrl( $srcPath ) ) {
*/
if( $this->isEmptyFile() )
return array( 'status' => self::EMPTY_FILE );
+
+ /**
+ * Look at the contents of the file; if we can recognize the
+ * type but it's corrupt or data of the wrong type, we should
+ * probably not accept it.
+ */
+ $verification = $this->verifyFile();
+ if( $verification !== true ) {
+ if( !is_array( $verification ) )
+ $verification = array( $verification );
+ return array( 'status' => self::VERIFICATION_ERROR,
+ 'details' => $verification );
+ }
+
$nt = $this->getTitle();
if( is_null( $nt ) ) {
$result = array( 'status' => $this->mTitleError );
if( $overwrite !== true )
return array( 'status' => self::OVERWRITE_EXISTING_FILE, 'overwrite' => $overwrite );
- /**
- * Look at the contents of the file; if we can recognize the
- * type but it's corrupt or data of the wrong type, we should
- * probably not accept it.
- */
- $verification = $this->verifyFile();
-
- if( $verification !== true ) {
- if( !is_array( $verification ) )
- $verification = array( $verification );
- return array( 'status' => self::VERIFICATION_ERROR,
- 'details' => $verification );
-
- }
-
$error = '';
if( !wfRunHooks( 'UploadVerification',
array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
// This status needs another name...
- return array( 'status' => self::UPLOAD_VERIFICATION_ERROR, 'error' => $error );
+ return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
}
return array( 'status' => self::OK );
#check mime type, if desired
global $wgVerifyMimeType;
- if ( $wgVerifyMimeType ) {
+ if ( $wgVerifyMimeType ) {
global $wgMimeTypeBlacklist;
if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) )
return array( 'filetype-badmime', $mime );
/**
* Check whether the user can edit, upload and create the image.
- *
+ *
* @param User $user the user to verify the permissions against
* @return mixed An array as returned by getUserPermissionsErrors or true
* in case the user has proper permissions.
/**
* Check for non fatal problems with the file
- *
+ *
* @return array Array of warnings
*/
public function checkWarnings() {
* but ignore things like ucfirst() and spaces/underscore things
*/
$comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
- $comparableName = Title::capitalize( $comparableName, NS_FILE );
+ global $wgCapitalLinks, $wgContLang;
+ if ( $wgCapitalLinks ) {
+ $comparableName = $wgContLang->ucfirst( $comparableName );
+ }
if( $this->mDesiredDestName != $filename && $comparableName != $filename )
$warnings['badfilename'] = $filename;
}
/**
- * Really perform the upload. Stores the file in the local repo, watches
+ * Really perform the upload. Stores the file in the local repo, watches
* if necessary and runs the UploadComplete hook.
- *
+ *
* @return mixed Status indicating the whether the upload succeeded.
*/
public function performUpload( $comment, $pageText, $watch, $user ) {
$status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
File::DELETE_SOURCE, $this->mFileProps, false, $user );
- if( $status->isGood() && $watch ){
- //make sure the watch commit happens inline
- $dbw = wfGetDB(DB_MASTER);
- $dbw->begin();
- $user->addWatch( $this->getLocalFile()->getTitle() );
- $dbw->commit();
- }
+ if( $status->isGood() && $watch )
+ $user->addWatch( $this->getLocalFile()->getTitle() );
if( $status->isGood() )
wfRunHooks( 'UploadComplete', array( &$this ) );
/**
* Returns the title of the file to be uploaded. Sets mTitleError in case
* the name was illegal.
- *
+ *
* @return Title The title of the file or null in case the name was illegal
*/
public function getTitle() {
}
/**
- * Return the local file and initializes if necessary.
+ * Return the local file and initializes if necessary.
*/
public function getLocalFile() {
if( is_null( $this->mLocalFile ) ) {
return $status;
}
- /**
+ /**
* Append a file to a stashed file.
- *
+ *
* @param string $srcPath Path to file to append from
* @param string $toAppendPath Path to file to append to
* @return Status Status
'mFileSize' => $this->mFileSize,
'mFileProps' => $this->mFileProps,
'version' => self::SESSION_VERSION,
- );
+ );
return $key;
}
return $key;
}
- /**
- * Remove a temporarily kept file stashed by saveTempUploadedFile().
- * @return success
- */
- public function unsaveUploadedFile() {
- $repo = RepoGroup::singleton()->getLocalRepo();
- $success = $repo->freeTemp( $this->mTempPath );
- return $success;
- }
/**
* If we've modified the upload file we need to manually remove it
# that does not seem to be worth the pain.
# Ask me (Duesentrieb) about it if it's ever needed.
$output = array();
- $output = wfShellExec($command, $exitCode);
-
+ if ( wfIsWindows() ) {
+ exec( "$command", $output, $exitCode );
+ } else {
+ exec( "$command 2>&1", $output, $exitCode );
+ }
# map exit code to AV_xxx constants.
$mappedCode = $exitCode;
}
}
-
if ( $mappedCode === AV_SCAN_FAILED ) {
# scan failed (code was mapped to false by $exitCodeMap)
wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
return true;
}
- /* Check shared conflicts: if the local file does not exist, but
- * wfFindFile finds a file, it exists in a shared repository.
- */
+ /* Check shared conflicts: if the local file does not exist, but
+ * wfFindFile finds a file, it exists in a shared repository.
+ */
$file = wfFindFile( $this->getTitle() );
if ( $file && !$wgUser->isAllowed( 'reupload-shared' ) )
return 'fileexists-shared-forbidden';
/**
* Helper function that does various existence checks for a file.
- * The following checks are performed:
+ * The following checks are performed:
* - The file exists
* - Article with the same name as the file exists
* - File exists with normalized extension
* - The file looks like a thumbnail and the original exists
- *
- * @param File $file The file to check
+ *
+ * @param File $file The file to check
* @return mixed False if the file does not exists, else an array
*/
public static function getExistsWarning( $file ) {
if( $file->getTitle()->getArticleID() )
return array( 'warning' => 'page-exists', 'file' => $file );
-
+
if ( $file->wasDeleted() && !$file->exists() )
- return array( 'warning' => 'was-deleted', 'file' => $file );
-
+ return array( 'warning' => 'was-deleted', 'file' => $file );
+
if( strpos( $file->getName(), '.' ) == false ) {
$partname = $file->getName();
$extension = '';
// File does not exist, but we just don't like the name
return array( 'warning' => 'thumb-name', 'file' => $file, 'thumbFile' => $file_thb );
}
-
+
foreach( self::getFilenamePrefixBlacklist() as $prefix ) {
if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix )
return array( 'warning' => 'bad-prefix', 'file' => $file, 'prefix' => $prefix );
}
-
+
return false;