<?php 
  include_once "./password.php";

  //  AudioStreamer, www.audiostreamer.org
  //  Copyright (C) <2013>  <Lieven Rottiers>
  //
  //  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 3 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, see <http://www.gnu.org/licenses/>.
	
  session_start();
  session_destroy();
  session_start();

  //database connection
  //also change this in audiostreamerlib.php
  $sdb = "./sqlite/audiostreamer.db";
  $dbh = new PDO("sqlite:".$sdb);
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
  $output = '';

  if (!empty($_POST)) { 
    $_SESSION["username"] = $_POST["username"]; 
    $_SESSION["password"] = $_POST["password"]; 

    //
    //check if working with the password file
    //and thus checking username and password
    //
    if (!empty($start_password)) {
      $username = $start_username;
      $password = $start_password;
      $_SESSION["userid"] = "";
      $_SESSION["ind_admin"] = "1";
    }
    else {
      $username="";
      $password="";
      $sql = "select id, name, password, ind_admin, ind_desktop from user where name='".$_SESSION["username"]."'";
      foreach ($dbh->query($sql) as $row) {
        $username = $row["name"];
        $password = $row["password"];
        $_SESSION["userid"] = $row["id"];
        $_SESSION["ind_admin"] = $row["ind_admin"];
        $_SESSION["ind_desktop"] = $row["ind_desktop"];
      }   
    }

    if (empty($_SESSION["username"])) { 
      $output = $output. '<script type="text/javascript">alert("User must be specified. Try again.");</script>'; 
      session_destroy(); 
    }
    else if (empty($_SESSION["password"])) { 
      $output = $output. '<script type="text/javascript">alert("Password must be specified. Try again.");</script>'; 
      session_destroy(); 
    }
    else if ($_SESSION["username"]!=$username) { 
      $output = $output. '<script type="text/javascript">alert("Wrong user. Login Failed. Try again.");</script>'; 
      session_destroy(); 
    }
    else if ($_SESSION["password"]!=$password) { 
      $output = $output. '<script type="text/javascript">alert("Wrong password. Login Failed. Try again.");</script>'; 
      session_destroy(); 
    }
    else { 
	//fetching current desktop
      $sql = "select id from desktop where ind_current = 1 and user='".$_SESSION["userid"]."'";
      foreach ($dbh->query($sql) as $row) {
        $_SESSION["desktopid"] = $row["id"];
      }   
      //fetching setting parameters
      $sql = "select param, value from param";
      foreach ($dbh->query($sql) as $row) {
        $_SESSION[$row["param"]] = $row["value"];
      }   
      header("Location: ./index.php"); 
    }
  }
  //close connection
  $dbh = null;
    
  //
  $output = '
<!DOCTYPE html>
<html>
<head>
<title>AudioStreamer Login</title>

<link rel="stylesheet" type="text/css" href="./app/css/AudioStreamer.css"> 
<link rel="shortcut icon" href="./favicon.ico" />
  
<script type="text/javascript" src="./app/js/jquery-1.7.1.min.js"></script> 
<script type="text/javascript" src="./app/js/jquery-ui-1.8.10.custom.min.js"></script> 
<script type="text/javascript" src="./app/js/jquery.jplayer.min.js"></script>
<script type="text/javascript" src="./app/js/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="./app/js/AudioStreamer.js"></script>'.$output;

  echo $output;

?>
 
</head>
<body>

  <div id="content">

    <div class="drag ui-widget ui-widget-content ui-corner-all" style="z-index: 9; position: absolute; width: 400px; height: 150px; ">
      <div class="handler"><img src="./app/img/grab.gif">Please login to AudioStreamer</div>
      <div class="api login"><div class="top-spacer"></div>
        <form method=post action="login.php"> 
          <br/>
          <table class="login">
            <tr><td rowspan="3"><img align="center" src="./app/img/audiostreamer_logo.gif" /></td></tr>
            <tr><td align="right">Username</td><td><input name=username></td></tr>
            <tr><td align="right">Password</td><td><input name=password type=password></td></tr>
            <tr><td></td><td></td><td><input type=submit value="Login"></td></tr>
          </table>
        </form> 
      </div>
    </div>
  </div>

<script type="text/javascript"> 
  pwidth = ($(document).width() - $(".drag").width()) / 2;
  pheight = ($(document).height() - $(".drag").height()) / 2;
  $('.drag').css({ 'top':pheight, 'left':pwidth });

  $('form:first *:input[type!=hidden]:first').focus();
</script>
 
</body>
</html>