wafInstalled = $this->detectWaf(); $this->wafFormData = [ 'enabled' => Setting::get('waf_enabled', '0') === '1', 'paranoia' => Setting::get('waf_paranoia', '1'), 'audit_log' => Setting::get('waf_audit_log', '1') === '1', ]; } protected function detectWaf(): bool { $paths = [ '/etc/nginx/modsec/main.conf', '/etc/nginx/modsecurity.conf', '/etc/modsecurity/modsecurity.conf', '/etc/modsecurity/modsecurity.conf-recommended', ]; foreach ($paths as $path) { if (file_exists($path)) { return true; } } return false; } protected function getForms(): array { return ['wafForm']; } public function wafForm(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema { return $schema ->statePath('wafFormData') ->schema([ Section::make(__('WAF Settings')) ->schema([ Toggle::make('enabled') ->label(__('Enable ModSecurity')) ->disabled(fn () => ! $this->wafInstalled) ->helperText(fn () => $this->wafInstalled ? null : __('ModSecurity is not installed. Install it to enable WAF.')), Select::make('paranoia') ->label(__('Paranoia Level')) ->options([ '1' => '1 - Basic', '2' => '2 - Moderate', '3' => '3 - Strict', '4' => '4 - Very Strict', ]) ->default('1'), Toggle::make('audit_log') ->label(__('Enable Audit Log')), ]) ->columns(2), ]); } public function saveWafSettings(): void { $data = $this->wafForm->getState(); $requestedEnabled = ! empty($data['enabled']); if ($requestedEnabled && ! $this->wafInstalled) { $requestedEnabled = false; } Setting::set('waf_enabled', $requestedEnabled ? '1' : '0'); Setting::set('waf_paranoia', (string) ($data['paranoia'] ?? '1')); Setting::set('waf_audit_log', ! empty($data['audit_log']) ? '1' : '0'); try { $agent = new AgentClient; $agent->wafApplySettings( $requestedEnabled, (string) ($data['paranoia'] ?? '1'), ! empty($data['audit_log']) ); if (! $this->wafInstalled && ! empty($data['enabled'])) { Notification::make() ->title(__('ModSecurity is not installed')) ->body(__('WAF was disabled automatically. Install ModSecurity to enable it.')) ->warning() ->send(); return; } Notification::make() ->title(__('WAF settings applied')) ->success() ->send(); } catch (Exception $e) { Notification::make() ->title(__('WAF settings saved, but apply failed')) ->body($e->getMessage()) ->warning() ->send(); } } }