Webpage for Games
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.

48 lines
1.4 KiB

<?php
include_once "mysql_connect.php";
$conn = new MySQLConnection();
$sql = "SELECT value FROM api_tokens WHERE name IN (
'uniliga_teams_toor_api_key',
'uniliga_teams_toor_client_id',
'uniliga_teams_toor_client_secret'
)";
$result = $conn->query($sql);
$apiKey = $result->fetch_row()[0];
$clientId = $result->fetch_row()[0];
$clientSecret = $result->fetch_row()[0];
$scopes = "organizer:view organizer:result organizer:participant";
echo "Secrets fetched from database\r\n";
$opts = [
"http" => [
"method" => "POST",
"header" => "X-Api-Key: $apiKey\r\nContent-type: application/x-www-form-urlencoded",
"content" => http_build_query([
"grant_type" => "client_credentials",
"client_id" => $clientId,
"client_secret" => $clientSecret,
"scope" => $scopes
])
]
];
$context = stream_context_create($opts);
$result = file_get_contents("https://api.toornament.com/oauth/v2/token", false, $context);
$token = json_decode($result)->access_token;
echo "Token received\r\n";
$tokenName = "uniliga_teams_toor_access_token";
$sql = "SELECT EXISTS(SELECT * FROM api_tokens WHERE name = '$tokenName')";
$exists = $conn->query($sql)->fetch_row()[0];
if ($exists == 1) {
$conn->query("UPDATE api_tokens SET value = '$token' WHERE name = '$tokenName'");
echo "Old token replaced\r\n";
} else {
$conn->query("INSERT INTO api_tokens (name, value) VALUES ('$tokenName', '$token')");
echo "New token inserted\r\n";
}