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