PluginProbe
WP 2FA – Two-factor authentication for WordPress / 2.9.3
WP 2FA – Two-factor authentication for WordPress v2.9.3
4.1.0 4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 All 42 releases
wp-2fa / wp-2fa.php
wp-2fa.php
307 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP 2FA - Two-factor authentication for WordPress .
4 *
5 * @copyright Copyright (C) 2013-2025, Melapress - support@melapress.com
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 or higher
7 *
8 * @wordpress-plugin
9 * Plugin Name: WP 2FA - Two-factor authentication for WordPress
10 * Version: 2.9.3
11 * Plugin URI: https://melapress.com/
12 * Description: Easily add an additional layer of security to your WordPress login pages. Enable Two-Factor Authentication for you and all your website users with this easy to use plugin.
13 * Author: Melapress
14 * Author URI: https://melapress.com/
15 * Text Domain: wp-2fa
16 * Domain Path: /languages/
17 * License: GPL v3
18 * Requires at least: 5.5
19 * Requires PHP: 7.4
20 * Network: true
21 *
22 * @package WP2FA
23 *
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 *
37 * @fs_ignore /dist/, /extensions/, /freemius/, /includes/, /languages/, /third-party/, /vendor/
38 */
39
40 use WP2FA\WP2FA;
41 use WP2FA\Utils\Migration;
42 use WP2FA\Extensions_Loader;
43 use WP2FA\Admin\Helpers\WP_Helper;
44 use WP2FA\Freemius\Freemius_Helper;
45 use WP2FA\Admin\Helpers\File_Writer;
46
47 if ( ! defined( 'ABSPATH' ) ) {
48 exit;
49 }
50
51 if ( defined( '\DISABLE_2FA_LOGIN' ) && \DISABLE_2FA_LOGIN ) {
52 return;
53 }
54
55 \add_action( 'doing_it_wrong_trigger_error', 'wp_2fa_trigger_error', 10, 4 );
56 \add_action( 'doing_it_wrong_run', 'wp_2fa_action_doing_it_wrong_run', 0, 3 );
57 \add_action( 'doing_it_wrong_run', 'wp_2fa_action_doing_it_wrong_run', 20, 3 );
58 \add_action( 'aadvana_trigger_error_doing_it_wrong', 'wp_2fa_trigger_error', 0, 4 );
59
60 // Useful global constants.
61 if ( ! defined( 'WP_2FA_VERSION' ) ) {
62 define( 'WP_2FA_VERSION', '2.9.3' );
63 define( 'WP_2FA_BASE', plugin_basename( __FILE__ ) );
64 define( 'WP_2FA_URL', plugin_dir_url( __FILE__ ) );
65 define( 'WP_2FA_PATH', WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . dirname( WP_2FA_BASE ) . DIRECTORY_SEPARATOR );
66 define( 'WP_2FA_INC', WP_2FA_PATH . 'includes/' );
67 define( 'WP_2FA_FILE', __FILE__ );
68 define( 'WP_2FA_LOGS_DIR', 'wp-2fa-logs' );
69
70 // Prefix used in usermetas, settings and transients.
71 define( 'WP_2FA_PREFIX', 'wp_2fa_' );
72 define( 'WP_2FA_POLICY_SETTINGS_NAME', WP_2FA_PREFIX . 'policy' );
73 define( 'WP_2FA_SETTINGS_NAME', WP_2FA_PREFIX . 'settings' );
74 define( 'WP_2FA_WHITE_LABEL_SETTINGS_NAME', WP_2FA_PREFIX . 'white_label' );
75 define( 'WP_2FA_EMAIL_SETTINGS_NAME', WP_2FA_PREFIX . 'email_settings' );
76
77 define( 'WP_2FA_PREFIX_PAGE', 'wp-2fa-' );
78
79 define( 'WP_2FA_TEXTDOMAIN', 'wp-2fa' );
80 }
81
82 // phpcs:disable
83 // phpcs:enable
84 // Include files.
85 require_once WP_2FA_INC . 'functions/core.php';
86
87 // Require Composer autoloader if it exists.
88 if ( file_exists( WP_2FA_PATH . 'vendor/autoload.php' ) ) {
89 require_once WP_2FA_PATH . 'vendor/autoload.php';
90 }
91
92 // run any required update routines.
93 Migration::migrate();
94
95 // Setup_Wizard.
96 if ( WP_Helper::is_multisite() ) {
97 \add_action( 'network_admin_menu', array( '\WP2FA\Admin\Setup_Wizard', 'network_admin_menus' ), 10 );
98 \add_action( 'admin_menu', array( '\WP2FA\Admin\Setup_Wizard', 'admin_menus' ), 10 );
99 } else {
100 \add_action( 'admin_menu', array( '\WP2FA\Admin\Setup_Wizard', 'admin_menus' ), 10 );
101 }
102
103 // Activation/Deactivation.
104 \register_activation_hook( WP_2FA_FILE, '\WP2FA\Core\activate' );
105 \register_deactivation_hook( WP_2FA_FILE, '\WP2FA\Core\deactivate' );
106 // Register our uninstallation hook.
107 \register_uninstall_hook( WP_2FA_FILE, '\WP2FA\Core\uninstall' );
108
109 \add_filter( 'plugins_loaded', array( WP2FA::class, 'init' ) );
110 \add_action( 'plugins_loaded', array( WP2FA::class, 'add_wizard_actions' ), 10 );
111
112
113 // phpcs:disable
114 // phpcs:enable
115
116 if ( ! defined( File_Writer::SECRET_NAME ) ) {
117 define( File_Writer::SECRET_NAME, WP2FA::get_secret_key() );
118
119 define( 'WP2FA_SECRET_IS_IN_DB', true );
120 }
121
122 // phpcs:disable
123 /* @free:start */
124 // phpcs:enable
125 if ( ! function_exists( 'wp2fa_free_on_plugin_activation' ) ) {
126 /**
127 * Takes care of deactivation of the premium plugin when the free plugin is activated.
128 *
129 * Note: This code MUST NOT be present in the premium version an is removed automatically during the build process.
130 *
131 * @since 2.0.0
132 */
133 function wp2fa_free_on_plugin_activation() {
134 $premium_version_slug = 'wp-2fa-premium/wp-2fa.php';
135 if ( is_plugin_active( $premium_version_slug ) ) {
136 deactivate_plugins( $premium_version_slug, true );
137 }
138 check_ssl();
139 }
140
141 \register_activation_hook( __FILE__, 'wp2fa_free_on_plugin_activation' );
142 }
143 // phpcs:disable
144 /* @free:end */
145 // phpcs:enable
146
147 /*
148 * Clears the config cache from the DB
149 *
150 * @return void
151 *
152 * @since 2.2.0
153 */
154 \add_action(
155 'upgrader_process_complete',
156 function () {
157 delete_transient( 'wp_2fa_config_file_hash' );
158 },
159 10,
160 2
161 );
162
163 if ( ! function_exists( 'wp_2f_is_just_in_time_for_2fa_domain' ) ) {
164 /**
165 * Whether it is the just_in_time_error for wp-2fa domains.
166 *
167 * @since 2.9.0
168 *
169 * @param string $status Status of the error.
170 * @param string $function_name Function name.
171 * @param string $message Message.
172 *
173 * @return bool
174 */
175 function wp_2f_is_just_in_time_for_2fa_domain( $status, string $function_name, string $message ): bool {
176
177 return '_load_textdomain_just_in_time' === $function_name && strpos( $message, '<code>' . WP_2FA_TEXTDOMAIN ) !== false;
178 }
179 }
180
181 if ( ! function_exists( 'wp_2fa_trigger_error' ) ) {
182 /**
183 * Catches errors which come from the doing_it_wrong() function, WP core does not provide much information about what is really going on and where, this method adds some more information to the error log.
184 *
185 * @param bool $status - Whether to trigger the error for _doing_it_wrong() calls. Default true.
186 * @param string $function_name - The name of the function that triggered the error (this is the WP function which is not called right, not the real function that actually called it).
187 * @param string $errstr - The WP error string (message).
188 * @param string $version - Since which WP version given error was added.
189 * @param int $errno - The number of the error (type of the error - that probably never get set by WP and always falls to the default which is E_USER_NOTICE).
190 *
191 * @return bool
192 *
193 * @since 2.9.0
194 */
195 function wp_2fa_trigger_error( $status, string $function_name, $errstr, $version, $errno = E_USER_NOTICE ) {
196
197 if ( false === $status ) {
198 return $status;
199 }
200
201 if ( wp_2f_is_just_in_time_for_2fa_domain( '', $function_name, $errstr ) ) {
202 // This error code is not included in error_reporting, so let it fall.
203 // through to the standard PHP error handler.
204 return false;
205 }
206 }
207 }
208
209 if ( ! function_exists( 'wp_2fa_action_doing_it_wrong_run' ) ) {
210 /**
211 * Action for _doing_it_wrong() calls.
212 *
213 * @since 2.9.0
214 *
215 * @param string $function_name The function that was called.
216 * @param string $message A message explaining what has been done incorrectly.
217 * @param string $version The version of WordPress where the message was added.
218 *
219 * @return void
220 */
221 function wp_2fa_action_doing_it_wrong_run( $function_name, $message, $version ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
222
223 global $wp_filter;
224
225 $function_name = (string) $function_name;
226 $message = (string) $message;
227
228 if ( ! class_exists( '\QM_Collectors', false ) || ! wp_2f_is_just_in_time_for_2fa_domain( '', $function_name, $message ) ) {
229 return;
230 }
231
232 $qm_collector_doing_it_wrong = \QM_Collectors::get( 'doing_it_wrong' );
233 $current_priority = $wp_filter['doing_it_wrong_run']->current_priority();
234
235 if ( null === $qm_collector_doing_it_wrong || false === $current_priority ) {
236 return;
237 }
238
239 switch ( $current_priority ) {
240 case 0:
241 \remove_action( 'doing_it_wrong_run', array( $qm_collector_doing_it_wrong, 'action_doing_it_wrong_run' ) );
242 break;
243
244 case 20:
245 \add_action( 'doing_it_wrong_run', array( $qm_collector_doing_it_wrong, 'action_doing_it_wrong_run' ), 10, 3 );
246 break;
247
248 default:
249 break;
250 }
251 }
252 }
253
254 if ( ! function_exists( 'check_ssl' ) ) {
255 /**
256 * Checks if the required library is installed and cancels the process if not.
257 *
258 * @return void
259 *
260 * @since 2.2.0
261 */
262 function check_ssl() {
263 if ( ! \WP2FA\Authenticator\Open_SSL::is_ssl_available() ) {
264 $html = '<div class="updated notice is-dismissible">
265 <p>' . \esc_html__( 'This plugin requires OpenSSL. Contact your web host or website administrator so they can enable OpenSSL. Re-activate the plugin once the library has been enabled.', 'wp-2fa' )
266 . '</p>
267 </div>';
268
269 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
270
271 exit();
272 }
273 }
274 }
275
276 if ( \PHP_VERSION_ID < 80000 && ! \interface_exists( 'Stringable' ) ) {
277 // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
278 interface Stringable {
279 /**
280 * Mockup function for PHP versions lower than 8.
281 *
282 * @return string
283 */
284 public function __toString();
285 }
286 }
287
288 if ( ! function_exists( 'str_starts_with' ) ) {
289 /**
290 * PHP lower than 8 is missing that function but it required in the newer versions of our plugin.
291 *
292 * @param string $haystack - The string to search in.
293 * @param string $needle - The needle to search for.
294 *
295 * @return bool
296 *
297 * @since 2.6.4
298 */
299 function str_starts_with( $haystack, $needle ): bool {
300 if ( '' === $needle ) {
301 return true;
302 }
303
304 return 0 === strpos( $haystack, $needle );
305 }
306 }
307