Add renderer / filter for ip addresses
authorMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:15:55 +0000 (16:15 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:15:55 +0000 (16:15 +0100)
wcfsetup/install/files/lib/system/gridView/filter/IpAddressFilter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/gridView/renderer/IpAddressColumnRenderer.class.php [new file with mode: 0644]

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 (file)
index 0000000..8669f7a
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace wcf\system\gridView\filter;
+
+use wcf\data\DatabaseObjectList;
+use wcf\system\form\builder\field\AbstractFormField;
+use wcf\system\form\builder\field\TextFormField;
+use wcf\system\WCF;
+use wcf\util\UserUtil;
+
+/**
+ * Filter for columns that contain ipv6 addresses, allowing the user to enter addresses in the ipv4 format.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..02ac06e
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use wcf\util\StringUtil;
+use wcf\util\UserUtil;
+
+/**
+ * Renders ipv6 embedded ipv4 address into ipv4 or returns input if true ipv6.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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));
+    }
+}