PluginProbe
GD Security Headers / 1.4
GD Security Headers v1.4
trunk 1.0 1.1 1.1.1 1.2 1.3 1.4 1.5 1.6 1.6.1 1.7 1.7.1 1.8 1.9
gd-security-headers / core / admin / plugin.php

plugin.php in GD Security Headers 1.4, at core/admin/plugin.php

355 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) { exit; }
4
5 class gdsih_admin_core extends d4p_admin_core {
6 public $plugin = 'gd-security-headers';
7
8 function __construct() {
9 parent::__construct();
10
11 $this->url = GDSIH_URL;
12
13 add_action('gdsih_plugin_init', array($this, 'core'));
14 }
15
16 public function core() {
17 parent::core();
18
19 add_action('network_admin_menu', array($this, 'admin_menu'));
20
21 add_filter('set-screen-option', array($this, 'screen_options_grid_rows_save'), 10, 3);
22
23 $this->init_ready();
24
25 if (gdsih_scope()->is_master_network_admin()) {
26 if (gdsih_settings()->is_install()) {
27 add_action('admin_notices', array($this, 'install_notice'));
28 }
29
30 if (gdsih_settings()->is_update()) {
31 add_action('admin_notices', array($this, 'update_notice'));
32 }
33 }
34 }
35
36 public function screen_options_grid_rows_save($status, $option, $value) {
37 if (in_array($option, array(
38 'gdsih_rows_per_page_csp_reports',
39 'gdsih_rows_per_page_xxp_reports'))) {
40 return $value;
41 }
42
43 return $status;
44 }
45
46 public function screen_options_grid_rows_csp_reports() {
47 $key = 'gdsih_rows_per_page_csp_reports';
48
49 $args = array(
50 'label' => __("Rows", "gd-security-headers"),
51 'default' => 25, 'option' => $key
52 );
53
54 add_screen_option('per_page', $args);
55
56 require_once(GDSIH_PATH.'core/grids/csp.php');
57
58 new gdsih_csp_report_grid();
59 }
60
61 public function screen_options_grid_rows_xxp_reports() {
62 $key = 'gdsih_rows_per_page_xxp_reports';
63
64 $args = array(
65 'label' => __("Rows", "gd-security-headers"),
66 'default' => 25, 'option' => $key
67 );
68
69 add_screen_option('per_page', $args);
70
71 require_once(GDSIH_PATH.'core/grids/xxp.php');
72
73 new gdsih_xxp_report_grid();
74 }
75
76 public function install_notice() {
77 if (current_user_can('install_plugins') && $this->page === false) {
78 echo '<div class="updated"><p>';
79 echo __("GD Security Headers is activated and it needs to finish installation.", "gd-security-headers");
80 echo ' <a href="'.network_admin_url('admin.php?page=gd-security-headers-front').'">'.__("Click Here", "gd-security-headers").'</a>.';
81 echo '</p></div>';
82 }
83 }
84
85 public function update_notice() {
86 if (current_user_can('install_plugins') && $this->page === false) {
87 echo '<div class="updated"><p>';
88 echo __("GD Security Headers is updated and it needs to finish the update process.", "gd-security-headers");
89 echo ' <a href="'.network_admin_url('admin.php?page=gd-security-headers-front').'">'.__("Click Here", "gd-security-headers").'</a>.';
90 echo '</p></div>';
91 }
92 }
93
94 public function init_ready() {
95 $this->menu_items = array(
96 'front' => array('title' => __("Overview", "gd-security-headers"), 'icon' => 'home'),
97 'about' => array('title' => __("About", "gd-security-headers"), 'icon' => 'info-circle'),
98 'csp-reports' => array('title' => __("CSP Reports", "gd-security-headers"), 'icon' => 'info-circle'),
99 'xxp-reports' => array('title' => __("XXP Reports", "gd-security-headers"), 'icon' => 'info-circle'),
100 'headers' => array('title' => __("Headers", "gd-security-headers"), 'icon' => 'code'),
101 'settings' => array('title' => __("Settings", "gd-security-headers"), 'icon' => 'cogs'),
102 'tools' => array('title' => __("Tools", "gd-security-headers"), 'icon' => 'wrench')
103 );
104 }
105
106 public function admin_init() {
107 d4p_include('grid', 'admin', GDSIH_D4PLIB);
108
109 do_action('gdsih_admin_init');
110 }
111
112 public function title() {
113 return 'GD Security Headers';
114 }
115
116 public function admin_menu() {
117 if (is_multisite() && is_blog_admin()) {
118 return;
119 }
120
121 $parent = 'gd-security-headers-front';
122
123 $this->page_ids[] = add_menu_page(
124 'GD Security Headers',
125 'Security Headers',
126 gdsih()->cap,
127 $parent,
128 array($this, 'panel_general'),
129 gdsih()->svg_icon);
130
131 foreach($this->menu_items as $item => $data) {
132 $this->page_ids[] = add_submenu_page($parent,
133 'GD Security Headers: '.$data['title'],
134 $data['title'],
135 gdsih()->cap,
136 'gd-security-headers-'.$item,
137 array($this, 'panel_general'));
138 }
139
140 $this->admin_load_hooks();
141 }
142
143 public function enqueue_scripts($hook) {
144 $load_admin_data = false;
145
146 if ($this->page !== false) {
147 d4p_admin_enqueue_defaults();
148
149 wp_enqueue_script('jquery-form');
150
151 wp_enqueue_style('fontawesome', GDSIH_URL.'d4plib/resources/fontawesome/css/font-awesome.min.css');
152
153 wp_enqueue_style('d4plib-font', $this->file('css', 'font', true), array(), D4P_VERSION);
154 wp_enqueue_style('d4plib-shared', $this->file('css', 'shared', true), array(), D4P_VERSION);
155 wp_enqueue_style('d4plib-admin', $this->file('css', 'admin', true), array('d4plib-shared'), D4P_VERSION);
156
157 wp_enqueue_script('d4plib-shared', $this->file('js', 'shared', true), array('jquery', 'wp-color-picker'), D4P_VERSION, true);
158 wp_enqueue_script('d4plib-admin', $this->file('js', 'admin', true), array('d4plib-shared'), D4P_VERSION, true);
159
160 wp_enqueue_style('gdsih-plugin', $this->file('css', 'plugin'), array('d4plib-admin', 'wp-jquery-ui-dialog'), gdsih_settings()->file_version());
161 wp_enqueue_script('gdsih-plugin', $this->file('js', 'plugin'), array('d4plib-admin', 'wpdialogs'), gdsih_settings()->file_version(), true);
162
163 if ($this->page == 'about') {
164 wp_enqueue_style('d4plib-grid', $this->file('css', 'grid', true), array(), D4P_VERSION.'.'.D4P_BUILD);
165 }
166
167 $_data = array(
168 'nonce' => wp_create_nonce('gdsih-admin-internal'),
169 'wp_version' => GDSIH_WPV,
170 'page' => $this->page,
171 'panel' => $this->panel,
172 'button_icon_ok' => '<i class="fa fa-check fa-fw" aria-hidden="true"></i> ',
173 'button_icon_cancel' => '<i class="fa fa-times fa-fw" aria-hidden="true"></i> ',
174 'button_icon_delete' => '<i class="fa fa-trash fa-fw" aria-hidden="true"></i> ',
175 'dialog_button_ok' => __("OK", "gd-security-headers"),
176 'dialog_button_cancel' => __("Cancel", "gd-security-headers"),
177 'dialog_button_delete' => __("Delete", "gd-security-headers"),
178 'dialog_button_remove' => __("Remove", "gd-security-headers"),
179 'dialog_button_clear' => __("Clear", "gd-security-headers"),
180 'dialog_title_areyousure' => __("Are you sure you want to do this?", "gd-security-headers"),
181 'dialog_content_pleasewait' => __("Please Wait...", "gd-security-headers")
182 );
183
184 wp_localize_script('gdsih-plugin', 'gdsih_data', $_data);
185
186 $load_admin_data = true;
187 }
188
189 if ($load_admin_data) {
190 wp_localize_script('d4plib-shared', 'd4plib_admin_data', array(
191 'string_media_image_title' => __("Select Image", "gd-security-headers"),
192 'string_media_image_button' => __("Use Selected Image", "gd-security-headers"),
193 'string_are_you_sure' => __("Are you sure you want to do this?", "gd-security-headers"),
194 'string_image_not_selected' => __("Image not selected.", "gd-security-headers")
195 ));
196 }
197 }
198
199 public function admin_load_hooks() {
200 foreach ($this->page_ids as $id) {
201 add_action('load-'.$id, array($this, 'load_admin_page'));
202 }
203
204 add_action('load-security-headers_page_gd-security-headers-csp-reports', array($this, 'screen_options_grid_rows_csp_reports'));
205 add_action('load-security-headers_page_gd-security-headers-xxp-reports', array($this, 'screen_options_grid_rows_xxp_reports'));
206 }
207
208 public function current_screen($screen) {
209 if (isset($_GET['panel']) && $_GET['panel'] != '') {
210 $this->panel = d4p_sanitize_slug($_GET['panel']);
211 }
212
213 $id = $screen->id;
214
215 if (gdsih_scope()->is_network_admin()) {
216 if ($id == 'toplevel_page_gd-security-headers-front-network') {
217 $this->page = 'front';
218 } else if (substr($id, 0, 42) == 'security-headers_page_gd-security-headers-') {
219 $this->page = substr($id, 42, strlen($id) - 50);
220 }
221 } else {
222 if ($id == 'toplevel_page_gd-security-headers-front') {
223 $this->page = 'front';
224 } else if (substr($id, 0, 42) == 'security-headers_page_gd-security-headers-') {
225 $this->page = substr($id, 42);
226 }
227 }
228
229 if (is_super_admin()) {
230 if (isset($_POST['gdsih_handler']) && $_POST['gdsih_handler'] == 'postback') {
231 require_once(GDSIH_PATH.'core/admin/postback.php');
232
233 new gdsih_admin_postback();
234 } else if (isset($_GET['gdsih_handler']) && $_GET['gdsih_handler'] == 'getback') {
235 require_once(GDSIH_PATH.'core/admin/getback.php');
236
237 new gdsih_admin_getback();
238 }
239 }
240 }
241
242 public function help_tab_getting_help() {
243 if ($this->panel == 'csp') {
244 get_current_screen()->add_help_tab(
245 array(
246 'id' => 'gdsec-help-settings-csp-header',
247 'title' => __("CSP Header", "gd-security-headers"),
248 'content' => $this->help_csp_header()
249 )
250 );
251
252 get_current_screen()->add_help_tab(
253 array(
254 'id' => 'gdsih-help-settings-csp-res',
255 'title' => __("CSP Resources", "gd-security-headers"),
256 'content' => $this->help_csp_resources()
257 )
258 );
259 }
260
261 get_current_screen()->add_help_tab(
262 array(
263 'id' => 'd4p-help-info',
264 'title' => __("Getting Help", "gd-security-headers"),
265 'content' => '<p>'.__("To get help with this plugin, you can start with Knowledge Base list of frequently asked questions and articles. If you have any questions, or you want to report a bug, or you have a suggestion, you can use support forum. All important links for this are on the right side of this help dialog.", "gd-security-headers").'</p>'
266 )
267 );
268 }
269
270 public function help_csp_header() {
271 $render = '<p>'.__("There are few more things you need to think about when setting up this rating addon.", "gd-security-headers").'</p>';
272 $render.= '<ul>';
273 $render.= '<li>'.__("Do not switch to the Live policy mode before you make all the tests with the Report policy mode.", "gd-security-headers").'</li>';
274 $render.= '<li>'.__("During the testing phase, it is best to disable Log option, or you will end up with a lot of reports logged. Use Log feature when you switch to Live policy mode.", "gd-security-headers").'</li>';
275 $render.= '<li>'.__("To test CSP, use Google Chrome or Mozilla Firefox with Firebug. But will display detailed information in the Console about each CSP issue.", "gd-security-headers").'</li>';
276 $render.= '</ul>';
277 $render.= '<p>'.__("To make sure you add valid sources to all source rules, here are few examples on what is accepted by the browsers. You can use '*' character as wildcard.", "gd-security-headers").'</p>';
278 $render.= '<ul>';
279 $render.= '<li><strong>https:</strong> - '.__("Matches any url over HTTPS scheme.", "gd-security-headers").'</li>';
280 $render.= '<li><strong>example.com</strong> - '.__("Matches both HTTP and HTTPS version of the URL.", "gd-security-headers").'</li>';
281 $render.= '<li><strong>https://*.example.com</strong> - '.__("Matches HTTPS subdomains for the URL, but now the main domain.", "gd-security-headers").'</li>';
282 $render.= '<li><strong>www.example.com:443</strong> - '.__("Matches exact domain URL, with the specified port.", "gd-security-headers").'</li>';
283 $render.= '<li><strong>*://*.example.com:*</strong> - '.__("Matches any scheme for subdomain and any port, but not the main domain.", "gd-security-headers").'</li>';
284 $render.= '<li><strong>www.example.com</strong> - '.__("Matches exact domain URL, no other subdomains.", "gd-security-headers").'</li>';
285 $render.= '</ul>';
286
287 return $render;
288 }
289
290 public function help_csp_resources() {
291 $render = '<p>'.__("To get more information about the Content Security Policy, check out these online resources.", "gd-security-headers").'</p>';
292 $render.= '<ul>';
293 $render.= '<li><a href="https://content-security-policy.com/" target="_blank">Content Security Policy (CSP) Quick Reference Guide</a></li>';
294 $render.= '<li><a href="https://www.w3.org/TR/CSP/" target="_blank">W3C - Content Security Policy</a></li>';
295 $render.= '</ul>';
296
297 return $render;
298 }
299
300 public function load_admin_page() {
301 $this->help_tab_sidebar();
302
303 do_action('gdsih_load_admin_page_'.$this->page);
304
305 if ($this->panel !== false && $this->panel != '') {
306 do_action('gdsih_load_admin_page_'.$this->page.'_'.$this->panel);
307 }
308
309 $this->help_tab_getting_help();
310 }
311
312 public function install_or_update() {
313 $install = gdsih_settings()->is_install();
314 $update = gdsih_settings()->is_update();
315
316 if ($install) {
317 include(GDSIH_PATH.'forms/install.php');
318 } else if ($update) {
319 include(GDSIH_PATH.'forms/update.php');
320 }
321
322 return $install || $update;
323 }
324
325 public function panel_general() {
326 if (!$this->install_or_update()) {
327 $path = GDSIH_PATH.'forms/'.$this->page.'.php';
328
329 $path = apply_filters('gdsih_admin_panel_'.$this->page, $path);
330
331 include($path);
332 }
333 }
334
335 public function current_url($with_panel = true) {
336 $page = 'admin.php?page=gd-security-headers-';
337
338 $page.= $this->page;
339
340 if ($with_panel && $this->panel !== false && $this->panel != '') {
341 $page.= '&panel='.$this->panel;
342 }
343
344 return self_admin_url($page);
345 }
346 }
347
348 global $_gdsih_core_admin;
349 $_gdsih_core_admin = new gdsih_admin_core();
350
351 function gdsih_admin() {
352 global $_gdsih_core_admin;
353 return $_gdsih_core_admin;
354 }
355