<?php
// +---------------------------------------------+
// |     Copyright  2003 - 2005 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
  die('File not found!');
  
define('ANONYMOUS', -1);

// ############################# LOG USER ON ###############################
 
function LoginUser($loginusername, $loginpassword)
{
	global $DB, $sdlanguage, $usersystem;
	
	if(strlen($loginusername))
	{
		$sql = "SELECT * FROM " . $usersystem['tblprefix'] . "users WHERE username = '" . addslashes($loginusername) . "'";

		// get userid for given username
		if($getuser = $DB->query_first($sql))
		{
			if(md5($loginpassword) === $getuser['user_password'] AND $getuser['user_active'] AND $getuser['user_id'] != ANONYMOUS)
			{
				// logged in
				$user = array('userid'         => $getuser['user_id'],   // phpbb guests are -1 userid
							  'usergroupids'   => 5,    // phpBB2 Registered, this is just the default group, more or gathered next:
							  'username'       => $getuser['username'],
							  'loggedin'       => 1,
							  'email'          => $getuser['user_email'],
							  'timezoneoffset' => $getuser['user_timezone'],
							  'dstonoff'       => 0,    // phpBB2 doesn't have a dst option
							  'dstauto'        => 0);   // phpBB2 doesn't have a dst option

				$usergroupids = array();

				// find the usergroupids
				// phpbb2 creates a new usergroup for each user, but I don't want to show hundreds of usergroups in the admin panel
				// so lets only select the usergroups that don't have a strlen of 0
				//$usergroupids[] = 5; // default of 5

				$getusergroups = $DB->query("SELECT g.group_id FROM " . $usersystem['tblprefix'] . "groups g, " . $usersystem['tblprefix'] . "user_group u
                             					WHERE u.user_id = $user[userid] AND u.group_id = g.group_id AND g.group_name != ''");

				while($usergroup = $DB->fetch_array($getusergroups))
				{
					// now find the usergroup name
					if(count($usergroupids) <= 0 || !in_array($usergroup['group_id'], $usergroupids))
					{
						$usergroupids[] = $usergroup['group_id'];
					}
				}

				// add the 'fake' registered group?
				if(count($usergroupids) <= 0 && $isregistered = $DB->query_first("SELECT g.group_id FROM " . $usersystem['tblprefix'] . "groups g, " . $usersystem['tblprefix'] . "user_group u
                                      												WHERE u.user_id = $user[userid] AND u.group_id = g.group_id AND g.group_name = ''"))
				{
					$usergroupids[] = -3; // usergroup id -3 is an emulated registered usergroup for phpBB2 created by weenCompany
				}

				$user['usergroupids'] = $usergroupids;
			}
			else
			{
				$loginerrors = $sdlanguage['wrong_password'];
			}
		}
		else
		{
			// wront username OR: username = ANONYMOUS, probably a hacker
			// lets just give them wrong username to throw them off
			$loginerrors = $sdlanguage['wrong_username'];
		}
	}
	else
	{
		$loginerrors = $sdlanguage['please_enter_username'];
	}
	return isset($loginerrors) ? $loginerrors : $user;
}

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

function GetUser($userid)
{
	global $DB, $usersystem;
	
	$getuser = $DB->query_first("SELECT * FROM " . $usersystem['tblprefix'] . "users WHERE user_id = $userid");

	if(isset($getuser))
	{
		$user = array('userid'         => $getuser['user_id'],   // phpbb guests are -1 userid
					  'username'       => $getuser['username'],
					  'loggedin'       => 1,
					  'email'          => $getuser['user_email'],
					  'timezoneoffset' => $getuser['user_timezone'],
					  'dstonoff'       => 0,    // phpBB2 doesn't have a dst option
					  'dstauto'        => 0);   // phpBB2 doesn't have a dst option
						  
		$usergroupids = array();

		// find the usergroupids
		// phpbb2 creates a new usergroup for each user, but I don't want to show hundreds of usergroups in the admin panel
		// so lets only select the usergroups that don't have a strlen of 0
		//$usergroupids[] = 5; // default of 5

		$getusergroups = $DB->query("SELECT g.group_id FROM " . $usersystem['tblprefix'] . "groups g, " . $usersystem['tblprefix'] . "user_group u
                       					WHERE u.user_id = $user[userid] AND u.group_id = g.group_id AND g.group_name != ''");

		while($usergroup = $DB->fetch_array($getusergroups))
		{
			// now find the usergroup name
			if(count($usergroupids) <= 0 || !in_array($usergroup['group_id'], $usergroupids))
			{
				$usergroupids[] = $usergroup['group_id'];
			}
		}

		// add the 'fake' registered group?
		if(count($usergroupids) <= 0 && $isregistered = $DB->query_first("SELECT g.group_id FROM " . $usersystem['tblprefix'] . "groups g, " . $usersystem['tblprefix'] . "user_group u
                                     												WHERE u.user_id = $user[userid] AND u.group_id = g.group_id AND g.group_name = ''"))
		{
			$usergroupids[] = -3; // usergroup id -3 is an emulated registered usergroup for phpBB2 created by weenCompany
		}

		$user['usergroupids'] = $usergroupids;
	}

	return $user;
}

?>