Skip to content

Commit 30e06ed

Browse files
authored
Merge pull request #79 from braekling/master
Proxy fix added
2 parents 1c7cd2c + 7704fed commit 30e06ed

File tree

3 files changed

+139
-136
lines changed

3 files changed

+139
-136
lines changed

proxy/matomo.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Piwik - free/libre analytics platform
4+
* Piwik Proxy Hide URL
5+
*
6+
* @link http://piwik.org/faq/how-to/#faq_132
7+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
if (file_exists('config.php')) {
10+
if (file_exists('config.local.php')) {
11+
include 'config.local.php';
12+
}
13+
include 'config.php';
14+
}
15+
// -----
16+
// Important: read the instructions in README.md or at:
17+
// https://github.com/piwik/piwik/tree/master/misc/proxy-hide-piwik-url#piwik-proxy-hide-url
18+
// -----
19+
// Edit the line below, and replace http://your-piwik-domain.example.org/piwik/
20+
// with your Piwik URL ending with a slash.
21+
// This URL will never be revealed to visitors or search engines.
22+
if (! isset($PIWIK_URL)) {
23+
$PIWIK_URL = 'http://your-piwik-domain.example.org/piwik/';
24+
}
25+
// Edit the line below, and replace xyz by the token_auth for the user "UserTrackingAPI"
26+
// which you created when you followed instructions above.
27+
if (! isset($TOKEN_AUTH)) {
28+
$TOKEN_AUTH = 'xyz';
29+
}
30+
// Maximum time, in seconds, to wait for the Piwik server to return the 1*1 GIF
31+
if (! isset($timeout)) {
32+
$timeout = 5;
33+
}
34+
function sendHeader($header, $replace = true)
35+
{
36+
headers_sent() || header($header, $replace);
37+
}
38+
function arrayValue($array, $key, $value = null)
39+
{
40+
if (!empty($array[$key])) {
41+
$value = $array[$key];
42+
}
43+
return $value;
44+
}
45+
function getContents($useCurl, $url, $options = false)
46+
{
47+
if (!$useCurl && ini_get('allow_url_fopen')) {
48+
$ctx = ($options?stream_context_create($options):NULL);
49+
return file_get_contents($url, 0, $ctx);
50+
} elseif ($useCurl && function_exists('curl_version'))
51+
return piwikFileGetContentsCurl($url, $options);
52+
else return 'Neither url_fopen nor cURL is available. Please enable at least one of them.';
53+
}
54+
function piwikFileGetContentsCurl($url, $options)
55+
{
56+
$ch = curl_init();
57+
curl_setopt($ch, CURLOPT_HEADER, 0);
58+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59+
curl_setopt($ch, CURLOPT_USERAGENT, $options['http']['user_agent']);
60+
curl_setopt($ch, CURLOPT_HTTPHEADER, array($options['http']['header']));
61+
curl_setopt($ch, CURLOPT_TIMEOUT, $options['http']['timeout']);
62+
curl_setopt($ch, CURLOPT_URL, $url);
63+
$data = curl_exec($ch);
64+
curl_close($ch);
65+
return $data;
66+
}
67+
// DO NOT MODIFY BELOW
68+
// ---------------------------
69+
// 1) PIWIK.JS PROXY: No _GET parameter, we serve the JS file
70+
if (empty($_GET)) {
71+
$modifiedSince = false;
72+
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
73+
$modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
74+
// strip any trailing data appended to header
75+
if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
76+
$modifiedSince = substr($modifiedSince, 0, $semicolon);
77+
}
78+
$modifiedSince = strtotime($modifiedSince);
79+
}
80+
// Re-download the piwik.js once a day maximum
81+
$lastModified = time() - 86400;
82+
// set HTTP response headers
83+
sendHeader('Vary: Accept-Encoding');
84+
// Returns 304 if not modified since
85+
if (!empty($modifiedSince) && $modifiedSince > $lastModified) {
86+
sendHeader(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
87+
} else {
88+
sendHeader('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
89+
sendHeader('Content-Type: application/javascript; charset=UTF-8');
90+
if ($piwikJs = getContents($useCurl, $PIWIK_URL . 'piwik.js')) {
91+
echo $piwikJs;
92+
} else {
93+
sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
94+
}
95+
}
96+
exit;
97+
}
98+
@ini_set('magic_quotes_runtime', 0);
99+
// 2) PIWIK.PHP PROXY: GET parameters found, this is a tracking request, we redirect it to Piwik
100+
$url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $TOKEN_AUTH);
101+
foreach ($_GET as $key => $value) {
102+
$url .= urlencode($key ). '=' . urlencode($value) . '&';
103+
}
104+
sendHeader("Content-Type: image/gif");
105+
$stream_options = array('http' => array(
106+
'user_agent' => arrayValue($_SERVER, 'HTTP_USER_AGENT', ''),
107+
'header' => sprintf("Accept-Language: %s\r\n", str_replace(array("\n", "\t", "\r"), "", arrayValue($_SERVER, 'HTTP_ACCEPT_LANGUAGE', ''))),
108+
'timeout' => $timeout
109+
));
110+
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
111+
// PHP 5.2 breaks with the new 204 status code so we force returning the image every time
112+
echo getContents($useCurl, $url . '&send_image=1', $stream_options);
113+
} else {
114+
// PHP 5.3 and above
115+
$content = getContents($useCurl, $url, $stream_options);
116+
// Forward the HTTP response code
117+
if (!headers_sent() && isset($http_response_header[0])) {
118+
header($http_response_header[0]);
119+
}
120+
echo $content;
121+
}
122+
function getVisitIp()
123+
{
124+
$matchIp = '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/';
125+
$ipKeys = array(
126+
'HTTP_X_FORWARDED_FOR',
127+
'HTTP_CLIENT_IP',
128+
'HTTP_CF_CONNECTING_IP',
129+
);
130+
foreach($ipKeys as $ipKey) {
131+
if (isset($_SERVER[$ipKey])
132+
&& preg_match($matchIp, $_SERVER[$ipKey])) {
133+
return $_SERVER[$ipKey];
134+
}
135+
}
136+
return arrayValue($_SERVER, 'REMOTE_ADDR');
137+
}

