2014年5月2日 星期五

以openid登入sfs3學務系統和xoops

以前將登入方式改以電子郵件認證,現在學校的電子郵件已使用Google Apps for Education,還是可以使用這種方式,只要把主機位址改成imap.gmail.com:993/imap/ssl/novalidate-cert。因為google有提供openid的服務,所以當電子郵件是登入狀態,使用openid就可以快速登入系統,不用再輸入帳號密碼,方便許多。只是sfs3和xoops要加以修改才可以使用openid登入。


一、sfs3學務系統修改方式:
1.下載openid.php
到https://code.google.com/p/lightopenid/downloads/下載lightopenid壓縮檔,將裡面的openid.php程式放到login.php相同的目錄下。
2.修改login.php
(1)修改登出部份,詢問是否要登出google帳號。
if ($_GET['logout'] == "yes"){
:
}
改為
if ($_GET['logout'] == "yes"){
if($_SESSION['session_login_from'] == 'google'){
$href=$SFS_PATH_HTML."/login.php?logout=google";
}
else{
$href=$SFS_PATH_HTML;
}
$CONN -> Execute ("update pro_user_state set pu_state=0,pu_time_over=now() where teacher_sn='{$_SESSION['session_tea_sn']}'") or user_error("更新失敗!",256);
session_destroy();
setcookie(session_name(),'',time()-3600);
$_SESSION = array();
header("Location: $href");
}
if($_GET['logout'] == "google"){
head("Google帳戶登出確認<", "", 1);
echo"<table style='width:100%;'><tr><td style='text-align:center;padding:15px;'>
<div  class='ui-widget-header ui-corner-top'  style='width:350px; padding:5px; margin:auto'>
<span style='text-align:center;'>Google帳戶登出確認</span>
</div>
<div  class='ui-widget-content ui-corner-bottom'  style='width:350px; padding:5px; margin:auto'>
<p>您已登出本系統,但是您從google帳戶登入,<br>
所以google帳戶仍然是登入狀態。<br>
</p>
<input type='button' value='登出google帳戶' onclick='location.href=\"https://accounts.google.com/Logout?hl=zh-TW\"'>
<input type='button' value='保持登入狀態' onclick='location.href=\"".$SFS_PATH_HTML."\"'>
</div>
</td></tr></table>";
foot();
exit;
}
(2)在 switch($op) { } 裡面增加
case "google":
login_google();
break;
(3)在最後面加入下列函式,修改sfs3學務系統網址和email地址
function login_google(){
global $CONN;
include_once 'openid.php';
try {
$openid = new LightOpenID('改成sfs3學務系統網址');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
}
elseif($openid->mode == 'cancel') {
header("location: login.php");
}
else {
$user_profile=$openid->getAttributes();
$log_id=explode("@",$user_profile['contact/email']);
if($log_id[1]!="改成電子郵件位址"){
header("location: login.php");
}
// 檢查學務系統帳號
if (!$CONN) user_error("資料庫連線不存在!請檢查相關設定!",256);
$sql_select = " select teacher_sn,name, login_pass from teacher_base where teach_condition = 0 and teach_id='$log_id[0]' and teach_id<>''";
$recordSet = $CONN -> Execute($sql_select) or trigger_error("資料連結錯誤:" . $sql_select, E_USER_ERROR);
while(list($teacher_sn, $name , $login_pass) = $recordSet -> FetchRow()){
$_SESSION['session_log_id'] = $log_id[0];
$_SESSION['session_log_pass'] = $login_pass;
$_SESSION['session_tea_sn'] = $teacher_sn;
$_SESSION['session_tea_name'] = $name;
$_SESSION['session_who'] = "教師";
$_SESSION[$session_prob] = get_prob_power($teacher_sn,"教師");
$_SESSION['session_login_from'] = 'google'; //紀錄下是從google openid登入
login_logger($teacher_sn,"教師");
$query = "insert into pro_user_state (teacher_sn,pu_state,pu_time,pu_ip) values($teacher_sn,1,now(),'{$_SERVER['REMOTE_ADDR']}')";
$CONN -> Execute($query) or user_error("新增失敗!<br>$query",256);
header("location: index.php");
}
}
}
catch(ErrorException $e) {
echo $e->getMessage();
}
}
3.修改themes/new/new_login.php,加入快速登入按鈕
<input type='button' value='透過google帳戶快速登入'  onclick='location.href=\"".$SFS_PATH_HTML."login.php?login&log_who=google\"'>

