blob: 0ffa33cf39bb8a7a1a4bc0b9601eaf8f54cb669f [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';
matheusfillipeabd513e2021-05-11 03:29:11 -03009
10if (!$DEBUG) error_reporting(0);
matheusfillipec0ce7fa2021-05-13 05:15:37 -030011else error_reporting(1);
matheusfillipeabd513e2021-05-11 03:29:11 -030012session_start();
13
matheusfillipeabd513e2021-05-11 03:29:11 -030014use Gregwar\Captcha\PhraseBuilder;
15
matheusfillipec0ce7fa2021-05-13 05:15:37 -030016$URI = array_slice(explode("/", explode('?', $_SERVER['REQUEST_URI'], 2)[0]), -1)[0];
matheusfillipef43dd962021-05-13 23:27:01 -030017if (strlen($URI) == 2) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030018 $GLOBALS["cc"] = $URI;
19 $_SESSION["cc"] = $URI;
20}
21
matheusfillipef43dd962021-05-13 23:27:01 -030022if (isset($_GET["lang"])) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030023 $GLOBALS["cc"] = $_GET["lang"];
24 $_SESSION["cc"] = $_GET["lang"];
25}
26
27$TEMPLATE = template_path();
28
29
Marc Kupietz0375b7e2023-03-04 18:13:26 +010030function send_confirmation_email(string $mail, object $smtp, string $url, User $user)
matheusfillipef43dd962021-05-13 23:27:01 -030031{
matheusfillipeabd513e2021-05-11 03:29:11 -030032 include 'config.php';
matheusfillipef43dd962021-05-13 23:27:01 -030033 $TEMPLATE = template_path();
Matheus Fillipe301684b2021-05-14 09:09:43 +030034 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -030035
36 send_mail($mail, $smtp, (object) [
37 "subject" => $MAIL_TEMPLATE->subject,
Marc Kupietz0375b7e2023-03-04 18:13:26 +010038 "text" => replace_all_user_variables($MAIL_TEMPLATE->text, $user, $url),
39 "html" => replace_all_user_variables($MAIL_TEMPLATE->html, $user, $url)
matheusfillipef43dd962021-05-13 23:27:01 -030040 ]);
41}
42
43function send_recovery_email(string $mail, object $smtp, string $url)
44{
45 include 'config.php';
matheusfillipef43dd962021-05-13 23:27:01 -030046 $TEMPLATE = template_path();
Matheus Fillipe301684b2021-05-14 09:09:43 +030047 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -030048
49 send_mail($mail, $smtp, (object) [
50 "subject" => $RECOVERY_EMAIL_TEMPLATE->subject,
51 "text" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->text),
52 "html" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->html)
53 ]);
54}
55
56function reload_captcha_script()
57{
58 include 'config.php';
59 $TEMPLATE = template_path();
60 include $TEMPLATE . "strings.php";
matheusfillipeabd513e2021-05-11 03:29:11 -030061 echo '
62 <script>
63 const reload_captcha = async (e) => {
64 var cont = document.getElementById("reload_captcha");
matheusfillipef43dd962021-05-13 23:27:01 -030065 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 -030066 var img = document.getElementById("captcha")
matheusfillipef43dd962021-05-13 23:27:01 -030067 var url = "' . $BASE_URL . '/captcha.php?token=' . $_SESSION["captcha_token"] . '"
matheusfillipeabd513e2021-05-11 03:29:11 -030068 await fetch(url, { cache: "reload", mode: "no-cors" })
69 .then(() => {
70 img.src = url+"&t=" + new Date().getTime();
71 setTimeout( () => {
72 cont.innerHTML = "<button id=\'reload\' class=\'btn btn-outline-info\' type=\'button\'> <span class=\'glyphicon glyphicon-refresh\' aria-hidden=\'true\'></span></button>";
73 bindButton()
74 }, 500);
75 })
76 }
77 function bindButton(){
78 var button = document.getElementById("reload");
79 button.addEventListener("click", reload_captcha)
80 }
81 bindButton()
82 </script>
83 ';
84}
85
matheusfillipef43dd962021-05-13 23:27:01 -030086function register_page($error = false)
87{
88 $TEMPLATE = template_path();
89 include 'config.php';
90 if ($error)
91 include $TEMPLATE . 'error.htm';
92 $_SESSION["captcha_token"] = generateRandomString(12);
93 include $TEMPLATE . "register.htm";
94 reload_captcha_script();
95}
matheusfillipeabd513e2021-05-11 03:29:11 -030096
matheusfillipef43dd962021-05-13 23:27:01 -030097
98function verify_request($user)
99{
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300100 $TEMPLATE = template_path();
matheusfillipeabd513e2021-05-11 03:29:11 -0300101 unset($_SESSION['captcha_token']);
matheusfillipef43dd962021-05-13 23:27:01 -0300102 include $TEMPLATE . 'strings.php';
matheusfillipeabd513e2021-05-11 03:29:11 -0300103 $password = $_POST["password"];
matheusfillipe47cf90b2021-05-13 03:36:21 -0300104 $error = "";
105
106 $error .= validate_username($user->user_name);
107 $error .= validate_name($user->name, $FIRST_NAME_VALIDATION_ERROR);
108 $error .= validate_name($user->last_name, $LAST_NAME_VALIDATION_ERROR);
109 $error .= validate_email($user->email);
110 $error .= validate_password($password);
111
matheusfillipeabd513e2021-05-11 03:29:11 -0300112
Marc Kupietzd2dfa002023-03-04 14:59:16 +0100113 if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) {
matheusfillipef43dd962021-05-13 23:27:01 -0300114 $error = $error . $STRINGS->wrong_captcha;
matheusfillipeabd513e2021-05-11 03:29:11 -0300115 }
116 unset($_SESSION["captcha"]);
matheusfillipef43dd962021-05-13 23:27:01 -0300117
matheusfillipeabd513e2021-05-11 03:29:11 -0300118 return $error;
119}
120
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100121function backup_user_in_session($user)
122{
123 $_SESSION['username'] = $user->user_name;
124 $_SESSION['first_name'] = $user->first_name;
125 $_SESSION['last_name'] = $user->last_name;
126 $_SESSION['email'] = $user->email;
127 $_SESSION['organization'] = $user->organization;
128}
129
matheusfillipef43dd962021-05-13 23:27:01 -0300130function approve_request($user)
131{
Matheus Fillipe301684b2021-05-14 09:09:43 +0300132 include 'config.php';
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300133 $token = generateRandomString();
matheusfillipeabd513e2021-05-11 03:29:11 -0300134 redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY);
135 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300136 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300137 $maillist = $pending->mails;
138 array_push($maillist, $user->email);
matheusfillipef43dd962021-05-13 23:27:01 -0300139 } else
matheusfillipeabd513e2021-05-11 03:29:11 -0300140 $maillist = [$user->email];
matheusfillipef43dd962021-05-13 23:27:01 -0300141 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300142
matheusfillipef43dd962021-05-13 23:27:01 -0300143 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300144 if (in_array(explode("@", $user->email)[1], $MAIL_HOST_DIRECT_FALLBACK))
145 $smtp = $FALLBACK_SMTP;
146 else
147 $smtp = $SMTP;
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100148 send_confirmation_email($user->email, $smtp, $url, $user);
matheusfillipeabd513e2021-05-11 03:29:11 -0300149 $_SESSION['resend'] = generateRandomString(12);
150 $_SESSION['token'] = $token;
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100151 backup_user_in_session($user);
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300152 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300153 include $TEMPLATE . "confirm_your_email.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300154}
155
Matheus Fillipe301684b2021-05-14 09:09:43 +0300156function recover_form($error = null)
157{
158 $TEMPLATE = template_path();
159 include 'config.php';
160 $_SESSION["captcha_token"] = generateRandomString(12);
161 if ($error)
162 include $TEMPLATE . 'error.htm';
163 include $TEMPLATE . "recover_email_form.htm";
164 reload_captcha_script();
165}
166
167function new_password_form($error = null)
168{
169 $TEMPLATE = template_path();
170 if ($error)
171 include $TEMPLATE . 'error.htm';
172 include $TEMPLATE . "recover_new_password_form.htm";
173}
174
matheusfillipeabd513e2021-05-11 03:29:11 -0300175
176// PAGE
matheusfillipef43dd962021-05-13 23:27:01 -0300177include $TEMPLATE . "header.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300178
179if ($_SERVER["REQUEST_METHOD"] == "POST") {
matheusfillipeabd513e2021-05-11 03:29:11 -0300180 if (isset($_POST['type'])) {
181 switch ($_POST['type']) {
182 case "register":
Marc Kupietza19f3072023-02-25 14:16:40 +0100183 $user = new User($_POST["username"], $_POST["name"], $_POST["last_name"], $_POST["email"], $_POST["password"], $_POST["organization"]);
matheusfillipef43dd962021-05-13 23:27:01 -0300184 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
185 include $TEMPLATE . "registration_limit.htm";
186 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300187 $error = verify_request($user);
188 if ($error)
189 register_page($error);
190 else
191 approve_request($user);
192 }
193 break;
matheusfillipe47cf90b2021-05-13 03:36:21 -0300194 case "recover":
matheusfillipef43dd962021-05-13 23:27:01 -0300195 $TEMPLATE = template_path();
196 unset($_SESSION['captcha_token']);
197 include $TEMPLATE . 'strings.php';
198
199 $email = $_POST["email"];
200 if (!ldap_mail_count($email)) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300201 unset($_POST['email']);
matheusfillipef43dd962021-05-13 23:27:01 -0300202 $error = $error . $STRINGS->recover_email_not_registered;
203 }
204
Marc Kupietzd2dfa002023-03-04 14:59:16 +0100205 if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) {
matheusfillipef43dd962021-05-13 23:27:01 -0300206 $error = $error . $STRINGS->wrong_captcha;
207 }
208
209 unset($_SESSION["captcha"]);
210 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
211 include $TEMPLATE . "registration_limit.htm";
212 } else {
213 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300214 recover_form($error);
matheusfillipef43dd962021-05-13 23:27:01 -0300215 } else {
216 include $TEMPLATE . 'strings.php';
217 $token = generateRandomString();
218 redis_set($token, $email, $MAIL_CONFIRMATION_AWAIT_DELAY);
219
220 $url = $BASE_URL . "?type=password_change&token=" . $token;
221 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
222 $smtp = $FALLBACK_SMTP;
223 else
224 $smtp = $SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300225 $_SESSION['resend'] = generateRandomString(12);
226 $_SESSION['token'] = $token;
227 $_SESSION['email'] = $email;
228 $_SESSION['recover'] = $email;
229 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300230 send_recovery_email($email, $smtp, $url);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300231 include $TEMPLATE . "confirm_your_email.htm";
matheusfillipef43dd962021-05-13 23:27:01 -0300232 }
233 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300234 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300235
236 case "password_change":
matheusfillipef43dd962021-05-13 23:27:01 -0300237 $password = $_POST['password'];
238 $error = validate_password($password);
239 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300240 new_password_form($error);
241 } else {
242 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300243 include $TEMPLATE . "recover_success.htm";
244 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -0300245 $email = $_SESSION["email_change"];
246 if (change_password($email, $password)) {
247 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
248 $smtp = $FALLBACK_SMTP;
249 else
250 $smtp = $SMTP;
251 send_mail($email, $smtp, $PASSWORD_CHANGED_EMAIL_TEMPLATE);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300252 } else {
matheusfillipef43dd962021-05-13 23:27:01 -0300253 include $TEMPLATE . "strings.php";
254 echo $STRINGS->change_password_ldap_error;
255 }
Matheus Fillipe301684b2021-05-14 09:09:43 +0300256 unset($_SESSION["email_change"]);
257 redis_delete($_SESSION['token']);
matheusfillipef43dd962021-05-13 23:27:01 -0300258 }
259 break;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300260 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300261 }
262} elseif (isset($_GET['type'])) {
263 switch ($_GET['type']) {
264 case "confirmation":
matheusfillipef43dd962021-05-13 23:27:01 -0300265 if (!isset($_GET["token"])) {
matheusfillipe47cf90b2021-05-13 03:36:21 -0300266 echo $RUNTIME_ERROR->user_trying_invalid_get;
matheusfillipef43dd962021-05-13 23:27:01 -0300267 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300268 $token = $_GET["token"];
269 $user = redis_get($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300270 if ($user && gettype($user) == "object") {
271 if (ldap_add_user($user)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300272 if ($REDIRECT_TO)
matheusfillipef43dd962021-05-13 23:27:01 -0300273 header("refresh:5;url=" . $REDIRECT_TO);
matheusfillipeabd513e2021-05-11 03:29:11 -0300274
275 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300276 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300277 $maillist = $pending->mails;
matheusfillipef43dd962021-05-13 23:27:01 -0300278 if (in_array($user->email, $maillist)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300279 unset($maillist[array_search($user->email, $maillist)]);
matheusfillipef43dd962021-05-13 23:27:01 -0300280 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300281 }
282 }
283 redis_inc_ipdata(getClientIP(), "register");
matheusfillipe47cf90b2021-05-13 03:36:21 -0300284 echo $STRINGS->email_confirmation;
Matheus Fillipe5ad0cd52022-05-05 06:10:41 +0300285 if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user);
matheusfillipef43dd962021-05-13 23:27:01 -0300286 include $TEMPLATE . "mail_confirmed.htm";
287 } else {
matheusfillipe47cf90b2021-05-13 03:36:21 -0300288 echo $STRINGS->email_confirmation;
matheusfillipef43dd962021-05-13 23:27:01 -0300289 include $TEMPLATE . "registration_error.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300290 }
291 redis_delete($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300292 } else {
293 include $TEMPLATE . "token_expired.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300294 }
295 }
296 break;
297 case "resend":
matheusfillipef43dd962021-05-13 23:27:01 -0300298 if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) {
299 include $TEMPLATE . "resend_mail.htm";
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100300 $user = new User($_SESSION["username"], $_SESSION["first_name"], $_SESSION["last_name"], $_SESSION["email"], "", $_SESSION["organization"]);
matheusfillipeabd513e2021-05-11 03:29:11 -0300301 $token = $_SESSION['token'];
matheusfillipef43dd962021-05-13 23:27:01 -0300302 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300303 $smtp = $FALLBACK_SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300304 $address = $_SESSION["email"];
Matheus Fillipe301684b2021-05-14 09:09:43 +0300305 if (isset($_SESSION['recover'])) {
matheusfillipef43dd962021-05-13 23:27:01 -0300306 $url = $BASE_URL . "?type=password_change&token=" . $token;
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100307 send_recovery_email($address, $smtp, $url, $user);
matheusfillipef43dd962021-05-13 23:27:01 -0300308 unset($_SESSION['recover']);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300309 } else
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100310 send_confirmation_email($address, $smtp, $url, $user);
matheusfillipeabd513e2021-05-11 03:29:11 -0300311 unset($_SESSION['resend']);
312 unset($_SESSION['token']);
Marc Kupietz0375b7e2023-03-04 18:13:26 +0100313 # unset($_SESSION['email']);
314 } else {
315 echo "<h2>A second email has already been sent to " . $_SESSION['email'] .".</h2>";
matheusfillipeabd513e2021-05-11 03:29:11 -0300316 }
317 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300318
matheusfillipe47cf90b2021-05-13 03:36:21 -0300319 case "recover":
Matheus Fillipe301684b2021-05-14 09:09:43 +0300320 recover_form();
matheusfillipe47cf90b2021-05-13 03:36:21 -0300321 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300322
323 case "password_change":
324 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300325 $token = $_GET["token"];
326 $email = redis_get($token);
327 $_SESSION["email_change"] = $email;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300328 $_SESSION["token"] = $token;
329 if ($email && gettype($email) == "string") {
330 new_password_form();
matheusfillipef43dd962021-05-13 23:27:01 -0300331 } else {
332 include $TEMPLATE . "token_expired.htm";
333 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300334 break;
matheusfillipeabd513e2021-05-11 03:29:11 -0300335 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300336} else {
337 unset($_SESSION['captcha_token']);
338 register_page();
339}
340
matheusfillipef43dd962021-05-13 23:27:01 -0300341include $TEMPLATE . "bottom.htm";