PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.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 / class-security-tweak.php

class-security-tweak.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at src/component/class-security-tweak.php

218 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles security tweaks.
4 *
5 * @package WP_Defender\Component
6 */
7
8 namespace WP_Defender\Component;
9
10 use Exception;
11 use SplFileObject;
12 use Calotes\Base\Component;
13 use Calotes\Helper\Array_Cache;
14 use WP_Defender\Model\Setting\Security_Tweaks;
15
16 /**
17 * Handles security tweaks operations such as retrieving issues, ignored, and fixed tweaks.
18 */
19 class Security_Tweak extends Component {
20
21 public const LOG_FILE_NAME = 'recommendations.log';
22
23 /**
24 * Model instance for caching.
25 *
26 * @var Security_Tweaks
27 */
28 public $model;
29
30 /**
31 * Retrieves the model instance, creating it if it does not exist.
32 *
33 * @return Security_Tweaks The security tweaks model.
34 */
35 protected function get_model() {
36 if ( is_object( $this->model ) ) {
37 return $this->model;
38 }
39 $this->model = new Security_Tweaks();
40
41 return $this->model;
42 }
43
44 /**
45 * Retrieves a list of current issues from the cache.
46 *
47 * @return array List of issues with labels and URLs.
48 */
49 public function get_issues() {
50 $issues = array();
51 $tweaks = Array_Cache::get( 'tweaks', 'tweaks' );
52 $issue_tweaks = $this->get_model()->issues;
53 foreach ( $issue_tweaks as $slug ) {
54 if ( isset( $tweaks[ $slug ] ) ) {
55 $issues[] = array(
56 'label' => $tweaks[ $slug ]->get_label(),
57 'url' => network_admin_url( 'admin.php?page=wdf-hardener' ) . '#' . $slug,
58 );
59 }
60 }
61
62 return $issues;
63 }
64
65 /**
66 * Retrieves a list of ignored tweaks from the cache.
67 *
68 * @return array List of ignored tweaks with labels and URLs.
69 */
70 public function get_ignored() {
71 $ignored = array();
72 $tweaks = Array_Cache::get( 'tweaks', 'tweaks' );
73 $ignored_tweaks = $this->get_model()->ignore;
74 foreach ( $ignored_tweaks as $slug ) {
75 if ( isset( $tweaks[ $slug ] ) ) {
76 $ignored[] = array(
77 'label' => $tweaks[ $slug ]->get_label(),
78 'url' => network_admin_url( 'admin.php?page=wdf-hardener&view=ignored' ) . '#' . $slug,
79 );
80 }
81 }
82
83 return $ignored;
84 }
85
86 /**
87 * Retrieves a list of fixed tweaks from the cache.
88 *
89 * @return array List of fixed tweaks with labels and URLs.
90 */
91 public function get_fixed() {
92 $fixed = array();
93 $tweaks = Array_Cache::get( 'tweaks', 'tweaks' );
94 $fixed_tweaks = $this->get_model()->fixed;
95 foreach ( $fixed_tweaks as $slug ) {
96 if ( isset( $tweaks[ $slug ] ) ) {
97 $fixed[] = array(
98 'label' => $tweaks[ $slug ]->get_label(),
99 'url' => network_admin_url( 'admin.php?page=wdf-hardener&view=resolved' ) . '#' . $slug,
100 );
101 }
102 }
103
104 return $fixed;
105 }
106
107 /**
108 * Generates a regex pattern for matching the hook line in a configuration file.
109 *
110 * @return string The regex pattern.
111 */
112 public function get_hook_line_pattern() {
113 global $wpdb;
114
115 return '/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/';
116 }
117
118 /**
119 * Checks if the wp-config.php file exists and is writable.
120 *
121 * @return bool True if the file exists and is writable, false otherwise.
122 */
123 public function advanced_check_file() {
124 global $wp_filesystem;
125 // Initialize the WP filesystem, no more using 'file-put-contents' function.
126 if ( empty( $wp_filesystem ) ) {
127 require_once ABSPATH . '/wp-admin/includes/file.php';
128 WP_Filesystem();
129 }
130 $path_to_wp_config = defender_wp_config_path();
131
132 return file_exists( $path_to_wp_config ) && $wp_filesystem->is_writable( $path_to_wp_config );
133 }
134
135 /**
136 * Retrieves a file object for the wp-config.php file.
137 *
138 * @return false|SplFileObject The file object or false on failure.
139 */
140 public function file() {
141 static $file = false;
142
143 if ( ! $file ) {
144 try {
145 $file = new SplFileObject( defender_wp_config_path(), 'r+' );
146 } catch ( Exception $e ) {
147 $this->log( $e->getMessage(), wd_internal_log() );
148
149 return false;
150 }
151 }
152
153 return $file;
154 }
155
156 /**
157 * Writes lines to the wp-config.php file.
158 *
159 * @param array $lines The lines to write to the file.
160 *
161 * @return bool True on success, false on failure.
162 */
163 public function write( $lines ) {
164 $file = $this->file();
165 $file->flock( LOCK_EX );
166 $file->fseek( 0 );
167
168 $bytes = $file->fwrite( implode( "\n", $lines ) );
169
170 if ( $bytes ) {
171 $file->ftruncate( $file->ftell() );
172 }
173
174 $file->flock( LOCK_UN );
175
176 return (bool) $bytes;
177 }
178
179 /**
180 * Generates a notice message for hosting issues related to file permissions.
181 *
182 * @param string $option The option related to the notice.
183 *
184 * @return string The notice message.
185 */
186 public function show_hosting_notice( $option ) {
187
188 return sprintf(
189 /* translators: %s: Option name. */
190 esc_html__(
191 'Some hostings do not allow you to make changes to the wp-config.php file. Please contact your hosting support team to switch %s ON or OFF on your site.',
192 'defender-security'
193 ),
194 $option
195 );
196 }
197
198 /**
199 * Generates a notice message for hosting issues with a code snippet.
200 *
201 * @param string $option The option related to the notice.
202 * @param string $code The code snippet to include in the notice.
203 *
204 * @return string The notice message with the code snippet.
205 */
206 public function show_hosting_notice_with_code( $option, $code ) {
207
208 return sprintf(
209 /* translators: %s - option */
210 esc_html__(
211 "Couldn't change the %s in your wp-config.php file. Please change it manually:",
212 'defender-security'
213 ) . '<p><b>' . $code . '</b></p>',
214 '<b>' . $option . '</b>'
215 );
216 }
217 }
218