<?
////////////////////////////////////////////////////////////////////////
/*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 new server section.";
 addevent($_SESSION['username'], $event);
} else {
$config = settings();
$username = $_SESSION['username'];
if(isset($_POST['subnew'])) {
 if (portexists($_POST['portbase'])) {
   echo "A server with port ".$_POST['portbase']." exists already. Please <a href=\"new.php\">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=\"new.php\">try again</a> with a different port.";
   exit;
 }
 // New servers are by default "not enabled"
 $enabled = 0;

 // Fetch values from form
 $srvname = $_POST['srvname'];
 $srvname = preg_replace('/\s/', '_', $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'];

 // If form's relayport is not set, assign '0' as values to relay port & host
 if(isset($_POST['isrelay'])){
   $relayport = $_POST['relayport'];
   $relayhost = $_POST['relayhost'];
 } else {
   $relayport = '0';
   $relayhost = '0';
 }
 $uid = $_POST['uid'];
 $autodump = $_POST['autodump'];
 $autodumptime = $_POST['autodumptime'];
 $publicsrv = $_POST['publicsrv'];
 $allowrelay = $_POST['allowrelay'];
 $allowpubrelay = $_POST['allowpubrelay'];
 $metainterval = $_POST['metainterval'];
 $genre = $_POST['genre'];
 $website = $_POST['website'];
 
 // Define common insert fields and values
 $insert = array(	'owner' 	=>	$uid,
			'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' =>	$allowpublicrelay, 
			'MetaInterval' 	=>	$metainterval,
			'bitrate'	=>	$bitrate );

// Extra work if AutoDJ is checked  	
if(isset($_POST['autodj'])) {

    // Assign values to extra fields
    $autodj = "1";
    $playlist = $_POST['playlist'];
    $random = $_POST['random'];

    // Define extra fields and values
    $extras = array ( 	'autodj' 	=> 	$autodj,
			'Playlist'	=>	$playlist,
			'random'	=>	$random,
			'genre'		=>	$genre,
			'website'	=>	$website );

    // Merge extras into common insert
    array_merge($insert, $extras);
}

// Do the actual insert
$db = dbConnect();
$id = $db->insert('servers', $insert);  

// If AutoDJ is set, generate IceCast config file and playlist
if ($autodj == "1") {
  genIces0Conf($id, $portbase, $playlist, $random);
  genicespls($id, $random);
}

// Create event in event log
$event = " created a new server with id ".$id." on port ".$portbase;
addevent($username,$event);

// Run MRTG update
generatemrtg();

// Set up sc_serv config file for this server
$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";
if ($relayport > 0) {
  $header .= "RelayPort=$relayport\n"
          . "RelayServer=$relayhost\n";
}
        $header .= "AutoDumpUsers=$autodump\n"
        . "AutoDumpSourceTime=$autodumptime\n"
        . "PublicServer=$publicsrv\n"
        . "AllowRelay=$allowrelay\n"
        . "AllowPublicRelay=$allowpubrelay\n"
	. "BanFile=".$conf['smi_path']."/logs/$portbase$srvname.ban\n"
	. "LogFile=".$conf['smi_path']."/logs/$portbase$srvname.log\n"
	. "W3CLog=".$conf['smi_path']."/logs/$portbase$srvname.w3c.log\n"
        . "MetaInterval=$metainterval\n";

// Save the configuration file
$fd = fopen($config['smi_path']."/servers/".$portbase."".$srvname.".conf", "w+");
fputs($fd, $header . "\n\n");
fclose($fd);

// If enabled start on creation, start the server
if ($config['auto_start'] == "on") {
  startstream($id);
}

// Spread the good news
echo "<b>New server '$srvname' created!<br><br></b>Click <a href=\"home.php\">here</a> to return to the front page. ";

// If enabled start on creation, start the server


} else {

// We were not called by form submit, so show the form
$db = dbConnect();
// 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="SERVER">
                </td>
        </tr>
	<tr>
		<td align="left" valign="top">
		Port: 
		</td>
		<td align="left" valign="top">
		<input type="text" name="portbase" value="<? echo nextport(); ?>">
		</td>
	</tr>
	<tr>
		<td align="left" valign="top">
		Admin Password: 
		</td>
		<td align="left" valign="top">
		<input type="text" name="adminpassword" value="changeme">
		</td>
	</tr>	
	<tr>
		<td align="left" valign="top">
		DJ Password: 
		</td>
		<td align="left" valign="top">
		<input type="text" name="djpassword" value="changeme">
		</td>
	</tr>
	<tr>
                <td align="left" valign="top">
                MaxUsers:
                </td>
                <td align="left" valign="top">
                <input type="text" name="maxuser" value="32">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Max Bitrate:
                </td>
                <td align="left" valign="top">
                <input type="text" name="bitrate" value="128">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                RealTime:
                </td>
                <td align="left" valign="top">
		<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">
		<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="10">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                W3CEnable:
                </td>
                <td align="left" valign="top">
                <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="ANY">
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Destination IP:
                </td>
                <td align="left" valign="top">
                <input type="text" name="dstip" value="ANY">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Yport:
                </td>
                <td align="left" valign="top">
                <input type="text" name="yport" value="80">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Name Lookups:
                </td>
                <td align="left" valign="top">
		<select name="namelookup">
                <option value="0">No</option>
                <option value="1">Yes</option>
                </select>
                </td>
	</tr>
	<tr>
                <td align="left" valign="top">
                Relay Server?:
                </td>
                <td align="left" valign="top">
                <input type="checkbox" name="isrelay" value="UNCHECKED">
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Relay Port:
                </td>
                <td align="left" valign="top">
                <input type="text" name="relayport" value="8000">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Relay Server:
                </td>
                <td align="left" valign="top">
                <input type="text" name="relayhost" value="127.0.0.1">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Auto Dump Users:
                </td>
                <td align="left" valign="top">
		<select name="autodump">
                <option value="0">No</option>
                <option value="1">Yes</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="30">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Public Server:
                </td>
                <td align="left" valign="top">
                <input type="text" name="publicsrv" value="default">
                </td>
        </tr>
        <tr>
                <td align="left" valign="top">
                Allow Relay:
                </td>
                <td align="left" valign="top">
                <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">
		<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="32768">
		<input type="hidden" name="submitnew" value="1">
                </td>
        </tr>
	<tr>
                <td align="left" valign="top">
                Assign to user:
                </td>
                <td align="left" valign="top">
		<select name="uid">
		<option value="null">---</option>
                <?
		userlist();
		?>
		</select>
                </td>
        </tr>
        <tr>
        				<td>
        				Use Auto DJ:
        				</td>
	             			<td>
        				<input type="checkbox" name="autodj">Yes</td>
			</tr>
			<tr>
						<td>
							Playlist:
						</td>
						<td><select name="playlist">
						<? playlist(); ?>
							</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">
					</td>
			</tr><tr>
					<td align="left" valign="top">
					Website:
					</td><td>
					<input type="text" name="website">
					</td>
			</tr><tr>
					<td align="left">
					<input type="submit" name="subnew" value="Create Server">
			</td>
			</tr>
	</table>
</form>
<?
}
}?>
