8e52f54b8daf80ad36be4c6d54103f7cb934f922
4 * Administrator Plugin - Options Page
6 * This script creates separate page, that allows to review and modify
7 * SquirrelMail configuration file.
9 * @author Philippe Mingo
10 * @copyright © 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @subpackage administrator
18 * parse the config file
20 * @param string $cfg_file
23 function parseConfig( $cfg_file ) {
27 $cfg = file( $cfg_file );
33 for ($i=0;$i<$l;$i++
) {
34 $line = trim( $cfg[$i] );
36 for ($j=0;$j<$s;$j++
) {
39 if ( $line{$j} == '=' ) {
40 // Ok, we've got a right value, lets detect what type
42 } else if ( $line{$j} == ';' ) {
49 if ( $line{$j} == ' ' ) {
53 // FIXME: this is only pour workaround for plugins[] array.
54 if ($line{$j}=='[' && $line{($j+
1)}==']') {
62 if ( $line{$j} == ';' ) {
67 if ( $line{$j} == '\\' ) {
70 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
72 $newcfg[$key] = $value . $delimiter;
81 if ( $line{$j} == ';' ) {
82 $newcfg{$key} = $value;
91 $line{$j}.$line{$j+
1} == '*/' ) {
98 switch ( $line{$j} ) {
101 // Double quote string
102 $delimiter = $value = $line{$j};
109 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
111 $newcfg{$key} = 'TRUE';
114 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
115 $newcfg{$key} = 'FALSE';
119 // Number or function call
126 if ( $line{$j} == '$' ) {
127 // We must detect $key name
130 } else if ( $s < $j +
2 ) {
131 } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
132 // Skip untill next ;
135 } else if ( $line{$j}.$line{$j+
1} == '/*' ) {
138 } else if ( $line{$j} == '#' ||
$line{$j}.$line{$j+
1} == '//' ) {
139 // Delete till the end of the line
148 * Change paths containing SM_PATH to admin-friendly paths
149 * relative to the config dir, i.e.:
150 * '' --> <empty string>
151 * SM_PATH . 'images/logo.gif' --> ../images/logo.gif
152 * '/absolute/path/logo.gif' --> /absolute/path/logo.gif
153 * 'http://whatever/' --> http://whatever
154 * Note removal of quotes in returned value
156 * @param string $old_path path that has to be converted
157 * @return string new path
160 function change_to_rel_path($old_path) {
161 $new_path = str_replace("SM_PATH . '", "../", $old_path);
162 $new_path = str_replace("../config/","", $new_path);
163 $new_path = str_replace("'","", $new_path);
168 * Change relative path (relative to config dir) to
169 * internal SM_PATH, i.e.:
170 * empty_string --> ''
171 * ../images/logo.gif --> SM_PATH . 'images/logo.gif'
172 * images/logo.gif --> SM_PATH . 'config/images/logo.gif'
173 * /absolute/path/logo.gif --> '/absolute/path/logo.gif'
174 * http://whatever/ --> 'http://whatever'
176 * @param string $old_path path that has to be converted
177 * @return string new path
180 function change_to_sm_path($old_path) {
181 if ( $old_path === '' ||
$old_path == "''" ) {
183 } elseif ( preg_match("/^(\/|http)/", $old_path) ) {
184 return "'" . $old_path . "'";
185 } elseif ( preg_match("/^(\$|SM_PATH)/", $old_path) ) {
190 $rel_path = explode("../", $old_path);
191 if ( count($rel_path) > 2 ) {
192 // Since we're relative to the config dir,
193 // more than 1 ../ puts us OUTSIDE the SM tree.
194 // get full path to config.php, then pop the filename
195 $abs_path = explode('/', realpath (SM_PATH
. 'config/config.php'));
196 array_pop ($abs_path);
197 foreach ( $rel_path as $subdir ) {
198 if ( $subdir === '' ) {
199 array_pop ($abs_path);
201 array_push($abs_path, $subdir);
204 foreach ($abs_path as $subdir) {
205 $new_path .= $subdir . '/';
207 $new_path = "'$new_path'";
208 } elseif ( count($rel_path) > 1 ) {
209 // we're within the SM tree, prepend SM_PATH
210 $new_path = str_replace('../',"SM_PATH . '", $old_path . "'");
212 // Last, if it's a relative path without a .. prefix,
213 // we're somewhere within the config dir, so prepend
214 // SM_PATH . 'config/
215 $new_path = "SM_PATH . 'config/" . $old_path . "'";
221 /* ---------------------- main -------------------------- */
222 /** main SquirrelMail include */
223 require('../../include/init.php');
224 /* configuration definitions */
225 include_once(SM_PATH
. 'plugins/administrator/defines.php');
226 /* additional functions */
227 include_once(SM_PATH
. 'plugins/administrator/auth.php');
229 global $data_dir, $username;
231 if ( !adm_check_user() ) {
232 header('Location: ' . SM_PATH
. 'src/options.php') ;
236 displayPageHeader($color, 'None');
240 foreach ( $defcfg as $key => $def ) {
244 $cfgfile = SM_PATH
. 'config/config.php';
245 parseConfig( SM_PATH
. 'config/config_default.php' );
246 parseConfig( $cfgfile );
248 $colapse = array( 'Titles' => 'off',
249 'Group1' => getPref($data_dir, $username, 'adm_Group1', 'off' ),
250 'Group2' => getPref($data_dir, $username, 'adm_Group2', 'on' ),
251 'Group3' => getPref($data_dir, $username, 'adm_Group3', 'on' ),
252 'Group4' => getPref($data_dir, $username, 'adm_Group4', 'on' ),
253 'Group5' => getPref($data_dir, $username, 'adm_Group5', 'on' ),
254 'Group6' => getPref($data_dir, $username, 'adm_Group6', 'on' ),
255 'Group7' => getPref($data_dir, $username, 'adm_Group7', 'on' ),
256 'Group8' => getPref($data_dir, $username, 'adm_Group8', 'on' ),
257 'Group9' => getPref($data_dir, $username, 'adm_Group9', 'on' ),
258 'Group10' => getPref($data_dir, $username, 'adm_Group10', 'on' ),
259 'Group11' => getPref($data_dir, $username, 'adm_Group11', 'on' ) );
261 /* look in $_GET array for 'switch' */
262 if ( sqgetGlobalVar('switch', $switch, SQ_GET
) ) {
263 if ( $colapse[$switch] == 'on' ) {
264 $colapse[$switch] = 'off';
266 $colapse[$switch] = 'on';
268 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
271 echo '<form action="options.php" method="post" name="options">' .
272 '<table width="95%" align="center" bgcolor="'.$color[5].'"><tr><td>'.
273 '<table width="100%" cellspacing="0" bgcolor="'.$color[4].'">'.
274 '<tr bgcolor="'.$color[5].'"><th colspan="2">'.
275 _("Configuration Administrator").'</th></tr>'.
276 '<tr bgcolor="'.$color[5].'"><td colspan="2" align="center"><small>'.
277 _("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.").
279 _("Run or consult conf.pl should you run into difficulty with your configuration.").
280 '</small></td></tr>';
283 $act_grp = 'Titles'; /* Active group */
285 foreach ( $newcfg as $k => $v ) {
286 $l = strtolower( $v );
287 $type = SMOPT_TYPE_UNDEFINED
;
288 $n = substr( $k, 1 );
289 $n = str_replace( '[', '_', $n );
290 $n = str_replace( ']', '_', $n );
294 if ( isset( $defcfg[$k] ) ) {
295 $name = $defcfg[$k]['name'];
296 $type = $defcfg[$k]['type'];
297 if ( isset( $defcfg[$k]['size'] ) ) {
298 $size = $defcfg[$k]['size'];
302 } else if ( $l == 'true' ) {
304 $type = SMOPT_TYPE_BOOLEAN
;
305 } else if ( $l == 'false' ) {
307 $type = SMOPT_TYPE_BOOLEAN
;
308 } else if ( $v{0} == "'" ) {
309 $type = SMOPT_TYPE_STRING
;
310 } else if ( $v{0} == '"' ) {
311 $type = SMOPT_TYPE_STRING
;
314 if ( substr( $k, 0, 7 ) == '$theme[' ) {
315 $type = SMOPT_TYPE_THEME
;
316 } else if ( substr( $k, 0, 9 ) == '$plugins[' ) {
317 $type = SMOPT_TYPE_PLUGINS
;
318 } else if ( substr( $k, 0, 13 ) == '$ldap_server[' ) {
319 $type = SMOPT_TYPE_LDAP
;
320 } else if ( substr( $k, 0, 9 ) == '$fontsets' ||
321 substr( $k, 0, 13 ) == '$aTemplateSet' ) {
322 $type = SMOPT_TYPE_CUSTOM
;
325 if ( $type == SMOPT_TYPE_TITLE ||
$colapse[$act_grp] == 'off' ) {
328 case SMOPT_TYPE_LDAP
:
329 case SMOPT_TYPE_CUSTOM
:
330 case SMOPT_TYPE_PLUGINS
:
331 case SMOPT_TYPE_THEME
:
332 case SMOPT_TYPE_HIDDEN
:
334 case SMOPT_TYPE_EXTERNAL
:
335 echo "<tr><td>$name</td><td><b>" .
336 $defcfg[$k]['value'] .
339 case SMOPT_TYPE_TITLE
:
340 if ( $colapse[$k] == 'on' ) {
345 echo '<tr bgcolor="'.$color[0].'"><th colspan="2">'.
346 "<a href=\"options.php?switch=$k\" style=\"text-decoration:none\">".
347 '<b>'.$sw.'</b></a> '.$name.'</th></tr>';
350 case SMOPT_TYPE_COMMENT
:
351 $v = substr( $v, 1, strlen( $v ) - 2 );
352 echo "<tr><td>$name</td><td>".
354 $newcfg[$k] = "'$v'";
355 if ( isset( $defcfg[$k]['comment'] ) ) {
356 echo ' ' . $defcfg[$k]['comment'];
360 case SMOPT_TYPE_INTEGER
:
361 /* look for variable $e in POST, fill into $v */
362 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
363 $v = intval( $new_v );
366 echo "<tr><td>$name</td><td>".
367 "<input size=\"10\" name=\"adm_$n\" value=\"$v\" />";
368 if ( isset( $defcfg[$k]['comment'] ) ) {
369 echo ' ' . $defcfg[$k]['comment'];
373 case SMOPT_TYPE_NUMLIST
:
374 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
378 echo "<tr><td>$name</td><td>";
379 echo "<select name=\"adm_$n\">";
380 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
381 echo "<option value=\"$kp\"";
383 echo ' selected="selected"';
385 echo ">$vp</option>";
388 if ( isset( $defcfg[$k]['comment'] ) ) {
389 echo ' ' . $defcfg[$k]['comment'];
393 case SMOPT_TYPE_STRLIST
:
394 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
395 $v = '"' . $new_v . '"';
398 echo "<tr><td>$name</td><td>".
399 "<select name=\"adm_$n\">";
400 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
401 echo "<option value=\"$kp\"";
402 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
403 echo ' selected="selected"';
405 echo ">$vp</option>";
408 if ( isset( $defcfg[$k]['comment'] ) ) {
409 echo ' ' . $defcfg[$k]['comment'];
414 case SMOPT_TYPE_TEXTAREA
:
415 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
416 $v = '"' . addslashes($new_v) . '"';
417 $newcfg[$k] = str_replace( "\n", '', $v );
419 echo "<tr><td valign=\"top\">$name</td><td>"
420 ."<textarea cols=\"$size\" rows=\"4\" name=\"adm_$n\">"
421 .htmlspecialchars(stripslashes(substr( $v, 1, strlen( $v ) - 2 )))
423 if ( isset( $defcfg[$k]['comment'] ) ) {
424 echo ' ' . $defcfg[$k]['comment'];
428 case SMOPT_TYPE_STRING
:
429 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
430 $v = '"' . addslashes($new_v) . '"';
433 if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
434 $v = "'" . $defcfg[$k]['default'] . "'";
437 echo "<tr><td>$name</td><td>"
438 ."<input size=\"$size\" name=\"adm_$n\" value=\""
439 .htmlspecialchars(stripslashes(substr( $v, 1, strlen( $v ) - 2 )))
441 if ( isset( $defcfg[$k]['comment'] ) ) {
442 echo ' ' . $defcfg[$k]['comment'];
446 case SMOPT_TYPE_BOOLEAN
:
447 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
451 $v = strtoupper( $v );
453 if ( $v == 'TRUE' ) {
454 $ct = ' checked="checked"';
458 $cf = ' checked="checked"';
460 echo "<tr><td>$name</td><td>" .
461 "<input$ct type=\"radio\" name=\"adm_$n\" value=\"TRUE\" />" . _("Yes") .
462 "<input$cf type=\"radio\" name=\"adm_$n\" value=\"FALSE\" />" . _("No");
463 if ( isset( $defcfg[$k]['comment'] ) ) {
464 echo ' ' . $defcfg[$k]['comment'];
468 case SMOPT_TYPE_PATH
:
469 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
470 // FIXME: fix use of $data_dir in $attachment_dir
471 $v = change_to_sm_path($new_v);
474 if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
475 $v = change_to_sm_path($defcfg[$k]['default']);
478 echo "<tr><td>$name</td><td>".
479 "<input size=\"$size\" name=\"adm_$n\" value=\"" . change_to_rel_path($v) . '" />';
480 if ( isset( $defcfg[$k]['comment'] ) ) {
481 echo ' ' . $defcfg[$k]['comment'];
486 echo "<tr><td>$name</td><td>" .
488 if ( isset( $defcfg[$k]['comment'] ) ) {
489 echo ' ' . $defcfg[$k]['comment'];
496 /* Special Themes Block */
497 if ( $colapse['Group7'] == 'off' ) {
499 echo '<tr><th>' . _("Theme Name") .
500 '</th><th>' . _("Theme Path") .
502 while ( isset( $newcfg["\$theme[$i]['NAME']"] ) ) {
503 $k1 = "\$theme[$i]['NAME']";
504 $e1 = "theme_name_$i";
505 if ( sqgetGlobalVar($e, $v1, SQ_POST
) ) {
506 $v1 = '"' . str_replace( '\"', '"', $v1 ) . '"';
507 $v1 = '"' . str_replace( '"', '\"', $v1 ) . '"';
512 $k2 = "\$theme[$i]['PATH']";
513 $e2 = "theme_path_$i";
514 if ( sqgetGlobalVar($e, $v2, SQ_POST
) ) {
515 $v2 = change_to_sm_path($v2);
520 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
521 $path = change_to_rel_path($v2);
523 "<td align=\"right\">$i. <input name=\"$e1\" value=\"$name\" size=\"30\" /></td>".
524 "<td><input name=\"$e2\" value=\"$path\" size=\"40\" /></td>".
531 /* Special Plugins Block */
532 if ( $colapse['Group8'] == 'on' ) {
537 echo '<tr bgcolor="'.$color[0].'"><th colspan="2">'.
538 '<a href="options.php?switch=Group8" style="text-decoration:none"><b>'.
539 $sw.'</b></a> '._("Plugins").'</th></tr>';
541 if ( $colapse['Group8'] == 'off' ) {
543 $plugpath = SM_PATH
. 'plugins/';
544 if ( file_exists($plugpath) ) {
545 $fd = opendir( $plugpath );
546 $op_plugin = array();
548 while (false !== ($file = readdir($fd))) {
549 if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
550 $op_plugin[] = $file;
557 /* Lets get the plugins that are active */
559 if ( sqgetGlobalVar('plg', $v, SQ_POST
) ) {
560 foreach ( $op_plugin as $plg ) {
561 if ( sqgetGlobalVar("plgs_$plg", $v2, SQ_POST
) && $v2 == 'on' ) {
566 foreach ( $plugins as $plg ) {
567 $k = "\$plugins[$i]";
568 $newcfg[$k] = "'$plg'";
571 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
572 $k = "\$plugins[$i]";
578 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
579 $k = "\$plugins[$i]";
581 $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
585 echo '<tr><td colspan="2"><input type="hidden" name="plg" value="on" /><table align="center">';
586 foreach ( $op_plugin as $plg ) {
587 if ( in_array( $plg, $plugins ) ) {
588 $sw = ' checked="checked"';
593 if (file_exists(SM_PATH
. "plugins/$plg/README")) {
594 echo "<a href=\"../$plg/README\" target=\"_blank\">$plg</a>";
599 "<td><input$sw type=\"checkbox\" name=\"plgs_$plg\" /></td>".
602 echo '</table></td></tr>';
604 echo '<tr><td colspan="2" align="center">'.
605 sprintf(_("Plugin directory could not be found: %s"), $plugpath).
609 echo '<tr bgcolor="'.$color[5].'"><th colspan="2"><input value="'.
610 _("Change Settings").'" type="submit" /><br />'.
611 '<a href="'.SM_PATH
.'src/configtest.php" target="_blank">'.
612 _("Test Configuration")."</a></th></tr>\n".
613 '</table></td></tr></table></form>';
616 Write the options to the file.
619 if ( $fp = @fopen
( $cfgfile, 'w' ) ) {
620 fwrite( $fp, "<?php\n".
622 " * SquirrelMail Configuration File\n".
623 " * Created using the Administrator Plugin\n".
626 "global \$version;\n" );
628 foreach ( $newcfg as $k => $v ) {
629 if ( $k{0} == '$' && $v <> '' ||
is_int($v)) {
630 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
631 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
632 $v = str_replace( 'array(', "array(\n\t", $v );
633 $v = str_replace( "',", "',\n\t", $v );
635 /* FIXME: add elseif that reverts plugins[#] to plugins[] */
636 fwrite( $fp, "$k = $v;\n" );
643 echo '<br /><p align="center"><big>'.
644 _("Config file can't be opened. Please check config.php.").