PluginProbe
HTTP Headers / 1.4.0
HTTP Headers v1.4.0
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.4.0, at http-headers.php

311 lines 11.9 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: This plugin adds CORS & security HTTP headers to your website. Improves your website overall security.
6 Version: 1.4.0
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 (get_option('hh_strict_transport_security_max_age') === false) {
31 $value = get_option('hh_strict_transport_security_value');
32 $max_age = preg_match('/max-age=(\d+)/', $value, $match) ? $match[1] : 0;
33 $sub_domains = strpos($value, 'includeSubDomains') !== false ? 1 : 0;
34 add_option('hh_strict_transport_security_max_age', $max_age, null, 'yes');
35 add_option('hh_strict_transport_security_sub_domains', $sub_domains, null, 'yes');
36 add_option('hh_strict_transport_security_preload', 0, null, 'yes');
37 }
38
39 if (get_option('hh_referrer_policy') === false) {
40 add_option('hh_referrer_policy', 0, null, 'yes');
41 add_option('hh_referrer_policy_value', null, null, 'yes');
42 }
43
44 if (get_option('hh_content_security_policy') === false) {
45 add_option('hh_content_security_policy', 0, null, 'yes');
46 add_option('hh_content_security_policy_value', null, null, 'yes');
47 }
48
49 if (get_option('hh_method') === false) {
50 add_option('hh_method', 'php', null, 'yes');
51 }
52
53 function get_http_headers() {
54 $headers = array();
55 if (get_option('hh_x_frame_options') == 1) {
56 $x_frame_options_value = strtoupper(get_option('hh_x_frame_options_value'));
57 if ($x_frame_options_value == 'ALLOW-FROM') {
58 $x_frame_options_value .= ' ' . get_option('hh_x_frame_options_domain');
59 }
60 $headers['X-Frame-Options'] = $x_frame_options_value;
61 }
62 if (get_option('hh_x_xxs_protection') == 1) {
63 $headers['X-XSS-Protection'] = get_option('hh_x_xxs_protection_value');
64 }
65 if (get_option('hh_x_content_type_options') == 1) {
66 $headers['X-Content-Type-Options'] = get_option('hh_x_content_type_options_value');
67 }
68 if (get_option('hh_strict_transport_security') == 1) {
69 $hh_strict_transport_security = array();
70
71 $hh_strict_transport_security_max_age = get_option('hh_strict_transport_security_max_age');
72 if ($hh_strict_transport_security_max_age !== false)
73 {
74 $hh_strict_transport_security[] = sprintf('max-age=%u', get_option('hh_strict_transport_security_max_age'));
75 if (get_option('hh_strict_transport_security_sub_domains'))
76 {
77 $hh_strict_transport_security[] = 'includeSubDomains';
78 }
79 if (get_option('hh_strict_transport_security_preload'))
80 {
81 $hh_strict_transport_security[] = 'preload';
82 }
83 } else {
84 $hh_strict_transport_security = array(get_option('hh_strict_transport_security_value'));
85 }
86 $headers['Strict-Transport-Security'] = join('; ', $hh_strict_transport_security);
87 }
88 if (get_option('hh_x_ua_compatible') == 1) {
89 $headers['X-UA-Compatible'] = get_option('hh_x_ua_compatible_value');
90 }
91 if (get_option('hh_public_key_pins') == 1) {
92 $public_key_pins_sha256_1 = get_option('hh_public_key_pins_sha256_1');
93 $public_key_pins_sha256_2 = get_option('hh_public_key_pins_sha256_2');
94 $public_key_pins_max_age = get_option('hh_public_key_pins_max_age');
95 $public_key_pins_sub_domains = get_option('hh_public_key_pins_sub_domains');
96 $public_key_pins_report_uri = get_option('hh_public_key_pins_report_uri');
97 if (!empty($public_key_pins_sha256_1) && !empty($public_key_pins_sha256_2) && !empty($public_key_pins_max_age)) {
98
99 $public_key_pins = array();
100 $public_key_pins[] = sprintf('pin-sha256="%s"', $public_key_pins_sha256_1);
101 $public_key_pins[] = sprintf('pin-sha256="%s"', $public_key_pins_sha256_2);
102 $public_key_pins[] = sprintf("max-age=%u", $public_key_pins_max_age);
103 if ($public_key_pins_sub_domains) {
104 $public_key_pins[] = "includeSubDomains";
105 }
106 if (!empty($public_key_pins_report_uri)) {
107 $public_key_pins[] = sprintf('report-uri="%s"', $public_key_pins_report_uri);
108 }
109 $headers['Public-Key-Pins'] = join('; ', $public_key_pins);
110 }
111 }
112
113 if (get_option('hh_content_security_policy') == 1)
114 {
115 $csp = array();
116 $values = get_option('hh_content_security_policy_value');
117 foreach ($values as $key => $val)
118 {
119 if (!empty($val))
120 {
121 $csp[] = sprintf("%s %s", $key, $val);
122 }
123 }
124 if (!empty($csp))
125 {
126 $headers['Content-Security-Policy'] = join('; ', $csp);
127 }
128 }
129
130 if (get_option('hh_access_control_allow_origin') == 1)
131 {
132 $value = get_option('hh_access_control_allow_origin_value');
133 switch ($value)
134 {
135 case 'HTTP_ORIGIN':
136 $value = @$_SERVER['HTTP_ORIGIN'];
137 break;
138 case 'origin':
139 $value = get_option('hh_access_control_allow_origin_url');
140 break;
141 }
142 if (!empty($value))
143 {
144 $headers['Access-Control-Allow-Origin'] = $value;
145 }
146 }
147 if (get_option('hh_access_control_allow_credentials') == 1)
148 {
149 $headers['Access-Control-Allow-Credentials'] = get_option('hh_access_control_allow_credentials_value');
150 }
151 if (get_option('hh_access_control_max_age') == 1)
152 {
153 $value = get_option('hh_access_control_max_age_value');
154 if (!empty($value))
155 {
156 $headers['Access-Control-Max-Age'] = intval($value);
157 }
158 }
159 if (get_option('hh_access_control_allow_methods') == 1)
160 {
161 $value = get_option('hh_access_control_allow_methods_value');
162 if (!empty($value))
163 {
164 $headers['Access-Control-Allow-Methods'] = join(', ', array_keys($value));
165 }
166 }
167 if (get_option('hh_access_control_allow_headers') == 1)
168 {
169 $value = get_option('hh_access_control_allow_headers_value');
170 if (!empty($value))
171 {
172 $headers['Access-Control-Allow-Headers'] = join(', ', array_keys($value));
173 }
174 }
175 if (get_option('hh_access_control_expose_headers') == 1)
176 {
177 $value = get_option('hh_access_control_expose_headers_value');
178 if (!empty($value))
179 {
180 $headers['Access-Control-Expose-Headers'] = join(', ', array_keys($value));
181 }
182 }
183 if (get_option('hh_p3p') == 1)
184 {
185 $value = get_option('hh_p3p_value');
186 if (!empty($value))
187 {
188 $headers['P3P'] = 'CP="' . join(' ', array_keys($value)) . '"';
189 }
190 }
191 if (get_option('hh_referrer_policy') == 1) {
192 $headers['Referrer-Policy'] = get_option('hh_referrer_policy_value');
193 }
194
195 return $headers;
196 }
197
198 function http_headers() {
199 if (get_option('hh_method') !== 'php') {
200 return;
201 }
202 $headers = get_http_headers();
203 foreach ($headers as $key => $value)
204 {
205 header(sprintf("%s: %s", $key, $value));
206 }
207 }
208
209 function http_headers_admin_add_page() {
210 add_options_page('HTTP Headers', 'HTTP Headers', 'manage_options', 'http-headers', 'http_headers_admin_page');
211 }
212
213 function http_headers_admin() {
214 register_setting('http-headers-mtd', 'hh_method');
215 register_setting('http-headers-xfo', 'hh_x_frame_options');
216 register_setting('http-headers-xfo', 'hh_x_frame_options_value');
217 register_setting('http-headers-xfo', 'hh_x_frame_options_domain');
218 register_setting('http-headers-xss', 'hh_x_xxs_protection');
219 register_setting('http-headers-xss', 'hh_x_xxs_protection_value');
220 register_setting('http-headers-cto', 'hh_x_content_type_options');
221 register_setting('http-headers-cto', 'hh_x_content_type_options_value');
222 register_setting('http-headers-sts', 'hh_strict_transport_security');
223 register_setting('http-headers-sts', 'hh_strict_transport_security_value'); //obsolete
224 register_setting('http-headers-sts', 'hh_strict_transport_security_max_age');
225 register_setting('http-headers-sts', 'hh_strict_transport_security_sub_domains');
226 register_setting('http-headers-sts', 'hh_strict_transport_security_preload');
227 register_setting('http-headers-pkp', 'hh_public_key_pins');
228 register_setting('http-headers-pkp', 'hh_public_key_pins_sha256_1');
229 register_setting('http-headers-pkp', 'hh_public_key_pins_sha256_2');
230 register_setting('http-headers-pkp', 'hh_public_key_pins_max_age');
231 register_setting('http-headers-pkp', 'hh_public_key_pins_sub_domains');
232 register_setting('http-headers-pkp', 'hh_public_key_pins_report_uri');
233 register_setting('http-headers-uac', 'hh_x_ua_compatible');
234 register_setting('http-headers-uac', 'hh_x_ua_compatible_value');
235 register_setting('http-headers-p3p', 'hh_p3p');
236 register_setting('http-headers-p3p', 'hh_p3p_value');
237 register_setting('http-headers-rp', 'hh_referrer_policy');
238 register_setting('http-headers-rp', 'hh_referrer_policy_value');
239 register_setting('http-headers-csp', 'hh_content_security_policy');
240 register_setting('http-headers-csp', 'hh_content_security_policy_value');
241 register_setting('http-headers-acao', 'hh_access_control_allow_origin');
242 register_setting('http-headers-acao', 'hh_access_control_allow_origin_value');
243 register_setting('http-headers-acao', 'hh_access_control_allow_origin_url');
244 register_setting('http-headers-acac', 'hh_access_control_allow_credentials');
245 register_setting('http-headers-acac', 'hh_access_control_allow_credentials_value');
246 register_setting('http-headers-acam', 'hh_access_control_allow_methods');
247 register_setting('http-headers-acam', 'hh_access_control_allow_methods_value');
248 register_setting('http-headers-acah', 'hh_access_control_allow_headers');
249 register_setting('http-headers-acah', 'hh_access_control_allow_headers_value');
250 register_setting('http-headers-aceh', 'hh_access_control_expose_headers');
251 register_setting('http-headers-aceh', 'hh_access_control_expose_headers_value');
252 register_setting('http-headers-acma', 'hh_access_control_max_age');
253 register_setting('http-headers-acma', 'hh_access_control_max_age_value');
254
255 if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true')
256 {
257 $htaccess_file = get_home_path().'.htaccess';
258 if (get_option('hh_method') == 'htaccess')
259 {
260 $lines = array();
261 $lines[] = '<FilesMatch "\.(php|html)$">';
262 $lines[] = ' <IfModule mod_headers.c>';
263 $headers = get_http_headers();
264 foreach ($headers as $key => $value)
265 {
266 $lines[] = sprintf(' Header set %s %s', $key, sprintf('%1$s%2$s%1$s', strpos($value, '"') === false ? '"' : "'", $value));
267 }
268 $lines[] = ' </IfModule>';
269 $lines[] = '</FilesMatch>';
270
271 insert_with_markers($htaccess_file, "HttpHeaders", $lines);
272 } else {
273 insert_with_markers($htaccess_file, "HttpHeaders", array());
274 }
275 }
276 }
277
278 function http_headers_settings_link( $links ) {
279 $url = get_admin_url() . 'options-general.php?page=http-headers';
280 $settings_link = '<a href="' . $url . '">' . __('Settings') . '</a>';
281 array_unshift( $links, $settings_link );
282 return $links;
283 }
284
285 function http_headers_after_setup_theme() {
286 add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'http_headers_settings_link');
287 }
288
289 function http_headers_enqueue($hook) {
290 if ( 'http-headers.php' != $hook ) {
291 # FIXME
292 //return;
293 }
294
295 wp_enqueue_script('http_headers_admin_scripts', plugin_dir_url( __FILE__ ) . 'assets/scripts.js');
296 wp_enqueue_style('http_headers_admin_styles', plugin_dir_url( __FILE__ ) . 'assets/styles.css');
297 }
298
299 if ( is_admin() ){ // admin actions
300 add_action('admin_menu', 'http_headers_admin_add_page');
301 add_action('admin_init', 'http_headers_admin');
302 add_action('admin_enqueue_scripts', 'http_headers_enqueue');
303 add_action('after_setup_theme', 'http_headers_after_setup_theme');
304 } else {
305 // non-admin enqueues, actions, and filters
306 add_action('send_headers', 'http_headers');
307 }
308
309 function http_headers_admin_page() {
310 include 'views/index.php';
311 }