Ejemplo n.º 1
0
 /**
  * Return the domain name for the current request. Heuristics are used to
  * detect the the domain name. This will fail horribly for extremely short
  * domain names. A domain name is assumed to be the rightmost part of the
  * full hostname. A test on very short strings is used to detect top level
  * domains such as .com, .nl and double ones like .co.uk. This obviously
  * fails for longer top level domains and extremely short domain names.
  *
  * \return Domain name for the current request.
  */
 public static function domain()
 {
     $host = AnewtRequest::host();
     /* If it looks like an ip address we just return the IP address */
     if (preg_match('/^[0-9]{1,3}(\\.[0-9]{1,3}){3}$/', $host)) {
         return $host;
     }
     $parts = explode('.', $host);
     $num_parts = count($parts);
     if ($num_parts <= 2) {
         /* The hostname doesn't contain a subdomain part, or it is not
          * a canonical hostname after all (eg. localhost). */
         $domain = $host;
     } else {
         /* There's at least 3 parts: xx.yy.zz. Return either:
          * - yy.zz if yy is > 2 characters (example.com)
          * - xx.yy..zz if yy is <= 2 characters (example.co.uk)
          */
         $zz = array_pop($parts);
         $yy = array_pop($parts);
         if (strlen($yy) > 2) {
             $domain = sprintf('%s.%s', $yy, $zz);
         } else {
             $xx = array_pop($parts);
             $domain = sprintf('%s.%s.%s', $xx, $yy, $zz);
         }
     }
     return $domain;
 }