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