-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-admin.php
2261 lines (1921 loc) · 80.5 KB
/
class-admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
defined('ABSPATH') or die("you do not have access to this page!");
if (!class_exists('rsssl_admin')) {
class rsssl_admin extends rsssl_front_end
{
private static $_this;
//true when siteurl and homeurl are defined in wp-config and can't be changed
public $wpconfig_siteurl_not_fixed = FALSE;
public $no_server_variable = FALSE;
public $errors = Array();
public $do_wpconfig_loadbalancer_fix = FALSE;
public $ssl_enabled = FALSE;
//for pro compatibility
public $do_not_edit_htaccess = FALSE;
//multisite variables
public $ssl_enabled_networkwide = FALSE;
public $selected_networkwide_or_per_site = FALSE;
//general settings
public $capability = 'manage_options';
public $ssl_test_page_error;
public $plugin_dir = "really-simple-ssl-on-specific-pages";
public $plugin_filename = "really-simple-ssl-on-specific-pages.php";
public $ABSpath;
//this option is needed for compatibility with the per page plugin.
public $hsts = FALSE;
public $ssl_success_message_shown = FALSE;
public $debug = TRUE;
public $debug_log;
public $plugin_conflict = ARRAY();
public $plugin_url;
public $plugin_version;
public $plugin_db_version;
public $plugin_upgraded;
public $mixed_content_fixer_status = 0;
public $ssl_type = "NA";
//possible values:
//"NA": test page did not return valid response
//"SERVER-HTTPS-ON"
//"SERVER-HTTPS-1"
//"SERVERPORT443"
//"LOADBALANCER"
//"CDN"
private $ad = false;
private $pro_url = "https://www.really-simple-ssl.com/pro";
function __construct()
{
if (isset(self::$_this))
wp_die(sprintf(__('%s is a singleton class and you cannot create a second instance.', 'really-simple-ssl'), get_class($this)));
self::$_this = $this;
$this->upgrade();
$this->get_options();
$this->get_admin_options();
$this->ABSpath = $this->getABSPATH();
$this->get_plugin_version();
$this->get_plugin_upgraded(); //call always, otherwise db version will not match anymore.
register_activation_hook(dirname(__FILE__) . "/" . $this->plugin_filename, array($this, 'activate'));
register_deactivation_hook(dirname(__FILE__) . "/" . $this->plugin_filename, array($this, 'deactivate'));
}
static function this()
{
return self::$_this;
}
public function upgrade()
{
//migrate SSL pages top post meta
$options = get_option('rlrsssl_options');
//only upgrade if the option is still there
if (isset($options['ssl_pages'])) {
$ssl_pages = $options['ssl_pages'];
foreach ($ssl_pages as $ssl_page_id) {
update_post_meta($ssl_page_id, "rsssl_ssl_page", true);
}
//create a backup
update_option("rsssl_backup_ssl_pages", $options['ssl_pages']);
//now remove it
unset($options['ssl_pages']);
update_option('rlrsssl_options', $options);
}
if (defined("RSSSL_RESTORE_SSL_PAGES") && RSSSL_RESTORE_SSL_PAGES) {
$ssl_pages = get_option("rsssl_backup_ssl_pages", $options['ssl_pages']);
foreach ($ssl_pages as $ssl_page_id) {
update_post_meta($ssl_page_id, "rsssl_ssl_page", true);
}
// $ssl_pages = $options['ssl_pages'];
// $options["ssl_pages"]=$ssl_pages;
// update_option('rlrsssl_options', $options);
}
}
/**
* @return bool
*
* Check if the htaccess file contains HSTS header
*
* Since 2.0
*
*/
public function contains_hsts()
{
if (!file_exists($this->ABSpath . ".htaccess")) {
$this->trace_log(".htaccess not found in " . $this->ABSpath);
$result = $this->hsts; //just return the setting.
} else {
$htaccess = file_get_contents($this->ABSpath . ".htaccess");
preg_match("/Strict-Transport-Security/", $htaccess, $check);
if (count($check) === 0) {
$result = false;
} else {
$result = true;
}
}
return $result;
}
/**
* @param $setting_name
*
* @return string
*
* Generate an enable link for the specific setting, redirects to settings page and highlights the setting.
*
*/
public function generate_enable_link($setting_name)
{
return add_query_arg(array("page"=>"rlrsssl_really_simple_ssl", "tab"=>"settings", "highlight"=>"$setting_name"),admin_url("options-general.php"));
}
/**
* Initializes the admin class
*
* @since 2.0
*
* @access public
*
*/
public function init()
{
$is_on_settings_page = $this->is_settings_page();
$this->get_plugin_url();//has to be after the domain list was built.
/*
Detect configuration when:
- SSL activation just confirmed.
- on settings page
- No SSL detected -> check again
*/
if ($this->clicked_activate_ssl() || !$this->ssl_enabled || !$this->site_has_ssl || $is_on_settings_page) {
$this->detect_configuration();
//flush caches when just activated ssl, or when ssl is enabled
if ($this->clicked_activate_ssl() || ($this->ssl_enabled)) {
global $rsssl_cache;
add_action('admin_init', array($rsssl_cache, 'flush'), 40);
}
if (!$this->wpconfig_ok()) {
//if we were to activate ssl, this could result in a redirect loop. So warn first.
add_action("admin_notices", array($this, 'show_notice_wpconfig_needs_fixes'));
if (is_multisite()) add_action('network_admin_notices', array($this, 'show_notice_wpconfig_needs_fixes'), 10);
$this->ssl_enabled = false;
$this->save_options();
} elseif ($this->ssl_enabled) {
add_action('init', array($this, 'configure_ssl'), 20);
}
}
if (is_multisite() && !$this->selected_networkwide_or_per_site) {
add_action('network_admin_notices', array($this, 'show_notice_activate_networkwide'), 10);
}
//when ssl and not enabled by user, ask for activation.
if (!$this->ssl_enabled && (!is_multisite() || $this->selected_networkwide_or_per_site)) {
$this->trace_log("ssl not enabled, show notice");
add_action("admin_notices", array($this, 'show_notice_activate_ssl'), 10);
}
add_action('plugins_loaded', array($this, 'check_plugin_conflicts'), 30);
//add the settings page for the plugin
add_action('admin_menu', array($this, 'setup_admin_page'), 30);
//check if the uninstallfile is safely renamed to php.
$this->check_for_uninstall_file();
//callbacks for the ajax dismiss buttons
add_action('wp_ajax_dismiss_success_message', array($this, 'dismiss_success_message_callback'));
//handle notices
add_action('admin_notices', array($this, 'show_notices'));
}
/**
* @return bool
*
* Check if the user has pressed the Activate SSL button
*
*/
private function clicked_activate_ssl()
{
if (!isset($_POST['rsssl_nonce']) || !wp_verify_nonce($_POST['rsssl_nonce'], 'rsssl_nonce')) return false;
if (isset($_POST['rsssl_do_activate_ssl'])) {
$this->ssl_enabled = true;
$this->save_options();
return true;
}
if (is_multisite() && isset($_POST['rsssl_do_activate_ssl_networkwide'])) {
$sites = $this->get_sites_bw_compatible();
foreach ($sites as $site) {
$this->switch_to_blog_bw_compatible($site);
$this->ssl_enabled = true;
$this->save_options();
restore_current_blog(); //switches back to previous blog, not current, so we have to do it each loop
}
$this->selected_networkwide_or_per_site = true;
$this->ssl_enabled_networkwide = true;
$this->save_options();
return true;
}
if (is_multisite() && isset($_POST['rsssl_do_activate_ssl_per_site'])) {
$this->ssl_enabled_networkwide = false;
$this->selected_networkwide_or_per_site = true;
$this->save_options();
return true;
}
return false;
}
/**
* @return bool
*
* Check if the wp-config file is ok
*
*/
public function wpconfig_ok()
{
if (($this->do_wpconfig_loadbalancer_fix || $this->no_server_variable || $this->wpconfig_siteurl_not_fixed) && !$this->wpconfig_is_writable()) {
return false;
} else {
return true;
}
}
/**
* @return array|int
*
* Change deprecated function depending on version.
*
*/
public function get_sites_bw_compatible()
{
global $wp_version;
$sites = ($wp_version >= 4.6) ? get_sites() : wp_get_sites();
return $sites;
}
/**
* @param $site
*
* The new get_sites function returns an object.
*
*/
public function switch_to_blog_bw_compatible($site)
{
global $wp_version;
if ($wp_version >= 4.6) {
switch_to_blog($site->blog_id);
} else {
switch_to_blog($site['blog_id']);
}
}
/**
* On plugin activation, we can check if it is networkwide or not.
*
* @since 2.1
*
* @access public
*
*/
public function activate($networkwide)
{
if (!is_multisite()) return;
//if networkwide, we ask, if not, we set it as selected.
if (!$networkwide) {
$this->trace_log("per site activation");
$this->ssl_enabled_networkwide = FALSE;
$this->selected_networkwide_or_per_site = TRUE;
$this->save_options();
}
}
/**
* Give the user an option to activate network wide or not.
*
* @since 2.3
*
* @access public
*
*/
public function show_notice_activate_networkwide()
{
//prevent showing the review on edit screen, as gutenberg removes the class which makes it editable.
$screen = get_current_screen();
if ( $screen->parent_base === 'edit' ) return;
if (is_main_site(get_current_blog_id()) && $this->wpconfig_ok()) {
?>
<div id="message" class="updated fade notice activate-ssl">
<h1><?php _e("Choose your preferred setup", "really-simple-ssl"); ?></h1>
<p>
<form action="" method="post">
<?php wp_nonce_field('rsssl_nonce', 'rsssl_nonce'); ?>
<input type="submit" class='button button-primary'
value="<?php _e("Activate SSL networkwide", "really-simple-ssl"); ?>"
id="rsssl_do_activate_ssl_networkwide" name="rsssl_do_activate_ssl_networkwide">
<input type="submit" class='button button-primary'
value="<?php _e("Activate SSL per site", "really-simple-ssl"); ?>"
id="rsssl_do_activate_ssl_per_site" name="rsssl_do_activate_ssl_per_site">
</form>
</p>
<p>
<?php _e("Networkwide activation does not check if a site has an SSL certificate, but you can select the pages you want on SSL for each site separately.", "really-simple-ssl"); ?>
</p>
</div>
<?php
}
}
/**
*
* This message is shown when no ssl is not enabled by the user yet
*/
public function show_notice_activate_ssl()
{
//prevent showing the review on edit screen, as gutenberg removes the class which makes it editable.
$screen = get_current_screen();
if ( $screen->parent_base === 'edit' ) return;
//for multisite, show no ssl message only on main blog.
if (is_multisite() && !is_main_site(get_current_blog_id()) && !$this->site_has_ssl) return;
if (!$this->wpconfig_ok()) return;
if (!current_user_can($this->capability)) return;
if (!$this->site_has_ssl) {
$class = "error fade notice activate-ssl";
$title = '';
$content = __("No SSL was detected. If you do have an ssl certificate, try to change your current url in the browser address bar to https.", "really-simple-ssl");
} else {
$class = "updated fade notice activate-ssl";
$title = __("Almost ready to enable SSL for some pages!", "really-simple-ssl");
$content = '';
}
ob_start();
?>
<p>
<?php _e("Some things can't be done automatically. Before you start, please check for: ", 'really-simple-ssl')?>
<ul>
<li><?php _e('Http references in your .css and .js files: change any http:// into //', 'really-simple-ssl'); ?></li>
<li><?php _e('Images, stylesheets or scripts from a domain without an ssl certificate: remove them or move to your own server.', 'really-simple-ssl'); ?></li>
</ul>
<?php if ($this->site_has_ssl) { ?>
<form action="" method="post">
<?php wp_nonce_field('rsssl_nonce', 'rsssl_nonce'); ?>
<input type="submit" class='button button-primary' value="Enable SSL per page!"
id="rsssl_do_activate_ssl" name="rsssl_do_activate_ssl">
</form>
<?php } ?>
</p>
<?php
$content .= ob_get_clean();
echo $this->notice_html( $class, $title, $content);
}
/**
* @since 2.3
* Shows option to buy pro
*/
public function show_pro()
{
if (!$this->ad) return;
?>
<p><?php _e('For an extensive scan of your website, with a list of items to fix, and instructions how to do it, Purchase Really Simple SSL Pro, which includes:', 'really-simple-ssl'); ?>
<ul class="rsssl_bullets">
<li><?php _e('Full website scan for mixed content in .css and .js files', 'really-simple-ssl'); ?></li>
<li><?php _e('Full website scan for any resource that is loaded from another domain, and cannot load over ssl', 'really-simple-ssl'); ?></li>
<li><?php _e('Full website scan to find external css or js files with mixed content.', 'really-simple-ssl'); ?></li>
</ul></p>
<a target="_blank" href="<?php echo $this->pro_url; ?>" class='button button-primary'>Learn about Really
Simple SSL PRO</a>
<?php
}
/**
* @return bool
*
* Check if wp-config is writeable
*
*/
public function wpconfig_is_writable()
{
$wpconfig_path = $this->find_wp_config_path();
if (is_writable($wpconfig_path))
return true;
else
return false;
}
/**
* Check if the uninstall file is renamed to .php
*/
protected function check_for_uninstall_file()
{
if (file_exists(dirname(__FILE__) . '/force-deactivate.php')) {
$this->errors["DEACTIVATE_FILE_NOT_RENAMED"] = true;
}
}
/**
* Get the options for this plugin
*
* @since 2.0
*
* @access public
*
*/
public function get_admin_options()
{
$options = get_option('rlrsssl_options');
if (isset($options)) {
$this->ssl_success_message_shown = isset($options['ssl_success_message_shown']) ? $options['ssl_success_message_shown'] : FALSE;
$this->plugin_db_version = isset($options['plugin_db_version']) ? $options['plugin_db_version'] : "1.0";
$this->debug = isset($options['debug']) ? $options['debug'] : FALSE;
}
if (is_multisite()) {
//set rewrite_rule_per_site is deprecated, moving to the ssl_enabled_networkwide property.
$network_options = get_site_option('rlrsssl_network_options');
if (isset($network_options)) {
$this->ssl_enabled_networkwide = isset($network_options['ssl_enabled_networkwide']) ? $network_options['ssl_enabled_networkwide'] : FALSE;
$this->selected_networkwide_or_per_site = isset($network_options['selected_networkwide_or_per_site']) ? $network_options['selected_networkwide_or_per_site'] : FALSE;
}
}
}
/**
* check if the plugin was upgraded to a new version
*
* @since 2.1
*
* @access public
*
*/
public function get_plugin_upgraded()
{
if ($this->plugin_db_version != $this->plugin_version) {
$this->plugin_db_version = $this->plugin_version;
$this->plugin_upgraded = true;
$this->save_options();
}
$this->plugin_upgraded = false;
}
/**
* Log events during plugin execution
*
* @since 2.1
*
* @access public
*
*/
public function trace_log($msg)
{
if (!$this->debug) return;
$this->debug_log = $this->debug_log . "<br>" . $msg;
//$this->debug_log = strstr($this->debug_log,'** Detecting configuration **');
error_log($msg);
}
/**
* Configures the site for ssl
*
* @since 2.2
*
* @access public
*
*/
public function configure_ssl()
{
if (!current_user_can($this->capability)) return;
$this->trace_log("** Configuring SSL **");
if ($this->site_has_ssl) {
//in a configuration of loadbalancer without a set server variable https = 0, add code to wpconfig
if ($this->do_wpconfig_loadbalancer_fix)
$this->wpconfig_loadbalancer_fix();
}
}
/**
* Check to see if we are on the settings page, action hook independent
*
* @since 2.1
*
* @access public
*
*/
public function is_settings_page()
{
if (!isset($_SERVER['QUERY_STRING'])) return false;
parse_str($_SERVER['QUERY_STRING'], $params);
if (array_key_exists("page", $params) && ($params["page"] == "rlrsssl_really_simple_ssl")) {
return true;
}
return false;
}
/**
* Retrieves the current version of this plugin
*
* @since 2.1
*
* @access public
*
*/
public function get_plugin_version()
{
if (!function_exists('get_plugins'))
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
$plugin_folder = get_plugins('/' . plugin_basename(dirname(__FILE__)));
$this->plugin_version = $plugin_folder[$this->plugin_filename]['Version'];
}
/**
* Find the path to wp-config
*
* @since 2.1
*
* @access public
*
*/
public function find_wp_config_path()
{
//limit nr of iterations to 20
$i = 0;
$maxiterations = 20;
$dir = dirname(__FILE__);
do {
$i++;
if (file_exists($dir . "/wp-config.php")) {
return $dir . "/wp-config.php";
}
} while (($dir = realpath("$dir/..")) && ($i < $maxiterations));
return null;
}
/**
* Check if the wpconfig is already fixed
*
* @since 2.2
*
* @access public
*
*/
public function wpconfig_has_fixes()
{
$wpconfig_path = $this->find_wp_config_path();
if (empty($wpconfig_path)) return false;
$wpconfig = file_get_contents($wpconfig_path);
//only one of two fixes possible.
if (strpos($wpconfig, "//Begin Really Simple SSL Load balancing fix") !== FALSE) {
return true;
}
if (strpos($wpconfig, "//Begin Really Simple SSL Server variable fix") !== FALSE) {
return true;
}
return false;
}
/**
* In case of load balancer without server https on, add fix in wp-config
*
* @since 2.1
*
* @access public
*
*/
public function wpconfig_loadbalancer_fix()
{
if (!current_user_can($this->capability)) return;
$wpconfig_path = $this->find_wp_config_path();
if (empty($wpconfig_path)) return;
$wpconfig = file_get_contents($wpconfig_path);
$this->wpconfig_loadbalancer_fix_failed = FALSE;
//only if loadbalancer AND NOT SERVER-HTTPS-ON should the following be added. (is_ssl = false)
if (strpos($wpconfig, "//Begin Really Simple SSL Load balancing fix") === FALSE) {
if (is_writable($wpconfig_path)) {
$rule = "\n" . "//Begin Really Simple SSL Load balancing fix" . "\n";
$rule .= 'if ((isset($_ENV["HTTPS"]) && ("on" == $_ENV["HTTPS"]))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "1") !== false))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "on") !== false))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_CF_VISITOR"]) && (strpos($_SERVER["HTTP_CF_VISITOR"], "https") !== false))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"], "https") !== false))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_X_FORWARDED_PROTO"], "https") !== false))' . "\n";
$rule .= '|| (isset($_SERVER["HTTP_X_PROTO"]) && (strpos($_SERVER["HTTP_X_PROTO"], "SSL") !== false))' . "\n";
$rule .= ') {' . "\n";
$rule .= '$_SERVER["HTTPS"] = "on";' . "\n";
$rule .= '}' . "\n";
$rule .= "//END Really Simple SSL" . "\n";
$insert_after = "<?php";
$pos = strpos($wpconfig, $insert_after);
if ($pos !== false) {
$wpconfig = substr_replace($wpconfig, $rule, $pos + 1 + strlen($insert_after), 0);
}
file_put_contents($wpconfig_path, $wpconfig);
if ($this->debug) {
$this->trace_log("wp config loadbalancer fix inserted");
}
} else {
if ($this->debug) {
$this->trace_log("wp config loadbalancer fix FAILED");
}
$this->wpconfig_loadbalancer_fix_failed = TRUE;
}
} else {
if ($this->debug) {
$this->trace_log("wp config loadbalancer fix already in place, great!");
}
}
$this->save_options();
}
/**
* Checks if we are on a subfolder install. (domain.com/site1 )
*
* @since 2.2
*
* @access protected
*
*/
protected function is_multisite_subfolder_install()
{
if (!is_multisite()) return FALSE;
//we check this manually, as the SUBDOMAIN_INSTALL constant of wordpress might return false for domain mapping configs
$is_subfolder = FALSE;
$sites = $this->get_sites_bw_compatible();
foreach ($sites as $site) {
$this->switch_to_blog_bw_compatible($site);
if ($this->is_subfolder(home_url())) {
$is_subfolder = TRUE;
}
restore_current_blog(); //switches back to previous blog, not current, so we have to do it each loop
if ($is_subfolder) return true;
}
return $is_subfolder;
}
/**
* Removing changes made to the wpconfig
*
* @since 2.1
*
* @access public
*
*/
public function remove_wpconfig_edit()
{
$wpconfig_path = $this->find_wp_config_path();
if (empty($wpconfig_path)) return;
$wpconfig = file_get_contents($wpconfig_path);
//check for permissions
if (!is_writable($wpconfig_path)) {
if ($this->debug) $this->trace_log("could not remove wpconfig edits, wp-config.php not writable");
$this->errors['wpconfig not writable'] = TRUE;
return;
}
//remove edits
$wpconfig = preg_replace("/\/\/Begin\s?Really\s?Simple\s?SSL.*?\/\/END\s?Really\s?Simple\s?SSL/s", "", $wpconfig);
$wpconfig = preg_replace("/\n+/", "\n", $wpconfig);
file_put_contents($wpconfig_path, $wpconfig);
//in multisite environment, with per site activation, re-add
if (is_multisite() && !$this->ssl_enabled_networkwide) {
if ($this->do_wpconfig_loadbalancer_fix)
$this->wpconfig_loadbalancer_fix();
}
}
/**
* Save the plugin options
*
* @since 2.0
*
* @access public
*
*/
public function save_options()
{
//any options added here should also be added to function options_validate()
$options = array(
'site_has_ssl' => $this->site_has_ssl,
'exclude_pages' => $this->exclude_pages,
'permanent_redirect' => $this->permanent_redirect,
'ssl_success_message_shown' => $this->ssl_success_message_shown,
'autoreplace_insecure_links' => $this->autoreplace_insecure_links,
'plugin_db_version' => $this->plugin_db_version,
'debug' => $this->debug,
'ssl_enabled' => $this->ssl_enabled,
'home_ssl' => $this->home_ssl,
);
update_option('rlrsssl_options', $options);
//save multisite options
if (is_multisite()) {
$network_options = array(
'ssl_enabled_networkwide' => $this->ssl_enabled_networkwide,
'selected_networkwide_or_per_site' => $this->selected_networkwide_or_per_site,
);
update_site_option('rlrsssl_network_options', $network_options);
}
}
/**
* Load the translation files
*
* @since 1.0
*
* @access public
*
*/
public function load_translation()
{
load_plugin_textdomain('really-simple-ssl', FALSE, dirname(plugin_basename(__FILE__)) . '/languages/');
}
/**
* Handles deactivation of this plugin
*
* @since 2.0
*
* @access public
*
*/
public function deactivate($networkwide)
{
$this->site_has_ssl = FALSE;
$this->ssl_success_message_shown = FALSE;
$this->autoreplace_insecure_links = TRUE;
$this->ssl_enabled = FALSE;
$this->save_options();
if ($networkwide) {
$this->ssl_enabled_networkwide = FALSE;
$this->selected_networkwide_or_per_site = FALSE;
$sites = $this->get_sites_bw_compatible();
foreach ($sites as $site) {
$this->switch_to_blog_bw_compatible($site);
$this->ssl_enabled = false;
$this->save_options();
restore_current_blog(); //switches back to previous blog, not current, so we have to do it each loop
}
}
$this->remove_wpconfig_edit();
}
private function check_for_siteurl_in_wpconfig()
{
$wpconfig_path = $this->find_wp_config_path();
if (empty($wpconfig_path)) return;
$wpconfig = file_get_contents($wpconfig_path);
$homeurl_pattern = '/(define\(\s*\'WP_HOME\'\s*,\s*\'http\:\/\/)/';
$siteurl_pattern = '/(define\(\s*\'WP_SITEURL\'\s*,\s*\'http\:\/\/)/';
$this->wpconfig_siteurl_not_fixed = FALSE;
if (preg_match($homeurl_pattern, $wpconfig) || preg_match($siteurl_pattern, $wpconfig)) {
$this->wpconfig_siteurl_not_fixed = TRUE;
$this->trace_log("siteurl or home url defines found in wpconfig");
}
}
/**
* Checks if we are currently on ssl protocol, but extends standard wp with loadbalancer check.
*
* @since 2.0
*
* @access public
*
*/
public function is_ssl_extended()
{
$server_var = FALSE;
if ((isset($_ENV['HTTPS']) && ('on' == $_ENV['HTTPS']))
|| (isset($_SERVER['HTTP_X_FORWARDED_SSL']) && (strpos($_SERVER['HTTP_X_FORWARDED_SSL'], '1') !== false))
|| (isset($_SERVER['HTTP_X_FORWARDED_SSL']) && (strpos($_SERVER['HTTP_X_FORWARDED_SSL'], 'on') !== false))
|| (isset($_SERVER['HTTP_CF_VISITOR']) && (strpos($_SERVER['HTTP_CF_VISITOR'], 'https') !== false))
|| (isset($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO']) && (strpos($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'], 'https') !== false))
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false))
|| (isset($_SERVER['HTTP_X_PROTO']) && (strpos($_SERVER['HTTP_X_PROTO'], 'SSL') !== false))
) {
$server_var = TRUE;
}
if (is_ssl() || $server_var) {
return true;
} else {
return false;
}
}
/**
* Checks for SSL by opening a test page in the plugin directory
*
* @since 2.0
*
* @access public
*
*/
public function detect_configuration()
{
global $rsssl_url;
$this->trace_log("** Detecting configuration **");
$this->trace_log("plugin version: " . $this->plugin_version);
$old_ssl_setting = $this->site_has_ssl;
$filecontents = "";
//if current page is on ssl, we can assume ssl is available, even when an errormsg was returned
if ($this->is_ssl_extended()) {
$this->trace_log("Already on SSL, start detecting configuration");
$this->site_has_ssl = TRUE;
} else {
//we're not on SSL, or no server vars were returned, so test with the test-page.
//plugin url: domain.com/wp-content/etc
$plugin_url = str_replace("http://", "https://", $this->plugin_url);
$testpage_url = trailingslashit($plugin_url) . "ssl-test-page.php";
$this->trace_log("Opening testpage to check for ssl: " . $testpage_url);
$filecontents = $rsssl_url->get_contents($testpage_url);
if ($rsssl_url->error_number != 0) {
$errormsg = $rsssl_url->get_curl_error($rsssl_url->error_number);
$this->site_has_ssl = FALSE;
$this->trace_log("No ssl detected. the ssl testpage returned an error: " . $errormsg);
} else {
$this->site_has_ssl = TRUE;
$this->trace_log("SSL test page loaded successfully");
}
}
if ($this->site_has_ssl) {
//check the type of ssl, either by parsing the returned string, or by reading the server vars.
if ((strpos($filecontents, "#CLOUDFRONT#") !== false) || (isset($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO']) && ($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] == 'https'))) {
$this->ssl_type = "CLOUDFRONT";
} elseif ((strpos($filecontents, "#CLOUDFLARE#") !== false) || (isset($_SERVER['HTTP_CF_VISITOR']) && ($_SERVER['HTTP_CF_VISITOR'] == 'https'))) {
$this->ssl_type = "CLOUDFLARE";
} elseif ((strpos($filecontents, "#LOADBALANCER#") !== false) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))) {
$this->ssl_type = "LOADBALANCER";
} elseif ((strpos($filecontents, "#CDN#") !== false) || (isset($_SERVER['HTTP_X_FORWARDED_SSL']) && ($_SERVER['HTTP_X_FORWARDED_SSL'] == 'on' || $_SERVER['HTTP_X_FORWARDED_SSL'] == '1'))) {
$this->ssl_type = "CDN";
} elseif ((strpos($filecontents, "#SERVER-HTTPS-ON#") !== false) || (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')) {
$this->ssl_type = "SERVER-HTTPS-ON";
} elseif ((strpos($filecontents, "#SERVER-HTTPS-1#") !== false) || (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == '1')) {
$this->ssl_type = "SERVER-HTTPS-1";
} elseif ((strpos($filecontents, "#SERVERPORT443#") !== false) || (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT']))) {
$this->ssl_type = "SERVERPORT443";
} elseif ((strpos($filecontents, "#ENVHTTPS#") !== false) || (isset($_ENV['HTTPS']) && ('on' == $_ENV['HTTPS']))) {
$this->ssl_type = "ENVHTTPS";
} elseif ((strpos($filecontents, "#NO KNOWN SSL CONFIGURATION DETECTED#") !== false)) {
//if we are here, SSL was detected, but without any known server variables set.
//So we can use this info to set a server variable ourselfes.
if (!$this->wpconfig_has_fixes()) {
$this->no_server_variable = TRUE;
}
$this->trace_log("No server variable detected ");
$this->ssl_type = "NA";
} else {
//no valid response, so set to NA
$this->ssl_type = "NA";
}
//check for is_ssl()
if ((!$this->is_ssl_extended() &&
(strpos($filecontents, "#SERVER-HTTPS-ON#") === false) &&
(strpos($filecontents, "#SERVER-HTTPS-1#") === false) &&
(strpos($filecontents, "#SERVERPORT443#") === false)) || (!is_ssl() && $this->is_ssl_extended())) {
//when is_ssl would return false, we should add some code to wp-config.php
if (!$this->wpconfig_has_fixes()) {
$this->trace_log("is_ssl() will return false: wp-config fix needed");
$this->do_wpconfig_loadbalancer_fix = TRUE;
}
}
$this->trace_log("ssl type: " . $this->ssl_type);
}
$this->check_for_siteurl_in_wpconfig();