Third batch of files modified to replace selected wgLang with wgContLang
[lhc/web/wiklou.git] / includes / SpecialUpload.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'Image.php' );
12
13 /**
14 * Entry point
15 */
16 function wfSpecialUpload() {
17 global $wgRequest;
18 $form = new UploadForm( $wgRequest );
19 $form->execute();
20 }
21
22 /**
23 *
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class UploadForm {
28 /**#@+
29 * @access private
30 */
31 var $mUploadAffirm, $mUploadFile, $mUploadDescription, $mIgnoreWarning;
32 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
33 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
34 var $mOname, $mSessionKey;
35 /**#@- */
36
37 /**
38 * Constructor : initialise object
39 * Get data POSTed through the form and assign them to the object
40 * @param $request Data posted.
41 */
42 function UploadForm( &$request ) {
43 $this->mUploadAffirm = $request->getVal( 'wpUploadAffirm' );
44 $this->mUploadFile = $request->getVal( 'wpUploadFile' );
45 $this->mUploadDescription = $request->getVal( 'wpUploadDescription');
46 $this->mIgnoreWarning = $request->getVal( 'wpIgnoreWarning');
47 $this->mUploadSaveName = $request->getVal( 'wpUploadSaveName');
48 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
49 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
50 $this->mUploadSize = $request->getVal( 'wpUploadSize');
51 $this->mUploadOldVersion = $request->getVal( 'wpUploadOldVersion');
52 $this->mUploadCopyStatus = $request->getVal( 'wpUploadCopyStatus');
53 $this->mUploadSource = $request->getVal( 'wpUploadSource');
54 $this->mReUpload = $request->getCheck( 'wpReUpload' );
55 $this->mAction = $request->getVal( 'action' );
56 $this->mUpload = $request->getCheck( 'wpUpload' );
57 $this->mSessionKey = $request->getVal( 'wpSessionKey' );
58
59 /** Generate a temporary name if we don't have one yet */
60 if ( ! $this->mUploadTempName ) {
61 $this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
62 }
63
64 /** Get size of file */
65 if ( ! $this->mUploadSize ) {
66 $this->mUploadSize = $request->getFileSize( 'wpUploadFile' );
67 }
68 $this->mOname = $request->getFileName( 'wpUploadFile' );
69 }
70
71 /**
72 * Start doing stuff
73 * @access public
74 */
75 function execute() {
76 global $wgUser, $wgOut;
77 global $wgDisableUploads;
78
79 /** Show an error message if file upload is disabled */
80 if ( $wgDisableUploads ) {
81 $wgOut->addWikiText( wfMsg( 'uploaddisabled' ) );
82 return;
83 }
84
85 /** Various rights checks */
86 if ( ( $wgUser->getID() == 0 )
87 OR $wgUser->isBlocked() ) {
88 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
89 return;
90 }
91 if ( wfReadOnly() ) {
92 $wgOut->readOnlyPage();
93 return;
94 }
95
96 if ( $this->mReUpload ) {
97 $this->unsaveUploadedFile();
98 $this->mainUploadForm( '' );
99 } else if ( 'submit' == $this->mAction || $this->mUpload ) {
100 $this->processUpload();
101 } else {
102 $this->mainUploadForm( '' );
103 }
104 }
105
106 /**
107 * Really do the upload
108 * Checks are made in SpecialUpload::execute()
109 * @access private
110 */
111 function processUpload() {
112 global $wgUser, $wgOut, $wgLang, $wgContLang;
113 global $wgUploadDirectory;
114 global $wgSavedFile, $wgUploadOldVersion;
115 global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
116 global $wgCheckFileExtensions, $wgStrictFileExtensions;
117 global $wgFileExtensions, $wgFileBlacklist, $wgUploadSizeWarning;
118
119 /** When using detailed copyright, if user filled field, assume he
120 * confirmed the upload */
121 if ( $wgUseCopyrightUpload ) {
122 $this->mUploadAffirm = 1;
123 if ($wgCheckCopyrightUpload &&
124 (trim ( $this->mUploadCopyStatus ) == '' || trim ( $this->mUploadSource ) == '' )) {
125 $this->mUploadAffirm = 0;
126 }
127 }
128
129 /** User need to confirm his upload */
130 if ( $this->mUploadAffirm != 1) {
131 $this->mainUploadForm( WfMsg( 'noaffirmation' ) );
132 return;
133 }
134
135 if ( $this->mOname != '' ) {
136 $basename = strrchr( $this->mOname, '/' );
137
138 if ( false === $basename ) { $basename = $this->mOname; }
139 else ( $basename = substr( $basename, 1 ) );
140
141
142 $ext = strrchr( $basename, '.' );
143 if ( false === $ext ) { $ext = ''; }
144 else { $ext = substr( $ext, 1 ); }
145
146 if ( '' == $ext ) { $xl = 0; } else { $xl = strlen( $ext ) + 1; }
147 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
148
149 if ( strlen( $partname ) < 3 ) {
150 $this->mainUploadForm( WfMsg( 'minlength' ) );
151 return;
152 }
153
154 $changed_name = false;
155 $bn = preg_replace ( "/[^".Title::legalChars()."]/", '-', $basename );
156 if ( 0 != strcmp( $bn, $basename ) )
157 {
158 $changed_name = true;
159 $basename = $bn;
160 }
161
162 $nt = Title::newFromText( $basename );
163 if( !$nt ) {
164 return $this->uploadError( wfMsg( 'illegalfilename', htmlspecialchars( $basename ) ) );
165 }
166 $nt->setNamespace( Namespace::getImage() );
167 $this->mUploadSaveName = $nt->getDBkey();
168
169 /* Don't allow users to override the blacklist */
170 if( $this->checkFileExtension( $ext, $wgFileBlacklist ) ||
171 ($wgStrictFileExtensions && !$this->checkFileExtension( $ext, $wgFileExtensions ) ) ) {
172 return $this->uploadError( wfMsg( 'badfiletype', htmlspecialchars( $ext ) ) );
173 }
174
175 if( !$this->verify( $this->mUploadTempName, $ext ) ) {
176 return $this->uploadError( wfMsg( 'uploadcorrupt' ) );
177 }
178
179 $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
180 if ( !$nt->userCanEdit() ) {
181 return $this->uploadError( wfMsg( 'protectedpage' ) );
182 }
183
184 if ( ! $this->mIgnoreWarning ) {
185 $warning = '';
186 if( $changed_name || 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) {
187 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
188 }
189
190 if ( $wgCheckFileExtensions ) {
191 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
192 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $ext ) ).'</li>';
193 }
194 }
195 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
196 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
197 }
198 if ( $this->mUploadSize == 0 ) {
199 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
200 }
201 if( $nt->getArticleID() ) {
202 $sk = $wgUser->getSkin();
203 $dname = $wgContLang->getNsText( Namespace::getImage() ) .':'.$this->mUploadSaveName;
204 $dlink = $sk->makeKnownLink( $dname, $dname );
205 $warning .= '<li>'.wfMsg( 'fileexists', $dlink ).'</li>';
206 }
207 if($warning != '') return $this->uploadWarning($warning);
208 }
209 } elseif(!isset($this->mUploadSaveName)) {
210 // no filename given even when reuploading
211 return $this->uploadError('<li>'.wfMsg( 'emptyfile' ).'</li>');
212
213 }
214 if ( !is_null( $this->mUploadOldVersion ) ) {
215 $wgUploadOldVersion = $this->mUploadOldVersion;
216 }
217 wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
218 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
219
220 $sk = $wgUser->getSkin();
221 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
222 $dname = $wgContLang->getNsText( Namespace::getImage() ) . ':'.$this->mUploadSaveName;
223 $dlink = $sk->makeKnownLink( $dname, $dname );
224
225 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
226 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
227 $wgOut->addHTML( '<p>'.$text."\n" );
228 $wgOut->returnToMain( false );
229 }
230
231 function checkFileExtension( $ext, $list ) {
232 return in_array( strtolower( $ext ), $list );
233 }
234
235 function saveUploadedFile( $saveName, $tempName ) {
236 global $wgSavedFile, $wgUploadOldVersion;
237 global $wgUploadDirectory, $wgOut;
238
239 $dest = wfImageDir( $saveName );
240 $archive = wfImageArchiveDir( $saveName );
241 $wgSavedFile = "{$dest}/{$saveName}";
242
243 if ( is_file( $wgSavedFile ) ) {
244 $wgUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
245
246 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
247 $wgOut->fileRenameError( $wgSavedFile,
248 "${archive}/{$wgUploadOldVersion}" );
249 return;
250 }
251 } else {
252 $wgUploadOldVersion = '';
253 }
254 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
255 $wgOut->fileCopyError( $tempName, $wgSavedFile );
256 }
257 chmod( $wgSavedFile, 0644 );
258 }
259
260 function unsaveUploadedFile() {
261 global $wgUploadDirectory, $wgOut, $wgRequest;
262
263 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
264 $wgUploadOldVersion = $this->mUploadOldVersion;
265
266 if ( ! @unlink( $wgSavedFile ) ) {
267 $wgOut->fileDeleteError( $wgSavedFile );
268 return;
269 }
270 if ( '' != $wgUploadOldVersion ) {
271 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
272 $archive = $wgUploadDirectory.'/archive/' . $hash{0} .
273 '/' . substr( $hash, 0, 2 );
274
275 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
276 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
277 $wgSavedFile );
278 }
279 }
280 }
281
282 function uploadError( $error ) {
283 global $wgOut;
284 $sub = wfMsg( 'uploadwarning' );
285 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
286 $wgOut->addHTML( "<h4 style='error'>{$error}</h4>\n" );
287 }
288
289 function uploadWarning( $warning ) {
290 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
291 global $wgSavedFile, $wgUploadOldVersion;
292 global $wgUseCopyrightUpload;
293
294 # wgSavedFile is stored in the session not the form, for security
295 $this->mSessionKey = mt_rand( 0, 0x7fffffff );
296 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
297
298 $sub = wfMsg( 'uploadwarning' );
299 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
300 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
301
302 $save = wfMsg( 'savefile' );
303 $reupload = wfMsg( 'reupload' );
304 $iw = wfMsg( 'ignorewarning' );
305 $reup = wfMsg( 'reuploaddesc' );
306 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
307 $action = $titleObj->escapeLocalURL( 'action=submit' );
308
309 if ( $wgUseCopyrightUpload )
310 {
311 $copyright = "
312 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\" />
313 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\" />
314 ";
315 } else {
316 $copyright = "";
317 }
318
319 $wgOut->addHTML( "
320 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
321 action=\"{$action}\">
322 <input type=hidden name=\"wpUploadAffirm\" value=\"1\" />
323 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\" />
324 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
325 {$copyright}
326 <input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) . "\" />
327 <input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) . "\" />
328 <input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) . "\" />
329 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
330 <input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) . "\" />
331 <table border='0'><tr>
332 <tr><td align='right'>
333 <input tabindex='2' type='submit' name=\"wpUpload\" value=\"{$save}\" />
334 </td><td align='left'>{$iw}</td></tr>
335 <tr><td align='right'>
336 <input tabindex='2' type='submit' name=\"wpReUpload\" value=\"{$reupload}\" />
337 </td><td align='left'>{$reup}</td></tr></table></form>\n" );
338 }
339
340 function mainUploadForm( $msg ) {
341 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
342 global $wgUseCopyrightUpload;
343
344 if ( '' != $msg ) {
345 $sub = wfMsg( 'uploaderror' );
346 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
347 "<h4 style='error'>{$msg}</h4>\n" );
348 } else {
349 $sub = wfMsg( 'uploadfile' );
350 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
351 }
352 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
353 $sk = $wgUser->getSkin();
354
355 $fn = wfMsg( 'filename' );
356 $fd = wfMsg( 'filedesc' );
357 $ulb = wfMsg( 'uploadbtn' );
358
359 $clink = $sk->makeKnownLink( wfMsg( 'copyrightpage' ),
360 wfMsg( 'copyrightpagename' ) );
361 $ca = wfMsg( 'affirmation', $clink );
362 $iw = wfMsg( 'ignorewarning' );
363
364 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
365 $action = $titleObj->escapeLocalURL();
366
367 $source = "
368 <td align='right'>
369 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
370 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
371 " ;
372 if ( $wgUseCopyrightUpload )
373 {
374 $source = "
375 <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
376 <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
377 htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
378 </tr><tr>
379 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
380 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
381 htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
382 " ;
383 }
384
385 $wgOut->addHTML( "
386 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
387 action=\"{$action}\">
388 <table border='0'><tr>
389 <td align='right'>{$fn}:</td><td align='left'>
390 <input tabindex='1' type='file' name=\"wpUploadFile\" value=\""
391 . htmlspecialchars( $this->mUploadFile ) . "\" size='40' />
392 </td></tr><tr>
393 <td align='right'>{$fd}:</td><td align='left'>
394 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
395 . htmlspecialchars( $this->mUploadDescription ) . "\" size='40' />
396 </td></tr><tr>
397 {$source}
398 </tr>
399 <tr><td></td><td align='left'>
400 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
401 </td></tr></table></form>\n" );
402 }
403
404 function verify( $tmpfile, $extension ) {
405 $fname = 'SpecialUpload::verify';
406 $mergeExtensions = array(
407 'jpg' => 'jpeg',
408 'tif' => 'tiff' );
409 $extensionTypes = array(
410 # See http://www.php.net/getimagesize
411 1 => 'gif',
412 2 => 'jpeg',
413 3 => 'png',
414 4 => 'swf',
415 5 => 'psd',
416 6 => 'bmp',
417 7 => 'tiff',
418 8 => 'tiff',
419 9 => 'jpc',
420 10 => 'jp2',
421 11 => 'jpx',
422 12 => 'jb2',
423 13 => 'swc',
424 14 => 'iff',
425 15 => 'wbmp',
426 16 => 'xbm' );
427
428 $extension = strtolower( $extension );
429 if( isset( $mergeExtensions[$extension] ) ) {
430 $extension = $mergeExtensions[$extension];
431 }
432 wfDebug( "$fname: Testing file '$tmpfile' with given extension '$extension'\n" );
433
434 if( !in_array( $extension, $extensionTypes ) ) {
435 # Not a recognized image type. We don't know how to verify these.
436 # They're allowed by policy or they wouldn't get this far, so we'll
437 # let them slide for now.
438 wfDebug( "$fname: Unknown extension; passing.\n" );
439 return true;
440 }
441
442 $data = @getimagesize( $tmpfile );
443 if( false === $data ) {
444 # Didn't recognize the image type.
445 # Either the image is corrupt or someone's slipping us some
446 # bogus data such as HTML+JavaScript trying to take advantage
447 # of an Internet Explorer security flaw.
448 wfDebug( "$fname: getimagesize() doesn't recognize the file; rejecting.\n" );
449 return false;
450 }
451
452 $imageType = $data[2];
453 if( !isset( $extensionTypes[$imageType] ) ) {
454 # Now we're kind of confused. Perhaps new image types added
455 # to PHP's support that we don't know about.
456 # We'll let these slide for now.
457 wfDebug( "$fname: getimagesize() knows the file, but we don't recognize the type; passing.\n" );
458 return true;
459 }
460
461 $ext = strtolower( $extension );
462 if( $extension != $extensionTypes[$imageType] ) {
463 # The given filename extension doesn't match the
464 # file type. Probably just a mistake, but it's a stupid
465 # one and we shouldn't let it pass. KILL THEM!
466 wfDebug( "$fname: file extension does not match recognized type; rejecting.\n" );
467 return false;
468 }
469
470 wfDebug( "$fname: all clear; passing.\n" );
471 return true;
472 }
473 }
474 ?>