/
setup.php
129 lines (102 loc) · 3.51 KB
/
setup.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
<?php
/*
* TwoFactorAuth
*
* Copyright (C) 2021-2022 e107 Inc. (https://www.e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
if(!defined('e107_INIT'))
{
require_once(__DIR__.'/../../class2.php');
}
// Make this page inaccessible when plugin is not installed.
if (!e107::isInstalled('twofactorauth'))
{
e107::redirect();
exit;
}
// Only show this page when user is logged in already .
if(!USER)
{
e107::redirect(e_BASE.'login.php');
}
// Load required files (TwoFactorAuth Library and twofactorauth class)
e107_require_once(e_PLUGIN.'twofactorauth/vendor/autoload.php');
use \RobThree\Auth\TwoFactorAuth;
$tfa_library = new TwoFactorAuth();
require_once(e_PLUGIN."twofactorauth/twofactorauth_class.php");
$tfa_class = new tfa_class();
// Load LAN files
e107::lan('twofactorauth', false, true);
$caption = LAN_2FA_TITLE." - ".LAN_SETTINGS;
e107::title($caption);
require_once(HEADERF);
$text = "";
$usersettings_url = e107::getUrl()->create('user/myprofile/edit', array('id' => USERID));
// Check if 2FA is already enabled for current user
$tfaActivated = $tfa_class->tfaActivated(USERID) ? true : false;
// Setting up 2FA
if(!$tfaActivated && isset($_POST['enter-totp-enable']))
{
$secret_key = (string) $_POST['secret_key'];
$totp = intval($_POST['totp']);
$totp = (string) $totp;
if($tfa_class->processEnable(USERID, $secret_key, $totp))
{
e107::getMessage()->addSuccess(e107::getParser()->toHTML(LAN_2FA_ENABLED, true));
$text = "<a class='btn btn-primary' href='".$usersettings_url."'>".LAN_2FA_RETURN_USERSETTINGS."</a>.";
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;
}
}
if($tfaActivated && isset($_POST['enter-totp-disable']))
{
$totp = intval($_POST['totp']);
$totp = (string) $totp;
if($tfa_class->processDisable(USERID, $totp))
{
e107::getMessage()->addSuccess(e107::getParser()->toHTML(LAN_2FA_DISABLED, true));
$text = "<a class='btn btn-primary' href='".$usersettings_url."'>".LAN_2FA_RETURN_USERSETTINGS."</a>.";
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;
}
}
// 2FA not setup yet, show instructions
if(!$tfaActivated)
{
// Generate Secret Key
$secret = $tfa_library->createSecret(160);
// Setup label - defaults to SITENAME
$label = e107::getPlugPref('twofactorauth', 'tfa_label');
if(empty($label))
{
$label = SITENAME;
}
$instructions1 = str_replace(
array("[", "]"),
array("<strong><a href='https://github.com/e107inc/twofactorauth#recommended-authenticator-applications' target='_blank'>", "</a></strong>"),
LAN_2FA_ENABLE_INSTRUCTIONS1
);
e107::getMessage()->addInfo($instructions1);
$text .= '<img class="center-block" src="' . $tfa_library->getQRCodeImageAsDataUri($label, $secret) . '"><br>';
$text .= '<p class="text-center font-italic">'.chunk_split($secret, 4, ' ').'</p>';
$text .= '<p>'.LAN_2FA_ENABLE_INSTRUCTIONS2.'</p>';
$text .= $tfa_class->showTotpInputForm('enable', $secret);
// TEMP FOR DEV PURPOSES
// $correct_totp = $tfa_library->getCode($secret);
// $text .= $correct_totp;
}
// 2FA is already activated, show option(s) to disable.
else
{
e107::getMessage()->addInfo(e107::getParser()->toHTML(LAN_2FA_DISABLE_INSTRUCTIONS, true));
$text .= $tfa_class->showTotpInputForm('disable');
}
// Let's render and show it all!
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;