5 * @subpackage SpecialPage
11 require_once( 'Image.php' );
16 function wfSpecialUpload() {
18 $form = new UploadForm( $wgRequest );
25 * @subpackage SpecialPage
28 var $mUploadAffirm, $mUploadFile, $mUploadDescription, $mIgnoreWarning;
29 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
30 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
31 var $mOname, $mSessionKey;
34 * Constructor : initialise object
35 * Get data POSTed through the form and assign them to the object
36 * @param $request Data posted.
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' );
55 /** Generate a temporary name if we don't have one yet */
56 if ( ! $this->mUploadTempName
) {
57 $this->mUploadTempName
= $request->getFileTempName( 'wpUploadFile' );
60 /** Get size of file */
61 if ( ! $this->mUploadSize
) {
62 $this->mUploadSize
= $request->getFileSize( 'wpUploadFile' );
64 $this->mOname
= $request->getFileName( 'wpUploadFile' );
72 global $wgUser, $wgOut;
73 global $wgDisableUploads;
75 /** Show an error message if file upload is disabled */
76 if ( $wgDisableUploads ) {
77 $wgOut->addWikiText( wfMsg( 'uploaddisabled' ) );
81 /** Various rights checks */
82 if ( ( $wgUser->getID() == 0 )
83 OR $wgUser->isBlocked() ) {
84 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
88 $wgOut->readOnlyPage();
92 if ( $this->mReUpload
) {
93 $this->unsaveUploadedFile();
94 $this->mainUploadForm( '' );
95 } else if ( 'submit' == $this->mAction ||
$this->mUpload
) {
96 $this->processUpload();
98 $this->mainUploadForm( '' );
103 * Really do the upload
104 * Checks are made in SpecialUpload::execute()
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;
115 if ( $wgUseCopyrightUpload ) {
116 $this->mUploadAffirm
= 1;
117 if ($wgCheckCopyrightUpload &&
118 (trim ( $this->mUploadCopyStatus
) == '' ||
trim ( $this->mUploadSource
) == '' )) {
119 $this->mUploadAffirm
= 0;
123 if ( $this->mUploadAffirm
!= 1) {
124 $this->mainUploadForm( WfMsg( 'noaffirmation' ) );
128 if ( '' != $this->mOname
) {
129 $basename = strrchr( $this->mOname
, '/' );
131 if ( false === $basename ) { $basename = $this->mOname
; }
132 else ( $basename = substr( $basename, 1 ) );
135 $ext = strrchr( $basename, '.' );
136 if ( false === $ext ) { $ext = ''; }
137 else { $ext = substr( $ext, 1 ); }
139 if ( '' == $ext ) { $xl = 0; } else { $xl = strlen( $ext ) +
1; }
140 $partname = substr( $basename, 0, strlen( $basename ) - $xl );
142 if ( strlen( $partname ) < 3 ) {
143 $this->mainUploadForm( WfMsg( 'minlength' ) );
147 $changed_name = false;
148 $bn = preg_replace ( "/[^".Title
::legalChars()."]/", '-', $basename );
149 if ( 0 != strcmp( $bn, $basename ) )
151 $changed_name = true;
156 $nt = Title
::newFromText( $basename );
158 return $this->uploadError( wfMsg( 'illegalfilename', htmlspecialchars( $basename ) ) );
160 $nt->setNamespace( Namespace::getImage() );
161 $this->mUploadSaveName
= $nt->getDBkey();
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 ) ) );
169 $this->saveUploadedFile( $this->mUploadSaveName
, $this->mUploadTempName
);
170 if ( !$nt->userCanEdit() ) {
171 return $this->uploadError( wfMsg( 'protectedpage' ) );
174 if ( ! $this->mIgnoreWarning
) {
176 if( $changed_name ||
0 != strcmp( ucfirst( $basename ), $this->mUploadSaveName
) ) {
177 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName
) ).'</li>';
180 if ( $wgCheckFileExtensions ) {
181 if ( ! $this->checkFileExtension( $ext, $wgFileExtensions ) ) {
182 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $ext ) ).'</li>';
185 if ( $wgUploadSizeWarning && ( $this->mUploadSize
> $wgUploadSizeWarning ) ) {
186 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
188 if ( $this->mUploadSize
== 0 ) {
189 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
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>';
197 if($warning != '') return $this->uploadWarning($warning);
200 if ( !is_null( $this->mUploadOldVersion
) ) {
201 $wgUploadOldVersion = $this->mUploadOldVersion
;
203 wfRecordUpload( $this->mUploadSaveName
, $wgUploadOldVersion, $this->mUploadSize
,
204 $this->mUploadDescription
, $this->mUploadCopyStatus
, $this->mUploadSource
);
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 );
211 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
212 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
213 $wgOut->addHTML( "<p>{$text}\n" );
214 $wgOut->returnToMain( false );
217 function checkFileExtension( $ext, $list ) {
218 return in_array( strtolower( $ext ), $list );
221 function saveUploadedFile( $saveName, $tempName ) {
222 global $wgSavedFile, $wgUploadOldVersion;
223 global $wgUploadDirectory, $wgOut;
225 $dest = wfImageDir( $saveName );
226 $archive = wfImageArchiveDir( $saveName );
227 $wgSavedFile = "{$dest}/{$saveName}";
229 if ( is_file( $wgSavedFile ) ) {
230 $wgUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
232 if ( ! rename( $wgSavedFile, "${archive}/{$wgUploadOldVersion}" ) ) {
233 $wgOut->fileRenameError( $wgSavedFile,
234 "${archive}/{$wgUploadOldVersion}" );
238 $wgUploadOldVersion = '';
240 if ( ! move_uploaded_file( $tempName, $wgSavedFile ) ) {
241 $wgOut->fileCopyError( $tempName, $wgSavedFile );
243 chmod( $wgSavedFile, 0644 );
246 function unsaveUploadedFile() {
247 global $wgUploadDirectory, $wgOut, $wgRequest;
249 $wgSavedFile = $_SESSION['wsUploadFiles'][$this->mSessionKey
];
250 $wgUploadOldVersion = $this->mUploadOldVersion
;
252 if ( ! @unlink
( $wgSavedFile ) ) {
253 $wgOut->fileDeleteError( $wgSavedFile );
256 if ( '' != $wgUploadOldVersion ) {
257 $hash = md5( substr( $wgUploadOldVersion, 15 ) );
258 $archive = $wgUploadDirectory.'/archive/' . $hash{0} .
259 '/' . substr( $hash, 0, 2 );
261 if ( ! rename( "{$archive}/{$wgUploadOldVersion}", $wgSavedFile ) ) {
262 $wgOut->fileRenameError( "{$archive}/{$wgUploadOldVersion}",
268 function uploadError( $error ) {
270 $sub = wfMsg( 'uploadwarning' );
271 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
272 $wgOut->addHTML( "<h4 style='error'>{$error}</h4>\n" );
275 function uploadWarning( $warning ) {
276 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
277 global $wgSavedFile, $wgUploadOldVersion;
278 global $wgUseCopyrightUpload;
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;
284 $sub = wfMsg( 'uploadwarning' );
285 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
286 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
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' );
295 if ( $wgUseCopyrightUpload )
298 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus
) . "\" />
299 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource
) . "\" />
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
) . "\" />
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" );
326 function mainUploadForm( $msg ) {
327 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
328 global $wgUseCopyrightUpload;
331 $sub = wfMsg( 'uploaderror' );
332 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
333 "<h4 style='error'>{$msg}</h4>\n" );
335 $sub = wfMsg( 'uploadfile' );
336 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
338 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
339 $sk = $wgUser->getSkin();
341 $fn = wfMsg( 'filename' );
342 $fd = wfMsg( 'filedesc' );
343 $ulb = wfMsg( 'uploadbtn' );
345 $clink = $sk->makeKnownLink( wfMsg( 'copyrightpage' ),
346 wfMsg( 'copyrightpagename' ) );
347 $ca = wfMsg( 'affirmation', $clink );
348 $iw = wfMsg( 'ignorewarning' );
350 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
351 $action = $titleObj->escapeLocalURL();
355 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
356 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
358 if ( $wgUseCopyrightUpload )
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>
365 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
366 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
367 htmlspecialchars($this->mUploadSource
). "\" size='40' /></td>
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' />
379 <td align='right'>{$fd}:</td><td align='left'>
380 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
381 . htmlspecialchars( $this->mUploadDescription
) . "\" size='40' />
385 <tr><td></td><td align='left'>
386 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
387 </td></tr></table></form>\n" );