function getIp() { if (isset($this->details['location_ip'])) { return Piwik_IP::N2P($this->details['location_ip']); } return false; }
/** * Logs the IntranetSubNetwork in the log_visit table */ public function logIntranetSubNetworkInfo($notification) { $visitorInfo =& $notification->getNotificationObject(); $ip = Piwik_IP::N2P($visitorInfo['location_ip']); // by default, we want the network name to be the IP address: $networkName = $ip; /** ********************************************************************************************* ****************** adopt the following lines according to your subnets ********************** **/ // Some default subnets: if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('0.0.0.0/0'))) { $networkName = 'Global IPv4'; } // all IPv4 addresses if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('::/0'))) { $networkName = 'Global IPv6'; } // IPv6 addresses if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('::ffff:0:0/96'))) { $networkName = 'Global IPv4'; } // IPv4 mapped IPv6 addresses // You may include your custom subnets: //if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('141.2.0.0/16'))) { $networkName = 'University Frankfurt'; } //if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('192.0.2.0/24'))) { $networkName = 'TEST-NET'; } //if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('198.51.100.0/24'))) { $networkName = 'TEST-NET-2'; } //if (Piwik_IP::isIpInRange($visitorInfo['location_ip'], array('2001:db8::/33', // '2001:db8:8000::/33'))) { $networkName = 'Doc-IPv6'; } /** ******************* end adopt here to your subnets ***************************************** ********************************************************************************************* **/ // add the IntranetSubNetwork value in the table log_visit $visitorInfo['location_IntranetSubNetwork'] = substr($networkName, 0, 100); }
/** * Logs the provider in the log_visit table */ public function logProviderInfo($notification) { $visitorInfo =& $notification->getNotificationObject(); $ip = Piwik_IP::N2P($visitorInfo['location_ip']); // In case the IP was anonymized, we should not continue since the DNS reverse lookup will fail and this will slow down tracking if (substr($ip, -2, 2) == '.0') { printDebug("IP Was anonymized so we skip the Provider DNS reverse lookup..."); return; } $hostname = $this->getHost($ip); $hostnameExtension = $this->getCleanHostname($hostname); // add the provider value in the table log_visit $visitorInfo['location_provider'] = $hostnameExtension; $visitorInfo['location_provider'] = substr($visitorInfo['location_provider'], 0, 100); // improve the country using the provider extension if valid $hostnameDomain = substr($hostnameExtension, 1 + strrpos($hostnameExtension, '.')); if ($hostnameDomain == 'uk') { $hostnameDomain = 'gb'; } if (array_key_exists($hostnameDomain, Piwik_Common::getCountriesList())) { $visitorInfo['location_country'] = $hostnameDomain; } }
/** * Returns the hostname given the internal representation of the * IP address * * @param string $ip Internal representation of IP address in binary-safe string * @return string hostname (or human-readable IP address) */ private function getHost($ip) { return trim(strtolower(@Piwik_IP::getHostByAddr(Piwik_IP::N2P($ip)))); }
/** * Get the start and end IP addresses for an IP address range * * @param string $ipRange IP address range in presentation format * @return array|false Array( low, high ) IP addresses in presentation format; or false if error */ public function getIpsForRange($ipRange) { $range = Piwik_IP::getIpsForRange($ipRange); if($range === false) { return false; } return array( Piwik_IP::N2P($range[0]), Piwik_IP::N2P($range[1]) ); }
/** * @group Core * @group IP * @dataProvider getIpsInRangeData */ public function testIsIpInRange($range, $test) { foreach ($test as $ip => $expected) { // range as a string $this->assertEquals($expected, Piwik_IP::isIpInRange(Piwik_IP::P2N($ip), array($range)), "{$ip} in {$range}"); // range as an array(low, high) $aRange = Piwik_IP::getIpsForRange($range); $aRange[0] = Piwik_IP::N2P($aRange[0]); $aRange[1] = Piwik_IP::N2P($aRange[1]); $this->assertEquals($expected, Piwik_IP::isIpInRange(Piwik_IP::P2N($ip), array($aRange)), "{$ip} in {$range}"); } }
function test_isIpInRange() { $tests = array('192.168.1.10' => array('192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => false, '::ffff:192.168.1.10' => false), '::ffff:192.168.1.10' => array('::ffff:192.168.1.9' => false, '::ffff:192.168.1.10' => true, '::ffff:c0a8:010a' => true, '0000:0000:0000:0000:0000:ffff:c0a8:010a' => true, '::ffff:192.168.1.11' => false, '192.168.1.10' => false), '192.168.1.10/32' => array('192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => false), '192.168.1.10/31' => array('192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => true, '192.168.1.12' => false), '192.168.1.128/25' => array('192.168.1.127' => false, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false), '192.168.1.10/24' => array('192.168.0.255' => false, '192.168.1.0' => true, '192.168.1.1' => true, '192.168.1.2' => true, '192.168.1.3' => true, '192.168.1.4' => true, '192.168.1.7' => true, '192.168.1.8' => true, '192.168.1.15' => true, '192.168.1.16' => true, '192.168.1.31' => true, '192.168.1.32' => true, '192.168.1.63' => true, '192.168.1.64' => true, '192.168.1.127' => true, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false), '192.168.1.*' => array('192.168.0.255' => false, '192.168.1.0' => true, '192.168.1.1' => true, '192.168.1.2' => true, '192.168.1.3' => true, '192.168.1.4' => true, '192.168.1.7' => true, '192.168.1.8' => true, '192.168.1.15' => true, '192.168.1.16' => true, '192.168.1.31' => true, '192.168.1.32' => true, '192.168.1.63' => true, '192.168.1.64' => true, '192.168.1.127' => true, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false)); // testing with a single range foreach ($tests as $range => $test) { foreach ($test as $ip => $expected) { // range as a string $this->assertEqual(Piwik_IP::isIpInRange(Piwik_IP::P2N($ip), array($range)), $expected, "{$ip} in {$range}"); // range as an array(low, high) $aRange = Piwik_IP::getIpsForRange($range); $aRange[0] = Piwik_IP::N2P($aRange[0]); $aRange[1] = Piwik_IP::N2P($aRange[1]); $this->assertEqual(Piwik_IP::isIpInRange(Piwik_IP::P2N($ip), array($aRange)), $expected, "{$ip} in {$range}"); } } }
$rows = Piwik_FetchAll("SELECT idvisit, location_ip, " . implode(',', array_keys($logVisitFieldsToUpdate)) . "\n\t\t\t\t\t\tFROM " . Piwik_Common::prefixTable('log_visit') . " \n\t\t\t\t\t\tLIMIT {$start}, {$limit}"); if (!count($rows)) { continue; } foreach ($rows as $i => $row) { $fieldsToSet = array(); foreach ($logVisitFieldsToUpdate as $field => $ignore) { if (empty($fieldsToSet[$field])) { $fieldsToSet[] = $field; } } // skip if it already has a location if (empty($fieldsToSet)) { continue; } $ip = Piwik_IP::N2P($row['location_ip']); $location = $provider->getLocation(array('ip' => $ip)); if (!empty($location[Piwik_UserCountry_LocationProvider::COUNTRY_CODE_KEY])) { $location[Piwik_UserCountry_LocationProvider::COUNTRY_CODE_KEY] = strtolower($location[Piwik_UserCountry_LocationProvider::COUNTRY_CODE_KEY]); } $row['location_country'] = strtolower($row['location_country']); $columnsToSet = array(); $bind = array(); foreach ($logVisitFieldsToUpdate as $column => $locationKey) { if (!empty($location[$locationKey]) && $location[$locationKey] != $row[$column]) { $columnsToSet[] = $column . ' = ?'; $bind[] = $location[$locationKey]; } } if (empty($columnsToSet)) { continue;
/** * Checks if the visitor ip is in the excluded list * * @param string $ip Long IP * @return bool */ protected function isVisitorIpExcluded($ip) { $websiteAttributes = Piwik_Common::getCacheWebsiteAttributes($this->idsite); if (!empty($websiteAttributes['excluded_ips'])) { if (Piwik_IP::isIpInRange($ip, $websiteAttributes['excluded_ips'])) { printDebug('Visitor IP ' . Piwik_IP::N2P($ip) . ' is excluded from being tracked'); return true; } } return false; }