| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Messages { | 
					
						
							|  |  |  |     // Message types
 | 
					
						
							|  |  |  |     const TYPE_SUCCESS = 'success'; | 
					
						
							|  |  |  |     const TYPE_ERROR = 'danger'; | 
					
						
							|  |  |  |     const TYPE_INFO = 'info'; | 
					
						
							|  |  |  |     const TYPE_WARNING = 'warning'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |     // Default message configurations
 | 
					
						
							|  |  |  |     const NOTICE = [ | 
					
						
							|  |  |  |         'DEFAULT' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_INFO, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ERROR = [ | 
					
						
							|  |  |  |         'DEFAULT' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const LOGIN = [ | 
					
						
							|  |  |  |         'LOGIN_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'LOGIN_FAILED' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'LOGOUT_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'IP_BLACKLISTED' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'IP_NOT_WHITELISTED' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'TOO_MANY_ATTEMPTS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |     const SECURITY = [ | 
					
						
							|  |  |  |         'WHITELIST_ADD_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'WHITELIST_ADD_ERROR' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'WHITELIST_REMOVE_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'WHITELIST_REMOVE_ERROR' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'BLACKLIST_ADD_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'BLACKLIST_ADD_ERROR' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'BLACKLIST_REMOVE_SUCCESS' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_SUCCESS, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'BLACKLIST_REMOVE_ERROR' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => true | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         'RATE_LIMIT_INFO' => [ | 
					
						
							|  |  |  |             'type' => self::TYPE_INFO, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |         'PERMISSION_DENIED' => [ | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |         'IP_REQUIRED' => [ | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |             'type' => self::TYPE_ERROR, | 
					
						
							|  |  |  |             'dismissible' => false | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |     private static $strings = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get message strings | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static function getStrings() { | 
					
						
							|  |  |  |         if (self::$strings === null) { | 
					
						
							|  |  |  |             self::$strings = require __DIR__ . '/../includes/messages-strings.php'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return self::$strings; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get message configuration by key | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function get($category, $key) { | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |         $config = constant("self::$category")[$key] ?? null; | 
					
						
							|  |  |  |         if (!$config) return null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $strings = self::getStrings(); | 
					
						
							|  |  |  |         $message = $strings[$category][$key] ?? ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return array_merge($config, ['message' => $message]); | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Render message HTML | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |     public static function render($category, $key, $customMessage = null, $dismissible = null) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |         $config = self::get($category, $key); | 
					
						
							|  |  |  |         if (!$config) return ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $message = $customMessage ?? $config['message']; | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |         $isDismissible = $dismissible ?? $config['dismissible']; | 
					
						
							|  |  |  |         $dismissClass = $isDismissible ? ' alert-dismissible fade show' : ''; | 
					
						
							|  |  |  |         $dismissButton = $isDismissible ? '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' : ''; | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return sprintf( | 
					
						
							|  |  |  |             '<div class="alert alert-%s%s" role="alert">%s%s</div>', | 
					
						
							|  |  |  |             $config['type'], | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |             $dismissClass, | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |             htmlspecialchars($message), | 
					
						
							|  |  |  |             $dismissButton | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Store message in session for display after redirect | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |     public static function flash($category, $key, $customMessage = null, $dismissible = null) { | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |         if (!isset($_SESSION['flash_messages'])) { | 
					
						
							|  |  |  |             $_SESSION['flash_messages'] = []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $_SESSION['flash_messages'][] = [ | 
					
						
							|  |  |  |             'category' => $category, | 
					
						
							|  |  |  |             'key' => $key, | 
					
						
							| 
									
										
										
										
											2025-01-06 09:13:28 +00:00
										 |  |  |             'custom_message' => $customMessage, | 
					
						
							|  |  |  |             'dismissible' => $dismissible | 
					
						
							| 
									
										
										
										
											2025-01-04 12:22:53 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get and clear all flash messages | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getFlash() { | 
					
						
							|  |  |  |         $messages = $_SESSION['flash_messages'] ?? []; | 
					
						
							|  |  |  |         unset($_SESSION['flash_messages']); | 
					
						
							|  |  |  |         return $messages; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |