From 5229c188b3733d6a391c1156043217f3cf831d1c Mon Sep 17 00:00:00 2001
From: Graham Cole <chckens@sucs.org>
Date: Thu, 17 Jan 2008 16:02:15 +0000
Subject: [PATCH] convert options component to use geeric error handling. Refs
 #49

---
 components/options.php | 47 +++++++++++++++++++-----------------------
 templates/options.tpl  |  9 --------
 2 files changed, 21 insertions(+), 35 deletions(-)

diff --git a/components/options.php b/components/options.php
index 7d5dd7e..2b22dee 100644
--- a/components/options.php
+++ b/components/options.php
@@ -16,26 +16,26 @@ $sucsdbname = 'sucs'; // This was stored in config, I just don't have it here in
 
 // function to change a user's password
 function changePassword ($oldpass, $newpass1, $newpass2) {
-	global $error, $session;
+	global $session;
 
 	if ($newpass1 !== $newpass2) {
 		// new passwords do not match!
-		$error[] = "New passwords do not match";
+		trigger_error("New passwords do not match", E_USER_WARNING);
 		return FALSE;
 	}
 
 	if (!strongPassword($newpass1)) {
-		$error[] = "Password is too weak";
+		trigger_error("New password is too weak.", E_USER_WARNING);
 		return FALSE;
 	}
 
 	if (!($ldap = @ldap_connect("ldap://localhost"))) {
-		$error[] = "LDAP connect failed";
+		trigger_error("LDAP connect failed", E_USER_ERROR);
 		return FALSE;
 	}
 	
 	if (!($ldapbind = ldap_bind($ldap, "uid=".$session->username.",ou=People,dc=sucs,dc=org",$oldpass))) {
-		$error[] = "Existing password incorrect";
+		trigger_error("Existing password incorrect", E_USER_WARNING);
 		ldap_close($ldap);
 		return FALSE;
 	}
@@ -57,13 +57,13 @@ function changeContactDetails($address, $phone, $externalEmail) {
 		return FALSE; // Valid Email handles errors itself
 	// Check valid phone number (do we have a function for this?)
 	if (empty($phone) || (strlen($phone) < 6)) {
-		$error[] = 'You must have a contact phone number!';
+		trigger_error("You must have a contact phone number!", E_USER_WARNING);
 		return FALSE;
 	}
 	// Update 
 	if ($sucsDB->Execute('UPDATE members SET address = ?,phone = ?, email = ?, lastupdate = now(), lastedit = uid WHERE username = ?', 
 			array($address, $phone, $externalEmail, $session->username)) === FALSE) {
-		$error[] = 'There was an error updating your contact details. Please contact admin.';
+		trigger_error("There was an error updating your contact details. Please contact admin.", E_USER_ERROR);
 		return FALSE;
 	}
 	return TRUE;
@@ -73,12 +73,12 @@ function changeGuestnetDetails($wiredMAC) {
 	global $sucsDB, $session, $error;
 	// Get UID as we don't have it yet	
 	if(($uid = $sucsDB->GetOne("SELECt uid FROM members WHERE username = ?", array($session->username))) === FALSE) {
-		$error[] = 'There was an error updating your GuestNET MAC Address. Please contact admin. (UID)';
+		trigger_error("There was an error updating your GuestNET MAC Address. Please contact admin. (UID)", E_USER_ERROR);
 		return FALSE;
 	}
 	// Replace the guestnet record with the one with the new mac
 	if (!$sucsDB->Replace('guestnet', array('mac' => "'$wiredMAC'", 'uid' => $uid), 'uid')) {
-		$error[] = 'There was an error updating your GuestNET MAC Address. Please contact admin. (QUERY)';
+		trigger_error("There was an error updating your GuestNET MAC Address. Please contact admin. (QUERY)", E_USER_ERROR);
 		return FALSE;
 	}
 	exec('/usr/local/bin/update-dhcpd', $placeholder, $returnVar);
@@ -87,7 +87,7 @@ function changeGuestnetDetails($wiredMAC) {
 		return TRUE;
 		//  $message[] = 'GuestNet MAC Address updated. Please wait a minute or two for the changes to take effect.';
 	} else {
-		$error[] = 'There was an error updating your GuestNet Mac Address. Please contact admin. (RETURNVAR)';
+		trigger_error("There was an error updating your GuestNet Mac Address. Please contact admin. (RETURNVAR)", E_USER_ERROR);
 		return FALSE;
 	}
 }
@@ -100,7 +100,6 @@ function isValidMACAddr($mac) {
 
 // Returns the users GuestNet MAC Address if they have one and false otherwise
 function getGuestNetMAC() {
-	global $error;
 	$ip = trim($_SERVER['REMOTE_ADDR']);
 	// Sanity Check ip?
 	// Check we are in the correct ip range (unregistered GuestNET addresses)
@@ -121,7 +120,7 @@ function getGuestNetMAC() {
 			}
 		}
 		// We didn't find their mac address :\
-		$error[] = 'There was an error finding your MAC Address. Please contact admin.';
+		trigger_error("There was an error finding your MAC Address. Please contact admin.", E_USER_ERROR);
 		return FALSE;
 	} else {
 		return FALSE;
@@ -130,13 +129,13 @@ function getGuestNetMAC() {
 
 // Delete the user's hackergotchi file cause they've decided they're too embarrassed by it
 function clearHackergotchi() {
-	global $error,$session;
+	global $session;
 	$imagefilepath = hackergotchiPath($session->username);
 
 	if (unlink($imagefilepath)) {
 		return true;
 	} else { 
-		$error[] = 'Unable to remove hackergotchi file';
+		trigger_error("Unable to remove hackergotchi file", E_USER_ERROR);
 		return false; 
 	}
 }
@@ -150,7 +149,7 @@ function hackergotchiPath($username) {
 
 // Takes an entry from $_FILES and makes this the new hackergotchi
 function updateHackergotchi($fileDetails) {
-	global $error,$session;
+	global $session;
 	if ( ((list($width, $height, $type, $attr) = @getimagesize($fileDetails['tmp_name'])) !== false) && 
 	     ($type == IMAGETYPE_PNG) && ($width <= 128) && ($height <= 128)) {
 
@@ -162,7 +161,7 @@ function updateHackergotchi($fileDetails) {
 		}
 	}
 	// We should have returned True by now
-	$error[] = 'There was an error updating your hackergotchi. Please contact admin.';
+	trigger_error("There was an error updating your hackergotchi. Please contact admin.", E_USER_ERROR);
 	return FALSE;
 }
 
@@ -183,30 +182,30 @@ if ($session->loggedin === TRUE) {
 		switch($_POST['action']) {
 			case 'changepass' :
 				if (changePassword($_POST['oldpass'], $_POST['newpass1'], $_POST['newpass2'])) {
-					$messages[] = 'Password changed.';
+					message_flash('Password changed.');
 				} else {
 					// Should we log something here for bug trcaking use?
-					$error[] = "Password change failed.";
+					trigger_error("Password change failed.", E_USER_WARNING);
 				}
 				break;
 			case 'changecontact' :
 				if(changeContactDetails($_POST['address'], $_POST['phone'], $_POST['email'])) {
-					$messages[] = 'Contact Details Updated.';
+					message_flash('Contact Details Updated.');
 				}
 				break;
 			case 'changeguestnet' :
 				if(changeGuestNetDetails($newGuestNetMAC)) {
-					$messages[] = 'GuestNet Details Updated!';
+					message_flash('GuestNet Details Updated!');
 				}
 				break;
 			case 'updatehackergotchi' :
 				if (updateHackergotchi($_FILES['hackergotchi'])) {
-					$messages[] = 'Hackergotchi Updated';
+					message_flash('Hackergotchi Updated');
 				} 
 				break;
 			case 'clearhackergotchi' :
 				if (clearHackergotchi()) {
-					$messages[] = 'Hackergotchi Cleared';
+					message_flash('Hackergotchi Cleared');
 				}
 				break;
 		}
@@ -236,10 +235,6 @@ if ($session->loggedin === TRUE) {
 
 }
 
-// merge messages into errors for the time being
-$error = array_merge((array)$error, (array)$messages);
-
-$smarty->assign('error', $error);
 $smarty->assign('url', $component['path']);
 $result = $smarty->fetch('options.tpl');
 $smarty->assign('title', "Options");
diff --git a/templates/options.tpl b/templates/options.tpl
index 3feb3bb..766b686 100644
--- a/templates/options.tpl
+++ b/templates/options.tpl
@@ -1,12 +1,3 @@
-{if !empty($error) }
-{foreach name=error from=$error item=errorMessage}
-	<div class="errorbar">
-		<div><div><div>
-			{$errorMessage}
-		</div></div></div>
-	</div>
-{/foreach}
-{/if}
 {if $session->loggedin }
 <!-- style="width: 80%; margin: auto;" was in here originally, why? -->
 <div class="box" >
-- 
GitLab