Fix plugin list in admin plugin
[squirrelmail.git] / plugins / administrator / options.php
index 7685d9ff17b2bd6999bed191584df1ac7ef3834a..cf9cb62a13ec404952e042d13fbe237ba4c933fe 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Administrator Plugin
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * Philippe Mingo
@@ -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,38 +123,92 @@ 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 = '$';
                 }
             }
         }
     }
+}
 
+/* Change paths containing SM_PATH to admin-friendly paths
+   relative to the config dir, i.e.:
+     ''                          --> <empty string>
+     SM_PATH . 'images/logo.gif' --> ../images/logo.gif
+     '/absolute/path/logo.gif'   --> /absolute/path/logo.gif
+     'http://whatever/'          --> http://whatever
+   Note removal of quotes in returned value
+*/
+function change_to_rel_path($old_path) {
+    $new_path = str_replace("SM_PATH . '", "../", $old_path); 
+    $new_path = str_replace("../config/","", $new_path);
+    $new_path = str_replace("'","", $new_path);
+    return $new_path;
+}
+
+/* Change relative path (relative to config dir) to 
+   internal SM_PATH, i.e.:
+     empty_string            --> ''
+     ../images/logo.gif      --> SM_PATH . 'images/logo.gif'
+     images/logo.gif         --> SM_PATH . 'config/images/logo.gif'
+     /absolute/path/logo.gif --> '/absolute/path/logo.gif'
+     http://whatever/        --> 'http://whatever'
+*/     
+function change_to_sm_path($old_path) {
+   if ( $old_path === '' || $old_path == "''" ) {
+     return "''";
+   } elseif ( preg_match("/^(\/|http)/", $old_path) ) {
+     return "'" . $old_path . "'";
+   } elseif ( preg_match("/^(\$|SM_PATH)/", $old_path) ) {
+     return $old_path;
+   }
+   
+   $new_path = '';
+   $rel_path = explode("../", $old_path);
+   if ( count($rel_path) > 2 ) {
+     // Since we're relative to the config dir, 
+     // more than 1 ../ puts us OUTSIDE the SM tree.
+     // get full path to config.php, then pop the filename
+     $abs_path = explode('/', realpath (SM_PATH . 'config/config.php'));
+     array_pop ($abs_path); 
+     foreach ( $rel_path as $subdir ) {
+       if ( $subdir === '' ) {
+         array_pop ($abs_path);
+       } else {
+         array_push($abs_path, $subdir);
+       }
+     }
+     foreach ($abs_path as $subdir) {
+       $new_path .= $subdir . '/';
+     }
+     $new_path = "'$new_path'";
+   } elseif ( count($rel_path) > 1 ) {
+     // we're within the SM tree, prepend SM_PATH
+     $new_path = str_replace('../',"SM_PATH . '", $old_path . "'");
+   } else {
+     // Last, if it's a relative path without a .. prefix, 
+     // we're somewhere within the config dir, so prepend
+     //  SM_PATH . 'config/  
+     $new_path = "SM_PATH . 'config/" . $old_path . "'";
+   }
+   return $new_path;
 }
 
+
 /* ---------------------- 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');
+define('SM_PATH','../../');
 
-GLOBAL $data_dir, $username;
+/* 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');
 
-$auth = FALSE;
-if ( $adm_id = fileowner('../config/config.php') ) {
-    $adm = posix_getpwuid( $adm_id );
-    if ( $username == $adm['name'] ) {
-        $auth = TRUE;
-    }
-}
+GLOBAL $data_dir, $username;
 
-if ( !auth ) {
-    header("Location: ../../src/options.php") ;
+if ( !adm_check_user() ) {
+    header('Location: ' . SM_PATH . 'src/options.php') ;
     exit;
 }
 
@@ -160,32 +220,45 @@ foreach ( $defcfg as $key => $def ) {
     $newcfg[$key] = '';
 }
 
-$cfgfile = '../config/config.php';
-parseConfig( '../config/config_default.php' );
+$cfgfile = SM_PATH . 'config/config.php';
+parseConfig( SM_PATH . '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 ) );
-
-if ( isset( $switch ) ) {
-    $colapse[$switch] = !$colapse[$switch];
+$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( $_GET['switch'] ) ) {
+    $switch = $_GET['switch'];
+    if ( $colapse[$switch] == 'on' ) {
+       $colapse[$switch] = 'off';
+    } else {
+       $colapse[$switch] = 'on';
+    }
     setPref($data_dir, $username, "adm_$switch", $colapse[$switch] );
 }
 
-echo "<form action=$PHP_SELF 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>";
+    "<tr bgcolor=\"$color[5]\"><th colspan=2>" . _("Configuration Administrator") . "</th></tr>",
+    "<tr bgcolor=\"$color[5]\"><td colspan=2i align=\"center\">";
+?>
+<small>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.<br />
+Run or consult conf.pl should you run into difficulty with your configuration.</small>
+</td></tr>
+<?php
 
 $act_grp = 'Titles';  /* Active group */
