PluginProbe
Two Factor Authentication / 1.15.5
Two Factor Authentication v1.15.5
1.12.2 1.13.0 1.14.10 1.14.11 1.14.14 1.14.15 1.14.16 1.14.17 1.14.23 1.14.24 1.14.26 1.14.27 1.14.3 1.14.4 1.14.5 1.14.7 1.14.8 1.15.5 1.16.0 1.2.10 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 All 98 releases
two-factor-authentication / two-factor-login.php

two-factor-login.php in Two Factor Authentication 1.15.5, at two-factor-login.php

292 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Two Factor Authentication
4 Plugin URI: https://www.simbahosting.co.uk/s3/product/two-factor-authentication/
5 Description: Secure your WordPress login forms with two factor authentication - including WooCommerce login forms
6 Author: David Anderson, original plugin by Oskar Hane and enhanced by Dee Nutbourne
7 Author URI: https://www.simbahosting.co.uk
8 Version: 1.15.5
9 Text Domain: two-factor-authentication
10 Domain Path: /languages
11 License: GPLv2 or later
12 */
13
14 register_activation_hook(__FILE__, 'simba_two_factor_authentication_activation');
15
16 if (!function_exists('simba_two_factor_authentication_activation')) {
17 function simba_two_factor_authentication_activation() {
18 if (!empty($GLOBALS['simba_two_factor_authentication'])) {
19 $is_2fa_plugin_active = false;
20 $installed_plugins_slugs = array_keys(get_plugins());
21 foreach ($installed_plugins_slugs as $installed_plugin_slug) {
22 if (is_plugin_active($installed_plugin_slug)) {
23 $temp_split_plugin_slug = explode('/', $installed_plugin_slug);
24 if (isset($temp_split_plugin_slug[1]) && 'two-factor-login.php' == $temp_split_plugin_slug[1]) {
25 $is_2fa_plugin_active = true;
26 break;
27 }
28 }
29 }
30
31 // We should prevent activation if and only if either the 2FA Premium or 2FA Free plugin is active.
32 // We should not prevent activation if either the AIOS plugin is active.
33 if ($is_2fa_plugin_active) {
34 if (file_exists(__DIR__.'/simba-tfa/premium/loader.php')) {
35 wp_die(esc_html__('To activate Two Factor Authentication Premium, first de-activate the free version (only one can be active at once).', 'two-factor-authentication'));
36 } else { // If the 2FA Premium plugin is active and tries to activate the 2FA Free Plugin, it throws a fatal error and stops activating the free version.
37 wp_die(esc_html__("You can't activate Two Factor Authentication (Free) because Two Factor Authentication Premium is active (only one can be active at once).", 'two-factor-authentication'));
38 }
39 }
40 }
41 }
42 }
43
44 if (!defined('SIMBA_TFA_TEXT_DOMAIN')) define('SIMBA_TFA_TEXT_DOMAIN', 'two-factor-authentication');
45 if (!class_exists('Simba_Two_Factor_Authentication_1')) require dirname(__FILE__).'/simba-tfa/simba-tfa.php';
46
47 if (!class_exists('Simba_Two_Factor_Authentication_Plugin')):
48 /**
49 * This parent-child relationship enables the two to be split without affecting backwards compatibility for developers making direct calls
50 *
51 * This class is for the plugin encapsulation.
52 */
53 class Simba_Two_Factor_Authentication_Plugin extends Simba_Two_Factor_Authentication_1 {
54
55 public $version = '1.15.5';
56
57 const PHP_REQUIRED = '5.6';
58
59 /**
60 * Constructor, run upon plugin initiation
61 *
62 * @uses __FILE__
63 */
64 public function __construct() {
65
66 add_action('plugins_loaded', array($this, 'plugins_loaded_load_textdomain'));
67
68 add_action('init', array($this, 'init_child'));
69
70 if (version_compare(PHP_VERSION, self::PHP_REQUIRED, '<' )) {
71 add_action('all_admin_notices', array($this, 'admin_notice_insufficient_php'));
72 $abort = true;
73 }
74
75 if (!function_exists('mcrypt_get_iv_size') && !function_exists('openssl_cipher_iv_length')) {
76 add_action('all_admin_notices', array($this, 'admin_notice_missing_mcrypt_and_openssl'));
77 $abort = true;
78 }
79
80 $encryption_enabled = $this->get_option('tfa_encrypt_secrets');
81 if ($encryption_enabled && (!defined('SIMBA_TFA_DB_ENCRYPTION_KEY') || '' === SIMBA_TFA_DB_ENCRYPTION_KEY)) {
82 add_action('all_admin_notices', array($this, 'admin_notice_missing_db_encryption_key'));
83 }
84
85 if (!empty($abort)) return;
86
87 // Menu entries
88 add_action('admin_menu', array($this, 'menu_entry_for_admin'));
89 add_action('admin_menu', array($this, 'menu_entry_for_user'));
90 add_action('network_admin_menu', array($this, 'menu_entry_for_user'));
91
92 // Add settings link in plugin list
93 $plugin = plugin_basename(__FILE__);
94 add_filter("plugin_action_links_$plugin", array($this, 'add_plugin_settings_link'));
95 add_filter("network_admin_plugin_action_links_$plugin", array($this, 'add_plugin_settings_link'));
96
97 $this->set_user_settings_page_slug('two-factor-auth-user');
98
99 $this->set_plugin_translate_url('https://translate.wordpress.org/projects/wp-plugins/two-factor-authentication/');
100
101 if (is_multisite() && function_exists('switch_to_blog')) {
102 $main_site_id = function_exists('get_main_site_id') ? get_main_site_id() : 1;
103 switch_to_blog($main_site_id);
104 }
105 $this->set_site_wide_administration_url(admin_url('options-general.php?page=two-factor-auth'));
106 if (is_multisite() && function_exists('restore_current_blog')) restore_current_blog();
107
108 $this->set_premium_version_url('https://www.simbahosting.co.uk/s3/product/two-factor-authentication/');
109 $this->set_faq_url('https://wordpress.org/plugins/two-factor-authentication/#faq');
110 parent::__construct();
111
112 }
113
114 /**
115 * Runs upon the WP filters plugin_action_links_(plugin) and network_plugin_action_links_(plugin)
116 *
117 * @param Array $links
118 *
119 * @return Array
120 */
121 public function add_plugin_settings_link($links) {
122 if (is_multisite()) {
123 $main_site_id = function_exists('get_main_site_id') ? get_main_site_id() : 1;
124 switch_to_blog($main_site_id);
125 $link = $this->get_settings_link();
126 restore_current_blog();
127 array_unshift($links, $link);
128 } else {
129 $link = $this->get_settings_link();
130 array_unshift($links, $link);
131 }
132
133 $link2 = '<a href="admin.php?page=two-factor-auth-user">'.__('User settings', 'two-factor-authentication').'</a>';
134 array_unshift($links, $link2);
135
136 return $links;
137 }
138
139 /**
140 * Get 2FA settings anchor tag link.
141 *
142 * @return string 2FA settings anchor tag link.
143 */
144 private function get_settings_link() {
145 return '<a href="'.admin_url('options-general.php').'?page=two-factor-auth">'.__('Plugin settings', 'two-factor-authentication').'</a>';
146 }
147
148 /**
149 * Runs upon the WP actions admin_menu and network_admin_menu
150 */
151 public function menu_entry_for_user() {
152
153 global $current_user;
154 if ($this->is_activated_for_user($current_user->ID)) {
155 add_menu_page(__('Two Factor Authentication', 'two-factor-authentication'), __('Two Factor Auth', 'two-factor-authentication'), 'read', 'two-factor-auth-user', array($this, 'show_dashboard_user_settings_page'), $this->includes_url().'/tfa_admin_icon_16x16.png', 72);
156 }
157 }
158
159 /**
160 * Runs upon the WP action admin_menu
161 */
162 public function menu_entry_for_admin() {
163
164 $skip_adding_options_menu_entry = (is_multisite() && (!is_super_admin() || !is_main_site()));
165
166 $skip_adding_options_menu_entry = apply_filters('simba_tfa_skip_adding_options_menu_entry', $skip_adding_options_menu_entry);
167
168 if ($skip_adding_options_menu_entry) return;
169
170 add_options_page(
171 __('Two Factor Authentication', 'two-factor-authentication'),
172 __('Two Factor Authentication', 'two-factor-authentication'),
173 $this->get_management_capability(),
174 'two-factor-auth',
175 array($this, 'show_admin_settings_page')
176 );
177 }
178
179 /**
180 * Include the admin settings page code
181 */
182 public function show_admin_settings_page() {
183
184 if (!is_admin() || !current_user_can($this->get_management_capability())) return;
185
186 $admin_settings_links = array();
187 if (!class_exists('Simba_Two_Factor_Authentication_Premium')) {
188 $admin_settings_links[] = array(
189 'url' => 'https://www.simbahosting.co.uk/s3/product/two-factor-authentication/',
190 'title' => __('Premium version', 'two-factor-authentication'),
191 );
192 }
193 $simba_tfa_support_url = apply_filters('simba_tfa_support_url', 'https://wordpress.org/support/plugin/two-factor-authentication/');
194
195 $admin_settings_links[] = array(
196 'url' => $simba_tfa_support_url,
197 'title' => __('Support', 'two-factor-authentication'),
198 );
199
200 $admin_settings_links[] = array(
201 'url' => 'https://profiles.wordpress.org/davidanderson#content-plugins',
202 'title' => __('More free plugins', 'two-factor-authentication'),
203 );
204
205 $admin_settings_links[] = array(
206 'url' => 'http://updraftplus.com',
207 'title' => 'UpdraftPlus - '.__('WordPress backups', 'two-factor-authentication'),
208 );
209
210 $admin_settings_links[] = array(
211 'url' => 'https://www.simbahosting.co.uk/s3/shop/',
212 'title' => __('More premium plugins', 'two-factor-authentication'),
213 );
214
215 $admin_settings_links[] = array(
216 'url' => 'https://twitter.com/updraftplus',
217 'title' => __('Twitter', 'two-factor-authentication'),
218 );
219
220 $admin_settings_links[] = array(
221 'url' => 'https://david.dw-perspective.org.uk',
222 'title' => __("Lead developer's homepage", 'two-factor-authentication'),
223 );
224
225 $admin_settings_links = apply_filters('simba_tfa_admin_settings_links', $admin_settings_links);
226
227 $this->include_template('admin-settings.php', array(
228 'settings_page_heading' => $this->get_settings_page_heading(),
229 'admin_settings_links' => $admin_settings_links,
230 ));
231 }
232
233 /**
234 * Runs conditionally on the WP action all_admin_notices
235 */
236 public function admin_notice_insufficient_php() {
237 /* translators: 1. PHP required 2. PHP version. */
238 $this->show_admin_warning('<strong>'.__('Higher PHP version required', 'two-factor-authentication').'</strong><br> '.sprintf(__('The Two Factor Authentication plugin requires PHP version %1$s or higher - your current version is only %2$s.', 'two-factor-authentication'), self::PHP_REQUIRED, PHP_VERSION), 'error');
239 }
240
241 /**
242 * Runs conditionally on the WP action all_admin_notices
243 */
244 public function admin_notice_missing_mcrypt_and_openssl() {
245 $this->show_admin_warning('<strong>'.__('PHP OpenSSL or mcrypt module required', 'two-factor-authentication').'</strong><br> '.__('The Two Factor Authentication plugin requires either the PHP openssl (preferred) or mcrypt module to be installed. Please ask your web hosting company to install one of them.', 'two-factor-authentication'), 'error');
246 }
247
248 /**
249 * Runs conditionally on the WP action all_admin_notices
250 */
251 public function admin_notice_missing_db_encryption_key() {
252 $this->show_admin_warning('<strong>'.__('Two Factor Authentication encryption key not found', 'two-factor-authentication').'</strong><br> '.htmlspecialchars(__('The "encrypt secrets" feature is currently enabled, but no encryption key has been found (set via the SIMBA_TFA_DB_ENCRYPTION_KEY constant).', 'two-factor-authentication').' '.__('This indicates that either setup failed, or your WordPress installation has been corrupted.', 'two-factor-authentication')) . ' <a href="' . esc_url($this->get_faq_url()) . '">'. __('Go here for the FAQs, which explain how a website owner can de-activate the plugin without needing to login.', 'two-factor-authentication') .'</a>', 'error');
253 }
254
255 /**
256 * Run upon the WP plugins_loaded action. This method is called even if main loading aborts - so don't put anything else in it (use a separate method).
257 */
258 public function plugins_loaded_load_textdomain() {
259 load_plugin_textdomain(
260 'two-factor-authentication',
261 false,
262 dirname(plugin_basename(__FILE__)).'/languages/'
263 );
264 }
265
266 /**
267 * Run upon the WP init action. This method is called even if main loading aborts - so don't put anything else in it (use a separate method).
268 */
269 public function init_child() {
270 /* translators: %s: plugin version. */
271 $this->set_settings_page_heading(sprintf(__('Two Factor Authentication (Version: %s) - Admin Settings', 'two-factor-authentication'), $this->version));
272 }
273 }
274 endif;
275
276 $GLOBALS['simba_two_factor_authentication'] = new Simba_Two_Factor_Authentication_Plugin();
277
278 if (file_exists(__DIR__.'/simba-tfa/premium/loader.php') && empty($GLOBALS['simba_two_factor_authentication_premium'])) {
279 if (!class_exists('Simba_Two_Factor_Authentication_Premium')) include_once(__DIR__.'/simba-tfa/premium/loader.php');
280
281 $GLOBALS['simba_two_factor_authentication_premium'] = new Simba_Two_Factor_Authentication_Premium($GLOBALS['simba_two_factor_authentication']);
282
283 if (!class_exists('Updraft_Manager_Updater_1_9')) require_once(plugin_dir_path(__FILE__).'vendor/davidanderson684/simba-plugin-manager-updater/class-udm-updater.php');
284
285 try {
286 new Updraft_Manager_Updater_1_9('https://www.simbahosting.co.uk/s3', 1, 'two-factor-authentication-premium/two-factor-login.php', array('require_login' => false));
287 } catch (Exception $e) {
288 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Required for error handling.
289 error_log($e->getMessage());
290 }
291 }
292