comments / single quotes.
[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 if ( $wgUseCopyrightUpload ) {
116 $this->mUploadAffirm = 1;
117 if ($wgCheckCopyrightUpload &&
118 (trim ( $this->mUploadCopyStatus ) == '' || trim ( $this->mUploadSource ) == '' )) {
119 $this->mUploadAffirm = 0;
120 }
121 }
122
123 if ( $this->mUploadAffirm != 1) {
124 $this->mainUploadForm( WfMsg( 'noaffirmation' ) );
125 return;
126 }
127
128 if ( '' != $this->mOname ) {
129 $basename = strrchr( $this->mOname, '/' );
130
131 if ( false === $basename ) { $basename = $this->mOname; }
132 else ( $basename = substr( $basename, 1 ) );
133
134
135 $ext = strrchr( $basename, '.' );
136 if ( false === $ext ) { $ext = ''; }
137 else { $ext = substr( $ext, 1 ); }
138
139 if ( '' == $ext ) { $xl = 0; } else { $xl = strlen( $ext ) + 1; }
140 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
141
142 if ( strlen( $partname ) < 3 ) {
143 $this->mainUploadForm( WfMsg( 'minlength' ) );
144 return;
145 }
146
147 $changed_name = false;
148 $bn = preg_replace ( "/[^".Title::legalChars()."]/", '-', $basename );
149 if ( 0 != strcmp( $bn, $basename ) )
150 {
151 $changed_name = true;
152 $basename = $bn;
153 }
154
155
156 $nt = Title::newFromText( $basename );
157 if( !$nt ) {
158 return $this->uploadError( wfMsg( 'illegalfilename', htmlspecialchars( $basename ) ) );
159 }
160 $nt->setNamespace( Namespace::getImage() );
161 $this->mUploadSaveName = $nt->getDBkey();
162
163 /* Don't allow users to override the blacklist */
164 if( $this->checkFileExtension( $ext, $wgFileBlacklist ) ||
165 ($wgStrictFileExtensions && !$this->checkFileExtension( $ext, $wgFileExtensions ) ) ) {
166 return $this->uploadError( wfMsg( 'badfiletype', htmlspecialchars( $ext ) ) );
167 }
168
169 $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName );
170 if ( !$nt->userCanEdit() ) {
171 return $this->uploadError( wfMsg( 'protectedpage' ) );
172 }
173
174 if ( ! $this->mIgnoreWarning ) {
175 $warning = '';
176 if( $changed_name || 0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName ) ) {
177 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
178 }
179
180 if ( $wgCheckFileExtensions ) {
181 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
182 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $ext ) ).'</li>';
183 }
184 }
185 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
186 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
187 }
188 if ( $this->mUploadSize == 0 ) {
189 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
190 }
191 if( $nt->getArticleID() ) {
192 $sk = $wgUser->getSkin();
193 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
194 $dlink = $sk->makeKnownLink( $dname, $dname );
195 $warning .= '<li>'.wfMsg( 'fileexists', $dlink ).'</li>';
196 }
197 if($warning != '') return $this->uploadWarning($warning);
198 }
199 }
200 if ( !is_null( $this->mUploadOldVersion ) ) {
201 $wgUploadOldVersion = $this->mUploadOldVersion;
202 }
203 wfRecordUpload( $this->mUploadSaveName, $wgUploadOldVersion, $this->mUploadSize,
204 $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
205
206 $sk = $wgUser->getSkin();
207 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
208 $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
209 $dlink = $sk->makeKnownLink( $dname, $dname );
210
211 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
212 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
213 $wgOut->addHTML( "<p>{$text}\n" );
214 $wgOut->returnToMain( false );
215 }
216
217 function checkFileExtension( $ext, $list ) {
218 return in_array( strtolower( $ext ), $list );
219 }
220
221 function saveUploadedFile( $saveName, $tempName ) {
222 global $wgSavedFile, $wgUploadOldVersion;
223 global $wgUploadDirectory, $wgOut;
224
225 $dest = wfImageDir( $saveName );
226 $archive = wfImageArchiveDir( $saveName );
227 $wgSavedFile = "{$dest}/{$saveName}";
228
229 if ( is_file( $wgSavedFile ) ) {
230 $wgUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
231
232 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
233 $wgOut->fileRenameError( $wgSavedFile,
234 "${archive}/{$wgUploadOldVersion}" );
235 return;
236 }
237 } else {
238 $wgUploadOldVersion = '';
239 }
240 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
241 $wgOut->fileCopyError( $tempName, $wgSavedFile );
242 }
243 chmod( $wgSavedFile, 0644 );
244 }
245
246 function unsaveUploadedFile() {
247 global $wgUploadDirectory, $wgOut, $wgRequest;
248
249 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey];
250 $wgUploadOldVersion = $this->mUploadOldVersion;
251
252 if ( ! @unlink( $wgSavedFile ) ) {
253 $wgOut->fileDeleteError( $wgSavedFile );
254 return;
255 }
256 if ( '' != $wgUploadOldVersion ) {
257 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
258 $archive = $wgUploadDirectory.'/archive/' . $hash{0} .
259 '/' . substr( $hash, 0, 2 );
260
261 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
262 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
263 $wgSavedFile );
264 }
265 }
266 }
267
268 function uploadError( $error ) {
269 global $wgOut;
270 $sub = wfMsg( 'uploadwarning' );
271 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
272 $wgOut->addHTML( "<h4 style='error'>{$error}</h4>\n" );
273 }
274
275 function uploadWarning( $warning ) {
276 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
277 global $wgSavedFile, $wgUploadOldVersion;
278 global $wgUseCopyrightUpload;
279
280 # wgSavedFile is stored in the session not the form, for security
281 $this->mSessionKey = mt_rand( 0, 0x7fffffff );
282 $_SESSION['wsUploadFiles'][$this->mSessionKey] = $wgSavedFile;
283
284 $sub = wfMsg( 'uploadwarning' );
285 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
286 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
287
288 $save = wfMsg( 'savefile' );
289 $reupload = wfMsg( 'reupload' );
290 $iw = wfMsg( 'ignorewarning' );
291 $reup = wfMsg( 'reuploaddesc' );
292 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
293 $action = $titleObj->escapeLocalURL( 'action=submit' );
294
295 if ( $wgUseCopyrightUpload )
296 {
297 $copyright = "
298 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\" />
299 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\" />
300 ";
301 } else {
302 $copyright = "";
303 }
304
305 $wgOut->addHTML( "
306 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
307 action=\"{$action}\">
308 <input type=hidden name=\"wpUploadAffirm\" value=\"1\" />
309 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\" />
310 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
311 {$copyright}
312 <input type=hidden name=\"wpUploadSaveName\" value=\"" . htmlspecialchars( $this->mUploadSaveName ) . "\" />
313 <input type=hidden name=\"wpUploadTempName\" value=\"" . htmlspecialchars( $this->mUploadTempName ) . "\" />
314 <input type=hidden name=\"wpUploadSize\" value=\"" . htmlspecialchars( $this->mUploadSize ) . "\" />
315 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
316 <input type=hidden name=\"wpUploadOldVersion\" value=\"" . htmlspecialchars( $wgUploadOldVersion) . "\" />
317 <table border='0'><tr>
318 <tr><td align='right'>
319 <input tabindex='2' type='submit' name=\"wpUpload\" value=\"{$save}\" />
320 </td><td align='left'>{$iw}</td></tr>
321 <tr><td align='right'>
322 <input tabindex='2' type='submit' name=\"wpReUpload\" value=\"{$reupload}\" />
323 </td><td align='left'>{$reup}</td></tr></table></form>\n" );
324 }
325
326 function mainUploadForm( $msg ) {
327 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
328 global $wgUseCopyrightUpload;
329
330 if ( '' != $msg ) {
331 $sub = wfMsg( 'uploaderror' );
332 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
333 "<h4 style='error'>{$msg}</h4>\n" );
334 } else {
335 $sub = wfMsg( 'uploadfile' );
336 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
337 }
338 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
339 $sk = $wgUser->getSkin();
340
341 $fn = wfMsg( 'filename' );
342 $fd = wfMsg( 'filedesc' );
343 $ulb = wfMsg( 'uploadbtn' );
344
345 $clink = $sk->makeKnownLink( wfMsg( 'copyrightpage' ),
346 wfMsg( 'copyrightpagename' ) );
347 $ca = wfMsg( 'affirmation', $clink );
348 $iw = wfMsg( 'ignorewarning' );
349
350 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
351 $action = $titleObj->escapeLocalURL();
352
353 $source = "
354 <td align='right'>
355 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
356 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
357 " ;
358 if ( $wgUseCopyrightUpload )
359 {
360 $source = "
361 <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
362 <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
363 htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
364 </tr><tr>
365 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
366 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
367 htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
368 " ;
369 }
370
371 $wgOut->addHTML( "
372 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
373 action=\"{$action}\">
374 <table border='0'><tr>
375 <td align='right'>{$fn}:</td><td align='left'>
376 <input tabindex='1' type='file' name=\"wpUploadFile\" value=\""
377 . htmlspecialchars( $this->mUploadFile ) . "\" size='40' />
378 </td></tr><tr>
379 <td align='right'>{$fd}:</td><td align='left'>
380 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
381 . htmlspecialchars( $this->mUploadDescription ) . "\" size='40' />
382 </td></tr><tr>
383 {$source}
384 </tr>
385 <tr><td></td><td align='left'>
386 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
387 </td></tr></table></form>\n" );
388 }
389 }
390 ?>