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