oasAdminAuth.class.php 3.4 KB

1234567891011121314151617181920212223242526272829
  1. <?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(); }
  2. private static function integrateLogin($sys_code){
  3. $user_key = $_REQUEST["adm_key"];
  4. $app_id = $_REQUEST['app_id'];
  5. // No login key
  6. if(empty($user_key)){
  7. self::directToLoginPage($app_id,'adm_key_is_null');
  8. }
  9. //get user information
  10. $getuser_api = self::$_ucHost . "/?m=getLoginUser&oas_user=".$user_key;
  11. $userinfo = file_get_contents($getuser_api);
  12. $userinfo = json_decode($userinfo,true);
  13. if($userinfo['status']=='ok'){
  14. $right = self::getUserRights($userinfo['val']['id'],$sys_code);
  15. if(is_array($right)){ // set login status,save user info in session
  16. $_SESSION[OAS_ADMIN_LOGIN_SESSION_KEY] = $right;
  17. $_SESSION[OAS_ADMIN_SESSION_APPID_KEY] = $app_id;
  18. }
  19. else{
  20. self::directToLoginPage($app_id,$right);
  21. }
  22. }
  23. else{
  24. self::directToLoginPage($app_id,'login_status_timeout');
  25. }
  26. } 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