<?php
/*
* -------------------------------
* Crée par Loïc TCHING
* 07/2013
* -------------------------------
*/
class User 
{
	private $id;
	private $email;    // email, fait office de login
	private $emailContact;	//email de contact du visiteur
	private $password;    // mot de passe
	private $firstName;   // prénom
	private $lastName;   // nom de famille
	private $age;   // age
	private $title;    // genre (m. mme. mlle. phd.)
	private $job;		//poste occupé
	private $adress;   // adresse
	private $zipCode;   // code postal
	private $city;	//ville
	private $country;   // code pays 3 lettres
	private $lastConnectDate; // date de dernière connexion
	private $activity;   // activité (actif ou inactif)
	private $rights;   // droits : 1 = admin, 2=agent, 999= super admin
	private $knonwIds = array(); // liste (serializée) des ip connues du visiteur
	private $photoFile;   // nom de fichier de son avatar
	private $firm;    // nom de la société
	private $tel;    // numero de telephone
	private $fax;    // numero de fax
	private $website;   // site internet
	private $registDate;  // date d'enregistrement
	private $socketAgentId;		// id de l'Agent (point de vue Socket IO)
	private $apizeeAgentId;		// id de l'Agent (point de vue Apizee)
	private $apizeeApiKey;		// clé de l'API l'Agent (point de vue Apizee)
	private $apizeeSiteId;		// clé de l'API l'Agent (point de vue Apizee)
	private $isAvailable;	// deifini si l'utilisateur, en tant que agent, est disponible
	private $agentUrlList;	// liste des host pour lesquels le user peut être agent
	private $db;
	private $muteAll;
	private $defaultGroup; // si l'agent fait partie d'un groupe, c'est le groupe qui sera chargé par défaut

	//////////////////////////////////////////////////
	// CONST \ DEST
	//////////////////////////////////////////////////
	public function __construct() 
	{
	}

	public function __destruct()
	{
	}
	
	//////////////////////////////////////////////////
	// FONCTIONS
	//////////////////////////////////////////////////
	
	public function checkLoginPasswdExt($email, $passwd)
	{
		$passwdMd5 = md5(md5($passwd));
		
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT USR_ID, USR_RIGHTS FROM t_user WHERE USR_PWD_MD5='" . $passwdMd5 . "' AND USR_MAIL='" . $email . "' AND USR_ACTIVITY='1' ");
		$req->execute(array());
		$data = $req->fetch();

		if ($data['USR_RIGHTS'] == '998' ) 
		{
			return "superadmin";
		}
		else
		{
			return $data['USR_ID'];
		}
	}
	
	public function checkLoginPasswdAdmin($email, $passwd)	
	{
		$passwdMd5 = md5($passwd);
		
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT USR_ID FROM t_user WHERE USR_PWD_MD5='" . $passwdMd5 . "' AND USR_MAIL='" . $email . "' AND USR_ACTIVITY='1' AND USR_RIGHTS > '2'");
		$req->execute(array());
		$data = $req->fetch();
		
		if ($data['USR_ID'] >= 1) 
		{
			return $data['USR_ID'];
		}
		else
			return null;
	}
	
	/****
	*  Données de l'utilisateur en fonction du id
	* 
	***/
	public function getDataFromId($id)
	{
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT * FROM t_user tu,t_licence_has_t_user tlicusr,t_licence tlic WHERE tu.USR_ID=tlicusr.T_USER_USR_USR_ID and tlicusr.T_LICENCE_LIC_LIC_ID=tlic.LIC_ID AND tu.USR_ID='" . $id."'");
		$req->execute(array());
		$data = $req->fetch();
		return $data;
	}
	
	public function getFilterFromIds($userId, $licId)
	{
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT * FROM t_licence_has_t_user tlicusr WHERE tlicusr.T_USER_USR_USR_ID='".$userId."' AND tlicusr.T_LICENCE_LIC_LIC_ID='" .$licId."'");
		$req->execute(array());
		$data = $req->fetch();
		return $data;
	}
	
	public function getSuperAgentIdFromKey($key)
	{
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT USR_ID FROM t_user tu,t_licence_has_t_user tlicusr ,t_licence tlic WHERE tu.USR_ID=tlicusr.T_USER_USR_USR_ID AND tlic.LIC_ID=tlicusr.T_LICENCE_LIC_LIC_ID AND tlic.LIC_KEY = '".$key."' AND USR_RIGHTS > 1");
		$req->execute(array());
		$data = $req->fetch();
		return $data['USR_ID'];
	}
	
