/** * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" * will return "example.org" * * @param string $default Default value to return if host unknown * @return string */ public static function getCurrentHost($default = 'unknown') { $hostHeaders = @Piwik_Config::getInstance()->General['proxy_host_headers']; if (!is_array($hostHeaders)) { $hostHeaders = array(); } $host = self::getHost(); $default = Piwik_Common::sanitizeInputValue($host ? $host : $default); return Piwik_IP::getNonProxyIpFromHeader($default, $hostHeaders); }
/** * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" * will return "example.org" * * @param string $default Default value to return if host unknown * @return string */ public static function getCurrentHost($default = 'unknown') { $hostHeaders = @Piwik_Config::getInstance()->General['proxy_host_headers']; if (!is_array($hostHeaders)) { $hostHeaders = array(); } $default = Piwik_Common::sanitizeInputValue($default); if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { $default = Piwik_Common::sanitizeInputValue($_SERVER['HTTP_HOST']); } return Piwik_IP::getNonProxyIpFromHeader($default, $hostHeaders); }
function test_getNonProxyIpFromHeader() { Piwik::createConfigObject(); Piwik_Config::getInstance()->setTestEnvironment(); $saved = $this->saveGlobals(array('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR')); $ips = array('0.0.0.0', '72.14.204.99', '127.0.0.1', '169.254.0.1', '208.80.152.2', '224.0.0.1'); // no proxies foreach ($ips as $ip) { $this->assertEqual(Piwik_IP::getNonProxyIpFromHeader($ip, array()), $ip, $ip); } // 1.1.1.1 is not a trusted proxy $_SERVER['REMOTE_ADDR'] = '1.1.1.1'; foreach ($ips as $ip) { $_SERVER['HTTP_X_FORWARDED_FOR'] = ''; $this->assertEqual(Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), '1.1.1.1', $ip); } // 1.1.1.1 is a trusted proxy $_SERVER['REMOTE_ADDR'] = '1.1.1.1'; foreach ($ips as $ip) { $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip; $this->assertEqual(Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), $ip, $ip); $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4, ' . $ip; $this->assertEqual(Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), $ip, $ip); // misconfiguration $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip . ', 1.1.1.1'; $this->assertEqual(Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), $ip, $ip); } $this->restoreGlobals($saved); }
/** * @group Core * @group IP * @dataProvider getIpTestData */ public function testGetNonProxyIpFromHeader3($ip) { // 1.1.1.1 is a trusted proxy $_SERVER['REMOTE_ADDR'] = '1.1.1.1'; $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip; $this->assertEquals($ip, Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR'))); $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4, ' . $ip; $this->assertEquals($ip, Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR'))); // misconfiguration $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip . ', 1.1.1.1'; $this->assertEquals($ip, Piwik_IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR'))); }
/** * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" * will return "example.org" * * @param string $default Default value to return if host unknown * @return string */ public static function getCurrentHost($default = 'unknown') { $hostHeaders = null; try { $config = Zend_Registry::get('config'); } catch (Exception $e) { $config = false; } if ($config !== false && $config->General->proxy_host_headers) { $hostHeaders = $config->General->proxy_host_headers->toArray(); } if (!is_array($hostHeaders)) { $hostHeaders = array(); } $default = Piwik_Common::sanitizeInputValue($default); if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { $default = Piwik_Common::sanitizeInputValue($_SERVER['HTTP_HOST']); } // temporary workaround for #1331 if (!method_exists('Piwik_IP', 'getNonProxyIpFromHeader')) { return $default; } return Piwik_IP::getNonProxyIpFromHeader($default, $hostHeaders); }