From 8db608d66aea8582dd71e25ede89663e827d716d Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 1 Feb 2023 20:08:41 +0100 Subject: [PATCH] fix: # and / in filter bridge --- bridges/FilterBridge.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/bridges/FilterBridge.php b/bridges/FilterBridge.php index d5c7346c05d..61ce6d78e23 100644 --- a/bridges/FilterBridge.php +++ b/bridges/FilterBridge.php @@ -16,7 +16,7 @@ class FilterBridge extends FeedExpander 'required' => true, ], 'filter' => [ - 'name' => 'Filter (regular expression)', + 'name' => 'Filter (regular expression!!!)', 'required' => false, ], 'filter_type' => [ @@ -87,8 +87,16 @@ protected function parseItem($newItem) } } - // Build regular expression - $regex = '/' . $this->getInput('filter') . '/'; + $filter = $this->getInput('filter'); + if (! str_contains($filter, '#')) { + $delimiter = '#'; + } elseif (! str_contains($filter, '/')) { + $delimiter = '/'; + } else { + throw new \Exception('Cannot use both / and # inside filter'); + } + + $regex = $delimiter . $filter . $delimiter; if ($this->getInput('case_insensitive')) { $regex .= 'i'; } @@ -105,6 +113,7 @@ protected function parseItem($newItem) $filter_fields[] = $item['title'] ?? null; } if ($this->getInput('target_uri')) { + // todo: maybe consider 'http' and 'https' equivalent? Also maybe optionally .www subdomain? $filter_fields[] = $item['uri'] ?? null; } @@ -115,7 +124,11 @@ protected function parseItem($newItem) if ($length_limit > 0) { $field = substr($field, 0, $length_limit); } - $keep_item |= boolval(preg_match($regex, $field)); + $result = preg_match($regex, $field); + if ($result === false) { + // todo: maybe notify user about the error here? + } + $keep_item |= boolval($result); if ($this->getInput('fix_encoding')) { $keep_item |= boolval(preg_match($regex, utf8_decode($field))); $keep_item |= boolval(preg_match($regex, utf8_encode($field)));