PluginProbe
HTTP Headers / 1.9.4
HTTP Headers v1.9.4
1.19.5 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.14.0 1.14.1 1.14.2 1.15.0 All 60 releases
http-headers / http-headers.php

http-headers.php in HTTP Headers 1.9.4, at http-headers.php

1,229 lines 44.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: HTTP Headers
4 Plugin URI: https://zinoui.com/blog/http-headers-for-wordpress
5 Description: A plugin for HTTP headers management including security, access-control (CORS), caching, compression, and authentication.
6 Version: 1.9.4
7 Author: Dimitar Ivanov
8 Author URI: https://zinoui.com
9 License: GPLv2 or later
10 Text Domain: http-headers
11 */
12
13 /*
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/copyleft/gpl.html>.
26
27 Copyright (c) 2017 Zino UI
28 */
29
30 if (!defined('ABSPATH')) {
31 exit;
32 }
33
34 if (get_option('hh_strict_transport_security_max_age') === false) {
35 $value = get_option('hh_strict_transport_security_value');
36 $max_age = preg_match('/max-age=(\d+)/', $value, $match) ? $match[1] : 0;
37 $sub_domains = strpos($value, 'includeSubDomains') !== false ? 1 : 0;
38 add_option('hh_strict_transport_security_max_age', $max_age, null, 'yes');
39 add_option('hh_strict_transport_security_sub_domains', $sub_domains, null, 'yes');
40 add_option('hh_strict_transport_security_preload', 0, null, 'yes');
41 }
42
43 if (get_option('hh_referrer_policy') === false) {
44 add_option('hh_referrer_policy', 0, null, 'yes');
45 add_option('hh_referrer_policy_value', null, null, 'yes');
46 }
47
48 if (get_option('hh_content_security_policy') === false) {
49 add_option('hh_content_security_policy', 0, null, 'yes');
50 add_option('hh_content_security_policy_value', null, null, 'yes');
51 }
52
53 if (get_option('hh_content_security_policy_report_only') === false) {
54 add_option('hh_content_security_policy_report_only', 0, null, 'yes');
55 }
56
57 if (get_option('hh_public_key_pins_report_only') === false) {
58 add_option('hh_public_key_pins_report_only', 0, null, 'yes');
59 }
60
61 if (get_option('hh_x_xxs_protection_uri') === false) {
62 add_option('hh_x_xxs_protection_uri', null, null, 'yes');
63 }
64
65 if (get_option('hh_method') === false) {
66 add_option('hh_method', 'php', null, 'yes');
67 }
68
69 if (get_option('hh_connection') === false) {
70 add_option('hh_connection', 0, null, 'yes');
71 add_option('hh_connection_value', null, null, 'yes');
72 }
73
74 if (get_option('hh_cache_control') === false) {
75 add_option('hh_cache_control', 0, null, 'yes');
76 add_option('hh_cache_control_value', null, null, 'yes');
77 }
78
79 if (get_option('hh_age') === false) {
80 add_option('hh_age', 0, null, 'yes');
81 add_option('hh_age_value', null, null, 'yes');
82 }
83
84 if (get_option('hh_pragma') === false) {
85 add_option('hh_pragma', 0, null, 'yes');
86 add_option('hh_pragma_value', null, null, 'yes');
87 }
88
89 if (get_option('hh_expires') === false) {
90 add_option('hh_expires', 0, null, 'yes');
91 add_option('hh_expires_value', null, null, 'yes');
92 add_option('hh_expires_type', null, null, 'yes');
93 }
94
95 if (get_option('hh_content_encoding') === false) {
96 add_option('hh_content_encoding', 0, null, 'yes');
97 add_option('hh_content_encoding_value', null, null, 'yes');
98 add_option('hh_content_encoding_ext', null, null, 'yes');
99 }
100
101 if (get_option('hh_vary') === false) {
102 add_option('hh_vary', 0, null, 'yes');
103 add_option('hh_vary_value', null, null, 'yes');
104 }
105
106 if (get_option('hh_x_powered_by') === false) {
107 add_option('hh_x_powered_by', 0, null, 'yes');
108 add_option('hh_x_powered_by_option', null, null, 'yes');
109 add_option('hh_x_powered_by_value', null, null, 'yes');
110 }
111
112 if (get_option('hh_www_authenticate') === false) {
113 add_option('hh_www_authenticate', 0, null, 'yes');
114 add_option('hh_www_authenticate_type', null, null, 'yes');
115 add_option('hh_www_authenticate_realm', null, null, 'yes');
116 add_option('hh_www_authenticate_user', null, null, 'yes');
117 add_option('hh_www_authenticate_pswd', null, null, 'yes');
118 }
119
120 if (get_option('hh_cookie_security') === false) {
121 add_option('hh_cookie_security', 0, null, 'yes');
122 add_option('hh_cookie_security_value', null, null, 'yes');
123 }
124
125 if (get_option('hh_expect_ct') === false) {
126 add_option('hh_expect_ct', 0, null, 'yes');
127 add_option('hh_expect_ct_max_age', null, null, 'yes');
128 add_option('hh_expect_ct_report_uri', null, null, 'yes');
129 add_option('hh_expect_ct_enforce', null, null, 'yes');
130 }
131
132 if (get_option('hh_timing_allow_origin') === false) {
133 add_option('hh_timing_allow_origin', 0, null, 'yes');
134 add_option('hh_timing_allow_origin_value', null, null, 'yes');
135 add_option('hh_timing_allow_origin_url', null, null, 'yes');
136 }
137
138 if (get_option('hh_custom_headers') === false) {
139 add_option('hh_custom_headers', 0, null, 'yes');
140 add_option('hh_custom_headers_value', null, null, 'yes');
141 }
142
143 if (get_option('hh_x_permitted_cross_domain_policies') === false) {
144 add_option('hh_x_permitted_cross_domain_policies', 0, null, 'yes');
145 add_option('hh_x_permitted_cross_domain_policies_value', null, null, 'yes');
146 }
147
148 if (get_option('hh_x_download_options') === false) {
149 add_option('hh_x_download_options', 0, null, 'yes');
150 add_option('hh_x_download_options_value', null, null, 'yes');
151 }
152
153 if (get_option('hh_x_dns_prefetch_control') === false) {
154 add_option('hh_x_dns_prefetch_control', 0, null, 'yes');
155 add_option('hh_x_dns_prefetch_control_value', null, null, 'yes');
156 }
157
158 if (get_option('hh_report_to') === false) {
159 add_option('hh_report_to', 0, null, 'yes');
160 add_option('hh_report_to_value', null, null, 'yes');
161 }
162
163 function get_http_headers() {
164 $statuses = array();
165 $unset = array();
166 $headers = array();
167 $append = array();
168 if (get_option('hh_x_frame_options') == 1) {
169 $x_frame_options_value = strtoupper(get_option('hh_x_frame_options_value'));
170 if ($x_frame_options_value == 'ALLOW-FROM') {
171 $x_frame_options_value .= ' ' . get_option('hh_x_frame_options_domain');
172 }
173 $headers['X-Frame-Options'] = $x_frame_options_value;
174 }
175 if (get_option('hh_x_powered_by') == 1) {
176 if (get_option('hh_x_powered_by_option') == 'set') {
177 $headers['X-Powered-By'] = get_option('hh_x_powered_by_value');
178 } else {
179 $unset[] = 'X-Powered-By';
180 }
181 }
182 if (get_option('hh_x_xxs_protection') == 1) {
183 $headers['X-XSS-Protection'] = get_option('hh_x_xxs_protection_value');
184 if ($headers['X-XSS-Protection'] == '1; report=') {
185 $headers['X-XSS-Protection'] .= get_option('hh_x_xxs_protection_uri');
186 }
187 }
188 if (get_option('hh_x_content_type_options') == 1) {
189 $headers['X-Content-Type-Options'] = get_option('hh_x_content_type_options_value');
190 }
191 if (get_option('hh_x_download_options') == 1) {
192 $headers['X-Download-Options'] = get_option('hh_x_download_options_value');
193 }
194 if (get_option('hh_x_permitted_cross_domain_policies') == 1) {
195 $headers['X-Permitted-Cross-Domain-Policies'] = get_option('hh_x_permitted_cross_domain_policies_value');
196 }
197 if (get_option('hh_x_dns_prefetch_control') == 1) {
198 $headers['X-DNS-Prefetch-Control'] = get_option('hh_x_dns_prefetch_control_value');
199 }
200 if (get_option('hh_connection') == 1) {
201 $headers['Connection'] = get_option('hh_connection_value');
202 }
203 if (get_option('hh_pragma') == 1) {
204 $headers['Pragma'] = get_option('hh_pragma_value');
205 }
206 if (get_option('hh_age') == 1) {
207 $headers['Age'] = sprintf("%u", get_option('hh_age_value'));
208 }
209 if (get_option('hh_cache_control') == 1) {
210 $hh_cache_control_value = get_option('hh_cache_control_value', array());
211 $tmp = array();
212 foreach ($hh_cache_control_value as $k => $v) {
213 if (in_array($k, array('max-age', 's-maxage'))) {
214 if (strlen($v) > 0) {
215 $tmp[] = sprintf("%s=%u", $k, $v);
216 }
217 } else {
218 $tmp[] = $k;
219 }
220 }
221 $hh_cache_control_value = join(', ', $tmp);
222 $headers['Cache-Control'] = $hh_cache_control_value;
223 }
224 if (get_option('hh_strict_transport_security') == 1) {
225 $hh_strict_transport_security = array();
226
227 $hh_strict_transport_security_max_age = get_option('hh_strict_transport_security_max_age');
228 if ($hh_strict_transport_security_max_age !== false)
229 {
230 $hh_strict_transport_security[] = sprintf('max-age=%u', get_option('hh_strict_transport_security_max_age'));
231 if (get_option('hh_strict_transport_security_sub_domains'))
232 {
233 $hh_strict_transport_security[] = 'includeSubDomains';
234 }
235 if (get_option('hh_strict_transport_security_preload'))
236 {
237 $hh_strict_transport_security[] = 'preload';
238 }
239 } else {
240 $hh_strict_transport_security = array(get_option('hh_strict_transport_security_value'));
241 }
242 $headers['Strict-Transport-Security'] = join('; ', $hh_strict_transport_security);
243 }
244 if (get_option('hh_x_ua_compatible') == 1) {
245 $headers['X-UA-Compatible'] = get_option('hh_x_ua_compatible_value');
246 }
247 if (get_option('hh_public_key_pins') == 1) {
248 $public_key_pins_sha256_1 = get_option('hh_public_key_pins_sha256_1');
249 $public_key_pins_sha256_2 = get_option('hh_public_key_pins_sha256_2');
250 $public_key_pins_max_age = get_option('hh_public_key_pins_max_age');
251 $public_key_pins_sub_domains = get_option('hh_public_key_pins_sub_domains');
252 $public_key_pins_report_uri = get_option('hh_public_key_pins_report_uri');
253 $public_key_pins_report_only = get_option('hh_public_key_pins_report_only');
254 if (!empty($public_key_pins_sha256_1) && !empty($public_key_pins_sha256_2) && !empty($public_key_pins_max_age)) {
255
256 $public_key_pins = array();
257 $public_key_pins[] = sprintf('pin-sha256="%s"', $public_key_pins_sha256_1);
258 $public_key_pins[] = sprintf('pin-sha256="%s"', $public_key_pins_sha256_2);
259 $public_key_pins[] = sprintf("max-age=%u", $public_key_pins_max_age);
260 if ($public_key_pins_sub_domains) {
261 $public_key_pins[] = "includeSubDomains";
262 }
263 if (!empty($public_key_pins_report_uri)) {
264 $public_key_pins[] = sprintf('report-uri="%s"', $public_key_pins_report_uri);
265 }
266 $headers['Public-Key-Pins'.($public_key_pins_report_only ? '-Report-Only' : NULL)] = join('; ', $public_key_pins);
267 }
268 }
269
270 if (get_option('hh_content_security_policy') == 1)
271 {
272 $csp = array();
273 $values = get_option('hh_content_security_policy_value');
274 $csp_report_only = get_option('hh_content_security_policy_report_only');
275 foreach ($values as $key => $val)
276 {
277 if (!empty($val))
278 {
279 $csp[] = sprintf("%s %s", $key, $val);
280 }
281 }
282 if (!empty($csp))
283 {
284 $headers['Content-Security-Policy'.($csp_report_only ? '-Report-Only' : NULL)] = join('; ', $csp);
285 }
286 }
287
288 if (get_option('hh_access_control_allow_origin') == 1)
289 {
290 $value = get_option('hh_access_control_allow_origin_value');
291 switch ($value)
292 {
293 case 'origin':
294 $value = get_option('hh_access_control_allow_origin_url', array());
295 if (is_scalar($value))
296 {
297 $value = array($value);
298 }
299 break;
300 }
301 if (!empty($value))
302 {
303 $headers['Access-Control-Allow-Origin'] = $value;
304 }
305 }
306 if (get_option('hh_access_control_allow_credentials') == 1)
307 {
308 $headers['Access-Control-Allow-Credentials'] = get_option('hh_access_control_allow_credentials_value');
309 }
310 if (get_option('hh_access_control_max_age') == 1)
311 {
312 $value = get_option('hh_access_control_max_age_value');
313 if (!empty($value))
314 {
315 $headers['Access-Control-Max-Age'] = intval($value);
316 }
317 }
318 if (get_option('hh_access_control_allow_methods') == 1)
319 {
320 $value = get_option('hh_access_control_allow_methods_value');
321 if (!empty($value))
322 {
323 $headers['Access-Control-Allow-Methods'] = join(', ', array_keys($value));
324 }
325 }
326 if (get_option('hh_access_control_allow_headers') == 1)
327 {
328 $value = get_option('hh_access_control_allow_headers_value');
329 if (!empty($value))
330 {
331 $headers['Access-Control-Allow-Headers'] = join(', ', array_keys($value));
332 }
333 }
334 if (get_option('hh_access_control_expose_headers') == 1)
335 {
336 $value = get_option('hh_access_control_expose_headers_value');
337 if (!empty($value))
338 {
339 $headers['Access-Control-Expose-Headers'] = join(', ', array_keys($value));
340 }
341 }
342 if (get_option('hh_p3p') == 1)
343 {
344 $value = get_option('hh_p3p_value');
345 if (!empty($value))
346 {
347 $headers['P3P'] = 'CP="' . join(' ', array_keys($value)) . '"';
348 }
349 }
350 if (get_option('hh_referrer_policy') == 1) {
351 $headers['Referrer-Policy'] = get_option('hh_referrer_policy_value');
352 }
353 if (get_option('hh_www_authenticate') == 1) {
354
355 switch (get_option('hh_www_authenticate_type')) {
356 case 'Basic':
357 if (!(isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
358 && $_SERVER['PHP_AUTH_USER'] == get_option('hh_www_authenticate_user')
359 && $_SERVER['PHP_AUTH_PW'] == get_option('hh_www_authenticate_pswd'))) {
360 $headers['WWW-Authenticate'] = sprintf("Basic realm='%s'", get_option('hh_www_authenticate_realm'));
361 $statuses['HTTP/1.1'] = '401 Unauthorized';
362 }
363 break;
364 case 'Digest':
365 if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
366 $realm = get_option('hh_www_authenticate_realm');
367 $headers['WWW-Authenticate'] = sprintf("Digest realm='%s',qop='auth',nonce='%s',opaque='%s'",
368 $realm, uniqid(), md5($realm));
369 $statuses['HTTP/1.1'] = '401 Unauthorized';
370 }
371 break;
372 }
373 }
374 if (get_option('hh_vary') == 1)
375 {
376 $value = get_option('hh_vary_value');
377 if (!empty($value))
378 {
379 $append['Vary'] = join(', ', array_keys($value));
380 }
381 }
382
383 if (get_option('hh_expect_ct') == 1) {
384 $expect_ct_max_age = get_option('hh_expect_ct_max_age');
385 $expect_ct_report_uri = get_option('hh_expect_ct_report_uri');
386 if (!empty($expect_ct_report_uri) && !empty($expect_ct_max_age)) {
387
388 $expect_ct = array();
389 $expect_ct[] = sprintf("max-age=%u", $expect_ct_max_age);
390 if (get_option('hh_expect_ct_enforce') == 1) {
391 $expect_ct[] = "enforce";
392 }
393 $expect_ct[] = sprintf('report-uri="%s"', $expect_ct_report_uri);
394 $headers['Expect-CT'] = join(', ', $expect_ct);
395 }
396 }
397 if (get_option('hh_custom_headers') == 1) {
398 $custom_headers = get_option('hh_custom_headers_value');
399 if (isset($custom_headers['name'], $custom_headers['value']) && !empty($custom_headers['name'])) {
400 foreach ($custom_headers['name'] as $key => $name) {
401 $name = trim($name);
402 $value = trim($custom_headers['value'][$key]);
403 if (empty($name) || empty($value)) {
404 continue;
405 }
406 $headers[$name] = $value;
407 }
408 }
409 }
410 if (get_option('hh_report_to') == 1) {
411 $report_to = get_option('hh_report_to_value');
412 $tmp = array();
413 foreach ($report_to as $item)
414 {
415 $tmp[] = sprintf('{"url": "%s", "group": "%s", "max-age": %u%s}',
416 $item['url'], $item['group'], $item['max-age'], isset($item['includeSubDomains']) ? ', includeSubDomains' : NULL);
417 }
418 $headers['Report-To'] = join(', ', $tmp);
419 }
420
421 return array($headers, $statuses, $unset, $append);
422 }
423
424 function http_digest_parse($txt) {
425 $txt = stripslashes($txt);
426
427 $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
428 $data = array();
429 $keys = implode('|', array_keys($needed_parts));
430
431 preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
432
433 foreach ($matches as $m) {
434 $data[$m[1]] = $m[3] ? $m[3] : $m[4];
435 unset($needed_parts[$m[1]]);
436 }
437
438 return $needed_parts ? false : $data;
439 }
440
441 function php_auth_digest() {
442 if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || get_option('hh_www_authenticate_user') != $data['username']) {
443 die('Wrong Credentials!');
444 }
445
446 $A1 = md5($data['username'] . ':' . get_option('hh_www_authenticate_realm') . ':' . get_option('hh_www_authenticate_pswd'));
447 $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
448 $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
449 if ($data['response'] != $valid_response) {
450 die('Wrong Credentials!');
451 }
452 }
453
454 function php_content_encoding() {
455 if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
456 ob_start('ob_gzhandler');
457 } else {
458 ob_start();
459 }
460 }
461
462 function http_headers() {
463 if (get_option('hh_method') !== 'php') {
464 return;
465 }
466 // PHP method below
467 list($headers, $statuses, $unset, $append) = get_http_headers();
468 $isCors = false;
469 foreach ($headers as $key => $value) {
470 if ($key == 'Access-Control-Allow-Origin') {
471 if (isset($_SERVER['HTTP_ORIGIN'])) {
472 if ($value == '*') {
473 $isCors = true;
474 header(sprintf("%s: *", $key));
475 }
476
477 if (is_array($value) && in_array($_SERVER['HTTP_ORIGIN'], $value)) {
478 $isCors = true;
479 header(sprintf("%s: %s", $key, $_SERVER['HTTP_ORIGIN']));
480 }
481 }
482 continue;
483 }
484 if (in_array($key, array('Access-Control-Allow-Headers', 'Access-Control-Allow-Methods', 'Access-Control-Allow-Credentials', 'Access-Control-Max-Age', 'Access-Control-Expose-Headers'))) {
485 if ($isCors) {
486 header(sprintf("%s: %s", $key, $value));
487 }
488 continue;
489 }
490 header(sprintf("%s: %s", $key, $value));
491 }
492 foreach ($append as $key => $value) {
493 header(sprintf("%s: %s", $key, $value), false);
494 }
495 foreach ($unset as $header) {
496 if (function_exists('header_remove')) {
497 header_remove($header);
498 } else {
499 header("$header:");
500 }
501 }
502 foreach ($statuses as $key => $value) {
503 header(sprintf("%s %s", $key, $value));
504 exit;
505 }
506
507 if (get_option('hh_www_authenticate') == 1) {
508 php_auth_digest();
509 }
510
511 if (get_option('hh_content_encoding') == 1) {
512 php_content_encoding();
513 }
514 }
515
516 function http_headers_admin_add_page() {
517 add_options_page('HTTP Headers', 'HTTP Headers', 'manage_options', 'http-headers', 'http_headers_admin_page');
518 }
519
520 function http_headers_admin() {
521 register_setting('http-headers-mtd', 'hh_method');
522 register_setting('http-headers-xfo', 'hh_x_frame_options');
523 register_setting('http-headers-xfo', 'hh_x_frame_options_value');
524 register_setting('http-headers-xfo', 'hh_x_frame_options_domain');
525 register_setting('http-headers-xss', 'hh_x_xxs_protection');
526 register_setting('http-headers-xss', 'hh_x_xxs_protection_value');
527 register_setting('http-headers-xss', 'hh_x_xxs_protection_uri');
528 register_setting('http-headers-cto', 'hh_x_content_type_options');
529 register_setting('http-headers-cto', 'hh_x_content_type_options_value');
530 register_setting('http-headers-sts', 'hh_strict_transport_security');
531 register_setting('http-headers-sts', 'hh_strict_transport_security_value'); //obsolete
532 register_setting('http-headers-sts', 'hh_strict_transport_security_max_age');
533 register_setting('http-headers-sts', 'hh_strict_transport_security_sub_domains');
534 register_setting('http-headers-sts', 'hh_strict_transport_security_preload');
535 register_setting('http-headers-pkp', 'hh_public_key_pins');
536 register_setting('http-headers-pkp', 'hh_public_key_pins_sha256_1');
537 register_setting('http-headers-pkp', 'hh_public_key_pins_sha256_2');
538 register_setting('http-headers-pkp', 'hh_public_key_pins_max_age');
539 register_setting('http-headers-pkp', 'hh_public_key_pins_sub_domains');
540 register_setting('http-headers-pkp', 'hh_public_key_pins_report_uri');
541 register_setting('http-headers-pkp', 'hh_public_key_pins_report_only');
542 register_setting('http-headers-uac', 'hh_x_ua_compatible');
543 register_setting('http-headers-uac', 'hh_x_ua_compatible_value');
544 register_setting('http-headers-p3p', 'hh_p3p');
545 register_setting('http-headers-p3p', 'hh_p3p_value');
546 register_setting('http-headers-rp', 'hh_referrer_policy');
547 register_setting('http-headers-rp', 'hh_referrer_policy_value');
548 register_setting('http-headers-csp', 'hh_content_security_policy');
549 register_setting('http-headers-csp', 'hh_content_security_policy_value');
550 register_setting('http-headers-csp', 'hh_content_security_policy_report_only');
551 register_setting('http-headers-acao', 'hh_access_control_allow_origin');
552 register_setting('http-headers-acao', 'hh_access_control_allow_origin_value');
553 register_setting('http-headers-acao', 'hh_access_control_allow_origin_url');
554 register_setting('http-headers-acac', 'hh_access_control_allow_credentials');
555 register_setting('http-headers-acac', 'hh_access_control_allow_credentials_value');
556 register_setting('http-headers-acam', 'hh_access_control_allow_methods');
557 register_setting('http-headers-acam', 'hh_access_control_allow_methods_value');
558 register_setting('http-headers-acah', 'hh_access_control_allow_headers');
559 register_setting('http-headers-acah', 'hh_access_control_allow_headers_value');
560 register_setting('http-headers-aceh', 'hh_access_control_expose_headers');
561 register_setting('http-headers-aceh', 'hh_access_control_expose_headers_value');
562 register_setting('http-headers-acma', 'hh_access_control_max_age');
563 register_setting('http-headers-acma', 'hh_access_control_max_age_value');
564 register_setting('http-headers-ce', 'hh_content_encoding');
565 register_setting('http-headers-ce', 'hh_content_encoding_value');
566 register_setting('http-headers-ce', 'hh_content_encoding_ext');
567 register_setting('http-headers-vary', 'hh_vary');
568 register_setting('http-headers-vary', 'hh_vary_value');
569 register_setting('http-headers-xpb', 'hh_x_powered_by');
570 register_setting('http-headers-xpb', 'hh_x_powered_by_option');
571 register_setting('http-headers-xpb', 'hh_x_powered_by_value');
572 register_setting('http-headers-wwa', 'hh_www_authenticate');
573 register_setting('http-headers-wwa', 'hh_www_authenticate_type');
574 register_setting('http-headers-wwa', 'hh_www_authenticate_realm');
575 register_setting('http-headers-wwa', 'hh_www_authenticate_user');
576 register_setting('http-headers-wwa', 'hh_www_authenticate_pswd');
577 register_setting('http-headers-cc', 'hh_cache_control');
578 register_setting('http-headers-cc', 'hh_cache_control_value');
579 register_setting('http-headers-age', 'hh_age');
580 register_setting('http-headers-age', 'hh_age_value');
581 register_setting('http-headers-pra', 'hh_pragma');
582 register_setting('http-headers-pra', 'hh_pragma_value');
583 register_setting('http-headers-exp', 'hh_expires');
584 register_setting('http-headers-exp', 'hh_expires_value');
585 register_setting('http-headers-exp', 'hh_expires_type');
586 register_setting('http-headers-con', 'hh_connection');
587 register_setting('http-headers-con', 'hh_connection_value');
588 register_setting('http-headers-cose', 'hh_cookie_security');
589 register_setting('http-headers-cose', 'hh_cookie_security_value');
590 register_setting('http-headers-ect', 'hh_expect_ct');
591 register_setting('http-headers-ect', 'hh_expect_ct_max_age');
592 register_setting('http-headers-ect', 'hh_expect_ct_report_uri');
593 register_setting('http-headers-ect', 'hh_expect_ct_enforce');
594 register_setting('http-headers-tao', 'hh_timing_allow_origin');
595 register_setting('http-headers-tao', 'hh_timing_allow_origin_value');
596 register_setting('http-headers-tao', 'hh_timing_allow_origin_url');
597 register_setting('http-headers-che', 'hh_custom_headers');
598 register_setting('http-headers-che', 'hh_custom_headers_value');
599 register_setting('http-headers-xdo', 'hh_x_download_options');
600 register_setting('http-headers-xdo', 'hh_x_download_options_value');
601 register_setting('http-headers-xpcd', 'hh_x_permitted_cross_domain_policies');
602 register_setting('http-headers-xpcd', 'hh_x_permitted_cross_domain_policies_value');
603 register_setting('http-headers-xdpc', 'hh_x_dns_prefetch_control');
604 register_setting('http-headers-xdpc', 'hh_x_dns_prefetch_control_value');
605 register_setting('http-headers-rt', 'hh_report_to');
606 register_setting('http-headers-rt', 'hh_report_to_value');
607 }
608
609 function http_headers_option($option) {
610 if (isset($_POST['hh_method']))
611 {
612 check_admin_referer('http-headers-mtd-options');
613 # When method is changed
614 update_headers_directives();
615 update_auth_credentials();
616 update_auth_directives();
617 update_content_encoding_directives();
618 update_expires_directives();
619 update_cookie_security_directives();
620 update_timing_directives();
621
622 } elseif (get_option('hh_method') == 'htaccess') {
623 # When particular header is changed
624 switch (true) {
625 case array_key_exists('hh_www_authenticate', $_POST):
626 check_admin_referer('http-headers-wwa-options');
627 update_auth_credentials();
628 update_auth_directives();
629 break;
630 case array_key_exists('hh_content_encoding', $_POST):
631 check_admin_referer('http-headers-ce-options');
632 update_content_encoding_directives();
633 break;
634 case array_key_exists('hh_vary', $_POST):
635 check_admin_referer('http-headers-vary-options');
636 update_content_encoding_directives();
637 break;
638 case array_key_exists('hh_expires', $_POST):
639 check_admin_referer('http-headers-exp-options');
640 update_expires_directives();
641 break;
642 case array_key_exists('hh_cookie_security', $_POST):
643 check_admin_referer('http-headers-cose-options');
644 update_cookie_security_directives();
645 break;
646 case array_key_exists('hh_timing_allow_origin', $_POST):
647 check_admin_referer('http-headers-tao-options');
648 update_timing_directives();
649 break;
650 case array_key_exists('option_page', $_POST) && strpos($_POST['option_page'], 'http-headers-') === 0:
651 check_admin_referer($_POST['option_page'].'-options');
652 update_headers_directives();
653 break;
654 }
655 }
656 }
657
658 function nginx_headers_directives() {
659 $lines = array();
660 list($headers, $statuses, $unset, $append) = get_http_headers();
661
662 foreach ($unset as $header) {
663 $lines[] = sprintf(' more_clear_headers "%s";', $header);
664 }
665 $cors = $cors_header = $cors_inner = $cors_footer = array();
666 $all = array();
667 foreach ($headers as $key => $value) {
668 if (in_array($key, array('WWW-Authenticate'))) {
669 continue;
670 }
671 if (in_array($key, array('X-Content-Type-Options'))) {
672 $all[] = sprintf('add_header %s %s always;', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
673 continue;
674 }
675 if ($key == 'Access-Control-Allow-Origin' && is_array($value)) {
676 $cors_header[] = sprintf('if ($http_origin ~* ^(%s)$) {', str_replace('.', '\.', join('|', $value)));
677 $cors_footer[] = '}';
678 $cors_inner[] = ' add_header Access-Control-Allow-Origin "$http_origin";';
679 continue;
680 }
681 if (in_array($key, array('Access-Control-Allow-Headers', 'Access-Control-Allow-Methods', 'Access-Control-Allow-Credentials', 'Access-Control-Max-Age', 'Access-Control-Expose-Headers'))) {
682 $cors_inner[] = sprintf(' add_header %s %s;', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
683 continue;
684 }
685 $lines[] = sprintf(' add_header %s %s;', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
686 }
687 foreach ($append as $key => $value) {
688 $lines[] = sprintf(' add_header %s %s;', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
689 }
690 if (!empty($cors_inner))
691 {
692 $cors = array_merge(
693 $cors_header,
694 $cors_inner,
695 $cors_footer
696 );
697 }
698 if (!empty($lines)) {
699 $lines = array_merge(
700 $all,
701 $cors,
702 array('location ~* \.(php|html)$ {'),
703 $lines,
704 array('}')
705 );
706 }
707 return $lines;
708 }
709
710 function nginx_content_encoding_directives() {
711 $lines = array();
712 if (get_option('hh_content_encoding') == 1) {
713
714 $lines[] = 'gzip on;';
715
716 $content_encoding_value = get_option('hh_content_encoding_value');
717 if (!$content_encoding_value) {
718 $content_encoding_value = array();
719 }
720
721 $content_encoding_ext = get_option('hh_content_encoding_ext');
722 if (!$content_encoding_ext) {
723 $content_encoding_ext = array();
724 }
725 if (!empty($content_encoding_ext)) {
726 //$lines[] = sprintf('<FilesMatch "\.(%s)$">', join('|', array_keys($content_encoding_ext)));
727 }
728 if (!empty($content_encoding_value)) {
729 $lines[] = sprintf('gzip_types %s;', join(' ', array_keys($content_encoding_value)));
730 }
731 }
732 return $lines;
733 }
734
735 function nginx_expires_directives() {
736 $lines = array();
737 if (get_option('hh_expires') == 1) {
738
739 $types = get_option('hh_expires_type', array());
740 $values = get_option('hh_expires_value', array());
741
742 $lines[] = 'map $sent_http_content_type $expires {';
743 foreach ($types as $type => $whatever) {
744 list($base, $period, $suffix) = explode('_', $values[$type]);
745 if (in_array($base, array('access', 'modification'))) {
746 $lines[] = $type != 'default'
747 ? sprintf(' %s %u%s;', $type, $period, $suffix[0])
748 : sprintf(' default %u%s;', $period, $suffix[0]);
749 } elseif ($base == 'invalid') {
750 $lines[] = $type != 'default'
751 ? sprintf(' %s 0;', $type)
752 : sprintf(' default 0;');
753 }
754 }
755 $lines[] = '}';
756
757 $lines[] = 'expires $expires;';
758 }
759 return $lines;
760 }
761
762 function nginx_timing_directives() {
763 $lines = array();
764 if (get_option('hh_timing_allow_origin') == 1) {
765 $value = get_option('hh_timing_allow_origin_value');
766 switch ($value)
767 {
768 case 'origin':
769 $value = get_option('hh_timing_allow_origin_url');
770 break;
771 }
772 if (!empty($value))
773 {
774 $lines[] = 'location ~* \.(js|css|jpe?g|png|gif|eot|otf|svg|ttf|woff2?)$ {';
775 $lines[] = sprintf(' add_header Timing-Allow-Origin "%s";', $value);
776 $lines[] = '}';
777 }
778 }
779 return $lines;
780 }
781
782 function nginx_auth_directives() {
783 $lines = array();
784 if (get_option('hh_www_authenticate') == 1) {
785
786 $type = get_option('hh_www_authenticate_type');
787
788 $file = $type == 'Basic' ? '.hh-htpasswd' : '.hh-htdigest';
789
790 $lines[] = 'location ~ ^\.hh-ht(digest|passwd)$ {';
791 $lines[] = ' deny all;';
792 $lines[] = '}';
793
794 $lines[] = sprintf('location %s {', get_home_path());
795 if ($type == 'Basic') {
796 $lines[] = sprintf(' auth_basic "%s";', get_option('hh_www_authenticate_realm'));
797 $lines[] = sprintf(' auth_basic_user_file %s%s;', get_home_path(), $file);
798 } else {
799 $lines[] = sprintf(' auth_digest "%s";', get_option('hh_www_authenticate_realm'));
800 $lines[] = sprintf(' auth_digest_user_file %s%s;', get_home_path(), $file);
801 }
802 $lines[] = '}';
803 }
804 return $lines;
805 }
806
807 function nginx_auth_credentials() {
808 return apache_auth_credentials();
809 }
810
811 function nginx_cookie_security_directives() {
812 $lines = array();
813
814 //TODO
815
816 return $lines;
817 }
818
819 function iis_headers_directives() {
820 //TODO scheduled for v2.0.0
821 }
822
823 function iis_content_encoding_directives() {
824 //TODO scheduled for v2.0.0
825 }
826
827 function iis_expires_directives() {
828 //TODO scheduled for v2.0.0
829 }
830
831 function iis_timing_directives() {
832 //TODO scheduled for v2.0.0
833 }
834
835 function iis_auth_directives() {
836 //TODO scheduled for v2.0.0
837 }
838
839 function iis_auth_credentials() {
840 //TODO scheduled for v2.0.0
841 }
842
843 function iis_cookie_security_directives() {
844 //TODO scheduled for v2.0.0
845 }
846
847 function apache_headers_directives() {
848 $lines = array();
849 list($headers, $statuses, $unset, $append) = get_http_headers();
850
851 foreach ($unset as $header) {
852 $lines[] = sprintf(' Header unset %s', $header);
853 }
854 $all = array();
855 foreach ($headers as $key => $value) {
856 if (in_array($key, array('WWW-Authenticate'))) {
857 continue;
858 }
859 if (in_array($key, array('X-Content-Type-Options'))) {
860 $all[] = sprintf(' Header always set %s %s', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
861 continue;
862 }
863 if ($key == 'Access-Control-Allow-Origin' && is_array($value)) {
864 $all[] = ' <IfModule mod_setenvif.c>';
865 $all[] = sprintf(' SetEnvIf Origin "^(%s)$" CORS=$0', str_replace('.', '\.', join('|', $value)));
866 $all[] = ' </IfModule>';
867 $all[] = ' Header set Access-Control-Allow-Origin %{CORS}e env=CORS';
868 continue;
869 }
870 if (in_array($key, array('Access-Control-Allow-Headers', 'Access-Control-Allow-Methods', 'Access-Control-Allow-Credentials', 'Access-Control-Max-Age', 'Access-Control-Expose-Headers'))) {
871 $all[] = sprintf(' Header set %s %s env=CORS', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
872 continue;
873 }
874 $lines[] = sprintf(' Header set %s %s', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
875 }
876 foreach ($append as $key => $value) {
877 $lines[] = sprintf(' Header append %s %s', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
878 }
879 if (!empty($lines)) {
880 $lines = array_merge(
881 array('<IfModule mod_headers.c>'),
882 $all,
883 array(' <FilesMatch "\.(php|html)$">'),
884 $lines,
885 array(' </FilesMatch>', '</IfModule>')
886 );
887 }
888 return $lines;
889 }
890
891 function apache_content_encoding_directives() {
892 $lines = array();
893 if (get_option('hh_content_encoding') == 1) {
894
895 $content_encoding_value = get_option('hh_content_encoding_value');
896 if (!$content_encoding_value) {
897 $content_encoding_value = array();
898 }
899
900 $content_encoding_ext = get_option('hh_content_encoding_ext');
901 if (!$content_encoding_ext) {
902 $content_encoding_ext = array();
903 }
904 if (!empty($content_encoding_ext)) {
905 $lines[] = sprintf('<FilesMatch "\.(%s)$">', join('|', array_keys($content_encoding_ext)));
906 $lines[] = ' <IfModule mod_deflate.c>';
907 $lines[] = ' SetOutputFilter DEFLATE';
908 $lines[] = ' </IfModule>';
909 $lines[] = '</FilesMatch>';
910 }
911 if (!empty($content_encoding_value)) {
912 if (!empty($lines)) {
913 $lines[] = '';
914 }
915 $lines[] = '<IfModule mod_deflate.c>';
916 foreach ($content_encoding_value as $item => $whatever) {
917 $lines[] = sprintf(' AddOutputFilterByType DEFLATE %s', $item);
918 }
919 $lines[] = '</IfModule>';
920 }
921 }
922
923 return $lines;
924 }
925
926 function apache_expires_directives() {
927 $lines = array();
928 if (get_option('hh_expires') == 1) {
929
930 $types = get_option('hh_expires_type', array());
931 $values = get_option('hh_expires_value', array());
932
933 $lines[] = '<IfModule mod_expires.c>';
934 $lines[] = ' ExpiresActive On';
935 foreach ($types as $type => $whatever) {
936 list($base, $period, $suffix) = explode('_', $values[$type]);
937 if (in_array($base, array('access', 'modification'))) {
938 $lines[] = $type != 'default'
939 ? sprintf(' ExpiresByType %s "%s plus %u %s"', $type, $base, $period, $suffix)
940 : sprintf(' ExpiresDefault "%s plus %u %s"', $base, $period, $suffix);
941 } elseif ($base == 'invalid') {
942 $lines[] = $type != 'default'
943 ? sprintf(' ExpiresByType %s A0', $type)
944 : sprintf(' ExpiresDefault A0');
945 }
946 }
947 $lines[] = '</IfModule>';
948 }
949
950 return $lines;
951 }
952
953 function apache_timing_directives() {
954 $lines = array();
955 if (get_option('hh_timing_allow_origin') == 1) {
956 $value = get_option('hh_timing_allow_origin_value');
957 switch ($value)
958 {
959 case 'origin':
960 $value = get_option('hh_timing_allow_origin_url');
961 break;
962 }
963 if (!empty($value))
964 {
965 $lines[] = '<IfModule mod_headers.c>';
966 $lines[] = ' <FilesMatch "\\.(js|css|jpe?g|png|gif|eot|otf|svg|ttf|woff2?)$">';
967 $lines[] = sprintf(' Header set Timing-Allow-Origin "%s"', $value);
968 $lines[] = ' </FilesMatch>';
969 $lines[] = '</IfModule>';
970 }
971 }
972
973 return $lines;
974 }
975
976 function apache_auth_directives() {
977 $lines = array();
978 if (get_option('hh_www_authenticate') == 1) {
979
980 $type = get_option('hh_www_authenticate_type');
981
982 $file = $type == 'Basic' ? '.hh-htpasswd' : '.hh-htdigest';
983
984 $lines[] = '<FilesMatch "^\.hh-ht(digest|passwd)$">';
985 $lines[] = ' Order deny,allow';
986 $lines[] = ' Deny from all';
987 $lines[] = '</FilesMatch>';
988
989 $lines[] = sprintf('<IfModule mod_auth_%s.c>', strtolower($type));
990 $lines[] = sprintf(' AuthType %s', get_option('hh_www_authenticate_type'));
991 $lines[] = sprintf(' AuthName "%s"', get_option('hh_www_authenticate_realm'));
992 $lines[] = sprintf(' AuthUserFile "%s%s"', get_home_path(), $file);
993 $lines[] = ' Require valid-user';
994 $lines[] = '</IfModule>';
995 }
996
997 return $lines;
998 }
999
1000 function apache_auth_credentials() {
1001 if (get_option('hh_www_authenticate') == 1) {
1002 $type = get_option('hh_www_authenticate_type');
1003 $usernames = get_option('hh_www_authenticate_user', array());
1004 $passwords = get_option('hh_www_authenticate_pswd', array());
1005 if (!is_array($usernames)) {
1006 $usernames = array($usernames);
1007 }
1008 if (!is_array($passwords)) {
1009 $passwords = array($passwords);
1010 }
1011 $realm = get_option('hh_www_authenticate_realm');
1012 $auth = array();
1013 switch ($type) {
1014 case 'Basic':
1015 $ht_file = get_home_path().'.hh-htpasswd';
1016 foreach ($usernames as $k => $user) {
1017 $auth[] = sprintf('%s:{SHA}%s', $user, base64_encode(sha1($passwords[$k], true)));
1018 }
1019 break;
1020 case 'Digest':
1021 $ht_file = get_home_path().'.hh-htdigest';
1022 foreach ($usernames as $k => $user) {
1023 $auth[] = sprintf('%s:%s:%s', $user, $realm, md5($user.':'.$realm.':'.$passwords[$k]));
1024 }
1025 break;
1026 }
1027 $auth = join("\n", $auth);
1028
1029 return compact('ht_file', 'auth');
1030 }
1031 return false;
1032 }
1033
1034 function apache_cookie_security_directives() {
1035 $lines = array();
1036 if (get_option('hh_cookie_security') == 1) {
1037 $value = get_option('hh_cookie_security_value', array());
1038 if (isset($value['HttpOnly'])) {
1039 $lines[] = 'php_flag session.cookie_httponly on';
1040 }
1041 if (isset($value['Secure'])) {
1042 $lines[] = 'php_flag session.cookie_secure on';
1043 }
1044 }
1045
1046 return $lines;
1047 }
1048
1049 function update_headers_directives() {
1050 $lines = array();
1051 if (get_option('hh_method') == 'htaccess') {
1052 $lines = apache_headers_directives();
1053 }
1054
1055 return insert_with_markers(get_home_path().'.htaccess', "HttpHeaders", $lines);
1056 }
1057
1058 function update_content_encoding_directives() {
1059 $lines = array();
1060 if (get_option('hh_method') == 'htaccess') {
1061 $lines = apache_content_encoding_directives();
1062 }
1063
1064 return insert_with_markers(get_home_path().'.htaccess', "HttpHeadersCompression", $lines);
1065 }
1066
1067 function update_expires_directives() {
1068 $lines = array();
1069 if (get_option('hh_method') == 'htaccess') {
1070 $lines = apache_expires_directives();
1071 }
1072
1073 return insert_with_markers(get_home_path().'.htaccess', "HttpHeadersExpires", $lines);
1074 }
1075
1076 function update_timing_directives() {
1077 $lines = array();
1078 if (get_option('hh_method') == 'htaccess') {
1079 $lines = apache_timing_directives();
1080 }
1081
1082 return insert_with_markers(get_home_path().'.htaccess', "HttpHeadersTiming", $lines);
1083 }
1084
1085 function update_auth_directives() {
1086 $lines = array();
1087 if (get_option('hh_method') == 'htaccess') {
1088 $lines = apache_auth_directives();
1089 }
1090
1091 return insert_with_markers(get_home_path().'.htaccess', "HttpHeadersAuth", $lines);
1092 }
1093
1094 function update_auth_credentials() {
1095 if (get_option('hh_method') == 'htaccess') {
1096 $credentials = apache_auth_credentials();
1097
1098 return @file_put_contents($credentials['ht_file'], $credentials['auth']);
1099 }
1100
1101 return false;
1102 }
1103
1104 function update_cookie_security_directives() {
1105 $lines = array();
1106 if (get_option('hh_method') == 'htaccess') {
1107 $lines = apache_cookie_security_directives();
1108 }
1109
1110 return insert_with_markers(get_home_path().'.htaccess', "HttpHeadersCookieSecurity", $lines);
1111 }
1112
1113 function http_headers_text_domain() {
1114 load_plugin_textdomain('http-headers', false, basename( dirname( __FILE__ ) ) . '/languages/');
1115 }
1116
1117 function http_headers_settings_link( $links ) {
1118 $url = get_admin_url() . 'options-general.php?page=http-headers';
1119 $settings_link = '<a href="' . $url . '">' . __('Settings', 'http-headers') . '</a>';
1120 array_unshift( $links, $settings_link );
1121 return $links;
1122 }
1123
1124 function http_headers_after_setup_theme() {
1125 add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'http_headers_settings_link');
1126 }
1127
1128 function http_headers_enqueue($hook) {
1129 if ( 'http-headers.php' != $hook ) {
1130 # FIXME
1131 //return;
1132 }
1133
1134 wp_enqueue_script('http_headers_admin_scripts', plugin_dir_url( __FILE__ ) . 'assets/scripts.js');
1135 wp_localize_script('http_headers_admin_scripts', 'hh', array(
1136 'lbl_delete' => __('Delete', 'http-headers'),
1137 'lbl_value' => __('Value', 'http-headers'),
1138 ));
1139 wp_enqueue_style('http_headers_admin_styles', plugin_dir_url( __FILE__ ) . 'assets/styles.css');
1140 }
1141
1142 function http_headers_ajax_inspect() {
1143 check_ajax_referer('inspect');
1144 if (current_user_can('manage_options')) {
1145 include 'views/ajax-inspect.php';
1146 }
1147 wp_die();
1148 }
1149
1150 function http_headers_post_import() {
1151 check_admin_referer('import');
1152 global $wpdb;
1153 if (!(isset($_FILES['file']['tmp_name'])
1154 && is_uploaded_file($_FILES['file']['tmp_name'])
1155 && $_FILES['file']['error'] == UPLOAD_ERR_OK
1156 )) {
1157 wp_redirect(sprintf("%soptions-general.php?page=http-headers&tab=advanced&status=ERR", get_admin_url()));
1158 exit;
1159 }
1160
1161 $string = @file_get_contents($_FILES['file']['tmp_name']);
1162 if ($string === false) {
1163 wp_redirect(sprintf("%soptions-general.php?page=http-headers&tab=advanced&status=ERR", get_admin_url()));
1164 exit;
1165 }
1166
1167 $arr = preg_split('/;(\s+)?\n/', $string);
1168 foreach ($arr as $statement) {
1169 $wpdb->query($statement);
1170 }
1171
1172 wp_redirect(sprintf("%soptions-general.php?page=http-headers&tab=advanced&status=OK", get_admin_url()));
1173 exit;
1174 }
1175
1176 function http_headers_post_export() {
1177 check_admin_referer('export');
1178 global $wpdb;
1179 include 'views/includes/config.inc.php';
1180 $statement = sprintf("SELECT * FROM wp_options WHERE option_name IN ('%s');", join("','", $options));
1181 $results = $wpdb->get_results($statement, ARRAY_A);
1182 $sql = array();
1183 foreach ($results as $item)
1184 {
1185 $value = str_replace("'", "''", $item['option_value']);
1186 $query = array();
1187 $query[] = "INSERT INTO wp_options (option_id, option_name, option_value, autoload)";
1188 $query[] = sprintf("VALUES (NULL, '%s', '%s', '%s')", $item['option_name'], $value, $item['autoload']);
1189 $query[] = sprintf("ON DUPLICATE KEY UPDATE option_value = '%s', autoload = '%s';", $value, $item['autoload']);
1190 $sql[] = join("\n", $query);
1191 }
1192
1193 $sql = join("\n\n", $sql);
1194 $length = function_exists('mb_strlen') ? mb_strlen($sql) : strlen($sql);
1195 $name = sprintf('WP-HTTP-Headers-%u.sql', time());
1196
1197 # Send headers
1198 header('Pragma: public');
1199 header('Expires: 0');
1200 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
1201 header('Cache-Control: private', false);
1202 header('Content-Transfer-Encoding: binary');
1203 header('Content-Disposition: attachment; filename="'.$name.'";');
1204 header('Content-Type: application/sql');
1205 header('Content-Length: ' . $length);
1206
1207 echo $sql;
1208 exit;
1209 }
1210
1211 if ( is_admin() ){ // admin actions
1212 add_action('admin_menu', 'http_headers_admin_add_page');
1213 add_action('admin_init', 'http_headers_admin');
1214 add_action("added_option", 'http_headers_option');
1215 add_action("updated_option", 'http_headers_option');
1216 add_action('admin_enqueue_scripts', 'http_headers_enqueue');
1217 add_action('after_setup_theme', 'http_headers_after_setup_theme');
1218 add_action('plugins_loaded', 'http_headers_text_domain');
1219 add_action('wp_ajax_inspect', 'http_headers_ajax_inspect');
1220 add_action('admin_post_import', 'http_headers_post_import');
1221 add_action('admin_post_export', 'http_headers_post_export');
1222 } else {
1223 // non-admin enqueues, actions, and filters
1224 add_action('send_headers', 'http_headers');
1225 }
1226
1227 function http_headers_admin_page() {
1228 include 'views/index.php';
1229 }