Quick patch for CORS check while in init state

beta
Ro 3 years ago
commit 3c3f2a0881

@ -2,50 +2,55 @@
class handleCors class handleCors
{ {
public function __construct() public function __construct()
{ {
//check settings to see if external api access is allowed //look to see if settings file exists. kinda important
$config = new Settings(); if (file_exists("../config/settings.json")) {
$settings = $config->getSettings(); //check settings to see if external api access is allowed
if ($settings["global"]["externalAPI"]) { $config = new Settings();
//echo "API STATUS: " . $settings["global"]["externalAPI"]; $settings = $config->getSettings();
if ($settings["global"]["externalAPI"] == "true") { if ($settings["global"]["externalAPI"]) {
//echo "API ACCESS ACTIVE"; //echo "API STATUS: " . $settings["global"]["externalAPI"];
// checks to see if origin is set if ($settings["global"]["externalAPI"] == "true") {
if (isset($_SERVER["HTTP_ORIGIN"])) { //echo "API ACCESS ACTIVE";
// You can decide if the origin in $_SERVER['HTTP_ORIGIN'] is something you want to allow, or as we do here, just allow all // checks to see if origin is set
header("Access-Control-Allow-Origin: {$_SERVER["HTTP_ORIGIN"]}"); if (isset($_SERVER["HTTP_ORIGIN"])) {
} else { // You can decide if the origin in $_SERVER['HTTP_ORIGIN'] is something you want to allow, or as we do here, just allow all
//No HTTP_ORIGIN set, so we allow any. You can disallow if needed here header("Access-Control-Allow-Origin: {$_SERVER["HTTP_ORIGIN"]}");
//never allow just any domain, so turn CORS off if no No HTTP_ORIGIN is set } else {
//header("Access-Control-Allow-Origin: *"); //No HTTP_ORIGIN set, so we allow any. You can disallow if needed here
} //never allow just any domain, so turn CORS off if no No HTTP_ORIGIN is set
//header("Access-Control-Allow-Origin: *");
}
header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 600"); // cache for 10 minutes header("Access-Control-Max-Age: 600"); // cache for 10 minutes
if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") { if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") {
if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"])) { if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"])) {
header( header(
"Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT" "Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT"
); );
} //Make sure you remove those you do not want to support } //Make sure you remove those you do not want to support
if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"])) { if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"])) {
header( header(
"Access-Control-Allow-Headers: {$_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]}" "Access-Control-Allow-Headers: {$_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]}"
); );
} }
//Just exit with 200 OK with the above headers for OPTIONS method //Just exit with 200 OK with the above headers for OPTIONS method
exit(0); exit(0);
}
} else {
//echo "API ACCESS ACTIVE";
}
} else {
//value doesn't exist, so whatevs
//echo "API ACCESS VALUE NOT PRESENT";
}
} else {
//init state, so chill
} }
} else {
//echo "API ACCESS ACTIVE";
}
} else {
//value doesn't exist, so whatevs
//echo "API ACCESS VALUE NOT PRESENT";
} }
}
} }

Loading…
Cancel
Save