proxy/piwik.php

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,2 @@
11
<?php
2-
/**
3-
* Piwik - free/libre analytics platform
4-
* Piwik Proxy Hide URL
5-
*
6-
* @link http://piwik.org/faq/how-to/#faq_132
7-
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8-
*/
9-
if (file_exists('config.php')) {
10-
if (file_exists('config.local.php')) {
11-
include 'config.local.php';
12-
}
13-
include 'config.php';
14-
}
15-
// -----
16-
// Important: read the instructions in README.md or at:
17-
// https://github.com/piwik/piwik/tree/master/misc/proxy-hide-piwik-url#piwik-proxy-hide-url
18-
// -----
19-
// Edit the line below, and replace http://your-piwik-domain.example.org/piwik/
20-
// with your Piwik URL ending with a slash.
21-
// This URL will never be revealed to visitors or search engines.
22-
if (! isset($PIWIK_URL)) {
23-
$PIWIK_URL = 'http://your-piwik-domain.example.org/piwik/';
24-
}
25-
// Edit the line below, and replace xyz by the token_auth for the user "UserTrackingAPI"
26-
// which you created when you followed instructions above.
27-
if (! isset($TOKEN_AUTH)) {
28-
$TOKEN_AUTH = 'xyz';
29-
}
30-
// Maximum time, in seconds, to wait for the Piwik server to return the 1*1 GIF
31-
if (! isset($timeout)) {
32-
$timeout = 5;
33-
}
34-
function sendHeader($header, $replace = true)
35-
{
36-
headers_sent() || header($header, $replace);
37-
}
38-
function arrayValue($array, $key, $value = null)
39-
{
40-
if (!empty($array[$key])) {
41-
$value = $array[$key];
42-
}
43-
return $value;
44-
}
45-
function getContents($useCurl, $url, $options = false)
46-
{
47-
if (!$useCurl && ini_get('allow_url_fopen')) {
48-
$ctx = ($options?stream_context_create($options):NULL);
49-
return file_get_contents($url, 0, $ctx);
50-
} elseif ($useCurl && function_exists('curl_version'))
51-
return piwikFileGetContentsCurl($url, $options);
52-
else return 'Neither url_fopen nor cURL is available. Please enable at least one of them.';
53-
}
54-
function piwikFileGetContentsCurl($url, $options)
55-
{
56-
$ch = curl_init();
57-
curl_setopt($ch, CURLOPT_HEADER, 0);
58-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59-
curl_setopt($ch, CURLOPT_USERAGENT, $options['http']['user_agent']);
60-
curl_setopt($ch, CURLOPT_HTTPHEADER, array($options['http']['header']));
61-
curl_setopt($ch, CURLOPT_TIMEOUT, $options['http']['timeout']);
62-
curl_setopt($ch, CURLOPT_URL, $url);
63-
$data = curl_exec($ch);
64-
curl_close($ch);
65-
return $data;
66-
}
67-
// DO NOT MODIFY BELOW
68-
// ---------------------------
69-
// 1) PIWIK.JS PROXY: No _GET parameter, we serve the JS file
70-
if (empty($_GET)) {
71-
$modifiedSince = false;
72-
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
73-
$modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
74-
// strip any trailing data appended to header
75-
if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
76-
$modifiedSince = substr($modifiedSince, 0, $semicolon);
77-
}
78-
$modifiedSince = strtotime($modifiedSince);
79-
}
80-
// Re-download the piwik.js once a day maximum
81-
$lastModified = time() - 86400;
82-
// set HTTP response headers
83-
sendHeader('Vary: Accept-Encoding');
84-
// Returns 304 if not modified since
85-
if (!empty($modifiedSince) && $modifiedSince > $lastModified) {
86-
sendHeader(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
87-
} else {
88-
sendHeader('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
89-
sendHeader('Content-Type: application/javascript; charset=UTF-8');
90-
if ($piwikJs = getContents($useCurl, $PIWIK_URL . 'piwik.js')) {
91-
echo $piwikJs;
92-
} else {
93-
sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
94-
}
95-
}
96-
exit;
97-
}
98-
@ini_set('magic_quotes_runtime', 0);
99-
// 2) PIWIK.PHP PROXY: GET parameters found, this is a tracking request, we redirect it to Piwik
100-
$url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $TOKEN_AUTH);
101-
foreach ($_GET as $key => $value) {
102-
$url .= urlencode($key ). '=' . urlencode($value) . '&';
103-
}
104-
sendHeader("Content-Type: image/gif");
105-
$stream_options = array('http' => array(
106-
'user_agent' => arrayValue($_SERVER, 'HTTP_USER_AGENT', ''),
107-
'header' => sprintf("Accept-Language: %s\r\n", str_replace(array("\n", "\t", "\r"), "", arrayValue($_SERVER, 'HTTP_ACCEPT_LANGUAGE', ''))),
108-
'timeout' => $timeout
109-
));
110-
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
111-
// PHP 5.2 breaks with the new 204 status code so we force returning the image every time
112-
echo getContents($useCurl, $url . '&send_image=1', $stream_options);
113-
} else {
114-
// PHP 5.3 and above
115-
$content = getContents($useCurl, $url, $stream_options);
116-
// Forward the HTTP response code
117-
if (!headers_sent() && isset($http_response_header[0])) {
118-
header($http_response_header[0]);
119-
}
120-
echo $content;
121-
}
122-
function getVisitIp()
123-
{
124-
$matchIp = '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/';
125-
$ipKeys = array(
126-
'HTTP_X_FORWARDED_FOR',
127-
'HTTP_CLIENT_IP',
128-
'HTTP_CF_CONNECTING_IP',
129-
);
130-
foreach($ipKeys as $ipKey) {
131-
if (isset($_SERVER[$ipKey])
132-
&& preg_match($matchIp, $_SERVER[$ipKey])) {
133-
return $_SERVER[$ipKey];
134-
}
135-
}
136-
return arrayValue($_SERVER, 'REMOTE_ADDR');
137-
}
2+
require_once('matomo.php');

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Add WP-Matomo to your /wp-content/plugins folder and enable it as [Network Plugi
144144
= 1.0.22 =
145145
* Bugfix: Innocraft cloud URL *.matomo.cloud will work
146146
* Option to configure filter_limit parameter (see expert settings)
147+
* Replaced piwik.php proxy script by matomo.php proxy script
147148

148149
= 1.0.21 =
149150
* Bugfix: Get HTTP mode working again

0 commit comments

Comments
 (0)