38 static $operators = [
'!',
'&',
'|'];
43 function __construct ($operator, $subparts)
45 $this->
operator = $operator;
46 $this->subparts = $subparts;
49 function __toString ()
51 return '('.$this->operator.join($this->subparts).
')';
54 function __invoke ($array)
57 switch ($this->
operator) {
59 return !$this->subparts[0]($array);
63 foreach ($this->subparts as $subpart) {
64 if ($subpart($array) == $stopValue) {
70 die(
'Unknown operator');
74 function getOperator ()
76 return $this->operator;
79 function getSubparts ()
81 return $this->subparts;
84 function listUsedAttributes (&$result = [])
86 foreach ($this->subparts as $subpart) {
87 $subpart->listUsedAttributes($result);
92 static function parse ($filter)
95 $filter = preg_replace([
'/^\\s*\\(/',
'/\\)\\s*$/'],
'', $filter);
97 if (in_array($filter[0], ldapFilter::$operators)) {
104 while (preg_match(
'/[^\\\\](\\(|\\))/', $filter, $m, PREG_OFFSET_CAPTURE, $offset)) {
105 $offset = $m[0][1] + 1;
106 if ($m[1][0] ==
'(') {
111 } elseif ($m[1][0] ==
')') {
114 $subfilters[] = ldapFilter::parse(substr($filter, $open + 1, $m[0][1] - $open));
118 if (in_array($filter[0], [
'&',
'|']) && (count($subfilters) == 1)) {
120 return $subfilters[0];
122 return new ldapFilter($filter[0], $subfilters);
124 } elseif (preg_match(
'/^([^\\(\\)\\=\\>\\<]+)('.join(
'|', ldapFilterLeaf::$operators).
')([^\\(\\)]*)$/', $filter, $m)) {
137 static $operators = [
'=',
'~=',
'>=',
'<='];
140 protected $dnFilter = FALSE;
142 function __construct ($left, $operator, $right)
144 if (@strrpos($left,
':dn:', -4) !== FALSE) {
145 $this->dnFilter = TRUE;
146 $left = substr($left, 0, -4);
148 parent::__construct($operator, [$left, $right]);
149 if (($this->
operator ==
'=') || ($this->
operator ==
'~=')) {
152 if (preg_match(
'/^\\*/', $this->subparts[1])) {
155 if (preg_match(
'/\\*$/', $this->subparts[1])) {
158 $search = preg_replace([
'/^\\*/',
'/\\*$/'],
'', $this->subparts[1]);
159 if ($this->dnFilter) {
160 $this->pattern =
'/'.$left.
'='.$prefix.preg_quote($search,
'/').$suffix.
',/';
161 } elseif ($this->subparts[1] ==
'*') {
162 $this->pattern =
'/^.*$/';
164 $this->pattern =
'/^'.$prefix.preg_quote($search,
'/').$suffix.
'$/';
169 function isDnFilter ()
171 return $this->dnFilter;
174 function __toString ()
176 return '('.$this->subparts[0].($this->dnFilter ?
':dn:' :
'').$this->
operator.$this->subparts[1].
')';
179 function __invoke ($array)
181 if ($this->dnFilter) {
182 switch ($this->
operator) {
184 trigger_error(
'Filter apply might not work as expected');
186 return (isset($array[
'dn']) && preg_match($this->pattern, $array[
'dn']));
188 die(
'Unsupported dn operator: '.$this->
operator);
191 if (isset($array[$this->subparts[0]])) {
192 $values = $array[$this->subparts[0]];
193 if (!is_array($values)) {
196 foreach ($values as $value) {
197 switch ($this->
operator) {
199 trigger_error(
'Filter apply might not work as expected');
201 if (preg_match($this->pattern, $value)) {
206 if ($value <= $this->subparts[1]) {
211 if ($value >= $this->subparts[1]) {
216 die(
'Unknown operator: '.$this->
operator);
223 function listUsedAttributes (&$result = [])
225 if ($this->dnFilter) {
226 $result[
'dn'] =
'dn';
228 $result[$this->subparts[0]] = $this->subparts[0];
235 function fdTemplateFilter (
ldapFilter $filter)
238 if ($filter->isDnFilter()) {
240 } elseif ($filter->getOperator() ==
'=') {
241 $subparts = $filter->getSubparts();
242 if ($subparts[0] ==
'_template_cn') {
243 return new ldapFilterLeaf(
'cn',
'=', $subparts[1]);
245 return new ldapFilterLeaf(
'fdTemplateField',
'=', $subparts[0].
':'.$subparts[1]);
248 trigger_error(
'Not able to adapt this filter for templates');
251 $subparts = $filter->getSubparts();
252 foreach ($subparts as &$subpart) {
253 $subpart = fdTemplateFilter($subpart);
256 return new ldapFilter($filter->getOperator(), $subparts);
262 function fdNoTemplateFilter (
ldapFilter $filter)
265 if (!$filter->isDnFilter()) {
266 $subparts = $filter->getSubparts();
267 if ($subparts[0] ==
'_template_cn') {
272 $subparts = $filter->getSubparts();
273 foreach ($subparts as &$subpart) {
274 $subpart = fdNoTemplateFilter($subpart);
277 return new ldapFilter($filter->getOperator(), array_filter($subparts));
Leaf of an LDAP filter, for instance (objectClass=*)
This class allows to parse and execute on a array an LDAP filter Example: $filter = ldapFilter::parse...
Parent class for all exceptions thrown in FusionDirectory.