Skip to content

Commit f7a642c

Browse files
author
joseph lenton
committed
JosephLenton#9 remains silent if returning non-HTML
Sometimes people request JS and CSS through PHP scripts, in a non-ajaxy way. For example linking to a PHP script in a script tag. This change will make PHP Error be silent if it believes it is on one of those occasions. It also changes the 'getResponseHeaders' method, to merge the apache and non-apache headers together. It seems one can contain info the other does not.
1 parent 2bd72a3 commit f7a642c

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed

src/php_error.php

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,47 @@ class ErrorHandler
520520
'is_subclass_of'
521521
);
522522

523+
/**
524+
* When returning values, if a mime type is set,
525+
* then PHP Error should only output if the mime type
526+
* is one of these.
527+
*/
528+
private static $ALLOWED_RETURN_MIME_TYPES = array(
529+
'text/html',
530+
'application/xhtml+xml'
531+
);
532+
533+
/**
534+
* This attempts to state if this is *not* a PHP request,
535+
* but it cannot say if it *is* a PHP request. It achieves
536+
* this by looking for a mime type.
537+
*
538+
* For example if the mime type is JavaScript, then we
539+
* know it's not PHP. However there is no "yes, this is
540+
* definitely a normal HTML response" flag we can check.
541+
*/
542+
private static function isNonPHPRequest() {
543+
/*
544+
* Check if we are a mime type that isn't allowed.
545+
*
546+
* Anything other than 'text/html' or similar will cause
547+
* this to turn off.
548+
*/
549+
$response = ErrorHandler::getResponseHeaders();
550+
551+
foreach ( $response as $key => $value ) {
552+
if ( strtolower($key) === 'content-type' ) {
553+
if ( ! in_array($value, ErrorHandler::$ALLOWED_RETURN_MIME_TYPES) ) {
554+
return true;
555+
}
556+
557+
break;
558+
}
559+
}
560+
561+
return false;
562+
}
563+
523564
/**
524565
* Looks up a description for the symbol given,
525566
* and if found, it is returned.
@@ -979,22 +1020,26 @@ private static function getRequestHeaders() {
9791020
}
9801021

9811022
private static function getResponseHeaders() {
982-
if ( function_exists('apache_response_headers') ) {
983-
return apache_response_headers();
984-
} else {
985-
$headers = array();
1023+
$headers = function_exists('apache_response_headers') ?
1024+
apache_response_headers() :
1025+
array() ;
9861026

987-
if ( function_exists('headers_list') ) {
988-
$hList = headers_list();
1027+
/*
1028+
* Merge the headers_list into apache_response_headers.
1029+
*
1030+
* This is because sometimes things are in one, which are
1031+
* not present in the other.
1032+
*/
1033+
if ( function_exists('headers_list') ) {
1034+
$hList = headers_list();
9891035

990-
foreach ($hList as $header) {
991-
$header = explode(":", $header);
992-
$headers[ array_shift($header) ] = trim( implode(":", $header) );
993-
}
1036+
foreach ($hList as $header) {
1037+
$header = explode(":", $header);
1038+
$headers[ array_shift($header) ] = trim( implode(":", $header) );
9941039
}
995-
996-
return $headers;
9971040
}
1041+
1042+
return $headers;
9981043
}
9991044

10001045
public static function identifyTypeHTML( $arg, $recurseLevels=1 ) {
@@ -1370,7 +1415,11 @@ private function endBuffer() {
13701415
global $_php_error_is_ini_enabled;
13711416

13721417
if ( $_php_error_is_ini_enabled ) {
1373-
if ( !$this->isAjax && $this->catchAjaxErrors ) {
1418+
if (
1419+
!$this->isAjax &&
1420+
$this->catchAjaxErrors &&
1421+
!ErrorHandler::isNonPHPRequest()
1422+
) {
13741423
$content = ob_get_contents();
13751424
$handlers = ob_list_handlers();
13761425

@@ -2219,8 +2268,20 @@ public function reportError( $code, $message, $errLine, $errFile, $ex=null ) {
22192268

22202269
$this->logError( $message, $errFile, $errLine, $ex );
22212270

2271+
/**
2272+
* It runs if:
2273+
* - it is globally enabled
2274+
* - this error handler is enabled
2275+
* - we believe it is a regular html request, or ajax
2276+
*/
22222277
global $_php_error_is_ini_enabled;
2223-
if ( $_php_error_is_ini_enabled ) {
2278+
if (
2279+
$_php_error_is_ini_enabled &&
2280+
$this->isOn() && (
2281+
$this->isAjax ||
2282+
!ErrorHandler::isNonPHPRequest()
2283+
)
2284+
) {
22242285
$root = $this->applicationRoot;
22252286

22262287
list( $ex, $stackTrace, $code, $errFile, $errLine ) =

0 commit comments

Comments
 (0)