-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
45 lines (38 loc) · 1.06 KB
/
index.php
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
<?php
include('inc.php');
if(preg_match('|^/timezone/([0-9\.-]+)/([0-9\.-]+)|', $_SERVER['REQUEST_URI'], $match) || (get('latitude') && get('longitude'))) {
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS, array(PDO::ATTR_PERSISTENT => TRUE));
$latitude = get('latitude') ?: $match[1];
$longitude = get('longitude') ?: $match[2];
$timezone = timezoneFromLocation($latitude, $longitude);
if($timezone) {
$tz = new DateTimeZone($timezone);
$now = new DateTime(date('c'));
$now->setTimeZone($tz);
respond(array(
'timezone' => $timezone,
'offset' => $now->format('P'),
'seconds' => (int)$now->format('Z')
));
} else {
respond(array(
'error' => 'no_data'
));
}
} else {
header('HTTP/1.1 404 Not Found');
respond(array(
'error' => 'not_found'
));
}
function respond($data) {
header('Content-type: application/json');
if(preg_match('/callback=([^\&]+)/', $_SERVER['REQUEST_URI'], $match) || get('callback')) {
$cb = get('callback') ?: $match[1];
echo $cb . '(';
echo json_encode($data);
echo ')';
} else {
echo json_encode($data);
}
}