Index: html/api/includes/alerts.inc.php
===================================================================
--- html/api/includes/alerts.inc.php	(revision 7244)
+++ html/api/includes/alerts.inc.php	(working copy)
@@ -18,24 +18,16 @@
 
 function get_alert($id)
 {
-
   if ($entry = get_alert_entry_by_id($id))
   {
-
     Flight::json(array('status' => 'ok', 'alert' => $entry));
-
   } else {
-
     Flight::halt('404', 'Alert entry ['.$id.'] not found');
-
   }
-
 }
 
-
 function get_alerts()
 {
-
   list($query, $param, $query_count) = build_alert_table_query($GLOBALS['vars']);
 
   // Fetch alerts
@@ -42,12 +34,14 @@
   $count  = dbFetchCell($query_count, $param);
   $alerts_db = dbFetchRows($query, $param);
 
-  foreach ($alerts_db AS $alert) { humanize_alert_entry($alert); $alerts[$alert['alert_table_id']] = $alert; }
+  foreach ($alerts_db as $alert) { humanize_alert_entry($alert); $alerts[$alert['alert_table_id']] = $alert; }
 
-  if($debug == TRUE ) {
+  if ($debug == TRUE )
+  {
     Flight::json(array('vars' => $GLOBALS['vars'], 'query' => $query, 'params' => $param, 'status' => 'ok', 'count' => $count, 'alerts' => $alerts));
   } else {
     Flight::json(array('status' => 'ok', 'count' => $count, 'alerts' => $alerts));
   }
+}
 
-}
+// EOF
Index: html/api/includes/bills.inc.php
===================================================================
--- html/api/includes/bills.inc.php	(revision 7244)
+++ html/api/includes/bills.inc.php	(working copy)
@@ -19,15 +19,12 @@
 
 function get_bill($id)
 {
-
-echo 'bill';
-
+  echo 'bill';
 }
 
-
 function get_bills()
 {
+  echo 'bills';
+}
 
-echo 'bills';
-
-}
+// EOF
Index: html/api/includes/devices.inc.php
===================================================================
--- html/api/includes/devices.inc.php	(revision 7244)
+++ html/api/includes/devices.inc.php	(working copy)
@@ -12,39 +12,32 @@
  *
  */
 
-
 Flight::route('GET /devices/@id',           'get_device');
 Flight::route('GET /devices',               'get_devices');
 
 Flight::route('POST /devices',              'add_device');
 
-function add_device()
+// FIXME another add_device function already exists!
+/*function add_device()
 {
   $thing = Flight::request()->data;
   print_r($thing);
-}
+}*/
 
 function get_device($id)
 {
-
   $device_id = ctype_digit($id) ? $id : get_device_id_by_hostname($id);
 
-  if($device = device_by_id_cache($device_id))
+  if ($device = device_by_id_cache($device_id))
   {
-
     Flight::json(array('status' => 'ok', 'device' => $device));
-
   } else {
-
     Flight::halt('404', 'Device ['.$id.'] not found');
-
   }
-
 }
 
 function get_devices()
 {
-
   $query_permitted = generate_query_permitted(array('device'), array('device_table' => 'devices'));
 
   $where_array = build_devices_where_array($GLOBALS['vars']);
@@ -70,7 +63,6 @@
   }
 
   Flight::json(array('status' => 'ok', 'count' => count($devices), 'devices' => $devices));
-
 }
 
 // EOF
