From 363b9a0638af5f5c100f781fc433858d75ed1e1f Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 19 Nov 2024 16:15:55 +0100 Subject: [PATCH] Add renderer / filter for ip addresses --- .../gridView/filter/IpAddressFilter.class.php | 53 +++++++++++++++++++ .../IpAddressColumnRenderer.class.php | 27 ++++++++++ 2 files changed, 80 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/gridView/filter/IpAddressFilter.class.php create mode 100644 wcfsetup/install/files/lib/system/gridView/renderer/IpAddressColumnRenderer.class.php diff --git a/wcfsetup/install/files/lib/system/gridView/filter/IpAddressFilter.class.php b/wcfsetup/install/files/lib/system/gridView/filter/IpAddressFilter.class.php new file mode 100644 index 0000000000..8669f7a41c --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/filter/IpAddressFilter.class.php @@ -0,0 +1,53 @@ + + * @since 6.2 + */ +class IpAddressFilter implements IGridViewFilter +{ + #[\Override] + public function getFormField(string $id, string $label): AbstractFormField + { + return TextFormField::create($id) + ->label($label); + } + + #[\Override] + public function applyFilter(DatabaseObjectList $list, string $id, string $value): void + { + $list->getConditionBuilder()->add( + "{$id} LIKE ?", + ['%' . WCF::getDB()->escapeLikeValue($this->convertIPv4To6($value)) . '%'] + ); + } + + #[\Override] + public function matches(string $filterValue, string $rowValue): bool + { + return \str_contains($rowValue, $this->convertIPv4To6($filterValue)); + } + + #[\Override] + public function renderValue(string $value): string + { + return $value; + } + + private function convertIPv4To6(string $value): string + { + return UserUtil::convertIPv4To6($value); + } +} diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/IpAddressColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/IpAddressColumnRenderer.class.php new file mode 100644 index 0000000000..02ac06e4e6 --- /dev/null +++ b/wcfsetup/install/files/lib/system/gridView/renderer/IpAddressColumnRenderer.class.php @@ -0,0 +1,27 @@ + + * @since 6.2 + */ +class IpAddressColumnRenderer extends DefaultColumnRenderer +{ + #[\Override] + public function render(mixed $value, mixed $context = null): string + { + if (!$value) { + return ''; + } + + return StringUtil::encodeHTML(UserUtil::convertIPv6To4($value)); + } +} -- 2.20.1