use class constant from r64403
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index 5d955b3..4f0522e 100644 (file)
@@ -33,6 +33,11 @@ abstract class UploadBase {
        const HOOK_ABORTED = 11;
 
        const SESSION_VERSION = 2;
+       const SESSION_KEYNAME = 'wsUploadData';
+
+       static public function getSessionKeyname() {
+               return self::SESSION_KEYNAME;
+       }
 
        /**
         * Returns true if uploads are enabled.
@@ -143,15 +148,37 @@ abstract class UploadBase {
        }
 
        /**
-        * Return the file size
+        * Return true if the file is empty
+        * @return bool
         */
        public function isEmptyFile() {
                return empty( $this->mFileSize );
        }
 
        /**
-        * @param string $srcPath the source path
-        * @returns the real path if it was a virtual URL
+        * Return the file size
+        * @return integer
+        */
+       public function getFileSize() {
+               return $this->mFileSize;
+       }
+
+       /**
+        * Append a file to the Repo file
+        *
+        * @param string $srcPath Path to source file
+        * @param string $toAppendPath Path to the Repo file that will be appended to.
+        * @return Status Status
+        */
+       protected function appendToUploadFile( $srcPath, $toAppendPath ) {
+               $repo = RepoGroup::singleton()->getLocalRepo();
+               $status = $repo->append( $srcPath, $toAppendPath );
+               return $status;
+       }
+
+       /**
+        * @param $srcPath String: the source path
+        * @return the real path if it was a virtual URL
         */
        function getRealPath( $srcPath ) {
                $repo = RepoGroup::singleton()->getLocalRepo();
@@ -163,9 +190,9 @@ abstract class UploadBase {
 
        /**
         * Verify whether the upload is sane.
-        * Returns self::OK or else an array with error information
+        * @return mixed self::OK or else an array with error information
         */
-       public function verifyUpload() {
+       public function verifyUpload( ) {
                /**
                 * If there was no filename or a zero size given, give up quick.
                 */
@@ -189,6 +216,31 @@ abstract class UploadBase {
                        );
                }
 
+               /**
+                * Make sure this file can be created
+                */
+               $result = $this->validateNameAndOverwrite();
+               if( $result !== true ) {
+                       return $result;
+               }
+
+               $error = '';
+               if( !wfRunHooks( 'UploadVerification',
+                               array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
+                       // @fixme This status needs another name...
+                       return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
+               }
+
+               return array( 'status' => self::OK );
+       }
+
+       /**
+        * Verify that the name is valid and, if necessary, that we can overwrite
+        *
+        * @return mixed true if valid, otherwise and array with 'status'
+        * and other keys
+        **/
+       public function validateNameAndOverwrite() {
                $nt = $this->getTitle();
                if( is_null( $nt ) ) {
                        $result = array( 'status' => $this->mTitleError );
@@ -212,15 +264,7 @@ abstract class UploadBase {
                                'overwrite' => $overwrite
                        );
                }
-
-               $error = '';
-               if( !wfRunHooks( 'UploadVerification',
-                               array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
-                       // This status needs another name...
-                       return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
-               }
-
-               return array( 'status' => self::OK );
+               return true;
        }
 
        /**
@@ -286,7 +330,7 @@ abstract class UploadBase {
        /**
         * Check whether the user can edit, upload and create the image.
         *
-        * @param User $user the user to verify the permissions against
+        * @param $user the User object to verify the permissions against
         * @return mixed An array as returned by getUserPermissionsErrors or true
         *               in case the user has proper permissions.
         */
@@ -313,7 +357,7 @@ abstract class UploadBase {
        /**
         * Check for non fatal problems with the file
         *
-        * @return array Array of warnings
+        * @return Array of warnings
         */
        public function checkWarnings() {
                $warnings = array();
@@ -390,11 +434,11 @@ abstract class UploadBase {
                $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
                        File::DELETE_SOURCE, $this->mFileProps, false, $user );
 
-               if( $status->isGood() && $watch ) {
-                       $user->addWatch( $this->getLocalFile()->getTitle() );
-               }
-
                if( $status->isGood() ) {
+                       if ( $watch ) {
+                               $user->addWatch( $this->getLocalFile()->getTitle() );
+                       }
+
                        wfRunHooks( 'UploadComplete', array( &$this ) );
                }
 
@@ -417,9 +461,7 @@ abstract class UploadBase {
                 * filter out illegal characters, and try to make a legible name
                 * out of it. We'll strip some silently that Title would die on.
                 */
-               $basename = $this->mDesiredDestName;
-
-               $this->mFilteredName = wfStripIllegalFilenameChars( $basename );
+               $this->mFilteredName = wfStripIllegalFilenameChars( $this->mDesiredDestName );
                /* Normalize to title form before we do any further processing */
                $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
                if( is_null( $nt ) ) {
@@ -466,11 +508,6 @@ abstract class UploadBase {
                        return $this->mTitle = null;
                }
 
-               $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
-               if( is_null( $nt ) ) {
-                       $this->mTitleError = self::ILLEGAL_FILENAME;
-                       return $this->mTitle = null;
-               }
                return $this->mTitle = $nt;
        }
 
@@ -492,9 +529,9 @@ abstract class UploadBase {
         * If the user doesn't explicitly cancel or accept, these files
         * can accumulate in the temp directory.
         *
-        * @param string $saveName - the destination filename
-        * @param string $tempSrc - the source temporary file to save
-        * @return string - full path the stashed file, or false on failure
+        * @param $saveName String: the destination filename
+        * @param $tempSrc String: the source temporary file to save
+        * @return String: full path the stashed file, or false on failure
         */
        protected function saveTempUploadedFile( $saveName, $tempSrc ) {
                $repo = RepoGroup::singleton()->getLocalRepo();
@@ -508,7 +545,7 @@ abstract class UploadBase {
         * Returns a key value which will be passed through a form
         * to pick up the path info on a later invocation.
         *
-        * @return int Session key
+        * @return Integer: session key
         */
        public function stashSession() {
                $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
@@ -516,11 +553,9 @@ abstract class UploadBase {
                        # Couldn't save the file.
                        return false;
                }
-               if( !isset( $_SESSION ) ) {
-                       session_start(); // start up the session (might have been previously closed to prevent php session locking)
-               }
+
                $key = $this->getSessionKey();
-               $_SESSION['wsUploadData'][$key] = array(
+               $_SESSION[self::SESSION_KEYNAME][$key] = array(
                        'mTempPath'       => $status->value,
                        'mFileSize'       => $this->mFileSize,
                        'mFileProps'      => $this->mFileProps,
@@ -534,7 +569,7 @@ abstract class UploadBase {
         */
        protected function getSessionKey() {
                $key = mt_rand( 0, 0x7fffffff );
-               $_SESSION['wsUploadData'][$key] = array();
+               $_SESSION[self::SESSION_KEYNAME][$key] = array();
                return $key;
        }
 
@@ -571,9 +606,9 @@ abstract class UploadBase {
         * Perform case-insensitive match against a list of file extensions.
         * Returns true if the extension is in the list.
         *
-        * @param string $ext
-        * @param array $list
-        * @return bool
+        * @param $ext String
+        * @param $list Array
+        * @return Boolean
         */
        public static function checkFileExtension( $ext, $list ) {
                return in_array( strtolower( $ext ), $list );
@@ -583,9 +618,9 @@ abstract class UploadBase {
         * Perform case-insensitive match against a list of file extensions.
         * Returns true if any of the extensions are in the list.
         *
-        * @param array $ext
-        * @param array $list
-        * @return bool
+        * @param $ext Array
+        * @param $list Array
+        * @return Boolean
         */
        public static function checkFileExtensionList( $ext, $list ) {
                foreach( $ext as $e ) {
@@ -599,9 +634,9 @@ abstract class UploadBase {
        /**
         * Checks if the mime type of the uploaded file matches the file extension.
         *
-        * @param string $mime the mime type of the uploaded file
-        * @param string $extension The filename extension that the file is to be served with
-        * @return bool
+        * @param $mime String: the mime type of the uploaded file
+        * @param $extension String: the filename extension that the file is to be served with
+        * @return Boolean
         */
        public static function verifyExtension( $mime, $extension ) {
                $magic = MimeMagic::singleton();
@@ -640,10 +675,10 @@ abstract class UploadBase {
         * potentially harmful. The present implementation will produce false
         * positives in some situations.
         *
-        * @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
+        * @param $file String: pathname to the temporary upload file
+        * @param $mime String: the mime type of the file
+        * @param $extension String: the extension of the file
+        * @return Boolean: true if the file contains something looking like embedded scripts
         */
        public static function detectScript( $file, $mime, $extension ) {
                global $wgAllowTitlesInSVG;
@@ -790,7 +825,7 @@ abstract class UploadBase {
         * This relies on the $wgAntivirus and $wgAntivirusSetup variables.
         * $wgAntivirusRequired may be used to deny upload if the scan fails.
         *
-        * @param string $file Pathname to the temporary upload file
+        * @param $file String: pathname to the temporary upload file
         * @return mixed false if not virus is found, NULL if the scan fails or is disabled,
         *         or a string containing feedback from the virus scanner if a virus was found.
         *         If textual feedback is missing but a virus was found, this function returns true.
@@ -935,9 +970,9 @@ abstract class UploadBase {
        /**
         * Check if a user is the last uploader
         *
-        * @param User $user
-        * @param string $img, image name
-        * @return bool
+        * @param $user User object
+        * @param $img String: image name
+        * @return Boolean
         */
        public static function userCanReUpload( User $user, $img ) {
                if( $user->isAllowed( 'reupload' ) ) {
@@ -964,7 +999,7 @@ abstract class UploadBase {
         * - File exists with normalized extension
         * - The file looks like a thumbnail and the original exists
         *
-        * @param File $file The file to check
+        * @param $file The File object to check
         * @return mixed False if the file does not exists, else an array
         */
        public static function getExistsWarning( $file ) {