[SUCS Devel] site r647 - in branches/sucs-site: . components htdocs

imranh at sucs.org imranh at sucs.org
Wed Mar 4 15:15:25 GMT 2015


Author: imranh
Date: 2015-03-04 15:15:22 +0000 (Wed, 04 Mar 2015)
New Revision: 647

Modified:
   branches/sucs-site/components/README
   branches/sucs-site/components/membershiprenew.php
   branches/sucs-site/components/motd_wrapper.php
   branches/sucs-site/components/options.php
   branches/sucs-site/components/printer.graph.php
   branches/sucs-site/components/printer.php
   branches/sucs-site/components/signup-admin.php
   branches/sucs-site/components/signup.php
   branches/sucs-site/components/susignup-admin.php
   branches/sucs-site/components/susignup.php
   branches/sucs-site/htdocs/index.php
   branches/sucs-site/settingstemplate
Log:
Make the sucs site use one global db conn to the sucs db, 

Modified: branches/sucs-site/components/README
===================================================================
--- branches/sucs-site/components/README	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/README	2015-03-04 15:15:22 UTC (rev 647)
@@ -24,7 +24,8 @@
 	[component]	component name
 
 $smarty		The smarty object
-$DB		The ADODB database object
+$DB		The sucssite ADODB database object
+$sucsDB		The sucs ADODB databse object
 $session	The Session object
 
 Session has the following properties

