<?php
if(isset($_GET['a']) && !empty($_GET['a']) && is_numeric($_GET['a']) && $_GET['a']<=3 && $_GET['a']>0)
{

if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
db_open();	
$a= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["a"]))));
$str="";
$msg="";
$data=array();
$aa=password($option=4,$_SESSION['userlevel'],$_SESSION['email'],$_SESSION['password'],$_SESSION['device_id'],0);
if ($aa==1)
{
$email=$_SESSION['email'];
$device_id=$_SESSION['device_id'];
$language=$_SESSION['language'];
if ($language==1)
	{
	$head1="Eνημέρωση επιλογών!";
	$head4=$head2="Καμία αλλαγή.";
	$head3="Ενημέρωση κωδικού!";
	$head5="Ενημέρωση email!";
	$head6="Αποστολή μηνύματος!";
	$head7="Σφάλμα μηνύματος.";
	$pattern="/[^!.,_A-Za-zάΆέΈήΉίΊόΌύΎώΏΖζΧχΨψωΩβΒνΝμΜΛλΚκΞξΗηΓγΦφΔδΣσΑαςΕεΡρΤτΥυΘθΙιΟοπΠ0-9\' ']/";
	
	}
else
	{
	$head1="Info Updated!";
	$head4=$head2="No action!";
	$head3="Password updated! ";
	$head5="Email updated!";
	$head6="Message sent!";
	$head7="Message cannot added!";
	$pattern="/[^!.,_A-Za-z0-9\' ']/";
	}

if($a==1)//device management
	{
	$string="";
	for($i=1;$i<=5;$i++)
		{
		$str="str".$i;
		if(isset($_GET[$str]))
			{
			$str= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET[$str]))));
			if($str=="atmosphere1")
				{
				$str="changes='1'";
				}
			else if($str=="atmosphere0")
				{
				$str="changes='0'";
				}			
			else if ($str=="troubleshoot1")
				{
				$str="troubleshoot='1'";
				}
			else if ($str=="troubleshoot0")
				{
				$str="troubleshoot='0'";
				}
			else if ($str=="lost1")
				{
				$str="lost='1'";
				}
			else if ($str=="lost0")
				{
				$str="lost='0'";
				}
			else if ($str=="s1" || $str=="s2" || $str=="s3" || $str=="s4" ||$str=="s0")
				{
				$str=$str[1];
				if ($str==0)
					{
					$str="sensor_overview='0'";
					}
				else
					{
					$str="sensor_overview='$str'";
					}
				}				
			else if	($str=="d1" || $str=="d2" || $str=="d3" || $str=="d4" || $str=="d0")
				{
				$str=$str[1];
				if($str==0)
					{
					$str="device_overview='0'";
					}
				else
					{
					$str="device_overview='$str'";
					}
				}
			else 
				{
				$str="";
				}
				
			if ($str!="")
				{
				if ($string!="")
					{
					$string=$string.",".$str;
					}
				else
					{
					$string=$str;
					}
				}
			}
				
		
		}	
		if ($string!="")
		{		
		$sql="UPDATE users SET $string WHERE email='$email' AND device_id='$device_id'";
		@ $result=mysql_query($sql);
		if ($result==1)
			{
			$msg="$head1 ";
			}
		else
			{
			$msg="$head2";
			}
		}	
	}
else if ($a==2) //change info
	{
	for($i=0;$i<=4;$i++)
		{
		$str="str".$i;
		if(isset($_GET[$str]) && !empty($_GET[$str]))
			{
			$data[$i]= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET[$str]))));
			}
		else
			{
			$data[$i]="";
			}
		}
	if (!empty($data[1]))
	{
	if($data[2]!="" && $data[3]=="")
		{
		$data[2]=$data[3]="";
		}
	else if ($data[2]!="" && $data[3]!="" && $data[2]==$data[3])
		{
		$us=user_find($email);
		$pass=drogos($data[2],$us[0]);
		$sql="UPDATE users SET password='$pass' WHERE email='$email' AND device_id='$device_id'";
		@ $result=mysql_query($sql);
		if ($result==1)
			{
			$msg="$head3";
			$_SESSION['password']=$pass;
			}
		else
			{
			$msg="$head4";
			}
		}
	if($data[4]!="")
		{
		$sql="UPDATE users SET email='$data[4]' WHERE email='$email' AND device_id='$device_id'";
		@ $result=mysql_query($sql);
		if ($result==1)
			{
			$msg=$msg."$head5";
			$_SESSION['email']=$email;
			}
		else
			{
			$msg=$msg."$head4";
			}
		}
	$device_id=$_SESSION['device_id'];
$sql="SELECT * FROM users where device_id='$device_id'";
@ $result=mysql_query($sql);
$row=mysql_fetch_array($result);
$email=$row['email'];
	session_unset();
	session_destroy();
	session_start();
	session_regenerate_id();
	$b=user_find($email);
	$_SESSION['email']=$b[2];
	$_SESSION['password']=$b[12];
	$_SESSION['device_id']=$b[1];
	$_SESSION['userlevel']=$b[3];
	$_SESSION['language']=$b[16];
	}
	}
else//admin contact
	{
	for($i=0;$i<=2;$i++)
		{
		$str="str".$i;
		if(isset($_GET[$str]) && !empty($_GET[$str]))
			{
			$data[$i]= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET[$str]))));
			}
		else
			{
			$data[$i]="";
			}
		}
	if(($data[1]!="" && $data[2]!="") && (!preg_match($pattern, $data[1]) && !preg_match($pattern, $data[2])))
		{
		$subject=$data[1];
		$message=$data[2];
		$us=user_find($email);
		$id=$us[0];
		$deid=$us[1];
		$date=date("Y-m-d H-i-s");
		$sql="INSERT INTO messages (message_id,device_id,user_id,subject,message,dater,r) VALUES(null,'$deid','$id','$subject','$message','$date','0')";
		@ $result=mysql_query($sql);
		if ($result==1)
			{
			$sql1="SELECT messages FROM users WHERE user_id='$id'";
			$r=mysql_fetch_array(mysql_query($sql1));
			$r=$r[0]+1;
			$sql2="UPDATE users SET messages='$r' WHERE user_id='$id'";
			@ $rr=mysql_query($sql2);
			if ($rr!=0)
				{
				$msg="$head6";
				}
			else
				{
				$msg="$head7";
				}
			}
		else
			{
			$msg="$head7";
			}
		}
	}
	


echo $msg;
	}
db_close();
	}
?>