| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Check if user has any of the required rights
 | 
					
						
							|  |  |  | if (!($userObject->hasRight($user_id, 'superuser') || | 
					
						
							|  |  |  |       $userObject->hasRight($user_id, 'edit whitelist') || | 
					
						
							|  |  |  |       $userObject->hasRight($user_id, 'edit blacklist') || | 
					
						
							|  |  |  |       $userObject->hasRight($user_id, 'edit ratelimiting'))) { | 
					
						
							|  |  |  |     include '../app/templates/error-unauthorized.php'; | 
					
						
							|  |  |  |     exit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  | if (!isset($currentUser)) { | 
					
						
							|  |  |  |     include '../app/templates/error-unauthorized.php'; | 
					
						
							|  |  |  |     exit; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Get current section
 | 
					
						
							|  |  |  | $section = isset($_POST['section']) ? $_POST['section'] : (isset($_GET['section']) ? $_GET['section'] : 'whitelist'); | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  | // Initialize RateLimiter
 | 
					
						
							|  |  |  | require_once '../app/classes/ratelimiter.php'; | 
					
						
							|  |  |  | $rateLimiter = new RateLimiter($dbWeb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | // Handle form submissions
 | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |     require_once '../app/classes/validator.php'; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |     $action = $_POST['action']; | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |     $validator = new Validator($_POST); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         switch ($action) { | 
					
						
							|  |  |  |             case 'add_whitelist': | 
					
						
							|  |  |  |                 if (!$userObject->hasRight($user_id, 'superuser') && !$userObject->hasRight($user_id, 'edit whitelist')) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     throw new Exception('Unauthorized action'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							|  |  |  |                         'max' => 45 // IPv6 max length
 | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     'description' => [ | 
					
						
							|  |  |  |                         'max' => 255 | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                     $is_network = isset($_POST['is_network']) && $_POST['is_network'] === 'on'; | 
					
						
							|  |  |  |                     if (!$rateLimiter->addToWhitelist($_POST['ip_address'], $is_network, $_POST['description'] ?? '', $currentUser, $user_id)) { | 
					
						
							|  |  |  |                         throw new Exception('Failed to add IP to whitelist'); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_ADD_ERROR', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'remove_whitelist': | 
					
						
							|  |  |  |                 if (!$userObject->hasRight($user_id, 'superuser') && !$userObject->hasRight($user_id, 'edit whitelist')) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     throw new Exception('Unauthorized action'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							|  |  |  |                         'max' => 45 | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                     if (!$rateLimiter->removeFromWhitelist($_POST['ip_address'], $currentUser, $user_id)) { | 
					
						
							|  |  |  |                         throw new Exception('Failed to remove IP from whitelist'); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_REMOVE_ERROR', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'add_blacklist': | 
					
						
							|  |  |  |                 if (!$userObject->hasRight($user_id, 'superuser') && !$userObject->hasRight($user_id, 'edit blacklist')) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     throw new Exception('Unauthorized action'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							|  |  |  |                         'max' => 45 | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     'reason' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							|  |  |  |                         'max' => 255 | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                     'expiry_hours' => [ | 
					
						
							|  |  |  |                         'numeric' => true, | 
					
						
							|  |  |  |                         'min' => 0, | 
					
						
							|  |  |  |                         'max' => 8760 // 1 year in hours
 | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                     $is_network = isset($_POST['is_network']) && $_POST['is_network'] === 'on'; | 
					
						
							|  |  |  |                     $expiry_hours = !empty($_POST['expiry_hours']) ? (int)$_POST['expiry_hours'] : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (!$rateLimiter->addToBlacklist($_POST['ip_address'], $is_network, $_POST['reason'], $currentUser, $user_id, $expiry_hours)) { | 
					
						
							|  |  |  |                         throw new Exception('Failed to add IP to blacklist'); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_ADD_ERROR', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'remove_blacklist': | 
					
						
							|  |  |  |                 if (!$userObject->hasRight($user_id, 'superuser') && !$userObject->hasRight($user_id, 'edit blacklist')) { | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     throw new Exception('Unauthorized action'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							|  |  |  |                         'max' => 45 | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                     if (!$rateLimiter->removeFromBlacklist($_POST['ip_address'], $currentUser, $user_id)) { | 
					
						
							|  |  |  |                         throw new Exception('Failed to remove IP from blacklist'); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_REMOVE_ERROR', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 throw new Exception('Invalid action'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-02-16 08:18:26 +00:00
										 |  |  |         Feedback::flash('SECURITY', 'ERROR', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |     // Redirect back to the appropriate section
 | 
					
						
							|  |  |  |     header("Location: $app_root?page=security§ion=" . urlencode($section)); | 
					
						
							|  |  |  |     exit; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  | // Always show rate limit info message for rate limiting section
 | 
					
						
							|  |  |  | if ($section === 'ratelimit') { | 
					
						
							|  |  |  |     $messages[] = ['category' => 'SECURITY', 'key' => 'RATE_LIMIT_INFO']; | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | // Get current lists
 | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | $whitelisted = $rateLimiter->getWhitelistedIps(); | 
					
						
							|  |  |  | $blacklisted = $rateLimiter->getBlacklistedIps(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 08:24:50 +00:00
										 |  |  | // Get any new feedback messages
 | 
					
						
							|  |  |  | include '../app/includes/feedback-get.php'; | 
					
						
							|  |  |  | include '../app/includes/feedback-show.php'; | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Load the template
 | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | include '../app/templates/security.php'; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | ?>
 |