-    
+
 foreach ( $newcfg as $k => $v ) {
     $l = strtolower( $v );
     $type = SMOPT_TYPE_UNDEFINED;
@@ -198,7 +271,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;
@@ -219,7 +296,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:
@@ -227,8 +304,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 = '(-)';
@@ -300,7 +382,7 @@ foreach ( $newcfg as $k => $v ) {
             }
             echo "</td></tr>\n";
             break;
-    
+
         case SMOPT_TYPE_TEXTAREA:
             if ( isset( $HTTP_POST_VARS[$e] ) ) {
                 $v = '"' . $HTTP_POST_VARS[$e] . '"';
@@ -318,6 +400,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'] ) ) {
@@ -347,6 +433,22 @@ foreach ( $newcfg as $k => $v ) {
             }
             echo "</td></tr>\n";
             break;
+       case SMOPT_TYPE_PATH:
+           if ( isset( $HTTP_POST_VARS[$e] ) ) {
+               $v = change_to_sm_path($HTTP_POST_VARS[$e]);
+               $newcfg[$k] = $v;
+            }
+            if ( $v == "''" && isset( $defcfg[$k]['default'] ) ) {
+               $v = change_to_sm_path($defcfg[$k]['default']);
+               $newcfg[$k] = $v;
+            }
+           echo "<tr><td>$name</td><td>".
+                 "<input size=\"$size\" name=\"adm_$n\" value=\"" . change_to_rel_path($v) . "\">";
+            if ( isset( $defcfg[$k]['comment'] ) ) {
+                 echo ' &nbsp; ' . $defcfg[$k]['comment'];
+            }
+           echo "</td></tr>\n";
+           break;
         default:
             echo "<tr><td>$name</td><td>" .
                  "<b><i>$v</i></b>";
@@ -356,10 +458,10 @@ foreach ( $newcfg as $k => $v ) {
             echo "</td></tr>\n";
         }
     }
-
 }
 
-if ( !($colapse['Group7']) ) {
+/* Special Themes Block */
+if ( $colapse['Group7'] == 'off' ) {
     $i = 0;
     echo '<tr><th>' . _("Theme Name") .
          '</th><th>' . _("Theme Path") .
@@ -368,7 +470,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];
@@ -376,72 +479,92 @@ if ( !($colapse['Group7']) ) {
         $k2 = "\$theme[$i]['PATH']";
         $e2 = "theme_path_$i";
         if ( isset( $HTTP_POST_VARS[$e2] ) ) {
-            $v2 = '"' . $HTTP_POST_VARS[$e2] . '"';
-            $newcfg[$k2] = $v2;
+            $v2 = change_to_sm_path($HTTP_POST_VARS[$e2]);
+           $newcfg[$k2] = $v2;
         } else {
             $v2 = $newcfg[$k2];
         }
         $name = substr( $v1, 1, strlen( $v1 ) - 2 );
-        $path = substr( $v2, 1, strlen( $v2 ) - 2 );
+        $path = change_to_rel_path($v2);
         echo '<tr>'.
              "<td align=right>$i. <input name=\"$e1\" value=\"$name\" size=30></td>".
              "<td><input name=\"$e2\" value=\"$path\" size=40></td>".
              "</tr>\n";
         $i++;
-    
+
     }
 }
 
