PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/misc/proxy-hide-piwik-url/piwik.php

https://github.com/CodeYellowBV/piwik
PHP | 93 lines | 56 code | 12 blank | 25 comment | 10 complexity | 89e6c03349f16e3d288f0d65b093510a MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  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. // -----
  10. // Important: read the instructions in README.md or at:
  11. // https://github.com/piwik/piwik/tree/master/misc/proxy-hide-piwik-url#piwik-proxy-hide-url
  12. // -----
  13. // Edit the line below, and replace http://your-piwik-domain.example.org/piwik/
  14. // with your Piwik URL ending with a slash.
  15. // This URL will never be revealed to visitors or search engines.
  16. $PIWIK_URL = 'http://your-piwik-domain.example.org/piwik/';
  17. // Edit the line below, and replace xyz by the token_auth for the user "UserTrackingAPI"
  18. // which you created when you followed instructions above.
  19. $TOKEN_AUTH = 'xyz';
  20. // Maximum time, in seconds, to wait for the Piwik server to return the 1*1 GIF
  21. $timeout = 5;
  22. // DO NOT MODIFY BELOW
  23. // ---------------------------
  24. // 1) PIWIK.JS PROXY: No _GET parameter, we serve the JS file
  25. if (empty($_GET)) {
  26. $modifiedSince = false;
  27. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  28. $modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  29. // strip any trailing data appended to header
  30. if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
  31. $modifiedSince = strtotime(substr($modifiedSince, 0, $semicolon));
  32. }
  33. }
  34. // Re-download the piwik.js once a day maximum
  35. $lastModified = time() - 86400;
  36. // set HTTP response headers
  37. header('Vary: Accept-Encoding');
  38. // Returns 304 if not modified since
  39. if (!empty($modifiedSince) && $modifiedSince < $lastModified) {
  40. header(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
  41. } else {
  42. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  43. @header('Content-Type: application/javascript; charset=UTF-8');
  44. if ($piwikJs = file_get_contents($PIWIK_URL . 'piwik.js')) {
  45. echo $piwikJs;
  46. } else {
  47. header($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
  48. }
  49. }
  50. exit;
  51. }
  52. @ini_set('magic_quotes_runtime', 0);
  53. // 2) PIWIK.PHP PROXY: GET parameters found, this is a tracking request, we redirect it to Piwik
  54. $url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $TOKEN_AUTH);
  55. foreach ($_GET as $key => $value) {
  56. $url .= $key . '=' . urlencode($value) . '&';
  57. }
  58. header("Content-Type: image/gif");
  59. $stream_options = array('http' => array(
  60. 'user_agent' => @$_SERVER['HTTP_USER_AGENT'],
  61. 'header' => sprintf("Accept-Language: %s\r\n", @str_replace(array("\n", "\t", "\r"), "", $_SERVER['HTTP_ACCEPT_LANGUAGE'])),
  62. 'timeout' => $timeout
  63. ));
  64. $ctx = stream_context_create($stream_options);
  65. echo file_get_contents($url, 0, $ctx);
  66. function getVisitIp()
  67. {
  68. $matchIp = '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/';
  69. $ipKeys = array(
  70. 'HTTP_X_FORWARDED_FOR',
  71. 'HTTP_CLIENT_IP',
  72. 'HTTP_CF_CONNECTING_IP',
  73. );
  74. foreach($ipKeys as $ipKey) {
  75. if (isset($_SERVER[$ipKey])
  76. && preg_match($matchIp, $_SERVER[$ipKey])) {
  77. return $_SERVER[$ipKey];
  78. }
  79. }
  80. return @$_SERVER['REMOTE_ADDR'];
  81. }