1
|
|
|
<?php |
2
|
|
|
namespace Http\Rest; |
3
|
|
|
|
4
|
|
|
use \Psr\Http\Message\ServerRequestInterface as Request; |
5
|
|
|
use \Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
|
7
|
|
|
require 'vendor/autoload.php'; |
8
|
|
|
|
9
|
|
|
class CORSMiddleware |
10
|
|
|
{ |
11
|
|
|
protected $container; |
12
|
|
|
protected $allowedOrigins; |
13
|
|
|
|
14
|
|
|
public function __construct($c) |
15
|
|
|
{ |
16
|
|
|
$settings = \Settings::getInstance(); |
17
|
|
|
$this->container = $c; |
18
|
|
|
$this->allowedOrigins = array( |
19
|
|
|
$settings->getGlobalSetting('www_url', 'https://www.burningflipside.com'), |
20
|
|
|
$settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com'), |
21
|
|
|
$settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com'), |
22
|
|
|
$settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com') |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __invoke($request, $response, $next) |
27
|
|
|
{ |
28
|
|
|
$route = $request->getAttribute("route"); |
29
|
|
|
$methods = []; |
30
|
|
|
|
31
|
|
|
if(!empty($route)) |
32
|
|
|
{ |
33
|
|
|
$pattern = $route->getPattern(); |
34
|
|
|
foreach($this->container->router->getRoutes() as $route) |
35
|
|
|
{ |
36
|
|
|
if($pattern === $route->getPattern()) |
37
|
|
|
{ |
38
|
|
|
$methods = array_merge_recursive($methods, $route->getMethods()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
else |
43
|
|
|
{ |
44
|
|
|
array_push($methods, $request->getMethod()); |
45
|
|
|
} |
46
|
|
|
$response = $next($request, $response); |
47
|
|
|
$origin = $request->getHeaderLine('origin'); |
48
|
|
|
if($origin === '') |
49
|
|
|
{ |
50
|
|
|
return $response; |
51
|
|
|
} |
52
|
|
|
if(in_array($origin, $this->allowedOrigins)) |
53
|
|
|
{ |
54
|
|
|
$response = $response->withHeader('Access-Control-Allow-Origin', $origin); |
55
|
|
|
$response = $response->withHeader('Access-Control-Allow-Credentials', 'true'); |
56
|
|
|
} |
57
|
|
|
$response = $response->withHeader('Access-Control-Allow-Headers', 'Authorization,Cookie,apikey'); |
58
|
|
|
return $response->withHeader("Access-Control-Allow-Methods", implode(",", $methods)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|