1 <?php
3 class faiDebconfConfigurations extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array();
8 var $objectclasses = array();
9 var $FAIstate = "";
10 var $DebconfConfigurations = array();
11 var $DebconfDefaults = array();
12 var $DebconfAll = array();
13 var $to_remove = array();
14 var $removed = array();
15 var $package_list = FALSE;
16 var $parent = FALSE;
17 var $packages = array();
19 /* Some settings */
20 var $query_gosa_si = TRUE;
21 var $merge_defaults_into_view = TRUE;
22 var $bool_mapping = array(
23 "0" => 'false',
24 "1" => 'true',
25 "true" => 'true',
26 "false" => 'false'
27 );
29 /*! \brief Configure all debconf configurations of a given package list
30 @param Object $config GOsa configuration object;
31 @param String The DN of the current package list.
32 */
33 function faiDebconfConfigurations (&$config, $dn= NULL, &$parent)
34 {
35 plugin::plugin ($config, $dn);
37 /* Store some information we need from the parent object */
38 $this->FAIstate = $parent->FAIstate;
39 $this->package_list = $parent->cn;
40 $this->parent = $parent;
42 /* Get the list of configurations from the parent object */
43 $this->DebconfConfigurations = $this->parent->ConfiguredPackages;
45 /* Load packages */
46 foreach(array_keys($this->parent->usedPackages) as $package) {
47 $this->packages[$package] = $package;
48 }
49 ksort($this->packages);
52 /* Determine and assemble release name */
53 $release = $this->parent->parent->parent->fai_release;
54 $tmp= preg_replace('/[,]*'.preg_quote(get_ou('faiBaseRDN'), '/').'.*$/', '', $release);
55 $tmp= preg_replace('/ou=/', '', $tmp);
56 $rev= array_reverse(split(',', $tmp));
57 $this->release= "/";
58 foreach ($rev as $part){
59 $this->release.= "/$part";
60 }
61 $this->release= preg_replace('#^[/]*#', '', $this->release);
63 /* Get the list of debconf configuration for the given package list */
64 $ldap = $config->get_ldap_link();
66 /* Get the defaults from gosa-si */
67 if ($this->query_gosa_si == TRUE) {
68 $q = new gosaSupportDaemon();
69 $ret = $q->FAI_get_packages($this->release,array("package","template"),$this->packages);
70 if($q->is_error()){
71 msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
72 }
73 else {
74 foreach($ret as $res) {
75 $package = $res['PACKAGE'];
76 if (isset($res['TEMPLATE'])) {
77 $template = base64_decode($res['TEMPLATE']);
78 $debconf_handle = new debconf(preg_replace('/_.*$/', '', get_browser_language()));
79 $debconf_handle->load_from_string($template);
80 $templates = $debconf_handle->template;
81 unset($debconf_handle);
82 foreach($templates as $template) {
83 $template['Save'] = FALSE;
84 if (!isset($this->DebconfDefaults[$package][$template['Name']])) {
85 $this->DebconfDefaults[$package][$template['Name']] = $template;
86 }
87 }
88 }
89 }
90 }
91 }
92 }
95 /*! \brief Return a save toggle */
96 function getSaveToggle ($identifier) {
97 list($package, $name) = $this->getIdentifierComponents(base64_decode($identifier));
99 /* We do not need a save toggle for manually entered parameters */
100 if (isset($this->DebconfDefaults[$package][$name])) {
101 $template = "
102 <input type='hidden' name='save_%s' value='%s'>
103 <input type='image' src='%s' width='18' name='toggle_%s_%s' value='%s' title='%s'>
104 ";
105 if (isset($this->DebconfConfigurations[$package][$name])) {
106 if ($this->DebconfConfigurations[$package][$name]['Save'] == TRUE) {
107 return(sprintf($template, $identifier, '0', 'images/save.png', 'nosave', $identifier, '0', _("Currently saved")));
108 }
109 else {
110 return(sprintf($template, $identifier, '1', 'images/empty.png', 'save', $identifier, '1', _("Currently not saved")));
111 };
112 } else {
113 /* Defaults w/o corresponding configuration entry are not saved by default
114 * so give the user the chance to change that */
115 return(sprintf($template, $identifier, '1', 'images/empty.png', 'save', $identifier, '1', _("Currently not saved")));
116 }
117 }
118 }
119 /*! \brief Returns a html interface, that allow to modify
120 the package attributes.
121 @return String HTML content/ui for this plugin
122 */
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 ksort($this->DebconfConfigurations);
130 if(is_object($this->dialog)) {
131 $this->dialog->save_object();
132 $display = $this->dialog->execute();
133 return($display);
134 }
136 $divlist = new divSelectBox("faiDebconfConfigurations");
137 $divlist->setHeight(400);
139 /* Define header */
140 $field1 = array ("string" => "<b>" . _("Package") . "</b>");
141 $field2 = array ("string" => "<b>" . _("Variable") . "</b>");
142 $field3 = array ("string" => "<b>" . _("Type"). "</b>");
143 $field4 = array ("string" => "<b>" . _("Value") . "</b>");
144 $field5 = array ("string" => "");
145 $divlist->AddEntry(array($field1,$field2,$field3,$field4,$field5));
147 /* Define some vars we will need in the following loop */
148 $str_reset = "<input type='image' title='"._("Reset")."'
149 src='images/lists/reload.png' name='reset_%s'> ";
150 $str_delete = "<input type='image' title='"._("Delete")."'
151 src='images/lists/trash.png' name='reset_%s'> ";
152 $str_edit = "<input type='image' title='"._("Edit")."'
153 src='images/lists/edit.png' name='edit_%s'> ";
154 $str_input = "<input type=text length=100 value=\"%s\" name='val_%s' %s>";
156 /* Merge defaults into display */
157 if ($this->merge_defaults_into_view) {
158 $defaults = array();
159 foreach($this->DebconfDefaults as $package => $package_data){
160 foreach($package_data as $debconf_info) {
161 $name = $debconf_info['Name'];
162 $value = $debconf_info['Default'];
163 $type = $debconf_info['Type'];
164 if(!in_array($type,array("boolean","multiselect","password","select","string","text"))){
165 continue;
166 }
167 $defaults[$package][$name] = array(
168 'Name' => $name,
169 'Value' => $value,
170 'Type' => $type,
171 'Save' => FALSE
172 );
173 }
174 }
175 $this->DebconfAll = $this->DebconfConfigurations;
177 foreach($this->packages as $package) {
178 if (isset($defaults[$package])) {
179 if (isset($this->DebconfConfigurations[$package])) {
180 $this->DebconfAll[$package] = array_merge($defaults[$package], $this->DebconfConfigurations[$package]);
181 }else {
182 $this->DebconfAll[$package] = $defaults[$package];
183 }
185 }
187 }
188 } else {
189 $this->DebconfAll = $this->DebconfConfigurations;
190 }
191 ksort($this->DebconfAll);
193 /* Build package list */
194 $defaults_count = count($this->DebconfDefaults);
195 foreach($this->DebconfAll as $package => $package_data) {
196 foreach($package_data as $debconf_info) {
197 /* Do not show removed entries */
198 if (isset($debconf_info['Status']) && $debconf_info['Status'] == 'removed') {
199 continue;
200 }
202 /* Base64-encoded identifier for every package/debconf_data combination
203 * is in the form of packge/Name e.g. locales/locales/locales_to_generate */
204 $identifier = base64_encode(sprintf("%s/%s",$package, $debconf_info['Name']));
206 /* Define actions for this entry */
207 $actions = "";
208 if(!preg_match('/^freeze/', $this->FAIstate)) {
209 /* Don't enable actions if no gosa-si defaults available */
210 $show_reset = FALSE;
211 $show_edit = FALSE;
212 $show_blank = FALSE;
213 if ($defaults_count != 0) {
214 $name = $debconf_info['Name'];
216 if (isset($this->DebconfDefaults[$package][$name])) {
217 $show_edit = TRUE;
218 }
219 else {
220 $show_delete = TRUE;
221 }
223 if (isset($this->DebconfConfigurations[$package][$name]) && isset($this->DebconfDefaults[$package][$name])) {
224 $val = $this->DebconfConfigurations[$package][$name]['Value'];
225 $default_val = $this->DebconfDefaults[$package][$name]['Default'];
226 /* Make values comparable */
227 if (is_array($val)) {
228 $val = "";
229 }
230 if ($val != $default_val) {
231 $show_reset = TRUE;
232 }
233 else {
234 $show_blank = TRUE;
235 }
236 }elseif (!isset($this->DebconfConfigurations[$package][$name]) && isset($this->DebconfDefaults[$package][$name])) {
237 $show_blank = TRUE;
238 }
239 }
241 if ($show_reset) {
242 $actions .= sprintf("$str_reset", $identifier);
243 } elseif ($show_blank) {
244 $actions .= $this->getSaveToggle($identifier);
245 } else {
246 $actions .= sprintf("$str_delete", $identifier);
247 }
248 if ($show_edit) {
249 $actions .= sprintf("$str_edit", $identifier);
250 }
251 }
253 /* Do not allow editing if FAIstate is freeze */
254 $input_disabled = "";
255 if (preg_match('/^freeze/', $this->FAIstate)) {
256 $input_disabled = "disabled";
257 }
259 /* Pre-Fill input field with the defined value */
260 $input_value = "";
261 if (!empty($debconf_info['Value'])) {
262 $input_value = htmlentities($debconf_info['Value']);
263 }
264 $input_field = sprintf("$str_input", $input_value, $identifier, $input_disabled);
267 $field1 = array( "string" => $package, "attach" => "style='width:120px;'" );
268 $field2 = array( "string" => $debconf_info['Name']);
269 $field3 = array( "string" => $debconf_info['Type'],"attach" => "style='width:100px;'");
270 $field4 = array( "string" => $input_field);
271 $field5 = array( "string" => $actions, "attach" => "style='width:60px; border-right:0px;'");
272 $divlist->AddEntry(array($field1, $field2, $field3, $field4, $field5));
273 }
274 }
275 /* Fill templating stuff */
276 $smarty = get_smarty();
277 $smarty->assign("divlist", $divlist->DrawList());
278 $smarty->assign("headline", sprintf(_("Debconf information for package list '%s'"), $this->package_list));
279 return($smarty->fetch(get_template_path('faiDebconfConfigurations.tpl', TRUE)));
280 }
283 function normalize_bool($bool_value, $check_mode=FALSE) {
284 $bool_value = strtolower($bool_value);
285 if(isset($this->bool_mapping[$bool_value])) {
286 return $this->bool_mapping[$bool_value];
287 } else {
288 return ($check_mode) ? FALSE : $bool_value;
289 }
290 }
292 /*! \brief Return the components of an identifier as an array of two elements */
293 function getIdentifierComponents($identifier) {
294 $identifier_components = explode('/', $identifier);
295 $package = $identifier_components[0];
296 $name = preg_replace("/^".$package."\//", "", $identifier, 1);
297 return(array($package, $name));
298 }
300 function DeleteEntry($identifier) {
301 list($package, $name) = $this->getIdentifierComponents($identifier);
302 if (isset($this->DebconfConfigurations[$package][$name])) {
303 $this->DebconfConfigurations[$package][$name]['Save'] = FALSE;
304 if (!isset($this->DebconfDefaults[$package][$name])) {
305 $this->DebconfConfigurations[$package][$name]['Status'] = "removed";
306 } else {
307 $this->DebconfConfigurations[$package][$name]['Value'] = $this->DebconfDefaults[$package][$name]['Default'];
308 }
309 }
311 /* Check if the package entry is still justified */
312 if (is_array($this->DebconfConfigurations[$package])) {
313 if (!count($this->DebconfConfigurations[$package]) > 0) {
314 unset($this->DebconfConfigurations[$package]);
315 }
316 }
317 }
319 function UpdateEntry($identifier, $newval="") {
320 list($package, $name) = $this->getIdentifierComponents($identifier);
321 if (!isset($this->DebconfConfigurations[$package][$name])) {
322 $this->DebconfConfigurations[$package][$name] = $this->DebconfDefaults[$package][$name];
323 }
325 /* Normalize boolean values */
326 if ($this->DebconfConfigurations[$package][$name]['Type'] == 'boolean') {
327 $newval = $this->normalize_bool($newval);
328 }
330 /* If the value changed, toggle the save flag */
331 $previous_value = "";
332 if (isset($this->DebconfConfigurations[$package][$name]['Value'])) {
333 $previous_value = $this->DebconfConfigurations[$package][$name]['Value'];
334 }
335 else {
336 $previous_value = $this->DebconfConfigurations[$package][$name]['Default'];
337 }
338 if (is_array($previous_value)) {
339 $previous_value = "";
340 }
342 if ($previous_value != $newval) {
343 $this->DebconfConfigurations[$package][$name]['Save'] = TRUE;
344 }
346 $this->DebconfConfigurations[$package][$name]['Value'] = $newval;
349 /* Set empty values to an array */
350 if (isset($this->DebconfConfigurations[$package][$name]) && !$this->DebconfConfigurations[$package][$name]['Value']) {
351 $this->DebconfConfigurations[$package][$name]['Value'] = array();
352 }
353 }
355 /*!\brief Toggle the save status of a given identifier */
356 function ToggleSave($identifier, $value) {
357 list($package, $name) = $this->getIdentifierComponents($identifier);
359 $save = ($value == 1) ? TRUE : FALSE;
361 if (!isset($this->DebconfConfigurations[$package][$name])) {
362 $this->DebconfConfigurations[$package][$name] = $this->DebconfDefaults[$package];
363 }
365 $this->DebconfConfigurations[$package][$name]['Save'] = $save;
366 }
368 /* Save data to object */
369 function save_object()
370 {
371 /* Process values */
372 if (isset($_POST['update_values'])) {
373 foreach($this->DebconfAll as $package => $package_data) {
374 foreach($package_data as $debconf_info) {
375 $identifier = sprintf("%s/%s",$package, $debconf_info['Name']);
376 $post_key = sprintf("val_%s", base64_encode($identifier));
377 if (isset($_POST[$post_key])) {
378 $this->UpdateEntry($identifier, get_post($post_key));
379 }
380 else {
381 $this->UpdateEntry($identifier);
382 }
383 }
384 }
385 }
387 foreach($_POST as $key => $value) {
388 if (preg_match("/^toggle_save_/", $key)) {
389 $identifier = base64_decode(str_replace("toggle_save_", "", $key));
390 $this->ToggleSave($identifier, 1);
391 }
393 if(preg_match("/^toggle_nosave_/", $key)) {
394 $identifier = base64_decode(str_replace("toggle_nosave_", "", $key));
395 $this->ToggleSave($identifier, 0);
396 }
398 if (preg_match("/^reset_/", $key)) {
399 $identifier = base64_decode(str_replace("reset_", "", $key));
400 $this->DeleteEntry($identifier);
401 break;
402 }
404 if (preg_match("/^edit_/", $key)) {
405 $identifier = base64_decode(str_replace("edit_", "", $key));
406 list($package, $name) = $this->getIdentifierComponents($identifier);
407 $this->dialog = new faiPackageConfiguration($this->config, $this->parent->dn, $package, $this->release, $this->DebconfConfigurations[$package]);
408 $this->dialog->used_by_faiDebconfConfigurations = TRUE;
409 break;
410 }
411 if (preg_match("/^NewDebconfEntry$/", $key)) {
412 $this->dialog = new NewDebconfConfiguration($this->config, $this->parent->dn, $this);
413 $this->is_dialog = TRUE;
414 break;
415 }
416 }
418 /* Cancel add debconf dialog */
419 if (isset($_POST['cancel_AddDeconf'])) {
420 $this->dialog = FALSE;
421 }
423 /* Save add debconf dialog */
424 if (isset($_POST['save_AddDebconf'])) {
425 $this->dialog->save_object();
426 $msgs = $this->dialog->check();
427 if (count($msgs)>0) {
428 foreach($msgs as $msg){
429 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
430 }
431 }else {
432 $new_config = $this->dialog->DebconfConfiguration;
433 $this->dialog = FALSE;
434 $package = $new_config['Package'];
435 $name = $new_config['FAIvariable'];
436 $data = array(
437 'Name' => $name,
438 'Value' => $new_config['FAIvariableContent'],
439 'Type' => $new_config['FAIvariableType'],
440 'Save' => TRUE,
441 'Status' => 'manually_added',
442 );
443 $this->DebconfConfigurations[$package][$name] = $data;
444 }
445 }
447 /* Handle the classical debconf dialog */
448 /* Cancel configuration */
449 if(isset($_POST['CancelObjectConfig'])){
450 $this->dialog = false;
451 $this->is_dialog=false;
452 }
454 /* Save Configuration */
455 if(isset($_POST['SaveObjectConfig'])){
456 if(!preg_match("/^freeze/", $this->FAIstate)){
457 $result = $this->dialog->save();
458 foreach($this->packages as $package) {
459 if (isset($result[$package])) {
460 foreach($result[$package] as $attr => $data) {
461 $identifier = sprintf("%s/%s", $package, $attr);
462 $this->UpdateEntry($identifier, $data['Value']);
463 $this->ToggleSave($identifier, $data['Save']);
464 }
465 }
466 }
467 }
468 $this->dialog = false;
469 $this->is_dialog=false;
470 }
472 }
474 /* Check supplied data */
475 function check()
476 {
477 /* Call common method to give check the hook */
478 $message= plugin::check();
479 $message = array();
481 foreach($this->DebconfConfigurations as $package => $package_data) {
482 foreach($package_data as $debconf_info) {
483 /* Do not show errors for values which are not saved to LDAP */
484 if (!$debconf_info['Save'] or (isset($debconf_info['Status']) && $debconf_info['Status']=='removed')) {
485 continue;
486 }
487 if ($debconf_info['Type'] == 'boolean') {
488 if (!empty($debconf_info['Value'])) {
489 $value = strtolower($debconf_info['Value']);
490 } else {
491 $value = "";
492 }
494 if (!$this->normalize_bool($value, TRUE)) {
495 $message[] = sprintf(_("The specified value '%s' for variable '%s' is not valid with the specified boolean variable type.<br><i>Allowed values: true, false, 1, 0.</i>"), $debconf_info['Value'], $debconf_info['Name']);
496 }
497 }
498 }
499 }
501 return ($message);
502 }
504 function save()
505 {
506 $this->save_object();
507 return($this->DebconfConfigurations);
508 }
509 }
510 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
511 ?>