Index: includes/discovery/functions.inc.php
===================================================================
--- includes/discovery/functions.inc.php	(revision 5165)
+++ includes/discovery/functions.inc.php	(working copy)
@@ -451,6 +451,8 @@
 
 function check_valid_sensors($device, $class, $valid, $poller_type = 'snmp')
 {
+  global $debug;
+
   $entries = dbFetchRows("SELECT * FROM `sensors` AS S, `devices` AS D WHERE S.`sensor_class` = ? AND S.`device_id` = D.`device_id` AND D.`device_id` = ? AND S.`poller_type` = ?", array($class, $device['device_id'], $poller_type));
 
   if (count($entries))
@@ -464,7 +466,7 @@
       {
         echo("-");
         dbDelete('sensors', "`sensor_id` =  ?", array($entry['sensor_id']));
-        log_event("Sensor deleted: ".$entry['sensor_class']." ".$entry['sensor_type']." ". $entry['sensor_index']." ".$entry['sensor_descr'], $device, 'sensor', $sensor_id);
+        log_event("Sensor deleted: ".$entry['sensor_class']." ".$entry['sensor_type']." ". $entry['sensor_index']." ".$entry['sensor_descr'], $device, 'sensor', $entry['sensor_id']);
       }
       unset($oid); unset($type);
     }
Index: includes/functions.php
===================================================================
--- includes/functions.php	(revision 5165)
+++ includes/functions.php	(working copy)
@@ -373,7 +373,7 @@
 
 // Adds the device to the database.
 // Returns $device_id number if added or FALSE if device not accessible.
