<?
////////////////////////////////////////////////////////////////////////
/*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.*/
////////////////////////////////////////////////////////////////////////

ob_start();
session_start();
require('include/functions.inc.php');
$db = dbConnect();

if(check_install() == "1") {
  header('Location: install/install.php');
} else {
  if(isset($_SESSION['username']) && $db->numRows("SELECT * FROM members WHERE username = '".$_SESSION['username']."'") > 0) {
    header('Location: home.php');
  } else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI Login</title>
<link rel="stylesheet" type="text/css" href="include/smi.css">
</head>
<body>
<center>
	<img src="imgs/smi-logo.jpg">
<br>
</center>
<?
  function form($error)
  {
?>
<table class="login" width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
<tr align="center">
	<form name="form1" method="post" action="">
   <td align="center">
	<table width="50%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr align="center">
   <td colspan="3">
	<? if($error){ 
	echo $error; 
	} else { 
	echo '<strong>SMI Login </strong>'; 
	} ?>
   </td>
</tr>
<tr align="center">
   <td>Username</td>
   <td>:</td>
   <td><input class="input" name="username" type="text" id="username"></td>

</tr>
<tr align="center">
   <td>Password</td>
   <td>:</td>
   <td><input class="input" name="password" type="password" id="password"></td>
</tr>
<tr>
   <td>
	&nbsp;
   </td>
   <td>
	&nbsp;
   </td>
   <td>
	<input type="submit" name="Submit" value="Login">
   </td>

</tr>
</table>
</td>
	</form>
</tr>
</table>
<?
  }
  if(isset($_REQUEST['Submit'])) {
    if(!$_POST['username'] || !$_POST['password']) {
      $error = 'Error: All fields are required';
      echo form($error);
    } else {
      if (login_check($_POST['username'],$_POST['password'])) {
        header('Location: home.php');
        $_SESSION["ip"] = getenv('REMOTE_ADDR');
        $_SESSION["username"] = $_POST['username'];
        $_SESSION["password"] = $_POST['password'];
        $timestamp = "".strftime('%T')." ".date('Y-m-d')."";
        $db = dbConnect();
        $insert = array(  'event_user' => $_POST['username'],
			  'event'      => $_POST['username'].'logged in from '.getenv('REMOTE_ADDR'),
			  'timestamp'  => $timestamp );
      } else {
        $error = "Invalid username or password, try again";
        echo form($error);
      }
    }
  } else {
    if(isset($_GET['logout'])) {
      session_destroy();
      $error = "Logged Out Success - Try Again"; 
      echo form($error); 
    } else { 
      if (isset($_SESSION['username'])) { 
        if (login_check($_SESSION['username'],$_SESSION['password'])) { 
        } else { 
          $error = "Please Login"; echo form($error); 
        } 
      } else { 
        $error = "Welcome, Please Login"; echo form($error); 
      } 
    } 
  } 
?>
<div align="center">
<p>&nbsp;</p>
</div> </body>

</html>
<?
}
}
?>
