Show a warning message when uploading empty files (e.g. due to typos)
[lhc/web/wiklou.git] / includes / SpecialUpload.php
1 <?php
2
3 function wfSpecialUpload()
4 {
5 global $wgRequest;
6 $form = new UploadForm( $wgRequest );
7 $form->execute();
8 }
9
10 class UploadForm {
11 var $mUploadAffirm, $mUploadFile, $mUploadDescription, $mIgnoreWarning;
12 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
13 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
14 var $mOname, $mSessionKey;
15
16 function UploadForm( &$request ) {
17 $this->mUploadAffirm = $request->getVal( 'wpUploadAffirm' );
18 $this->mUploadFile = $request->getVal( 'wpUploadFile' );
19 $this->mUploadDescription = $request->getVal( 'wpUploadDescription');
20 $this->mIgnoreWarning = $request->getVal( 'wpIgnoreWarning');
21 $this->mUploadSaveName = $request->getVal( 'wpUploadSaveName');
22 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
23 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
24 $this->mUploadSize = $request->getVal( 'wpUploadSize');
25 $this->mUploadOldVersion = $request->getVal( 'wpUploadOldVersion');
26 $this->mUploadCopyStatus = $request->getVal( 'wpUploadCopyStatus');
27 $this->mUploadSource = $request->getVal( 'wpUploadSource');
28 $this->mReUpload = $request->getCheck( 'wpReUpload' );
29 $this->mAction = $request->getVal( 'action' );
30 $this->mUpload = $request->getCheck( 'wpUpload' );
31 $this->mSessionKey = $request->getVal( 'wpSessionKey' );
32
33 if ( ! $this->mUploadTempName ) {
34 $this->mUploadTempName = @$_FILES['wpUploadFile']['tmp_name'];
35 }
36 if ( ! $this->mUploadSize ) {
37 $this->mUploadSize = @$_FILES['wpUploadFile']['size'];
38 }
39 $this->mOname = $request->getGPCVal( $_FILES['wpUploadFile'], 'name', "" );
40
41 }
42
43 function execute() {
44 global $wgUser, $wgOut;
45 global $wgDisableUploads;
46
47 if ( $wgDisableUploads ) {
48 $wgOut->addWikiText( wfMsg( "uploaddisabled" ) );
49 return;
50 }
51 if ( ( 0 == $wgUser->getID() )
52 or $wgUser->isBlocked() ) {
53 $wgOut->errorpage( "uploadnologin", "uploadnologintext" );
54 return;
55 }
56 if ( wfReadOnly() ) {
57 $wgOut->readOnlyPage();
58 return;
59 }
60 if ( $this->mReUpload ) {
61 $this->unsaveUploadedFile();
62 $this->mainUploadForm( "" );
63 } else if ( "submit" == $this->mAction || $this->mUpload ) {
64 $this->processUpload();
65 } else {
66 $this->mainUploadForm( "" );
67 }
68 }
69
70
71 function processUpload()
72 {
73 global $wgUser, $wgOut, $wgLang;
74 global $wgUploadDirectory;
75 global $wgSavedFile, $wgUploadOldVersion;
76 global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
77 global $wgCheckFileExtensions, $wgStrictFileExtensions;
78 global $wgFileExtensions, $wgFileBlacklist, $wgUploadSizeWarning;
79
80 if ( $wgUseCopyrightUpload ) {
81 $this->mUploadAffirm = 1;
82 if ($wgCheckCopyrightUpload &&
83 (trim ( $this->mUploadCopyStatus ) == "" || trim ( $this->mUploadSource ) == "" )) {
84 $this->mUploadAffirm = 0;
85 }
86 }
87
88 if ( 1 != $this->mUploadAffirm ) {
89 $this->mainUploadForm( WfMsg( "noaffirmation" ) );
90 return;
91 }
92
93 if ( "" != $this->mOname ) {
94 $basename = strrchr( $this->mOname, "/" );
95 if ( false === $basename ) { $basename = $this->mOname; }
96 else ( $basename = substr( $basename, 1 ) );
97
98 $ext = strrchr( $basename, "." );
99 if ( false === $ext ) { $ext = ""; }
100 else { $ext = substr( $ext, 1 ); }
101
102 if ( "" == $ext ) { $xl = 0; } else { $xl = strlen( $ext ) + 1; }
103 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
104
105 if ( strlen( $partname ) < 3 ) {
106 $this->mainUploadForm( WfMsg( "minlength" ) );
107 return;
108 }
109 $nt = Title::newFromText( $basename );
110 if( !$nt ) {
111 return $this->uploadError( wfMsg( "illegalfilename", htmlspecialchars( $basename ) ) );
112 }
113 $nt->setNamespace( Namespace::getImage() );
114 $this->mUploadSaveName = $nt->getDBkey();
115
116 /* Don't allow users to override the blacklist */
117 if( $this->checkFileExtension( $ext, $wgFileBlacklist ) ||
118 ($wgStrictFileExtensions && !$this->checkFileExtension( $ext, $wgFileExtensions ) ) ) {
119 return $this->uploadError( wfMsg( "badfiletype", htmlspecialchars( $ext ) ) );
120 }
121
122 $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
123 if ( !$nt->userCanEdit() ) {
124 return $this->uploadError( wfMsg( "protectedpage" ) );
125 }
126
127 if ( ! $this->mIgnoreWarning ) {
128 $warning = '';
129 if( 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) {
130 $warning .= '<li>'.wfMsg( "badfilename", htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
131 }
132
133 if ( $wgCheckFileExtensions ) {
134 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
135 $warning .= '<li>'.wfMsg( "badfiletype", htmlspecialchars( $ext ) ).'</li>';
136 }
137 }
138 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
139 $warning .= '<li>'.wfMsg( "largefile" ).'</li>';
140 }
141 if ( $this->mUploadSize == 0 ) {
142 $warning .= '<li>'.wfMsg( "emptyfile" ).'</li>';
143 }
144 if( $nt->getArticleID() ) {
145 $sk = $wgUser->getSkin();
146 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
147 $dlink = $sk->makeKnownLink( $dname, $dname );
148 $warning .= '<li>'.wfMsg( "fileexists", $dlink ).'</li>';
149 }
150 if($warning != '') return $this->uploadWarning($warning);
151 }
152 }
153 if ( !is_null( $this->mUploadOldVersion ) ) {
154 $wgUploadOldVersion = $this->mUploadOldVersion;
155 }
156 wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
157 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
158
159 $sk = $wgUser->getSkin();
160 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
161 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
162 $dlink = $sk->makeKnownLink( $dname, $dname );
163
164 $wgOut->addHTML( "<h2>" . wfMsg( "successfulupload" ) . "</h2>\n" );
165 $text = wfMsg( "fileuploaded", $ilink, $dlink );
166 $wgOut->addHTML( "<p>{$text}\n" );
167 $wgOut->returnToMain( false );
168 }
169
170 function checkFileExtension( $ext, $list ) {
171 return in_array( strtolower( $ext ), $list );
172 }
173
174 function saveUploadedFile( $saveName, $tempName )
175 {
176 global $wgSavedFile, $wgUploadOldVersion;
177 global $wgUploadDirectory, $wgOut;
178
179 $dest = wfImageDir( $saveName );
180 $archive = wfImageArchiveDir( $saveName );
181 $wgSavedFile = "{$dest}/{$saveName}";
182
183 if ( is_file( $wgSavedFile ) ) {
184 $wgUploadOldVersion = gmdate( "YmdHis" ) . "!{$saveName}";
185
186 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
187 $wgOut->fileRenameError( $wgSavedFile,
188 "${archive}/{$wgUploadOldVersion}" );
189 return;
190 }
191 } else {
192 $wgUploadOldVersion = "";
193 }
194 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
195 $wgOut->fileCopyError( $tempName, $wgSavedFile );
196 }
197 chmod( $wgSavedFile, 0644 );
198 }
199
200 function unsaveUploadedFile()
201 {
202 global $wgUploadDirectory, $wgOut, $wgRequest;
203
204 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
205 $wgUploadOldVersion = $this->mUploadOldVersion;
206
207 if ( ! @unlink( $wgSavedFile ) ) {
208 $wgOut->fileDeleteError( $wgSavedFile );
209 return;
210 }
211 if ( "" != $wgUploadOldVersion ) {
212 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
213 $archive = "{$wgUploadDirectory}/archive/" . $hash{0} .
214 "/" . substr( $hash, 0, 2 );
215
216 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
217 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
218 $wgSavedFile );
219 }
220 }
221 }
222
223 function uploadError( $error )
224 {
225 global $wgOut;
226 $sub = wfMsg( "uploadwarning" );
227 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
228 $wgOut->addHTML( "<h4><font color=red>{$error}</font></h4>\n" );
229 }
230
231 function uploadWarning( $warning )
232 {
233 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
234 global $wgSavedFile, $wgUploadOldVersion;
235 global $wgUseCopyrightUpload;
236
237 # wgSavedFile is stored in the session not the form, for security
238 $this->mSessionKey = mt_rand( 0, 0x7fffffff );
239 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
240
241 $sub = wfMsg( "uploadwarning" );
242 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
243 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
244
245 $save = wfMsg( "savefile" );
246 $reupload = wfMsg( "reupload" );
247 $iw = wfMsg( "ignorewarning" );
248 $reup = wfMsg( "reuploaddesc" );
249 $titleObj = Title::makeTitle( NS_SPECIAL, "Upload" );
250 $action = $titleObj->escapeLocalURL( "action=submit" );
251
252 if ( $wgUseCopyrightUpload )
253 {
254 $copyright = "
255 <input type=hidden name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\">
256 <input type=hidden name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\">
257 ";
258 } else {
259 $copyright = "";
260 }
261
262 $wgOut->addHTML( "
263 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
264 action=\"{$action}\">
265 <input type=hidden name=\"wpUploadAffirm\" value=\"1\">
266 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\">
267 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\">
268 {$copyright}
269 <input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) . "\">
270 <input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) . "\">
271 <input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) . "\">
272 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\">
273 <input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) . "\">
274 <table border=0><tr>
275 <tr><td align=right>
276 <input tabindex=2 type=submit name=\"wpUpload\" value=\"{$save}\">
277 </td><td align=left>{$iw}</td></tr>
278 <tr><td align=right>
279 <input tabindex=2 type=submit name=\"wpReUpload\" value=\"{$reupload}\">
280 </td><td align=left>{$reup}</td></tr></table></form>\n" );
281 }
282
283 function mainUploadForm( $msg )
284 {
285 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
286 global $wgUseCopyrightUpload;
287
288 if ( "" != $msg ) {
289 $sub = wfMsg( "uploaderror" );
290 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
291 "<h4><font color=red>{$msg}</font></h4>\n" );
292 } else {
293 $sub = wfMsg( "uploadfile" );
294 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
295 }
296 $wgOut->addHTML( "<p>" . wfMsg( "uploadtext" ) );
297 $sk = $wgUser->getSkin();
298
299 $fn = wfMsg( "filename" );
300 $fd = wfMsg( "filedesc" );
301 $ulb = wfMsg( "uploadbtn" );
302
303 $clink = $sk->makeKnownLink( wfMsg( "copyrightpage" ),
304 wfMsg( "copyrightpagename" ) );
305 $ca = wfMsg( "affirmation", $clink );
306 $iw = wfMsg( "ignorewarning" );
307
308 $titleObj = Title::makeTitle( NS_SPECIAL, "Upload" );
309 $action = $titleObj->escapeLocalURL();
310
311 $source = "
312 <td align=right>
313 <input tabindex=3 type=checkbox name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\">
314 </td><td align=left><label for=\"wpUploadAffirm\">{$ca}</label></td>
315 " ;
316 if ( $wgUseCopyrightUpload )
317 {
318 $source = "
319 <td align=right nowrap>" . wfMsg ( "filestatus" ) . ":</td>
320 <td><input tabindex=3 type=text name=\"wpUploadCopyStatus\" value=\"" .
321 htmlspecialchars($this->mUploadCopyStatus). "\" size=40></td>
322 </tr><tr>
323 <td align=right>". wfMsg ( "filesource" ) . ":</td>
324 <td><input tabindex=4 type=text name=\"wpUploadSource\" value=\"" .
325 htmlspecialchars($this->mUploadSource). "\" size=40></td>
326 " ;
327 }
328
329 $wgOut->addHTML( "
330 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
331 action=\"{$action}\">
332 <table border=0><tr>
333 <td align=right>{$fn}:</td><td align=left>
334 <input tabindex=1 type=file name=\"wpUploadFile\" value=\""
335 . htmlspecialchars( $this->mUploadFile ) . "\" size=40>
336 </td></tr><tr>
337 <td align=right>{$fd}:</td><td align=left>
338 <input tabindex=2 type=text name=\"wpUploadDescription\" value=\""
339 . htmlspecialchars( $this->mUploadDescription ) . "\" size=40>
340 </td></tr><tr>
341 {$source}
342 </tr>
343 <tr><td>&nbsp;</td><td align=left>
344 <input tabindex=5 type=submit name=\"wpUpload\" value=\"{$ulb}\">
345 </td></tr></table></form>\n" );
346 }
347 }
348 ?>