	/****
	*  Données de l'utilisateur en fonction du id
	* 
	***/
	public function getDataFromEmail($email)
	{
		$bdd = connectBddOVH();
		$req = $bdd->prepare("SELECT USR_ID FROM t_user WHERE USR_MAIL='" . $email."'");
		$req->execute(array());
		$data = $req->fetch();
		return $data;
	}

	/****
	*  Update de l'utilisateur en fonction du id
	* 
	***/
	public function updateDataFromId($id, $userfirstname,$userlastname,$usertitle,$userage,$useradress,$userzip,$usercity,$usercountry,$userfax,$usertel,$userjob,$userfirm,$useremailcontact,$userweb)
	{   
		$bdd = connectBddOVH();
		$req = $bdd->prepare("UPDATE t_user SET USR_TITLE=?,USR_FIRST_NAME=?,USR_LAST_NAME=?,USR_AGE=?,USR_ADR=?,USR_CP=?,USR_CITY=?,USR_COUNTRY=?,USR_TEL=?,USR_FAX=?,USR_JOB=?,USR_FIRM=?,USR_MAIL_CONTACT=?,USR_WEBSITE=? WHERE USR_ID=?");
		$rs = $req->execute(array($usertitle,
							$userfirstname,
							$userlastname,
							$userage,
							$useradress,
							$userzip,
							$usercity,
							$usercountry,
							$usertel,
							$userfax,
							$userjob,
							$userfirm,
							$useremailcontact,
							$userweb,
							$id));
		return $rs;
	}
	
