| 
									
										
										
										
											2025-02-17 12:36:00 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Session Middleware | 
					
						
							| 
									
										
										
										
											2025-02-19 13:31:01 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-02-17 12:36:00 +00:00
										 |  |  |  * Validates session status and handles session timeout. | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  |  * If session is invalid, redirects to login page. | 
					
						
							| 
									
										
										
										
											2025-02-17 12:36:00 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  | function applySessionMiddleware($config, $app_root, $isTest = false) { | 
					
						
							|  |  |  |     // Start session if not already started
 | 
					
						
							|  |  |  |     if (session_status() !== PHP_SESSION_ACTIVE) { | 
					
						
							|  |  |  |         Session::startSession(); | 
					
						
							| 
									
										
										
										
											2025-02-19 13:31:01 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  |     // Check session validity
 | 
					
						
							|  |  |  |     if (!Session::isValidSession()) { | 
					
						
							| 
									
										
										
										
											2025-04-14 07:06:13 +00:00
										 |  |  |         // Only show session timeout message if there was an active session
 | 
					
						
							| 
									
										
										
										
											2025-04-14 12:31:19 +00:00
										 |  |  |         // and we haven't shown it yet
 | 
					
						
							|  |  |  |         if (isset($_SESSION['LAST_ACTIVITY']) && !isset($_SESSION['session_timeout_shown'])) { | 
					
						
							| 
									
										
										
										
											2025-04-14 07:06:13 +00:00
										 |  |  |             Feedback::flash('LOGIN', 'SESSION_TIMEOUT'); | 
					
						
							| 
									
										
										
										
											2025-04-14 12:31:19 +00:00
										 |  |  |             $_SESSION['session_timeout_shown'] = true; | 
					
						
							| 
									
										
										
										
											2025-04-14 07:06:13 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  |         // Session invalid, clean up and redirect
 | 
					
						
							|  |  |  |         Session::cleanup($config); | 
					
						
							| 
									
										
										
										
											2025-04-12 13:22:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  |         if (!$isTest) { | 
					
						
							|  |  |  |             header('Location: ' . $app_root . '?page=login'); | 
					
						
							|  |  |  |             exit(); | 
					
						
							| 
									
										
										
										
											2025-04-12 13:22:41 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-13 16:11:52 +00:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2025-04-12 13:22:41 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } |