-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.php
124 lines (115 loc) · 3.83 KB
/
helper.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
<?php
// Load the Twofactor_Auth_Module Class
require_once(dirname(__FILE__).'/../twofactor/authmod.php');
class helper_plugin_twofactoraltemail extends Twofactor_Auth_Module {
/**
* If the user has a valid email address in their profile, then this can be used.
*/
public function canUse($user = null){
global $USERINFO;
return ($this->_settingExists("verified", $user) && (empty($USERINFO) || $this->_settingGet("email", '', $user) != $USERINFO['mail']) && $this->getConf('enable') === 1);
}
/**
* This module can not provide authentication functionality at the main login screen.
*/
public function canAuthLogin() {
return false;
}
/**
* This user will need to verify their email.
*/
public function renderProfileForm(){
$elements = array();
// Prompt for an email address.
$email = $this->_settingGet("email");
$elements[] = form_makeTextField('altemail_email', $email, $this->getLang('email'), '' , 'block', array('size'=>'50', 'autocomplete'=>'off'));
// If email has not been verified, then do so here.
if (!$this->_settingExists("verified") && $email) {
// Render the HTML to prompt for the verification/activation OTP.
$elements[] = '<span>'.$this->getLang('verifynotice').'</span>';
$elements[] = form_makeTextField('altemail_verify', '', $this->getLang('verifymodule'), '', 'block', array('size'=>'50', 'autocomplete'=>'off'));
$elements[] = form_makeCheckboxField('altemail_send', '1', $this->getLang('resendcode'),'','block');
}
if ($this->_settingExists("email")) {
// Render the element to remove email.
$elements[] = form_makeCheckboxField('altemail_disable', '1', $this->getLang('killmodule'), '', 'block');
}
return $elements;
}
/**
* Process any user configuration.
*/
public function processProfileForm(){
global $INPUT, $USERINFO;
if ($INPUT->bool('altemail_disable', false)) {
// Delete the email address.
$this->_settingDelete("email");
// Delete the verified setting.
$this->_settingDelete("verified");
return 'deleted';
}
$oldemail = $this->_settingGet("email", '');
if ($oldemail) {
if ($INPUT->bool('altemail_send', false)) {
return 'otp';
}
$otp = $INPUT->str('altemail_verify', '');
if ($otp) { // The user will use email.
$checkResult = $this->processLogin($otp);
// If the code works, then flag this account to use email.
if ($checkResult == false) {
return 'failed';
}
else {
$this->_settingSet("verified", true);
return 'verified';
}
}
}
$changed = null;
$email = $INPUT->str('altemail_email', '');
if ($email != $oldemail) {
if ($email == $USERINFO['mail']) {
msg($this->getLang('notsameemail'),-1);
}
else {
if ($this->_settingSet("email", $email)== false) {
msg("TwoFactor: Error setting alternate email.", -1);
}
// Delete the verification for the email if it was changed.
$this->_settingDelete("verified");
$changed = true;
}
}
// If the data changed and we have everything needed to use this module, send an otp.
if ($changed && $this->_settingExists("email")) {
$changed = 'otp';
}
return $changed;
}
/**
* This module can send messages.
*/
public function canTransmitMessage(){
return true;
}
/**
* Transmit the message via email to the address on file.
* As a special case, configure the mail settings to send only via text.
*/
public function transmitMessage($subject, $message, $force = false){
if (!$this->canUse() && !$force) { return false; }
$to = $this->_settingGet("email");
// Create the email object.
$mail = new Mailer();
$mail->to($to);
$mail->subject($subject);
$mail->setText($message);
$result = $mail->send();
return $result;
}
/**
* This module uses the default authentication.
*/
//public function processLogin($code);
}