1 | <?php |
||
17 | class CrossOriginRequestPlugin extends AbstractPlugin |
||
18 | { |
||
19 | /** the PHP constant for the Origin HTTP request header */ |
||
20 | const HEADER_CLIENT_ORIGIN = 'HTTP_ORIGIN'; |
||
21 | |||
22 | /** The HTTP header for allowing cross origin requests */ |
||
23 | const HEADER_ALLOW_ORIGIN = 'Access-Control-Allow-Origin'; |
||
24 | /** The HTTP header for allowing a list of headers */ |
||
25 | const HEADER_ALLOW_HEADERS = 'Access-Control-Allow-Headers'; |
||
26 | /** The HTTP header for allowing a list of methods */ |
||
27 | const HEADER_ALLOW_METHODS = 'Access-Control-Allow-Methods'; |
||
28 | /** The HTTP header for allowing credentials */ |
||
29 | const HEADER_ALLOW_CREDENTIALS = 'Access-Control-Allow-Credentials'; |
||
30 | /** The HTTP header for the max age to cache access control */ |
||
31 | const HEADER_MAX_AGE = 'Access-Control-Max-Age'; |
||
32 | |||
33 | /** the config key for the whitelist of allowed services */ |
||
34 | const CONFIG_SERVICE_WHITELIST = 'whitelist'; |
||
35 | /** the config key for the whitelist of allowed origin domains */ |
||
36 | const CONFIG_ORIGIN_WHITELIST = 'ignoreOrigins'; |
||
37 | /** the magic config option to allow all methods on a service */ |
||
38 | const CONFIG_ALL_METHODS = 'all'; |
||
39 | |||
40 | /** A constant indicating how long (in seconds) a user agent should cache |
||
41 | cross origin preflight response headers */ |
||
42 | const MAX_AGE = 86400; // 1 day |
||
43 | |||
44 | // the array of allowed headers the user agent can send in a cross origin request |
||
45 | private static $allowedHeaders = array( |
||
46 | 'accept', 'content-type' |
||
47 | ); |
||
48 | |||
49 | // the array of allowed HTTP verbs that can be used to perform cross origin requests |
||
50 | private static $allowedMethods = array( |
||
51 | 'GET', 'POST', 'OPTIONS' |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Invoked directly after the router decides which handler will be used. |
||
56 | * @param AbstractHandler $handler The handler selected by the router. |
||
57 | */ |
||
58 | 8 | public function afterHandlerSelected(AbstractHandler $handler) |
|
87 | |||
88 | /** |
||
89 | * Processes the list of requests to check if any should be blocked due |
||
90 | * to CORS policy. |
||
91 | * @param array $requests The array of requests. |
||
92 | */ |
||
93 | 6 | private function processRequestsForAccessDenial(array $requests) |
|
107 | |||
108 | /** |
||
109 | * Adds additional headers for the OPTIONS http verb. |
||
110 | */ |
||
111 | 1 | private function addHeadersForOptionsRequests() |
|
134 | |||
135 | /** |
||
136 | * Returns whether or not the current service/method combination is enabled |
||
137 | * for cross origin requests. |
||
138 | * @param string $service The service requested. |
||
139 | * @param string|null $method The method requested. |
||
140 | * @return boolean Returns true if the service/method pair is in the whitelist and |
||
141 | * false otherwise. |
||
142 | */ |
||
143 | 6 | protected function isServiceEnabledForCrossOrigin($service, $method) |
|
174 | |||
175 | /** |
||
176 | * Returns true if the current requests is a cross origin request (i.e. does |
||
177 | * the Origin HTTP header exist in the request) and false otherwise. |
||
178 | * @return boolean Returns true if the request is cross origin and false otherwise. |
||
179 | */ |
||
180 | 8 | protected function isRequestCrossOrigin() |
|
192 | } |
||
193 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: