Ejemplo n.º 1
0
/**
 * Get the base domain (without protocol and any subdomain) of an URL.
 *
 * Gets a max of 3 domain parts (x.y.tld)
 *
 * @param string URL
 * @return string the base domain (may become empty, if found invalid)
 */
function get_base_domain($url)
{
    global $evo_charset;
    //echo '<p>'.$url;
    // Chop away the http part and the path:
    $domain = preg_replace('~^([a-z]+://)?([^:/#]+)(.*)$~i', '\\2', $url);
    if (empty($domain) || preg_match('~^(\\d+\\.)+\\d+$~', $domain)) {
        // Empty or All numeric = IP address, don't try to cut it any further
        return $domain;
    }
    //echo '<br>'.$domain;
    // Get the base domain up to 3 levels (x.y.tld):
    // NOTE: "_" is not really valid, but for Windows it is..
    // NOTE: \w includes "_"
    // convert URL to IDN:
    $domain = idna_encode($domain);
    $domain_pattern = '~ ( \\w (\\w|-|_)* \\. ){0,2}   \\w (\\w|-|_)* $~ix';
    if (!preg_match($domain_pattern, $domain, $match)) {
        return '';
    }
    $base_domain = convert_charset(idna_decode($match[0]), $evo_charset, 'UTF-8');
    // Remove any www. prefix:
    $base_domain = preg_replace('~^www\\.~i', '', $base_domain);
    //echo '<br>'.$base_domain.'</p>';
    return $base_domain;
}
Ejemplo n.º 2
0
 /**
  * Check the content of a given URL (referer), if the requested URI (with different hostname variations)
  * is present.
  *
  * @todo Use DB cache to avoid checking the same page again and again! (Plugin DB table)
  *
  * @param string
  * @param string URI to append to matching pattern for hostnames
  * @return boolean
  */
 function is_referer_linking_us($referer, $uri)
 {
     global $misc_inc_path, $lib_subdir, $ReqHost;
     if (empty($referer)) {
         return false;
     }
     // Load page content (max. 500kb), using fsockopen:
     $url_parsed = @parse_url($referer);
     if (!$url_parsed) {
         return false;
     }
     if (empty($url_parsed['scheme'])) {
         $url_parsed = parse_url('http://' . $referer);
     }
     $host = $url_parsed['host'];
     $port = empty($url_parsed['port']) ? 80 : $url_parsed['port'];
     $path = empty($url_parsed['path']) ? '/' : $url_parsed['path'];
     if (!empty($url_parsed['query'])) {
         $path .= '?' . $url_parsed['query'];
     }
     $fp = @fsockopen($host, $port, $errno, $errstr, 30);
     if (!$fp) {
         // could not access referring page
         $this->debug_log('is_referer_linking_us(): could not access &laquo;' . $referer . '&raquo; (host: ' . $host . '): ' . $errstr . ' (#' . $errno . ')');
         return false;
     }
     // Set timeout for data:
     if (function_exists('stream_set_timeout')) {
         stream_set_timeout($fp, 20);
     } else {
         socket_set_timeout($fp, 20);
     }
     // PHP 4
     // Send request:
     $out = "GET {$path} HTTP/1.0\r\n";
     $out .= "Host: {$host}:{$port}\r\n";
     $out .= "Connection: Close\r\n\r\n";
     fwrite($fp, $out);
     // Skip headers:
     $i = 0;
     $source_charset = 'iso-8859-1';
     // default
     while (($s = fgets($fp, 4096)) !== false) {
         $i++;
         if ($s == "\r\n" || $i > 100) {
             break;
         }
         if (preg_match('~^Content-Type:.*?charset=([\\w-]+)~i', $s, $match)) {
             $source_charset = $match[1];
         }
     }
     // Get the refering page's content
     $content_ref_page = '';
     $bytes_read = 0;
     while (($s = fgets($fp, 4096)) !== false) {
         $content_ref_page .= $s;
         $bytes_read += strlen($s);
         if ($bytes_read > 512000) {
             // do not pull more than 500kb of data!
             break;
         }
     }
     fclose($fp);
     if (!strlen($content_ref_page)) {
         $this->debug_log('is_referer_linking_us(): empty $content_ref_page (' . bytesreadable($bytes_read) . ' read)');
         return false;
     }
     $have_idn_name = false;
     // Build the search pattern:
     // We match for basically for 'href="[SERVER][URI]', where [SERVER] is a list of possible hosts (especially IDNA)
     $search_pattern = '~\\shref=["\']?https?://(';
     $possible_hosts = array($_SERVER['HTTP_HOST']);
     if ($_SERVER['SERVER_NAME'] != $_SERVER['HTTP_HOST']) {
         $possible_hosts[] = $_SERVER['SERVER_NAME'];
     }
     $search_pattern_hosts = array();
     foreach ($possible_hosts as $l_host) {
         if (preg_match('~^([^.]+\\.)(.*?)([^.]+\\.[^.]+)$~', $l_host, $match)) {
             // we have subdomains in this hostname
             if (stristr($match[1], 'www')) {
                 // search also for hostname without 'www.'
                 $search_pattern_hosts[] = $match[2] . $match[3];
             }
         }
         $search_pattern_hosts[] = $l_host;
     }
     $search_pattern_hosts = array_unique($search_pattern_hosts);
     foreach ($search_pattern_hosts as $l_host) {
         // add IDN, because this could be linked:
         $l_idn_host = idna_decode($l_host);
         // the decoded puny-code ("xn--..") name (utf8)
         if ($l_idn_host != $l_host) {
             $have_idn_name = true;
             $search_pattern_hosts[] = $l_idn_host;
         }
     }
     // add hosts to pattern, preg_quoted
     for ($i = 0, $n = count($search_pattern_hosts); $i < $n; $i++) {
         $search_pattern_hosts[$i] = preg_quote($search_pattern_hosts[$i], '~');
     }
     $search_pattern .= implode('|', $search_pattern_hosts) . ')';
     if (empty($uri)) {
         // host(s) should end with "/", "'", '"', "?" or whitespace
         $search_pattern .= '[/"\'\\s?]';
     } else {
         $search_pattern .= preg_quote($uri, '~');
         // URI should end with "'", '"' or whitespace
         $search_pattern .= '["\'\\s]';
     }
     $search_pattern .= '~i';
     if ($have_idn_name) {
         // Convert charset to UTF-8, because the decoded domain name is UTF-8, too:
         if (can_convert_charsets('utf-8', $source_charset)) {
             $content_ref_page = convert_charset($content_ref_page, 'utf-8', $source_charset);
         } else {
             $this->debug_log('is_referer_linking_us(): warning: cannot convert charset of referring page');
         }
     }
     if (preg_match($search_pattern, $content_ref_page)) {
         $this->debug_log('is_referer_linking_us(): found current URL in page (' . bytesreadable($bytes_read) . ' read)');
         return true;
     } else {
         if (strpos($referer, $ReqHost) === 0 && !empty($uri)) {
             // Referer is the same host.. just search for $uri
             if (strpos($content_ref_page, $uri) !== false) {
                 $this->debug_log('is_referer_linking_us(): found current URI in page (' . bytesreadable($bytes_read) . ' read)');
                 return true;
             }
         }
         $this->debug_log('is_referer_linking_us(): ' . sprintf('did not find &laquo;%s&raquo; in &laquo;%s&raquo; (%s bytes read).', $search_pattern, $referer, bytesreadable($bytes_read)));
         return false;
     }
 }