-
Notifications
You must be signed in to change notification settings - Fork 17
/
CorsSlim.php
129 lines (109 loc) · 4.18 KB
/
CorsSlim.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace CorsSlim;
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
class CorsSlim extends \Slim\Middleware {
protected $settings;
public function __construct($settings = array()) {
$this->settings = array_merge(array(
'origin' => '*', // Wide Open!
'allowMethods' => 'GET,HEAD,PUT,POST,DELETE'
), $settings);
}
protected function setOrigin($req, $rsp) {
$origin = $this->settings['origin'];
if (is_callable($origin)) {
// Call origin callback with request origin
$origin = call_user_func($origin,
$req->headers->get("Origin")
);
}
// handle multiple allowed origins
if(is_array($origin)) {
$allowedOrigins = $origin;
// default to the first allowed origin
$origin = reset($allowedOrigins);
// but use a specific origin if there is a match
foreach($allowedOrigins as $allowedOrigin) {
if($allowedOrigin === $req->headers->get("Origin")) {
$origin = $allowedOrigin;
break;
}
}
}
$rsp->headers->set('Access-Control-Allow-Origin', $origin);
}
protected function setExposeHeaders($req, $rsp) {
if (isset($this->settings['exposeHeaders'])) {
$exposeHeaders = $this->settings['exposeHeaders'];
if (is_array($exposeHeaders)) {
$exposeHeaders = implode(", ", $exposeHeaders);
}
$rsp->headers->set('Access-Control-Expose-Headers', $exposeHeaders);
}
}
protected function setMaxAge($req, $rsp) {
if (isset($this->settings['maxAge'])) {
$rsp->headers->set('Access-Control-Max-Age', $this->settings['maxAge']);
}
}
protected function setAllowCredentials($req, $rsp) {
if (isset($this->settings['allowCredentials']) && $this->settings['allowCredentials'] === True) {
$rsp->headers->set('Access-Control-Allow-Credentials', 'true');
}
}
protected function setAllowMethods($req, $rsp) {
if (isset($this->settings['allowMethods'])) {
$allowMethods = $this->settings['allowMethods'];
if (is_array($allowMethods)) {
$allowMethods = implode(", ", $allowMethods);
}
$rsp->headers->set('Access-Control-Allow-Methods', $allowMethods);
}
}
protected function setAllowHeaders($req, $rsp) {
if (isset($this->settings['allowHeaders'])) {
$allowHeaders = $this->settings['allowHeaders'];
if (is_array($allowHeaders)) {
$allowHeaders = implode(", ", $allowHeaders);
}
}
else { // Otherwise, use request headers
$allowHeaders = $req->headers->get("Access-Control-Request-Headers");
}
if (isset($allowHeaders)) {
$rsp->headers->set('Access-Control-Allow-Headers', $allowHeaders);
}
}
protected function setCorsHeaders($app) {
$req = $app->request();
$rsp = $app->response();
// http://www.html5rocks.com/static/images/cors_server_flowchart.png
// Pre-flight
if ($app->request->isOptions()) {
$this->setOrigin($req, $rsp);
$this->setMaxAge($req, $rsp);
$this->setAllowCredentials($req, $rsp);
$this->setAllowMethods($req, $rsp);
$this->setAllowHeaders($req, $rsp);
}
else {
$this->setOrigin($req, $rsp);
$this->setExposeHeaders($req, $rsp);
$this->setAllowCredentials($req, $rsp);
}
}
public function call() {
$this->setCorsHeaders($this->app);
if(!$this->app->request->isOptions()) {
$this->next->call();
}
}
public static function routeMiddleware($settings = array()) {
$cors = new CorsSlim($settings);
return function() use ($cors, $settings) {
$app = (array_key_exists('appName', $settings)) ? \Slim\Slim::getInstance($settings['appName']) : \Slim\Slim::getInstance();
$cors->setCorsHeaders($app);
};
}
}
?>