Code

Updated client dns handling
[gosa.git] / include / class_debconfTemplate.inc
1 <?php
3 class debconf
4 {
5   var $package= "";
6   var $language= "";
7   var $loaded_template= FALSE;
8   var $template_directory= "";
9   var $template= array();
12   function debconf($package= "", $language= "")
13   {
14     $this->set_language($language);
15     $this->set_package($package);
16   }
19   function set_package($package)
20   {
21     $this->package= $package;
22   }
25   function set_template_directory($directory)
26   {
27     if (is_dir($directory) && is_readable($directory)){
28       $this->template_directory = $directory;
29       return TRUE;
30     }
32     $this->template_directory= "";
33     return FALSE;
34   }
37   function set_language($language)
38   {
39     $this->language= $language;
40   }
43   function load()
44   {
45     if( TRUE === $this->has_template() ) {
46     
47       /* Try to load package based template file */
48       $this->template= array();
50       /* Read template array */
51       $post_name             = 0;
52       $langcode              = $this->language.".UTF-8";
53       $in_description        = FALSE;
54       $got_local_description = FALSE;
56       /* get filename */
57       $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
59       /* Check if file is readable */
60       if (!is_file($filename) || !is_readable($filename)){
61         return(FALSE);
62       }
64       /* Open file and read content line by line */
65       $fh= fopen($filename, 'r');
67       /* While the file handle is valid && there is still data to read -> parse configuration file */
68       while ($fh && !feof($fh)){
69         $line= fgets($fh, 1024);
71         /* Reset description flag */
72         if ($in_description && !preg_match("/^ /", $line)){
73           $in_description= FALSE;
74         }
76         /* Template header */
77         if (preg_match("/^Template: /", $line)){
78           $post_name ++; 
79           $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
80           $this->template[$post_name]['Name'] = $name;
81           $this->template[$post_name]['Default'] ="";
82          
83           $got_local_description= FALSE;
84           continue;
85         }
87         /* Get type */
88         if (preg_match("/^Type: /", $line)){
89           $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
90           $this->template[$post_name]['Type']= $type;
91           continue;
92         }
94         /* Get default */
95         if (preg_match("/^Default: /", $line)){
96           $this->template[$post_name]['Default']= "";
97           $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
98           $this->template[$post_name]['Default']= $default;
99           continue;
100         }
102         /* Get description */
103         if (!$got_local_description && preg_match("/^Description: /", $line)){
104           $this->template[$post_name]['Description']= "";
105           $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
106           $this->template[$post_name]['Topic']= $description;
107           $this->template[$post_name]['Description']= "";
108           $in_description= TRUE;
109           continue;
110         }
112         /* Fill description */
113         if (!$got_local_description && $in_description){
114           $description= preg_replace("/^ (.*)$/", "\\1", $line);
115           $this->template[$post_name]['Description'].= $description;
116           continue;
117         }
119         /* Get local description */
120         if (preg_match("/^Description-$langcode: /", $line)){
121           $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
122           $this->template[$post_name]['Topic']= $description;
123           $in_description= TRUE;
124           $got_local_description= TRUE;
125           $this->template[$post_name]['Description']= "";
126           continue;
127         }
129         /* Fill local description */
130         if ($got_local_description && $in_description){
131           $description= preg_replace("/^ (.*)$/", "\\1", $line);
132           $this->template[$post_name]['Description'].= $description;
133           continue;
134         }
136         /* Get native choices */
137         if (preg_match("/^Choices: /", $line)){
138           $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
139           $this->template[$post_name]['Choices']= $type;
140         }
142         /* Get local choices */
143         if (preg_match("/^Choices-$langcode: /", $line)){
144           $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
145           $this->template[$post_name]['Choices-local']= $type;
146         }
148       }
150       fclose($fh);
151       $this->loaded_template= TRUE;
152       
153       $tmp= array();
154       foreach($this->template as $post_name => $template){
155         $template['post_name'] = "post_".$post_name;
156         $tmp[] = $template;
157       }
158       $this->template = $tmp;
160       return (TRUE);
161     }
163     $this->loaded_template= FALSE;
164     return (FALSE);
165   }
168   function has_template()
169   {
170     /* Reject requests, if parameters are not set */
171     if ($this->package == "" || $this->template_directory == ""){
172       return (FALSE);
173     }
174     $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
175     return (is_file($filename) && is_readable($filename));
176   }
179   /* Check if some fields are posted */
180   function PostCheck()
181   {
182     /* Walk through all template variables */
183     foreach($this->template as $post_name => $entry){
185       /* Check if this var is set*/
186       if(isset($_POST[$entry['post_name']])){
188         /* special handling for arrays */
189         if(is_array($_POST[$entry['post_name']])){
190           $str = "";
191           foreach($_POST[$entry['post_name']] as $val){
192             $str.= $val.", ";
193           }
194           $str = preg_replace("/\,\ $/","",$str);
195           $this->template[$post_name]['Default'] = $str;
196         }else{
197           $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
198         }
199       }
200     }
201     
202     foreach($this->template as $post_name => $entry){
203       if(isset($_POST["multi-".$entry['post_name']])){ 
204         $this->template[$post_name]['Default']= "";
205         foreach($_POST as $name => $value){
206           if(preg_match("/".$entry['post_name']."-multi-/",$name)){
207             $this->template[$post_name]['Default'] .= $value.", ";
208           }
209         } 
210         $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
211       }
212     }
215   }
218   /* This funtion sets the defualt value */
219   function SetDefault($var,$val)
220   {
221     if ($this->loaded_template) {
222       foreach($this->template as $key => $tmp){
223         if($tmp['Name'] == $var ){
224           $this->template[$key]['Default'] = $val;
225         }
226       }
227     }
228   }
231   /* Display all possible options in html*/
232   function get_dialog()
233   {
234     if ($this->loaded_template) {
235       $result= "<table summary=''>";
237       foreach ($this->template as $post_name => $entry){
239         $types= array("boolean" => "", "multiselect" => "", "note" => "",
240             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
242         /* Check if type is available */
243         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
245           /* Produce type specific output */
246           $fn= "render_".$entry['Type'];
247           $str = $this->$fn($entry);
248           if(!empty($str)){
249             $result.=$str."<tr><td colspan='2'><p class='seperator'>&nbsp;</p></td></tr>";
250           }
251         } else {
252           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
253         }
254       }
256     
257       $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
258       return ($result);
259     } else {
260       return _("This package has no debconf options.");
261     }
262   }
265   function render_boolean($data)
266   {
268     $post_name= $data['post_name'];
269     $result="
270       <tr>
271       <td valign='top' style='width:100%'>
272       <h2>".$data['Topic']."</h2>".$data['Description']."
273       </td>
274       <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
276     foreach(array("true","false") as $value){
277       if($data['Default'] == $value){
278         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
279       }else{
280         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
281       }
282       $result.="<br>";
283     }
285     $result.= "
286       </td>
287       </tr>
288       ";
290     return ($result);
291   }
294   function render_multiselect($data)
295   {
296     $post_name= $data['post_name'];
297     if (preg_match('/\$\{/', $data['Choices'])){
298       $result= $this->render_string($data);
299     } else {
300       $choices= "";
301       foreach (split(", ", $data['Choices']) as $choice){
302         $choices[]= $choice;
303       }
306       $result="
307         <tr>
308         <td valign='top'>
309         <h2>".$data['Topic']."</h2>".$data['Description']."
310         </td>
311         <td valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
312           <input type='hidden' name='multi-".$post_name."' value='1'>
313         ";
314         
315       $defs = split(", ",$data['Default']);
316       foreach($choices as $value){
317         if(in_array($value,$defs)){
318           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
319         }else{
320           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
321         }
322       }
324     $result .=    "</td>
325         </tr>
326         ";
327     }    
329     return ($result);
330   }
333   function render_note($data)
334   {
335     /* Ignore notes, they do not makes sense, since we don't get any
336        chance to test entered values... */
337     return ("");
338   }
341   function render_password($data)
342   {
343     $result=  "";
344     $result.= "<tr><td valign='top'>";
345     $result.= "<h2>".$data['Topic']."</h2>".$data['Description']."</td><td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">&nbsp;<input type='text' name='".$data['post_name']."' value='".$data['Default']."'></b><br><br>";
346     $result.= $data['Description'];
347     $result.= "</td>";
349     return ($result);
350   }
353   function render_select($data)
354   {
355     $post_name= $data['post_name'];
357     if (preg_match('/\$\{/', $data['Choices'])){
358       $choices= array("Need to use some text...");
359     } else {
360       $choices= "";
361       foreach (split(", ", $data['Choices']) as $choice){
362         $choices[]= $choice;
363       }
364     }
367     $result="
368       
369       <tr>
370       <td valign='top'>
371       <h2>".$data['Topic']."</h2>".$data['Description']."
372       </td>
373       <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
374       ";
376     foreach($choices as $value){
377       if($data['Default'] == $value){
378         $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
379       }else{
380         $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
381       }
382     }
384     $result.= "
385       
386       </td>
387       </tr>
388       ";
390     return ($result);
391   }
394   function render_string($data)
395   {
396     $result=  "
397                 <tr>
398                   <td valign='top'>
399                     <h2>".$data['Topic']."</h2>".$data['Description']."
400                   </td>
401                   <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
402                     <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
403                   </td>
404                 </tr>
405               ";
407     return ($result);
408   }
411   function render_text($data)
412   {
413     /* Ignore text messages, they are normally used for status hints. */
414     return ("");
415   }
418   function render_title($data)
419   {
420     /* Ignore text messages, they are normally used for status hints. */
421     return ("");
422   }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>