Mediawiki uploads on an open basedir server

From HunterWiki

Jump to: navigation, search

Here's what I had to do to get Mediawiki to accept uploads when I'm on a server with open_basedir protection:

Edit LocalSettings.php as follows:

$wgHashedUploadDirectory = false;
$wgUploadPath       = "$wgScriptPath/images";
$wgUploadDirectory  = "/true/path/on/server/wiki/images";

(This part may be unnecessary, by the time I got it working I was too lazy to figure out if this was still needed)

Edit WebRequest.php as follows:

	/**
	 * Return the path to the temporary file where PHP has stored the upload.
	 * @param $key String:
	 * @return string or NULL if no such file.
	 */
	function getFileTempname( $key ) {
		if( !isset( $_FILES[$key] ) ) {
			return NULL;
		}

		// MDH 20070125: Try moving somewhere allowed
		$dest = "/true/path/on/server/wiki/images/tmp/".$this->getFileName( $key );
		move_uploaded_file($_FILES[$key]['tmp_name'], $dest);
//	        return $_FILES[$key]['tmp_name'];
		return $dest;
	}

(This presumes the getFileTempname never gets called twice on one invocation, which may or may not be universally true, but has certainly been true for me so far) This change makes all subsequent references to the uploaded file refer to your actual approved file space, however it causes subsequent calls to move_uploaded_file to fail (see next step).

Edit SpecialUpload.php as follows:

	/**
	 * Initialize the uploaded file from PHP data
	 * @access private
	 */
	function initializeFromUpload( $request ) {
		$this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
		$this->mUploadSize     = $request->getFileSize( 'wpUploadFile' );
		$this->mOname          = $request->getFileName( 'wpUploadFile' );
		$this->mUploadError    = $request->getUploadError( 'wpUploadFile' );
		$this->mSessionKey     = false;
		$this->mStashed        = false;
		// MDH 20070125: Try letting us handle it
//		$this->mRemoveTempFile = false; // PHP will handle this
		$this->mRemoveTempFile = true;
	}

This change triggers the subsequent code to use a rename function for the move to the final location, as opposed to the move_uploaded_file.

Hope this is helpful to someone else someday!

Personal tools