-
Notifications
You must be signed in to change notification settings - Fork 11
/
wp-google-authenticator.php
445 lines (371 loc) · 11.6 KB
/
wp-google-authenticator.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
<?php
/**
* @package WP Google Authenticator
* @author Julien Liabeuf <[email protected]>
* @license GPL-2.0+
* @link https://julienliabeuf.com
* @copyright 2016 Julien Liabeuf
*
* @wordpress-plugin
* Plugin Name: WP Authenticator
* Plugin URI: https://wordpress.org/plugins/wp-google-authenticator/
* Description: WP Authenticator provides a safe way to add 2-factor authentication to your WordPress site using the Google 2FA system with the Google Authenticator app.
* Version: 1.1.1
* Author: Julien Liabeuf
* Author URI: https://julienliabeuf.com
* Text Domain: wpga
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'WP_Google_Authenticator' ) ):
/**
* Main WP Google Authenticator class
*
* This class is the one and only instance of the plugin. It is used
* to load the core and all its components.
*
* @since 1.2.0
*/
final class WP_Google_Authenticator {
/**
* @var WP_Google_Authenticator Holds the unique instance of WP Google Authenticator
* @since 1.2.0
*/
private static $instance;
/**
* Possible error message.
*
* @since 1.2.0
* @var null|WP_Error
*/
protected $error = null;
/**
* Minimum version of WordPress required ot run the plugin
*
* @since 1.2.0
* @var string
*/
public $wordpress_version_required = '3.8';
/**
* Required version of PHP.
*
* Follow WordPress latest requirements and require
* PHP version 5.2 at least.
*
* @since 1.2.0
* @var string
*/
public $php_version_required = '5.2';
/**
* Holds the instance of our authentication class
*
* @since 1.2.0
* @var WPGA_Authenticate
*/
public $authenticate;
/**
* Holds the settings class
*
* @since 1.2
* @var WPGA_Settings
*/
public $settings;
/**
* Holds the recovery key instance
*
* @since 1.2
* @var WPGA_Recovery_Key
*/
public $recovery;
/**
* Holds the access log class instance
*
* @since 1.2
* @var WPGA_Access_Log
*/
public $access_log;
/**
* Instantiate and return the unique WP Google Authenticator object
*
* @since 1.2.0
* @return object WP_Google_Authenticator Unique instance of WP Google Authenticator
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Google_Authenticator ) ) {
self::$instance = new WP_Google_Authenticator;
self::$instance->init();
}
return self::$instance;
}
/**
* Instantiate the plugin
*
* @since 1.2.0
* @return void
*/
private function init() {
// First of all we need the constants
self::$instance->setup_constants();
// Make sure the WordPress version is recent enough
if ( ! self::$instance->is_version_compatible() ) {
self::$instance->add_error( sprintf( __( 'WP Google Authenticator requires WordPress version %s or above. Please update WordPress to run this plugin.', 'wpga' ), self::$instance->wordpress_version_required ) );
}
// Make sure we have a version of PHP that's not too old
if ( ! self::$instance->is_php_version_enough() ) {
self::$instance->add_error( sprintf( __( 'WP Google Authenticator requires PHP version %s or above. Read more information about <a %s>how you can update</a>.', 'wpga' ), self::$instance->wordpress_version_required, 'a href="http://www.wpupdatephp.com/update/" target="_blank"' ) );
}
// If we have any error, don't load the plugin
if ( is_a( self::$instance->error, 'WP_Error' ) ) {
add_action( 'admin_notices', array( self::$instance, 'display_error' ), 10, 0 );
return;
}
self::$instance->setup_database_constants();
self::$instance->includes();
self::$instance->authenticate = new WPGA_Authenticate();
self::$instance->recovery = new WPGA_Recovery_Key();
self::$instance->access_log = new WPGA_Access_Log();
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
self::$instance->settings = new WPGA_Settings( 'network' );
}
add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ) );
// Check for network activation
add_action( 'admin_notices', array( self::$instance, 'multisite_check' ), 5 );
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 1.2.0
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpga' ), '1.2.0' );
}
/**
* Disable unserializing of the class
*
* @since 1.2.0
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpga' ), '1.2.0' );
}
/**
* Setup all plugin constants
*
* @since 1.2.0
* @return void
*/
private function setup_constants() {
define( 'WPGA_VERSION', '1.1.1' );
define( 'WPGA_DB_VERSION', '1' );
define( 'WPGA_NAME', 'WP Authenticator' );
define( 'WPGA_AUTHOR', 'Julien Liabeuf' );
define( 'WPGA_URI', 'https://julienliabeuf.com' );
define( 'WPGA_URL', plugin_dir_url( __FILE__ ) );
define( 'WPGA_PATH', plugin_dir_path( __FILE__ ) );
define( 'WPGA_ROOT', trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
define( 'WPGA_PREFIX', 'wpga' );
define( 'WPGA_BASENAME', plugin_basename( __FILE__ ) );
define( 'WPGA_LOG', false );
}
/**
* Setup the custom database table constants
*
* @since 2.0
* @return void
*/
private function setup_database_constants() {
global $wpdb;
define( 'wpga_recovery_keys_table_name', 'wpga_recovery_keys' );
define( 'wpga_apps_access_log_table_name', 'wpga_apps_access_log' );
define( 'wpga_recovery_keys_table', $wpdb->prefix . wpga_recovery_keys_table_name );
define( 'wpga_apps_access_log_table', $wpdb->prefix . wpga_apps_access_log_table_name );
}
/**
* Check if the core version is compatible with this plugin.
*
* @since 1.2.0
* @return boolean
*/
private function is_version_compatible() {
if ( empty( self::$instance->wordpress_version_required ) ) {
return true;
}
if ( version_compare( get_bloginfo( 'version' ), self::$instance->wordpress_version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Check if the version of PHP is compatible with this plugin.
*
* @since 1.2.0
* @return boolean
*/
private function is_php_version_enough() {
/**
* No version set, we assume everything is fine.
*/
if ( empty( self::$instance->php_version_required ) ) {
return true;
}
if ( version_compare( phpversion(), self::$instance->php_version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Check if the site is a multisite network and, if so, if the plugin is network activated or not
*
* If the plugin is not network activated, we display a warning message highlighting the security issue related to having it active on a per-site basis.
*
* @since 1.2
* @return void
*/
public function multisite_check() {
if ( true === is_multisite() && false === self::$instance->is_network_enabled() ) {
wpga_register_notice( 'wpga_not_network_activated', 'error', sprintf( __( '%2$s is only active on the current site of your network. This introduces a security risk. <strong>It is strongly advised that you network-activate the plugin for maximum security</strong>. <a href="%1$s" target="_blank">Read more about this</a>.', 'wpga' ), '#', WPGA_NAME ) );
}
}
/**
* Check if the plugin is network-enabled
*
* @since 1.2
* @return bool
*/
public function is_network_enabled() {
if ( false === is_multisite() ) {
return false;
}
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
return is_plugin_active_for_network( WPGA_BASENAME );
}
/**
* Add error.
*
* Add a new error to the WP_Error object
* and create the object if it doesn't exist yet.
*
* @since 1.2.0
*
* @param string $message Error message to add
*
* @return void
*/
private function add_error( $message ) {
if ( ! is_object( $this->error ) || ! is_a( $this->error, 'WP_Error' ) ) {
$this->error = new WP_Error();
}
$this->error->add( 'addon_error', $message );
}
/**
* Display error.
*
* Get all the error messages and display them
* in the admin notices.
*
* @since 1.2.0
* @return void
*/
public function display_error() {
if ( ! is_a( $this->error, 'WP_Error' ) ) {
return;
}
$message = self::$instance->error->get_error_messages(); ?>
<div class="error">
<p>
<?php
if ( count( $message ) > 1 ) {
echo '<ul>';
foreach ( $message as $msg ) {
echo "<li>$msg</li>";
}
echo '</li>';
} else {
echo $message[0];
}
?>
</p>
</div>
<?php
}
/**
* Include all files used sitewide
*
* @since 1.2.0
* @return void
*/
private function includes() {
if ( is_admin() ) {
// We don't need all this during Ajax processing
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
require( WPGA_PATH . 'includes/admin/functions-user-profile.php' );
require( WPGA_PATH . 'includes/admin/functions-secret.php' );
require( WPGA_PATH . 'includes/admin/functions-misc.php' );
require( WPGA_PATH . 'includes/admin/install.php' );
// Load all the options callbacks
foreach ( glob( WPGA_PATH . 'includes/admin/views/options/option-*.php' ) as $filename ) {
include $filename;
}
}
}
require( WPGA_PATH . 'includes/admin/class-settings.php' );
require( WPGA_PATH . 'includes/admin/functions-settings.php' );
require( WPGA_PATH . 'includes/functions-login.php' );
require( WPGA_PATH . 'includes/functions-totp.php' );
require( WPGA_PATH . 'includes/functions-users.php' );
require( WPGA_PATH . 'includes/functions-recovery.php' );
require( WPGA_PATH . 'includes/functions-deprecated.php' );
require( WPGA_PATH . 'includes/class-recovery-key.php' );
require( WPGA_PATH . 'includes/class-authenticate.php' );
require( WPGA_PATH . 'includes/class-user.php' );
require( WPGA_PATH . 'includes/class-access-log.php' );
require( WPGA_PATH . 'includes/functions-apps-passwords.php' );
require( WPGA_PATH . 'includes/scripts-styles.php' );
}
/**
* Load the plugin text domain for translation.
*
* @return boolean True if the language file was loaded, false otherwise
* @since 1.2.0
*/
public function load_plugin_textdomain() {
$lang_dir = WPGA_ROOT . 'languages/';
$land_path = WPGA_PATH . 'languages/';
$locale = apply_filters( 'plugin_locale', get_locale(), 'wpga' );
$mofile = "wpga-$locale.mo";
if ( file_exists( $land_path . $mofile ) ) {
$language = load_textdomain( 'wpga', $land_path . $mofile );
} else {
$language = load_plugin_textdomain( 'wpga', false, $lang_dir );
}
return $language;
}
}
endif;
/**
* The main function responsible for returning the unique WP Google Authenticator instance
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* @since 1.2.0
* @return WP_Google_Authenticator
*/
function WPGA() {
return WP_Google_Authenticator::instance();
}
// Get WP Google Authenticator Running
WPGA();