replace ajax downloadurl disallowed on IE (not same domain?) by PHP
authorGuillaume Cottenceau <gcottenc@gmail.com>
Mon, 15 Nov 2010 14:57:14 +0000 (15:57 +0100)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Mon, 15 Nov 2010 14:57:14 +0000 (15:57 +0100)
countries.phpi [new file with mode: 0644]
googlemaps.js
index.php [moved from index.html with 71% similarity]

diff --git a/countries.phpi b/countries.phpi
new file mode 100644 (file)
index 0000000..802fba0
--- /dev/null
@@ -0,0 +1,245 @@
+<?php
+
+// countries with their center latlng - from CIA world factbook
+$countries = array( 'AD' => array( 42.5000, 1.5000 ),
+                    'AE' => array( 24.0000, 54.0000 ),
+                    'AF' => array( 33.0000, 65.0000 ),
+                    'AG' => array( 17.0500, -61.8000 ),
+                    'AI' => array( 18.2500, -63.1667 ),
+                    'AL' => array( 41.0000, 20.0000 ),
+                    'AM' => array( 40.0000, 45.0000 ),
+                    'AN' => array( 12.2500, -68.7500 ),
+                    'AO' => array( -12.5000, 18.5000 ),
+                    'AP' => array( 35.0000, 105.0000 ),
+                    'AQ' => array( -90.0000, 0.0000 ),
+                    'AR' => array( -34.0000, -64.0000 ),
+                    'AS' => array( -14.3333, -170.0000 ),
+                    'AT' => array( 47.3333, 13.3333 ),
+                    'AU' => array( -27.0000, 133.0000 ),
+                    'AW' => array( 12.5000, -69.9667 ),
+                    'AZ' => array( 40.5000, 47.5000 ),
+                    'BA' => array( 44.0000, 18.0000 ),
+                    'BB' => array( 13.1667, -59.5333 ),
+                    'BD' => array( 24.0000, 90.0000 ),
+                    'BE' => array( 50.8333, 4.0000 ),
+                    'BF' => array( 13.0000, -2.0000 ),
+                    'BG' => array( 43.0000, 25.0000 ),
+                    'BH' => array( 26.0000, 50.5500 ),
+                    'BI' => array( -3.5000, 30.0000 ),
+                    'BJ' => array( 9.5000, 2.2500 ),
+                    'BM' => array( 32.3333, -64.7500 ),
+                    'BN' => array( 4.5000, 114.6667 ),
+                    'BO' => array( -17.0000, -65.0000 ),
+                    'BR' => array( -10.0000, -55.0000 ),
+                    'BS' => array( 24.2500, -76.0000 ),
+                    'BT' => array( 27.5000, 90.5000 ),
+                    'BV' => array( -54.4333, 3.4000 ),
+                    'BW' => array( -22.0000, 24.0000 ),
+                    'BY' => array( 53.0000, 28.0000 ),
+                    'BZ' => array( 17.2500, -88.7500 ),
+                    'CA' => array( 60.0000, -95.0000 ),
+                    'CC' => array( -12.5000, 96.8333 ),
+                    'CD' => array( 0.0000, 25.0000 ),
+                    'CF' => array( 7.0000, 21.0000 ),
+                    'CG' => array( -1.0000, 15.0000 ),
+                    'CH' => array( 47.0000, 8.0000 ),
+                    'CI' => array( 8.0000, -5.0000 ),
+                    'CK' => array( -21.2333, -159.7667 ),
+                    'CL' => array( -30.0000, -71.0000 ),
+                    'CM' => array( 6.0000, 12.0000 ),
+                    'CN' => array( 35.0000, 105.0000 ),
+                    'CO' => array( 4.0000, -72.0000 ),
+                    'CR' => array( 10.0000, -84.0000 ),
+                    'CU' => array( 21.5000, -80.0000 ),
+                    'CV' => array( 16.0000, -24.0000 ),
+                    'CX' => array( -10.5000, 105.6667 ),
+                    'CY' => array( 35.0000, 33.0000 ),
+                    'CZ' => array( 49.7500, 15.5000 ),
+                    'DE' => array( 51.0000, 9.0000 ),
+                    'DJ' => array( 11.5000, 43.0000 ),
+                    'DK' => array( 56.0000, 10.0000 ),
+                    'DM' => array( 15.4167, -61.3333 ),
+                    'DO' => array( 19.0000, -70.6667 ),
+                    'DZ' => array( 28.0000, 3.0000 ),
+                    'EC' => array( -2.0000, -77.5000 ),
+                    'EE' => array( 59.0000, 26.0000 ),
+                    'EG' => array( 27.0000, 30.0000 ),
+                    'EH' => array( 24.5000, -13.0000 ),
+                    'ER' => array( 15.0000, 39.0000 ),
+                    'ES' => array( 40.0000, -4.0000 ),
+                    'ET' => array( 8.0000, 38.0000 ),
+                    'EU' => array( 47.0000, 8.0000 ),
+                    'FI' => array( 64.0000, 26.0000 ),
+                    'FJ' => array( -18.0000, 175.0000 ),
+                    'FK' => array( -51.7500, -59.0000 ),
+                    'FM' => array( 6.9167, 158.2500 ),
+                    'FO' => array( 62.0000, -7.0000 ),
+                    'FR' => array( 46.0000, 2.0000 ),
+                    'GA' => array( -1.0000, 11.7500 ),
+                    'GB' => array( 54.0000, -2.0000 ),
+                    'GD' => array( 12.1167, -61.6667 ),
+                    'GE' => array( 42.0000, 43.5000 ),
+                    'GF' => array( 4.0000, -53.0000 ),
+                    'GH' => array( 8.0000, -2.0000 ),
+                    'GI' => array( 36.1833, -5.3667 ),
+                    'GL' => array( 72.0000, -40.0000 ),
+                    'GM' => array( 13.4667, -16.5667 ),
+                    'GN' => array( 11.0000, -10.0000 ),
+                    'GP' => array( 16.2500, -61.5833 ),
+                    'GQ' => array( 2.0000, 10.0000 ),
+                    'GR' => array( 39.0000, 22.0000 ),
+                    'GS' => array( -54.5000, -37.0000 ),
+                    'GT' => array( 15.5000, -90.2500 ),
+                    'GU' => array( 13.4667, 144.7833 ),
+                    'GW' => array( 12.0000, -15.0000 ),
+                    'GY' => array( 5.0000, -59.0000 ),
+                    'HK' => array( 22.2500, 114.1667 ),
+                    'HM' => array( -53.1000, 72.5167 ),
+                    'HN' => array( 15.0000, -86.5000 ),
+                    'HR' => array( 45.1667, 15.5000 ),
+                    'HT' => array( 19.0000, -72.4167 ),
+                    'HU' => array( 47.0000, 20.0000 ),
+                    'ID' => array( -5.0000, 120.0000 ),
+                    'IE' => array( 53.0000, -8.0000 ),
+                    'IL' => array( 31.5000, 34.7500 ),
+                    'IN' => array( 20.0000, 77.0000 ),
+                    'IO' => array( -6.0000, 71.5000 ),
+                    'IQ' => array( 33.0000, 44.0000 ),
+                    'IR' => array( 32.0000, 53.0000 ),
+                    'IS' => array( 65.0000, -18.0000 ),
+                    'IT' => array( 42.8333, 12.8333 ),
+                    'JM' => array( 18.2500, -77.5000 ),
+                    'JO' => array( 31.0000, 36.0000 ),
+                    'JP' => array( 36.0000, 138.0000 ),
+                    'KE' => array( 1.0000, 38.0000 ),
+                    'KG' => array( 41.0000, 75.0000 ),
+                    'KH' => array( 13.0000, 105.0000 ),
+                    'KI' => array( 1.4167, 173.0000 ),
+                    'KM' => array( -12.1667, 44.2500 ),
+                    'KN' => array( 17.3333, -62.7500 ),
+                    'KP' => array( 40.0000, 127.0000 ),
+                    'KR' => array( 37.0000, 127.5000 ),
+                    'KW' => array( 29.3375, 47.6581 ),
+                    'KY' => array( 19.5000, -80.5000 ),
+                    'KZ' => array( 48.0000, 68.0000 ),
+                    'LA' => array( 18.0000, 105.0000 ),
+                    'LB' => array( 33.8333, 35.8333 ),
+                    'LC' => array( 13.8833, -61.1333 ),
+                    'LI' => array( 47.1667, 9.5333 ),
+                    'LK' => array( 7.0000, 81.0000 ),
+                    'LR' => array( 6.5000, -9.5000 ),
+                    'LS' => array( -29.5000, 28.5000 ),
+                    'LT' => array( 56.0000, 24.0000 ),
+                    'LU' => array( 49.7500, 6.1667 ),
+                    'LV' => array( 57.0000, 25.0000 ),
+                    'LY' => array( 25.0000, 17.0000 ),
+                    'MA' => array( 32.0000, -5.0000 ),
+                    'MC' => array( 43.7333, 7.4000 ),
+                    'MD' => array( 47.0000, 29.0000 ),
+                    'ME' => array( 42.0000, 19.0000 ),
+                    'MG' => array( -20.0000, 47.0000 ),
+                    'MH' => array( 9.0000, 168.0000 ),
+                    'MK' => array( 41.8333, 22.0000 ),
+                    'ML' => array( 17.0000, -4.0000 ),
+                    'MM' => array( 22.0000, 98.0000 ),
+                    'MN' => array( 46.0000, 105.0000 ),
+                    'MO' => array( 22.1667, 113.5500 ),
+                    'MP' => array( 15.2000, 145.7500 ),
+                    'MQ' => array( 14.6667, -61.0000 ),
+                    'MR' => array( 20.0000, -12.0000 ),
+                    'MS' => array( 16.7500, -62.2000 ),
+                    'MT' => array( 35.8333, 14.5833 ),
+                    'MU' => array( -20.2833, 57.5500 ),
+                    'MV' => array( 3.2500, 73.0000 ),
+                    'MW' => array( -13.5000, 34.0000 ),
+                    'MX' => array( 23.0000, -102.0000 ),
+                    'MY' => array( 2.5000, 112.5000 ),
+                    'MZ' => array( -18.2500, 35.0000 ),
+                    'NA' => array( -22.0000, 17.0000 ),
+                    'NC' => array( -21.5000, 165.5000 ),
+                    'NE' => array( 16.0000, 8.0000 ),
+                    'NF' => array( -29.0333, 167.9500 ),
+                    'NG' => array( 10.0000, 8.0000 ),
+                    'NI' => array( 13.0000, -85.0000 ),
+                    'NL' => array( 52.5000, 5.7500 ),
+                    'NO' => array( 62.0000, 10.0000 ),
+                    'NP' => array( 28.0000, 84.0000 ),
+                    'NR' => array( -0.5333, 166.9167 ),
+                    'NU' => array( -19.0333, -169.8667 ),
+                    'NZ' => array( -41.0000, 174.0000 ),
+                    'OM' => array( 21.0000, 57.0000 ),
+                    'PA' => array( 9.0000, -80.0000 ),
+                    'PE' => array( -10.0000, -76.0000 ),
+                    'PF' => array( -15.0000, -140.0000 ),
+                    'PG' => array( -6.0000, 147.0000 ),
+                    'PH' => array( 13.0000, 122.0000 ),
+                    'PK' => array( 30.0000, 70.0000 ),
+                    'PL' => array( 52.0000, 20.0000 ),
+                    'PM' => array( 46.8333, -56.3333 ),
+                    'PR' => array( 18.2500, -66.5000 ),
+                    'PS' => array( 32.0000, 35.2500 ),
+                    'PT' => array( 39.5000, -8.0000 ),
+                    'PW' => array( 7.5000, 134.5000 ),
+                    'PY' => array( -23.0000, -58.0000 ),
+                    'QA' => array( 25.5000, 51.2500 ),
+                    'RE' => array( -21.1000, 55.6000 ),
+                    'RO' => array( 46.0000, 25.0000 ),
+                    'RS' => array( 44.0000, 21.0000 ),
+                    'RU' => array( 60.0000, 100.0000 ),
+                    'RW' => array( -2.0000, 30.0000 ),
+                    'SA' => array( 25.0000, 45.0000 ),
+                    'SB' => array( -8.0000, 159.0000 ),
+                    'SC' => array( -4.5833, 55.6667 ),
+                    'SD' => array( 15.0000, 30.0000 ),
+                    'SE' => array( 62.0000, 15.0000 ),
+                    'SG' => array( 1.3667, 103.8000 ),
+                    'SH' => array( -15.9333, -5.7000 ),
+                    'SI' => array( 46.0000, 15.0000 ),
+                    'SJ' => array( 78.0000, 20.0000 ),
+                    'SK' => array( 48.6667, 19.5000 ),
+                    'SL' => array( 8.5000, -11.5000 ),
+                    'SM' => array( 43.7667, 12.4167 ),
+                    'SN' => array( 14.0000, -14.0000 ),
+                    'SO' => array( 10.0000, 49.0000 ),
+                    'SR' => array( 4.0000, -56.0000 ),
+                    'ST' => array( 1.0000, 7.0000 ),
+                    'SV' => array( 13.8333, -88.9167 ),
+                    'SY' => array( 35.0000, 38.0000 ),
+                    'SZ' => array( -26.5000, 31.5000 ),
+                    'TC' => array( 21.7500, -71.5833 ),
+                    'TD' => array( 15.0000, 19.0000 ),
+                    'TF' => array( -43.0000, 67.0000 ),
+                    'TG' => array( 8.0000, 1.1667 ),
+                    'TH' => array( 15.0000, 100.0000 ),
+                    'TJ' => array( 39.0000, 71.0000 ),
+                    'TK' => array( -9.0000, -172.0000 ),
+                    'TM' => array( 40.0000, 60.0000 ),
+                    'TN' => array( 34.0000, 9.0000 ),
+                    'TO' => array( -20.0000, -175.0000 ),
+                    'TR' => array( 39.0000, 35.0000 ),
+                    'TT' => array( 11.0000, -61.0000 ),
+                    'TV' => array( -8.0000, 178.0000 ),
+                    'TW' => array( 23.5000, 121.0000 ),
+                    'TZ' => array( -6.0000, 35.0000 ),
+                    'UA' => array( 49.0000, 32.0000 ),
+                    'UG' => array( 1.0000, 32.0000 ),
+                    'UM' => array( 19.2833, 166.6000 ),
+                    'US' => array( 38.0000, -97.0000 ),
+                    'UY' => array( -33.0000, -56.0000 ),
+                    'UZ' => array( 41.0000, 64.0000 ),
+                    'VA' => array( 41.9000, 12.4500 ),
+                    'VC' => array( 13.2500, -61.2000 ),
+                    'VE' => array( 8.0000, -66.0000 ),
+                    'VG' => array( 18.5000, -64.5000 ),
+                    'VI' => array( 18.3333, -64.8333 ),
+                    'VN' => array( 16.0000, 106.0000 ),
+                    'VU' => array( -16.0000, 167.0000 ),
+                    'WF' => array( -13.3000, -176.2000 ),
+                    'WS' => array( -13.5833, -172.3333 ),
+                    'YE' => array( 15.0000, 48.0000 ),
+                    'YT' => array( -12.8333, 45.1667 ),
+                    'ZA' => array( -29.0000, 24.0000 ),
+                    'ZM' => array( -15.0000, 30.0000 ),
+                    'ZW' => array( -20.0000, 30.0000 ) );
+
+ ?>
\ No newline at end of file
index 66557023caee54cb0071f8d5a78046f5508b615f..11efa28eceea87fb85fda779ca76e41d4368f1f9 100644 (file)
@@ -4,20 +4,6 @@ function debug(txt) {
     document.getElementById("debugbox").innerHTML += new Date() + txt + "<br/>";
 }
 
