From bd1ecd071f516384f9e2b16c001e1c9fcdb57a14 Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Thu, 30 Apr 2009 13:16:07 +0000
Subject: [PATCH] Cleaned up code Better error handling: still needs fixing

---
 TODO                         |   2 +-
 webui/wisp-multiuser-add.php | 229 ++++++++++++++++++-----------------
 2 files changed, 119 insertions(+), 112 deletions(-)

diff --git a/TODO b/TODO
index 412a1c95..5f485c81 100644
--- a/TODO
+++ b/TODO
@@ -6,8 +6,8 @@ smradiusd:
 WebUI:
 WiSP -> User List -> Edit: Must be able to clear fields (MAC Address)
 WiSP User Add -> Check if only certain fields are blank. Might want to add user without MAC Address.
-Fix up multi-user-add, code is a bit fugly, use lastinsertid
 wisp-user-edit.php - proper sql error handling, use 1 query to pull in all attribs and check them in a hash, nigel to then cleanup
+wisp-multiuser-add: only generating two users with error - needs fix
 
 
 Realm config
diff --git a/webui/wisp-multiuser-add.php b/webui/wisp-multiuser-add.php
index 72b354b5..f4bc5833 100644
--- a/webui/wisp-multiuser-add.php
+++ b/webui/wisp-multiuser-add.php
@@ -78,7 +78,6 @@ 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'])) {
@@ -91,138 +90,149 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 		$timeLimit = (int)$_POST['time_limit'];
 		$loginNamePrefix = $_POST['login_prefix'];
 
-		for ($counter = 0; $counter <= $numberOfUsers; $counter += 1) {
-			# Check if user already exists
-			$checkUsernameDuplicates = 0;
+		for ($counter = 0; $counter <= $numberOfUsers; $counter++) {
 
+			# Loop and try add user, maybe its duplicate?
 			do {
-				# Generate random username
-				$randomString = chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122));
-
-				# If there is no login name prefix
-				if (empty($loginNamePrefix)) {
-					$userName = $randomString;
+				$isDuplicate = 0;
 
-					$lookForUser = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username LIKE '%$userName%'");
-
-					# If the user was found
-					if ($lookForUser->rowCount() > 0) {
-						$checkUsernameDuplicates = 1;
-					} else {
-						$checkUsernameDuplicates = 0;
-					}
+				# Generate random username
+				$randomString = "";
+				for ($i = 0; $i < 8; $i++) { $randomString .= chr(rand(97,122)); }
 
 				# If there is a login name prefix
-				} else {
+				if (isset($loginNamePrefix) && $loginNamePrefix != "") {
 					$userName = $loginNamePrefix."_".$randomString;
+				# If there is no login name prefix
+				} else {
+					$userName = $randomString;
+				}
 
-					$lookForUser = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username LIKE '%$userName%'");
+				$stmt = $db->query("
+					SELECT 
+						COUNT(*) AS Duplicate
+					FROM 
+						${DB_TABLE_PREFIX}users 
+					WHERE 
+						Username LIKE '%$userName%'
+				");
 
-					# If the user was found
-					if ($lookForUser->rowCount() > 0) {
-						$checkUsernameDuplicates = 1;
-					} else {
-						$checkUsernameDuplicates = 0;
-					}
-				}
+				$row = $stmt->fetchObject();
 
-			} while ($checkUsernameDuplicates > 0);
+			} while ($row->duplicate > 0);
 
 			#Insert user into users table
