<?php
global $inputs,$dbase,$querybuild;
require_once RIBM_ROOT ."/sites/default/theme/function/chk_device.inc";  // Return user device 
require_once RIBM_ROOT . '/sites/default/theme/function/awaiting_resHome_fn.inc'; // check for awaiting response
	$Eerror = ''; $Perror=''; $Ierror='';
	
	$proftable = DB_name.'.profile';			// Profile Table  
	
	$ProfileId = $inputs->ProfileId;			// Session Profileid 
	$farwardurl= isset($inputs->forward)?$inputs->forward:'';
	$viewid = $inputs->viewid;
	//echo "coming"; exit;
	/*$url =$_SERVER['HTTP_REFERER'];
	if(strpos($url, 'mobile')!==false)
	{
	header ("Location:  https://nikah.com/login");
	}
	else
	{
	}*/
	//echo $inputs->tok_id.$inputs->dev_id; exit;
	/*$tok_id = $inputs->tok_id;
	$dev_id = $inputs->dev_id;
	$_SESSION['tok_id']= $tok_id;
	$_SESSION['dev_id']= $dev_id;*/	
	$checkandroid = chkandroiddevice();
	$ua = sanity($_SERVER['HTTP_USER_AGENT']);
	$TOKEN_ID ='';
	//echo $ua;
	if(stripos($ua,'TOKENID') !== false) { // && stripos($ua,'mobile') !== false) {}
	$va = strstr($ua,"TOKENID");
	$tokexp = explode("TOKENID:",$va);
	$TOKEN_ID = trim($tokexp['1'],' ');
		
	}
	$TOKEN_ID = $inputs->token_id; 
	//echo $TOKEN_ID;
	//$getheader = getallheaders();
	//print_r($getheader);
	//echo $checkandroid."android";
	
 	if(isset($ProfileId) && $inputs->tok_id == '' && !isset($TOKEN_ID)) 
 	{  			// Redirect to welcome page if profileid exists in session
 		$getTICnt = fnGetTotalInitiateCnt($ProfileId);
 		if ($getTICnt > 0) {
 			header("Location:".SITE_PATH."/login?task=awtResHome");
 		} else {
			$profileexist = $querybuild->build_query('pro_profileid',profile,'pro_profileid',$ProfileId,'','','','','','');			
			if($profileexist)
			{
				header("Location: ".SITE_PATH."/users/welcome");
			}
			else
			{
				header("Location:".SITE_PATH."/login?task=logout&forward=50");
			}
 		}
 		//header("Location: users/welcome");  
 	}
 	
 	//echo $inputs->tok_id."coming"; exit;
 	// insert device details in devices table
 	if((($inputs->tok_id!='' && (isset($inputs->tok_id)) && $inputs->tok_id!='null') || $TOKEN_ID!='') && $checkandroid=='0')//$inputs->dev_id
 	{	
		/*// check device id exists 
 		$chkexitdev = $querybuild->build_query("count(sno)","devices","device_id='$inputs->dev_id' and token_id='$inputs->tok_id' limit 1","","","","","","","");	// Check if userid already exists //device_id='$inputs->dev_id' and 
 		//$chkexitdev = $querybuild->build_query("count(sno)","devices","device_id='67c0dbdfe2bfbac8' and token_id='APA91bHV8Nb3myU5I1HMyOOMxNRT5Dn7dCwUs2jgv3-bEqB7dJybuzErVCbVoJmOW22ilq4jds9e8HMaFVFyMs9ApVoWJUAp7rto9MF8HTR8pqyCzyzcvvI' limit 1","","","","","","","");	// Check if userid already exists
 		 //echo "coming".$chkexitdev; exit;
 		 if($chkexitdev=='0') {			  
 		$IP = $_SERVER['REMOTE_ADDR'];		//ip address
 		//GET /login?android=23&dev_id=%20&model=Lenovo%20X3a40&brand=Lenovo&product=x3_row&lang=English&width=1080&height=1920&mo=123&tok_id=dXbGEr16K80:APA91bHQiihOHMMxVc-TYjcg4nZ3XoTVfViPT8AZOaUHYhTrzDyOPfZ4s1d6IR3-hiK6VduL_UsYhnzFVnlqDHV5PeLW9YPVrFZmoaItDjWj_ZEMv9saI6NZzw9LAsQ1ApoE3s1PFRCx HTTP/1.0
 		$screen = ($inputs->width && $inputs->height)?$inputs->width.'-'.$inputs->height:'';  // screen width and height
 		$sql = "insert into devices (device_id,token_id,ip,device,device_model,version,language,screen_size,mob_num,download_date) values ('
$inputs->dev_id','$inputs->tok_id','$IP','$inputs->brand','$inputs->model','$inputs->android','$inputs->lang','$screen','$inputs->mo',curdate())";
 		$dbase->db_sql_query($sql);
 		
 		
 	}
 	else {  */
		
		// atuo login for andriod / ios 
 		// check if profile exists for device_id
 	   if($inputs->tok_id != '')
 	   {
 		$chkprofexis = $querybuild->build_query("pid","devices","token_id='$inputs->tok_id' and logout='0' limit 1","","","","","","","");	// Check if userid already exists //device_id='$inputs->dev_id' and 
 	   }
 	   else 
 	   {
	  // $url =isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
 	   $chkprofexis = $querybuild->build_query("pid","devices","token_id='$TOKEN_ID' and logout='0' limit 1","","","","","","","");
 	   //echo $chkprofexis; exit;
 	   }
 	   
 	   if($TOKEN_ID!='' && isset($chkprofexis))
 	   {
		   session_start(); 
		   $logid = $chkprofexis; 
		   $selectdetvalues = array("dpro_name","dpro_aboutme");
	       $detprofval = $querybuild->build_query($selectdetvalues,det_profile,'dpro_profileid',$logid,'looparray','','','','','');
	       $sesname = $detprofval['dpro_name'];
	       $abtself = $detprofval['dpro_aboutme'];
		  
		   $selectprofvalues = array("pro_gender","pro_proid","pro_adminappstatus");
	            $profloginval = $querybuild->build_query($selectprofvalues,profile,'pro_profileid',$logid,'looparray','','','','','');
	            $gender = $profloginval['pro_gender'];
	            $rmchat = $profloginval['pro_proid'];
	            $appstatus = $profloginval['pro_adminappstatus'];
	            
				/*$sesname = $querybuild->build_query('dpro_name',det_profile,'dpro_profileid',$logid,'','','','','','');	// User Name
 		 		$abtself = $querybuild->build_query('dpro_aboutme',det_profile,'dpro_profileid',$logid,'','','','','','');	// About me*/
 		 		
 		 		/*$gender = $querybuild->build_query('pro_gender',profile,'pro_email',$emailid,'','','','','','');		// Gender $emailid
 		 		$rmchat = $querybuild->build_query('pro_proid',profile,'pro_profileid',$logid,'','','','','','');		// pro_proid for chat userid
 		 		$appstatus = $querybuild->build_query('pro_adminappstatus',profile,'pro_profileid',$logid,'','','','','','');	// Profileid from emailid*/
 		 		
 		 		$chkexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_users','userid',$rmchat,'','','','','','');	// Check if userid already exists
 		 		$chksexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_status','userid',$rmchat,'','','','','','');	// check if profile is in online
 		 		
 		 		
 		 		
 		 		$himher = ($gender=='32')?'her':'him';		// Alerts Gender
 		 		// Redirect to register3 page if about myself is less than 40 char
 		 		if($appstatus=='2')
 		 		{
 		 			$_SESSION['reg_ses_name'] = $sesname;
 		 			$_SESSION['TProfileId'] = $logid;
 		 		
 		 			header("Location: ".SITE_PATH."/register?task=disapp");
 		 		}
 		 		else if(strlen($abtself)<40 || $appstatus=='3')
 		 		{
 		 			$_SESSION['reg_ses_name'] = $sesname;
 		 			$_SESSION['TProfileId'] = $logid;
 		 				
 		 			header("Location: ".SITE_PATH."/register?task=register3&incomp=incomp");
 		 		}
 		 		else
 		 		{
 		 		// if about my self is greater than 40 char
 		 		$logid = $chkprofexis;
 		 		$_SESSION['ProfileId'] = $logid;		// Set profileid in session
 		 		$_SESSION['username'] = $rmchat;		// set chat Username in session
 		 		$IP = $_SERVER['REMOTE_ADDR'];
 		 		
 		 		$selectalues = array("pro_proid","pro_religion","pro_resid","pro_nstate","pro_ncity","pro_nation","pro_gender","pro_adminappstatus","pro_type","pro_latitute","pro_longtitude","pro_marital","pro_age","pro_edu","pro_hide","pro_readquaran","pro_gpoints");
			    $myprofile	 = $querybuild->build_query($selectalues,profile,'pro_profileid',$logid,'looparray','','','','','');
					
					$_SESSION['reg_ses_name'] 	= $sesname;//Dproname
					$_SESSION['my_proid'] 		= $myprofile['pro_proid'];
					$_SESSION['my_religion'] 	= $myprofile['pro_religion'];
					$_SESSION['my_resid'] 		= $myprofile['pro_resid'];
					$_SESSION['my_nstate'] 		= $myprofile['pro_nstate'];
					$_SESSION['my_ncity'] 		= $myprofile['pro_ncity'];
					$_SESSION['my_nation'] 		= $myprofile['pro_nation'];
					$_SESSION['my_gender'] 		= $myprofile['pro_gender'];
					$_SESSION['my_status'] 		= $myprofile['pro_adminappstatus'];
					$_SESSION['my_protype'] 	= $myprofile['pro_type'];
					$_SESSION['my_lat'] 		= $myprofile['pro_latitute'];
					$_SESSION['my_long']	 	= $myprofile['pro_longtitude'];
					$_SESSION['my_marital'] 	= $myprofile['pro_marital'];
					$_SESSION['my_age']	 		= $myprofile['pro_age'];
					$_SESSION['my_edu']	 		= $myprofile['pro_edu'];
					$_SESSION['my_hide']	 	= $myprofile['pro_hide'];
					$_SESSION['my_readquaran']	= $myprofile['pro_readquaran'];
					$_SESSION['my_progpoints']	= $myprofile['pro_gpoints'];
					
		// update logout status - only for android / ios status
 		 		/*if($TOKEN_ID && $checkandroid=='1')
 		 		{
 		 			$sql = "update devices set pid='$logid',logout='0',app_version='$inputs->appver' where token_id='$inputs->tok_id'";
 		 			$dbase->db_sql_query($sql);
 		 		}*/
 		 		// Update lastlogin time and login count, login device details in profile table
 		 		$sql = "update $proftable set pro_lastonline = now(),pro_logcnt=pro_logcnt+1,pro_device='$mydevice' where pro_profileid = '$logid'";
 		 		$rs = $dbase->db_sql_query($sql);
 		 			
 		 		// Insert ip details in ip country table
 		 		$sql = "insert into ip_history values (NULL,'$logid','$IP','',now(),'$mydevice')";
 		 		$rs = $dbase->db_sql_query($sql);
 		 			
 		 		// insert chat users table if not exists
 		 		if($chkexit=='0')
 		 		{
 		 			$sql = "insert into ".CDB_name.".chat_users values (NULL,'$rmchat','$logid','$sesname','','','','available','','','')";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 			
 		 		// Update status if already user is in status table
 		 		if($chksexit)
 		 		{
 		 			$sql = "update ".CDB_name.".chat_status set status='available' where userid='$rmchat'";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 		else
 		 		{
 		 			// Insert user details in status table and set member is in online
 		 			$sql = "insert into ".CDB_name.".chat_status values ('$rmchat','','available','','')";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 			
 		 		// Recent Activity set
 		 		$act_txt = "has logged in ".$himher." profile.";
 		 		$sql = "insert into ".DB_name.".recent_activity (sno,profileid,action,date) values (NULL,'$logid','$act_txt',now())";
 		 		$rs = $dbase->db_sql_query($sql);
 		 		ini_set('session.gc_maxlifetime', 25200);	
 		 		// Redirected to welcome page
 		 		//header("Location: users/welcome");
 		 		$getTICnt = fnGetTotalInitiateCnt($logid);
 		 		if ($getTICnt > 0) {
 		 			header("Location:".SITE_PATH."/login?task=awtResHome");
 		 		}
					
			if($farwardurl == '1')	
			{
			header("Location: ".SITE_PATH."/users/welcome");
			}elseif($farwardurl == '2')	
			{
			header("Location: ".SITE_PATH."/users/inbox");
			}elseif($farwardurl == '3')
			{
			header("Location: ".SITE_PATH."/users/upgradedesk");
			}elseif($farwardurl == '4')
			{
			header("Location: ".SITE_PATH."/users/myprofile");
			}elseif($farwardurl == '5')
			{
			header("Location: ".SITE_PATH."/users/upload_photos");
			}elseif($farwardurl == '6')
			{
			header("Location: ".SITE_PATH."/users/settings");
			}elseif($farwardurl == '7')
			{
			header("Location: ".SITE_PATH."/users/filter");
			}elseif($farwardurl == '9')
			{
			header("Location: ".SITE_PATH."/help?task=contact_us");
			}
			elseif($farwardurl == '10')
			{
			header("Location: ".SITE_PATH."/users/search");
			}elseif($farwardurl == '11')
			{			
			header("Location: ".SITE_PATH."/users/viewprofile?vpid=$viewid");
			}elseif($farwardurl == '12')
			{
			$viewid = $inputs->viewid;
			header("Location: ".SITE_PATH."/users/history?hpid=$viewid");
			}elseif($farwardurl == '13')
			{
			header("Location: ".SITE_PATH."/users/favouriteme");
			}elseif($farwardurl == '14')
			{
			header("Location: ".SITE_PATH."/users/favouriteothers");
			}elseif($farwardurl == '15')
			{
			header("Location: ".SITE_PATH."/users/statistics");
			}elseif($farwardurl == '16')
			{
			header("Location: ".SITE_PATH."/users/viewothers");
			}elseif($farwardurl == '17')
			{
			header("Location: ".SITE_PATH."/users/ignored");
			}
			else
			{
			header("Location: ".SITE_PATH."/users/welcome");
			}
			
			/*
			elseif($farwardurl == '8')
			{
			header("Location: ".SITE_PATH."/users/editfilter");
			}*/
			
	   }
	 // For TOKEN_ID values
		//exit;   
	   }
 		//echo $chkprofexis; exit;
 		//$chkprofexis = $querybuild->build_query("pid","devices","device_id='67c0dbdfe2bfbac8' and token_id='APA91bHV8Nb3myU5I1HMyOOMxNRT5Dn7dCwUs2jgv3-bEqB7dJybuzErVCbVoJmOW22ilq4jds9e8HMaFVFyMs9ApVoWJUAp7rto9MF8HTR8pqyCzyzcvvI' and logout='0' limit 1","","","","","","","");	// Check if userid already exists
 		// echo $chkprofexis; exit;
 		 	// if profile exists
 		 	if($chkprofexis)
 		 	{
				//echo "coming"; exit;
				$logid = $chkprofexis; 	
				$selectdetvalues = array("dpro_name","dpro_aboutme");
	            $detprofval = $querybuild->build_query($selectdetvalues,det_profile,'dpro_profileid',$logid,'looparray','','','','','');
	            $sesname = $detprofval['dpro_name'];
	            $abtself = $detprofval['dpro_aboutme'];
	            
	            $selectprofvalues = array("pro_gender","pro_proid","pro_adminappstatus");
	            $profloginval = $querybuild->build_query($selectprofvalues,profile,'pro_profileid',$logid,'looparray','','','','','');
	            $gender = $profloginval['pro_gender'];
	            $rmchat = $profloginval['pro_proid'];
	            $appstatus = $profloginval['pro_adminappstatus'];
	            
				/*$sesname = $querybuild->build_query('dpro_name',det_profile,'dpro_profileid',$logid,'','','','','','');	// User Name
 		 		$abtself = $querybuild->build_query('dpro_aboutme',det_profile,'dpro_profileid',$logid,'','','','','','');	// About me*/
 		 		
 		 		/*$gender = $querybuild->build_query('pro_gender',profile,'pro_email',$emailid,'','','','','','');		// Gender $emailid
 		 		$rmchat = $querybuild->build_query('pro_proid',profile,'pro_profileid',$logid,'','','','','','');		// pro_proid for chat userid
 		 		$appstatus = $querybuild->build_query('pro_adminappstatus',profile,'pro_profileid',$logid,'','','','','','');	// Profileid from emailid*/
 		 		
 		 		$chkexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_users','userid',$rmchat,'','','','','','');	// Check if userid already exists
 		 		$chksexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_status','userid',$rmchat,'','','','','','');	// check if profile is in online
 		 		
 		 		
 		 		
 		 		$himher = ($gender=='32')?'her':'him';		// Alerts Gender
 		 		// Redirect to register3 page if about myself is less than 40 char
 		 		if($appstatus=='2')
 		 		{
 		 			$_SESSION['reg_ses_name'] = $sesname;
 		 			$_SESSION['TProfileId'] = $logid;
 		 		
 		 			header("Location: ".SITE_PATH."/register?task=disapp");
 		 		}
 		 		else if(strlen($abtself)<40 || $appstatus=='3')
 		 		{
 		 			$_SESSION['reg_ses_name'] = $sesname;
 		 			$_SESSION['TProfileId'] = $logid;
 		 				
 		 			header("Location: ".SITE_PATH."/register?task=register3&incomp=incomp");
 		 		}
 		 		else
 		 		{
 		 		// if about my self is greater than 40 char
 		 		$logid = $chkprofexis;
 		 		$_SESSION['ProfileId'] = $logid;		// Set profileid in session
 		 		$_SESSION['username'] = $rmchat;		// set chat Username in session
 		 		$IP = $_SERVER['REMOTE_ADDR'];
 		 		
 		 		$selectalues = array("pro_proid","pro_religion","pro_resid","pro_nstate","pro_ncity","pro_nation","pro_gender","pro_adminappstatus","pro_type","pro_latitute","pro_longtitude","pro_marital","pro_age","pro_edu","pro_hide","pro_readquaran","pro_gpoints");
			    $myprofile	 = $querybuild->build_query($selectalues,profile,'pro_profileid',$logid,'looparray','','','','','');
					
					$_SESSION['reg_ses_name'] 	= $sesname;//Dproname
					$_SESSION['my_proid'] 		= $myprofile['pro_proid'];
					$_SESSION['my_religion'] 	= $myprofile['pro_religion'];
					$_SESSION['my_resid'] 		= $myprofile['pro_resid'];
					$_SESSION['my_nstate'] 		= $myprofile['pro_nstate'];
					$_SESSION['my_ncity'] 		= $myprofile['pro_ncity'];
					$_SESSION['my_nation'] 		= $myprofile['pro_nation'];
					$_SESSION['my_gender'] 		= $myprofile['pro_gender'];
					$_SESSION['my_status'] 		= $myprofile['pro_adminappstatus'];
					$_SESSION['my_protype'] 	= $myprofile['pro_type'];
					$_SESSION['my_lat'] 		= $myprofile['pro_latitute'];
					$_SESSION['my_long']	 	= $myprofile['pro_longtitude'];
					$_SESSION['my_marital'] 	= $myprofile['pro_marital'];
					$_SESSION['my_age']	 		= $myprofile['pro_age'];
					$_SESSION['my_edu']	 		= $myprofile['pro_edu'];
					$_SESSION['my_hide']	 	= $myprofile['pro_hide'];
					$_SESSION['my_readquaran']	= $myprofile['pro_readquaran'];
					$_SESSION['my_progpoints']	= $myprofile['pro_gpoints'];
 		 			
 		 		// update logout status - only for android / ios status
 		 		if($inputs->tok_id && $checkandroid=='1')
 		 		{
 		 			$sql = "update devices set pid='$logid',logout='0',app_version='$inputs->appver' where token_id='$inputs->tok_id'";
 		 			$dbase->db_sql_query($sql);
 		 		}
 		 		// Update lastlogin time and login count, login device details in profile table
 		 		$sql = "update $proftable set pro_lastonline = now(),pro_logcnt=pro_logcnt+1,pro_device='$mydevice' where pro_profileid = '$logid'";
 		 		$rs = $dbase->db_sql_query($sql);
 		 			
 		 		// Insert ip details in ip country table
 		 		$sql = "insert into ip_history values (NULL,'$logid','$IP','',now(),'$mydevice')";
 		 		$rs = $dbase->db_sql_query($sql);
 		 			
 		 		// insert chat users table if not exists
 		 		if($chkexit=='0')
 		 		{
 		 			$sql = "insert into ".CDB_name.".chat_users values (NULL,'$rmchat','$logid','$sesname','','','','available','','','')";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 			
 		 		// Update status if already user is in status table
 		 		if($chksexit)
 		 		{
 		 			$sql = "update ".CDB_name.".chat_status set status='available' where userid='$rmchat'";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 		else
 		 		{
 		 			// Insert user details in status table and set member is in online
 		 			$sql = "insert into ".CDB_name.".chat_status values ('$rmchat','','available','','')";
 		 			$rs = $dbase->db_sql_query($sql);
 		 		}
 		 			
 		 		// Recent Activity set
 		 		$act_txt = "has logged in ".$himher." profile.";
 		 		$sql = "insert into ".DB_name.".recent_activity (sno,profileid,action,date) values (NULL,'$logid','$act_txt',now())";
 		 		$rs = $dbase->db_sql_query($sql);
 		 		ini_set('session.gc_maxlifetime', 25200);	
 		 		// Redirected to welcome page
 		 		//header("Location: users/welcome");
 		 		$getTICnt = fnGetTotalInitiateCnt($logid);
 		 		if ($getTICnt > 0) {
 		 			header("Location:".SITE_PATH."/login?task=awtResHome");
 		 		} else {
 		 			header("Location: ".SITE_PATH."/users/welcome");
 		 		}
 		 	}  }
 	//}
 	}
	
	// Actions once login button pressed 
	if($inputs->ulogin=='1')
	{
		//header("Location: server_maintanance");
		$emailid = $inputs->log_email;		// Email ID
		$logpassword = $inputs->log_pwd;		// Password 
		/*if($emailid=='v.m.azhar@gmail.com' && $logpassword=='Bluebay079')
		{*/
		if($emailid=='')
		$Eerror = trans('l_enteremailadd');		// error if email id field is empty
		
		if($logpassword=='')
		$Perror = trans('l_enterpwd');			// Error if password field is empty
		
		$chkusers = checkuserlogin($emailid,$logpassword);		//check login user details 
		$chkusersmul =	 checkuserloginss($emailid);
		$chkuser =  $chkusers[0];
		//echo $chkuser; exit;
		if(!$chkuser) 	{
		$Ierror = trans('l_invalidema');  }   // Error if email and password not match 
		
		// If no error and email/password matches in DB
		if($chkuser && !$Eerror && !$Perror && !$Ierror)
		{	
			// Keep me signined in - Store values in cookie 
			if($inputs->keepsign)
			{
				setcookie( "Uname", $emailid, strtotime( '+1 year' ) );	// Email 
				setcookie( "Upass", $logpassword, strtotime( '+1 year' ) );		// Password 
			}
			 
			// if Single Profile created for logged email id 
			if($chkuser)
			{  
				if($chkusersmul=='1') {
			
				//echo "aaaa";  exit;
			//	$logid = $querybuild->build_query("pro_profileid",profile,"pro_email='$emailid' limit 1","","","","","","","");	// Profileid from emailid
				$logindet = $querybuild->build_query("pro_profileid,pro_gender,pro_proid,pro_adminappstatus",profile,"pro_email='$emailid' limit 1","","looparray","","","","","");	// Profileid from emailid
				
				$logid = $logindet['pro_profileid'];
				$gender = $logindet['pro_gender'];
				$rmchat	= $logindet['pro_proid'];
				$appstatus = $logindet['pro_adminappstatus'];
				
				$profdet = $querybuild->build_query("dpro_name,dpro_aboutme",det_profile,"dpro_profileid",$logid,"looparray","","","","","");	// Profileid from emailid
				
				$sesname = $profdet['dpro_name'];
				$abtself = $profdet['dpro_aboutme'];
				
				$chkexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_users','userid',$rmchat,'','','','','','');	// Check if userid already exists
 		 		$chksexit = $querybuild->build_query('count(userid)',CDB_name.'.chat_status','userid',$rmchat,'','','','','','');	// check if profile is in online
 		 		
 		 		//single bulid query for multiple				
			/*		
				$sesname = $querybuild->build_query('dpro_name',det_profile,'dpro_profileid',$logid,'','','','','','');	// User Name
				$abtself = $querybuild->build_query('dpro_aboutme',det_profile,'dpro_profileid',$logid,'','','','','','');	// About me 
				$gender = $querybuild->build_query('pro_gender',profile,'pro_email',$emailid,'','','','','','');		// Gender
				$rmchat = $querybuild->build_query('pro_proid',profile,'pro_profileid',$logid,'','','','','','');		// pro_proid for chat userid 
				$appstatus = $querybuild->build_query('pro_adminappstatus',profile,'pro_profileid',$logid,'','','','','','');	// Profileid from emailid
			*/
				//single bulid query for multiple
				
				$himher = ($gender=='32')?'her':'him';		// Alerts Gender 
					// Redirect to register3 page if about myself is less than 40 char 
				if($appstatus=='5')
				{
					$_SESSION['reg_ses_name'] = $sesname;
					$_SESSION['TProfileId'] = $logid;
						
					header("Location: ".SITE_PATH."/register?task=disapp");
				}
				else if($appstatus=='2')
				{
					$_SESSION['reg_ses_name'] = $sesname;
					$_SESSION['TProfileId'] = $logid;
						
					header("Location: ".SITE_PATH."/register?task=disapp");
				}
			/*	else if(strlen($abtself)<40 || $appstatus=='3')
				{
					$_SESSION['reg_ses_name'] = $sesname;
					$_SESSION['TProfileId'] = $logid;
					
					header("Location: register?task=register3&incomp=incomp");  
				}*/
				else if($appstatus=='3')
				{
					if(strlen($abtself)<40){
						$_SESSION['reg_ses_name'] = $sesname;
						$_SESSION['TProfileId'] = $logid;						
						header("Location: ".SITE_PATH."/register?task=register3&incomp=incomp");
					} else{
						$_SESSION['reg_ses_name'] = $sesname;
						$_SESSION['TProfileId'] = $logid;
						$_SESSION['ProfileId'] = $logid;
						$_sessionpid = $logid;			
						$_SESSION['page3'] = "register3";//die;
					//$_SESSION['page4'] = 'yes';
					//header("Location: register?task=register4");
					//header("Location: ?task=register4&ph_error=yes");
					header("Location: ".SITE_PATH."/register?task=register4");
					}
				}
				else 
				{ 
					//echo $inputs->dev_id."coming"; exit;
					 // if about my self is greater than 40 char
					$_SESSION['ProfileId'] = $logid;		// Set profileid in session 
					$_SESSION['username'] = $rmchat;		// set chat Username in session 
					$IP = $_SERVER['REMOTE_ADDR'];
					
					//$myname = $querybuild->build_query('dpro_name',det_profile,"dpro_profileid='$logid'",'','','','','','','');
					$myname = $sesname;
					//$mytype = $querybuild->build_query('pro_type',profile,'pro_profileid',$logid,'','','','','',''); 
					$selectalues = array("pro_proid","pro_religion","pro_resid","pro_nstate","pro_ncity","pro_nation","pro_gender","pro_adminappstatus","pro_type","pro_latitute","pro_longtitude","pro_marital","pro_age","pro_edu","pro_hide","pro_readquaran","pro_gpoints");
					$myprofile	 = $querybuild->build_query($selectalues,profile,'pro_profileid',$logid,'looparray','','','','','');
					
					$_SESSION['reg_ses_name'] 	= $myname;//Dproname
					$_SESSION['my_proid'] 		= $myprofile['pro_proid'];
					$_SESSION['my_religion'] 	= $myprofile['pro_religion'];
					$_SESSION['my_resid'] 		= $myprofile['pro_resid'];
					$_SESSION['my_nstate'] 		= $myprofile['pro_nstate'];
					$_SESSION['my_ncity'] 		= $myprofile['pro_ncity'];
					$_SESSION['my_nation'] 		= $myprofile['pro_nation'];
					$_SESSION['my_gender'] 		= $myprofile['pro_gender'];
					$_SESSION['my_status'] 		= $myprofile['pro_adminappstatus'];
					$_SESSION['my_protype'] 	= $myprofile['pro_type'];
					$_SESSION['my_lat'] 		= $myprofile['pro_latitute'];
					$_SESSION['my_long']	 	= $myprofile['pro_longtitude'];
					$_SESSION['my_marital'] 	= $myprofile['pro_marital'];
					$_SESSION['my_age']	 		= $myprofile['pro_age'];
					$_SESSION['my_edu']	 		= $myprofile['pro_edu'];
					$_SESSION['my_hide']	 	= $myprofile['pro_hide'];
					$_SESSION['my_readquaran']	= $myprofile['pro_readquaran'];
					$_SESSION['my_progpoints']	= $myprofile['pro_gpoints'];
					
					$gender = $myprofile['pro_gender'];
					$uphoto = new Photographs;
					$photo = $uphoto->getMyphotos($logid,$gender,'sr');
					$_SESSION['my_srphoto']	= $photo;
					
					//echo $inputs->tok_id; exit;
					// update logout status - only for android / ios status
					if($inputs->tok_id!='' && $inputs->tok_id!='null' && $checkandroid=='1')
					{
						ini_set('session.gc_maxlifetime', 25200);
						//echo $inputs->tok_id; exit;
						$chkprofexis1 = $querybuild->build_query("pid","devices","token_id='$inputs->tok_id' limit 1","","","","","","",""); //device_id='$inputs->dev_id' and 
						//echo $chkprofexis1; exit;
						if($chkprofexis1)
						{
							$sql = "update devices set pid='$logid',logout='0',app_version='$inputs->appver' where token_id='$inputs->tok_id'"; // and device_id='$inputs->dev_id'
							$dbase->db_sql_query($sql);
						}
						else
						{
							$IP = $_SERVER['REMOTE_ADDR'];		//ip address
							//GET /login?android=23&dev_id=%20&model=Lenovo%20X3a40&brand=Lenovo&product=x3_row&lang=English&width=1080&height=1920&mo=123&tok_id=dXbGEr16K80:APA91bHQiihOHMMxVc-TYjcg4nZ3XoTVfViPT8AZOaUHYhTrzDyOPfZ4s1d6IR3-hiK6VduL_UsYhnzFVnlqDHV5PeLW9YPVrFZmoaItDjWj_ZEMv9saI6NZzw9LAsQ1ApoE3s1PFRCx HTTP/1.0
							$screen = ($inputs->width && $inputs->height)?$inputs->width.'-'.$inputs->height:'';  // screen width and height
							$sql = "insert into devices (pid,device_id,token_id,ip,device,device_model,version,language,screen_size,mob_num,download_date,logout,app_version) values ('$logid','
					$inputs->dev_id','$inputs->tok_id','$IP','$inputs->brand','$inputs->model','$inputs->android','$inputs->lang','$screen','$inputs->mo',curdate(),'0','$inputs->appver')"; 
							$dbase->db_sql_query($sql);
					   }
					}
					/*if($inputs->tok_id)
					{
						$sql = "update devices set pid='$logid',logout='0' where token_id='$inputs->tok_id'";
						$dbase->db_sql_query($sql);
					}*/
					/*if($inputs->sdev_id)
					{
						$sql = "update devices set pid='$logid',logout='0' where device_id='$inputs->sdev_id'";
						$dbase->db_sql_query($sql);
					}*/
					$previosonline = $querybuild->build_query('pro_lastonline',profile,'pro_profileid',$logid,'','','','','','');	
					$_SESSION['log_lastonline'] 	= $previosonline;
					// Update lastlogin time and login count, login device details in profile table
					$sql = "update $proftable set pro_lastonline = now(),pro_logcnt=pro_logcnt+1,pro_device='$mydevice' where pro_profileid = '$logid'";
					$rs = $dbase->db_sql_query($sql);
					
					// Insert ip details in ip country table
					$sql = "insert into ip_history values (NULL,'$logid','$IP','',now(),'$mydevice')";
					$rs = $dbase->db_sql_query($sql);
					
					// insert chat users table if not exists
					if($chkexit=='0') 
					{ 
						$sql = "insert into ".CDB_name.".chat_users values (NULL,'$rmchat','$logid','$sesname','','','','available','','','')";
						$rs = $dbase->db_sql_query($sql);
					}
					
					// Update status if already user is in status table
					if($chksexit)
					{
						$sql = "update ".CDB_name.".chat_status set status='available' where userid='$rmchat'";
						$rs = $dbase->db_sql_query($sql);
					}
					else 
					{	
						// Insert user details in status table and set member is in online 
						$sql = "insert into ".CDB_name.".chat_status values ('$rmchat','','available','','')";
						$rs = $dbase->db_sql_query($sql);
					}
					
					// Recent Activity set 
					$act_txt = "has logged in ".$himher." profile.";
					$sql = "insert into ".DB_name.".recent_activity (sno,profileid,action,date) values (NULL,'$logid','$act_txt',now())";
					$rs = $dbase->db_sql_query($sql);
					
					// Redirected to welcome page
					//header("Location: users/welcome");
					$getTICnt = fnGetTotalInitiateCnt($logid);
					if ($getTICnt > 0) {
						header("Location:".SITE_PATH."/login?task=awtResHome");
					} else {
						header("Location: ".SITE_PATH."/users/welcome");
					}
				}
			} 
			else 
			{ 
				//echo "hhhhh "; exit;
						// If multiple profiles created for logged email ID
				$_SESSION['multiemaillog'] = $emailid;			// set EMailid in session
					// Redirected to multiple email section
				header("Location: ".SITE_PATH."/login?task=multiprofile");  
			}  }
		}
	 /* }
	  else
	  {
		  header("Location: server_maintanance");
	  }*/
	}
?>
