transformMsg profiling hook, shows when site messages suck
[lhc/web/wiklou.git] / includes / SpecialUpload.php
index c17fea2..ff32564 100644 (file)
@@ -8,9 +8,9 @@
 /**
  *
  */
-require_once( 'Image.php' );
-require_once( 'MacBinary.php' );
-
+require_once 'Image.php';
+require_once 'MacBinary.php';
+require_once 'Licenses.php';
 /**
  * Entry point
  */
@@ -29,7 +29,7 @@ class UploadForm {
        /**#@+
         * @access private
         */
-       var $mUploadFile, $mUploadDescription, $mIgnoreWarning, $mUploadError;
+       var $mUploadFile, $mUploadDescription, $mLicense ,$mIgnoreWarning, $mUploadError;
        var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
        var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
        var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile;
@@ -53,8 +53,11 @@ class UploadForm {
                $this->mUpload            = $request->getCheck( 'wpUpload' );
 
                $this->mUploadDescription = $request->getText( 'wpUploadDescription' );
+               $this->mLicense           = $request->getText( 'wpLicense' );
                $this->mUploadCopyStatus  = $request->getText( 'wpUploadCopyStatus' );
-               $this->mUploadSource      = $request->getText( 'wpUploadSource');
+               $this->mUploadSource      = $request->getText( 'wpUploadSource' );
+               $this->mWatchthis         = $request->getBool( 'wpWatchthis' );
+               wfDebug( "UploadForm: watchthis is: '$this->mWatchthis'\n" );
 
                $this->mAction            = $request->getVal( 'action' );
 
@@ -96,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;
@@ -138,9 +153,7 @@ class UploadForm {
         * @access private
         */
        function processUpload() {
-               global $wgUser, $wgOut, $wgLang, $wgContLang;
-               global $wgUploadDirectory;
-               global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
+               global $wgUser, $wgOut, $wgUploadDirectory;
 
                /* Check for PHP error if any, requires php 4.2 or newer */
                if ( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
@@ -201,6 +214,14 @@ class UploadForm {
                        return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
                }
 
+               /**
+                * In some cases we may forbid overwriting of existing files.
+                */
+               $overwrite = $this->checkOverwrite( $this->mUploadSaveName );
+               if( WikiError::isError( $overwrite ) ) {
+                       return $this->uploadError( $overwrite->toString() );
+               }
+
                /* Don't allow users to override the blacklist (check file extension) */
                global $wgStrictFileExtensions;
                global $wgFileExtensions, $wgFileBlacklist;
@@ -224,12 +245,26 @@ class UploadForm {
                        }
                }
 
+               /**
+                * Provide an opportunity for extensions to add futher checks
+                */
+               $error = '';
+               if( !wfRunHooks( 'UploadVerification',
+                               array( $this->mUploadSaveName, $this->mUploadTempName, &$error ) ) ) {
+                       return $this->uploadError( $error );
+               }
+
                /**
                 * Check for non-fatal conditions
                 */
                if ( ! $this->mIgnoreWarning ) {
                        $warning = '';
-                       if( $this->mUploadSaveName != ucfirst( $filtered ) ) {
+
+                       global $wgCapitalLinks;
+                       if( $wgCapitalLinks ) {
+                               $filtered = ucfirst( $filtered );
+                       }
+                       if( $this->mUploadSaveName != $filtered ) {
                                $warning .=  '<li>'.wfMsgHtml( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
                        }
 
@@ -281,8 +316,10 @@ class UploadForm {
                        $img = Image::newFromName( $this->mUploadSaveName );
                        $success = $img->recordUpload( $this->mUploadOldVersion,
                                                        $this->mUploadDescription,
+                                                       $this->mLicense,
                                                        $this->mUploadCopyStatus,
-                                                       $this->mUploadSource );
+                                                       $this->mUploadSource,
+                                                       $this->mWatchthis );
 
                        if ( $success ) {
                                $this->showSuccess();
@@ -456,7 +493,7 @@ class UploadForm {
         * @access private
         */
        function uploadWarning( $warning ) {
-               global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
+               global $wgOut, $wgUser, $wgUploadDirectory, $wgRequest;
                global $wgUseCopyrightUpload;
 
                $this->mSessionKey = $this->stashSession();
@@ -490,7 +527,9 @@ class UploadForm {
                <input type='hidden' name='wpIgnoreWarning' value='1' />
                <input type='hidden' name='wpSessionKey' value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
                <input type='hidden' name='wpUploadDescription' value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
+               <input type='hidden' name='wpLicense' value=\"" . htmlspecialchars( $this->mLicense ) . "\" />
                <input type='hidden' name='wpDestFile' value=\"" . htmlspecialchars( $this->mDestFile ) . "\" />
+               <input type='hidden' name='wpWatchthis' value=\"" . htmlspecialchars( intval( $this->mWatchthis ) ) . "\" />
        {$copyright}
        <table border='0'>
                <tr>
@@ -518,7 +557,7 @@ class UploadForm {
         * @access private
         */
        function mainUploadForm( $msg='' ) {
-               global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
+               global $wgOut, $wgUser, $wgUploadDirectory, $wgRequest;
                global $wgUseCopyrightUpload;
 
                $cols = intval($wgUser->getOption( 'cols' ));
@@ -531,13 +570,21 @@ class UploadForm {
                        $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
                          "<span class='error'>{$msg}</span>\n" );
                }
+               $wgOut->addHTML( '<div id="uploadtext">' );
                $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
+               $wgOut->addHTML( '</div>' );
                $sk = $wgUser->getSkin();
 
 
                $sourcefilename = wfMsgHtml( 'sourcefilename' );
                $destfilename = wfMsgHtml( 'destfilename' );
                $summary = wfMsgWikiHtml( 'fileuploadsummary' );
+
+               $licenses = new Licenses();
+               $license = wfMsgHtml( 'license' );
+               $nolicense = wfMsgHtml( 'nolicense' );
+               $licenseshtml = $licenses->getHtml();
+
                $ulb = wfMsgHtml( 'uploadbtn' );
 
 
@@ -545,43 +592,98 @@ class UploadForm {
                $action = $titleObj->escapeLocalURL();
 
                $encDestFile = htmlspecialchars( $this->mDestFile );
-               $source = null;
 
-               if ( $wgUseCopyrightUpload )
-                 {
-                       $source = "
-       <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
-       <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
-       htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
-       </tr><tr>
-       <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
-       <td><input tabindex='4' type='text' name='wpUploadSource' value=\"" .
-       htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
-       " ;
-                 }
+               $watchChecked = $wgUser->getOption( 'watchdefault' )
+                       ? 'checked="checked"'
+                       : '';
 
                $wgOut->addHTML( "
        <form id='upload' method='post' enctype='multipart/form-data' action=\"$action\">
-       <table border='0'><tr>
-
-       <td align='right'>{$sourcefilename}:</td><td align='left'>
-       <input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' onchange='fillDestFilename()' size='40' />
-       </td></tr><tr>
-
-       <td align='right'>{$destfilename}:</td><td align='left'>
-       <input tabindex='1' type='text' name='wpDestFile' id='wpDestFile' size='40' value=\"$encDestFile\" />
-       </td></tr><tr>
-
-       <td align='right'>{$summary}</td><td align='left'>
-       <textarea tabindex='2' name='wpUploadDescription' rows='6' cols='{$cols}'{$ew}>"        
-         . htmlspecialchars( $this->mUploadDescription ) .
-       "</textarea>
-       </td></tr><tr>
-       {$source}
+               <table border='0'>
+               <tr>
+                       <td align='right'><label for='wpUploadFile'>{$sourcefilename}:</label></td>
+                       <td align='left'>
+                               <input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " . ($this->mDestFile?"":"onchange='fillDestFilename()' ") . "size='40' />
+                       </td>
+               </tr>
+               <tr>
+                       <td align='right'><label for='wpDestFile'>{$destfilename}:</label></td>
+                       <td align='left'>
+                               <input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='40' value=\"$encDestFile\" />
+                       </td>
+               </tr>
+               <tr>
+                       <td align='right'><label for='wpUploadDescription'>{$summary}</label></td>
+                       <td align='left'>
+                               <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$ew}>" . htmlspecialchars( $this->mUploadDescription ) . "</textarea>
+                       </td>
+               </tr>
+               <tr>" );
+
+       if ( $licenseshtml != '' ) {
+               global $wgStylePath;
+               $wgOut->addHTML( "
+                       <td align='right'><label for='wpLicense'>$license:</label></td>
+                       <td align='left'>
+                               <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>
+                       </td>
+                       </tr>
+                       <tr>
+               ");
+       }
+
+       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>
+                       <tr>
+                               <td align='right'><label for='wpUploadCopyStatus'>$filesource:</label></td>
+                               <td><input tabindex='6' type='text' name='wpUploadSource' id='wpUploadCopyStatus' value=\"$uploadsource\" size='40' /></td>
+                       </tr>
+                       <tr>
+               ");
+       }
+
+
+       $wgOut->addHtml( "
+               <td></td>
+               <td>
+                       <input tabindex='7' type='checkbox' name='wpWatchthis' id='wpWatchthis' $watchChecked value='true' />
+                       <label for='wpWatchthis'>" . wfMsgHtml( 'watchthis' ) . "</label>
+                       <input tabindex='8' type='checkbox' name='wpIgnoreWarning' id='wpIgnoreWarning' value='true' />
+                       <label for='wpIgnoreWarning'>" . wfMsgHtml( 'ignorewarnings' ) . "</label>
+               </td>
+       </tr>
+       <tr>
+
+       </tr>
+       <tr>
+               <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>
-       <tr><td></td><td align='left'>
-       <input tabindex='5' type='submit' name='wpUpload' value=\"{$ulb}\" />
-       </td></tr></table></form>\n" );
+
+       </table>
+       </form>" );
        }
 
        /* -------------------------------------------------------------- */
@@ -687,12 +789,16 @@ class UploadForm {
        function verifyExtension( $mime, $extension ) {
                $fname = 'SpecialUpload::verifyExtension';
 
-               if (!$mime || $mime=="unknown" || $mime=="unknown/unknown") {
-                       wfDebug( "$fname: passing file with unknown mime type\n" );
-                       return true;
-               }
+               $magic =& wfGetMimeMagic();
 
-               $magic=& wfGetMimeMagic();
+               if ( ! $mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
+                       if ( ! $magic->isRecognizableExtension( $extension ) ) {
+                               wfDebug( "$fname: passing file with unknown detected mime type; unrecognized extension '$extension', can't verify\n" );
+                               return true;
+                       } else {
+                               wfDebug( "$fname: rejecting file with unknown detected mime type; recognized extension '$extension', so probably invalid file\n" );
+                               return false;
+                       }
 
                $match= $magic->isMatchingExtension($extension,$mime);
 
@@ -929,5 +1035,44 @@ class UploadForm {
                }
        }
 
+       /**
+        * Check if there's an overwrite conflict and, if so, if restrictions
+        * forbid this user from performing the upload.
+        *
+        * @return mixed true on success, WikiError on failure
+        * @access private
+        */
+       function checkOverwrite( $name ) {
+               $img = Image::newFromName( $name );
+               if( is_null( $img ) ) {
+                       // Uh... this shouldn't happen ;)
+                       // But if it does, fall through to previous behavior
+                       return false;
+               }
+
+               $error = '';
+               if( $img->exists() ) {
+                       global $wgUser, $wgOut;
+                       if( $img->isLocal() ) {
+                               if( !$wgUser->isAllowed( 'reupload' ) ) {
+                                       $error = 'fileexists-forbidden';
+                               }
+                       } else {
+                               if( !$wgUser->isAllowed( 'reupload' ) ||
+                                   !$wgUser->isAllowed( 'reupload-shared' ) ) {
+                                       $error = "fileexists-shared-forbidden";
+                               }
+                       }
+               }
+
+               if( $error ) {
+                       $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
+                       return new WikiError( $wgOut->parse( $errorText ) );
+               }
+
+               // Rockin', go ahead and upload
+               return true;
+       }
+
 }
 ?>