blob: 1df4cc69e51fc47cae6b0f568b8389ac9c95e319 [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
matheusfillipef43dd962021-05-13 23:27:01 -030030function send_confirmation_email(string $mail, object $smtp, string $url)
31{
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,
38 "text" => str_replace("{{url}}", $url, $MAIL_TEMPLATE->text),
39 "html" => str_replace("{{url}}", $url, $MAIL_TEMPLATE->html)
40 ]);
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
113 if (!(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
matheusfillipef43dd962021-05-13 23:27:01 -0300121function approve_request($user)
122{
Matheus Fillipe301684b2021-05-14 09:09:43 +0300123 include 'config.php';
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300124 $token = generateRandomString();
matheusfillipeabd513e2021-05-11 03:29:11 -0300125 redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY);
126 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300127 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300128 $maillist = $pending->mails;
129 array_push($maillist, $user->email);
matheusfillipef43dd962021-05-13 23:27:01 -0300130 } else
matheusfillipeabd513e2021-05-11 03:29:11 -0300131 $maillist = [$user->email];
matheusfillipef43dd962021-05-13 23:27:01 -0300132 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300133
matheusfillipef43dd962021-05-13 23:27:01 -0300134 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300135 if (in_array(explode("@", $user->email)[1], $MAIL_HOST_DIRECT_FALLBACK))
136 $smtp = $FALLBACK_SMTP;
137 else
138 $smtp = $SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300139 send_confirmation_email($user->email, $smtp, $url);
matheusfillipeabd513e2021-05-11 03:29:11 -0300140 $_SESSION['resend'] = generateRandomString(12);
141 $_SESSION['token'] = $token;
142 $_SESSION['email'] = $user->email;
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300143 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300144 include $TEMPLATE . "confirm_your_email.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300145}
146
Matheus Fillipe301684b2021-05-14 09:09:43 +0300147function recover_form($error = null)
148{
149 $TEMPLATE = template_path();
150 include 'config.php';
151 $_SESSION["captcha_token"] = generateRandomString(12);
152 if ($error)
153 include $TEMPLATE . 'error.htm';
154 include $TEMPLATE . "recover_email_form.htm";
155 reload_captcha_script();
156}
157
158function new_password_form($error = null)
159{
160 $TEMPLATE = template_path();
161 if ($error)
162 include $TEMPLATE . 'error.htm';
163 include $TEMPLATE . "recover_new_password_form.htm";
164}
165
matheusfillipeabd513e2021-05-11 03:29:11 -0300166
167// PAGE
matheusfillipef43dd962021-05-13 23:27:01 -0300168include $TEMPLATE . "header.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300169
170if ($_SERVER["REQUEST_METHOD"] == "POST") {
matheusfillipeabd513e2021-05-11 03:29:11 -0300171 if (isset($_POST['type'])) {
172 switch ($_POST['type']) {
173 case "register":
Marc Kupietza19f3072023-02-25 14:16:40 +0100174 $user = new User($_POST["username"], $_POST["name"], $_POST["last_name"], $_POST["email"], $_POST["password"], $_POST["organization"]);
matheusfillipef43dd962021-05-13 23:27:01 -0300175 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
176 include $TEMPLATE . "registration_limit.htm";
177 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300178 $error = verify_request($user);
179 if ($error)
180 register_page($error);
181 else
182 approve_request($user);
183 }
184 break;
matheusfillipe47cf90b2021-05-13 03:36:21 -0300185 case "recover":
matheusfillipef43dd962021-05-13 23:27:01 -0300186 $TEMPLATE = template_path();
187 unset($_SESSION['captcha_token']);
188 include $TEMPLATE . 'strings.php';
189
190 $email = $_POST["email"];
191 if (!ldap_mail_count($email)) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300192 unset($_POST['email']);
matheusfillipef43dd962021-05-13 23:27:01 -0300193 $error = $error . $STRINGS->recover_email_not_registered;
194 }
195
196 if (!(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) {
197 $error = $error . $STRINGS->wrong_captcha;
198 }
199
200 unset($_SESSION["captcha"]);
201 if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) {
202 include $TEMPLATE . "registration_limit.htm";
203 } else {
204 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300205 recover_form($error);
matheusfillipef43dd962021-05-13 23:27:01 -0300206 } else {
207 include $TEMPLATE . 'strings.php';
208 $token = generateRandomString();
209 redis_set($token, $email, $MAIL_CONFIRMATION_AWAIT_DELAY);
210
211 $url = $BASE_URL . "?type=password_change&token=" . $token;
212 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
213 $smtp = $FALLBACK_SMTP;
214 else
215 $smtp = $SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300216 $_SESSION['resend'] = generateRandomString(12);
217 $_SESSION['token'] = $token;
218 $_SESSION['email'] = $email;
219 $_SESSION['recover'] = $email;
220 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300221 send_recovery_email($email, $smtp, $url);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300222 include $TEMPLATE . "confirm_your_email.htm";
matheusfillipef43dd962021-05-13 23:27:01 -0300223 }
224 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300225 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300226
227 case "password_change":
matheusfillipef43dd962021-05-13 23:27:01 -0300228 $password = $_POST['password'];
229 $error = validate_password($password);
230 if ($error) {
Matheus Fillipe301684b2021-05-14 09:09:43 +0300231 new_password_form($error);
232 } else {
233 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300234 include $TEMPLATE . "recover_success.htm";
235 include $TEMPLATE . "email.php";
matheusfillipef43dd962021-05-13 23:27:01 -0300236 $email = $_SESSION["email_change"];
237 if (change_password($email, $password)) {
238 if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK))
239 $smtp = $FALLBACK_SMTP;
240 else
241 $smtp = $SMTP;
242 send_mail($email, $smtp, $PASSWORD_CHANGED_EMAIL_TEMPLATE);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300243 } else {
matheusfillipef43dd962021-05-13 23:27:01 -0300244 include $TEMPLATE . "strings.php";
245 echo $STRINGS->change_password_ldap_error;
246 }
Matheus Fillipe301684b2021-05-14 09:09:43 +0300247 unset($_SESSION["email_change"]);
248 redis_delete($_SESSION['token']);
matheusfillipef43dd962021-05-13 23:27:01 -0300249 }
250 break;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300251 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300252 }
253} elseif (isset($_GET['type'])) {
254 switch ($_GET['type']) {
255 case "confirmation":
matheusfillipef43dd962021-05-13 23:27:01 -0300256 if (!isset($_GET["token"])) {
matheusfillipe47cf90b2021-05-13 03:36:21 -0300257 echo $RUNTIME_ERROR->user_trying_invalid_get;
matheusfillipef43dd962021-05-13 23:27:01 -0300258 } else {
matheusfillipeabd513e2021-05-11 03:29:11 -0300259 $token = $_GET["token"];
260 $user = redis_get($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300261 if ($user && gettype($user) == "object") {
262 if (ldap_add_user($user)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300263 if ($REDIRECT_TO)
matheusfillipef43dd962021-05-13 23:27:01 -0300264 header("refresh:5;url=" . $REDIRECT_TO);
matheusfillipeabd513e2021-05-11 03:29:11 -0300265
266 $pending = redis_get("pending");
matheusfillipef43dd962021-05-13 23:27:01 -0300267 if ($pending) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300268 $maillist = $pending->mails;
matheusfillipef43dd962021-05-13 23:27:01 -0300269 if (in_array($user->email, $maillist)) {
matheusfillipeabd513e2021-05-11 03:29:11 -0300270 unset($maillist[array_search($user->email, $maillist)]);
matheusfillipef43dd962021-05-13 23:27:01 -0300271 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
matheusfillipeabd513e2021-05-11 03:29:11 -0300272 }
273 }
274 redis_inc_ipdata(getClientIP(), "register");
matheusfillipe47cf90b2021-05-13 03:36:21 -0300275 echo $STRINGS->email_confirmation;
Matheus Fillipe5ad0cd52022-05-05 06:10:41 +0300276 if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user);
matheusfillipef43dd962021-05-13 23:27:01 -0300277 include $TEMPLATE . "mail_confirmed.htm";
278 } else {
matheusfillipe47cf90b2021-05-13 03:36:21 -0300279 echo $STRINGS->email_confirmation;
matheusfillipef43dd962021-05-13 23:27:01 -0300280 include $TEMPLATE . "registration_error.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300281 }
282 redis_delete($token);
matheusfillipef43dd962021-05-13 23:27:01 -0300283 } else {
284 include $TEMPLATE . "token_expired.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300285 }
286 }
287 break;
288 case "resend":
matheusfillipef43dd962021-05-13 23:27:01 -0300289 if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) {
290 include $TEMPLATE . "resend_mail.htm";
matheusfillipeabd513e2021-05-11 03:29:11 -0300291 $token = $_SESSION['token'];
matheusfillipef43dd962021-05-13 23:27:01 -0300292 $url = $BASE_URL . "?type=confirmation&token=" . $token;
matheusfillipeabd513e2021-05-11 03:29:11 -0300293 $smtp = $FALLBACK_SMTP;
matheusfillipef43dd962021-05-13 23:27:01 -0300294 $address = $_SESSION["email"];
Matheus Fillipe301684b2021-05-14 09:09:43 +0300295 if (isset($_SESSION['recover'])) {
matheusfillipef43dd962021-05-13 23:27:01 -0300296 $url = $BASE_URL . "?type=password_change&token=" . $token;
297 send_recovery_email($address, $smtp, $url);
298 unset($_SESSION['recover']);
Matheus Fillipe301684b2021-05-14 09:09:43 +0300299 } else
matheusfillipef43dd962021-05-13 23:27:01 -0300300 send_confirmation_email($address, $smtp, $url);
matheusfillipeabd513e2021-05-11 03:29:11 -0300301 unset($_SESSION['resend']);
302 unset($_SESSION['token']);
303 unset($_SESSION['email']);
304 }
305 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300306
matheusfillipe47cf90b2021-05-13 03:36:21 -0300307 case "recover":
Matheus Fillipe301684b2021-05-14 09:09:43 +0300308 recover_form();
matheusfillipe47cf90b2021-05-13 03:36:21 -0300309 break;
matheusfillipef43dd962021-05-13 23:27:01 -0300310
311 case "password_change":
312 $TEMPLATE = template_path();
matheusfillipef43dd962021-05-13 23:27:01 -0300313 $token = $_GET["token"];
314 $email = redis_get($token);
315 $_SESSION["email_change"] = $email;
Matheus Fillipe301684b2021-05-14 09:09:43 +0300316 $_SESSION["token"] = $token;
317 if ($email && gettype($email) == "string") {
318 new_password_form();
matheusfillipef43dd962021-05-13 23:27:01 -0300319 } else {
320 include $TEMPLATE . "token_expired.htm";
321 }
matheusfillipe47cf90b2021-05-13 03:36:21 -0300322 break;
matheusfillipeabd513e2021-05-11 03:29:11 -0300323 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300324} else {
325 unset($_SESSION['captcha_token']);
326 register_page();
327}
328
matheusfillipef43dd962021-05-13 23:27:01 -0300329include $TEMPLATE . "bottom.htm";