PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.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 / component / security-tweaks / class-hide-error.php

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

184 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles the functionality to hide error reporting in a WordPress environment.
4 *
5 * @package WP_Defender\Component\Security_Tweaks
6 */
7
8 namespace WP_Defender\Component\Security_Tweaks;
9
10 use WP_Error;
11 use WP_Defender\Component\Security_Tweaks\Servers\Server;
12 use WP_Defender\Component\Security_Tweak as Security_Tweak_Component;
13
14 /**
15 * It provides methods to check, enable, disable, and revert the error reporting settings.
16 */
17 class Hide_Error extends Abstract_Security_Tweaks {
18
19 /**
20 * Slug identifier for the component.
21 *
22 * @var string
23 */
24 public string $slug = 'hide-error';
25
26 /**
27 * Check whether the issue has been resolved or not.
28 *
29 * @return bool
30 */
31 public function check(): bool {
32 $debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
33
34 return ! $debug;
35 }
36
37 /**
38 * Here is the code for processing, if the return is true, we add it to resolve list, WP_Error if any error.
39 *
40 * @return bool|WP_Error
41 */
42 public function process() {
43 return $this->set_debug_data( false );
44 }
45
46 /**
47 * This is for un-do stuff that has be done in @process.
48 *
49 * @return bool|WP_Error
50 */
51 public function revert() {
52 return $this->set_debug_data( true );
53 }
54
55 /**
56 * Shield up.
57 *
58 * @return bool
59 */
60 public function shield_up(): bool {
61 return true;
62 }
63
64 /**
65 * Set debug data in wp-config.php.
66 *
67 * @param bool $value of constant.
68 *
69 * @return bool|WP_Error
70 */
71 private function set_debug_data( bool $value ) {
72 $sec_tweak_component = new Security_Tweak_Component();
73 $obj_file = 'flywheel' === Server::get_current_server()
74 ? $sec_tweak_component->advanced_check_file()
75 : $sec_tweak_component->file();
76 if ( false === $obj_file ) {
77 return new WP_Error(
78 'defender_file_not_writable',
79 esc_html__( 'The file wp-config.php is not writable', 'defender-security' )
80 );
81 } elseif ( is_numeric( $obj_file ) ) {
82 return new WP_Error(
83 'defender_file_not_writable',
84 $sec_tweak_component->show_hosting_notice( 'debug mode' )
85 );
86 }
87
88 $value = $value ? 'true' : 'false';
89 $pattern = "/^define\(\s*['|\"]WP_DEBUG['|\"],(.*)\)/";
90 $debug_type = 'WP_DEBUG';
91 $hook_line_pattern = $sec_tweak_component->get_hook_line_pattern();
92 $debug_line = "define( '{$debug_type}', {$value} ); // Added by Defender";
93 $lines = array();
94 $line_found = false;
95 $hook_line_no = null;
96
97 foreach ( $obj_file as $line ) {
98 if ( ! $line_found && preg_match( $pattern, $line ) ) {
99 // If this is revert request and the changes is not made by us throw error.
100 if ( 'true' === $value && ! preg_match(
101 "/^define\(\s*['|\"]{$debug_type}['|\"],(.*)\);\s*\/\/\s*Added\s*by\s*Defender.?.*/i",
102 $line
103 ) ) {
104 return new WP_Error(
105 'defender_file_not_writable',
106 $sec_tweak_component->show_hosting_notice_with_code( $debug_type, $debug_line )
107 );
108 }
109
110 $lines[] = $debug_line;
111 $line_found = true;
112 continue;
113 }
114
115 // If there is no match, keep reference of `hook_line_no` so that we can insert data there as needed.
116 if ( ! $line_found && preg_match( $hook_line_pattern, $line ) ) {
117 $hook_line_no = $obj_file->key();
118 $lines[ $hook_line_no + 1 ] = trim( $line );
119 continue;
120 }
121
122 $lines[] = trim( $line );
123 }
124
125 // There is no match, so set WP_DEBUG data just before the hook line ei: `$table_prefix`.
126 if ( ! $line_found && ! is_null( $hook_line_no ) ) {
127 $line_found = true;
128 $lines[ $hook_line_no ] = $debug_line;
129 ksort( $lines );
130 }
131
132 return $line_found
133 ? $sec_tweak_component->write( $lines )
134 : new WP_Error(
135 'defender_line_not_found',
136 esc_html__( 'Error writing to file.', 'defender-security' ),
137 404
138 );
139 }
140
141 /**
142 * Retrieve the tweak's label.
143 *
144 * @return string
145 */
146 public function get_label(): string {
147 return esc_html__( 'Turn off error debugging', 'defender-security' );
148 }
149
150 /**
151 * Get the error reason.
152 *
153 * @return string
154 */
155 public function get_error_reason(): string {
156 return wp_kses(
157 __( '<strong>Error debugging is turned on</strong> and may expose details hackers can use.', 'defender-security' ),
158 array(
159 'strong' => array(),
160 )
161 );
162 }
163
164 /**
165 * Return a summary data of this tweak.
166 *
167 * @return array
168 */
169 public function to_array(): array {
170 return array(
171 'slug' => $this->slug,
172 'title' => $this->get_label(),
173 'errorReason' => $this->get_error_reason(),
174 'successReason' => wp_strip_all_tags( __( 'Error reporting is turned off.', 'defender-security' ) ),
175 'misc' => array(
176 'show_revert_button' => true,
177 'show_action_button' => true,
178 ),
179 'bulk_description' => wp_strip_all_tags( __( 'Turn off error debugging on live sites to help protect sensitive code details. When debugging is no longer needed, turn off error debugging to reduce the risk of exposing information attackers can use.', 'defender-security' ) ),
180 'bulk_title' => wp_strip_all_tags( __( 'Error Reporting', 'defender-security' ) ),
181 );
182 }
183 }
184