SpecialUpload.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * Entry point
  8. */
  9. function wfSpecialUpload() {
  10. global $wgRequest;
  11. $form = new UploadForm( $wgRequest );
  12. $form->execute();
  13. }
  14. /**
  15. * implements Special:Upload
  16. * @ingroup SpecialPage
  17. */
  18. class UploadForm {
  19. const SUCCESS = 0;
  20. const BEFORE_PROCESSING = 1;
  21. const LARGE_FILE_SERVER = 2;
  22. const EMPTY_FILE = 3;
  23. const MIN_LENGTH_PARTNAME = 4;
  24. const ILLEGAL_FILENAME = 5;
  25. const PROTECTED_PAGE = 6;
  26. const OVERWRITE_EXISTING_FILE = 7;
  27. const FILETYPE_MISSING = 8;
  28. const FILETYPE_BADTYPE = 9;
  29. const VERIFICATION_ERROR = 10;
  30. const UPLOAD_VERIFICATION_ERROR = 11;
  31. const UPLOAD_WARNING = 12;
  32. const INTERNAL_ERROR = 13;
  33. /**#@+
  34. * @access private
  35. */
  36. var $mComment, $mLicense, $mIgnoreWarning, $mCurlError;
  37. var $mDestName, $mTempPath, $mFileSize, $mFileProps;
  38. var $mCopyrightStatus, $mCopyrightSource, $mReUpload, $mAction, $mUploadClicked;
  39. var $mSrcName, $mSessionKey, $mStashed, $mDesiredDestName, $mRemoveTempFile, $mSourceType;
  40. var $mDestWarningAck, $mCurlDestHandle;
  41. var $mLocalFile;
  42. # Placeholders for text injection by hooks (must be HTML)
  43. # extensions should take care to _append_ to the present value
  44. var $uploadFormTextTop;
  45. var $uploadFormTextAfterSummary;
  46. const SESSION_VERSION = 1;
  47. /**#@-*/
  48. /**
  49. * Constructor : initialise object
  50. * Get data POSTed through the form and assign them to the object
  51. * @param $request Data posted.
  52. */
  53. function UploadForm( &$request ) {
  54. global $wgAllowCopyUploads;
  55. $this->mDesiredDestName = $request->getText( 'wpDestFile' );
  56. $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' );
  57. $this->mComment = $request->getText( 'wpUploadDescription' );
  58. $this->mForReUpload = $request->getBool( 'wpForReUpload' );
  59. $this->mReUpload = $request->getCheck( 'wpReUpload' );
  60. if( !$request->wasPosted() ) {
  61. # GET requests just give the main form; no data except destination
  62. # filename and description
  63. return;
  64. }
  65. # Placeholders for text injection by hooks (empty per default)
  66. $this->uploadFormTextTop = "";
  67. $this->uploadFormTextAfterSummary = "";
  68. $this->mUploadClicked = $request->getCheck( 'wpUpload' );
  69. $this->mLicense = $request->getText( 'wpLicense' );
  70. $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
  71. $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
  72. $this->mWatchthis = $request->getBool( 'wpWatchthis' );
  73. $this->mSourceType = $request->getText( 'wpSourceType' );
  74. $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
  75. $this->mAction = $request->getVal( 'action' );
  76. $this->mSessionKey = $request->getInt( 'wpSessionKey' );
  77. if( !empty( $this->mSessionKey ) &&
  78. isset( $_SESSION['wsUploadData'][$this->mSessionKey]['version'] ) &&
  79. $_SESSION['wsUploadData'][$this->mSessionKey]['version'] == self::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. $data = $_SESSION['wsUploadData'][$this->mSessionKey];
  87. $this->mTempPath = $data['mTempPath'];
  88. $this->mFileSize = $data['mFileSize'];
  89. $this->mSrcName = $data['mSrcName'];
  90. $this->mFileProps = $data['mFileProps'];
  91. $this->mCurlError = 0/*UPLOAD_ERR_OK*/;
  92. $this->mStashed = true;
  93. $this->mRemoveTempFile = false;
  94. } else {
  95. /**
  96. *Check for a newly uploaded file.
  97. */
  98. if( $wgAllowCopyUploads && $this->mSourceType == 'web' ) {
  99. $this->initializeFromUrl( $request );
  100. } else {
  101. $this->initializeFromUpload( $request );
  102. }
  103. }
  104. }
  105. /**
  106. * Initialize the uploaded file from PHP data
  107. * @access private
  108. */
  109. function initializeFromUpload( $request ) {
  110. $this->mTempPath = $request->getFileTempName( 'wpUploadFile' );
  111. $this->mFileSize = $request->getFileSize( 'wpUploadFile' );
  112. $this->mSrcName = $request->getFileName( 'wpUploadFile' );
  113. $this->mCurlError = $request->getUploadError( 'wpUploadFile' );
  114. $this->mSessionKey = false;
  115. $this->mStashed = false;
  116. $this->mRemoveTempFile = false; // PHP will handle this
  117. }
  118. /**
  119. * Copy a web file to a temporary file
  120. * @access private
  121. */
  122. function initializeFromUrl( $request ) {
  123. global $wgTmpDirectory;
  124. $url = $request->getText( 'wpUploadFileURL' );
  125. $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
  126. $this->mTempPath = $local_file;
  127. $this->mFileSize = 0; # Will be set by curlCopy
  128. $this->mCurlError = $this->curlCopy( $url, $local_file );
  129. $pathParts = explode( '/', $url );
  130. $this->mSrcName = array_pop( $pathParts );
  131. $this->mSessionKey = false;
  132. $this->mStashed = false;
  133. // PHP won't auto-cleanup the file
  134. $this->mRemoveTempFile = file_exists( $local_file );
  135. }
  136. /**
  137. * Safe copy from URL
  138. * Returns true if there was an error, false otherwise
  139. */
  140. private function curlCopy( $url, $dest ) {
  141. global $wgUser, $wgOut, $wgHTTPProxy;
  142. if( !$wgUser->isAllowed( 'upload_by_url' ) ) {
  143. $wgOut->permissionRequired( 'upload_by_url' );
  144. return true;
  145. }
  146. # Maybe remove some pasting blanks :-)
  147. $url = trim( $url );
  148. if( stripos($url, 'http://') !== 0 && stripos($url, 'ftp://') !== 0 ) {
  149. # Only HTTP or FTP URLs
  150. $wgOut->showErrorPage( 'upload-proto-error', 'upload-proto-error-text' );
  151. return true;
  152. }
  153. # Open temporary file
  154. $this->mCurlDestHandle = @fopen( $this->mTempPath, "wb" );
  155. if( $this->mCurlDestHandle === false ) {
  156. # Could not open temporary file to write in
  157. $wgOut->showErrorPage( 'upload-file-error', 'upload-file-error-text');
  158. return true;
  159. }
  160. $ch = curl_init();
  161. curl_setopt( $ch, CURLOPT_HTTP_VERSION, 1.0); # Probably not needed, but apparently can work around some bug
  162. curl_setopt( $ch, CURLOPT_TIMEOUT, 10); # 10 seconds timeout
  163. curl_setopt( $ch, CURLOPT_LOW_SPEED_LIMIT, 512); # 0.5KB per second minimum transfer speed
  164. curl_setopt( $ch, CURLOPT_URL, $url);
  165. if( $wgHTTPProxy ) {
  166. curl_setopt( $ch, CURLOPT_PROXY, $wgHTTPProxy );
  167. }
  168. curl_setopt( $ch, CURLOPT_WRITEFUNCTION, array( $this, 'uploadCurlCallback' ) );
  169. curl_exec( $ch );
  170. $error = curl_errno( $ch ) ? true : false;
  171. $errornum = curl_errno( $ch );
  172. // if ( $error ) print curl_error ( $ch ) ; # Debugging output
  173. curl_close( $ch );
  174. fclose( $this->mCurlDestHandle );
  175. unset( $this->mCurlDestHandle );
  176. if( $error ) {
  177. unlink( $dest );
  178. if( wfEmptyMsg( "upload-curl-error$errornum", wfMsg("upload-curl-error$errornum") ) )
  179. $wgOut->showErrorPage( 'upload-misc-error', 'upload-misc-error-text' );
  180. else
  181. $wgOut->showErrorPage( "upload-curl-error$errornum", "upload-curl-error$errornum-text" );
  182. }
  183. return $error;
  184. }
  185. /**
  186. * Callback function for CURL-based web transfer
  187. * Write data to file unless we've passed the length limit;
  188. * if so, abort immediately.
  189. * @access private
  190. */
  191. function uploadCurlCallback( $ch, $data ) {
  192. global $wgMaxUploadSize;
  193. $length = strlen( $data );
  194. $this->mFileSize += $length;
  195. if( $this->mFileSize > $wgMaxUploadSize ) {
  196. return 0;
  197. }
  198. fwrite( $this->mCurlDestHandle, $data );
  199. return $length;
  200. }
  201. /**
  202. * Start doing stuff
  203. * @access public
  204. */
  205. function execute() {
  206. global $wgUser, $wgOut;
  207. global $wgEnableUploads;
  208. # Check php's file_uploads setting
  209. if( !wfIniGetBool( 'file_uploads' ) ) {
  210. $wgOut->showErrorPage( 'uploaddisabled', 'php-uploaddisabledtext', array( $this->mDesiredDestName ) );
  211. return;
  212. }
  213. # Check uploading enabled
  214. if( !$wgEnableUploads ) {
  215. $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext', array( $this->mDesiredDestName ) );
  216. return;
  217. }
  218. # Check permissions
  219. if( !$wgUser->isAllowed( 'upload' ) ) {
  220. if( !$wgUser->isLoggedIn() ) {
  221. $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
  222. } else {
  223. $wgOut->permissionRequired( 'upload' );
  224. }
  225. return;
  226. }
  227. # Check blocks
  228. if( $wgUser->isBlocked() ) {
  229. $wgOut->blockedPage();
  230. return;
  231. }
  232. if( wfReadOnly() ) {
  233. $wgOut->readOnlyPage();
  234. return;
  235. }
  236. if( $this->mReUpload ) {
  237. if( !$this->unsaveUploadedFile() ) {
  238. return;
  239. }
  240. # Because it is probably checked and shouldn't be
  241. $this->mIgnoreWarning = false;
  242. $this->mainUploadForm();
  243. } else if( 'submit' == $this->mAction || $this->mUploadClicked ) {
  244. $this->processUpload();
  245. } else {
  246. $this->mainUploadForm();
  247. }
  248. $this->cleanupTempFile();
  249. }
  250. /**
  251. * Do the upload
  252. * Checks are made in SpecialUpload::execute()
  253. *
  254. * @access private
  255. */
  256. function processUpload(){
  257. global $wgUser, $wgOut, $wgFileExtensions, $wgLang;
  258. $details = null;
  259. $value = null;
  260. $value = $this->internalProcessUpload( $details );
  261. switch($value) {
  262. case self::SUCCESS:
  263. $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
  264. break;
  265. case self::BEFORE_PROCESSING:
  266. break;
  267. case self::LARGE_FILE_SERVER:
  268. $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
  269. break;
  270. case self::EMPTY_FILE:
  271. $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
  272. break;
  273. case self::MIN_LENGTH_PARTNAME:
  274. $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
  275. break;
  276. case self::ILLEGAL_FILENAME:
  277. $filtered = $details['filtered'];
  278. $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
  279. break;
  280. case self::PROTECTED_PAGE:
  281. $wgOut->showPermissionsErrorPage( $details['permissionserrors'] );
  282. break;
  283. case self::OVERWRITE_EXISTING_FILE:
  284. $errorText = $details['overwrite'];
  285. $this->uploadError( $wgOut->parse( $errorText ) );
  286. break;
  287. case self::FILETYPE_MISSING:
  288. $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
  289. break;
  290. case self::FILETYPE_BADTYPE:
  291. $finalExt = $details['finalExt'];
  292. $this->uploadError(
  293. wfMsgExt( 'filetype-banned-type',
  294. array( 'parseinline' ),
  295. htmlspecialchars( $finalExt ),
  296. $wgLang->commaList( $wgFileExtensions ),
  297. $wgLang->formatNum( count($wgFileExtensions) )
  298. )
  299. );
  300. break;
  301. case self::VERIFICATION_ERROR:
  302. $veri = $details['veri'];
  303. $this->uploadError( $veri->toString() );
  304. break;
  305. case self::UPLOAD_VERIFICATION_ERROR:
  306. $error = $details['error'];
  307. $this->uploadError( $error );
  308. break;
  309. case self::UPLOAD_WARNING:
  310. $warning = $details['warning'];
  311. $this->uploadWarning( $warning );
  312. break;
  313. case self::INTERNAL_ERROR:
  314. $internal = $details['internal'];
  315. $this->showError( $internal );
  316. break;
  317. default:
  318. throw new MWException( __METHOD__ . ": Unknown value `{$value}`" );
  319. }
  320. }
  321. /**
  322. * Really do the upload
  323. * Checks are made in SpecialUpload::execute()
  324. *
  325. * @param array $resultDetails contains result-specific dict of additional values
  326. *
  327. * @access private
  328. */
  329. function internalProcessUpload( &$resultDetails ) {
  330. global $wgUser;
  331. if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
  332. {
  333. wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
  334. return self::BEFORE_PROCESSING;
  335. }
  336. /**
  337. * If there was no filename or a zero size given, give up quick.
  338. */
  339. if( trim( $this->mSrcName ) == '' || empty( $this->mFileSize ) ) {
  340. return self::EMPTY_FILE;
  341. }
  342. /* Check for curl error */
  343. if( $this->mCurlError ) {
  344. return self::BEFORE_PROCESSING;
  345. }
  346. /**
  347. * Chop off any directories in the given filename. Then
  348. * filter out illegal characters, and try to make a legible name
  349. * out of it. We'll strip some silently that Title would die on.
  350. */
  351. if( $this->mDesiredDestName ) {
  352. $basename = $this->mDesiredDestName;
  353. } else {
  354. $basename = $this->mSrcName;
  355. }
  356. $filtered = wfStripIllegalFilenameChars( $basename );
  357. /* Normalize to title form before we do any further processing */
  358. $nt = Title::makeTitleSafe( NS_FILE, $filtered );
  359. if( is_null( $nt ) ) {
  360. $resultDetails = array( 'filtered' => $filtered );
  361. return self::ILLEGAL_FILENAME;
  362. }
  363. $filtered = $nt->getDBkey();
  364. /**
  365. * We'll want to blacklist against *any* 'extension', and use
  366. * only the final one for the whitelist.
  367. */
  368. list( $partname, $ext ) = $this->splitExtensions( $filtered );
  369. if( count( $ext ) ) {
  370. $finalExt = $ext[count( $ext ) - 1];
  371. } else {
  372. $finalExt = '';
  373. }
  374. # If there was more than one "extension", reassemble the base
  375. # filename to prevent bogus complaints about length
  376. if( count( $ext ) > 1 ) {
  377. for( $i = 0; $i < count( $ext ) - 1; $i++ )
  378. $partname .= '.' . $ext[$i];
  379. }
  380. if( strlen( $partname ) < 1 ) {
  381. return self::MIN_LENGTH_PARTNAME;
  382. }
  383. $this->mLocalFile = wfLocalFile( $nt );
  384. $this->mDestName = $this->mLocalFile->getName();
  385. /**
  386. * If the image is protected, non-sysop users won't be able
  387. * to modify it by uploading a new revision.
  388. */
  389. $permErrors = $nt->getUserPermissionsErrors( 'edit', $wgUser );
  390. $permErrorsUpload = $nt->getUserPermissionsErrors( 'upload', $wgUser );
  391. $permErrorsCreate = ( $nt->exists() ? array() : $nt->getUserPermissionsErrors( 'create', $wgUser ) );
  392. if( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
  393. // merge all the problems into one list, avoiding duplicates
  394. $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsUpload, $permErrors ) );
  395. $permErrors = array_merge( $permErrors, wfArrayDiff2( $permErrorsCreate, $permErrors ) );
  396. $resultDetails = array( 'permissionserrors' => $permErrors );
  397. return self::PROTECTED_PAGE;
  398. }
  399. /**
  400. * In some cases we may forbid overwriting of existing files.
  401. */
  402. $overwrite = $this->checkOverwrite( $this->mDestName );
  403. if( $overwrite !== true ) {
  404. $resultDetails = array( 'overwrite' => $overwrite );
  405. return self::OVERWRITE_EXISTING_FILE;
  406. }
  407. /* Don't allow users to override the blacklist (check file extension) */
  408. global $wgCheckFileExtensions, $wgStrictFileExtensions;
  409. global $wgFileExtensions, $wgFileBlacklist;
  410. if ($finalExt == '') {
  411. return self::FILETYPE_MISSING;
  412. } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) ||
  413. ($wgCheckFileExtensions && $wgStrictFileExtensions &&
  414. !$this->checkFileExtension( $finalExt, $wgFileExtensions ) ) ) {
  415. $resultDetails = array( 'finalExt' => $finalExt );
  416. return self::FILETYPE_BADTYPE;
  417. }
  418. /**
  419. * Look at the contents of the file; if we can recognize the
  420. * type but it's corrupt or data of the wrong type, we should
  421. * probably not accept it.
  422. */
  423. if( !$this->mStashed ) {
  424. $this->mFileProps = File::getPropsFromPath( $this->mTempPath, $finalExt );
  425. $this->checkMacBinary();
  426. $veri = $this->verify( $this->mTempPath, $finalExt );
  427. if( $veri !== true ) { //it's a wiki error...
  428. $resultDetails = array( 'veri' => $veri );
  429. return self::VERIFICATION_ERROR;
  430. }
  431. /**
  432. * Provide an opportunity for extensions to add further checks
  433. */
  434. $error = '';
  435. if( !wfRunHooks( 'UploadVerification',
  436. array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
  437. $resultDetails = array( 'error' => $error );
  438. return self::UPLOAD_VERIFICATION_ERROR;
  439. }
  440. }
  441. /**
  442. * Check for non-fatal conditions
  443. */
  444. if ( ! $this->mIgnoreWarning ) {
  445. $warning = '';
  446. $comparableName = str_replace( ' ', '_', $basename );
  447. global $wgCapitalLinks, $wgContLang;
  448. if ( $wgCapitalLinks ) {
  449. $comparableName = $wgContLang->ucfirst( $comparableName );
  450. }
  451. if( $comparableName !== $filtered ) {
  452. $warning .= '<li>'.wfMsgHtml( 'badfilename', htmlspecialchars( $this->mDestName ) ).'</li>';
  453. }
  454. global $wgCheckFileExtensions;
  455. if ( $wgCheckFileExtensions ) {
  456. if ( !$this->checkFileExtension( $finalExt, $wgFileExtensions ) ) {
  457. global $wgLang;
  458. $warning .= '<li>' .
  459. wfMsgExt( 'filetype-unwanted-type',
  460. array( 'parseinline' ),
  461. htmlspecialchars( $finalExt ),
  462. $wgLang->commaList( $wgFileExtensions ),
  463. $wgLang->formatNum( count($wgFileExtensions) )
  464. ) . '</li>';
  465. }
  466. }
  467. global $wgUploadSizeWarning;
  468. if ( $wgUploadSizeWarning && ( $this->mFileSize > $wgUploadSizeWarning ) ) {
  469. $skin = $wgUser->getSkin();
  470. $wsize = $skin->formatSize( $wgUploadSizeWarning );
  471. $asize = $skin->formatSize( $this->mFileSize );
  472. $warning .= '<li>' . wfMsgHtml( 'large-file', $wsize, $asize ) . '</li>';
  473. }
  474. if ( $this->mFileSize == 0 ) {
  475. $warning .= '<li>'.wfMsgHtml( 'emptyfile' ).'</li>';
  476. }
  477. if ( !$this->mDestWarningAck ) {
  478. $warning .= self::getExistsWarning( $this->mLocalFile );
  479. }
  480. $warning .= $this->getDupeWarning( $this->mTempPath, $finalExt, $nt );
  481. if( $warning != '' ) {
  482. /**
  483. * Stash the file in a temporary location; the user can choose
  484. * to let it through and we'll complete the upload then.
  485. */
  486. $resultDetails = array( 'warning' => $warning );
  487. return self::UPLOAD_WARNING;
  488. }
  489. }
  490. /**
  491. * Try actually saving the thing...
  492. * It will show an error form on failure.
  493. */
  494. if( !$this->mForReUpload ) {
  495. $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
  496. $this->mCopyrightStatus, $this->mCopyrightSource );
  497. }
  498. $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
  499. File::DELETE_SOURCE, $this->mFileProps );
  500. if ( !$status->isGood() ) {
  501. $resultDetails = array( 'internal' => $status->getWikiText() );
  502. return self::INTERNAL_ERROR;
  503. } else {
  504. if ( $this->mWatchthis ) {
  505. global $wgUser;
  506. $wgUser->addWatch( $this->mLocalFile->getTitle() );
  507. }
  508. // Success, redirect to description page
  509. $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere?
  510. wfRunHooks( 'UploadComplete', array( &$this ) );
  511. return self::SUCCESS;
  512. }
  513. }
  514. /**
  515. * Do existence checks on a file and produce a warning
  516. * This check is static and can be done pre-upload via AJAX
  517. * Returns an HTML fragment consisting of one or more LI elements if there is a warning
  518. * Returns an empty string if there is no warning
  519. */
  520. static function getExistsWarning( $file ) {
  521. global $wgUser, $wgContLang;
  522. // Check for uppercase extension. We allow these filenames but check if an image
  523. // with lowercase extension exists already
  524. $warning = '';
  525. $align = $wgContLang->isRtl() ? 'left' : 'right';
  526. if( strpos( $file->getName(), '.' ) == false ) {
  527. $partname = $file->getName();
  528. $rawExtension = '';
  529. } else {
  530. $n = strrpos( $file->getName(), '.' );
  531. $rawExtension = substr( $file->getName(), $n + 1 );
  532. $partname = substr( $file->getName(), 0, $n );
  533. }
  534. $sk = $wgUser->getSkin();
  535. if ( $rawExtension != $file->getExtension() ) {
  536. // We're not using the normalized form of the extension.
  537. // Normal form is lowercase, using most common of alternate
  538. // extensions (eg 'jpg' rather than 'JPEG').
  539. //
  540. // Check for another file using the normalized form...
  541. $nt_lc = Title::makeTitle( NS_FILE, $partname . '.' . $file->getExtension() );
  542. $file_lc = wfLocalFile( $nt_lc );
  543. } else {
  544. $file_lc = false;
  545. }
  546. if( $file->exists() ) {
  547. $dlink = $sk->makeKnownLinkObj( $file->getTitle() );
  548. if ( $file->allowInlineDisplay() ) {
  549. $dlink2 = $sk->makeImageLinkObj( $file->getTitle(), wfMsgExt( 'fileexists-thumb', 'parseinline' ),
  550. $file->getName(), $align, array(), false, true );
  551. } elseif ( !$file->allowInlineDisplay() && $file->isSafeFile() ) {
  552. $icon = $file->iconThumb();
  553. $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
  554. $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' . $dlink . '</div>';
  555. } else {
  556. $dlink2 = '';
  557. }
  558. $warning .= '<li>' . wfMsgExt( 'fileexists', array('parseinline','replaceafter'), $dlink ) . '</li>' . $dlink2;
  559. } elseif( $file->getTitle()->getArticleID() ) {
  560. $lnk = $sk->makeKnownLinkObj( $file->getTitle(), '', 'redirect=no' );
  561. $warning .= '<li>' . wfMsgExt( 'filepageexists', array( 'parseinline', 'replaceafter' ), $lnk ) . '</li>';
  562. } elseif ( $file_lc && $file_lc->exists() ) {
  563. # Check if image with lowercase extension exists.
  564. # It's not forbidden but in 99% it makes no sense to upload the same filename with uppercase extension
  565. $dlink = $sk->makeKnownLinkObj( $nt_lc );
  566. if ( $file_lc->allowInlineDisplay() ) {
  567. $dlink2 = $sk->makeImageLinkObj( $nt_lc, wfMsgExt( 'fileexists-thumb', 'parseinline' ),
  568. $nt_lc->getText(), $align, array(), false, true );
  569. } elseif ( !$file_lc->allowInlineDisplay() && $file_lc->isSafeFile() ) {
  570. $icon = $file_lc->iconThumb();
  571. $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
  572. $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' . $dlink . '</div>';
  573. } else {
  574. $dlink2 = '';
  575. }
  576. $warning .= '<li>' .
  577. wfMsgExt( 'fileexists-extension', 'parsemag',
  578. $file->getTitle()->getPrefixedText(), $dlink ) .
  579. '</li>' . $dlink2;
  580. } elseif ( ( substr( $partname , 3, 3 ) == 'px-' || substr( $partname , 2, 3 ) == 'px-' )
  581. && ereg( "[0-9]{2}" , substr( $partname , 0, 2) ) )
  582. {
  583. # Check for filenames like 50px- or 180px-, these are mostly thumbnails
  584. $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $rawExtension );
  585. $file_thb = wfLocalFile( $nt_thb );
  586. if ($file_thb->exists() ) {
  587. # Check if an image without leading '180px-' (or similiar) exists
  588. $dlink = $sk->makeKnownLinkObj( $nt_thb);
  589. if ( $file_thb->allowInlineDisplay() ) {
  590. $dlink2 = $sk->makeImageLinkObj( $nt_thb,
  591. wfMsgExt( 'fileexists-thumb', 'parseinline' ),
  592. $nt_thb->getText(), $align, array(), false, true );
  593. } elseif ( !$file_thb->allowInlineDisplay() && $file_thb->isSafeFile() ) {
  594. $icon = $file_thb->iconThumb();
  595. $dlink2 = '<div style="float:' . $align . '" id="mw-media-icon">' .
  596. $icon->toHtml( array( 'desc-link' => true ) ) . '<br />' .
  597. $dlink . '</div>';
  598. } else {
  599. $dlink2 = '';
  600. }
  601. $warning .= '<li>' . wfMsgExt( 'fileexists-thumbnail-yes', 'parsemag', $dlink ) .
  602. '</li>' . $dlink2;
  603. } else {
  604. # Image w/o '180px-' does not exists, but we do not like these filenames
  605. $warning .= '<li>' . wfMsgExt( 'file-thumbnail-no', 'parseinline' ,
  606. substr( $partname , 0, strpos( $partname , '-' ) +1 ) ) . '</li>';
  607. }
  608. }
  609. $filenamePrefixBlacklist = self::getFilenamePrefixBlacklist();
  610. # Do the match
  611. foreach( $filenamePrefixBlacklist as $prefix ) {
  612. if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
  613. $warning .= '<li>' . wfMsgExt( 'filename-bad-prefix', 'parseinline', $prefix ) . '</li>';
  614. break;
  615. }
  616. }
  617. if ( $file->wasDeleted() && !$file->exists() ) {
  618. # If the file existed before and was deleted, warn the user of this
  619. # Don't bother doing so if the file exists now, however
  620. $ltitle = SpecialPage::getTitleFor( 'Log' );
  621. $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ),
  622. 'type=delete&page=' . $file->getTitle()->getPrefixedUrl() );
  623. $warning .= '<li>' . wfMsgWikiHtml( 'filewasdeleted', $llink ) . '</li>';
  624. }
  625. return $warning;
  626. }
  627. /**
  628. * Get a list of warnings
  629. *
  630. * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
  631. * @return array list of warning messages
  632. */
  633. static function ajaxGetExistsWarning( $filename ) {
  634. $file = wfFindFile( $filename );
  635. if( !$file ) {
  636. // Force local file so we have an object to do further checks against
  637. // if there isn't an exact match...
  638. $file = wfLocalFile( $filename );
  639. }
  640. $s = '&nbsp;';
  641. if ( $file ) {
  642. $warning = self::getExistsWarning( $file );
  643. if ( $warning !== '' ) {
  644. $s = "<ul>$warning</ul>";
  645. }
  646. }
  647. return $s;
  648. }
  649. /**
  650. * Render a preview of a given license for the AJAX preview on upload
  651. *
  652. * @param string $license
  653. * @return string
  654. */
  655. public static function ajaxGetLicensePreview( $license ) {
  656. global $wgParser, $wgUser;
  657. $text = '{{' . $license . '}}';
  658. $title = Title::makeTitle( NS_FILE, 'Sample.jpg' );
  659. $options = ParserOptions::newFromUser( $wgUser );
  660. // Expand subst: first, then live templates...
  661. $text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options );
  662. $output = $wgParser->parse( $text, $title, $options );
  663. return $output->getText();
  664. }
  665. /**
  666. * Check for duplicate files and throw up a warning before the upload
  667. * completes.
  668. */
  669. function getDupeWarning( $tempfile, $extension, $destinationTitle ) {
  670. $hash = File::sha1Base36( $tempfile );
  671. $dupes = RepoGroup::singleton()->findBySha1( $hash );
  672. $archivedImage = new ArchivedFile( null, 0, $hash.".$extension" );
  673. if( $dupes ) {
  674. global $wgOut;
  675. $msg = "<gallery>";
  676. foreach( $dupes as $file ) {
  677. $title = $file->getTitle();
  678. # Don't throw the warning when the titles are the same, it's a reupload
  679. # and highly redundant.
  680. if ( !$title->equals( $destinationTitle ) || !$this->mForReUpload ) {
  681. $msg .= $title->getPrefixedText() .
  682. "|" . $title->getText() . "\n";
  683. }
  684. }
  685. $msg .= "</gallery>";
  686. return "<li>" .
  687. wfMsgExt( "file-exists-duplicate", array( "parse" ), count( $dupes ) ) .
  688. $wgOut->parse( $msg ) .
  689. "</li>\n";
  690. } elseif ( $archivedImage->getID() > 0 ) {
  691. global $wgOut;
  692. $name = Title::makeTitle( NS_FILE, $archivedImage->getName() )->getPrefixedText();
  693. return Xml::tags( 'li', null, wfMsgExt( 'file-deleted-duplicate', array( 'parseinline' ), array( $name ) ) );
  694. } else {
  695. return '';
  696. }
  697. }
  698. /**
  699. * Get a list of blacklisted filename prefixes from [[MediaWiki:filename-prefix-blacklist]]
  700. *
  701. * @return array list of prefixes
  702. */
  703. public static function getFilenamePrefixBlacklist() {
  704. $blacklist = array();
  705. $message = wfMsgForContent( 'filename-prefix-blacklist' );
  706. if( $message && !( wfEmptyMsg( 'filename-prefix-blacklist', $message ) || $message == '-' ) ) {
  707. $lines = explode( "\n", $message );
  708. foreach( $lines as $line ) {
  709. // Remove comment lines
  710. $comment = substr( trim( $line ), 0, 1 );
  711. if ( $comment == '#' || $comment == '' ) {
  712. continue;
  713. }
  714. // Remove additional comments after a prefix
  715. $comment = strpos( $line, '#' );
  716. if ( $comment > 0 ) {
  717. $line = substr( $line, 0, $comment-1 );
  718. }
  719. $blacklist[] = trim( $line );
  720. }
  721. }
  722. return $blacklist;
  723. }
  724. /**
  725. * Stash a file in a temporary directory for later processing
  726. * after the user has confirmed it.
  727. *
  728. * If the user doesn't explicitly cancel or accept, these files
  729. * can accumulate in the temp directory.
  730. *
  731. * @param string $saveName - the destination filename
  732. * @param string $tempName - the source temporary file to save
  733. * @return string - full path the stashed file, or false on failure
  734. * @access private
  735. */
  736. function saveTempUploadedFile( $saveName, $tempName ) {
  737. global $wgOut;
  738. $repo = RepoGroup::singleton()->getLocalRepo();
  739. $status = $repo->storeTemp( $saveName, $tempName );
  740. if ( !$status->isGood() ) {
  741. $this->showError( $status->getWikiText() );
  742. return false;
  743. } else {
  744. return $status->value;
  745. }
  746. }
  747. /**
  748. * Stash a file in a temporary directory for later processing,
  749. * and save the necessary descriptive info into the session.
  750. * Returns a key value which will be passed through a form
  751. * to pick up the path info on a later invocation.
  752. *
  753. * @return int
  754. * @access private
  755. */
  756. function stashSession() {
  757. $stash = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
  758. if( !$stash ) {
  759. # Couldn't save the file.
  760. return false;
  761. }
  762. $key = mt_rand( 0, 0x7fffffff );
  763. $_SESSION['wsUploadData'][$key] = array(
  764. 'mTempPath' => $stash,
  765. 'mFileSize' => $this->mFileSize,
  766. 'mSrcName' => $this->mSrcName,
  767. 'mFileProps' => $this->mFileProps,
  768. 'version' => self::SESSION_VERSION,
  769. );
  770. return $key;
  771. }
  772. /**
  773. * Remove a temporarily kept file stashed by saveTempUploadedFile().
  774. * @access private
  775. * @return success
  776. */
  777. function unsaveUploadedFile() {
  778. global $wgOut;
  779. if( !$this->mTempPath ) return true; // nothing to delete
  780. $repo = RepoGroup::singleton()->getLocalRepo();
  781. $success = $repo->freeTemp( $this->mTempPath );
  782. if ( ! $success ) {
  783. $wgOut->showFileDeleteError( $this->mTempPath );
  784. return false;
  785. } else {
  786. return true;
  787. }
  788. }
  789. /* -------------------------------------------------------------- */
  790. /**
  791. * @param string $error as HTML
  792. * @access private
  793. */
  794. function uploadError( $error ) {
  795. global $wgOut;
  796. $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
  797. $wgOut->addHTML( '<span class="error">' . $error . '</span>' );
  798. }
  799. /**
  800. * There's something wrong with this file, not enough to reject it
  801. * totally but we require manual intervention to save it for real.
  802. * Stash it away, then present a form asking to confirm or cancel.
  803. *
  804. * @param string $warning as HTML
  805. * @access private
  806. */
  807. function uploadWarning( $warning ) {
  808. global $wgOut;
  809. global $wgUseCopyrightUpload;
  810. $this->mSessionKey = $this->stashSession();
  811. if( !$this->mSessionKey ) {
  812. # Couldn't save file; an error has been displayed so let's go.
  813. return;
  814. }
  815. $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
  816. $wgOut->addHTML( '<ul class="warning">' . $warning . "</ul>\n" );
  817. $titleObj = SpecialPage::getTitleFor( 'Upload' );
  818. if ( $wgUseCopyrightUpload ) {
  819. $copyright = Xml::hidden( 'wpUploadCopyStatus', $this->mCopyrightStatus ) . "\n" .
  820. Xml::hidden( 'wpUploadSource', $this->mCopyrightSource ) . "\n";
  821. } else {
  822. $copyright = '';
  823. }
  824. $wgOut->addHTML(
  825. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ),
  826. 'enctype' => 'multipart/form-data', 'id' => 'uploadwarning' ) ) . "\n" .
  827. Xml::hidden( 'wpIgnoreWarning', '1' ) . "\n" .
  828. Xml::hidden( 'wpSessionKey', $this->mSessionKey ) . "\n" .
  829. Xml::hidden( 'wpUploadDescription', $this->mComment ) . "\n" .
  830. Xml::hidden( 'wpLicense', $this->mLicense ) . "\n" .
  831. Xml::hidden( 'wpDestFile', $this->mDesiredDestName ) . "\n" .
  832. Xml::hidden( 'wpWatchthis', $this->mWatchthis ) . "\n" .
  833. "{$copyright}<br />" .
  834. Xml::submitButton( wfMsg( 'ignorewarning' ), array ( 'name' => 'wpUpload', 'id' => 'wpUpload', 'checked' => 'checked' ) ) . ' ' .
  835. Xml::submitButton( wfMsg( 'reuploaddesc' ), array ( 'name' => 'wpReUpload', 'id' => 'wpReUpload' ) ) .
  836. Xml::closeElement( 'form' ) . "\n"
  837. );
  838. }
  839. /**
  840. * Displays the main upload form, optionally with a highlighted
  841. * error message up at the top.
  842. *
  843. * @param string $msg as HTML
  844. * @access private
  845. */
  846. function mainUploadForm( $msg='' ) {
  847. global $wgOut, $wgUser, $wgLang, $wgMaxUploadSize;
  848. global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview;
  849. global $wgRequest, $wgAllowCopyUploads;
  850. global $wgStylePath, $wgStyleVersion;
  851. $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
  852. $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview;
  853. $adc = wfBoolToStr( $useAjaxDestCheck );
  854. $alp = wfBoolToStr( $useAjaxLicensePreview );
  855. $autofill = wfBoolToStr( $this->mDesiredDestName == '' );
  856. $wgOut->addScript( "<script type=\"text/javascript\">
  857. wgAjaxUploadDestCheck = {$adc};
  858. wgAjaxLicensePreview = {$alp};
  859. wgUploadAutoFill = {$autofill};
  860. </script>" );
  861. $wgOut->addScriptFile( 'upload.js' );
  862. $wgOut->addScriptFile( 'edit.js' ); // For <charinsert> support
  863. if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
  864. {
  865. wfDebug( "Hook 'UploadForm:initial' broke output of the upload form\n" );
  866. return false;
  867. }
  868. if( $this->mDesiredDestName ) {
  869. $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
  870. // Show a subtitle link to deleted revisions (to sysops et al only)
  871. if( $title instanceof Title && ( $count = $title->isDeleted() ) > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
  872. $link = wfMsgExt(
  873. $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
  874. array( 'parse', 'replaceafter' ),
  875. $wgUser->getSkin()->makeKnownLinkObj(
  876. SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
  877. wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
  878. )
  879. );
  880. $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
  881. }
  882. // Show the relevant lines from deletion log (for still deleted files only)
  883. if( $title instanceof Title && $title->isDeletedQuick() && !$title->exists() ) {
  884. $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
  885. }
  886. }
  887. $cols = intval($wgUser->getOption( 'cols' ));
  888. if( $wgUser->getOption( 'editwidth' ) ) {
  889. $width = " style=\"width:100%\"";
  890. } else {
  891. $width = '';
  892. }
  893. if ( '' != $msg ) {
  894. $sub = wfMsgHtml( 'uploaderror' );
  895. $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
  896. "<span class='error'>{$msg}</span>\n" );
  897. }
  898. $wgOut->addHTML( '<div id="uploadtext">' );
  899. $wgOut->addWikiMsg( 'uploadtext', $this->mDesiredDestName );
  900. $wgOut->addHTML( "</div>\n" );
  901. # Print a list of allowed file extensions, if so configured. We ignore
  902. # MIME type here, it's incomprehensible to most people and too long.
  903. global $wgCheckFileExtensions, $wgStrictFileExtensions,
  904. $wgFileExtensions, $wgFileBlacklist;
  905. $allowedExtensions = '';
  906. if( $wgCheckFileExtensions ) {
  907. if( $wgStrictFileExtensions ) {
  908. # Everything not permitted is banned
  909. $extensionsList =
  910. '<div id="mw-upload-permitted">' .
  911. wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
  912. "</div>\n";
  913. } else {
  914. # We have to list both preferred and prohibited
  915. $extensionsList =
  916. '<div id="mw-upload-preferred">' .
  917. wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
  918. "</div>\n" .
  919. '<div id="mw-upload-prohibited">' .
  920. wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
  921. "</div>\n";
  922. }
  923. } else {
  924. # Everything is permitted.
  925. $extensionsList = '';
  926. }
  927. # Get the maximum file size from php.ini as $wgMaxUploadSize works for uploads from URL via CURL only
  928. # See http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize for possible values of upload_max_filesize
  929. $val = trim( ini_get( 'upload_max_filesize' ) );
  930. $last = strtoupper( ( substr( $val, -1 ) ) );
  931. switch( $last ) {
  932. case 'G':
  933. $val2 = substr( $val, 0, -1 ) * 1024 * 1024 * 1024;
  934. break;
  935. case 'M':
  936. $val2 = substr( $val, 0, -1 ) * 1024 * 1024;
  937. break;
  938. case 'K':
  939. $val2 = substr( $val, 0, -1 ) * 1024;
  940. break;
  941. default:
  942. $val2 = $val;
  943. }
  944. $val2 = $wgAllowCopyUploads ? min( $wgMaxUploadSize, $val2 ) : $val2;
  945. $maxUploadSize = '<div id="mw-upload-maxfilesize">' .
  946. wfMsgExt( 'upload-maxfilesize', array( 'parseinline', 'escapenoentities' ),
  947. $wgLang->formatSize( $val2 ) ) .
  948. "</div>\n";
  949. $sourcefilename = wfMsgExt( 'sourcefilename', array( 'parseinline', 'escapenoentities' ) );
  950. $destfilename = wfMsgExt( 'destfilename', array( 'parseinline', 'escapenoentities' ) );
  951. $msg = $this->mForReUpload ? 'filereuploadsummary' : 'fileuploadsummary';
  952. $summary = wfMsgExt( $msg, 'parseinline' );
  953. $licenses = new Licenses();
  954. $license = wfMsgExt( 'license', array( 'parseinline' ) );
  955. $nolicense = wfMsgHtml( 'nolicense' );
  956. $licenseshtml = $licenses->getHtml();
  957. $ulb = wfMsgHtml( 'uploadbtn' );
  958. $titleObj = SpecialPage::getTitleFor( 'Upload' );
  959. $encDestName = htmlspecialchars( $this->mDesiredDestName );
  960. $watchChecked = $this->watchCheck() ? 'checked="checked"' : '';
  961. # Re-uploads should not need "file exist already" warnings
  962. $warningChecked = ($this->mIgnoreWarning || $this->mForReUpload) ? 'checked="checked"' : '';
  963. // Prepare form for upload or upload/copy
  964. if( $wgAllowCopyUploads && $wgUser->isAllowed( 'upload_by_url' ) ) {
  965. $filename_form =
  966. "<input type='radio' id='wpSourceTypeFile' name='wpSourceType' value='file' " .
  967. "onchange='toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\")' checked='checked' />" .
  968. "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " .
  969. "onfocus='" .
  970. "toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\");" .
  971. "toggle_element_check(\"wpSourceTypeFile\",\"wpSourceTypeURL\")' " .
  972. "onchange='fillDestFilename(\"wpUploadFile\")' size='60' />" .
  973. wfMsgHTML( 'upload_source_file' ) . "<br/>" .
  974. "<input type='radio' id='wpSourceTypeURL' name='wpSourceType' value='web' " .
  975. "onchange='toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\")' />" .
  976. "<input tabindex='1' type='text' name='wpUploadFileURL' id='wpUploadFileURL' " .
  977. "onfocus='" .
  978. "toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\");" .
  979. "toggle_element_check(\"wpSourceTypeURL\",\"wpSourceTypeFile\")' " .
  980. "onchange='fillDestFilename(\"wpUploadFileURL\")' size='60' disabled='disabled' />" .
  981. wfMsgHtml( 'upload_source_url' ) ;
  982. } else {
  983. $filename_form =
  984. "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " .
  985. ($this->mDesiredDestName?"":"onchange='fillDestFilename(\"wpUploadFile\")' ") .
  986. "size='60' />" .
  987. "<input type='hidden' name='wpSourceType' value='file' />" ;
  988. }
  989. if ( $useAjaxDestCheck ) {
  990. $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp;</td></tr>";
  991. $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"';
  992. } else {
  993. $warningRow = '';
  994. $destOnkeyup = '';
  995. }
  996. $encComment = htmlspecialchars( $this->mComment );
  997. $wgOut->addHTML(
  998. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL(),
  999. 'enctype' => 'multipart/form-data', 'id' => 'mw-upload-form' ) ) .
  1000. Xml::openElement( 'fieldset' ) .
  1001. Xml::element( 'legend', null, wfMsg( 'upload' ) ) .
  1002. Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-upload-table' ) ) .
  1003. "<tr>
  1004. {$this->uploadFormTextTop}
  1005. <td class='mw-label'>
  1006. <label for='wpUploadFile'>{$sourcefilename}</label>
  1007. </td>
  1008. <td class='mw-input'>
  1009. {$filename_form}
  1010. </td>
  1011. </tr>
  1012. <tr>
  1013. <td></td>
  1014. <td>
  1015. {$maxUploadSize}
  1016. {$extensionsList}
  1017. </td>
  1018. </tr>
  1019. <tr>
  1020. <td class='mw-label'>
  1021. <label for='wpDestFile'>{$destfilename}</label>
  1022. </td>
  1023. <td class='mw-input'>"
  1024. );
  1025. if( $this->mForReUpload ) {
  1026. $wgOut->addHTML(
  1027. Xml::hidden( 'wpDestFile', $this->mDesiredDestName, array('id'=>'wpDestFile','tabindex'=>2) ) .
  1028. "<tt>" .
  1029. $encDestName .
  1030. "</tt>"
  1031. );
  1032. }
  1033. else {
  1034. $wgOut->addHTML(
  1035. "<input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='60'
  1036. value=\"{$encDestName}\" onchange='toggleFilenameFiller()' $destOnkeyup />"
  1037. );
  1038. }
  1039. $wgOut->addHTML(
  1040. "</td>
  1041. </tr>
  1042. <tr>
  1043. <td class='mw-label'>
  1044. <label for='wpUploadDescription'>{$summary}</label>
  1045. </td>
  1046. <td class='mw-input'>
  1047. <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6'
  1048. cols='{$cols}'{$width}>$encComment</textarea>
  1049. {$this->uploadFormTextAfterSummary}
  1050. </td>
  1051. </tr>
  1052. <tr>"
  1053. );
  1054. # Re-uploads should not need license info
  1055. if ( !$this->mForReUpload && $licenseshtml != '' ) {
  1056. global $wgStylePath;
  1057. $wgOut->addHTML( "
  1058. <td class='mw-label'>
  1059. <label for='wpLicense'>$license</label>
  1060. </td>
  1061. <td class='mw-input'>
  1062. <select name='wpLicense' id='wpLicense' tabindex='4'
  1063. onchange='licenseSelectorCheck()'>
  1064. <option value=''>$nolicense</option>
  1065. $licenseshtml
  1066. </select>
  1067. </td>
  1068. </tr>
  1069. <tr>"
  1070. );
  1071. if( $useAjaxLicensePreview ) {
  1072. $wgOut->addHTML( "
  1073. <td></td>
  1074. <td id=\"mw-license-preview\"></td>
  1075. </tr>
  1076. <tr>"
  1077. );
  1078. }
  1079. }
  1080. if ( !$this->mForReUpload && $wgUseCopyrightUpload ) {
  1081. $filestatus = wfMsgExt( 'filestatus', 'escapenoentities' );
  1082. $copystatus = htmlspecialchars( $this->mCopyrightStatus );
  1083. $filesource = wfMsgExt( 'filesource', 'escapenoentities' );
  1084. $uploadsource = htmlspecialchars( $this->mCopyrightSource );
  1085. $wgOut->addHTML( "
  1086. <td class='mw-label' style='white-space: nowrap;'>
  1087. <label for='wpUploadCopyStatus'>$filestatus</label></td>
  1088. <td class='mw-input'>
  1089. <input tabindex='5' type='text' name='wpUploadCopyStatus' id='wpUploadCopyStatus'
  1090. value=\"$copystatus\" size='60' />
  1091. </td>
  1092. </tr>
  1093. <tr>
  1094. <td class='mw-label'>
  1095. <label for='wpUploadCopyStatus'>$filesource</label>
  1096. </td>
  1097. <td class='mw-input'>
  1098. <input tabindex='6' type='text' name='wpUploadSource' id='wpUploadCopyStatus'
  1099. value=\"$uploadsource\" size='60' />
  1100. </td>
  1101. </tr>
  1102. <tr>"
  1103. );
  1104. }
  1105. $wgOut->addHTML( "
  1106. <td></td>
  1107. <td>
  1108. <input tabindex='7' type='checkbox' name='wpWatchthis' id='wpWatchthis' $watchChecked value='true' />
  1109. <label for='wpWatchthis'>" . wfMsgHtml( 'watchthisupload' ) . "</label>
  1110. <input tabindex='8' type='checkbox' name='wpIgnoreWarning' id='wpIgnoreWarning' value='true' $warningChecked />
  1111. <label for='wpIgnoreWarning'>" . wfMsgHtml( 'ignorewarnings' ) . "</label>
  1112. </td>
  1113. </tr>
  1114. $warningRow
  1115. <tr>
  1116. <td></td>
  1117. <td class='mw-input'>
  1118. <input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\"" .
  1119. $wgUser->getSkin()->tooltipAndAccesskey( 'upload' ) . " />
  1120. </td>
  1121. </tr>
  1122. <tr>
  1123. <td></td>
  1124. <td class='mw-input'>"
  1125. );
  1126. $wgOut->addHTML( '<div class="mw-editTools">' );
  1127. $wgOut->addWikiMsgArray( 'edittools', array(), array( 'content' ) );
  1128. $wgOut->addHTML( '</div>' );
  1129. $wgOut->addHTML( "
  1130. </td>
  1131. </tr>" .
  1132. Xml::closeElement( 'table' ) .
  1133. Xml::hidden( 'wpDestFileWarningAck', '', array( 'id' => 'wpDestFileWarningAck' ) ) .
  1134. Xml::hidden( 'wpForReUpload', $this->mForReUpload, array( 'id' => 'wpForReUpload' ) ) .
  1135. Xml::closeElement( 'fieldset' ) .
  1136. Xml::closeElement( 'form' )
  1137. );
  1138. $uploadfooter = wfMsgNoTrans( 'uploadfooter' );
  1139. if( $uploadfooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadfooter ) ){
  1140. $wgOut->addWikiText( '<div id="mw-upload-footer-message">' . $uploadfooter . '</div>' );
  1141. }
  1142. }
  1143. /* -------------------------------------------------------------- */
  1144. /**
  1145. * See if we should check the 'watch this page' checkbox on the form
  1146. * based on the user's preferences and whether we're being asked
  1147. * to create a new file or update an existing one.
  1148. *
  1149. * In the case where 'watch edits' is off but 'watch creations' is on,
  1150. * we'll leave the box unchecked.
  1151. *
  1152. * Note that the page target can be changed *on the form*, so our check
  1153. * state can get out of sync.
  1154. */
  1155. function watchCheck() {
  1156. global $wgUser;
  1157. if( $wgUser->getOption( 'watchdefault' ) ) {
  1158. // Watch all edits!
  1159. return true;
  1160. }
  1161. $local = wfLocalFile( $this->mDesiredDestName );
  1162. if( $local && $local->exists() ) {
  1163. // We're uploading a new version of an existing file.
  1164. // No creation, so don't watch it if we're not already.
  1165. return $local->getTitle()->userIsWatching();
  1166. } else {
  1167. // New page should get watched if that's our option.
  1168. return $wgUser->getOption( 'watchcreations' );
  1169. }
  1170. }
  1171. /**
  1172. * Split a file into a base name and all dot-delimited 'extensions'
  1173. * on the end. Some web server configurations will fall back to
  1174. * earlier pseudo-'extensions' to determine type and execute
  1175. * scripts, so the blacklist needs to check them all.
  1176. *
  1177. * @return array
  1178. */
  1179. public function splitExtensions( $filename ) {
  1180. $bits = explode( '.', $filename );
  1181. $basename = array_shift( $bits );
  1182. return array( $basename, $bits );
  1183. }
  1184. /**
  1185. * Perform case-insensitive match against a list of file extensions.
  1186. * Returns true if the extension is in the list.
  1187. *
  1188. * @param string $ext
  1189. * @param array $list
  1190. * @return bool
  1191. */
  1192. function checkFileExtension( $ext, $list ) {
  1193. return in_array( strtolower( $ext ), $list );
  1194. }
  1195. /**
  1196. * Perform case-insensitive match against a list of file extensions.
  1197. * Returns true if any of the extensions are in the list.
  1198. *
  1199. * @param array $ext
  1200. * @param array $list
  1201. * @return bool
  1202. */
  1203. public function checkFileExtensionList( $ext, $list ) {
  1204. foreach( $ext as $e ) {
  1205. if( in_array( strtolower( $e ), $list ) ) {
  1206. return true;
  1207. }
  1208. }
  1209. return false;
  1210. }
  1211. /**
  1212. * Verifies that it's ok to include the uploaded file
  1213. *
  1214. * @param string $tmpfile the full path of the temporary file to verify
  1215. * @param string $extension The filename extension that the file is to be served with
  1216. * @return mixed true of the file is verified, a WikiError object otherwise.
  1217. */
  1218. function verify( $tmpfile, $extension ) {
  1219. #magically determine mime type
  1220. $magic = MimeMagic::singleton();
  1221. $mime = $magic->guessMimeType($tmpfile,false);
  1222. #check mime type, if desired
  1223. global $wgVerifyMimeType;
  1224. if ($wgVerifyMimeType) {
  1225. wfDebug ( "\n\nmime: <$mime> extension: <$extension>\n\n");
  1226. #check mime type against file extension
  1227. if( !self::verifyExtension( $mime, $extension ) ) {
  1228. return new WikiErrorMsg( 'uploadcorrupt' );
  1229. }
  1230. #check mime type blacklist
  1231. global $wgMimeTypeBlacklist;
  1232. if( isset($wgMimeTypeBlacklist) && !is_null($wgMimeTypeBlacklist) ) {
  1233. if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
  1234. return new WikiErrorMsg( 'filetype-badmime', htmlspecialchars( $mime ) );
  1235. }
  1236. # Check IE type
  1237. $fp = fopen( $tmpfile, 'rb' );
  1238. $chunk = fread( $fp, 256 );
  1239. fclose( $fp );
  1240. $extMime = $magic->guessTypesForExtension( $extension );
  1241. $ieTypes = $magic->getIEMimeTypes( $tmpfile, $chunk, $extMime );
  1242. foreach ( $ieTypes as $ieType ) {
  1243. if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
  1244. return new WikiErrorMsg( 'filetype-bad-ie-mime', $ieType );
  1245. }
  1246. }
  1247. }
  1248. }
  1249. #check for htmlish code and javascript
  1250. if( $this->detectScript ( $tmpfile, $mime, $extension ) ) {
  1251. return new WikiErrorMsg( 'uploadscripted' );
  1252. }
  1253. if( $extension == 'svg' || $mime == 'image/svg+xml' ) {
  1254. if( $this->detectScriptInSvg( $tmpfile ) ) {
  1255. return new WikiErrorMsg( 'uploadscripted' );
  1256. }
  1257. }
  1258. /**
  1259. * Scan the uploaded file for viruses
  1260. */
  1261. $virus= $this->detectVirus($tmpfile);
  1262. if ( $virus ) {
  1263. return new WikiErrorMsg( 'uploadvirus', htmlspecialchars($virus) );
  1264. }
  1265. wfDebug( __METHOD__.": all clear; passing.\n" );
  1266. return true;
  1267. }
  1268. /**
  1269. * Checks if the mime type of the uploaded file matches the file extension.
  1270. *
  1271. * @param string $mime the mime type of the uploaded file
  1272. * @param string $extension The filename extension that the file is to be served with
  1273. * @return bool
  1274. */
  1275. static function verifyExtension( $mime, $extension ) {
  1276. $magic = MimeMagic::singleton();
  1277. if ( ! $mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
  1278. if ( ! $magic->isRecognizableExtension( $extension ) ) {
  1279. wfDebug( __METHOD__.": passing file with unknown detected mime type; " .
  1280. "unrecognized extension '$extension', can't verify\n" );
  1281. return true;
  1282. } else {
  1283. wfDebug( __METHOD__.": rejecting file with unknown detected mime type; ".
  1284. "recognized extension '$extension', so probably invalid file\n" );
  1285. return false;
  1286. }
  1287. $match= $magic->isMatchingExtension($extension,$mime);
  1288. if ($match===NULL) {
  1289. wfDebug( __METHOD__.": no file extension known for mime type $mime, passing file\n" );
  1290. return true;
  1291. } elseif ($match===true) {
  1292. wfDebug( __METHOD__.": mime type $mime matches extension $extension, passing file\n" );
  1293. #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
  1294. return true;
  1295. } else {
  1296. wfDebug( __METHOD__.": mime type $mime mismatches file extension $extension, rejecting file\n" );
  1297. return false;
  1298. }
  1299. }
  1300. /**
  1301. * Heuristic for detecting files that *could* contain JavaScript instructions or
  1302. * things that may look like HTML to a browser and are thus
  1303. * potentially harmful. The present implementation will produce false positives in some situations.
  1304. *
  1305. * @param string $file Pathname to the temporary upload file
  1306. * @param string $mime The mime type of the file
  1307. * @param string $extension The extension of the file
  1308. * @return bool true if the file contains something looking like embedded scripts
  1309. */
  1310. function detectScript($file, $mime, $extension) {
  1311. global $wgAllowTitlesInSVG;
  1312. #ugly hack: for text files, always look at the entire file.
  1313. #For binarie field, just check the first K.
  1314. if (strpos($mime,'text/')===0) $chunk = file_get_contents( $file );
  1315. else {
  1316. $fp = fopen( $file, 'rb' );
  1317. $chunk = fread( $fp, 1024 );
  1318. fclose( $fp );
  1319. }
  1320. $chunk= strtolower( $chunk );
  1321. if (!$chunk) return false;
  1322. #decode from UTF-16 if needed (could be used for obfuscation).
  1323. if (substr($chunk,0,2)=="\xfe\xff") $enc= "UTF-16BE";
  1324. elseif (substr($chunk,0,2)=="\xff\xfe") $enc= "UTF-16LE";
  1325. else $enc= NULL;
  1326. if ($enc) $chunk= iconv($enc,"ASCII//IGNORE",$chunk);
  1327. $chunk= trim($chunk);
  1328. #FIXME: convert from UTF-16 if necessarry!
  1329. wfDebug("SpecialUpload::detectScript: checking for embedded scripts and HTML stuff\n");
  1330. #check for HTML doctype
  1331. if (eregi("<!DOCTYPE *X?HTML",$chunk)) return true;
  1332. /**
  1333. * Internet Explorer for Windows performs some really stupid file type
  1334. * autodetection which can cause it to interpret valid image files as HTML
  1335. * and potentially execute JavaScript, creating a cross-site scripting
  1336. * attack vectors.
  1337. *
  1338. * Apple's Safari browser also performs some unsafe file type autodetection
  1339. * which can cause legitimate files to be interpreted as HTML if the
  1340. * web server is not correctly configured to send the right content-type
  1341. * (or if you're really uploading plain text and octet streams!)
  1342. *
  1343. * Returns true if IE is likely to mistake the given file for HTML.
  1344. * Also returns true if Safari would mistake the given file for HTML
  1345. * when served with a generic content-type.
  1346. */
  1347. $tags = array(
  1348. '<a href',
  1349. '<body',
  1350. '<head',
  1351. '<html', #also in safari
  1352. '<img',
  1353. '<pre',
  1354. '<script', #also in safari
  1355. '<table'
  1356. );
  1357. if( ! $wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
  1358. $tags[] = '<title';
  1359. }
  1360. foreach( $tags as $tag ) {
  1361. if( false !== strpos( $chunk, $tag ) ) {
  1362. return true;
  1363. }
  1364. }
  1365. /*
  1366. * look for javascript
  1367. */
  1368. #resolve entity-refs to look at attributes. may be harsh on big files... cache result?
  1369. $chunk = Sanitizer::decodeCharReferences( $chunk );
  1370. #look for script-types
  1371. if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim',$chunk)) return true;
  1372. #look for html-style script-urls
  1373. if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true;
  1374. #look for css-style script-urls
  1375. if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true;
  1376. wfDebug("SpecialUpload::detectScript: no scripts found\n");
  1377. return false;
  1378. }
  1379. function detectScriptInSvg( $filename ) {
  1380. $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
  1381. return $check->filterMatch;
  1382. }
  1383. /**
  1384. * @todo Replace this with a whitelist filter!
  1385. */
  1386. function checkSvgScriptCallback( $element, $attribs ) {
  1387. $stripped = $this->stripXmlNamespace( $element );
  1388. if( $stripped == 'script' ) {
  1389. wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file.\n" );
  1390. return true;
  1391. }
  1392. foreach( $attribs as $attrib => $value ) {
  1393. $stripped = $this->stripXmlNamespace( $attrib );
  1394. if( substr( $stripped, 0, 2 ) == 'on' ) {
  1395. wfDebug( __METHOD__ . ": Found script attribute '$attrib'='value' in uploaded file.\n" );
  1396. return true;
  1397. }
  1398. if( $stripped == 'href' && strpos( strtolower( $value ), 'javascript:' ) !== false ) {
  1399. wfDebug( __METHOD__ . ": Found script href attribute '$attrib'='$value' in uploaded file.\n" );
  1400. return true;
  1401. }
  1402. }
  1403. }
  1404. private function stripXmlNamespace( $name ) {
  1405. // 'http://www.w3.org/2000/svg:script' -> 'script'
  1406. $parts = explode( ':', strtolower( $name ) );
  1407. return array_pop( $parts );
  1408. }
  1409. /**
  1410. * Generic wrapper function for a virus scanner program.
  1411. * This relies on the $wgAntivirus and $wgAntivirusSetup variables.
  1412. * $wgAntivirusRequired may be used to deny upload if the scan fails.
  1413. *
  1414. * @param string $file Pathname to the temporary upload file
  1415. * @return mixed false if not virus is found, NULL if the scan fails or is disabled,
  1416. * or a string containing feedback from the virus scanner if a virus was found.
  1417. * If textual feedback is missing but a virus was found, this function returns true.
  1418. */
  1419. function detectVirus($file) {
  1420. global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
  1421. if ( !$wgAntivirus ) {
  1422. wfDebug( __METHOD__.": virus scanner disabled\n");
  1423. return NULL;
  1424. }
  1425. if ( !$wgAntivirusSetup[$wgAntivirus] ) {
  1426. wfDebug( __METHOD__.": unknown virus scanner: $wgAntivirus\n" );
  1427. $wgOut->wrapWikiMsg( '<div class="error">$1</div>', array( 'virus-badscanner', $wgAntivirus ) );
  1428. return wfMsg('virus-unknownscanner') . " $wgAntivirus";
  1429. }
  1430. # look up scanner configuration
  1431. $command = $wgAntivirusSetup[$wgAntivirus]["command"];
  1432. $exitCodeMap = $wgAntivirusSetup[$wgAntivirus]["codemap"];
  1433. $msgPattern = isset( $wgAntivirusSetup[$wgAntivirus]["messagepattern"] ) ?
  1434. $wgAntivirusSetup[$wgAntivirus]["messagepattern"] : null;
  1435. if ( strpos( $command,"%f" ) === false ) {
  1436. # simple pattern: append file to scan
  1437. $command .= " " . wfEscapeShellArg( $file );
  1438. } else {
  1439. # complex pattern: replace "%f" with file to scan
  1440. $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
  1441. }
  1442. wfDebug( __METHOD__.": running virus scan: $command \n" );
  1443. # execute virus scanner
  1444. $exitCode = false;
  1445. #NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
  1446. # that does not seem to be worth the pain.
  1447. # Ask me (Duesentrieb) about it if it's ever needed.
  1448. $output = array();
  1449. if ( wfIsWindows() ) {
  1450. exec( "$command", $output, $exitCode );
  1451. } else {
  1452. exec( "$command 2>&1", $output, $exitCode );
  1453. }
  1454. # map exit code to AV_xxx constants.
  1455. $mappedCode = $exitCode;
  1456. if ( $exitCodeMap ) {
  1457. if ( isset( $exitCodeMap[$exitCode] ) ) {
  1458. $mappedCode = $exitCodeMap[$exitCode];
  1459. } elseif ( isset( $exitCodeMap["*"] ) ) {
  1460. $mappedCode = $exitCodeMap["*"];
  1461. }
  1462. }
  1463. if ( $mappedCode === AV_SCAN_FAILED ) {
  1464. # scan failed (code was mapped to false by $exitCodeMap)
  1465. wfDebug( __METHOD__.": failed to scan $file (code $exitCode).\n" );
  1466. if ( $wgAntivirusRequired ) {
  1467. return wfMsg('virus-scanfailed', array( $exitCode ) );
  1468. } else {
  1469. return NULL;
  1470. }
  1471. } else if ( $mappedCode === AV_SCAN_ABORTED ) {
  1472. # scan failed because filetype is unknown (probably imune)
  1473. wfDebug( __METHOD__.": unsupported file type $file (code $exitCode).\n" );
  1474. return NULL;
  1475. } else if ( $mappedCode === AV_NO_VIRUS ) {
  1476. # no virus found
  1477. wfDebug( __METHOD__.": file passed virus scan.\n" );
  1478. return false;
  1479. } else {
  1480. $output = join( "\n", $output );
  1481. $output = trim( $output );
  1482. if ( !$output ) {
  1483. $output = true; #if there's no output, return true
  1484. } elseif ( $msgPattern ) {
  1485. $groups = array();
  1486. if ( preg_match( $msgPattern, $output, $groups ) ) {
  1487. if ( $groups[1] ) {
  1488. $output = $groups[1];
  1489. }
  1490. }
  1491. }
  1492. wfDebug( __METHOD__.": FOUND VIRUS! scanner feedback: $output \n" );
  1493. return $output;
  1494. }
  1495. }
  1496. /**
  1497. * Check if the temporary file is MacBinary-encoded, as some uploads
  1498. * from Internet Explorer on Mac OS Classic and Mac OS X will be.
  1499. * If so, the data fork will be extracted to a second temporary file,
  1500. * which will then be checked for validity and either kept or discarded.
  1501. *
  1502. * @access private
  1503. */
  1504. function checkMacBinary() {
  1505. $macbin = new MacBinary( $this->mTempPath );
  1506. if( $macbin->isValid() ) {
  1507. $dataFile = tempnam( wfTempDir(), "WikiMacBinary" );
  1508. $dataHandle = fopen( $dataFile, 'wb' );
  1509. wfDebug( "SpecialUpload::checkMacBinary: Extracting MacBinary data fork to $dataFile\n" );
  1510. $macbin->extractData( $dataHandle );
  1511. $this->mTempPath = $dataFile;
  1512. $this->mFileSize = $macbin->dataForkLength();
  1513. // We'll have to manually remove the new file if it's not kept.
  1514. $this->mRemoveTempFile = true;
  1515. }
  1516. $macbin->close();
  1517. }
  1518. /**
  1519. * If we've modified the upload file we need to manually remove it
  1520. * on exit to clean up.
  1521. * @access private
  1522. */
  1523. function cleanupTempFile() {
  1524. if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
  1525. wfDebug( "SpecialUpload::cleanupTempFile: Removing temporary file {$this->mTempPath}\n" );
  1526. unlink( $this->mTempPath );
  1527. }
  1528. }
  1529. /**
  1530. * Check if there's an overwrite conflict and, if so, if restrictions
  1531. * forbid this user from performing the upload.
  1532. *
  1533. * @return mixed true on success, WikiError on failure
  1534. * @access private
  1535. */
  1536. function checkOverwrite( $name ) {
  1537. $img = wfFindFile( $name );
  1538. $error = '';
  1539. if( $img ) {
  1540. global $wgUser, $wgOut;
  1541. if( $img->isLocal() ) {
  1542. if( !self::userCanReUpload( $wgUser, $img->name ) ) {
  1543. $error = 'fileexists-forbidden';
  1544. }
  1545. } else {
  1546. if( !$wgUser->isAllowed( 'reupload' ) ||
  1547. !$wgUser->isAllowed( 'reupload-shared' ) ) {
  1548. $error = "fileexists-shared-forbidden";
  1549. }
  1550. }
  1551. }
  1552. if( $error ) {
  1553. $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
  1554. return $errorText;
  1555. }
  1556. // Rockin', go ahead and upload
  1557. return true;
  1558. }
  1559. /**
  1560. * Check if a user is the last uploader
  1561. *
  1562. * @param User $user
  1563. * @param string $img, image name
  1564. * @return bool
  1565. */
  1566. public static function userCanReUpload( User $user, $img ) {
  1567. if( $user->isAllowed( 'reupload' ) )
  1568. return true; // non-conditional
  1569. if( !$user->isAllowed( 'reupload-own' ) )
  1570. return false;
  1571. $dbr = wfGetDB( DB_SLAVE );
  1572. $row = $dbr->selectRow('image',
  1573. /* SELECT */ 'img_user',
  1574. /* WHERE */ array( 'img_name' => $img )
  1575. );
  1576. if ( !$row )
  1577. return false;
  1578. return $user->getId() == $row->img_user;
  1579. }
  1580. /**
  1581. * Display an error with a wikitext description
  1582. */
  1583. function showError( $description ) {
  1584. global $wgOut;
  1585. $wgOut->setPageTitle( wfMsg( "internalerror" ) );
  1586. $wgOut->setRobotPolicy( "noindex,nofollow" );
  1587. $wgOut->setArticleRelated( false );
  1588. $wgOut->enableClientCache( false );
  1589. $wgOut->addWikiText( $description );
  1590. }
  1591. /**
  1592. * Get the initial image page text based on a comment and optional file status information
  1593. */
  1594. static function getInitialPageText( $comment, $license, $copyStatus, $source ) {
  1595. global $wgUseCopyrightUpload;
  1596. if ( $wgUseCopyrightUpload ) {
  1597. if ( $license != '' ) {
  1598. $licensetxt = '== ' . wfMsgForContent( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
  1599. }
  1600. $pageText = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $comment . "\n" .
  1601. '== ' . wfMsgForContent ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
  1602. "$licensetxt" .
  1603. '== ' . wfMsgForContent ( 'filesource' ) . " ==\n" . $source ;
  1604. } else {
  1605. if ( $license != '' ) {
  1606. $filedesc = $comment == '' ? '' : '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $comment . "\n";
  1607. $pageText = $filedesc .
  1608. '== ' . wfMsgForContent ( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
  1609. } else {
  1610. $pageText = $comment;
  1611. }
  1612. }
  1613. return $pageText;
  1614. }
  1615. /**
  1616. * If there are rows in the deletion log for this file, show them,
  1617. * along with a nice little note for the user
  1618. *
  1619. * @param OutputPage $out
  1620. * @param string filename
  1621. */
  1622. private function showDeletionLog( $out, $filename ) {
  1623. global $wgUser;
  1624. $loglist = new LogEventsList( $wgUser->getSkin(), $out );
  1625. $pager = new LogPager( $loglist, 'delete', false, $filename );
  1626. if( $pager->getNumRows() > 0 ) {
  1627. $out->addHTML( '<div class="mw-warning-with-logexcerpt">' );
  1628. $out->addWikiMsg( 'upload-wasdeleted' );
  1629. $out->addHTML(
  1630. $loglist->beginLogEventsList() .
  1631. $pager->getBody() .
  1632. $loglist->endLogEventsList()
  1633. );
  1634. $out->addHTML( '</div>' );
  1635. }
  1636. }
  1637. }