79ddf48939ddf99ad9ed9711d899dfa843196d16
[squirrelmail.git] / plugins / administrator / options.php
1 <?php
2
3 /**
4 * Administrator Plugin
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Philippe Mingo
10 *
11 * $Id$
12 */
13
14 function parseConfig( $cfg_file ) {
15
16 global $newcfg;
17
18 $cfg = file( $cfg_file );
19 $mode = '';
20 $l = count( $cfg );
21 $modifier = FALSE;
22
23 for ($i=0;$i<$l;$i++) {
24 $line = trim( $cfg[$i] );
25 $s = strlen( $line );
26 for ($j=0;$j<$s;$j++) {
27 switch ( $mode ) {
28 case '=':
29 if ( $line{$j} == '=' ) {
30 // Ok, we've got a right value, lets detect what type
31 $mode = 'D';
32 } else if ( $line{$j} == ';' ) {
33 // hu! end of command
34 $key = $mode = '';
35 }
36 break;
37 case 'K':
38 // Key detect
39 if( $line{$j} == ' ' ) {
40 $mode = '=';
41 } else {
42 $key .= $line{$j};
43 }
44 break;
45 case ';':
46 // Skip until next ;
47 if ( $line{$j} == ';' ) {
48 $mode = '';
49 }
50 break;
51 case 'S':
52 if ( $line{$j} == '\\' ) {
53 $value .= $line{$j};
54 $modifier = TRUE;
55 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
56 // End of string;
57 $newcfg[$key] = $value . $delimiter;
58 $key = $value = '';
59 $mode = ';';
60 } else {
61 $value .= $line{$j};
62 $modifier = FALSE;
63 }
64 break;
65 case 'N':
66 if ( $line{$j} == ';' ) {
67 $newcfg{$key} = $value;
68 $key = $mode = '';
69 } else {
70 $value .= $line{$j};
71 }
72 break;
73 case 'C':
74 // Comments
75 if ( $line{$j}.$line{$j+1} == '*/' ) {
76 $mode = '';
77 $j++;
78 }
79 break;
80 case 'D':
81 // Delimiter detect
82 switch ( $line{$j} ) {
83 case '"':
84 case "'":
85 // Double quote string
86 $delimiter = $value = $line{$j};
87 $mode = 'S';
88 break;
89 case ' ':
90 // Nothing yet
91 break;
92 default:
93 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
94 // Boolean TRUE
95 $newcfg{$key} = 'TRUE';
96 $key = '';
97 $mode = ';';
98 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
99 $newcfg{$key} = 'FALSE';
100 $key = '';
101 $mode = ';';
102 } else {
103 // Number or function call
104 $mode = 'N';
105 $value = $line{$j};
106 }
107 }
108 break;
109 default:
110 if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
111 // Skip untill next ;
112 $mode = ';';
113 $j += 6;
114 } else if ( $line{$j}.$line{$j+1} == '/*' ) {
115 $mode = 'C';
116 $j++;
117 } else if ( $line{$j} == '#' || $line{$j}.$line{$j+1} == '//' ) {
118 // Delete till the end of the line
119 $j = $s;
120 } else if ( $line{$j} == '$' ) {
121 // We must detect $key name
122 $mode = 'K';
123 $key = '$';
124 }
125 }
126 }
127 }
128
129 }
130
131 /* ---------------------- main -------------------------- */
132
133 chdir('..');
134 require_once('../src/validate.php');
135 require_once('../functions/page_header.php');
136 require_once('../functions/imap.php');
137 require_once('../src/load_prefs.php');
138 require_once('../plugins/administrator/defines.php');
139
140 $auth = FALSE;
141 if ( $adm_id = fileowner('../config/config.php') ) {
142 $adm = posix_getpwuid( $adm_id );
143 if ( $username == $adm['name'] ) {
144 $auth = TRUE;
145 }
146 }
147
148 if ( !auth ) {
149 header("Location: ../../src/options.php") ;
150 exit;
151 }
152
153 displayPageHeader($color, 'None');
154
155 $newcfg = array( );
156
157 foreach ( $defcfg as $key => $def ) {
158 $newcfg[$key] = '';
159 }
160
161 $cfgfile = '../config/config.php';
162 parseConfig( '../config/config_default.php' );
163 parseConfig( $cfgfile );
164
165 echo "<form action=$PHP_SELF method=post>" .
166 "<br><center><table width=95% bgcolor=\"$color[5]\"><tr><td>".
167 "<table width=100% cellspacing=0 bgcolor=\"$color[4]\">" ,
168 "<tr bgcolor=\"$color[5]\"><th colspan=2>" . _("Configuration Administrator") . "</th></tr>";
169 foreach ( $newcfg as $k => $v ) {
170 $l = strtolower( $v );
171 $type = SMOPT_TYPE_UNDEFINED;
172 $n = substr( $k, 1 );
173 $n = str_replace( '[', '_', $n );
174 $n = str_replace( ']', '_', $n );
175 $e = 'adm_' . $n;
176 $name = $k;
177 $size = 50;
178 if ( isset( $defcfg[$k] ) ) {
179 $name = $defcfg[$k]['name'];
180 $type = $defcfg[$k]['type'];
181 $size = $defcfg[$k]['size'];
182 } else if ( $l == 'true' ) {
183 $v = 'TRUE';
184 $type = SMOPT_TYPE_BOOLEAN;
185 } else if ( $l == 'false' ) {
186 $v = 'FALSE';
187 $type = SMOPT_TYPE_BOOLEAN;
188 } else if ( $v{0} == "'" ) {
189 $type = SMOPT_TYPE_STRING;
190 } else if ( $v{0} == '"' ) {
191 $type = SMOPT_TYPE_STRING;
192 }
193
194 switch ( $type ) {
195 case SMOPT_TYPE_TITLE:
196 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>$name</th></tr>";
197 break;
198 case SMOPT_TYPE_COMMENT:
199 $v = substr( $v, 1, strlen( $v ) - 2 );
200 echo "<tr><td>$name</td><td>".
201 "<b>$v</b>";
202 $newcfg[$k] = "'$v'";
203 break;
204 case SMOPT_TYPE_INTEGER:
205 if ( isset( $HTTP_POST_VARS[$e] ) ) {
206 $v = intval( $HTTP_POST_VARS[$e] );
207 $newcfg[$k] = $v;
208 }
209 echo "<tr><td>$name</td><td>".
210 "<input size=10 name=\"adm_$n\" value=\"$v\">";
211 break;
212 case SMOPT_TYPE_NUMLIST:
213 if ( isset( $HTTP_POST_VARS[$e] ) ) {
214 $v = $HTTP_POST_VARS[$e];
215 $newcfg[$k] = $v;
216 }
217 echo "<tr><td>$name</td><td>";
218 echo "<select name=\"adm_$n\">";
219 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
220 echo "<option value=\"$kp\"";
221 if ( $kp == $v ) {
222 echo ' selected';
223 }
224 echo ">$vp</option>";
225 }
226 echo '</select>';
227 break;
228 case SMOPT_TYPE_STRLIST:
229 if ( isset( $HTTP_POST_VARS[$e] ) ) {
230 $v = '"' . $HTTP_POST_VARS[$e] . '"';
231 $newcfg[$k] = $v;
232 }
233 echo "<tr><td>$name</td><td>".
234 "<select name=\"adm_$n\">";
235 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
236 echo "<option value=\"$kp\"";
237 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
238 echo ' selected';
239 }
240 echo ">$vp</option>";
241 }
242 echo '</select>';
243 break;
244
245 case SMOPT_TYPE_STRING:
246 if ( isset( $HTTP_POST_VARS[$e] ) ) {
247 $v = '"' . $HTTP_POST_VARS[$e] . '"';
248 $newcfg[$k] = $v;
249 }
250 echo "<tr><td>$name</td><td>".
251 "<input size=\"$size\" name=\"adm_$n\" value=\"" . substr( $v, 1, strlen( $v ) - 2 ) . "\">";
252 break;
253 case SMOPT_TYPE_BOOLEAN:
254 if ( isset( $HTTP_POST_VARS[$e] ) ) {
255 $v = $HTTP_POST_VARS[$e];
256 $newcfg[$k] = $v;
257 } else {
258 $v = strtoupper( $v );
259 }
260 if ( $v == 'TRUE' ) {
261 $ct = ' checked';
262 $cf = '';
263 } else {
264 $ct = '';
265 $cf = ' checked';
266 }
267 echo "<tr><td>$name</td><td>" .
268 "<INPUT$ct type=radio NAME=\"adm_$n\" value=\"TRUE\">" . _("Yes") .
269 "<INPUT$cf type=radio NAME=\"adm_$n\" value=\"FALSE\">" . _("No");
270 break;
271 default:
272 echo "<tr><td>$name</td><td>" .
273 "<b><i>$v</i></b>";
274 }
275 echo "</td></tr>\n";
276 }
277 echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
278 _("Change Settings") . "\" type=submit></th></tr>" ,
279 '</table></td></tr></table></form>';
280
281 /*
282 Write the options to the file.
283 */
284
285 $fp = fopen( $cfgfile, 'w' );
286 fwrite( $fp, "<?PHP\n".
287 "/**\n".
288 " * SquirrelMail Configuration File\n".
289 " * Created using the Administrator Plugin\n".
290 " */\n\n" );
291
292 fwrite( $fp, 'GLOBAL ' );
293 $not_first = FALSE;
294 foreach ( $newcfg as $k => $v ) {
295 if ( $k{0} == '$' ) {
296 if( $i = strpos( $k, '[' ) ) {
297 if( strpos( $k, '[0]' ) ) {
298 if( $not_first ) {
299 fwrite( $fp, ', ' );
300 }
301 fwrite( $fp, substr( $k, 0, $i) );
302 $not_first = TRUE;
303 }
304 } else {
305 if( $not_first ) {
306 fwrite( $fp, ', ' );
307 }
308 fwrite( $fp, $k );
309 $not_first = TRUE;
310 }
311 }
312 }
313 fwrite( $fp, ";\n" );
314 foreach ( $newcfg as $k => $v ) {
315 if ( $k{0} == '$' ) {
316 fwrite( $fp, "$k = $v;\n" );
317 }
318 }
319 fwrite( $fp, '?>' );
320 fclose( $fp );
321 ?>