新增 Linuxdo 登录与绑定功能

This commit is contained in:
2024-12-21 17:59:43 +08:00
parent 35fecbb6d0
commit 88952cfb7a

164
linuxdo.php Normal file
View File

@@ -0,0 +1,164 @@
<?php
@header('Content-Type: text/html; charset=UTF-8');
include 'config.php';
include 'function.php';
$act = input('act');
$mySQLi = new mysqli($_CONFIG_DB['db_host'], $_CONFIG_DB['db_user'], $_CONFIG_DB['db_password'], $_CONFIG_DB['db_name'], $_CONFIG_DB['db_port']);
if($mySQLi->connect_errno) exit($mySQLi->connect_error);
$mySQLi->set_charset($_CONFIG_DB['db_charset']);
switch ($act) {
case 'reg':
$player = [
'username' => $userInfo['user_username'],
'password' => $userInfo['user_api_key'],
'email' => $userInfo['user_email'],
'reg_ip' => '127.0.0.1',
'reg_time' => time(),
'reg_source' => 'linuxdo',
]
$stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type='linuxdo' and connect_id=?');
$stmt->bind_param('s', $userInfo['user_username']);
$stmt->execute();
break;
default:
$code = $_GET['code'];
$key = base64_encode($_LINUXDO_CONNECT['client_id'].':'.$_LINUXDO_CONNECT['client_secret']);
$header = [
'Authorization: Basic '.$key
];
$post = http_build_query([
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => ''
]);
$getTokenRes = get_curl('https://connect.linux.do/oauth2/token', $post, 0, 0, $header);
$getTokenArr = json_decode($getTokenRes, true);
if (isset($getTokenArr['access_token'])) {
$access_token = $getTokenArr['access_token'];
$header = [
'Authorization: Bearer '.$access_token
];
$getUserRes = get_curl('https://connect.linux.do/api/user', 0, 0, 0, $header);
$getUserArr = json_decode($getUserRes, true);
$userInfo = []
if (isset($getUserArr['id'])) {
// 保存每个用户数据项到 session 中
$userInfo['user_id'] = $getUserArr['id'];
$userInfo['user_sub'] = $getUserArr['sub'];
$userInfo['user_username'] = $getUserArr['username'];
$userInfo['user_login'] = $getUserArr['login'];
$userInfo['user_name'] = $getUserArr['name'];
$userInfo['user_email'] = $getUserArr['email'];
$userInfo['user_avatar_template'] = $getUserArr['avatar_template'];
$userInfo['user_avatar_url'] = $getUserArr['avatar_url'];
$userInfo['user_active'] = $getUserArr['active'];
$userInfo['user_trust_level'] = $getUserArr['trust_level'];
$userInfo['user_silenced'] = $getUserArr['silenced'];
$userInfo['user_external_ids'] = $getUserArr['external_ids'] ?? 'null';
$userInfo['user_api_key'] = $getUserArr['api_key'];
}
// 判断是否已经关联
$stmt = $mySQLi->prepare('select player_id from player_connect_threeparty where type='linuxdo' and connect_id=?');
$stmt->bind_param('s', $userInfo['user_username']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
if(!empty($row)){
$getPlayer = $mySQLi->prepare('select username,password from player where id=?');
$getPlayer->bind_param('s', $row['player_id']);
$getPlayer->execute();
$res = $getPlayer->get_result();
$account = $res->fetch_array();
exit("<script language='javascript'>window.location.href='/play?account={$account['username']}&token={$account['password']}';</script>");
}
//echo json_encode($getUserArr);
} else {
echo json_encode($getTokenArr);
}
break;
}
// cURL 函数
function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobaody=0, $addheader=0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
$httpheader[] = "Connection: close";
if ($header) {
$httpheader = array_merge($httpheader, $header);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, false);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
if ($referer == 1) {
curl_setopt($ch, CURLOPT_REFERER, '');
} else {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36");
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
?>
<html>
<head>
<style>
html,body{
margin:0;
}
</style>
</head>
<body>
<?php if(empty($row)){?>
当前尚未有你的游戏账号,请选择<br>
<a href="/linudo?act=bind&connect_id=<?=$userInfo['user_username']?>">Linuxdo账号绑定</a></br>
如果已有账号,请输入账号和密码
<form action="/linudo?act=reg&connect_id=<?=$userInfo['user_username']?>">
<input type="text" id="account" placeholder="请输入账号" onKeyUp="value = value.replace(/[\W]/g, '')" autocomplete="off" disableautocomplete>
<input type="password" id="password" placeholder="请输入密码">
<button type="submit">绑定</button>
</form>
<?php }?>
</body>
</html>