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