]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace PicoFarad\Response; | |
4 | ||
5 | ||
6 | function force_download($filename) | |
7 | { | |
8 | header('Content-Disposition: attachment; filename="'.$filename.'"'); | |
9 | } | |
10 | ||
11 | ||
12 | function content_type($mimetype) | |
13 | { | |
14 | header('Content-Type: '.$mimetype); | |
15 | } | |
16 | ||
17 | ||
18 | function status($status_code) | |
19 | { | |
20 | $sapi_name = php_sapi_name(); | |
21 | ||
22 | if (strpos($sapi_name, 'apache') !== false || $sapi_name === 'cli-server') { | |
23 | header('HTTP/1.0 '.$status_code); | |
24 | } | |
25 | else { | |
26 | header('Status: '.$status_code); | |
27 | } | |
28 | } | |
29 | ||
30 | ||
31 | function redirect($url) | |
32 | { | |
33 | header('Location: '.$url); | |
34 | exit; | |
35 | } | |
36 | ||
37 | ||
38 | function json(array $data, $status_code = 200) | |
39 | { | |
40 | status($status_code); | |
41 | ||
42 | header('Content-Type: application/json'); | |
43 | echo json_encode($data); | |
44 | ||
45 | exit; | |
46 | } | |
47 | ||
48 | ||
49 | function text($data, $status_code = 200) | |
50 | { | |
51 | status($status_code); | |
52 | ||
53 | header('Content-Type: text/plain; charset=utf-8'); | |
54 | echo $data; | |
55 | ||
56 | exit; | |
57 | } | |
58 | ||
59 | ||
60 | function html($data, $status_code = 200) | |
61 | { | |
62 | status($status_code); | |
63 | ||
64 | header('Content-Type: text/html; charset=utf-8'); | |
65 | echo $data; | |
66 | ||
67 | exit; | |
68 | } | |
69 | ||
70 | ||
71 | function xml($data, $status_code = 200) | |
72 | { | |
73 | status($status_code); | |
74 | ||
75 | header('Content-Type: text/xml; charset=utf-8'); | |
76 | echo $data; | |
77 | ||
78 | exit; | |
79 | } | |
80 | ||
81 | ||
82 | function js($data, $status_code = 200) | |
83 | { | |
84 | status($status_code); | |
85 | ||
86 | header('Content-Type: text/javascript; charset=utf-8'); | |
87 | echo $data; | |
88 | ||
89 | exit; | |
90 | } | |
91 | ||
92 | ||
93 | function binary($data, $status_code = 200) | |
94 | { | |
95 | status($status_code); | |
96 | ||
97 | header('Content-Transfer-Encoding: binary'); | |
98 | header('Content-Type: application/octet-stream'); | |
99 | echo $data; | |
100 | ||
101 | exit; | |
102 | } | |
103 | ||
104 | ||
105 | function csp(array $policies = array()) | |
106 | { | |
107 | $policies['default-src'] = "'self'"; | |
108 | $values = ''; | |
109 | ||
110 | foreach ($policies as $policy => $hosts) { | |
111 | ||
112 | if (is_array($hosts)) { | |
113 | ||
114 | $acl = ''; | |
115 | ||
116 | foreach ($hosts as &$host) { | |
117 | ||
118 | if ($host === '*' || $host === 'self' || strpos($host, 'http') === 0) { | |
119 | $acl .= $host.' '; | |
120 | } | |
121 | } | |
122 | } | |
123 | else { | |
124 | ||
125 | $acl = $hosts; | |
126 | } | |
127 | ||
128 | $values .= $policy.' '.trim($acl).'; '; | |
129 | } | |
130 | ||
131 | header('Content-Security-Policy: '.$values); | |
132 | } | |
133 | ||
134 | ||
135 | function nosniff() | |
136 | { | |
137 | header('X-Content-Type-Options: nosniff'); | |
138 | } | |
139 | ||
140 | ||
141 | function xss() | |
142 | { | |
143 | header('X-XSS-Protection: 1; mode=block'); | |
144 | } | |
145 | ||
146 | ||
147 | function hsts() | |
148 | { | |
149 | header('Strict-Transport-Security: max-age=31536000'); | |
150 | } | |
151 | ||
152 | ||
153 | function xframe($mode = 'DENY', array $urls = array()) | |
154 | { | |
155 | header('X-Frame-Options: '.$mode.' '.implode(' ', $urls)); | |
156 | } |