matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php |
| 2 | require_once 'vendor/autoload.php'; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 3 | include 'config.php'; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 4 | include_once 'redis.php'; |
| 5 | include_once 'utils.php'; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 6 | include_once 'mail.php'; |
| 7 | include_once 'ldap.php'; |
| 8 | include_once 'validators.php'; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 9 | include_once 'User.php'; |
| 10 | require __DIR__ . '/vendor/autoload.php'; |
| 11 | use Monolog\Level; |
| 12 | use Monolog\Logger as Logger; |
| 13 | use Monolog\Handler\StreamHandler; |
| 14 | use Monolog\Handler\RotatingFileHandler; |
| 15 | use \User as User; |
Marc Kupietz | 3f6a598 | 2023-03-06 07:04:59 +0100 | [diff] [blame] | 16 | include $TEMPLATE . "strings.php"; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 17 | $log = new Logger('signup'); |
| 18 | $log->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/signup.log', 0, Logger::DEBUG)); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 19 | |
| 20 | if (!$DEBUG) error_reporting(0); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 21 | else error_reporting(1); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 22 | session_start(); |
| 23 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 24 | use Gregwar\Captcha\PhraseBuilder; |
| 25 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 26 | $URI = array_slice(explode("/", explode('?', $_SERVER['REQUEST_URI'], 2)[0]), -1)[0]; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 27 | if (strlen($URI) == 2) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 28 | $GLOBALS["cc"] = $URI; |
| 29 | $_SESSION["cc"] = $URI; |
| 30 | } |
| 31 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 32 | if (isset($_GET["lang"])) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 33 | $GLOBALS["cc"] = $_GET["lang"]; |
| 34 | $_SESSION["cc"] = $_GET["lang"]; |
| 35 | } |
| 36 | |
| 37 | $TEMPLATE = template_path(); |
| 38 | |
| 39 | |
Marc Kupietz | 3f6a598 | 2023-03-06 07:04:59 +0100 | [diff] [blame] | 40 | function send_welcome_email(User $user) |
| 41 | { |
| 42 | global $SMTP, $SERVICE_ACRONYM, $WELCOME_TEMPLATE, $TEMPLATE; |
| 43 | $TEMPLATE = template_path(); |
| 44 | include $TEMPLATE . "email.php"; |
| 45 | $url = ""; |
| 46 | |
| 47 | send_mail($user->email, $SMTP, (object) [ |
| 48 | "subject" => $WELCOME_TEMPLATE->subject, |
| 49 | "text" => replace_all_user_variables($WELCOME_TEMPLATE->text, $user, $url), |
| 50 | "html" => replace_all_user_variables($WELCOME_TEMPLATE->html, $user, $url) |
| 51 | ]); |
| 52 | } |
| 53 | |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 54 | function send_confirmation_email(string $mail, object $smtp, string $url, User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 55 | { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 56 | include 'config.php'; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 57 | $TEMPLATE = template_path(); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 58 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 59 | |
| 60 | send_mail($mail, $smtp, (object) [ |
| 61 | "subject" => $MAIL_TEMPLATE->subject, |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 62 | "text" => replace_all_user_variables($MAIL_TEMPLATE->text, $user, $url), |
| 63 | "html" => replace_all_user_variables($MAIL_TEMPLATE->html, $user, $url) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 64 | ]); |
| 65 | } |
| 66 | |
| 67 | function send_recovery_email(string $mail, object $smtp, string $url) |
| 68 | { |
| 69 | include 'config.php'; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 70 | $TEMPLATE = template_path(); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 71 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 72 | |
| 73 | send_mail($mail, $smtp, (object) [ |
| 74 | "subject" => $RECOVERY_EMAIL_TEMPLATE->subject, |
| 75 | "text" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->text), |
| 76 | "html" => str_replace("{{url}}", $url, $RECOVERY_EMAIL_TEMPLATE->html) |
| 77 | ]); |
| 78 | } |
| 79 | |
| 80 | function reload_captcha_script() |
| 81 | { |
| 82 | include 'config.php'; |
Marc Kupietz | 3f6a598 | 2023-03-06 07:04:59 +0100 | [diff] [blame] | 83 | |
| 84 | if ($CAPTCHA_LENGTH > 0) { |
| 85 | $TEMPLATE = template_path(); |
| 86 | include $TEMPLATE . "strings.php"; |
| 87 | echo ' |
| 88 | <script> |
| 89 | const reload_captcha = async (e) => { |
| 90 | var cont = document.getElementById("reload_captcha"); |
| 91 | cont.innerHTML = "<div class=\'spinner-border text-info\' role=\'status\'><span class=\'sr-only\'>' . $STRINGS->reloading_captcha . '</span></div>"; |
| 92 | var img = document.getElementById("captcha") |
| 93 | var url = "' . $BASE_URL . '/captcha.php?token=' . $_SESSION["captcha_token"] . '" |
| 94 | await fetch(url, { cache: "reload", mode: "no-cors" }) |
| 95 | .then(() => { |
| 96 | img.src = url+"&t=" + new Date().getTime(); |
| 97 | setTimeout( () => { |
| 98 | cont.innerHTML = "<button id=\'reload\' class=\'btn btn-outline-info\' type=\'button\'> <span class=\'glyphicon glyphicon-refresh\' aria-hidden=\'true\'></span></button>"; |
| 99 | bindButton() |
| 100 | }, 500); |
| 101 | }) |
| 102 | } |
| 103 | function bindButton(){ |
| 104 | var button = document.getElementById("reload"); |
| 105 | button.addEventListener("click", reload_captcha) |
| 106 | } |
| 107 | bindButton() |
| 108 | </script> |
| 109 | '; |
| 110 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 111 | } |
| 112 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 113 | function register_page($error = false) |
| 114 | { |
| 115 | $TEMPLATE = template_path(); |
| 116 | include 'config.php'; |
| 117 | if ($error) |
| 118 | include $TEMPLATE . 'error.htm'; |
| 119 | $_SESSION["captcha_token"] = generateRandomString(12); |
| 120 | include $TEMPLATE . "register.htm"; |
| 121 | reload_captcha_script(); |
| 122 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 123 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 124 | |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 125 | function verify_request(User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 126 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 127 | $TEMPLATE = template_path(); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 128 | unset($_SESSION['captcha_token']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 129 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 130 | $password = $_POST["password"]; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 131 | $error = ""; |
| 132 | |
| 133 | $error .= validate_username($user->user_name); |
| 134 | $error .= validate_name($user->name, $FIRST_NAME_VALIDATION_ERROR); |
| 135 | $error .= validate_name($user->last_name, $LAST_NAME_VALIDATION_ERROR); |
| 136 | $error .= validate_email($user->email); |
| 137 | $error .= validate_password($password); |
| 138 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 139 | |
Marc Kupietz | d2dfa00 | 2023-03-04 14:59:16 +0100 | [diff] [blame] | 140 | if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 141 | $error = $error . $STRINGS->wrong_captcha; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 142 | } |
| 143 | unset($_SESSION["captcha"]); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 144 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 145 | return $error; |
| 146 | } |
| 147 | |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 148 | function backup_user_in_session($user) |
| 149 | { |
| 150 | $_SESSION['username'] = $user->user_name; |
| 151 | $_SESSION['first_name'] = $user->first_name; |
| 152 | $_SESSION['last_name'] = $user->last_name; |
| 153 | $_SESSION['email'] = $user->email; |
| 154 | $_SESSION['organization'] = $user->organization; |
| 155 | } |
| 156 | |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 157 | function approve_request(User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 158 | { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 159 | include 'config.php'; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 160 | global $log; |
| 161 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 162 | $token = generateRandomString(); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 163 | redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY); |
| 164 | $pending = redis_get("pending"); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 165 | if ($pending) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 166 | $maillist = $pending->mails; |
| 167 | array_push($maillist, $user->email); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 168 | } else |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 169 | $maillist = [$user->email]; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 170 | redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 171 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 172 | $url = $BASE_URL . "?type=confirmation&token=" . $token; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 173 | if (in_array(explode("@", $user->email)[1], $MAIL_HOST_DIRECT_FALLBACK)) |
| 174 | $smtp = $FALLBACK_SMTP; |
| 175 | else |
| 176 | $smtp = $SMTP; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 177 | send_confirmation_email($user->email, $smtp, $url, $user); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 178 | $_SESSION['resend'] = generateRandomString(12); |
| 179 | $_SESSION['token'] = $token; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 180 | backup_user_in_session($user); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 181 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 182 | include $TEMPLATE . "confirm_your_email.htm"; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 183 | $log->info("Registration process started for " . user_to_string($user)); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 184 | } |
| 185 | |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 186 | function recover_form($error = null) |
| 187 | { |
| 188 | $TEMPLATE = template_path(); |
| 189 | include 'config.php'; |
| 190 | $_SESSION["captcha_token"] = generateRandomString(12); |
| 191 | if ($error) |
| 192 | include $TEMPLATE . 'error.htm'; |
| 193 | include $TEMPLATE . "recover_email_form.htm"; |
| 194 | reload_captcha_script(); |
| 195 | } |
| 196 | |
| 197 | function new_password_form($error = null) |
| 198 | { |
| 199 | $TEMPLATE = template_path(); |
| 200 | if ($error) |
| 201 | include $TEMPLATE . 'error.htm'; |
| 202 | include $TEMPLATE . "recover_new_password_form.htm"; |
| 203 | } |
| 204 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 205 | |
| 206 | // PAGE |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 207 | include $TEMPLATE . "header.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 208 | |
| 209 | if ($_SERVER["REQUEST_METHOD"] == "POST") { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 210 | if (isset($_POST['type'])) { |
| 211 | switch ($_POST['type']) { |
| 212 | case "register": |
Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 213 | $user = new User($_POST["username"], $_POST["name"], $_POST["last_name"], $_POST["email"], $_POST["password"], $_POST["organization"]); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 214 | if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) { |
| 215 | include $TEMPLATE . "registration_limit.htm"; |
| 216 | } else { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 217 | $error = verify_request($user); |
| 218 | if ($error) |
| 219 | register_page($error); |
| 220 | else |
| 221 | approve_request($user); |
| 222 | } |
| 223 | break; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 224 | case "recover": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 225 | $TEMPLATE = template_path(); |
| 226 | unset($_SESSION['captcha_token']); |
| 227 | include $TEMPLATE . 'strings.php'; |
| 228 | |
| 229 | $email = $_POST["email"]; |
| 230 | if (!ldap_mail_count($email)) { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 231 | unset($_POST['email']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 232 | $error = $error . $STRINGS->recover_email_not_registered; |
| 233 | } |
| 234 | |
Marc Kupietz | d2dfa00 | 2023-03-04 14:59:16 +0100 | [diff] [blame] | 235 | if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 236 | $error = $error . $STRINGS->wrong_captcha; |
| 237 | } |
| 238 | |
| 239 | unset($_SESSION["captcha"]); |
| 240 | if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) { |
| 241 | include $TEMPLATE . "registration_limit.htm"; |
| 242 | } else { |
| 243 | if ($error) { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 244 | recover_form($error); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 245 | } else { |
| 246 | include $TEMPLATE . 'strings.php'; |
| 247 | $token = generateRandomString(); |
| 248 | redis_set($token, $email, $MAIL_CONFIRMATION_AWAIT_DELAY); |
| 249 | |
| 250 | $url = $BASE_URL . "?type=password_change&token=" . $token; |
| 251 | if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK)) |
| 252 | $smtp = $FALLBACK_SMTP; |
| 253 | else |
| 254 | $smtp = $SMTP; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 255 | $_SESSION['resend'] = generateRandomString(12); |
| 256 | $_SESSION['token'] = $token; |
| 257 | $_SESSION['email'] = $email; |
| 258 | $_SESSION['recover'] = $email; |
| 259 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 260 | send_recovery_email($email, $smtp, $url); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 261 | include $TEMPLATE . "confirm_your_email.htm"; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 262 | $log->info("Password recovery email sent to " . $email); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 263 | } |
| 264 | } |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 265 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 266 | |
| 267 | case "password_change": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 268 | $password = $_POST['password']; |
| 269 | $error = validate_password($password); |
| 270 | if ($error) { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 271 | new_password_form($error); |
| 272 | } else { |
| 273 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 274 | include $TEMPLATE . "recover_success.htm"; |
| 275 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 276 | $email = $_SESSION["email_change"]; |
| 277 | if (change_password($email, $password)) { |
| 278 | if (in_array(explode("@", $email)[1], $MAIL_HOST_DIRECT_FALLBACK)) |
| 279 | $smtp = $FALLBACK_SMTP; |
| 280 | else |
| 281 | $smtp = $SMTP; |
| 282 | send_mail($email, $smtp, $PASSWORD_CHANGED_EMAIL_TEMPLATE); |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 283 | $log->info("Password changed for " . $email); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 284 | } else { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 285 | include $TEMPLATE . "strings.php"; |
| 286 | echo $STRINGS->change_password_ldap_error; |
| 287 | } |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 288 | unset($_SESSION["email_change"]); |
| 289 | redis_delete($_SESSION['token']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 290 | } |
| 291 | break; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 292 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 293 | } |
| 294 | } elseif (isset($_GET['type'])) { |
| 295 | switch ($_GET['type']) { |
| 296 | case "confirmation": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 297 | if (!isset($_GET["token"])) { |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 298 | echo $RUNTIME_ERROR->user_trying_invalid_get; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 299 | } else { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 300 | $token = $_GET["token"]; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 301 | $user = redis_get_user($token); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 302 | if ($user && gettype($user) == "object") { |
| 303 | if (ldap_add_user($user)) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 304 | if ($REDIRECT_TO) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 305 | header("refresh:5;url=" . $REDIRECT_TO); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 306 | |
| 307 | $pending = redis_get("pending"); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 308 | if ($pending) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 309 | $maillist = $pending->mails; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 310 | if (in_array($user->email, $maillist)) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 311 | unset($maillist[array_search($user->email, $maillist)]); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 312 | redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | redis_inc_ipdata(getClientIP(), "register"); |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 316 | $log->info("User registered: " . user_to_string($user)); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 317 | echo $STRINGS->email_confirmation; |
Matheus Fillipe | 5ad0cd5 | 2022-05-05 06:10:41 +0300 | [diff] [blame] | 318 | if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 319 | include $TEMPLATE . "mail_confirmed.htm"; |
| 320 | } else { |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 321 | $log->error("User registration failed for: " . user_to_string($user)); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 322 | echo $STRINGS->email_confirmation; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 323 | include $TEMPLATE . "registration_error.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 324 | } |
| 325 | redis_delete($token); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 326 | } else { |
| 327 | include $TEMPLATE . "token_expired.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | break; |
| 331 | case "resend": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 332 | if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) { |
| 333 | include $TEMPLATE . "resend_mail.htm"; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 334 | $user = new User($_SESSION["username"], $_SESSION["first_name"], $_SESSION["last_name"], $_SESSION["email"], "", $_SESSION["organization"]); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 335 | $token = $_SESSION['token']; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 336 | $url = $BASE_URL . "?type=confirmation&token=" . $token; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 337 | $smtp = $FALLBACK_SMTP; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 338 | $address = $_SESSION["email"]; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 339 | if (isset($_SESSION['recover'])) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 340 | $url = $BASE_URL . "?type=password_change&token=" . $token; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 341 | send_recovery_email($address, $smtp, $url, $user); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 342 | unset($_SESSION['recover']); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 343 | } else |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 344 | send_confirmation_email($address, $smtp, $url, $user); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 345 | unset($_SESSION['resend']); |
| 346 | unset($_SESSION['token']); |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 347 | # unset($_SESSION['email']); |
| 348 | } else { |
| 349 | echo "<h2>A second email has already been sent to " . $_SESSION['email'] .".</h2>"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 350 | } |
| 351 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 352 | |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 353 | case "recover": |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 354 | recover_form(); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 355 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 356 | |
| 357 | case "password_change": |
| 358 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 359 | $token = $_GET["token"]; |
| 360 | $email = redis_get($token); |
| 361 | $_SESSION["email_change"] = $email; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 362 | $_SESSION["token"] = $token; |
| 363 | if ($email && gettype($email) == "string") { |
| 364 | new_password_form(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 365 | } else { |
| 366 | include $TEMPLATE . "token_expired.htm"; |
| 367 | } |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 368 | break; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 369 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 370 | } else { |
| 371 | unset($_SESSION['captcha_token']); |
| 372 | register_page(); |
| 373 | } |
| 374 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 375 | include $TEMPLATE . "bottom.htm"; |