blob: 901fb83693cc09e0a372f85f499a6fa227f2d3ba [file] [log] [blame]
<?php
/*
* Copyright 2019, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
$route = $_GET['route'];
$method = $_SERVER['REQUEST_METHOD'];
//Ensure all requests are made over https
if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== "on") {
header('HTTP/1.0 403 Forbidden');
echo 'All requests must be made over https';
die();
}
//Implements basic auth for all routes
//Be sure to change the username and password
if($_SERVER['PHP_AUTH_USER'] != 'username'
|| $_SERVER['PHP_AUTH_PW'] != 'password') {
header('WWW-Authenticate: Basic');
header('HTTP/1.0 401 Unauthorized');
die('Unauthorized');
}
require 'apiv3methods.php';
//loads the posted data of the request
$request = file_get_contents('php://input');
if($method === 'GET') {
//GET /v3/HealthCheck
if($route === 'HealthCheck') {
try {
header('Content-type:application/json');
echo healthCheck();
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
}
}
else {
//an unkown GET request
header('HTTP/1.0 404 Not Found');
echo '404<br>';
echo 'Method: ' . $method;
}
}
else if($method === 'POST') {
switch($route) {
// POST /v3/CheckAvailability
case 'CheckAvailability':
try {
header('Content-type:application/json');
echo checkAvailability($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/CreateBooking
case 'CreateBooking':
try {
header('Content-type:application/json');
echo createBooking($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
// POST /v3/UpdateBooking
case 'UpdateBooking':
try {
header('Content-type:application/json');
echo updateBooking($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/GetBookingStatus
case 'GetBookingStatus':
try {
header('Content-type:application/json');
echo getBookingStatus($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/ListBookings
case 'ListBookings':
try {
header('Content-type:application/json');
echo listBookings($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/CheckOrderFulfillability
case 'CheckOrderFulfillability':
try {
header('Content-type:application/json');
echo checkOrderFulfillability($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/CreateOrder
case 'CreateOrder':
try {
header('Content-type:application/json');
echo createOrder($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//POST /v3/ListOrders
case 'ListOrders':
try {
header('Content-type:application/json');
echo listOrders($request);
}
catch(Exception $e) {
//TO-DO: add specific error handling
echo $e->getMessage();
die();
}
break;
//An unkown post request
default:
header('HTTP/1.0 404 Not Found');
echo '404<br>';
echo 'Method: ' . $method;
}
}
//A request with a non post or get method
else {
header('HTTP/1.0 404 Not Found');
echo '404<br>';
echo 'Method: ' . $method;
}