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() ) {
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 $fh= fopen($filename, 'r');
58 while (!feof($fh)){
59 $line= fgets($fh, 1024);
61 /* Reset description flag */
62 if ($in_description && !preg_match("/^ /", $line)){
63 $in_description= FALSE;
64 }
66 /* Template header */
67 if (preg_match("/^Template: /", $line)){
68 $post_name ++;
69 $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
70 $this->template[$post_name]['Name'] = $name;
71 $this->template[$post_name]['Default'] ="";
73 $got_local_description= FALSE;
74 continue;
75 }
77 /* Get type */
78 if (preg_match("/^Type: /", $line)){
79 $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
80 $this->template[$post_name]['Type']= $type;
81 continue;
82 }
84 /* Get default */
85 if (preg_match("/^Default: /", $line)){
86 $this->template[$post_name]['Default']= "";
87 $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
88 $this->template[$post_name]['Default']= $default;
89 continue;
90 }
92 /* Get description */
93 if (!$got_local_description && preg_match("/^Description: /", $line)){
94 $this->template[$post_name]['Description']= "";
95 $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
96 $this->template[$post_name]['Topic']= $description;
97 $this->template[$post_name]['Description']= "";
98 $in_description= TRUE;
99 continue;
100 }
102 /* Fill description */
103 if (!$got_local_description && $in_description){
104 $description= preg_replace("/^ (.*)$/", "\\1", $line);
105 $this->template[$post_name]['Description'].= $description;
106 continue;
107 }
109 /* Get local description */
110 if (preg_match("/^Description-$langcode: /", $line)){
111 $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
112 $this->template[$post_name]['Topic']= $description;
113 $in_description= TRUE;
114 $got_local_description= TRUE;
115 $this->template[$post_name]['Description']= "";
116 continue;
117 }
119 /* Fill local description */
120 if ($got_local_description && $in_description){
121 $description= preg_replace("/^ (.*)$/", "\\1", $line);
122 $this->template[$post_name]['Description'].= $description;
123 continue;
124 }
126 /* Get native choices */
127 if (preg_match("/^Choices: /", $line)){
128 $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
129 $this->template[$post_name]['Choices']= $type;
130 }
132 /* Get local choices */
133 if (preg_match("/^Choices-$langcode: /", $line)){
134 $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
135 $this->template[$post_name]['Choices-local']= $type;
136 }
138 }
140 fclose($fh);
141 $this->loaded_template= TRUE;
143 $tmp= array();
144 foreach($this->template as $post_name => $template){
145 $template['post_name'] = "post_".$post_name;
146 $tmp[] = $template;
147 }
148 $this->template = $tmp;
150 return (TRUE);
151 }
153 $this->loaded_template= FALSE;
154 return (FALSE);
155 }
158 function has_template()
159 {
160 /* Reject requests, if parameters are not set */
161 if ($this->package == "" || $this->template_directory == ""){
162 return (FALSE);
163 }
164 $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
165 return (is_file($filename) && is_readable($filename));
166 }
169 /* Check if some fields are posted */
170 function PostCheck()
171 {
172 /* Walk through all template variables */
173 foreach($this->template as $post_name => $entry){
175 /* Check if this var is set*/
176 if(isset($_POST[$entry['post_name']])){
178 /* special handling for arrays */
179 if(is_array($_POST[$entry['post_name']])){
180 $str = "";
181 foreach($_POST[$entry['post_name']] as $val){
182 $str.= $val.", ";
183 }
184 $str = preg_replace("/\,\ $/","",$str);
185 $this->template[$post_name]['Default'] = $str;
186 }else{
187 $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
188 }
189 }
190 }
192 foreach($this->template as $post_name => $entry){
193 if(isset($_POST["multi-".$entry['post_name']])){
194 $this->template[$post_name]['Default']= "";
195 foreach($_POST as $name => $value){
196 if(preg_match("/".$entry['post_name']."-multi-/",$name)){
197 $this->template[$post_name]['Default'] .= $value.", ";
198 }
199 }
200 $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
201 }
202 }
205 }
208 /* This funtion sets the defualt value */
209 function SetDefault($var,$val)
210 {
211 if ($this->loaded_template) {
212 foreach($this->template as $key => $tmp){
213 if($tmp['Name'] == $var ){
214 $this->template[$key]['Default'] = $val;
215 }
216 }
217 }
218 }
221 /* Display all possible options in html*/
222 function get_dialog()
223 {
224 if ($this->loaded_template) {
225 $result= "<table summary=''>";
227 foreach ($this->template as $post_name => $entry){
229 $types= array("boolean" => "", "multiselect" => "", "note" => "",
230 "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
232 /* Check if type is available */
233 if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
235 /* Produce type specific output */
236 $fn= "render_".$entry['Type'];
237 $str = $this->$fn($entry);
238 if(!empty($str)){
239 $result.=$str."<tr><td colspan='2'><p class='seperator'> </p></td></tr>";
240 }
241 } else {
242 //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
243 }
244 }
247 $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
248 return ($result);
249 } else {
250 return _("This package has no debconf options.");
251 }
252 }
255 function render_boolean($data)
256 {
258 $post_name= $data['post_name'];
259 $result="
260 <tr>
261 <td valign='top' style='width:100%'>
262 <h2>".$data['Topic']."</h2>".$data['Description']."
263 </td>
264 <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
266 foreach(array("true","false") as $value){
267 if($data['Default'] == $value){
268 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
269 }else{
270 $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
271 }
272 $result.="<br>";
273 }
275 $result.= "
276 </td>
277 </tr>
278 ";
280 return ($result);
281 }
284 function render_multiselect($data)
285 {
286 $post_name= $data['post_name'];
287 if (preg_match('/\$\{/', $data['Choices'])){
288 $result= $this->render_string($data);
289 } else {
290 $choices= "";
291 foreach (split(", ", $data['Choices']) as $choice){
292 $choices[]= $choice;
293 }
296 $result="
297 <tr>
298 <td valign='top'>
299 <h2>".$data['Topic']."</h2>".$data['Description']."
300 </td>
301 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
302 <input type='hidden' name='multi-".$post_name."' value='1'>
303 ";
305 $defs = split(", ",$data['Default']);
306 foreach($choices as $value){
307 if(in_array($value,$defs)){
308 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
309 }else{
310 $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
311 }
312 }
314 $result .= "</td>
315 </tr>
316 ";
317 }
319 return ($result);
320 }
323 function render_note($data)
324 {
325 /* Ignore notes, they do not makes sense, since we don't get any
326 chance to test entered values... */
327 return ("");
328 }
331 function render_password($data)
332 {
333 $result= "";
334 $result.= "<tr><td valign='top'>";
335 $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>";
336 $result.= $data['Description'];
337 $result.= "</td>";
339 return ($result);
340 }
343 function render_select($data)
344 {
345 $post_name= $data['post_name'];
347 if (preg_match('/\$\{/', $data['Choices'])){
348 $choices= array("Need to use some text...");
349 } else {
350 $choices= "";
351 foreach (split(", ", $data['Choices']) as $choice){
352 $choices[]= $choice;
353 }
354 }
357 $result="
359 <tr>
360 <td valign='top'>
361 <h2>".$data['Topic']."</h2>".$data['Description']."
362 </td>
363 <td valign='top' style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
364 ";
366 foreach($choices as $value){
367 if($data['Default'] == $value){
368 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
369 }else{
370 $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
371 }
372 }
374 $result.= "
376 </td>
377 </tr>
378 ";
380 return ($result);
381 }
384 function render_string($data)
385 {
386 $result= "
387 <tr>
388 <td valign='top'>
389 <h2>".$data['Topic']."</h2>".$data['Description']."
390 </td>
391 <td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
392 <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
393 </td>
394 </tr>
395 ";
397 return ($result);
398 }
401 function render_text($data)
402 {
403 /* Ignore text messages, they are normally used for status hints. */
404 return ("");
405 }
408 function render_title($data)
409 {
410 /* Ignore text messages, they are normally used for status hints. */
411 return ("");
412 }
414 }
417 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
418 ?>