From c147bc6dc36e8e7615892e7187e08d40500df006 Mon Sep 17 00:00:00 2001
From: Nigel Kukard <nkukard@lbsd.net>
Date: Thu, 30 Apr 2009 11:23:07 +0000
Subject: [PATCH] * wisp code cleanup

---
 webui/wisp-locations-add.php    | 26 ++-------
 webui/wisp-locations-delete.php | 58 +++++++++++++-------
 webui/wisp-locations-manage.php | 18 +++----
 webui/wisp-multiuser-add.php    | 47 +++++++++--------
 webui/wisp-user-add.php         | 17 +++---
 webui/wisp-user-delete.php      | 27 +++++++---
 webui/wisp-user-list.php        | 64 +++++++++++-----------
 webui/wisp-user-logs.php        | 94 +++++++++++----------------------
 8 files changed, 165 insertions(+), 186 deletions(-)

diff --git a/webui/wisp-locations-add.php b/webui/wisp-locations-add.php
index e52d48cd..0565618f 100644
--- a/webui/wisp-locations-add.php
+++ b/webui/wisp-locations-add.php
@@ -29,7 +29,6 @@ printHeader(array(
 ));
 
 if (isset($_POST['frmaction']) && $_POST['frmaction'] == "add") {
-
 ?>
 
 	<p class="pageheader">Add location</p>
@@ -54,56 +53,41 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "add") {
 
 # Check we have all params
 } elseif (isset($_POST['frmaction']) && $_POST['frmaction'] == "add2") {
-
 ?>
-
 	<p class="pageheader">Location Add Results</p>
-
 <?php
 
 	# Check name
 	if (empty($_POST['location'])) {
-
 ?>
-
 		<div class="warning">Location cannot be empty</div>
-
 <?php
 
 	# Add to database
 	} else {
 		$stmt = $db->prepare("INSERT INTO ${DB_TABLE_PREFIX}wisp_locations (Location) VALUES (?)");
 		$res = $stmt->execute(array(
-				$_POST['location'],
-				));
-		# Was it successful?
-		if ($res) {
+			$_POST['location'],
+		));
 
+		# Was it successful?
+		if ($res !== FALSE) {
 ?>
-
 			<div class="notice">Location added</div>
-
 <?php
-
 		} else {
-
 ?>
-
 			<div class="warning">Failed to add location</div>
 			<div class="warning"><?php print_r($stmt->errorInfo()) ?></div>
-
 <?php
 
 		}
 	}
-} else {
 
+} else {
 ?>
-
 	<div class="warning">Invalid invocation</div>
-
 <?php
-
 }
 
 printFooter();
