 <?php
  session_start();
  include_once('config/config.php');
  include_once('config/class.php');

  $con =new DB;

  if(isset($_POST['login'])){
    $username = trim($_POST['email']);
    $password = filter_var($_POST['password']);

    //hashed passwords
    $hash_pass = sha1($password);

    //checks if the username and password matches in the database
    $query = $con->getRows('system_users', array('where'=>'email_address="'.$username.'" and password="'.$hash_pass.'"', 'return_type'=>'single'));
    if(!empty($query)){
      //define user session
      $_SESSION['user_id']   = $query['user_id'];
      $_SESSION['user_role'] = $query['user_type'];
      $_SESSION['UPSWD']   = $hash_pass;
      $_SESSION['name'] =ucwords($query['first_name']).' '.ucwords($query['last_name']);
      if($query['user_type'] == 'Admin'){
          //header("Location:user_accounts/admin/dashboard.php");
          echo "<script>window.location.href = \"user_accounts/admin/dashboard.php\";</script>";
      }else {
          header("Location:user_accounts/editors/dashboard.php");
          //echo "<script>window.location.href = 'user_accounts/editors/dashboard.php'</script>";
      }

    }else{
      echo '<div class="alert alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-user"></span> Your username/password combination is invalid!</div>';
    }
  }
