00001 <?php
00002
00003 function
wfSpecialUpload()
00004 {
00005 global
$wgRequest;
00006 $form =
new UploadForm( $wgRequest );
00007 $form->execute();
00008 }
00009
00010 class UploadForm {
00011 var
$mUploadAffirm,
$mUploadFile,
$mUploadDescription,
$mIgnoreWarning;
00012 var
$mUploadSaveName,
$mUploadTempName,
$mUploadSize,
$mUploadOldVersion;
00013 var
$mUploadCopyStatus,
$mUploadSource,
$mReUpload,
$mAction,
$mUpload;
00014 var
$mOname,
$mSessionKey;
00015
00016 function
UploadForm( &$request ) {
00017 $this->mUploadAffirm = $request->getVal( 'wpUploadAffirm' );
00018 $this->mUploadFile = $request->getVal( 'wpUploadFile' );
00019 $this->mUploadDescription = $request->getVal( 'wpUploadDescription');
00020 $this->mIgnoreWarning = $request->getVal( 'wpIgnoreWarning');
00021 $this->mUploadSaveName = $request->getVal( 'wpUploadSaveName');
00022 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
00023 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
00024 $this->mUploadSize = $request->getVal( 'wpUploadSize');
00025 $this->mUploadOldVersion = $request->getVal( 'wpUploadOldVersion');
00026 $this->mUploadCopyStatus = $request->getVal( 'wpUploadCopyStatus');
00027 $this->mUploadSource = $request->getVal( 'wpUploadSource');
00028 $this->mReUpload = $request->getCheck( 'wpReUpload' );
00029 $this->mAction = $request->getVal( 'action' );
00030 $this->mUpload = $request->getCheck( 'wpUpload' );
00031 $this->mSessionKey = $request->getVal( 'wpSessionKey' );
00032
00033
if ( ! $this->mUploadTempName ) {
00034 $this->mUploadTempName = @$_FILES['wpUploadFile']['tmp_name'];
00035 }
00036
if ( ! $this->mUploadSize ) {
00037 $this->mUploadSize = @$_FILES['wpUploadFile']['size'];
00038 }
00039 $this->mOname = $request->getGPCVal( $_FILES['wpUploadFile'], '
name',
"" );
00040
00041 }
00042
00043 function
execute() {
00044 global
$wgUser,
$wgOut;
00045 global
$wgDisableUploads;
00046
00047
if (
$wgDisableUploads ) {
00048
$wgOut->addWikiText(
wfMsg(
"uploaddisabled" ) );
00049
return;
00050 }
00051
if ( ( 0 ==
$wgUser->getID() )
00052 or
$wgUser->isBlocked() ) {
00053
$wgOut->errorpage(
"uploadnologin",
"uploadnologintext" );
00054
return;
00055 }
00056
if (
wfReadOnly() ) {
00057
$wgOut->readOnlyPage();
00058
return;
00059 }
00060
if ( $this->mReUpload ) {
00061 $this->
unsaveUploadedFile();
00062 $this->
mainUploadForm(
"" );
00063 }
else if (
"submit" == $this->mAction || $this->mUpload ) {
00064 $this->
processUpload();
00065 }
else {
00066 $this->
mainUploadForm(
"" );
00067 }
00068 }
00069
00070
00071 function
processUpload()
00072 {
00073 global
$wgUser,
$wgOut,
$wgLang;
00074 global
$wgUploadDirectory;
00075 global $wgSavedFile, $wgUploadOldVersion;
00076 global $wgUseCopyrightUpload;
00077 global
$wgCheckFileExtensions,
$wgStrictFileExtensions;
00078 global
$wgFileExtensions,
$wgFileBlacklist,
$wgUploadSizeWarning;
00079
00080
if ( $wgUseCopyrightUpload ) {
00081 $this->mUploadAffirm = 1;
00082
if ( trim ( $this->mUploadCopyStatus ) ==
"" || trim ( $this->mUploadSource ) ==
"" ) {
00083 $this->mUploadAffirm = 0;
00084 }
00085 }
00086
00087
if ( 1 != $this->mUploadAffirm ) {
00088 $this->
mainUploadForm( WfMsg(
"noaffirmation" ) );
00089
return;
00090 }
00091
00092
if (
"" != $this->mOname ) {
00093 $basename = strrchr( $this->mOname,
"/" );
00094
if (
false === $basename ) { $basename = $this->mOname; }
00095
else ( $basename = substr( $basename, 1 ) );
00096
00097 $ext = strrchr( $basename,
"." );
00098
if (
false === $ext ) { $ext =
""; }
00099
else { $ext = substr( $ext, 1 ); }
00100
00101
if (
"" == $ext ) { $xl = 0; }
else { $xl = strlen( $ext ) + 1; }
00102 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
00103
00104
if ( strlen( $partname ) < 3 ) {
00105 $this->
mainUploadForm( WfMsg(
"minlength" ) );
00106
return;
00107 }
00108 $nt = Title::newFromText( $basename );
00109 $this->mUploadSaveName = $nt->getDBkey();
00110
00111
00112
if( $this->
checkFileExtension( $ext, $wgFileBlacklist ) ||
00113 (
$wgStrictFileExtensions && !$this->
checkFileExtension( $ext, $wgFileExtensions ) ) ) {
00114
return $this->
uploadError(
wfMsg(
"badfiletype", $ext ) );
00115 }
00116
00117 $this->
saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
00118
if ( ( ! $this->mIgnoreWarning ) &&
00119 ( 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) ) {
00120
return $this->
uploadWarning(
wfMsg(
"badfilename", $this->mUploadSaveName ) );
00121 }
00122
00123
if (
$wgCheckFileExtensions ) {
00124
if ( ( ! $this->mIgnoreWarning ) &&
00125 ( ! $this->
checkFileExtension( $ext, $wgFileExtensions ) ) ) {
00126
return $this->
uploadWarning(
wfMsg(
"badfiletype", $ext ) );
00127 }
00128 }
00129
if (
$wgUploadSizeWarning && ( ! $this->mIgnoreWarning ) &&
00130 ( $this->mUploadSize >
$wgUploadSizeWarning ) ) {
00131
return $this->
uploadWarning(
wfMsg(
"largefile" ) );
00132 }
00133 }
00134
if ( !is_null( $this->mUploadOldVersion ) ) {
00135 $wgUploadOldVersion = $this->mUploadOldVersion;
00136 }
00137
wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
00138 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
00139
00140 $sk =
$wgUser->getSkin();
00141 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
00142 $dname =
$wgLang->getNsText( Namespace::getImage() ) .
":{$this->mUploadSaveName}";
00143 $dlink = $sk->makeKnownLink( $dname, $dname );
00144
00145
$wgOut->addHTML(
"<h2>" .
wfMsg(
"successfulupload" ) .
"</h2>\n" );
00146 $text =
wfMsg(
"fileuploaded", $ilink, $dlink );
00147
$wgOut->addHTML(
"<p>{$text}\n" );
00148
$wgOut->returnToMain(
false );
00149 }
00150
00151 function
checkFileExtension( $ext, $list ) {
00152
return in_array( strtolower( $ext ), $list );
00153 }
00154
00155 function
saveUploadedFile( $saveName, $tempName )
00156 {
00157 global $wgSavedFile, $wgUploadOldVersion;
00158 global
$wgUploadDirectory,
$wgOut;
00159
00160 $dest =
wfImageDir( $saveName );
00161 $archive =
wfImageArchiveDir( $saveName );
00162 $wgSavedFile =
"{$dest}/{$saveName}";
00163
00164
if ( is_file( $wgSavedFile ) ) {
00165 $wgUploadOldVersion = gmdate(
"YmdHis" ) .
"!{$saveName}";
00166
00167
if ( ! rename( $wgSavedFile,
"${archive}/{$wgUploadOldVersion}" ) ) {
00168
$wgOut->fileRenameError( $wgSavedFile,
00169
"${archive}/{$wgUploadOldVersion}" );
00170
return;
00171 }
00172 }
else {
00173 $wgUploadOldVersion =
"";
00174 }
00175
if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
00176
$wgOut->fileCopyError( $tempName, $wgSavedFile );
00177 }
00178 chmod( $wgSavedFile, 0644 );
00179 }
00180
00181 function
unsaveUploadedFile()
00182 {
00183 global
$wgUploadDirectory,
$wgOut,
$wgRequest;
00184
00185 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
00186 $wgUploadOldVersion = $this->mUploadOldVersion;
00187
00188
if ( ! @unlink( $wgSavedFile ) ) {
00189
$wgOut->fileDeleteError( $wgSavedFile );
00190
return;
00191 }
00192
if (
"" != $wgUploadOldVersion ) {
00193 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
00194 $archive =
"{$wgUploadDirectory}/archive/" . $hash{0} .
00195
"/" . substr( $hash, 0, 2 );
00196
00197
if ( ! rename(
"{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
00198
$wgOut->fileRenameError(
"{$archive}/{$wgUploadOldVersion}",
00199 $wgSavedFile );
00200 }
00201 }
00202 }
00203
00204 function
uploadError( $error )
00205 {
00206 global
$wgOut;
00207 $sub =
wfMsg(
"uploadwarning" );
00208
$wgOut->addHTML(
"<h2>{$sub}</h2>\n" );
00209
$wgOut->addHTML(
"<h4><font color=red>{$error}</font></h4>\n" );
00210 }
00211
00212 function
uploadWarning( $warning )
00213 {
00214 global
$wgOut,
$wgUser,
$wgLang,
$wgUploadDirectory,
$wgRequest;
00215 global $wgSavedFile, $wgUploadOldVersion;
00216 global $wgUseCopyrightUpload;
00217
00218
# wgSavedFile is stored in the session not the form, for security
00219
$this->mSessionKey = mt_rand( 0, 0x7fffffff );
00220 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
00221
00222 $sub =
wfMsg(
"uploadwarning" );
00223
$wgOut->addHTML(
"<h2>{$sub}</h2>\n" );
00224
$wgOut->addHTML(
"<h4><font color=red>{$warning}</font></h4>\n" );
00225
00226 $save =
wfMsg(
"savefile" );
00227 $reupload =
wfMsg(
"reupload" );
00228 $iw =
wfMsg(
"ignorewarning" );
00229 $reup =
wfMsg(
"reuploaddesc" );
00230 $titleObj = Title::makeTitle(
NS_SPECIAL,
"Upload" );
00231
$action = $titleObj->escapeLocalURL(
"action=submit" );
00232
00233
if ( $wgUseCopyrightUpload )
00234 {
00235 $copyright =
"
00236
<input type=hidden name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) .
"\">
00237
<input type=hidden name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) .
"\">
00238
";
00239 }
else {
00240 $copyright =
"";
00241 }
00242
00243
$wgOut->addHTML(
"
00244
<form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
00245
action=\"{$action}\">
00246
<input type=hidden name=\"wpUploadAffirm\" value=\"1\">
00247
<input type=hidden name=\"wpIgnoreWarning\" value=\"1\">
00248
<input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) .
"\">
00249
{$copyright}
00250
<input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) .
"\">
00251
<input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) .
"\">
00252
<input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) .
"\">
00253
<input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) .
"\">
00254
<input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) .
"\">
00255
<table border=0><tr>
00256
<tr><td align=right>
00257
<input tabindex=2 type=submit name=\"wpUpload\" value=\"{$save}\">
00258
</td><td align=left>{$iw}</td></tr>
00259
<tr><td align=right>
00260
<input tabindex=2 type=submit name=\"wpReUpload\" value=\"{$reupload}\">
00261
</td><td align=left>{$reup}</td></tr></table></form>\n" );
00262 }
00263
00264 function
mainUploadForm( $msg )
00265 {
00266 global
$wgOut,
$wgUser,
$wgLang,
$wgUploadDirectory,
$wgRequest;
00267 global $wgUseCopyrightUpload;
00268
00269
if (
"" != $msg ) {
00270 $sub =
wfMsg(
"uploaderror" );
00271
$wgOut->addHTML(
"<h2>{$sub}</h2>\n" .
00272
"<h4><font color=red>{$msg}</font></h4>\n" );
00273 }
else {
00274 $sub =
wfMsg(
"uploadfile" );
00275
$wgOut->addHTML(
"<h2>{$sub}</h2>\n" );
00276 }
00277
$wgOut->addHTML(
"<p>" .
wfMsg(
"uploadtext" ) );
00278 $sk =
$wgUser->getSkin();
00279
00280 $fn =
wfMsg(
"filename" );
00281 $fd =
wfMsg(
"filedesc" );
00282 $ulb =
wfMsg(
"uploadbtn" );
00283
00284 $clink = $sk->makeKnownLink(
wfMsg(
"copyrightpage" ),
00285
wfMsg(
"copyrightpagename" ) );
00286 $ca =
wfMsg(
"affirmation", $clink );
00287 $iw =
wfMsg(
"ignorewarning" );
00288
00289 $titleObj = Title::makeTitle(
NS_SPECIAL,
"Upload" );
00290
$action = $titleObj->escapeLocalURL();
00291
00292 $source =
"
00293
<td align=right>
00294
<input tabindex=3 type=checkbox name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\">
00295
</td><td align=left><label for=\"wpUploadAffirm\">{$ca}</label></td>
00296
" ;
00297
if ( $wgUseCopyrightUpload )
00298 {
00299 $source =
"
00300
<td align=right nowrap>" .
wfMsg (
"filestatus" ) .
":</td>
00301
<td><input tabindex=3 type=text name=\"wpUploadCopyStatus\" value=\"" .
00302 htmlspecialchars($this->mUploadCopyStatus).
"\" size=40></td>
00303
</tr><tr>
00304
<td align=right>".
wfMsg (
"filesource" ) .
":</td>
00305
<td><input tabindex=4 type=text name=\"wpUploadSource\" value=\"" .
00306 htmlspecialchars($this->mUploadSource).
"\" size=40></td>
00307
" ;
00308 }
00309
00310
$wgOut->addHTML(
"
00311
<form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
00312
action=\"{$action}\">
00313
<table border=0><tr>
00314
<td align=right>{$fn}:</td><td align=left>
00315
<input tabindex=1 type=file name=\"wpUploadFile\" value=\""
00316 . htmlspecialchars( $this->mUploadFile ) .
"\" size=40>
00317
</td></tr><tr>
00318
<td align=right>{$fd}:</td><td align=left>
00319
<input tabindex=2 type=text name=\"wpUploadDescription\" value=\""
00320 . htmlspecialchars( $this->mUploadDescription ) .
"\" size=40>
00321
</td></tr><tr>
00322
{$source}
00323
</tr>
00324
<tr><td> </td><td align=left>
00325
<input tabindex=5 type=submit name=\"wpUpload\" value=\"{$ulb}\">
00326
</td></tr></table></form>\n" );
00327 }
00328 }
00329 ?>