FusionDirectory
 All Data Structures Files Functions Variables
class_objects.inc
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
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 class objects
23 {
40  static function ls ($types, $attrs = NULL, $ou = NULL, $filter = '', $checkAcl = FALSE, $scope = 'subtree')
41  {
42  global $ui, $config;
43 
44  if ($ou === NULL) {
45  $ou = $config->current['BASE'];
46  }
47 
48  if (!is_array($types)) {
49  $types = array($types);
50  }
51 
52  if ($checkAcl) {
53  if (count($types) > 1) {
54  throw new FusionDirectoryException('Cannot evaluate ACL for several types');
55  }
56  $infos = static::infos(reset($types));
57  $acl = $infos['aclCategory'].'/'.$infos['mainTab'];
58  }
59 
60  $attrsAcls = array();
61  if ($attrs === NULL) {
62  $attrs = array();
63  foreach ($types as $type) {
64  $infos = static::infos($type);
65  if ($infos['mainAttr']) {
66  $attrs[] = $infos['mainAttr'];
67  }
68  }
69  $attrs = array_unique($attrs);
70  if (count($attrs) == 1) {
71  $attrs = $attrs[0];
72  }
73  if (count($attrs) == 0) {
74  $attrs = array('dn' => 'raw');
75  }
76  } elseif ($checkAcl) {
77  if (is_array($attrs)) {
78  $search_attrs = array_keys($attrs);
79  } else {
80  $search_attrs = array($attrs);
81  }
82  foreach ($search_attrs as $search_attr) {
83  $category = $ui->getAttributeCategory($types[0], $search_attr);
84  if ($category === FALSE) {
85  throw new FusionDirectoryException('Could not find ACL for attribute "'.$search_attr.'"');
86  }
87  if (strpos($ui->get_permissions($ou, $category, $search_attr), 'r') === FALSE) {
88  $attrsAcls[$search_attr] = array($category, $search_attr);
89  }
90  }
91  }
92 
93  if (is_array($attrs)) {
94  $search_attrs = array_keys($attrs);
95  } else {
96  $search_attrs = array($attrs);
97  }
98 
99  try {
100  $ldap = static::search($types, $search_attrs, $ou, $filter, $checkAcl, $scope);
101  } catch (NonExistingBranchException $e) {
102  return array();
103  }
104  $result = array();
105  while ($fetched_attrs = $ldap->fetch()) {
106  $key = $fetched_attrs['dn'];
107  if ($checkAcl && !preg_match('/r/', $ui->get_permissions($key, $acl))) {
108  continue;
109  }
110  if (is_array($attrs)) {
111  $result[$key] = array();
112  foreach ($attrs as $attr => $mode) {
113  if (isset($fetched_attrs[$attr])) {
114  if (isset($attrsAcls[$attr]) &&
115  (strpos($ui->get_permissions($key, $attrsAcls[$attr][0], $attrsAcls[$attr][1]), 'r') === FALSE)) {
116  continue;
117  }
118  switch ($mode) {
119  case '*':
120  unset($fetched_attrs[$attr]['count']);
121  case 'raw':
122  $result[$key][$attr] = $fetched_attrs[$attr];
123  break;
124  case 1:
125  default:
126  $result[$key][$attr] = $fetched_attrs[$attr][0];
127  }
128  }
129  }
130  if (count($result[$key]) === 0) {
131  unset($result[$key]);
132  }
133  } elseif (isset($fetched_attrs[$attrs])) {
134  if (isset($attrsAcls[$attrs]) &&
135  (strpos($ui->get_permissions($key, $attrsAcls[$attrs][0], $attrsAcls[$attrs][1]), 'r') === FALSE)) {
136  continue;
137  }
138  $result[$key] = $fetched_attrs[$attrs][0];
139  }
140  }
141  return $result;
142  }
143 
153  static function count ($types, $ou = NULL, $filter = '', $checkAcl = FALSE)
154  {
155  try {
156  $ldap = static::search($types, array('dn'), $ou, $filter, $checkAcl);
157  } catch (EmptyFilterException $e) {
158  return 0;
159  } catch (NonExistingBranchException $e) {
160  return 0;
161  }
162  return $ldap->count();
163  }
164 
165  private static function search ($types, $search_attrs, $ou = NULL, $filter = '', $checkAcl = FALSE, $scope = 'subtree', $templateSearch = FALSE)
166  {
167  global $config, $ui;
168 
169  if (!is_array($types)) {
170  $types = array($types);
171  }
172 
173  if ($ou === NULL) {
174  $ou = $config->current['BASE'];
175  }
176 
177  $typeFilters = array();
178  foreach ($types as $type) {
179  $infos = static::infos($type);
180 
181  if ($infos['filter'] == '') {
182  continue;
183  }
184  $typeFilters[] = $infos['filter'];
185  }
186  if (empty($typeFilters)) {
187  throw new EmptyFilterException();
188  }
189 
190  $ldap = $config->get_ldap_link();
191  if (!$ldap->dn_exists($ou)) {
192  throw new NonExistingBranchException();
193  }
194  if (empty($filter)) {
195  $filter = '(|'.implode($typeFilters).')';
196  } else {
197  if ($checkAcl) {
198  if (count($types) > 1) {
199  throw new FusionDirectoryException('Cannot evaluate ACL for several types');
200  }
201  $filterObject = ldapFilter::parse($filter);
202  $filterAttributes = $filterObject->listUsedAttributes();
203  foreach ($filterAttributes as $acl) {
204  $category = $ui->getAttributeCategory($types[0], $acl);
205  if ($category === FALSE) {
206  throw new FusionDirectoryException('Could not find ACL for attribute "'.$acl.'"');
207  }
208  if (strpos($ui->get_permissions($ou, $category, $acl), 'r') === FALSE) {
209  throw new FusionDirectoryException('Not enough rights to use "'.$acl.'" in filter');
210  }
211  }
212  }
213  if (!preg_match('/^\(.*\)$/', $filter)) {
214  $filter = '('.$filter.')';
215  }
216  $filter = '(&'.$filter.'(|'.implode($typeFilters).'))';
217  }
218  if ($templateSearch) {
219  $templateFilterObject = new ldapFilter(
220  '&',
221  array(
222  new ldapFilterLeaf('objectClass', '=', 'fdTemplate'),
223  fdTemplateFilter(ldapFilter::parse($filter)),
224  )
225  );
226  $filter = "$templateFilterObject";
227  }
228  $ldap->cd($ou);
229  $ldap->search($filter, $search_attrs, $scope);
230  if (!$ldap->success()) {
231  throw new LDAPFailureException($ldap->get_error());
232  }
233  return $ldap;
234  }
235 
244  static function open ($dn, $type)
245  {
246  $infos = static::infos($type);
247  $tabClass = $infos['tabClass'];
248 
249  $tabObject = new $tabClass($type, $dn);
250  $tabObject->set_acl_base();
251  @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, "Openned as $type object");
252 
253  return $tabObject;
254  }
255 
256  static function link ($dn, $type, $subaction = '', $text = NULL, $icon = TRUE)
257  {
258  global $config;
259 
260  $infos = static::infos($type);
261  if (!isset($infos['management'])) {
262  throw new NoManagementClassException('Asked for link for type "'.$type.'" but it does not have a management class');
263  }
264  $pInfos = pluglist::pluginInfos($infos['management']);
265  $index = $pInfos['INDEX'];
266  $action = 'edit';
267  if ($subaction != '') {
268  $action .= '_'.$subaction;
269  }
270  $href = "main.php?plug=$index&amp;reset=1&amp;act=listing_$action&amp;dn=".urlencode($dn);
271 
272  if ($text === NULL) {
273  $ldap = $config->get_ldap_link();
274  $ldap->cat($dn, array($infos['nameAttr']));
275  if ($attrs = $ldap->fetch()) {
276  if (isset($attrs[$infos['nameAttr']][0])) {
277  $text = $attrs[$infos['nameAttr']][0];
278  } else {
279  $text = $dn;
280  }
281  } else {
282  throw new NonExistingLdapNodeException('Dn '.$dn.' not found in LDAP');
283  }
284  } elseif (is_array($text)) {
285  $text = $text[$infos['nameAttr']][0];
286  }
287 
288  if ($icon && isset($infos['icon'])) {
289  $text = '<img alt="'.$infos['name'].'" title="'.$dn.'" src="'.htmlentities($infos['icon'], ENT_COMPAT, 'UTF-8').'" class="center"/>&nbsp;'.$text;
290  }
291 
292  return '<a href="'.$href.'">'.$text.'</a>';
293  }
294 
295  static function create ($type)
296  {
297  return static::open('new', $type);
298  }
299 
300  static function infos ($type)
301  {
302  global $config;
303 
304  if (!isset($config->data['OBJECTS'][strtoupper($type)])) {
305  throw new NonExistingObjectTypeException('Non-existing type "'.$type.'"');
306  }
307 
308  return $config->data['OBJECTS'][strtoupper($type)];
309  }
310 
311  static function isOfType ($attrs, $type)
312  {
313  $filter = static::getFilterObject($type);
314  return $filter($attrs);
315  }
316 
317  /* This method allows to cache parsed filter in filterObject key in objectTypes */
318  static function getFilterObject ($type)
319  {
320  global $config;
321 
322  if (!isset($config->data['OBJECTS'][strtoupper($type)])) {
323  throw new NonExistingObjectTypeException('Non-existing type "'.$type.'"');
324  }
325 
326  $infos =& $config->data['OBJECTS'][strtoupper($type)];
327 
328  if (!isset($infos['filterObject'])) {
329  $infos['filterObject'] = ldapFilter::parse($infos['filter']);
330  }
331  return $infos['filterObject'];
332  }
333 
334  static function types ()
335  {
336  global $config;
337  return array_keys($config->data['OBJECTS']);
338  }
339 
340  /* !\brief This method returns a list of all available templates for the given type
341  */
342  static function getTemplates ($type, $requiredPermissions = 'r', $filter = '')
343  {
344  global $config, $ui;
345 
346  $infos = static::infos($type);
347 
348  $templates = array();
349  $ldap = $config->get_ldap_link();
350  foreach ($config->departments as $key => $value) {
351  // Search all templates from the current dn.
352  try {
353  $ldap = static::search($type, array('cn'), $infos['ou'].$value, $filter, FALSE, 'subtree', TRUE);
354  } catch (NonExistingBranchException $e) {
355  continue;
356  }
357  if ($ldap->count() != 0) {
358  while ($attrs = $ldap->fetch()) {
359  $dn = $attrs['dn'];
360  if ($requiredPermissions != '') {
361  if (!preg_match('/'.$requiredPermissions.'/', $ui->get_permissions($dn, $infos['aclCategory'].'/'.'template'))) {
362  continue;
363  }
364  }
365  $templates[$dn] = $attrs['cn'][0].' - '.$key;
366  }
367  }
368  }
369  natcasesort($templates);
370  reset($templates);
371  return $templates;
372  }
373 }
374 
375 ?>
static count($types, $ou=NULL, $filter= '', $checkAcl=FALSE)
Get count of objects of objectTypes from $types in $ou.
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.
DEBUG($level, $line, $function, $file, $data, $info= '')
Debug level action.
Definition: functions.inc:200
static ls($types, $attrs=NULL, $ou=NULL, $filter= '', $checkAcl=FALSE, $scope= 'subtree')
Get list of object of objectTypes from $types in $ou.
static open($dn, $type)
Create the tab object for the given dn.