Merge "Some additional test cases for Parsoid."
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index 48ea584..49713fc 100644 (file)
@@ -78,7 +78,7 @@ abstract class UploadBase {
                                                                self::ILLEGAL_FILENAME => 'illegal-filename',
                                                                self::OVERWRITE_EXISTING_FILE => 'overwrite',
                                                                self::VERIFICATION_ERROR => 'verification-error',
-                                                               self::HOOK_ABORTED =>  'hookaborted',
+                                                               self::HOOK_ABORTED => 'hookaborted',
                                                                self::WINDOWS_NONASCII_FILENAME => 'windows-nonascii-filename',
                                                                self::FILENAME_TOO_LONG => 'filename-toolong',
                );
@@ -209,7 +209,7 @@ abstract class UploadBase {
        /**
         * Initialize from a WebRequest. Override this in a subclass.
         */
-       public abstract function initializeFromRequest( &$request );
+       abstract public function initializeFromRequest( &$request );
 
        /**
         * Fetch the file. Usually a no-op
@@ -239,7 +239,7 @@ abstract class UploadBase {
         * Get the base 36 SHA1 of the file
         * @return string
         */
-       protected function getTempFileSha1Base36() {
+       public function getTempFileSha1Base36() {
                return FSFile::getSha1Base36FromPath( $this->mTempPath );
        }
 
@@ -360,7 +360,7 @@ abstract class UploadBase {
                global $wgVerifyMimeType;
                wfProfileIn( __METHOD__ );
                if ( $wgVerifyMimeType ) {
-                       wfDebug ( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n");
+                       wfDebug ( "\n\nmime: <$mime> extension: <{$this->mFinalExtension}>\n\n" );
                        global $wgMimeTypeBlacklist;
                        if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
                                wfProfileOut( __METHOD__ );
@@ -409,7 +409,7 @@ abstract class UploadBase {
                $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
 
                # check mime type, if desired
-               $mime = $this->mFileProps[ 'file-mime' ];
+               $mime = $this->mFileProps['file-mime'];
                $status = $this->verifyMimeType( $mime );
                if ( $status !== true ) {
                        wfProfileOut( __METHOD__ );
@@ -768,7 +768,7 @@ abstract class UploadBase {
                }
 
                if( strlen( $partname ) < 1 ) {
-                       $this->mTitleError =  self::MIN_LENGTH_PARTNAME;
+                       $this->mTitleError = self::MIN_LENGTH_PARTNAME;
                        return $this->mTitle = null;
                }
 
@@ -940,7 +940,7 @@ abstract class UploadBase {
                # ugly hack: for text files, always look at the entire file.
                # For binary field, just check the first K.
 
-               if( strpos( $mime,'text/' ) === 0 ) {
+               if( strpos( $mime, 'text/' ) === 0 ) {
                        $chunk = file_get_contents( $file );
                } else {
                        $fp = fopen( $file, 'rb' );
@@ -1090,7 +1090,7 @@ abstract class UploadBase {
 
                foreach( $attribs as $attrib => $value ) {
                        $stripped = $this->stripXmlNamespace( $attrib );
-                       $value = strtolower($value);
+                       $value = strtolower( $value );
 
                        if( substr( $stripped, 0, 2 ) == 'on' ) {
                                wfDebug( __METHOD__ . ": Found event-handler attribute '$attrib'='$value' in uploaded file.\n" );
@@ -1143,8 +1143,8 @@ abstract class UploadBase {
                        # use CSS styles to bring in remote code
                        # catch url("http:..., url('http:..., url(http:..., but not url("#..., url('#..., url(#....
                        if( $stripped == 'style' && preg_match_all( '!((?:font|clip-path|fill|filter|marker|marker-end|marker-mid|marker-start|mask|stroke)\s*:\s*url\s*\(\s*["\']?\s*[^#]+.*?\))!sim', $value, $matches ) ) {
-                               foreach ($matches[1] as $match) {
-                                       if (!preg_match( '!(?:font|clip-path|fill|filter|marker|marker-end|marker-mid|marker-start|mask|stroke)\s*:\s*url\s*\(\s*(#|\'#|"#)!sim', $match ) ) {
+                               foreach ( $matches[1] as $match ) {
+                                       if ( !preg_match( '!(?:font|clip-path|fill|filter|marker|marker-end|marker-mid|marker-start|mask|stroke)\s*:\s*url\s*\(\s*(#|\'#|"#)!sim', $match ) ) {
                                                wfDebug( __METHOD__ . ": Found svg setting a style with remote url '$attrib'='$value' in uploaded file.\n" );
                                                return true;
                                        }
@@ -1234,27 +1234,22 @@ abstract class UploadBase {
                        }
                }
 
+               /* NB: AV_NO_VIRUS is 0 but AV_SCAN_FAILED is false,
+                * so we need the strict equalities === and thus can't use a switch here
+                */
                if ( $mappedCode === AV_SCAN_FAILED ) {
                        # scan failed (code was mapped to false by $exitCodeMap)
                        wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
 
-                       if ( $wgAntivirusRequired ) {
-                               wfProfileOut( __METHOD__ );
-                               return wfMessage( 'virus-scanfailed', array( $exitCode ) )->text();
-                       } else {
-                               wfProfileOut( __METHOD__ );
-                               return null;
-                       }
+                       $output = $wgAntivirusRequired ? wfMessage( 'virus-scanfailed', array( $exitCode ) )->text() : null;
                } elseif ( $mappedCode === AV_SCAN_ABORTED ) {
                        # scan failed because filetype is unknown (probably imune)
                        wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
-                       wfProfileOut( __METHOD__ );
-                       return null;
+                       $output = null;
                } elseif ( $mappedCode === AV_NO_VIRUS ) {
                        # no virus found
                        wfDebug( __METHOD__ . ": file passed virus scan.\n" );
-                       wfProfileOut( __METHOD__ );
-                       return false;
+                       $output = false;
                } else {
                        $output = trim( $output );
 
@@ -1270,9 +1265,10 @@ abstract class UploadBase {
                        }
 
                        wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
-                       wfProfileOut( __METHOD__ );
-                       return $output;
                }
+
+               wfProfileOut( __METHOD__ );
+               return $output;
        }
 
        /**
@@ -1383,7 +1379,7 @@ abstract class UploadBase {
 
                if ( self::isThumbName( $file->getName() ) ) {
                        # Check for filenames like 50px- or 180px-, these are mostly thumbnails
-                       $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $extension, NS_FILE );
+                       $nt_thb = Title::newFromText( substr( $partname, strpos( $partname, '-' ) + 1 ) . '.' . $extension, NS_FILE );
                        $file_thb = wfLocalFile( $nt_thb );
                        if( $file_thb->exists() ) {
                                return array(
@@ -1424,10 +1420,10 @@ abstract class UploadBase {
                $n = strrpos( $filename, '.' );
                $partname = $n ? substr( $filename, 0, $n ) : $filename;
                return (
-                                       substr( $partname , 3, 3 ) == 'px-' ||
-                                       substr( $partname , 2, 3 ) == 'px-'
+                                       substr( $partname, 3, 3 ) == 'px-' ||
+                                       substr( $partname, 2, 3 ) == 'px-'
                                ) &&
-                               preg_match( "/[0-9]{2}/" , substr( $partname , 0, 2 ) );
+                               preg_match( "/[0-9]{2}/", substr( $partname, 0, 2 ) );
        }
 
        /**