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