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