Files
chuanqi-client-config/linuxdo.php
Kubbo 7893eade44 style(linuxdo): 删除多余的换行符
- 移除了 "如果已有账号,请输入账号和密码" 前面的多余换行符
- 优化了页面布局,提高了用户体验
2024-12-22 11:40:53 +08:00

226 lines
9.3 KiB
PHP

<?php
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 'bind':
$stmt1 = $mySQLi->prepare('insert into `player_connect_threeparty` (player_id, type, connect_id) values(?, `linuxdo`, ?)');
$stmt1->bind_param('ssisiissis', input('account'), input('connect_id'));
$stmt1->execute();
$stmt1->close();
$stmt2 = $mySQLi->prepare('select password from player where username=?');
$stmt2->bind_param('s', input('account'));
$stmt2->execute();
$result = $stmt2->get_result();
$data = $result->fetch_array();
$result->free_result();
$stmt2->close();
exit(json_encode($data));
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;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="applicable-device" content="mobile">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="full-screen" content="yes" />
<meta name="x5-fullscreen" content="true" />
<meta name="360-fullscreen" content="true" />
<meta name="screen-orientation" content="portrait">
<meta name="x5-orientation" content="portrait">
<meta name="x5-page-mode" content="app">
<title><?= $_CONFIG['game_name'] ?> <?= $_CONFIG['game_description'] ?></title>
<meta name="description" content="<?= $_CONFIG['game_name'] ?> <?= $_CONFIG['game_description'] ?>">
<meta name="keywords" content="<?= $_CONFIG['game_name'] ?>,<?= $_CONFIG['game_description'] ?>">
<link rel="stylesheet" href="static/css/login.css?v=1.1.6" />
<script>
document.onkeydown = document.onkeyup = document.onkeypress = function (event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 123) {
e.returnValue = false;
return false;
}
}
</script>
</head>
<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false">
<?php if (empty($row)) { ?>
<div style="width:400px">
当前尚未有你的游戏账号,请选择<br>
<a onclick="registerDirect" style="cursor:pointer"><b>Linuxdo账号绑定</b></a></br>
<input type="hidden" id="linuxdo" value="<?= $userInfo['user_username'] ?>">
<script>
function handleBind(account, linuxdo_account) {
return fetch("/linuxdo?act=bind&account=" + account + "&connect_id=" + linuxdo_account).then(res => {
const { password } = res.data
location.href = "/play?account=" + document.getElementById("linuxdo").value + "&token=" + password;
})
}
function processLogin(params = {}, connect_id) {
const formData = new FormData();
formData.append("serverId", "1");
Object.entries(params).forEach(([key, value]) => formData.append(key, value));
fetch("/api?act=reg", { method: "POST", body: formData }).then(res => {
if (res.code == '0') {
handleBind(res.data.account, connect_id)
} else {
showTips(res.msg, 6, 'error');
return
})
})
}
function registerDirect() {
const linuxdo_account = document.getElementById("linuxdo").value;
processLogin({ type: "1", account: linuxdo_account, password: "1", password2: "1" }, linuxdo_account)
}
function linkAccount() {
const account = document.getElementById("account").value;
const password = document.getElementById("password").value;
const linuxdo_account = document.getElementById("linuxdo").value;
processLogin({ type: "0", account, password, }, linuxdo_account)
}
</script>
<br>
如果已有账号,请输入账号和密码<br>
<input type="text" id="account" placeholder="请输入账号" onKeyUp="value = value.replace(/[\W]/g, '')"
autocomplete="off" disableautocomplete>
<br>
<input type="password" id="password" placeholder="请输入密码">
<br>
<button type="submit" @click="linkAccount">绑定并登录</button>
</div>
<?php } ?>
</body>
</html>