Add php protobuffer support for transition to GTFS-realtime
Add php protobuffer support for transition to GTFS-realtime

file:b/.gitignore (new)
--- /dev/null
+++ b/.gitignore
@@ -1,1 +1,8 @@
 
+/labs/tiles/12
+/labs/tiles/13
+/labs/tiles/14
+/labs/tiles/15
+/labs/tiles/16
+/labs/tiles/17
+/labs/tiles/19

--- a/aws/awsStartup.sh
+++ b/aws/awsStartup.sh
@@ -5,38 +5,9 @@
 #postgres postgres-server php-pg
 #http://www.how2forge.org/installing-lighttpd-with-php5-and-mysql-support-on-fedora-12
 
-cp /root/aws.php /tmp/
-mkdir /var/www/lib/staticmaplite/cache 
-chcon -h system_u:object_r:httpd_sys_content_t /var/www
-chcon -R -h root:object_r:httpd_sys_content_t /var/www/*
-chcon -R -t httpd_sys_content_rw_t /var/www/lib/staticmaplite/cache
-chmod -R 777 /var/www/lib/staticmaplite/cache 
-chcon -R -t httpd_sys_content_rw_t /var/www/labs/tiles
-chmod -R 777 /var/www/labs/tiles
-wget http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip \
--O /var/www/cbrfeed.zip
+sh busuiphp.sh
+sh busuidb.sh
+sh busuiotp.sh
 
-createdb transitdata
-createlang -d transitdata plpgsql
-psql -d transitdata -f /var/www/lib/postgis.sql
-# curl https://github.com/maxious/ACTBus-ui/raw/master/transitdata.cbrfeed.sql.gz -o transitdata.cbrfeed.sql.gz 
-#made with pg_dump transitdata | gzip -c >  transitdata.cbrfeed.sql.gz
-gunzip /var/www/transitdata.cbrfeed.sql.gz
-psql -d transitdata -f /var/www/transitdata.cbrfeed.sql
-#createuser transitdata -SDRP
-#password transitdata
-#psql -d transitdata -c \"GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips TO transitdata;\"
-#psql -d transitdata -c "GRANT SELECT,INSERT ON	TABLE myway_observations,myway_routes,myway_stops,myway_timingdeltas TO transitdata;"
-#psql -d transitdata -c	"GRANT SELECT,INSERT,UPDATE ON TABLE myway_routes,myway_stops TO transitdata;"
-##psql -d transitdata -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO transitdata;"
-php /var/www/updatedb.php
 
-wget http://s3-ap-southeast-1.amazonaws.com/busresources/Graph.obj \
--O /tmp/Graph.obj
-rm -rfv /usr/share/tomcat6/webapps/opentripplanner*
-wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-webapp.war \
--O /usr/share/tomcat6/webapps/opentripplanner-webapp.war
-wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-api-webapp.war \
--O /usr/share/tomcat6/webapps/opentripplanner-api-webapp.war
-/etc/init.d/tomcat6 restart
 

file:b/aws/busuidb.sh (new)
--- /dev/null
+++ b/aws/busuidb.sh
@@ -1,1 +1,14 @@
-
+createdb transitdata
+createlang -d transitdata plpgsql
+psql -d transitdata -f /var/www/lib/postgis.sql
+# curl https://github.com/maxious/ACTBus-ui/raw/master/transitdata.cbrfeed.sql.gz -o transitdata.cbrfeed.sql.gz 
+#made with pg_dump transitdata | gzip -c >  transitdata.cbrfeed.sql.gz
+gunzip /var/www/transitdata.cbrfeed.sql.gz
+psql -d transitdata -f /var/www/transitdata.cbrfeed.sql
+#createuser transitdata -SDRP
+#password transitdata
+#psql -d transitdata -c "GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips TO transitdata;"
+#psql -d transitdata -c "GRANT SELECT,INSERT ON	TABLE myway_observations,myway_routes,myway_stops,myway_timingdeltas TO transitdata;"
+#psql -d transitdata -c	"GRANT SELECT,INSERT,UPDATE ON TABLE myway_routes,myway_stops TO transitdata;"
+##psql -d transitdata -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO transitdata;"
+php /var/www/updatedb.php

file:b/aws/busuiotp.sh (new)
--- /dev/null
+++ b/aws/busuiotp.sh
@@ -1,1 +1,10 @@
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/Graph.obj \
+-O /tmp/Graph.obj
+/etc/init.d/tomcat6 stop
+rm -rfv /usr/share/tomcat6/webapps/opentripplanner*
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-webapp.war \
+-O /usr/share/tomcat6/webapps/opentripplanner-webapp.war
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-api-webapp.war \
+-O /usr/share/tomcat6/webapps/opentripplanner-api-webapp.war
+/etc/init.d/tomcat6 restart
 

--- /dev/null
+++ b/aws/busuiotp.testing.sh
@@ -1,1 +1,10 @@
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/testing/Graph.obj \
+-O /tmp/Graph.obj
+/etc/init.d/tomcat6 stop
+rm -rfv /usr/share/tomcat6/webapps/opentripplanner*
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/testing/opentripplanner-webapp.war \
+-O /usr/share/tomcat6/webapps/opentripplanner-webapp.war
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/testing/opentripplanner-api-webapp.war \
+-O /usr/share/tomcat6/webapps/opentripplanner-api-webapp.war
+/etc/init.d/tomcat6 restart
 

file:b/aws/busuiphp.sh (new)
--- /dev/null
+++ b/aws/busuiphp.sh
@@ -1,1 +1,16 @@
+cp /root/aws.php /tmp/
+mkdir /var/www/lib/staticmaplite/cache 
+chcon -h system_u:object_r:httpd_sys_content_t /var/www
+chcon -R -h root:object_r:httpd_sys_content_t /var/www/*
 
+chcon -R -t httpd_sys_content_rw_t /var/www/lib/staticmaplite/cache
+chmod -R 777 /var/www/lib/staticmaplite/cache 
+
+chcon -R -t httpd_sys_content_rw_t /var/www/labs/tiles
+chmod -R 777 /var/www/labs/tiles
+
+chcon -R -t httpd_sys_content_rw_t /var/www/lib/openid-php/oid_store
+chmod -R 777 /var/www/lib/openid-php/oid_store
+
+wget http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip \
+-O /var/www/cbrfeed.zip

--- /dev/null
+++ b/aws/data-sources.xml
@@ -1,1 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
+        <!-- Single graph -->
+        <import resource="classpath:org/opentripplanner/api/application-context.xml" />
+
+        <bean id="graphBundle" class="org.opentripplanner.model.GraphBundle">
+                <property name="path" value="/tmp/" />
+        </bean>
+
+</beans>
+

--- /dev/null
+++ b/include/common-auth.inc.php
@@ -1,1 +1,91 @@
+<?php
+function getScheme()
+{
+     $scheme = 'http';
+     if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
+        $scheme .= 's';
+         } 
+    return $scheme;
+    } 
 
+function getTrustRoot()
+{
+     return sprintf("%s://%s:%s%s/",
+         getScheme(), $_SERVER['SERVER_NAME'],
+         $_SERVER['SERVER_PORT'],
+         dirname($_SERVER['PHP_SELF']));
+    } 
+
+
+// Includes required files
+set_include_path(get_include_path() . PATH_SEPARATOR . $labsPath."lib/openid-php/");
+require_once "Auth/OpenID/Consumer.php";
+require_once "Auth/OpenID/FileStore.php";
+require_once "Auth/OpenID/AX.php";
+
+
+
+function login()
+{
+  // Just tested this with/for Google, needs trying with others ...
+$oid_identifier = 'https://www.google.com/accounts/o8/id';
+    // Create file storage area for OpenID data
+    $store = new Auth_OpenID_FileStore('lib/openid-php/oid_store');
+    // Create OpenID consumer
+    $consumer = new Auth_OpenID_Consumer($store);
+    // Create an authentication request to the OpenID provider
+    $auth = $consumer -> begin($oid_identifier);
+    
+    // Create attribute request object
+    // See http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters
+    // Usage: make($type_uri, $count=1, $required=false, $alias=null)
+    $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/contact/email', 2, 1, 'email');
+    $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/namePerson/first', 1, 1, 'firstname');
+    $attribute[] = Auth_OpenID_AX_AttrInfo :: make('http://axschema.org/namePerson/last', 1, 1, 'lastname');
+    
+    // Create AX fetch request
+    $ax = new Auth_OpenID_AX_FetchRequest;
+    
+    // Add attributes to AX fetch request
+    foreach($attribute as $attr) {
+        $ax -> add($attr);
+        } 
+    
+    // Add AX fetch request to authentication request
+    $auth -> addExtension($ax);
+    $_SESSION['returnURL'] = curPageURL();
+    // Redirect to OpenID provider for authentication
+    $url = $auth -> redirectURL(getTrustRoot(), $_SESSION['returnURL']);
+    header('Location: ' . $url);
+    } 
+
+
+function auth()
+
+{
+  if ($_SESSION['authed'] == true) return true;
+
+     // Create file storage area for OpenID data
+    $store = new Auth_OpenID_FileStore('lib/openid-php/oid_store');
+     // Create OpenID consumer
+    $consumer = new Auth_OpenID_Consumer($store);
+     // Create an authentication request to the OpenID provider
+    $response = $consumer -> complete($_SESSION['returnURL']);
+    
+     if ($response -> status == Auth_OpenID_SUCCESS) {
+        // Get registration informations
+        $ax = new Auth_OpenID_AX_FetchResponse();
+         $obj = $ax -> fromSuccessResponse($response);
+         $email = $obj -> data['http://axschema.org/contact/email'][0];
+         var_dump($email);
+         if ($email != "maxious@gmail.com") {
+            die("Access Denied");
+             } else {
+               $_SESSION['authed'] = true;
+             }
+        } else {
+        login();
+         } 
+    } 
+    if ($_REQUEST['janrain_nonce']) auth();
+?>

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -25,7 +25,7 @@
 }
 function include_header($pageTitle, $pageType, $opendiv = true, $geolocate = false, $datepicker = false)
 {
-	global $labsPath;
+	global $labsPath,$serviceAlertsEnabled;
 	echo '
 <!DOCTYPE html> 
 <html lang="en">
@@ -156,6 +156,13 @@
 				echo '<div id="servicewarning">Buses are running on an altered timetable today due to industrial action/public holiday. See <a href="http://www.action.act.gov.au">http://www.action.act.gov.au</a> for details.</div>';
 			}
 		}
+		if ($serviceAlertsEnabled) {
+		$serviceAlerts = getServiceAlerts("network","network");
+		foreach ($serviceAlerts['entities'] as $entity) {
+			echo "<div id='servicewarning'>".date("F j, g:i a",strtotime($entity['alert']['active_period']['start']))." to ". date("F j, g:i a", strtotime($entity['alert']['active_period']['end']))."{$entity['alert']['header_text']['translation']['text']}<br>Warning: {$entity['alert']['description_text']['translation']['text']} 
+			<br><a href='{$entity['alert']['url']['translation']['text']}'>Source</a>  </div>";
+		}
+	}
 	}
 }
 function include_footer()

--- a/include/common-transit.inc.php
+++ b/include/common-transit.inc.php
@@ -45,5 +45,122 @@
 		return "";
 	}
 }
+
+$serviceAlertCause = Array(
+UNKNOWN_CAUSE
+OTHER_CAUSE
+TECHNICAL_PROBLEM
+STRIKE
+DEMONSTRATION
+ACCIDENT
+HOLIDAY
+WEATHER
+MAINTENANCE
+CONSTRUCTION
+POLICE_ACTIVITY
+MEDICAL_EMERGENCY
+
+Unknown cause
+Other cause (not represented by any of these options)
+Technical problem
+Strike
+Demonstration
+Accident
+Holiday
+Weather
+Maintenance
+Construction
+Police activity
+Medical emergency
+);
+$serviceAlertEffect = Array(
+NO_SERVICE
+REDUCED_SERVICE
+SIGNIFICANT_DELAYS
+DETOUR
+ADDITIONAL_SERVICE
+MODIFIED_SERVICE
+OTHER_EFFECT
+UNKNOWN_EFFECT
+STOP_MOVED
+
+No service
+Reduced service
+Significant delays (insignificant delays should only be provided through Trip updates).
+Detour
+Additional service
+Modified service
+Stop moved
+Other effect (not represented by any of these options)
+Unknown effect);
+
+function getServiceAlerts($filter_class, $filter_id) {
+/*
+
+  also need last modified epoch of client gtfs
+  
+         - add,remove,patch,inform (null)
+            - stop
+            - trip
+            - network
+          - classes (WHERE=)
+            - route (short_name or route_id)
+            - street
+            - stop
+            - trip 
+            Currently support:
+            network inform
+            trip patch: stop remove
+            street inform: route inform, trip inform, stop inform
+            route patch: trip remove
+            */
+$return = Array();
+$return['header']['gtfs_realtime_version'] = "1";
+$return['header']['timestamp'] = time();
+$return['header']['incrementality'] =  "FULL_DATASET";
+$return['entities'] = Array();
+foreach(getCurrentAlerts() as $alert) {
+	$informedEntities = getInformedAlerts($alert['id'],$_REQUEST['filter_class'],$_REQUEST['filter_id']);
+	if (sizeof($informedEntities) >0) {
+		$entity = Array();
+		$entity['id'] = $alert['id'];
+		$entity['alert']['active_period']['start'] = $alert['start'];
+		$entity['alert']['active_period']['end'] = $alert['end'];
+		$entity['alert']['url']['translation']['text'] = $alert['url'];
+		$entity['alert']['url']['translation']['language'] = 'en';
+		$entity['alert']['header_text']['translation']['text'] = $alert['header'];
+		$entity['alert']['header_text']['translation']['language'] = 'en';
+		$entity['alert']['description_text']['translation']['text'] = $alert['description'];
+		$entity['alert']['description_text']['translation']['language'] = 'en';
+		
+		foreach ($informedEntities as $informedEntity) {
+			$informed = Array();
+			$informed[$informedEntity['informed_class']."_id"] = $informedEntity['informed_id'];
+			if ($informedEntity['informed_action'] != "") $informed["x-action"] = $informedEntity['informed_action'];
+			$informed[$informedEntity['class']."_type"] = $informedEntity['type'];
+			$entity['informed'][] = $informed; 
+		}
+		$return['entities'][] = $entity;
+	}
+}
+return $return;
+}
+function getServiceAlertsByClass() {
+	$return = Array();
+	$alerts = getServiceAlerts("","");
+	foreach ($alerts['entities'] as $entity) {
+		foreach ($entity['informed'] as $informed) {
+			foreach($informed as $key => $value){
+				if (strpos("_id",$key) > 0) {
+					$parts = explode($key);
+					$class = $parts[0];
+					$id = $value;
+				}
+			}
+		$return[$class][$id][]['entity'] = $entity;
+		$return[$class][$id][]['action'] = $informed["x-action"];
+	}
+	}
+}
 ?>
 

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -10,6 +10,7 @@
 	"database",
 	"other"
 );
