PluginProbe
Loginizer / 2.0.0
Loginizer v2.0.0
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / main / settings / sso.php

sso.php in Loginizer 2.0.0, at main/settings/sso.php

317 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')){
4 die('HACKING ATTEMPT!');
5 }
6
7 function loginizer_sso(){
8 global $loginizer, $error;
9
10 if(!current_user_can('manage_options')){
11 wp_die('Sorry, but you do not have permissions to change settings.');
12 }
13
14 if(empty($_POST['lz_generate_sso']) && empty($_POST['lz_delete_sso'])){
15 loginizer_sso_t();
16 return;
17 }
18
19 if(!defined('LOGINIZER_PREMIUM')){
20 $error[] = __('SSO is a Pro feature so it can not be used with the free version.', 'loginizer');
21 loginizer_sso_t();
22 return;
23 }
24
25 // Checking for form nonce
26 if(!wp_verify_nonce($_POST['security'], 'loginizer_nonce')){
27 $error[] = __('Security Check Failed!', 'loginizer');
28 loginizer_sso_t();
29 return;
30 }
31
32 if(!empty($_POST['lz_delete_sso'])){
33 loginizer_delete_sso();
34 return;
35 }
36
37 if(empty($_POST['sso_user'])){
38 $error[] = __('Please select a user for whom you want to generate the link', 'loginizer');
39 loginizer_sso_t();
40 return;
41 }
42
43 $sso_ttl = 600;
44 if(!empty($_POST['sso_ttl']) && is_numeric($_POST['sso_ttl'])){
45 $sso_ttl = (int) sanitize_text_field($_POST['sso_ttl']);
46 }
47
48 $sso_attempts = 1;
49 if(!empty($_POST['sso_attempts']) && is_numeric($_POST['sso_attempts'])){
50 $sso_attempts = (int) sanitize_text_field($_POST['sso_attempts']);
51
52 // The attempts need to be 15 or less
53 if($sso_attempts > 15 || $sso_attempts < 1){
54 $sso_attempts = 1;
55 }
56 }
57
58 $username = sanitize_text_field($_POST['sso_user']);
59 $user = get_user_by('login', $username);
60
61 if(empty($user) || empty($user->ID)){
62 $error[] = __('The given user was not found !', 'loginizer');
63 loginizer_sso_t();
64 return;
65 }
66
67 $loginizer['sso_link'] = loginizer_create_sso($user->ID, $sso_ttl, $sso_attempts);
68
69 loginizer_sso_t();
70 }
71
72 function loginizer_delete_sso(){
73 global $error;
74
75 if(empty($_POST['lz_checksso_link'])){
76 $error[] = __('Please select SSO Links to delete!', 'loginizer');
77 loginizer_sso_t();
78 return;
79 }
80
81 $sso_ids = map_deep($_POST['lz_checksso_link'], 'sanitize_text_field');
82
83 if(empty($_POST['lz_checksso_link'])){
84 $error[] = __('SSO IDs were malformed', 'loginizer');
85 loginizer_sso_t();
86 return;
87 }
88
89 $sso_links = get_option('loginizer_sso_links', []);
90 $update_sso_links = false;
91
92 foreach($sso_ids as $sso_id){
93 delete_user_meta($sso_id, 'loginizer_sso_' . $sso_id);
94 delete_user_meta($sso_id, 'loginizer_sso_' . $sso_id . '_expires');
95 delete_user_meta($sso_id, 'loginizer_sso_' . $sso_id . '_attempts');
96
97 if(!empty($sso_links)){
98 unset($sso_links[$sso_id]);
99 $update_sso_links = true;
100 }
101 }
102
103 if(!empty($update_sso_links)){
104 update_option('loginizer_sso_links', $sso_links);
105 }
106
107 loginizer_sso_t();
108 }
109
110
111 function loginizer_sso_t(){
112 global $loginizer, $error;
113
114 loginizer_page_header('SSO');
115 loginizer_feature_available('Single Sign-On');
116
117 lz_report_error($error);
118 ?>
119 <style>
120 .loginizer-sso-link{
121 padding:1rem 1rem;
122 color:#052c65;
123 background-color:#cfe2ff;
124 border:1px solid #9ec5fe;
125 border-radius:0.375rem;
126 }
127
128 .loginizer-sso-copy{
129 margin-right:10px;
130 cursor:pointer;
131 font-weight:500;
132 }
133 </style>
134
135 <script>
136 jQuery(document).ready(function(){
137
138 jQuery('.loginizer-sso-copy').on('click', function(){
139 navigator.clipboard.writeText(jQuery(this).parent().text());
140 jQuery(this).removeClass('dashicons');
141 jQuery(this).removeClass('dashicons-admin-page');
142 jQuery(this).text('Copied');
143
144 setTimeout(() =>{
145 jQuery(this).text('');
146 jQuery(this).addClass('dashicons');
147 jQuery(this).addClass('dashicons-admin-page');
148 }, 1000);
149
150 });
151
152 jQuery('#lz_check_all_sso_link').on('change', function(){
153 if(jQuery(this).is(':checked')){
154 jQuery('input[name="lz_checksso_link[]"]').prop('checked', true);
155 return;
156 }
157
158 jQuery('input[name="lz_checksso_link[]"]').prop('checked', false);
159 });
160
161 });
162 </script>
163
164
165 <div id="" class="postbox" loginizer-premium-only="1">
166
167 <div class="postbox-header">
168 <h2 class="hndle ui-sortable-handle">
169 <span><?php esc_html_e('Generate SSO', 'loginizer');?></span>
170 </h2>
171 </div>
172
173 <div class="inside">
174 <?php
175 $user_list = get_users();
176
177 $sso_links = get_option('loginizer_sso_links', []);
178 ?>
179 <form action="" method="post" enctype="multipart/form-data">
180 <?php if(!empty($loginizer['sso_link'])){
181 echo '<div class="loginizer-sso-link"><span class="dashicons dashicons-admin-page loginizer-sso-copy"></span>'.esc_url($loginizer['sso_link']).'</div>';
182 }
183
184 echo wp_nonce_field('loginizer_nonce', 'security');
185 ?>
186
187 <table class="form-table">
188 <tr>
189 <td scope="row" valign="top" colspan="2">
190 <i><?php esc_html_e('You can generate SSO link or send it to an email, to give them access to your WordPress admin', 'loginizer'); ?></i>
191 </td>
192 </tr>
193 <tr>
194 <td scope="row" valign="top" style="width:300px !important">
195 <label for="lz-sso-email"><?php esc_html_e('Email', 'loginizer'); ?></label><br>
196 <span class="exp"><?php esc_html_e('Email of the person you want to send the SSO to', 'loginizer'); ?></span>
197 </td>
198 <td>
199 <input id="lz-sso-email" type="email" name="sso_email" placeholder="name@email.com"/>
200 <p class="description"><?php esc_html_e('You can leave it empty if you just want to create a SSO link', 'loginizer'); ?></p>
201 </td>
202 </tr>
203 <tr>
204 <td scope="row" valign="top" style="width:300px !important">
205 <label for="lz-sso-user"><?php esc_html_e('User', 'loginizer'); ?></label><br>
206 <span class="exp"><?php esc_html_e('User for who\'s account you want to generate the SSO', 'loginizer'); ?></span>
207 </td>
208 <td>
209 <input type="text" id="lz-sso-user" name="sso_user" placeholder="Username">
210 </td>
211 </tr>
212 <tr>
213 <td scope="row" valign="top">
214 <label for="lz-sso-ttl"><?php esc_html_e('Time to Live', 'loginizer'); ?></label><br>
215 <span class="exp"><?php esc_html_e('Select the duration for which the SSO stays alive', 'loginizer'); ?></span>
216 </td>
217 <td>
218 <select id="lz-sso-ttl" name="sso_ttl" style="width:175px;">
219 <option value="300">5 minutes</option>
220 <option value="600">10 minutes</option>
221 <option value="1800">30 minutes</option>
222 <option value="3600">1 hour</option>
223 <option value="21600">6 hours</option>
224 <option value="43200">12 hours</option>
225 <option value="86400">24 hours</option>
226 <option value="172800">2 Days</option>
227 </select>
228 </td>
229 </tr>
230 <tr>
231 <td scope="row" valign="top">
232 <label for="lz-sso-attempts"><?php esc_html_e('Login Attempts', 'loginizer'); ?></label><br>
233 <span class="exp"><?php esc_html_e('Number of times you want your user to be able to login through same link by default it\'s 1 time and maximum is 15 times', 'loginizer'); ?></span>
234 </td>
235 <td>
236 <input type="number" id="lz-sso-attempts" name="sso_attempts" min="1" max="15" placeholder="Attempt Count" value="1" style="width:175px;">
237 </td>
238 </tr>
239 <tr>
240 <td>
241 </td>
242 <td>
243 <input type="submit" class="button button-primary" name="lz_generate_sso" value="Generate SSO"/>
244 </td>
245 </tr>
246
247 </table>
248 </form>
249 <br/>
250 <form method="POST">
251 <?php echo wp_nonce_field('loginizer_nonce', 'security'); ?>
252 <table class="wp-list-table widefat fixed users" border="0">
253 <tr>
254 <th scope="row" valign="top" style="background:#EFEFEF;" width="20"><input type="checkbox" id="lz_check_all_sso_link" style="margin-left:-1px;"/></th>
255 <th scope="row" valign="top" style="background:#EFEFEF;"><?php esc_html_e('User ID','loginizer'); ?></th>
256 <th scope="row" valign="top" style="background:#EFEFEF;"><?php esc_html_e('Username','loginizer'); ?></th>
257 <th scope="row" valign="top" style="background:#EFEFEF;"><?php esc_html_e('SSO Link','loginizer'); ?></th>
258 <th scope="row" valign="top" style="background:#EFEFEF;"><?php esc_html_e('Attempts Remaining','loginizer'); ?></th>
259 <th scope="row" valign="top" style="background:#EFEFEF;"><?php esc_html_e('Expiring in','loginizer'); ?> <span class="dashicons dashicons-clock"></span></th>
260 </tr>
261
262 <?php
263
264 if(empty($sso_links)){
265 echo '<tr><td colspan="4">'.esc_html__('No SSO link has been created yet.', 'loginizer').'</td></tr>';
266 } else {
267 $expired_links = [];
268
269 foreach($sso_links as $u_id => $sso_link){
270 $user_info = get_userdata($u_id);
271 $expire_utime = get_user_meta($u_id, 'loginizer_sso_'.$u_id.'_expires', true);
272 $sso_attempts = get_user_meta($u_id, 'loginizer_sso_'.$u_id.'_attempts', true);
273
274 if(empty($expire_utime)){
275 $expired_links[] = $u_id;
276 continue;
277 }
278
279 if($expire_utime < time()){
280 $expired_links[] = $u_id;
281 continue;
282 }
283
284 echo '<tr><td><input type="checkbox" name="lz_checksso_link[]" value="'.esc_attr($u_id).'" style="margin-left:-1px;"/></td>
285 <td>'.esc_html($u_id).'</td>
286 <td>'.esc_html($user_info->user_login).'</td>
287 <td>'.esc_url($sso_link).'</td>
288 <td>'.esc_html($sso_attempts).'</td>
289 <td>'.esc_html(human_time_diff(time(), $expire_utime)).'</td>
290 </tr>';
291 }
292
293
294 foreach($expired_links as $expired_link){
295 delete_user_meta($expired_link, 'loginizer_sso_'. $expired_link);
296 delete_user_meta($expired_link, 'loginizer_sso_'. $expired_link. '_expires');
297
298 unset($sso_links[$expired_link]);
299 }
300
301 if(!empty($expired_links)){
302 update_option('loginizer_sso_links', $sso_links);
303 }
304 }
305
306 ?>
307
308 </table><br/>
309 <input type="submit" name="lz_delete_sso" class="button button-primary action" value="<?php esc_html_e('Delete Selected Links', 'loginizer');?>">
310 </form>
311 </div>
312 </div>
313
314 <?php
315 loginizer_page_footer();
316
317 }