Files
ritzenbergen-backend/bulitipp/tippeintragen.php

67 lines
2.3 KiB
PHP
Executable File

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Authorization");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
require '../vendor/autoload.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
include("jwtsecret.php");
include("../../mysqlverbinden.php");
include("../rowforeach.php");
include("inc.php");
$headers = getallheaders();
if(isset($headers['Authorization'])) {
$authHeader = $headers['Authorization'];
if (preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
$token = $matches[1];
} else {
die("Invalid Authorization header format");
}
} else {
die("Authorization header not found");
}
$payload = JWT::decode($token, new Key($secret, 'HS256'));
$userid= $payload->id;
if($payload->exp < time()){
die("Token abgelaufen");
}
if(!isset($_GET["spieltag"])) die("GET spieltag fehlt");
$spieltag = $_GET["spieltag"];
if(!isset($_GET["tipps"])) die("GET tipps fehlt");
$tipps = json_decode($_GET["tipps"],true);
if(deadline($spieltag)){
die("deadline überschritten");
}
$tippids=[];
foreach($tipps as $tipp){
$paarungsid = $tipp['paarung']["id"];
$score1 = $tipp['heim'];
$score2 = $tipp['gast'];
$update=!mysqli_execute_query($db_id,"SELECT COUNT(*) AS anzahl FROM `buli-tipps` WHERE `spieltag`=? AND user=?;",[$spieltag,$userid])->fetch_assoc()["anzahl"]==0;
if(!$update){
mysqli_execute_query($db_id,"INSERT INTO `buli-tipp` (`spieltag`,`paarung`,`score1`,`score2`) VALUES (?,?,?,?);",[$spieltag,$paarungsid,$score1,$score2]);
$tippids[] = mysqli_insert_id($db_id);
}
else
mysqli_execute_query($db_id,"UPDATE `buli-tipp` AS bt JOIN `buli-tipps` AS bts ON bt.id IN (bts.tipp1, bts.tipp2, bts.tipp3, bts.tipp4, bts.tipp5, bts.tipp6, bts.tipp7, bts.tipp8, bts.tipp9) SET bt.score1=?, bt.score2=? WHERE bt.spieltag=? AND bt.paarung=? AND bts.user=?;",[$score1,$score2,$spieltag,$paarungsid,$userid]);
}
array_push($tippids, $spieltag);
array_push($tippids, $payload->id);
if(!$update) mysqli_execute_query($db_id,"INSERT INTO `buli-tipps` (`tipp1`, `tipp2`, `tipp3`, `tipp4`, `tipp5`, `tipp6`, `tipp7`, `tipp8`, `tipp9`, `spieltag`, `user`) VALUES (?,?,?,?,?,?,?,?,?,?,?);",$tippids);