Piwik\Plugins\Provider\Provider::getCleanHostname PHP Method

getCleanHostname() public static method

Returns the hostname extension (site.co.jp in fvae.VARG.ceaga.site.co.jp) given the full hostname looked up from the IP
public static getCleanHostname ( string $hostname ) : string
$hostname string
return string
    public static function getCleanHostname($hostname)
    {
        $extToExclude = array('com', 'net', 'org', 'co');
        $off = strrpos($hostname, '.');
        $ext = substr($hostname, $off);
        if (empty($off) || is_numeric($ext) || strlen($hostname) < 5) {
            return 'Ip';
        } else {
            $cleanHostname = null;
            /**
             * Triggered when prettifying a hostname string.
             *
             * This event can be used to customize the way a hostname is displayed in the
             * Providers report.
             *
             * **Example**
             *
             *     public function getCleanHostname(&$cleanHostname, $hostname)
             *     {
             *         if ('fvae.VARG.ceaga.site.co.jp' == $hostname) {
             *             $cleanHostname = 'site.co.jp';
             *         }
             *     }
             *
             * @param string &$cleanHostname The hostname string to display. Set by the event
             *                               handler.
             * @param string $hostname The full hostname.
             */
            Piwik::postEvent('Provider.getCleanHostname', array(&$cleanHostname, $hostname));
            if ($cleanHostname !== null) {
                return $cleanHostname;
            }
            $e = explode('.', $hostname);
            $s = sizeof($e);
            // if extension not correct
            if (isset($e[$s - 2]) && in_array($e[$s - 2], $extToExclude)) {
                return $e[$s - 3] . "." . $e[$s - 2] . "." . $e[$s - 1];
            } else {
                return $e[$s - 2] . "." . $e[$s - 1];
            }
        }
    }

Usage Example

示例#1
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;
 }
All Usage Examples Of Piwik\Plugins\Provider\Provider::getCleanHostname