Ejemplo n.º 1
0
 /**
  * Returns the hostname given the IP address string
  *
  * @param string $ipStr IP Address
  * @return string hostname (or human-readable IP address)
  */
 private function getHost($ipStr)
 {
     $ip = IP::fromStringIP($ipStr);
     $host = $ip->getHostname();
     $host = $host === null ? $ipStr : $host;
     return trim(strtolower($host));
 }
Ejemplo n.º 2
0
 /**
  * Hook on Tracker.Visit.setVisitorIp to anomymize visitor IP addresses
  * @param string $ip IP address in binary format (network format)
  */
 public function setVisitorIpAddress(&$ip)
 {
     $ipObject = IP::fromBinaryIP($ip);
     if (!$this->isActive()) {
         Common::printDebug("Visitor IP was _not_ anonymized: " . $ipObject->toString());
         return;
     }
     $privacyConfig = new Config();
     $newIpObject = self::applyIPMask($ipObject, $privacyConfig->ipAddressMaskLength);
     $ip = $newIpObject->toBinary();
     Common::printDebug("Visitor IP (was: " . $ipObject->toString() . ") has been anonymized: " . $newIpObject->toString());
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
 {
     if ($this->enabled) {
         $ip = IP::fromStringIP($request->getClientIp());
         $valid = F\some($this->whiteList, function ($range) use($ip) {
             return $ip->isInRange($range);
         });
         if (!$valid) {
             return new Response('Forbidden', 403);
         }
     }
     return $this->app->handle($request, $type, $catch);
 }
Ejemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function geocode($address)
 {
     if (!filter_var($address, FILTER_VALIDATE_IP)) {
         throw new UnsupportedOperation('The IpRangeGeoIp provider does not support street addresses, only IP addresses.');
     }
     if ('127.0.0.1' === $address) {
         return $this->returnResults([$this->getLocalhostDefaults()]);
     }
     $ip = Network\IP::fromStringIP($address);
     $result = [];
     foreach ($this->getData() as $row) {
         if (isset($row['networks']) && $ip->isInRanges($row['networks']) === true) {
             if (isset($row['address']) && is_array($row['address'])) {
                 $result = $row['address'];
             }
             // match -> stop
             break;
         }
     }
     if (count($result) === 0) {
         throw new NoResult(sprintf('No results found for IP address "%s".', $address));
     }
     return $this->returnResults([$this->fixEncoding(array_merge($this->getDefaults(), ['latitude' => isset($result['latitude']) ? $result['latitude'] : null, 'longitude' => isset($result['longitude']) ? $result['longitude'] : null, 'bounds' => ['south' => isset($result['bounds']['south']) ? $result['bounds']['south'] : null, 'west' => isset($result['bounds']['west']) ? $result['bounds']['west'] : null, 'north' => isset($result['bounds']['north']) ? $result['bounds']['north'] : null, 'east' => isset($result['bounds']['east']) ? $result['bounds']['east'] : null], 'streetNumber' => isset($result['streetNumber']) ? $result['streetNumber'] : null, 'streetName' => isset($result['streetName']) ? $result['streetName'] : null, 'locality' => isset($result['locality']) ? $result['locality'] : null, 'postalCode' => isset($result['postalCode']) ? $result['postalCode'] : null, 'subLocality' => isset($result['subLocality']) ? $result['subLocality'] : null, 'adminLevels' => [], 'country' => isset($result['country']) ? $result['country'] : null, 'countryCode' => isset($result['countryCode']) ? $result['countryCode'] : null, 'timezone' => isset($result['timezone']) ? $result['timezone'] : null]))]);
 }
Ejemplo n.º 5
0
 /**
  * Checks if the visitor ip is in the excluded list
  *
  * @return bool
  */
 protected function isVisitorIpExcluded()
 {
     $websiteAttributes = Cache::getCacheWebsiteAttributes($this->idSite);
     if (!empty($websiteAttributes['excluded_ips'])) {
         $ip = IP::fromBinaryIP($this->ip);
         if ($ip->isInRanges($websiteAttributes['excluded_ips'])) {
             Common::printDebug('Visitor IP ' . $ip->toString() . ' is excluded from being tracked');
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 6
0
 /**
  * Returns an IP address from an array that was passed into getLocation. This
  * will return an IPv4 address or IPv6 address.
  *
  * @param  array $info Must have 'ip' key.
  * @return string|null
  */
 protected function getIpFromInfo($info)
 {
     $ip = \Piwik\Network\IP::fromStringIP($info['ip']);
     if ($ip instanceof \Piwik\Network\IPv6 && $ip->isMappedIPv4()) {
         return $ip->toIPv4String();
     } else {
         return $ip->toString();
     }
 }
Ejemplo n.º 7
0
 /**
  * Returns the last IP address in a comma separated list, subject to an optional exclusion list.
  *
  * @param string $csv Comma separated list of elements.
  * @param array $excludedIps Optional list of excluded IP addresses (or IP address ranges).
  * @return string Last (non-excluded) IP address in the list or an empty string if all given IPs are excluded.
  */
 public static function getLastIpFromList($csv, $excludedIps = null)
 {
     $p = strrpos($csv, ',');
     if ($p !== false) {
         $elements = explode(',', $csv);
         for ($i = count($elements); $i--;) {
             $element = trim(Common::sanitizeInputValue($elements[$i]));
             $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element));
             if (empty($excludedIps) || !in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps)) {
                 return $element;
             }
         }
         return '';
     }
     return trim(Common::sanitizeInputValue($csv));
 }
Ejemplo n.º 8
0
function isIpInRange($ip, $ipRanges)
{
    $ip = \Piwik\Network\IP::fromBinaryIP($ip);
    return $ip->isInRanges($ipRanges);
}
Ejemplo n.º 9
0
 /**
  * Determines if the IP address is in a search engine bot IP address range
  *
  * @param $ip
  * @return bool
  */
 public static function isInRangesOfSearchBots($ip)
 {
     $ip = IP::fromBinaryIP($ip);
     $isInRanges = $ip->isInRanges(self::getBotIpRanges());
     return $isInRanges;
 }
Ejemplo n.º 10
0
 /**
  * Called by event `Tracker.newVisitorInformation`
  *
  * @see getListHooksRegistered()
  */
 public function logIntranetSubNetworkInfo(&$visitorInfo)
 {
     if (!file_exists($this->getDataFilePath())) {
         Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath());
         return;
     }
     $data = (include $this->getDataFilePath());
     if ($data === false) {
         // no data file found
         // @todo ...inform the user/ log something
         Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath());
         return;
     }
     foreach ($data as $value) {
         $ip = Network\IP::fromBinaryIP($visitorInfo['location_ip']);
         if (isset($value['networks']) && $ip->isInRanges($value['networks']) === true) {
             // values with the same key are not overwritten by right!
             // http://www.php.net/manual/en/language.operators.array.php
             if (isset($value['visitorInfo'])) {
                 $visitorInfo = $value['visitorInfo'] + $visitorInfo;
             }
             return;
         }
     }
     // if nothing was matched, you can define default values if you want to
     if (isset($data['noMatch']) && isset($data['noMatch']['visitorInfo'])) {
         $visitorInfo = $data['noMatch']['visitorInfo'] + $visitorInfo;
         return;
     }
 }