/** Undocumented Function. * Basically performs the whole login routine * @todo Document it */ function login() { $allow_login_query = "SELECT `value` FROM `" . MYSQL_DATABASE_PREFIX . "global` WHERE `attribute` = 'allow_login'"; $allow_login_result = mysql_query($allow_login_query); $allow_login_result = mysql_fetch_array($allow_login_result); if (isset($_GET['subaction'])) { if ($_GET['subaction'] == "resetPasswd") { return resetPasswd($allow_login_result[0]); } if ($allow_login_result[0]) { if ($_GET['subaction'] == "register") { require_once "registration.lib.php"; return register(); } } global $openid_enabled; if ($openid_enabled == 'true' && $allow_login_result[0]) { if ($_GET['subaction'] == "openid_login") { if (isset($_POST['process'])) { $openid_url = trim($_POST['openid_identifier']); openid_endpoint($openid_url); } } if ($_GET['subaction'] == "openid_verify") { if ($_GET['openid_mode'] != "cancel") { $openid_url = $_GET['openid_identity']; // Get the user's OpenID Identity as returned to us from the OpenID Provider $openid = new Dope_OpenID($openid_url); //Create a new Dope_OpenID object. $validate_result = $openid->validateWithServer(); //validate to see if everything was recieved properly if ($validate_result === TRUE) { $userinfo = $openid->filterUserInfo($_GET); return openid_login($userinfo); } else { if ($openid->isError() === TRUE) { // Else if you're here, there was some sort of error during processing. $the_error = $openid->getError(); $error = "Error Code: {$the_error['code']}<br />"; $error .= "Error Description: {$the_error['description']}<br />"; } else { //Else validation with the server failed for some reason. $error = "Error: Could not validate the OpenID at {$_SESSION['openid_url']}"; } } } else { displayerror("User cancelled the OpenID authorization"); } } if ($_GET['subaction'] == "openid_pass") { if (!isset($_SESSION['openid_url']) || !isset($_SESSION['openid_email'])) { displayerror("You are trying to link an OpenID account without validating your log-in. Please <a href=\"./+login\">Login</a> with your OpenID account first."); return; } else { $openid_url = $_SESSION['openid_url']; $openid_email = $_SESSION['openid_email']; unset($_SESSION['openid_url']); unset($_SESSION['openid_email']); if (!isset($_POST['user_password'])) { displayerror("Empty Passwords not allowed"); return; } $user_passwd = $_POST['user_password']; $info = getUserInfo($openid_email); if (!$info) { displayerror("No user with Email {$openid_email}"); } else { $check = checkLogin($info['user_loginmethod'], $info['user_name'], $openid_email, $user_passwd); if ($check) { //Password was correct. Link the account $query = "INSERT INTO `" . MYSQL_DATABASE_PREFIX . "openid_users` (`openid_url`,`user_id`) VALUES ('{$openid_url}'," . $info['user_id'] . ")"; $result = mysql_query($query) or die(mysql_error() . " in login() subaction=openid_pass while trying to Link OpenID account"); if ($result) { displayinfo("Account successfully Linked. Log In one more time to continue."); } } else { displayerror("The password you specified was incorrect"); } } } } if ($_GET['subaction'] == "quick_openid_reg") { if (!isset($_SESSION['openid_url']) || !isset($_SESSION['openid_email'])) { displayerror("You are trying to register an OpenID account without validating your log-in. Please <a href=\"./+login\">Login</a> with your OpenID account first."); return; } else { $openid_url = $_SESSION['openid_url']; $openid_email = $_SESSION['openid_email']; unset($_SESSION['openid_url']); unset($_SESSION['openid_email']); if (!isset($_POST['user_name']) || $_POST['user_name'] == "") { displayerror("You didn't specified your Full name. Please <a href=\"./+login\">Login</a> again."); return; } $openid_fname = escape($_POST['user_name']); //Now let's start making the dummy user $query = "INSERT INTO `" . MYSQL_DATABASE_PREFIX . "users` " . "(`user_name`, `user_email`, `user_fullname`, `user_password`, `user_activated`,`user_loginmethod`) " . "VALUES ('" . $openid_email . "', '" . $openid_email . "','" . $openid_fname . "','0',1,'openid');"; $result = mysql_query($query) or die(mysql_error() . " in login() subaction=quick_openid_reg while trying to insert information of new account"); if ($result) { $id = mysql_insert_id(); $query = "INSERT INTO `" . MYSQL_DATABASE_PREFIX . "openid_users` (`openid_url`,`user_id`) VALUES ('{$openid_url}'," . $id . ")"; $result = mysql_query($query) or die(mysql_error() . " in login() subaction=quick_openid_reg while trying to Link OpenID account"); if ($result) { displayinfo("Account successfully registered. You can now login via OpenID. Please complete your profile information after logging in."); } } return ""; } } } } if (!isset($_POST['user_email'])) { return loginForm($allow_login_result[0]); } else { /*if it is, then userLDAPVerify($user_email,$user_passwd); if the password is correct, update his password in DB else $dontloginLDAP = true; } else { if(userLDAPVerify($user_email,$user_passwd)) { create his row in DB with loginmethod = ldap and user_activated = 1 (for this, use the createUser funciton in common.lib.php) } }*/ global $cookieSupported; $login_status = false; if ($cookieSupported == true) { if ($_POST['user_email'] == "" || $_POST['user_password'] == "") { displayerror("Blank e-mail or password NOT allowed. <br /><input type=\"button\" onclick=\"history.go(-1)\" value=\"Go back\" />"); return loginForm($allow_login_result[0]); } else { $user_email = escape($_POST['user_email']); $user_passwd = escape($_POST['user_password']); $login_method = ''; if (!check_email($user_email)) { displayerror("Your E-Mail Provider has been blackilisted. Please contact the website administrator"); return loginForm($allow_login_result[0]); } if ($temp = getUserInfo($user_email)) { // check if exists in DB $login_status = checkLogin($temp['user_loginmethod'], $temp['user_name'], $user_email, $user_passwd); // This is to make sure when user logs in through LDAP, ADS or IMAP accounts, his passwords should be changed in database also, incase its old. if ($login_status) { updateUserPassword($user_email, $user_passwd); } //update passwd in db } else { //if user is not in db global $authmethods; if (strpos($user_email, '@') > -1) { $tmp = explode('@', $user_email); $user_name = $tmp[0]; $user_domain = strtolower($tmp[1]); } else { $user_name = $user_email; } if (isset($user_domain) && $user_domain == $authmethods['imap']['user_domain']) { if ($login_status = checkLogin('imap', $user_name, $user_email, $user_passwd)) { $login_method = 'imap'; } } elseif (isset($user_domain) && $user_domain == $authmethods['ads']['user_domain']) { if ($login_status = checkLogin('ads', $user_name, $user_email, $user_passwd)) { $login_method = 'ads'; } } elseif (isset($user_domain) && $user_domain == $authmethods['ldap']['user_domain']) { if ($login_status = checkLogin('ldap', $user_name, $user_email, $user_passwd)) { $login_method = 'ldap'; } } if ($login_status) { //create new user in db and activate the user (only if user's login is valid) $user_fullname = strtoupper($user_name); $user_md5passwd = md5($user_passwd); $query = "INSERT INTO `" . MYSQL_DATABASE_PREFIX . "users` " . "(`user_id`, `user_name`, `user_email`, `user_fullname`, `user_password`, `user_loginmethod`, `user_activated`) " . "VALUES (DEFAULT, '{$user_name}', '{$user_email}', '{$user_fullname}', '{$user_md5passwd}', '{$login_method}', '1')"; mysql_query($query) or die(mysql_error() . " creating new user !"); } else { displaywarning("Incorrect username and/or password for <b>" . (isset($user_domain) ? $user_domain . "</b> domain!" : $user_name . "</b> user")); } } if ($login_status) { $temp = getUserInfo($user_email); if (!$temp['user_activated']) { displayinfo("The e-mail has not yet been verified. Kindly check your email and click on verification link. <br /><input type=\"button\" onclick=\"history.go(-1)\" value=\"Go back\" />"); // if user exists in db and admin has set user_activated = false delibrately // then it means that the user has been denied access !!! } else { $query = "UPDATE `" . MYSQL_DATABASE_PREFIX . "users` SET `user_lastlogin`=NOW() WHERE `" . MYSQL_DATABASE_PREFIX . "users`.`user_id` ='{$temp['user_id']}'"; mysql_query($query) or die(mysql_error() . " in login.lib.L:111"); $_SESSION['last_to_last_login_datetime'] = $temp['user_lastlogin']; setAuth($temp['user_id']); //exit(); //displayinfo("Welcome " . $temp['user_name'] . "!"); return $temp['user_id']; } } else { displaywarning("Wrong E-mail or password. <a href='./+login&subaction=resetPasswd'>Lost Password?</a><br />"); return loginForm($allow_login_result[0]); } } return 0; } else { showCookieWarning(); return 0; } } }
curl_close($ch); $output = json_decode($output); $info = $output->id_token; if (!$info) { $error = "Invalid token"; } else { require_once 'JWT.php'; $final = JWT::decode($info, $client_secret, false); $email = $final->email; if ($final->sub) { $link_identifier = "accounts.google.com/{$final->sub}"; if (openid_login($link_identifier)) { header("Location: " . URL . "info/dashboard"); exit; } elseif ($final->openid_id) { if (openid_login($final->openid_id, $link_identifier)) { header("Location: " . URL . "info/dashboard"); exit; } else { $error = "User not found."; } } else { $error = "User not found."; } } else { $error = "Invalid response."; } } } else { $state = md5(rand()); $_SESSION['state'] = $state;
$final = JWT::decode($info, $client_secret, false); $email = $final->email; if ($final->sub) { $link_identifier = "accounts.google.com/{$final->sub}"; if (openid_login($link_identifier)) { $error = "User already exists"; $user_already_exists = true; } else { // Let's register this user. $post = array("screenname" => mysql_clean($final->email), "fullname" => mysql_clean($final->email), "mail" => mysql_clean($final->email), "creation_date" => "NOW()", "last_login" => "NOW()", "status" => 1); $db->insert("openid_users", $post); $openid_user = $db->insert_id; $url = $final->iss . "/" . $final->sub; $post = array("userid" => $openid_user, "url" => mysql_clean($url)); $db->insert("openid_identities", $post); openid_login($url); header("Location: " . URL . "info/dashboard"); exit; } } else { $error = "Invalid response."; } } } else { $state = md5(rand()); $_SESSION['state'] = $state; } ?> <!doctype html> <!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]--> <!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><![endif]-->