root 2 år sedan
förälder
incheckning
04bb71b1cf
4 ändrade filer med 109 tillägg och 41 borttagningar
  1. 2 2
      html/admin/devices/switchstatus.php
  2. 1 1
      html/admin/users/add_dhcp.php
  3. 102 38
      html/inc/common.php
  4. 4 0
      html/inc/consts.php

+ 2 - 2
html/admin/devices/switchstatus.php

@@ -324,7 +324,7 @@ print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['log
             print "<td class=\"$cl_error\">" . $perrors . "</td>\n";
             print "<td class=\"$cl_error\">" . $perrors . "</td>\n";
             print "<td class='" . $cl . "' ><button name=\"write\" class=\"j-submit-report\" onclick=\"window.open('portmactable.php?id=" . $row['id'] . "')\">" . $row['last_mac_count'] . "</button></td>\n";
             print "<td class='" . $cl . "' ><button name=\"write\" class=\"j-submit-report\" onclick=\"window.open('portmactable.php?id=" . $row['id'] . "')\">" . $row['last_mac_count'] . "</button></td>\n";
             print "<td class='" . $cl . "'>" . $sfp_status . " " . $poe_info . "</td>\n";
             print "<td class='" . $cl . "'>" . $sfp_status . " " . $poe_info . "</td>\n";
-            if (isset($poe_status) and !$row['skip'] and !$device['is_router']) {
+            if (isset($poe_status) and !$row['skip']) {
                 print "<td class=\"data\">";
                 print "<td class=\"data\">";
                 if ($device['vendor_id'] != 9) {
                 if ($device['vendor_id'] != 9) {
                     if ($poe_status == 2) {
                     if ($poe_status == 2) {
@@ -340,7 +340,7 @@ print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['log
             } else {
             } else {
                 print "<td>" . WEB_msg_unsupported . "</td>\n";
                 print "<td>" . WEB_msg_unsupported . "</td>\n";
             }
             }
-            if (isset($padmin) and !$row['uplink'] and !$row['skip'] and !$device['is_router']) {
+            if (isset($padmin) and !$row['uplink'] and !$row['skip']) {
                 print "<td class=\"data\">";
                 print "<td class=\"data\">";
                 if ($device['vendor_id'] != 9) {
                 if ($device['vendor_id'] != 9) {
                     if ($padmin >= 2) {
                     if ($padmin >= 2) {

+ 1 - 1
html/admin/users/add_dhcp.php

@@ -17,6 +17,6 @@ if (!empty($_GET["ip"]) and !empty($_GET["mac"])) {
 		LOG_INFO($db_link, "Run command: $run_cmd ");
 		LOG_INFO($db_link, "Run command: $run_cmd ");
         } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
         } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
 	}
 	}
-  
 unset($_GET);
 unset($_GET);
+logout();
 ?>
 ?>

+ 102 - 38
html/inc/common.php

@@ -4,7 +4,6 @@ if (!defined("CONFIG")) {
 }
 }
 
 
 require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/class.simple.mail.php");
 require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/class.simple.mail.php");
-
 require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
 require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
 
 
 #ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
 #ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
@@ -2389,6 +2388,7 @@ function get_sfp_status($vendor_id, $port, $ip, $community='public', $version='2
         return;
         return;
     }
     }
 
 
+    try {
     $status = '';
     $status = '';
     // eltex
     // eltex
     if ($vendor_id == 2) {
     if ($vendor_id == 2) {
@@ -2549,16 +2549,20 @@ function get_sfp_status($vendor_id, $port, $ip, $community='public', $version='2
                 }
                 }
                 if (!empty($tx)) {
                 if (!empty($tx)) {
                     $tx = preg_replace('/"/', '', $tx);
                     $tx = preg_replace('/"/', '', $tx);
+		    try {
                     list($tx_dbm, $pattern) = explode('.', $tx);
                     list($tx_dbm, $pattern) = explode('.', $tx);
-                    $tx_dbm = round(trim($tx_dbm) / 100, 2);
+                    $tx_dbm = round(floatval(trim($tx_dbm)) / 100, 2);
+		    } catch (Exception $e) { $tx_dbm = 0; }
                     if (abs($tx_dbm) > 0.1) {
                     if (abs($tx_dbm) > 0.1) {
                         $status .= ' Tx: ' . $tx_dbm . ' dBm';
                         $status .= ' Tx: ' . $tx_dbm . ' dBm';
                     }
                     }
                 }
                 }
                 if (!empty($rx)) {
                 if (!empty($rx)) {
                     $rx = preg_replace('/"/', '', $rx);
                     $rx = preg_replace('/"/', '', $rx);
+		    try {
                     list($rx_dbm, $pattern) = explode('.', $rx);
                     list($rx_dbm, $pattern) = explode('.', $rx);
-                    $rx_dbm = round(trim($rx_dbm) / 100, 2);
+                    $rx_dbm = round(floatval(trim($rx_dbm)) / 100, 2);
+		    } catch (Exception $e) { $rx_dbm=0; }
                     if (abs($rx_dbm) > 0.1) {
                     if (abs($rx_dbm) > 0.1) {
                         $status .= ' Rx: ' . $rx_dbm . ' dBm';
                         $status .= ' Rx: ' . $rx_dbm . ' dBm';
                     }
                     }
@@ -2573,7 +2577,9 @@ function get_sfp_status($vendor_id, $port, $ip, $community='public', $version='2
         }
         }
         return $status;
         return $status;
     }
     }
-    return;
+    } catch (Exception $e) {
+	return;
+    }
 }
 }
 
 
 
 
@@ -3374,22 +3380,35 @@ function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $communit
 function get_snmp($ip, $community, $version, $oid)
 function get_snmp($ip, $community, $version, $oid)
 {
 {
     snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
     snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
+    $result = NULL;
+    try {
     if ($version == 2) {
     if ($version == 2) {
         $result = snmp2_get($ip, $community, $oid, SNMP_timeout, SNMP_retry);
         $result = snmp2_get($ip, $community, $oid, SNMP_timeout, SNMP_retry);
-    }
+	}
     if ($version == 1) {
     if ($version == 1) {
         $result = snmpget($ip, $community, $oid, SNMP_timeout, SNMP_retry);
         $result = snmpget($ip, $community, $oid, SNMP_timeout, SNMP_retry);
+	}
+    if (!$result) { $result = ''; }
+    } catch (Exception $e) {
+#	echo 'Caught exception: ',  $e->getMessage(), "\n";
+	$result = NULL;
     }
     }
     return $result;
     return $result;
 }
 }
 
 
 function set_snmp($ip, $community, $version, $oid, $field, $value)
 function set_snmp($ip, $community, $version, $oid, $field, $value)
 {
 {
+    $result = false;
+    try {
     if ($version == 2) {
     if ($version == 2) {
         $result = snmp2_set($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
         $result = snmp2_set($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
-    }
+	}
     if ($version == 1) {
     if ($version == 1) {
         $result = snmpset($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
         $result = snmpset($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
+	}
+    } catch (Exception $e) {
+#	echo 'Caught exception: ',  $e->getMessage(), "\n";
+	$result = false;
     }
     }
     return $result;
     return $result;
 }
 }
@@ -3797,18 +3816,29 @@ function get_record($db, $table, $filter)
         LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
         LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
         return;
         return;
     }
     }
-    $old_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
-    $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
-    $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
+    $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
+    $get_record = mysqli_query($db, $get_sql);
+    if (!$get_record) {
+	LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
+	return;
+	}
+    $fields = [];
+    while ($field = mysqli_fetch_field($get_record)) {
+	$f_table = $field->table;
+	$f_name = $field->name;
+	$fields[$f_table][$f_name]=$field;
+    }
+    $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
     $result = NULL;
     $result = NULL;
-    if (!empty($old)) {
-        foreach ($old as $key => $value) {
-            if (!isset($value) or $value === 'NULL') {
-                $value = '';
-            }
-            if (!empty($key)) {
-                $result[$key] = $value;
+    if (!empty($record)) {
+        foreach ($record as $key => $value) {
+            if (!isset($value) or $value === 'NULL' or $value == NULL) {
+		if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
+		    if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
+		    if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
+		}
             }
             }
+            if (!empty($key)) { $result[$key] = $value; }
         }
         }
     }
     }
     return $result;
     return $result;
@@ -3828,15 +3858,28 @@ function get_records($db, $table, $filter)
     if (isset($filter)) {
     if (isset($filter)) {
         $s_filter = 'WHERE ' . $filter;
         $s_filter = 'WHERE ' . $filter;
     }
     }
-    $old_sql = "SELECT * FROM $table $s_filter";
-    $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
+    $get_sql = "SELECT * FROM $table $s_filter";
+    $get_record = mysqli_query($db, $get_sql);
+    if (!$get_record) {
+	LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
+	return;
+	}
+    $fields = [];
+    while ($field = mysqli_fetch_field($get_record)) {
+	$f_table = $field->table;
+	$f_name = $field->name;
+	$fields[$f_table][$f_name]=$field;
+    }
     $result = NULL;
     $result = NULL;
     $index = 0;
     $index = 0;
-    while ($old = mysqli_fetch_array($old_record, MYSQLI_ASSOC)) {
-        foreach ($old as $key => $value) {
-            if (!isset($value) or $value === 'NULL') {
-                $value = '';
-            }
+    while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
+        foreach ($rec as $key => $value) {
+            if (!isset($value) or $value === 'NULL' or $value == NULL) {
+		if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
+		    if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
+		    if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
+		    }
+		}
             $result[$index][$key] = $value;
             $result[$index][$key] = $value;
         }
         }
         $index++;
         $index++;
@@ -3846,21 +3889,32 @@ function get_records($db, $table, $filter)
 
 
 function get_records_sql($db, $sql)
 function get_records_sql($db, $sql)
 {
 {
+    $result = NULL;
     if (empty($sql)) {
     if (empty($sql)) {
         LOG_ERROR($db, "Empty query! Skip command.");
         LOG_ERROR($db, "Empty query! Skip command.");
-        return;
+        return $result;
+    }
+    $records = mysqli_query($db, $sql);
+    if (!$records) {
+	LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
+	return $result;
+	}
+    $fields = [];
+    //we assume that fields with the same name have the same type
+    while ($field = mysqli_fetch_field($records)) {
+	$f_name = $field->name;
+	$fields[$f_name]=$field;
     }
     }
-    $record = mysqli_query($db, $sql) or LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
     $index = 0;
     $index = 0;
-    $result = NULL;
-    while ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
+    while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
         foreach ($rec as $key => $value) {
         foreach ($rec as $key => $value) {
-            if (!isset($value) or $value === 'NULL') {
-                $value = '';
-            }
-            if (!empty($key)) {
-                $result[$index][$key] = $value;
-            }
+            if (!isset($value) or $value === 'NULL' or $value == NULL) {
+		if (!empty($key) and !empty($fields[$key])) {
+		    if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
+		    if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
+		    }
+		}
+            if (!empty($key)) { $result[$index][$key] = $value; }
         }
         }
         $index++;
         $index++;
     }
     }
@@ -3874,17 +3928,27 @@ function get_record_sql($db, $sql)
         LOG_ERROR($db, "Empty query! Skip command.");
         LOG_ERROR($db, "Empty query! Skip command.");
         return $result;
         return $result;
     }
     }
-    $record = mysqli_query($db, $sql . " LIMIT 1") or LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
+    $record = mysqli_query($db, $sql . " LIMIT 1");
     if (!isset($record)) {
     if (!isset($record)) {
+	LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
         return $result;
         return $result;
+	}
+    $fields = [];
+    //we assume that fields with the same name have the same type
+    while ($field = mysqli_fetch_field($record)) {
+	$f_name = $field->name;
+	$fields[$f_name]=$field;
     }
     }
     $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
     $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
     if (!empty($rec)) {
     if (!empty($rec)) {
         foreach ($rec as $key => $value) {
         foreach ($rec as $key => $value) {
-            if (!isset($value) or $value === 'NULL') {
-                $value = '';
-            }
-            $result[$key] = $value;
+            if (!isset($value) or $value === 'NULL' or $value == NULL) {
+		if (!empty($key) and !empty($fields[$key])) {
+		    if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
+		    if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
+		    }
+		}
+            if (!empty($key)) { $result[$key] = $value; }
         }
         }
     }
     }
     return $result;
     return $result;

+ 4 - 0
html/inc/consts.php

@@ -174,4 +174,8 @@ define("L_INFO",2);
 define("L_VERBOSE",3);
 define("L_VERBOSE",3);
 define("L_DEBUG",255);
 define("L_DEBUG",255);
 
 
+//mysql field types
+define('MYSQL_FIELD_DIGIT', array(1,2,3,4,5,8,9,246));
+define('MYSQL_FIELD_STRING', array(252,253,254));
+
 ?>
 ?>