From ca10558c056d18ed25c1442ca2225835efb37451 Mon Sep 17 00:00:00 2001
From: Tom Lake <tswsl1989@sucs.org>
Date: Thu, 27 Oct 2011 15:23:00 +0000
Subject: [PATCH] Port the printer shame script from the old site, rearrange
 the disk shame script (and rename it) Also includes fixes made to the site
 during freshers week that haven't been committed yet

---
 .../{disk.graph/index.php => disk.graph.php}  |   4 +-
 components/{shame.php => disk.php}            |   2 +-
 components/membershiprenew.php                |   4 +-
 components/printer.graph.php                  |  32 +++++
 components/printer.php                        | 131 ++++++++++++++++++
 components/signup.php                         |  11 +-
 {components/disk.graph => lib}/pieChart.php   |  16 ++-
 lib/punbb/include/common.php                  |   1 -
 lib/validation.php                            |   5 +-
 9 files changed, 187 insertions(+), 19 deletions(-)
 rename components/{disk.graph/index.php => disk.graph.php} (84%)
 rename components/{shame.php => disk.php} (86%)
 create mode 100644 components/printer.graph.php
 create mode 100644 components/printer.php
 rename {components/disk.graph => lib}/pieChart.php (91%)

diff --git a/components/disk.graph/index.php b/components/disk.graph.php
similarity index 84%
rename from components/disk.graph/index.php
rename to components/disk.graph.php
index 598e360..a91e2f4 100644
--- a/components/disk.graph/index.php
+++ b/components/disk.graph.php
@@ -21,6 +21,6 @@
 	print_r($slice);
 	print_r($item);*/
 
-	include("pieChart.php");
-	piechart("Top 12 Users of disk space", $slice, $item)
+	include("../../lib/pieChart.php");
+	piechart("Top 12 Users of disk space", $slice, $item, 1)
 ?>
diff --git a/components/shame.php b/components/disk.php
similarity index 86%
rename from components/shame.php
rename to components/disk.php
index 6119770..ad5b7fb 100644
--- a/components/shame.php
+++ b/components/disk.php
@@ -1,6 +1,6 @@
 <?	exec("/usr/local/bin/shame.py -a", $Shame);
 	$smarty->assign('title',"The SUCS Hall of Shame");
-	$o="<div style='text-align: center;'><img src=\"/images/shame.php\" alt=\"pie chart of top 12 disk users\"/></div>";
+	$o="<div style='text-align: center;'><img src=\"/images/disk.graph.php\" alt=\"pie chart of top 12 disk users\"/></div>";
 
 	$o.="<table align=\"center\">\n";
 	$o.="\t<tr><th align=\"center\">User</th><th align=\"center\">Home Directory Size</th></tr>\n";
diff --git a/components/membershiprenew.php b/components/membershiprenew.php
index 54d77a1..04a33ed 100644
--- a/components/membershiprenew.php
+++ b/components/membershiprenew.php
@@ -109,8 +109,8 @@ if (isset($session->groups[$permission])) {
             $getrefresh=$refreshval;
 	    }
 	    else{
-	        // otherwise use 10
-	        $refreshval="10";
+	        // If no valid value assigned, default to OFF
+	        $refreshval="n";
 	    }
 	    $optrefresh=$refreshval;
 	    $smarty->assign("refresh", $refreshval);
