FusionDirectory
 All Data Structures Files Functions Variables
class_ldapFilter.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2013-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
22 
38 {
39  static $operators = array('!', '&', '|');
40 
41  protected $operator;
42  protected $subparts;
43 
44  function __construct($operator, $subparts)
45  {
46  $this->operator = $operator;
47  $this->subparts = $subparts;
48  }
49 
50  function __toString()
51  {
52  return '('.$this->operator.join($this->subparts).')';
53  }
54 
55  function __invoke($array)
56  {
57  $stopValue = FALSE;
58  switch ($this->operator) {
59  case '!':
60  return !$this->subparts[0]($array);
61  case '|':
62  $stopValue = TRUE;
63  case '&':
64  foreach ($this->subparts as $subpart) {
65  if ($subpart($array) == $stopValue) {
66  return $stopValue;
67  }
68  }
69  return !$stopValue;
70  default:
71  die('Unknown operator');
72  }
73  }
74 
75  function getOperator()
76  {
77  return $this->operator;
78  }
79 
80  function getSubparts()
81  {
82  return $this->subparts;
83  }
84 
85  function listUsedAttributes(&$result = array())
86  {
87  foreach ($this->subparts as $subpart) {
88  $subpart->listUsedAttributes($result);
89  }
90  return $result;
91  }
92 
93  static function parse($filter)
94  {
95  // Remove starting and ending parenthesis
96  $filter = preg_replace(array('/^\\s*\\(/', '/\\)\\s*$/'), '', $filter);
97 
98  if (in_array($filter[0], ldapFilter::$operators)) {
99  $subfilters = array();
100  /* We need an ending parenthesis in order to catch last subpart correctly */
101  $filter .= ')';
102  $offset = 0;
103  $level = 0;
104  $open;
105  while (preg_match('/[^\\\\](\\(|\\))/', $filter, $m, PREG_OFFSET_CAPTURE, $offset)) {
106  $offset = $m[0][1] + 1;
107  if ($m[1][0] == '(') {
108  if ($level == 0) {
109  $open = $m[1][1];
110  }
111  $level++;
112  } elseif ($m[1][0] == ')') {
113  $level--;
114  if ($level == 0) {
115  $subfilters[] = ldapFilter::parse(substr($filter, $open + 1, $m[0][1] - $open));
116  }
117  }
118  }
119  return new ldapFilter($filter[0], $subfilters);
120  } else {
121  preg_match('/^(.+)('.join('|', ldapFilterLeaf::$operators).')(.+)$/', $filter, $m);
122  return new ldapFilterLeaf($m[1], $m[2], $m[3]);
123  }
124  }
125 }
126 
128 {
129  static $operators = array('=','=~','>','>=','<','<=');
130 
131  protected $pattern;
132 
133  function __construct($left, $operator, $right)
134  {
135  parent::__construct($operator, array($left, $right));
136  if (($this->operator == '=') || ($this->operator == '=~')) {
137  $prefix = '';
138  $suffix = '';
139  if (preg_match('/^\\*/', $this->subparts[1])) {
140  $prefix = '.*';
141  }
142  if (preg_match('/\\*$/', $this->subparts[1])) {
143  $suffix = '.*';
144  }
145  $search = preg_replace(array('/^\\*/','/\\*$/'), '', $this->subparts[1]);
146  $this->pattern = '/^'.$prefix.preg_quote($search, '/').$suffix.'$/';
147  }
148  }
149 
150  function __toString()
151  {
152  return '('.$this->subparts[0].$this->operator.$this->subparts[1].')';
153  }
154 
155  function __invoke($array)
156  {
157  if (isset($array[$this->subparts[0]])) {
158  $values = $array[$this->subparts[0]];
159  if (!is_array($values)) {
160  $values = array($values);
161  }
162  foreach ($values as $value) {
163  switch ($this->operator) {
164  case '=~':
165  trigger_error('Filter apply might not work as expected');
166  case '=':
167  if (preg_match($this->pattern, $value)) {
168  return TRUE;
169  }
170  break;
171  case '<':
172  if ($value < $this->subparts[1]) {
173  return TRUE;
174  }
175  break;
176  case '>':
177  if ($value > $this->subparts[1]) {
178  return TRUE;
179  }
180  break;
181  case '<=':
182  if ($value <= $this->subparts[1]) {
183  return TRUE;
184  }
185  break;
186  case '>=':
187  if ($value >= $this->subparts[1]) {
188  return TRUE;
189  }
190  break;
191  default:
192  die('Unknown operator: '.$this->operator);
193  }
194  }
195  }
196  return FALSE;
197  }
198 
199  function listUsedAttributes(&$result = array())
200  {
201  if ($this->dnFilter) {
202  $result['dn'] = 'dn';
203  } else {
204  $result[$this->subparts[0]] = $this->subparts[0];
205  }
206  return $result;
207  }
208 }
209 
210 function fdTemplateFilter($filter)
211 {
212  if ($filter instanceof ldapFilterLeaf) {
213  if ($filter->getOperator() == '=') {
214  $subparts = $filter->getSubparts();
215  return new ldapFilterLeaf('fdTemplateField', '=', $subparts[0].':'.$subparts[1]);
216  } else {
217  trigger_error('Not able to adapt this filter for templates');
218  }
219  } else {
220  $subparts = $filter->getSubparts();
221  foreach ($subparts as &$subpart) {
222  $subpart = fdTemplateFilter($subpart);
223  }
224  unset($subpart);
225  return new ldapFilter($filter->getOperator(), $subparts);
226  }
227  return $filter;
228 }
229 
230 ?>
This class allows to parse and execute on a array an LDAP filter Example: $filter = ldapFilter::parse...