| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Check if user has any of the required rights
 | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  | if (!($userObject->hasRight($userId, 'superuser') || | 
					
						
							|  |  |  |       $userObject->hasRight($userId, 'edit whitelist') || | 
					
						
							|  |  |  |       $userObject->hasRight($userId, 'edit blacklist') || | 
					
						
							|  |  |  |       $userObject->hasRight($userId, 'edit ratelimiting'))) { | 
					
						
							| 
									
										
										
										
											2025-01-04 10:30:44 +00:00
										 |  |  |     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'; | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  | $rateLimiter = new RateLimiter($db); | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-17 13:15:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Apply rate limiting for security operations
 | 
					
						
							|  |  |  |     require_once '../app/includes/rate_limit_middleware.php'; | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |     checkRateLimit($db, 'security', $userId); | 
					
						
							| 
									
										
										
										
											2025-02-17 13:15:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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': | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                 if (!$userObject->hasRight($userId, 'superuser') && !$userObject->hasRight($userId, 'edit whitelist')) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'PERMISSION_DENIED'); | 
					
						
							|  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         'max' => 45, // Max length for IPv6
 | 
					
						
							|  |  |  |                         'ip' => true | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     ], | 
					
						
							|  |  |  |                     'description' => [ | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         'required' => true, | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                         'max' => 255 | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							|  |  |  |                     $is_network = isset($_POST['is_network']) && $_POST['is_network'] === 'on'; | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                     if (!$rateLimiter->addToWhitelist($_POST['ip_address'], $is_network, $_POST['description'] ?? '', $currentUser, $userId)) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         Feedback::flash('SECURITY', 'WHITELIST_ADD_FAILED'); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         Feedback::flash('SECURITY', 'WHITELIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_ADD_ERROR_IP', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'remove_whitelist': | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                 if (!$userObject->hasRight($userId, 'superuser') && !$userObject->hasRight($userId, 'edit whitelist')) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'PERMISSION_DENIED'); | 
					
						
							|  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         'max' => 45, | 
					
						
							|  |  |  |                         'ip' => true | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                     if (!$rateLimiter->removeFromWhitelist($_POST['ip_address'], $currentUser, $userId)) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         Feedback::flash('SECURITY', 'WHITELIST_REMOVE_FAILED'); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         Feedback::flash('SECURITY', 'WHITELIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'WHITELIST_REMOVE_FAILED', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'add_blacklist': | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                 if (!$userObject->hasRight($userId, 'superuser') && !$userObject->hasRight($userId, 'edit blacklist')) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'PERMISSION_DENIED'); | 
					
						
							|  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         'max' => 45, | 
					
						
							|  |  |  |                         'ip' => true | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     ], | 
					
						
							|  |  |  |                     '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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                     if (!$rateLimiter->addToBlacklist($_POST['ip_address'], $is_network, $_POST['reason'], $currentUser, $userId, $expiry_hours)) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         Feedback::flash('SECURITY', 'BLACKLIST_ADD_FAILED'); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         Feedback::flash('SECURITY', 'BLACKLIST_ADD_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_ADD_ERROR_IP', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'remove_blacklist': | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                 if (!$userObject->hasRight($userId, 'superuser') && !$userObject->hasRight($userId, 'edit blacklist')) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'PERMISSION_DENIED'); | 
					
						
							|  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $rules = [ | 
					
						
							|  |  |  |                     'ip_address' => [ | 
					
						
							|  |  |  |                         'required' => true, | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         'max' => 45, | 
					
						
							|  |  |  |                         'ip' => true | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     ] | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if ($validator->validate($rules)) { | 
					
						
							| 
									
										
										
										
											2025-04-14 07:39:58 +00:00
										 |  |  |                     if (!$rateLimiter->removeFromBlacklist($_POST['ip_address'], $currentUser, $userId)) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                         Feedback::flash('SECURITY', 'BLACKLIST_REMOVE_FAILED'); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         Feedback::flash('SECURITY', 'BLACKLIST_REMOVE_SUCCESS'); | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                     Feedback::flash('SECURITY', 'BLACKLIST_REMOVE_FAILED', $validator->getFirstError()); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2025-02-10 17:33:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'INVALID_ACTION'); | 
					
						
							| 
									
										
										
										
											2025-01-04 11:41:02 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-02-24 12:08:05 +00:00
										 |  |  |         Feedback::flash('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') { | 
					
						
							| 
									
										
										
										
											2025-02-17 13:15:05 +00:00
										 |  |  |     $system_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
 | 
					
						
							| 
									
										
										
										
											2025-02-17 14:50:57 +00:00
										 |  |  | include '../app/helpers/feedback.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'; |