/** * @param $dn * @param array $props * @return Node */ public function createChildByDN($dn, $props = array()) { $dn = $this->stripMyDN($dn); $parts = array_reverse(LdapUtils::explodeDN($dn)); $parent = $this; while ($rdn = array_shift($parts)) { if ($parent->hasChildRDN($rdn)) { $child = $parent->getChildByRDN($rdn); } else { $child = Node::createWithRDN($parent, $rdn, (array) $props); $parent->addChild($child); } $parent = $child; } return $child; }
/** * Render and return a valid LDAP filter expression of the given filter * * @param FilterExpression $filter * * @return string */ protected function renderFilterExpression(FilterExpression $filter) { $column = $filter->getColumn(); $sign = $filter->getSign(); $expression = $filter->getExpression(); $format = '%1$s%2$s%3$s'; if ($expression === null || $expression === true) { $expression = '*'; } elseif (is_array($expression)) { $seqFormat = '|(%s)'; if ($sign === '!=') { $seqFormat = '!(' . $seqFormat . ')'; $sign = '='; } $seqParts = array(); foreach ($expression as $expressionValue) { $seqParts[] = sprintf($format, LdapUtils::quoteForSearch($column), $sign, LdapUtils::quoteForSearch($expressionValue, true)); } return sprintf($seqFormat, implode(')(', $seqParts)); } if ($sign === '!=') { $format = '!(%1$s=%3$s)'; } return sprintf($format, LdapUtils::quoteForSearch($column), $sign, LdapUtils::quoteForSearch($expression, true)); }
/** * Fetch result as tree * * @return Root * * @todo This is untested waste, not being used anywhere and ignores the query's order and base dn. * Evaluate whether it's reasonable to properly implement and test it. */ public function fetchTree() { $result = $this->fetchAll(); $sorted = array(); $quotedDn = preg_quote($this->ds->getDn(), '/'); foreach ($result as $key => &$item) { $new_key = LdapUtils::implodeDN(array_reverse(LdapUtils::explodeDN(preg_replace('/,' . $quotedDn . '$/', '', $key)))); $sorted[$new_key] = $key; } unset($groups); ksort($sorted); $tree = Root::forConnection($this->ds); $root_dn = $tree->getDN(); foreach ($sorted as $sort_key => &$key) { if ($key === $root_dn) { continue; } $tree->createChildByDN($key, $result[$key]); } return $tree; }
/** * Return the LDAP filter to be applied on this query * * @return string * * @throws LdapException In case the objectClass filter does not exist */ protected function renderFilter() { if (!isset($this->filters['objectClass'])) { throw new LdapException('Object class is mandatory'); } $parts = array(); foreach ($this->filters as $key => $value) { if ($value instanceof Expression) { $parts[] = (string) $value; } else { $parts[] = sprintf('%s=%s', LdapUtils::quoteForSearch($key), LdapUtils::quoteForSearch($value, true)); } } if (count($parts) > 1) { return '(&(' . implode(')(', $parts) . '))'; } else { return '(' . $parts[0] . ')'; } }