From 26006d92efe57af19a76669049c9fe2467e83df8 Mon Sep 17 00:00:00 2001 From: fowr <89118232+perdedora@users.noreply.github.com> Date: Thu, 10 Apr 2025 11:50:59 -0300 Subject: [PATCH] FloodManager.php: add flood manager for post filtering and flood control --- inc/Controller/FloodManager.php | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 inc/Controller/FloodManager.php diff --git a/inc/Controller/FloodManager.php b/inc/Controller/FloodManager.php new file mode 100644 index 00000000..c3231f33 --- /dev/null +++ b/inc/Controller/FloodManager.php @@ -0,0 +1,92 @@ +filterService = $filterService; + $this->floodService = $floodService; + $this->notes = $notes; + $this->logger = $logger; + } + + /** + * Processes an incoming post through the filter and flood system. + * + * Applies filters first. If a filter matches, returns the filter result + * If no filter matches, records a flood entry, purges old entries, and returns null. + * Handles critical errors internally. + * + * @param array $post The post data array. + * @return array|null The matching filter configuration if found, otherwise null. + * Returns null also if a critical error occurs during processing. + */ + public function processPost(array $post): ?array { + try { + $filterResult = $this->filterService->applyFilters($post); + + if ($filterResult !== null) { + if (isset($filterResult['add_note']) && $filterResult['add_note']) { + $this->notes->add( + $post['ip'], -1, 'Autoban message: ' . $post['body'] + ); + } + return $filterResult; + } + + $this->floodService->recordFloodEntry($post); + + $this->floodService->purgeOldEntries(); + + return null; + } catch (\Throwable $e) { + $this->logger->log( + LogDriver::ERROR, + "Critical error in flood filtering system: " . $e->getMessage() + ); + return null; + } + } +}