Fix http://bugzilla.wikipedia.org/show_bug.cgi?id=538.
[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;
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 $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
176 if ( !$nt->userCanEdit() ) {
177 return $this->uploadError( wfMsg( 'protectedpage' ) );
178 }
179
180 if ( ! $this->mIgnoreWarning ) {
181 $warning = '';
182 if( $changed_name || 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) {
183 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
184 }
185
186 if ( $wgCheckFileExtensions ) {
187 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
188 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $ext ) ).'</li>';
189 }
190 }
191 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
192 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
193 }
194 if ( $this->mUploadSize == 0 ) {
195 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
196 }
197 if( $nt->getArticleID() ) {
198 $sk = $wgUser->getSkin();
199 $dname = $wgLang->getNsText( Namespace::getImage() ) .':'.$this->mUploadSaveName;
200 $dlink = $sk->makeKnownLink( $dname, $dname );
201 $warning .= '<li>'.wfMsg( 'fileexists', $dlink ).'</li>';
202 }
203 if($warning != '') return $this->uploadWarning($warning);
204 }
205 } elseif(!isset($this->mUploadSaveName)) {
206 // no filename given even when reuploading
207 return $this->uploadError('<li>'.wfMsg( 'emptyfile' ).'</li>');
208
209 }
210 if ( !is_null( $this->mUploadOldVersion ) ) {
211 $wgUploadOldVersion = $this->mUploadOldVersion;
212 }
213 wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
214 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
215
216 $sk = $wgUser->getSkin();
217 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
218 $dname = $wgLang->getNsText( Namespace::getImage() ) . ':'.$this->mUploadSaveName;
219 $dlink = $sk->makeKnownLink( $dname, $dname );
220
221 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
222 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
223 $wgOut->addHTML( '<p>'.$text."\n" );
224 $wgOut->returnToMain( false );
225 }
226
227 function checkFileExtension( $ext, $list ) {
228 return in_array( strtolower( $ext ), $list );
229 }
230
231 function saveUploadedFile( $saveName, $tempName ) {
232 global $wgSavedFile, $wgUploadOldVersion;
233 global $wgUploadDirectory, $wgOut;
234
235 $dest = wfImageDir( $saveName );
236 $archive = wfImageArchiveDir( $saveName );
237 $wgSavedFile = "{$dest}/{$saveName}";
238
239 if ( is_file( $wgSavedFile ) ) {
240 $wgUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
241
242 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
243 $wgOut->fileRenameError( $wgSavedFile,
244 "${archive}/{$wgUploadOldVersion}" );
245 return;
246 }
247 } else {
248 $wgUploadOldVersion = '';
249 }
250 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
251 $wgOut->fileCopyError( $tempName, $wgSavedFile );
252 }
253 chmod( $wgSavedFile, 0644 );
254 }
255
256 function unsaveUploadedFile() {
257 global $wgUploadDirectory, $wgOut, $wgRequest;
258
259 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
260 $wgUploadOldVersion = $this->mUploadOldVersion;
261
262 if ( ! @unlink( $wgSavedFile ) ) {
263 $wgOut->fileDeleteError( $wgSavedFile );
264 return;
265 }
266 if ( '' != $wgUploadOldVersion ) {
267 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
268 $archive = $wgUploadDirectory.'/archive/' . $hash{0} .
269 '/' . substr( $hash, 0, 2 );
270
271 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
272 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
273 $wgSavedFile );
274 }
275 }
276 }
277
278 function uploadError( $error ) {
279 global $wgOut;
280 $sub = wfMsg( 'uploadwarning' );
281 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
282 $wgOut->addHTML( "<h4 style='error'>{$error}</h4>\n" );
283 }
284
285 function uploadWarning( $warning ) {
286 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
287 global $wgSavedFile, $wgUploadOldVersion;
288 global $wgUseCopyrightUpload;
289
290 # wgSavedFile is stored in the session not the form, for security
291 $this->mSessionKey = mt_rand( 0, 0x7fffffff );
292 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
293
294 $sub = wfMsg( 'uploadwarning' );
295 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
296 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
297
298 $save = wfMsg( 'savefile' );
299 $reupload = wfMsg( 'reupload' );
300 $iw = wfMsg( 'ignorewarning' );
301 $reup = wfMsg( 'reuploaddesc' );
302 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
303 $action = $titleObj->escapeLocalURL( 'action=submit' );
304
305 if ( $wgUseCopyrightUpload )
306 {
307 $copyright = "
308 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\" />
309 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\" />
310 ";
311 } else {
312 $copyright = "";
313 }
314
315 $wgOut->addHTML( "
316 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
317 action=\"{$action}\">
318 <input type=hidden name=\"wpUploadAffirm\" value=\"1\" />
319 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\" />
320 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
321 {$copyright}
322 <input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) . "\" />
323 <input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) . "\" />
324 <input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) . "\" />
325 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
326 <input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) . "\" />
327 <table border='0'><tr>
328 <tr><td align='right'>
329 <input tabindex='2' type='submit' name=\"wpUpload\" value=\"{$save}\" />
330 </td><td align='left'>{$iw}</td></tr>
331 <tr><td align='right'>
332 <input tabindex='2' type='submit' name=\"wpReUpload\" value=\"{$reupload}\" />
333 </td><td align='left'>{$reup}</td></tr></table></form>\n" );
334 }
335
336 function mainUploadForm( $msg ) {
337 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
338 global $wgUseCopyrightUpload;
339
340 if ( '' != $msg ) {
341 $sub = wfMsg( 'uploaderror' );
342 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
343 "<h4 style='error'>{$msg}</h4>\n" );
344 } else {
345 $sub = wfMsg( 'uploadfile' );
346 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
347 }
348 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
349 $sk = $wgUser->getSkin();
350
351 $fn = wfMsg( 'filename' );
352 $fd = wfMsg( 'filedesc' );
353 $ulb = wfMsg( 'uploadbtn' );
354
355 $clink = $sk->makeKnownLink( wfMsg( 'copyrightpage' ),
356 wfMsg( 'copyrightpagename' ) );
357 $ca = wfMsg( 'affirmation', $clink );
358 $iw = wfMsg( 'ignorewarning' );
359
360 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
361 $action = $titleObj->escapeLocalURL();
362
363 $source = "
364 <td align='right'>
365 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
366 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
367 " ;
368 if ( $wgUseCopyrightUpload )
369 {
370 $source = "
371 <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
372 <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
373 htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
374 </tr><tr>
375 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
376 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
377 htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
378 " ;
379 }
380
381 $wgOut->addHTML( "
382 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
383 action=\"{$action}\">
384 <table border='0'><tr>
385 <td align='right'>{$fn}:</td><td align='left'>
386 <input tabindex='1' type='file' name=\"wpUploadFile\" value=\""
387 . htmlspecialchars( $this->mUploadFile ) . "\" size='40' />
388 </td></tr><tr>
389 <td align='right'>{$fd}:</td><td align='left'>
390 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
391 . htmlspecialchars( $this->mUploadDescription ) . "\" size='40' />
392 </td></tr><tr>
393 {$source}
394 </tr>
395 <tr><td></td><td align='left'>
396 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
397 </td></tr></table></form>\n" );
398 }
399 }
400 ?>