<?
////////////////////////////////////////////////////////////////////////
/*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.*/
////////////////////////////////////////////////////////////////////////
if(useraccess($_SESSION['username']) < "4") {
  echo "ACCESS DENIED - INCIDENT REPORTED";
  $event = " Attempted to access the restricted edit server section.";
  addevent($_SESSION['username'], $event);
} else {
  $username = $_SESSION['username'];
  $config = settings();
  $db = dbConnect();
  
  /****************************
    CALLED BY FORM SUBMIT
  ****************************/
  if(isset($_POST['subedit'])) {
    if (portexists($_POST['portbase']) && ($_POST['oldport'] != $_POST['portbase'])) {
      $backurl = "edit.php?portbase=".$_POST['oldport']."&id=".$_POST['id']."&status=0";
      echo "A server with port ".$_POST['portbase']." exists already - no changes was made. Please <a href=\"".$backurl."\">try again</a> with a different port.";
      exit;
    } elseif ($_POST['portbase'] < $config['start_portbase']) {
      echo "Server port number must be ".$config['start_portbase']." or higher. Please <a href=\"".$backurl."\">try again</a> with a different port.";
      exit;
    } // end of if port exists //

    $status = $_POST['status'];
    $oldport = $_POST['oldport'];
    $oldname = $_POST['oldname'];
    $id = $_POST['id'];
    deletefile($config['smi_path']."/servers/".$oldport.$oldname.".conf");
    $srvname = $_POST['srvname'];
    $portbase = $_POST['portbase'];
    $adminpassword = $_POST['adminpassword'];
    $djpassword = $_POST['djpassword'];
    $maxuser = $_POST['maxuser'];
    $bitrate = $_POST['bitrate'];
    $realtime = $_POST['realtime'];
    $screenlog = $_POST['screenlog'];
    $showlastsongs = $_POST['showlastsongs'];
    $w3cenable = $_POST['w3cenable'];
    $srcip = $_POST['srcip'];
    $dstip = $_POST['dstip'];
    $yport = $_POST['yport'];
    $namelookup = $_POST['namelookup'];
    
    // Set relay to field values or '0'
    if($_POST['isrelay']) {
      $relayport = $_POST['relayport'];
      $relayhost = $_POST['relayhost'];
    } else {
      $relayport = '0';
      $relayhost = '0';
    } // end if isrelay

    $autodump = $_POST['autodump'];
    $autodumptime = $_POST['autodumptime'];
    $publicsrv = $_POST['publicsrv'];
    $allowrelay = $_POST['allowrelay'];
    $allowpubrelay = $_POST['allowpubrelay'];
    $metainterval = $_POST['metainterval'];
    $genre = $_POST['genre'];
    $website = $_POST['website'];

    // Check if privilege is high enough
    if(useraccess($_SESSION['username']) >= "4") {	  
      // Start by stopping the stream
      stopstream($id);
      deletefile('ices/'.$id.'.'.$oldport.'.conf');
      
      if(isset($_POST['autodj'])) {
        $autodj = "1";
        $random = $_POST['random'];
        $playlist = $_POST['playlist'];
     
        $db->query('UPDATE servers SET AdminPassword="'.$adminpassword.'",servername="'.$srvname.'",created="'.$username.'",MaxUser="'.$maxuser.'",
        Password="'.$djpassword.'",PortBase="'.$portbase.'",RealTime="'.$realtime.'",ScreenLog="'.$screenlog.'",ShowLastSongs="'.$showlastsongs.'",
        W3CEnable="'.$w3cenable.'",SrcIP="'.$srcip.'",DestIP="'.$dstip.'",Yport="'.$yport.'",NameLookups="'.$namelookup.'",RelayPort="'.$relayport.'", 
        RelayServer="'.$relayhost.'",AutoDumpUsers="'.$autodump.'", AutoDumpSourceTime="'.$autodumptime.'",PublicServer="'.$publicsrv.'",
        AllowRelay="'.$allowrelay.'",AllowPublicRelay="'.$allowpubrelay.'",MetaInterval="'.$metainterval.'",
        autodj="'.$autodj.'",Playlist="'.$playlist.'",genre="'.$genre.'",website="'.$website.'",bitrate="'.$bitrate.'" WHERE id="'.$id.'"');
        
        genIces0Conf($id, $portbase, $playlist, $random);
        genicespls($id,$random);
        reloadstream($id);
      } else {

$db->query('UPDATE servers SET AdminPassword="'.$adminpassword.'",servername="'.$srvname.'",created="'.$username.'",MaxUser="'.$maxuser.'",
Password="'.$djpassword.'",PortBase="'.$portbase.'",RealTime="'.$realtime.'",ScreenLog="'.$screenlog.'",ShowLastSongs="'.$showlastsongs.'",W3CEnable="'.$w3cenable.'",
SrcIP="'.$srcip.'",DestIP="'.$dstip.'",Yport="'.$yport.'",NameLookups="'.$namelookup.'",RelayPort="'.$relayport.'",RelayServer="'.$relayhost.'",AutoDumpUsers="'.$autodump.'",
AutoDumpSourceTime="'.$autodumptime.'",PublicServer="'.$publicsrv.'",AllowRelay="'.$allowrelay.'",AllowPublicRelay="'.$allowpubrelay.'",
MetaInterval="'.$metainterval.'",bitrate="'.$bitrate.'" WHERE id="'.$id.'"');
      }

    } else {
      if(getuid($_SESSION['username']) == getowner_byid($id)) {
        if(isset($_POST['autodj'])) {
          $autodj = "1";
	  $playlist = $_POST['playlist'];
	  $random = $_POST['random'];
			
$db->query('UPDATE servers SET AdminPassword="'.$adminpassword.'" AND servername="'.$srvname.'" AND Password="'.$djpassword.'" AND RealTime="'.$realtime.'"
AND ScreenLog="'.$screenlog.'" AND ShowLastSongs="'.$showlastsongs.'" AND W3CEnable="'.$w3cenable.'" AND SrcIP="'.$srcip.'" AND DestIP="'.$dstip.'" AND
Yport="'.$yport.'" AND NameLookups="'.$namelookup.'" AND RelayPort="'.$relayport.'" AND RelayServer="'.$relayserver.'" AND AutoDumpUsers="'.$autodump.'"
AND AutoDumpSourceTime="'.$autodumptime.'" AND PublicServer="'.$publicsrv.'" AND AllowRelay="'.$allowrelay.'" AND AllowPublicRelay="'.$allowpubrelay.'" AND
MetaInterval="'.$metainterval.'" AND autodj="'.$autodj.'" AND Playlist="'.$playlist.'" AND genre="'.$genre.'" AND website="'.$website.'" AND bitrate="'.$bitrate.'"
 WHERE id="'.$id.'"');
			genIces0Conf($id, $portbase, $playlist, $random);
			genicespls($id,$random);
			reloadstream($this_id);
	  } else {
$db->query('UPDATE servers SET AdminPassword="'.$adminpassword.'" AND servername="'.$srvname.'" AND Password="'.$djpassword.'" AND RealTime="'.$realtime.'"
AND ScreenLog="'.$screenlog.'" AND ShowLastSongs="'.$showlastsongs.'" AND W3CEnable="'.$w3cenable.'" AND SrcIP="'.$srcip.'" AND DestIP="'.$dstip.'" AND
Yport="'.$yport.'" AND NameLookups="'.$namelookup.'" AND RelayPort="'.$relayport.'" AND RelayServer="'.$relayserver.'" AND AutoDumpUsers="'.$autodump.'"
AND AutoDumpSourceTime="'.$autodumptime.'" AND PublicServer="'.$publicsrv.'" AND AllowRelay="'.$allowrelay.'" AND AllowPublicRelay="'.$allowpubrelay.'" AND
MetaInterval="'.$metainterval.'" AND bitrate="'.$bitrate.'" WHERE id="'.$id.'"');
	 }
      } else {
      echo "DENIED - INCIDENT REPORTED!";
      $event = " Attempted to modify a server they don't own";
      addevent($_SESSION['username'], $event);
    }
  }
  $event = " Edited server on port $portbase";
  addevent($username,$event);

////////////////////////////////////////////////
//
// We keep the relay and non relay configs seperate
// This way if we have more granularity between the two
//
////////////////////////////////////////////////
  if($relayport == "0") {
 	$header = ";\n"
        . "; $srvname Configuration file\n"
        . "; Auto-generated by SMI\n"
        . ";\n"
	. "AdminPassword=$adminpassword\n"
  . "MaxUser=$maxuser\n"
  . "Password=$djpassword\n"
  . "PortBase=$portbase\n"
  . "RealTime=$realtime\n"
  . "ScreenLog=$screenlog\n"
  . "ShowLastSongs=$showlastsongs\n"
  . "W3CEnable=$w3cenable\n"
  . "SrcIP=$srcip\n"
  . "DestIP=$dstip\n"
  . "Yport=$yport\n"
  . "NameLookups=$namelookup\n"
	. "AutoDumpUsers=$autodump\n"
	. "AutoDumpSourceTime=$autodumptime\n"
	. "PublicServer=$publicsrv\n"
	. "AllowRelay=$allowrelay\n"
	. "AllowPublicRelay=$allowpubrelay\n"
	. "BanFile=".$config['smi_path']."/logs/$portbase$srvname.ban\n"
	. "LogFile=".$config['smi_path']."/logs/$portbase$srvname.log\n"
  . "W3CLog=".$config['smi_path']."/logs/$portbase$srvname.w3c.log\n"
	. "MetaInterval=$metainterval\n";
    $fd = fopen($config['smi_path']."/servers/$portbase$srvname.conf", "w+");
    fputs($fd, $header . "\n\n");
    fclose($fd);
  } else {
    $header = ";\n"
    . "; $srvname Configuration file\n"
    . "; Auto-generated by SMI\n"
    . ";\n"
	  . "AdminPassword=$adminpassword\n"
    . "MaxUser=$maxuser\n"
    . "Password=$djpassword\n"
    . "PortBase=$portbase\n"
    . "RealTime=$realtime\n"
    . "ScreenLog=$screenlog\n"
    . "ShowLastSongs=$showlastsongs\n"
    . "W3CEnable=$w3cenable\n"
    . "SrcIP=$srcip\n"
    . "DestIP=$dstip\n"
    . "Yport=$yport\n"
    . "NameLookups=$namelookup\n"
	  . "RelayPort=$relayport\n"
    . "RelayServer=$relayhost\n"
    . "AutoDumpUsers=$autodump\n"
    . "AutoDumpSourceTime=$autodumptime\n"
    . "PublicServer=$publicsrv\n"
    . "AllowRelay=$allowrelay\n"
    . "AllowPublicRelay=$allowpubrelay\n"
	  . "BanFile=".$config['smi_path']."/logs/$portbase$srvname.ban\n"
	  . "LogFile=".$config['smi_path']."/logs/$portbase$srvname.log\n"
    . "W3CLog=".$config['smi_path']."/logs/$portbase$srvname.w3c.log\n"
    . "MetaInterval=$metainterval\n";
    $fd = fopen($config['smi_path']."/servers/$portbase$srvname.conf", "w+");
    fputs($fd, $header . "\n\n");
    fclose($fd);
  } // end if/else relayport = 0


////////////////////////////////////////////////
//
// If we've changed our server name we need to remove old files
//
////////////////////////////////////////////////
if($srvname != $oldname) {
  deletefile("".$config['smi_path']."/logs/".$oldport."".$oldname.".log");
  deletefile("".$config['smi_path']."/logs/".$oldport."".$oldname.".w3c.log");
} // end if srvname != oldname


////////////////////////////////////////////////
//
// If we've changed ports then we need to remove all old files
//
////////////////////////////////////////////////
if($oldport != $portbase) {
  deletefile("".$config['smi_path']."/logs/".$oldport."".$oldname.".log");
  deletefile("".$config['smi_path']."/logs/".$oldport."".$oldname.".w3c.log");

  // Rename MRTG stats and graphs, regenerate mrtg.cfg
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id.".log", "".$config['smi_path']."/mrtg/".$portbase."".$id.".log");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id.".old", "".$config['smi_path']."/mrtg/".$portbase."".$id.".old");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id.".html", "".$config['smi_path']."/mrtg/".$portbase."".$id.".html");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id."-day.png", "".$config['smi_path']."/mrtg/".$portbase."".$id."-day.png");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id."-week.png", "".$config['smi_path']."/mrtg/".$portbase."".$id."-week.png");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id."-month.png", "".$config['smi_path']."/mrtg/".$portbase."".$id."-month.png");
  renamefile("".$config['smi_path']."/mrtg/".$oldport."".$id."-year.png", "".$config['smi_path']."/mrtg/".$portbase."".$id."-year.png");
  renamefile("".$config['smi_path']."/logs/".$oldport."".$srvname.".log", "".$config['smi_path']."/mrtg/".$portbase."".$id.".log"); 
  renamefile("".$config['smi_path']."/logs/".$oldport."".$srvname.".w3c.log", "".$config['smi_path']."/mrtg/".$portbase."".$id.".w3c.log");
  generatemrtg();
} // end if oldport != portbase



