7885b9a9733156c79b82a13441400b1b0ed72a00
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-2017 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @subpackage administrator
17 define('PAGE_NAME', 'administrator_options');
20 * parse the config file
22 * @param string $cfg_file
25 function parseConfig( $cfg_file ) {
29 $cfg = file( $cfg_file );
35 for ($i=0;$i<$l;$i++
) {
36 $line = trim( $cfg[$i] );
38 for ($j=0;$j<$s;$j++
) {
41 if ( $line{$j} == '=' ) {
42 // Ok, we've got a right value, lets detect what type
44 } else if ( $line{$j} == ';' ) {
51 if ( $line{$j} == ' ' ) {
55 // FIXME: this is only pour workaround for plugins[] array.
56 if ($line{$j}=='[' && $line{($j+
1)}==']') {
64 if ( $line{$j} == ';' ) {
69 if ( $line{$j} == '\\' ) {
72 } else if ( $line{$j} == $delimiter && $modifier === FALSE ) {
74 $newcfg[$key] = $value . $delimiter;
83 if ( $line{$j} == ';' ) {
84 $newcfg{$key} = $value;
93 $line{$j}.$line{$j+
1} == '*/' ) {
100 switch ( $line{$j} ) {
103 // Double quote string
104 $delimiter = $value = $line{$j};
111 if ( strtoupper( substr( $line, $j, 4 ) ) == 'TRUE' ) {
113 $newcfg{$key} = 'TRUE';
116 } else if ( strtoupper( substr( $line, $j, 5 ) ) == 'FALSE' ) {
117 $newcfg{$key} = 'FALSE';
121 // Number or function call
128 if ( $line{$j} == '$' ) {
129 // We must detect $key name
132 } else if ( $s < $j +
2 ) {
133 } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
134 // Skip untill next ;
137 } else if ( $line{$j}.$line{$j+
1} == '/*' ) {
140 } else if ( $line{$j} == '#' ||
$line{$j}.$line{$j+
1} == '//' ) {
141 // Delete till the end of the line
150 * Change paths containing SM_PATH to admin-friendly paths
151 * relative to the config dir, i.e.:
152 * '' --> <empty string>
153 * SM_PATH . 'images/logo.gif' --> ../images/logo.gif
154 * '/absolute/path/logo.gif' --> /absolute/path/logo.gif
155 * 'http://whatever/' --> http://whatever
156 * Note removal of quotes in returned value
158 * @param string $old_path path that has to be converted
159 * @return string new path
162 function change_to_rel_path($old_path) {
163 $new_path = str_replace("SM_PATH . '", "../", $old_path);
164 $new_path = str_replace("../config/","", $new_path);
165 $new_path = str_replace("'","", $new_path);
170 * Change relative path (relative to config dir) to
171 * internal SM_PATH, i.e.:
172 * empty_string --> ''
173 * ../images/logo.gif --> SM_PATH . 'images/logo.gif'
174 * images/logo.gif --> SM_PATH . 'config/images/logo.gif'
175 * C:/absolute/win/path --> 'C:/absolute/win/path'
176 * /absolute/path/logo.gif --> '/absolute/path/logo.gif'
177 * http://whatever/ --> 'http://whatever'
179 * @param string $old_path path that has to be converted
180 * @return string new path
183 function change_to_sm_path($old_path) {
184 if ( $old_path === '' ||
$old_path == "''" ) {
186 } elseif ( preg_match("/^(\/|http)/", $old_path) ||
187 substr($old_path,1,2) == ':/' ) {
188 return "'" . $old_path . "'";
189 } elseif ( preg_match("/^(\$|SM_PATH)/", $old_path) ) {
194 $rel_path = explode("../", $old_path);
195 if ( count($rel_path) > 2 ) {
196 // Since we're relative to the config dir,
197 // more than 1 ../ puts us OUTSIDE the SM tree.
198 // get full path to config.php, then pop the filename
199 $abs_path = explode('/', realpath (SM_PATH
. 'config/config.php'));
200 array_pop ($abs_path);
201 foreach ( $rel_path as $subdir ) {
202 if ( $subdir === '' ) {
203 array_pop ($abs_path);
205 array_push($abs_path, $subdir);
208 foreach ($abs_path as $subdir) {
209 $new_path .= $subdir . '/';
211 $new_path = "'$new_path'";
212 } elseif ( count($rel_path) > 1 ) {
213 // we're within the SM tree, prepend SM_PATH
214 $new_path = str_replace('../',"SM_PATH . '", $old_path . "'");
216 // Last, if it's a relative path without a .. prefix,
217 // we're somewhere within the config dir, so prepend
218 // SM_PATH . 'config/
219 $new_path = "SM_PATH . 'config/" . $old_path . "'";
225 /* ---------------------- main -------------------------- */
226 /** main SquirrelMail include */
227 require('../../include/init.php');
228 /* configuration definitions */
229 include_once(SM_PATH
. 'plugins/administrator/defines.php');
230 /* additional functions */
231 include_once(SM_PATH
. 'plugins/administrator/auth.php');
233 global $data_dir, $username;
235 if ( !adm_check_user() ) {
236 header('Location: ' . SM_PATH
. 'src/options.php') ;
240 displayPageHeader($color);
244 foreach ( $defcfg as $key => $def ) {
248 $cfgfile = SM_PATH
. 'config/config.php';
249 parseConfig( SM_PATH
. 'config/config_default.php' );
250 parseConfig( $cfgfile );
252 $colapse = array( 'Titles' => 'off',
253 'Group1' => getPref($data_dir, $username, 'adm_Group1', 'off' ),
254 'Group2' => getPref($data_dir, $username, 'adm_Group2', 'on' ),
255 'Group3' => getPref($data_dir, $username, 'adm_Group3', 'on' ),
256 'Group4' => getPref($data_dir, $username, 'adm_Group4', 'on' ),
257 'Group5' => getPref($data_dir, $username, 'adm_Group5', 'on' ),
258 'Group6' => getPref($data_dir, $username, 'adm_Group6', 'on' ),
259 'Group7' => getPref($data_dir, $username, 'adm_Group7', 'on' ),
260 'Group8' => getPref($data_dir, $username, 'adm_Group8', 'on' ),
261 'Group9' => getPref($data_dir, $username, 'adm_Group9', 'on' ),
262 'Group10' => getPref($data_dir, $username, 'adm_Group10', 'on' ),
263 'Group11' => getPref($data_dir, $username, 'adm_Group11', 'on' ) );
265 /* look in $_GET array for 'switch' */
266 if ( sqgetGlobalVar('switch', $switch, SQ_GET
) ) {
267 if ( $colapse[$switch] == 'on' ) {
268 $colapse[$switch] = 'off';
270 $colapse[$switch] = 'on';
272 setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
275 echo '<form action="options.php" method="post" name="options">' .
276 '<table width="95%" align="center" bgcolor="'.$color[5].'"><tr><td>'.
277 '<table width="100%" cellspacing="0" bgcolor="'.$color[4].'">'.
278 '<tr bgcolor="'.$color[5].'"><th colspan="2">'.
279 _("Configuration Administrator").'</th></tr>'.
280 '<tr bgcolor="'.$color[5].'"><td colspan="2" align="center"><small>'.
281 _("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.").
283 _("Run or consult conf.pl should you run into difficulty with your configuration.").
284 '</small></td></tr>';
287 $act_grp = 'Titles'; /* Active group */
289 foreach ( $newcfg as $k => $v ) {
290 $l = strtolower( $v );
291 $type = SMOPT_TYPE_UNDEFINED
;
292 $n = substr( $k, 1 );
293 $n = str_replace( '[', '_', $n );
294 $n = str_replace( ']', '_', $n );
298 if ( isset( $defcfg[$k] ) ) {
299 $name = $defcfg[$k]['name'];
300 $type = $defcfg[$k]['type'];
301 if ( isset( $defcfg[$k]['size'] ) ) {
302 $size = $defcfg[$k]['size'];
306 } else if ( $l == 'true' ) {
308 $type = SMOPT_TYPE_BOOLEAN
;
309 } else if ( $l == 'false' ) {
311 $type = SMOPT_TYPE_BOOLEAN
;
312 } else if ( $v{0} == "'" ) {
313 $type = SMOPT_TYPE_STRING
;
314 } else if ( $v{0} == '"' ) {
315 $type = SMOPT_TYPE_STRING
;
318 if ( substr( $k, 0, 7 ) == '$theme[' ) {
319 $type = SMOPT_TYPE_THEME
;
320 } else if ( substr( $k, 0, 9 ) == '$plugins[' ) {
321 $type = SMOPT_TYPE_PLUGINS
;
322 } else if ( substr( $k, 0, 13 ) == '$ldap_server[' ) {
323 $type = SMOPT_TYPE_LDAP
;
324 } else if ( substr( $k, 0, 9 ) == '$fontsets' ||
325 substr( $k, 0, 13 ) == '$aTemplateSet' ) {
326 $type = SMOPT_TYPE_CUSTOM
;
329 if ( $type == SMOPT_TYPE_TITLE ||
$colapse[$act_grp] == 'off' ) {
332 case SMOPT_TYPE_LDAP
:
333 case SMOPT_TYPE_CUSTOM
:
334 case SMOPT_TYPE_PLUGINS
:
335 case SMOPT_TYPE_THEME
:
336 case SMOPT_TYPE_HIDDEN
:
338 case SMOPT_TYPE_EXTERNAL
:
339 echo "<tr><td>$name</td><td><b>" .
340 $defcfg[$k]['value'] .
343 case SMOPT_TYPE_TITLE
:
344 if ( $colapse[$k] == 'on' ) {
349 echo '<tr bgcolor="'.$color[0].'"><th colspan="2">'.
350 "<a href=\"options.php?switch=$k\" style=\"text-decoration:none\">".
351 '<b>'.$sw.'</b></a> '.$name.'</th></tr>';
354 case SMOPT_TYPE_COMMENT
:
355 $v = substr( $v, 1, strlen( $v ) - 2 );
356 echo "<tr><td>$name</td><td>".
358 $newcfg[$k] = "'$v'";
359 if ( isset( $defcfg[$k]['comment'] ) ) {
360 echo ' ' . $defcfg[$k]['comment'];
364 case SMOPT_TYPE_INTEGER
:
365 /* look for variable $e in POST, fill into $v */
366 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
367 $v = intval( $new_v );
370 echo "<tr><td>$name</td><td>".
371 "<input size=\"10\" name=\"adm_$n\" value=\"$v\" />";
372 if ( isset( $defcfg[$k]['comment'] ) ) {
373 echo ' ' . $defcfg[$k]['comment'];
377 case SMOPT_TYPE_NUMLIST
:
378 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
382 echo "<tr><td>$name</td><td>";
383 echo "<select name=\"adm_$n\">";
384 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
385 echo "<option value=\"$kp\"";
387 echo ' selected="selected"';
389 echo ">$vp</option>";
392 if ( isset( $defcfg[$k]['comment'] ) ) {
393 echo ' ' . $defcfg[$k]['comment'];
397 case SMOPT_TYPE_STRLIST
:
398 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
399 $v = '"' . $new_v . '"';
402 echo "<tr><td>$name</td><td>".
403 "<select name=\"adm_$n\">";
404 foreach ( $defcfg[$k]['posvals'] as $kp => $vp ) {
405 echo "<option value=\"$kp\"";
406 if ( $kp == substr( $v, 1, strlen( $v ) - 2 ) ) {
407 echo ' selected="selected"';
409 echo ">$vp</option>";
412 if ( isset( $defcfg[$k]['comment'] ) ) {
413 echo ' ' . $defcfg[$k]['comment'];
418 case SMOPT_TYPE_TEXTAREA
:
419 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
420 $v = '"' . addslashes($new_v) . '"';
421 $newcfg[$k] = str_replace( "\n", '', $v );
423 echo "<tr><td valign=\"top\">$name</td><td>"
424 ."<textarea cols=\"$size\" rows=\"4\" name=\"adm_$n\">"
425 .sm_encode_html_special_chars(stripslashes(substr( $v, 1, strlen( $v ) - 2 )))
427 if ( isset( $defcfg[$k]['comment'] ) ) {
428 echo ' ' . $defcfg[$k]['comment'];
432 case SMOPT_TYPE_STRING
:
433 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
434 $v = '"' . addslashes($new_v) . '"';
437 if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
438 $v = "'" . $defcfg[$k]['default'] . "'";
441 echo "<tr><td>$name</td><td>"
442 ."<input size=\"$size\" name=\"adm_$n\" value=\""
443 .sm_encode_html_special_chars(stripslashes(substr( $v, 1, strlen( $v ) - 2 )))
445 if ( isset( $defcfg[$k]['comment'] ) ) {
446 echo ' ' . $defcfg[$k]['comment'];
450 case SMOPT_TYPE_BOOLEAN
:
451 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
455 $v = strtoupper( $v );
457 if ( $v == 'TRUE' ) {
458 $ct = ' checked="checked"';
462 $cf = ' checked="checked"';
464 echo "<tr><td>$name</td><td>" .
465 "<input$ct type=\"radio\" name=\"adm_$n\" value=\"TRUE\" />" . _("Yes") .
466 "<input$cf type=\"radio\" name=\"adm_$n\" value=\"FALSE\" />" . _("No");
467 if ( isset( $defcfg[$k]['comment'] ) ) {
468 echo ' ' . $defcfg[$k]['comment'];
472 case SMOPT_TYPE_PATH
:
473 if ( sqgetGlobalVar($e, $new_v, SQ_POST
) ) {
474 // FIXME: fix use of $data_dir in $attachment_dir
475 $v = change_to_sm_path($new_v);
478 if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
479 $v = change_to_sm_path($defcfg[$k]['default']);
482 echo "<tr><td>$name</td><td>".
483 "<input size=\"$size\" name=\"adm_$n\" value=\"" . change_to_rel_path($v) . '" />';
484 if ( isset( $defcfg[$k]['comment'] ) ) {
485 echo ' ' . $defcfg[$k]['comment'];
490 echo "<tr><td>$name</td><td>" .
492 if ( isset( $defcfg[$k]['comment'] ) ) {
493 echo ' ' . $defcfg[$k]['comment'];
500 /* Special Themes Block */
501 if ( $colapse['Group7'] == 'off' ) {
503 echo '<tr><th>' . _("Theme Name") .
504 '</th><th>' . _("Theme Path") .
506 while ( isset( $newcfg["\$theme[$i]['NAME']"] ) ) {
507 $k1 = "\$theme[$i]['NAME']";
508 $e1 = "theme_name_$i";
509 if ( sqgetGlobalVar($e, $v1, SQ_POST
) ) {
510 $v1 = '"' . str_replace( '\"', '"', $v1 ) . '"';
511 $v1 = '"' . str_replace( '"', '\"', $v1 ) . '"';
516 $k2 = "\$theme[$i]['PATH']";
517 $e2 = "theme_path_$i";
518 if ( sqgetGlobalVar($e, $v2, SQ_POST
) ) {
519 $v2 = change_to_sm_path($v2);
524 $name = substr( $v1, 1, strlen( $v1 ) - 2 );
525 $path = change_to_rel_path($v2);
527 "<td align=\"right\">$i. <input name=\"$e1\" value=\"$name\" size=\"30\" /></td>".
528 "<td><input name=\"$e2\" value=\"$path\" size=\"40\" /></td>".
535 /* Special Plugins Block */
536 if ( $colapse['Group8'] == 'on' ) {
541 echo '<tr bgcolor="'.$color[0].'"><th colspan="2">'.
542 '<a href="options.php?switch=Group8" style="text-decoration:none"><b>'.
543 $sw.'</b></a> '._("Plugins").'</th></tr>';
545 if ( $colapse['Group8'] == 'off' ) {
547 $plugpath = SM_PATH
. 'plugins/';
548 if ( file_exists($plugpath) ) {
549 $fd = opendir( $plugpath );
550 $op_plugin = array();
552 while (false !== ($file = readdir($fd))) {
553 if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
554 $op_plugin[] = $file;
561 /* Lets get the plugins that are active */
563 if ( sqgetGlobalVar('plg', $v, SQ_POST
) ) {
564 foreach ( $op_plugin as $plg ) {
565 if ( sqgetGlobalVar("plgs_$plg", $v2, SQ_POST
) && $v2 == 'on' ) {
570 foreach ( $plugins as $plg ) {
571 $k = "\$plugins[$i]";
572 $newcfg[$k] = "'$plg'";
575 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
576 $k = "\$plugins[$i]";
582 while ( isset( $newcfg["\$plugins[$i]"] ) ) {
583 $k = "\$plugins[$i]";
585 $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
589 echo '<tr><td colspan="2"><input type="hidden" name="plg" value="on" /><table align="center">';
590 foreach ( $op_plugin as $plg ) {
591 if ( in_array( $plg, $plugins ) ) {
592 $sw = ' checked="checked"';
597 if (file_exists(SM_PATH
. "plugins/$plg/README")) {
598 echo "<a href=\"../$plg/README\" target=\"_blank\">$plg</a>";
603 "<td><input$sw type=\"checkbox\" name=\"plgs_$plg\" /></td>".
606 echo '</table></td></tr>';
608 echo '<tr><td colspan="2" align="center">'.
609 sprintf(_("Plugin directory could not be found: %s"), $plugpath).
613 echo '<tr bgcolor="'.$color[5].'"><th colspan="2"><input value="'.
614 _("Change Settings").'" type="submit" /><br />'.
615 '<a href="'.SM_PATH
.'src/configtest.php" target="_blank">'.
616 _("Test Configuration")."</a></th></tr>\n".
617 '</table></td></tr></table></form>';
620 Write the options to the file.
623 if ( $fp = @fopen
( $cfgfile, 'w' ) ) {
624 fwrite( $fp, "<?php\n".
626 " * SquirrelMail Configuration File\n".
627 " * Created using the Administrator Plugin\n".
631 foreach ( $newcfg as $k => $v ) {
632 if ( $k{0} == '$' && $v <> '' ||
is_int($v)) {
633 if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
634 $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
635 $v = str_replace( 'array(', "array(\n\t", $v );
636 $v = str_replace( "',", "',\n\t", $v );
638 /* FIXME: add elseif that reverts plugins[#] to plugins[] */
639 fwrite( $fp, "$k = $v;\n" );
646 echo '<br /><p align="center"><big>'.
647 _("Config file can't be opened. Please check config.php.").