-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-mfa.php
More file actions
100 lines (85 loc) · 3.36 KB
/
Copy pathsimple-mfa.php
File metadata and controls
100 lines (85 loc) · 3.36 KB
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
<?php
/**
* Plugin Name: Simple MFA
* Plugin URI: https://votre-site.com/wp-simple-mfa
* Description: A simple and lightweight Multi-Factor Authentication plugin supporting TOTP and Email.
* Author: Jonathan Scapin & Armand Matthey-Doret
* Author URI: https://jonathanscapin.fr
* Text Domain: simple-mfa
* Domain Path: /languages
* Version: 0.2.4
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 6.8
* Requires PHP: 8.1
*
* @package Simple_Mfa
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'SIMPLE_MFA_VERSION', '0.2.4' );
define( 'SIMPLE_MFA_DIR', plugin_dir_path( __FILE__ ) );
define( 'SIMPLE_MFA_FILE', __FILE__ );
function simple_mfa_defaults(): array {
return [
'enabled' => 1,
'totp_enabled' => 1,
'email_otp_enabled' => 0,
'grace_period' => 7,
'trust_duration' => 30,
'code_expiry' => 10,
];
}
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
spl_autoload_register(function ($class) {
$prefix = 'SimpleMfa\\';
$base_dir = __DIR__ . '/src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) return;
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) require $file;
});
new \SimpleMfa\Admin\Settings();
new \SimpleMfa\Admin\Profile();
new \SimpleMfa\Core\Auth();
register_activation_hook( __FILE__, function() {
global $wpdb;
$table_name = $wpdb->prefix . 'mfa_logs';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
user_login varchar(60) NOT NULL,
status varchar(20) NOT NULL,
method varchar(20) DEFAULT '' NOT NULL,
ip_address varchar(45) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
});
add_action( 'phpmailer_init', function ( $phpmailer ) {
$mfa_settings = get_option( 'mfa_settings', [] );
if ( ! empty( $mfa_settings['enable_custom_smtp'] ) ) {
$phpmailer->isSMTP();
$phpmailer->Host = ! empty( $mfa_settings['smtp_host'] ) ? $mfa_settings['smtp_host'] : 'localhost';
$phpmailer->Port = ! empty( $mfa_settings['smtp_port'] ) ? $mfa_settings['smtp_port'] : 25;
$phpmailer->SMTPAuth = false;
$phpmailer->SMTPSecure = false;
$default_email = get_option( 'admin_email' );
$from_email = ! empty( $mfa_settings['smtp_from'] ) ? $mfa_settings['smtp_from'] : $default_email;
$from_name = ! empty( $mfa_settings['smtp_from_name'] ) ? $mfa_settings['smtp_from_name'] : get_bloginfo( 'name' );
$phpmailer->From = $from_email;
$phpmailer->FromName = $from_name;
$phpmailer->Sender = $from_email;
}
}, 999 );
add_action( 'wp_mail_failed', function ( $error ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'WP Mail Failed: ' . print_r( $error->get_error_data(), true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
}
}, 10 );