blob: ba273e4a8fc0421910a7df58dd2967ed4b20757d [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
2require_once 'vendor/autoload.php';
Matheus Fillipe301684b2021-05-14 09:09:43 +03003include 'config.php';
matheusfillipeabd513e2021-05-11 03:29:11 -03004include_once 'redis.php';
5include_once 'utils.php';
Matheus Fillipe301684b2021-05-14 09:09:43 +03006include_once 'mail.php';
7include_once 'ldap.php';
8include_once 'validators.php';
Marc Kupietz0215a442023-03-05 18:34:16 +01009include_once 'User.php';
10require __DIR__ . '/vendor/autoload.php';
11use Monolog\Level;
12use Monolog\Logger as Logger;
13use Monolog\Handler\StreamHandler;
14use Monolog\Handler\RotatingFileHandler;
15use \User as User;
16$log = new Logger('signup');
17$log->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/signup.log', 0, Logger::DEBUG));
matheusfillipeabd513e2021-05-11 03:29:11 -030018
19if (!$DEBUG) error_reporting(0);
matheusfillipec0ce7fa2021-05-13 05:15:37 -030020else error_reporting(1);
matheusfillipeabd513e2021-05-11 03:29:11 -030021session_start();
22
matheusfillipeabd513e2021-05-11 03:29:11 -030023use Gregwar\Captcha\PhraseBuilder;
24
matheusfillipec0ce7fa2021-05-13 05:15:37 -030025$URI = array_slice(explode("/", explode('?', $_SERVER['REQUEST_URI'], 2)[0]), -1)[0];
matheusfillipef43dd962021-05-13 23:27:01 -030026if (strlen($URI) == 2) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030027 $GLOBALS["cc"] = $URI;
28 $_SESSION["cc"] = $URI;
29}
30
matheusfillipef43dd962021-05-13 23:27:01 -030031if (isset($_GET["lang"])) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030032 $GLOBALS["cc"] = $_GET["lang"];
33 $_SESSION["cc"] = $_GET["lang"];
34}
35
36$TEMPLATE = template_path();
37
38
Marc Kupietza3f3fdb2023-03-04 18:13:26 +010039function send_confirmation_email(string $mail, object $smtp, string $url, User $user)
matheusfillipef43dd962021-05-13 23:27:01 -030040{
matheusfillipeabd513e2021-05-11 03:29:11 -030041 include 'config.php';
matheusfillipef43dd962021-05-13 23:27:01 -030042 $TEMPLATE = template_path();
Matheus Fillipe301684b2021-05-14 09:09:43 +030043 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -030044
45 send_mail($mail, $smtp, (object) [
46 "subject" => $MAIL_TEMPLATE->subject,
Marc Kupietza3f3fdb2023-03-04 18:13:26 +010047 "text" => replace_all_user_variables($MAIL_TEMPLATE->text, $user, $url),
48 "html" => replace_all_user_variables($MAIL_TEMPLATE->html, $user, $url)
matheusfillipef43dd962021-05-13 23:27:01 -030049 ]);
50}
51
52function send_recovery_email(string $mail, object $smtp, string $url)
53{
54 include 'config.php';
matheusfillipef43dd962021-05-13 23:27:01 -030055 $TEMPLATE = template_path();
Matheus Fillipe301684b2021-05-14 09:09:43 +030056 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -030057
58 send_mail($mail, $smtp, (object) [
59 "subject" => $RECOVERY_EMAIL_TEMPLATE->subject,
60 "text" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->text),
61 "html" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->html)
62 ]);
63}
64
65function reload_captcha_script()
66{
67 include 'config.php';
68 $TEMPLATE = template_path();
69 include $TEMPLATE . "strings.php";
matheusfillipeabd513e2021-05-11 03:29:11 -030070 echo '
71 <script>
72 const reload_captcha = async (e) => {
73 var cont = document.getElementById("reload_captcha");
matheusfillipef43dd962021-05-13 23:27:01 -030074 cont.innerHTML = "<div class=\'spinner-border text-info\' role=\'status\'><span class=\'sr-only\'>' . $STRINGS->reloading_captcha . '</span></div>";
matheusfillipeabd513e2021-05-11 03:29:11 -030075 var img = document.getElementById("captcha")
matheusfillipef43dd962021-05-13 23:27:01 -030076 var url = "' . $BASE_URL . '/captcha.php?token=' . $_SESSION["captcha_token"] . '"
matheusfillipeabd513e2021-05-11 03:29:11 -030077 await fetch(url, { cache: "reload", mode: "no-cors" })
78 .then(() => {
79 img.src = url+"&t=" + new Date().getTime();
80 setTimeout( () => {
81 cont.innerHTML = "<button id=\'reload\' class=\'btn btn-outline-info\' type=\'button\'> <span class=\'glyphicon glyphicon-refresh\' aria-hidden=\'true\'></span></button>";
82 bindButton()
83 }, 500);
84 })
85 }
86 function bindButton(){
87 var button = document.getElementById("reload");
88 button.addEventListener("click", reload_captcha)
89 }
90 bindButton()
91 </script>
92 ';
93}
94
matheusfillipef43dd962021-05-13 23:27:01 -030095function register_page($error = false)
96{
97 $TEMPLATE = template_path();
98 include 'config.php';
99 if ($error)
100 include $TEMPLATE . 'error.htm';
101 $_SESSION["captcha_token"] = generateRandomString(12);
102 include $TEMPLATE . "register.htm";
103 reload_captcha_script();
104}
matheusfillipeabd513e2021-05-11 03:29:11 -0300105
matheusfillipef43dd962021-05-13 23:27:01 -0300106
Marc Kupietz0215a442023-03-05 18:34:16 +0100107function verify_request(User $user)
matheusfillipef43dd962021-05-13 23:27:01 -0300108{
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300109 $TEMPLATE = template_path();
matheusfillipeabd513e2021-05-11 03:29:11 -0300110 unset($_SESSION['captcha_token']);
matheusfillipef43dd962021-05-13 23:27:01 -0300111 include $TEMPLATE . 'strings.php';
matheusfillipeabd513e2021-05-11 03:29:11 -0300112 $password = $_POST["password"];
matheusfillipe47cf90b2021-05-13 03:36:21 -0300113 $error = "";
114
115 $error .= validate_username($user->user_name);
116 $error .= validate_name($user->name, $FIRST_NAME_VALIDATION_ERROR);
117 $error .= validate_name($user->last_name, $LAST_NAME_VALIDATION_ERROR);
118 $error .= validate_email($user->email);
119 $error .= validate_password($password);
120
matheusfillipeabd513e2021-05-11 03:29:11 -0300121
Marc Kupietz493198f2023-03-04 14:59:16 +0100122 if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) {
matheusfillipef43dd962021-05-13 23:27:01 -0300123 $error = $error . $STRINGS->wrong_captcha;
matheusfillipeabd513e2021-05-11 03:29:11 -0300124 }
125 unset($_SESSION["captcha"]);
matheusfillipef43dd962021-05-13 23:27:01 -0300126
matheusfillipeabd513e2021-05-11 03:29:11 -0300127 return $error;
128}
129
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100130function backup_user_in_session($user)
131{
132 $_SESSION['username'] = $user->user_name;
133 $_SESSION['first_name'] = $user->first_name;
134 $_SESSION['last_name'] = $user->last_name;
135 $_SESSION['email'] = $user->email;
136 $_SESSION['organization'] = $user->organization;
137}
138
Marc Kupietz0215a442023-03-05 18:34:16 +0100139function approve_request(User $user)
matheusfillipef43dd962021-05-13 23:27:01 -0300140{
Matheus Fillipe301684b2021-05-14 09:09:43 +0300141 include 'config.php';
Marc Kupietz0215a442023-03-05 18:34:16 +0100142 global $log;
143
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300144 $token = generateRandomString();
matheusfillipeabd513e2021-05-11 03:29:11 -0300145 redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY);
146 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300147 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300148 $maillist = $pending->mails;
149 array_push($maillist, $user->email);
matheusfillipef43dd962021-05-13 23:27:01 -0300150 } else
matheusfillipeabd513e2021-05-11 03:29:11 -0300151 $maillist = [$user->email];
matheusfillipef43dd962021-05-13 23:27:01 -0300152 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300153
matheusfillipef43dd962021-05-13 23:27:01 -0300154 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300155 if (in_array(explode("@", $user->email)[1], $MAIL_HOST_DIRECT_FALLBACK))
156 $smtp = $FALLBACK_SMTP;
157 else
158 $smtp = $SMTP;
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100159 send_confirmation_email($user->email, $smtp, $url, $user);
matheusfillipeabd513e2021-05-11 03:29:11 -0300160 $_SESSION['resend'] = generateRandomString(12);
161 $_SESSION['token'] = $token;
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100162 backup_user_in_session($user);
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300163 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300164 include $TEMPLATE . "confirm_your_email.htm";
Marc Kupietz0215a442023-03-05 18:34:16 +0100165 $log->info("Registration process started for " . user_to_string($user));
matheusfillipeabd513e2021-05-11 03:29:11 -0300166}
167
Matheus Fillipe301684b2021-05-14 09:09:43 +0300168function recover_form($error = null)
169{
170 $TEMPLATE = template_path();
171 include 'config.php';
172 $_SESSION["captcha_token"] = generateRandomString(12);
173 if ($error)
174 include $TEMPLATE . 'error.htm';
175 include $TEMPLATE . "recover_email_form.htm";
176 reload_captcha_script();
177}
178
179function new_password_form($error = null)
180{
181 $TEMPLATE = template_path();
182 if ($error)
183 include $TEMPLATE . 'error.htm';
184 include $TEMPLATE . "recover_new_password_form.htm";
185}
186
matheusfillipeabd513e2021-05-11 03:29:11 -0300187
188// PAGE
matheusfillipef43dd962021-05-13 23:27:01 -0300189include $TEMPLATE . "header.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300190
191if ($_SERVER["REQUEST_METHOD"] == "POST") {
matheusfillipeabd513e2021-05-11 03:29:11 -0300192 if (isset($_POST['type'])) {
193 switch ($_POST['type']) {
194 case "register":
Marc Kupietza19f3072023-02-25 14:16:40 +0100195 $user = new User($_POST["username"], $_POST["name"], $_POST["last_name"], $_POST["email"], $_POST["password"], $_POST["organization"]);
matheusfillipef43dd962021-05-13 23:27:01 -0300196 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
197 include $TEMPLATE . "registration_limit.htm";
198 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300199 $error = verify_request($user);
200 if ($error)
201 register_page($error);
202 else
203 approve_request($user);
204 }
205 break;
matheusfillipe47cf90b2021-05-13 03:36:21 -0300206 case "recover":
matheusfillipef43dd962021-05-13 23:27:01 -0300207 $TEMPLATE = template_path();
208 unset($_SESSION['captcha_token']);
209 include $TEMPLATE . 'strings.php';
210
211 $email = $_POST["email"];
212 if (!ldap_mail_count($email)) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300213 unset($_POST['email']);
matheusfillipef43dd962021-05-13 23:27:01 -0300214 $error = $error . $STRINGS->recover_email_not_registered;
215 }
216
Marc Kupietz493198f2023-03-04 14:59:16 +0100217 if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) {
matheusfillipef43dd962021-05-13 23:27:01 -0300218 $error = $error . $STRINGS->wrong_captcha;
219 }
220
221 unset($_SESSION["captcha"]);
222 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
223 include $TEMPLATE . "registration_limit.htm";
224 } else {
225 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300226 recover_form($error);
matheusfillipef43dd962021-05-13 23:27:01 -0300227 } else {
228 include $TEMPLATE . 'strings.php';
229 $token = generateRandomString();
230 redis_set($token, $email, $MAIL_CONFIRMATION_AWAIT_DELAY);
231
232 $url = $BASE_URL . "?type=password_change&token=" . $token;
233 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
234 $smtp = $FALLBACK_SMTP;
235 else
236 $smtp = $SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300237 $_SESSION['resend'] = generateRandomString(12);
238 $_SESSION['token'] = $token;
239 $_SESSION['email'] = $email;
240 $_SESSION['recover'] = $email;
241 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300242 send_recovery_email($email, $smtp, $url);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300243 include $TEMPLATE . "confirm_your_email.htm";
Marc Kupietz0215a442023-03-05 18:34:16 +0100244 $log->info("Password recovery email sent to " . $email);
matheusfillipef43dd962021-05-13 23:27:01 -0300245 }
246 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300247 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300248
249 case "password_change":
matheusfillipef43dd962021-05-13 23:27:01 -0300250 $password = $_POST['password'];
251 $error = validate_password($password);
252 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300253 new_password_form($error);
254 } else {
255 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300256 include $TEMPLATE . "recover_success.htm";
257 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -0300258 $email = $_SESSION["email_change"];
259 if (change_password($email, $password)) {
260 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
261 $smtp = $FALLBACK_SMTP;
262 else
263 $smtp = $SMTP;
264 send_mail($email, $smtp, $PASSWORD_CHANGED_EMAIL_TEMPLATE);
Marc Kupietz0215a442023-03-05 18:34:16 +0100265 $log->info("Password changed for " . $email);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300266 } else {
matheusfillipef43dd962021-05-13 23:27:01 -0300267 include $TEMPLATE . "strings.php";
268 echo $STRINGS->change_password_ldap_error;
269 }
Matheus Fillipe301684b2021-05-14 09:09:43 +0300270 unset($_SESSION["email_change"]);
271 redis_delete($_SESSION['token']);
matheusfillipef43dd962021-05-13 23:27:01 -0300272 }
273 break;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300274 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300275 }
276} elseif (isset($_GET['type'])) {
277 switch ($_GET['type']) {
278 case "confirmation":
matheusfillipef43dd962021-05-13 23:27:01 -0300279 if (!isset($_GET["token"])) {
matheusfillipe47cf90b2021-05-13 03:36:21 -0300280 echo $RUNTIME_ERROR->user_trying_invalid_get;
matheusfillipef43dd962021-05-13 23:27:01 -0300281 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300282 $token = $_GET["token"];
Marc Kupietz0215a442023-03-05 18:34:16 +0100283 $user = redis_get_user($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300284 if ($user && gettype($user) == "object") {
285 if (ldap_add_user($user)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300286 if ($REDIRECT_TO)
matheusfillipef43dd962021-05-13 23:27:01 -0300287 header("refresh:5;url=" . $REDIRECT_TO);
matheusfillipeabd513e2021-05-11 03:29:11 -0300288
289 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300290 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300291 $maillist = $pending->mails;
matheusfillipef43dd962021-05-13 23:27:01 -0300292 if (in_array($user->email, $maillist)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300293 unset($maillist[array_search($user->email, $maillist)]);
matheusfillipef43dd962021-05-13 23:27:01 -0300294 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300295 }
296 }
297 redis_inc_ipdata(getClientIP(), "register");
Marc Kupietz0215a442023-03-05 18:34:16 +0100298 $log->info("User registered: " . user_to_string($user));
matheusfillipe47cf90b2021-05-13 03:36:21 -0300299 echo $STRINGS->email_confirmation;
Matheus Fillipe5ad0cd52022-05-05 06:10:41 +0300300 if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user);
matheusfillipef43dd962021-05-13 23:27:01 -0300301 include $TEMPLATE . "mail_confirmed.htm";
302 } else {
Marc Kupietz0215a442023-03-05 18:34:16 +0100303 $log->error("User registration failed for: " . user_to_string($user));
matheusfillipe47cf90b2021-05-13 03:36:21 -0300304 echo $STRINGS->email_confirmation;
matheusfillipef43dd962021-05-13 23:27:01 -0300305 include $TEMPLATE . "registration_error.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300306 }
307 redis_delete($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300308 } else {
309 include $TEMPLATE . "token_expired.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300310 }
311 }
312 break;
313 case "resend":
matheusfillipef43dd962021-05-13 23:27:01 -0300314 if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) {
315 include $TEMPLATE . "resend_mail.htm";
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100316 $user = new User($_SESSION["username"], $_SESSION["first_name"], $_SESSION["last_name"], $_SESSION["email"], "", $_SESSION["organization"]);
matheusfillipeabd513e2021-05-11 03:29:11 -0300317 $token = $_SESSION['token'];
matheusfillipef43dd962021-05-13 23:27:01 -0300318 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300319 $smtp = $FALLBACK_SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300320 $address = $_SESSION["email"];
Matheus Fillipe301684b2021-05-14 09:09:43 +0300321 if (isset($_SESSION['recover'])) {
matheusfillipef43dd962021-05-13 23:27:01 -0300322 $url = $BASE_URL . "?type=password_change&token=" . $token;
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100323 send_recovery_email($address, $smtp, $url, $user);
matheusfillipef43dd962021-05-13 23:27:01 -0300324 unset($_SESSION['recover']);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300325 } else
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100326 send_confirmation_email($address, $smtp, $url, $user);
matheusfillipeabd513e2021-05-11 03:29:11 -0300327 unset($_SESSION['resend']);
328 unset($_SESSION['token']);
Marc Kupietza3f3fdb2023-03-04 18:13:26 +0100329 # unset($_SESSION['email']);
330 } else {
331 echo "<h2>A second email has already been sent to " . $_SESSION['email'] .".</h2>";
matheusfillipeabd513e2021-05-11 03:29:11 -0300332 }
333 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300334
matheusfillipe47cf90b2021-05-13 03:36:21 -0300335 case "recover":
Matheus Fillipe301684b2021-05-14 09:09:43 +0300336 recover_form();
matheusfillipe47cf90b2021-05-13 03:36:21 -0300337 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300338
339 case "password_change":
340 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300341 $token = $_GET["token"];
342 $email = redis_get($token);
343 $_SESSION["email_change"] = $email;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300344 $_SESSION["token"] = $token;
345 if ($email && gettype($email) == "string") {
346 new_password_form();
matheusfillipef43dd962021-05-13 23:27:01 -0300347 } else {
348 include $TEMPLATE . "token_expired.htm";
349 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300350 break;
matheusfillipeabd513e2021-05-11 03:29:11 -0300351 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300352} else {
353 unset($_SESSION['captcha_token']);
354 register_page();
355}
356
matheusfillipef43dd962021-05-13 23:27:01 -0300357include $TEMPLATE . "bottom.htm";