@@ -158,8 +158,8 @@ function secupress_check_ban_ips_maybe_send_unban_email( $ip ) {
158
158
}
159
159
160
160
// Send message.
161
- $ url = esc_url_raw ( wp_nonce_url ( home_url ( '?action=secupress_self-unban-ip ' ), 'secupress_self-unban-ip- ' . $ ip ) );
162
- $ message = ' <p> ' . sprintf (
161
+ $ url = str_replace ( ' & ' , ' & ' , esc_url_raw ( wp_nonce_url ( home_url ( '?action=secupress_self-unban-ip ' ), 'secupress_self-unban-ip- ' . $ ip ) ) );
162
+ $ message = sprintf (
163
163
/** Translators: %s is a "unlock yourself" link. */
164
164
__ ( 'You got yourself locked out?
165
165
@@ -168,8 +168,8 @@ function secupress_check_ban_ips_maybe_send_unban_email( $ip ) {
168
168
Regards,
169
169
All at ###SITENAME###
170
170
###SITEURL### ' , 'secupress ' ),
171
- ' <a href=" ' . $ url . ' "> ' . __ ( 'unlock yourself ' , 'secupress ' ) . '</a> ( ' . $ url . ') '
172
- ) . ' </p> ' ;
171
+ __ ( 'unlock yourself ' , 'secupress ' ) . ' ( ' . $ url . ' ) '
172
+ );
173
173
174
174
$ subject = sprintf ( __ ( 'Unban yourself from %s ' , 'secupress ' ), home_url () );
175
175
/**
@@ -511,7 +511,6 @@ function secupress_add_cookiehash_muplugin() {
511
511
secupress_auto_login ( 'WP_Config ' );
512
512
}
513
513
514
-
515
514
add_action ( 'plugins_loaded ' , 'secupress_add_salt_muplugin ' , 50 );
516
515
/**
517
516
* Will create a mu plugin to early set the salt keys.
@@ -535,7 +534,7 @@ function secupress_add_salt_muplugin() {
535
534
return ;
536
535
}
537
536
538
- if ( ! is_array ( $ data ) || ! isset ( $ data ['ID ' ], $ data [ ' username ' ] ) ) {
537
+ if ( ! is_array ( $ data ) || ! isset ( $ data ['ID ' ] ) ) {
539
538
secupress_delete_site_transient ( 'secupress-add-salt-muplugin ' );
540
539
return ;
541
540
}
@@ -546,15 +545,8 @@ function secupress_add_salt_muplugin() {
546
545
547
546
secupress_delete_site_transient ( 'secupress-add-salt-muplugin ' );
548
547
549
- // Make sure we find the `wp-config.php` file.
550
- $ wpconfig_filepath = secupress_is_wpconfig_writable ();
551
-
552
- if ( ! $ wpconfig_filepath ) {
553
- return ;
554
- }
555
-
556
548
// Create the MU plugin.
557
- if ( ! defined ( 'SECUPRESS_SALT_KEYS_MODULE_ACTIVE ' ) ) {
549
+ if ( ! defined ( 'SECUPRESS_SALT_KEYS_MODULE_EXISTS ' ) ) {
558
550
$ alicia_keys = file_get_contents ( SECUPRESS_INC_PATH . 'data/salt-keys.phps ' );
559
551
$ args = array (
560
552
'{{PLUGIN_NAME}} ' => SECUPRESS_PLUGIN_NAME ,
@@ -571,48 +563,45 @@ function secupress_add_salt_muplugin() {
571
563
}
572
564
}
573
565
574
- /**
575
- * Remove old secret keys from the `wp-config.php` file and add a comment.
576
- * We have to make sure the comment is added, only once, only if one or more keys are found, even if some secret keys are missing, and do not create useless empty lines.
577
- */
578
- $ wp_filesystem = secupress_get_filesystem ();
579
- $ wpconfig_content = $ wp_filesystem ->get_contents ( $ wpconfig_filepath );
580
- $ keys = array ( 'AUTH_KEY ' , 'SECURE_AUTH_KEY ' , 'LOGGED_IN_KEY ' , 'NONCE_KEY ' , 'AUTH_SALT ' , 'SECURE_AUTH_SALT ' , 'LOGGED_IN_SALT ' , 'NONCE_SALT ' );
581
- $ comment_added = false ;
582
- $ comment = '/** If you want to add secret keys back in wp-config.php, get new ones at https://api.wordpress.org/secret-key/1.1/salt, then delete this file. */ ' ;
583
- $ placeholder = '/** SecuPress salt placeholder. */ ' ;
584
-
585
- foreach ( $ keys as $ i => $ constant ) {
586
- $ pattern = '@define\s*\(\s*([ \'"]) ' . $ constant . '\1.*@ ' ;
587
-
588
- if ( preg_match ( $ pattern , $ wpconfig_content , $ matches ) ) {
589
- $ replace = $ comment_added ? $ placeholder : $ comment ;
590
- $ wpconfig_content = str_replace ( $ matches [0 ], $ replace , $ wpconfig_content );
591
- $ comment_added = true ;
592
- }
593
- }
594
-
595
- if ( $ comment_added ) {
596
- $ wpconfig_content = str_replace ( $ placeholder . "\n" , '' , $ wpconfig_content );
597
-
598
- $ wp_filesystem ->put_contents ( $ wpconfig_filepath , $ wpconfig_content , FS_CHMOD_FILE );
599
- }
600
-
566
+ $ keys = array ( 'AUTH_KEY ' , 'SECURE_AUTH_KEY ' , 'LOGGED_IN_KEY ' , 'NONCE_KEY ' , 'AUTH_SALT ' , 'SECURE_AUTH_SALT ' , 'LOGGED_IN_SALT ' , 'NONCE_SALT ' );
601
567
// Remove old secret keys from the database.
602
568
foreach ( $ keys as $ constant ) {
603
569
delete_site_option ( $ constant );
604
570
}
605
571
606
- // Destroy the user session.
607
- wp_clear_auth_cookie ();
608
- if ( function_exists ( 'wp_destroy_current_session ' ) ) { // WP 4.0 min.
609
- wp_destroy_current_session ();
610
- }
572
+ // Make sure we find the `wp-config.php` file.
573
+ $ wpconfig_filepath = secupress_is_wpconfig_writable ();
574
+
575
+
576
+ if ( $ wpconfig_filepath ) {
577
+ /**
578
+ * Remove old secret keys from the `wp-config.php` file and add a comment.
579
+ * We have to make sure the comment is added, only once, only if one or more keys are found, even if some secret keys are missing, and do not create useless empty lines.
580
+ */
581
+ $ wp_filesystem = secupress_get_filesystem ();
582
+ $ wpconfig_content = $ wp_filesystem ->get_contents ( $ wpconfig_filepath );
583
+ $ comment_added = false ;
584
+ $ comment = '/** If you want to add secret keys back in wp-config.php, get new ones at https://api.wordpress.org/secret-key/1.1/salt, then delete this file. */ ' ;
585
+ $ placeholder = '/** SecuPress salt placeholder. */ ' ;
586
+
587
+ foreach ( $ keys as $ i => $ constant ) {
588
+ $ pattern = '@define\s*\(\s*([ \'"]) ' . $ constant . '\1.*@ ' ;
589
+
590
+ if ( preg_match ( $ pattern , $ wpconfig_content , $ matches ) ) {
591
+ $ replace = $ comment_added ? $ placeholder : $ comment ;
592
+ $ wpconfig_content = str_replace ( $ matches [0 ], $ replace , $ wpconfig_content );
593
+ $ comment_added = true ;
594
+ }
595
+ }
611
596
612
- $ token = md5 ( time () );
613
- secupress_set_site_transient ( ' secupress_auto_login_ ' . $ token , array ( $ data [ ' username ' ] , 'Salt_Keys ' ), MINUTE_IN_SECONDS );
597
+ if ( $ comment_added ) {
598
+ $ wpconfig_content = str_replace ( $ placeholder . "\n" , '' , $ wpconfig_content );
614
599
615
- wp_safe_redirect ( esc_url_raw ( add_query_arg ( 'secupress_auto_login_token ' , $ token , secupress_get_current_url ( 'raw ' ) ) ) );
600
+ $ wp_filesystem ->put_contents ( $ wpconfig_filepath , $ wpconfig_content , FS_CHMOD_FILE );
601
+ }
602
+ }
603
+
604
+ secupress_auto_login ( 'Salt_Keys ' );
616
605
die ();
617
606
}
618
607
@@ -743,3 +732,49 @@ function secupress_get_php_versions() {
743
732
if ( is_admin () ) {
744
733
add_action ( 'user_register ' , array ( 'SecuPress_Admin_Pointers ' , 'dismiss_pointers_for_new_users ' ) );
745
734
}
735
+
736
+
737
+ /**
738
+ * Redirect the user on a specific URL to be autologged-in
739
+ *
740
+ * @since 2.0
741
+ * @author Julio Potier
742
+ *
743
+ * @param (string) $module The SecuPress module to be redirected
744
+ * @param (WP_User|int) $user The user to be logged in
745
+ **/
746
+ function secupress_auto_login ( $ module , $ user = null ) {
747
+ if ( is_int ( $ user ) ) {
748
+ $ user = new WP_User ( $ user );
749
+ }
750
+ if ( is_a ( $ user , 'WP_User ' ) ) {
751
+ $ current_user = $ user ;
752
+ } else {
753
+ $ current_user = wp_get_current_user ();
754
+ }
755
+ if ( ! $ current_user ) {
756
+ return ;
757
+ }
758
+ $ token = md5 ( time () . $ module );
759
+ secupress_set_site_transient ( 'secupress_auto_login_ ' . $ token , array ( $ current_user ->user_login , $ module ), MINUTE_IN_SECONDS );
760
+
761
+ wp_safe_redirect ( esc_url_raw ( add_query_arg ( 'secupress_auto_login_token ' , $ token ) ) );
762
+ die ();
763
+ }
764
+
765
+ add_filter ( 'authenticate ' , 'secupress_authenticate_cookie ' , 0 );
766
+ function secupress_authenticate_cookie ( $ user ) {
767
+ $ data = secupress_get_site_transient ( 'secupress-auto-login ' );
768
+
769
+ if ( ! $ data ) {
770
+ return $ user ;
771
+ }
772
+
773
+ secupress_delete_site_transient ( 'secupress-auto-login ' );
774
+
775
+ if ( ! is_array ( $ data ) || ! isset ( $ data ['ID ' ] ) ) {
776
+ return $ user ;
777
+ }
778
+
779
+ secupress_auto_login ( 'Salt_Keys ' , $ user );
780
+ }
0 commit comments