File "control.php"

Full Path: /home/analogde/www/RaspBerry/Dev/Raspberry/01/control.php
File size: 5.97 KB
MIME-type: text/x-php
Charset: utf-8

<?php
	session_start();
	
	error_reporting(0);

	$MySQLUsername = "root";
	$MySQLPassword = "";
	$MySQLHost = "localhost";
	$MySQLDB = "gpio";

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

	if(isset($_POST['action']))
	{
	    if($_POST['action'] == "setPassword")
		{
			$password1 = $_POST['password1'];
			$password2 = $_POST['password2'];
			if($password1 != $password2)
			{
			    header('Location: control.php');
			} 
			
			$password = mysql_real_escape_string($_POST['password1']);
			if(strlen($password) > 6)
			{
			   mysql_close();
				header('location: control.php');
			} 
			mysql_query("UPDATE users SET password='$password' WHERE username='admin'");
			mysql_close(); 
			echo("fqfqsfdsfdsfdsfdsfsd");
		}
   }

	if(isset($_GET['action']))
	{
	    if($_GET['action'] == "logout")
		{
		   $_SESSION = array();
		   session_destroy();
		   header('Location: login.php');
	    } 
		else if ($_GET['action'] == "setPassword")
		{
			$titi = 1;
			print '
					<form name="changePassword" action="control.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: control.php');
					} 
					else if($action == "turnOff")
					{	$setting = "0";
						mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
						mysql_close();
						header('Location: control.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 '
								<html><head><title>Update Pin ' . $pin . '</title></head><body>
								<table border="0">
								<form name="edit" action="control.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>
								</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: control.php');
					}
					else {
							header('Location: control.php');
					}
			}
			
	} 
	
	else 
	{
			print '
					<html>
					<link rel="shortcut icon" href="mosfet.ico" type="image/x-icon" />
					<head>
					<title>Alarm</title>
					</head>
					<font face="verdana">
					<p>Console de l\'alarme   <a href="control.php?action=setPassword">Change Password</a></p>';
		
					$query = mysql_query("SELECT pinNumber, pinStatus FROM pinStatus;");
					$query2 = mysql_query("SELECT pinNumber, pinDescription FROM pinDescription;");
					$totalGPIOCount = mysql_num_rows($query);
					$currentGPIOCount = 0;
					print '<table name="GPIO" border="1" cellpadding="5">';
					print '<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th><th>Action</th><th>Edit</th></tr>';
		
					while ($currentGPIOCount < $totalGPIOCount)
					{
						$pinRow = mysql_fetch_assoc($query);
						$descRow = mysql_fetch_assoc($query2);
						$pinNumber = $pinRow['pinNumber'];
						$pinStatus = $pinRow['pinStatus'];
						$pinDescription = $descRow['pinDescription'];
						if($pinStatus == "0")
						{
							$buttonValue = "Turn On";
							$action = "turnOn";
							$image = "off.jpg";
						}
						else
						{
							$buttonValue = "Turn Off";
							$action = "turnOff";
							$image = "on.jpg";
						}

						print '<tr>';
						print '<td align="center">' . $pinNumber . '</td>
							   <td>' . $pinDescription . '</td>
							   <td align="center"><img src="' . $image . '" width="50"></td>
							   <td align="center" valign="middle">
							   <form name="pin' . $pinNumber . 'edit" action="control.php" method="get">
							   <input type="hidden" name="action" value="' . $action . '">
							   <input type="hidden" name="pin" value="' . $pinNumber . '">
							   <input type="submit" value="' . $buttonValue . '"></form></td>
							   <td><form name="pin' . $pinNumber . '" action="control.php" method="get">
							   <input type="hidden" name="action" value="edit">
							   <input type="hidden" name="pin" value="' . $pinNumber . '">
							   <input type="submit" value="Edit"></form></td>';
						print '</tr>';
						$currentGPIOCount ++;
					}
		
					print '</table>';
					mysql_close();

					print '
							<br><br>
							<a href="control.php?action=logout">Log out</a>
							</font>
							</html>';
	}
	
	//exec("python client.py")
	
	//import subprocess

# if the script don't need output.
//subprocess.call("php /path/to/your/script.php")

# if you want output
//proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
//script_response = proc.stdout.read()

//$result = exec("/path/to/python2.3 /full/path/to/myprogram.py &> /full/path/to/home/pylog.txt");
//echo $result;
	
	
	
/*Calling python script from html/php

import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.output(7,True) ## Turn on GPIO pin 7*/

//http://www.pihomeserver.fr/2013/05/27/raspberry-pi-home-server-lancer-un-programme-automatiquement-au-demarrage/


?>