RPi Web Control

I have made progress and based on an instructables guide (see my previous article) I completed the design of my Web RPi control panel.
See below screenshots.
RPi_Web_Control_1

RPi_Web_Control_2

And here is the code for those who want:

GPIOServer.sh:

# Script created by Laurent Mahe from Daniel Curzon's original script (http://www.instructables.com/member/drcurzon)
# Initial version created December 2012

###################################
#####  EDIT THESE BEFORE USE  #####
###################################
mysqlusername="****"
mysqlpassword="****"
mysqlhostname="192.168.0.24"

#######################################################################
# setup:
#	Program the GPIO correctly and initialise the different parameters
#######################################################################

Setup ()
{
	printf "\033c"
	echo "Setup in progress..."
	Door_Lock=8
	MyLED=0
	MyLED_Status=0
	AlarmStatus=0
	MyLight=3
	MyPIR=4
	MyPIR2=5
	MyBuzzer=0

	#Invoke GPIO
	#echo "0" > /sys/class/gpio/export
	#echo "17" > /sys/class/gpio/export
	#echo "18" > /sys/class/gpio/export
	#echo "22" > /sys/class/gpio/export
	#echo "23" > /sys/class/gpio/export

	echo "in" > /sys/class/gpio/gpio0/direction
	echo "out" > /sys/class/gpio/gpio17/direction
	gpio -g mode 18 in
	echo "in" > /sys/class/gpio/gpio22/direction
	echo "in" > /sys/class/gpio/gpio23/direction
	echo "in" > /sys/class/gpio/gpio24/direction

	curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1
	curl http://192.168.0.24:8080/2/detection/start > /dev/null 2>&1
    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
	if [ `gpio read $Door_Lock` = 1 ]; then
	  echo "Lock Status = [1] Unlocked" 
	else
	  echo "Lock Status = [0] Locked"
	  gpio write $MyLED 1
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
	  echo -e `date +%F_%T` "Activating motion detection...\n"
	  curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
	  waitDoor_Unlock
	fi
	Update_MySQL
}

#######################################################################
# waitDoor_Lock:
#	Wait for the door to be locked. Because we are using pin 17 (which
#	has a pull up resistor) we wait for the signal to go low indicating
# 	the door has been locked, then wait 3 min before activating motion.
#######################################################################

waitDoor_Lock ()
{
  echo -n "Waiting for door to be locked... "
  while [ `gpio read $Door_Lock` = 1 ]; do
    Update_MySQL
  done
  echo -e "\n"`date +%F_%T` "Countdown 3 min..."
  gpio write $MyLED 1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
  sleep 180
  echo -e `date +%F_%T` "Activating motion detection...\n"
  curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
}

#######################################################################
# waitDoor_Unlock:
#	Wait for the door to be unlocked. Because we are using pin 17 (which
#	has a pull up resistor) we wait for the signal to go high indicating
# 	the door has been unlocked.
#######################################################################

waitDoor_Unlock ()
{
  echo -n "Waiting for door to be unlocked... "
  while [ `gpio read $Door_Lock` = 0 ]; do
    gpio write $MyLED $MyLED_Status
    if [ $MyLED_Status = 0 ]; then
      MyLED_Status=1
    else
      MyLED_Status=0
    fi
    Update_MySQL
  done
  echo -e "\n"`date +%F_%T` "Pausing motion detection...\n"
  curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
  gpio write $MyLED 0
}

#######################################################################
# SetBuzzer:
#	Plays buzzer
#	Use: SetBuzzer x y where x is Freq and y is length
#######################################################################

SetBuzzer ()
{
  local note="$1" time="$2"
  if test "$note" -eq 0 ; then
    gpio -g mode 18 in
  else
    local period="$(perl -e"printf'%.0f',600000/440/2**(($note-69)/12)")"
	gpio -g mode 18 pwm
    gpio pwmr "$((period))"
    gpio -g pwm 18 "$((period/2))"
    gpio pwm-ms
  fi
  sleep "$time"
}

#######################################################################
# Update_MySQL:
#	Checks PIR + Photo-Resistor:
#	if no movement after 23:00 and light is on -> Alarm
#	if door is unlocked and no light after 23:00 -> Alarm
#	Finally update MySQL table for Web Control Panel
#######################################################################

