<?php

// +---------------------------------------------+
// |     Copyright  2008 - 2018 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
  die('File not found!');

// ############################# LOG USER ON ###############################

function LoginUser($loginusername, $loginpassword)
{
  global $DB;
	
  if(strlen($loginusername))
  {
  	$sql = "SELECT userid, usergroupid, email, username, password, banned, activated FROM " . TABLE_PREFIX . "users WHERE username = '" .  addslashes($loginusername) . "'";

    if($user = $DB->query_first($sql))
    {
      if($user['banned'] == 1)
      {
        $loginerrors = true;
      }
      else if($user['activated'] == 0)
      {
        $loginerrors = true;
      }
      else if($user['password'] != md5($loginpassword))
      {
        $loginerrors = true;
      }
      else
      {
        $user['usergroupids']   = $user['usergroupid'];
        $user['loggedin']       = 1;
        $user['username']       = $user['username'];
        $user['password']       = $user['password'];
      }
    }
    else
    {
      $loginerrors = true;
    }
  }
  else
  {
    $loginerrors = true;
  }

  return $loginerrors? 'login error' : $user;
}

// ############################# GET USER DETAILS ###############################

function GetUser($userid)
{
  global $DB;
	
  $user = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "users WHERE userid = '" . $userid . "'");

  if(isset($user))
  {
  	$user['usergroupids']   = $user['usergroupid'];
  	$user['loggedin']       = 1;
  }
  
  return $user;
}


?>