<?php

class login
{
  public function __construct()
  {
    $this->Database                         = new Database();                             /** Datenbankklasse instanzieren */
    $this->pwHash                           = new pwhash(BCRYPT_HASH_ROUNDS, FALSE);
  }
  
  public function checkLogin($values)
  {
    $db                                     = $this->Database;
    $sqlUser                                = 'SELECT password, uuid FROM tbl_users WHERE username = :username AND account_locked = :account_locked';
    $db->query($sqlUser);
    $db->bind(':username', $values['username']);
    $db->bind(':account_locked', 0);
    $row                                    = $db->single();
    
    $pwHash                                 = $this->pwHash;
    $loginPass                              = $values['password'];
    $dbPasswordHash                         = $row['password'];
    $uuid                                   = $row['uuid'];
    $loginPasspwHash                        = $pwHash->CheckPassword($loginPass, $dbPasswordHash);
    
    if ($loginPasspwHash                    == 1) {
      return $uuid;
    } else {
      return 0;
    }
  }
  
  public function getUserData($uuid)
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM tbl_users WHERE uuid = :uuid';
    $db->query($sql);
    $db->bind(':uuid', $uuid);
    return $db->single();
  }
  
  public function checkAutoLogin($identifier, $secToken, $ipAddress)
  {
    $db                                     = $this->Database;
    $db->query('SELECT COUNT(id) as amount FROM tbl_users_login WHERE users_uuid = :users_uuid AND sec_token = :sec_token AND ip_address = :ip_address');
    $db->bind(':users_uuid', $identifier);
    $db->bind(':sec_token', $secToken);
    $db->bind(':ip_address', security::decode($ipAddress));
    
    $row                                    = $db->single();
    return $row['amount'];
  }
  
  public function saveCookieLogin($userID, $sectoken)
  {
    $usersLoginData                         = array(
      'users_uuid'                          => $userID,
      'sec_token'                           => $sectoken,
      'date_cookie_set'                     => strtotime(date("Y-m-d H:i:s")),
      'ip_address'                          => $_SERVER['REMOTE_ADDR'],
      'user_agent'                          => $_SERVER['HTTP_USER_AGENT']
    );
    return pdoDbInsert("tbl_users_login", $usersLoginData);
  }
  
  public function getUnauthorizedLoginsExt()
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM tbl_login_ext_not_allowed WHERE is_deactivated = :is_deactivated ORDER BY id DESC';
    $db->query($sql);
    $db->bind(':is_deactivated', 0);
    return $db->resultset();
  }
  
  public function deactivateUnauthorizedLoginsExt($id)
  {
    $updateArray                            = array(
        'is_deactivated'                    => 1,
    );
    $whereCause                             = 'id = :id';
    $whereVal                               = $id;
    return pdoDbUpdate("tbl_login_ext_not_allowed", $updateArray, $whereCause, $whereVal);
  }
  
  public function getUnauthorizedLogins()
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM tbl_login_incorrect WHERE is_deactivated = :is_deactivated ORDER BY id DESC';
    $db->query($sql);
    $db->bind(':is_deactivated', 0);
    return $db->resultset();
  }
  
  public function deactivateUnauthorizedLogins($id)
  {
    $updateArray                            = array(
        'is_deactivated'                    => 1,
    );
    $whereCause                             = 'id = :id';
    $whereVal                               = $id;
    return pdoDbUpdate("tbl_login_incorrect", $updateArray, $whereCause, $whereVal);
  }
  
  public function getAuthorizedLogins()
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM tbl_login_correct WHERE is_deactivated = :is_deactivated ORDER BY id DESC';
    $db->query($sql);
    $db->bind(':is_deactivated', 0);
    return $db->resultset();
  }
  
  public function deactivateAuthorizedLogins($id)
  {
    $updateArray                            = array(
        'is_deactivated'                    => 1,
    );
    $whereCause                             = 'id = :id';
    $whereVal                               = $id;
    return pdoDbUpdate("tbl_login_correct", $updateArray, $whereCause, $whereVal);
  }
  
  public function isCookie($value)
  {
    switch ($value) {
      case 0:
        return "";
        break;
      case 1:
        return '<i class="fa fa-birthday-cake" aria-hidden="true"></i>';
        break;
    }
    
  }
  
  public function getBlockedIPAdresses()
  {
    $db                               = new Database();
    $sql                              = 'SELECT COUNT(id) as amount, ip_address FROM tbl_login_incorrect WHERE is_unlocked = :is_unlocked GROUP BY ip_address';
    $db->query($sql);
    $db->bind(':is_unlocked', 0);
    $rows                             = $db->resultset();
    
    foreach ($rows as $row) {
      if ($row['amount']              >= MAX_FAILED_LOGINS) {
        $ipAddress[]                  = $row['ip_address'];
      }
    }
    return $ipAddress;
  }
  
  public function unlockBlockedIpAddresses($ipAddress)
  {
    $db                               = new Database();
    $sql                              = 'UPDATE tbl_login_incorrect SET is_unlocked = :is_unlocked WHERE ip_address = :ip_address';
    $db->query($sql);
    $db->bind(':is_unlocked', 1);
    $db->bind('ip_address', $ipAddress);
    return $db->execute();
  }
}

