<?
////////////////////////////////////////////////////////////////////////
/*SMI - SHOUTcast Management Interface
A web based shoutcast server management program
Founding Author: Scott D. Harvanek <scotth@sourcemirrors.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
////////////////////////////////////////////////////////////////////////
require('header.php');

// Process changes if we are called by form
if (isset($_POST['configsub'])) {
  unset($_POST['configsub']);
  updatesettings($_POST);
?>
  <script type="text/javascript">
    alert("Configuration updated!"); 
    window.location="setup.php?group=<? echo $_POST['category']; ?>";
  </script>
<?
  exit;
}
// Use first settings group as default
if (!isset($_REQUEST['group'])) { $_REQUEST['group'] = 1; }

// Build menu of setting groups
$menu = "|&nbsp;&nbsp;";
foreach (confgroups() as $group) {
  if ($group['id'] == $_REQUEST['group']) {
    $activegroup = $group; 
    $menu .= "<strong>".$group['name']."</strong>";
  } else { 
    $menu .= "<a class=\"headbarlink\" href=\"".$_SERVER['PHP_SELF']."?group=".$group['id']."\">".$group['name']."</a>"; 
  }
  $menu .= "&nbsp;&nbsp;|&nbsp;&nbsp;"; 
}
?>
<table border="0" width="100%">
  <tr>
    <td valign="top">
      <table border="0" width="600px">
        <tr>
          <td align="left">
            <b>Administration - <? echo $activegroup['title']; ?></b><br>
          </td>
        </tr>
        <tr>
          <td class="headbarfont">
            <? echo $menu; ?>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<?
// Store fields from submit
?>
<form action="" method="post" enctype="multipart/form-data">
<table border = "0">
<?

/*
   	Preprocess dynamic update of multiple choices
*/

$config = settings();
$dnas = array();

// Find available SHOUTcast DNAS binaries
$path = $config['smi_path']."/shoutcast";
if ($mainhandle = opendir($path)) {
  // Read contents of 'shoutcast/' directory
  while ($dir=readdir($mainhandle)) {
    // Do not read directories '.', '..', '.svn', etc.
    if (is_dir($path.'/'.$dir) && substr($dir, 0, 1) != ".") {
      // Open the directories we found under 'shoutcast/'
      if ($localhandle = opendir($path.'/'.$dir)) {
        // Check the names of contents that are files, add sc_serv
        while ($file=readdir($localhandle)) {
          if (is_file($path.'/'.$dir.'/'.$file) 
	      && (strtoupper($file) == "SC_SERV" || strtoupper($file) == "SC_SERV.EXE")) {
	    // If it's an sc_serv binary, add it to the list of DNAS'es
	    $dnas[$dir] = $path."/".$dir."/".$file;
          }
        }
      }
    }
  }
}

// Find the record id for the 'sc_serv' setting
$db = dbConnect();
$row = $db->getRow('config', array('setting' => 'sc_serv'), 'id');
$configid = $row[0];

// Replace any existing entries with the DNAS binary paths found
$db->delete('config_sets', array('configid' => $configid));
foreach ($dnas as $key => $value) {
  $db->insert('config_sets', array( 'configid' 	=> $configid, 
				    'value' 	=> $value,
				    'caption'   => $key )
             );
}
  

/*
        Proceed to build settings form
*/

// Fetch settings for selected group (if any)
$settings = groupsettings($_REQUEST['group']);
if (isset($settings)) {
  foreach ($settings as $setting) {
    echo "<tr>\n<td>".$setting['title']."</td><td>&nbsp;:</td>\n";
    // Check to see if this is a multiple choice setting
    $options = settingoptions($setting['id']);
    if (isset($options)) {
      echo "<td><select name=\"".$setting['setting']."\">\n";
      foreach ($options as $option) {
        echo "<option value=\"".$option['value']."\"";
        if ($option['value'] == $setting['value']) { 
          echo " selected>"; 
        } else { 
          echo ">"; 
        }
        echo $option['caption']."</option>\n";
      }
      echo "</select></td></tr>\n";
    } else { 
      if (strlen($setting['value']) < 8) { 
        $fieldsize = "10"; 
      } else {
        $fieldsize = "50";
      }
      echo "<td><input type=\"text\" name=\"".$setting['setting']."\" 
            value=\"".$setting['value']."\" size=\"".$fieldsize."\"></td>\n</tr>\n";
    }
  }
  echo "<input type=\"hidden\" name=\"category\" value=\"".$_REQUEST['group']."\">";
  $lastline = "<input type=\"submit\" name=\"configsub\" value=\"Submit changes\">";
} else {
  $lastline = $activegroup['title']." category is empty.";
}
?>
  <tr>
    <td><br>&nbsp;</td><td>&nbsp;</td>
    <td><? echo $lastline; ?></td>
  </tr>
</table>
</form>
<?
require('footer.php');
?>