	/****
	*  ajouter un agent
	* 
	***/
	public function add($newAgentfirstname, $newAgentlastname, $newAgentemail, $newAgentPwd, $apizee_site_id, $apizee_api_key, $newAgentApizeeId, $id_licence, $rights )
	{		
		$newAgentPwdMd5 = md5(md5($newAgentPwd));
		
		$bdd = connectBddOVH();
		$req = $bdd->prepare("INSERT INTO t_user (USR_FIRST_NAME,USR_LAST_NAME,USR_MAIL,USR_MAIL_CONTACT,USR_PWD,USR_PWD_MD5,USR_RIGHTS ) VALUES(:fn, :ln, :um, :umc, :up, :upm, :ur) ");
		$rs = $req->execute(array("fn" => $newAgentfirstname,
									"ln" => $newAgentlastname,
									"um"  => $newAgentemail,
									"umc"  => $newAgentemail,
									"up" => $newAgentPwd,
									"upm" => $newAgentPwdMd5,
									"ur" => $rights
									));

		//Puis on insert dans la table de correspondance t_licence_has_t_user
		$id_user = '';
		$id_user =$bdd->lastInsertId();
		
		$bdd2 = connectBddOVH();
		$req2 = $bdd2->prepare("INSERT INTO t_licence_has_t_user (T_USER_USR_USR_ID,T_LICENCE_LIC_LIC_ID ) VALUES(:user, :licence) ");
		$rs2 = $req->execute(array("user" => $id_user,"licence" => $id_licence));
		return $rs2;
	}
	/****
	*  ajouter un agent Multisite
	* 
	***/
	public function addMulti($newAgentfirstname, $newAgentlastname, $newAgentemail, $newAgentPwd, $apizee_site_id, $apizee_api_key, $newAgentApizeeId, $listKeys, $rights )
	{	
		$newAgentPwdMd5 = md5(md5($newAgentPwd));

		$bdd = connectBddOVH();
		$req = $bdd->prepare("INSERT INTO t_user (USR_FIRST_NAME,USR_LAST_NAME,USR_MAIL,USR_MAIL_CONTACT,USR_PWD,USR_PWD_MD5,USR_RIGHTS ) VALUES(:fn, :ln, :um, :umc, :up, :upm, :ur) ");
		$rs = $req->execute(array("fn" => $newAgentfirstname,
									"ln" => $newAgentlastname,
									"um"  => $newAgentemail,
									"umc"  => $newAgentemail,
									"up" => $newAgentPwd,
									"upm" => $newAgentPwdMd5,
									"ur" => $rights
									));

		//Puis on insert dans la table de correspondance t_licence_has_t_user
		$id_user = '';
		$id_user =$bdd->lastInsertId();

		$listKeysArray = explode("|", $listKeys);
		
		$request = "";
		$first = 0;
		foreach($listKeysArray as $key)
		{
			if($key != "")
			{
				if($first == 0)
				{	
					$request = "LIC_KEY = '".$key."'";
					$first = 1;
				}
				else
				$request .= " OR LIC_KEY = '".$key."'";
			}
		}
	
		$bdd2 = connectBddOVH();
		$req2 = $bdd2->prepare("SELECT LIC_ID FROM t_licence WHERE ".$request);
		$req2->execute(array());
		while ($data2 = $req2->fetch())
		{
			$res3 = false;
			if($request != "" AND !empty($id_user))
			{
				$bdd3 = connectBddOVH();
				$req3 = $bdd3->prepare("INSERT INTO t_licence_has_t_user (T_USER_USR_USR_ID,T_LICENCE_LIC_LIC_ID ) VALUES(:user, :licence) ");
				$res3 = $req->execute(array("user" => $id_user, "licence" => $data2['LIC_ID']));
			}
		}
		
		return $res3;
	}
	/****
	*  ajouter un agent Multisite
	* 
	***/
	public function addWatcheeMulti($newAgentfirstname, $newAgentlastname, $newAgentemail, $newAgentPwd, $listIds, $rights, $priority, $keywords, $activity = 1)
	{	
		$newAgentPwdMd5 = md5(md5($newAgentPwd));

		$bdd = connectBddOVH();
		$req = $bdd->prepare("INSERT INTO t_user (USR_FIRST_NAME,USR_LAST_NAME,USR_MAIL,USR_MAIL_CONTACT,USR_PWD,USR_PWD_MD5,USR_RIGHTS,USR_PRIORITY,USR_KEYWORDS,USR_ACTIVITY ) VALUES(:fn, :ln, :um, :umc, :up, :upm, :ur,:upr,:uk, :ua) ");
		$rs = $req->execute(array("fn" => $newAgentfirstname,
									"ln" => $newAgentlastname,
									"um"  => $newAgentemail,
									"umc"  => $newAgentemail,
									"up" => $newAgentPwd,
									"upm" => $newAgentPwdMd5,
									"ur" => $rights,
									"upr" => $priority,
									"uk" => $keywords,
									"ua" => $activity
									));

		//Puis on insert dans la table de correspondance t_licence_has_t_user
		$id_user = '';
		$id_user =$bdd->lastInsertId();
	
		foreach($listIds as $id)
		{
			if($id['LIC_ID'] != "" && $id_user != '')
			{
				$bdd3 = connectBddOVH();
				$req3 = $bdd3->prepare("INSERT INTO t_licence_has_t_user (T_USER_USR_USR_ID,T_LICENCE_LIC_LIC_ID ) VALUES(:user, :licence) ");
				$res3 = $req3->execute(array("user" => $id_user, "licence" => $id['LIC_ID']));
			}
		}
		
		return $id_user;
	}	
	//////////////////////////////////////////////////
	// ACCESSEURS
	//////////////////////////////////////////////////
	
	// id
	public function getId() {
		return $this->id;
	}

	public function setId($newId) {
		$this->id = $newId;
	}
	
	// email
	public function getEmail() {
		return $this->email;
	}

	public function setEmail($newEmail) {
		$this->email = $newEmail;
	}

	//emailContact
	public function getEmailContact() {
		return $this->emailContact;
	}

	public function setEmailContact($newEmailContact) {
		$this->emailContact = $newEmailContact;
	}	
	
	//defaultGroup
	public function getDefaultGroup() {
		return $this->defaultGroup;
	}

	public function setDefaultGroup($newDefaultGroup) {
		$this->defaultGroup = $newDefaultGroup;
	}
	
	// password
	public function getPassword() {
		return $this->password;
	}

	public function setPassword($newPassword) {
		$this->password = $newPassword;
	}

	// firstName
	public function getFirstName() {
		return $this->firstName;
	}

	public function setFirstName($newFirstName) {
		$this->firstName = $newFirstName;
	}

	// lastName
	public function getLastName() {
		return $this->lastName;
	}

	public function setLastName($newLastName) {
		$this->lastName = $newLastName;
	}

	// age
	public function getAge() {
		return $this->age;
	}