-function add_device($host, $snmpver, $port = '161', $transport = 'udp', $error = FALSE)
+function add_device($host, $snmpver = array(), $port = '161', $transport = 'udp', $error = FALSE)
 {
   global $config;
 
@@ -949,17 +949,20 @@
 function parse_email($emails)
 {
   $result = array();
-  $regex = '/^[\"\']?([^\"\']+)[\"\']?\s{0,}<([^@]+@[^>]+)>$/';
+  $regex = '/^\s*[\"\']?\s*([^\"\']+)?\s*[\"\']?\s*<([^@]+@[^>]+)>\s*$/';
   if (is_string($emails))
   {
     $emails = preg_split('/[,;]\s{0,}/', $emails);
     foreach ($emails as $email)
     {
+      $email = trim($email);
       if (preg_match($regex, $email, $out, PREG_OFFSET_CAPTURE))
       {
-        $result[$out[2][0]] = $out[1][0];
+        $result[$out[2][0]] = trim($out[1][0]);
+      } else if (strpos($email, "@") && !preg_match('/\s/', $email)) {
+        $result[$email] = NULL;
       } else {
-        if (strpos($email, "@")) { $result[$email] = NULL; }
+        return FALSE;
       }
     }
   } else {
Index: poll-billing.php
===================================================================
--- poll-billing.php	(revision 5165)
+++ poll-billing.php	(working copy)
@@ -60,6 +60,7 @@
 
 function poll_bill($bill)
 {
+  global $options;
 
   $ports = dbFetchRows("SELECT * FROM `bill_ports` as P, `ports` as I, `devices` as D WHERE P.bill_id=? AND I.port_id = P.port_id AND D.device_id = I.device_id", array($bill['bill_id']));
   if (isset($options['d'])) {
Index: includes/rewrites.php
===================================================================
--- includes/rewrites.php	(revision 5165)
+++ includes/rewrites.php	(working copy)
@@ -36,7 +36,7 @@
   $check['entity_status'] = array('up' => 0, 'down' => 0, 'unknown' => 0, 'delay' => 0, 'suppress' => 0);
   foreach ($check['entities'] as $alert_table_id => $alert_table_entry)
   {
-    if ($alert_table_entry['alert_status'] == '1')       { ++$check['entity_status']['up'];
+    if ($alert_table_entry['alert_status'] == '1')      { ++$check['entity_status']['up'];
     } elseif($alert_table_entry['alert_status'] == '0') { ++$check['entity_status']['down'];
     } elseif($alert_table_entry['alert_status'] == '2') { ++$check['entity_status']['delay'];
     } elseif($alert_table_entry['alert_status'] == '3') { ++$check['entity_status']['suppress'];
@@ -45,7 +45,7 @@
 
   $check['num_entities'] = count($check['entities']);
 
-  if ($check['entity_status']['up'] == count($entities))
+  if ($check['entity_status']['up'] == $check['num_entities'])
   {
     $check['class']  = "green"; $check['table_tab_colour'] = "#194b7f"; $check['html_row_class'] = "";
   } elseif($check['entity_status']['down'] > '0') {
@@ -106,7 +106,7 @@
   if (!isset($entry['last_alerted']) || $entry['last_alerted'] == '0') { $entry['alerted'] = "<i>Never</i>"; } else { $entry['alerted'] = formatUptime(time()-$entry['last_alerted'], 'short-3'); }
   if (!isset($entry['last_recovered']) || $entry['last_recovered'] == '0') { $entry['recovered'] = "<i>Never</i>"; } else { $entry['recovered'] = formatUptime(time()-$entry['last_recovered'], 'short-3'); }
 
-  if (!isset($entry['ignore_until']) || $entry['ignore_until'] == '0') { $entry['ignore_until_text'] = "<i>Disabled</i>"; } else { $entry['ignore_until_text'] = format_timestamp($entry['ignore_until'], NULL); }
+  if (!isset($entry['ignore_until']) || $entry['ignore_until'] == '0') { $entry['ignore_until_text'] = "<i>Disabled</i>"; } else { $entry['ignore_until_text'] = format_timestamp($entry['ignore_until']); }
   if (!isset($entry['ignore_until_ok']) || $entry['ignore_until_ok'] == '0') { $entry['ignore_until_ok_text'] = "<i>Disabled</i>"; } else { $entry['ignore_until_ok_text'] = '<span class="purple">Yes</span>'; }
 
   // Set humanized so we can check for it later.
Index: includes/billing.php
===================================================================
--- includes/billing.php	(revision 5165)
+++ includes/billing.php	(working copy)
@@ -59,7 +59,7 @@
 
 function getValues($port)
 {
-  global $config;
+  global $config, $device;
 
   if ($device['snmpver'] == "1") {
     $oids = "IF-MIB::ifInOctets.".$port['ifIndex']." IF-MIB::ifOutOctets.".$port['ifIndex'];
@@ -87,11 +87,11 @@
   $row = dbFetchRow("SELECT counter,delta FROM `bill_port_".mres($inout)."_data` WHERE `port_id`='".mres($port_id)."' ORDER BY timestamp DESC LIMIT 0,1");
   if (is_numeric($row['delta']))
   {
-    $return[counter] = $row['counter'];
-    $return[delta] = $row['delta'];
-    $return[state] = "ok";
+    $return['counter'] = $row['counter'];
+    $return['delta'] = $row['delta'];
+    $return['state'] = "ok";
   } else {
-    $return[state] = "failed";
+    $return['state'] = "failed";
   }
   return($return);
 }
@@ -104,13 +104,13 @@
 
   if (is_numeric($row['delta']))
   {
-    $return[delta]     = $row['delta'];
-    $return[delta_in]  = $row['delta_in'];
-    $return[delta_out] = $row['delta_out'];
-    $return[timestamp] = $row['timestamp'];
-    $return[state] = "ok";
+    $return['delta']     = $row['delta'];
+    $return['delta_in']  = $row['delta_in'];
+    $return['delta_out'] = $row['delta_out'];
+    $return['timestamp'] = $row['timestamp'];
+    $return['state'] = "ok";
   } else {
-    $return[state] = "failed";
+    $return['state'] = "failed";
   }
 
   return($return);
@@ -149,9 +149,9 @@
 
 function getRates($bill_id,$datefrom,$dateto)
 {
-  $mq_text = "SELECT count(delta) FROM bill_data ";
-  $mq_text .= " WHERE bill_id = '".mres($bill_id)."'";
-  $mq_text .= " AND timestamp > '".mres($datefrom)."' AND timestamp <= '".mres($dateto)."'";
+  $mq_sql = "SELECT count(delta) FROM bill_data ";
+  $mq_sql .= " WHERE bill_id = '".mres($bill_id)."'";
+  $mq_sql .= " AND timestamp > '".mres($datefrom)."' AND timestamp <= '".mres($dateto)."'";
   $measurements = dbFetchCell($mq_sql);
   $measurement_95th = round($measurements /100 * 95) - 1;