<?php
session_start();
include("include/db.conn.php");
include("include/conf.class.php");
include("include/admin.class.php");
if(isset($_POST['loginform'])){
	$buy_product=md5("buy_product");
	$main_login=md5("main_login");
	$new_user=md5("new_user");
	
	if($_POST['loginform']==$buy_product){$action='2';}else if($_POST['loginform']==$main_login){$action='1';}else if($_POST['loginform']==$new_user){$action='3';}

switch($action){
	case 1: if(isset($_POST['password']) && isset($_POST['username']) && isset($_POST['company_id']))
			{
				//check post value is null
				if($_POST['password']=="" || $_POST['username']=="" || $_POST['company_id']=="")
				{
						$_SESSION['msglog']="field_empty";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
				
				//check username and password
				
				$username = strtoupper($cartCore->ClearInput($_POST['username']));
				$company_id = $cartCore->ClearInput($_POST['company_id']);
				$encrypt_submitted_password = md5($_POST['password']);
				$result   = $cartCore->get_system_user_details_by_username($username);
				if(mysql_num_rows($result))
				{
					$row = mysql_fetch_assoc($result);
					$encrypt_orginal_password = $row['PASSWORD'];
					
					
			$fetch_company_result   = $cartCore->fetch_system_user_company_details_by_userid($row['USER_ID']); //get company_details
					
					if($encrypt_submitted_password!=$encrypt_orginal_password)
					{
						$_SESSION['msglog']="un_pw_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else if($row['STATUS']!=true)
					{
						$_SESSION['msglog']="denied_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else if($fetch_company_result['COMPANY_ID']!=$company_id)
					{
						$_SESSION['msglog']="company_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else
					{
						$token_login=base64_encode(time());
						$_SESSION['token']=$token_login; 
						
						$check_user_login=mysql_query("select * from cart_user_logins where user_id='".$row['USER_ID']."'");
							if(mysql_num_rows($check_user_login))
							{ 
								mysql_query("update cart_user_logins set date_time=CURRENT_TIMESTAMP,ip_address='".$_SERVER['REMOTE_ADDR']."' where user_id=".$row['USER_ID']);
							}
							else
							{
								mysql_query("insert into `cart_user_logins` (`user_id`, `ip_address`, `date_time`) values ('".$row['USER_ID']."', '".$_SERVER['REMOTE_ADDR']."', CURRENT_TIMESTAMP)");
							}
							
							mysql_query("update cart_user set login_token='".$token_login."' where user_id=".$row['USER_ID']);
							
								$_SESSION['tkusernameCART'] = $row['USERNAME'];
								$_SESSION['tklogin_tokenCART']      = $row['LOGIN_TOKEN']; 
								$_SESSION['tkcompanyCART']      = $fetch_company_result['COMPANY_ID'];
								header("location:index.php?token_id=".$token_login);
					exit;	
					}	
				}
				else
				{
						$_SESSION['msglog']="unkown_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
			}
	break;
	
	case 2:
	
				if(isset($_POST['password']) && isset($_POST['username']))
				{
					$back_link=$_SESSION['ONE_BACK_ID'];
				//check post value is null
				if($_POST['password']=="" || $_POST['username']=="")
				{
						$_SESSION['msglog']="field_empty";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
				if (!filter_var($_POST['username'], FILTER_VALIDATE_EMAIL)) {
  						$_SESSION['msglog']="email_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
				
				$username = strtoupper($_POST['username']);
				$encrypt_submitted_password = $cartCore->md5_pure_encode($_POST['password']);
				$system_user="0";
				$result   = $cartCore->get_system_user_details_by_username_filter_system_user($username,$system_user);
				
				if(mysql_num_rows($result))
				{
					
					$row = mysql_fetch_assoc($result);
					$encrypt_orginal_password = $row['PASSWORD'];
					
					if($encrypt_submitted_password!=$encrypt_orginal_password)
					{
						$_SESSION['msglog']="un_pw_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else if($row['STATUS']!=true)
					{
						$_SESSION['msglog']="denied_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else
					{
						$token_login=base64_encode(time());
						$_SESSION['token']=$token_login; 
						
						$check_user_login=mysql_query("select * from cart_user_logins where user_id='".$row['USER_ID']."'");
							if(mysql_num_rows($check_user_login))
							{ 
								mysql_query("update cart_user_logins set date_time=CURRENT_TIMESTAMP,ip_address='".$_SERVER['REMOTE_ADDR']."' where user_id=".$row['USER_ID']);
							}
							else
							{
								mysql_query("insert into `cart_user_logins` (`user_id`, `ip_address`, `date_time`) values ('".$row['USER_ID']."', '".$_SERVER['REMOTE_ADDR']."', CURRENT_TIMESTAMP)");
							}
							
							mysql_query("update cart_user set login_token='".$token_login."' where user_id=".$row['USER_ID']);
							
								$_SESSION['tkusernameCART'] = $row['USERNAME'];
								$_SESSION['tklogin_tokenCART']      = $row['LOGIN_TOKEN']; 
								$_SESSION['tkcompanyCART']      = md5("customer");
								
								if(isset($_SESSION['CHECKOUT']))
								{
								header("location:form_wizards.php?token_id=".$token_login."&bk=".$back_link);
								}
								else
								{
									header("location:customer_order.php?token_id=$token_login&bk=$back_link");
								}
					exit;	
					}	
					
					
					echo 'hi';
					
					
				}
				else
				{
					
						$_SESSION['msglog']="unkown_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
				
				
				
				}
				
				break;
				
	case 3:
	
				$status=$cartAdminMain->create_new_cutomer_account();
				
		if($status=='ok')
		{
	
				if(isset($_POST['password']) && isset($_POST['username']))
				{
					$back_link=$_SESSION['ONE_BACK_ID'];
				//check post value is null
				if($_POST['password']=="" || $_POST['username']=="")
				{
						$_SESSION['msglog']="field_empty";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
				if (!filter_var($_POST['username'], FILTER_VALIDATE_EMAIL)) {
  						$_SESSION['msglog']="email_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
				
				$username = strtoupper($_POST['username']);
				$encrypt_submitted_password = $cartCore->md5_pure_encode($_POST['password']);
				$system_user="0";
				$result   = $cartCore->get_system_user_details_by_username_filter_system_user($username,$system_user);
				
				if(mysql_num_rows($result))
				{
					
					$row = mysql_fetch_assoc($result);
					$encrypt_orginal_password = $row['PASSWORD'];
					
					if($encrypt_submitted_password!=$encrypt_orginal_password)
					{
						$_SESSION['msglog']="un_pw_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else if($row['STATUS']!=true)
					{
						$_SESSION['msglog']="denied_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
					}
					
					else
					{
						$token_login=base64_encode(time());
						$_SESSION['token']=$token_login; 
						
						
						$check_user_login=mysql_query("select * from cart_user_logins where user_id='".$row['USER_ID']."'");
							if(mysql_num_rows($check_user_login))
							{ 
								mysql_query("update cart_user_logins set date_time=CURRENT_TIMESTAMP,ip_address='".$_SERVER['REMOTE_ADDR']."' where user_id=".$row['USER_ID']);
							}
							else
							{
								mysql_query("insert into `cart_user_logins` (`user_id`, `ip_address`, `date_time`) values ('".$row['USER_ID']."', '".$_SERVER['REMOTE_ADDR']."', CURRENT_TIMESTAMP)");
							}
							
							mysql_query("update cart_user set login_token='".$token_login."' where user_id=".$row['USER_ID']);
							
								$_SESSION['tkusernameCART'] = $row['USERNAME'];
								$_SESSION['tklogin_tokenCART']      = $row['LOGIN_TOKEN']; 
								$_SESSION['tkcompanyCART']      = md5("customer");
								if(isset($_SESSION['CHECKOUT']))
								{
								header("location:form_wizards.php?token_id=$token_login&bk=$back_link");
								}
								else
								{
									header("location:customer_order.php?token_id=$token_login&bk=$back_link");
								}
					exit;	
					}		
					
				}
				else
				{
					
						$_SESSION['msglog']="unkown_error";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
				}
				
				
				
				}
		}
		else
		{
						$_SESSION['msglog']="sameemailerr";
						$_SESSION['msgloglogin']=true; 
						header("location:login.php");
						exit;
		}

	case 4: if(isset($_POST['emailid'])){
				$emailid = mysql_real_escape_string($_POST['emailid']);
				$result  = mysql_query("select * from sms_user_elements where email='".$emailid."'");
				if(mysql_num_rows($result)){
					include("include/mail/mail.class.php");
					$smsMail       = new smsmail();
					$temp_password = substr(uniqid(), -6, 6);
					$row           = mysql_fetch_assoc($result);
					
					mysql_query("update sms_user set password='".md5($temp_password)."' where username=".$row['USERNAME']);
					$subject =  "SMS Admin : Your password has been reset";
                    $body    =  "Hi, '".$row['USERNAME']."'<br><br>" .
								"Your new login information is: <br><br>" .
								"Username: " . $row['USERNAME'] . "<br>" .
								"Password: " . $temp_password . "<br><br>" ."Thanking You.";
					$smsMail->sendEMail($emailid, $subject, $body);			
					$_SESSION['msglog'] = "RESET";
					header("location:index.php");
					exit;
				}else{
					$_SESSION['msglog'] = "Email id does not exists.";
					header("location:index.php");
					exit;
				}
			}
	break;
	
	default : 
					header("location:login.php");
					break;
		}
}
?>