c0a0bfdebc845679583913cf20399248bbc7f61e
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class filter {
25 var $xmlData;
26 var $searches= array();
27 var $search;
28 var $category= "";
29 var $objectStorage= array();
30 var $base= "";
31 var $scope= "";
32 var $query;
33 var $value= "";
34 var $initial= false;
35 var $scopeMode= "auto";
36 var $converter= null;
37 var $pid;
40 function filter($filename)
41 {
42 global $config;
44 // Load eventually passed filename
45 if (!$this->load($filename)) {
46 die("Cannot parse $filename!");
47 }
49 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
50 }
53 function load($filename)
54 {
55 $contents = file_get_contents($filename);
56 $this->xmlData= xml::xml2array($contents, 1);
58 if (!isset($this->xmlData['filterdef'])) {
59 return false;
60 }
62 $this->xmlData= $this->xmlData["filterdef"];
64 // Load filter
65 if (isset($this->xmlData['search'])) {
67 // Array conversion
68 if (!is_array($this->xmlData['search'])) {
69 $searches= array($this->xmlData['search']);
70 } else {
71 $searches= $this->xmlData['search'];
72 }
74 /* Store available searches */
75 foreach ($this->xmlData['search'] as $search) {
77 /* Do multi conversation */
78 if (!is_array($search['query'])){
79 $search['query']= array($search['query']);
80 }
82 /* Store search */
83 $this->searches[$search['tag']]= $search;
85 }
86 } else {
87 return false;
88 }
90 // Transfer scope
91 $this->scopeMode= $this->xmlData['definition']['scope'];
92 if ($this->scopeMode == "auto") {
93 $this->scope= "one";
94 } else {
95 $this->scope= $this->scopeMode;
96 }
98 // Transfer initial value
99 if (isset($this->xmlData['definition']['initial']) && $this->xmlData['definition']['initial'] == "true"){
100 $this->initial= true;
101 }
103 // Transfer category
104 if (isset($this->xmlData['definition']['category'])){
105 $this->category= $this->xmlData['definition']['category'];
106 }
108 // Set default search mode
109 $this->setSearch($this->xmlData['definition']['default']);
111 return true;
112 }
115 function setSearch($method)
116 {
117 $patch= null;
118 if (is_array($this->category)) {
119 $categories= $this->category;
120 } else {
121 $categories= array($this->category);
122 }
123 $userfilters= userFilter::getFilter($categories);
125 // User filter selected?
126 if (isset($userfilters[$method])){
127 $usermethod= $method;
128 $patch= $userfilters[$method]['filter'];
129 $method= $userfilters[$method]['parent'];
130 }
132 // Move information
133 if (isset($this->searches[$method])) {
134 $this->query= array_merge($this->searches[$method]['query']);
135 if (!isset($this->query[0])) {
136 $this->query= array($this->query);
137 }
139 // Patch first filter?
140 if ($patch) {
141 $this->query[0]['filter']= $patch;
142 $method= $usermethod;
143 }
145 $this->search= $method;
146 } else {
147 die ("Invalid search module!");
148 }
149 }
152 function getTextfield($tag, $value= "", $element= null)
153 {
154 $size= 30;
155 $maxlength= 30;
156 $result= "<input class='filter_textfield' id='$tag' name='$tag' type='text' size='$size' maxlength='{$maxlength}' value='".$value."'>";
157 if ($element && isset($element['autocomplete'])) {
158 $frequency= "0.5";
159 $characters= "1";
160 if (isset($element['autocomplete']['frequency'])) {
161 $frequency= $element['autocomplete']['frequency'];
162 }
163 if (isset($element['autocomplete']['characters'])) {
164 $characters= $element['autocomplete']['characters'];
165 }
166 $result.= "<div id='autocomplete$tag' class='autocomplete'></div>".
167 "<script type='text/javascript'>".
168 "new Ajax.Autocompleter('$tag', 'autocomplete$tag', 'autocomplete.php', { minChars: $characters, frequency: $frequency });".
169 "</script>";
171 $this->autocompleters[$tag]= $element;
172 }
173 return $result;
174 }
177 function getCurrentBase()
178 {
179 if (isset($this->search->base) && (string)$this->search->scope != "auto") {
180 return false;
181 }
183 return $this->base;
184 }
187 function getCurrentScope()
188 {
189 if (isset($this->search->scope) && (string)$this->search->scope != "auto") {
190 return (string)$this->search->scope;
191 }
193 return $this->scope;
194 }
197 function setConverter($hook)
198 {
199 $this->converter= $hook;
200 }
203 function setObjectStorage($storage)
204 {
205 $this->objectStorage= $storage;
206 }
209 function setBase($base)
210 {
211 $this->base= $base;
212 }
215 function setCurrentScope($scope)
216 {
217 $this->scope= $scope;
218 }
221 function renderApply()
222 {
223 return ("<input type='submit' name='apply' value='"._("Apply filter")."'>");
224 }
227 function renderScope()
228 {
229 $checked= $this->scope == "sub"?" checked":"";
230 return "<input type='checkbox' id='SCOPE' name='SCOPE' value='1' onClick='document.mainform.submit();'$checked> <LABEL for='SCOPE'>"._("Search in subtrees")."</LABEL>";
231 }
234 function render()
235 {
236 $content= "<table class='filter-wrapper'><tr><td>".$this->renderFilterMenu()."</td><td>";
237 $content.= "<div class='search-filter'>".$this->getTextfield('search_filter', $this->value, $this->searches[$this->search])."</div>".
238 " <button class='search-filter' type='submit' title='"._("Search")."'>".image("images/find.png")."</button></td></tr></table>";
240 // Return meta data
241 return ("<input type='hidden' name='FILTER_PID' value='".$this->pid."'>".$content);
242 }
245 function query()
246 {
247 global $class_mapping;
248 $result= array();
250 // Return empty list if initial is not set
251 if (!$this->initial) {
252 $this->initial= true;
253 return $result;
254 }
256 // Go thru all queries and merge results
257 foreach ($this->query as $query) {
258 if (!isset($query['backend']) || !isset($query['filter']) || !isset($query['attribute'])) {
259 die("No backend specified in search config.");
260 }
262 // Is backend available?
263 $backend= "filter".$query['backend'];
264 if (!isset($class_mapping["$backend"])) {
265 die("Invalid backend specified in search config.");
266 }
268 // Load filter and attributes
269 $filter= $query['filter'];
270 $attributes= $query['attribute'];
272 // Handle converters if present
273 if ($this->converter) {
274 preg_match('/([^:]+)::(.*)$/', $this->converter, $m);
275 $filter= call_user_func(array($m[1], $m[2]), preg_replace('/\$/', $this->value, $filter));
276 }
278 // Do not replace escaped \$ - This is required to be able to search for e.g. windows machines.
279 if ($this->value == "") {
280 $filter= preg_replace("/\\$/", '*', $filter);
281 } else {
282 $filter= preg_replace("/\\$/", "*".normalizeLdap($this->value)."*", $filter);
283 }
285 $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes, $this->category, $this->objectStorage));
286 }
288 return ($result);
289 }
292 function update()
293 {
294 if (isset($_POST['FILTER_PID']) && $_POST['FILTER_PID'] == $this->pid) {
296 // Save input field
297 if (isset($_POST['search_filter'])) {
298 $this->value= validate($_POST['search_filter']);
299 }
301 // Save scope if needed
302 if ($this->scopeMode == "auto" && isset($_POST['act']) && $_POST['act'] == "toggle-subtree") {
303 $this->scope= ($this->scope == "one")?"sub":"one";
304 }
306 // Switch filter?
307 if (is_array($this->category)) {
308 $categories= $this->category;
309 } else {
310 $categories= array($this->category);
311 }
313 if (isset($_POST['act'])) {
314 foreach (array_merge($this->searches, userFilter::getFilter($categories)) as $tag => $cfg) {
315 if ($_POST['act'] == "filter-$tag") {
316 $this->setSearch($tag);
317 break;
318 }
319 }
320 }
321 }
323 }
326 function getCompletitionList($config, $value="*")
327 {
328 global $class_mapping;
329 $res= array();
331 // Load result attributes
332 $attributes= $config['autocomplete']['attribute'];
333 if (!is_array($attributes)) {
334 $attributes= array($attributes);
335 }
337 // Do the query
338 $result= array();
340 // Is backend available?
341 $queries= $config['query'];
342 if (!isset($queries[0])){
343 $queries= array($queries);
344 }
345 foreach ($queries as $query) {
346 $backend= "filter".$query['backend'];
347 if (!isset($class_mapping["$backend"])) {
348 die("Invalid backend specified in search config.");
349 }
350 $filter= preg_replace("/\\$/", "*".normalizeLdap($value)."*", $query['filter']);
352 $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
353 $this->category, $this->objectStorage));
354 }
356 foreach ($result as $entry) {
357 foreach ($attributes as $attribute) {
358 if (is_array($entry[$attribute])) {
359 for ($i= 0; $i<$entry[$attribute]['count']; $i++) {
360 if (mb_stristr($entry[$attribute][$i], $value)) {
361 $res[]= $entry[$attribute][$i];
362 }
363 }
364 } else {
365 $res[]= $entry[$attribute];
366 }
367 }
368 }
370 return $res;
371 }
374 function processAutocomplete()
375 {
376 global $class_mapping;
377 $result= array();
379 // Introduce maximum number of entries
380 $max= 25;
382 if(isset($this->searches[$this->search]['autocomplete'])){
383 $result= $this->getCompletitionList($this->searches[$this->search], $_POST['search_filter']);
384 $result= array_unique($result);
385 asort($result);
387 echo '<ul>';
388 foreach ($result as $entry) {
389 echo '<li>'.mark($_POST['search_filter'], $entry).'</li>';
390 if ($max-- == 0) {
391 break;
392 }
393 }
395 echo '</ul>';
396 }
397 }
400 function getObjectBase($dn)
401 {
402 global $config;
403 $base= "";
405 // Try every object storage
406 $storage= $this->objectStorage;
407 if (!is_array($storage)){
408 $storage= array($storage);
409 }
410 foreach ($storage as $location) {
411 $pattern= "/^[^,]+,".preg_quote($location, '/')."/i";
412 $base= preg_replace($pattern, '', $dn);
413 }
415 /* Set to base, if we're not on a correct subtree */
416 if (!isset($config->idepartments[$base])){
417 $base= $config->current['BASE'];
418 }
420 return $base;
421 }
425 function renderFilterMenu()
426 {
427 // Load shortcut
428 $result= "<input type='hidden' name='act' id='filter' value=''><div style='display:none'><input type='submit' name='exec_filter' id='exec_filter' value=''></div>".
429 "<ul class='level1' id='filter-root'><li><a href='#'>".image("images/filter.png").image("images/lists/sort-down.png")."</a>";
431 // Build ul/li list
432 $separator= " style='border-top:1px solid #AAA' ";
433 $result.= "<ul class='level2'>";
435 // Build in filters
436 foreach ($this->searches as $tag => $config) {
437 if ($tag == $this->search) {
438 $result.= "<li><a href='#'>".image("images/checked.png")." "._($config['label'])."</a></li>";
439 } else {
440 $result.= "<li><a href='#' onClick='document.getElementById(\"filter\").value= \"filter-$tag\";mainform.submit();'>".image("images/empty.png")." "._($config['label'])."</a></li>";
441 }
442 }
444 // User defined filters
445 $first= true;
446 if (is_array($this->category)) {
447 $categories= $this->category;
448 } else {
449 $categories= array($this->category);
450 }
451 foreach (userFilter::getFilter($categories) as $tag => $config) {
452 if ($tag == $this->search) {
453 $result.= "<li".($first?$separator:"")."><a href='#'>".image("images/checked.png")." "._($config['description'])."</a></li>";
454 } else {
455 $result.= "<li".($first?$separator:"")."><a href='#' onClick='document.getElementById(\"filter\").value= \"filter-$tag\";mainform.submit();'>".image("images/empty.png")." "._($config['description'])."</a></li>";
456 }
458 $first= false;
459 }
461 // Render scope if set to auto
462 if ($this->scopeMode == "auto") {
463 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"filter\").value= \"toggle-subtree\";mainform.submit();'>".($this->scope=="one"?image("images/empty.png"):image("images/checked.png"))." "._("Search in subtrees")."</a></li>";
464 }
467 // Edit filter menu
468 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"filter\").value= \"config-filter\";mainform.submit();'>".image("images/configure.png")." "._("Edit filters")."...</a></li>";
470 $result.= "</ul>";
472 $script= '<script type="text/javascript" language="JavaScript">var menu2; menu2= new Menu("filter-root", "menu2", configMenu)</script>';
474 return "<div id='filtermenu'>".$result."</li></ul><div>$script";
475 }
478 function getFixedFilters()
479 {
480 return array_keys($this->searches);
481 }
483 }
486 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
487 ?>