PluginProbe
Two Factor Authentication / 1.14.23
Two Factor Authentication v1.14.23
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.23, at simba-tfa/includes/tfa_frontend.php

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