1 <?php
3 class debconf
4 {
5 var $package= "";
6 var $language= "";
7 var $loaded_template= FALSE;
8 var $template= array();
11 function debconf($language= "")
12 {
13 $this->set_language($language);
14 }
16 function set_language($language)
17 {
18 $this->language= $language;
19 }
21 function load_from_string($str)
22 {
23 $lines = split("\n",$str);
24 $this->template = array();
25 $post_name = 0;
26 $langcode = $this->language.".UTF-8";
27 $in_description = FALSE;
28 $got_local_description = FALSE;
30 foreach($lines as $line){
32 /* Reset description flag */
33 if ($in_description && !preg_match("/^ /", $line)){
34 $in_description= FALSE;
35 }
37 /* Template header */
38 if (preg_match("/^Template: /", $line)){
39 $post_name ++;
40 $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
41 $this->template[$post_name]['Name'] = $name;
42 $this->template[$post_name]['Default'] ="";
44 $got_local_description= FALSE;
45 continue;
46 }
48 /* Get type */
49 if (preg_match("/^Type: /", $line)){
50 $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
51 $this->template[$post_name]['Type']= $type;
52 continue;
53 }
55 /* Get default */
56 if (preg_match("/^Default: /", $line)){
57 $this->template[$post_name]['Default']= "";
58 $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
59 $this->template[$post_name]['Default']= $default;
60 continue;
61 }
63 /* Get description */
64 if (!$got_local_description && preg_match("/^Description: /i", $line)){
65 $description= trim(preg_replace("/^Description: (.*)$/i", "\\1", $line));
66 $this->template[$post_name]['Topic']= $description;
67 $this->template[$post_name]['Description']= "";
68 $in_description= TRUE;
69 continue;
70 }
72 /* Fill description */
73 if (!$got_local_description && $in_description){
74 $description= preg_replace("/^ (.*)$/", "\\1", $line);
75 $this->template[$post_name]['Description'].= $description;
76 continue;
77 }
79 /* Get local description */
80 if (preg_match("/^Description-$langcode: /i", $line)){
81 $description= trim(preg_replace("/^Description-$langcode: (.*)$/i", "\\1", $line));
82 $this->template[$post_name]['Topic']= $description;
83 $in_description= TRUE;
84 $got_local_description= TRUE;
85 $this->template[$post_name]['Description']= "";
86 continue;
87 }
89 /* Fill local description */
90 if ($got_local_description && $in_description){
91 $description= preg_replace("/^ (.*)$/", "\\1", $line);
92 $this->template[$post_name]['Description'].= $description;
93 continue;
94 }
96 /* Get native choices */
97 if (preg_match("/^Choices: /", $line)){
98 $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
99 $this->template[$post_name]['Choices']= $type;
100 }
102 /* Get local choices */
103 if (preg_match("/^Choices-$langcode: /", $line)){
104 $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
105 $this->template[$post_name]['Choices-local']= $type;
106 }
108 }
110 $this->loaded_template= TRUE;
112 $tmp= array();
113 foreach($this->template as $post_name => $template){
114 $template['post_name'] = "post_".$post_name;
115 $tmp[] = $template;
116 }
117 $this->template = $tmp;
119 return (TRUE);
120 }
122 function has_template()
123 {
124 return(count($this->template) != FALSE);
125 }
128 /* Check if some fields are posted */
129 function PostCheck()
130 {
131 /* Walk through all template variables */
132 foreach($this->template as $post_name => $entry){
134 /* Check if this var is set*/
135 if(isset($_POST[$entry['post_name']])){
137 /* special handling for arrays */
138 if(is_array($_POST[$entry['post_name']])){
139 $str = "";
140 foreach($_POST[$entry['post_name']] as $val){
141 $str.= $val.", ";
142 }
143 $str = preg_replace("/\,\ $/","",$str);
144 $this->template[$post_name]['Default'] = $str;
145 }else{
146 $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
147 }
148 }
149 }
151 foreach($this->template as $post_name => $entry){
152 if(isset($_POST["multi-".$entry['post_name']])){
153 $this->template[$post_name]['Default']= "";
154 foreach($_POST as $name => $value){
155 if(preg_match("/".$entry['post_name']."-multi-/",$name)){
156 $this->template[$post_name]['Default'] .= $value.", ";
157 }
158 }
159 $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
160 }
161 }
162 }
165 /* This funtion sets the defualt value */
166 function SetDefault($var,$val)
167 {
168 if ($this->loaded_template) {
169 foreach($this->template as $key => $tmp){
170 if($tmp['Name'] == $var ){
171 $this->template[$key]['Default'] = $val;
172 }
173 }
174 }
175 }
178 /* Display all possible options in html*/
179 function get_dialog()
180 {
181 if ($this->loaded_template) {
182 $result= "<table summary=''>";
184 foreach ($this->template as $post_name => $entry){
186 $types= array("boolean" => "", "multiselect" => "", "note" => "",
187 "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
189 /* Check if type is available */
190 if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
192 /* Produce type specific output */
193 $fn= "render_".$entry['Type'];
194 $str = $this->$fn($entry);
195 if(!empty($str)){
196 $result.=$str."<tr><td colspan='2'><p class='seperator'> </p></td></tr>";
197 }
198 } else {
199 //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
200 }
201 }
204 $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
205 return ($result);
206 } else {
207 return _("This package has no debconf options.");
208 }
209 }
212 function render_boolean($data)
213 {
214 $post_name= $data['post_name'];
215 $result="
216 <tr>
217 <td valign='top' style='width:100%'>
218 <h2>".$data['Topic']."</h2>".$data['Description']."
219 </td>
220 <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
222 foreach(array("true","false") as $value){
223 if($data['Default'] == $value){
224 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
225 }else{
226 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
227 }
228 $result.="<br>";
229 }
231 $result.= "
232 </td>
233 </tr>
234 ";
236 return ($result);
237 }
240 function render_multiselect($data)
241 {
242 $post_name= $data['post_name'];
243 if (preg_match('/\$\{/', $data['Choices'])){
244 $result= $this->render_string($data);
245 } else {
246 $choices= "";
247 foreach (split(", ", $data['Choices']) as $choice){
248 $choices[]= $choice;
249 }
252 $result="
253 <tr>
254 <td valign='top'>
255 <h2>".$data['Topic']."</h2>".$data['Description']."
256 </td>
257 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
258 <input type='hidden' name='multi-".$post_name."' value='1'>
259 ";
261 $defs = split(", ",$data['Default']);
262 foreach($choices as $value){
263 if(in_array($value,$defs)){
264 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
265 }else{
266 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
267 }
268 }
270 $result .= "</td>
271 </tr>
272 ";
273 }
275 return ($result);
276 }
279 function render_note($data)
280 {
281 /* Ignore notes, they do not makes sense, since we don't get any
282 chance to test entered values... */
283 return ("");
284 }
287 function render_password($data)
288 {
289 $result= "";
290 $result.= "<tr><td valign='top'>";
291 $result.= "<h2>".$data['Topic']."</h2>".$data['Description']."</td><td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\"> <input type='text' name='".$data['post_name']."' value='".$data['Default']."'></b><br><br>";
292 $result.= $data['Description'];
293 $result.= "</td>";
295 return ($result);
296 }
299 function render_select($data)
300 {
301 $post_name= $data['post_name'];
303 if (preg_match('/\$\{/', $data['Choices'])){
304 $choices= array("Need to use some text...");
305 } else {
306 $choices= "";
307 foreach (split(", ", $data['Choices']) as $choice){
308 $choices[]= $choice;
309 }
310 }
313 $result="
315 <tr>
316 <td valign='top'>
317 <h2>".$data['Topic']."</h2>".$data['Description']."
318 </td>
319 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
320 ";
322 foreach($choices as $value){
323 if($data['Default'] == $value){
324 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
325 }else{
326 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
327 }
328 }
330 $result.= "
332 </td>
333 </tr>
334 ";
336 return ($result);
337 }
340 function render_string($data)
341 {
342 $result= "
343 <tr>
344 <td valign='top'>
345 <h2>".$data['Topic']."</h2>".$data['Description']."
346 </td>
347 <td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
348 <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
349 </td>
350 </tr>
351 ";
353 return ($result);
354 }
357 function render_text($data)
358 {
359 /* Ignore text messages, they are normally used for status hints. */
360 return ("");
361 }
364 function render_title($data)
365 {
366 /* Ignore text messages, they are normally used for status hints. */
367 return ("");
368 }
370 }
373 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
374 ?>