	public function setAge($newAge) {
		$this->age = $newAge;
	}
	
	// title
	public function getTitle() {
		return $this->title;
	}

	public function setTitle($newTitle) {
		$this->title = $newTitle;
	}

	// job
	public function getJob() {
		return $this->job;
	}

	public function setJob($newJob) {
		$this->job = $newJob;
	}
	
	// adress
	public function getAdress() {
		return $this->adress;
	}

	public function setAdress($newAdress) {
		$this->adress = $newAdress;
	}

	// zipCode
	public function getZipCode() {
		return $this->zipCode;
	}

	public function setZipCode($newZipCode) {
		$this->zipCode = $newZipCode;
	}

	// city
	public function getCity() {
		return $this->city;
	}

	public function setCity($newCity) {
		$this->city = $newCity;
	}
	
	// country
	public function getCountry() {
		return $this->country;
	}

	public function setCountry($newCountry) {
		$this->country = $newCountry;
	}

	// lastConnectDate
	public function getLastConnectDate() {
		return $this->lastConnectDate;
	}

	public function setLastConnectDate($newLastConnectDate) {
		$this->lastConnectDate = $newLastConnectDate;
	}

	// activity
	public function getActivity() {
		return $this->activity;
	}

	public function setActivity($newActivity) {
		$this->activity = $newActivity;
	}

	// rights
	public function getRights() {
		return $this->rights;
	}

	public function setRights($newRights) {
		$this->rights = $newRights;
	}

	// knonwIds
	public function getKnonwIds() {
		return $this->knonwIds;
	}

	public function setKnonwIds($newKnonwIds) {
		$this->knonwIds = $newKnonwIds;
	}
	
	// photoFile
	public function getPhotoFile() {
		return $this->photoFile;
	}

	public function setPhotoFile($newPhotoFile) {
		$this->photoFile = $newPhotoFile;
	}

	// 
	public function getFirm() {
		return $this->firm;
	}

	public function setFirm($newFirm) {
		$this->firm = $newFirm;
	}

	// tel
	public function getTel() {
		return $this->tel;
	}

	public function setTel($newTel) {
		$this->tel = $newTel;
	}

	// fax
	public function getFax() {
		return $this->fax;
	}

	public function setFax($newFax) {
		$this->fax = $newFax;
	}

	// website
	public function getWebsite() {
		return $this->website;
	}

	public function setWebsite($newWebsite) {
		$this->website = $newWebsite;
	}

	// RegistDate
	public function getRegistDate() {
		return $this->registDate;
	}

	public function setRegistDate($newRegistDate) {
		$this->registDate = $newRegistDate;
	}
	
	// APIZEE AGENT ID
	public function getApizeeAgentId()
	{
		return $this->apizeeAgentId;
	}
	public function setApizeeAgentId($newApizeeAgentId)
	{
		$this->apizeeAgentId = $newApizeeAgentId;
	}	
	
	// SOCKET AGENT ID
	public function getSocketAgentId()
	{
		return $this->socketAgentId;
	}
	public function setSocketAgentId($newSocketAgentId)
	{
		$this->socketAgentId = $newSocketAgentId;
	}
	
	//apizeeApiKey
	public function getApizeeApiKey()
	{
		return $this->apizeeApiKey;
	}
	public function setApizeeApiKey($newApizeeApiKey)
	{
		$this->apizeeApiKey = $newApizeeApiKey;
	}
	
	//apizeeSiteId
	public function getApizeeSiteId()
	{
		return $this->apizeeSiteId;
	}
	public function setApizeeSiteId($newApizeeSiteId)
	{
		$this->apizeeSiteId = $newApizeeSiteId;
	}	
	
	//isAvailable
	public function getIsAvailable()
	{
		return $this->isAvailable;
	}
	public function setIsAvailable($newIsAvailable)
	{
		$this->isAvailable = $newIsAvailable;
	}
	//agentUrlList
	public function getAgentUrlList()
	{
		return $this->agentUrlList;
	}
	public function setAgentUrlList($newAgentUrlList)
	{
		$this->agentUrlList = $newAgentUrlList;
	}
	//muteAll
	public function getMuteAll()
	{
		return $this->muteAll;
	}
	public function setMuteAll($newMuteAll)
	{
		$this->muteAll = $newMuteAll;
	}
}
?>