admin
3 months ago
cli
8 months ago
lib
3 months ago
captcha.php
3 months ago
cloudsecure-wp.php
3 months ago
common.php
3 months ago
config.php
2 years ago
disable-access-system-file.php
4 months ago
disable-author-query.php
2 years ago
disable-login.php
3 months ago
disable-restapi.php
7 months ago
disable-xmlrpc.php
1 year ago
htaccess.php
3 months ago
login-log.php
3 months ago
login-notification.php
1 year ago
rename-login-page.php
3 months ago
restrict-admin-page.php
3 months ago
server-error-notification.php
3 months ago
two-factor-authentication.php
3 months ago
unify-messages.php
2 years ago
update-notice.php
7 months ago
waf-engine.php
3 months ago
waf.php
3 months ago
htaccess.php
269 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CloudSecureWP_Htaccess extends CloudSecureWP_Common { |
| 8 | private const FILE_PATH = ABSPATH . '.htaccess'; |
| 9 | private const TAG_PLUGIN_SETTINGS = 'CloudSecure WP Security Settings'; |
| 10 | private const TAG_WP_SETTINGS = 'WordPress'; |
| 11 | private const TAG_PREFIX_START = '# BEGIN '; |
| 12 | private const TAG_PREFIX_END = '# END '; |
| 13 | private $htaccess_enabled = null; |
| 14 | |
| 15 | function __construct( array $info ) { |
| 16 | parent::__construct( $info ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * .htaccessファイルの存在確認 |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | private function is_exists(): bool { |
| 25 | if ( false !== file_exists( self::FILE_PATH ) ) { |
| 26 | return true; |
| 27 | } |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * .htaccessファイルコンテンツ取得 |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | private function load_contents(): string { |
| 37 | if ( $this->is_exists() ) { |
| 38 | |
| 39 | $contents = file_get_contents( self::FILE_PATH ); |
| 40 | |
| 41 | if ( false !== $contents ) { |
| 42 | return $contents; |
| 43 | } |
| 44 | } |
| 45 | return ''; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * .htaccessファイルコンテンツを上書き |
| 50 | * |
| 51 | * @param string $contents |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function save_contents( string $contents ): bool { |
| 55 | if ( ! empty( $contents ) ) { |
| 56 | if ( @file_put_contents( self::FILE_PATH, $contents, LOCK_EX ) ) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * .htaccessファイルの有効判定 |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function is_enabled(): bool { |
| 69 | if ( is_null( $this->htaccess_enabled ) ) { |
| 70 | if ( is_writable( self::FILE_PATH ) ) { |
| 71 | $this->htaccess_enabled = true; |
| 72 | } else { |
| 73 | $this->htaccess_enabled = false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $this->htaccess_enabled; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * htaccess用設定のマッチパターンを取得 |
| 82 | * |
| 83 | * @param string $tag |
| 84 | * @return string |
| 85 | */ |
| 86 | private function get_setting_pattern( string $tag ): string { |
| 87 | return '/' . self::TAG_PREFIX_START . $tag . '.*?' . self::TAG_PREFIX_END . $tag . '\r?\n/s'; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * プラグイン設定タグ取得 |
| 92 | */ |
| 93 | public function get_plugin_settings_tag(): string { |
| 94 | return self::TAG_PLUGIN_SETTINGS; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * .htaccessプラグイン設定の存在確認 |
| 99 | * |
| 100 | * @param string $tag |
| 101 | * @return bool |
| 102 | */ |
| 103 | public function setting_tag_exists( string $tag ): bool { |
| 104 | $contents = $this->load_contents(); |
| 105 | if ( ! empty( $contents ) ) { |
| 106 | |
| 107 | $pattern = $this->get_setting_pattern( $tag ); |
| 108 | if ( preg_match( $pattern, $contents ) ) { |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * プラグイン設定を.htaccessから削除 |
| 117 | * |
| 118 | * @param array<string> $tags |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function remove_settings( array $tags ): bool { |
| 122 | $contents = $this->load_contents(); |
| 123 | if ( ! empty( $contents ) ) { |
| 124 | $old = $tmp = $contents; |
| 125 | |
| 126 | foreach ( $tags as $tag ) { |
| 127 | $pattern = $this->get_setting_pattern( $tag ); |
| 128 | $tmp = preg_replace( $pattern, '', $tmp ); |
| 129 | } |
| 130 | |
| 131 | if ( $old === $tmp ) { |
| 132 | return true; |
| 133 | |
| 134 | } else { |
| 135 | $contents = $tmp; |
| 136 | |
| 137 | if ( false !== $this->save_contents( $contents ) ) { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * プラグイン用htaccess設定タグ追加 |
| 147 | */ |
| 148 | public function add_plugin_settings_tag(): bool { |
| 149 | $contents = $this->load_contents(); |
| 150 | |
| 151 | if ( ! empty( $contents ) ) { |
| 152 | $plugin_setting = self::TAG_PREFIX_START . $this->get_plugin_settings_tag() . "\n"; |
| 153 | $plugin_setting .= self::TAG_PREFIX_END . $this->get_plugin_settings_tag() . "\n"; |
| 154 | $wp_tag_start = self::TAG_PREFIX_START . self::TAG_WP_SETTINGS; |
| 155 | $new_contents = preg_replace( |
| 156 | '/' . preg_quote( $wp_tag_start, '/' ) . '/', |
| 157 | addcslashes( $plugin_setting . $wp_tag_start, '\\$' ), |
| 158 | $contents, |
| 159 | 1, |
| 160 | $count |
| 161 | ); |
| 162 | |
| 163 | if ( 0 === $count ) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if ( false !== $this->save_contents( $new_contents ) ) { |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * 各機能用htaccess設定追加 |
| 176 | * |
| 177 | * @param string $tag |
| 178 | * @param string $setting |
| 179 | * @return bool |
| 180 | */ |
| 181 | public function add_feature_setting( string $tag, string $setting ): bool { |
| 182 | $contents = $this->load_contents(); |
| 183 | |
| 184 | if ( ! empty( $contents ) ) { |
| 185 | $add_setting = self::TAG_PREFIX_START . $tag . "\n"; |
| 186 | $add_setting .= $setting; |
| 187 | $add_setting .= self::TAG_PREFIX_END . $tag . "\n"; |
| 188 | $plugin_tag_end = self::TAG_PREFIX_END . $this->get_plugin_settings_tag(); |
| 189 | $new_contents = preg_replace( |
| 190 | '/' . preg_quote( $plugin_tag_end, '/' ) . '/', |
| 191 | addcslashes( $add_setting . $plugin_tag_end, '\\$' ), |
| 192 | $contents, |
| 193 | 1, |
| 194 | $count |
| 195 | ); |
| 196 | |
| 197 | if ( 0 === $count ) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if ( false !== $this->save_contents( $new_contents ) ) { |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * CloudSecure WP Security Settingsブロックを再� |
| 210 | �置 |
| 211 | * |
| 212 | * @return void |
| 213 | */ |
| 214 | public function reorganize_plugin_settings_blocks(): void { |
| 215 | // .htaccessファイルの� |
| 216 | 容を取得 |
| 217 | $contents = $this->load_contents(); |
| 218 | if ( empty( $contents ) ) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // CloudSecure WP Security Settingsブロックをすべて取得 |
| 223 | $plugin_pattern = $this->get_setting_pattern( self::TAG_PLUGIN_SETTINGS ); |
| 224 | $matches = array(); |
| 225 | |
| 226 | $result = preg_match_all( $plugin_pattern, $contents, $matches ); |
| 227 | if ( false === $result || 0 === $result ) { |
| 228 | // ブロックを取得できなかった場合 |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // 見つかったブロックから1番目のブロックを取得 |
| 233 | $found_blocks = $matches[0]; |
| 234 | $first_block = $found_blocks[0]; |
| 235 | |
| 236 | // すべてのCloudSecure WP Security Settingsブロックを削除 |
| 237 | $contents_without_blocks = preg_replace( |
| 238 | $plugin_pattern, |
| 239 | '', |
| 240 | $contents, |
| 241 | -1, |
| 242 | $count |
| 243 | ); |
| 244 | if ( is_null( $contents_without_blocks ) || 0 === $count ) { |
| 245 | // ブロックを削除できなかった場合 |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | // WordPressブロックの開始タグを探して、その直前にプラグインブロックを挿� |
| 250 | � |
| 251 | $wp_tag_start = self::TAG_PREFIX_START . self::TAG_WP_SETTINGS; |
| 252 | $new_contents = preg_replace( |
| 253 | '/' . preg_quote( $wp_tag_start, '/' ) . '/', |
| 254 | addcslashes( $first_block . $wp_tag_start, '\\$' ), |
| 255 | $contents_without_blocks, |
| 256 | 1, |
| 257 | $count |
| 258 | ); |
| 259 | if ( is_null( $new_contents ) || 0 === $count ) { |
| 260 | // ブロックを挿� |
| 261 | �できなかった場合 |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // .htaccessファイルに保存 |
| 266 | $this->save_contents( $new_contents ); |
| 267 | } |
| 268 | } |
| 269 |