src/Controller/ApiController.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. class ApiController extends AbstractController
  9. {
  10.     private $username;
  11.     private $password;
  12.     private $headers;
  13.     public function __construct() {
  14.         $headers getallheaders();
  15.         $this->headers $headers;
  16.         if (isset($headers['Authorization'])) {
  17.             $auth str_replace('Basic ','',$headers['Authorization']);
  18.             $un_pw base64_decode($auth);
  19.             $un_pw explode(':',$un_pw);
  20.             $this->username $un_pw[0];
  21.             $this->password $un_pw[1];
  22.         } else {
  23.             $this->username null;
  24.             $this->password null;
  25.         }
  26.     }
  27.     private function pre($t,$e=false) {
  28.         echo '<pre>';
  29.         print_r($t);
  30.         echo '</pre>';
  31.         if ($e) exit;
  32.     }
  33.     private function authIsOK() {
  34.         $auth false;
  35.         $key array_search($this->password,API_USERS);
  36.         if ($key) {
  37.             if ($key == $this->username) {
  38.                 $auth true;
  39.             }
  40.         }
  41.         return $auth;
  42.     }
  43.     private function setRaceCommentsReadyForRaceId($race_id) {
  44.         $em $this->getDoctrine()->getManager();
  45.         $sql 'update races set rac_comments_ready = 1 where id = ' $race_id;
  46.         $sta $em->getConnection()->prepare($sql);
  47.         $sta->execute();
  48.         return $sql;
  49.     }
  50.     private function findRaceIdForRaceOnDateOnTrack($commoncode,$date,$raceno) {
  51.         $em $this->getDoctrine()->getManager();
  52.         $sql 'select r.id from races r
  53.                 left join meetings m on r.rac_mee_id = m.id
  54.                 left join tracks t on m.mee_tra_id = t.id
  55.                 where t.trk_commoncode = "' $commoncode '" and m.mee_date = "' $date '" and r.rac_raceno =' $raceno;
  56.         $sta $em->getConnection()->prepare($sql);
  57.         $sta->execute();
  58.         $results $sta->fetchOne();
  59.         return $results;
  60.     }
  61.     private function findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno) {
  62.         $em $this->getDoctrine()->getManager();
  63.         $sql 'select * from starts s
  64.                     left join races r on s.sta_rac_id = r.id
  65.                     left join meetings m on r.rac_mee_id = m.id
  66.                     left join tracks t on m.mee_tra_id = t.id
  67.                     where t.trk_commoncode = "' $commoncode '" and m.mee_date = "' $date '" and r.rac_raceno = ' $raceno ' and s.sta_startno = ' $startno;
  68.         $sta $em->getConnection()->prepare($sql);
  69.         $sta->execute();
  70.         $results $sta->fetchAll();
  71.         return $results;
  72.     }
  73.     private function updateStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno,$comment,$character) {
  74.         $em $this->getDoctrine()->getManager();
  75.         $sql 'update starts s
  76.                 left join races r on s.sta_rac_id = r.id
  77.                 left join meetings m on r.rac_mee_id = m.id
  78.                 left join tracks t on m.mee_tra_id = t.id
  79.                 set sta_comment = "' $comment '", sta_comment_ss = "' $comment '", sta_character = ' $character ', sta_character_ss = ' $character '
  80.                 where t.trk_commoncode = "' $commoncode '" and m.mee_date = "' $date '" and r.rac_raceno = ' $raceno ' and s.sta_startno = ' $startno;
  81.         $sta $em->getConnection()->prepare($sql);
  82.         if ($sta->execute()) return true;
  83.         else return false;
  84.     }
  85.     /**
  86.      * @Route("/", name="api_homepage")
  87.      */
  88.     public function homepage(): Response
  89.     {
  90.         return new JsonResponse(array('status' => 'error''message' => 'No valid API endpoint'));
  91.     }
  92.     /**
  93.      * @Route("/v1/postcomment", name="api_postcomment", methods={"POST"})
  94.      */
  95.     public function postcomment(Request $request): Response
  96.     {
  97.         $status 'success';
  98.         $message '';
  99.         if ($this->authIsOK()) {
  100.             if ($request->getMethod() == 'POST') {
  101.                 $p $_POST;
  102.                 if (count($p) == 6) {
  103.                     $commoncode $p['commoncode'];
  104.                     $date $p['date'];
  105.                     $raceno $p['raceno'];
  106.                     $startno $p['startno'];
  107.                     $comment $p['comment'];
  108.                     $character $p['character'];
  109.                     $race_id $this->findRaceIdForRaceOnDateOnTrack($commoncode,$date,$raceno);
  110.                     $results $this->findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno);
  111.                     if (count($results) == 1) {
  112.                         // Update comment
  113.                         $this->updateStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno,$comment,$character);
  114.                         $message 'Comment updated successfully!';
  115.                         // Set the race's "rac_comments_ready" to true
  116.                         $this->setRaceCommentsReadyForRaceId($race_id);
  117.                     } else {
  118.                         $status 'error';
  119.                         $message 'Could not find unique start for given credentials!';
  120.                     }
  121.                 } else {
  122.                     $status 'error';
  123.                     $message 'Wrong number of body parameters! [commoncode, date, raceno, startno, comment, character]';
  124.                 }
  125.             } else {
  126.                 $status 'error';
  127.                 $message 'This endpoint requires HTTP_POST!';
  128.             }
  129.         } else {
  130.             $status 'error';
  131.             $message 'Wrong username and/or password in authentication!';
  132.         }
  133.         return new JsonResponse(array('status' => $status'message' => $message ));
  134.     }
  135.     /**
  136.      * @Route("/v1/getcomment", name="api_getcomment", methods={"POST"})
  137.      */
  138.     public function getcomment(Request $request): Response
  139.     {
  140.         $status 'success';
  141.         $message '';
  142.         $response '';
  143.         if ($this->authIsOK()) {
  144.             if ($request->getMethod() == 'POST') {
  145.                 $p $_POST;
  146.                 if (count($p) == 4) {
  147.                     $commoncode $p['commoncode'];
  148.                     $date $p['date'];
  149.                     $raceno $p['raceno'];
  150.                     $startno $p['startno'];
  151.                     $results $this->findStartInRaceOnDateOnTrack($commoncode,$date,$raceno,$startno);
  152.                     if (count($results) == 1) {
  153.                         $message 'Comment found!';
  154.                         $response = ['comment' => $results[0]['sta_comment'], 'character' => $results[0]['sta_character']];
  155.                     } else {
  156.                         $status 'error';
  157.                         $message 'Could not find unique start for given credentials!';
  158.                     }
  159.                 } else {
  160.                     $status 'error';
  161.                     $message 'Wrong number of body parameters! [commoncode, date, raceno, startno]';
  162.                 }
  163.             } else {
  164.                 $status 'error';
  165.                 $message 'This endpoint requires HTTP_POST!';
  166.             }
  167.         } else {
  168.             $status 'error';
  169.             $message 'Wrong username and/or password in authentication!';
  170.         }
  171.         return new JsonResponse(array('status' => $status'message' => $message'response' => $response ));
  172.     }
  173.     /**
  174.      * @Route("/v1/test_post", name="api_test_post")
  175.      */
  176.     public function test_post(Request $request): Response
  177.     {
  178.         if ($request->getMethod() == 'POST') {
  179.             $p $_POST;
  180.             dd($p);
  181.         }
  182.         return $this->render('api/test_post.html.twig');
  183.     }
  184. }