Headers.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace dokuwiki\plugin\upgrade\HTTP;
  3. /**
  4. * Utilities to send HTTP Headers
  5. */
  6. class Headers
  7. {
  8. /**
  9. * Send a Content-Security-Polica Header
  10. *
  11. * Expects an associative array with individual policies and their values
  12. *
  13. * @param array $policy
  14. */
  15. static public function contentSecurityPolicy($policy)
  16. {
  17. foreach ($policy as $key => $values) {
  18. // if the value is not an array, we also accept newline terminated strings
  19. if (!is_array($values)) $values = explode("\n", $values);
  20. $values = array_map('trim', $values);
  21. $values = array_unique($values);
  22. $values = array_filter($values);
  23. $policy[$key] = $values;
  24. }
  25. $cspheader = 'Content-Security-Policy:';
  26. foreach ($policy as $key => $values) {
  27. if ($values) {
  28. $cspheader .= " $key " . join(' ', $values) . ';';
  29. } else {
  30. $cspheader .= " $key;";
  31. }
  32. }
  33. header($cspheader);
  34. }
  35. }