. namespace hoplite\http; require_once HOPLITE_ROOT . '/http/interceptor.php'; require_once HOPLITE_ROOT . '/http/response_code.php'; class CorsOptionsInterceptor implements Interceptor { private $allowed_origins = []; public function __construct($allowed_origins = []) { $this->allowed_origins = $allowed_origins; } public function DoIntercept(FrontController $controller, Action $action = NULL, Request $request, Response $response) { if ($action === NULL) { return; } // If a CORS pre-flight is in process, interrupt the action flow and // permit the request. if ($request->http_method == 'OPTIONS' && isset($request->data['_SERVER']['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { if (in_array($request->data['_SERVER']['HTTP_ORIGIN'], $this->allowed_origins)) { $controller->SendResponseCode(ResponseCode::OK); } else { $controller->SendResponseCode(ResponseCode::FORBIDDEN); return; } } } }