二、xoops修改方式:
1.下載openid.php
到https://code.google.com/p/lightopenid/downloads/下載lightopenid壓縮檔,將裡面的openid.php程式放到checklogin.php相同的目錄下。
2.修改include/checklogin.php
(1)原 xoops 認證方式
include_once $GLOBALS['xoops']->path('class/auth/authfactory.php');
xoops_loadLanguage('auth');
$xoopsAuth =& XoopsAuthFactory::getAuthConnection($myts->addSlashes($uname));
$user = $xoopsAuth->authenticate($myts->addSlashes($uname), $myts->addSlashes($pass));
改為
function google_auth(){
include_once 'include/openid.php';
try {
$openid = new LightOpenID(XOOPS_URL);
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
}
elseif($openid->mode == 'cancel') {
redirect_header(XOOPS_URL.'/user.php', 3, _US_INCORRECTLOGIN);
exit();
}
else {
$user_profile=$openid->getAttributes();
$log_id=explode("@",$user_profile['contact/email']);
if($log_id[1]!="改成電子郵件位址"){
redirect_header(XOOPS_URL.'/user.php', 3, _US_INCORRECTLOGIN);
exit();
}
$uname=$log_id[0];
$member_handler =& xoops_gethandler('member');
$myts =& MyTextsanitizer::getInstance();
$getuser =& $member_handler->getUsers(new Criteria('uname', $myts->addSlashes($uname)));
if (empty($getuser)){ // 不是 xoops 會員,請離開!
redirect_header(XOOPS_URL.'/user.php', 3, _US_INCORRECTLOGIN);
exit();
}
else{
return $getuser[0];
}
}
catch(ErrorException $e) {
echo $e->getMessage();
return false;
}
}
if(isset($_GET['login'])){ //google登入
$user=google_auth();
}
else{  //原認證方式
include_once $GLOBALS['xoops']->path('class/auth/authfactory.php');
xoops_loadLanguage('auth');
$xoopsAuth =& XoopsAuthFactory::getAuthConnection($myts->addSlashes($uname));
$user = $xoopsAuth->authenticate($myts->addSlashes($uname), $myts->addSlashes($pass));
}
(2)紀錄下是從google登入的
$_SESSION = array();
後面增加
if(isset($_GET['login'])){
$_SESSION['login_from'] = 'google';
}
2.加入快速登入按鈕
<input type='button' value='google登入' title="透過google帳戶登入"
onclick='location.href="user.php?op=login&login"'>
3.修改登出部份,詢問是否要登出google帳號
(1)修改modules/profile/user.php
if ($op == 'logout') {
:
:
}
改為
if ($op == 'logout') {
    $message = '';
    if($_SESSION['login_from'] == 'google'){
    $href="google_logout.php";
    }
    // Regenerate a new session id and destroy old session
    $GLOBALS["sess_handler"]->regenerate_id(true);
    $_SESSION = array();
    setcookie($GLOBALS['xoopsConfig']['usercookie'], 0, -1, '/', XOOPS_COOKIE_DOMAIN, 0);
    setcookie($GLOBALS['xoopsConfig']['usercookie'], 0, - 1, '/');
    // clear entry from online users table
    if (is_object($GLOBALS['xoopsUser'])) {
        $online_handler =& xoops_gethandler('online');
        $online_handler->destroy($GLOBALS['xoopsUser']->getVar('uid'));
    }
    $message = _US_LOGGEDOUT . '<br />' . _US_THANKYOUFORVISIT;
    redirect_header(XOOPS_URL . '/' . $href , 1, $message);
    exit();
}
(2)在xoops根目錄新增一個檔案google_logout.php,內容為:
<?php
if (!isset($mainfile)) {
include("mainfile.php");
}

include("header.php");

echo"
<div style='border:1px solid #666666;background-color:#009900;width:350px; padding:5px; margin:auto;text-align:center;'>
<span style='text-align:center;color: #FFFFFF;'>Google帳戶登出確認</span>
</div>
<div style='border:1px solid #666666;width:350px; padding:5px; margin:auto;text-align:center;'>
<p>您已登出本系統,但是您從google帳戶登入,<br>
所以google帳戶仍然是登入狀態。<br>
</p>
<input type='button' value='登出google帳戶' onclick='location.href=\"https://accounts.google.com/Logout?hl=zh-TW\"'>
<input type='button' value='保持登入狀態' onclick='location.href=\"".XOOPS_URL."\"'>
</div>
";

include("footer.php");
?>

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。