<?php
// Class for managing players account

class PlayerAccount
{
	private $player_id;
	
	/**
	 * @name CreateNewPlayer
	 * @param s_pseudo string
	 * @param s_password string
	 * @param s_email string
	 * @param s_desc string[optional]
	 * @param s_avatar string[optional]
	 * @return int
	 * @desc Create a new player in database. Return 0 when ok.
	 */
	function CreateNewPlayer($s_pseudo,$s_password,$s_email,$s_desc="",$s_avatar="")
	{
		if(mysql_num_rows(mysql_query('SELECT "x" FROM users WHERE username="'.$s_pseudo.'"'))>0) return 4;
		if(ereg('.*@.*\..*',$s_email)===false) return 2;
		if(mysql_num_rows(mysql_query('SELECT "x" FROM users WHERE email="'.$s_email.'"'))>0) return 3;
		
		mysql_query('INSERT INTO users (username,user_password,email,description,avatar) VALUES("'.$s_pseudo.'",MD5("'.$s_password.'"),"'.$s_email.'","'.$s_desc.'","'.$s_avatar.'")');
		if(mysql_affected_rows()==0) return 1;
		
		$this->player_id=mysql_insert_id();
		return 0;		
	}
	
	/**
	 * @name SetPlayer
	 * @param s_pseudo string
	 * @param s_password string
	 * @return int
	 * @desc Associate a player to the class. Return 0 when success
	 */
	function SetPlayer($s_pseudo,$s_password)
	{
		$r_res=mysql_query('SELECT user_id FROM users WHERE username="'.$s_pseudo.'" AND user_password=MD5("'.$s_password.'")');
		if(mysql_num_rows($r_res)==0) return 1;
		
		$this->player_id=mysql_result($r_res,0,0);
		return 0;
	}
	
	/**
	 * @name ResetPlayerPassword
	 * @param s_pseudo string
	 * @param s_subject string
	 * @param s_body string
	 * @desc Replace the passowrd of the specified player and send mail.
	 */
	function ResetPlayerPassword($s_pseudo,$s_subject,$s_body,$s_sender)
	{
		$s_caracters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$s_newpassword="";
		for($i=0;$i<8;$i++) $s_newpassword.=$s_caracters[rand(0,61)];
		
		mysql_query('UPDATE users SET user_password=MD5("'.$s_newpassword.'") WHERE username="'.$s_pseudo.'"');
		if(mysql_affected_rows()!=1)
		{
			return 1;
		}
		else
		{
			$r_email=mysql_query('SELECT email FROM users WHERE username="'.$s_pseudo.'"');
			$s_email=mysql_result($r_email,0,'email');
			mail($s_email,$s_subject,str_replace('###',$s_pseudo,str_replace('&&&',$s_newpassword,$s_body)),"From: ".$s_sender);
			return 0;
		}		
	}
	
	/**
	 * @name DeletePlayer
	 * @param s_pseudo string
	 * @param s_password string
	 * @desc Delete the player associate to the class.
	 */
	function DeletePlayer()
	{
		mysql_query('DELETE FROM player_totals WHERE pid='.$this->player_id);
		mysql_query('DELETE FROM player_stats WHERE pid='.$this->player_id);
		mysql_query('DELETE FROM banlist WHERE ban_userid='.$this->player_id);
		mysql_query('DELETE FROM users WHERE user_id='.$this->player_id);
	}
	
	/**
	 * @name UpdatePlayerProfile
	 * @param s_password string
	 * @param s_email string
	 * @param s_desc string
	 * @param s_avatar string
	 * @return int
	 * @desc Update the player profile. Return 0 when success
	 */
	function UpdatePlayerProfile($s_password,$s_email,$s_desc,$s_avatar)
	{
		if(ereg('(.*@.*\..*',$s_email)===false) return 2;
		if(mysql_num_rows(mysql_query('SELECT "x" FROM users WHERE email="'.$s_email.'"'))>0) return 3;

		mysql_query('UPDATE users SET user_password=MD5("'.$s_password.'"),email="'.$s_email.'",description="'.$s_desc.'",avatar="'.$s_avatar.'" WHERE user_id='.$this->player_id);
		if(mysql_affected_rows()!=1) return 1;
		
		return 0;
	}
	
	/**
	 * @name GetPlayerName
	 * @return string
	 * @desc Return the pseudo of the player
	 */
	function GetPlayerName ()
	{
		$r_res=mysql_query('SELECT username FROM users WHERE user_id='.$this->player_id);
		return mysql_result($r_res,0,0);
	}
	
	/**
	 * @name GetPlayerLevel
	 * @return string
	 * @desc Return the admin level of the player
	 */
	function GetPlayerLevel ()
	{
		$r_res=mysql_query('SELECT user_level FROM users WHERE user_id='.$this->player_id);
		return mysql_result($r_res,0,0);
	}
	
	/**
	 * @name SetPlayerID
	 * @param i_pid - int
	 * @desc Set the Player_ID used by the class
	 */
	function SetPlayerID ($i_pid)
	{
		$this->player_id=$i_pid;
	}
	
	/**
	 * @name GetPlayerID
	 * @desc return the Player_ID used by the class
	 */
	function GetPlayerID ()
	{
		return $this->player_id;
	}
}
?>