1 <?php
3 class userFilterEditor extends plugin
4 {
5 public $pathTitle= "Edit";
7 // The original entry else array(), allows us to perform existence checks.
8 public $entry = array();
10 // The values
11 public $name = "";
12 public $description = "";
13 public $parent = "";
14 public $selectedCategories = array();
15 public $share = FALSE;
16 public $enabled = TRUE;
17 public $queries = array();
19 public $listing = NULL;
21 // The list of all categories mangaged by the current filter object.
22 // Used in the grop-down box.
23 public $orig_name = "";
24 public $backends = array('LDAP','LDAPBlacklist', 'SYSTEMS', 'FAI', 'GroupLDAP','ACL', 'OPSIPackages','APPLICATIONS','MIMETYPES','CONFIGPROPERTIES');
27 /*! \brief Instantiate the filter editing dialog.
28 * Parses the filter info into editable data.
29 */
30 function __construct($entry, $listing)
31 {
32 $this->listing = &$listing;
33 if($entry){
34 $this->entry = $entry;
35 $this->parent = $entry['parent'];
36 $this->name = $entry['tag'];
37 $this->description = $entry['description'];
39 foreach($entry['query'] as $query){
40 $query['filter'] = userFilterEditor::_autoIndentFilter($query['filter'], " ");
41 $this->queries[] = $query;
42 }
43 $this->selectedCategories = $entry['categories'];
44 $this->share = in_array("share",$entry['flags']);
45 $this->enable = in_array("enable",$entry['flags']);
46 }
47 $this->orig_name = $this->name;
48 }
51 /*! \brief Automatic indent indentation for filters.
52 */
53 static function _autoIndentFilter($str, $indent = " ")
54 {
55 // Remove line breaks and escaped brackets
56 $str = preg_replace('/[\t ]*\n[\t ]*/', "", $str);
57 $str = preg_replace('/\\\\\\(/', "::OPEN::", $str);
58 $str = preg_replace('/\\\\\\)/', "::CLOSE::", $str);
60 // Add a line break infront of every bracket
61 $str = preg_replace('/\\(/', "\n(", $str);
62 $str = preg_replace('/\\)/', ")\n", $str);
64 // Split by linebreaks
65 $lines = preg_split("/\n/", $str);
66 $str = "";
67 $i = 0;
69 // Walk trough search blocks
70 foreach($lines as $line){
71 $line = trim($line);
72 if(empty($line)) continue;
74 // Go back one level in indentation
75 if(!preg_match("/\\(.*\\)/", $line) && preg_match('/\\)$/', $line)){
76 $i --;
77 }
79 $str.= "\n";
80 $str = str_pad($str,strlen($str)+$i, $indent);
81 $str.= $line;
83 // Go one level deeper in indentation
84 if(!preg_match("/\\(.*\\)/", $line) && preg_match('/^\\(/', $line)){
85 $i ++;
86 }
87 }
88 $str = preg_replace('/::OPEN::/', '\(', $str);
89 $str = preg_replace('/::CLOSE::/', '\)', $str);
90 return($str);
91 }
94 /*! \brief Retunrs the filters original name
95 * @param The original name of the filter (if none was given
96 * an empty string is returned)
97 */
98 function getOriginalName()
99 {
100 return($this->orig_name);
101 }
104 /*! \brief Retunrs the filters name.
105 * @param The name of the filter
106 */
107 function getCurrentName()
108 {
109 return($this->name);
110 }
113 /*! \brief Generates the <HTML> content, to edit the filter settings.
114 * @return String HTML form.
115 */
116 function execute()
117 {
118 plugin::execute();
120 $smarty = get_smarty();
122 // Build up HTML compliant html output
123 $queries = array();
124 foreach($this->queries as $key => $query){
125 $query['filter'] = htmlentities($query['filter'],ENT_COMPAT,'UTF-8');
126 $queries[$key] = $query;
127 }
129 // Build up list of hard coded filters
130 $filter= $this->listing->getFilter();
132 $smarty->assign("fixedFilters", array_keys($filter->searches));
133 $smarty->assign('parent', $this->parent);
134 $smarty->assign('backends', $this->backends);
135 $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
136 $smarty->assign('queries', $queries);
137 $smarty->assign('share', $this->share);
138 $smarty->assign('enable', $this->enabled);
139 $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
140 $smarty->assign('selectedCategories', $this->selectedCategories);
141 $smarty->assign('availableCategories', array_unique($this->listing->categories));
142 return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
143 }
146 /*! \brief Keep values entered in the input form of the dialog. (POST/GET)
147 */
148 function save_object()
149 {
150 if(isset($_POST['userFilterEditor'])){
152 // Get posted strings
153 foreach(array('name','description', 'parent') as $attr){
154 if(isset($_POST[$attr])){
155 $this->$attr = get_post($attr);
156 }
157 }
159 // Filter needs special handling, it may contain charactes like < and >
160 // wich are stipped out by get_post() && validate()
161 foreach($this->queries as $key => $query){
162 if(isset($_POST['filter_'.$key])){
163 $f = get_post('filter_'.$key);
164 $this->queries[$key]['filter'] = $f;
165 $this->queries[$key]['backend'] = get_post('backend_'.$key);
166 }
167 }
169 foreach($this->queries as $key => $query){
170 if(isset($_POST['removeQuery_'.$key])){
171 unset($this->queries[$key]);
172 $this->queries = array_values($this->queries);
173 }
174 }
176 // Get posted flags
177 $this->share = isset($_POST['shareFilter']);
178 $this->enable = isset($_POST['enableFilter']);
180 // Get additional category
181 if(isset($_POST['addCategory'])){
182 if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
183 $this->selectedCategories[] = get_post('manualCategory');
184 }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
185 $this->selectedCategories[] = get_post('availableCategory');
186 }
187 $this->selectedCategories = array_unique($this->selectedCategories);
188 }
190 // Remove categories
191 if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
192 foreach($_POST['usedCategory'] as $cat){
193 if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
194 }
195 }
197 // Add new query
198 if(isset($_POST['addQuery'])){
200 $filter= $this->listing->getFilter();
201 $backend = 'LDAP';
202 $query = "(objectClass=*)";
203 if(isset($filter->searches[$this->parent])){
205 $tmp = $filter->searches[$this->parent];
206 if(isset($tmp['query'][count($this->queries)])){
207 $query = $tmp['query'][count($this->queries)]['filter'];
208 $backend = $tmp['query'][count($this->queries)]['backend'];
209 }elseif(isset($tmp['query']['filter'])){
210 $query = $tmp['query']['filter'];
211 $backend = $tmp['query']['backend'];
212 }
213 }
215 $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query," "));
216 }
217 }
218 }
221 /*! \brief Validate user input
222 * @return Array An Array containing potential error messages
223 */
224 function check()
225 {
226 $msgs = plugin::check();
228 // Check if the name is given
229 if(empty($this->name)){
230 $msgs[] = msgPool::required(_("Name"));
231 }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
233 // Check for a valid name, no special chars here - in particular no ;
234 $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
235 }
237 // Description is a must value.
238 if(empty($this->description)){
239 $msgs[] = msgPool::required(_("Description"));
240 }
242 // Count the number of opening and closing brackets - exclude escaped ones.
243 foreach($this->queries as $key => $query){
244 $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
245 $o = substr_count($f, '(');
246 $c = substr_count($f, ')');
247 if($o != $c){
248 $msgs[] = sprintf(_("Error in filter #%s: %s opening and %s closing brackets detected!"), bold($key+1), bold($o), bold($c));
249 }
250 }
252 return($msgs);
253 }
256 /*! \brief Transforms the entered values into a filter object (array) which is useable
257 * for the userFilter overview dialog.
258 * @return Returns transformed filter data.
259 */
260 function save()
261 {
262 $ret= array();
263 $ret['parent'] = $this->parent;
264 $ret['tag'] = $this->name;
265 $ret['description'] = $this->description;
266 $ret['categories'] = $this->selectedCategories;
267 $ret['query'] = $this->queries;
268 $ret['flags'] = array();
269 if($this->share){
270 $ret['flags'][] = "share";
271 }
272 if($this->enable){
273 $ret['flags'][] = "enable";
274 }
275 return($ret);
276 }
277 }
279 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
280 ?>