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