From ff41a14237f5415994154264835bf83cd50bdd8e Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 24 Mar 2024 23:07:21 +0900 Subject: [PATCH] Add Context::setCorsPolicy() method #2287 --- classes/context/Context.class.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/classes/context/Context.class.php b/classes/context/Context.class.php index dcec8a598..4a15ebe37 100644 --- a/classes/context/Context.class.php +++ b/classes/context/Context.class.php @@ -473,6 +473,32 @@ class Context } } + /** + * Set CORS policy for the current request. + * + * @param string $origin Origin to allow, or '*' + * @param array $methods + * @param array $headers + * @param int $max_age + * @return void + */ + public static function setCorsPolicy(string $origin = '*', array $methods = [], array $headers = [], int $max_age = 0): void + { + header('Access-Control-Allow-Origin: ' . $origin); + if (count($methods)) + { + header('Access-Control-Allow-Methods: ' . implode(', ', $methods)); + } + if (count($headers)) + { + header('Access-Control-Allow-Headers: ' . implode(', ', $headers)); + } + if ($max_age > 0) + { + header('Access-Control-Max-Age: ' . $max_age); + } + } + /** * Load the database information *