Error\n"; echo "$msg\n"; echo ""; exit; } error_log($_SERVER['REQUEST_URI']); if (!preg_match('/saved/', $_SERVER['REQUEST_URI'])) { $host = 'api.hostip.info'; $port = 80; $path = '/get_html.php?position=true&ip=' . $_SERVER['REMOTE_ADDR']; error_log('Geolocate with api.hostip.info...'); $fp = @fsockopen($host, $port, $errno, $errstr); if (!$fp) fatal("

Unable to open connection to $host ". "(port $port):

$errstr
"); fwrite($fp, "GET $path HTTP/1.0\r\n"); fwrite($fp, "Host: $host:$port\r\n"); fwrite($fp, "\r\n"); $buf = ''; $length = ''; while (!feof($fp)) { $buf .= fread($fp, 8192); // Parse the HTTP answer and try to extract response code, // headers and body $answer = parse_answer($buf); // We need to discover Content-Length, to know when server // finishes sending data if (!$length) { if ($answer[0] == 200) $length = $answer[1]['content-length']; } // When length is available, we can check if response has // been fully transmitted; this is the case when all what // we received equals the headers plus the full contents. if ($length) { if (strlen_b($buf) == strlen_b($answer[3]) + $length) break; } } fclose($fp); $body = $answer[2]; $latitude = $longitude = $zoom = 0; if (preg_match('/Latitude: (\S+)/', $body, $matches)) { error_log('Got lat/lng information from api.hostip.info'); $latitude = $matches[1]; $zoom = 8; } if (preg_match('/Longitude: (\S+)/', $body, $matches)) { $longitude = $matches[1]; } if (($latitude == 0 || $longitude == 0) && preg_match('/Country: .*?\((\S{2})\)/', $body, $matches)) { $country = $matches[1]; if ($country == 'XX') { // no luck with hostip.info, at least position to proper country with whois on IP error_log('Geolocate with whois...'); $whois = shell_exec("whois " . $_SERVER['REMOTE_ADDR']); if (preg_match('/country:\s*(\S{2})/i', $whois, $matches)) { error_log('Got country information from whois'); $country = $matches[1]; } } else { error_log('Got only country information from api.hostip.info'); } include('countries.phpi'); $latitude = $countries[$country][0]; $longitude = $countries[$country][1]; $zoom = 5; } } // PHP's strlen should return the length of a string in bytes, // except when using multibyte strings, in which case // mb_orig_strlen must be used (in case strlen was overloaded // with mb_strlen). function strlen_b($str) { if (function_exists('mb_orig_strlen')) { return mb_orig_strlen($str); } else { return strlen($str); } } // Parse an HTTP answer to extract response code, headers and body // Returns an array containing: response code, headers, body, raw headers function parse_answer($answer) { // This first pattern matching allows to separate first line // (server response status), headers, and data contents if (ereg("^(([^\n]+)\r\n(.*)\r\n\r\n)(.*)", $answer, $regs)) { $full_headers = $regs[1]; $response = $regs[2]; $headers_string = $regs[3]; $body = $regs[4]; // Parse first line (server response status) if (ereg("^HTTP/[0-9\.]+ ([0-9]+)", $response, $regs)) { $response_code = $regs[1]; } else { fatal("

Unable to parse response line $response ". "from the server.

"); } // Parse headers and build a hash with them foreach (split("\r\n", $headers_string) as $line) { if (ereg("^([^:]+): (.*)", $line, $regs)) { $headers[strtolower($regs[1])] = $regs[2]; } else { fatal("

Unable to parse header line $line ". "from the server. ". "All headers:

$headers_string
"); } } } else { // Return -1 as response code if parsing was not possible return array(-1); } return array($response_code, $headers, $body, $full_headers); } ?> Routes Compare
- - - -