| 1234567891011121314151617181920212223242526272829 | 
							- <?php
/*
 * OAS Admin Auth
 * 
 * Author: XUCHAGN ZHANG
 *
 * Login status data in session like this:
 * 
 *	{
 *		email: "zhangxuchang@oasgame.com",
 *		permissionid: "40001,40002,40003,40004,40004001",
 *		uid: "200000000000000"
 *	}
 *
 * */
define('OAS_ADMIN_LOGIN_SESSION_KEY','oas_admin_login_key');
define('OAS_ADMIN_SESSION_APPID_KEY','oas_admin_session_appid_key');
session_start();
class OAS_AdminAuth{
	
	private static $_adminHost = "http://adm.oasgames.com";
	private static $_ucHost    = "http://passport.oasgames.com";
	
	// Sys user login
	public static function loginHandler($sys_code){
		
		if(!empty($_REQUEST['adm_key'])){
			self::integrateLogin($sys_code);
		}
	}
	
	public static function getLoginUser(){
		
		$loginUser = $_SESSION[OAS_ADMIN_LOGIN_SESSION_KEY];
		if(empty($loginUser)){
			self::directToLoginPage();
		}
		return $loginUser;
		
	}
	
	public static function logoutHandler(){
		$_SESSION[OAS_ADMIN_LOGIN_SESSION_KEY] = null;
		self::directToLoginPage();
	}
	
 
- 	private static function integrateLogin($sys_code){
 
- 	
 
- 		$user_key = $_REQUEST["adm_key"];
 
- 		$app_id   = $_REQUEST['app_id'];
		
 
- 		// No login key
 
- 		if(empty($user_key)){
 
- 			self::directToLoginPage($app_id,'adm_key_is_null');
 
- 		}
 
- 	
 
- 		//get user information
 
- 		$getuser_api = self::$_ucHost . "/?m=getLoginUser&oas_user=".$user_key;
 
- 		$userinfo    = file_get_contents($getuser_api);
 
- 		$userinfo    = json_decode($userinfo,true);
 
- 	
 
- 		if($userinfo['status']=='ok'){
 
- 			
			$right = self::getUserRights($userinfo['val']['id'],$sys_code);
 
- 			
			if(is_array($right)){
				// set login status,save user info in session
 
- 				$_SESSION[OAS_ADMIN_LOGIN_SESSION_KEY] = $right;
 
- 				$_SESSION[OAS_ADMIN_SESSION_APPID_KEY] = $app_id;
 
- 			}
 
- 			else{
 
- 				self::directToLoginPage($app_id,$right);
 
- 			}
 
- 		}
 
- 		else{
 
- 			self::directToLoginPage($app_id,'login_status_timeout');
 
- 		}
 
- 	}
	
	private static function getUserRights($uid,$sys_code){
		
		$sys  = $sys_code;
		$key  = md5( $sys . $uid . 'c16a292901bf9db7');
		$api  = self::$_adminHost . "/api/get_per_uid_sys.php?uid=$uid&systemid=$sys&secrtkey=$key";
		
		$res = self::makeRequest($api,array(),10);
		
		if(empty($res)){
			return 'get_right_no_response';
		}
		
		$rights = json_decode($res,true);
		
		if($rights['status']=="fail"){
			return "get_right_err_{$rights['err_code']}";
		}
		return $rights['val'];
	}
	private static function directToLoginPage($appid='',$err=''){
		
		// app id
		if(!empty($appid)){
			$app_id = $appid;
		}
		else{
			$app_id = $_SESSION[OAS_ADMIN_SESSION_APPID_KEY];
		}
		
		$url = self::$_adminHost . "/login.php?app_id=$app_id";
		
		// err msg
		if(!empty($err)){
			$url = "$url&err=$err";
		}
		// direct		
		header("Location: $url");
		exit();
	}
	
	private static function makeRequest($url, $params,$timeout=15,$post=true,$file_upload=false) {
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_POST, $post);
	    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		if($file_upload == false){
	    	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));  
		}
		else{
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}
	    $result = curl_exec($ch);
	    curl_close($ch);
	    return $result;
	}	
	
}// class end
 
 
  |