| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component; |
| 4 |
|
| 5 |
use Calotes\Base\Component; |
| 6 |
use Calotes\Helper\Array_Cache; |
| 7 |
use SplFileObject; |
| 8 |
use WP_Defender\Model\Setting\Security_Tweaks; |
| 9 |
|
| 10 |
class Security_Tweak extends Component { |
| 11 |
|
| 12 |
/** |
| 13 |
* Use for cache. |
| 14 |
* |
| 15 |
* @var Security_Tweaks |
| 16 |
*/ |
| 17 |
public $model; |
| 18 |
|
| 19 |
/** |
| 20 |
* Safe way to get cached model. |
| 21 |
* |
| 22 |
* @return Security_Tweaks |
| 23 |
*/ |
| 24 |
protected function get_model() { |
| 25 |
if ( is_object( $this->model ) ) { |
| 26 |
return $this->model; |
| 27 |
} |
| 28 |
$this->model = new Security_Tweaks(); |
| 29 |
|
| 30 |
return $this->model; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_issues() { |
| 34 |
$issues = array(); |
| 35 |
$tweaks = Array_Cache::get( 'tweaks', 'tweaks' ); |
| 36 |
$issue_tweaks = $this->get_model()->issues; |
| 37 |
foreach ( $issue_tweaks as $slug ) { |
| 38 |
if ( isset( $tweaks[ $slug ] ) ) { |
| 39 |
$tweak_arr = $tweaks[ $slug ]->to_array(); |
| 40 |
$issues[] = array( |
| 41 |
'label' => $tweak_arr['title'], |
| 42 |
'url' => network_admin_url( 'admin.php?page=wdf-hardener' ) . '#' . $slug, |
| 43 |
); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return $issues; |
| 48 |
} |
| 49 |
|
| 50 |
public function get_ignored() { |
| 51 |
$ignored = array(); |
| 52 |
$tweaks = Array_Cache::get( 'tweaks', 'tweaks' ); |
| 53 |
$ignored_tweaks = $this->get_model()->ignore; |
| 54 |
foreach ( $ignored_tweaks as $slug ) { |
| 55 |
if ( isset( $tweaks[ $slug ] ) ) { |
| 56 |
$tweak_arr = $tweaks[ $slug ]->to_array(); |
| 57 |
$ignored[] = array( |
| 58 |
'label' => $tweak_arr['title'], |
| 59 |
'url' => network_admin_url( 'admin.php?page=wdf-hardener&view=ignored' ) . '#' . $slug, |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $ignored; |
| 65 |
} |
| 66 |
|
| 67 |
public function get_fixed() { |
| 68 |
$fixed = array(); |
| 69 |
$tweaks = Array_Cache::get( 'tweaks', 'tweaks' ); |
| 70 |
$fixed_tweaks = $this->get_model()->fixed; |
| 71 |
foreach ( $fixed_tweaks as $slug ) { |
| 72 |
if ( isset( $tweaks[ $slug ] ) ) { |
| 73 |
$tweak_arr = $tweaks[ $slug ]->to_array(); |
| 74 |
$fixed[] = array( |
| 75 |
'label' => $tweak_arr['title'], |
| 76 |
'url' => network_admin_url( 'admin.php?page=wdf-hardener&view=resolved' ) . '#' . $slug, |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return $fixed; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get hook line pattern. |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function get_hook_line_pattern() { |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
return '/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return false |
| 97 |
*/ |
| 98 |
public function advanced_check_file() { |
| 99 |
$path_to_wp_config = defender_wp_config_path(); |
| 100 |
|
| 101 |
return file_exists( $path_to_wp_config ) && is_writable( $path_to_wp_config ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get file instance. |
| 106 |
* |
| 107 |
* @return false|SplFileObject |
| 108 |
*/ |
| 109 |
public function file() { |
| 110 |
static $file = false; |
| 111 |
|
| 112 |
if ( ! $file ) { |
| 113 |
try { |
| 114 |
$file = new SplFileObject( defender_wp_config_path(), 'r+' ); |
| 115 |
} catch ( \Exception $e ) { |
| 116 |
$this->log( $e->getMessage(), 'internal.log' ); |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $file; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Write to the file. |
| 126 |
* |
| 127 |
* @param array $lines |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public function write( $lines ) { |
| 132 |
$file = $this->file(); |
| 133 |
$file->flock( LOCK_EX ); |
| 134 |
$file->fseek( 0 ); |
| 135 |
|
| 136 |
$bytes = $file->fwrite( implode( "\n", $lines ) ); |
| 137 |
|
| 138 |
if ( $bytes ) { |
| 139 |
$file->ftruncate( $file->ftell() ); |
| 140 |
} |
| 141 |
|
| 142 |
$file->flock( LOCK_UN ); |
| 143 |
|
| 144 |
return (bool) $bytes; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Show hosting notice. |
| 149 |
* |
| 150 |
* @param string $option |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public function show_hosting_notice( $option ) { |
| 155 |
|
| 156 |
return sprintf( |
| 157 |
/* translators: %s: Option name. */ |
| 158 |
__( '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.', 'wpdef' ), |
| 159 |
$option |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Show hosting notice. |
| 165 |
* |
| 166 |
* @param string $option |
| 167 |
* @param string $code |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public function show_hosting_notice_with_code( $option, $code ) { |
| 172 |
|
| 173 |
return sprintf( |
| 174 |
/* translators: %s - option */ |
| 175 |
__( "Couldn't change the %s in your wp-config.php file. Please change it manually:", 'wpdef' ) . '<p><b>' . $code . '</b></p>', |
| 176 |
'<b>' . $option . '</b>' |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
|