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