<?php
ob_start();

$profile  = getenv("SAVVY_PROFILE");
if ($profile) {
  require_once("../../core/backend.${profile}.php");
} else {
  require_once('../../core/backend.php');
}

include_once '../config.php';
require_once('../constants.php');

require_once('../common/vendor/autoload.php');
require_once('../common/Api.php');
require_once('../common/Db.php');
require_once('../common/Gis.php');
require_once('../common/Utilities.php');
require_once('../common/Logger.php');

require_once('Activity.php');
require_once('ActivityApi.php');

require_once('ActivityDirect.php');
require_once('ActivityDirectApi.php');

require_once('Address.php');
require_once('AddressApi.php');

require_once('ByCategory.php');
require_once('ByCategoryApi.php');

require_once('Compare.php');
require_once('CompareApi.php');

require_once('Geocode.php');
require_once('GeocodeApi.php');

require_once('Geofence.php');
require_once('GeofenceApi.php');

require_once('MultiModal.php');
require_once('MultiModalFilter.php');
require_once('MultiModalApi.php');

require_once('Populardirect.php');
require_once('PopulardirectApi.php');

require_once('Popular.php');
require_once('PopularApi.php');

require_once('Quotes.php');
require_once('QuoteAPI.php');

require_once('QuoteGroup.php');
require_once('QuoteGroupApi.php');

require_once('Radius.php');
require_once('RadiusApi.php');

require_once('Report.php');
require_once('ReportApi.php');

require_once('Scooter.php');
require_once('ScooterApi.php');

require_once('TripInsights.php');
require_once('TripInsightsApi.php');

require_once('Trips.php');
require_once('TripsApi.php');
require_once('TripOptionsApi.php');

require_once('RouteApi.php');

$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
$scooterAuthToken = $savvyext->cfgReadChar('system.scooter_token');

ob_end_clean();

header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, client_id");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header('Content-type: application/json');

if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
    exit();
}

$headers = []; foreach (getallheaders() as $key=>$val) { $headers[strtolower($key)] = $val; } 
if ((!isset($headers["Authorization"]) || substr($headers["Authorization"],-strlen($httpAuthToken))!=$httpAuthToken) &&
    (!isset($headers["authorization"]) || substr($headers["authorization"],-strlen($httpAuthToken))!=$httpAuthToken)) {
    header('HTTP/1.1 401 Unauthorized');
    header('Status: 401 Unauthorized');
    echo "{\"status\":\"Missing authorization\"}";
    exit();
}

openlog("TripsApi", LOG_PID | LOG_PERROR, LOG_LOCAL0);

ob_start();

try {
	if (strpos($_SERVER['REQUEST_URI'],'/api/')===false) {
		throw new Exception("Invalid API request");
	}
	$requestUri = explode('/', trim($_SERVER['REQUEST_URI'],'/'));
	while (array_shift($requestUri) !== 'api') {
	};
	if ($requestUri[0]=='activity') {
		$api = new ActivityApi($requestUri);
	} 
	else if ($requestUri[0]=='activitydirect') {
		$api = new ActivityDirectApi($requestUri);
	}
	else if ($requestUri[0]=='address') {
		$api = new AddressApi($requestUri);
	}
	else if ($requestUri[0]=='bycategory') {
		$api = new ByCategoryApi($requestUri);
	}
	else if ($requestUri[0]=='compare') {
		$api = new CompareApi($requestUri);
	}
	else if ($requestUri[0]=='geofence') {
		$api = new GeofenceApi($requestUri);
	}
	else if ($requestUri[0]=='geocode') {
		$api = new GeocodeApi($requestUri);
	}
	else if ($requestUri[0]=='multimodal') {
		$api = new MultiModalApi($requestUri);
	}
	else if ($requestUri[0]=='popular') {
		$api = new PopularApi($requestUri);
	}
	else if ($requestUri[0]=='populardirect') {
		$api = new PopulardirectApi($requestUri);
	}
	else if ($requestUri[0]=='quote') {
		$api = new QuoteApi($requestUri);
	}
	else if ($requestUri[0]=='quotegroup') {
		$api = new QuoteGroupApi($requestUri);
	}
	else if ($requestUri[0]=='radius') {
		$api = new RadiusApi($requestUri);
	}	
	else if ($requestUri[0]=='report') {
		$api = new ReportApi($requestUri);
	}	
	else if ($requestUri[0]=='scooter') {
		$api = new ScooterApi($requestUri);
	}	
	else if ($requestUri[0]=='tripinsights') {
		$api = new TripInsightsApi($requestUri);
	}
	else if ($requestUri[0]=='trips') {
		$api = new TripsApi($requestUri);
	}
	else if ($requestUri[0]=='tripoptions') {
		$api = new TripOptionsApi($requestUri);
	}
	else if ($requestUri[0]=='route') {
		$api = new RouteApi($requestUri);
	}
	else {
		ob_end_clean();
		echo json_encode(Array('error' => 'Invalid API request'));
		exit();
	}
	$result = $api->run();
	ob_end_clean();
	echo $result;
}
catch (Exception $e) {
	ob_end_clean();
	echo json_encode(Array('error' => $e->getMessage()));
}

closelog();
