Fixed last groups problem.
[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 GLOBAL $data_dir, $username;
141
142 $auth = FALSE;
143 if ( $adm_id = fileowner('../config/config.php') ) {
144 $adm = posix_getpwuid( $adm_id );
145 if ( $username == $adm['name'] ) {
146 $auth = TRUE;
147 }
148 }
149
150 if ( !auth ) {
151 header("Location: ../../src/options.php") ;
152 exit;
153 }
154
155 displayPageHeader($color, 'None');
156
157 $newcfg = array( );
158
159 foreach ( $defcfg as $key => $def ) {
160 $newcfg[$key] = '';
161 }
162
163 $cfgfile = '../config/config.php';
164 parseConfig( '../config/config_default.php' );
165 parseConfig( $cfgfile );
166
167 $colapse = array( 'Titles' => FALSE,
168 'Group1' => getPref($data_dir, $username, 'adm_Group1', FALSE ),
169 'Group2' => getPref($data_dir, $username, 'adm_Group2', TRUE ),
170 'Group3' => getPref($data_dir, $username, 'adm_Group3', TRUE ),
171 'Group4' => getPref($data_dir, $username, 'adm_Group4', TRUE ),
172 'Group5' => getPref($data_dir, $username, 'adm_Group5', TRUE ),
173 'Group6' => getPref($data_dir, $username, 'adm_Group6', TRUE ),
174 'Group7' => getPref($data_dir, $username, 'adm_Group7', TRUE ),
175 'Group8' => getPref($data_dir, $username, 'adm_Group8', TRUE ) );
176
177 if ( isset( $switch ) ) {
178 $colapse[$switch] = !$colapse[$switch];
179 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
180 }
181
182 echo "<form action=$PHP_SELF method=post>" .
183 "<br><center><table width=95% bgcolor=\"$color[5]\"><tr><td>".
184 "<table width=100% cellspacing=0 bgcolor=\"$color[4]\">" ,
185 "<tr bgcolor=\"$color[5]\"><th colspan=2>" . _("Configuration Administrator") . "</th></tr>";
186
187 $act_grp = 'Titles'; /* Active group */
188
189 foreach ( $newcfg as $k => $v ) {
190 $l = strtolower( $v );
191 $type = SMOPT_TYPE_UNDEFINED;
192 $n = substr( $k, 1 );
193 $n = str_replace( '[', '_', $n );
194 $n = str_replace( ']', '_', $n );
195 $e = 'adm_' . $n;
196 $name = $k;
197 $size = 50;
198 if ( isset( $defcfg[$k] ) ) {
199 $name = $defcfg[$k]['name'];
200 $type = $defcfg[$k]['type'];
201 $size = $defcfg[$k]['size'];
202 } else if ( $l == 'true' ) {
203 $v = 'TRUE';
204 $type = SMOPT_TYPE_BOOLEAN;
205 } else if ( $l == 'false' ) {
206 $v = 'FALSE';
207 $type = SMOPT_TYPE_BOOLEAN;
208 } else if ( $v{0} == "'" ) {
209 $type = SMOPT_TYPE_STRING;
210 } else if ( $v{0} == '"' ) {
211 $type = SMOPT_TYPE_STRING;
212 }
213
214 if ( substr( $k, 0, 7 ) == '$theme[' ) {
215 $type = SMOPT_TYPE_THEME;
216 } else if ( substr( $k, 0, 9 ) == '$plugins[' ) {
217 $type = SMOPT_TYPE_PLUGINS;
218 } else if ( substr( $k, 0, 13 ) == '$ldap_server[' ) {
219 $type = SMOPT_TYPE_LDAP;
220 }
221
222 if( $type == SMOPT_TYPE_TITLE || !$colapse[$act_grp] ) {
223
224 switch ( $type ) {
225 case SMOPT_TYPE_LDAP:
226 case SMOPT_TYPE_PLUGINS:
227 case SMOPT_TYPE_THEME:
228 case SMOPT_TYPE_HIDDEN:
229 break;
230 case SMOPT_TYPE_TITLE:
231 if ( $colapse[$k] ) {
232 $sw = '(+)';
233 } else {
234 $sw = '(-)';
235 }
236 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
237 "<a href=options.php?switch=$k STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
238 "$name</th></tr>";
239 $act_grp = $k;
240 break;
241 case SMOPT_TYPE_COMMENT:
242 $v = substr( $v, 1, strlen( $v ) - 2 );
243 echo "<tr><td>$name</td><td>".
244 "<b>$v</b>";
245 $newcfg[$k] = "'$v'";
246 if ( isset( $defcfg[$k]['comment'] ) ) {
247 echo ' &nbsp; ' . $defcfg[$k]['comment'];
248 }
249 echo "</td></tr>\n";
250 break;
251 case SMOPT_TYPE_INTEGER:
252 if ( isset( $HTTP_POST_VARS[$e] ) ) {
253 $v = intval( $HTTP_POST_VARS[$e] );
254 $newcfg[$k] = $v;
255 }
256 echo "<tr><td>$name</td><td>".
257 "<input size=10 name=\"adm_$n\" value=\"$v\">";
258 if ( isset( $defcfg[$k]['comment'] ) ) {
259 echo ' &nbsp; ' . $defcfg[$k]['comment'];
260 }
261 echo "</td></tr>\n";
262 break;
263 case SMOPT_TYPE_NUMLIST:
264 if ( isset( $HTTP_POST_VARS[$e] ) ) {
265 $v = $HTTP_POST_VARS[$e];
266 $newcfg[$k] = $v;
267 }
268 echo "<tr><td>$name</td><td>";
269 echo "<select name=\"adm_$n\">";
270 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
271 echo "<option value=\"$kp\"";
272 if ( $kp == $v ) {
273 echo ' selected';
274 }
275 echo ">$vp</option>";
276 }
277 echo '</select>';
278 if ( isset( $defcfg[$k]['comment'] ) ) {
279 echo ' &nbsp; ' . $defcfg[$k]['comment'];
280 }
281 echo "</td></tr>\n";
282 break;
283 case SMOPT_TYPE_STRLIST:
284 if ( isset( $HTTP_POST_VARS[$e] ) ) {
285 $v = '"' . $HTTP_POST_VARS[$e] . '"';
286 $newcfg[$k] = $v;
287 }
288 echo "<tr><td>$name</td><td>".
289 "<select name=\"adm_$n\">";
290 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
291 echo "<option value=\"$kp\"";
292 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
293 echo ' selected';
294 }
295 echo ">$vp</option>";
296 }
297 echo '</select>';
298 if ( isset( $defcfg[$k]['comment'] ) ) {
299 echo ' &nbsp; ' . $defcfg[$k]['comment'];
300 }
301 echo "</td></tr>\n";
302 break;
303
304 case SMOPT_TYPE_TEXTAREA:
305 if ( isset( $HTTP_POST_VARS[$e] ) ) {
306 $v = '"' . $HTTP_POST_VARS[$e] . '"';
307 $newcfg[$k] = str_replace( "\n", '', $v );
308 }
309 echo "<tr><td valign=top>$name</td><td>".
310 "<textarea cols=\"$size\" name=\"adm_$n\">" . substr( $v, 1, strlen( $v ) - 2 ) . "</textarea>";
311 if ( isset( $defcfg[$k]['comment'] ) ) {
312 echo ' &nbsp; ' . $defcfg[$k]['comment'];
313 }
314 echo "</td></tr>\n";
315 break;
316 case SMOPT_TYPE_STRING:
317 if ( isset( $HTTP_POST_VARS[$e] ) ) {
318 $v = '"' . $HTTP_POST_VARS[$e] . '"';
319 $newcfg[$k] = $v;
320 }
321 echo "<tr><td>$name</td><td>".
322 "<input size=\"$size\" name=\"adm_$n\" value=\"" . substr( $v, 1, strlen( $v ) - 2 ) . "\">";
323 if ( isset( $defcfg[$k]['comment'] ) ) {
324 echo ' &nbsp; ' . $defcfg[$k]['comment'];
325 }
326 echo "</td></tr>\n";
327 break;
328 case SMOPT_TYPE_BOOLEAN:
329 if ( isset( $HTTP_POST_VARS[$e] ) ) {
330 $v = $HTTP_POST_VARS[$e];
331 $newcfg[$k] = $v;
332 } else {
333 $v = strtoupper( $v );
334 }
335 if ( $v == 'TRUE' ) {
336 $ct = ' checked';
337 $cf = '';
338 } else {
339 $ct = '';
340 $cf = ' checked';
341 }
342 echo "<tr><td>$name</td><td>" .
343 "<INPUT$ct type=radio NAME=\"adm_$n\" value=\"TRUE\">" . _("Yes") .
344 "<INPUT$cf type=radio NAME=\"adm_$n\" value=\"FALSE\">" . _("No");
345 if ( isset( $defcfg[$k]['comment'] ) ) {
346 echo ' &nbsp; ' . $defcfg[$k]['comment'];
347 }
348 echo "</td></tr>\n";
349 break;
350 default:
351 echo "<tr><td>$name</td><td>" .
352 "<b><i>$v</i></b>";
353 if ( isset( $defcfg[$k]['comment'] ) ) {
354 echo ' &nbsp; ' . $defcfg[$k]['comment'];
355 }
356 echo "</td></tr>\n";
357 }
358 }
359
360 }
361
362 if ( !($colapse['Group7']) ) {
363 $i = 0;
364 echo '<tr><th>' . _("Theme Name") .
365 '</th><th>' . _("Theme Path") .
366 '</th></tr>';
367 while ( isset( $newcfg["\$theme[$i]['NAME']"] ) ) {
368 $k1 = "\$theme[$i]['NAME']";
369 $e1 = "theme_name_$i";
370 if ( isset( $HTTP_POST_VARS[$e1] ) ) {
371 $v1 = '"' . $HTTP_POST_VARS[$e1] . '"';
372 $newcfg[$k1] = $v1;
373 } else {
374 $v1 = $newcfg[$k1];
375 }
376 $k2 = "\$theme[$i]['PATH']";
377 $e2 = "theme_path_$i";
378 if ( isset( $HTTP_POST_VARS[$e2] ) ) {
379 $v2 = '"' . $HTTP_POST_VARS[$e2] . '"';
380 $newcfg[$k2] = $v2;
381 } else {
382 $v2 = $newcfg[$k2];
383 }
384 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
385 $path = substr( $v2, 1, strlen( $v2 ) - 2 );
386 echo '<tr>'.
387 "<td align=right>$i. <input name=\"$e1\" value=\"$name\" size=30></td>".
388 "<td><input name=\"$e2\" value=\"$path\" size=40></td>".
389 "</tr>\n";
390 $i++;
391
392 }
393 }
394
395 if ( $colapse['Group8'] ) {
396 $sw = '(+)';
397 } else {
398 $sw = '(-)';
399 }
400 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
401 "<a href=options.php?switch=Group7 STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
402 _("Plugins") . '</th></tr>';
403
404 if( !$colapse['Group7'] ) {
405
406 $fd = opendir( '../plugins/' );
407 $op_plugin = array();
408 while (false!==($file = readdir($fd))) {
409 if ($file != '.' && $file != '..' && $file != 'CVS' ) {
410 if ( filetype( $file ) == 'dir' ) {
411 $op_plugin[] = $file;
412 }
413 }
414 }
415 closedir($fd);
416 asort( $op_plugin );
417
418 $i = 0;
419 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
420 $k = "\$plugins[$i]";
421 $e = "plugin_$i";
422 if ( isset( $HTTP_POST_VARS[$e] ) ) {
423 $v = '"' . $HTTP_POST_VARS[$e] . '"';
424 $newcfg[$k] = $v;
425 } else {
426 $v = $newcfg[$k];
427 }
428 $name = substr( $v, 1, strlen( $v ) - 2 );
429 echo '<tr>'.
430 "<td align=right>$i.</td>".
431 "<td><select name=\"$e\">";
432 foreach ( $op_plugin as $op ) {
433 if ( $op == $name ) {
434 $cs = ' selected';
435 } else {
436 $cs = '';
437 }
438 echo "<option$cs>$op</option>";
439 }
440 echo "</select></td>".
441 '</tr>';
442 $i++;
443
444 }
445 }
446 echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
447 _("Change Settings") . "\" type=submit></th></tr>" ,
448 '</table></td></tr></table></form>';
449
450 /*
451 Write the options to the file.
452 */
453
454 $fp = fopen( $cfgfile, 'w' );
455 fwrite( $fp, "<?PHP\n".
456 "/**\n".
457 " * SquirrelMail Configuration File\n".
458 " * Created using the Administrator Plugin\n".
459 " */\n" );
460
461 /*
462 fwrite( $fp, 'GLOBAL ' );
463 $not_first = FALSE;
464 foreach ( $newcfg as $k => $v ) {
465 if ( $k{0} == '$' ) {
466 if( $i = strpos( $k, '[' ) ) {
467 if( strpos( $k, '[0]' ) ) {
468 if( $not_first ) {
469 fwrite( $fp, ', ' );
470 }
471 fwrite( $fp, substr( $k, 0, $i) );
472 }
473 } else {
474 if( $not_first ) {
475 fwrite( $fp, ', ' );
476 }
477 fwrite( $fp, $k );
478 }
479 $not_first = TRUE;
480 }
481 }
482 fwrite( $fp, ";\n" );
483 */
484 foreach ( $newcfg as $k => $v ) {
485 if ( $k{0} == '$' ) {
486 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
487 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
488 $v = str_replace( 'array(', "array(\n\t", $v );
489 $v = str_replace( "',", "',\n\t", $v );
490 }
491 fwrite( $fp, "$k = $v;\n" );
492 }
493 }
494 fwrite( $fp, '?>' );
495 fclose( $fp );
496 ?>