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 var $mUploadAffirm, $mUploadFile, $mUploadDescription, $mIgnoreWarning;
29 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
30 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
31 var $mOname, $mSessionKey;
32
33 /**
34 * Constructor : initialise object
35 * Get data POSTed through the form and assign them to the object
36 * @param $request Data posted.
37 */
38 function UploadForm( &$request ) {
39 $this->mUploadAffirm = $request->getVal( 'wpUploadAffirm' );
40 $this->mUploadFile = $request->getVal( 'wpUploadFile' );
41 $this->mUploadDescription = $request->getVal( 'wpUploadDescription');
42 $this->mIgnoreWarning = $request->getVal( 'wpIgnoreWarning');
43 $this->mUploadSaveName = $request->getVal( 'wpUploadSaveName');
44 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
45 $this->mUploadTempName = $request->getVal( 'wpUploadTempName');
46 $this->mUploadSize = $request->getVal( 'wpUploadSize');
47 $this->mUploadOldVersion = $request->getVal( 'wpUploadOldVersion');
48 $this->mUploadCopyStatus = $request->getVal( 'wpUploadCopyStatus');
49 $this->mUploadSource = $request->getVal( 'wpUploadSource');
50 $this->mReUpload = $request->getCheck( 'wpReUpload' );
51 $this->mAction = $request->getVal( 'action' );
52 $this->mUpload = $request->getCheck( 'wpUpload' );
53 $this->mSessionKey = $request->getVal( 'wpSessionKey' );
54
55 /** Generate a temporary name if we don't have one yet */
56 if ( ! $this->mUploadTempName ) {
57 $this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
58 }
59
60 /** Get size of file */
61 if ( ! $this->mUploadSize ) {
62 $this->mUploadSize = $request->getFileSize( 'wpUploadFile' );
63 }
64 $this->mOname = $request->getFileName( 'wpUploadFile' );
65 }
66
67 /**
68 * Start doing stuff
69 * @access public
70 */
71 function execute() {
72 global $wgUser, $wgOut;
73 global $wgDisableUploads;
74
75 /** Show an error message if file upload is disabled */
76 if ( $wgDisableUploads ) {
77 $wgOut->addWikiText( wfMsg( 'uploaddisabled' ) );
78 return;
79 }
80
81 /** Various rights checks */
82 if ( ( $wgUser->getID() == 0 )
83 OR $wgUser->isBlocked() ) {
84 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
85 return;
86 }
87 if ( wfReadOnly() ) {
88 $wgOut->readOnlyPage();
89 return;
90 }
91
92 if ( $this->mReUpload ) {
93 $this->unsaveUploadedFile();
94 $this->mainUploadForm( '' );
95 } else if ( 'submit' == $this->mAction || $this->mUpload ) {
96 $this->processUpload();
97 } else {
98 $this->mainUploadForm( '' );
99 }
100 }
101
102 /**
103 * Really do the upload
104 * Checks are made in SpecialUpload::execute()
105 * @access private
106 */
107 function processUpload() {
108 global $wgUser, $wgOut, $wgLang;
109 global $wgUploadDirectory;
110 global $wgSavedFile, $wgUploadOldVersion;
111 global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
112 global $wgCheckFileExtensions, $wgStrictFileExtensions;
113 global $wgFileExtensions, $wgFileBlacklist, $wgUploadSizeWarning;
114
115 /** When using detailed copyright, if user filled field, assume he
116 * confirmed the upload */
117 if ( $wgUseCopyrightUpload ) {
118 $this->mUploadAffirm = 1;
119 if ($wgCheckCopyrightUpload &&
120 (trim ( $this->mUploadCopyStatus ) == '' || trim ( $this->mUploadSource ) == '' )) {
121 $this->mUploadAffirm = 0;
122 }
123 }
124
125 /** User need to confirm his upload */
126 if ( $this->mUploadAffirm != 1) {
127 $this->mainUploadForm( WfMsg( 'noaffirmation' ) );
128 return;
129 }
130
131 if ( $this->mOname != '' ) {
132 $basename = strrchr( $this->mOname, '/' );
133
134 if ( false === $basename ) { $basename = $this->mOname; }
135 else ( $basename = substr( $basename, 1 ) );
136
137
138 $ext = strrchr( $basename, '.' );
139 if ( false === $ext ) { $ext = ''; }
140 else { $ext = substr( $ext, 1 ); }
141
142 if ( '' == $ext ) { $xl = 0; } else { $xl = strlen( $ext ) + 1; }
143 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
144
145 if ( strlen( $partname ) < 3 ) {
146 $this->mainUploadForm( WfMsg( 'minlength' ) );
147 return;
148 }
149
150 $changed_name = false;
151 $bn = preg_replace ( "/[^".Title::legalChars()."]/", '-', $basename );
152 if ( 0 != strcmp( $bn, $basename ) )
153 {
154 $changed_name = true;
155 $basename = $bn;
156 }
157
158 $nt = Title::newFromText( $basename );
159 if( !$nt ) {
160 return $this->uploadError( wfMsg( 'illegalfilename', htmlspecialchars( $basename ) ) );
161 }
162 $nt->setNamespace( Namespace::getImage() );
163 $this->mUploadSaveName = $nt->getDBkey();
164
165 /* Don't allow users to override the blacklist */
166 if( $this->checkFileExtension( $ext, $wgFileBlacklist ) ||
167 ($wgStrictFileExtensions && !$this->checkFileExtension( $ext, $wgFileExtensions ) ) ) {
168 return $this->uploadError( wfMsg( 'badfiletype', htmlspecialchars( $ext ) ) );
169 }
170
171 $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
172 if ( !$nt->userCanEdit() ) {
173 return $this->uploadError( wfMsg( 'protectedpage' ) );
174 }
175
176 if ( ! $this->mIgnoreWarning ) {
177 $warning = '';
178 if( $changed_name || 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) {
179 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
180 }
181
182 if ( $wgCheckFileExtensions ) {
183 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
184 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $ext ) ).'</li>';
185 }
186 }
187 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
188 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
189 }
190 if ( $this->mUploadSize == 0 ) {
191 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
192 }
193 if( $nt->getArticleID() ) {
194 $sk = $wgUser->getSkin();
195 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
196 $dlink = $sk->makeKnownLink( $dname, $dname );
197 $warning .= '<li>'.wfMsg( 'fileexists', $dlink ).'</li>';
198 }
199 if($warning != '') return $this->uploadWarning($warning);
200 }
201 } else {
202 return $this->uploadError('<li>'.wfMsg( 'emptyfile' ).'</li>');
203
204 }
205 if ( !is_null( $this->mUploadOldVersion ) ) {
206 $wgUploadOldVersion = $this->mUploadOldVersion;
207 }
208 wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
209 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
210
211 $sk = $wgUser->getSkin();
212 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
213 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
214 $dlink = $sk->makeKnownLink( $dname, $dname );
215
216 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
217 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
218 $wgOut->addHTML( "<p>{$text}\n" );
219 $wgOut->returnToMain( false );
220 }
221
222 function checkFileExtension( $ext, $list ) {
223 return in_array( strtolower( $ext ), $list );
224 }
225
226 function saveUploadedFile( $saveName, $tempName ) {
227 global $wgSavedFile, $wgUploadOldVersion;
228 global $wgUploadDirectory, $wgOut;
229
230 $dest = wfImageDir( $saveName );
231 $archive = wfImageArchiveDir( $saveName );
232 $wgSavedFile = "{$dest}/{$saveName}";
233
234 if ( is_file( $wgSavedFile ) ) {
235 $wgUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
236
237 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
238 $wgOut->fileRenameError( $wgSavedFile,
239 "${archive}/{$wgUploadOldVersion}" );
240 return;
241 }
242 } else {
243 $wgUploadOldVersion = '';
244 }
245 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
246 $wgOut->fileCopyError( $tempName, $wgSavedFile );
247 }
248 chmod( $wgSavedFile, 0644 );
249 }
250
251 function unsaveUploadedFile() {
252 global $wgUploadDirectory, $wgOut, $wgRequest;
253
254 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
255 $wgUploadOldVersion = $this->mUploadOldVersion;
256
257 if ( ! @unlink( $wgSavedFile ) ) {
258 $wgOut->fileDeleteError( $wgSavedFile );
259 return;
260 }
261 if ( '' != $wgUploadOldVersion ) {
262 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
263 $archive = $wgUploadDirectory.'/archive/' . $hash{0} .
264 '/' . substr( $hash, 0, 2 );
265
266 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
267 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
268 $wgSavedFile );
269 }
270 }
271 }
272
273 function uploadError( $error ) {
274 global $wgOut;
275 $sub = wfMsg( 'uploadwarning' );
276 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
277 $wgOut->addHTML( "<h4 style='error'>{$error}</h4>\n" );
278 }
279
280 function uploadWarning( $warning ) {
281 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
282 global $wgSavedFile, $wgUploadOldVersion;
283 global $wgUseCopyrightUpload;
284
285 # wgSavedFile is stored in the session not the form, for security
286 $this->mSessionKey = mt_rand( 0, 0x7fffffff );
287 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
288
289 $sub = wfMsg( 'uploadwarning' );
290 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
291 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
292
293 $save = wfMsg( 'savefile' );
294 $reupload = wfMsg( 'reupload' );
295 $iw = wfMsg( 'ignorewarning' );
296 $reup = wfMsg( 'reuploaddesc' );
297 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
298 $action = $titleObj->escapeLocalURL( 'action=submit' );
299
300 if ( $wgUseCopyrightUpload )
301 {
302 $copyright = "
303 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\" />
304 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\" />
305 ";
306 } else {
307 $copyright = "";
308 }
309
310 $wgOut->addHTML( "
311 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
312 action=\"{$action}\">
313 <input type=hidden name=\"wpUploadAffirm\" value=\"1\" />
314 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\" />
315 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
316 {$copyright}
317 <input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) . "\" />
318 <input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) . "\" />
319 <input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) . "\" />
320 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
321 <input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) . "\" />
322 <table border='0'><tr>
323 <tr><td align='right'>
324 <input tabindex='2' type='submit' name=\"wpUpload\" value=\"{$save}\" />
325 </td><td align='left'>{$iw}</td></tr>
326 <tr><td align='right'>
327 <input tabindex='2' type='submit' name=\"wpReUpload\" value=\"{$reupload}\" />
328 </td><td align='left'>{$reup}</td></tr></table></form>\n" );
329 }
330
331 function mainUploadForm( $msg ) {
332 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
333 global $wgUseCopyrightUpload;
334
335 if ( '' != $msg ) {
336 $sub = wfMsg( 'uploaderror' );
337 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
338 "<h4 style='error'>{$msg}</h4>\n" );
339 } else {
340 $sub = wfMsg( 'uploadfile' );
341 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
342 }
343 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
344 $sk = $wgUser->getSkin();
345
346 $fn = wfMsg( 'filename' );
347 $fd = wfMsg( 'filedesc' );
348 $ulb = wfMsg( 'uploadbtn' );
349
350 $clink = $sk->makeKnownLink( wfMsg( 'copyrightpage' ),
351 wfMsg( 'copyrightpagename' ) );
352 $ca = wfMsg( 'affirmation', $clink );
353 $iw = wfMsg( 'ignorewarning' );
354
355 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
356 $action = $titleObj->escapeLocalURL();
357
358 $source = "
359 <td align='right'>
360 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
361 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
362 " ;
363 if ( $wgUseCopyrightUpload )
364 {
365 $source = "
366 <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
367 <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
368 htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
369 </tr><tr>
370 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
371 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
372 htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
373 " ;
374 }
375
376 $wgOut->addHTML( "
377 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
378 action=\"{$action}\">
379 <table border='0'><tr>
380 <td align='right'>{$fn}:</td><td align='left'>
381 <input tabindex='1' type='file' name=\"wpUploadFile\" value=\""
382 . htmlspecialchars( $this->mUploadFile ) . "\" size='40' />
383 </td></tr><tr>
384 <td align='right'>{$fd}:</td><td align='left'>
385 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
386 . htmlspecialchars( $this->mUploadDescription ) . "\" size='40' />
387 </td></tr><tr>
388 {$source}
389 </tr>
390 <tr><td></td><td align='left'>
391 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
392 </td></tr></table></form>\n" );
393 }
394 }
395 ?>