class_objects.inc 15.48 KiB
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2013-2016  FusionDirectory
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
/*!
 * \brief Class for handling objects and their types
 * Allows to list, open, create and delete objects
class objects
  /*!
   * \brief Get list of object of objectTypes from $types in $ou
   * \param array   $types the objectTypes to list
   * \param mixed   $attrs The attributes to fetch.
   * If this is a single value, the resulting associative array will have for each dn the value of this attribute.
   * If this is an array, the keys must be the wanted attributes, and the values can be either 1, '*' or 'raw'
   *  depending if you want a single value or an array of values. 'raw' means untouched LDAP value and is only useful for dns.
   *  Other values are considered to be 1.
   * \param string  $ou the LDAP branch to search in, base will be used if it is NULL
   * \param string  $filter an additional filter to use in the LDAP search.
   * \param boolean $checkAcl should ACL be ignored or checked? Defaults to FALSE.
   * \param string  $scope scope, defaults to subtree. When using one, be careful what you put in $ou.
   * \return The list of objects as an associative array (keys are dns)
  static function ls ($types, $attrs = NULL, $ou = NULL, $filter = '', $checkAcl = FALSE, $scope = 'subtree', $templateSearch = FALSE, $sizeLimit = FALSE)
    global $ui, $config;
    if ($ou === NULL) {
      $ou = $config->current['BASE'];
    if (!is_array($types)) {
      $types = array($types);
    if ($checkAcl) {
      if (count($types) > 1) {
        throw new FusionDirectoryException('Cannot evaluate ACL for several types');
      $infos  = static::infos(reset($types));
      $acl    = $infos['aclCategory'].'/'.$infos['mainTab'];
      $tplAcl = $infos['aclCategory'].'/template';
    $attrsAcls = array();
    if ($attrs === NULL) {
      $attrs = array();
      foreach ($types as $type) {
        $infos = static::infos($type);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if ($infos['mainAttr']) { $attrs[] = $infos['mainAttr']; } } $attrs = array_unique($attrs); if (count($attrs) == 1) { $attrs = $attrs[0]; } if (count($attrs) == 0) { $attrs = array('dn' => 'raw'); } } elseif ($checkAcl) { if (is_array($attrs)) { $search_attrs = array_keys($attrs); } else { $search_attrs = array($attrs); } foreach ($search_attrs as $search_attr) { $category = $ui->getAttributeCategory($types[0], $search_attr); if ($category === FALSE) { throw new FusionDirectoryException('Could not find ACL for attribute "'.$search_attr.'" for type "'.$types[0].'"'); } if ($category === TRUE) { continue; } if (strpos($ui->get_permissions($ou, $category, $search_attr), 'r') === FALSE) { $attrsAcls[$search_attr] = array($category, $search_attr); } } } if (is_array($attrs)) { $search_attrs = array_keys($attrs); if ($templateSearch) { $search_attrs[] = 'fdTemplateField'; $search_attrs[] = 'cn'; } } else { $search_attrs = array($attrs); } try { $ldap = static::search($types, $search_attrs, $ou, $filter, $checkAcl, $scope, $templateSearch, $partialFilterAcls, $sizeLimit); } catch (NonExistingBranchException $e) { return array(); } $result = array(); while ($fetched_attrs = $ldap->fetch()) { $key = $fetched_attrs['dn']; if ($checkAcl) { if (strpos($ui->get_permissions($key, $acl), 'r') === FALSE) { continue; } foreach ($partialFilterAcls as $partialFilterAcl) { if (strpos($ui->get_permissions($key, $partialFilterAcl[0], $partialFilterAcl[1]), 'r') === FALSE) { continue 2; } } } if (is_array($attrs)) { $result[$key] = array(); foreach ($attrs as $attr => $mode) { if (isset($fetched_attrs[$attr])) { if (isset($attrsAcls[$attr]) && (strpos($ui->get_permissions($key, $attrsAcls[$attr][0], $attrsAcls[$attr][1]), 'r') === FALSE)) { continue; } switch ($mode) { case '*': unset($fetched_attrs[$attr]['count']);
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
case 'raw': $result[$key][$attr] = $fetched_attrs[$attr]; break; case 1: default: $result[$key][$attr] = $fetched_attrs[$attr][0]; } } } if ($templateSearch) { if ( isset($fetched_attrs['cn']) && (!$checkAcl || (strpos($ui->get_permissions($key, $tplAcl, 'template_cn'), 'r') !== FALSE)) ) { $result[$key]['cn'] = $fetched_attrs['cn'][0]; } $result[$key]['fdTemplateField'] = array(); foreach ($fetched_attrs['fdTemplateField'] as $templateField) { $attr = explode(':', $templateField, 2)[0]; if (isset($attrs[$attr])) { if (isset($attrsAcls[$attr]) && (strpos($ui->get_permissions($key, $attrsAcls[$attr][0], $attrsAcls[$attr][1]), 'r') === FALSE)) { continue; } $result[$key]['fdTemplateField'][] = $templateField; } } if (empty($result[$key]['fdTemplateField'])) { unset($result[$key]['fdTemplateField']); } } if (count($result[$key]) === 0) { unset($result[$key]); } } elseif (isset($fetched_attrs[$attrs])) { if (isset($attrsAcls[$attrs]) && (strpos($ui->get_permissions($key, $attrsAcls[$attrs][0], $attrsAcls[$attrs][1]), 'r') === FALSE)) { continue; } $result[$key] = $fetched_attrs[$attrs][0]; } } return $result; } /*! * \brief Get count of objects of objectTypes from $types in $ou * * \param array $types the objectTypes to list * \param string $ou the LDAP branch to search in, base will be used if it is NULL * \param string $filter an additional filter to use in the LDAP search. * \param boolean $checkAcl Should ACL be checked on the filtered attributes. * * \return The number of objects of type $type in $ou */ static function count ($types, $ou = NULL, $filter = '', $checkAcl = FALSE, $templateSearch = FALSE) { try { $ldap = static::search($types, array('dn'), $ou, $filter, $checkAcl, 'subtree', $templateSearch, $partialFilterAcls); if (!empty($partialFilterAcls)) { throw new FusionDirectoryException('Not enough rights to use "'.$partialFilterAcls[0][1].'" in filter'); } } catch (EmptyFilterException $e) { return 0; } catch (NonExistingBranchException $e) { return 0; } return $ldap->count(); }