File: //home/awaldron/_phpassport/api.pennhillspassport.com/index.php
<?php
error_reporting(0);
//date_default_timezone_set('America/New_York');
// Access-Control headers are received during OPTIONS requests
if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, PUT, HEAD, DELETE, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
require 'vendor/autoload.php';
require_once 'v1/_globals.php';
define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
define('ENV', 'production');
define('DISPLAY_ERRORS', TRUE);
define('ERROR_REPORTING', E_ALL & ~E_NOTICE);
define('LOG_ERRORS', FALSE);
register_shutdown_function('shut');
set_error_handler('handler');
ob_start();
// INCLUDE OUR ENDPOINT FILES
require_once 'v1/onesignal.php';
require_once 'v1/cron_news.php';
require_once 'v1/cron_events.php';
require_once 'v1/auth.php';
require_once 'v1/news.php';
require_once 'v1/events.php';
require_once 'v1/directory.php';
require_once 'v1/realestate.php';
require_once 'v1/stats.php';
require_once 'v1/settings.php';
// SET CORS POLICY
//cors();
// PARSE THE URL AND DATA
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', substr($_SERVER['REQUEST_URI'],1));
if ($request[0] && $request[1] && $request[2]) {
$function = strtolower($method.'_'.$request[0].'_'.$request[1].'_'.$request[2]);
} else {
$function = 'notfound404';
}
// remove the first 3 items from the request
array_shift($request);
array_shift($request);
array_shift($request);
// format the post body
$body = [];
$postBody = file_get_contents('php://input');
if ( strlen($postBody) > 0 ) {
$body = json_decode( $postBody, true );
}
// Connect to the database
$db = dbconnect();
// Check the token
$token = array_key_exists('HTTP_AUTHORIZATION', $_SERVER) ? $_SERVER['HTTP_AUTHORIZATION'] :
(array_key_exists('REDIRECT_HTTP_AUTHORIZATION', $_SERVER) ? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] : 0);
$auth_user = !empty($token) ? check_token( $db, $token ) : 0;
// Check if user is an Admin
$admin = 0;
if (!empty($auth_user)) {
$admin = qVal($db, "SELECT user_id FROM users WHERE admin='100' AND user_id=".$auth_user);
}
// Call the endpoint
if (function_exists($function)) {
call_user_func($function, $request, $body, $auth_user, !!$admin, $db);
} else {
response( 404, ['success'=>false,'error'=>'Endpoint not found.'] );
}
ob_end_flush();