<?php

function db_close() {
@ mysql_close($db);
}

function db_open() {	
	$dtbhost=":/zstorage/home/ictest00120/mysql/run/mysql.sock";
	$dtbuser="root";
	$dtbpass="root";
	
	$dtbname="smartgreenhouse";
	@ $db = mysql_connect("$dtbhost", "$dtbuser", "$dtbpass") or die('00: No Connection');
	@ mysql_select_db("$dtbname") or die('01: No Database');
	mysql_query("SET CHARACTER SET 'utf8'"); 
	mysql_query("SET NAMES 'utf8'");
}

function password($option,$userlevel,$email,$password,$device_id,$live_login)
{
if(is_numeric($option) && ($option<=4))
	{
	$password= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($password))));
	$userlevel= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($userlevel))));
	$email= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($email))));
	$device_id= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($device_id))));
	$live_login= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($live_login))));
	$final="";
	if (preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email) && ($userlevel==0 || $userlevel==1) && is_numeric($device_id) && is_numeric($live_login))
	{
	
	if ($option==1)//to live login paizei to rolo tou private_id
		{
		$sql_insert="INSERT INTO users (user_id,device_id,email,password,userlevel,last_login,messages,live_login,troubleshoot,changes,device_overview,sensor_overview,lost,visits,active,private_id,language)
		VALUES(null,'$device_id','$email','','$userlevel','','','','','','','','','','0','$live_login','1')";
		@ $res=mysql_query($sql_insert);
		if ($res!=0)
			{
			$a=user_find($email);
			$id=$a[0];
			$aa=drogos($password,$id);
			$password=$aa;
			$sql="UPDATE users SET password='$password' WHERE email='$email' AND user_id='$id' AND device_id='$device_id'";
			@ $result= mysql_query($sql);
			if ($result!=0)
				{
				$final=1;
				}
			}
		}
	else if ($option==2)
		{
		$a=user_find($email);
		$userlevel=$a[3];
		$id=$a[0];
		$aa=drogos($password,$id);
		$password=$aa;
		$sql="SELECT * FROM users WHERE email='$email' AND password='$password' AND live_login<'$live_login' AND active='1'";
		@ $resulta= mysql_query($sql);
		@ $result=mysql_numrows($resulta);
		if ($result !=0)
			{
			$live_login=strtotime("+ 5 minutes", $live_login);
			$sql1="UPDATE users SET live_login='$live_login',visits=visits+1 WHERE email='$email' AND password='$password' AND active='1'";
			@ $resulto=mysql_query($sql1);
			if ($resulto!=0)
				{
				if ($userlevel==1)
					{
					$final=1;
					}
				else
					{
					$final=11;
					}
				}
			}
		}
	else if($option==3)
		{
		$a=user_find($email);
		$userlevel=$a[3];
		$id=$a[0];
		$aa=drogos($password,$id);
		$password=$aa;
		$sql="SELECT * FROM users WHERE email='$email' AND password='$password' AND device_id='$device_id' AND userlevel='$userlevel' AND active='1'";
		@ $resulta= mysql_query($sql);
		$r=mysql_numrows($resulta);
		if ($r==1)
			{
			$final=1;
			}
		}
	else if($option==4)
		{
		$sql="SELECT * FROM users WHERE email='$email' AND password='$password' AND device_id='$device_id' AND userlevel='$userlevel' AND active='1'";
		@ $resulta= mysql_query($sql);
		$r=mysql_numrows($resulta);
		if ($r==1)
			{
			$live_login=strtotime("+ 5 minutes", $live_login);
			$sql1="UPDATE users SET live_login='$live_login' WHERE email='$email' AND password='$password'";
			@ $resulto=mysql_query($sql1);
			if ($resulto!=0)
				{
				$final=1;
				}
			}
		}
	else
		{
		die();
		}
	
	if ($final!=1 && $final!=11)
		{
		$final=2;
		}
	return $final;
	}
}
}


function drogos($password,$id)
{
$password= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($password))));
$id= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($id))));
$password=strrev($password);
$l=strlen($password);
$key='p'.$id.'a'.$l.'s'.$l.'t'.$id.'@'.$l.'n'.$id.'*'.$l.'k'.$password[0].'a';
$m=(($l*$id) % 69);
if ($id<$l)
	{
	$m=$m + ($l % $id);
	}
else
	{
	$m=$m + ($id % $l);
	}
$hash=sha1(md5($key.$password));
for ($i=1;$i<=$m;$i++)
	{
	$hash=sha1($hash);
	}
return $hash;
}

function user_find($email)
{
$email= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($email))));
$data=array();

$sql1="SELECT * FROM users WHERE email='$email'";
@ $resu=mysql_query($sql1);
@ $result=mysql_numrows($resu);
			if ($result!=0)
				{
				$row=mysql_fetch_array($resu);
				$data[0]=$row["user_id"];
				$data[1]=$row["device_id"];
				$data[2]=$row["email"];
				$data[3]=$row["userlevel"];
				$data[4]=$row["last_login"];
				$data[5]=$row["messages"];
				$data[6]=$row["live_login"];
				$data[7]=$row["troubleshoot"];
				$data[8]=$row["changes"];
				$data[9]=$row["device_overview"];
				$data[10]=$row["sensor_overview"];
				$data[11]=$row["lost"];	
				$data[12]=$row["password"];	
				$data[16]=$row["language"];	
				}

return $data;
}



?>