-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
504 lines (448 loc) · 16.3 KB
/
functions.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Get the current URL
*
* @return string the current URL
*/
function otpa_get_current_url() {
return home_url( $_SERVER['REQUEST_URI'] );
}
/**
* Get the currently active Authentication Gateway class name
*
* @return string the currently active Authentication Gateway class name
*/
function otpa_get_active_gateway_class_name() {
return Otpa_Abstract_Gateway::get_gateway_class_name( Otpa_Settings::get_current_gateway_id() );
}
/**
* Generate a One-Time Password code
*
* @param int $length The OTP Code length
* @param string $chars The characters to randomly choose from when generating the code
* @return string the generated OTP Code
*/
function otpa_generate_otp_code( $length, $chars ) {
$code = '';
for ( $i = 0; $i < $length; $i++ ) {
$code .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 );
}
return $code;
}
/**
* Mask an email address with "*"
*
* @param string $email The email address to mask
* @param int $username_char_front The number of leading characters to leave unmasked for the username part of the email address
* @param int $username_char_back The number of trailing characters to leave unmasked for the username part of the email address
* @param int $domain_char_front The number of leading characters to leave unmasked for the domain part of the email address
* @param int $domain_char_back The number of trailing characters to leave unmasked for the domain part of the email address
* @return string the masked email
*/
function otpa_mask_email(
$email,
$username_char_front = 1,
$username_char_back = 1,
$domain_char_front = 1,
$domain_char_back = 1
) {
$mail_parts = explode( '@', $email );
$mail_parts[0] = otpa_mask_string( $mail_parts[0], $username_char_front, $username_char_back );
$domain_parts = explode( '.', $mail_parts[1] );
$domain_tld = end( $domain_parts );
array_pop( $domain_parts );
$domain_name = implode( '.', $domain_parts );
$mail_parts[1] = otpa_mask_string( $domain_name, $domain_char_front, $domain_char_back ) . '.' . $domain_tld;
return implode( '@', $mail_parts );
}
/**
* Mask a phone number with "*"
*
* @param string $phone The phone number to mask
* @param int $char_front The number of leading characters to leave unmasked
* @param int $char_back The number of trailing characters to leave unmasked
* @param string $prefix The phone prefix (left unmasked)
* @return string the masked email
*/
function otpa_mask_phone( $phone, $char_front = 3, $char_back = 3, $prefix = '' ) {
return $prefix . otpa_mask_string( str_replace( $prefix, '', $phone ), $char_front, $char_back );
}
/**
* Mask a string with "*"
*
* @param string $string The string to mask
* @param int $char_front The number of leading characters to leave unmasked
* @param int $char_back The number of trailing characters to leave unmasked
* @return string the masked string
*/
function otpa_mask_string( $string, $char_front = 1, $char_back = 1 ) {
$len = strlen( $string );
if ( $len <= $char_front || $len <= $char_front || $len === $char_front + $char_back ) {
$char_front = 0;
$char_back = 0;
}
$masked_string = substr( $string, 0, $char_front );
$masked_string .= str_repeat( '*', $len - $char_front - $char_back );
$masked_string .= substr( $string, $len - $char_back, $char_back );
return $masked_string;
}
/**
* Adjust a hex color brightness
*
* @param string $hex Hex color e.g. #111111.
* @param int $steps Factor by which to brighten/darken ranging from -255 (darken) to 255 (brighten).
* @return string brightened/darkened hex color
*/
function otpa_adjust_color_brightness( $hex, $steps ) {
$steps = max( -255, min( 255, $steps ) );
$hex = str_replace( '#', '', $hex );
if ( 3 === strlen( $hex ) ) {
$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
}
$r = hexdec( substr( $hex, 0, 2 ) );
$g = hexdec( substr( $hex, 2, 2 ) );
$b = hexdec( substr( $hex, 4, 2 ) );
$r = max( 0, min( 255, $r + $steps ) );
$g = max( 0, min( 255, $g + $steps ) );
$b = max( 0, min( 255, $b + $steps ) );
$r_hex = str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT );
$g_hex = str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT );
$b_hex = str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT );
return '#' . $r_hex . $g_hex . $b_hex;
}
/**
* Minify a CSS string (imperfect)
*
* @param string $css The CSS to minify
* @see https://datayze.com/howto/minify-css-with-php
* @return string the minified CSS
*/
function otpa_simple_minify_css( $css ) {
$css = preg_replace( '/\/\*((?!\*\/).)*\*\//', '', $css ); // negative look ahead
$css = preg_replace( '/\s{2,}/', ' ', $css );
$css = preg_replace( '/\s*([:;{}])\s*/', '$1', $css );
$css = preg_replace( '/;}/', '}', $css );
return $css;
}
/**
* Convert a timestamp into a human-readable duration in hours, minutes, seconds.
* Hours, minutes and seconds are included in the final string only if not 0.
*
* @param int $timestamp The timestamp to convert
* @return string the human-readable duration
*/
function otpa_human_timing( $timestamp ) {
$now = time();
$diff = $timestamp - $now;
$hours = floor( $diff / 3600 );
$minutes = floor( ( $diff / 60 ) % 60 );
$seconds = $diff % 60;
if ( 0 < $hours && 0 < $minutes && 0 < $seconds ) {
$timing = sprintf(
sprintf(
// translators: %1$s hours, %2$s minutes and %2$s seconds
__( '%1$s, %2$s and %3$s', 'otpa' ),
// translators: %s number of hours
_n( '%s hour', '%s hours', $hours, 'otpa' ),
// translators: %s number of minutes
_n( '%s minute', '%s minutes', $minutes, 'otpa' ),
// translators: %s number of seconds
_n( '%s second', '%s seconds', $seconds, 'otpa' )
),
$hours,
$minutes,
$seconds
);
} elseif ( 0 < $hours && 0 < $minutes ) {
$timing = sprintf(
sprintf(
// translators: %1$s hours and %2$s minutes
__( '%1$s and %2$s', 'otpa' ),
// translators: %s number of hours
_n( '%s hour', '%s hours', $hours, 'otpa' ),
// translators: %s number of minutes
_n( '%s minute', '%s minutes', $minutes, 'otpa' )
),
$hours,
$minutes
);
} elseif ( 0 < $minutes && 0 < $seconds ) {
$timing = sprintf(
sprintf(
// translators: %1$s minutes and %2$s seconds
__( '%1$s and %2$s', 'otpa' ),
// translators: %s number of minutes
_n( '%s minute', '%s minutes', $minutes, 'otpa' ),
// translators: %s number of seconds
_n( '%s second', '%s seconds', $seconds, 'otpa' )
),
$minutes,
$seconds
);
} elseif ( 0 < $hours && 0 < $seconds ) {
$timing = sprintf(
sprintf(
// translators: %1$s hours and %2$s seconds
__( '%1$s and %2$s', 'otpa' ),
// translators: %s number of hours
_n( '%s hour', '%s hours', $hours, 'otpa' ),
// translators: %s number of seconds
_n( '%s second', '%s seconds', $seconds, 'otpa' )
),
$hours,
$seconds
);
} elseif ( 0 < $hours ) {
// translators: %s number of hours
$timing = sprintf( _n( '%s hour', '%s hours', $hours, 'otpa' ), $hours );
} elseif ( 0 < $minutes ) {
// translators: %s number of minutes
$timing = sprintf( _n( '%s minute', '%s minutes', $minutes, 'otpa' ), $minutes );
} elseif ( 0 < $seconds ) {
// translators: %s number of seconds
$timing = sprintf( _n( '%s second', '%s seconds', $seconds, 'otpa' ), $seconds );
}
return $timing;
}
/**
* Log an expression with optional information in log file
*
* @param mixed $expression The expression to log
* @param string $extended Additional information - default empty
*/
function otpa_log( $expression, $extend = '' ) {
Otpa_Logger::log( $expression, $extend );
}
/**
* Log an expression in database
*
* @param mixed $expression The expression to log
* @param string $log_level The log severity level - one of 'info', 'success', 'warning', 'alert' - default 'info'
* @param string $force Whether to force the log when logging is disabled - default false
*/
function otpa_db_log( $expression, $log_level = 'info', $force = false ) {
Otpa_Logger::log( $expression, $log_level, 'db_log', $force );
}
/**
* Log an expression in database
*
* @see otpa_db_log
*/
function otpa_dblog( $expression, $log_level = 'info', $force = false ) {
otpa_db_log( $expression, $log_level, $force );
}
/**
* Get the account validation information
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
* @return array {
* An array of account validation information
*
* @type bool $validated Whether the user account is validated
* @type int $expiry Timestamp when the account validation expires
* @type bool $need_validation Whether the account needs validation a next login
* @type bool $force_validation Whether account validation is currently forced even if the user is currently logged in
* @type bool $role_excluded Whether the user of the account has a role bypassing validation
* }
*/
function otpa_get_user_account_validation_info( $user_id = false ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_account_validation = new Otpa_Account_Validation( $otpa );
$expiry = $otpa_account_validation->get_user_validation_expiry( $user );
switch ( $expiry ) {
case Otpa_Account_Validation::NO_VALIDATION_EXPIRY:
$expiry = 'identifier_changed';
break;
case Otpa_Account_Validation::VALIDATION_LOGOUT_EXPIRY:
$expiry = 'next_login';
break;
default:
$expiry = ( time() > $expiry ) ? 'next_login' : $expiry;
break;
}
return array(
'validated' => $otpa_account_validation->is_user_validated( $user, true ),
'expiry' => $expiry,
'need_validation' => $otpa_account_validation->need_account_validation( $user ),
'force_validation' => (bool) get_user_meta( $user->ID, 'otpa_force_account_validation', true ),
'role_excluded' => $otpa_account_validation->is_user_validation_excluded( $user->ID ),
);
}
/**
* Validate a user account
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
*/
function otpa_do_account_validation( $user_id = false ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_account_validation = new Otpa_Account_Validation( $otpa );
$otpa_account_validation->validate_account( $user->ID );
}
/**
* Invalidate a user account
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
*/
function otpa_reset_account_validation( $user_id = false ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_account_validation = new Otpa_Account_Validation( $otpa );
$otpa_account_validation->set_need_account_validation( $user->user_login, $user, true );
}
/**
* Check whether the user of the account has a role bypassing validation
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
* @return bool whether the user of the account has a role bypassing validation
*/
function otpa_is_user_account_validation_excluded( $user_id = false ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_account_validation = new Otpa_Account_Validation( $otpa );
return $otpa_account_validation->is_user_validation_excluded( $user_id );
}
/**
* Get the 2fa information
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
* @return array {
* An array of 2fa information
*
* @type bool $active Whether 2fa is active for the user account
* }
*/
function otpa_get_user_2fa_info( $user_id = false ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_2fa = new Otpa_2FA( $otpa );
return array(
'active' => $otpa_2fa->is_user_2fa_active( $user, true ),
);
}
/**
* Set the 2fa status of a user account
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
* @param bool $active Whether 2fa should be active for the account
*/
function otpa_set_user_2fa_active( $user_id = false, $active = true ) {
$user = ( $user_id ) ? get_user_by( 'ID', $user_id ) : wp_get_current_user();
$otpa_settings = new Otpa_Settings();
$otpa = new Otpa( $otpa_settings );
$otpa_2fa = new Otpa_2FA( $otpa );
$otpa_2fa->set_user_2fa_active( $user, $active );
}
/**
* Check whether the OTP identifiers are synced with an existing user meta
*
* @return bool whether the OTP identifiers are synced with an existing user meta
*/
function otpa_is_identifier_synced() {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return ! empty( $gateway->get_option( 'sync_metakey' ) );
}
/**
* Check whether the OTP gateway is the WordPress User email
*
* @return bool whether the OTP gateway is the WordPress User email
*/
function otpa_is_email_gateway() {
return 'Otpa_WP_Email_Gateway' !== otpa_get_active_gateway_class_name();
}
/**
* Get a user's OTP identifier
*
* @param int $user_id If falsey, the current user's ID ; the ID of a user otherwise - default false
* @return string the user's identifier
*/
function otpa_get_user_identifier( $user_id = false ) {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->get_user_identifier( $user_id );
}
/**
* Get a user by specified OTP identifier
*
* @param string $identifier The identifier to get a user by
* @return bool|WP_User false if no user found, the user oject otherwise
*/
function otpa_get_user_by_identifier( $identifier ) {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->get_user_by_identifier( $identifier );
}
/**
* Check whether the specified OTP identifier is a valid identifier
*
* @param string the specified OTP identifier
* @return bool whether the specified OTP identifier is a valid identifier
*/
function otpa_is_valid_identifier( $identifier ) {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->is_valid_identifier( $identifier );
}
/**
* Sanitize specified OTP identifier
*
* @param string the specified OTP identifier
* @return string the sanitized OTP identifier
*/
function otpa_sanitize_user_identifier( $identifier ) {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->sanitize_user_identifier( $identifier );
}
/**
* Set a user's OTP identifier
*
* @param string the OTP identifier
* @param int the user ID
* @return bool|string false on failure, the new identifier on success
*/
function otpa_set_user_identifier( $identifier, $user_id ) {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->set_user_identifier( $identifier, $user_id );
}
/**
* Check whether the active Authentication Gateway allows to change OTP identifiers
*
* @return bool whether the active Authentication Gateway allows to change OTP identifiers
*/
function otpa_gateway_allow_edit_identifier() {
$gateway_class = otpa_get_active_gateway_class_name();
$gateway = new $gateway_class( false );
return $gateway->allow_edit_identifier();
}
/**
* Get the plugin's settings
*
* @return array {
* An array of settings
*
* @type array $general The general settings
* @type array $gateway The gateway settings
* @type array $style The OTP Form style settings
* }
*/
function otpa_get_settings() {
return array(
'general' => Otpa_Settings::get_options(),
'gateway' => Otpa_Abstract_Gateway::get_gateway_options( Otpa_Settings::get_current_gateway_id() ),
'style' => Otpa_Style_Settings::get_options(),
);
}