PluginProbe ʕ •ᴥ•ʔ
Easy HTTPS Redirection (SSL) / 1.9.2
Easy HTTPS Redirection (SSL) v1.9.2
trunk 1.5 1.6 1.8 1.9.1 1.9.2 2.0.0 2.0.1
https-redirection / https-rules-helper.php
https-redirection Last commit date
css 12 years ago images 12 years ago js 6 years ago languages 12 years ago https-redirection-settings.php 6 years ago https-redirection.php 3 years ago https-rules-helper.php 3 years ago https-utillity-functions.php 6 years ago readme.txt 1 year ago screenshot-1.png 12 years ago
https-rules-helper.php
193 lines
1 <?php
2
3 class HTTPSRDRCTN_RULES {
4
5 function __construct() {
6
7 }
8
9 function write_to_htaccess() {
10 //clean up old rules first
11 if ( $this->delete_from_htaccess() == -1 ) {
12 return -1; //unable to write to the file
13 }
14
15 $htaccess = ABSPATH . '.htaccess';
16 //get the subdirectory if it is installed in one
17 $siteurl = explode( '/', get_option( 'siteurl' ) );
18 if ( isset( $siteurl[ 3 ] ) ) {
19 $dir = '/' . $siteurl[ 3 ] . '/';
20 } else {
21 $dir = '/';
22 }
23
24 if ( ! $f = @fopen( $htaccess, 'a+' ) ) {
25 @chmod( $htaccess, 0644 );
26 if ( ! $f = @fopen( $htaccess, 'a+' ) ) {
27 return -1;
28 }
29 }
30
31 //backup_a_file($htaccess); //TODO - should we back up htaccess file?
32
33 @ini_set( 'auto_detect_line_endings', true );
34 $ht = explode( PHP_EOL, implode( '', file( $htaccess ) ) ); //parse each line of file into array
35
36 $rules = $this->getrules();
37 if ( $rules == -1 ) {
38 return -1;
39 }
40
41 $rulesarray = explode( PHP_EOL, $rules );
42 $contents = array_merge( $rulesarray, $ht );
43
44 if ( ! $f = @fopen( $htaccess, 'w+' ) ) {
45 return -1; //we can't write to the file
46 }
47
48 $blank = false;
49
50 //write each line to file
51 foreach ( $contents as $insertline ) {
52 if ( trim( $insertline ) == '' ) {
53 if ( $blank == false ) {
54 fwrite( $f, PHP_EOL . trim( $insertline ) );
55 }
56 $blank = true;
57 } else {
58 $blank = false;
59 fwrite( $f, PHP_EOL . trim( $insertline ) );
60 }
61 }
62 @fclose( $f );
63 return 1; //success
64 }
65
66 function getrules() {
67 @ini_set( 'auto_detect_line_endings', true );
68
69 //figure out what server they're using
70 if ( strstr( strtolower( filter_var( $_SERVER[ 'SERVER_SOFTWARE' ], FILTER_SANITIZE_STRING ) ), 'apache' ) ) {
71 $server_type = 'apache';
72 } else if ( strstr( strtolower( filter_var( $_SERVER[ 'SERVER_SOFTWARE' ], FILTER_SANITIZE_STRING ) ), 'nginx' ) ) {
73 $server_type = 'nginx';
74 } else if ( strstr( strtolower( filter_var( $_SERVER[ 'SERVER_SOFTWARE' ], FILTER_SANITIZE_STRING ) ), 'litespeed' ) ) {
75 $server_type = 'litespeed';
76 } else { //unsupported server
77 return -1;
78 }
79
80 //check if some plugins are active to avoid incompatability issues
81 // WP Fastest Cache
82 if ( isset( $GLOBALS[ "wp_fastest_cache" ] ) ) {
83 $wpfc = true;
84 $wpfc_rules = '# WP Fastest Cache compatability' . PHP_EOL;
85 $wpfc_rules .= 'RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)' . PHP_EOL;
86 } else {
87 $wpfc = false;
88 }
89
90 $rules = '';
91 $httpsrdrctn_options = get_option( 'httpsrdrctn_options' );
92 $https_full_domain = $httpsrdrctn_options[ 'https_domain' ];
93 $auto_redirect_enabled = $httpsrdrctn_options[ 'https' ];
94
95 if ( $auto_redirect_enabled != '1' ) {
96 //HTTPS Redirection is NOT enabled
97 return $rules;
98 }
99
100 if ( $https_full_domain == '1' ) {//HTTPS Redirection on Full Site
101 $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
102 $rules .= 'RewriteEngine On' . PHP_EOL;
103
104 $rules .= 'RewriteCond %{HTTP:X-Forwarded-Proto} !https' . PHP_EOL; //Handle traffic connecting to your proxy or load balancer
105 $rules .= 'RewriteCond %{HTTPS} off' . PHP_EOL; //Alternative is to use RewriteCond %{SERVER_PORT} !^443$
106 if ( $wpfc ) {
107 $rules .= $wpfc_rules;
108 }
109 $rules .= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]' . PHP_EOL;
110
111 $rules .= '</IfModule>' . PHP_EOL;
112 } else {//HTTPS Redirection on a Few Pages ONLY
113 if ( empty( $httpsrdrctn_options[ 'https_pages_array' ] ) ) {
114 //No specific page has been configured
115 return '';
116 }
117
118 $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
119 $rules .= 'RewriteEngine On' . PHP_EOL;
120
121 $rules .= 'RewriteCond %{HTTP:X-Forwarded-Proto} !https' . PHP_EOL; //Handle traffic connecting to your proxy or load balancer
122 $rules .= 'RewriteCond %{HTTPS} off' . PHP_EOL; //Alternative is to use RewriteCond %{SERVER_PORT} !^443$
123 if ( $wpfc ) {
124 $rules .= $wpfc_rules;
125 }
126 $count = 0;
127 $total_pages = count( $httpsrdrctn_options[ 'https_pages_array' ] );
128 foreach ( $httpsrdrctn_options[ 'https_pages_array' ] as $https_page ) {
129 //Add a RewriteCond line for each of the individual pages
130
131 $count ++;
132
133 if ( empty( $https_page ) ) {
134 continue;
135 }
136
137 $rules .= 'RewriteCond %{REQUEST_URI} ' . trim( $https_page );
138 if ( $total_pages != $count ) {//This is not the last page so join them with an OR condition
139 $rules .= ' [OR]';
140 }
141 $rules .= PHP_EOL;
142 }
143
144 $rules .= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]' . PHP_EOL;
145
146 $rules .= '</IfModule>' . PHP_EOL;
147 }
148
149 //Add outer markers if we have rules
150 if ( $rules != '' ) {
151 $rules = "# BEGIN HTTPS Redirection Plugin" . PHP_EOL . $rules . "# END HTTPS Redirection Plugin" . PHP_EOL;
152 }
153
154 return $rules;
155 }
156
157 function delete_from_htaccess( $section = 'HTTPS Redirection Plugin' ) {
158 $htaccess = ABSPATH . '.htaccess';
159
160 @ini_set( 'auto_detect_line_endings', true );
161 if ( ! file_exists( $htaccess ) ) {
162 $ht = @fopen( $htaccess, 'a+' );
163 @fclose( $ht );
164 }
165 $ht_contents = explode( PHP_EOL, implode( '', file( $htaccess ) ) ); //parse each line of file into array
166 if ( $ht_contents ) { //as long as there are lines in the file
167 $state = true;
168 if ( ! $f = @fopen( $htaccess, 'w+' ) ) {
169 @chmod( $htaccess, 0644 );
170 if ( ! $f = @fopen( $htaccess, 'w+' ) ) {
171 return -1;
172 }
173 }
174
175 foreach ( $ht_contents as $n => $markerline ) { //for each line in the file
176 if ( strpos( $markerline, '# BEGIN ' . $section ) !== false ) { //if we're at the beginning of the section
177 $state = false;
178 }
179 if ( $state == true ) { //as long as we're not in the section keep writing
180 fwrite( $f, trim( $markerline ) . PHP_EOL );
181 }
182 if ( strpos( $markerline, '# END ' . $section ) !== false ) { //see if we're at the end of the section
183 $state = true;
184 }
185 }
186 @fclose( $f );
187 return 1;
188 }
189 return 1;
190 }
191
192 }
193