-			$userInsert = $db->prepare("
+			$stmt = $db->prepare("
 				INSERT INTO
 					${DB_TABLE_PREFIX}users (Username)
 				VALUES
 					(?)
 			");
-
-			$userInsertExec = $userInsert->execute(array($userName));
-
-			$failed = 0;
+			$res = $stmt->execute(array($userName));
 
 			# After a user add is successful, continue with inserting the other data
-			if ($userInsertExec) {
+			if ($res !== FALSE) {
 
 				# Get user ID to insert into other tables
-				$getUserID = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username = '$userName'");
-				$resultRow = $getUserID->fetchObject();
-				$userID = $resultRow->id;
-
-				# Inset UserID into wisp_userdata table
-				$userDataStatement = $db->prepare("	INSERT INTO
-																${DB_TABLE_PREFIX}wisp_userdata (UserID)
-													VALUES
-																(?)
-													");
-
-				$userDataResult = $userDataStatement->execute(array($userID));
-
-				# Generate a password
-				$userPassword = chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122)).
-								chr(rand(97,122));
-
-				# Insert password into user_attributes table
-				$userPasswordStatement = $db->prepare("	INSERT INTO
-																	${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
-														VALUES
-																	($userID,'User-Password','==',?)
-														");
-
-				$userPasswordResult = $userPasswordStatement->execute(array($userPassword));
+				$userID = $db->lastInsertId();
+
+				if (isset($userID)) {
+					# Inset UserID into wisp_userdata table
+					$stmt = $db->prepare("
+									INSERT INTO
+										${DB_TABLE_PREFIX}wisp_userdata (UserID)
+									VALUES
+										(?)
+					");
+
+					$res = $stmt->execute(array($userID));
+				} else {
+					$res = 0;
+?>
+					<div class="warning">Failed to retreive user ID</div>
+<?php
+				}
+
+
+				if ($res !== FALSE) {
+					# Generate password
+					$userPassword = "";
+					for ($passCount = 0; $passCount < 8; $passCount++) {
+					$userPassword .= chr(rand(97,122));
+					}
+
+					# Insert password into user_attributes table
+					$stmt = $db->prepare("
+									INSERT INTO
+										${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
+									VALUES
+										($userID,'User-Password','==',?)
+					");
+
+					$res = $stmt->execute(array($userPassword));
+				} else {
+?>
+					<div class="warning">Failed to add user password</div>
+					<div class="warning"><?php print_r($stmt->errorInfo()); ?></div>
+<?php
+				}
 				
-				# Insert data limit into user_attributes table
-				$userDataLimitStatement = $db->prepare("INSERT INTO
-																	${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
-														VALUES
-																	($userID,'SMRadius-Capping-Traffic-Limit',':=',?)
-														");
-
-				$userDataLimitResult = $userDataLimitStatement->execute(array($dataLimit,));
+
+				if ($res !== FALSE) {
+					# Insert data limit into user_attributes table
+					$stmt = $db->prepare("
+									INSERT INTO
+										${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
+									VALUES
+										($userID,'SMRadius-Capping-Traffic-Limit',':=',?)
+					");
+
+					$res = $stmt->execute(array($dataLimit));
+				} else {
+?>
+					<div class="warning">Failed to add data cap</div>
+					<div class="warning"><?php print_r($stmt->errorInfo()); ?></div>
+<?php
+				}
+
 				
-				# Insert time limit into user_attributes table
-				$userTimeStatement = $db->prepare("	INSERT INTO
-																${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
-													VALUES
-																($userID,'SMRadius-Capping-UpTime-Limit',':=',?)
-													");
-
-				$userTimeResult = $userTimeStatement->execute(array($timeLimit,));
-
-				# Insert timeout into user_attributes table
-				$userTimeOutStatement = $db->prepare("	INSERT INTO
-																	${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
-														VALUES
-																	($userID,'Session-Timeout','+=',?)
-													");
-
-				$userTimeOutResult = $userTimeOutStatement->execute(array($sessionTimeout,));
-
-				if ($userTimeOutResult && $userTimeResult && $userDataResult && $userPasswordResult && $userDataLimitResult) {
-					$failed = 0;
+				if ($res !== FALSE) {
+					# Insert time limit into user_attributes table
+					$stmt = $db->prepare("
+									INSERT INTO
+										${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
+									VALUES
+										($userID,'SMRadius-Capping-UpTime-Limit',':=',?)
+					");
+
+					$res = $stmt->execute(array($timeLimit));
 				} else {
-					$failed = 1;
+?>
+					<div class="warning">Failed to add uptime cap</div>
+					<div class="warning"><?php print_r($stmt->errorInfo()); ?></div>
+<?php
+				}
+
+
+				if ($res !== FALSE) {
+					# Insert timeout into user_attributes table
+					$stmt = $db->prepare("
+									INSERT INTO
+										${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
+									VALUES
+										($userID,'Session-Timeout','+=',?)
+					");
+
+					$res = $stmt->execute(array($sessionTimeout));
+				} else {
+?>
+					<div class="warning">Failed to add uptime cap</div>
+					<div class="warning"><?php print_r($stmt->errorInfo()); ?></div>
+<?php
 				}
-			# If one was not successful, rollback
-			} else {
-				print_r($db->errorInfo());
-				$db->rollback;
-				$failed = 1;
-				break;
 			}
-		}
+
+
 			# Check if all is ok, if so, we can commit, else must rollback
 			if ($res !== FALSE) {
 				$db->commit();
@@ -236,14 +246,11 @@ if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
 <?php
 			}
 		}
-
 	} else {
-
 ?>
 		<div class="warning">One or more fields have been left empty</div>
 <?php
 	}
-
 }
 
 printFooter();
-- 
GitLab