session_start();
$username = "";
$password = "";
$loginerror = "";
$fromurl = '';
if(isset($_GET['fromurl']) && trim($_GET['fromurl'])!= '') {
$fromurl = $_GET['fromurl'];
}else if(isset($_SERVER['HTTP_REFERER']) && trim($_SERVER['HTTP_REFERER'])!= '') {
$fromurl = $_SERVER['HTTP_REFERER'];
}
//防止同一个服务器装了多个upassport互相干扰
$key = md5(DB_DATABASE.DB_USER.DB_PASSWORD);
if(!isset($_SESSION['passport_app'])) {
unset($_SESSION['u']);
$_SESSION['passport_app'] = $key;
include('themes/'.THEME.'/login.html');
exit;
}else if($_SESSION['passport_app'] != $key) {
unset($_SESSION['u']);
$_SESSION['passport_app'] = $key;
include('themes/'.THEME.'/login.html');
exit;
}
if (isset($_POST['op'])&&trim($_POST['op'])=='dologin') {
$valid = 0;
$authnum = $_POST['authnum'];
$username = $_POST['username'];
$fromurl = $_POST['fromurl'];
if ($authnum && trim($_SESSION['authnum'])==$authnum && $username) {
$password = $_POST['password'];
$user = $db->getRow("select * from $dbutils->user where username =
'$username' and password = '$password'");
if (!empty($user) && $user['user_id']!=0) {
$u['user_id'] = $user['user_id'];
$u['username'] = $user['username'];
$u['truename'] = $user['truename'];
$u['group'] = $db->getRow("select ug.group_id,g.group_name from
$dbutils->user_group ug,$dbutils->group g where ug.group_id =
g.group_id and ug.user_id = ".$user['user_id']);
$u['roles'] = $db->getAll("select ur.role_id,r.role_name,
r.privileges from $dbutils->user_role ur,$dbutils->role r where
ur.role_id = r.role_id and ur.user_id = ".$user['user_id']);
$privileges = Array();
foreach($u['roles'] as $role) {
$role_privis = explode(',',$role['privileges']);
foreach($role_privis as $p) {
if(!in_array($p,$privileges)) array_push($privileges,$p);
}
}
$u['privileges'] = $privileges ;
$_SESSION['u'] = $u;
$valid =1;
}
}
if (!$valid) {
$loginerror = '
'."非法登陆".'
';
$username = $_POST['username'];
unset($u);
}
}//用户正常跳转或者访问
$u = isset($_SESSION['u']) ? $_SESSION['u'] : '';
if (empty($u)) { //用户不存在,跳到登陆界面
include('themes/'.THEME.'/login.html');
exit;
}else { //用户存在,表示已经登陆过了
if(trim($fromurl)=='') { //没有referer,则显示默认主页,列出所有应用
$t->assign('u',$u);
$t->render('index.html', "欢迎登陆通行证",'wrap.html',true);
}else { //从别的应用转过来的,可能是上面 2 3 4,此时必有referer
//根据referer的url得到当前的应用的key和完整的地址
$passport_app = $db->GetRow("select * from $dbutils->app where
INSTR('$fromurl', url)=1");
$userinfo = passport_encrypt(passport_encode($u), $passport_app['key']);
$verify = md5($userinfo.$fromurl.$passport_app['key']);
header("Location: ".$passport_app['login_url'].
"?userinfo=".rawurlencode($userinfo).
"&fromurl=".rawurlencode($fromurl).
"&verify=$verify");
}
}