Time to stop coding for tonight. Making this kind of terrible bugs is bad.
[squirrelmail.git] / plugins / administrator / options.php
index 3d045bec5d76969e2e19742d0c459cc1346ee68a..32c7cb77849c128dbe5596ddf3fdae969343752e 100644 (file)
@@ -72,7 +72,8 @@ function parseConfig( $cfg_file ) {
                 break;
             case 'C':
                 // Comments
-                if ( $line{$j}.$line{$j+1} == '*/' ) {
+                if ( $s > $j + 1  &&
+                     $line{$j}.$line{$j+1} == '*/' ) {
                     $mode = '';
                     $j++;
                 }
@@ -107,7 +108,12 @@ function parseConfig( $cfg_file ) {
                 }
                 break;
             default:
-                if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
+                if ( $line{$j} == '$' ) {
+                    // We must detect $key name
+                    $mode = 'K';
+                    $key = '$';
+                } else if ( $s < $j + 2 ) {
+                } else if ( strtoupper( substr( $line, $j, 7 ) ) == 'GLOBAL ' ) {
                     // Skip untill next ;
                     $mode = ';';
                     $j += 6;
@@ -117,10 +123,6 @@ function parseConfig( $cfg_file ) {
                 } else if ( $line{$j} == '#' || $line{$j}.$line{$j+1} == '//' ) {
                     // Delete till the end of the line
                     $j = $s;
-                } else if ( $line{$j} == '$' ) {
-                    // We must detect $key name
-                    $mode = 'K';
-                    $key = '$';
                 }
             }
         }
@@ -130,13 +132,15 @@ function parseConfig( $cfg_file ) {
 
 /* ---------------------- main -------------------------- */
 
-chdir('..');
-require_once('../src/validate.php');
-require_once('../functions/page_header.php');
-require_once('../functions/imap.php');
-require_once('../src/load_prefs.php');
-require_once('../plugins/administrator/defines.php');
-require_once('../plugins/administrator/auth.php');
+define('SM_PATH','../../');
+
+/* SquirrelMail required files. */
+require_once(SM_PATH . 'include/validate.php');
+require_once(SM_PATH . 'functions/page_header.php');
+require_once(SM_PATH . 'functions/imap.php');
+require_once(SM_PATH . 'include/load_prefs.php');
+require_once(SM_PATH . 'plugins/administrator/defines.php');
+require_once(SM_PATH . 'plugins/administrator/auth.php');
 
 GLOBAL $data_dir, $username;
 
@@ -157,23 +161,29 @@ $cfgfile = '../config/config.php';
 parseConfig( '../config/config_default.php' );
 parseConfig( $cfgfile );
 
-$colapse = array( 'Titles' => FALSE,
-                  'Group1' => getPref($data_dir, $username, 'adm_Group1', FALSE ),
-                  'Group2' => getPref($data_dir, $username, 'adm_Group2', TRUE ),
-                  'Group3' => getPref($data_dir, $username, 'adm_Group3', TRUE ),
-                  'Group4' => getPref($data_dir, $username, 'adm_Group4', TRUE ),
-                  'Group5' => getPref($data_dir, $username, 'adm_Group5', TRUE ),
-                  'Group6' => getPref($data_dir, $username, 'adm_Group6', TRUE ),
-                  'Group7' => getPref($data_dir, $username, 'adm_Group7', TRUE ),
-                  'Group8' => getPref($data_dir, $username, 'adm_Group8', TRUE ) );
+$colapse = array( 'Titles' => 'off',
+                  'Group1' => getPref($data_dir, $username, 'adm_Group1', 'off' ),
+                  'Group2' => getPref($data_dir, $username, 'adm_Group2', 'on' ),
+                  'Group3' => getPref($data_dir, $username, 'adm_Group3', 'on' ),
+                  'Group4' => getPref($data_dir, $username, 'adm_Group4', 'on' ),
+                  'Group5' => getPref($data_dir, $username, 'adm_Group5', 'on' ),
+                  'Group6' => getPref($data_dir, $username, 'adm_Group6', 'on' ),
+                  'Group7' => getPref($data_dir, $username, 'adm_Group7', 'on' ),
+                  'Group8' => getPref($data_dir, $username, 'adm_Group8', 'on' ) );
 
 if ( isset( $switch ) ) {
-    $colapse[$switch] = !$colapse[$switch];
+
+    if ( $colapse[$switch] == 'on' ) {
+       $colapse[$switch] = 'off';
+    } else {
+       $colapse[$switch] = 'on';
+    }
     setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
+
 }
 
-echo "<form action=options.php method=post>" .
-    "<br><center><table width=95% bgcolor=\"$color[5]\"><tr><td>".
+echo "<form action=options.php method=post name=options>" .
+    "<center><table width=95% bgcolor=\"$color[5]\"><tr><td>".
     "<table width=100% cellspacing=0 bgcolor=\"$color[4]\">" ,
     "<tr bgcolor=\"$color[5]\"><th colspan=2>" . _("Configuration Administrator") . "</th></tr>";
 
@@ -191,7 +201,11 @@ foreach ( $newcfg as $k => $v ) {
     if ( isset( $defcfg[$k] ) ) {
         $name = $defcfg[$k]['name'];
         $type = $defcfg[$k]['type'];
-        $size = $defcfg[$k]['size'];
+        if ( isset( $defcfg[$k]['size'] ) ) {
+            $size = $defcfg[$k]['size'];
+        } else {
+            $size = 40;
+        }
     } else if ( $l == 'true' ) {
         $v = 'TRUE';
         $type = SMOPT_TYPE_BOOLEAN;
@@ -212,7 +226,7 @@ foreach ( $newcfg as $k => $v ) {
         $type = SMOPT_TYPE_LDAP;
     }
 
-    if( $type == SMOPT_TYPE_TITLE || !$colapse[$act_grp] ) {
+    if( $type == SMOPT_TYPE_TITLE || $colapse[$act_grp] == 'off' ) {
 
         switch ( $type ) {
         case SMOPT_TYPE_LDAP:
@@ -220,8 +234,13 @@ foreach ( $newcfg as $k => $v ) {
         case SMOPT_TYPE_THEME:
         case SMOPT_TYPE_HIDDEN:
             break;
+        case SMOPT_TYPE_EXTERNAL:
+            echo "<tr><td>$name</td><td><b>" .
+                 $defcfg[$k]['value'] .
+                 "</b></td></tr>";
+            break;
         case SMOPT_TYPE_TITLE:
-            if ( $colapse[$k] ) {
+            if ( $colapse[$k] == 'on' ) {
                 $sw = '(+)';
             } else {
                 $sw = '(-)';
@@ -311,6 +330,10 @@ foreach ( $newcfg as $k => $v ) {
                 $v = '"' . $HTTP_POST_VARS[$e] . '"';
                 $newcfg[$k] = $v;
             }
+            if ( $v == '""' && isset( $defcfg[$k]['default'] ) ) {
+                $v = "'" . $defcfg[$k]['default'] . "'";
+                $newcfg[$k] = $v;
+            }
             echo "<tr><td>$name</td><td>".
                  "<input size=\"$size\" name=\"adm_$n\" value=\"" . substr( $v, 1, strlen( $v ) - 2 ) . "\">";
             if ( isset( $defcfg[$k]['comment'] ) ) {
@@ -353,7 +376,7 @@ foreach ( $newcfg as $k => $v ) {
 }
 
 /* Special Themes Block */
-if ( !($colapse['Group7']) ) {
+if ( $colapse['Group7'] == 'off' ) {
     $i = 0;
     echo '<tr><th>' . _("Theme Name") .
          '</th><th>' . _("Theme Path") .
@@ -362,7 +385,8 @@ if ( !($colapse['Group7']) ) {
         $k1 = "\$theme[$i]['NAME']";
         $e1 = "theme_name_$i";
         if ( isset( $HTTP_POST_VARS[$e1] ) ) {
-            $v1 = '"' . $HTTP_POST_VARS[$e1] . '"';
+            $v1 = '"' . str_replace( '\"', '"', $HTTP_POST_VARS[$e1] ) . '"';
+            $v1 = '"' . str_replace( '"', '\"', $v1 ) . '"';
             $newcfg[$k1] = $v1;
         } else {
             $v1 = $newcfg[$k1];
@@ -370,7 +394,8 @@ if ( !($colapse['Group7']) ) {
         $k2 = "\$theme[$i]['PATH']";
         $e2 = "theme_path_$i";
         if ( isset( $HTTP_POST_VARS[$e2] ) ) {
-            $v2 = '"' . $HTTP_POST_VARS[$e2] . '"';
+            $v2 = '"' . str_replace( '\"', '"', $HTTP_POST_VARS[$e2] ) . '"';
+            $v2 = '"' . str_replace( '"', '\"', $v2 ) . '"';
             $newcfg[$k2] = $v2;
         } else {
             $v2 = $newcfg[$k2];
@@ -387,7 +412,7 @@ if ( !($colapse['Group7']) ) {
 }
 
 /* Special Plugins Block */
-if ( $colapse['Group8'] ) {
+if ( $colapse['Group8'] == 'on' ) {
     $sw = '(+)';
 } else {
     $sw = '(-)';
@@ -396,7 +421,7 @@ echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
      "<a href=options.php?switch=Group8 STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
      _("Plugins") . '</th></tr>';
 
-if( !$colapse['Group8'] ) {
+if( $colapse['Group8'] == 'off' ) {
 
     $fd = opendir( '../plugins/' );
     $op_plugin = array();
@@ -416,7 +441,8 @@ if( !$colapse['Group8'] ) {
     $plugins = array();
     if ( isset( $HTTP_POST_VARS['plg'] ) ) {
         foreach ( $op_plugin as $plg ) {
-            if ( $HTTP_POST_VARS["plgs_$plg"] == 'on' ) {
+            if ( isset( $HTTP_POST_VARS["plgs_$plg"] ) &&
+                 $HTTP_POST_VARS["plgs_$plg"] == 'on' ) {
                 $plugins[] = $plg;
             }
         }
@@ -449,7 +475,7 @@ if( !$colapse['Group8'] ) {
         }
         echo '<tr>' .
              "<td>$plg</td><td><input$sw type=checkbox name=plgs_$plg></td>".
-             '</tr>';
+             "</tr>\n";
     }
     echo '</td></tr></table>';
 
@@ -462,46 +488,29 @@ echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
     Write the options to the file.
 */
 
-$fp = fopen( $cfgfile, 'w' );
-fwrite( $fp, "<?PHP\n".
-            "/**\n".
-            " * SquirrelMail Configuration File\n".
-            " * Created using the Administrator Plugin\n".
-            " */\n" );
-
-/*
-fwrite( $fp, 'GLOBAL ' );
-$not_first = FALSE;
-foreach ( $newcfg as $k => $v ) {
-    if ( $k{0} == '$' ) {
-        if( $i = strpos( $k, '[' ) ) {
-            if( strpos( $k, '[0]' ) ) {
-                if( $not_first ) {
-                    fwrite( $fp, ', ' );
-                }
-                fwrite( $fp, substr( $k, 0, $i) );
-            }
-        } else {
-            if( $not_first ) {
-                fwrite( $fp, ', ' );
+if( $fp = @fopen( $cfgfile, 'w' ) ) {
+    fwrite( $fp, "<?PHP\n".
+    "/**\n".
+    " * SquirrelMail Configuration File\n".
+    " * Created using the Administrator Plugin\n".
+    " */\n" );
+
+    foreach ( $newcfg as $k => $v ) {
+        if ( $k{0} == '$' && $v <> '' ) {
+            if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
+                $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
+                $v = str_replace( 'array(', "array(\n\t", $v );
+                $v = str_replace( "',", "',\n\t", $v );
             }
-            fwrite( $fp, $k );
+            fwrite( $fp, "$k = $v;\n" );
         }
-        $not_first = TRUE;
-    }
-}
-fwrite( $fp, ";\n" );
-*/
-foreach ( $newcfg as $k => $v ) {
-    if ( $k{0} == '$' && $v <> '' ) {
-        if ( substr( $k, 1, 11 ) == 'ldap_server' ) {
-            $v = substr( $v, 0, strlen( $v ) - 1 ) . "\n)";
-            $v = str_replace( 'array(', "array(\n\t", $v );
-            $v = str_replace( "',", "',\n\t", $v );
-        }
-        fwrite( $fp, "$k = $v;\n" );
     }
+    fwrite( $fp, '?>' );
+    fclose( $fp );
+} else {
+    echo '<font size=+1><br>'.
+         _("Config file can't be opened. Please check config.php.").
+         '</font>';
 }
-fwrite( $fp, '?>' );
-fclose( $fp );
-?>
\ No newline at end of file
+
+?>