forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode.php
More file actions
48 lines (34 loc) · 1.23 KB
/
geocode.php
File metadata and controls
48 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once "rate_limit.php";
if ( ! isset($CFG->GEOCODE_KEY) ) {
die('$CFG->GEOCODE_KEY not set');
}
$address = false;
if ( isset($_GET['address']) ) $address = $_GET['address'];
if ( ! $address && isset($_GET['query']) ) $address = $_GET['query'];
if ( ! $address ) {
die("No address...");
}
if ( ! isset($_GET['key']) || $_GET['key'] != '42' ) {
die("Missing/incorrect key= parameter (it is an easy number to guess)...");
}
$ipaddr = \Tsugi\Util\Net::getIP();
$delta = check_rate_limit('/tmp/geocode.db', $ipaddr, $address);
$do_json = strpos($_SERVER['REQUEST_URI'],'/xml') === false;
$fragment = $do_json ? 'json' : 'xml';
$serviceurl = "https://maps.googleapis.com/maps/api/geocode/$fragment?key=".$CFG->GEOCODE_KEY."&address=";
if ( $do_json ) {
header('Content-Type: application/json');
} else {
header('Content-Type: application/xml');
}
$url = $serviceurl . urlencode($address);
if ( filter_bad_things($address, $ipaddr) ) return;
error_log("geocode $address $ipaddr $delta");
if ( $delta < 7 ) sleep(7);
$contents = @file_get_contents($url);
if ( ! is_string($contents) ) {
error_log(strlen($contents)." bytes retrieved in error ".$url);
$contents = '{ "error": "Error from Google" }';
}
echo($contents);