Skip to content
Snippets Groups Projects
Commit 57c00dfc authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Moved agency-specific stuff out of code and into config.

parent 3fbb3ce6
No related branches found
No related tags found
No related merge requests found
<?php <?php
/* /*
This file serves up data for asynchronous requests (usually as JSON). This file serves up data for asynchronous requests (usually as JSON).
*/ */
require_once('config.php');
require_once('functions.php');
if (!isset($_GET['command'])) die(); if (!isset($_GET['command'])) die();
$agency = "rutgers"; $agency = "rutgers";
if ($_GET['command'] == "vehicleLocations") { if ($_GET['command'] == "vehicleLocations") {
// return a JSON array of vehicles and their locations. // return a JSON array of vehicles and their locations.
require_once('config.php'); $db = dbc();
require_once('functions.php');
// MAX age of a bus location to return
$db = dbc(); $maxAge = 120;
// MAX age of a bus location to return $time = 0;
$maxAge = 120; if (isset($_GET['t'])) $time = $db->real_escape_string((int)$_GET['t']);
if ($time < $maxAge) $time = time() - $maxAge;
$time = 0; $sql = <<< V_LOC
if (isset($_GET['t'])) $time = $db->real_escape_string((int)$_GET['t']); SELECT
if ($time < $maxAge) $time = time() - $maxAge; `id`, `title` AS `route`, `routeTag`, `dirTag`, `lat`, `lon`, `heading`, `speedKmHr`
$sql = <<< V_LOC FROM `vehicleLocations`
SELECT LEFT JOIN `routes` ON `vehicleLocations`.`routeTag` = `routes`.`tag`
`id`, `title` AS `route`, `routeTag`, `dirTag`, `lat`, `lon`, `heading`, `speedKmHr` WHERE `time` > '{$time}'
FROM `vehicleLocations`
LEFT JOIN `routes` ON `vehicleLocations`.`routeTag` = `routes`.`tag`
WHERE `time` > '{$time}'
V_LOC; V_LOC;
$result = dbq($db, $sql); $result = dbq($db, $sql);
$locations = array(); $locations = array();
while($row = $result->fetch_assoc()) $locations[$row['id']] = $row; while($row = $result->fetch_assoc()) $locations[$row['id']] = $row;
// predictions // predictions
$sql = <<< V_LOC $sql = <<< V_LOC
SELECT SELECT
`predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime` `predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime`
FROM `predictions` FROM `predictions`
LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag` LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag`
ORDER BY `epochTime` ASC ORDER BY `epochTime` ASC
V_LOC; V_LOC;
$result = dbq($db, $sql); $result = dbq($db, $sql);
while($row = $result->fetch_assoc()) if (isset($locations[$row['vehicle']])) $locations[$row['vehicle']]['stops'][] = $row; while($row = $result->fetch_assoc()) if (isset($locations[$row['vehicle']])) $locations[$row['vehicle']]['stops'][] = $row;
foreach ($locations as $key => $location) { foreach ($locations as $key => $location) {
$locations[$key]['route'] = cleanRouteName($location['route']); $locations[$key]['route'] = cleanRouteName($location['route']);
$locations[$key]['stops'] = array_slice($location['stops'], 0, 8); $locations[$key]['stops'] = array_slice($location['stops'], 0, 8);
} }
$lastFetched = 0; $lastFetched = 0;
$result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'vehicleLocations' LIMIT 1", true); $result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'vehicleLocations' LIMIT 1", true);
if ($result->num_rows == 1) { if ($result->num_rows == 1) {
$arr = $result->fetch_array(); $arr = $result->fetch_array();
$lastFetched = $arr[0]; $lastFetched = $arr[0];
} }
// also return lastTime! // also return lastTime!
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode(array( echo json_encode(array(
"vehicles" => $locations, "vehicles" => $locations,
"lastTime" => $lastFetched, "lastTime" => $lastFetched,
)); ));
} elseif ($_GET['command'] == "routeConfig") { } elseif ($_GET['command'] == "routeConfig") {
// return a JSON array of routes and stops. // return a JSON array of routes and stops.
require_once('config.php');
require_once('functions.php'); $db = dbc();
$db = dbc(); $sql = "SELECT `tag`, `title` FROM `routes`";
$result = dbq($db, $sql);
$sql = "SELECT `tag`, `title` FROM `routes`"; $routes = array();
$result = dbq($db, $sql); while($row = $result->fetch_assoc()) $routes[$row['tag']] = $row;
$routes = array();
while($row = $result->fetch_assoc()) $routes[$row['tag']] = $row; $sql = "SELECT `tag`, `title`, `lat`, `lon`, `stopId` FROM `stops`";
$result = dbq($db, $sql);
$sql = "SELECT `tag`, `title`, `lat`, `lon`, `stopId` FROM `stops`"; $stops = array();
$result = dbq($db, $sql); while($row = $result->fetch_assoc()) {
$stops = array(); $stops[$row['tag']] = $row;
while($row = $result->fetch_assoc()) { $stops[$row['tag']]['routes'] = array();
$stops[$row['tag']] = $row; }
$stops[$row['tag']]['routes'] = array();
}
$sql = "SELECT `route`, `stop` FROM `route_stops`";
$result = dbq($db, $sql);
while($row = $result->fetch_assoc()) $stops[$row['stop']]['routes'][$row['route']] = array();
$sql = "SELECT `routes`.`tag`, `routes`.`title`, `stopTag`, `epochTime`, `vehicle` FROM `predictions` LEFT JOIN `routes` ON `predictions`.`routeTag` = `routes`.`tag` WHERE `routes`.`agency` = '{$agency}'";
$result = dbq($db, $sql);
while($row = $result->fetch_assoc()) $stops[$row['stopTag']]['routes'][$row['tag']][] = array("epochTime" => $row['epochTime'], "vehicle" => $row['vehicle']);
// post proc
foreach ($routes as $key => $route) {
$routes[$key]['title'] = cleanRouteName($route['title']);
}
$unset = array();
foreach ($stops as $key => $stop) {
if (!in_array($key, $unset)) {
foreach ($stops as $key2 => $stop2) {
if ($key !== $key2 && $stop['title'] == $stop2['title']) {
$stops[$key]['routes'] = array_merge($stop['routes'], $stop2['routes']);
$unset[] = $key2;
}
}
}
}
foreach ($unset as $un) unset($stops[$un]);
$lastFetched = 0;
$result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'routeConfig' LIMIT 1", true);
if ($result->num_rows == 1) {
$arr = $result->fetch_array();
$lastFetched = $arr[0];
}
// also return lastTime!
header("Content-type: application/json");
echo json_encode(array(
"routes" => $routes,
"stops" => $stops,
"lastTime" => $lastFetched,
));
}
function cleanRouteName($name) { $sql = "SELECT `route`, `stop` FROM `route_stops`";
if ($name == "New Brunsquick 1 Shuttle") $name = "NB 1"; $result = dbq($db, $sql);
else if ($name == "New Brunsquick 2 Shuttle") $name = "NB 2"; while($row = $result->fetch_assoc()) $stops[$row['stop']]['routes'][$row['route']] = array();
return $name;
$sql = "SELECT `routes`.`tag`, `routes`.`title`, `stopTag`, `epochTime`, `vehicle` FROM `predictions` LEFT JOIN `routes` ON `predictions`.`routeTag` = `routes`.`tag` WHERE `routes`.`agency` = '{$agency}'";
$result = dbq($db, $sql);
while($row = $result->fetch_assoc()) $stops[$row['stopTag']]['routes'][$row['tag']][] = array("epochTime" => $row['epochTime'], "vehicle" => $row['vehicle']);
// post proc
foreach ($routes as $key => $route) {
$routes[$key]['title'] = cleanRouteName($route['title']);
}
$unset = array();
foreach ($stops as $key => $stop) {
if (!in_array($key, $unset)) {
foreach ($stops as $key2 => $stop2) {
if ($key !== $key2 && $stop['title'] == $stop2['title']) {
$stops[$key]['routes'] = array_merge($stop['routes'], $stop2['routes']);
$unset[] = $key2;
}
}
}
}
foreach ($unset as $un) unset($stops[$un]);
$lastFetched = 0;
$result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'routeConfig' LIMIT 1", true);
if ($result->num_rows == 1) {
$arr = $result->fetch_array();
$lastFetched = $arr[0];
}
// also return lastTime!
header("Content-type: application/json");
echo json_encode(array(
"routes" => $routes,
"stops" => $stops,
"lastTime" => $lastFetched,
));
} }
?> ?>
...@@ -17,8 +17,6 @@ div#topBar { ...@@ -17,8 +17,6 @@ div#topBar {
display: table; display: table;
width: 100%; width: 100%;
z-index: 1; z-index: 1;
background-color: #222; background-color: #222;
border-top: 2px solid #555; border-top: 2px solid #555;
border-bottom: 2px solid #555; border-bottom: 2px solid #555;
...@@ -240,14 +238,15 @@ a.tooltip { ...@@ -240,14 +238,15 @@ a.tooltip {
screen and (-webkit-min-device-pixel-ratio: 1.5), screen and (-webkit-min-device-pixel-ratio: 1.5),
screen and (-moz-min-device-pixel-ratio: 1.5), screen and (-moz-min-device-pixel-ratio: 1.5),
screen and (min-device-pixel-ratio: 1.5) { screen and (min-device-pixel-ratio: 1.5) {
html {
font-size: 0.5em;
line-height: 0.5em;
}
div#routeselector { div#routeselector {
width: 100%; width: 100%;
min-height: 56px;
line-height:56px;
} }
div#routeselector .bar { div#routeselector .bar {
font-size: 20px; /* 1em */
width: 10em; width: 10em;
height: 1em; height: 1em;
position: relative; position: relative;
...@@ -255,22 +254,12 @@ screen and (min-device-pixel-ratio: 1.5) { ...@@ -255,22 +254,12 @@ screen and (min-device-pixel-ratio: 1.5) {
} }
div#routeselector .routeBtn { div#routeselector .routeBtn {
font-size: 1.5em;
margin-bottom: 0px; margin-bottom: 0px;
} }
div#lastUpdate { div#lastUpdate {
display:none !important; display:none !important;
} }
div#locationButton {
width: 64px; }
height: 64px;
background-size: 64px 64px;
}
.leaflet-popup-content {
font-size: 2em;
}
}
\ No newline at end of file
...@@ -25,9 +25,15 @@ function dbq($db, $query, $dieOnError = false) { ...@@ -25,9 +25,15 @@ function dbq($db, $query, $dieOnError = false) {
} else return $result; } else return $result;
} }
function cleanRouteName($name) {
if ($name == "New Brunsquick 1 Shuttle") $name = "NB 1";
else if ($name == "New Brunsquick 2 Shuttle") $name = "NB 2";
return $name;
}
function sanitize($string) { function sanitize($string) {
$pattern = "/[^\w.-\s]/"; $pattern = "/[^\w.-\s]/";
return preg_replace($pattern,"",$string); return preg_replace($pattern,"",$string);
} }
?> ?>
\ No newline at end of file
<?php
require_once('config.php');
?>
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<title>RU Bus Map</title> <title><?php echo $cfg['agency']['name']; ?></title>
<meta name="description" content="Real-time animated map of the bus system at Rutgers University New Brunswick."> <meta name="description" content=<?php echo json_encode($cfg['agency']['description']); ?>>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=0.5; maximum-scale=0.5; user-scalable=no;" /> <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1; user-scalable=no;" />
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link href="css/leaflet.css" rel="stylesheet" /> <link href="css/leaflet.css" rel="stylesheet" />
<link href="css/leaflet.label.css" rel="stylesheet" /> <link href="css/leaflet.label.css" rel="stylesheet" />
<link href="css/style.css" rel="stylesheet" /> <link href="css/style.css" rel="stylesheet" />
<?php if (isset($_GET['embed'])) { ?> <?php if (isset($_GET['embed'])): ?>
<link href="css/embed.css" rel="stylesheet" /> <link href="css/embed.css" rel="stylesheet" />
<?php } ?> <?php endif; ?>
<script src="js/fastclick.js"></script> <script>
<script src="js/jquery.min.js"></script> window.agency = <?php echo json_encode($cfg['agency']['name']); ?>;
<script src="js/leaflet.js"></script> window.busIconUrl = <?php echo json_encode($cfg['agency']['busIconUrl'], JSON_UNESCAPED_SLASHES); ?>;
<script src="js/leaflet.marker.rotate.js"></script> </script>
<script src="js/leaflet.label.js"></script> <script src="js/fastclick.js"></script>
<script src="js/jquery.raptorize.js"></script> <script src="js/jquery.min.js"></script>
<script src="js/script.js"></script> <script src="js/leaflet.js"></script>
<?php <script src="js/leaflet.marker.rotate.js"></script>
if (isset($_GET['centerLat']) && isset($_GET['centerLon'])) { <script src="js/leaflet.label.js"></script>
$lat = doubleval($_GET['centerLat']); <script src="js/jquery.raptorize.js"></script>
$lon = doubleval($_GET['centerLon']); <script src="js/script.js"></script>
if (isset($_GET['zoom'])) $zoom = intval($_GET['zoom']); <script>
else $zoom = 'undefined'; $(document).ready(function() {
echo '<script>$(document).ready(function(){ window.map.setView(['.$lat.','.$lon.'], '.$zoom.'); });</script>'; // setup and create tile layer on map
} L.tileLayer(<?php echo json_encode($cfg['agency']['tileUrl'], JSON_UNESCAPED_SLASHES); ?>, {
?> subdomains: <?php echo json_encode($cfg['agency']['tileSubdomains']); ?>,
tileset: <?php echo json_encode($cfg['agency']['tileset']); ?>,
errorTileUrl: <?php echo json_encode($cfg['agency']['errorTileUrl'], JSON_UNESCAPED_SLASHES); ?>,
attribution: <?php echo json_encode($cfg['agency']['attribution'], JSON_UNESCAPED_SLASHES); ?>,
detectRetina: true,
}).addTo(window.map);
});
</script>
<?php
// Set initial lat/lon if requested.
if (isset($_GET['centerLat']) && isset($_GET['centerLon'])) {
// sanitize user inputs by converting them to doubles/ints.
$lat = doubleval($_GET['centerLat']);
$lon = doubleval($_GET['centerLon']);
if (isset($_GET['zoom'])) $zoom = intval($_GET['zoom']);
else $zoom = 'undefined';
echo '<script>$(document).ready(function(){ window.map.setView(['.$lat.','.$lon.'], '.$zoom.'); });</script>';
}
?>
</head> </head>
<body> <body>
<div id="container"> <div id="container">
<div id="topBar"> <div id="topBar">
<div id="routeselector">Please enable Javascript to use this map.</div> <div id="routeselector">Please enable Javascript to use this map.</div>
</div> </div>
<div id="map"></div> <div id="map"></div>
<div id="locationButton"></div> <div id="locationButton"></div>
</div> </div>
<script>
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://analytics.ant.sr/";
_paq.push(["setTrackerUrl", u+"piwik.php"]);
_paq.push(["setSiteId", "3"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script>
</body> </body>
</html> </html>
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment