Example #1
0
 function getIp()
 {
     if (isset($this->details['location_ip'])) {
         return IPUtils::binaryToStringIP($this->details['location_ip']);
     }
     return null;
 }
Example #2
0
 /**
  * @param Request $request
  * @param Visitor $visitor
  * @param Action|null $action
  * @return mixed
  */
 public function onNewVisit(Request $request, Visitor $visitor, $action)
 {
     // Adding &dp=1 will disable the provider plugin, this is an "unofficial" parameter used to speed up log importer
     $disableProvider = $request->getParam('dp');
     if (!empty($disableProvider)) {
         return false;
     }
     // if provider info has already been set, abort
     $locationValue = $visitor->getVisitorColumn('location_provider');
     if (!empty($locationValue)) {
         return false;
     }
     $ip = $visitor->getVisitorColumn('location_ip');
     $privacyConfig = new PrivacyManagerConfig();
     if (!$privacyConfig->useAnonymizedIpForVisitEnrichment) {
         $ip = $request->getIp();
     }
     $ip = IPUtils::binaryToStringIP($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') {
         Common::printDebug("IP Was anonymized so we skip the Provider DNS reverse lookup...");
         return false;
     }
     $hostname = $this->getHost($ip);
     $hostnameExtension = ProviderPlugin::getCleanHostname($hostname);
     // add the provider value in the table log_visit
     $locationProvider = substr($hostnameExtension, 0, 100);
     return $locationProvider;
 }
Example #3
0
 private function getIpAddress($anonymizedIp, \Piwik\Tracker\Request $request)
 {
     $privacyConfig = new PrivacyManagerConfig();
     $ip = $request->getIp();
     if ($privacyConfig->useAnonymizedIpForVisitEnrichment) {
         $ip = $anonymizedIp;
     }
     $ipAddress = IPUtils::binaryToStringIP($ip);
     return $ipAddress;
 }
 /**
  *
  * @param Visitor $visitor            
  * @param Request $request            
  *
  * @return string
  */
 private function getIpAddress(Visitor $visitor, Request $request)
 {
     if ($this->ipToUse !== null) {
         return $this->ipToUse;
     }
     $privacyConfig = new PrivacyManagerConfig();
     $ip = $request->getIp();
     if ($privacyConfig->useAnonymizedIpForVisitEnrichment) {
         $ip = $visitor->getVisitorColumn('location_ip');
     }
     $ip = IPUtils::binaryToStringIP($ip);
     $this->ipToUse = $ip;
     return $ip;
 }
Example #5
0
 public function insertVisitorLocation(&$visitorInfo, \Piwik\Tracker\Request $request)
 {
     $dbPath = PIWIK_INCLUDE_PATH . '/plugins/IP2Location/data/';
     $dbFile = '';
     if ($handle = opendir($dbPath)) {
         while (false !== ($file = readdir($handle))) {
             if (strtoupper(substr($file, -4)) == '.BIN') {
                 $dbFile = $dbPath . $file;
                 break;
             }
         }
         closedir($handle);
     }
     if ($dbFile) {
         $privacyConfig = new PrivacyManagerConfig();
         $ipAddress = IPUtils::binaryToStringIP($privacyConfig->useAnonymizedIpForVisitEnrichment ? $visitorInfo['location_ip'] : $request->getIp());
         $result = IP2LocationAPI::lookup($ipAddress, $dbFile);
         $countryCode = $result['countryCode'] != '-' ? strtolower($result['countryCode']) : null;
         $regionName = $result['regionName'] != '-' ? $result['regionName'] : null;
         $regionCode = $this->getRegionCode(strtoupper($countryCode), $regionName);
         $cityName = !preg_match('/not supported/', $result['cityName']) && $result['cityName'] != '-' ? $result['cityName'] : null;
         $latitude = !preg_match('/not supported/', $result['latitude']) && $result['latitude'] != '-' ? $result['latitude'] : null;
         $longitude = !preg_match('/not supported/', $result['longitude']) && $result['longitude'] != '-' ? $result['longitude'] : null;
         if ($countryCode) {
             $visitorInfo['location_country'] = $countryCode;
         }
         if ($regionCode) {
             $visitorInfo['location_region'] = $regionCode;
         }
         if ($cityName) {
             $visitorInfo['location_city'] = $cityName;
         }
         if ($latitude) {
             $visitorInfo['location_latitude'] = $latitude;
         }
         if ($longitude) {
             $visitorInfo['location_longitude'] = $longitude;
         }
     }
 }
Example #6
0
 private function printVisitorInformation()
 {
     $debugVisitInfo = $this->visitProperties->getProperties();
     $debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']);
     $debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']);
     $debugVisitInfo['location_ip'] = IPUtils::binaryToStringIP($debugVisitInfo['location_ip']);
     Common::printDebug($debugVisitInfo);
 }
Example #7
0
 /**
  * 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 = IPUtils::getIPRangeBounds($ipRange);
     if ($range === null) {
         return false;
     }
     return array(IPUtils::binaryToStringIP($range[0]), IPUtils::binaryToStringIP($range[1]));
 }
 /**
  * Geolcates an existing visit and then updates it if it's current attributes are different than
  * what was geolocated. Also updates all conversions of a visit.
  *
  * **This method should NOT be used from within the tracker.**
  *
  * @param array $visit The visit information. Must contain an `"idvisit"` element and `"location_ip"` element.
  * @param bool $useClassCache
  * @return array|null The visit properties that were updated in the DB mapped to the updated values. If null,
  *                    required information was missing from `$visit`.
  */
 public function attributeExistingVisit($visit, $useClassCache = true)
 {
     if (empty($visit['idvisit'])) {
         $this->logger->debug('Empty idvisit field. Skipping re-attribution..');
         return null;
     }
     $idVisit = $visit['idvisit'];
     if (empty($visit['location_ip'])) {
         $this->logger->debug('Empty location_ip field for idvisit = %s. Skipping re-attribution.', array('idvisit' => $idVisit));
         return null;
     }
     $ip = IPUtils::binaryToStringIP($visit['location_ip']);
     $location = $this->getLocation(array('ip' => $ip), $useClassCache);
     $valuesToUpdate = $this->getVisitFieldsToUpdate($visit, $location);
     if (!empty($valuesToUpdate)) {
         $this->logger->debug('Updating visit with idvisit = {idVisit} (IP = {ip}). Changes: {changes}', array('idVisit' => $idVisit, 'ip' => $ip, 'changes' => $valuesToUpdate));
         $this->dao->updateVisits($valuesToUpdate, $idVisit);
         $this->dao->updateConversions($valuesToUpdate, $idVisit);
     } else {
         $this->logger->debug('Nothing to update for idvisit = %s (IP = {ip}). Existing location info is same as geolocated.', array('idVisit' => $idVisit, 'ip' => $ip));
     }
     return $valuesToUpdate;
 }