Fix URL used to redirect
[lhc/web/wiklou.git] / includes / SpecialUpload.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'Image.php' );
12
13 /**
14 * Entry point
15 */
16 function wfSpecialUpload() {
17 global $wgRequest;
18 $form = new UploadForm( $wgRequest );
19 $form->execute();
20 }
21
22 /**
23 *
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class UploadForm {
28 /**#@+
29 * @access private
30 */
31 var $mUploadAffirm, $mUploadFile, $mUploadDescription, $mIgnoreWarning;
32 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
33 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
34 var $mOname, $mSessionKey;
35 /**#@-*/
36
37 /**
38 * Constructor : initialise object
39 * Get data POSTed through the form and assign them to the object
40 * @param $request Data posted.
41 */
42 function UploadForm( &$request ) {
43 if( !$request->wasPosted() ) {
44 # GET requests just give the main form; no data.
45 return;
46 }
47
48 $this->mUploadAffirm = $request->getCheck( 'wpUploadAffirm' );
49 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning');
50 $this->mReUpload = $request->getCheck( 'wpReUpload' );
51 $this->mUpload = $request->getCheck( 'wpUpload' );
52
53 $this->mUploadDescription = $request->getText( 'wpUploadDescription' );
54 $this->mUploadCopyStatus = $request->getText( 'wpUploadCopyStatus' );
55 $this->mUploadSource = $request->getText( 'wpUploadSource');
56
57 $this->mAction = $request->getVal( 'action' );
58
59 $this->mSessionKey = $request->getInt( 'wpSessionKey' );
60 if( !empty( $this->mSessionKey ) &&
61 isset( $_SESSION['wsUploadData'][$this->mSessionKey] ) ) {
62 /**
63 * Confirming a temporarily stashed upload.
64 * We don't want path names to be forged, so we keep
65 * them in the session on the server and just give
66 * an opaque key to the user agent.
67 */
68 $data = $_SESSION['wsUploadData'][$this->mSessionKey];
69 $this->mUploadTempName = $data['mUploadTempName'];
70 $this->mUploadSize = $data['mUploadSize'];
71 $this->mOname = $data['mOname'];
72 } else {
73 /**
74 *Check for a newly uploaded file.
75 */
76 $this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
77 $this->mUploadSize = $request->getFileSize( 'wpUploadFile' );
78 $this->mOname = $request->getFileName( 'wpUploadFile' );
79 $this->mSessionKey = false;
80 }
81 }
82
83 /**
84 * Start doing stuff
85 * @access public
86 */
87 function execute() {
88 global $wgUser, $wgOut;
89 global $wgDisableUploads;
90
91 /** Show an error message if file upload is disabled */
92 if( $wgDisableUploads ) {
93 $wgOut->addWikiText( wfMsg( 'uploaddisabled' ) );
94 return;
95 }
96
97 /** Various rights checks */
98 if( ( $wgUser->getID() == 0 )
99 OR $wgUser->isBlocked() ) {
100 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
101 return;
102 }
103 if( wfReadOnly() ) {
104 $wgOut->readOnlyPage();
105 return;
106 }
107
108 if( $this->mReUpload ) {
109 $this->unsaveUploadedFile();
110 $this->mainUploadForm();
111 } else if ( 'submit' == $this->mAction || $this->mUpload ) {
112 $this->processUpload();
113 } else {
114 $this->mainUploadForm();
115 }
116 }
117
118 /* -------------------------------------------------------------- */
119
120 /**
121 * Really do the upload
122 * Checks are made in SpecialUpload::execute()
123 * @access private
124 */
125 function processUpload() {
126 global $wgUser, $wgOut, $wgLang, $wgContLang;
127 global $wgUploadDirectory;
128 global $wgUseCopyrightUpload, $wgCheckCopyrightUpload;
129
130 /**
131 * If there was no filename or a zero size given, give up quick.
132 */
133 if( ( trim( $this->mOname ) == '' ) || empty( $this->mUploadSize ) ) {
134 return $this->mainUploadForm('<li>'.wfMsg( 'emptyfile' ).'</li>');
135 }
136
137 /**
138 * When using detailed copyright, if user filled field, assume he
139 * confirmed the upload
140 */
141 if ( $wgUseCopyrightUpload ) {
142 $this->mUploadAffirm = true;
143 if( $wgCheckCopyrightUpload &&
144 ( trim( $this->mUploadCopyStatus ) == '' ||
145 trim( $this->mUploadSource ) == '' ) ) {
146 $this->mUploadAffirm = false;
147 }
148 }
149
150 /** User need to confirm his upload */
151 if( !$this->mUploadAffirm ) {
152 $this->mainUploadForm( wfMsg( 'noaffirmation' ) );
153 return;
154 }
155
156 # Chop off any directories in the given filename
157 $basename = basename( $this->mOname );
158
159 /**
160 * We'll want to blacklist against *any* 'extension', and use
161 * only the final one for the whitelist.
162 */
163 list( $partname, $ext ) = $this->splitExtensions( $basename );
164 if( count( $ext ) ) {
165 $finalExt = $ext[count( $ext ) - 1];
166 } else {
167 $finalExt = '';
168 }
169 $fullExt = implode( '.', $ext );
170
171 if ( strlen( $partname ) < 3 ) {
172 $this->mainUploadForm( wfMsg( 'minlength' ) );
173 return;
174 }
175
176 /**
177 * Filter out illegal characters, and try to make a legible name
178 * out of it. We'll strip some silently that Title would die on.
179 */
180 $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $basename );
181 $nt = Title::newFromText( $filtered );
182 if( is_null( $nt ) ) {
183 return $this->uploadError( wfMsg( 'illegalfilename', htmlspecialchars( $filtered ) ) );
184 }
185 $nt =& Title::makeTitle( NS_IMAGE, $nt->getDBkey() );
186 $this->mUploadSaveName = $nt->getDBkey();
187
188 /**
189 * If the image is protected, non-sysop users won't be able
190 * to modify it by uploading a new revision.
191 */
192 if( !$nt->userCanEdit() ) {
193 return $this->uploadError( wfMsg( 'protectedpage' ) );
194 }
195
196 /* Don't allow users to override the blacklist */
197 global $wgStrictFileExtensions;
198 global $wgFileExtensions, $wgFileBlacklist;
199 if( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) ||
200 ($wgStrictFileExtensions &&
201 !$this->checkFileExtension( $finalExt, $wgFileExtensions ) ) ) {
202 return $this->uploadError( wfMsg( 'badfiletype', htmlspecialchars( $fullExt ) ) );
203 }
204
205 /**
206 * Look at the contents of the file; if we can recognize the
207 * type but it's corrupt or data of the wrong type, we should
208 * probably not accept it.
209 */
210 if( !$this->verify( $this->mUploadTempName, $finalExt ) ) {
211 return $this->uploadError( wfMsg( 'uploadcorrupt' ) );
212 }
213
214 /**
215 * Check for non-fatal conditions
216 */
217 if ( ! $this->mIgnoreWarning ) {
218 $warning = '';
219 if( $this->mUploadSaveName != ucfirst( $filtered ) ) {
220 $warning .= '<li>'.wfMsg( 'badfilename', htmlspecialchars( $this->mUploadSaveName ) ).'</li>';
221 }
222
223 global $wgCheckFileExtensions;
224 if ( $wgCheckFileExtensions ) {
225 if ( ! $this->checkFileExtension( $finalExt, $wgFileExtensions ) ) {
226 $warning .= '<li>'.wfMsg( 'badfiletype', htmlspecialchars( $fullExt ) ).'</li>';
227 }
228 }
229
230 global $wgUploadSizeWarning;
231 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
232 $warning .= '<li>'.wfMsg( 'largefile' ).'</li>';
233 }
234 if ( $this->mUploadSize == 0 ) {
235 $warning .= '<li>'.wfMsg( 'emptyfile' ).'</li>';
236 }
237
238 if( $nt->getArticleID() ) {
239 global $wgUser;
240 $sk = $wgUser->getSkin();
241 $dlink = $sk->makeKnownLinkObj( $nt );
242 $warning .= '<li>'.wfMsg( 'fileexists', $dlink ).'</li>';
243 }
244
245 if( $warning != '' ) {
246 /**
247 * Stash the file in a temporary location; the user can choose
248 * to let it through and we'll complete the upload then.
249 */
250 return $this->uploadWarning($warning);
251 }
252 }
253
254 /**
255 * Try actually saving the thing...
256 * It will show an error form on failure.
257 */
258 if( $this->saveUploadedFile( $this->mUploadSaveName,
259 $this->mUploadTempName,
260 !empty( $this->mSessionKey ) ) ) {
261 /**
262 * Update the upload log and create the description page
263 * if it's a new file.
264 */
265 wfRecordUpload( $this->mUploadSaveName,
266 $this->mUploadOldVersion,
267 $this->mUploadSize,
268 $this->mUploadDescription,
269 $this->mUploadCopyStatus,
270 $this->mUploadSource );
271 $this->showSuccess();
272 }
273 }
274
275 /**
276 * Move the uploaded file from its temporary location to the final
277 * destination. If a previous version of the file exists, move
278 * it into the archive subdirectory.
279 *
280 * @todo If the later save fails, we may have disappeared the original file.
281 *
282 * @param string $saveName
283 * @param string $tempName full path to the temporary file
284 * @param bool $useRename if true, doesn't check that the source file
285 * is a PHP-managed upload temporary
286 */
287 function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
288 global $wgUploadDirectory, $wgOut;
289
290 $dest = wfImageDir( $saveName );
291 $archive = wfImageArchiveDir( $saveName );
292 $this->mSavedFile = "{$dest}/{$saveName}";
293
294 if( is_file( $this->mSavedFile ) ) {
295 $this->mUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
296
297 if( !rename( $this->mSavedFile, "${archive}/{$this->mUploadOldVersion}" ) ) {
298 $wgOut->fileRenameError( $this->mSavedFile,
299 "${archive}/{$this->mUploadOldVersion}" );
300 return false;
301 }
302 } else {
303 $this->mUploadOldVersion = '';
304 }
305
306 if( $useRename ) {
307 if( !rename( $tempName, $this->mSavedFile ) ) {
308 $wgOut->fileCopyError( $tempName, $this->mSavedFile );
309 return false;
310 }
311 } else {
312 if( !move_uploaded_file( $tempName, $this->mSavedFile ) ) {
313 $wgOut->fileCopyError( $tempName, $this->mSavedFile );
314 return false;
315 }
316 }
317 chmod( $this->mSavedFile, 0644 );
318 return true;
319 }
320
321 /**
322 * Stash a file in a temporary directory for later processing
323 * after the user has confirmed it.
324 *
325 * If the user doesn't explicitly cancel or accept, these files
326 * can accumulate in the temp directory.
327 *
328 * @param string $saveName - the destination filename
329 * @param string $tempName - the source temporary file to save
330 * @return string - full path the stashed file, or false on failure
331 * @access private
332 */
333 function saveTempUploadedFile( $saveName, $tempName ) {
334 global $wgOut;
335
336 $archive = wfImageArchiveDir( $saveName, 'temp' );
337 $stash = $archive . '/' . gmdate( "YmdHis" ) . '!' . $saveName;
338
339 if ( !move_uploaded_file( $tempName, $stash ) ) {
340 $wgOut->fileCopyError( $tempName, $stash );
341 return false;
342 }
343
344 return $stash;
345 }
346
347 /**
348 * Stash a file in a temporary directory for later processing,
349 * and save the necessary descriptive info into the session.
350 * Returns a key value which will be passed through a form
351 * to pick up the path info on a later invocation.
352 *
353 * @return int
354 * @access private
355 */
356 function stashSession() {
357 $stash = $this->saveTempUploadedFile(
358 $this->mUploadSaveName, $this->mUploadTempName );
359
360 if( !$stash ) {
361 # Couldn't save the file.
362 return false;
363 }
364
365 $key = mt_rand( 0, 0x7fffffff );
366 $_SESSION['wsUploadData'][$key] = array(
367 'mUploadTempName' => $stash,
368 'mUploadSize' => $this->mUploadSize,
369 'mOname' => $this->mOname );
370 return $key;
371 }
372
373 /**
374 * Remove a temporarily kept file stashed by saveTempUploadedFile().
375 * @access private
376 */
377 function unsaveUploadedFile() {
378 if ( ! @unlink( $this->mUploadTempName ) ) {
379 $wgOut->fileDeleteError( $this->mUploadTempName );
380 }
381 }
382
383 /* -------------------------------------------------------------- */
384
385 /**
386 * Show some text and linkage on successful upload.
387 * @access private
388 */
389 function showSuccess() {
390 global $wgUser, $wgOut, $wgContLang;
391
392 $sk = $wgUser->getSkin();
393 $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
394 $dname = $wgContLang->getNsText( Namespace::getImage() ) . ':'.$this->mUploadSaveName;
395 $dlink = $sk->makeKnownLink( $dname, $dname );
396
397 $wgOut->addHTML( '<h2>' . wfMsg( 'successfulupload' ) . "</h2>\n" );
398 $text = wfMsg( 'fileuploaded', $ilink, $dlink );
399 $wgOut->addHTML( '<p>'.$text."\n" );
400 $wgOut->returnToMain( false );
401 }
402
403 /**
404 * @param string $error as HTML
405 * @access private
406 */
407 function uploadError( $error ) {
408 global $wgOut;
409 $sub = wfMsg( 'uploadwarning' );
410 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
411 $wgOut->addHTML( "<h4 class='error'>{$error}</h4>\n" );
412 }
413
414 /**
415 * There's something wrong with this file, not enough to reject it
416 * totally but we require manual intervention to save it for real.
417 * Stash it away, then present a form asking to confirm or cancel.
418 *
419 * @param string $warning as HTML
420 * @access private
421 */
422 function uploadWarning( $warning ) {
423 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
424 global $wgUseCopyrightUpload;
425
426 $this->mSessionKey = $this->stashSession();
427 if( !$this->mSessionKey ) {
428 # Couldn't save file; an error has been displayed so let's go.
429 return;
430 }
431
432 $sub = wfMsg( 'uploadwarning' );
433 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
434 $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br/>\n" );
435
436 $save = wfMsg( 'savefile' );
437 $reupload = wfMsg( 'reupload' );
438 $iw = wfMsg( 'ignorewarning' );
439 $reup = wfMsg( 'reuploaddesc' );
440 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
441 $action = $titleObj->escapeLocalURL( 'action=submit' );
442
443 if ( $wgUseCopyrightUpload )
444 {
445 $copyright = "
446 <input type='hidden' name=\"wpUploadCopyStatus\" value=\"" . htmlspecialchars( $this->mUploadCopyStatus ) . "\" />
447 <input type='hidden' name=\"wpUploadSource\" value=\"" . htmlspecialchars( $this->mUploadSource ) . "\" />
448 ";
449 } else {
450 $copyright = "";
451 }
452
453 $wgOut->addHTML( "
454 <form id=\"uploadwarning\" method=\"post\" enctype=\"multipart/form-data\"
455 action=\"{$action}\">
456 <input type=hidden name=\"wpUploadAffirm\" value=\"1\" />
457 <input type=hidden name=\"wpIgnoreWarning\" value=\"1\" />
458 <input type=hidden name=\"wpSessionKey\" value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
459 <input type=hidden name=\"wpUploadDescription\" value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
460 {$copyright}
461 <table border='0'><tr>
462 <tr><td align='right'>
463 <input tabindex='2' type='submit' name=\"wpUpload\" value=\"{$save}\" />
464 </td><td align='left'>{$iw}</td></tr>
465 <tr><td align='right'>
466 <input tabindex='2' type='submit' name=\"wpReUpload\" value=\"{$reupload}\" />
467 </td><td align='left'>{$reup}</td></tr></table></form>\n" );
468 }
469
470 /**
471 * Displays the main upload form, optionally with a highlighted
472 * error message up at the top.
473 *
474 * @param string $msg as HTML
475 * @access private
476 */
477 function mainUploadForm( $msg='' ) {
478 global $wgOut, $wgUser, $wgLang, $wgUploadDirectory, $wgRequest;
479 global $wgUseCopyrightUpload;
480
481 if ( '' != $msg ) {
482 $sub = wfMsg( 'uploaderror' );
483 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
484 "<h4 class='error'>{$msg}</h4>\n" );
485 } else {
486 $sub = wfMsg( 'uploadfile' );
487 $wgOut->addHTML( "<h2>{$sub}</h2>\n" );
488 }
489 $wgOut->addWikiText( wfMsg( 'uploadtext' ) );
490 $sk = $wgUser->getSkin();
491
492 $fn = wfMsg( 'filename' );
493 $fd = wfMsg( 'filedesc' );
494 $ulb = wfMsg( 'uploadbtn' );
495
496 $clink = $sk->makeKnownLink( wfMsgForContent( 'copyrightpage' ),
497 wfMsg( 'copyrightpagename' ) );
498 $ca = wfMsg( 'affirmation', $clink );
499 $iw = wfMsg( 'ignorewarning' );
500
501 $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' );
502 $action = $titleObj->escapeLocalURL();
503
504 $source = "
505 <td align='right'>
506 <input tabindex='3' type='checkbox' name=\"wpUploadAffirm\" value=\"1\" id=\"wpUploadAffirm\" />
507 </td><td align='left'><label for=\"wpUploadAffirm\">{$ca}</label></td>
508 " ;
509 if ( $wgUseCopyrightUpload )
510 {
511 $source = "
512 <td align='right' nowrap='nowrap'>" . wfMsg ( 'filestatus' ) . ":</td>
513 <td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
514 htmlspecialchars($this->mUploadCopyStatus). "\" size='40' /></td>
515 </tr><tr>
516 <td align='right'>". wfMsg ( 'filesource' ) . ":</td>
517 <td><input tabindex='4' type='text' name=\"wpUploadSource\" value=\"" .
518 htmlspecialchars($this->mUploadSource). "\" size='40' /></td>
519 " ;
520 }
521
522 $wgOut->addHTML( "
523 <form id=\"upload\" method=\"post\" enctype=\"multipart/form-data\"
524 action=\"{$action}\">
525 <table border='0'><tr>
526 <td align='right'>{$fn}:</td><td align='left'>
527 <input tabindex='1' type='file' name=\"wpUploadFile\" size='40' />
528 </td></tr><tr>
529 <td align='right'>{$fd}:</td><td align='left'>
530 <input tabindex='2' type='text' name=\"wpUploadDescription\" value=\""
531 . htmlspecialchars( $this->mUploadDescription ) . "\" size='40' />
532 </td></tr><tr>
533 {$source}
534 </tr>
535 <tr><td></td><td align='left'>
536 <input tabindex='5' type='submit' name=\"wpUpload\" value=\"{$ulb}\" />
537 </td></tr></table></form>\n" );
538 }
539
540 /* -------------------------------------------------------------- */
541
542 /**
543 * Split a file into a base name and all dot-delimited 'extensions'
544 * on the end. Some web server configurations will fall back to
545 * earlier pseudo-'extensions' to determine type and execute
546 * scripts, so the blacklist needs to check them all.
547 *
548 * @return array
549 */
550 function splitExtensions( $filename ) {
551 $bits = explode( '.', $filename );
552 $basename = array_shift( $bits );
553 return array( $basename, $bits );
554 }
555
556 /**
557 * Perform case-insensitive match against a list of file extensions.
558 * Returns true if the extension is in the list.
559 *
560 * @param string $ext
561 * @param array $list
562 * @return bool
563 */
564 function checkFileExtension( $ext, $list ) {
565 return in_array( strtolower( $ext ), $list );
566 }
567
568 /**
569 * Perform case-insensitive match against a list of file extensions.
570 * Returns true if any of the extensions are in the list.
571 *
572 * @param array $ext
573 * @param array $list
574 * @return bool
575 */
576 function checkFileExtensionList( $ext, $list ) {
577 foreach( $ext as $e ) {
578 if( in_array( strtolower( $e ), $list ) ) {
579 return true;
580 }
581 }
582 return false;
583 }
584
585 /**
586 * Returns false if the file is of a known type but can't be recognized,
587 * indicating a corrupt file.
588 * Returns true otherwise; unknown file types are not checked if given
589 * with an unrecognized extension.
590 *
591 * @param string $tmpfile Pathname to the temporary upload file
592 * @param string $extension The filename extension that the file is to be served with
593 * @return bool
594 */
595 function verify( $tmpfile, $extension ) {
596 if( $this->triggersIEbug( $tmpfile ) ) {
597 return false;
598 }
599
600 $fname = 'SpecialUpload::verify';
601 $mergeExtensions = array(
602 'jpg' => 'jpeg',
603 'tif' => 'tiff' );
604 $extensionTypes = array(
605 # See http://www.php.net/getimagesize
606 1 => 'gif',
607 2 => 'jpeg',
608 3 => 'png',
609 4 => 'swf',
610 5 => 'psd',
611 6 => 'bmp',
612 7 => 'tiff',
613 8 => 'tiff',
614 9 => 'jpc',
615 10 => 'jp2',
616 11 => 'jpx',
617 12 => 'jb2',
618 13 => 'swc',
619 14 => 'iff',
620 15 => 'wbmp',
621 16 => 'xbm' );
622
623 $extension = strtolower( $extension );
624 if( isset( $mergeExtensions[$extension] ) ) {
625 $extension = $mergeExtensions[$extension];
626 }
627 wfDebug( "$fname: Testing file '$tmpfile' with given extension '$extension'\n" );
628
629 if( !in_array( $extension, $extensionTypes ) ) {
630 # Not a recognized image type. We don't know how to verify these.
631 # They're allowed by policy or they wouldn't get this far, so we'll
632 # let them slide for now.
633 wfDebug( "$fname: Unknown extension; passing.\n" );
634 return true;
635 }
636
637 $data = @getimagesize( $tmpfile );
638 if( false === $data ) {
639 # Didn't recognize the image type.
640 # Either the image is corrupt or someone's slipping us some
641 # bogus data such as HTML+JavaScript trying to take advantage
642 # of an Internet Explorer security flaw.
643 wfDebug( "$fname: getimagesize() doesn't recognize the file; rejecting.\n" );
644 return false;
645 }
646
647 $imageType = $data[2];
648 if( !isset( $extensionTypes[$imageType] ) ) {
649 # Now we're kind of confused. Perhaps new image types added
650 # to PHP's support that we don't know about.
651 # We'll let these slide for now.
652 wfDebug( "$fname: getimagesize() knows the file, but we don't recognize the type; passing.\n" );
653 return true;
654 }
655
656 $ext = strtolower( $extension );
657 if( $extension != $extensionTypes[$imageType] ) {
658 # The given filename extension doesn't match the
659 # file type. Probably just a mistake, but it's a stupid
660 # one and we shouldn't let it pass. KILL THEM!
661 wfDebug( "$fname: file extension does not match recognized type; rejecting.\n" );
662 return false;
663 }
664
665 wfDebug( "$fname: all clear; passing.\n" );
666 return true;
667 }
668
669 /**
670 * Internet Explorer for Windows performs some really stupid file type
671 * autodetection which can cause it to interpret valid image files as HTML
672 * and potentially execute JavaScript, creating a cross-site scripting
673 * attack vectors.
674 *
675 * Returns true if IE is likely to mistake the given file for HTML.
676 *
677 * @param string $filename
678 * @return bool
679 */
680 function triggersIEbug( $filename ) {
681 $file = fopen( $filename, 'rb' );
682 $chunk = strtolower( fread( $file, 200 ) );
683 fclose( $file );
684
685 $tags = array( '<html', '<head', '<body', '<script' );
686 foreach( $tags as $tag ) {
687 if( false !== strpos( $chunk, $tag ) ) {
688 return true;
689 }
690 }
691 return false;
692 }
693 }
694 ?>