-function downloadUrl(url, callback) {
-    var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
-    request.onreadystatechange = function() {
-        if (request.readyState == 4) {
-            request.onreadystatechange = function() {};
-            if (callback) {
-                callback(request.responseText, request.status);
-            }
-        }
-    };
-    request.open('GET', url, true);
-    request.send(null);
-}
-
 function getIcon(filename) {
     return new google.maps.MarkerImage("icons/" + filename + ".png",
                                        new google.maps.Size(32, 32),
similarity index 71%
rename from index.html
rename to index.php
index 5b16e28efc3d31b84c0707398e101682428da5a9..ca56a5acafe1984f70dd7c4a236a747b3ab0ad24 100644 (file)
+++ b/index.php
@@ -1,3 +1,122 @@
+<?php
+
+ini_set("track_errors", 1);
+ini_set('display_errors', 0);
+
+// Helper function for fatal errors
+function fatal($msg) {
+    echo "<h2>Error</h2>\n";
+    echo "$msg\n";
+    echo "</body></html>";
+    exit;
+
+}
+
+$host = 'api.hostip.info';
+$port = 80;
+$path = '/get_html.php?position=true&ip=' . $_SERVER['REMOTE_ADDR'];
+
+$fp = @fsockopen($host, $port, $errno, $errstr);
+if (!$fp)
+    fatal("<p>Unable to open connection to <tt>$host</tt> ".
+          "(port <tt>$port</tt>):</p><pre>$errstr</pre>");
+
+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)) {
+    $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];
+    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("<p>Unable to parse response line <tt>$response</tt> ".
+                  "from the server.</p>");
+        }
+        // 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("<p>Unable to parse header line <tt>$line</tt> ".
+                      "from the server. ".
+                      "All headers:</p><pre>$headers_string</pre>");
+            }
+        }
+    } else {
+        // Return -1 as response code if parsing was not possible
+        return array(-1);
+    }
+    return array($response_code, $headers, $body, $full_headers);
+}
+
+?>
+
 <html>
 <head>
     <title>Routes Compare</title>
       function initialize() {
           map = new google.maps.Map(document.getElementById("mapbox"),
                                     { mapTypeId: google.maps.MapTypeId.ROADMAP,
-                                      zoom: 8 });
+                                      zoom: 2 });
 
           mylat = 0;
           mylng = 0;
               }
               startUp();
           } else {
-              document.getElementById('submitshortresult').innerHTML = '<em>Please wait, geolocating you...</em>';
-              downloadUrl("http://api.hostip.info/get_html.php?position=true",
-                          function(data) {
-                              var baseinfomessage = 'This is where I located you,<br/>based on your IP address.<br/>';
-                              if (data.match(/Latitude: (\S+)/)) {
-                                  mylat = RegExp.$1;
-                                  if (data.match(/Longitude: (\S+)/)) {
-                                      mylng = RegExp.$1;
-                                  }
-                              } else if (data.match(/Country: .*?\((\S{2})\)/)) {
-                                  var country = RegExp.$1;
-                                  if (country == 'XX') {
-                                      map.setZoom(2);
-                                      baseinfomessage = 'It was not possible to locate<br/>you, based on your IP address<br/>'
-                                                        + '<a href="http://www.hostip.info/">(submit your location!)</a>.<br/>';
-                                  } else {
-                                      map.setZoom(5);
-                                      mylat = eval("lats." + country);
-                                      mylng = eval("lngs." + country);
-                                  }
-                              }
-                              me = startUp();
-                              document.getElementById('submitshortresult').innerHTML = '';
-                              document.getElementById('submitshortresult').style.color = 'red';
-                              info_window = new google.maps.InfoWindow({ content: baseinfomessage
-                                                                                  + 'You can drag this icon<br/>to move your position.<br/>'
-                                                                                  + '<div class="fakelink" style="float: right" onclick="javascript:info_window.close()">Okay</div>' });
-                              info_window.open(map, me);
-                          });
+              <?php
+                  if ($latitude) {
+               ?>
+              mylat = <?php echo $latitude ?>;
+              mylng = <?php echo $longitude ?>;
+              var baseinfomessage = 'This is where I located you,<br/>based on your IP address.<br/>';
+              <?php
+                  } else {
+               ?>
+              var baseinfomessage = 'It was not possible to locate<br/>you, based on your IP address<br/>'
+                                  + '<a href="http://www.hostip.info/">(submit your location!)</a>.<br/>';
+              <?php
+                  }
+               ?>
+              map.setZoom(<?php echo $zoom ?>);
+              me = startUp();
+              info_window = new google.maps.InfoWindow({ content: baseinfomessage
+                                                                  + 'You can drag this icon<br/>to move your position.<br/>'
+                                                                  + '<div class="fakelink" style="float: right" onclick="javascript:info_window.close()">Okay</div>' });
+              info_window.open(map, me);
               if (!document.cookie || document.cookie.indexOf("not-a-newbie") == -1) {
                   document.getElementById("newbie").style.display = '';
               }
       </div>
 
       <form action="javascript:submit()">
-      Type in an address:
+      Address:
       
       <input type="text" size="32" id="address"/>
       <script type="text/javascript">
         document.getElementById('address').focus();
       </script>
       <input type="submit" value="Show route!"/>
-      <span id="submitshortresult">
+      <span id="submitshortresult" style="color: red">
       </span>
       </form>