diff --git a/webui/wisp-locations-delete.php b/webui/wisp-locations-delete.php
index 581bbfac..daf798fd 100644
--- a/webui/wisp-locations-delete.php
+++ b/webui/wisp-locations-delete.php
@@ -38,11 +38,10 @@ printHeader(array(
 
 # Display delete confirm screen
 if (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete") {
+
 	# Check a user was selected
 	if (isset($_POST['location_id'])) {
-
 ?>
-
 		<p class="pageheader">Delete Location</p>
 
 		<form action="wisp-locations-delete.php" method="post">
@@ -54,82 +53,101 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete") {
 				<input type="submit" name="confirm" value="no" />
 			</div>
 		</form>
-
 <?php
 
 	} else {
-
 ?>
-
 		<div class="warning">No location selected</div>
-
 <?php
-
 	}
+
 # SQL Updates
 } elseif (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete2") {
-
 ?>
-
 	<p class="pageheader">Location Delete Results</p>
-
 <?php
 
 	if (isset($_POST['location_id'])) {
+
 		if (isset($_POST['confirm']) && $_POST['confirm'] == "yes") {
+
 			$db->beginTransaction();
 
-			$res = $db->exec("UPDATE ${DB_TABLE_PREFIX}wisp_userdata SET LocationID = NULL WHERE LocationID = ".$db->quote($_POST['location_id']));
+			$res = $db->exec("
+				UPDATE 
+					${DB_TABLE_PREFIX}wisp_userdata 
+				SET 
+					LocationID = NULL 
+				WHERE 
+					LocationID = ".$db->quote($_POST['location_id'])."
+			");
+
 			if ($res !== FALSE) {
 ?>
 				<div class="notice">Location members unlinked</div>
 <?php
 			} else {
 ?>
-				<div class="warning">Error unlinking members from location</div>
+				<div class="warning">Error removing users from location</div>
 				<div class="warning"><?php print_r($db->errorInfo()); ?></div>
 <?php
 				$db->rollback();
 			}
 
 			if ($res !== FALSE) {
-				$res = $db->exec("DELETE FROM ${DB_TABLE_PREFIX}wisp_locations WHERE ID = ".$db->quote($_POST['location_id']));
+
+				$res = $db->exec("
+					DELETE FROM 
+						${DB_TABLE_PREFIX}wisp_locations 
+					WHERE 
+						ID = ".$db->quote($_POST['location_id'])."
+				");
+
 				if ($res !== FALSE) {
 ?>
 					<div class="notice">Location deleted</div>
 <?php
 				} else {
 ?>
-					<div class="warning">Error deleting location</div>
+					<div class="warning">Error removing location</div>
 					<div class="warning"><?php print_r($db->errorInfo()); ?></div>
 <?php
 					$db->rollback();
 				}
+
 			}
-			if ($res) {
+
+			# Check if all is ok, if so, we can commit, else must rollback
+			if ($res !== FALSE) {
+				$db->commit();
 ?>
-				<div class="notice">Location with ID: <?php echo $_POST['location_id']; ?> deleted</div>
+				<div class="notice">Changes comitted.</div>
+<?php
+			} else {
+				$db->rollback();
+?>
+				<div class="notice">Changes reverted.</div>
 <?php
-				$db->commit();
 			}
+
 		} else {
 ?>
 			<div class="warning">Delete location aborted</div>
 <?php
 		}
+
 	} else {
 ?>
-
 		<div class="warning">Invocation error, no location ID selected</div>
-
 <?php
-
 	}
+
 } else {
 ?>
 	<div class="warning">Invocation error</div>
 <?php
 }
+
 printFooter();
 
 
diff --git a/webui/wisp-locations-manage.php b/webui/wisp-locations-manage.php
index 7d035614..82e2002e 100644
--- a/webui/wisp-locations-manage.php
+++ b/webui/wisp-locations-manage.php
@@ -27,8 +27,7 @@ printHeader(array(
 ));
 
 # If we have no action, display list
-if (!isset($_POST['frmaction']))
-{
+if (!isset($_POST['frmaction'])) {
 ?>
 	<p class="pageheader">Location List</p>
 
@@ -66,23 +65,20 @@ if (!isset($_POST['frmaction']))
 				<td class="textcenter">ID</td>
 				<td class="textcenter">Location</td>
 			</tr>
-
 <?php
-
 			$sql = "SELECT Name FROM ${DB_TABLE_PREFIX}wisp_locations ORDER BY Name ASC";
 			$res = $db->query($sql);
 
 			# List users
 			while ($row = $res->fetchObject()) {
-
 ?>
-
-					<tr class="resultsitem">
-						<td><input type="radio" name="location_id" value="<?php echo $row->id; ?>"/></td>
-						<td><?php echo $row->name; ?></td>
-					</tr>
+				<tr class="resultsitem">
+					<td><input type="radio" name="location_id" value="<?php echo $row->id; ?>"/></td>
+					<td><?php echo $row->name; ?></td>
+				</tr>
 <?php
 			}
+
 			if ($res->rowCount() == 0) {
 ?>
 				<p />
@@ -91,12 +87,14 @@ if (!isset($_POST['frmaction']))
 				</tr>
 <?php
 			}
+
 			$res->closeCursor();
 ?>
 		</table>
 	</form>
 <?php
 }
+
 printFooter();
 
 # vim: ts=4
diff --git a/webui/wisp-multiuser-add.php b/webui/wisp-multiuser-add.php
index d1b1b545..72b354b5 100644
--- a/webui/wisp-multiuser-add.php
+++ b/webui/wisp-multiuser-add.php
@@ -30,9 +30,7 @@ printHeader(array(
 
 
 if (!isset($_POST['frmaction'])) {
-
 ?>
-
 	<p class="pageheader">Add WiSP Users</p>
 
 	<!-- Add user input fields -->
@@ -74,19 +72,17 @@ if (!isset($_POST['frmaction'])) {
 	</form>
 
 <?php
-
 }
 
 if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
-
 ?>
-
 	<p class="pageheader">Add WiSP Users</p>
-
 <?php
 	#FIXME
 	# Perform checks on input
-	if (!empty($_POST['num_users']) && !empty($_POST['session_timeout']) && !empty($_POST['data_limit']) && !empty($_POST['time_limit'])) {
+	if (!empty($_POST['num_users']) && !empty($_POST['session_timeout']) && !empty($_POST['data_limit']) 
+			&& !empty($_POST['time_limit'])) {
+
 		$db->beginTransaction();
 
 		$numberOfUsers = (int)$_POST['num_users'];
@@ -96,7 +92,6 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 		$loginNamePrefix = $_POST['login_prefix'];
 
 		for ($counter = 0; $counter <= $numberOfUsers; $counter += 1) {
-
 			# Check if user already exists
 			$checkUsernameDuplicates = 0;
 
@@ -137,17 +132,21 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 						$checkUsernameDuplicates = 0;
 					}
 				}
+
 			} while ($checkUsernameDuplicates > 0);
 
 			#Insert user into users table
-			$userInsert = $db->prepare("INSERT INTO
-													${DB_TABLE_PREFIX}users (Username)
-										VALUES
-													(?)
-										");
+			$userInsert = $db->prepare("
+				INSERT INTO
+					${DB_TABLE_PREFIX}users (Username)
+				VALUES
+					(?)
+			");
+
 			$userInsertExec = $userInsert->execute(array($userName));
 
 			$failed = 0;
+
 			# After a user add is successful, continue with inserting the other data
 			if ($userInsertExec) {
 
@@ -224,25 +223,27 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 				break;
 			}
 		}
-		if ($failed == 0) {
-			$db->commit();
-
+			# Check if all is ok, if so, we can commit, else must rollback
+			if ($res !== FALSE) {
+				$db->commit();
 ?>
-
-				<div class="notice">Users added</div>
-
+				<div class="notice">Changes comitted.</div>
 <?php
-
+			} else {
+				$db->rollback();
+?>
+				<div class="notice">Changes reverted.</div>
+<?php
+			}
 		}
+
 	} else {
 
 ?>
-
 		<div class="warning">One or more fields have been left empty</div>
-
 <?php
-
 	}
+
 }
 
 printFooter();
diff --git a/webui/wisp-user-add.php b/webui/wisp-user-add.php
index 1c098f13..e6d612e0 100644
--- a/webui/wisp-user-add.php
+++ b/webui/wisp-user-add.php
@@ -30,9 +30,7 @@ printHeader(array(
 
 
 if (!isset($_POST['frmaction'])) {
-
 ?>
-
 	<p class="pageheader">Add WiSP User</p>
 
 	<!-- Add user input fields -->
@@ -168,11 +166,8 @@ if (!isset($_POST['frmaction'])) {
 }
 	
 if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
-
 ?>
-
 	<p class="pageheader">Add user</p>
-
 <?php
 
 	$db->beginTransaction();
@@ -181,12 +176,10 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 	$stmt = $db->prepare("INSERT INTO ${DB_TABLE_PREFIX}users (Username) VALUES (?)");
 	$res = $stmt->execute(array($_POST['user_name']));
 
-
 	if ($res !== FALSE) {
 ?>
 		<div class="notice">User added</div>
 <?php
-
 		# Grab inserted ID
 		$userID = $db->lastInsertId();
 
@@ -258,7 +251,6 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 		}
 	}
 
-
 	if ($res !== FALSE) {
 		# Insert IP Address
 		$stmt = $db->prepare("
@@ -347,7 +339,6 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 		}
 	}
 
-
 	if ($res !== FALSE) {
 		# Insert user data
 		$stmt = $db->prepare("
@@ -377,11 +368,17 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 		}
 	}
 
-
+	# Check if all is ok, if so, we can commit, else must rollback
 	if ($res !== FALSE) {
 		$db->commit();
+?>
+		<div class="notice">Changes comitted.</div>
+<?php
 	} else {
 		$db->rollback();
+?>
+		<div class="notice">Changes reverted.</div>
+<?php
 	}
 }
 
diff --git a/webui/wisp-user-delete.php b/webui/wisp-user-delete.php
index 6370e161..86d9036e 100644
--- a/webui/wisp-user-delete.php
+++ b/webui/wisp-user-delete.php
@@ -37,11 +37,10 @@ printHeader(array(
 
 # Display delete confirm screen
 if (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete") {
+
 	# Check a user was selected
 	if (isset($_POST['user_id'])) {
-
 ?>
-
 		<p class="pageheader">Remove User</p>
 
 		<form action="wisp-user-delete.php" method="post">
@@ -55,24 +54,23 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete") {
 				<input type="submit" name="confirm" value="no" />
 			</div>
 		</form>
-
 <?php
 
 	} else {
-
 ?>
-
 		<div class="warning">No user selected</div>
-
 <?php
 	}
+
 # SQL Updates
 } elseif (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete2") {
 ?>
 	<p class="pageheader">User Remove Results</p>
 <?php
 	if (isset($_POST['user_id'])) {
+
 		if (isset($_POST['confirm']) && $_POST['confirm'] == "yes") {
+
 			$db->beginTransaction();
 			# Delete user data
 			$res = $db->exec("DELETE FROM wisp_userdata WHERE UserID = ".$db->quote($_POST['user_id']));
@@ -118,16 +116,32 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "delete") {
 <?php
 				$db->rollback();
 			}
+
+			# Check if all is ok, if so, we can commit, else must rollback
+			if ($res !== FALSE) {
+				$db->commit();
+?>
+				<div class="notice">Changes comitted.</div>
+<?php
+			} else {
+				$db->rollback();
+?>
+				<div class="notice">Changes reverted.</div>
+<?php
+			}
+
 		} else {
 ?>
 			<div class="warning">Delete user aborted</div>
 <?php
 		}
+
 	} else {
 ?>
 		<div class="warning">No user selected</div>
 <?php
 	}
+
 } else {
 
 ?>
@@ -139,4 +153,3 @@ printFooter();
 
 # vim: ts=4
 ?>
-
diff --git a/webui/wisp-user-list.php b/webui/wisp-user-list.php
index 45a346b0..9ca75b88 100644
--- a/webui/wisp-user-list.php
+++ b/webui/wisp-user-list.php
@@ -67,8 +67,9 @@ if (!isset($_POST['frmaction'])) {
 		</table>
 	</form>
 <?php
-}
-if (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
+
+
+} elseif (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
 ?>
 	<form id="main_form" action="wisp-user-list.php" method="post">
 
@@ -114,14 +115,15 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
 				<td class="textcenter">IP Address</td>
 			</tr>
 <?php
-			$sql = "SELECT
-						ID, Name
-					FROM
-						${DB_TABLE_PREFIX}wisp_locations
-					ORDER BY
+			$sql = "
+				SELECT
+					ID, Name
+				FROM
+					${DB_TABLE_PREFIX}wisp_locations
+				ORDER BY
 						Name
 					ASC
-					";
+			";
 			$res = $db->query($sql);
 
 			$locationsIDtoName = array();
@@ -192,39 +194,38 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
 			# Query based on user input
 			$sql = "
 				SELECT
-						users.ID, 
-						users.Username,
-						wisp_userdata.UserID,
-						wisp_userdata.FirstName,
-						wisp_userdata.LastName,
-						wisp_userdata.Email, 
-						wisp_userdata.Phone,
-						wisp_userdata.LocationID
+					users.ID, 
+					users.Username,
+					wisp_userdata.UserID,
+					wisp_userdata.FirstName,
+					wisp_userdata.LastName,
+					wisp_userdata.Email, 
+					wisp_userdata.Phone,
+					wisp_userdata.LocationID
 				FROM 
-						users, wisp_userdata $extraTables
+					users, wisp_userdata $extraTables
 				WHERE 
-						users.ID = wisp_userdata.UserID
-						$extraSQL
-						$sortSQL
-				";
+					users.ID = wisp_userdata.UserID
+					$extraSQL
+					$sortSQL
+			";
 
 			$res = $db->prepare($sql);
 			$res->execute($extraSQLVals);
 
 			# List users
 			while ($row = $res->fetchObject()) {
-
 				# Second dirty query to get user's attributes
 				$tempUserID = $row->id;
 				$attrQuery = "
-						SELECT
-								Name,
-								Value
-						FROM
-								user_attributes
-						WHERE
-								UserID = $tempUserID
-						";
+					SELECT
+						Name,
+						Value
+					FROM
+						user_attributes
+					WHERE
+						UserID = $tempUserID
+				";
 				
 				$dataCap = NULL;
 				$timeCap = NULL;
@@ -264,13 +265,11 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
 					<td><?php echo $timeCap; ?> Min</td>
 					<td><?php echo $userIP; ?></td>
 				</tr>
-
 <?php
 			}
 
 			# If there were no rows, complain
 			if ($res->rowCount() == 0) {
-
 ?>
 				<p />
 				<tr>
@@ -284,6 +283,7 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "dofilter") {
 	</form>
 <?php
 }
+
 printFooter();
 
 # vim: ts=4
diff --git a/webui/wisp-user-logs.php b/webui/wisp-user-logs.php
index 55c16c7b..e1994f79 100644
--- a/webui/wisp-user-logs.php
+++ b/webui/wisp-user-logs.php
@@ -38,7 +38,6 @@ printHeader(array(
 <p class="pageheader">WiSP User Log</p>
 
 <?php
-
 if (isset($_POST['user_id'])) {
 
 	# Which user in the accounting table should we look for?
@@ -47,9 +46,7 @@ if (isset($_POST['user_id'])) {
 	$row = $stmt->fetchObject();
 	$stmt->closeCursor();
 	$getuser = $row->username;
-
 ?>
-
 	<form id="main_form" action="wisp-user-logs.php" method="post">
 		<!-- User input from and to dates -->
 		<div>
@@ -126,37 +123,37 @@ if (isset($_POST['user_id'])) {
 		# Query to get all default data
 		$sql = "
 			SELECT
-					EventTimestamp, 
-					ServiceType,
-					FramedProtocol,
-					NASPort,
-					NASPortType, 
-					CallingStationID, 
-					CalledStationID, 
-					NASPortID, 
-					AcctSessionID, 
-					FramedIPAddress, 
-					AcctAuthentic, 
-					NASIdentifier, 
-					NASIPAddress, 
-					AcctDelayTime, 
-					AcctSessionTime, 
-					AcctInputOctets, 
-					AcctInputGigawords, 
-					AcctOutputOctets, 
-					AcctOutputGigawords, 
-					AcctStatusType, 
-					AcctTerminateCause 
+				EventTimestamp, 
+				ServiceType,
+				FramedProtocol,
+				NASPort,
+				NASPortType, 
+				CallingStationID, 
+				CalledStationID, 
+				NASPortID, 
+				AcctSessionID, 
+				FramedIPAddress, 
+				AcctAuthentic, 
+				NASIdentifier, 
+				NASIPAddress, 
+				AcctDelayTime, 
+				AcctSessionTime, 
+				AcctInputOctets, 
+				AcctInputGigawords, 
+				AcctOutputOctets, 
+				AcctOutputGigawords, 
+				AcctStatusType, 
+				AcctTerminateCause 
 			FROM 
-					${DB_TABLE_PREFIX}accounting 
+				${DB_TABLE_PREFIX}accounting 
 			WHERE 
-					Username = '$getuser'
-					$extraSQL
+				Username = '$getuser'
+				$extraSQL
 			ORDER BY
-					EventTimestamp
+				EventTimestamp
 			DESC
-				$limitSQL
-			";
+			$limitSQL
+		";
 
 		$res = $db->prepare($sql);
 		$res->execute($extraSQLVals);
@@ -166,7 +163,6 @@ if (isset($_POST['user_id'])) {
 		$totalSessionTime = 0;
 
 		while ($row = $res->fetchObject()) {
-
 			# Input
 			$inputDataItem = 0;
 
@@ -199,9 +195,7 @@ if (isset($_POST['user_id'])) {
 			}
 
 			$totalSessionTime += $sessionTimeItem;
-
 ?>
-
 			<tr class="resultsitem">
 				<td class="textcenter"><?php echo $row->eventtimestamp; ?></td>
 				<td class="textcenter"><?php echo $row->servicetype; ?></td>
@@ -223,69 +217,43 @@ if (isset($_POST['user_id'])) {
 				<td class="textcenter"><?php echo $row->acctstatustype; ?></td>
 				<td class="textcenter"><?php echo strRadiusTermCode($row->acctterminatecause); ?></td>
 			</tr>
-
 <?php
-
 		}
-		if ($res->rowCount() == 0) {
 
+		if ($res->rowCount() == 0) {
 ?>
-
 			<tr>
 				<td colspan="23" class="textcenter">No logs found for user: <?php echo $getuser; ?></td>
 			</tr>
-
 <?php
-
 		} else {
-
 ?>
 
 			<tr class="resultsitem">
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
+				<td colspan="13"</td>
 				<td class="textcenter" style="font-weight: bold;"><? printf('%.2f',$totalSessionTime); ?> Min</td>
 				<td class="textcenter" style="font-weight: bold;"><? printf('%.2f',$totalInputData); ?> MB</td>
 				<td class="textcenter" style="font-weight: bold;"><? printf('%.2f',$totalOutputData); ?> MB</td>
-				<td class="textcenter"></td>
-				<td class="textcenter"></td>
 			</tr>
-
 <?php
-
 		}
+
 		$res->closeCursor();
 ?>
 		</table>
+
 <?php
 
 } else {
-
 ?>
 	<div class="warning">No user selected</div>
 <?php
-
 }
 
 ?>
-
-
 <?php
 
 printFooter();
 
-
 # vim: ts=4
 ?>
-- 
GitLab