PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
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 / component / security-tweaks / hide-error.php

hide-error.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/component/security-tweaks/hide-error.php

323 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Component\Security_Tweaks;
4
5 use WP_Defender\Component\Security_Tweak as Security_Tweak_Component;
6 use WP_Defender\Component\Security_Tweaks\Servers\Server;
7 use WP_Error;
8 use Calotes\Base\Component;
9
10 /**
11 * Class Hide_Error
12 * @package WP_Defender\Component\Security_Tweaks
13 */
14 class Hide_Error extends Component {
15 public $slug = 'hide-error';
16 public $what_to_change = array();
17
18 /**
19 * @return bool
20 */
21 public function check() {
22 $data = $this->what_to_change();
23
24 return $data['resolved'];
25 }
26
27 /**
28 * Here is the code for processing, if the return is true, we add it to resolve list, WP_Error if any error.
29 *
30 * @return bool|\WP_Error
31 */
32 public function process() {
33 $data = $this->what_to_change();
34 $this->what_to_change = $data['required_change'];
35 if ( empty( $this->what_to_change ) ) {
36 return false;
37 }
38
39 if ( in_array( 'wp_debug', $this->what_to_change, true ) ) {
40 return $this->disable_debug();
41 } elseif ( in_array( 'wp_debug_display', $this->what_to_change, true ) ) {
42 return $this->disable_debug_display();
43 } elseif ( in_array( 'wp_debug_log', $this->what_to_change, true ) ) {
44 return $this->disable_debug_log();
45 }
46
47 return false;
48 }
49
50 /**
51 * This is for un-do stuff that has be done in @process.
52 *
53 * @return bool|\WP_Error
54 */
55 public function revert() {
56 $data = $this->what_to_change();
57 $this->what_to_change = $data['required_change'];
58 if ( empty( $this->what_to_change ) ) {
59 return false;
60 }
61
62 if ( in_array( 'wp_debug', $this->what_to_change, true ) ) {
63 return $this->enable_debug();
64 } elseif ( in_array( 'wp_debug_display', $this->what_to_change, true ) ) {
65 return $this->enable_debug_display();
66 } elseif ( in_array( 'wp_debug_log', $this->what_to_change, true ) ) {
67 return $this->enable_debug_log();
68 }
69
70 return false;
71 }
72
73 /**
74 * Shield up.
75 *
76 * @return bool
77 */
78 public function shield_up() {
79 return true;
80 }
81
82 /**
83 * Get whether to change WP_DEBUG or WP_DEBUG_DISPLAY constant.
84 * https://wordpress.org/support/article/debugging-in-wordpress/
85 *
86 * @return array
87 */
88 private function what_to_change() {
89 $debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
90 $debug_log = defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG;
91 $debug_display = defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
92 $what_to_change = array(
93 'wp_debug',
94 'wp_debug_log',
95 'wp_debug_display',
96 );
97 $resolved = false;
98 if ( $debug ) {
99 if ( $debug_log && $debug_display ) {
100 return array(
101 'resolved' => $resolved,
102 'required_change' => $what_to_change,
103 );
104 } elseif ( ! $debug_log && ! $debug_display ) {
105 $what_to_change = array( 'wp_debug' );
106 } elseif ( $debug_log && ! $debug_display ) {
107 $what_to_change = array( 'wp_debug', 'wp_debug_log' );
108 } elseif ( ! $debug_log && $debug_display ) {
109 $what_to_change = array( 'wp_debug', 'wp_debug_display' );
110 }
111 } else {
112 if ( ! $debug_log && ! $debug_display ) {
113 return array(
114 'resolved' => true,
115 'required_change' => $what_to_change,
116 );
117 } elseif ( $debug_log && $debug_display ) {
118 $what_to_change = array( 'wp_debug' );
119 } elseif ( ! $debug_log && $debug_display ) {
120 $what_to_change = array( 'wp_debug', 'wp_debug_log' );
121 } elseif ( $debug_log && ! $debug_display ) {
122 $what_to_change = array( 'wp_debug', 'wp_debug_display' );
123 }
124 $resolved = true;
125 }
126
127 return array(
128 'resolved' => $resolved,
129 'required_change' => $what_to_change,
130 );
131 }
132
133 /**
134 * Enable debugging.
135 *
136 * @return bool
137 */
138 private function enable_debug() {
139 return $this->set_debug_data( 'wp_debug', true );
140 }
141
142 /**
143 * Disable debugging.
144 *
145 * @return bool
146 */
147 private function disable_debug() {
148 return $this->set_debug_data( 'wp_debug', false );
149 }
150
151 /**
152 * Enable debug display.
153 *
154 * @return bool
155 */
156 private function enable_debug_display() {
157 return $this->set_debug_data( 'wp_debug_display', true );
158 }
159
160 /**
161 * Disable debug display.
162 *
163 * @return bool
164 */
165 private function disable_debug_display() {
166 return $this->set_debug_data( 'wp_debug_display', false );
167 }
168
169 /**
170 * Enable debug log.
171 *
172 * @return bool
173 */
174 private function enable_debug_log() {
175 return $this->set_debug_data( 'wp_debug_log', true );
176 }
177
178 /**
179 * Disable debug log.
180 *
181 * @return bool
182 */
183 private function disable_debug_log() {
184 return $this->set_debug_data( 'wp_debug_log', false );
185 }
186
187 /**
188 * Set debug data in wp-config.php.
189 *
190 * @param string $debug_type
191 * @param bool $value
192 *
193 * @return bool|WP_Error
194 */
195 private function set_debug_data( $debug_type, $value ) {
196 $sec_tweak_component = new Security_Tweak_Component();
197 $obj_file = 'flywheel' === Server::get_current_server()
198 ? $sec_tweak_component->advanced_check_file()
199 : $sec_tweak_component->file();
200 if ( false === $obj_file ) {
201 return new WP_Error(
202 'defender_file_not_writable',
203 __( 'The file wp-config.php is not writable', 'wpdef' )
204 );
205 } elseif ( is_numeric( $obj_file ) ) {
206 return new WP_Error(
207 'defender_file_not_writable',
208 $sec_tweak_component->show_hosting_notice( 'debug mode' )
209 );
210 }
211
212 $value = $value ? 'true' : 'false';
213 $pattern = $this->get_pattern( $debug_type );
214 $debug_type = strtoupper( $debug_type );
215 $hook_line_pattern = $sec_tweak_component->get_hook_line_pattern();
216 $debug_line = "define( '{$debug_type}', {$value} ); // Added by Defender";
217 $lines = array();
218 $line_found = false;
219 $hook_line_no = null;
220
221 foreach ( $obj_file as $line ) {
222 if ( ! $line_found && preg_match( $pattern, $line ) ) {
223 // If this is revert request and the changes is not made by us throw error.
224 if ( 'true' === $value && ! preg_match( "/^define\(\s*['|\"]{$debug_type}['|\"],(.*)\);\s*\/\/\s*Added\s*by\s*Defender.?.*/i", $line ) ) {
225 return new WP_Error(
226 'defender_file_not_writable',
227 $sec_tweak_component->show_hosting_notice_with_code( $debug_type, $debug_line )
228 );
229 }
230
231 $lines[] = $debug_line;
232 $line_found = true;
233 continue;
234 }
235
236 // If there is no match, keep reference of `hook_line_no` so that we can insert data there as needed.
237 if ( ! $line_found && preg_match( $hook_line_pattern, $line ) ) {
238 $hook_line_no = $obj_file->key();
239 $lines[ $hook_line_no + 1 ] = trim( $line );
240 continue;
241 }
242
243 $lines[] = trim( $line );
244 }
245
246 // There is no match, so set WP_DEBUG data just before the hook line ei: `$table_prefix`.
247 if ( ! $line_found && ! is_null( $hook_line_no ) ) {
248 $line_found = true;
249 $lines[ $hook_line_no ] = $debug_line;
250 ksort( $lines );
251 }
252
253 return $line_found
254 ? $sec_tweak_component->write( $lines )
255 : new WP_Error(
256 'defender_line_not_found',
257 __( 'Error writing to file.', 'wpdef' ),
258 404
259 );
260 }
261
262 /**
263 * Get pattern for any WP_DEBUG const.
264 *
265 * @param string $type
266 *
267 * @return string
268 */
269 private function get_pattern( $type ) {
270 if ( 'wp_debug' === $type ) {
271 return $this->get_wp_debug_pattern();
272 } elseif ( 'wp_debug_display' === $type ) {
273 return $this->get_wp_debug_display_pattern();
274 } else {
275 return $this->get_wp_debug_log_pattern();
276 }
277 }
278
279 /**
280 * Get pattern for WP_DEBUG.
281 *
282 * @return string
283 */
284 private function get_wp_debug_pattern() {
285 return "/^define\(\s*['|\"]WP_DEBUG['|\"],(.*)\)/";
286 }
287
288 /**
289 * Get pattern for WP_DEBUG_DISPLAY.
290 *
291 * @return string
292 */
293 private function get_wp_debug_display_pattern() {
294 return "/^define\(\s*['|\"]WP_DEBUG_DISPLAY['|\"], (.*)\)/";
295 }
296
297 /**
298 * Get pattern for WP_DEBUG_LOG.
299 *
300 * @return string
301 */
302 private function get_wp_debug_log_pattern() {
303 return "/^define\(\s*['|\"]WP_DEBUG_LOG['|\"], (.*)\)/";
304 }
305
306 /**
307 * Return a summary data of this tweak.
308 *
309 * @return array
310 */
311 public function to_array() {
312 return array(
313 'slug' => $this->slug,
314 'title' => __( 'Hide error reporting', 'wpdef' ),
315 'errorReason' => __( 'Error debugging is currently allowed.', 'wpdef' ),
316 'successReason' => __( 'You\'ve disabled all error reporting, Houston will never report a problem.', 'wpdef' ),
317 'misc' => array(),
318 'bulk_description' => __( 'Error debugging feature is useful for active development, but on live sites provides hackers yet another way to find loopholes in your site\'s security. We will disable error reporting for you.', 'wpdef' ),
319 'bulk_title' => __( 'Error Reporting', 'wpdef' ),
320 );
321 }
322 }
323