|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leaf\Http; |
| 4 | + |
| 5 | +/** |
| 6 | + * Leaf CORS Module |
| 7 | + * ------- |
| 8 | + * CORS simplified. Enable CORS with various options. |
| 9 | + * Inspired by Express js's CORS package. |
| 10 | + * |
| 11 | + * @version 1.0 |
| 12 | + * @since 3.0-beta |
| 13 | + */ |
| 14 | +class Cors |
| 15 | +{ |
| 16 | + protected static $config = []; |
| 17 | + |
| 18 | + protected static $defaultConfig = [ |
| 19 | + "origin" => "*", |
| 20 | + "methods" => "GET,HEAD,PUT,PATCH,POST,DELETE", |
| 21 | + "allowedHeaders" => "*", |
| 22 | + "exposedHeaders" => "", |
| 23 | + "credentials" => false, |
| 24 | + "maxAge" => null, |
| 25 | + "preflightContinue" => false, |
| 26 | + "optionsSuccessStatus" => 204, |
| 27 | + ]; |
| 28 | + |
| 29 | + public static function config($config = []) |
| 30 | + { |
| 31 | + static::$config = array_merge(static::$defaultConfig, $config); |
| 32 | + |
| 33 | + if (Request::getMethod() === "OPTIONS") { |
| 34 | + if (static::$config["preflightContinue"]) { |
| 35 | + // skip to code |
| 36 | + } else { |
| 37 | + static::configureOrigin(); |
| 38 | + static::configureHeaders(); |
| 39 | + static::configureExposedHeaders(); |
| 40 | + static::configureMaxAge(); |
| 41 | + static::configureCredentials(); |
| 42 | + static::configureMethods(); |
| 43 | + |
| 44 | + Response::throwErr( |
| 45 | + "", |
| 46 | + static::$config["optionsSuccessStatus"] |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected static function configureMethods() |
| 53 | + { |
| 54 | + if (is_array(static::$config["methods"])) { |
| 55 | + static::$config["methods"] = implode(",", static::$config["methods"]); |
| 56 | + } |
| 57 | + |
| 58 | + Headers::accessControl("Allow-Methods", static::$config["methods"]); |
| 59 | + } |
| 60 | + |
| 61 | + protected static function configureOrigin() |
| 62 | + { |
| 63 | + $origin = static::$config["origin"]; |
| 64 | + |
| 65 | + // Safari (and potentially other browsers) need content-length 0, |
| 66 | + // for 204 or they just hang waiting for a body |
| 67 | + Headers::set("Content-Length", "0"); |
| 68 | + Headers::accessControl( |
| 69 | + "Allow-Origin", |
| 70 | + static::isOriginAllowed($origin) ? $_SERVER['HTTP_ORIGIN'] : false |
| 71 | + ); |
| 72 | + |
| 73 | + if ($origin !== "*") { |
| 74 | + Headers::set("Vary", "Origin"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + protected static function configureHeaders() |
| 79 | + { |
| 80 | + $headers = static::$config["allowedHeaders"]; |
| 81 | + |
| 82 | + if (!$headers) { |
| 83 | + // .headers wasn't specified, so reflect the request headers |
| 84 | + $headers = Headers::get("access-control-request-headers"); |
| 85 | + Headers::set("Vary", "Access-Control-Request-Headers"); |
| 86 | + } |
| 87 | + |
| 88 | + if ($headers) { |
| 89 | + Headers::accessControl( |
| 90 | + "Allow-Headers", |
| 91 | + is_array($headers) ? implode(", ", $headers) : (strlen($headers) ? $headers : "*") |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + protected static function configureExposedHeaders() |
| 97 | + { |
| 98 | + $headers = static::$config["exposedHeaders"]; |
| 99 | + |
| 100 | + if ($headers) { |
| 101 | + Headers::accessControl( |
| 102 | + "Expose-Headers", |
| 103 | + is_array($headers) ? implode(", ", $headers) : $headers |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected static function configureMaxAge() |
| 109 | + { |
| 110 | + if (is_int(static::$config["maxAge"])) { |
| 111 | + Headers::accessControl([ |
| 112 | + "Max-Age" => static::$config["maxAge"], |
| 113 | + ]); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + protected static function configureCredentials() |
| 118 | + { |
| 119 | + if (static::$config["credentials"] === true) { |
| 120 | + Headers::accessControl("Allow-Credentials", "true"); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected static function isOriginAllowed($allowedOrigin) |
| 125 | + { |
| 126 | + $origin = $_SERVER['HTTP_ORIGIN']; |
| 127 | + |
| 128 | + if (is_array($allowedOrigin)) { |
| 129 | + for ($i = 0; $i < count($allowedOrigin); $i++) { |
| 130 | + if (static::isOriginAllowed($origin, $allowedOrigin[$i])) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | + } else if (is_string($allowedOrigin)) { |
| 137 | + return $origin === $allowedOrigin; |
| 138 | + } else if (@preg_match($allowedOrigin, null) === false) { |
| 139 | + return !!preg_match($allowedOrigin, $origin); |
| 140 | + } else { |
| 141 | + return !!$allowedOrigin; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments