| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:34:16 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * User registration | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This page ("register") handles user registration if the feature is enabled in the configuration. | 
					
						
							|  |  |  |  * It accepts a POST request with a username and password, attempts to register the user, | 
					
						
							|  |  |  |  * and redirects to the login page on success or displays an error message on failure. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  | // registration is allowed, go on
 | 
					
						
							| 
									
										
										
										
											2025-02-06 11:18:19 +00:00
										 |  |  | if ($config['registration_enabled'] == true) { | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // connect to database
 | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |         $dbWeb = connectDB($config); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  |         if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |             require_once '../app/classes/validator.php'; | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |             $validator = new Validator($_POST); | 
					
						
							|  |  |  |             $rules = [ | 
					
						
							|  |  |  |                 'username' => [ | 
					
						
							|  |  |  |                     'required' => true, | 
					
						
							|  |  |  |                     'min' => 3, | 
					
						
							|  |  |  |                     'max' => 20 | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 'password' => [ | 
					
						
							|  |  |  |                     'required' => true, | 
					
						
							|  |  |  |                     'min' => 8, | 
					
						
							|  |  |  |                     'max' => 100 | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 'confirm_password' => [ | 
					
						
							|  |  |  |                     'required' => true, | 
					
						
							|  |  |  |                     'matches' => 'password' | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2024-09-13 10:49:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |             if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                 $username = $_POST['username']; | 
					
						
							|  |  |  |                 $password = $_POST['password']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // registering
 | 
					
						
							|  |  |  |                 $result = $userObject->register($username, $password); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // redirect to login
 | 
					
						
							|  |  |  |                 if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('NOTICE', 'DEFAULT', "Registration successful. You can log in now."); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |                     header('Location: ' . htmlspecialchars($app_root)); | 
					
						
							|  |  |  |                     exit(); | 
					
						
							|  |  |  |                 // registration fail, redirect to login
 | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('ERROR', 'DEFAULT', "Registration failed. $result"); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |                     header('Location: ' . htmlspecialchars($app_root)); | 
					
						
							|  |  |  |                     exit(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:25:17 +00:00
										 |  |  |                 header('Location: ' . htmlspecialchars($app_root . '?page=register')); | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  |                 exit(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  |     } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |         Feedback::flash('ERROR', 'DEFAULT', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |     // Get any new messages
 | 
					
						
							|  |  |  |     include '../app/includes/messages.php'; | 
					
						
							|  |  |  |     include '../app/includes/messages-show.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Load the template
 | 
					
						
							| 
									
										
										
										
											2024-08-12 11:12:24 +00:00
										 |  |  |     include '../app/templates/form-register.php'; | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // registration disabled
 | 
					
						
							|  |  |  | } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |     echo Feedback::render('NOTICE', 'DEFAULT', 'Registration is disabled', false); | 
					
						
							| 
									
										
										
										
											2024-08-01 08:12:54 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ?>
 |