PluginProbe
Two Factor Authentication / 1.14.14
Two Factor Authentication v1.14.14
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 / simba-tfa / includes / tfa_frontend.php

tfa_frontend.php in Two Factor Authentication 1.14.14, at simba-tfa/includes/tfa_frontend.php

205 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('Access denied.');
3
4 class Simba_TFA_Frontend {
5
6 private $mother;
7
8 /**
9 * Class constructor
10 *
11 * @param Object $mother
12 */
13 public function __construct($mother) {
14
15 $this->mother = $mother;
16 add_action('wp_ajax_tfa_frontend', array($this, 'ajax'));
17 add_shortcode('twofactor_user_settings', array($this, 'tfa_user_settings_front'));
18 }
19
20 /**
21 * Runs upon the WP action wp_ajax_tfa_frontend
22 *
23 * @uses die()
24 */
25 public function ajax() {
26 $totp_controller = $this->mother->get_controller('totp');
27 global $current_user;
28
29 $return_array = array();
30
31 if (empty($_POST) || empty($_POST['subaction']) || !isset($_POST['nonce']) || !is_user_logged_in() || !wp_verify_nonce($_POST['nonce'], 'tfa_frontend_nonce')) die('Security check');
32
33 if ('savesettings' == $_POST['subaction']) {
34 if (empty($_POST['settings']) || !is_string($_POST['settings'])) die;
35
36 parse_str(stripslashes($_POST['settings']), $posted_settings);
37
38 if (isset($posted_settings['tfa_algorithm_type'])) {
39 $old_algorithm = $totp_controller->get_user_otp_algorithm($current_user->ID);
40
41 if ($old_algorithm != $posted_settings['tfa_algorithm_type'])
42 $totp_controller->changeUserAlgorithmTo($current_user->ID, $posted_settings['tfa_algorithm_type']);
43
44 //Re-fetch the algorithm type, url and private string
45 $variables = $this->tfa_fetch_assort_vars();
46
47 $return_array['qr'] = $totp_controller->tfa_qr_code_url($variables['algorithm_type'], $variables['url'], $variables['tfa_priv_key']);
48 $return_array['al_type_disp'] = $this->tfa_algorithm_info($variables['algorithm_type']);
49 }
50
51 if (isset($posted_settings['tfa_enable_tfa'])) {
52
53 $allow_enable_or_disable = false;
54
55 if (empty($posted_settings['require_current']) || !$posted_settings['tfa_enable_tfa']) {
56 $allow_enable_or_disable = true;
57 } else {
58
59 if (!isset($posted_settings['tfa_enable_current']) || '' == $posted_settings['tfa_enable_current']) {
60 $return_array['message'] = __('To enable TFA, you must enter the current code.', 'two-factor-authentication');
61 $return_array['error'] = 'code_absent';
62 } else {
63 // Third parameter: don't allow emergency codes
64 if ($totp_controller->check_code_for_user($current_user->ID, $posted_settings['tfa_enable_current'], false)) {
65 $allow_enable_or_disable = true;
66 } else {
67 $return_array['error'] = 'code_wrong';
68 $return_array['message'] = apply_filters('simba_tfa_message_code_incorrect', __('The TFA code you entered was incorrect.', 'two-factor-authentication'));
69 }
70 }
71
72 }
73
74 if ($allow_enable_or_disable) $this->mother->change_tfa_enabled_status($current_user->ID, $posted_settings['tfa_enable_tfa']);
75 }
76
77 $return_array['result'] = 'saved';
78
79 echo json_encode($return_array);
80 }
81
82 die;
83 }
84
85 /**
86 * Make the algorithm information string easier to update
87 *
88 * @param String $algorithm_type - totp|hotp
89 */
90 public function tfa_algorithm_info($algorithm_type) {
91 $al_type_disp = strtoupper($algorithm_type);
92 $al_type_desc = ($algorithm_type == 'totp' ? __('a time based', 'two-factor-authentication') : __('an event based', 'two-factor-authentication'));
93
94 return array('disp' => $al_type_disp, 'desc' => $al_type_desc);
95 }
96
97 /**
98 * Make the assorted required variables more accessible for ajax
99 *
100 * Returns: Site URL, private key, emergency codes, algorithm type
101 *
102 * @return Array
103 */
104 public function tfa_fetch_assort_vars() {
105 global $current_user;
106 $totp_controller = $this->mother->get_controller('totp');
107
108 $url = preg_replace('/^https?:\/\//i', '', site_url());
109
110 $tfa_priv_key_64 = get_user_meta($current_user->ID, 'tfa_priv_key_64', true);
111
112 if (!$tfa_priv_key_64) $tfa_priv_key_64 = $totp_controller->addPrivateKey($current_user->ID);
113
114 $tfa_priv_key = trim($totp_controller->getPrivateKeyPlain($tfa_priv_key_64, $current_user->ID));
115
116 $algorithm_type = $totp_controller->get_user_otp_algorithm($current_user->ID);
117
118 return apply_filters('simba_tfa_fetch_assort_vars', array(
119 'url' => $url,
120 'tfa_priv_key_64' => $tfa_priv_key_64,
121 'tfa_priv_key' => $tfa_priv_key,
122 'emergency_str' => '<em>'.__('No emergency codes left. Sorry.', 'two-factor-authentication').'</em>',
123 'algorithm_type' => $algorithm_type
124 ), $totp_controller, $current_user);
125 }
126
127 /**
128 * Paints out the 'save settings' button
129 */
130 public function save_settings_button() {
131 echo '<button style="margin-left: 4px;margin-bottom: 10px" class="simbatfa_settings_save button button-primary">'.__('Save Settings', 'two-factor-authentication').'</button>';
132 }
133
134 /**
135 * Paint output for the TFA on/off radio
136 *
137 * @param String $style - valid values are 'show_current' and 'require_current'
138 */
139 public function settings_enable_or_disable_output($style = 'show_current') {
140 $this->save_settings_javascript_output();
141 global $current_user;
142 ?>
143 <div class="simbatfa_frontend_settings_box tfa_settings_form">
144 <p><?php $this->mother->paint_enable_tfa_radios($current_user->ID, true, $style); ?></p>
145 <button style="margin-left: 4px; margin-bottom: 10px;" class="button button-primary simbatfa_settings_save"><?php _e('Save Settings', 'two-factor-authentication'); ?></button>
146 </div>
147 <?php
148 }
149
150 /**
151 * Enqueue scripts
152 */
153 public function save_settings_javascript_output() {
154
155 static $is_already_added = false;
156 if ($is_already_added) return;
157 $is_already_added = true;
158
159 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
160 wp_register_script('jquery-blockui', $this->mother->includes_url().'/jquery.blockUI' . $suffix . '.js', array('jquery'), '2.60');
161
162 $script_ver = (defined('WP_DEBUG') && WP_DEBUG) ? time() : filemtime($this->mother->includes_dir().'/frontend-settings.js');
163
164 wp_enqueue_script('simba-tfa-frontend-settings', $this->mother->includes_url().'/frontend-settings.js', array('jquery-blockui'), $script_ver);
165
166 $ajax_url = admin_url('admin-ajax.php');
167 // 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.
168 if (!is_admin() && substr(strtolower($ajax_url), 0, 6) == 'https:' && !is_ssl()) {
169 $also_try = 'http:'.substr($ajax_url, 6);
170 } else {
171 $also_try = '';
172 }
173
174 $localize = array(
175 'ask' => __('You have unsaved settings.', 'two-factor-authentication'),
176 'saving' => __('Saving...', 'two-factor-authentication'),
177 'ajax_url' => $ajax_url,
178 'also_try' => $also_try,
179 'nonce' => wp_create_nonce('tfa_frontend_nonce'),
180 'response' => __('Response:', 'two-factor-authentication'),
181 );
182
183 wp_localize_script('simba-tfa-frontend-settings', 'simba_tfa_frontend', $localize);
184
185 }
186
187 /**
188 * Shortcode function for twofactor_user_settings
189 *
190 * @param Array $atts
191 * @param Null|String $content
192 *
193 * @return String
194 */
195 public function tfa_user_settings_front($atts, $content = null) {
196
197 if (!is_user_logged_in()) return '';
198
199 global $current_user;
200
201 return $this->mother->include_template('shortcode-tfa-user-settings.php', array('is_activated_for_user' => $current_user->ID, 'tfa_frontend' => $this), true);
202
203 }
204 }
205