<?php
	class OpenSim
	{
		public function isLoginValid($name, $password)
		{
			global $RUNTIME;

			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM UserAccounts WHERE FirstName = ? AND LastName = ? LIMIT 1");
			$statementUser->execute(explode(" ", trim($name))); 

			while($rowUser = $statementUser->fetch()) 
			{
				$statementAuth = $RUNTIME['PDO']->prepare("SELECT * FROM auth WHERE UUID = ? LIMIT 1");
				$statementAuth->execute(array($rowUser['PrincipalID'])); 

				while($rowAuth = $statementAuth->fetch()) 
				{
					if(md5(md5($password).":".$rowAuth['passwordSalt']) == $rowAuth['passwordHash'])
					{
						return true;
					}
				}
			}

			return false;
		}

		public function getUserName($userID)
		{
			global $RUNTIME;
			
			if($userID == "00000000-0000-0000-0000-000000000000")
				return "Unknown User";

			if(isset($RUNTIME['CACHE']['USERNAME'][$userID]))
				return $RUNTIME['CACHE']['USERNAME'][$userID];

			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM UserAccounts WHERE PrincipalID = ?");
			$statementUser->execute(array($userID));

			while($rowUser = $statementUser->fetch()) 
			{
				$RUNTIME['CACHE']['USERNAME'][$userID] = $rowUser['FirstName']." ".$rowUser['LastName'];
				return $rowUser['FirstName']." ".$rowUser['LastName'];
			}

			$statementGridUser = $RUNTIME['PDO']->prepare("SELECT * FROM GridUser");
			$statementGridUser->execute(array($userID));

			while($rowGridUser = $statementGridUser->fetch()) 
			{
				$UserData = explode(";", $rowGridUser['UserID']);

				if(count($UserData) >= 3)
				{
					$DBUserID = $UserData[0];
					$DBUserName = $UserData[2];

					$RUNTIME['CACHE']['USERNAME'][$userID] = $DBUserName;

					if($DBUserID == $userID)
						return $DBUserName;
				}
			}

			$statementFriends = $RUNTIME['PDO']->prepare("SELECT * FROM Friends");
			$statementFriends->execute(array($userID));

			while($rowFriends = $statementFriends->fetch()) 
			{
				$UserData = explode(";", $rowFriends['PrincipalID']);

				if(count($UserData) == 4)
				{
					$DBUserID = $UserData[0];
					$DBUserName = $UserData[2];

					$RUNTIME['CACHE']['USERNAME'][$userID] = $DBUserName;

					if($DBUserID == $userID)
						return $DBUserName;
				}
			}

			return "Unknown User";
		}

		public function getUserUUID($UserName)
		{
			global $RUNTIME;
			
			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM UserAccounts");
			$statementUser->execute();

			while($rowUser = $statementUser->fetch()) 
			{
				$SQLUserName = $rowUser['FirstName']." ".$rowUser['LastName'];

				if($SQLUserName == $UserName)
				{
					return $rowUser['PrincipalID'];
				}
			}

			return null;
		}

		public function getRegionName($regionID)
		{
			global $RUNTIME;
			
			$statementRegion = $RUNTIME['PDO']->prepare("SELECT * FROM regions WHERE uuid = ?");
			$statementRegion->execute(array($regionID));

			while($rowRegion = $statementRegion->fetch()) 
			{
				return $rowRegion['regionName'];
			}

			return "Unknown Region";
		}

		public function getPartner($userID)
		{
			global $RUNTIME;
			
			$statement = $RUNTIME['PDO']->prepare("SELECT * FROM userprofile WHERE useruuid = ?");
			$statement->execute(array($userID));

			while($row = $statement->fetch()) 
			{
				if($row['profilePartner'] != "00000000-0000-0000-0000-000000000000")
					return $row['profilePartner'];
			}

			return null;
		}

		public function allowOfflineIM($userID)
		{
			global $RUNTIME;
			
			$statement = $RUNTIME['PDO']->prepare("SELECT * FROM usersettings WHERE useruuid = ?");
			$statement->execute(array($userID));

			while($row = $statement->fetch()) 
			{
				return strtoupper($row['imviaemail']);
			}

			return "FALSE";
		}

		public function getUserMail($userID)
		{
			global $RUNTIME;
			
			$statement = $RUNTIME['PDO']->prepare("SELECT * FROM UserAccounts WHERE PrincipalID = ?");
			$statement->execute(array($userID));

			while($row = $statement->fetch()) 
			{
				return $row['Email'];
			}

			return "";
		}

		public function getUserCount()
		{
			global $RUNTIME;
			
			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM UserAccounts");
			$statementUser->execute();
			return $statementUser->rowCount();
		}

		public function getRegionCount()
		{
			global $RUNTIME;
			
			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM regions");
			$statementUser->execute();
			return $statementUser->rowCount();
		}

		public function getOnlineCount()
		{
			global $RUNTIME;


			$statementUser = $RUNTIME['PDO']->prepare("SELECT * FROM Presence");
			$statementUser->execute();
			return $statementUser->rowCount();
		}

		public function gen_uuid() 
		{
			return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
				// 32 bits for "time_low"
				mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
		
				// 16 bits for "time_mid"
				mt_rand( 0, 0xffff ),
		
				// 16 bits for "time_hi_and_version",
				// four most significant bits holds version number 4
				mt_rand( 0, 0x0fff ) | 0x4000,
		
				// 16 bits, 8 bits for "clk_seq_hi_res",
				// 8 bits for "clk_seq_low",
				// two most significant bits holds zero and one for variant DCE1.1
				mt_rand( 0, 0x3fff ) | 0x8000,
		
				// 48 bits for "node"
				mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
			);
		}
    }
?>