a0045b977f0fcdf7ec49b69a2260f5a7747109ec
[squirrelmail.git] / plugins / administrator / options.php
1 <?php
2
3 /**
4 * Administrator Plugin
5 *
6 * Copyright (c) 1999-2004 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 * @package plugins
13 * @subpackage administrator
14 */
15
16 /**
17 * parse the config file
18 */
19 function parseConfig( $cfg_file ) {
20
21 global $newcfg;
22
23 $cfg = file( $cfg_file );
24 $mode = '';
25 $l = count( $cfg );
26 $modifier = FALSE;
27
28 for ($i=0;$i<$l;$i++) {
29 $line = trim( $cfg[$i] );
30 $s = strlen( $line );
31 for ($j=0;$j<$s;$j++) {
32 switch ( $mode ) {
33 case '=':
34 if ( $line{$j} == '=' ) {
35 // Ok, we've got a right value, lets detect what type
36 $mode = 'D';
37 } else if ( $line{$j} == ';' ) {
38 // hu! end of command
39 $key = $mode = '';
40 }
41 break;
42 case 'K':
43 // Key detect
44 if( $line{$j} == ' ' ) {
45 $mode = '=';
46 } else {
47 $key .= $line{$j};
48 }
49 break;
50 case ';':
51 // Skip until next ;
52 if ( $line{$j} == ';' ) {
53 $mode = '';
54 }
55 break;
56 case 'S':
57 if ( $line{$j} == '\\' ) {
58 $value .= $line{$j};
59 $modifier = TRUE;
60 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
61 // End of string;
62 $newcfg[$key] = $value . $delimiter;
63 $key = $value = '';
64 $mode = ';';
65 } else {
66 $value .= $line{$j};
67 $modifier = FALSE;
68 }
69 break;
70 case 'N':
71 if ( $line{$j} == ';' ) {
72 $newcfg{$key} = $value;
73 $key = $mode = '';
74 } else {
75 $value .= $line{$j};
76 }
77 break;
78 case 'C':
79 // Comments
80 if ( $s > $j + 1 &&
81 $line{$j}.$line{$j+1} == '*/' ) {
82 $mode = '';
83 $j++;
84 }
85 break;
86 case 'D':
87 // Delimiter detect
88 switch ( $line{$j} ) {
89 case '"':
90 case "'":
91 // Double quote string
92 $delimiter = $value = $line{$j};
93 $mode = 'S';
94 break;
95 case ' ':
96 // Nothing yet
97 break;
98 default:
99 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
100 // Boolean TRUE
101 $newcfg{$key} = 'TRUE';
102 $key = '';
103 $mode = ';';
104 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
105 $newcfg{$key} = 'FALSE';
106 $key = '';
107 $mode = ';';
108 } else {
109 // Number or function call
110 $mode = 'N';
111 $value = $line{$j};
112 }
113 }
114 break;
115 default:
116 if ( $line{$j} == '$' ) {
117 // We must detect $key name
118 $mode = 'K';
119 $key = '$';
120 } else if ( $s < $j + 2 ) {
121 } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
122 // Skip untill next ;
123 $mode = ';';
124 $j += 6;
125 } else if ( $line{$j}.$line{$j+1} == '/*' ) {
126 $mode = 'C';
127 $j++;
128 } else if ( $line{$j} == '#' || $line{$j}.$line{$j+1} == '//' ) {
129 // Delete till the end of the line
130 $j = $s;
131 }
132 }
133 }
134 }
135 }
136
137 /* Change paths containing SM_PATH to admin-friendly paths
138 relative to the config dir, i.e.:
139 '' --> <empty string>
140 SM_PATH . 'images/logo.gif' --> ../images/logo.gif
141 '/absolute/path/logo.gif' --> /absolute/path/logo.gif
142 'http://whatever/' --> http://whatever
143 Note removal of quotes in returned value
144 */
145 function change_to_rel_path($old_path) {
146 $new_path = str_replace("SM_PATH . '", "../", $old_path);
147 $new_path = str_replace("../config/","", $new_path);
148 $new_path = str_replace("'","", $new_path);
149 return $new_path;
150 }
151
152 /* Change relative path (relative to config dir) to
153 internal SM_PATH, i.e.:
154 empty_string --> ''
155 ../images/logo.gif --> SM_PATH . 'images/logo.gif'
156 images/logo.gif --> SM_PATH . 'config/images/logo.gif'
157 /absolute/path/logo.gif --> '/absolute/path/logo.gif'
158 http://whatever/ --> 'http://whatever'
159 */
160 function change_to_sm_path($old_path) {
161 if ( $old_path === '' || $old_path == "''" ) {
162 return "''";
163 } elseif ( preg_match("/^(\/|http)/", $old_path) ) {
164 return "'" . $old_path . "'";
165 } elseif ( preg_match("/^(\$|SM_PATH)/", $old_path) ) {
166 return $old_path;
167 }
168
169 $new_path = '';
170 $rel_path = explode("../", $old_path);
171 if ( count($rel_path) > 2 ) {
172 // Since we're relative to the config dir,
173 // more than 1 ../ puts us OUTSIDE the SM tree.
174 // get full path to config.php, then pop the filename
175 $abs_path = explode('/', realpath (SM_PATH . 'config/config.php'));
176 array_pop ($abs_path);
177 foreach ( $rel_path as $subdir ) {
178 if ( $subdir === '' ) {
179 array_pop ($abs_path);
180 } else {
181 array_push($abs_path, $subdir);
182 }
183 }
184 foreach ($abs_path as $subdir) {
185 $new_path .= $subdir . '/';
186 }
187 $new_path = "'$new_path'";
188 } elseif ( count($rel_path) > 1 ) {
189 // we're within the SM tree, prepend SM_PATH
190 $new_path = str_replace('../',"SM_PATH . '", $old_path . "'");
191 } else {
192 // Last, if it's a relative path without a .. prefix,
193 // we're somewhere within the config dir, so prepend
194 // SM_PATH . 'config/
195 $new_path = "SM_PATH . 'config/" . $old_path . "'";
196 }
197 return $new_path;
198 }
199
200
201 /* ---------------------- main -------------------------- */
202
203 /** @ignore */
204 define('SM_PATH','../../');
205
206 /* SquirrelMail required files. */
207 require_once(SM_PATH . 'include/validate.php');
208 require_once(SM_PATH . 'functions/page_header.php');
209 require_once(SM_PATH . 'functions/imap.php');
210 require_once(SM_PATH . 'include/load_prefs.php');
211 require_once(SM_PATH . 'plugins/administrator/defines.php');
212 require_once(SM_PATH . 'plugins/administrator/auth.php');
213
214 GLOBAL $data_dir, $username;
215
216 if ( !adm_check_user() ) {
217 header('Location: ' . SM_PATH . 'src/options.php') ;
218 exit;
219 }
220
221 displayPageHeader($color, 'None');
222
223 $newcfg = array( );
224
225 foreach ( $defcfg as $key => $def ) {
226 $newcfg[$key] = '';
227 }
228
229 $cfgfile = SM_PATH . 'config/config.php';
230 parseConfig( SM_PATH . 'config/config_default.php' );
231 parseConfig( $cfgfile );
232
233 $colapse = array( 'Titles' => 'off',
234 'Group1' => getPref($data_dir, $username, 'adm_Group1', 'off' ),
235 'Group2' => getPref($data_dir, $username, 'adm_Group2', 'on' ),
236 'Group3' => getPref($data_dir, $username, 'adm_Group3', 'on' ),
237 'Group4' => getPref($data_dir, $username, 'adm_Group4', 'on' ),
238 'Group5' => getPref($data_dir, $username, 'adm_Group5', 'on' ),
239 'Group6' => getPref($data_dir, $username, 'adm_Group6', 'on' ),
240 'Group7' => getPref($data_dir, $username, 'adm_Group7', 'on' ),
241 'Group8' => getPref($data_dir, $username, 'adm_Group8', 'on' ),
242 'Group9' => getPref($data_dir, $username, 'adm_Group9', 'on' ),
243 'Group10' => getPref($data_dir, $username, 'adm_Group10', 'on' ) );
244
245 /* look in $_GET array for 'switch' */
246 if ( sqgetGlobalVar('switch', $switch, SQ_GET) ) {
247 if ( $colapse[$switch] == 'on' ) {
248 $colapse[$switch] = 'off';
249 } else {
250 $colapse[$switch] = 'on';
251 }
252 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
253 }
254
255 echo '<form action="options.php" method="post" name="options">' .
256 "<center><table width=\"95%\" bgcolor=\"$color[5]\"><tr><td>".
257 "<table width=\"100%\" cellspacing=0 bgcolor=\"$color[4]\">" ,
258 "<tr bgcolor=\"$color[5]\"><th colspan=2>" . _("Configuration Administrator") . "</th></tr>",
259 "<tr bgcolor=\"$color[5]\"><td colspan=2 align=\"center\">";
260
261 echo "<small>";
262 echo _("Note: it is recommended that you configure your system using conf.pl, and not this plugin. conf.pl contains additional information regarding the purpose of variables and appropriate values, as well as additional verification steps.");
263 echo "<br />";
264 echo _("Run or consult conf.pl should you run into difficulty with your configuration.");
265 echo "</small></td></tr>";
266
267
268 $act_grp = 'Titles'; /* Active group */
269
270 foreach ( $newcfg as $k => $v ) {
271 $l = strtolower( $v );
272 $type = SMOPT_TYPE_UNDEFINED;
273 $n = substr( $k, 1 );
274 $n = str_replace( '[', '_', $n );
275 $n = str_replace( ']', '_', $n );
276 $e = 'adm_' . $n;
277 $name = $k;
278 $size = 50;
279 if ( isset( $defcfg[$k] ) ) {
280 $name = $defcfg[$k]['name'];
281 $type = $defcfg[$k]['type'];
282 if ( isset( $defcfg[$k]['size'] ) ) {
283 $size = $defcfg[$k]['size'];
284 } else {
285 $size = 40;
286 }
287 } else if ( $l == 'true' ) {
288 $v = 'TRUE';
289 $type = SMOPT_TYPE_BOOLEAN;
290 } else if ( $l == 'false' ) {
291 $v = 'FALSE';
292 $type = SMOPT_TYPE_BOOLEAN;
293 } else if ( $v{0} == "'" ) {
294 $type = SMOPT_TYPE_STRING;
295 } else if ( $v{0} == '"' ) {
296 $type = SMOPT_TYPE_STRING;
297 }
298
299 if ( substr( $k, 0, 7 ) == '$theme[' ) {
300 $type = SMOPT_TYPE_THEME;
301 } else if ( substr( $k, 0, 9 ) == '$plugins[' ) {
302 $type = SMOPT_TYPE_PLUGINS;
303 } else if ( substr( $k, 0, 13 ) == '$ldap_server[' ) {
304 $type = SMOPT_TYPE_LDAP;
305 }
306
307 if( $type == SMOPT_TYPE_TITLE || $colapse[$act_grp] == 'off' ) {
308
309 switch ( $type ) {
310 case SMOPT_TYPE_LDAP:
311 case SMOPT_TYPE_PLUGINS:
312 case SMOPT_TYPE_THEME:
313 case SMOPT_TYPE_HIDDEN:
314 break;
315 case SMOPT_TYPE_EXTERNAL:
316 echo "<tr><td>$name</td><td><b>" .
317 $defcfg[$k]['value'] .
318 "</b></td></tr>";
319 break;
320 case SMOPT_TYPE_TITLE:
321 if ( $colapse[$k] == 'on' ) {
322 $sw = '(+)';
323 } else {
324 $sw = '(-)';
325 }
326 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
327 "<a href=\"options.php?switch=$k\" style=\"text-decoration:none\"><b>$sw</b> </a>" .
328 "$name</th></tr>";
329 $act_grp = $k;
330 break;
331 case SMOPT_TYPE_COMMENT:
332 $v = substr( $v, 1, strlen( $v ) - 2 );
333 echo "<tr><td>$name</td><td>".
334 "<b>$v</b>";
335 $newcfg[$k] = "'$v'";
336 if ( isset( $defcfg[$k]['comment'] ) ) {
337 echo ' &nbsp; ' . $defcfg[$k]['comment'];
338 }
339 echo "</td></tr>\n";
340 break;
341 case SMOPT_TYPE_INTEGER:
342 /* look for variable $e in POST, fill into $v */
343 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
344 $v = intval( $v );
345 $newcfg[$k] = $v;
346 }
347 echo "<tr><td>$name</td><td>".
348 "<input size=10 name=\"adm_$n\" value=\"$v\">";
349 if ( isset( $defcfg[$k]['comment'] ) ) {
350 echo ' &nbsp; ' . $defcfg[$k]['comment'];
351 }
352 echo "</td></tr>\n";
353 break;
354 case SMOPT_TYPE_NUMLIST:
355 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
356 $newcfg[$k] = $v;
357 }
358 echo "<tr><td>$name</td><td>";
359 echo "<select name=\"adm_$n\">";
360 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
361 echo "<option value=\"$kp\"";
362 if ( $kp == $v ) {
363 echo ' selected';
364 }
365 echo ">$vp</option>";
366 }
367 echo '</select>';
368 if ( isset( $defcfg[$k]['comment'] ) ) {
369 echo ' &nbsp; ' . $defcfg[$k]['comment'];
370 }
371 echo "</td></tr>\n";
372 break;
373 case SMOPT_TYPE_STRLIST:
374 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
375 $v = '"' . $v . '"';
376 $newcfg[$k] = $v;
377 }
378 echo "<tr><td>$name</td><td>".
379 "<select name=\"adm_$n\">";
380 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
381 echo "<option value=\"$kp\"";
382 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
383 echo ' selected';
384 }
385 echo ">$vp</option>";
386 }
387 echo '</select>';
388 if ( isset( $defcfg[$k]['comment'] ) ) {
389 echo ' &nbsp; ' . $defcfg[$k]['comment'];
390 }
391 echo "</td></tr>\n";
392 break;
393
394 case SMOPT_TYPE_TEXTAREA:
395 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
396 $v = '"' . $v . '"';
397 $newcfg[$k] = str_replace( "\n", '', $v );
398 }
399 echo "<tr><td valign=\"top\">$name</td><td>".
400 "<textarea cols=\"$size\" rows=\"4\" name=\"adm_$n\">" . substr( $v, 1, strlen( $v ) - 2 ) . "</textarea>";
401 if ( isset( $defcfg[$k]['comment'] ) ) {
402 echo ' &nbsp; ' . $defcfg[$k]['comment'];
403 }
404 echo "</td></tr>\n";
405 break;
406 case SMOPT_TYPE_STRING:
407 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
408 $v = '"' . $v . '"';
409 $newcfg[$k] = $v;
410 }
411 if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
412 $v = "'" . $defcfg[$k]['default'] . "'";
413 $newcfg[$k] = $v;
414 }
415 echo "<tr><td>$name</td><td>".
416 "<input size=\"$size\" name=\"adm_$n\" value=\"" . substr( $v, 1, strlen( $v ) - 2 ) . "\">";
417 if ( isset( $defcfg[$k]['comment'] ) ) {
418 echo ' &nbsp; ' . $defcfg[$k]['comment'];
419 }
420 echo "</td></tr>\n";
421 break;
422 case SMOPT_TYPE_BOOLEAN:
423 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
424 $newcfg[$k] = $v;
425 } else {
426 $v = strtoupper( $v );
427 }
428 if ( $v == 'TRUE' ) {
429 $ct = ' checked';
430 $cf = '';
431 } else {
432 $ct = '';
433 $cf = ' checked';
434 }
435 echo "<tr><td>$name</td><td>" .
436 "<input$ct type=\"radio\" name=\"adm_$n\" value=\"TRUE\">" . _("Yes") .
437 "<input$cf type=\"radio\" name=\"adm_$n\" value=\"FALSE\">" . _("No");
438 if ( isset( $defcfg[$k]['comment'] ) ) {
439 echo ' &nbsp; ' . $defcfg[$k]['comment'];
440 }
441 echo "</td></tr>\n";
442 break;
443 case SMOPT_TYPE_PATH:
444 if ( sqgetGlobalVar($e, $v, SQ_POST) ) {
445 $v = change_to_sm_path($v);
446 $newcfg[$k] = $v;
447 }
448 if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
449 $v = change_to_sm_path($defcfg[$k]['default']);
450 $newcfg[$k] = $v;
451 }
452 echo "<tr><td>$name</td><td>".
453 "<input size=\"$size\" name=\"adm_$n\" value=\"" . change_to_rel_path($v) . "\">";
454 if ( isset( $defcfg[$k]['comment'] ) ) {
455 echo ' &nbsp; ' . $defcfg[$k]['comment'];
456 }
457 echo "</td></tr>\n";
458 break;
459 default:
460 echo "<tr><td>$name</td><td>" .
461 "<b><i>$v</i></b>";
462 if ( isset( $defcfg[$k]['comment'] ) ) {
463 echo ' &nbsp; ' . $defcfg[$k]['comment'];
464 }
465 echo "</td></tr>\n";
466 }
467 }
468 }
469
470 /* Special Themes Block */
471 if ( $colapse['Group7'] == 'off' ) {
472 $i = 0;
473 echo '<tr><th>' . _("Theme Name") .
474 '</th><th>' . _("Theme Path") .
475 '</th></tr>';
476 while ( isset( $newcfg["\$theme[$i]['NAME']"] ) ) {
477 $k1 = "\$theme[$i]['NAME']";
478 $e1 = "theme_name_$i";
479 if ( sqgetGlobalVar($e, $v1, SQ_POST) ) {
480 $v1 = '"' . str_replace( '\"', '"', $v1 ) . '"';
481 $v1 = '"' . str_replace( '"', '\"', $v1 ) . '"';
482 $newcfg[$k1] = $v1;
483 } else {
484 $v1 = $newcfg[$k1];
485 }
486 $k2 = "\$theme[$i]['PATH']";
487 $e2 = "theme_path_$i";
488 if ( sqgetGlobalVar($e, $v2, SQ_POST) ) {
489 $v2 = change_to_sm_path($v2);
490 $newcfg[$k2] = $v2;
491 } else {
492 $v2 = $newcfg[$k2];
493 }
494 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
495 $path = change_to_rel_path($v2);
496 echo '<tr>'.
497 "<td align=\"right\">$i. <input name=\"$e1\" value=\"$name\" size=30></td>".
498 "<td><input name=\"$e2\" value=\"$path\" size=40></td>".
499 "</tr>\n";
500 $i++;
501
502 }
503 }
504
505 /* Special Plugins Block */
506 if ( $colapse['Group8'] == 'on' ) {
507 $sw = '(+)';
508 } else {
509 $sw = '(-)';
510 }
511 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
512 "<a href=\"options.php?switch=Group8\" STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
513 _("Plugins") . '</th></tr>';
514
515 if( $colapse['Group8'] == 'off' ) {
516
517 $plugpath = SM_PATH . 'plugins/';
518 if ( file_exists($plugpath) ) {
519 $fd = opendir( $plugpath );
520 $op_plugin = array();
521 $p_count = 0;
522 while (false !== ($file = readdir($fd))) {
523 if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
524 $op_plugin[] = $file;
525 $p_count++;
526 }
527 }
528 closedir($fd);
529 asort( $op_plugin );
530
531 /* Lets get the plugins that are active */
532 $plugins = array();
533 if ( sqgetGlobalVar('plg', $v, SQ_POST) ) {
534 foreach ( $op_plugin as $plg ) {
535 if ( sqgetGlobalVar("plgs_$plg", $v, SQ_POST) && $v == 'on' ) {
536 $plugins[] = $plg;
537 }
538 }
539 $i = 0;
540 foreach ( $plugins as $plg ) {
541 $k = "\$plugins[$i]";
542 $newcfg[$k] = "'$plg'";
543 $i++;
544 }
545 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
546 $k = "\$plugins[$i]";
547 $newcfg[$k] = '';
548 $i++;
549 }
550 } else {
551 $i = 0;
552 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
553 $k = "\$plugins[$i]";
554 $v = $newcfg[$k];
555 $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
556 $i++;
557 }
558 }
559 echo "<tr><td colspan=2><input type=\"hidden\" name=\"plg\" value=\"on\"><center><table>";
560 foreach ( $op_plugin as $plg ) {
561 if ( in_array( $plg, $plugins ) ) {
562 $sw = ' checked';
563 } else {
564 $sw = '';
565 }
566 echo '<tr>' .
567 "<td>$plg</td><td><input$sw type=\"checkbox\" name=plgs_$plg></td>".
568 "</tr>\n";
569 }
570 echo '</table></center></td></tr>';
571 } else {
572 echo '<tr><td colspan=2 align="center">' . sprintf(_("Plugin directory could not be found: %s"),$plugpath) . "</td></tr>\n";
573 }
574 }
575 echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
576 _("Change Settings") . '" type="submit"><br />'.
577 '<a href="'.SM_PATH.'src/configtest.php" target="_blank">'._("Test Configuration").
578 "</a></th></tr>\n" ,
579 '</table></td></tr></table></center></form>';
580
581 /*
582 Write the options to the file.
583 */
584
585 if( $fp = @fopen( $cfgfile, 'w' ) ) {
586 fwrite( $fp, "<?PHP\n".
587 "/**\n".
588 " * SquirrelMail Configuration File\n".
589 " * Created using the Administrator Plugin\n".
590 " */\n".
591 "\n".
592 "global \$version;\n" );
593
594 foreach ( $newcfg as $k => $v ) {
595 if ( $k{0} == '$' && $v <> '' || is_int($v)) {
596 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
597 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
598 $v = str_replace( 'array(', "array(\n\t", $v );
599 $v = str_replace( "',", "',\n\t", $v );
600 }
601 fwrite( $fp, "$k = $v;\n" );
602 }
603 }
604 fwrite( $fp, '?>' );
605 fclose( $fp );
606 } else {
607 echo '<br /><p align="center"><big>'.
608 _("Config file can't be opened. Please check config.php.").
609 '</big></p>';
610 }
611 ?>
612 </body></html>