Skip to content

Commit

Permalink
Supports jsonp callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Sep 30, 2012
1 parent 5c4daa3 commit 7fdaae2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
<?php
include('inc.php');

if(preg_match('|^/timezone/([0-9\.-]+)/([0-9\.-]+)$|', $_SERVER['REQUEST_URI'], $match) || (get('latitude') && get('longitude'))) {
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);

header('Content-type: application/json');

if($timezone) {
$tz = new DateTimeZone($timezone);
$now = new DateTime(date('c'));
$now->setTimeZone($tz);

echo json_encode(array(
respond(array(
'timezone' => $timezone,
'offset' => $now->format('P'),
'seconds' => (int)$now->format('Z')
));
} else {
echo json_encode(array(
respond(array(
'error' => 'no_data'
));
}

} else {
header('HTTP/1.1 404 Not Found');
header('Content-type: application/json');
echo json_encode(array(
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);
}
}

0 comments on commit 7fdaae2

Please sign in to comment.