-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirm.php
263 lines (221 loc) · 10.1 KB
/
confirm.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Confirm self registered user.
*
* @package auth_wallet
* @copyright 2023 Mohammad Farouk <phun.for.physics@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__.'/auth.php');
require_once(__DIR__.'/lib.php');
require_once($CFG->dirroot . '/enrol/wallet/locallib.php');
require_once($CFG->dirroot . '/login/lib.php');
require_once($CFG->libdir . '/authlib.php');
require_once($CFG->dirroot.'/user/editlib.php');
global $SESSION;
$p = optional_param('p', '', PARAM_ALPHANUM); // Parameter: secret.
$s = optional_param('s', '', PARAM_RAW); // Parameter: username.
$data = optional_param('data', '', PARAM_RAW);
$redirect = optional_param('redirect', '', PARAM_URL);
if (empty($redirect)) {
if (isset($SESSION->wantsurl)) {
$redirect = (new moodle_url($SESSION->wantsurl))->out(false);
} else if ($base = get_user_preferences('auth_wallet_wantsurl', false)) {
$redirect = (new moodle_url($base))->out(false);
} else {
$redirect = core_login_get_return_url();
}
}
// Logout button pressed.
$logout = optional_param('logout', false, PARAM_BOOL);
if ($logout) {
require_logout();
redirect($CFG->wwwroot.'/');
exit;
}
$emailconfirm = get_config('auth_wallet', 'emailconfirm');
$all = get_config('auth_wallet', 'all');
$PAGE->set_url('/auth/wallet/confirm.php');
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('login');
if (empty($all)) {
if (!$auth = signup_get_user_confirmation_authplugin() || $auth->authtype !== 'wallet') {
throw new moodle_exception('confirmationnotenabled');
}
}
$authplugin = new auth_plugin_wallet();
// Part 1 of the code after pressing the confirmation link in email.
// Or redirected from the same page if email confirmation not required or already confirmed.
if ((!empty($p) && !empty($s)) || !empty($data)) {
if (!empty($data)) {
$dataelements = explode('/', $data, 2); // Stop after 1st slash. Rest is username.
$usersecret = $dataelements[0];
$username = $dataelements[1];
} else {
$usersecret = $p;
$username = $s;
}
$user = get_complete_user_data('username', $username);
if (!$user || isguestuser($user)) {
throw new \moodle_exception('invalidconfirmdata', '', '', s($username));
}
// Check confirmation validation.
$confirmed = $authplugin->user_confirm($username, $usersecret);
// User already confirmed.
if ($confirmed == AUTH_CONFIRM_ALREADY) {
$PAGE->navbar->add(get_string("alreadyconfirmed"));
$PAGE->set_title(get_string("alreadyconfirmed"));
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
echo "<p>".get_string("alreadyconfirmed")."</p>\n";
echo $OUTPUT->single_button(core_login_get_return_url(), get_string('courses'));
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
} else if ($confirmed == AUTH_CONFIRM_OK) { // Confirmation validated successfully.
// The user has confirmed successfully, let's log them in.
if (empty($user->suspended)) {
complete_user_login($user);
\core\session\manager::apply_concurrent_login_limit($user->id, session_id());
// Check where to go, $redirect has a higher preference.
if (!empty($redirect)) {
if (!empty($SESSION->wantsurl)) {
unset($SESSION->wantsurl);
}
redirect($redirect);
}
}
$PAGE->navbar->add(get_string("confirmed"));
$PAGE->set_title(get_string("confirmed"));
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
echo "<h3>".get_string("thanks").", ". fullname($USER) . "</h3>\n";
echo "<p>".get_string("confirmed")."</p>\n";
echo $OUTPUT->single_button(core_login_get_return_url(), get_string('continue'));
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
} else if ($confirmed == AUTH_CONFIRM_ERROR) {
// Error while confirming.
// NOTE throw error.
debugging('Confirmation Error.');
redirect(new moodle_url('/login/index.php'), get_string('invalidconfirmdata', 'error'), null, 'error');
}
// If AUTH_CONFIRM_FAIL means that the user not confirmed yet, go to part 2 of the code.
}
// Part 2 of the code.
// The user confirmed by email or not required for confirmation.
// This will display the payment options.
// Get username from the passed parameters.
if (!empty($data)) {
$dataelements = explode('/', $data, 2); // Stop after 1st slash. Rest is username.
$username = $dataelements[1];
} else if (!empty($s)) {
$username = $s;
}
// Or just check if the user already loggedin for payments.
if (!empty($username)) {
$user = get_complete_user_data('username', $username);
} else if (isloggedin() && !isguestuser()) {
global $USER;
$user = fullclone($USER);
}
if (!empty($user) && is_object($user) && !isguestuser($user)) {
// Check if the user already confirmed by payments or not.
$payconfirm = auth_wallet_is_confirmed($user);
$all = get_config('auth_wallet', 'all');
if (empty($user->suspended)) {
if (!empty($user->confirmed)
|| empty($emailconfirm) // The user already confirmed by email or not required.
&& (
$user->auth == 'wallet' // This user auth must be wallet.
|| ( // Or any other auth but with configuration all enabled.
!empty($all)
&& (empty($user->secret) || !empty($user->confirmed))
)
)
) {
// Reaching this part of the code means either the user confirmed by email already and wait payment confirmation,
// or confirmation by email is disabled.
// Prepare redirection url.
if (!empty($user->confirmed)) {
$url = $redirect;
} else {
$params = [
's' => (empty($s)) ? $user->username : $s,
];
if (empty($user->secret)) {
$user->secret = random_string(15);
$DB->set_field('user', 'secret', $user->secret, ['id' => $user->id]);
}
$params['p'] = $user->secret;
$params['redirect'] = $redirect;
$url = new \moodle_url('/auth/wallet/confirm.php', $params);
}
// Login the user to enable payment.
if (!isloggedin() || empty($user->id)) {
$user = complete_user_login($user);
\core\session\manager::apply_concurrent_login_limit($user->id, session_id());
}
require_login(null, false);
if (!empty($payconfirm)) {
// Already confirmed by payment.
// Redirect to set them confirmed.
redirect($url);
} else {
// Display the payment page.
$PAGE->set_title($COURSE->fullname);
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
$balance = (new enrol_wallet\util\balance($user->id))->get_total_balance();
$confirmmethod = get_config('auth_wallet', 'criteria');
$a = [
'balance' => $balance,
'currency' => get_config('enrol_wallet', 'currency'),
'name' => fullname($user),
];
$confirmmethod = get_config('auth_wallet', 'criteria');
if ($confirmmethod === 'balance') { // Minimum balance required.
$extrafee = get_config('auth_wallet', 'extra_fee');
$a['required'] = get_config('auth_wallet', 'required_balance');
$a['rest'] = ($a['required'] - $balance);
$a['extrafee'] = !empty($extrafee) ? get_string('extrafeerequired', 'auth_wallet', $extrafee) : '';
echo get_string('payment_required', 'auth_wallet', $a);
echo enrol_wallet_display_topup_options();
} else if ($confirmmethod === 'fee') { // Signup fee reqired.
$a['required'] = get_config('auth_wallet', 'required_fee');
$a['rest'] = ($a['required'] - $balance);
echo get_string('fee_required', 'auth_wallet', $a);
echo enrol_wallet_display_topup_options();
} else { // Shouldn't happen.
echo $OUTPUT->notification(get_string('settingerror', 'auth_wallet'), 'error', false);
}
$url = new \moodle_url('/auth/wallet/confirm.php', ['logout' => 1]);
echo $OUTPUT->single_button($url, get_string('logout'));
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
}
}
}
}
// Not recognized user, suspended user or not confirmed by email yet.
redirect(new moodle_url('/login/index.php'), get_string('errorwhenconfirming'), null, 'error');