PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.3.8
CloudSecure WP Security v1.3.8
1.4.10 1.4.9 trunk 0.9.0 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.19 1.3.2 1.3.20 1.3.21 1.3.22 1.3.23 1.3.24 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8
cloudsecure-wp-security / modules / htaccess.php
cloudsecure-wp-security / modules Last commit date
admin 1 year ago lib 1 year ago captcha.php 2 years ago cloudsecure-wp.php 1 year ago common.php 1 year ago config.php 2 years ago disable-access-system-file.php 1 year ago disable-author-query.php 2 years ago disable-login.php 1 year ago disable-restapi.php 2 years ago disable-xmlrpc.php 1 year ago htaccess.php 1 year ago login-log.php 1 year ago login-notification.php 1 year ago rename-login-page.php 1 year ago restrict-admin-page.php 1 year ago server-error-notification.php 1 year ago two-factor-authentication.php 1 year ago unify-messages.php 2 years ago update-notice.php 2 years ago waf-engine.php 1 year ago waf.php 1 year ago
htaccess.php
188 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 ) ) {
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 $plagin_setting = self::TAG_PREFIX_START . $this->get_plugin_settings_tag() . "\n";
153 $plagin_setting .= self::TAG_PREFIX_END . $this->get_plugin_settings_tag() . "\n";
154 $wp_tag_start = self::TAG_PREFIX_START . self::TAG_WP_SETTINGS;
155 $contents = str_replace( $wp_tag_start, $plagin_setting . $wp_tag_start, $contents );
156
157 if ( false !== $this->save_contents( $contents ) ) {
158 return true;
159 }
160 }
161 return false;
162 }
163
164 /**
165 * 各機能用htaccess設定追加
166 *
167 * @param string $tag
168 * @param string $setting
169 * @return bool
170 */
171 public function add_feature_setting( string $tag, string $setting ): bool {
172 $contents = $this->load_contents();
173
174 if ( ! empty( $contents ) ) {
175 $add_setting = self::TAG_PREFIX_START . $tag . "\n";
176 $add_setting .= $setting;
177 $add_setting .= self::TAG_PREFIX_END . $tag . "\n";
178 $plugin_tag_end = self::TAG_PREFIX_END . $this->get_plugin_settings_tag();
179 $contents = str_replace( $plugin_tag_end, $add_setting . $plugin_tag_end, $contents );
180
181 if ( false !== $this->save_contents( $contents ) ) {
182 return true;
183 }
184 }
185 return false;
186 }
187 }
188