From: Magnus Manske Date: Tue, 22 Aug 2006 10:08:10 +0000 (+0000) Subject: Option: Allow upload from URL. Set $wgAllowCopyUploads = true ; X-Git-Tag: 1.31.0-rc.0~55932 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=0a7dc1b2f66e3ec57a8efcf6118e65be6c492d87;p=lhc%2Fweb%2Fwiklou.git Option: Allow upload from URL. Set $wgAllowCopyUploads = true ; --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 368e1ec55f..8bb8fd1832 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -147,6 +147,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 7044) Introduce "padleft" and "padright" colon functions * Pass page title as parameters to "linkshere" and "nolinkshere" and update default message text +* Allows to upload from publicy accessible URL. Set $wgAllowCopyUploads = true ; in LocalSettings.php == Languages updated == diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 57bf270f1c..e9889fdd72 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -337,6 +337,8 @@ $wgSharedUploadDBname = false; $wgSharedUploadDBprefix = ''; /** Cache shared metadata in memcached. Don't do this if the commons wiki is in a different memcached domain */ $wgCacheSharedUploads = true; +/** Allow for upload to be copied from an URL. Requires Special:Upload?source=web */ +$wgAllowCopyUploads = false; /** * Point the upload navigation link to an external URL diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index 06336df9f8..f14eeccf65 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -30,7 +30,7 @@ class UploadForm { var $mUploadFile, $mUploadDescription, $mLicense ,$mIgnoreWarning, $mUploadError; var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion; var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload; - var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile; + var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile, $mSourceType; /**#@-*/ /** @@ -39,6 +39,7 @@ class UploadForm { * @param $request Data posted. */ function UploadForm( &$request ) { + global $wgAllowCopyUploads; $this->mDestFile = $request->getText( 'wpDestFile' ); if( !$request->wasPosted() ) { @@ -55,6 +56,7 @@ class UploadForm { $this->mUploadCopyStatus = $request->getText( 'wpUploadCopyStatus' ); $this->mUploadSource = $request->getText( 'wpUploadSource' ); $this->mWatchthis = $request->getBool( 'wpWatchthis' ); + $this->mSourceType = $request->getBool( 'wpSourceType' ); wfDebug( "UploadForm: watchthis is: '$this->mWatchthis'\n" ); $this->mAction = $request->getVal( 'action' ); @@ -79,16 +81,49 @@ class UploadForm { /** *Check for a newly uploaded file. */ - $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; - $this->mRemoveTempFile = false; // PHP will handle this + if( $wgAllowCopyUploads && $this->mSourceType == 'web' ) { + $this->initialize_web_file( $request ); + } else { + $this->initialize_php_file( $request ); + } } } + /** + * Initialize the uploaded file from PHP data + * @access private + */ + function initialize_php_file( &$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; + $this->mRemoveTempFile = false; // PHP will handle this + } + + /** + * Copy a web file to a temporary file + * @access private + */ + function initialize_web_file( &$request ) { + global $wgTmpDirectory; + $url = $request->getText( 'wpUploadFile' ); + $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' ); + + # Maybe check for filesize($url) first? + $error = !@copy( $url, $local_file ); + + $this->mUploadTempName = $local_file; + $this->mUploadSize = filesize( $local_file ); + $this->mOname = array_pop( explode( '/', $url ) ); + $this->mUploadError = $error; + $this->mSessionKey = false; + $this->mStashed = false; + $this->mRemoveTempFile = false; // PHP will *not* handle this + } + /** * Start doing stuff * @access public @@ -126,17 +161,17 @@ class UploadForm { } /** Check if the image directory is writeable, this is a common mistake */ - if ( !is_writeable( $wgUploadDirectory ) ) { + if( !is_writeable( $wgUploadDirectory ) ) { $wgOut->addWikiText( wfMsg( 'upload_directory_read_only', $wgUploadDirectory ) ); return; } if( $this->mReUpload ) { - if ( !$this->unsaveUploadedFile() ) { + if( !$this->unsaveUploadedFile() ) { return; } $this->mainUploadForm(); - } else if ( 'submit' == $this->mAction || $this->mUpload ) { + } else if( 'submit' == $this->mAction || $this->mUpload ) { $this->processUpload(); } else { $this->mainUploadForm(); @@ -156,7 +191,7 @@ class UploadForm { global $wgUser, $wgOut; /* Check for PHP error if any, requires php 4.2 or newer */ - if ( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) { + if( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) { $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) ); return; } @@ -170,7 +205,7 @@ class UploadForm { } # Chop off any directories in the given filename - if ( $this->mDestFile ) { + if( $this->mDestFile ) { $basename = wfBaseName( $this->mDestFile ); } else { $basename = wfBaseName( $this->mOname ); @@ -196,7 +231,7 @@ class UploadForm { $partname .= '.' . $ext[$i]; } - if ( strlen( $partname ) < 3 ) { + if( strlen( $partname ) < 3 ) { $this->mainUploadForm( wfMsgHtml( 'minlength' ) ); return; } @@ -363,7 +398,9 @@ class UploadForm { * is a PHP-managed upload temporary */ function saveUploadedFile( $saveName, $tempName, $useRename = false ) { - global $wgOut; + global $wgOut, $wgAllowCopyUploads; + + if ( !$useRename AND $wgAllowCopyUploads AND $this->mSourceType == 'web' ) $useRename = true; $fname= "SpecialUpload::saveUploadedFile"; @@ -586,6 +623,7 @@ class UploadForm { function mainUploadForm( $msg='' ) { global $wgOut, $wgUser; global $wgUseCopyrightUpload; + global $wgRequest, $wgAllowCopyUploads; $cols = intval($wgUser->getOption( 'cols' )); $ew = $wgUser->getOption( 'editwidth' ); @@ -623,6 +661,14 @@ class UploadForm { $watchChecked = $wgUser->getOption( 'watchdefault' ) ? 'checked="checked"' : ''; + + if ( $wgAllowCopyUploads AND $wgRequest->getText('source') == 'web' ) { + $sourcetype = 'text'; + $source_comment = '' . wfMsgHtml( 'upload_source_url' ); + } else { + $sourcetype = 'file'; + $source_comment = ''; + } $wgOut->addHTML( "
@@ -630,7 +676,7 @@ class UploadForm { - mDestFile?"":"onchange='fillDestFilename()' ") . "size='40' /> + mDestFile?"":"onchange='fillDestFilename()' ") . "size='40' />{$source_comment} diff --git a/languages/MessagesEn.php b/languages/MessagesEn.php index f7440ff927..ce7049492b 100644 --- a/languages/MessagesEn.php +++ b/languages/MessagesEn.php @@ -1149,6 +1149,7 @@ created and by whom, and anything else you may know about it. If this is an imag 'license' => 'Licensing', 'nolicense' => 'None selected', 'licenses' => '-', # Don't duplicate this in translations +'upload_source_url' => ' (vaild, publicy accessible URL)', # Image list #