Create getauthtoken.php

This commit is contained in:
Mario 2023-02-13 11:12:28 +02:00 committed by GitHub
parent d547286e9b
commit 8171f0db66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 0 deletions

44
getauthtoken.php Normal file
View File

@ -0,0 +1,44 @@
<?php
require_once 'core/classes.php';
require_once 'core/classes/user.php';
header('content-type:application/json');
ob_start();
session_start();
$user = new User($con, $_SESSION['user'] ?? 0);
if(!$user->isLoggedIn()) {
header('location: /login');
exit;
}
$placeid = $_GET['placeid'];
if (!isset($placeid)) {
echo "No placeid";
exit;
}
$username = $user->getUsername();
$id = $user->getID($con, $user->getUsername());
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
} // pls don't hurt me
$checkifuser = $con->prepare('SELECT COUNT(*) FROM games WHERE id=:placeid');
$checkifuser->bindParam(':placeid', $placeid);
$checkifuser->execute();
$momentoftruth = $checkifuser->fetchColumn();
if ($momentoftruth == 1) {
$token = generateRandomString(500);
$fbi = $con->prepare('INSERT INTO tokens (token, placeid, userid) VALUES (:token, :placeid, :userid)');
$fbi->bindParam(':token', $token);
$fbi->bindParam(':placeid', $placeid);
$fbi->bindParam(':userid', $id);
$fbi->execute();
echo $token;
} else {
echo 'No placeId found';
exit;
}
?>