<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
class ApiController extends AbstractController
{
private $username;
private $password;
private $headers;
public function __construct() {
$headers = getallheaders();
$this->headers = $headers;
if (isset($headers['Authorization'])) {
$auth = str_replace('Basic ','',$headers['Authorization']);
$un_pw = base64_decode($auth);
$un_pw = explode(':',$un_pw);
$this->username = $un_pw[0];
$this->password = $un_pw[1];
} else {
$this->username = null;
$this->password = null;
}
}
private function pre($t,$e=false) {
echo '<pre>';
print_r($t);
echo '</pre>';
if ($e) exit;
}
private function authIsOK() {
$auth = false;
$key = array_search($this->password,API_USERS);
if ($key) {
if ($key == $this->username) {
$auth = true;
}
}
return $auth;
}
private function setRaceCommentsReadyForRaceId($race_id) {
$em = $this->getDoctrine()->getManager();
$sql = 'update races set rac_comments_ready = 1 where id = ' . $race_id;
$sta = $em->getConnection()->prepare($sql);
$sta->execute();
return $sql;
}
private function findRaceIdForRaceOnDateOnTrack($commoncode,$date,$raceno) {
$em = $this->getDoctrine()->getManager();
$sql = 'select r.id from races r
left join meetings m on r.rac_mee_id = m.id
left join tracks t on m.mee_tra_id = t.id
where t.trk_commoncode = "' . $commoncode . '" and m.mee_date = "' . $date . '" and r.rac_raceno =' . $raceno;
$sta = $em->getConnection()->prepare($sql);
$sta->execute();
$results = $sta->fetchOne();
return $results;
}
private function findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno) {
$em = $this->getDoctrine()->getManager();
$sql = 'select * from starts s
left join races r on s.sta_rac_id = r.id
left join meetings m on r.rac_mee_id = m.id
left join tracks t on m.mee_tra_id = t.id
where t.trk_commoncode = "' . $commoncode . '" and m.mee_date = "' . $date . '" and r.rac_raceno = ' . $raceno . ' and s.sta_startno = ' . $startno;
$sta = $em->getConnection()->prepare($sql);
$sta->execute();
$results = $sta->fetchAll();
return $results;
}
private function updateStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno,$comment,$character) {
$em = $this->getDoctrine()->getManager();
$sql = 'update starts s
left join races r on s.sta_rac_id = r.id
left join meetings m on r.rac_mee_id = m.id
left join tracks t on m.mee_tra_id = t.id
set sta_comment = "' . $comment . '", sta_comment_ss = "' . $comment . '", sta_character = ' . $character . ', sta_character_ss = ' . $character . '
where t.trk_commoncode = "' . $commoncode . '" and m.mee_date = "' . $date . '" and r.rac_raceno = ' . $raceno . ' and s.sta_startno = ' . $startno;
$sta = $em->getConnection()->prepare($sql);
if ($sta->execute()) return true;
else return false;
}
/**
* @Route("/", name="api_homepage")
*/
public function homepage(): Response
{
return new JsonResponse(array('status' => 'error', 'message' => 'No valid API endpoint'));
}
/**
* @Route("/v1/postcomment", name="api_postcomment", methods={"POST"})
*/
public function postcomment(Request $request): Response
{
$status = 'success';
$message = '';
if ($this->authIsOK()) {
if ($request->getMethod() == 'POST') {
$p = $_POST;
if (count($p) == 6) {
$commoncode = $p['commoncode'];
$date = $p['date'];
$raceno = $p['raceno'];
$startno = $p['startno'];
$comment = $p['comment'];
$character = $p['character'];
$race_id = $this->findRaceIdForRaceOnDateOnTrack($commoncode,$date,$raceno);
$results = $this->findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno);
if (count($results) == 1) {
// Update comment
$this->updateStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno,$comment,$character);
$message = 'Comment updated successfully!';
// Set the race's "rac_comments_ready" to true
$this->setRaceCommentsReadyForRaceId($race_id);
} else {
$status = 'error';
$message = 'Could not find unique start for given credentials!';
}
} else {
$status = 'error';
$message = 'Wrong number of body parameters! [commoncode, date, raceno, startno, comment, character]';
}
} else {
$status = 'error';
$message = 'This endpoint requires HTTP_POST!';
}
} else {
$status = 'error';
$message = 'Wrong username and/or password in authentication!';
}
return new JsonResponse(array('status' => $status, 'message' => $message ));
}
/**
* @Route("/v1/getcomment", name="api_getcomment", methods={"POST"})
*/
public function getcomment(Request $request): Response
{
$status = 'success';
$message = '';
$response = '';
if ($this->authIsOK()) {
if ($request->getMethod() == 'POST') {
$p = $_POST;
if (count($p) == 4) {
$commoncode = $p['commoncode'];
$date = $p['date'];
$raceno = $p['raceno'];
$startno = $p['startno'];
$results = $this->findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno);
if (count($results) == 1) {
$message = 'Comment found!';
$response = ['comment' => $results[0]['sta_comment'], 'character' => $results[0]['sta_character']];
} else {
$status = 'error';
$message = 'Could not find unique start for given credentials!';
}
} else {
$status = 'error';
$message = 'Wrong number of body parameters! [commoncode, date, raceno, startno]';
}
} else {
$status = 'error';
$message = 'This endpoint requires HTTP_POST!';
}
} else {
$status = 'error';
$message = 'Wrong username and/or password in authentication!';
}
return new JsonResponse(array('status' => $status, 'message' => $message, 'response' => $response ));
}
/**
* @Route("/v1/test_post", name="api_test_post")
*/
public function test_post(Request $request): Response
{
if ($request->getMethod() == 'POST') {
$p = $_POST;
dd($p);
}
return $this->render('api/test_post.html.twig');
}
}