From a9fb8c7b89def45a3754fb383ce3e76488ed7156 Mon Sep 17 00:00:00 2001 From: KyleK Date: Sun, 14 Sep 2014 14:18:01 +0200 Subject: Replace header() with Common::sendHeader() when possible --- misc/others/geoipUpdateRows.php | 4 +--- misc/proxy-hide-piwik-url/piwik.php | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) (limited to 'misc') diff --git a/misc/others/geoipUpdateRows.php b/misc/others/geoipUpdateRows.php index df480d395d..b90a2d376c 100755 --- a/misc/others/geoipUpdateRows.php +++ b/misc/others/geoipUpdateRows.php @@ -89,9 +89,7 @@ if (!Common::isPhpCliMode()) { function geoipUpdateError($message) { Log::error($message); - if (!Common::isPhpCliMode()) { - @header('HTTP/1.1 500 Internal Server Error', $replace = true, $responseCode = 500); - } + Common::sendHeader('HTTP/1.1 500 Internal Server Error', $replace = true, $responseCode = 500); exit; } diff --git a/misc/proxy-hide-piwik-url/piwik.php b/misc/proxy-hide-piwik-url/piwik.php index 846a82313b..19493aeccc 100644 --- a/misc/proxy-hide-piwik-url/piwik.php +++ b/misc/proxy-hide-piwik-url/piwik.php @@ -24,6 +24,19 @@ $TOKEN_AUTH = 'xyz'; // Maximum time, in seconds, to wait for the Piwik server to return the 1*1 GIF $timeout = 5; +function sendHeader($header, $replace = true) +{ + headers_sent() || header($head, $replace); +} + +function arrayValue($array, $key, $value = null) +{ + if (!empty($array[$key])) { + $value = $array[$key]; + } + return $value; +} + // DO NOT MODIFY BELOW // --------------------------- // 1) PIWIK.JS PROXY: No _GET parameter, we serve the JS file @@ -40,18 +53,18 @@ if (empty($_GET)) { $lastModified = time() - 86400; // set HTTP response headers - header('Vary: Accept-Encoding'); + sendHeader('Vary: Accept-Encoding'); // Returns 304 if not modified since if (!empty($modifiedSince) && $modifiedSince < $lastModified) { - header(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL'])); + sendHeader(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL'])); } else { - header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); - @header('Content-Type: application/javascript; charset=UTF-8'); + sendHeader('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + sendHeader('Content-Type: application/javascript; charset=UTF-8'); if ($piwikJs = file_get_contents($PIWIK_URL . 'piwik.js')) { echo $piwikJs; } else { - header($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error'); + sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error'); } } exit; @@ -65,10 +78,10 @@ $url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $T foreach ($_GET as $key => $value) { $url .= $key . '=' . urlencode($value) . '&'; } -header("Content-Type: image/gif"); +sendHeader("Content-Type: image/gif"); $stream_options = array('http' => array( - 'user_agent' => @$_SERVER['HTTP_USER_AGENT'], - 'header' => sprintf("Accept-Language: %s\r\n", @str_replace(array("\n", "\t", "\r"), "", $_SERVER['HTTP_ACCEPT_LANGUAGE'])), + 'user_agent' => arrayValue($_SERVER, 'HTTP_USER_AGENT', ''), + 'header' => sprintf("Accept-Language: %s\r\n", str_replace(array("\n", "\t", "\r"), "", arrayValue($_SERVER, 'HTTP_ACCEPT_LANGUAGE', ''))), 'timeout' => $timeout )); $ctx = stream_context_create($stream_options); @@ -88,5 +101,5 @@ function getVisitIp() return $_SERVER[$ipKey]; } } - return @$_SERVER['REMOTE_ADDR']; + return arrayValue($_SERVER, 'REMOTE_ADDR'); } -- cgit v1.2.3