+$serviceAlertsEnabled = true;
 $cloudmadeAPIkey = "daa03470bb8740298d4b10e3f03d63e6";
 $googleMapsAPIkey = "ABQIAAAA95XYXN0cki3Yj_Sb71CFvBTPaLd08ONybQDjcH_VdYtHHLgZvRTw2INzI_m17_IoOUqH3RNNmlTk1Q";
 $otpAPIurl = 'http://localhost:8080/opentripplanner-api-webapp/';
@@ -42,6 +43,7 @@
 
 include_once ("common-request.inc.php");
 include_once ("common-session.inc.php");
+include_once ("common-auth.inc.php");
 include_once ("common-template.inc.php");
 
 
@@ -54,6 +56,7 @@
 	global $debugOkay;
 	return in_array($debugReason, $debugOkay, false) && isDebugServer();
 }
+
 function debug($msg, $debugReason = "other")
 {
 	if (isDebug($debugReason)) echo "\n<!-- " . date(DATE_RFC822) . "\n $msg -->\n";
@@ -186,5 +189,6 @@
   } 
   return implode( $glue, $retVal ); 
 } 
+
 ?>
 

--- a/include/db/route-dao.inc.php
+++ b/include/db/route-dao.inc.php
@@ -1,211 +1,222 @@
 <?php
 function getRoute($routeID)
