6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
13 * @subpackage administrator
17 * parse the config file
19 function parseConfig( $cfg_file ) {
23 $cfg = file( $cfg_file );
28 for ($i=0;$i<$l;$i++
) {
29 $line = trim( $cfg[$i] );
31 for ($j=0;$j<$s;$j++
) {
34 if ( $line{$j} == '=' ) {
35 // Ok, we've got a right value, lets detect what type
37 } else if ( $line{$j} == ';' ) {
44 if( $line{$j} == ' ' ) {
52 if ( $line{$j} == ';' ) {
57 if ( $line{$j} == '\\' ) {
60 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
62 $newcfg[$key] = $value . $delimiter;
71 if ( $line{$j} == ';' ) {
72 $newcfg{$key} = $value;
81 $line{$j}.$line{$j+
1} == '*/' ) {
88 switch ( $line{$j} ) {
91 // Double quote string
92 $delimiter = $value = $line{$j};
99 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
101 $newcfg{$key} = 'TRUE';
104 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
105 $newcfg{$key} = 'FALSE';
109 // Number or function call
116 if ( $line{$j} == '$' ) {
117 // We must detect $key name
120 } else if ( $s < $j +
2 ) {
121 } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
122 // Skip untill next ;
125 } else if ( $line{$j}.$line{$j+
1} == '/*' ) {
128 } else if ( $line{$j} == '#' ||
$line{$j}.$line{$j+
1} == '//' ) {
129 // Delete till the end of the line
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
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);
152 /* Change relative path (relative to config dir) to
153 internal SM_PATH, i.e.:
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'
160 function change_to_sm_path($old_path) {
161 if ( $old_path === '' ||
$old_path == "''" ) {
163 } elseif ( preg_match("/^(\/|http)/", $old_path) ) {
164 return "'" . $old_path . "'";
165 } elseif ( preg_match("/^(\$|SM_PATH)/", $old_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);
181 array_push($abs_path, $subdir);
184 foreach ($abs_path as $subdir) {
185 $new_path .= $subdir . '/';
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 . "'");
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 . "'";
201 /* ---------------------- main -------------------------- */
204 define('SM_PATH','../../');
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');
214 GLOBAL $data_dir, $username;
216 if ( !adm_check_user() ) {
217 header('Location: ' . SM_PATH
. 'src/options.php') ;
221 displayPageHeader($color, 'None');
225 foreach ( $defcfg as $key => $def ) {
229 $cfgfile = SM_PATH
. 'config/config.php';
230 parseConfig( SM_PATH
. 'config/config_default.php' );
231 parseConfig( $cfgfile );
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' ) );
245 /* look in $_GET array for 'switch' */
246 if ( sqgetGlobalVar('switch', $switch, SQ_GET
) ) {
247 if ( $colapse[$switch] == 'on' ) {
248 $colapse[$switch] = 'off';
250 $colapse[$switch] = 'on';
252 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
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\">";
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.");
264 echo _("Run or consult conf.pl should you run into difficulty with your configuration.");
265 echo "</small></td></tr>";
268 $act_grp = 'Titles'; /* Active group */
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 );
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'];
287 } else if ( $l == 'true' ) {
289 $type = SMOPT_TYPE_BOOLEAN
;
290 } else if ( $l == '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
;
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
;
307 if( $type == SMOPT_TYPE_TITLE ||
$colapse[$act_grp] == 'off' ) {
310 case SMOPT_TYPE_LDAP
:
311 case SMOPT_TYPE_PLUGINS
:
312 case SMOPT_TYPE_THEME
:
313 case SMOPT_TYPE_HIDDEN
:
315 case SMOPT_TYPE_EXTERNAL
:
316 echo "<tr><td>$name</td><td><b>" .
317 $defcfg[$k]['value'] .
320 case SMOPT_TYPE_TITLE
:
321 if ( $colapse[$k] == 'on' ) {
326 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
327 "<a href=options.php?switch=$k STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
331 case SMOPT_TYPE_COMMENT
:
332 $v = substr( $v, 1, strlen( $v ) - 2 );
333 echo "<tr><td>$name</td><td>".
335 $newcfg[$k] = "'$v'";
336 if ( isset( $defcfg[$k]['comment'] ) ) {
337 echo ' ' . $defcfg[$k]['comment'];
341 case SMOPT_TYPE_INTEGER
:
342 /* look for variable $e in POST, fill into $v */
343 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
347 echo "<tr><td>$name</td><td>".
348 "<input size=10 name=\"adm_$n\" value=\"$v\">";
349 if ( isset( $defcfg[$k]['comment'] ) ) {
350 echo ' ' . $defcfg[$k]['comment'];
354 case SMOPT_TYPE_NUMLIST
:
355 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
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\"";
365 echo ">$vp</option>";
368 if ( isset( $defcfg[$k]['comment'] ) ) {
369 echo ' ' . $defcfg[$k]['comment'];
373 case SMOPT_TYPE_STRLIST
:
374 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
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 ) ) {
385 echo ">$vp</option>";
388 if ( isset( $defcfg[$k]['comment'] ) ) {
389 echo ' ' . $defcfg[$k]['comment'];
394 case SMOPT_TYPE_TEXTAREA
:
395 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
397 $newcfg[$k] = str_replace( "\n", '', $v );
399 echo "<tr><td valign=top>$name</td><td>".
400 "<textarea cols=\"$size\" name=\"adm_$n\">" . substr( $v, 1, strlen( $v ) - 2 ) . "</textarea>";
401 if ( isset( $defcfg[$k]['comment'] ) ) {
402 echo ' ' . $defcfg[$k]['comment'];
406 case SMOPT_TYPE_STRING
:
407 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
411 if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
412 $v = "'" . $defcfg[$k]['default'] . "'";
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 ' ' . $defcfg[$k]['comment'];
422 case SMOPT_TYPE_BOOLEAN
:
423 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
426 $v = strtoupper( $v );
428 if ( $v == 'TRUE' ) {
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 ' ' . $defcfg[$k]['comment'];
443 case SMOPT_TYPE_PATH
:
444 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
445 $v = change_to_sm_path($v);
448 if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
449 $v = change_to_sm_path($defcfg[$k]['default']);
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 ' ' . $defcfg[$k]['comment'];
460 echo "<tr><td>$name</td><td>" .
462 if ( isset( $defcfg[$k]['comment'] ) ) {
463 echo ' ' . $defcfg[$k]['comment'];
470 /* Special Themes Block */
471 if ( $colapse['Group7'] == 'off' ) {
473 echo '<tr><th>' . _("Theme Name") .
474 '</th><th>' . _("Theme Path") .
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 ) . '"';
486 $k2 = "\$theme[$i]['PATH']";
487 $e2 = "theme_path_$i";
488 if ( sqgetGlobalVar($e, $v2, SQ_POST
) ) {
489 $v2 = change_to_sm_path($v2);
494 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
495 $path = change_to_rel_path($v2);
497 "<td align=right>$i. <input name=\"$e1\" value=\"$name\" size=30></td>".
498 "<td><input name=\"$e2\" value=\"$path\" size=40></td>".
505 /* Special Plugins Block */
506 if ( $colapse['Group8'] == 'on' ) {
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>';
515 if( $colapse['Group8'] == 'off' ) {
517 $plugpath = SM_PATH
. 'plugins/';
518 if ( file_exists($plugpath) ) {
519 $fd = opendir( $plugpath );
520 $op_plugin = array();
522 while (false !== ($file = readdir($fd))) {
523 if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
524 $op_plugin[] = $file;
531 /* Lets get the plugins that are active */
533 if ( sqgetGlobalVar('plg', $v, SQ_POST
) ) {
534 foreach ( $op_plugin as $plg ) {
535 if ( sqgetGlobalVar("plgs_$plg", $v, SQ_POST
) && $v == 'on' ) {
540 foreach ( $plugins as $plg ) {
541 $k = "\$plugins[$i]";
542 $newcfg[$k] = "'$plg'";
545 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
546 $k = "\$plugins[$i]";
552 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
553 $k = "\$plugins[$i]";
555 $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
559 echo "<tr><td colspan=2><input type=hidden name=plg value=on><center><table><tr><td>";
560 foreach ( $op_plugin as $plg ) {
561 if ( in_array( $plg, $plugins ) ) {
567 "<td>$plg</td><td><input$sw type=checkbox name=plgs_$plg></td>".
570 echo '</td></tr></table></td></tr>';
572 echo '<tr><td colspan=2 align="center">Plugin directory could not be found: ' . $plugpath . "</td></tr>\n";
575 echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
576 _("Change Settings") . "\" type=submit></th></tr>" ,
577 '</table></td></tr></table></form>';
580 Write the options to the file.
583 if( $fp = @fopen
( $cfgfile, 'w' ) ) {
584 fwrite( $fp, "<?PHP\n".
586 " * SquirrelMail Configuration File\n".
587 " * Created using the Administrator Plugin\n".
590 "global \$version;\n" );
592 foreach ( $newcfg as $k => $v ) {
593 if ( $k{0} == '$' && $v <> '' ) {
594 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
595 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
596 $v = str_replace( 'array(', "array(\n\t", $v );
597 $v = str_replace( "',", "',\n\t", $v );
599 fwrite( $fp, "$k = $v;\n" );
605 echo '<font size=+1><br>'.
606 _("Config file can't be opened. Please check config.php.").