PluginProbe ʕ •ᴥ•ʔ
Easy HTTPS Redirection (SSL) / 1.9.1
Easy HTTPS Redirection (SSL) v1.9.1
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 6 years ago https-rules-helper.php 6 years ago https-utillity-functions.php 6 years ago readme.txt 3 years ago screenshot-1.png 12 years ago
https-rules-helper.php
191 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 %{SERVER_PORT} !^443$' . PHP_EOL; //Alternative is to use RewriteCond %{HTTPS} off
105 if ( $wpfc ) {
106 $rules .= $wpfc_rules;
107 }
108 $rules .= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]' . PHP_EOL;
109
110 $rules .= '</IfModule>' . PHP_EOL;
111 } else {//HTTPS Redirection on a Few Pages ONLY
112 if ( empty( $httpsrdrctn_options[ 'https_pages_array' ] ) ) {
113 //No specific page has been configured
114 return '';
115 }
116
117 $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
118 $rules .= 'RewriteEngine On' . PHP_EOL;
119
120 $rules .= 'RewriteCond %{SERVER_PORT} !^443$' . PHP_EOL; //Alternative is to use RewriteCond %{HTTPS} off
121 if ( $wpfc ) {
122 $rules .= $wpfc_rules;
123 }
124 $count = 0;
125 $total_pages = count( $httpsrdrctn_options[ 'https_pages_array' ] );
126 foreach ( $httpsrdrctn_options[ 'https_pages_array' ] as $https_page ) {
127 //Add a RewriteCond line for each of the individual pages
128
129 $count ++;
130
131 if ( empty( $https_page ) ) {
132 continue;
133 }
134
135 $rules .= 'RewriteCond %{REQUEST_URI} ' . trim( $https_page );
136 if ( $total_pages != $count ) {//This is not the last page so join them with an OR condition
137 $rules .= ' [OR]';
138 }
139 $rules .= PHP_EOL;
140 }
141
142 $rules .= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]' . PHP_EOL;
143
144 $rules .= '</IfModule>' . PHP_EOL;
145 }
146
147 //Add outer markers if we have rules
148 if ( $rules != '' ) {
149 $rules = "# BEGIN HTTPS Redirection Plugin" . PHP_EOL . $rules . "# END HTTPS Redirection Plugin" . PHP_EOL;
150 }
151
152 return $rules;
153 }
154
155 function delete_from_htaccess( $section = 'HTTPS Redirection Plugin' ) {
156 $htaccess = ABSPATH . '.htaccess';
157
158 @ini_set( 'auto_detect_line_endings', true );
159 if ( ! file_exists( $htaccess ) ) {
160 $ht = @fopen( $htaccess, 'a+' );
161 @fclose( $ht );
162 }
163 $ht_contents = explode( PHP_EOL, implode( '', file( $htaccess ) ) ); //parse each line of file into array
164 if ( $ht_contents ) { //as long as there are lines in the file
165 $state = true;
166 if ( ! $f = @fopen( $htaccess, 'w+' ) ) {
167 @chmod( $htaccess, 0644 );
168 if ( ! $f = @fopen( $htaccess, 'w+' ) ) {
169 return -1;
170 }
171 }
172
173 foreach ( $ht_contents as $n => $markerline ) { //for each line in the file
174 if ( strpos( $markerline, '# BEGIN ' . $section ) !== false ) { //if we're at the beginning of the section
175 $state = false;
176 }
177 if ( $state == true ) { //as long as we're not in the section keep writing
178 fwrite( $f, trim( $markerline ) . PHP_EOL );
179 }
180 if ( strpos( $markerline, '# END ' . $section ) !== false ) { //see if we're at the end of the section
181 $state = true;
182 }
183 }
184 @fclose( $f );
185 return 1;
186 }
187 return 1;
188 }
189
190 }
191