1 <?php
3 class debconf
4 {
5 var $package= "";
6 var $language= "";
7 var $has_template= FALSE;
8 var $template_directory= "/var/lib/dpkg/info";
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 return ($this->load());
23 }
26 function set_template_directory($directory)
27 {
28 if (is_dir($directory) && is_readable($directory)){
29 $this->template_directory = $directory;
30 return TRUE;
31 }
33 $this->template_directory= "";
34 return FALSE;
35 }
38 function set_language($language)
39 {
40 $this->language= $language;
41 }
44 function load()
45 {
46 /* Reject requests, if parameters are not set */
47 if ($this->package == "" || $this->template_directory == ""){
48 return (FALSE);
49 }
51 /* Try to load package based template file */
52 $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
53 if (is_file($filename) && is_readable($filename)){
54 $this->template= array();
56 /* Read template array */
57 $post_name = 0;
58 $langcode = $this->language.".UTF-8";
59 $in_description = FALSE;
60 $got_local_description= FALSE;
62 $fh= fopen($filename, 'r');
64 while (!feof($fh)){
65 $line= fgets($fh, 1024);
67 /* Reset description flag */
68 if ($in_description && !preg_match("/^ /", $line)){
69 $in_description= FALSE;
70 }
72 /* Template header */
73 if (preg_match("/^Template: /", $line)){
74 $post_name ++;
75 $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
76 $this->template[$post_name]['Name'] = $name;
77 $this->template[$post_name]['Default'] ="";
79 $got_local_description= FALSE;
80 continue;
81 }
83 /* Get type */
84 if (preg_match("/^Type: /", $line)){
85 $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
86 $this->template[$post_name]['Type']= $type;
87 continue;
88 }
90 /* Get default */
91 if (preg_match("/^Default: /", $line)){
92 $this->template[$post_name]['Default']= "";
93 $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
94 $this->template[$post_name]['Default']= $default;
95 continue;
96 }
98 /* Get description */
99 if (!$got_local_description && preg_match("/^Description: /", $line)){
100 $this->template[$post_name]['Description']= "";
101 $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
102 $this->template[$post_name]['Topic']= $description;
103 $this->template[$post_name]['Description']= "";
104 $in_description= TRUE;
105 continue;
106 }
108 /* Fill description */
109 if (!$got_local_description && $in_description){
110 $description= preg_replace("/^ (.*)$/", "\\1", $line);
111 $this->template[$post_name]['Description'].= $description;
112 continue;
113 }
115 /* Get local description */
116 if (preg_match("/^Description-$langcode: /", $line)){
117 $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
118 $this->template[$post_name]['Topic']= $description;
119 $in_description= TRUE;
120 $got_local_description= TRUE;
121 $this->template[$post_name]['Description']= "";
122 continue;
123 }
125 /* Fill local description */
126 if ($got_local_description && $in_description){
127 $description= preg_replace("/^ (.*)$/", "\\1", $line);
128 $this->template[$post_name]['Description'].= $description;
129 continue;
130 }
132 /* Get native choices */
133 if (preg_match("/^Choices: /", $line)){
134 $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
135 $this->template[$post_name]['Choices']= $type;
136 }
138 /* Get local choices */
139 if (preg_match("/^Choices-$langcode: /", $line)){
140 $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
141 $this->template[$post_name]['Choices-local']= $type;
142 }
144 }
146 fclose($fh);
147 $this->has_template= TRUE;
149 $tmp= array();
150 foreach($this->template as $post_name => $template){
151 $template['post_name'] = "post_".$post_name;
152 $tmp[] = $template;
153 }
154 $this->template = $tmp;
156 return (TRUE);
157 }
159 $this->has_template= FALSE;
160 return (FALSE);
161 }
164 function has_template()
165 {
166 return ($this->has_template);
167 }
170 /* Check if some fields are posted */
171 function PostCheck()
172 {
173 /* Walk through all template variables */
174 foreach($this->template as $post_name => $entry){
176 /* Check if this var is set*/
177 if(isset($_POST[$entry['post_name']])){
179 /* special handling for arrays */
180 if(is_array($_POST[$entry['post_name']])){
181 $str = "";
182 foreach($_POST[$entry['post_name']] as $val){
183 $str.= $val.", ";
184 }
185 $str = preg_replace("/\,\ $/","",$str);
186 $this->template[$post_name]['Default'] = $str;
187 }else{
188 $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
189 }
190 }
191 }
193 foreach($this->template as $post_name => $entry){
194 if(isset($_POST["multi-".$entry['post_name']])){
195 $this->template[$post_name]['Default']= "";
196 foreach($_POST as $name => $value){
197 if(preg_match("/".$entry['post_name']."-multi-/",$name)){
198 $this->template[$post_name]['Default'] .= $value.", ";
199 }
200 }
201 $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
202 }
203 }
206 }
209 /* This funtion sets the defualt value */
210 function SetDefault($var,$val)
211 {
212 if($this->has_template){
213 foreach($this->template as $key => $tmp){
214 if($tmp['Name'] == $var ){
215 $this->template[$key]['Default'] = $val;
216 }
217 }
218 }
219 }
222 /* Display all possible options in html*/
223 function get_dialog()
224 {
225 if ($this->has_template){
226 $result= "<table>";
228 foreach ($this->template as $post_name => $entry){
230 $types= array("boolean" => "", "multiselect" => "", "note" => "",
231 "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
233 /* Check if type is available */
234 if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
236 /* Produce type specific output */
237 $fn= "render_".$entry['Type'];
238 $str = $this->$fn($entry);
239 if(!empty($str)){
240 $result.=$str."<tr><td colspan='2'><p class='seperator'> </p></td></tr>";
241 }
242 } else {
243 //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
244 }
245 }
248 $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
249 return ($result);
250 } else {
251 return _("This package has no debconf options.");
252 }
253 }
256 function render_boolean($data)
257 {
259 $post_name= $data['post_name'];
260 $result="
261 <tr>
262 <td valign='top' style='width:100%'>
263 <h2>".$data['Topic']."</h2>".$data['Description']."
264 </td>
265 <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
267 foreach(array("true","false") as $value){
268 if($data['Default'] == $value){
269 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
270 }else{
271 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
272 }
273 $result.="<br>";
274 }
276 $result.= "
277 </td>
278 </tr>
279 ";
281 return ($result);
282 }
285 function render_multiselect($data)
286 {
287 $post_name= $data['post_name'];
288 if (preg_match('/\$\{/', $data['Choices'])){
289 $result= $this->render_string($data);
290 } else {
291 $choices= "";
292 foreach (split(", ", $data['Choices']) as $choice){
293 $choices[]= $choice;
294 }
297 $result="
298 <tr>
299 <td valign='top'>
300 <h2>".$data['Topic']."</h2>".$data['Description']."
301 </td>
302 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
303 <input type='hidden' name='multi-".$post_name."' value='1'>
304 ";
306 $defs = split(", ",$data['Default']);
307 foreach($choices as $value){
308 if(in_array($value,$defs)){
309 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
310 }else{
311 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
312 }
313 }
315 $result .= "</td>
316 </tr>
317 ";
318 }
320 return ($result);
321 }
324 function render_note($data)
325 {
326 /* Ignore notes, they do not makes sense, since we don't get any
327 chance to test entered values... */
328 return ("");
329 }
332 function render_password($data)
333 {
334 $result= "";
335 $result.= "<tr><td valign='top'>";
336 $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>";
337 $result.= $data['Description'];
338 $result.= "</td>";
340 return ($result);
341 }
344 function render_select($data)
345 {
346 $post_name= $data['post_name'];
348 if (preg_match('/\$\{/', $data['Choices'])){
349 $choices= array("Need to use some text...");
350 } else {
351 $choices= "";
352 foreach (split(", ", $data['Choices']) as $choice){
353 $choices[]= $choice;
354 }
355 }
358 $result="
360 <tr>
361 <td valign='top'>
362 <h2>".$data['Topic']."</h2>".$data['Description']."
363 </td>
364 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
365 ";
367 foreach($choices as $value){
368 if($data['Default'] == $value){
369 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
370 }else{
371 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
372 }
373 }
375 $result.= "
377 </td>
378 </tr>
379 ";
381 return ($result);
382 }
385 function render_string($data)
386 {
387 $result= "
388 <tr>
389 <td valign='top'>
390 <h2>".$data['Topic']."</h2>".$data['Description']."
391 </td>
392 <td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
393 <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
394 </td>
395 </tr>
396 ";
398 return ($result);
399 }
402 function render_text($data)
403 {
404 /* Ignore text messages, they are normally used for status hints. */
405 return ("");
406 }
409 function render_title($data)
410 {
411 /* Ignore text messages, they are normally used for status hints. */
412 return ("");
413 }
415 }
418 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
419 ?>