-if ( $colapse['Group8'] ) {
+/* Special Plugins Block */
+if ( $colapse['Group8'] == 'on' ) {
     $sw = '(+)';
 } else {
     $sw = '(-)';
 }
 echo "<tr bgcolor=\"$color[0]\"><th colspan=2>" .
-     "<a href=options.php?switch=Group7 STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
+     "<a href=options.php?switch=Group8 STYLE=\"text-decoration:none\"><b>$sw</b> </a>" .
      _("Plugins") . '</th></tr>';
 
-if( !$colapse['Group7'] ) {
+if( $colapse['Group8'] == 'off' ) {
 
-    $fd = opendir( '../plugins/' );
-    $op_plugin = array();
-    while (false!==($file = readdir($fd))) {
-        if ($file != '.' && $file != '..' && $file != 'CVS' ) {
-            if ( filetype( $file ) == 'dir' ) {
-                $op_plugin[] = $file;
+  $plugpath = SM_PATH . 'plugins/';
+  if ( file_exists($plugpath) ) {
+      $fd = opendir( $plugpath );
+      $op_plugin = array();
+      $p_count = 0;
+      while (false !== ($file = readdir($fd))) {
+        if ($file != '.' && $file != '..' && $file != 'CVS' && is_dir($plugpath . $file) ) {
+            $op_plugin[] = $file;
+            $p_count++;
+        }
+      }
+      closedir($fd);
+      asort( $op_plugin );
+
+      /* Lets get the plugins that are active */
+      $plugins = array();
+      if ( isset( $HTTP_POST_VARS['plg'] ) ) {
+        foreach ( $op_plugin as $plg ) {
+            if ( isset( $HTTP_POST_VARS["plgs_$plg"] ) &&
+                 $HTTP_POST_VARS["plgs_$plg"] == 'on' ) {
+                $plugins[] = $plg;
             }
         }
-    }
-    closedir($fd);
-    asort( $op_plugin );
-    
-    $i = 0;
-    while ( isset( $newcfg["\$plugins[$i]"] ) ) {
-        $k = "\$plugins[$i]";
-        $e = "plugin_$i";
-        if ( isset( $HTTP_POST_VARS[$e] ) ) {
-            $v = '"' . $HTTP_POST_VARS[$e] . '"';
-            $newcfg[$k] = $v;
-        } else {
+        $i = 0;
+        foreach ( $plugins as $plg ) {
+            $k = "\$plugins[$i]";
+            $newcfg[$k] = "'$plg'";
+            $i++;
+        }
+        while ( isset( $newcfg["\$plugins[$i]"] ) ) {
+            $k = "\$plugins[$i]";
+            $newcfg[$k] = '';
+            $i++;
+        }
+      } else {
+        $i = 0;
+        while ( isset( $newcfg["\$plugins[$i]"] ) ) {
+            $k = "\$plugins[$i]";
             $v = $newcfg[$k];
+            $plugins[] = substr( $v, 1, strlen( $v ) - 2 );
+            $i++;
         }
-        $name = substr( $v, 1, strlen( $v ) - 2 );
-        echo '<tr>'.
-             "<td align=right>$i.</td>".
-             "<td><select name=\"$e\">";
-        foreach ( $op_plugin as $op ) {
-            if ( $op == $name ) {
-                $cs = ' selected';
-            } else {
-                $cs = '';
-            }
-            echo "<option$cs>$op</option>";
+      }
+      echo "<tr><td colspan=2><input type=hidden name=plg value=on><center><table><tr><td>";
+      foreach ( $op_plugin as $plg ) {
+        if ( in_array( $plg, $plugins ) ) {
+            $sw = ' checked';
+        } else {
+            $sw = '';
         }
-        echo "</select></td>".
-             '</tr>';
-        $i++;
-    
-    }
+        echo '<tr>' .
+             "<td>$plg</td><td><input$sw type=checkbox name=plgs_$plg></td>".
+             "</tr>\n";
+      }
+      echo '</td></tr></table></td></tr>';
+  } else {
+      echo '<tr><td colspan=2 align="center">Plugin directory could not be found: ' . $plugpath . "</td></tr>\n";
+  }
 }
 echo "<tr bgcolor=\"$color[5]\"><th colspan=2><input value=\"" .
      _("Change Settings") . "\" type=submit></th></tr>" ,
@@ -451,46 +574,28 @@ 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" );
+if( $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, ', ' );
+    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} == '$' ) {
-        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
+?>