////////////////////////////////////////////////
//
//  If our server is running we need to commit changes
// This execution is less than a second on my running machine so end users
// shouldn't even notice ;)
//
////////////////////////////////////////////////
  if($status == "1") {
    startstream($id);
  }
  echo "<b>Server $srvname edited<br><br></b>Click <a href=\"home.php\">here</a> to return to the front page. ";


  } else {


////////////////////////////////////////////////
//
// If we haven't sent a POST then we display our form
//
////////////////////////////////////////////////
$rows = $db->getRows('servers', array('PortBase' => $_REQUEST['portbase'], 'id' => $_REQUEST['id']));
foreach ($rows as $row) {
// Show creation form
?>
<b>New Server</b>
<form method="post" action="">
<table border="0">
	<tr>
                <td align="left" valign="top">
                Server Name:
                </td>
                <td align="left" valign="top">
                <input type="text" name="srvname" value="<?echo $row[34];?>">
		<input type="hidden" name="oldname" value="<?echo $row[34];?>">
                </td>
        </tr>
	<tr>
		<td align="left" valign="top">
		Port: 
		</td>
		<td align="left" valign="top">
		<?
		if(useraccess($_SESSION['username']) >= "4"){
		?><input type="text" name="portbase" value="<?echo $row[4];?>"><?
		}else{
		echo $row[4];}?>
		</td>
	</tr>
	<tr>
		<td align="left" valign="top">
		Admin Password: 
		</td>
		<td align="left" valign="top">
		<input type="text" name="adminpassword" value="<?echo $row[16];?>">
		</td>
	</tr>	
	<tr>
		<td align="left" valign="top">
		DJ Password: 
		</td>
		<td align="left" valign="top">
		<input type="text" name="djpassword" value="<?echo $row[3];?>">
		</td>
	</tr>
	<tr>
                <td align="left" valign="top">
                MaxUsers:
                </td>
                <td align="left" valign="top">
		<?if(useraccess($_SESSION['username']) >= "4"){?>
                <input type="text" name="maxuser" value="<?echo $row[2];?>">
		<?}else{
                		echo $row[2];}?>
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Max Bitrate:
                </td>
                <td align="left" valign="top">
                <?if(useraccess($_SESSION['username']) >= "4"){?>
                <input type="text" name="bitrate" value="<?echo $row[45];?>">
                <?}else{
                                echo $row[45];}?>
                </td>
        </tr>

        <tr>
                <td align="left" valign="top">
                RealTime:
                </td>
                <td align="left" valign="top">
		<?if($row[6] == "0"){?>
		<select name="realtime">
                <option value="1">Yes</option>
                <option value="0" SELECTED>No</option>
                </select>
		<?}else{?>
		<select name="realtime">
                <option value="1">Yes</option>
                <option value="0">No</option>
                </select>
		<?}?>
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Screen Log:
                </td>
                <td align="left" valign="top">
		<?if($row[7] == "0"){?>
                <select name="screenlog">
                <option value="1">Yes</option>
                <option value="0" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="screenlog">
                <option value="1">Yes</option>
                <option value="0">No</option>
                </select>
                <?}?>

                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Show Last Songs:
                </td>
                <td align="left" valign="top">
                <input type="text" name="showlastsongs" value="<?echo $row[33];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                W3CEnable:
                </td>
                <td align="left" valign="top">
                <?if($row[8] == "no"){?>
                <select name="w3cenable">
                <option value="yes">Yes</option>
                <option value="no" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="w3cenable">
                <option value="yes">Yes</option>
                <option value="no">No</option>
                </select>
                <?}?>

		</td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Source IP:
                </td>
                <td align="left" valign="top">
                <input type="text" name="srcip" value="<?echo $row[10];?>">
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Destination IP:
                </td>
                <td align="left" valign="top">
                <input type="text" name="dstip" value="<?echo $row[11];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Yport:
                </td>
                <td align="left" valign="top">
                <input type="text" name="yport" value="<?echo $row[12];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Name Lookups:
                </td>
                <td align="left" valign="top">
		<?if($row[13] == "0"){?>
                <select name="namelookup">
                <option value="1">Yes</option>
                <option value="0" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="namelookup">
                <option value="1">Yes</option>
                <option value="0">No</option>
                </select>
                <?}?>

                </td>
	</tr>
	<tr>
                <td align="left" valign="top">
                Relay Server?:
                </td>
                <td align="left" valign="top">
		<?if($row[14] == "0"){?>
                <input type="checkbox" name="isrelay">
		<?}else{?>
		<input type="checkbox" name="isrelay" checked>
		<?}?>
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Relay Port:
                </td>
                <td align="left" valign="top">
                <input type="text" name="relayport" value="<?echo $row[14];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Relay Server:
                </td>
                <td align="left" valign="top">
                <input type="text" name="relayhost" value="<?echo $row[15];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Auto Dump Users:
                </td>
                <td align="left" valign="top">
		<?if($row[17] == "0"){?>
                <select name="autodump">
                <option value="1">Yes</option>
                <option value="0" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="autodump">
                <option value="1">Yes</option>
                <option value="0">No</option>
                </select>
                <?}?>

                </td>
        </tr><tr>
                <td align="left" valign="top">
                Auto Dump Source Time:
                </td>
                <td align="left" valign="top">
                <input type="text" name="autodumptime" value="<?echo $row[18];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Public Server:
                </td>
                <td align="left" valign="top">
                <input type="text" name="publicsrv" value="<?echo $row[21];?>">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Allow Relay:
                </td>
                <td align="left" valign="top">
		<?if($row[22] == "No"){?>
                <select name="allowrelay">
                <option value="Yes">Yes</option>
                <option value="No" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="allowrelay">
                <option value="Yes">Yes</option>
                <option value="No">No</option>
                </select>
                <?}?>

                </td>
        </tr><tr>
                <td align="left" valign="top">
                Allow Public Relay:
                </td>
                <td align="left" valign="top">
		<?if($row[23] == "No"){?>
                <select name="allowpubrelay">
                <option value="Yes">Yes</option>
                <option value="No" SELECTED>No</option>
                </select>
                <?}else{?>
                <select name="allowpubrelay">
                <option value="Yes">Yes</option>
                <option value="No">No</option>
                </select>
                <?}?>
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Meta Interval:
                </td>
                <td align="left" valign="top">
                <input type="text" name="metainterval" value="<?echo $row[24];?>">
                </td>
        </tr>
	<tr>
		<td align="left" valign="top">
		AutoDJ:
		</td>		
		<td align="left" valign="top">
		<input type="checkbox" name="autodj"<?if($row[40] == "1"){echo "CHECKED";}?>>Yes</td>
	</tr>
	<tr>
		<td align="left" valign="top">
                Playlist:
                </td>
                <td align="left" valign="top">
		<select name="playlist">
                        <? $plsq = $db->getRows('playlist');
                                foreach ((array)$plsq as $pls) {
                                        if($pls[0] == $row[41]){
                                        echo "<option value=\"".$pls[0]."\" SELECTED>".$pls[3]."</option>";
                                        }else{
                                        echo "<option value=\"".$pls[0]."\">".$pls[3]."</option>";
                                                }
                        }



?>
		</select>
		</td>
	</tr>
	<tr>
		<td align="left" valign="top">
		Randomize Playlist:
		</td><td>
		<select name="random">
		<option value="0">No</option>
		<option value="1">Yes</option>
		</select>
		</td>
	</tr><tr>
		<td align="left" valign="top">
		Genre:
		</td><td>
		<input type="text" name="genre" value="<?echo $row[43];?>">
		</td>
	</tr><tr>
		<td align="left" valign="top">
		Website:
		</td><td>
		<input type="text" name="website" value="<?echo $row[44];?>">
		</td>
	</tr><tr>
		<td align="left">
		<?if($_REQUEST['status'] == "1"){?>
		<input type="hidden" name="status" value="1">
		<input type="submit" name="subedit" value="Commit Changes">
		<?}else{?>
		<input type="submit" name="subedit" value="Commit Changes">
		<?}?>
		<input type="hidden" name="oldport" value="<? echo $_REQUEST['portbase'];?>">
		<input type="hidden" name="id" value="<?echo $row[0];?>">
</table>
</form>

<?
  }
 }
} // end if/else useraccess < 4
?>
