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 = explode("\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'><hr></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 <h3>".$data['Topic']."</h3>".$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 $data['Description'].= '<br><br><b>'._('This debconf question is dynamically generated during package installation and requires choosing between specific options which cannot be presented here. The entered text needs to be one of the valid choices in order to take effect.').'</b>';
245 $result= $this->render_string($data);
246 } else {
247 $choices= "";
248 foreach (explode(", ", $data['Choices']) as $choice){
249 $choices[]= $choice;
250 }
253 $result="
254 <tr>
255 <td valign='top'>
256 <h3>".$data['Topic']."</h3>".$data['Description']."
257 </td>
258 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
259 <input type='hidden' name='multi-".$post_name."' value='1'>
260 ";
262 $defs = explode(", ",$data['Default']);
263 foreach($choices as $value){
264 if(in_array($value,$defs)){
265 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
266 }else{
267 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
268 }
269 }
271 $result .= "</td>
272 </tr>
273 ";
274 }
276 return ($result);
277 }
280 function render_note($data)
281 {
282 /* Ignore notes, they do not makes sense, since we don't get any
283 chance to test entered values... */
284 return ("");
285 }
288 function render_password($data)
289 {
290 $result= "";
291 $result.= "<tr><td valign='top'>";
292 $result.= "<h3>".$data['Topic']."</h3>".$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>";
293 $result.= $data['Description'];
294 $result.= "</td>";
296 return ($result);
297 }
300 function render_select($data)
301 {
302 $post_name= $data['post_name'];
304 if (preg_match('/\$\{/', $data['Choices'])){
305 $result = $this->render_multiselect($data);
306 } else {
307 $choices= "";
308 foreach (explode(", ", $data['Choices']) as $choice){
309 $choices[]= $choice;
310 }
312 $result="
314 <tr>
315 <td valign='top'>
316 <h3>".$data['Topic']."</h3>".$data['Description']."
317 </td>
318 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
319 ";
321 foreach($choices as $value){
322 if($data['Default'] == $value){
323 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
324 }else{
325 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
326 }
327 }
329 $result.= "
331 </td>
332 </tr>
333 ";
334 }
336 return ($result);
337 }
340 function render_string($data)
341 {
342 $result= "
343 <tr>
344 <td valign='top'>
345 <h3>".$data['Topic']."</h3>".$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 ?>