4 * Administrator Plugin - Options Page
6 * This script creates separate page, that allows to review and modify
7 * SquirrelMail configuration file.
10 * @author Philippe Mingo
11 * @copyright (c) 1999-2004 The SquirrelMail Project Team
12 * @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 );
32 for ($i=0;$i<$l;$i++
) {
33 $line = trim( $cfg[$i] );
35 for ($j=0;$j<$s;$j++
) {
38 if ( $line{$j} == '=' ) {
39 // Ok, we've got a right value, lets detect what type
41 } else if ( $line{$j} == ';' ) {
48 if( $line{$j} == ' ' ) {
56 if ( $line{$j} == ';' ) {
61 if ( $line{$j} == '\\' ) {
64 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
66 $newcfg[$key] = $value . $delimiter;
75 if ( $line{$j} == ';' ) {
76 $newcfg{$key} = $value;
85 $line{$j}.$line{$j+
1} == '*/' ) {
92 switch ( $line{$j} ) {
95 // Double quote string
96 $delimiter = $value = $line{$j};
103 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
105 $newcfg{$key} = 'TRUE';
108 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
109 $newcfg{$key} = 'FALSE';
113 // Number or function call
120 if ( $line{$j} == '$' ) {
121 // We must detect $key name
124 } else if ( $s < $j +
2 ) {
125 } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
126 // Skip untill next ;
129 } else if ( $line{$j}.$line{$j+
1} == '/*' ) {
132 } else if ( $line{$j} == '#' ||
$line{$j}.$line{$j+
1} == '//' ) {
133 // Delete till the end of the line
142 * Change paths containing SM_PATH to admin-friendly paths
143 * relative to the config dir, i.e.:
144 * '' --> <empty string>
145 * SM_PATH . 'images/logo.gif' --> ../images/logo.gif
146 * '/absolute/path/logo.gif' --> /absolute/path/logo.gif
147 * 'http://whatever/' --> http://whatever
148 * Note removal of quotes in returned value
150 * @param string $old_path path that has to be converted
151 * @return string new path
154 function change_to_rel_path($old_path) {
155 $new_path = str_replace("SM_PATH . '", "../", $old_path);
156 $new_path = str_replace("../config/","", $new_path);
157 $new_path = str_replace("'","", $new_path);
162 * Change relative path (relative to config dir) to
163 * internal SM_PATH, i.e.:
164 * empty_string --> ''
165 * ../images/logo.gif --> SM_PATH . 'images/logo.gif'
166 * images/logo.gif --> SM_PATH . 'config/images/logo.gif'
167 * /absolute/path/logo.gif --> '/absolute/path/logo.gif'
168 * http://whatever/ --> 'http://whatever'
170 * @param string $old_path path that has to be converted
171 * @return string new path
174 function change_to_sm_path($old_path) {
175 if ( $old_path === '' ||
$old_path == "''" ) {
177 } elseif ( preg_match("/^(\/|http)/", $old_path) ) {
178 return "'" . $old_path . "'";
179 } elseif ( preg_match("/^(\$|SM_PATH)/", $old_path) ) {
184 $rel_path = explode("../", $old_path);
185 if ( count($rel_path) > 2 ) {
186 // Since we're relative to the config dir,
187 // more than 1 ../ puts us OUTSIDE the SM tree.
188 // get full path to config.php, then pop the filename
189 $abs_path = explode('/', realpath (SM_PATH
. 'config/config.php'));
190 array_pop ($abs_path);
191 foreach ( $rel_path as $subdir ) {
192 if ( $subdir === '' ) {
193 array_pop ($abs_path);
195 array_push($abs_path, $subdir);
198 foreach ($abs_path as $subdir) {
199 $new_path .= $subdir . '/';
201 $new_path = "'$new_path'";
202 } elseif ( count($rel_path) > 1 ) {
203 // we're within the SM tree, prepend SM_PATH
204 $new_path = str_replace('../',"SM_PATH . '", $old_path . "'");
206 // Last, if it's a relative path without a .. prefix,
207 // we're somewhere within the config dir, so prepend
208 // SM_PATH . 'config/
209 $new_path = "SM_PATH . 'config/" . $old_path . "'";
215 /* ---------------------- main -------------------------- */
218 define('SM_PATH','../../');
220 /* SquirrelMail required files. */
221 require_once(SM_PATH
. 'include/validate.php');
222 require_once(SM_PATH
. 'functions/page_header.php');
223 require_once(SM_PATH
. 'functions/imap.php');
224 require_once(SM_PATH
. 'include/load_prefs.php');
225 require_once(SM_PATH
. 'plugins/administrator/defines.php');
226 require_once(SM_PATH
. 'plugins/administrator/auth.php');
228 global $data_dir, $username;
230 if ( !adm_check_user() ) {
231 header('Location: ' . SM_PATH
. 'src/options.php') ;
235 displayPageHeader($color, 'None');
239 foreach ( $defcfg as $key => $def ) {
243 $cfgfile = SM_PATH
. 'config/config.php';
244 parseConfig( SM_PATH
. 'config/config_default.php' );
245 parseConfig( $cfgfile );
247 $colapse = array( 'Titles' => 'off',
248 'Group1' => getPref($data_dir, $username, 'adm_Group1', 'off' ),
249 'Group2' => getPref($data_dir, $username, 'adm_Group2', 'on' ),
250 'Group3' => getPref($data_dir, $username, 'adm_Group3', 'on' ),
251 'Group4' => getPref($data_dir, $username, 'adm_Group4', 'on' ),
252 'Group5' => getPref($data_dir, $username, 'adm_Group5', 'on' ),
253 'Group6' => getPref($data_dir, $username, 'adm_Group6', 'on' ),
254 'Group7' => getPref($data_dir, $username, 'adm_Group7', 'on' ),
255 'Group8' => getPref($data_dir, $username, 'adm_Group8', 'on' ),
256 'Group9' => getPref($data_dir, $username, 'adm_Group9', 'on' ),
257 'Group10' => getPref($data_dir, $username, 'adm_Group10', 'on' ) );
259 /* look in $_GET array for 'switch' */
260 if ( sqgetGlobalVar('switch', $switch, SQ_GET
) ) {
261 if ( $colapse[$switch] == 'on' ) {
262 $colapse[$switch] = 'off';
264 $colapse[$switch] = 'on';
266 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
269 echo '<form action="options.php" method="post" name="options">' .
270 "<center><table width=\"95%\" bgcolor=\"$color[5]\"><tr><td>".
271 "<table width=\"100%\" cellspacing=\"0\" bgcolor=\"$color[4]\">" ,
272 "<tr bgcolor=\"$color[5]\"><th colspan=\"2\">" . _("Configuration Administrator") . "</th></tr>",
273 "<tr bgcolor=\"$color[5]\"><td colspan=\"2\" align=\"center\">";
276 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.");
278 echo _("Run or consult conf.pl should you run into difficulty with your configuration.");
279 echo "</small></td></tr>";
282 $act_grp = 'Titles'; /* Active group */
284 foreach ( $newcfg as $k => $v ) {
285 $l = strtolower( $v );
286 $type = SMOPT_TYPE_UNDEFINED
;
287 $n = substr( $k, 1 );
288 $n = str_replace( '[', '_', $n );
289 $n = str_replace( ']', '_', $n );
293 if ( isset( $defcfg[$k] ) ) {
294 $name = $defcfg[$k]['name'];
295 $type = $defcfg[$k]['type'];
296 if ( isset( $defcfg[$k]['size'] ) ) {
297 $size = $defcfg[$k]['size'];
301 } else if ( $l == 'true' ) {
303 $type = SMOPT_TYPE_BOOLEAN
;
304 } else if ( $l == 'false' ) {
306 $type = SMOPT_TYPE_BOOLEAN
;
307 } else if ( $v{0} == "'" ) {
308 $type = SMOPT_TYPE_STRING
;
309 } else if ( $v{0} == '"' ) {
310 $type = SMOPT_TYPE_STRING
;
313 if ( substr( $k, 0, 7 ) == '$theme[' ) {
314 $type = SMOPT_TYPE_THEME
;
315 } else if ( substr( $k, 0, 9 ) == '$plugins[' ) {
316 $type = SMOPT_TYPE_PLUGINS
;
317 } else if ( substr( $k, 0, 13 ) == '$ldap_server[' ) {
318 $type = SMOPT_TYPE_LDAP
;
321 if( $type == SMOPT_TYPE_TITLE ||
$colapse[$act_grp] == 'off' ) {
324 case SMOPT_TYPE_LDAP
:
325 case SMOPT_TYPE_PLUGINS
:
326 case SMOPT_TYPE_THEME
:
327 case SMOPT_TYPE_HIDDEN
:
329 case SMOPT_TYPE_EXTERNAL
:
330 echo "<tr><td>$name</td><td><b>" .
331 $defcfg[$k]['value'] .
334 case SMOPT_TYPE_TITLE
:
335 if ( $colapse[$k] == 'on' ) {
340 echo "<tr bgcolor=\"$color[0]\"><th colspan=\"2\">" .
341 "<a href=\"options.php?switch=$k\" style=\"text-decoration:none\"><b>$sw</b> </a>" .
345 case SMOPT_TYPE_COMMENT
:
346 $v = substr( $v, 1, strlen( $v ) - 2 );
347 echo "<tr><td>$name</td><td>".
349 $newcfg[$k] = "'$v'";
350 if ( isset( $defcfg[$k]['comment'] ) ) {
351 echo ' ' . $defcfg[$k]['comment'];
355 case SMOPT_TYPE_INTEGER
:
356 /* look for variable $e in POST, fill into $v */
357 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
361 echo "<tr><td>$name</td><td>".
362 "<input size=\"10\" name=\"adm_$n\" value=\"$v\">";
363 if ( isset( $defcfg[$k]['comment'] ) ) {
364 echo ' ' . $defcfg[$k]['comment'];
368 case SMOPT_TYPE_NUMLIST
:
369 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
372 echo "<tr><td>$name</td><td>";
373 echo "<select name=\"adm_$n\">";
374 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
375 echo "<option value=\"$kp\"";
379 echo ">$vp</option>";
382 if ( isset( $defcfg[$k]['comment'] ) ) {
383 echo ' ' . $defcfg[$k]['comment'];
387 case SMOPT_TYPE_STRLIST
:
388 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
392 echo "<tr><td>$name</td><td>".
393 "<select name=\"adm_$n\">";
394 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
395 echo "<option value=\"$kp\"";
396 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
399 echo ">$vp</option>";
402 if ( isset( $defcfg[$k]['comment'] ) ) {
403 echo ' ' . $defcfg[$k]['comment'];
408 case SMOPT_TYPE_TEXTAREA
:
409 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
411 $newcfg[$k] = str_replace( "\n", '', $v );
413 echo "<tr><td valign=\"top\">$name</td><td>".
414 "<textarea cols=\"$size\" rows=\"4\" name=\"adm_$n\">" . substr( $v, 1, strlen( $v ) - 2 ) . "</textarea>";
415 if ( isset( $defcfg[$k]['comment'] ) ) {
416 echo ' ' . $defcfg[$k]['comment'];
420 case SMOPT_TYPE_STRING
:
421 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
425 if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
426 $v = "'" . $defcfg[$k]['default'] . "'";
429 echo "<tr><td>$name</td><td>".
430 "<input size=\"$size\" name=\"adm_$n\" value=\"" . substr( $v, 1, strlen( $v ) - 2 ) . "\">";
431 if ( isset( $defcfg[$k]['comment'] ) ) {
432 echo ' ' . $defcfg[$k]['comment'];
436 case SMOPT_TYPE_BOOLEAN
:
437 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
440 $v = strtoupper( $v );
442 if ( $v == 'TRUE' ) {
449 echo "<tr><td>$name</td><td>" .
450 "<input$ct type=\"radio\" name=\"adm_$n\" value=\"TRUE\">" . _("Yes") .
451 "<input$cf type=\"radio\" name=\"adm_$n\" value=\"FALSE\">" . _("No");
452 if ( isset( $defcfg[$k]['comment'] ) ) {
453 echo ' ' . $defcfg[$k]['comment'];
457 case SMOPT_TYPE_PATH
:
458 if ( sqgetGlobalVar($e, $v, SQ_POST
) ) {
459 $v = change_to_sm_path($v);
462 if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
463 $v = change_to_sm_path($defcfg[$k]['default']);
466 echo "<tr><td>$name</td><td>".
467 "<input size=\"$size\" name=\"adm_$n\" value=\"" . change_to_rel_path($v) . "\">";
468 if ( isset( $defcfg[$k]['comment'] ) ) {
469 echo ' ' . $defcfg[$k]['comment'];
474 echo "<tr><td>$name</td><td>" .
476 if ( isset( $defcfg[$k]['comment'] ) ) {
477 echo ' ' . $defcfg[$k]['comment'];
484 /* Special Themes Block */
485 if ( $colapse['Group7'] == 'off' ) {
487 echo '<tr><th>' . _("Theme Name") .
488 '</th><th>' . _("Theme Path") .
490 while ( isset( $newcfg["\$theme[$i]['NAME']"] ) ) {
491 $k1 = "\$theme[$i]['NAME']";
492 $e1 = "theme_name_$i";
493 if ( sqgetGlobalVar($e, $v1, SQ_POST
) ) {
494 $v1 = '"' . str_replace( '\"', '"', $v1 ) . '"';
495 $v1 = '"' . str_replace( '"', '\"', $v1 ) . '"';
500 $k2 = "\$theme[$i]['PATH']";
501 $e2 = "theme_path_$i";
502 if ( sqgetGlobalVar($e, $v2, SQ_POST
) ) {
503 $v2 = change_to_sm_path($v2);
508 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
509 $path = change_to_rel_path($v2);
511 "<td align=\"right\">$i. <input name=\"$e1\" value=\"$name\" size=\"30\"></td>".
512 "<td><input name=\"$e2\" value=\"$path\" size=\"40\"></td>".
519 /* Special Plugins Block */
520 if ( $colapse['Group8'] == 'on' ) {
525 echo "<tr bgcolor=\"$color[0]\"><th colspan=\"2\">" .
526 "<a href=\"options.php?switch=Group8\" style=\"text-decoration:none\"><b>$sw</b> </a>" .
527 _("Plugins") . '</th></tr>';
529 if( $colapse['Group8'] == 'off' ) {
531 $plugpath = SM_PATH
. 'plugins/';
532 if ( file_exists($plugpath) ) {
533 $fd = opendir( $plugpath );
534 $op_plugin = array();
536 while (false !== ($file = readdir($fd))) {
537 if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
538 $op_plugin[] = $file;
545 /* Lets get the plugins that are active */
547 if ( sqgetGlobalVar('plg', $v, SQ_POST
) ) {
548 foreach ( $op_plugin as $plg ) {
549 if ( sqgetGlobalVar("plgs_$plg", $v, SQ_POST
) && $v == 'on' ) {
554 foreach ( $plugins as $plg ) {
555 $k = "\$plugins[$i]";
556 $newcfg[$k] = "'$plg'";
559 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
560 $k = "\$plugins[$i]";
566 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
567 $k = "\$plugins[$i]";
569 $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
573 echo "<tr><td colspan=\"2\"><input type=\"hidden\" name=\"plg\" value=\"on\"><center><table>";
574 foreach ( $op_plugin as $plg ) {
575 if ( in_array( $plg, $plugins ) ) {
581 if (file_exists(SM_PATH
. "plugins/$plg/README")) {
582 echo "<a href=\"../$plg/README\" target=\"_blank\">$plg</a>";
587 "<td><input$sw type=\"checkbox\" name=\"plgs_$plg\"></td>".
590 echo '</table></center></td></tr>';
592 echo '<tr><td colspan="2" align="center">' . sprintf(_("Plugin directory could not be found: %s"),$plugpath) . "</td></tr>\n";
595 echo "<tr bgcolor=\"$color[5]\"><th colspan=\"2\"><input value=\"" .
596 _("Change Settings") . '" type="submit"><br />'.
597 '<a href="'.SM_PATH
.'src/configtest.php" target="_blank">'._("Test Configuration").
599 '</table></td></tr></table></center></form>';
602 Write the options to the file.
605 if( $fp = @fopen
( $cfgfile, 'w' ) ) {
606 fwrite( $fp, "<?PHP\n".
608 " * SquirrelMail Configuration File\n".
609 " * Created using the Administrator Plugin\n".
612 "global \$version;\n" );
614 foreach ( $newcfg as $k => $v ) {
615 if ( $k{0} == '$' && $v <> '' ||
is_int($v)) {
616 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
617 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
618 $v = str_replace( 'array(', "array(\n\t", $v );
619 $v = str_replace( "',", "',\n\t", $v );
621 fwrite( $fp, "$k = $v;\n" );
627 echo '<br /><p align="center"><big>'.
628 _("Config file can't be opened. Please check config.php.").