f118b56183157d953f5bbb0aba43251594292874
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 = "";
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['name'];
37 $this->description = $entry['description'];
39 foreach($entry['queries'] 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('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
135 $smarty->assign('queries', $queries);
136 $smarty->assign('share', $this->share);
137 $smarty->assign('enable', $this->enabled);
138 $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
139 $smarty->assign('selectedCategories', $this->selectedCategories);
140 $smarty->assign('availableCategories', array_unique($this->listing->categories));
141 return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
142 }
145 /*! \brief Keep values entered in the input form of the dialog. (POST/GET)
146 */
147 function save_object()
148 {
149 if(isset($_POST['userFilterEditor'])){
151 // Get posted strings
152 foreach(array('name','description', 'parent') as $attr){
153 if(isset($_POST[$attr])){
154 $this->$attr = get_post($attr);
155 }
156 }
158 // Filter needs special handling, it may contain charactes like < and >
159 // wich are stipped out by get_post() && validate()
160 foreach($this->queries as $key => $query){
161 if(isset($_POST['filter_'.$key])){
162 $f = mb_convert_encoding($_POST['filter_'.$key], 'UTF-8');
163 if(get_magic_quotes_gpc()){
164 $f = stripcslashes($f);
165 }
166 $this->queries[$key]['filter'] = $f;
167 $this->queries[$key]['backend'] = get_post('backend_'.$key);
168 }
169 }
171 foreach($this->queries as $key => $query){
172 if(isset($_POST['removeQuery_'.$key])){
173 unset($this->queries[$key]);
174 $this->queries = array_values($this->queries);
175 }
176 }
178 // Get posted flags
179 $this->share = isset($_POST['shareFilter']);
180 $this->enable = isset($_POST['enableFilter']);
182 // Get additional category
183 if(isset($_POST['addCategory'])){
184 if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
185 $this->selectedCategories[] = get_post('manualCategory');
186 }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
187 $this->selectedCategories[] = get_post('availableCategory');
188 }
189 }
191 // Remove categories
192 if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
193 foreach($_POST['usedCategory'] as $cat){
194 if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
195 }
196 }
198 // Add new query
199 if(isset($_POST['addQuery'])){
201 $filter= $this->listing->getFilter();
202 $backend = 'filterLDAP';
203 $query = "(objectClass=*)";
204 if(isset($filter->searches[$this->parent])){
205 $query = $filter->searches[$this->parent]['query']['filter'];
206 $backend = $filter->searches[$this->parent]['query']['backend'];
207 }
209 $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query," "));
210 }
211 }
212 }
215 /*! \brief Validate user input
216 * @return Array An Array containing potential error messages
217 */
218 function check()
219 {
220 $msgs = plugin::check();
222 // Check if the name is given
223 if(empty($this->name)){
224 $msgs[] = msgPool::required(_("Name"));
225 }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
227 // Check for a valid name, no special chars here - in particular no ;
228 $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
229 }
231 // Description is a must value.
232 if(empty($this->description)){
233 $msgs[] = msgPool::required(_("Description"));
234 }
236 // Count the number of opening and closing brackets - exclude escaped ones.
237 foreach($this->queries as $key => $query){
238 $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
239 $o = substr_count($f, '(');
240 $c = substr_count($f, ')');
241 if($o != $c){
242 $msgs[] = sprintf(_("Please check your filter #%s. You have '%s' opening and '%s' closing brackets!"), ($key+1),$o, $c);
243 }
244 }
246 return($msgs);
247 }
250 /*! \brief Transforms the entered values into a filter object (array) which is useable
251 * for the userFilter overview dialog.
252 * @return Returns transformed filter data.
253 */
254 function save()
255 {
256 $ret= array();
257 $ret['parent'] = $this->parent;
258 $ret['name'] = $this->name;
259 $ret['description'] = $this->description;
260 $ret['categories'] = $this->selectedCategories;
261 $ret['queries'] = $this->queries;
262 $ret['flags'] = array();
263 if($this->share){
264 $ret['flags'][] = "share";
265 }
266 if($this->enable){
267 $ret['flags'][] = "enable";
268 }
269 return($ret);
270 }
271 }
273 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
274 ?>