-{
-	global $conn;
-	$query = "Select * from routes where route_id = :routeID LIMIT 1";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":routeID", $routeID);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetch(PDO::FETCH_ASSOC);
-}
+
+{
+     global $conn;
+     $query = "Select * from routes where route_id = :routeID LIMIT 1";
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":routeID", $routeID);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetch(PDO :: FETCH_ASSOC);
+    } 
 
 function getRouteByFullName($routeFullName)
-{
-	global $conn;
-	$query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":routeFullName", $routeFullName);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetch(PDO::FETCH_ASSOC);
-}
+
+{
+     global $conn;
+     $query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1";
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":routeFullName", $routeFullName);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetch(PDO :: FETCH_ASSOC);
+    } 
 
 function getRoutes()
-{
-	global $conn;
-	$query = "Select * from routes order by route_short_name;";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+
+{
+     global $conn;
+     $query = "Select * from routes order by route_short_name;";
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetchAll();
+    } 
 function getRoutesByNumber($routeNumber = "")
-{
-	global $conn;
-	if ($routeNumber != "") {
-		$query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes  join trips on trips.route_id =
+
+{
+     global $conn;
+     if ($routeNumber != "") {
+        $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes  join trips on trips.route_id =
 routes.route_id join stop_times on stop_times.trip_id = trips.trip_id
 where route_short_name = :routeNumber OR route_short_name LIKE :routeNumber2 order by route_short_name;";
-	}
-	else {
-		$query = "SELECT DISTINCT route_short_name from routes order by route_short_name";
-	}
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	if ($routeNumber != "") {
-		$query->bindParam(":routeNumber", $routeNumber);
-                $routeNumber2 = "% ".$routeNumber;
-		$query->bindParam(":routeNumber2", $routeNumber2);
-	}
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+         } 
+    else {
+        $query = "SELECT DISTINCT route_short_name from routes order by route_short_name";
+         } 
+    debug($query, "database");
+     $query = $conn -> prepare($query);
+     if ($routeNumber != "") {
+        $query -> bindParam(":routeNumber", $routeNumber);
+         $routeNumber2 = "% " . $routeNumber;
+         $query -> bindParam(":routeNumber2", $routeNumber2);
+         } 
+    $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetchAll();
+    } 
 function getRoutesByNumberSeries($routeNumberSeries = "")
-{
-	global $conn;
-	if (strlen($routeNumberSeries) == 1) {
-		return getRoutesByNumber($routeNumberSeries);
-	}
-	$seriesMin = substr($routeNumberSeries, 0, -1) . "0";
-	$seriesMax = substr($routeNumberSeries, 0, -1) . "9";
-	$query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes  join trips on trips.route_id =
+
+{
+     global $conn;
+     if (strlen($routeNumberSeries) == 1) {
+        return getRoutesByNumber($routeNumberSeries);
+         } 
+    $seriesMin = substr($routeNumberSeries, 0, -1) . "0";
+     $seriesMax = substr($routeNumberSeries, 0, -1) . "9";
+     $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes  join trips on trips.route_id =
 routes.route_id join stop_times on stop_times.trip_id = trips.trip_id where to_number(route_short_name, 'FM999') between :seriesMin and :seriesMax OR route_short_name LIKE :routeNumberSeries order by route_short_name;";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":seriesMin", $seriesMin);
-	$query->bindParam(":seriesMax", $seriesMax);
-        $routeNumberSeries = "% ".substr($routeNumberSeries, 0, -1)."%";
-        $query->bindParam(":routeNumberSeries", $routeNumberSeries);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":seriesMin", $seriesMin);
+     $query -> bindParam(":seriesMax", $seriesMax);
+     $routeNumberSeries = "% " . substr($routeNumberSeries, 0, -1) . "%";
+     $query -> bindParam(":routeNumberSeries", $routeNumberSeries);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetchAll();
+    } 
 function getRouteNextTrip($routeID)
-{
-	global $conn;
-	$query = "select * from routes join trips on trips.route_id = routes.route_id
+
+{
+     global $conn;
+     $query = "select * from routes join trips on trips.route_id = routes.route_id
 join stop_times on stop_times.trip_id = trips.trip_id where
 arrival_time > :currentTime and routes.route_id = :routeID order by
 arrival_time limit 1";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":currentTime", current_time());
-	$query->bindParam(":routeID", $routeID);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	$r = $query->fetch(PDO::FETCH_ASSOC);
-
-	// past last trip of the day special case
-	if (sizeof($r) < 16) {
-		$query = "select * from routes join trips on trips.route_id = routes.route_id
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":currentTime", current_time());
+     $query -> bindParam(":routeID", $routeID);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    $r = $query -> fetch(PDO :: FETCH_ASSOC);
+    
+     // past last trip of the day special case
+    if (sizeof($r) < 16) {
+        $query = "select * from routes join trips on trips.route_id = routes.route_id
 join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = :routeID order by
 arrival_time DESC limit 1";
-		debug($query, "database");
-		$query = $conn->prepare($query);
-		$query->bindParam(":routeID", $routeID);
-		$query->execute();
-		if (!$query) {
-			databaseError($conn->errorInfo());
-			return Array();
-		}
-            
-		$r = $query->fetch(PDO::FETCH_ASSOC);
-	}
-	return $r;
-}
+         debug($query, "database");
+         $query = $conn -> prepare($query);
+         $query -> bindParam(":routeID", $routeID);
+         $query -> execute();
+         if (!$query) {
+            databaseError($conn -> errorInfo());
+             return Array();
+             } 
+        
+        $r = $query -> fetch(PDO :: FETCH_ASSOC);
+         } 
+    return $r;
+    } 
 function getTimeInterpolatedRouteAtStop($routeID, $stop_id)
-{
-	$nextTrip = getRouteNextTrip($routeID);
-	if ($nextTrip['trip_id']) {
-		foreach (getTimeInterpolatedTrip($nextTrip['trip_id']) as $tripStop) {
-			if ($tripStop['stop_id'] == $stop_id) return $tripStop;
-		}
-	}
-	return Array();
-}
+
+{
+     $nextTrip = getRouteNextTrip($routeID);
+     if ($nextTrip['trip_id']) {
+        foreach (getTimeInterpolatedTrip($nextTrip['trip_id']) as $tripStop) {
+            if ($tripStop['stop_id'] == $stop_id) return $tripStop;
+             } 
+        } 
+    return Array();
+    } 
 function getRouteTrips($routeID)
-{
-	global $conn;
-	$query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence from routes join trips on trips.route_id = routes.route_id
+
+{
+     global $conn;
+     $query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence from routes join trips on trips.route_id = routes.route_id
 join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = :routeID and stop_sequence = '1' order by
 arrival_time ";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":routeID", $routeID);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":routeID", $routeID);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetchAll();
+    } 
 function getRoutesByDestination($destination = "", $service_period = "")
-{
-	global $conn;
-	if ($service_period == "") $service_period = service_period();
-	if ($destination != "") {
-		$query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
+
+{
+     global $conn;
+     if ($service_period == "") $service_period = service_period();
+     if ($destination != "") {
+        $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
 FROM stop_times join trips on trips.trip_id =
 stop_times.trip_id join routes on trips.route_id = routes.route_id
 WHERE route_long_name = :destination AND  service_id=:service_period order by route_short_name";
-	}
-	else {
-		$query = "SELECT DISTINCT route_long_name
+         } 
+    else {
+        $query = "SELECT DISTINCT route_long_name
 FROM stop_times join trips on trips.trip_id =
 stop_times.trip_id join routes on trips.route_id = routes.route_id
 WHERE service_id= :service_period order by route_long_name";
-	}
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":service_period", $service_period);
-	if ($destination != "") $query->bindParam(":destination", $destination);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+         } 
+    debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":service_period", $service_period);
+     if ($destination != "") $query -> bindParam(":destination", $destination);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         } 
+    return $query -> fetchAll();
+    } 
 function getRoutesBySuburb($suburb, $service_period = "")
-{
-	if ($service_period == "") $service_period = service_period();
-	global $conn;
-	$query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name
+
+{
+     if ($service_period == "") $service_period = service_period();
+     global $conn;
+     $query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name
 FROM stop_times join trips on trips.trip_id = stop_times.trip_id
 join routes on trips.route_id = routes.route_id
 join stops on stops.stop_id = stop_times.stop_id
 WHERE zone_id LIKE ':suburb AND service_id=:service_period ORDER BY route_short_name";
-	debug($query, "database");
-	$query = $conn->prepare($query);
-	$query->bindParam(":service_period", $service_period);
-        $suburb = "%" . $suburb . ";%";
-	$query->bindParam(":suburb", $suburb);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
-		return Array();
-	}
-	return $query->fetchAll();
-}
+     debug($query, "database");
+     $query = $conn -> prepare($query);
+     $query -> bindParam(":service_period", $service_period);
+     $suburb = "%" . $suburb . ";%";
+     $query -> bindParam(":suburb", $suburb);
+     $query -> execute();
+     if (!$query) {
+        databaseError($conn -> errorInfo());
+         return Array();
+         }