diff --git a/components/printer.graph.php b/components/printer.graph.php
new file mode 100644
index 0000000..5375f47
--- /dev/null
+++ b/components/printer.graph.php
@@ -0,0 +1,32 @@
+<?
+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();
+	for ($i=0;$i<$num;$i++) {
+	$row[$i] = $res->FetchRow();
+		if ($i < 12)
+		{
+			$item[]=$row[$i]['username'];
+			$slice[]=$row[$i]['pages'];
+		}
+		else {
+			$otherSize += $row[$i]['pages'];
+		}
+	}
+//	$itemName[]="Other";
+//	$sliceSize[]=$otherSize;
+	
+	$slice = array_merge(array($otherSize),$slice);
+	$item = array_merge(array("Other"), $item);
+
+/*	echo "<pre>";
+	print_r($slice);
+	print_r($item);*/
+
+	include("../../lib/pieChart.php");
+	piechart("Top 12, Users of Printing", $slice, $item,0)
+?>
diff --git a/components/printer.php b/components/printer.php
new file mode 100644
index 0000000..4f74242
--- /dev/null
+++ b/components/printer.php
@@ -0,0 +1,131 @@
+<?PHP
+/******
+ * Printer Shame List
+ * Ported from old site by tswsl1989
+ *
+ */
+
+$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";
+
+//pull in all the current users (ie not old) who have printed stuff
+$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();
+//for each row stuff the infomation into an array, then free up the database resorces
+for ($i=0;$i<$num;$i++) {
+	$data[$i] = $res->FetchRow();
+	$data[$i]['month'] = 0;
+}
+$res->Close();
+
+//get the second since epoch
+$thisSecond = gmmktime();
+//for each user who has printed stuff add the first printed time (since epoch) to the array 
+for ($i=0;$i<$num;$i++) {
+	$res = $sucsDB->Execute("select date_part('epoch',start) as first from printer where username ='".$data[$i]['username']."' order by start asc limit 1;");
+	$temp = $res->FetchRow();
+	$data[$i]['first'] = $temp['first'];
+	$res->Close();
+}
+
+$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++) {        
+	$dates[$i] = $res->FetchRow($res, $i);
+}
+$res->Close();
+
+
+// get the current month
+$theMonth = date("m");
+$theYear = date("Y");
+for ($i=0;$i<$datesnum;$i++) {
+	$userMonth = substr($dates[$i]['start'], 5, 2);
+	$userYear = substr($dates[$i]['start'], 0, 4);
+	if (($userMonth == $theMonth) && ($userYear == $theYear)) {
+		for ($j=0;$j<$num;$j++) {
+			if ($data[$j]['username'] == $dates[$i]['username']) {
+				$data[$j]['month'] += $dates[$i]['pages'];
+			}
+		} 
+	}
+}
+
+//start the total counter
+$total = 0;
+//for each user.. print a row in the table
+for ($i=0;$i<$num;$i++) {
+	$table.="<tr align=\"center\">\n";
+	$table.="<td>{$data[$i]['username']}</td>\n";
+	$table.="<td>{$data[$i]['pages']}</td>\n";
+	//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) { 
+		$table.="<td>Not enough data</td>\n";
+	}
+	//traffic lights for pinter useage, adjust values as required
+	elseif($crazyGuess <= 25) {
+		$table.="<td BGCOLOR=\"Green\">".$crazyGuess."</td>\n";
+	}
+	elseif($crazyGuess <= 33) {
+		$table.="<td BGCOLOR=\"Orange\">".$crazyGuess."</td>\n";
+	}
+	else{
+		$table.="<td BGCOLOR=\"Red\">".$crazyGuess."</td>\n";
+	}
+	if ($data[$i]['month'] >= 300) {
+		$table.="<td BGCOLOR=\"Red\">{$data[$i]['month']}</td>\n";
+	} else {
+		$table.="<td>{$data[$i]['month']}</td>\n";
+	}
+	$table.="<td>".sprintf("£ %01.2f",round(0.1*$data[$i]['pages'],2))."</td>\n";
+	//add this users useage to the total
+	$total += $data[$i]['pages'];
+	$table.="</tr>\n";
+}
+
+//get the first print date
+$res = $sucsDB->Execute("select date_part('epoch',start) as first from printer order by start asc limit 1;");
+$temp = $res->FetchRow();
+$date['first'] = getdate($temp['first']);
+$res->Close();
+//get the last print date
+$res = $sucsDB->Execute("select date_part('epoch',start) as last from printer order by start desc limit 1;");
+$temp = $res->FetchRow();
+$date['last'] = getdate($temp['last']);
+$res->Close();
+//get the number of pages printed by "old" users and add it to the total
+$res = $sucsDB->Execute("select sum(pages) as pages from printer where username = 'old' group by username order by sum(pages) desc");
+$old = $res->FetchRow();
+$old = $old[0];
+$res->Close();
+$total += $old;
+//Output two lines at the bottem of the table, one for "old" users and the other for the total
+$table.="<tr align=\"center\">\n\t<th>Old Users</th>\n\t<th>$old</th>\n\t<th>n/a</th>\n</tr>";
+$table.="<tr align=\"center\">\n\t<th>Total</th>\n\t<th>$total</th>\n\t<th>".round((2629743.83/($thisSecond-$date['first'][0]))*$total)."</th>\n</tr>";
+$table.="</table>\n";
+
+//output the dates of the first and last prints recorded
+$out.="<p>First Data -  ".$date['first'][weekday].", ".$date['first'][mday]." ".$date['first'][month]." ".$date['first'][year].", at ".$date['first'][hours].":".$date['first'][minutes].":".$date['first'][seconds]."<br />";
+$out.="Last Data -  ".$date['last'][weekday].", ".$date['last'][mday]." ".$date['last'][month]." ".$date['last'][year].", at ".$date['last'][hours].":".$date['last'][minutes].":".$date['last'][seconds]."</p>";
+//do some silly maths to work out lots of silly things
+$paperarea = round(0.21*0.297*$total,3); //area of paper
+$paperweight = round($paperarea*.08, 3); //how much that would weigh
+$numtrees = round($paperweight/730.296, 6); //*very* roughly how many trees that would be
+$cost = round(0.1*$total,2);
+$out.="<p>That's ".$paperarea."m<sup>2</sup> of paper, weighing ".$paperweight."kg!<br>\n";
+$out.="This is equivalent to approximately ".$numtrees." trees.<br>\n";
+$out.="That would have cost our members a grand total of £".$cost." if it were printed in the library.. not bad for £5!";
+
+//done
+$out.="<p>Note: The number of pages is the number spooled and may be more than the actual number printed</p>";
+$out.=$table;
+$sucsDB->Close();
+$smarty->assign('body',$out);
+?>
diff --git a/components/signup.php b/components/signup.php
index d86e24d..d2534d0 100644
--- a/components/signup.php
+++ b/components/signup.php
@@ -183,7 +183,7 @@ if(isset($_REQUEST['signupid'])&&isset($_REQUEST['signuppw'])){
 					    mail(
 						    $error_email,
 						    "Signup Error",
-						    "User ".$fields['username']." already exsists in the databse, THIS SHOULD NEVER HAPPERN\n Love the signup system.\n\nP.S. the signup id is: ".$signupid,
+						    "User ".$fields['username']." already exists in the databse, THIS SHOULD NEVER HAPPEN\n Love the signup system.\n\nP.S. the signup id is: ".$signupid,
 						    "From: \"SUCS Admin\" <admin@sucs.org>"
 					    );
 					    $failed=true;
@@ -323,8 +323,11 @@ if(isset($_REQUEST['signupid'])&&isset($_REQUEST['signuppw'])){
 				    }
 			    }
 	
