Verified Commit fb35a648 authored by dockx thibault's avatar dockx thibault
Browse files

:ambulance: (filter) - modify filtering logic

Adapts filtering to php8.2
No related merge requests found
Showing with 31 additions and 13 deletions
+31 -13
...@@ -138,34 +138,52 @@ class ldapFilterLeaf extends ldapFilter implements \Stringable ...@@ -138,34 +138,52 @@ class ldapFilterLeaf extends ldapFilter implements \Stringable
protected $pattern; protected $pattern;
protected $dnFilter = FALSE; protected $dnFilter = FALSE;
function __construct($left, $operator, $right)
function __construct ($left, $operator, $right)
{ {
if (@strrpos((string) $left, ':dn:', -4) !== FALSE) { // Ensure $left is a string for consistency
$this->dnFilter = TRUE; $left = (string) $left;
$left = substr((string) $left, 0, -4);
// Check if $left ends with ':dn:' using str_ends_with()
if (str_ends_with($left, ':dn:')) {
$this->dnFilter = true;
$left = substr($left, 0, -4); // Remove ':dn:'
} }
parent::__construct($operator, [$left, $right]); parent::__construct($operator, [$left, $right]);
if (($this->operator == '=') || ($this->operator == '~=')) {
if ($this->operator === '=' || $this->operator === '~=') {
$prefix = ''; $prefix = '';
$suffix = ''; $suffix = '';
if (preg_match('/^\\*/', (string) $this->subparts[1])) {
// Detect * at start and end using a single regex
if (preg_match('/^\*(.*)\*$/', (string) $this->subparts[1], $matches)) {
$prefix = '.*'; $prefix = '.*';
}
if (preg_match('/\\*$/', (string) $this->subparts[1])) {
$suffix = '.*'; $suffix = '.*';
$search = $matches[1]; // Extract inner content
} else {
$search = trim((string) $this->subparts[1], '*');
if (str_starts_with((string) $this->subparts[1], '*')) {
$prefix = '.*';
}
if (str_ends_with((string) $this->subparts[1], '*')) {
$suffix = '.*';
}
} }
$search = preg_replace(['/^\\*/','/\\*$/'], '', (string) $this->subparts[1]);
$escapedSearch = preg_quote($search, '/');
// Construct the pattern
if ($this->dnFilter) { if ($this->dnFilter) {
$this->pattern = '/'.$left.'='.$prefix.preg_quote((string) $search, '/').$suffix.',/'; $this->pattern = "/{$left}={$prefix}{$escapedSearch}{$suffix},/";
} elseif ($this->subparts[1] == '*') { } elseif ($this->subparts[1] === '*') {
$this->pattern = '/^.*$/'; $this->pattern = '/^.*$/';
} else { } else {
$this->pattern = '/^'.$prefix.preg_quote((string) $search, '/').$suffix.'$/'; $this->pattern = "/^{$prefix}{$escapedSearch}{$suffix}$/";
} }
} }
} }
function isDnFilter () function isDnFilter ()
{ {
return $this->dnFilter; return $this->dnFilter;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment