Browse Source

Cleanup more PHP warnings and fix a bug with showing switch configs.

Joe Clarke 7 years ago
parent
commit
398182d4d4
3 changed files with 26 additions and 20 deletions
  1. 7 5
      www/add_logic.php
  2. 10 8
      www/index.php
  3. 9 7
      www/logicsw.php

+ 7 - 5
www/add_logic.php

@@ -61,9 +61,11 @@ if (isset($_REQUEST['submit'])) {
 	$pid = $_REQUEST['pid'];
 	$mdf = $_REQUEST['mdf'];
 	$idf = $_REQUEST['idf'];
-	$is_idf = $_REQUEST['is_idf'];
+	$is_idf = (isset($_REQUEST['is_idf'])) ? $_REQUEST['is_idf'] : array();
 	$ports = $_REQUEST['ports'];
 	for ($i = 0; $i < count($name); $i++) {
+		$curr_idf = (isset($is_idf[$i])) ? $is_idf[$i] : 0;
+
 		if ($name[$i] === NULL || $name[$i] == "") {
 			continue;
 		}
@@ -78,7 +80,7 @@ if (isset($_REQUEST['submit'])) {
 			array_push($errors, 'Please select an MDF for row ' . ($i + 1));
 		}
 
-		if (ADDRESS_SCHEME == 'IDF' && $idf[$i] == '__BOGUS__' && $is_idf[$i] == 0) {
+		if (ADDRESS_SCHEME == 'IDF' && $idf[$i] == '__BOGUS__' && $curr_idf == 0) {
 			array_push($errors, 'Please select an IDF for row ' . ($i + 1));
 		}
 
@@ -87,7 +89,7 @@ if (isset($_REQUEST['submit'])) {
 		if (count($errors) == 0) {
 			$ip = NULL;
 			$sql = '';
-			if ((ADDRESS_SCHEME == 'IDF' && $is_idf[$i] == 0) || ADDRESS_SCHEME == 'NEXT_FREE' || ADDRESS_SCHEME == 'DNS') {
+			if ((ADDRESS_SCHEME == 'IDF' && $curr_idf == 0) || ADDRESS_SCHEME == 'NEXT_FREE' || ADDRESS_SCHEME == 'DNS') {
 				$params = array();
 				if (ADDRESS_SCHEME == 'IDF') {
 					$sql = 'SELECT ip_address FROM SWITCHES WHERE name = ?';
@@ -161,12 +163,12 @@ if (isset($_REQUEST['submit'])) {
 				if (PEAR::isError($sth)) {
 					array_push($errors, "Failed to prepare query for row " . ($i + 1) . ": {$sth->getUserInfo()}");
 				} else {
-					$res = $sth->execute(array($name[$i], $pid[$i], $is_idf[$i], $ip, $ports[$i]));
+					$res = $sth->execute(array($name[$i], $pid[$i], $curr_idf, $ip, $ports[$i]));
 					$sth->free();
 					if (PEAR::isError($res)) {
 						array_push($errors, "Failed to execute query for row " . ($i + 1) . ": {$res->getUserInfo()}");
 					} else {
-						$logger->info("User {$ruser} added new logical switch: name={$name[$i]}, pid={$pid[$i]}, is_idf={$is_idf[$i]}, ip_adress={$ip}, ports_required={$ports[$i]}");
+						$logger->info("User {$ruser} added new logical switch: name={$name[$i]}, pid={$pid[$i]}, is_idf={$curr_idf}, ip_adress={$ip}, ports_required={$ports[$i]}");
 					}
 
 					if ($ip !== NULL) {

+ 10 - 8
www/index.php

@@ -58,11 +58,12 @@ $errors = array();
 if (isset($_REQUEST['prev_checked'])) {
 	$pc = $_REQUEST['prev_checked'];
 	$pas = $_REQUEST['prev_assigned_switch'];
-	$c = $_REQUEST['checked'];
+	$c = (isset($_REQUEST['checked'])) ? $_REQUEST['checked'] : array();
 	$as = $_REQUEST['assigned_switch'];
-	$d = $_REQUEST['delete'];
+	$d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array();
 	foreach ($pc as $swsn => $value) {
-		if ($d[$swsn] == 1) {
+		$curr_del = (isset($d[$swsn])) ? $d[$swsn] : 0;
+		if ($curr_del == 1) {
 			$sql = "DELETE FROM DEVICE_MAP WHERE serial_number='{$swsn}'";
 			$res = $dbh->query($sql);
 			if (PEAR::isError($res)) {
@@ -72,8 +73,9 @@ if (isset($_REQUEST['prev_checked'])) {
 			}
 			continue;
 		}
-		if ($value != $c[$swsn] || $pas[$swsn] != $as[$swsn]) {
-			$nc = ($c[$swsn] == 1) ? 1 : 0;
+		$curr_checked = (isset($c[$swsn])) ? $c[$swsn] : 0;
+		if ($value != $curr_checked || $pas[$swsn] != $as[$swsn]) {
+			$nc = $curr_checked;
 			$additional = '';
 			if ($nc == 1 && $as == '__BOGUS__') {
 				array_push($errors, "You must select a logical switch assignment for $swsn");
@@ -121,10 +123,10 @@ if (isset($_REQUEST['filter'])) {
 		$wc = 'WHERE provisioned_status=\'' . PROVISION_SUCCESS . '\'';
 		break;
 	case 'r':
-		$wc = 'WHERE device_status=2';
+		$wc = 'WHERE device_status=\'' . REACHABILITY_REACHABLE . '\'';
 		break;
 	case 'ur':
-		$wc = 'WHERE device_status=0 OR device_status=1';
+		$wc = 'WHERE device_status=\'' . REACHABILITY_NEVER_REACHABLE . '\' OR device_status=\'' . REACHABILITY_NOW_UNREACHABLE . '\'';
 		break;
 	default:
 		if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) {
@@ -451,7 +453,7 @@ foreach ($switches as $row) {
 	}
 ?>
 	      </select>
-	      &nbsp;<a href="#" onClick='var lsw = document.getElementById("asw<?=$i?>"); if (lsw.value == "__BOGUS__") { alert("There is no logical switch associated to this device."); return false; } else { window.open("<?=$base?>/logicsw.php?switch_name=" + lsw.value, "Logical switch " + lsw.value, "height=480,width=980"); return false; }'><img src="/images/switch.gif" border="0" title="View logical switch"></a>&nbsp;<a href="#" onClick='var pros = document.getElementById("pros<?=$row['serial_number']?>"); var lsw = document.getElementById("asw<?=$i?>"); if (pros.value == "0") { alert("This switch config has not yet been generated."); return false; } else { window.open("<?=$base?>/show_config.php?cfg=" + lsw.value + "-config.txt&dname=" + lsw.value, "Config for switch " + lsw.value, "height=650,width=980"); return false; }'><img src="/images/mag.gif" border="0" title="View switch config"></a>
+	      &nbsp;<a href="#" onClick='var lsw = document.getElementById("asw<?=$i?>"); if (lsw.value == "__BOGUS__") { alert("There is no logical switch associated to this device."); return false; } else { window.open("<?=$base?>/logicsw.php?switch_name=" + lsw.value, "Logical switch " + lsw.value, "height=480,width=980"); return false; }'><img src="/images/switch.gif" border="0" title="View logical switch"></a>&nbsp;<a href="#" onClick='var pros = document.getElementById("pros<?=$row['serial_number']?>"); var lsw = document.getElementById("asw<?=$i?>"); if (pros.value < PROVISION_FAIL) { alert("This switch config has not yet been generated."); return false; } else { window.open("<?=$base?>/show_config.php?cfg=" + lsw.value + "-config.txt&dname=" + lsw.value, "Config for switch " + lsw.value, "height=650,width=980"); return false; }'><img src="/images/mag.gif" border="0" title="View switch config"></a>
             </td>
 <?php
 	$color = '#FFFFFF';

+ 9 - 7
www/logicsw.php

@@ -64,10 +64,11 @@ if (isset($_REQUEST['submit_top']) || isset($_REQUEST['submit_bottom'])) {
 	$pid = $_REQUEST['pid'];
 	$loc = $_REQUEST['location'];
 	$exp = $_REQUEST['exception'];
-	$idf = $_REQUEST['is_idf'];
-	$d = $_REQUEST['delete'];
+	$idf = (isset($_REQUEST['is_idf'])) ? $_REQUEST['is_idf'] : array();
+	$d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array();
 	foreach ($pia as $sname => $value) {
-		if ($d[$sname] == 1) {
+		$curr_del = (isset($d[$sname])) ? $d[$sname] : 0;
+		if ($curr_del == 1) {
 			if ($ia[$sname] !== NULL && $ia[$sname] != '') {
 				$sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
 				$sth = $dbh->prepare($sql);
@@ -98,7 +99,8 @@ if (isset($_REQUEST['submit_top']) || isset($_REQUEST['submit_bottom'])) {
 			}
 			continue;
 		}
-		if ($value != $ia[$sname] || $ploc[$sname] != $loc[$sname] || $ppid[$sname] != $pid[$sname] || $pidf[$sname] != $idf[$sname]) {
+		$curr_idf = (isset($idf[$sname])) ? $idf[$sname] : 0;
+		if ($value != $ia[$sname] || $ploc[$sname] != $loc[$sname] || $ppid[$sname] != $pid[$sname] || $pidf[$sname] != $curr_idf) {
 			if ($pid[$sname] == '__BOGUS__') {
 				array_push($errors, "You must select a Product ID for {$sname}");
 			}
@@ -108,18 +110,18 @@ if (isset($_REQUEST['submit_top']) || isset($_REQUEST['submit_bottom'])) {
 			$newa = "'{$ia[$sname]}'";
 			if ($ia[$sname] != '' && !$row['address']) {
 				array_push($errors, "IP address {$ia[$sname]} is not valid for switch {$sname}");
-			} else if ((ADDRESS_SCHEME == 'IDF' && $row['reserved'] == 1 && $idf[$sname] != 1) || (ADDRESS_SCHEME != 'IDF' && ADDRESS_SCHEME != 'DNS' && $row['reserved'] == 1)) {
+			} else if ((ADDRESS_SCHEME == 'IDF' && $row['reserved'] == 1 && $curr_idf != 1) || (ADDRESS_SCHEME != 'IDF' && ADDRESS_SCHEME != 'DNS' && $row['reserved'] == 1)) {
 				array_push($errors, "IP address {$ia[$sname]} is reserved and cannot be used for this switch");
 			} else if ($ia[$sname] == '') {
 				$newa = "NULL";
 			}
 			if (count($errors) == 0) {
-				$sql = "UPDATE SWITCHES SET ip_address = $newa, location = '{$loc[$sname]}', pid = '{$pid[$sname]}', is_idf = '{$idf[$sname]}' WHERE name='{$sname}'";
+				$sql = "UPDATE SWITCHES SET ip_address = $newa, location = '{$loc[$sname]}', pid = '{$pid[$sname]}', is_idf = '{$curr_idf}' WHERE name='{$sname}'";
 				$res = $dbh->query($sql);
 				if (PEAR::isError($res)) {
 					array_push($errors, "Failed to update $sname: '{$res->getUserInfo()}'");
 				} else {
-					$logger->info("User {$ruser} updated logical switches: location={$loc[$sname]}, pid={$pid[$sname]}, is_idf={$idf[$sname]} for switch {$sname}");
+					$logger->info("User {$ruser} updated logical switches: location={$loc[$sname]}, pid={$pid[$sname]}, is_idf={$curr_idf} for switch {$sname}");
 					if ($ia[$sname] != '') {
 						$sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
 						$sth = $dbh->prepare($sql);