Fix regression with unclosed extension tags.
[lhc/web/wiklou.git] / includes / SpecialUpload.php
index fd8e31e..84b732f 100644 (file)
@@ -48,7 +48,7 @@ class UploadForm {
                        return;
                }
 
-               $this->mIgnoreWarning     = $request->getCheck( 'wpIgnoreWarning');
+               $this->mIgnoreWarning     = $request->getCheck( 'wpIgnoreWarning' );
                $this->mReUpload          = $request->getCheck( 'wpReUpload' );
                $this->mUpload            = $request->getCheck( 'wpUpload' );
 
@@ -99,17 +99,29 @@ class UploadForm {
                global $wgUser, $wgOut;
                global $wgEnableUploads, $wgUploadDirectory;
 
-               /** Show an error message if file upload is disabled */
-               if( ! $wgEnableUploads ) {
-                       $wgOut->addWikiText( wfMsg( 'uploaddisabled' ) );
+               # Check uploading enabled
+               if( !$wgEnableUploads ) {
+                       $wgOut->errorPage( 'uploaddisabled', 'uploaddisabledtext' );
                        return;
                }
 
-               /** Various rights checks */
-               if( !$wgUser->isAllowed( 'upload' ) || $wgUser->isBlocked() ) {
-                       $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
+               # Check permissions
+               if( $wgUser->isLoggedIn() ) {
+                       if( !$wgUser->isAllowed( 'upload' ) ) {
+                               $wgOut->permissionRequired( 'upload' );
+                               return;
+                       }
+               } else {
+                       $wgOut->errorPage( 'uploadnologin', 'uploadnologintext' );
                        return;
                }
+
+               # Check blocks
+               if( $wgUser->isBlocked() ) {
+                       $wgOut->blockedPage();
+                       return;
+               }
+
                if( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
                        return;
@@ -141,9 +153,7 @@ class UploadForm {
         * @access private
         */
        function processUpload() {
-               global $wgUser, $wgOut, $wgLang, $wgContLang;
-               global $wgUploadDirectory;
-               global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
+               global $wgUser, $wgOut;
 
                /* Check for PHP error if any, requires php 4.2 or newer */
                if ( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
@@ -161,9 +171,9 @@ class UploadForm {
 
                # Chop off any directories in the given filename
                if ( $this->mDestFile ) {
-                       $basename = basename( $this->mDestFile );
+                       $basename = wfBaseName( $this->mDestFile );
                } else {
-                       $basename = basename( $this->mOname );
+                       $basename = wfBaseName( $this->mOname );
                }
 
                /**
@@ -171,6 +181,7 @@ class UploadForm {
                 * only the final one for the whitelist.
                 */
                list( $partname, $ext ) = $this->splitExtensions( $basename );
+               
                if( count( $ext ) ) {
                        $finalExt = $ext[count( $ext ) - 1];
                } else {
@@ -178,6 +189,13 @@ class UploadForm {
                }
                $fullExt = implode( '.', $ext );
 
+               # If there was more than one "extension", reassemble the base
+               # filename to prevent bogus complaints about length
+               if( count( $ext ) > 1 ) {
+                       for( $i = 0; $i < count( $ext ) - 1; $i++ )
+                               $partname .= '.' . $ext[$i];
+               }
+
                if ( strlen( $partname ) < 3 ) {
                        $this->mainUploadForm( wfMsgHtml( 'minlength' ) );
                        return;
@@ -203,7 +221,7 @@ class UploadForm {
                if( !$nt->userCanEdit() ) {
                        return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
                }
-               
+
                /**
                 * In some cases we may forbid overwriting of existing files.
                 */
@@ -234,7 +252,7 @@ class UploadForm {
                                return $this->uploadError( $veri->toString() );
                        }
                }
-               
+
                /**
                 * Provide an opportunity for extensions to add futher checks
                 */
@@ -249,7 +267,7 @@ class UploadForm {
                 */
                if ( ! $this->mIgnoreWarning ) {
                        $warning = '';
-                       
+
                        global $wgCapitalLinks;
                        if( $wgCapitalLinks ) {
                                $filtered = ucfirst( $filtered );
@@ -280,6 +298,16 @@ class UploadForm {
                                $sk = $wgUser->getSkin();
                                $dlink = $sk->makeKnownLinkObj( $nt );
                                $warning .= '<li>'.wfMsgHtml( 'fileexists', $dlink ).'</li>';
+                       } else {
+                               # If the file existed before and was deleted, warn the user of this
+                               # Don't bother doing so if the image exists now, however
+                               $image = new Image( $nt );
+                               if( $image->wasDeleted() ) {
+                                       $skin = $wgUser->getSkin();
+                                       $ltitle = Title::makeTitle( NS_SPECIAL, 'Log' );
+                                       $llink = $skin->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ), 'type=delete&page=' . $nt->getPrefixedUrl() );
+                                       $warning .= wfOpenElement( 'li' ) . wfMsgWikiHtml( 'filewasdeleted', $llink ) . wfCloseElement( 'li' );
+                               }
                        }
 
                        if( $warning != '' ) {
@@ -313,6 +341,7 @@ class UploadForm {
 
                        if ( $success ) {
                                $this->showSuccess();
+                               wfRunHooks( 'UploadComplete', array( &$img ) );
                        } else {
                                // Image::recordUpload() fails if the image went missing, which is
                                // unlikely, hence the lack of a specialised message
@@ -334,7 +363,7 @@ class UploadForm {
         *                        is a PHP-managed upload temporary
         */
        function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
-               global $wgUploadDirectory, $wgOut;
+               global $wgOut;
 
                $fname= "SpecialUpload::saveUploadedFile";
 
@@ -483,7 +512,7 @@ class UploadForm {
         * @access private
         */
        function uploadWarning( $warning ) {
-               global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
+               global $wgOut;
                global $wgUseCopyrightUpload;
 
                $this->mSessionKey = $this->stashSession();
@@ -525,13 +554,13 @@ class UploadForm {
                <tr>
                        <tr>
                                <td align='right'>
-                                       <input tabindex='2' type='submit' name='wpUpload' value='$save' />
+                                       <input tabindex='2' type='submit' name='wpUpload' value=\"$save\" />
                                </td>
                                <td align='left'>$iw</td>
                        </tr>
                        <tr>
                                <td align='right'>
-                                       <input tabindex='2' type='submit' name='wpReUpload' value='{$reupload}' />
+                                       <input tabindex='2' type='submit' name='wpReUpload' value=\"{$reupload}\" />
                                </td>
                                <td align='left'>$reup</td>
                        </tr>
@@ -547,7 +576,7 @@ class UploadForm {
         * @access private
         */
        function mainUploadForm( $msg='' ) {
-               global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
+               global $wgOut, $wgUser;
                global $wgUseCopyrightUpload;
 
                $cols = intval($wgUser->getOption( 'cols' ));
@@ -574,7 +603,7 @@ class UploadForm {
                $license = wfMsgHtml( 'license' );
                $nolicense = wfMsgHtml( 'nolicense' );
                $licenseshtml = $licenses->getHtml();
-               
+
                $ulb = wfMsgHtml( 'uploadbtn' );
 
 
@@ -586,7 +615,7 @@ class UploadForm {
                $watchChecked = $wgUser->getOption( 'watchdefault' )
                        ? 'checked="checked"'
                        : '';
-               
+
                $wgOut->addHTML( "
        <form id='upload' method='post' enctype='multipart/form-data' action=\"$action\">
                <table border='0'>
@@ -609,12 +638,15 @@ class UploadForm {
                        </td>
                </tr>
                <tr>" );
-       
-       if ( $licenseshtml != '' ) {
-               $wgOut->addHTML( "
+
+               if ( $licenseshtml != '' ) {
+                       global $wgStylePath;
+                       $wgOut->addHTML( "
                        <td align='right'><label for='wpLicense'>$license:</label></td>
                        <td align='left'>
-                               <select name='wpLicense' id='wpLicense' tabindex='4'>
+                               <script type='text/javascript' src=\"$wgStylePath/common/upload.js\"></script>
+                               <select name='wpLicense' id='wpLicense' tabindex='4'
+                                       onchange='licenseSelectorCheck()'>
                                        <option value=''>$nolicense</option>
                                        $licenseshtml
                                </select>
@@ -622,15 +654,15 @@ class UploadForm {
                        </tr>
                        <tr>
                ");
-       }
+               }
 
-       if ( $wgUseCopyrightUpload ) {
-               $filestatus = wfMsgHtml ( 'filestatus' );
-               $copystatus =  htmlspecialchars( $this->mUploadCopyStatus );
-               $filesource = wfMsgHtml ( 'filesource' );
-               $uploadsource = htmlspecialchars( $this->mUploadSource );
-               
-               $wgOut->addHTML( "
+               if ( $wgUseCopyrightUpload ) {
+                       $filestatus = wfMsgHtml ( 'filestatus' );
+                       $copystatus =  htmlspecialchars( $this->mUploadCopyStatus );
+                       $filesource = wfMsgHtml ( 'filesource' );
+                       $uploadsource = htmlspecialchars( $this->mUploadSource );
+                       
+                       $wgOut->addHTML( "
                                <td align='right' nowrap='nowrap'><label for='wpUploadCopyStatus'>$filestatus:</label></td>
                                <td><input tabindex='5' type='text' name='wpUploadCopyStatus' id='wpUploadCopyStatus' value=\"$copystatus\" size='40' /></td>
                        </tr>
@@ -640,10 +672,10 @@ class UploadForm {
                        </tr>
                        <tr>
                ");
-       }
-       
-       
-       $wgOut->addHtml( "
+               }
+
+
+               $wgOut->addHtml( "
                <td></td>
                <td>
                        <input tabindex='7' type='checkbox' name='wpWatchthis' id='wpWatchthis' $watchChecked value='true' />
@@ -659,6 +691,16 @@ class UploadForm {
                <td></td>
                <td align='left'><input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\" /></td>
        </tr>
+
+       <tr>
+               <td></td>
+               <td align='left'>
+               " );
+               $wgOut->addWikiText( wfMsgForContent( 'edittools' ) );
+               $wgOut->addHTML( "
+               </td>
+       </tr>
+
        </table>
        </form>" );
        }
@@ -740,7 +782,7 @@ class UploadForm {
                }
 
                #check for htmlish code and javascript
-               if( $this->detectScript ( $tmpfile, $mime ) ) {
+               if( $this->detectScript ( $tmpfile, $mime, $extension ) ) {
                        return new WikiErrorMsg( 'uploadscripted' );
                }
 
@@ -800,9 +842,11 @@ class UploadForm {
        *
        * @param string $file Pathname to the temporary upload file
        * @param string $mime The mime type of the file
+       * @param string $extension The extension of the file
        * @return bool true if the file contains something looking like embedded scripts
        */
-       function detectScript($file,$mime) {
+       function detectScript($file, $mime, $extension) {
+               global $wgAllowTitlesInSVG;
 
                #ugly hack: for text files, always look at the entire file.
                #For binarie field, just check the first K.
@@ -857,9 +901,11 @@ class UploadForm {
                        '<img',
                        '<pre',
                        '<script', #also in safari
-                       '<table',
-                       '<title'   #also in safari
+                       '<table'
                        );
+               if( ! $wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
+                       $tags[] = '<title';
+               }
 
                foreach( $tags as $tag ) {
                        if( false !== strpos( $chunk, $tag ) ) {
@@ -897,7 +943,7 @@ class UploadForm {
        *         If textual feedback is missing but a virus was found, this function returns true.
        */
        function detectVirus($file) {
-               global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired;
+               global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
 
                $fname= "SpecialUpload::detectVirus";
 
@@ -1011,7 +1057,7 @@ class UploadForm {
                        unlink( $this->mUploadTempName );
                }
        }
-       
+
        /**
         * Check if there's an overwrite conflict and, if so, if restrictions
         * forbid this user from performing the upload.
@@ -1026,7 +1072,7 @@ class UploadForm {
                        // But if it does, fall through to previous behavior
                        return false;
                }
-               
+
                $error = '';
                if( $img->exists() ) {
                        global $wgUser, $wgOut;
@@ -1041,12 +1087,12 @@ class UploadForm {
                                }
                        }
                }
-               
+
                if( $error ) {
                        $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
                        return new WikiError( $wgOut->parse( $errorText ) );
                }
-               
+
                // Rockin', go ahead and upload
                return true;
        }