PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.2.4
CloudSecure WP Security v1.2.4
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 2 years ago lib 2 years ago captcha.php 2 years ago cloudsecure-wp.php 2 years ago common.php 2 years ago config.php 2 years ago disable-author-query.php 2 years ago disable-login.php 2 years ago disable-restapi.php 2 years ago disable-xmlrpc.php 2 years ago htaccess.php 2 years ago login-log.php 2 years ago login-notification.php 2 years ago rename-login-page.php 2 years ago restrict-admin-page.php 2 years ago server-error-notification.php 2 years ago two-factor-authentication.php 2 years ago unify-messages.php 2 years ago update-notice.php 2 years ago
htaccess.php
180 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 * @return bool
22 */
23 private function is_exists(): bool {
24 if ( false !== file_exists( self::FILE_PATH ) ) {
25 return true;
26 }
27 return false;
28 }
29
30 /**
31 * .htaccessファイルコンテンツ取得
32 * @return string
33 */
34 private function load_contents(): string {
35 if ( $this->is_exists() ) {
36
37 $contents = file_get_contents( self::FILE_PATH );
38
39 if ( false !== $contents ) {
40 return $contents;
41 }
42 }
43 return '';
44 }
45
46 /**
47 * .htaccessファイルコンテンツを上書き
48 * @param string $contents
49 * @return bool
50 */
51 public function save_contents( string $contents ): bool {
52 if ( ! empty( $contents ) ) {
53 if ( @file_put_contents( self::FILE_PATH, $contents ) ) {
54 return true;
55 }
56 }
57 return false;
58 }
59
60 /**
61 * .htaccessファイルの有効判定
62 * @return bool
63 */
64 public function is_enabled(): bool {
65 if ( is_null( $this->htaccess_enabled ) ) {
66 if ( is_writable( self::FILE_PATH ) ) {
67 $this->htaccess_enabled = true;
68 } else {
69 $this->htaccess_enabled = false;
70 }
71 }
72
73 return $this->htaccess_enabled;
74 }
75
76 /**
77 * htaccess用設定のマッチパターンを取得
78 * @param string $tag
79 * @return string
80 */
81 private function get_setting_pattern( string $tag ): string {
82 return '/' . self::TAG_PREFIX_START . $tag . '.*' . self::TAG_PREFIX_END . $tag . '\r?\n/s';
83 }
84
85 /**
86 * プラグイン設定タグ取得
87 */
88 public function get_plugin_settings_tag(): string {
89 return self::TAG_PLUGIN_SETTINGS;
90 }
91
92 /**
93 * .htaccessプラグイン設定の存在確認
94 * @param string $tag
95 * @return bool
96 */
97 public function setting_tag_exists( string $tag ): bool {
98 $contents = $this->load_contents();
99 if ( ! empty( $contents ) ) {
100
101 $pattern = $this->get_setting_pattern( $tag );
102 if ( preg_match( $pattern, $contents ) ) {
103 return true;
104 }
105 }
106 return false;
107 }
108
109 /**
110 * プラグイン設定を.htaccessから削除
111 * @param array<string> $tags
112 * @return bool
113 */
114 public function remove_settings( array $tags ): bool {
115 $contents = $this->load_contents();
116 if ( ! empty( $contents ) ) {
117 $old = $tmp = $contents;
118
119 foreach ( $tags as $tag ) {
120 $pattern = $this->get_setting_pattern( $tag );
121 $tmp = preg_replace( $pattern, '', $tmp );
122 }
123
124 if ( $old === $tmp ) {
125 return true;
126
127 } else {
128 $contents = $tmp;
129
130 if ( false !== $this->save_contents( $contents ) ) {
131 return true;
132 }
133 }
134 }
135 return false;
136 }
137
138 /**
139 * プラグイン用htaccess設定タグ追加
140 */
141 public function add_plugin_settings_tag(): bool {
142 $contents = $this->load_contents();
143
144 if ( ! empty( $contents ) ) {
145 $plagin_setting = self::TAG_PREFIX_START . $this->get_plugin_settings_tag()."\n";
146 $plagin_setting .= self::TAG_PREFIX_END . $this->get_plugin_settings_tag()."\n";
147 $wp_tag_start = self::TAG_PREFIX_START.self::TAG_WP_SETTINGS;
148 $contents = str_replace( $wp_tag_start, $plagin_setting . $wp_tag_start, $contents );
149
150 if ( false !== $this->save_contents( $contents ) ) {
151 return true;
152 }
153 }
154 return false;
155 }
156
157 /**
158 * 各機能用htaccess設定追加
159 * @param string $tag
160 * @param string $setting
161 * @return bool
162 */
163 public function add_feature_setting( string $tag, string $setting ): bool {
164 $contents = $this->load_contents();
165
166 if ( ! empty( $contents ) ) {
167 $add_setting = self::TAG_PREFIX_START . $tag . "\n";
168 $add_setting .= $setting;
169 $add_setting .= self::TAG_PREFIX_END . $tag . "\n";
170 $plugin_tag_end = self::TAG_PREFIX_END . $this->get_plugin_settings_tag();
171 $contents = str_replace( $plugin_tag_end, $add_setting . $plugin_tag_end, $contents );
172
173 if ( false !== $this->save_contents( $contents ) ) {
174 return true;
175 }
176 }
177 return false;
178 }
179 }
180