Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

piwik.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. function sendHeader($header, $replace = true)
  23. {
  24. headers_sent() || header($head, $replace);
  25. }
  26. function arrayValue($array, $key, $value = null)
  27. {
  28. if (!empty($array[$key])) {
  29. $value = $array[$key];
  30. }
  31. return $value;
  32. }
  33. // DO NOT MODIFY BELOW
  34. // ---------------------------
  35. // 1) PIWIK.JS PROXY: No _GET parameter, we serve the JS file
  36. if (empty($_GET)) {
  37. $modifiedSince = false;
  38. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  39. $modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  40. // strip any trailing data appended to header
  41. if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
  42. $modifiedSince = strtotime(substr($modifiedSince, 0, $semicolon));
  43. }
  44. }
  45. // Re-download the piwik.js once a day maximum
  46. $lastModified = time() - 86400;
  47. // set HTTP response headers
  48. sendHeader('Vary: Accept-Encoding');
  49. // Returns 304 if not modified since
  50. if (!empty($modifiedSince) && $modifiedSince < $lastModified) {
  51. sendHeader(sprintf("%s 304 Not Modified", $_SERVER['SERVER_PROTOCOL']));
  52. } else {
  53. sendHeader('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  54. sendHeader('Content-Type: application/javascript; charset=UTF-8');
  55. if ($piwikJs = file_get_contents($PIWIK_URL . 'piwik.js')) {
  56. echo $piwikJs;
  57. } else {
  58. sendHeader($_SERVER['SERVER_PROTOCOL'] . '505 Internal server error');
  59. }
  60. }
  61. exit;
  62. }
  63. @ini_set('magic_quotes_runtime', 0);
  64. // 2) PIWIK.PHP PROXY: GET parameters found, this is a tracking request, we redirect it to Piwik
  65. $url = sprintf("%spiwik.php?cip=%s&token_auth=%s&", $PIWIK_URL, getVisitIp(), $TOKEN_AUTH);
  66. foreach ($_GET as $key => $value) {
  67. $url .= $key . '=' . urlencode($value) . '&';
  68. }
  69. sendHeader("Content-Type: image/gif");
  70. $stream_options = array('http' => array(
  71. 'user_agent' => arrayValue($_SERVER, 'HTTP_USER_AGENT', ''),
  72. 'header' => sprintf("Accept-Language: %s\r\n", str_replace(array("\n", "\t", "\r"), "", arrayValue($_SERVER, 'HTTP_ACCEPT_LANGUAGE', ''))),
  73. 'timeout' => $timeout
  74. ));
  75. $ctx = stream_context_create($stream_options);
  76. echo file_get_contents($url, 0, $ctx);
  77. function getVisitIp()
  78. {
  79. $matchIp = '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/';
  80. $ipKeys = array(
  81. 'HTTP_X_FORWARDED_FOR',
  82. 'HTTP_CLIENT_IP',
  83. 'HTTP_CF_CONNECTING_IP',
  84. );
  85. foreach($ipKeys as $ipKey) {
  86. if (isset($_SERVER[$ipKey])
  87. && preg_match($matchIp, $_SERVER[$ipKey])) {
  88. return $_SERVER[$ipKey];
  89. }
  90. }
  91. return arrayValue($_SERVER, 'REMOTE_ADDR');
  92. }