00f34f9cfe6e10daeef2c97926c57929e989d43f
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', 'SYSTEMS', 'FAI', 'GroupLDAP','ACL', 'OPSIPackages','APPLICATIONS','MIMETYPES');
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 = mb_convert_encoding($_POST['filter_'.$key], 'UTF-8');
164 if(get_magic_quotes_gpc()){
165 $f = stripcslashes($f);
166 }
167 $this->queries[$key]['filter'] = $f;
168 $this->queries[$key]['backend'] = get_post('backend_'.$key);
169 }
170 }
172 foreach($this->queries as $key => $query){
173 if(isset($_POST['removeQuery_'.$key])){
174 unset($this->queries[$key]);
175 $this->queries = array_values($this->queries);
176 }
177 }
179 // Get posted flags
180 $this->share = isset($_POST['shareFilter']);
181 $this->enable = isset($_POST['enableFilter']);
183 // Get additional category
184 if(isset($_POST['addCategory'])){
185 if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
186 $this->selectedCategories[] = get_post('manualCategory');
187 }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
188 $this->selectedCategories[] = get_post('availableCategory');
189 }
190 $this->selectedCategories = array_unique($this->selectedCategories);
191 }
193 // Remove categories
194 if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
195 foreach($_POST['usedCategory'] as $cat){
196 if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
197 }
198 }
200 // Add new query
201 if(isset($_POST['addQuery'])){
203 $filter= $this->listing->getFilter();
204 $backend = 'LDAP';
205 $query = "(objectClass=*)";
206 if(isset($filter->searches[$this->parent])){
208 $tmp = $filter->searches[$this->parent];
209 if(isset($tmp['query'][count($this->queries)])){
210 $query = $tmp['query'][count($this->queries)]['filter'];
211 $backend = $tmp['query'][count($this->queries)]['backend'];
212 }elseif(isset($tmp['query']['filter'])){
213 $query = $tmp['query']['filter'];
214 $backend = $tmp['query']['backend'];
215 }
216 }
218 $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query," "));
219 }
220 }
221 }
224 /*! \brief Validate user input
225 * @return Array An Array containing potential error messages
226 */
227 function check()
228 {
229 $msgs = plugin::check();
231 // Check if the name is given
232 if(empty($this->name)){
233 $msgs[] = msgPool::required(_("Name"));
234 }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
236 // Check for a valid name, no special chars here - in particular no ;
237 $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
238 }
240 // Description is a must value.
241 if(empty($this->description)){
242 $msgs[] = msgPool::required(_("Description"));
243 }
245 // Count the number of opening and closing brackets - exclude escaped ones.
246 foreach($this->queries as $key => $query){
247 $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
248 $o = substr_count($f, '(');
249 $c = substr_count($f, ')');
250 if($o != $c){
251 $msgs[] = sprintf(_("Error in filter #%s: %s opening and %s closing brackets detected!"), ($key+1),$o, $c);
252 }
253 }
255 return($msgs);
256 }
259 /*! \brief Transforms the entered values into a filter object (array) which is useable
260 * for the userFilter overview dialog.
261 * @return Returns transformed filter data.
262 */
263 function save()
264 {
265 $ret= array();
266 $ret['parent'] = $this->parent;
267 $ret['tag'] = $this->name;
268 $ret['description'] = $this->description;
269 $ret['categories'] = $this->selectedCategories;
270 $ret['query'] = $this->queries;
271 $ret['flags'] = array();
272 if($this->share){
273 $ret['flags'][] = "share";
274 }
275 if($this->enable){
276 $ret['flags'][] = "enable";
277 }
278 return($ret);
279 }
280 }
282 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
283 ?>