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; |
| 16 | $log = new Logger('signup'); |
| 17 | $log->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/signup.log', 0, Logger::DEBUG)); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 18 | |
| 19 | if (!$DEBUG) error_reporting(0); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 20 | else error_reporting(1); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 21 | session_start(); |
| 22 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 23 | use Gregwar\Captcha\PhraseBuilder; |
| 24 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 25 | $URI = array_slice(explode("/", explode('?', $_SERVER['REQUEST_URI'], 2)[0]), -1)[0]; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 26 | if (strlen($URI) == 2) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 27 | $GLOBALS["cc"] = $URI; |
| 28 | $_SESSION["cc"] = $URI; |
| 29 | } |
| 30 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 31 | if (isset($_GET["lang"])) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 32 | $GLOBALS["cc"] = $_GET["lang"]; |
| 33 | $_SESSION["cc"] = $_GET["lang"]; |
| 34 | } |
| 35 | |
| 36 | $TEMPLATE = template_path(); |
| 37 | |
| 38 | |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 39 | function send_confirmation_email(string $mail, object $smtp, string $url, User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 40 | { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 41 | include 'config.php'; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 42 | $TEMPLATE = template_path(); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 43 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 44 | |
| 45 | send_mail($mail, $smtp, (object) [ |
| 46 | "subject" => $MAIL_TEMPLATE->subject, |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 47 | "text" => replace_all_user_variables($MAIL_TEMPLATE->text, $user, $url), |
| 48 | "html" => replace_all_user_variables($MAIL_TEMPLATE->html, $user, $url) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 49 | ]); |
| 50 | } |
| 51 | |
| 52 | function send_recovery_email(string $mail, object $smtp, string $url) |
| 53 | { |
| 54 | include 'config.php'; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 55 | $TEMPLATE = template_path(); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 56 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 57 | |
| 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 | |
| 65 | function reload_captcha_script() |
| 66 | { |
| 67 | include 'config.php'; |
| 68 | $TEMPLATE = template_path(); |
| 69 | include $TEMPLATE . "strings.php"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 70 | echo ' |
| 71 | <script> |
| 72 | const reload_captcha = async (e) => { |
| 73 | var cont = document.getElementById("reload_captcha"); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 74 | cont.innerHTML = "<div class=\'spinner-border text-info\' role=\'status\'><span class=\'sr-only\'>' . $STRINGS->reloading_captcha . '</span></div>"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 75 | var img = document.getElementById("captcha") |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 76 | var url = "' . $BASE_URL . '/captcha.php?token=' . $_SESSION["captcha_token"] . '" |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 77 | 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 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 95 | function 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 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 105 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 106 | |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 107 | function verify_request(User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 108 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 109 | $TEMPLATE = template_path(); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 110 | unset($_SESSION['captcha_token']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 111 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 112 | $password = $_POST["password"]; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 113 | $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 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 121 | |
Marc Kupietz | d2dfa00 | 2023-03-04 14:59:16 +0100 | [diff] [blame] | 122 | if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 123 | $error = $error . $STRINGS->wrong_captcha; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 124 | } |
| 125 | unset($_SESSION["captcha"]); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 126 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 127 | return $error; |
| 128 | } |
| 129 | |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 130 | function 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 Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 139 | function approve_request(User $user) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 140 | { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 141 | include 'config.php'; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 142 | global $log; |
| 143 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 144 | $token = generateRandomString(); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 145 | redis_set($token, $user, $MAIL_CONFIRMATION_AWAIT_DELAY); |
| 146 | $pending = redis_get("pending"); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 147 | if ($pending) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 148 | $maillist = $pending->mails; |
| 149 | array_push($maillist, $user->email); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 150 | } else |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 151 | $maillist = [$user->email]; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 152 | redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 153 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 154 | $url = $BASE_URL . "?type=confirmation&token=" . $token; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 155 | if (in_array(explode("@", $user->email)[1], $MAIL_HOST_DIRECT_FALLBACK)) |
| 156 | $smtp = $FALLBACK_SMTP; |
| 157 | else |
| 158 | $smtp = $SMTP; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 159 | send_confirmation_email($user->email, $smtp, $url, $user); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 160 | $_SESSION['resend'] = generateRandomString(12); |
| 161 | $_SESSION['token'] = $token; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 162 | backup_user_in_session($user); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 163 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 164 | include $TEMPLATE . "confirm_your_email.htm"; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 165 | $log->info("Registration process started for " . user_to_string($user)); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 166 | } |
| 167 | |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 168 | function 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 | |
| 179 | function 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 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 187 | |
| 188 | // PAGE |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 189 | include $TEMPLATE . "header.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 190 | |
| 191 | if ($_SERVER["REQUEST_METHOD"] == "POST") { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 192 | if (isset($_POST['type'])) { |
| 193 | switch ($_POST['type']) { |
| 194 | case "register": |
Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 195 | $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] | 196 | if (redis_inc_ipdata(getClientIP(), "register", true) > $HOURLY_REGISTRATIONS) { |
| 197 | include $TEMPLATE . "registration_limit.htm"; |
| 198 | } else { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 199 | $error = verify_request($user); |
| 200 | if ($error) |
| 201 | register_page($error); |
| 202 | else |
| 203 | approve_request($user); |
| 204 | } |
| 205 | break; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 206 | case "recover": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 207 | $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 Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 213 | unset($_POST['email']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 214 | $error = $error . $STRINGS->recover_email_not_registered; |
| 215 | } |
| 216 | |
Marc Kupietz | d2dfa00 | 2023-03-04 14:59:16 +0100 | [diff] [blame] | 217 | if ($CAPTCHA_LENGTH > 0 && !(isset($_SESSION['captcha']) && PhraseBuilder::comparePhrases($_SESSION['captcha'], $_POST['captcha']))) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 218 | $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 Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 226 | recover_form($error); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 227 | } 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; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 237 | $_SESSION['resend'] = generateRandomString(12); |
| 238 | $_SESSION['token'] = $token; |
| 239 | $_SESSION['email'] = $email; |
| 240 | $_SESSION['recover'] = $email; |
| 241 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 242 | send_recovery_email($email, $smtp, $url); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 243 | include $TEMPLATE . "confirm_your_email.htm"; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 244 | $log->info("Password recovery email sent to " . $email); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 245 | } |
| 246 | } |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 247 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 248 | |
| 249 | case "password_change": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 250 | $password = $_POST['password']; |
| 251 | $error = validate_password($password); |
| 252 | if ($error) { |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 253 | new_password_form($error); |
| 254 | } else { |
| 255 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 256 | include $TEMPLATE . "recover_success.htm"; |
| 257 | include $TEMPLATE . "email.php"; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 258 | $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 Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 265 | $log->info("Password changed for " . $email); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 266 | } else { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 267 | include $TEMPLATE . "strings.php"; |
| 268 | echo $STRINGS->change_password_ldap_error; |
| 269 | } |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 270 | unset($_SESSION["email_change"]); |
| 271 | redis_delete($_SESSION['token']); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 272 | } |
| 273 | break; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 274 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 275 | } |
| 276 | } elseif (isset($_GET['type'])) { |
| 277 | switch ($_GET['type']) { |
| 278 | case "confirmation": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 279 | if (!isset($_GET["token"])) { |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 280 | echo $RUNTIME_ERROR->user_trying_invalid_get; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 281 | } else { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 282 | $token = $_GET["token"]; |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 283 | $user = redis_get_user($token); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 284 | if ($user && gettype($user) == "object") { |
| 285 | if (ldap_add_user($user)) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 286 | if ($REDIRECT_TO) |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 287 | header("refresh:5;url=" . $REDIRECT_TO); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 288 | |
| 289 | $pending = redis_get("pending"); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 290 | if ($pending) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 291 | $maillist = $pending->mails; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 292 | if (in_array($user->email, $maillist)) { |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 293 | unset($maillist[array_search($user->email, $maillist)]); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 294 | redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | redis_inc_ipdata(getClientIP(), "register"); |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 298 | $log->info("User registered: " . user_to_string($user)); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 299 | echo $STRINGS->email_confirmation; |
Matheus Fillipe | 5ad0cd5 | 2022-05-05 06:10:41 +0300 | [diff] [blame] | 300 | if (isset($POST_REGISTER_HOOK)) $POST_REGISTER_HOOK($user); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 301 | include $TEMPLATE . "mail_confirmed.htm"; |
| 302 | } else { |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame^] | 303 | $log->error("User registration failed for: " . user_to_string($user)); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 304 | echo $STRINGS->email_confirmation; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 305 | include $TEMPLATE . "registration_error.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 306 | } |
| 307 | redis_delete($token); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 308 | } else { |
| 309 | include $TEMPLATE . "token_expired.htm"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | break; |
| 313 | case "resend": |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 314 | if (isset($_GET['token']) && isset($_SESSION['resend']) && $_GET['token'] == $_SESSION['resend']) { |
| 315 | include $TEMPLATE . "resend_mail.htm"; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 316 | $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] | 317 | $token = $_SESSION['token']; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 318 | $url = $BASE_URL . "?type=confirmation&token=" . $token; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 319 | $smtp = $FALLBACK_SMTP; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 320 | $address = $_SESSION["email"]; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 321 | if (isset($_SESSION['recover'])) { |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 322 | $url = $BASE_URL . "?type=password_change&token=" . $token; |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 323 | send_recovery_email($address, $smtp, $url, $user); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 324 | unset($_SESSION['recover']); |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 325 | } else |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 326 | send_confirmation_email($address, $smtp, $url, $user); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 327 | unset($_SESSION['resend']); |
| 328 | unset($_SESSION['token']); |
Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 329 | # unset($_SESSION['email']); |
| 330 | } else { |
| 331 | echo "<h2>A second email has already been sent to " . $_SESSION['email'] .".</h2>"; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 332 | } |
| 333 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 334 | |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 335 | case "recover": |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 336 | recover_form(); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 337 | break; |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 338 | |
| 339 | case "password_change": |
| 340 | $TEMPLATE = template_path(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 341 | $token = $_GET["token"]; |
| 342 | $email = redis_get($token); |
| 343 | $_SESSION["email_change"] = $email; |
Matheus Fillipe | 301684b | 2021-05-14 09:09:43 +0300 | [diff] [blame] | 344 | $_SESSION["token"] = $token; |
| 345 | if ($email && gettype($email) == "string") { |
| 346 | new_password_form(); |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 347 | } else { |
| 348 | include $TEMPLATE . "token_expired.htm"; |
| 349 | } |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 350 | break; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 351 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 352 | } else { |
| 353 | unset($_SESSION['captcha_token']); |
| 354 | register_page(); |
| 355 | } |
| 356 | |
matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 357 | include $TEMPLATE . "bottom.htm"; |