tfa = $tfa;
$this->otp_helper = new HOTP();
add_action('plugins_loaded', array($this, 'plugins_loaded'));
add_action('admin_init', array($this, 'admin_init'));
if (!is_admin()) {
add_action('init', array($this, 'check_possible_reset'));
}
// Potentially show off-sync message for hotp
add_action('admin_notices', array($this, 'tfa_show_hotp_off_sync_message'));
}
/**
* Return whether or not this class detected and saved new settings
*
* @return Boolean
*/
public function were_settings_saved() {
return $this->settings_saved;
}
/**
* Runs upon the WP action admin_init
*/
public function admin_init() {
$this->check_possible_reset();
global $current_user;
if (!empty($_REQUEST['_tfa_activate_nonce']) && !empty($_POST['tfa_enable_tfa']) && wp_verify_nonce($_REQUEST['_tfa_activate_nonce'], 'tfa_activate') && !empty($_GET['settings-updated'])) {
$this->tfa->change_tfa_enabled_status($current_user->ID, $_POST['tfa_enable_tfa']);
$this->settings_saved = true;
}
if (!empty($_REQUEST['_tfa_algorithm_nonce']) && !empty($_POST['tfa_algorithm_type']) && !empty($_GET['settings-updated']) && wp_verify_nonce($_REQUEST['_tfa_algorithm_nonce'], 'tfa_algorithm')) {
$old_algorithm = $this->get_user_otp_algorithm($current_user->ID);
if ($old_algorithm != $_POST['tfa_algorithm_type']) {
$this->changeUserAlgorithmTo($current_user->ID, $_POST['tfa_algorithm_type']);
}
$this->settings_saved = true;
}
if (!empty($_GET['warning_button_clicked']) && !empty($_REQUEST['resyncnonce']) && wp_verify_nonce($_REQUEST['resyncnonce'], 'tfaresync')) {
delete_user_meta($current_user->ID, 'tfa_hotp_off_sync');
}
}
/**
* Enqueue adding of JavaScript for footer
*/
public function add_footer() {
static $added_footer = false;
if ($added_footer) return;
$added_footer = true;
$qr_script_file = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? 'jquery-qrcode.js' : 'jquery-qrcode.min.js';
$qr_script_ver = (defined('WP_DEBUG') && WP_DEBUG) ? time() : filemtime($this->tfa->includes_dir()."/jquery-qrcode/$qr_script_file");
wp_register_script('jquery-qrcode', $this->tfa->includes_url()."/jquery-qrcode/$qr_script_file", array('jquery'), $qr_script_ver);
$script_ver = (defined('WP_DEBUG') && WP_DEBUG) ? time() : filemtime($this->tfa->includes_dir()."/totp.js");
// Adds the necessary JavaScript for rendering and updating QR codes, and handling trusted devices removal in the admin area
wp_enqueue_script('simba-tfa-totp', $this->tfa->includes_url()."/totp.js", array('jquery-qrcode'), $script_ver);
wp_localize_script('simba-tfa-totp', 'simbatfa_totp', $this->translation_strings());
}
/**
* Get textual strings used from JavaScript
*
* @return Array
*/
private function translation_strings() {
// It's possible that FORCE_ADMIN_SSL will make that SSL, whilst the user is on the front-end having logged in over non-SSL - and as a result, their login cookies won't get sent, and they're not registered as logged in.
$ajax_url = admin_url('admin-ajax.php');
$also_try = '';
if (!is_admin() && substr(strtolower($ajax_url), 0, 6) == 'https:' && !is_ssl()) {
$also_try = 'http:'.substr($ajax_url, 6);
}
return apply_filters('simba_tfa_totp_translation_strings', array(
'ajax_url' => $ajax_url,
'updating' => __('Updating...', 'all-in-one-wp-security-and-firewall'),
'tfa_shared_nonce' => wp_create_nonce('tfa_shared_nonce'),
'also_try' => $also_try,
'response' => __('Response:', 'all-in-one-wp-security-and-firewall'),
));
}
/**
* Return a link to refresh the current OTP code
*
* @return String
*/
public function refresh_current_otp_link() {
return ''.__('(update)', 'all-in-one-wp-security-and-firewall').'';
}
/**
* Echo the radio buttons for changing between TOTP/HOTP
*
* TODO: Hide this choice on new installs (TOTP only)
*
* @param Integer $user_id
*/
protected function print_algorithm_choice_radios($user_id) {
if (!$user_id) return;
$types = array(
'totp' => __('TOTP (time based - most common algorithm; used by Google Authenticator)', 'all-in-one-wp-security-and-firewall'),
'hotp' => __('HOTP (event based)', 'all-in-one-wp-security-and-firewall')
);
$setting = $this->get_user_otp_algorithm($user_id);
foreach ($types as $id => $name) {
print '
\n";
}
}
/**
* Print out the advanced settings box - choice of algorithm
*
* @param Boolean|Callable $submit_button_callback - if not a callback, then