29 protected $searchAttributes = [];
30 protected $scope =
'one';
31 protected $showTemplates = FALSE;
33 protected $search =
'';
35 protected $filterElements = [];
37 protected $fixedScope;
53 $this->parent = $parent;
55 $this->pid = preg_replace(
'/[^0-9]/',
'', microtime(TRUE));
57 foreach ($this->parent->objectTypes as $type) {
58 $this->types[
'filter_type_'.$type] = [
61 'infos' => objects::infos($type),
65 $this->filterElements = [];
66 foreach ($filterElementDefinitions as $filterElementDefinition) {
67 list($class, $args) = $filterElementDefinition;
68 $this->filterElements[] =
new $class($this, ...$args);
71 $this->fixedScope = $fixedScope;
74 function setScope (
string $scope)
76 $this->scope = $scope;
79 protected function showScope ():
bool 81 if ($this->fixedScope !== NULL) {
82 return !$this->fixedScope;
84 return $this->parent->listing->getBaseMode();
89 $this->filterElements[] = $element;
92 function update (
string $base)
96 if (isset($_POST[
'FILTER_PID']) && ($_POST[
'FILTER_PID'] == $this->pid)) {
98 foreach ($this->types as $key => &$data) {
99 $data[
'show'] = isset($_POST[$key]);
103 $this->showTemplates = isset($_POST[
'filter_type_TEMPLATE']);
105 if ($this->showScope()) {
106 $this->scope = (isset($_POST[
'SCOPE']) ?
'sub' :
'one');
109 $this->search = (isset($_POST[
'SEARCH']) ? $_POST[
'SEARCH'] :
'');
111 foreach ($this->filterElements as $element) {
116 foreach ($this->parent->objectTypes as $type) {
117 $attrs = objects::getSearchedAttributes($type);
119 $this->searchAttributes[$type] = [];
120 $this->parent->listing->fillSearchedAttributes($type, $attrs);
121 foreach ($attrs as $attr => $acl) {
122 $rights = $ui->get_permissions($base, $acl, $attr);
123 if (strpos($rights,
'r') !== FALSE) {
124 $this->searchAttributes[$type][] = $attr;
137 $smarty->assign(
'SCOPE', $this->scope);
138 $smarty->assign(
'SHOWSCOPE', $this->showScope());
139 $smarty->assign(
'FILTER_PID', $this->pid);
140 $smarty->assign(
'SEARCH', $this->search);
141 $smarty->assign(
'TYPES', $this->types);
144 foreach ($this->searchAttributes as $type => $attrs) {
145 foreach ($attrs as $attr) {
146 $searchAttrs[] = strtolower($type).
'/'.$attr;
149 $smarty->assign(
'SEARCHDESC', sprintf(_(
'Searches in %s'), implode(
', ', $searchAttrs)));
151 $parentClass = get_class($this->parent);
152 if (!$parentClass::$skipTemplates) {
153 $smarty->assign(
'TEMPLATES', $this->showTemplates);
156 foreach ($this->filterElements as $element) {
157 $elements[] = $element->render();
159 $smarty->assign(
'FILTERS', $elements);
165 return $smarty->fetch($file);
168 function query (array $attrs,
string $base): array
172 $objectTypeCount = [];
175 $ui->getSizeLimitHandler()->setLimitExceeded(FALSE);
176 foreach ($this->parent->objectTypes as $type) {
177 $infos = $this->types[
'filter_type_'.$type][
'infos'];
178 if ($this->scope ==
'one') {
179 $searchBase = (empty($infos[
'ou']) ? $base : $infos[
'ou'].$base);
184 $attrsAsked = $attrs;
185 if (!empty($infos[
'mainAttr']) && !isset($attrsAsked[$infos[
'mainAttr']])) {
187 $attrsAsked[$infos[
'mainAttr']] =
'*';
189 if (!empty($infos[
'nameAttr']) && !isset($attrsAsked[$infos[
'nameAttr']])) {
191 $attrsAsked[$infos[
'nameAttr']] =
'*';
194 foreach ($attrsAsked as $attr => $data) {
196 $category = $ui->getAttributeCategory($type, $attr);
197 if ($category === FALSE) {
198 unset($attrsAsked[$attr]);
202 $elementFilters = [];
203 if (!empty($this->search)) {
204 if (preg_match(
'/^\(.+\)$/', $this->search)) {
205 $elementFilters[] = $this->search;
207 $searchAttributesTmp = $this->searchAttributes[$type];
208 if ($this->showTemplates) {
209 $searchAttributesTmp[] =
'_template_cn';
211 $elementFilters[] =
'(|('.implode(
'=*'.ldap_escape_f($this->search).
'*)(', $searchAttributesTmp).
'=*'.ldap_escape_f($this->search).
'*))';
215 $typeElementFilters = $elementFilters;
216 foreach ($this->filterElements as $element) {
217 $skip = $element->getFilters($type, $typeElementFilters);
218 if ($skip === TRUE) {
223 if (!empty($typeElementFilters)) {
224 $filter =
'(&'.implode(
'', $typeElementFilters).
')';
227 $parentClass = get_class($this->parent);
228 if (!$parentClass::$skipTemplates && $this->showTemplates) {
230 $ldapEntries =
objects::ls($type, $attrsAsked, (($this->scope ==
'one') ?
'ou=templates,'.$searchBase : $searchBase), $filter, TRUE, $this->scope, TRUE);
232 $objectTypeCount[
'template_'.$type] = count($ldapEntries);
233 foreach ($ldapEntries as $dn => $entry) {
234 $entries[$dn] =
new ListingEntry($this->parent->listing,
'template_'.$type, $dn, $entry, $row++);
239 _(
"Could not search for \"%s\" templates:\n%s"),
240 $this->types[
'filter_type_'.$type][
'infos'][
'name'],
250 if (!$this->types[
'filter_type_'.$type][
'show']) {
255 $ldapEntries =
objects::ls($type, $attrsAsked, $searchBase, $filter, TRUE, $this->scope, FALSE, TRUE);
257 $ldapEntries = $this->filterEntries($ldapEntries);
261 _(
"Could not search for \"%s\":\n%s"),
262 $this->types[
'filter_type_'.$type][
'infos'][
'name'],
269 $this->types[
'filter_type_'.$type][
'show'] = FALSE;
273 $objectTypeCount[$type] = count($ldapEntries);
274 foreach ($ldapEntries as $dn => $entry) {
275 if (isset($entries[$dn])) {
276 trigger_error(
'Same dn '.$dn.
' found as several object types');
278 $entries[$dn] =
new ListingEntry($this->parent->listing, $type, $dn, $entry, $row++);
282 return [$entries, $objectTypeCount];
285 protected function filterEntries (array $ldapEntries): array
287 if (!empty($this->parent->whiteList)) {
288 foreach ($ldapEntries as $dn => $entry) {
289 if (in_array($dn, $this->parent->whiteList[
'dn'])) {
292 foreach ($this->parent->whiteList[
'branches'] as $branch) {
293 if (preg_match(
'/'.preg_quote($branch,
'/').
'$/', $dn)) {
297 unset($ldapEntries[$dn]);
300 if (isset($this->parent->blackList)) {
301 foreach ($this->parent->blackList as $attr_name => $attr_values) {
302 foreach ($attr_values as $match) {
303 foreach ($ldapEntries as $dn => $entry) {
304 if (isset($entry[$attr_name])) {
305 $test = $entry[$attr_name];
306 if (!is_array($test)) {
309 if (in_array($match, $test)) {
310 unset($ldapEntries[$dn]);
htmlescape(string $str)
Escape string for HTML output.
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
This class handles an element from the management filter box.
static ls($types, $attrs=NULL, string $ou=NULL, string $filter='', bool $checkAcl=FALSE, string $scope='subtree', bool $templateSearch=FALSE, bool $sizeLimit=FALSE)
Get list of object of objectTypes from $types in $ou.
& get_smarty()
Get global smarty object.
This class handles the management filter box.
Parent class for all exceptions thrown in FusionDirectory.
__construct(management $parent, bool $fixedScope=NULL, array $filterElementDefinitions=[])
Create a management filter.