| 
									
										
										
										
											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'])) { | 
					
						
							|  |  |  |     $action = $_POST['action']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         switch ($action) { | 
					
						
							|  |  |  |             case 'add_whitelist': | 
					
						
							|  |  |  |                 if (!$userObject->hasRight($user_id, 'superuser') && !$userObject->hasRight($user_id, 'edit whitelist')) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'PERMISSION_DENIED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (empty($_POST['ip_address'])) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'IP_REQUIRED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  |                 $is_network = isset($_POST['is_network']) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 if (!$rateLimiter->addToWhitelist($_POST['ip_address'], $is_network, $_POST['description'] ?? '', $currentUser, $user_id)) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'WHITELIST_ADD_ERROR')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                 Messages::flash('SECURITY', 'WHITELIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											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-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'PERMISSION_DENIED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (empty($_POST['ip_address'])) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'IP_REQUIRED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (!$rateLimiter->removeFromWhitelist($_POST['ip_address'], $currentUser, $user_id)) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'WHITELIST_REMOVE_ERROR')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                 Messages::flash('SECURITY', 'WHITELIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											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-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'PERMISSION_DENIED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (empty($_POST['ip_address'])) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'IP_REQUIRED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  |                 $is_network = isset($_POST['is_network']) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 $expiry_hours = !empty($_POST['expiry_hours']) ? intval($_POST['expiry_hours']) : null; | 
					
						
							|  |  |  |                 if (!$rateLimiter->addToBlacklist($_POST['ip_address'], $is_network, $_POST['reason'] ?? '', $currentUser, $user_id, $expiry_hours)) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'BLACKLIST_ADD_ERROR')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                 Messages::flash('SECURITY', 'BLACKLIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											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-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'PERMISSION_DENIED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (empty($_POST['ip_address'])) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'IP_REQUIRED')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (!$rateLimiter->removeFromBlacklist($_POST['ip_address'], $currentUser, $user_id)) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                     throw new Exception(Messages::get('SECURITY', 'BLACKLIST_REMOVE_ERROR')['message']); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |                 Messages::flash('SECURITY', 'BLACKLIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |         $messages[] = ['category' => 'SECURITY', 'key' => 'CUSTOM_ERROR', 'custom_message' => $e->getMessage()]; | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |         Messages::flash('SECURITY', 'CUSTOM_ERROR', 'custom_message'); | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |     if (empty($messages)) { | 
					
						
							|  |  |  |         // Only redirect if there were no errors
 | 
					
						
							|  |  |  |         header("Location: {$app_root}?page=security§ion={$section}"); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |         exit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | ?>
 |