PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.0
Defender Security – Malware Scanner, Login Security & Firewall v5.6.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / controller / class-security-headers.php

class-security-headers.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.0, at src/controller/class-security-headers.php

244 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles security headers settings.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use Calotes\Helper\HTTP;
12 use Calotes\Component\Request;
13 use Calotes\Component\Response;
14 use WP_Defender\Component\Config\Config_Hub_Helper;
15
16 /**
17 * Class Security_Headers
18 * Contains methods to handle security headers.
19 */
20 class Security_Headers extends Event {
21
22 /**
23 * The model for handling the data.
24 *
25 * @var \WP_Defender\Model\Setting\Security_Headers
26 */
27 public $model;
28
29 /**
30 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
31 */
32 public function __construct() {
33 add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) );
34 $this->model = wd_di()->get( \WP_Defender\Model\Setting\Security_Headers::class );
35 $this->init_headers();
36 $this->register_routes();
37 }
38
39 /**
40 * Safe way to get cached model.
41 *
42 * @return \WP_Defender\Model\Setting\Security_Headers
43 */
44 private function get_model() {
45 if ( is_object( $this->model ) ) {
46 return $this->model;
47 }
48
49 return new \WP_Defender\Model\Setting\Security_Headers();
50 }
51
52 /**
53 * Provide data to the frontend via localized script.
54 *
55 * @param array $data Data collection is ready to passed.
56 *
57 * @return array Modified data array with added this controller data.
58 */
59 public function script_data( array $data ): array {
60 $data['security_headers'] = $this->data_frontend();
61
62 return $data;
63 }
64
65 /**
66 * Save settings.
67 *
68 * @param Request $request The request object containing new settings data.
69 *
70 * @return Response
71 * @defender_route
72 */
73 public function save_settings( Request $request ) {
74 $data = $request->get_data_by_model( $this->model );
75 $this->model->import( $data );
76 if ( $this->model->validate() ) {
77 $this->model->save();
78 Config_Hub_Helper::set_clear_active_flag();
79
80 if ( $this->maybe_track() ) {
81 // The current model data.
82 $is_active_curr_data = $this->get_model()->is_any_activated();
83 // The previous model data.
84 $prev_data = $this->get_model()->get_old_settings();
85
86 $is_active_prev_data = false;
87
88 if ( ! empty( $prev_data ) ) {
89 $is_active_prev_data = true === $prev_data['sh_xframe'] || true === $prev_data['sh_xss_protection']
90 || true === $prev_data['sh_content_type_options'] || true === $prev_data['sh_feature_policy']
91 || true === $prev_data['sh_strict_transport'] || true === $prev_data['sh_referrer_policy'];
92 }
93
94 $need_track = false;
95
96 if ( $is_active_prev_data && ! $is_active_curr_data ) {
97 $need_track = true;
98 $event = 'def_feature_deactivated';
99 } elseif ( ! $is_active_prev_data && $is_active_curr_data ) {
100 $need_track = true;
101 $event = 'def_feature_activated';
102 }
103
104 // Other conditions without State's changes.
105 if ( $need_track ) {
106 $data = array(
107 'Feature' => 'Security Headers',
108 'Triggered From' => 'Feature page',
109 );
110 $this->track_feature( $event, $data );
111 }
112 }
113
114 return new Response(
115 true,
116 array_merge(
117 array(
118 'message' => esc_html__( 'Your settings have been updated.', 'defender-security' ),
119 'auto_close' => true,
120 ),
121 $this->data_frontend()
122 )
123 );
124 }
125
126 return new Response( false, array( 'message' => $this->model->get_formatted_errors() ) );
127 }
128
129 /**
130 * Init headers.
131 *
132 * @return void
133 */
134 public function init_headers(): void {
135 if ( ! defined( 'DOING_AJAX' ) ) {
136 // Refresh if on admin, on page with headers.
137 if ( ( is_admin() || is_network_admin() )
138 &&
139 (
140 ( 'wdf-advanced-tools' === HTTP::get( 'page' ) )
141 || ( 'wp-defender' === HTTP::get( 'page' ) )
142 )
143 ) {
144 // This meant we don't have any data or data is overdue need to refresh list of headers.
145 $this->model->refresh_headers();
146 } elseif ( defined( 'DOING_CRON' ) ) {
147 // If this is in cronjob, we refresh it too.
148 $this->model->refresh_headers();
149 }
150 }
151
152 foreach ( $this->model->get_headers() as $rule ) {
153 $rule->add_hooks();
154 }
155 }
156
157 /**
158 * Removes settings for all submodules.
159 */
160 public function remove_settings() {
161 }
162
163 /**
164 * Delete all the data & the cache.
165 */
166 public function remove_data() {
167 }
168
169 /**
170 * Converts the current object state to an array.
171 *
172 * @return array The array representation of the object.
173 */
174 public function to_array() {
175 $misc = $this->get_model()->refresh_headers();
176
177 return array_slice( $misc, 0, 3 );
178 }
179
180 /**
181 * Get data about headers.
182 *
183 * @return array
184 */
185 public function get_type_headers(): array {
186 return $this->get_model()->get_headers_by_type();
187 }
188
189 /**
190 * Provides data for the frontend.
191 *
192 * @return array An array of data for the frontend.
193 */
194 public function data_frontend(): array {
195 $model = $this->get_model();
196
197 return array_merge(
198 array(
199 'model' => $model->export(),
200 'misc' => $model->get_headers_as_array( true ),
201 'enabled' => $model->get_enabled_headers( 3 ),
202 ),
203 $this->dump_routes_and_nonces()
204 );
205 }
206
207 /**
208 * Provides data for the dashboard widget.
209 *
210 * @return array An array of dashboard widget data.
211 */
212 public function dashboard_widget(): array {
213 return array( 'enabled' => $this->get_model()->get_enabled_headers( 3 ) );
214 }
215
216 /**
217 * Imports data into the model.
218 *
219 * @param array $data Data to be imported into the model.
220 */
221 public function import_data( array $data ) {
222 $model = $this->get_model();
223
224 $model->import( $data );
225 if ( $model->validate() ) {
226 $model->save();
227 }
228 }
229
230 /**
231 * Exports strings.
232 *
233 * @return array An array of strings.
234 */
235 public function export_strings(): array {
236 return array(
237 $this->get_model()->is_any_activated() ? esc_html__( 'Active', 'defender-security' ) : esc_html__(
238 'Inactive',
239 'defender-security'
240 ),
241 );
242 }
243 }
244