|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Piwik - Open source web analytics |
| 3 | + * Piwik - free/libre analytics platform |
4 | 4 | * Piwik Proxy Hide URL |
5 | 5 | * |
6 | 6 | * @link http://piwik.org/faq/how-to/#faq_132 |
7 | 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
8 | 8 | */ |
9 | 9 |
|
| 10 | +if (file_exists('config.php')) { |
| 11 | + include 'config.php'; |
| 12 | +} |
| 13 | + |
10 | 14 | // ----- |
11 | 15 | // Important: read the instructions in README.md or at: |
12 | 16 | // https://github.com/piwik/piwik/tree/master/misc/proxy-hide-piwik-url#piwik-proxy-hide-url |
|
15 | 19 | // Edit the line below, and replace http://your-piwik-domain.example.org/piwik/ |
16 | 20 | // with your Piwik URL ending with a slash. |
17 | 21 | // 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 | +} |
19 | 25 |
|
20 | 26 | // Edit the line below, and replace xyz by the token_auth for the user "UserTrackingAPI" |
21 | 27 | // which you created when you followed instructions above. |
22 | | -$TOKEN_AUTH = 'xyz'; |
| 28 | +if (! isset($TOKEN_AUTH)) { |
| 29 | + $TOKEN_AUTH = 'xyz'; |
| 30 | +} |
23 | 31 |
|
24 | 32 | // 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 | +} |
26 | 41 |
|
| 42 | +function arrayValue($array, $key, $value = null) |
| 43 | +{ |
| 44 | + if (!empty($array[$key])) { |
| 45 | + $value = $array[$key]; |
| 46 | + } |
| 47 | + return $value; |
| 48 | +} |
27 | 49 |
|
28 | 50 | // DO NOT MODIFY BELOW |
29 | 51 | // --------------------------- |
|
34 | 56 | $modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE']; |
35 | 57 | // strip any trailing data appended to header |
36 | 58 | if (false !== ($semicolon = strpos($modifiedSince, ';'))) { |
37 | | - $modifiedSince = strtotime(substr($modifiedSince, 0, $semicolon)); |
| 59 | + $modifiedSince = substr($modifiedSince, 0, $semicolon); |
38 | 60 | } |
| 61 | + $modifiedSince = strtotime($modifiedSince); |
39 | 62 | } |
40 | 63 | // Re-download the piwik.js once a day maximum |
41 | 64 | $lastModified = time() - 86400; |
42 | 65 |
|
43 | 66 | // set HTTP response headers |
44 | | - header('Vary: Accept-Encoding'); |
| 67 | + sendHeader('Vary: Accept-Encoding'); |
45 | 68 |
|
46 | 69 | // 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'])); |
49 | 72 | } 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')) { |
53 | 76 | echo $piwikJs; |
54 | 77 | } else { |
55 | | - header($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error'); |
| 78 | + sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error'); |
56 | 79 | } |
57 | 80 | } |
58 | 81 | exit; |
59 | 82 | } |
60 | 83 |
|
| 84 | +@ini_set('magic_quotes_runtime', 0); |
| 85 | + |
61 | 86 | // 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 | + |
63 | 89 | foreach ($_GET as $key => $value) { |
64 | | - $url .= $key . '=' . urlencode($value) . '&'; |
| 90 | + $url .= urlencode($key ). '=' . urlencode($value) . '&'; |
65 | 91 | } |
66 | | -header("Content-Type: image/gif"); |
| 92 | +sendHeader("Content-Type: image/gif"); |
67 | 93 | $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', ''))), |
70 | 96 | 'timeout' => $timeout |
71 | 97 | )); |
72 | 98 | $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