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