CorsHeader.php 922 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. use \Psr\Http\Message\{
  3. ServerRequestInterface,
  4. ResponseInterface
  5. };
  6. class CorsHeader {
  7. public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) {
  8. if(isset($_SERVER['HTTP_ORIGIN'])) {
  9. $origin = $_SERVER['HTTP_ORIGIN'];
  10. if(in_array($origin, ['http://localhost:8080', 'https://vv.ecconia.de'])) {
  11. header('Access-Control-Allow-Origin: ' . $origin);
  12. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  13. header('Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range');
  14. if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  15. header('Access-Control-Max-Age: 1728000');
  16. header('Content-Type: text/plain charset=UTF-8');
  17. header('Content-Length: 0');
  18. return $response->withStatus(204);
  19. }
  20. }
  21. }
  22. return $next($request, $response);
  23. }
  24. }