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