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 $name= "";
58 $langcode= $this->language.".UTF-8";
59 $in_description= FALSE;
60 $got_local_description= FALSE;
61 $fh= fopen($filename, 'r');
63 while (!feof($fh)){
64 $line= fgets($fh, 1024);
66 /* Reset description flag */
67 if ($in_description && !preg_match("/^ /", $line)){
68 $in_description= FALSE;
69 }
71 /* Template header */
72 if (preg_match("/^Template: /", $line)){
73 $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
74 $this->template[$name]= array();
75 $got_local_description= FALSE;
76 continue;
77 }
79 /* Get type */
80 if (preg_match("/^Type: /", $line)){
81 $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
82 $this->template[$name]['Type']= $type;
83 continue;
84 }
86 /* Get default */
87 if (preg_match("/^Default: /", $line)){
88 $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
89 $this->template[$name]['Default']= $default;
90 continue;
91 }
93 /* Get description */
94 if (!$got_local_description && preg_match("/^Description: /", $line)){
95 $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
96 $this->template[$name]['Topic']= $description;
97 $this->template[$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[$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[$name]['Topic']= $description;
113 $in_description= TRUE;
114 $got_local_description= TRUE;
115 $this->template[$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[$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[$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[$name]['Choices-local']= $type;
136 }
138 }
140 fclose($fh);
141 $this->has_template= TRUE;
142 return (TRUE);
143 }
145 $this->has_template= FALSE;
146 return (FALSE);
147 }
150 function has_template()
151 {
152 return ($this->has_template);
153 }
156 function get_dialog()
157 {
158 if ($this->has_template){
159 $result= "";
161 foreach ($this->template as $name => $entry){
163 $types= array("boolean" => "", "multiselect" => "", "note" => "",
164 "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
166 /* Check if type is available */
167 if (isset($types[$entry['Type']])){
169 /* Produce type specific output */
170 $fn= "render_".$entry['Type'];
171 $result.= $this->$fn($entry);
173 } else {
174 php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
175 }
176 }
178 return ($result);
179 } else {
180 return _("This package has no debconf options.");
181 }
182 }
185 function render_boolean($data)
186 {
187 $result= "<table width='100%' border=1>";
188 $result.= "<tr><td valign='top'>";
189 $result.= "<b>".$data['Topic']."</b><br><br>";
190 $result.= $data['Description'];
191 $result.= "</td><td width='10%' valign='top'>O Yes<br>O No</td>";
192 $result.= "</table>";
194 return ($result);
195 }
198 function render_multiselect($data)
199 {
200 if (preg_match('/\$\{/', $data['Choices'])){
201 $choices= "Need to use some text...";
202 } else {
203 $choices= "";
204 foreach (split(", ", $data['Choices']) as $choice){
205 $choices.= "[ ] ".$choice."<br>";
206 }
207 }
209 $result= "<table width='100%' border=1>";
210 $result.= "<tr><td valign='top'>";
211 $result.= "<b>".$data['Topic']."</b><br><br>";
212 $result.= $data['Description'];
213 $result.= "</td><td width='30%' valign='top'>$choices</td>";
214 $result.= "</table>";
216 return ($result);
217 }
220 function render_note($data)
221 {
222 /* Ignore notes, they do not makes sense, since we don't get any
223 chance to test entered values... */
224 return ("");
225 }
228 function render_password($data)
229 {
230 $result= "<table width='100%' border=1>";
231 $result.= "<tr><td valign='top'>";
232 $result.= "<b>".$data['Topic']." [......................]</b><br><br>";
233 $result.= $data['Description'];
234 $result.= "</td></table>";
236 return ($result);
237 }
240 function render_select($data)
241 {
242 if (preg_match('/\$\{/', $data['Choices'])){
243 $choices= "Need to use some text...";
244 } else {
245 $choices= "";
246 foreach (split(", ", $data['Choices']) as $choice){
247 $choices.= "[ ] ".$choice."<br>";
248 }
249 }
251 $result= "<table width='100%' border=1>";
252 $result.= "<tr><td valign='top'>";
253 $result.= "<b>".$data['Topic']."</b><br><br>";
254 $result.= $data['Description'];
255 $result.= "</td><td width='30%' valign='top'>$choices</td>";
256 $result.= "</table>";
258 return ($result);
259 }
262 function render_string($data)
263 {
264 $result= "<table width='100%' border=1>";
265 $result.= "<tr><td valign='top'>";
266 $result.= "<b>".$data['Topic']." [......................]</b><br><br>";
267 $result.= $data['Description'];
268 $result.= "</td></table>";
270 return ($result);
271 }
274 function render_text($data)
275 {
276 /* Ignore text messages, they are normally used for status hints. */
277 return ("");
278 }
281 function render_title($data)
282 {
283 /* Ignore text messages, they are normally used for status hints. */
284 return ("");
285 }
288 function get_template_packages()
289 {
290 }
293 function set_default($default)
294 {
295 }
298 }
300 # Example:
301 #$debconf= new debconf("libnss-ldap", "de");
302 #echo $debconf->get_dialog();
304 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
305 ?>