File: /home/imensosw/public_html/imenso.co/demo/claapp/login.php
<?php
ob_start();
session_start();
require("config/setting.php");
require("config/Crud.php");
// return claims
function token_test($token) {
$res = 0;
// 1 create array from token separated by dot (.)
$token_arr = explode('.', $token);
$header_enc = $token_arr[0];
$claim_enc = $token_arr[1];
$sig_enc = $token_arr[2];
// 2 base 64 url decoding
$header = base64_url_decode($header_enc);
$claim = base64_url_decode($claim_enc);
$sig = base64_url_decode($sig_enc);
return $claim;
}
function base64_url_decode($arg) {
$res = $arg;
$res = str_replace('-', '+', $res);
$res = str_replace('_', '/', $res);
switch (strlen($res) % 4) {
case 0:
break;
case 2:
$res .= "==";
break;
case 3:
$res .= "=";
break;
default:
break;
}
$res = base64_decode($res);
return $res;
}
// login wit azure
error_reporting(-1);
ini_set('display_errors', 'On');
//$redirect="http%3A%2F%2localhost%2Ftest%2Fazurcode%2F";
$redirect="https://cla.nthriveeducation.com/login.php";
//Ekyg3spoq7AfarTeee/OC9Of+et3DmraYRiUH9uPa7w=
if (!isset($_GET['code'])) {
$authUrl = "https://login.microsoftonline.com/common/oauth2/authorize?";
$authUrl .= "client_id=1e7d81bb-fe45-47de-a7b8-ced3159254d0";
$authUrl .= "&response_type=code";
$authUrl .= "&redirect_uri=".$redirect;
$authUrl .= "&response_mode=query";
$authUrl .= "&resource=https%3A%2F%2Fmanagement.azure.com%2F";
$authUrl .= "&state=12345";
/*
$authUrl ="https://login.microsoftonline.com/common/oauth2/authorize?";
$authUrl .="client_id=c96f52b3-51ec-45b4-ad54-b047581b8e17";
$authUrl .="&response_type=id_token";
$authUrl .="&redirect_uri=".$redirect;
$authUrl .="&response_mode=form_post";
$authUrl .="&scope=openid";
$authUrl .="&state=12345";
$authUrl .="&nonce=678910";*/
//header('Location: '.$authUrl);
} else {
$accesscode = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://login.microsoftonline.com/common/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
$client_id = "1e7d81bb-fe45-47de-a7b8-ced3159254d0";
$client_secret = "IICsoLxnJ8Lfu3l2lBD01l9SyvAVaVRBNPesHg3/Gx0=";
//$client_secret="Fv4AI54YBEFYtKctTFl8Bgz27DJFMSYRk1aaA3cUBwU=";
curl_setopt($ch, CURLOPT_POSTFIELDS,
"grant_type=authorization_code&client_id=".$client_id."&redirect_uri=".$redirect."&resource=https%3A%2F%2Fmanagement.azure.com%2F&&code=".$accesscode."&client_secret=".urlencode($client_secret));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$jsonoutput = json_decode($server_output, true);
//print_r($jsonoutput); die();
$bearertoken = $jsonoutput['access_token'];
/* $url = "https://management.azure.com/subscriptions/?api-version=2015-01-01";
$ch = curl_init($url);
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Authorization: Bearer '. $bearertoken;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$result = curl_exec($ch);
curl_close($ch);
$result;
//echo "<br>";*/
$emailErr="";
if($bearertoken!=""){
$final_res=json_decode(token_test($bearertoken));
//print_r($final_res);
//die();
$id=0;
$email=$final_res->upn;
$name=$final_res->name." ".$final_res->family_name;
$crud=New Crud();
$query="select * from users where email='".$email."'";
$result=$crud->getData($query);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $email=="") {
$emailErr = "Invalid email format";
}
if($result && $emailErr=="")
{
$id = $result[0]['id'];
if($result[0]['clacreator']==1){
$_SESSION['nthrive_type']="creator";
}
if($result[0]['claadmin']==1){
$_SESSION['nthrive_type']="manager";
}
if($result[0]['appadmin']==1){
$_SESSION['nthrive_type']="admin";
}
if($result[0]['clauser']==1){
$_SESSION['nthrive_type']="user";
}
}
else
{
$id = $crud->execute("INSERT INTO users(name,created,email,appadmin,claadmin,clacreator,clauser) VALUES('$name','1','$email',0,0,0,1)");
$_SESSION['nthrive_type']="user";
}
if($id && $emailErr=="")
{
$_SESSION['nthrive_id']=$id;
$_SESSION['nthrive_email']=$email;
$_SESSION['nthrive_name']=$name;
$_SESSION['nthrive_timezone']="";
$_SESSION['success']="Successfully logged in...";
header('location:user');
}
else
{
echo "email:".$email."<br>";
echo "name:".$name."<br>";
echo "id:".$id."<br>";
$_SESSION['warrning']="Invalid username or password";
// header('location:login.php');
}
}
if($emailErr!="")
{
$_SESSION['warrning']=$emailErr;
}
else
{
$_SESSION['warrning']="Invalid username or password ";
}
die ();
}
//end azur
$crud = new Crud();
if(isset($_SESSION['nthrive_type']) && $_SESSION['nthrive_type']=="creator")
{
header('location:creator');
}
else if(isset($_SESSION['nthrive_type']) && $_SESSION['nthrive_type']=="manager")
{
header('location:manager');
}
else if(isset($_SESSION['nthrive_type']) && $_SESSION['nthrive_type']=="user")
{
header('location:user');
}
else if(isset($_SESSION['nthrive_type']) && $_SESSION['nthrive_type']=="admin")
{
header('location:admin');
}
if(isset($_POST['username']))
{
$email = $crud->escape_string($_POST['username']);
$query="select * from users where email='".$email."'";
$result=$crud->getData($query);
if($result)
{
if($result[0]['clacreator']==1){
$_SESSION['nthrive_type']="creator";
}
if($result[0]['claadmin']==1){
$_SESSION['nthrive_type']="manager";
}
if($result[0]['appadmin']==1){
$_SESSION['nthrive_type']="admin";
}
if($result[0]['clauser']==1){
$_SESSION['nthrive_type']="user";
}
$_SESSION['nthrive_id']=$result[0]['id'];
$_SESSION['nthrive_email']=$result[0]['email'];
$_SESSION['nthrive_name']=$result[0]['name'];
$_SESSION['nthrive_timezone']=$result[0]['timezone'];
$_SESSION['success']="Successfully logged in...";
header('location:user');
exit();
}
else
{
$_SESSION['warrning']="Invalid username or password";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>nThrive Incubator</title>
<!-- Bootstrap -->
<link href="libs/bootstrap.css" rel="stylesheet" type="text/css">
<!-- Web Font -->
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
<!-- Custom CSS -->
<link href="style.css" rel="stylesheet" type="text/css" media="all">
<!-- Font Awesome -->
<link href="libs/font-awesome.min.css" rel="stylesheet">
<style type="text/css">
body{ background:/* #4a1c6b*/ #4d356b; width: 100%; height: 100% }
</style>
</head>
<body>
<div class="main-container container padding-10">
<div class="login-container">
<div class="" id="login-DataRiverLogo">
<img src="images/nthrive-logo.png" alt="nThrive Analytics" title="nThrive Analytics" class="pull-left">
<div class="top_msg text-right pull-right">Closed Loop Audit</div>
</div>
<div class="lc-block" id="l-login">
<div class="m_50 text-center">
<a href="#" class="btn microsoft"> Loging with Microsoft Account</a>
</div>
</div>
<div class="lc-block toggled" id="l-login">
<form action="" method="post" id="login_form">
<div class="input-group m-b-20">
<span class="input-group-addon p-l-0"><i class="glyphicon glyphicon-user"></i></span>
<div class="fg-line">
<input id="username" autocomplete="off" required autocapitalize="none" name="username" class="form-control" placeholder="Username" type="email">
</div>
</div>
<div class="input-group m-b-20">
<span class="input-group-addon p-l-0"><i class="glyphicon glyphicon-lock"></i></span>
<div class="fg-line">
<input id="password" autocomplete="off" required autocapitalize="none" name="password" class="form-control" placeholder="Password" type="password">
</div>
</div>
<div class="clearfix"></div>
<!-- <div class="checkbox">
<label>
<input name="remember" value="" type="checkbox">
<i class="input-helper"></i>
Remember Me
</label>
</div>-->
<input type="submit" value="submit" name="login" class="btn btn-success">
<!-- <button class="btn btn-login btn-success btn-float waves-effect waves-circle waves-float" type="submit" name="login"><i class="glyphicon glyphicon-arrow-right"></i></button> -->
<a href="<?php echo $authUrl ?>" class="btn Azure">Login With Azure</a>
<!--<ul class="login-navigation">
<li class="l-forget-password">Forgot Password?</li>
</ul>-->
</form>
</div>
<!-- Forgot Password -->
<div class="lc-block" id="l-forget-password">
<p class="text-left">Let us know your username and we'll send you an email to reset your password.</p>
<div class="input-group m-b-20">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<div class="fg-line">
<input autocapitalize="none" autocomplete="off" id="reset-password-username" class="form-control" placeholder="Username" type="text">
</div>
</div>
<a href="#" id="submit-unknownPassword" class="btn btn-login btn-success btn-float waves-effect waves-circle waves-float"><i class="glyphicon glyphicon-arrow-right"></i></a>
<ul class="login-navigation">
<li class="l-login">Sign-In</li>
</ul>
</div>
</div>
</div>
<?php
if(isset($_SESSION['success']))
{
?>
<div class="alert alert-success fade in fixed_right">
<a href="#" class="close" data-dismiss="alert">×</a>
<!-- <strong>Success!</strong> --> <?php echo $_SESSION['success']; ?>.
</div>
<?php
unset($_SESSION['success']);
}
else if(isset($_SESSION['error']))
{
?>
<div class="alert alert-danger fade in fixed_right">
<a href="#" class="close" data-dismiss="alert">×</a>
<!-- <strong>Error!</strong> --> <?php echo $_SESSION['error']; ?>.
</div>
<?php
unset($_SESSION['error']);
}
else if(isset($_SESSION['warrning']))
{
?>
<div class="alert alert-warning fade in fixed_right">
<a href="#" class="close" data-dismiss="alert">×</a>
<!-- <strong>Warning!</strong> --> <?php echo $_SESSION['warrning']; ?>.
</div>
<?php
unset($_SESSION['warrning']);
}
?>
<script src="libs/jquery.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="libs/bootstrap.min.js"></script>
<script type="text/javascript" src="libs/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#login_form").validate();
})
// alert("asdf");
$(".l-login").click(function(){
// alert("asdf");
$("#l-forget-password").removeClass("toggled");
$("#l-login").addClass("toggled");
});
$(".l-forget-password").click(function(){
// alert("asdf");
$("#l-login").removeClass("toggled");
$("#l-forget-password").addClass("toggled");
});
if($('.alert').length)
{
setTimeout(function(){ $('.close').trigger('click'); }, 2000);
}
</script>
</body>
</html>