You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
6.8 KiB
208 lines
6.8 KiB
<?php |
|
/** |
|
* 应用方法 |
|
* 2020.02 by xiaowei |
|
*/ |
|
|
|
function id_aes_encrypt($plaintext, $key){ |
|
$cipherMethod = 'aes-128-ctr'; |
|
$ivlen = openssl_cipher_iv_length($cipherMethod); |
|
$iv = openssl_random_pseudo_bytes(16); |
|
$ciphertext = openssl_encrypt($plaintext, $cipherMethod, $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); |
|
$res = $iv . $ciphertext; |
|
$code = bin2hex($res); |
|
return $code; |
|
} |
|
|
|
function id_aes_decrypt($enc, $key){ |
|
$raw = hex2bin($enc); |
|
$cipherMethod = 'aes-128-ctr'; |
|
$ivlen = openssl_cipher_iv_length($cipherMethod); |
|
$iv = substr($raw, 0, $ivlen); |
|
$raw = substr($raw, $ivlen); |
|
$res = openssl_decrypt($raw, $cipherMethod, $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); |
|
return $res; |
|
} |
|
|
|
function id_CKdecrypt($enc){ |
|
$ck_auth = id_aes_decrypt($enc, API_KEY); |
|
$jsonarr = json_decode($ck_auth,true); |
|
return $jsonarr; |
|
} |
|
|
|
//登录数字平台接口 |
|
function digitLogin($code='') |
|
{ |
|
if($code){ |
|
$id_ckuserinfo = $id_ckuser['client']; |
|
$re_login = CallInterface(DIGIT_API_URL.'/v1/user/login?code='.$code,'GET'); |
|
//print_r($re_login); |
|
$jsonarr = json_decode($re_login,true); |
|
if($jsonarr['success']===true){ |
|
$data = $jsonarr['data']; |
|
$base64_data = base64_encode($data['client']['appid'].'.'.$data['access_token'].'.'.$data['client']['uid']); |
|
//print_r($base64_data); |
|
setcookie('digit_authentication',$base64_data,time()+3600*24*14,'/'); |
|
setcookie('digit_accessToken',$data['access_token'],time()+3600*24*3,'/'); |
|
setcookie('digit_refreshToken',$data['refresh_token'],time()+3600*24*14,'/'); |
|
setcookie('digit_expiresTime',$data['expires_time'],time()+3600*24*3,'/'); |
|
} |
|
} |
|
} |
|
|
|
function CallInterface($URL,$type,$params='',$arr_header=''){ |
|
$ch = curl_init(); |
|
if($arr_header){ |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header); |
|
} |
|
curl_setopt ($ch, CURLOPT_URL, $URL); |
|
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); |
|
switch ($type){ |
|
case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break; |
|
case "POST": curl_setopt($ch, CURLOPT_POST,true); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; |
|
case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; |
|
case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; |
|
} |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
|
$file_contents = curl_exec($ch); |
|
curl_close($ch); |
|
return $file_contents; |
|
} |
|
|
|
|
|
//刷新token |
|
function id_refresh_token() |
|
{ |
|
$id_ckuser = id_CKdecrypt($_COOKIE["fr_id_auth"]); |
|
$id_ckuserinfo = $id_ckuser['client']; |
|
$data = '&appid='.$id_ckuserinfo['appid'].'&uid='.$id_ckuserinfo['uid'].'&refresh_token='.$id_ckuser['refresh_token']; |
|
$re_login = CallInterface(API_URL.'/v1/token/refresh/'.$data,'GET'); |
|
//print_r($re_login); |
|
$jsonarr = json_decode($re_login,true); |
|
if($jsonarr['message']=='success'){ |
|
$new_json = json_encode($jsonarr['data'],JSON_UNESCAPED_UNICODE); |
|
$u_data = id_aes_encrypt($new_json, API_KEY); |
|
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); |
|
setcookie('fr_id_auth',$u_data,time()+3600*24*14,'/'); |
|
}else{ |
|
setcookie('fr_id_auth','',0,'/'); |
|
} |
|
} |
|
|
|
//刷新token 前后分离用 |
|
function setRefreshToken() |
|
{ |
|
$id_ckuser = id_CKdecrypt($_COOKIE["fr_id_auth"]); |
|
$id_ckuserinfo = $id_ckuser['client']; |
|
$data = '&appid='.$id_ckuserinfo['appid'].'&uid='.$id_ckuserinfo['uid'].'&refresh_token='.$id_ckuser['refresh_token']; |
|
$re_login = CallInterface(DIGIT_API_URL.'/v1/token/refresh/'.$data,'GET'); |
|
$jsonarr = json_decode($re_login,true); |
|
if($jsonarr['message']=='success'){ |
|
$new_json = json_encode($jsonarr['data'],JSON_UNESCAPED_UNICODE); |
|
$u_data = id_aes_encrypt($new_json, API_KEY); |
|
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); |
|
setcookie('fr_access_token',$jsonarr['data']['access_token'],time()+3600*24*365,'/'); |
|
setcookie('fr_uid',$jsonarr['data']['client']['uid'],time()+3600*24*365,'/'); |
|
setcookie('fr_appid',$jsonarr['data']['client']['appid'],time()+3600*24*365,'/'); |
|
setcookie('fr_id_auth',$u_data,time()+3600*24*14,'/'); |
|
}else{ |
|
setcookie('fr_id_auth','',0,'/'); |
|
} |
|
} |
|
|
|
//生成用户资料 |
|
function id_user_info($uid,$appid,$access_token){ |
|
$au_header = ['authentication:'.base64_encode($appid.'.'.$access_token.'.'.$uid)]; |
|
$re_api = CallInterface(API_URL.'/v1/user/getUserinfo/?uid='.$uid,'GET','',$au_header); |
|
$u_data = id_aes_encrypt($re_api, API_KEY); |
|
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); |
|
set_idcookie('fr_id_userinfo',$u_data,IDCookieOptions()); |
|
} |
|
|
|
//写入access_token |
|
function set_access_token($code){ |
|
$id_ckauthinfo = $code['client']; |
|
set_idcookie('fr_access_token',$code['access_token'],IDCookieOptions()); |
|
set_idcookie('fr_uid',$id_ckauthinfo['uid'],IDCookieOptions()); |
|
set_idcookie('fr_appid',$id_ckauthinfo['appid'],IDCookieOptions()); |
|
} |
|
//删除access_token |
|
function del_access_token(){ |
|
set_idcookie('access_token','',IDCookieOptions(0)); |
|
set_idcookie('uid','',IDCookieOptions(0)); |
|
set_idcookie('appid','',IDCookieOptions(0)); |
|
} |
|
|
|
|
|
//属性参数 |
|
function IDCookieOptions($exptime=1){ |
|
$Options = [ |
|
'expires' => $exptime ? time()+3600*24*14:0, |
|
'domain' => '', |
|
'httponly' => false, |
|
'samesite' => 'None', |
|
'secure' => true, |
|
'path' => '/' |
|
]; |
|
return $Options; |
|
} |
|
|
|
|
|
//idapi 写cookie方式 |
|
function set_idcookie($name, $value, array $options) |
|
{ |
|
setcookie($name,$value,$options['expires'],'/'); |
|
/*if(id_ChromeVer()<79){ |
|
setcookie($name,$value,$options['expires'],'/'); |
|
}else{ |
|
$header = 'Set-Cookie:'; |
|
$header .= rawurlencode($name) . '=' . rawurlencode($value) . ';'; |
|
if (isset($options['expires'])) { |
|
$header .= 'expires=' . \gmdate('D, d-M-Y H:i:s T', $options['expires']) . ';'; |
|
} |
|
if (isset($options['expires'])) { |
|
$header .= 'Max-Age=' . max(0, (int) ($options['expires'] - time())) . ';'; |
|
} |
|
if (!empty($options['path'])) { |
|
$header .= 'path=' . $options['path']. ';'; |
|
} |
|
if (!empty($options['domain'])) { |
|
$header .= 'domain=' . rawurlencode($options['domain']) . ';'; |
|
} |
|
if (!empty($options['secure'])) { |
|
$header .= 'Secure;'; |
|
} |
|
if (!empty($options['httponly'])) { |
|
$header .= 'HttpOnly;'; |
|
} |
|
if (!empty($options['samesite'])) { |
|
$header .= 'SameSite=' . rawurlencode($options['samesite']); |
|
} |
|
header($header, false); |
|
} */ |
|
} |
|
|
|
|
|
|
|
//Chrome版本 |
|
function id_ChromeVer(){ |
|
if (empty($_SERVER['HTTP_USER_AGENT'])){ |
|
return 'unknow'; |
|
} |
|
$agent= $_SERVER['HTTP_USER_AGENT']; |
|
if (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)){ |
|
return $regs[1]; |
|
} |
|
else{ |
|
return 'unknow'; |
|
} |
|
} |
|
|
|
|
|
?>
|