Index: html/api/includes/ports.inc.php
===================================================================
--- html/api/includes/ports.inc.php	(revision 7244)
+++ html/api/includes/ports.inc.php	(working copy)
@@ -16,25 +16,21 @@
 
 function get_port($id)
 {
-
-  if(port_permitted($id))
+  if (port_permitted($id))
   {
-    if( $port = get_port_by_id($id) ) {
+    if ($port = get_port_by_id($id))
+    {
       Flight::json(array('status' => 'ok', 'port' => $port));
     } else {
       Flight::halt('404', 'Port ['.$id.'] not found.');
     }
   } else {
-
     Flight::halt('404', 'Port ['.$id.'] not found or not permitted.');
-
   }
-
 }
 
 function get_ports()
 {
-
   $where_array = build_ports_where_array($GLOBALS['vars']);
 
   $where = ' WHERE 1 ';
@@ -41,22 +37,25 @@
   $where .= implode('', $where_array);
 
   $query = 'SELECT *, `ports`.`port_id` AS `port_id` FROM `ports`';
-  $query .= ' LEFT JOIN `ports-state` AS S ON `ports`.`port_id` = S.`port_id`';
+  $query .= ' LEFT JOIN `ports-state` USING (`port_id`)';
   $query .= $where;
 
-
   $ports_db = dbFetchRows($query, $param);
   port_permitted_array($ports_db);
 
-  foreach ($ports_db as $port) {
+  foreach ($ports_db as $port)
+  {
     humanize_port($port);
     $ports[$port['port_id']] = $port;
   }
 
   unset($ports_db);
-  if ($debug == TRUE) {
+  if ($debug == TRUE)
+  {
     Flight::json(array('status' => 'ok', 'vars' => $GLOBALS['vars'], 'query' => $query, 'count' => count($ports), 'ports' => $ports));
   } else {
     Flight::json(array('status' => 'ok', 'count' => count($ports), 'ports' => $ports));
   }
-}
\ No newline at end of file
+}
+
+// EOF
Index: html/api/index.php
===================================================================
--- html/api/index.php	(revision 7244)
+++ html/api/index.php	(working copy)
@@ -12,25 +12,12 @@
  *
  */
 
-if (isset($_GET['debug']) && $_GET['debug']) {
-  ini_set('display_errors', 1);
-  ini_set('display_startup_errors', 0);
-  ini_set('log_errors', 0);
-  ini_set('allow_url_fopen', 0);
-  ini_set('error_reporting', E_ALL);
-  $debug = TRUE;
-}
+$config['install_dir'] = "../..";
 
-include_once("../../includes/defaults.inc.php");
-include_once("../../config.php");
-include_once("../../includes/definitions.inc.php");
+include_once('../../includes/sql-config.inc.php');
 
-include_once($config['install_dir'] . "/includes/common.inc.php");
-include($config['install_dir'] . "/includes/rewrites.inc.php");
-include($config['install_dir'] . "/includes/entities.inc.php");
-
-include($config['html_dir'] . "/includes/functions.inc.php");
-include($config['html_dir'] . "/includes/authenticate.inc.php");
+include($config['html_dir'] . '/includes/functions.inc.php');
+include($config['html_dir'] . '/includes/authenticate.inc.php');
 /*
 if (isset($_POST['hash']) && is_numeric($_POST['id']))
 {
@@ -47,9 +34,9 @@
   }
 }
 */
-require('flight/Flight.php');
 
-if (!$_SESSION['authenticated']) {
+if (!$_SESSION['authenticated'])
+{
   Flight::halt(401, 'Unauthenticated');
 }
 
@@ -57,9 +44,11 @@
 
 $vars = get_vars('get');
 
-include("includes/alerts.inc.php");
-include("includes/bills.inc.php");
-include("includes/devices.inc.php");
-include("includes/ports.inc.php");
+include($config['html_dir'] . '/api/includes/alerts.inc.php');
+include($config['html_dir'] . '/api/includes/bills.inc.php');
+include($config['html_dir'] . '/api/includes/devices.inc.php');
+include($config['html_dir'] . '/api/includes/ports.inc.php');
 
 Flight::start();
+
+// EOF
Index: includes/common.inc.php
===================================================================
--- includes/common.inc.php	(revision 7244)
+++ includes/common.inc.php	(working copy)
@@ -31,6 +31,9 @@
     case 'cli':
       include_once $base_dir . 'cli/cli.php'; // Cli classes required base functions
       break;
+    case 'Flight':
+      include_once $base_dir . 'flight/Flight.php';
+      break;
     default:
       if (is_file($base_dir . 'pear/' . $class_file))
       {