Skip to content

Commit 51494aa

Browse files
committed
changed piwik-proxy.php file to latest one from https://github.com/piwik/tracker-proxy/blob/master/piwik.php
changed searched variable name back to $PIWIK_URL to avoid modifying proxy file variable names.
1 parent 9be8300 commit 51494aa

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

upload/admin/controller/module/piwik.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function index() {
1616
$path_to_file = implode("/",explode("/", DIR_APPLICATION, -2)) . "/piwik-proxy.php";
1717
if (file_exists($path_to_file)) {
1818
$file_contents = file_get_contents($path_to_file);
19-
$file_contents = preg_replace('/\$ANALYTICS_URL = \'.{1,512}?\';/', '$ANALYTICS_URL = \'http://' . $this->request->post['piwik_analytics_url'] . '\';', $file_contents, 1);
19+
$file_contents = preg_replace('/\$PIWIK_URL = \'.{1,512}?\';/', '$PIWIK_URL = \'http://' . $this->request->post['piwik_analytics_url'] . '\';', $file_contents, 1);
2020
$file_contents = preg_replace('/\$TOKEN_AUTH = \'[a-z0-9]{1,32}\';/', '$TOKEN_AUTH = \'' . $this->request->post['piwik_token_auth'] . '\';', $file_contents, 1);
2121
file_put_contents($path_to_file,$file_contents);
2222
}

upload/piwik-proxy.php

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22
/**
3-
* Piwik - Open source web analytics
3+
* Piwik - free/libre analytics platform
44
* Piwik Proxy Hide URL
55
*
66
* @link http://piwik.org/faq/how-to/#faq_132
77
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
88
*/
99

10+
if (file_exists('config.php')) {
11+
include 'config.php';
12+
}
13+
1014
// -----
1115
// Important: read the instructions in README.md or at:
1216
// https://github.com/piwik/piwik/tree/master/misc/proxy-hide-piwik-url#piwik-proxy-hide-url
@@ -15,15 +19,33 @@
1519
// Edit the line below, and replace http://your-piwik-domain.example.org/piwik/
1620
// with your Piwik URL ending with a slash.
1721
// This URL will never be revealed to visitors or search engines.
18-
$ANALYTICS_URL = 'http://your-piwik-domain.example.org/piwik/';
22+
if (! isset($PIWIK_URL)) {
23+
$PIWIK_URL = 'http://your-piwik-domain.example.org/piwik/';
24+
}
1925

2026
// Edit the line below, and replace xyz by the token_auth for the user "UserTrackingAPI"
2127
// which you created when you followed instructions above.
22-
$TOKEN_AUTH = 'xyz';
28+
if (! isset($TOKEN_AUTH)) {
29+
$TOKEN_AUTH = 'xyz';
30+
}
2331

2432
// Maximum time, in seconds, to wait for the Piwik server to return the 1*1 GIF
25-
$timeout = 5;
33+
if (! isset($timeout)) {
34+
$timeout = 5;
35+
}
36+
37+
function sendHeader($header, $replace = true)
38+
{
39+
headers_sent() || header($header, $replace);
40+
}
2641

42+
function arrayValue($array, $key, $value = null)
43+
{
44+
if (!empty($array[$key])) {
45+
$value = $array[$key];
46+
}
47+
return $value;
48+
}
2749

2850
// DO NOT MODIFY BELOW
2951
// ---------------------------
@@ -34,40 +56,79 @@
3456
$modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
3557
// strip any trailing data appended to header
3658
if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
37-
$modifiedSince = strtotime(substr($modifiedSince, 0, $semicolon));
59+
$modifiedSince = substr($modifiedSince, 0, $semicolon);
3860
}
61+
$modifiedSince = strtotime($modifiedSince);
3962
}
4063
// Re-download the piwik.js once a day maximum
4164
$lastModified = time() - 86400;
4265

4366
// set HTTP response headers
44-
header('Vary: Accept-Encoding');
67+
sendHeader('Vary: Accept-Encoding');
4568

4669
// Returns 304 if not modified since
47-
if (!empty($modifiedSince) && $modifiedSince < $lastModified) {
48-
header(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
70+
if (!empty($modifiedSince) && $modifiedSince > $lastModified) {
71+
sendHeader(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
4972
} else {
50-
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
51-
@header('Content-Type: application/javascript; charset=UTF-8');
52-
if ($piwikJs = file_get_contents($ANALYTICS_URL . 'piwik.js')) {
73+
sendHeader('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
74+
sendHeader('Content-Type: application/javascript; charset=UTF-8');
75+
if ($piwikJs = file_get_contents($PIWIK_URL . 'piwik.js')) {
5376
echo $piwikJs;
5477
} else {
55-
header($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
78+
sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
5679
}
5780
}
5881
exit;
5982
}
6083

84+
@ini_set('magic_quotes_runtime', 0);
85+
6186
// 2) PIWIK.PHP PROXY: GET parameters found, this is a tracking request, we redirect it to Piwik
62-
$url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $ANALYTICS_URL, @$_SERVER['REMOTE_ADDR'], $TOKEN_AUTH);
87+
$url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $TOKEN_AUTH);
88+
6389
foreach ($_GET as $key => $value) {
64-
$url .= $key . '=' . urlencode($value) . '&';
90+
$url .= urlencode($key ). '=' . urlencode($value) . '&';
6591
}
66-
header("Content-Type: image/gif");
92+
sendHeader("Content-Type: image/gif");
6793
$stream_options = array('http' => array(
68-
'user_agent' => @$_SERVER['HTTP_USER_AGENT'],
69-
'header' => sprintf("Accept-Language: %s\r\n", @str_replace(array("\n", "\t", "\r"), "", $_SERVER['HTTP_ACCEPT_LANGUAGE'])),
94+
'user_agent' => arrayValue($_SERVER, 'HTTP_USER_AGENT', ''),
95+
'header' => sprintf("Accept-Language: %s\r\n", str_replace(array("\n", "\t", "\r"), "", arrayValue($_SERVER, 'HTTP_ACCEPT_LANGUAGE', ''))),
7096
'timeout' => $timeout
7197
));
7298
$ctx = stream_context_create($stream_options);
73-
echo file_get_contents($url, 0, $ctx);
99+
100+
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
101+
102+
// PHP 5.2 breaks with the new 204 status code so we force returning the image every time
103+
echo file_get_contents($url . '&send_image=1', 0, $ctx);
104+
105+
} else {
106+
107+
// PHP 5.3 and above
108+
$content = file_get_contents($url, 0, $ctx);
109+
110+
// Forward the HTTP response code
111+
if (!headers_sent() && isset($http_response_header[0])) {
112+
header($http_response_header[0]);
113+
}
114+
115+
echo $content;
116+
117+
}
118+
119+
function getVisitIp()
120+
{
121+
$matchIp = '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/';
122+
$ipKeys = array(
123+
'HTTP_X_FORWARDED_FOR',
124+
'HTTP_CLIENT_IP',
125+
'HTTP_CF_CONNECTING_IP',
126+
);
127+
foreach($ipKeys as $ipKey) {
128+
if (isset($_SERVER[$ipKey])
129+
&& preg_match($matchIp, $_SERVER[$ipKey])) {
130+
return $_SERVER[$ipKey];
131+
}
132+
}
133+
return arrayValue($_SERVER, 'REMOTE_ADDR');
134+
}

0 commit comments

Comments
 (0)