-	
-		            $addtolist ="".$fields['email']."\n".$fields['studentid']."@swan.ac.uk";
+                            if ($row['type']!=2){	
+			            $addtolist ="".$fields['email']."\n".$fields['studentid']."@swan.ac.uk";
+                            }else{
+				    $addtolist="".$fields['email']."\n"; //Societies don't have student email addresses
+		            }
 			    file_put_contents('/tmp/listadd.'.$fields['username'],$addtolist);
 			    unset($execoutputarr);
 			    exec(
@@ -353,7 +356,7 @@ if(isset($_REQUEST['signupid'])&&isset($_REQUEST['signuppw'])){
 			    $errorreparray=$_POST;
 			    unset($errorroparray[signuppw]);
 			    $errorreparray[uid]=$uid;
-			    $errorreport = "User inputed detailes:\n\n";
+			    $errorreport = "User input details:\n\n";
 			    foreach ($errorreparray as $key => $value){
 				    $errorreport .= $key.": ".$value."\n";
 			    }
diff --git a/components/disk.graph/pieChart.php b/lib/pieChart.php
similarity index 91%
rename from components/disk.graph/pieChart.php
rename to lib/pieChart.php
index 7edcc68..2b38c09 100644
--- a/components/disk.graph/pieChart.php
+++ b/lib/pieChart.php
@@ -1,6 +1,6 @@
 <?
 
-function piechart($title, $slice, $itemName) {
+function piechart($title, $slice, $itemName, $fsizes=0) {
 
 	function matchset($xx)
 	{
@@ -132,13 +132,15 @@ function piechart($title, $slice, $itemName) {
 	
 		ImageFilledRectangle($im, 340, $adjPosition, 350, ($adjPosition+10), $black);
 		ImageFilledRectangle($im, 341, ($adjPosition+1), 349, ($adjPosition+9), $WedgeColor);
-		if($sliced[$z] >= "1000" && $sliced[$z] < "1000000")
-		{
-			$sliced[$z] = $sliced[$z]/1000;
-			$sliced[$z] = sprintf("%01.2f", "$sliced[$z]")."G";
+		if($fsizes){
+			if($sliced[$z] >= "1000" && $sliced[$z] < "1000000")
+			{
+				$sliced[$z] = $sliced[$z]/1000;
+				$sliced[$z] = sprintf("%01.2f", "$sliced[$z]")."G";
+			}	
+			else
+				$sliced[$z] = "$sliced[$z]"."M";
 		}
-		else
-		$sliced[$z] = "$sliced[$z]"."M";
 		$sliceLen = strlen($sliced[$z]);
 		if($sliceLen == '5'){$sliced[$z] = " "."$sliced[$z]";}
 		if($sliceLen == '4'){$sliced[$z] = "  "."$sliced[$z]";}
diff --git a/lib/punbb/include/common.php b/lib/punbb/include/common.php
index 75aaa7a..26af9e6 100644
--- a/lib/punbb/include/common.php
+++ b/lib/punbb/include/common.php
@@ -54,7 +54,6 @@ $pun_start = ((float)$usec + (float)$sec);
 // Make sure PHP reports all errors except E_NOTICE. PunBB supports E_ALL, but a lot of scripts it may interact with, do not.
 error_reporting(E_ALL ^ E_NOTICE);
 
-// Turn off magic_quotes_runtime
 if (get_magic_quotes_runtime())
 	set_magic_quotes_runtime(0);
 
diff --git a/lib/validation.php b/lib/validation.php
index 076ecb5..7aad3c6 100644
--- a/lib/validation.php
+++ b/lib/validation.php
@@ -253,8 +253,9 @@ function validSocName($socname,$override){
 
 function validAddress($address){
 	global $error;
-	$address = sanitizeAddress($address);
-        if(!preg_match("/^([A-Z0-9]([[:alnum:]]|[ ./'-])*\n)+[A-Z0-9]([[:alnum:]]|[ ./'-])*$/",$address)){
+    $address = sanitizeAddress($address);
+    $regex="/^([A-Z0-9]([[:alnum:]]|[ .\/'-])*\n)+[A-Z0-9]([[:alnum:]]|[ .\/'-])*$/";
+        if(!preg_match($regex,$address)){
                 $error = "Please supply at least two valid lines of address.";
                 return false;
         }   
-- 
GitLab