Fix spelling MIN_LENGHT_PARTNAME
[lhc/web/wiklou.git] / includes / specials / SpecialUpload.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7
8 /**
9 * Entry point
10 */
11 function wfSpecialUpload() {
12 global $wgRequest;
13 $form = new UploadForm( $wgRequest );
14 $form->execute();
15 }
16
17 /**
18 * implements Special:Upload
19 * @ingroup SpecialPage
20 */
21 class UploadForm {
22 /**#@+
23 * @access private
24 */
25 var $mComment, $mLicense, $mIgnoreWarning;
26 var $mCopyrightStatus, $mCopyrightSource, $mReUpload, $mAction, $mUploadClicked;
27 var $mDestWarningAck;
28 var $mLocalFile;
29
30 var $mUpload; // Instance of UploadFromBase or derivative
31
32 # Placeholders for text injection by hooks (must be HTML)
33 # extensions should take care to _append_ to the present value
34 var $uploadFormTextTop;
35 var $uploadFormTextAfterSummary;
36
37 /**#@-*/
38
39 /**
40 * Constructor : initialise object
41 * Get data POSTed through the form and assign them to the object
42 * @param $request Data posted.
43 */
44 function __construct( &$request ) {
45 $this->mDesiredDestName = $request->getText( 'wpDestFile' );
46 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' );
47 $this->mComment = $request->getText( 'wpUploadDescription' );
48
49 if( !$request->wasPosted() ) {
50 # GET requests just give the main form; no data except destination
51 # filename and description
52 return;
53 }
54
55 # Placeholders for text injection by hooks (empty per default)
56 $this->uploadFormTextTop = "";
57 $this->uploadFormTextAfterSummary = "";
58
59 $this->mReUpload = $request->getCheck( 'wpReUpload' );
60 $this->mUploadClicked = $request->getCheck( 'wpUpload' );
61
62 $this->mLicense = $request->getText( 'wpLicense' );
63 $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
64 $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
65 $this->mWatchthis = $request->getBool( 'wpWatchthis' );
66 $this->mSourceType = $request->getText( 'wpSourceType' );
67 $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
68
69 $this->mAction = $request->getVal( 'action' );
70
71 $desiredDestName = $request->getText( 'wpDestFile' );
72 if( !$desiredDestName )
73 $desiredDestName = $request->getText( 'wpUploadFile' );
74
75 $this->mSessionKey = $request->getInt( 'wpSessionKey' );
76 if( !empty( $this->mSessionKey ) &&
77 isset( $_SESSION['wsUploadData'][$this->mSessionKey]['version'] ) &&
78 $_SESSION['wsUploadData'][$this->mSessionKey]['version'] ==
79 UploadFromBase::SESSION_VERSION ) {
80 /**
81 * Confirming a temporarily stashed upload.
82 * We don't want path names to be forged, so we keep
83 * them in the session on the server and just give
84 * an opaque key to the user agent.
85 */
86
87 $this->mUpload = new UploadFromStash( $desiredDestName );
88 $data = $_SESSION['wsUploadData'][$this->mSessionKey];
89 $this->mUpload->initialize( $data );
90
91 } else {
92 /**
93 *Check for a newly uploaded file.
94 */
95 if( UploadFromUrl::isEnabled() && $this->mSourceType == 'web' ) {
96 $this->mUpload = new UploadFromUrl( $desiredDestName );
97 $this->mUpload->initialize( $request->getText( 'wpUploadFileURL' ) );
98 } else {
99 $this->mUpload = new UploadFromUpload( $desiredDestName );
100 $this->mUpload->initialize(
101 $request->getFileTempName( 'wpUploadFile' ),
102 $request->getFileSize( 'wpUploadFile' ),
103 $request->getFileName( 'wpUploadFile' )
104 );
105 }
106 }
107 }
108
109
110
111 /**
112 * Start doing stuff
113 * @access public
114 */
115 function execute() {
116 global $wgUser, $wgOut;
117
118 # Check uploading enabled
119 if( !UploadFromBase::isEnabled() ) {
120 $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext', array( $this->mDesiredDestName ) );
121 return;
122 }
123
124 # Check permissions
125 if( $this->mUpload ) {
126 $permission = $this->mUpload->isAllowed( $wgUser );
127 } else {
128 $permission = $wgUser->isAllowed( 'upload' ) ? true : 'upload';
129 }
130 if( $permission !== true ) {
131 if( !$wgUser->isLoggedIn() ) {
132 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
133 } else {
134 $wgOut->permissionRequired( $permission );
135 }
136 return;
137 }
138
139 # Check blocks
140 if( $wgUser->isBlocked() ) {
141 $wgOut->blockedPage();
142 return;
143 }
144
145 if( wfReadOnly() ) {
146 $wgOut->readOnlyPage();
147 return;
148 }
149
150 if( $this->mReUpload ) {
151 // User did not choose to ignore warnings
152 if( !$this->mUpload->unsaveUploadedFile() ) {
153 return;
154 }
155 # Because it is probably checked and shouldn't be
156 $this->mIgnoreWarning = false;
157
158 $this->mainUploadForm();
159 } elseif( $this->mUpload && (
160 'submit' == $this->mAction ||
161 $this->mUploadClicked
162 ) ) {
163 $this->processUpload();
164 } else {
165 $this->mainUploadForm();
166 }
167
168 if( $this->mUpload )
169 $this->mUpload->cleanupTempFile();
170 }
171
172 /**
173 * Do the upload
174 * Checks are made in SpecialUpload::execute()
175 *
176 * @access private
177 */
178 function processUpload(){
179 global $wgUser, $wgOut, $wgFileExtensions, $wgLang;
180 $details = null;
181 $value = null;
182 $value = $this->internalProcessUpload( $details );
183
184 switch($value) {
185 case UploadFromBase::SUCCESS:
186 $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
187 break;
188
189 case UploadFromBase::BEFORE_PROCESSING:
190 // Do... nothing? Why?
191 break;
192
193 case UploadFromBase::LARGE_FILE_SERVER:
194 $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
195 break;
196
197 case UploadFromBase::EMPTY_FILE:
198 $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
199 break;
200
201 case UploadFromBase::MIN_LENGTH_PARTNAME:
202 $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
203 break;
204
205 case UploadFromBase::ILLEGAL_FILENAME:
206 $this->uploadError( wfMsgExt( 'illegalfilename',
207 'parseinline', $details['filtered'] ) );
208 break;
209
210 case UploadFromBase::PROTECTED_PAGE:
211 $wgOut->showPermissionsErrorPage( $details['permissionserrors'] );
212 break;
213
214 case UploadFromBase::OVERWRITE_EXISTING_FILE:
215 $this->uploadError( wfMsgExt( $details['overwrite'],
216 'parseinline' ) );
217 break;
218
219 case UploadFromBase::FILETYPE_MISSING:
220 $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
221 break;
222
223 case UploadFromBase::FILETYPE_BADTYPE:
224 $finalExt = $details['finalExt'];
225 $this->uploadError(
226 wfMsgExt( 'filetype-banned-type',
227 array( 'parseinline' ),
228 htmlspecialchars( $finalExt ),
229 implode(
230 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
231 $wgFileExtensions
232 ),
233 $wgLang->formatNum( count( $wgFileExtensions ) )
234 )
235 );
236 break;
237
238 case UploadFromBase::VERIFICATION_ERROR:
239 $args = $details['veri'];
240 $code = array_shift( $args );
241 $this->uploadError( wfMsgExt( $code, 'parseinline', $args ) );
242 break;
243
244 case UploadFromBase::UPLOAD_VERIFICATION_ERROR:
245 $error = $details['error'];
246 $this->uploadError( wfMsgExt( $error, 'parseinline' ) );
247 break;
248
249 case UploadFromBase::UPLOAD_WARNING:
250 $warning = $details['warning'];
251 $this->uploadWarning( $warning );
252 break;
253
254 case UploadFromBase::INTERNAL_ERROR:
255 $status = $details['internal'];
256 $this->showError( $wgOut->parse( $status->getWikiText() ) );
257 break;
258
259 default:
260 throw new MWException( __METHOD__ . ": Unknown value `{$value}`" );
261 }
262 }
263
264 /**
265 * Really do the upload
266 * Checks are made in SpecialUpload::execute()
267 *
268 * @param array $resultDetails contains result-specific dict of additional values
269 *
270 * @access private
271 */
272 function internalProcessUpload( &$resultDetails ) {
273 global $wgUser;
274
275 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
276 {
277 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file." );
278 return UploadFromBase::BEFORE_PROCESSING;
279 }
280
281 /**
282 * If the image is protected, non-sysop users won't be able
283 * to modify it by uploading a new revision.
284 */
285 $permErrors = $this->mUpload->verifyPermissions( $wgUser );
286 if( $permErrors !== true ) {
287 $resultDetails = array( 'permissionserrors' => $permErrors );
288 return UploadFromBase::PROTECTED_PAGE;
289 }
290
291 // Check whether this is a sane upload
292 $result = $this->mUpload->verifyUpload( $resultDetails );
293 if( $result != UploadFromBase::OK )
294 return $result;
295
296 $this->mLocalFile = $this->mUpload->getLocalFile();
297
298 if( !$this->mIgnoreWarning ) {
299 $warnings = $this->mUpload->checkWarnings();
300
301 if( count( $warnings ) ) {
302 $resultDetails = array( 'warning' => $warnings );
303 return UploadFromBase::UPLOAD_WARNING;
304 }
305 }
306
307
308 /**
309 * Try actually saving the thing...
310 * It will show an error form on failure. No it will not.
311 */
312 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
313 $this->mCopyrightStatus, $this->mCopyrightSource );
314
315 $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $wgUser );
316
317 if ( !$status->isGood() ) {
318 $resultDetails = array( 'internal' => $status );
319 return UploadFromBase::INTERNAL_ERROR;
320 } else {
321 // Success, redirect to description page
322 // WTF WTF WTF?
323 $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere?
324 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
325 return UploadFromBase::SUCCESS;
326 }
327 }
328
329 /**
330 * Do existence checks on a file and produce a warning
331 * This check is static and can be done pre-upload via AJAX
332 * Returns an HTML fragment consisting of one or more LI elements if there is a warning
333 * Returns an empty string if there is no warning
334 */
335 static function getExistsWarning( $exists ) {
336 global $wgUser, $wgContLang;
337
338 if( $exists === false )
339 return '';
340
341 $warning = '';
342 $align = $wgContLang->isRtl() ? 'left' : 'right';
343
344 list( $existsType, $file ) = $exists;
345
346 $sk = $wgUser->getSkin();
347
348 if( $existsType == 'exists' ) {
349 // Exact match
350 $dlink = $sk->makeKnownLinkObj( $file->getTitle() );
351 if ( $file->allowInlineDisplay() ) {
352 $dlink2 = $sk->makeImageLinkObj( $file->getTitle(), wfMsgExt( 'fileexists-thumb', 'parseinline' ),
353 $file->getName(), $align, array(), false, true );
354 } elseif ( !$file->allowInlineDisplay() && $file->isSafeFile() ) {
355 $icon = $file->iconThumb();
356 $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
357 $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' . $dlink . '</div>';
358 } else {
359 $dlink2 = '';
360 }
361
362 $warning .= '<li>' . wfMsgExt( 'fileexists', array('parseinline','replaceafter'), $dlink ) . '</li>' . $dlink2;
363
364 } elseif( $existsType == 'page-exists' ) {
365 $lnk = $sk->makeKnownLinkObj( $file->getTitle(), '', 'redirect=no' );
366 $warning .= '<li>' . wfMsgExt( 'filepageexists', array( 'parseinline', 'replaceafter' ), $lnk ) . '</li>';
367 } elseif ( $existsType == 'exists-normalized' ) {
368 # Check if image with lowercase extension exists.
369 # It's not forbidden but in 99% it makes no sense to upload the same filename with uppercase extension
370 $dlink = $sk->makeKnownLinkObj( $file->getTitle() );
371 if ( $file->allowInlineDisplay() ) {
372 $dlink2 = $sk->makeImageLinkObj( $file->getTitle(), wfMsgExt( 'fileexists-thumb', 'parseinline' ),
373 $file->getTitle()->getText(), $align, array(), false, true );
374 } elseif ( !$file->allowInlineDisplay() && $file->isSafeFile() ) {
375 $icon = $file->iconThumb();
376 $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
377 $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' . $dlink . '</div>';
378 } else {
379 $dlink2 = '';
380 }
381
382 $warning .= '<li>' .
383 wfMsgExt( 'fileexists-extension', 'parsemag',
384 $file->getTitle()->getPrefixedText(), $dlink ) .
385 '</li>' . $dlink2;
386
387 } elseif ( $existsType == 'thumb' ) {
388 # Check if an image without leading '180px-' (or similiar) exists
389 $dlink = $sk->makeKnownLinkObj( $file->getTitle() );
390 if ( $file->allowInlineDisplay() ) {
391 $dlink2 = $sk->makeImageLinkObj( $file->getTitle(),
392 wfMsgExt( 'fileexists-thumb', 'parseinline' ),
393 $file->getTitle()->getText(), $align, array(), false, true );
394 } elseif ( !$file->allowInlineDisplay() && $file->isSafeFile() ) {
395 $icon = $file->iconThumb();
396 $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
397 $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' .
398 $dlink . '</div>';
399 } else {
400 $dlink2 = '';
401 }
402 $warning .= '<li>' . wfMsgExt( 'fileexists-thumbnail-yes', 'parsemag', $dlink ) .
403 '</li>' . $dlink2;
404 }
405 return $warning;
406 }
407
408 /**
409 * Get a list of warnings
410 *
411 * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
412 * @return array list of warning messages
413 */
414 static function ajaxGetExistsWarning( $filename ) {
415 $file = wfFindFile( $filename );
416 if( !$file ) {
417 // Force local file so we have an object to do further checks against
418 // if there isn't an exact match...
419 $file = wfLocalFile( $filename );
420 }
421 $s = '&nbsp;';
422 if ( $file ) {
423 $exists = UploadFromBase::getExistsWarning( $file );
424 $warning = self::getExistsWarning( $exists );
425 // FIXME: We probably also want the prefix blacklist and the wasdeleted check here
426 if ( $warning !== '' ) {
427 $s = "<ul>$warning</ul>";
428 }
429 }
430 return $s;
431 }
432
433 /**
434 * Render a preview of a given license for the AJAX preview on upload
435 *
436 * @param string $license
437 * @return string
438 */
439 public static function ajaxGetLicensePreview( $license ) {
440 global $wgParser, $wgUser;
441 $text = '{{' . $license . '}}';
442 $title = Title::makeTitle( NS_IMAGE, 'Sample.jpg' );
443 $options = ParserOptions::newFromUser( $wgUser );
444
445 // Expand subst: first, then live templates...
446 $text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options );
447 $output = $wgParser->parse( $text, $title, $options );
448
449 return $output->getText();
450 }
451
452 /**
453 * Check for duplicate files and throw up a warning before the upload
454 * completes.
455 */
456 public static function getDupeWarning( $dupes ) {
457 if( $dupes ) {
458 global $wgOut;
459 $msg = "<gallery>";
460 foreach( $dupes as $file ) {
461 $title = $file->getTitle();
462 $msg .= $title->getPrefixedText() .
463 "|" . $title->getText() . "\n";
464 }
465 $msg .= "</gallery>";
466 return "<li>" .
467 wfMsgExt( "file-exists-duplicate", array( "parse" ), count( $dupes ) ) .
468 $wgOut->parse( $msg ) .
469 "</li>\n";
470 } else {
471 return '';
472 }
473 }
474
475 /**
476 * Remove a temporarily kept file stashed by saveTempUploadedFile().
477 * @access private
478 * @return success
479 */
480 function unsaveUploadedFile() {
481 global $wgOut;
482 $success = $this->mUpload->unsaveUploadedFile();
483 if ( ! $success ) {
484 $wgOut->showFileDeleteError( $this->mUpload->getTempPath() );
485 return false;
486 } else {
487 return true;
488 }
489 }
490
491 /* -------------------------------------------------------------- */
492
493 /**
494 * @param string $error as HTML
495 * @access private
496 */
497 function uploadError( $error ) {
498 global $wgOut;
499 $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
500 $wgOut->addHTML( '<span class="error">' . $error . '</span>' );
501 }
502
503 /**
504 * There's something wrong with this file, not enough to reject it
505 * totally but we require manual intervention to save it for real.
506 * Stash it away, then present a form asking to confirm or cancel.
507 *
508 * @param string $warning as HTML
509 * @access private
510 */
511 function uploadWarning( $warnings ) {
512 global $wgOut, $wgUser;
513 global $wgUseCopyrightUpload;
514
515 $this->mSessionKey = $this->mUpload->stashSession();
516 if( !$this->mSessionKey ) {
517 # Couldn't save file; an error has been displayed so let's go.
518 return;
519 }
520
521 $sk = $wgUser->getSkin();
522
523 $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
524 $wgOut->addHTML( '<ul class="warning">' );
525 foreach( $warnings as $warning => $args ) {
526 $msg = null;
527 if( $warning == 'exists' ) {
528 if ( !$this->mDestWarningAck )
529 $msg = self::getExistsWarning( $args );
530 } elseif( $warning == 'duplicate' ) {
531 $msg = $this->getDupeWarning( $args );
532 } elseif( $warning == 'filewasdeleted' ) {
533 $ltitle = SpecialPage::getTitleFor( 'Log' );
534 $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ),
535 'type=delete&page=' . $args->getPrefixedUrl() );
536 $msg = "\t<li>" . wfMsgWikiHtml( 'filewasdeleted', $llink ) . "</li>\n";
537 } else {
538 if( is_bool( $args ) )
539 $args = array();
540 elseif( !is_array( $args ) )
541 $args = array( $args );
542 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
543 }
544 if( $msg )
545 $wgOut->addHTML( $msg );
546 }
547
548 $titleObj = SpecialPage::getTitleFor( 'Upload' );
549
550 if ( $wgUseCopyrightUpload ) {
551 $copyright = Xml::hidden( 'wpUploadCopyStatus', $this->mCopyrightStatus ) . "\n" .
552 Xml::hidden( 'wpUploadSource', $this->mCopyrightSource ) . "\n";
553 } else {
554 $copyright = '';
555 }
556
557 $wgOut->addHTML(
558 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ),
559 'enctype' => 'multipart/form-data', 'id' => 'uploadwarning' ) ) . "\n" .
560 Xml::hidden( 'wpIgnoreWarning', '1' ) . "\n" .
561 Xml::hidden( 'wpSessionKey', $this->mSessionKey ) . "\n" .
562 Xml::hidden( 'wpUploadDescription', $this->mComment ) . "\n" .
563 Xml::hidden( 'wpLicense', $this->mLicense ) . "\n" .
564 Xml::hidden( 'wpDestFile', $this->mDesiredDestName ) . "\n" .
565 Xml::hidden( 'wpWatchthis', $this->mWatchthis ) . "\n" .
566 "{$copyright}<br />" .
567 Xml::submitButton( wfMsg( 'ignorewarning' ), array ( 'name' => 'wpUpload', 'id' => 'wpUpload', 'checked' => 'checked' ) ) . ' ' .
568 Xml::submitButton( wfMsg( 'reuploaddesc' ), array ( 'name' => 'wpReUpload', 'id' => 'wpReUpload' ) ) .
569 Xml::closeElement( 'form' ) . "\n"
570 );
571 }
572
573 /**
574 * Displays the main upload form, optionally with a highlighted
575 * error message up at the top.
576 *
577 * @param string $msg as HTML
578 * @access private
579 */
580 function mainUploadForm( $msg='' ) {
581 global $wgOut, $wgUser, $wgLang, $wgMaxUploadSize;
582 global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview;
583 global $wgRequest;
584 global $wgStylePath, $wgStyleVersion;
585
586 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
587 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview;
588
589 $adc = wfBoolToStr( $useAjaxDestCheck );
590 $alp = wfBoolToStr( $useAjaxLicensePreview );
591 $autofill = wfBoolToStr( $this->mDesiredDestName == '' );
592
593 $wgOut->addScript( "<script type=\"text/javascript\">
594 wgAjaxUploadDestCheck = {$adc};
595 wgAjaxLicensePreview = {$alp};
596 wgUploadAutoFill = {$autofill};
597 </script>" );
598 $wgOut->addScriptFile( 'upload.js' );
599 $wgOut->addScriptFile( 'edit.js' ); // For <charinsert> support
600
601 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
602 {
603 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
604 return false;
605 }
606
607 if( $this->mDesiredDestName ) {
608 $title = Title::makeTitleSafe( NS_IMAGE, $this->mDesiredDestName );
609 // Show a subtitle link to deleted revisions (to sysops et al only)
610 if( $title instanceof Title && ( $count = $title->isDeleted() ) > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
611 $link = wfMsgExt(
612 $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
613 array( 'parse', 'replaceafter' ),
614 $wgUser->getSkin()->makeKnownLinkObj(
615 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
616 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
617 )
618 );
619 $wgOut->addHtml( "<div id=\"contentSub2\">{$link}</div>" );
620 }
621
622 // Show the relevant lines from deletion log (for still deleted files only)
623 if( $title instanceof Title && $title->isDeleted() > 0 && !$title->exists() ) {
624 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
625 }
626 }
627
628 $cols = intval($wgUser->getOption( 'cols' ));
629
630 if( $wgUser->getOption( 'editwidth' ) ) {
631 $width = " style=\"width:100%\"";
632 } else {
633 $width = '';
634 }
635
636 if ( '' != $msg ) {
637 $sub = wfMsgHtml( 'uploaderror' );
638 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
639 "<span class='error'>{$msg}</span>\n" );
640 }
641 $wgOut->addHTML( '<div id="uploadtext">' );
642 $wgOut->addWikiMsg( 'uploadtext', $this->mDesiredDestName );
643 $wgOut->addHTML( "</div>\n" );
644
645 # Print a list of allowed file extensions, if so configured. We ignore
646 # MIME type here, it's incomprehensible to most people and too long.
647 global $wgCheckFileExtensions, $wgStrictFileExtensions,
648 $wgFileExtensions, $wgFileBlacklist;
649
650 $allowedExtensions = '';
651 if( $wgCheckFileExtensions ) {
652 $delim = wfMsgExt( 'comma-separator', array( 'escapenoentities' ) );
653 if( $wgStrictFileExtensions ) {
654 # Everything not permitted is banned
655 $extensionsList =
656 '<div id="mw-upload-permitted">' .
657 wfMsgWikiHtml( 'upload-permitted', implode( $wgFileExtensions, $delim ) ) .
658 "</div>\n";
659 } else {
660 # We have to list both preferred and prohibited
661 $extensionsList =
662 '<div id="mw-upload-preferred">' .
663 wfMsgWikiHtml( 'upload-preferred', implode( $wgFileExtensions, $delim ) ) .
664 "</div>\n" .
665 '<div id="mw-upload-prohibited">' .
666 wfMsgWikiHtml( 'upload-prohibited', implode( $wgFileBlacklist, $delim ) ) .
667 "</div>\n";
668 }
669 } else {
670 # Everything is permitted.
671 $extensionsList = '';
672 }
673
674 # Get the maximum file size from php.ini as $wgMaxUploadSize works for uploads from URL via CURL only
675 # See http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize for possible values of upload_max_filesize
676 $val = trim( ini_get( 'upload_max_filesize' ) );
677 $last = strtoupper( ( substr( $val, -1 ) ) );
678 switch( $last ) {
679 case 'G':
680 $val2 = substr( $val, 0, -1 ) * 1024 * 1024 * 1024;
681 break;
682 case 'M':
683 $val2 = substr( $val, 0, -1 ) * 1024 * 1024;
684 break;
685 case 'K':
686 $val2 = substr( $val, 0, -1 ) * 1024;
687 break;
688 default:
689 $val2 = $val;
690 }
691 $val2 = UploadFromUrl::isEnabled() ? min( $wgMaxUploadSize, $val2 ) : $val2;
692 $maxUploadSize = '<div id="mw-upload-maxfilesize">' .
693 wfMsgExt( 'upload-maxfilesize', array( 'parseinline', 'escapenoentities' ),
694 $wgLang->formatSize( $val2 ) ) .
695 "</div>\n";
696
697 $sourcefilename = wfMsgExt( 'sourcefilename', array( 'parseinline', 'escapenoentities' ) );
698 $destfilename = wfMsgExt( 'destfilename', array( 'parseinline', 'escapenoentities' ) );
699
700 $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
701
702 $licenses = new Licenses();
703 $license = wfMsgExt( 'license', array( 'parseinline' ) );
704 $nolicense = wfMsgHtml( 'nolicense' );
705 $licenseshtml = $licenses->getHtml();
706
707 $ulb = wfMsgHtml( 'uploadbtn' );
708
709
710 $titleObj = SpecialPage::getTitleFor( 'Upload' );
711
712 $encDestName = htmlspecialchars( $this->mDesiredDestName );
713
714 $watchChecked = $this->watchCheck()
715 ? 'checked="checked"'
716 : '';
717 $warningChecked = $this->mIgnoreWarning ? 'checked' : '';
718
719 // Prepare form for upload or upload/copy
720 if( UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' ) ) {
721 $filename_form =
722 "<input type='radio' id='wpSourceTypeFile' name='wpSourceType' value='file' " .
723 "onchange='toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\")' checked='checked' />" .
724 "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " .
725 "onfocus='" .
726 "toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\");" .
727 "toggle_element_check(\"wpSourceTypeFile\",\"wpSourceTypeURL\")' " .
728 "onchange='fillDestFilename(\"wpUploadFile\")' size='60' />" .
729 wfMsgHTML( 'upload_source_file' ) . "<br/>" .
730 "<input type='radio' id='wpSourceTypeURL' name='wpSourceType' value='web' " .
731 "onchange='toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\")' />" .
732 "<input tabindex='1' type='text' name='wpUploadFileURL' id='wpUploadFileURL' " .
733 "onfocus='" .
734 "toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\");" .
735 "toggle_element_check(\"wpSourceTypeURL\",\"wpSourceTypeFile\")' " .
736 "onchange='fillDestFilename(\"wpUploadFileURL\")' size='60' disabled='disabled' />" .
737 wfMsgHtml( 'upload_source_url' ) ;
738 } else {
739 $filename_form =
740 "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " .
741 ($this->mDesiredDestName?"":"onchange='fillDestFilename(\"wpUploadFile\")' ") .
742 "size='60' />" .
743 "<input type='hidden' name='wpSourceType' value='file' />" ;
744 }
745 if ( $useAjaxDestCheck ) {
746 $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp;</td></tr>";
747 $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"';
748 } else {
749 $warningRow = '';
750 $destOnkeyup = '';
751 }
752
753 $encComment = htmlspecialchars( $this->mComment );
754
755 $wgOut->addHTML(
756 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL(),
757 'enctype' => 'multipart/form-data', 'id' => 'mw-upload-form' ) ) .
758 Xml::openElement( 'fieldset' ) .
759 Xml::element( 'legend', null, wfMsg( 'upload' ) ) .
760 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-upload-table' ) ) .
761 "<tr>
762 {$this->uploadFormTextTop}
763 <td class='mw-label'>
764 <label for='wpUploadFile'>{$sourcefilename}</label>
765 </td>
766 <td class='mw-input'>
767 {$filename_form}
768 </td>
769 </tr>
770 <tr>
771 <td></td>
772 <td>
773 {$maxUploadSize}
774 {$extensionsList}
775 </td>
776 </tr>
777 <tr>
778 <td class='mw-label'>
779 <label for='wpDestFile'>{$destfilename}</label>
780 </td>
781 <td class='mw-input'>
782 <input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='60'
783 value=\"{$encDestName}\" onchange='toggleFilenameFiller()' $destOnkeyup />
784 </td>
785 </tr>
786 <tr>
787 <td class='mw-label'>
788 <label for='wpUploadDescription'>{$summary}</label>
789 </td>
790 <td class='mw-input'>
791 <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6'
792 cols='{$cols}'{$width}>$encComment</textarea>
793 {$this->uploadFormTextAfterSummary}
794 </td>
795 </tr>
796 <tr>"
797 );
798
799 if ( $licenseshtml != '' ) {
800 global $wgStylePath;
801 $wgOut->addHTML( "
802 <td class='mw-label'>
803 <label for='wpLicense'>$license</label>
804 </td>
805 <td class='mw-input'>
806 <select name='wpLicense' id='wpLicense' tabindex='4'
807 onchange='licenseSelectorCheck()'>
808 <option value=''>$nolicense</option>
809 $licenseshtml
810 </select>
811 </td>
812 </tr>
813 <tr>"
814 );
815 if( $useAjaxLicensePreview ) {
816 $wgOut->addHtml( "
817 <td></td>
818 <td id=\"mw-license-preview\"></td>
819 </tr>
820 <tr>"
821 );
822 }
823 }
824
825 if ( $wgUseCopyrightUpload ) {
826 $filestatus = wfMsgExt( 'filestatus', 'escapenoentities' );
827 $copystatus = htmlspecialchars( $this->mCopyrightStatus );
828 $filesource = wfMsgExt( 'filesource', 'escapenoentities' );
829 $uploadsource = htmlspecialchars( $this->mCopyrightSource );
830
831 $wgOut->addHTML( "
832 <td class='mw-label' style='white-space: nowrap;'>
833 <label for='wpUploadCopyStatus'>$filestatus</label></td>
834 <td class='mw-input'>
835 <input tabindex='5' type='text' name='wpUploadCopyStatus' id='wpUploadCopyStatus'
836 value=\"$copystatus\" size='60' />
837 </td>
838 </tr>
839 <tr>
840 <td class='mw-label'>
841 <label for='wpUploadCopyStatus'>$filesource</label>
842 </td>
843 <td class='mw-input'>
844 <input tabindex='6' type='text' name='wpUploadSource' id='wpUploadCopyStatus'
845 value=\"$uploadsource\" size='60' />
846 </td>
847 </tr>
848 <tr>"
849 );
850 }
851
852 $wgOut->addHtml( "
853 <td></td>
854 <td>
855 <input tabindex='7' type='checkbox' name='wpWatchthis' id='wpWatchthis' $watchChecked value='true' />
856 <label for='wpWatchthis'>" . wfMsgHtml( 'watchthisupload' ) . "</label>
857 <input tabindex='8' type='checkbox' name='wpIgnoreWarning' id='wpIgnoreWarning' value='true' $warningChecked/>
858 <label for='wpIgnoreWarning'>" . wfMsgHtml( 'ignorewarnings' ) . "</label>
859 </td>
860 </tr>
861 $warningRow
862 <tr>
863 <td></td>
864 <td class='mw-input'>
865 <input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\"" . $wgUser->getSkin()->tooltipAndAccesskey( 'upload' ) . " />
866 </td>
867 </tr>
868 <tr>
869 <td></td>
870 <td class='mw-input'>"
871 );
872 $wgOut->addWikiText( wfMsgForContent( 'edittools' ) );
873 $wgOut->addHTML( "
874 </td>
875 </tr>" .
876 Xml::closeElement( 'table' ) .
877 Xml::hidden( 'wpDestFileWarningAck', '', array( 'id' => 'wpDestFileWarningAck' ) ) .
878 Xml::closeElement( 'fieldset' ) .
879 Xml::closeElement( 'form' )
880 );
881 $uploadfooter = wfMsgNoTrans( 'uploadfooter' );
882 if( $uploadfooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadfooter ) ){
883 $wgOut->addWikiText( '<div id="mw-upload-footer-message">' . $uploadfooter . '</div>' );
884 }
885 }
886
887 /* -------------------------------------------------------------- */
888
889 /**
890 * See if we should check the 'watch this page' checkbox on the form
891 * based on the user's preferences and whether we're being asked
892 * to create a new file or update an existing one.
893 *
894 * In the case where 'watch edits' is off but 'watch creations' is on,
895 * we'll leave the box unchecked.
896 *
897 * Note that the page target can be changed *on the form*, so our check
898 * state can get out of sync.
899 */
900 function watchCheck() {
901 global $wgUser;
902 if( $wgUser->getOption( 'watchdefault' ) ) {
903 // Watch all edits!
904 return true;
905 }
906
907 $local = wfLocalFile( $this->mDesiredDestName );
908 if( $local && $local->exists() ) {
909 // We're uploading a new version of an existing file.
910 // No creation, so don't watch it if we're not already.
911 return $local->getTitle()->userIsWatching();
912 } else {
913 // New page should get watched if that's our option.
914 return $wgUser->getOption( 'watchcreations' );
915 }
916 }
917
918 /**
919 * Check if a user is the last uploader
920 *
921 * @param User $user
922 * @param string $img, image name
923 * @return bool
924 * @deprecated Use UploadFromBase::userCanReUpload
925 */
926 public static function userCanReUpload( User $user, $img ) {
927 wfDeprecated( __METHOD__ );
928
929 if( $user->isAllowed( 'reupload' ) )
930 return true; // non-conditional
931 if( !$user->isAllowed( 'reupload-own' ) )
932 return false;
933
934 $dbr = wfGetDB( DB_SLAVE );
935 $row = $dbr->selectRow('image',
936 /* SELECT */ 'img_user',
937 /* WHERE */ array( 'img_name' => $img )
938 );
939 if ( !$row )
940 return false;
941
942 return $user->getId() == $row->img_user;
943 }
944
945 /**
946 * Display an error with a wikitext description
947 */
948 function showError( $description ) {
949 global $wgOut;
950 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
951 $wgOut->setRobotPolicy( "noindex,nofollow" );
952 $wgOut->setArticleRelated( false );
953 $wgOut->enableClientCache( false );
954 $wgOut->addWikiText( $description );
955 }
956
957 /**
958 * Get the initial image page text based on a comment and optional file status information
959 */
960 static function getInitialPageText( $comment, $license, $copyStatus, $source ) {
961 global $wgUseCopyrightUpload;
962 if ( $wgUseCopyrightUpload ) {
963 if ( $license != '' ) {
964 $licensetxt = '== ' . wfMsgForContent( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
965 }
966 $pageText = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $comment . "\n" .
967 '== ' . wfMsgForContent ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
968 "$licensetxt" .
969 '== ' . wfMsgForContent ( 'filesource' ) . " ==\n" . $source ;
970 } else {
971 if ( $license != '' ) {
972 $filedesc = $comment == '' ? '' : '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $comment . "\n";
973 $pageText = $filedesc .
974 '== ' . wfMsgForContent ( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
975 } else {
976 $pageText = $comment;
977 }
978 }
979 return $pageText;
980 }
981
982 /**
983 * If there are rows in the deletion log for this file, show them,
984 * along with a nice little note for the user
985 *
986 * @param OutputPage $out
987 * @param string filename
988 */
989 private function showDeletionLog( $out, $filename ) {
990 global $wgUser;
991 $loglist = new LogEventsList( $wgUser->getSkin(), $out );
992 $pager = new LogPager( $loglist, 'delete', false, $filename );
993 if( $pager->getNumRows() > 0 ) {
994 $out->addHtml( '<div id="mw-upload-deleted-warn">' );
995 $out->addWikiMsg( 'upload-wasdeleted' );
996 $out->addHTML(
997 $loglist->beginLogEventsList() .
998 $pager->getBody() .
999 $loglist->endLogEventsList()
1000 );
1001 $out->addHtml( '</div>' );
1002 }
1003 }
1004 }