Modified: branches/sucs-site/components/membershiprenew.php
===================================================================
--- branches/sucs-site/components/membershiprenew.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/membershiprenew.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -12,24 +12,20 @@
 // Only staff can use this page
 if (isset($session->groups[$permission])) {
     $smarty->assign("staff", TRUE);
-    // connect to sucs database
- 	$sucsDB = NewADOConnection('postgres8');
-	$sucsDB->Connect('dbname=sucs user=apache');
-	$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 //	$sucsDB->debug = true;
-	
+
 	// handle updates
     if(isset($_POST['uid']) && isset($_POST['lastupdate'])){
         // Check data hasn't changed and that nothing is broked
         $query = "SELECT * FROM members WHERE uid=? AND lastupdate=?";
         $array = array($_POST['uid'], $_POST['lastupdate']);
         $data = $sucsDB->GetAll($query,$array);
-        
+
         // If there is only one record then everything is fine
         if(sizeof($data)==1){
             // get info on currently logged in user
             $user = posix_getpwnam($session->username);
-            
+
             // Update record
             $record = $data[0];
             $query = "UPDATE members";
@@ -39,7 +35,7 @@
             if($enable){
                 $sucsDB->query($query,$array);
             }
-            
+
             // emailing contact (tresurer)
 			$message  = "Account renewal notification\n\n";
 			$message .= "Account   : ".$record['username']."\n";
@@ -47,7 +43,7 @@
 			$message .= "Renewed by: ".$user['name']."\n\n";
 			$message .= "Regards\n  eclipse's script";
 			mail($inform,"Account Renewal",$message);
-			
+
 			// emailing user
 			$message = "Your Swansea University Computer Society (SUCS) membership has been renewed\n\n";
 			$message .= "Username: ".$record['username']."\n";
@@ -59,14 +55,14 @@
 			mail($record['email'],"SUCS account renewal",$message,$header);
 			// sucs account
 			mail($record['username']."@sucs.org","SUCS account renewal",$message,$header);
-            
+
             message_flash("Renewed account for: ".htmlentities($record['username']));
         }
         else{
             trigger_error("Number of recored returned: ".sizeof($data).". Expected: 1.", E_USER_ERROR);
         }
     }
-    
+
     // if sort is specified in GET
     if(isset($_GET["sort"])){
         $sortoptions = array("username","sid","realname");
@@ -85,8 +81,8 @@
     else{
         $sort='username';
     }
-    
-    
+
+
     //Get members details
     $query = "SELECT * FROM members, member_type";
 	$query .= " WHERE paid != ?";
@@ -96,7 +92,7 @@
 	$array = array($paydate);
 	$data = $sucsDB->GetAll($query, $array);
     $smarty->assign("members", $data);
-    
+
     // set refresh rate
   	$autorefresh=$_GET["autorefresh"];
   	// if autorefresh is not 'n'
@@ -120,13 +116,13 @@
         $getrefresh='n';
         $optrefresh='n';
     }
-    
+
     // compile passthrough url
     // sort
     if(isset($getsort)){
         $getout="?sort=".$getsort;
     }
-    
+
     // autorefresh
     if(isset($getrefresh)){
         if(isset($getout)){
@@ -136,7 +132,7 @@
             $getout = "?autorefresh=".$getrefresh;
         }
     }
-    
+
     // set smarty variables
     $smarty->assign("self",$baseurl.$path.$getout);
     $smarty->assign("optionrefresh",$optrefresh);

Modified: branches/sucs-site/components/motd_wrapper.php
===================================================================
--- branches/sucs-site/components/motd_wrapper.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/motd_wrapper.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -2,12 +2,6 @@
 require("/var/www/sucssite/htdocs/settings.php");
 require("/usr/share/php/adodb/adodb.inc.php");
 
-$dbname = 'sucssite';
-$dbuser = 'apache';
-
-$DB = NewADOConnection('postgres8');
-$DB->Connect('dbname='.$dbname.' user='.$dbuser);
 $DB->SetCharSet('utf8');
-$DB->SetFetchMode(ADODB_FETCH_ASSOC);
 
 include($base."components/motd.php");

Modified: branches/sucs-site/components/options.php
===================================================================
--- branches/sucs-site/components/options.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/options.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -10,7 +10,6 @@
 // These could possibly be moved somewhere saner?
 define('GUESTNET_LOW','137.44.10.130'); // These could possibly be a set of ranges or a subnet which might be saner?
 define('GUESTNET_HIGH','137.44.10.134'); // These could possibly be a set of ranges or a subnet which might be saner?
-$sucsdbname = 'sucs'; // This was stored in config, I just don't have it here in the test
 
 // Helper Functions
 // Could these be moved to some kind of library?
@@ -38,7 +37,7 @@
 		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))) {
 		trigger_error("Existing password incorrect", E_USER_WARNING);
 		ldap_close($ldap);
@@ -58,14 +57,14 @@
 	global $sucsDB, $session, $error;
 	// Do some kind of checking here
 	// Check valid email (do we have a function for this?
-	if (!validEmail($externalEmail)) 
+	if (!validEmail($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)) {
 		trigger_error("You must have a contact phone number!", E_USER_WARNING);
 		return FALSE;
 	}
-	// Update 
+	// Update
 	if ($sucsDB->Execute('UPDATE members SET address = ?,phone = ?, email = ?, lastupdate = now(), lastedit = uid WHERE username = ?', 
 			array($address, $phone, $externalEmail, $session->username)) === FALSE) {
 		trigger_error("There was an error updating your contact details. Please contact admin.", E_USER_ERROR);
@@ -76,7 +75,7 @@
 
 function changeGuestnetDetails($wiredMAC) {
 	global $sucsDB, $session, $error;
-	// Get UID as we don't have it yet	
+	// Get UID as we don't have it yet
 	if(($uid = $sucsDB->GetOne("SELECt uid FROM members WHERE username = ?", array($session->username))) === FALSE) {
 		trigger_error("There was an error updating your GuestNET MAC Address. Please contact admin. (UID)", E_USER_ERROR);
 		return FALSE;
@@ -87,7 +86,7 @@
 		return FALSE;
 	}
 	exec('/usr/local/bin/update-dhcpd', $placeholder, $returnVar);
-	
+
 	if ($returnVar == 0) {
 		return TRUE;
 		//  $message[] = 'GuestNet MAC Address updated. Please wait a minute or two for the changes to take effect.';
@@ -112,7 +111,7 @@
 	// Sanity Check ip?
 	// Check we are in the correct ip range (unregistered GuestNET addresses)
 	if (ip2long($ip) && (ip2long($ip) >= ip2long(GUESTNET_LOW)) && (ip2long($ip) <= ip2long(GUESTNET_HIGH))) {
-	
+
 		exec("sudo /usr/local/sbin/gw-copy-arp.sh", $placeholder, $returnVar);
 		if ($returnVar == 0) {
 			$arpLines = file('/tmp/gwarp');
@@ -158,8 +157,7 @@
 // Takes an entry from $_FILES and makes this the new hackergotchi
 function updateHackergotchi($fileDetails) {
 	global $session;
-	if ( ((list($width, $height, $type, $attr) = @getimagesize($fileDetails['tmp_name'])) !== false)) { 
-	    
+	if ( ((list($width, $height, $type, $attr) = @getimagesize($fileDetails['tmp_name'])) !== false)) {
 		if ($type != IMAGETYPE_PNG) {
 			trigger_error("Uploaded hackergotchi is not in PNG format. Please convert your image to PNG and try again.", E_USER_ERROR);
 			return FALSE;
@@ -174,7 +172,7 @@
 		if( move_uploaded_file($fileDetails['tmp_name'], $imagefilepath) && chmod($imagefilepath, 0644) ) {
 			return TRUE;
 		}
-	} 
+	}
 	// We should have returned True by now
 	trigger_error("There was an error updating your hackergotchi. Please contact admin.", E_USER_ERROR);
 	return FALSE;
@@ -217,11 +215,8 @@
 
 if ($session->loggedin === TRUE) {
 
-	$sucsDB = NewADOConnection('postgres8');
-	$sucsDB->Connect('dbname='.$sucsdbname.' user=apache');
-	$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 //	$sucsDB->debug = true;
-	
+
 	$newGuestNetMAC = getGuestNetMAC();
 
 	// Handle Postbacks
@@ -248,7 +243,7 @@
 			case 'updatehackergotchi' :
 				if (updateHackergotchi($_FILES['hackergotchi'])) {
 					message_flash('Hackergotchi Updated');
-				} 
+				}
 				break;
 			case 'clearhackergotchi' :
 				if (clearHackergotchi()) {

Modified: branches/sucs-site/components/printer.graph.php
===================================================================
--- branches/sucs-site/components/printer.graph.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/printer.graph.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -1,8 +1,4 @@
 <?
-include("adodb/adodb.inc.php");
-$sucsDB = NewADOConnection('postgres8');
-$sucsDB->Connect('dbname=sucs user=apache');
-$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 
 $res = $sucsDB->Execute("select username, sum(pages) as pages from printer where username != 'old' group by username order by sum(pages) desc");
 $num = $res->RecordCount();
@@ -19,7 +15,7 @@
 	}
 //	$itemName[]="Other";
 //	$sliceSize[]=$otherSize;
-	
+
 	$slice = array_merge(array($otherSize),$slice);
 	$item = array_merge(array("Other"), $item);
 
@@ -29,4 +25,4 @@
 
 	include("../../lib/pieChart.php");
 	piechart("Top 12, Users of Printing", $slice, $item,0)
-?>
+?>
\ No newline at end of file

Modified: branches/sucs-site/components/printer.php
===================================================================
--- branches/sucs-site/components/printer.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/printer.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -9,9 +9,6 @@
 $timestamp = '2014-02-05 18:29:02+00'; //display printer stats since this time
 
 $smarty->assign('title',"Printer Statistics");
-$sucsDB = NewADOConnection('postgres8');
-$sucsDB->Connect('dbname=sucs user=apache');
-$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 
 // $out="<img src=\"/images/printer.graph.php\" alt=\"pie chart of top 12 printer users\"/>";
 $table="<table border=1 cellpadding=3>\n<tr>\n<th>Username</th>\n<th>Pages</th>\n<th>Projected Monthly Usage</th>\n<th>Printed This Month</th>\n<th>What it would cost in the library</th></tr>\n";
@@ -38,7 +35,7 @@
 
 $res = $sucsDB->Execute("select username, pages, start from printer where username != 'old'");
 $datesnum = $res->RecordCount();//for each row stuff the infomation into an array, then free up the database resorces
-for ($i=0;$i<$datesnum;$i++) {        
+for ($i=0;$i<$datesnum;$i++) {
 	$dates[$i] = $res->FetchRow($res, $i);
 }
 $res->Close();
@@ -55,7 +52,7 @@
 			if ($data[$j]['username'] == $dates[$i]['username']) {
 				$data[$j]['month'] += $dates[$i]['pages'];
 			}
-		} 
+		}
 	}
 }
 
@@ -69,7 +66,7 @@
 	//avg pages per month = seconds in a month / (now - first print) * pages
 	$crazyGuess = round((2629743.83/($thisSecond-$data[$i]['first']))*$data[$i]['pages']);
 	//if the user has only started printing in the last month its not sensible to give a guess of average useage
-	if ($thisSecond-$data[$i]['first'] <= 2629743.83) { 
+	if ($thisSecond-$data[$i]['first'] <= 2629743.83) {
 		$table.="<td>Not enough data</td>\n";
 	}
 	//traffic lights for pinter useage, adjust values as required

Modified: branches/sucs-site/components/signup-admin.php
===================================================================
--- branches/sucs-site/components/signup-admin.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/signup-admin.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -5,10 +5,6 @@
 if (isset($session->groups[$permission])) {
 	$smarty->assign("staff",TRUE);
 
-	// connect to sucs database
-	$sucsDB = NewADOConnection('postgres8');
-	$sucsDB->Connect('dbname=sucs user=apache');
-	$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 	//get unused signup slips
 	$query  = "SELECT signup.id, signup.sid, member_type.typename, members.username, signup.card";
        	$query .= " FROM signup LEFT JOIN member_type ON signup.type = member_type.id LEFT JOIN members ON signup.issuedby=members.uid";

Modified: branches/sucs-site/components/signup.php
===================================================================
--- branches/sucs-site/components/signup.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/signup.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -35,17 +35,9 @@
     //set signup details
     $signupid = $_REQUEST['signupid'];
     $signuppw = $_REQUEST['signuppw'];
-    // connect to sucs database
-    $sucsDB = NewADOConnection('postgres8');
+
     //$sucsDB->debug = true;
 
-    // -------------------------------------------------
-    // DEV:SET THIS TO YOUR DATBASE FOR DEV MODE
-    // -------------------------------------------------
-    $sucsDB->Connect('dbname=sucs');
-
-
-    $sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
     // get row(s)
     $query = "SELECT * FROM signup WHERE id=? AND password=?";
     $array = array($signupid,$signuppw);
@@ -379,7 +371,7 @@
 		    $smarty->assign("fields",$fields);
 		    $smarty->assign("errors",$errors);
         	    $smarty->append('extra_scripts', $script);
-                    $mode='re-form';	
+                    $mode='re-form';
 		}
 	    }
 	    else{

Modified: branches/sucs-site/components/susignup-admin.php
===================================================================
--- branches/sucs-site/components/susignup-admin.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/susignup-admin.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -1,6 +1,6 @@
 <?PHP
 /***
- * SU Signup admin 
+ * SU Signup admin
  * Allows us to search the SU api for a given student number and checks if they're a SUCS member.
  * If they are - allows account renewal
  * If not - allows signup to be bootstrapped as per susignup component
@@ -10,7 +10,7 @@
 include("../lib/date.php");
 include("../suapi.inc.php");
 
-//Restrict access to staff. 
+//Restrict access to staff.
 $permission="sucsstaff";
 $apibaseurl="https://$suapi_user:$suapi_pass@su-api.sucs.org/memberships/Membership.asmx";
 
@@ -24,13 +24,6 @@
 		$mode = urldecode($_REQUEST['mode']);
 	}
 
-	//Set up SUCS DB Connection.
-	//Note that $DB is the generic sucssite connection
-	
-	$sucsDB = NewADOConnection('postgres8');
-	$sucsDB->Connect('dbname=sucs user=apache');
-	$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
-
 	if ($mode == 'search') {
 		if (empty($_REQUEST['sid']) || empty($_REQUEST['snsubmit'])) {
 			$mode = 'error';

Modified: branches/sucs-site/components/susignup.php
===================================================================
--- branches/sucs-site/components/susignup.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/components/susignup.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -12,10 +12,10 @@
 //login
 if(!empty($_REQUEST['sid'])&&!empty($_REQUEST['transactionID'])){
 //set signup details
-	
+
 	$sid = $_REQUEST['sid'];
         $transactionID = $_REQUEST['transactionID'];
-                                            
+
 	//set POST variables
 	$url = "https://$suapi_user:$suapi_pass@su-api.sucs.org/memberships/Membership.asmx/GetMemberByTransactionID";
 	$fields = array(
@@ -36,21 +36,21 @@
 	curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
 	//execute post
 	$result = curl_exec($ch);
-	
+
 	//close connection
 	curl_close($ch);
 	if(!$result){
 		$mode = "error";
 		$smarty->assign("error_text", "Invalid query returned");
-	} else {	
+	} else {
 		libxml_use_internal_errors(true);
 		$xml=simplexml_load_string($result);
 		if (!$xml||isset($xml->head->title)) {
 			$mode='error';
 			$smarty->assign("error_text", "An error occurred communicating with the SUSU API");		
 		} else{
-			$re1='.*?';	
-			$re2='(\\{.*?\\})';	
+			$re1='.*?';
+			$re2='(\\{.*?\\})';
 			if ($c=preg_match_all ("/".$re1.$re2."/is", $result, $matches))
 			{
 			  	$json_string=$matches[1][0];
@@ -61,16 +61,9 @@
 			if($array['cardNumber'] == $sid && $sid != ""){
 				$mode = 'form';
 
-				// connect to sucs database
-				$sucsDB = NewADOConnection('postgres8');
-					
 				// SET THIS FOR DEBUG MODE
 				// $sucsDB->debug = true;
-				// SET THIS TO YOUR DB FOR TESTING
-				$sucsDB->Connect('dbname=sucs user=apache');
 
-				$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
-				
 				$fullName = $array['firstName']." ".$array['lastName'];
 				$query = "INSERT INTO transactions (transactionID,fullName,emailAddress,cardNumber,personID) values(?, ?, ?, ?, ?)";
 				$attribs[] = $transactionID;
@@ -86,7 +79,7 @@
 					unset($attribs);
 					$attribs[]=$transactionID;
 					if($sucsDB->Execute($query,$attribs)) {
-					
+
 						$query = "SELECT * FROM transactions WHERE transactionID = ?";
 						$attribs= array();
 						$attribs[]=$transactionID;
@@ -145,7 +138,7 @@
 				}
 
 				if($valid)
-				{	
+				{
 					unset($attribs);
 					unset($query);
 					$query = "SELECT * FROM members WHERE sid = ?";
@@ -160,7 +153,7 @@
 						$q="UPDATE members SET paid=?, lastupdate=DEFAULT, lastedit=? WHERE username=?;";
         					$r=$sucsDB->Execute($q, array(paidUntil(time()), '99999', $data->fields['username']));
 						if (!$r) {
-							mail(  
+							mail(
 								$error_email,
 								"SUSignup Error",
 								"An error occured in the susignup system at stage 2.5\n Love the signup system.\n\nP.S. The Error is:\n ".$sucsDB->ErrorMsg(),
@@ -178,7 +171,7 @@
 							$message .= "**** Payment was made via the SU payments system ****\n";
 							$message .= "Regards\n  The SU Renewals script";
 							mail("treasurer at sucs.org","Account Renewal",$message);
-	
+
 							$message = "Your Swansea University Computer Society (SUCS) membership has been renewed\n\n";
 							$message .= "Username: ".$renew_name."\n";
 							$message .= "If you do not know or have forgotten your password, please email admin at sucs.org to arrange for it to be changed.\n\n";
@@ -204,21 +197,21 @@
 									"From: \"SUCS Admin\" <admin at sucs.org>"
 								);
 							}
-				        	}	
+				        	}
 					} else {
-						$pass = make_password();		
+						$pass = make_password();
 						unset($query);
 						$query = "insert into signup (password,sid,issuedby) values( ?, ?, ?) returning id";
 						unset($attribs);
 						$attribs[]=$pass;
 						$attribs[]=$array['cardNumber'];
 						$attribs[]='99999';
-					
+
 						$iddata = $sucsDB->Execute($query,$attribs);
 						$id = $iddata->fields['id'];
 						if (!$iddata) {
 							$mode="error";
-							mail(   
+							mail(
 								$error_email,
 								"SUSignup Error",
 								"An error occured in the susignup system at stage 3\n Love the signup system.\n\nP.S. The Error is:\n ".$sucsDB->ErrorMsg(),
@@ -250,8 +243,8 @@
 							}
 						}
 					}
-				} 
-			} 
+				}
+			}
 		}
 	}
 }

Modified: branches/sucs-site/htdocs/index.php
===================================================================
--- branches/sucs-site/htdocs/index.php	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/htdocs/index.php	2015-03-04 15:15:22 UTC (rev 647)
@@ -37,12 +37,17 @@
 
 $smarty->assign("baseurl", $baseurl);
 
-// Initialise the database
+// Initialise the sucssite database conn
 require("/usr/share/php/adodb/adodb.inc.php");
 $DB = NewADOConnection('postgres9');
 $DB->Connect('dbname='.$dbname.' user='.$dbuser);
 $DB->SetFetchMode(ADODB_FETCH_ASSOC);
 
+// Initialise the sucs database conn
+$sucsDB = NewADOConnection('postgres9');
+$sucsDB->Connect('dbname='.$sucsdbname.' user='.$dbuser);
+$sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
+
 // Include the session library
 require($base."lib/session.php");
 $session = new Session;

Modified: branches/sucs-site/settingstemplate
===================================================================
--- branches/sucs-site/settingstemplate	2015-03-02 12:35:41 UTC (rev 646)
+++ branches/sucs-site/settingstemplate	2015-03-04 15:15:22 UTC (rev 647)
@@ -4,6 +4,7 @@
 $baseurl = "/~$USERNAME/sucssite";
 $preferred_hostname = "sucs.org";
 $dbname="beta";
+$sucsdbname="sucs";
 $dbuser="$USERNAME";
 
 // Settings for current election (god knows why they are here):




More information about the Devel mailing list