Update_MySQL ()
{
	  #checks time of day. If past 23:00, sets alarm if (no light AND door unlocked) OR (light AND no movememnt)
	  MyTime=`date +%H`
#	  if (( 1 )); then
	  if (( (${MyTime:1:1} > 2 && ${MyTime:0:1} == 2) || (${MyTime:1:1} < 4 && ${MyTime:0:1} == 0))); then
		if [ `gpio read $MyPIR` = 0 ] && [ `gpio read $MyPIR2` = 0 ] && [ `gpio read $MyLight` = 1 ]; then
		  echo -e `date +%F_%T` "\n"" Late, lights on and no movement --> [ALARM]"
		  AlarmStatus=1
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='18'";)
		  SetBuzzer 90 0.5
		else
		  AlarmStatus=0
		  gpio -g mode 18 in
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
		fi
		if [ `gpio read $Door_Lock` = 1 ] && [ `gpio read $MyLight` = 0 ]; then
		  echo -e `date +%F_%T` "\n"" Late, lights off and Door not locked --> [ALARM]"
		  AlarmStatus=1
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='18'";)
		  SetBuzzer 90 0.5
		else
		  AlarmStatus=0
		  gpio -g mode 18 in
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
		fi
	  fi
	  if ((AlarmStatus == 1)); then
			AlarmStatus=0
			gpio -g mode 18 in
			$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
	  fi

	#Get Door Lock Status
	if ((`cat /sys/class/gpio/gpio0/value` == 0)); then
		#set Door status in SQL to 1 (locked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
	else
		#set Door status in SQL to 0 (unlocked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='0'";)
	fi

	#Get Photo Resistor Status
	if ((`cat /sys/class/gpio/gpio22/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='22'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='22'";)
	fi

	#Get PIR1 Status
	if ((`cat /sys/class/gpio/gpio23/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='23'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='23'";)
	fi 

	#Get PIR2 Status
	if ((`cat /sys/class/gpio/gpio24/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='24'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='24'";)
	fi 

	#Get Door LED
	if ((`cat /sys/class/gpio/gpio17/value` == 0)); then
		#set Door status in SQL to 1 (locked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='17'";)
	else
		#set Door status in SQL to 0 (unlocked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
	fi

	#Set Buzzer
	if [ $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='18'";) == "1" ]; then
		if (($MyBuzzer == 0)); then
			SetBuzzer 90 0.5
			MyBuzzer=1
		fi
	else
		gpio -g mode 18 in
		MyBuzzer=0
	fi

	CAM1=`curl -s http://192.168.0.24:8080/1/detection/status`
	MyCAM1=${CAM1:137:6}
	if [ $MyCAM1 == "ACTIVE" ]; then
		#update SQL as active
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
	else
		#either in pause or not available
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
	fi

	CAM2=`curl -s http://192.168.0.24:8080/2/detection/status`
	MyCAM2=${CAM2:137:6}
	if [ $MyCAM2 == "ACTIVE" ]; then
		#update SQL as active
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
	else
		#either in pause or not available
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
	fi
}

#Start Loop
while :
do
  Setup
  waitDoor_Lock
  waitDoor_Unlock
done

 

control.php:

<HTML>
<HEAD>
<title>RPi Control Panel</title>
<link rel="stylesheet" type="text/css" href="StyleSheet.css" />
<HEAD>
<STYLE type="text/css">
	BODY
	{
	background-attachment: fixed;
	background-image:  url(RPi_Bkgrd.jpg);
	background-repeat: no-repeat;
	background-position: center center;
	}
</STYLE>
<script src="jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready (function () {
	var updater = setTimeout (function () {
	$("div#myDiv").load("index.php","action=refresh");
	}, 500);
});
</script>
</HEAD>
<BODY>

<?php
session_start();
//////////////////////////////
// EDIT THESE TWO VARIABLES //
//////////////////////////////
$MySQLUsername = "****";
$MySQLPassword = "****";

/////////////////////////////////
// DO NOT EDIT BELOW THIS LINE //
/////////////////////////////////
$MySQLHost = "127.0.0.1";
$MySQLDB = "gpio";

/* gets the data from a URL */
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
	print 'ERROR - Please set up the script first';
	exit();
}

$dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
mysql_select_db($MySQLDB, $dbConnection);

$tbl = '';
$tbl .= "<table name='GPIO' border='1' cellpadding='5'>";
$tbl .= "<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th></tr>";

//Door Lock
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=0");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=0");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "Lock-Off.png";
} else {
	$image = "Lock-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Door LED
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=17");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=17");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "LED-Off.png";
} else {
	$image = "LED-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Buzzer
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=18");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=18");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Buzzer-on";
	$image = "Buzzer-Off.png";
} else {
	$action = "Buzzer-off";
	$image = "Buzzer-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

//Photo Resistor
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=22");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=22");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "Photo-Resistor-Off.png";
} else {
	$image = "Photo-Resistor-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//PIR1
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=23");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=23");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "PIR-Off.png";
} else {
	$image = "PIR-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//PIR1
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=24");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=24");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "PIR-Off.png";
} else {
	$image = "PIR-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Garden Cam
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=91");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=91");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Back-on";
	$image = "CCTV-Off.png";
} else {
	$action = "Back-off";
	$image = "CCTV-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

//Front Cam
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=92");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=92");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Front-on";
	$image = "CCTV-Off.png";
} else {
	$action = "Front-off";
	$image = "CCTV-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

$tbl .= "</table>";

If (isset($_POST['action'])){
	If ($_POST['action'] == "setPassword"){
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		If ($password1 != $password2){
			header('Location: index.php');
		}
		$password = mysql_real_escape_string($_POST['password1']);
		If (strlen($password) > 28){
			mysql_close();
			header('location: index.php');
		}
		$resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
		$resetResult = mysql_query($resetQuery);
		If (mysql_num_rows($resetResult) < 1){
			mysql_close();
			header('location: index.php');
		}
		$resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
		$resetHash = hash('sha256', $salt . hash('sha256', $password));
		$hash = hash('sha256', $password);
		function createSalt(){
			$string = md5(uniqid(rand(), true));
			return substr($string, 0, 8);
		}
		$salt = createSalt();
		$hash = hash('sha256', $salt . $hash);
		mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
		mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
		mysql_close();
		header('location: index.php');
	}
}
If ((isset($_POST['username'])) && (isset($_POST['password']))){
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
	$loginResult = mysql_query($loginQuery);
	If (mysql_num_rows($loginResult) < 1){
		mysql_close();
		header('location: index.php?error=incorrectLogin');
	}
	$loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
	$loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
	If ($loginHash != $loginData['password']){
		mysql_close();
		header('location: index.php?error=incorrectLogin');
	} else {
		session_regenerate_id();
		$_SESSION['username'] = "admin";
		$_SESSION['userID'] = "1";
		mysql_close();
		header('location: index.php');
	}
}
If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
	print '
		<div id="main">
		<table border="0" width="90%">
		<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
		</table>
		<font face="verdana">
		<center>
	';
	print '
	<table border="0" align="center">
	<form name="login" action="index.php" method="post">
	<tr>
	<td>Username: </td><td><input type="text" name="username"></td>
	</tr>
	<tr>
	<td>Password: </td><td><input type="password" name="password"></td>
	</tr>
	<tr>
	<td colspan="2" align="center"><input type="submit" value="Log In"></td>
	</tr>
	</form>
	</table>
	</div>
	</body>
	</html>
	';
	die();
}
If (isset($_GET['action'])){
	If ($_GET['action'] == "logout"){
		$_SESSION = array();
		session_destroy();
		header('Location: index.php');
	} else If ($_GET['action'] == "setPassword"){
		print '
		<div id="main">
			<table border="0" width="90%">
			<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
			</table>
			<font face="verdana">
			<center>
		';
		print '
		<form name="changePassword" action="index.php" method="post">
		<input type="hidden" name="action" value="setPassword">
		<p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
		</form>
		';
	} else {
		$action = $_GET['action'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: index.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: index.php');
		} else If ($action == "Back-on"){
			exec ("curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Back-off"){
			exec ("curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Front-on"){
			exec ("curl http://192.168.0.24:8080/2/detection/start > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Front-off"){
			exec ("curl http://192.168.0.24:8080/2/detection/pause > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Buzzer-on"){
			exec ('$(mysql -B --host=' . $MySQLHost  . ' --disable-column-names --user=' . $MySQLUsername . ' --password=' . $MySQLPassword . ' gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=18";)', &$output);
			header('Location: index.php');
		} else If ($action == "Buzzer-off"){
			exec ('$(mysql -B --host=' . $MySQLHost  . ' --disable-column-names --user=' . $MySQLUsername . ' --password=' . $MySQLPassword . ' gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=18";)', &$output);
			header('Location: index.php');
		} else IF ($action =="edit"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
			$descRow = mysql_fetch_assoc($query);
			$description = $descRow['pinDescription'];
			print '
				<div id="main">
				<table border="0" width="90%">
				<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
				</table>
				<font face="verdana">
				<center>
			';
			print '
			<table border="0">
			<form name="edit" action="index.php" method="get">
			<input type="hidden" name="action" value="update">
			<input type="hidden" name="pin" value="' . $pin . '">
			<tr>
			<td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
			</tr>
			</form>
			</table>
			</div>
			</body></html>
			';
			mysql_close();
		} else IF ($action =="update"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$description = mysql_real_escape_string($_GET['description']);
			mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
			header('Location: index.php');
		}
		else IF ($action =="refresh")
		{
			echo $tbl;
			die ();
		}
		else {
			header('Location: index.php');
		}

	}
} else {
	mysql_close();
	print '
		<div id="main">
		<table border="0" width="90%">
		<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
		</table>
		<font face="verdana">
		<center>
		<br><br><div id="myDiv">
	';
	echo $tbl;	
	print '</div><br><br>
	<p><a href="index.php?action=setPassword">Change Password</a></p>
	<a href="index.php?action=logout">Log out</a>
	</center>
	</font></div>
	</body></html>
	';
}	
?>