PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.2.2
SiteGuard WP Plugin v1.2.2
1.8.7 1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.4.3 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.11 1.7.12 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.0-beta1 1.8.0-beta2 1.8.0-beta3 1.8.0-beta4
siteguard / classes / siteguard-htaccess.php
siteguard / classes Last commit date
siteguard-admin-filter.php 11 years ago siteguard-base.php 11 years ago siteguard-captcha.php 11 years ago siteguard-config.php 11 years ago siteguard-disable-pingback.php 11 years ago siteguard-htaccess.php 11 years ago siteguard-login-alert.php 11 years ago siteguard-login-history.php 11 years ago siteguard-login-lock.php 11 years ago siteguard-rename-login.php 11 years ago siteguard-updates-notify.php 11 years ago siteguard-waf-exclude-rule.php 11 years ago
siteguard-htaccess.php
231 lines
1 <?php
2
3 class SiteGuard_Htaccess extends SiteGuard_Base {
4 public static $htaccess_mark_start = '#SITEGUARD_PLUGIN_SETTINGS_START';
5 public static $htaccess_mark_end = '#SITEGUARD_PLUGIN_SETTINGS_END';
6
7 function __construct( ) {
8 }
9 static function get_htaccess_file( ) {
10 return ABSPATH.'.htaccess';
11 }
12 static function get_tmp_dir( ) {
13 return SITEGUARD_PATH . 'tmp/';
14 }
15 static function get_htaccess_new_file( ) {
16 return tempnam( SiteGuard_Htaccess::get_tmp_dir( ), 'htaccess_' );
17 }
18 static function make_tmp_dir( ) {
19 $dir = SiteGuard_Htaccess::get_tmp_dir( );
20 if ( ! wp_mkdir_p( $dir ) ) {
21 siteguard_error_log( "make tempdir failed: $dir" );
22 return false;
23 }
24 $htaccess_file = $dir . '.htaccess';
25
26 if ( file_exists( $htaccess_file ) ) {
27 return true;
28 }
29
30 if ( $handle = @fopen( $htaccess_file, 'w' ) ) {
31 fwrite( $handle, 'Order deny,allow' . "\n" );
32 fwrite( $handle, 'Deny from all' . "\n" );
33 fclose( $handle );
34 }
35
36 return true;
37 }
38 static function is_exists_setting( $mark ) {
39 $result = false;
40 if ( '' == $mark ) {
41 $mark_start = SiteGuard_Htaccess::$htaccess_mark_start;
42 $mark_end = SiteGuard_Htaccess::$htaccess_mark_end;
43 } else {
44 $mark_start = $mark . '_START';
45 $mark_end = $mark . '_END';
46 }
47 $current_file = SiteGuard_Htaccess::get_htaccess_file( );
48 if ( ! file_exists( $current_file ) ) {
49 return $result;
50 }
51 $fr = @fopen( $current_file, 'r' );
52 if ( null == $fr ) {
53 return $result;
54 }
55 $line_num = 0;
56 $start_line = 0;
57 $end_line = 0;
58 while ( ! feof( $fr ) ) {
59 $line = fgets( $fr, 4096 );
60 $line_num++;
61 if ( false !== strpos( $line, $mark_start ) ) {
62 $start_line = $line_num;
63 }
64 if ( false !== strpos( $line, $mark_end ) ) {
65 $end_line = $line_num;
66 if ( $start_line > 0 && ( $end_line - $start_line ) > 1 ) {
67 $result = true;
68 }
69 break;
70 }
71 }
72 @fclose( $fr );
73
74 return $result;
75 }
76 static function clear_settings( $mark ) {
77 if ( ! SiteGuard_Htaccess::make_tmp_dir( ) ) {
78 return false;
79 }
80 if ( '' == $mark ) {
81 $mark_start = SiteGuard_Htaccess::$htaccess_mark_start;
82 $mark_end = SiteGuard_Htaccess::$htaccess_mark_end;
83 } else {
84 $mark_start = $mark . '_START';
85 $mark_end = $mark . '_END';
86 }
87 $flag_settings = false;
88 $current_file = SiteGuard_Htaccess::get_htaccess_file( );
89 if ( ! file_exists( $current_file ) ) {
90 @touch( $current_file );
91 @chmod( $current_file, 0604 );
92 }
93 $fr = @fopen( $current_file, 'r' );
94 if ( null == $fr ) {
95 siteguard_error_log( "fopen failed: $current_file" );
96 return false;
97 }
98 $new_file = SiteGuard_Htaccess::get_htaccess_new_file( );
99 $fw = @fopen( $new_file, 'w' );
100 if ( null == $fw ) {
101 siteguard_error_log( "fopen failed: $new_file" );
102 return false;
103 }
104 while ( ! feof( $fr ) ) {
105 $line = fgets( $fr, 4096 );
106 if ( false !== strpos( $line, $mark_start ) ) {
107 $flag_settings = true;
108 }
109 if ( false == $flag_settings ) {
110 fputs( $fw, $line, 4096 );
111 }
112 if ( true == $flag_settings && false !== strpos( $line, $mark_end ) ) {
113 $flag_settings = false;
114 }
115 }
116 fclose( $fr );
117 fclose( $fw );
118 @chmod( $new_file, 0604 );
119 if ( ! rename( $new_file, $current_file ) ) {
120 siteguard_error_log( "rename failed: $new_file $current_file" );
121 return false;
122 }
123 return true;
124 }
125 function update_settings( $mark, $data ) {
126 if ( ! SiteGuard_Htaccess::make_tmp_dir( ) ) {
127 return false;
128 }
129 $flag_write = false;
130 $flag_through = true;
131 $flag_wp = false;
132 $flag_wp_set = false;
133 $wp_settings = '';
134 $mark_start = $mark . '_START';
135 $mark_end = $mark . '_END';
136 $mark_wp_start = '# BEGIN WordPress';
137 $mark_wp_end = '# END WordPress';
138 $current_file = SiteGuard_Htaccess::get_htaccess_file( );
139 if ( ! file_exists( $current_file ) ) {
140 @touch( $current_file );
141 @chmod( $current_file, 0604 );
142 }
143 if ( ! is_readable( $current_file ) ) {
144 siteguard_error_log( "file not readable: $current_file" );
145 return false;
146 }
147 $fr = @fopen( $current_file, 'r' );
148 if ( null == $fr ) {
149 siteguard_error_log( "fopen failed: $current_file" );
150 return false;
151 }
152 $new_file = SiteGuard_Htaccess::get_htaccess_new_file( );
153 if ( ! is_writable( $new_file ) ) {
154 siteguard_error_log( "file not writable: $new_file" );
155 return false;
156 }
157 $fw = @fopen( $new_file, 'w' );
158 if ( null == $fw ) {
159 siteguard_error_log( "fopen failed: $new_file" );
160 return false;
161 }
162 while ( ! feof( $fr ) ) {
163 $line = fgets( $fr, 4096 );
164
165 // Save WordPress settings.
166 // WordPress settings has to be written after SiteGuard settings.
167 if ( false == $flag_write && false == $flag_wp_set && false !== strpos( $line, $mark_wp_start ) ) {
168 $flag_wp = true;
169 $flag_wp_set = true;
170 }
171 if ( $flag_wp_set ) {
172 $wp_settings .= $line;
173 if ( false !== strpos( $line, $mark_wp_end ) ) {
174 $flag_wp_set = false;
175 }
176 continue;
177 }
178
179 if ( false !== strpos( $line, $mark_start ) ) {
180 fwrite( $fw, $line , strlen( $line ) );
181 fwrite( $fw, $data, strlen( $data ) );
182 $flag_write = true;
183 $flag_through = false;
184 continue;
185 }
186 if ( false == $flag_write && false !== strpos( $line, SiteGuard_Htaccess::$htaccess_mark_end ) ) {
187 fwrite( $fw, $mark_start . "\n", strlen( $mark_start ) + 1 );
188 fwrite( $fw, $data, strlen( $data ) );
189 fwrite( $fw, $mark_end . "\n", strlen( $mark_end ) + 1 );
190 $flag_write = true;
191 }
192 if ( false == $flag_through && false !== strpos( $line, $mark_end ) ) {
193 $flag_through = true;
194 }
195 if ( $flag_through ) {
196 fwrite( $fw, $line, strlen( $line ) );
197 if ( false == $flag_wp && false !== strpos( $line, $mark_wp_start ) ) {
198 $flag_wp = true;
199 }
200 }
201 }
202 if ( false == $flag_write ) {
203 fwrite( $fw, "\n" . SiteGuard_Htaccess::$htaccess_mark_start . "\n", strlen( SiteGuard_Htaccess::$htaccess_mark_start ) + 2 );
204 fwrite( $fw, $mark_start . "\n", strlen( $mark_start ) + 1 );
205 fwrite( $fw, $data, strlen( $data ) );
206 fwrite( $fw, $mark_end . "\n", strlen( $mark_end ) + 1 );
207 fwrite( $fw, SiteGuard_Htaccess::$htaccess_mark_end . "\n", strlen( SiteGuard_Htaccess::$htaccess_mark_end ) + 1 );
208 }
209 if ( '' != $wp_settings ) { // Write saved WordPress Settings
210 fwrite( $fw, "\n", 1 );
211 fwrite( $fw, $wp_settings, strlen( $wp_settings ) );
212 fwrite( $fw, "\n", 1 );
213 } else if ( false == $flag_wp ) { // Write empty WordPress Settings
214 fwrite( $fw, "\n", 1 );
215 fwrite( $fw, $mark_wp_start . "\n", strlen( $mark_wp_start ) + 1 );
216 fwrite( $fw, $mark_wp_end . "\n", strlen( $mark_wp_end ) + 1 );
217 fwrite( $fw, "\n", 1 );
218 }
219 fclose( $fr );
220 fclose( $fw );
221 @chmod( $new_file, 0604 );
222 if ( ! rename( $new_file, $current_file ) ) {
223 siteguard_error_log( "rename failed: $new_file $current_file" );
224 return false;
225 }
226 return true;
227 }
228 }
229
230 ?>
231