You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

40 lines
1.0 KiB

  1. <?php
  2. namespace dokuwiki\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. public static 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 " . implode(' ', $values) . ';';
  29. } else {
  30. $cspheader .= " $key;";
  31. }
  32. }
  33. header($cspheader);
  34. }
  35. }