<?php
//phpinfo();die;
session_start();
ob_start();
require_once("./connection/setting_jaldhi_class.php");
$info = new clsSettings();
$connect = $info->funConnect();

$myusername = trim($_REQUEST['txtUser']);
$mypassword = trim($_REQUEST['txtPass']);

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$currentdate =  date("Y-m-d H:i:s", time());
$lastsession_refresh = date("Y-m-d H:i:s", strtotime("-30 minute", time()));
//and (LOGIN_STATUS='OFFLINE' or SESSION_DATETIME < '".$lastsession_refresh."')
$sql = "SELECT * FROM login WHERE USERNAME='$myusername' and STATUS ='1' ";

$result = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1) {
	$rows = mysql_fetch_assoc($result);
	if ($rows['PASSWORD'] == $mypassword) {
		$done = false;
		$token = uniqid();
		$uid  = $_SESSION['uid'] = $rows['LOGINID'];
		$user_name = $_SESSION['username']  = $rows['USERNAME'];
		$user_display = $_SESSION['display'] = $rows['CONTACT_PERSON'];
		$_SESSION['gender'] = $rows['GENDER'];
		$_SESSION['usertoken'] = $token;
		$user_type = $rows['USER_TYPE'];
		$_SESSION['showNewYearPop'] = 1;

		$_SESSION['rMuser']  = $rows['RM_USER'];
		$_SESSION['sopfuser']  = $rows['SOPF_USER'];

		$_SESSION['disPL']  = $rows['DIS_PL'];
		$_SESSION['DIS_VEN_L']  = $rows['DIS_VEN_L'];
		$_SESSION['COM_SH']  = $rows['COM_SH'];
		$_SESSION['DETA_REG']  = $rows['DETA_REG'];

		$_SESSION['PAY_ACCESS']  = $rows['PAY_ACCESS'];

		$_SESSION['company']  = $rows['MCOMPANYID'];

		//....................for access log.................................
		$sql2 = "insert into access_log_master (LOGINID, IN_DATE, USER_TYPE) values ('" . $rows['LOGINID'] . "',NOW(),'" . $rows['USER_TYPE'] . "')";
		mysql_query($sql2) or die(mysql_error());

		$sql4 = "update login set LOGIN_STATUS='ONLINE', TOKEN='" . $token . "', SESSION_DATETIME='" . $currentdate . "' where LOGINID='" . $rows['LOGINID'] . "'";
		mysql_query($sql4) or die(mysql_error());

		$sql3 = "select max(LOG_ID) as max from access_log_master";

		$res3 = mysql_query($sql3);

		$rows3 = mysql_fetch_assoc($res3);

		$_SESSION['log_id']  = $rows3['max'];

		//....................ends here......................................

		if ($user_type == 'superadmin') {
			$_SESSION['satype'] = $user_type;
			header("Location:./sadmin/");
		}
		if ($user_type == 'admin') {
			$_SESSION['atype'] = $user_type;
			header("Location:./admin/");
		}
		if ($user_type == 'internal_user') {
			$_SESSION['iutype'] = $user_type;
			header("Location:./internal-user/index.php");
		}
		if ($user_type == 'mgmt_user') {
			$_SESSION['iutype'] = $user_type;
			header("Location:./internal-user/index.php");
		}

		if ($user_type == 'rm_user') {
			$_SESSION['iutype'] = $user_type;
			header("Location:./internal-user/index.php");
		}
	} else {
		$_SESSION['adminmsg'] = 2;
		$_SESSION['adminun'] = $_REQUEST['txtUser'];
		header("Location:./index.php");
	}
} else {
	$_SESSION['adminmsg'] = 1;
	header("Location:./index.php");
}
ob_end_flush();
