1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zaphpa\Middleware; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ZaphpaCORS |
7
|
|
|
* @package Zaphpa\BaseMiddleware |
8
|
|
|
* |
9
|
|
|
* Usage: |
10
|
|
|
* $router->attach('ZaphpaCORS', '*') |
11
|
|
|
* ->restrict('GET', '/users') |
12
|
|
|
*/ |
13
|
|
|
class CORS extends \Zaphpa\BaseMiddleware { |
14
|
|
|
|
15
|
|
|
private $domain; |
16
|
|
|
|
17
|
|
|
function __construct($domain = '*') { |
18
|
|
|
$this->domain = $domain; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function preflight() { |
22
|
|
|
|
23
|
|
|
$allowedMethods = self::$context['http_method']; |
24
|
|
|
|
25
|
|
|
$req = new \Zaphpa\Request(); |
26
|
|
|
$res = new \Zaphpa\Response($req); |
27
|
|
|
|
28
|
|
|
$res->addHeader("Access-Control-Allow-Origin", $this->domain); |
29
|
|
|
$res->setFormat("text/plain"); |
30
|
|
|
$headers = array ( |
31
|
|
|
"Access-Control-Allow-Methods" => $allowedMethods, |
32
|
|
|
"Access-Control-Allow-Headers" => array ( |
33
|
|
|
"origin", "accept", "content-type", "authorization", |
34
|
|
|
"x-http-method-override", "x-pingother", "x-requested-with", |
35
|
|
|
"if-match", "if-modified-since", "if-none-match", "if-unmodified-since" |
36
|
|
|
), |
37
|
|
|
"Access-Control-Expose-Headers" => array ( |
38
|
|
|
"tag", "link", |
39
|
|
|
"X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset", |
40
|
|
|
"X-OAuth-Scopes", "X-Accepted-OAuth-Scopes" |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
foreach ($headers as $key => $vals) { |
45
|
|
|
$res->addHeader($key, $vals); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$res->sendHeaders(); |
49
|
|
|
exit(0); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|