<?php include("header.php");
$Email="";$Password="";


include_once("config.php");
include_once("includes/functions.php");
include_once("google_config.php");

//echo'<pre>';print_r();exit;

//state
if(!$fbuser){
	$fbuser = null;
	$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
	$output = '<a href="'.$loginUrl.'"><img src="images/fb_login.png"></a>'; 	
}
else
{
	if(isset($_REQUEST['state']))
	{
		if(!$fbuser){
		$fbuser = null;
		$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
		$output = '<a href="'.$loginUrl.'"><img src="images/fb_login.png"></a>'; 	
	}else{
		$user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
		$user = new Users();
		$user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['locale'],$user_profile['picture']['data']['url']);
		 }
	}
	
		
	
}
$userProfile=array();
	
	if(isset($_REQUEST['code']))
	{
		
		$gClient->authenticate();
		//$_SESSION['token'] = $gClient->getAccessToken();
		$userProfile = $google_oauthV2->userinfo->get();
		
		//echo'<pre>';print_r($userProfile);
		
		if(!empty($userProfile))
		{
		$name=$userProfile['name'];
		$email=$userProfile['email'];
		$gender=$userProfile['gender'];
		
		$sqlcheck=mysql_query("select * from tbl_users where email='$email' and fullname='$name'");
		if(mysql_num_rows($sqlcheck)>0)
		{
			$rec=mysql_fetch_assoc($sqlcheck);
			
			$update=mysql_query("update tbl_users set fullname='$name' where email='$email' and fullname='$name'");
			$lst=$rec['userid'];
		}
		else
		{
			$insert=mysql_query("insert into tbl_users (fullname,gender,email,date)values('$name','$gender','$email',now())");
			$lst=mysql_insert_id();
		}
		
		$_SESSION['cid']=$lst;
		$_SESSION['cname']=$name;
		$_SESSION['cemail']=$email;
		
		?>
		<script>
		window.location="account_home";
		</script>
		<?php
		}
		
	}

$authUrl = $gClient->createAuthUrl();

if(isset($_POST['login']))
{
	
	
	$email=$_POST['email'];
	$password=md5($_POST['password']);
	
	$sqlcheck=mysql_query("select * from tbl_users where email='$email' and password='$password'");
	if(mysql_num_rows($sqlcheck)>0)
	{
		if(isset($_POST['remember']))
		{
			setcookie('Email',$email);
			setcookie('Password',$_POST['password']);
		}
		else
		{
			setcookie("Email", $email, time()-1);
    		setcookie("Password", $_POST['password'], time()-1);
		}
		$resu=mysql_fetch_assoc($sqlcheck);
		$_SESSION['cid']=$resu['userid'];
		$_SESSION['cname']=$resu['fullname'];
		$_SESSION['cemail']=$resu['email'];
		
		?>
		<script>
        window.location="account_home";
        </script>
		<?php
		
	}
	else
	{
		
		 
		setcookie("Email", $email, time()-1);
    	setcookie("Password", $_POST['password'], time()-1);
		
		?>
		<script>
alert('This email does not match to our record');
        window.location.href=window.location.href;
        </script>
		<?php
	}
	
	
}

if (isset($_COOKIE['Email'])) {
    $Email = $_COOKIE['Email'];
  }

  if (isset($_COOKIE['Password'])) {
    $Password = $_COOKIE['Password'];
  }
  
 

?>
 
<div class="main-container">
<div class="container">
<div class="row">
<div class="col-sm-5 login-box">
<div class="panel panel-default">
<div class="panel-intro text-center">
 
<span class="logo-icon"><div align="center"><img src="images/user.png" style="width=50px; height=50px"></div>
</div>
<form role="form" method="post">
<div class="panel-body">

<div class="form-group">
<label for="sender-email" class="control-label">Email:</label>
<div class="input-icon"> <i class="icon-user fa"></i>
<input id="sender-email" value="<?php echo $Email;?>" name="email" type="email" placeholder="Email" class="form-control email" required>
</div>
</div>
<div class="form-group">
<label for="user-pass" class="control-label">Password:</label>
<div class="input-icon"> <i class="icon-lock fa"></i>
<input type="password" name="password" value="<?php echo $Password;?>" class="form-control" placeholder="Password" id="user-pass" required>
</div>
</div>
<div class="form-group">
<input type="submit" name="login"  class="btn btn-primary  btn-block" value="Login">
</div>

</div>
<div class="panel-footer">
<div class="checkbox pull-left">
<label> <input type="checkbox" <?php if(isset($_COOKIE['Email'])) { echo "checked"; }?> value="1" name="remember" id="remember"> Keep me logged in</label>
</div>
<p class="text-center pull-right"> <a href="forgot_password"> Lost your password? </a> </p>
<div style=" clear:both"></div>

 <?php
	if(!$fbuser){
	$fbuser = null;
	$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
	$output ='<a href="'.$loginUrl.'" class="btn btn-block btn-social btn-facebook" style="background-color:#3b5998; color:#FFF; border-color:#3b5998; border-radius: 4px;">
<span class="fa fa-facebook"></span> Sign in with Facebook
</a>';
echo $output;
	} ?>  
	
	<br>
	 <?php if(isset($authUrl)) {?>
<a href="<?php echo $authUrl;?>" class="btn btn-block btn-social" style="background-color:#ca1515; color:#FFF; border-color:#ca1515; border-radius: 4px;"><i class="fa  fa-google-plus"></i> <span>Join using your Google Account</span>
</a>
<?php } ?>
</div>


</form>
</div>
<div class="login-box-btm text-center">
<p> Don't have an account? <br>
<a href="signup"><strong>Sign Up !</strong> </a> </p>
</div>
</div>
</div>
</div>
</div>
 
