Skip to content

Instantly share code, notes, and snippets.

@leonardorifeli
Created June 21, 2014 17:51
Show Gist options
  • Save leonardorifeli/8076dfc1c53f3ffe4108 to your computer and use it in GitHub Desktop.
Save leonardorifeli/8076dfc1c53f3ffe4108 to your computer and use it in GitHub Desktop.
88bit - Classe de usuários
88bit.com.br
<?php
class Users extends DataBase {
public $id;
public $email;
public $birthDate;
public $fullName;
public $city;
public $state;
public $password;
public $facebook;
public $skype;
public $description;
public $image;
public $outherEmail;
public $slug;
public $phone;
public $phoneCell;
public $accessIp;
public $accessNavigator;
public $accessDateTime;
public $accessHostName;
public $accessComputer;
public $accessLocalIp;
public $accessTotal;
public $createComputer;
public $createLocalIp;
public $createIp;
public $createNavigator;
public $createDateTime;
public $createHostName;
public $createByFacebook;
public $keyId;
public $confirmEmail;
public $type;
public $active;
public $facebookId;
public $facebookLink;
public $facebookLocale;
public $facebookUsername;
public $facebookAccessToken;
protected $timeCookies = 172800;
protected $valuesOfInser = array();
protected $valuesOfBindValue = array();
private $usuarioEncontrado = FALSE;
public function __construct($email = "") {
if ($email != "") {
parent::$isQueryPersonalizada = TRUE;
parent::$haveBindValues = TRUE;
$selectQuery = "SELECT * FROM users WHERE Email = :email AND Active = 1 LIMIT 0,1";
parent::$bindValues['name'][] = ":email";
parent::$bindValues['value'][] = $email;
parent::setQuery($selectQuery);
$getInfo = parent::listar();
if ($getInfo !== FALSE) {
$configSystem = new ConfigSystem();
$this->id = $getInfo->Id;
$this->email = $getInfo->Email;
$this->birthDate = $getInfo->BirthDate;
$this->fullName = $getInfo->FullName;
$this->city = $getInfo->City;
$this->state = $getInfo->State;
$this->password = $getInfo->Password;
$this->facebook = $getInfo->Facebook;
$this->skype = $getInfo->Skype;
$this->description = $getInfo->Description;
$this->image = $getInfo->Image;
$this->outherEmail = $getInfo->OutherEmail;
$this->slug = $configSystem->getUrlSite() . $getInfo->Slug;
$this->phone = $getInfo->Phone;
$this->phoneCell = $getInfo->PhoneCell;
$this->accessIp = $getInfo->AccessIp;
$this->accessNavigator = $getInfo->AccessNavigator;
$this->accessDateTime = $getInfo->AccessDateTime;
$this->accessHostName = $getInfo->AccessHostName;
$this->accessComputer = $getInfo->AccessComputer;
$this->accessLocalIp = $getInfo->AccessLocalIp;
$this->accessTotal = $getInfo->AccessTotal;
$this->createComputer = $getInfo->CreateComputer;
$this->createLocalIp = $getInfo->CreateLocalIp;
$this->createIp = $getInfo->CreateIp;
$this->createNavigator = $getInfo->CreateNavigator;
$this->createDateTime = $getInfo->CreateDateTime;
$this->createHostName = $getInfo->CreateHostName;
$this->createByFacebook = $getInfo->CreateByFacebook;
$this->type = $getInfo->Type;
$this->active = $getInfo->Active;
$this->keyId = $getInfo->KeyId;
$this->confirmEmail = $getInfo->ConfirmEmail;
$this->facebookId = $getInfo->FacebookId;
$this->facebookAccessToken = $getInfo->FacebookAccessToken;
$this->facebookLink = $getInfo->FacebookLink;
$this->facebookUsername = $getInfo->FacebookUsername;
$this->facebookLocale = $getInfo->FacebookLocale;
$this->usuarioEncontrado = TRUE;
} else {
return FALSE;
}
}
}
public function getImage() {
$configSystem = new ConfigSystem();
$imagemUsuario = $configSystem->getUploadsDirectory() . $this->image;
if (file_exists($imagemUsuario)) {
$this->image = $configSystem->getUploadsCompleteDirectory() . $this->image;
} else {
$this->image = "{$configSystem->getUploadsCompleteDirectory()}Default.png";
}
return $this->image;
}
public function verifyExists($userEmail) {
if ($userEmail != "") {
$this->__construct($userEmail);
if ($this->usuarioEncontrado !== FALSE) {
return TRUE;
} else {
return FALSE;
}
}
}
public function createCookies($Email, $timeCookie) {
if ($this->verifyExists($Email) !== FALSE) {
$getInfo = new Users($Email);
$this->valuesOfInser = array("bindValue" => array());
$this->valuesOfBindValue = array("valuesBindValue" => array());
array_push($this->valuesOfInser['bindValue'], "Id");
array_push($this->valuesOfInser['bindValue'], "Email");
array_push($this->valuesOfInser['bindValue'], "BirthDate");
array_push($this->valuesOfInser['bindValue'], "FullName");
array_push($this->valuesOfInser['bindValue'], "City");
array_push($this->valuesOfInser['bindValue'], "State");
array_push($this->valuesOfInser['bindValue'], "Password");
array_push($this->valuesOfInser['bindValue'], "Facebook");
array_push($this->valuesOfInser['bindValue'], "Skype");
array_push($this->valuesOfInser['bindValue'], "Description");
array_push($this->valuesOfInser['bindValue'], "Image");
array_push($this->valuesOfInser['bindValue'], "OutherEmail");
array_push($this->valuesOfInser['bindValue'], "Slug");
array_push($this->valuesOfInser['bindValue'], "Phone");
array_push($this->valuesOfInser['bindValue'], "PhoneCell");
array_push($this->valuesOfInser['bindValue'], "AccessIp");
array_push($this->valuesOfInser['bindValue'], "AccessNavigator");
array_push($this->valuesOfInser['bindValue'], "AccessDateTime");
array_push($this->valuesOfInser['bindValue'], "AccessHostName");
array_push($this->valuesOfInser['bindValue'], "AccessComputer");
array_push($this->valuesOfInser['bindValue'], "AccessLocalIp");
array_push($this->valuesOfInser['bindValue'], "AccessTotal");
array_push($this->valuesOfInser['bindValue'], "CreateComputer");
array_push($this->valuesOfInser['bindValue'], "CreateLocalIp");
array_push($this->valuesOfInser['bindValue'], "CreateIp");
array_push($this->valuesOfInser['bindValue'], "CreateNavigator");
array_push($this->valuesOfInser['bindValue'], "CreateDateTime");
array_push($this->valuesOfInser['bindValue'], "CreateHostName");
array_push($this->valuesOfInser['bindValue'], "CreateByFacebook");
array_push($this->valuesOfInser['bindValue'], "FacebookLink");
array_push($this->valuesOfInser['bindValue'], "FacebookId");
array_push($this->valuesOfInser['bindValue'], "FacebookLocale");
array_push($this->valuesOfInser['bindValue'], "Facebookname");
array_push($this->valuesOfInser['bindValue'], "FacebookAccessToken");
array_push($this->valuesOfInser['bindValue'], "Type");
array_push($this->valuesOfInser['bindValue'], "Active");
array_push($this->valuesOfInser['bindValue'], "UpdateInfo");
/* Value of cookies */
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->id);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Email);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->BirthDate);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->FullName);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->City);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->State);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Password);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Facebook);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Skype);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Description);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Image);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->OutherEmail);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Slug);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Phone);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->PhoneCell);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessIp);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessNavigator);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessDateTime);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessHostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessComputer);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessLocalIp);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->AccessTotal);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateComputer);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateLocalIp);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateIp);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateNavigator);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateDateTime);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateHostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->CreateByFacebook);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->FacebookLink);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->FacebookId);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->FacebookLocale);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Facebookname);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->FacebookAccessToken);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Type);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->Active);
array_push($this->valuesOfBindValue['valuesBindValue'], $getInfo->UpdateInfo);
$timeCookie = (int) $timeCookie;
foreach ($this->valuesOfBindValue['valuesBindValue'] as $key => $value) {
$name = $this->valuesOfInser['bindValue'][$key];
createCookie($name, $value, $timeCookie);
}
return TRUE;
}
}
public function loggedInSystem($email, $password, $enviarEmail = 0) {
if (($email != "") && ($password != "")) {
parent::$isQueryPersonalizada = TRUE;
$select = sprintf("SELECT * FROM WHERE Email = '%s' AND Password = '%s' AND Active = 1 AND ConfirmEmail = 1 LIMIT 0, 1", $email, $password);
parent::setQuery($select);
$getInfo = parent::listar();
if ($getInfo !== FALSE) {
$Ip = $_SERVER['REMOTE_ADDR'];
$Navigator = $_SERVER['HTTP_USER_AGENT'];
$dateAndHour = date(
" Y/m/ d H:i:s");
$HostName = gethostbyaddr($Ip);
$IpLocal = gethostbyname($Ip);
$newTotalAccess = $getInfo->AccessTotal + 1;
$queryUpdate = sprintf("UPDATE SET AccessIp = '%s', AccessNavigator = '%s', AccessDateTime = '%s', AccessHostName = '%s', AccessComputer = '%s', AccessLocalIp = '%s', AccessTotal = %s WHERE Email = '%s'", $Ip, $Navigator, $dateAndHour, $HostName, $HostName, $IpLocal, $newTotalAccess, $email);
$executar = parent::executarQuery($queryUpdate);
if ($executar === FALSE) {
$configSystem = new ConfigSystem();
$emailSystem = new Email();
$emailSystem->setSendFor("Leonardo Rifeli ", "[email protected]");
$emailSystem->sendMail("Erro ao atualizar dados de usuário ", "Olá Leonardo Rifeli, a classe <strong>.class.php</strong> encontra-se com erros no método <strong>loggedInSystem();
</strong>");
}
if ($enviarEmail == 0) {
$emailClass = new Email ();
$emailClass->setSendFor($usuarioConfirma->Email, $usuarioConfirma->FullName);
$emailClass->sendMail("Bem-vindo ao 88bit", $emailClass->setMessage("email-cadastro/{$email}"));
}
/*
$select = sprintf("SELECT * FROM WHERE Email = '%s' AND Password = '%s' AND Active = 1 LIMIT 0, 1", $email, $password);
parent::setQuery($select);
$getInfo = parent::listar();
*/
if ((isset($_POST['mantenhaConectado']))) {
$this->timeCookies = (int) 1209600;
$timeCookie = (int) $this->timeCookies;
} else {
$timeCookie = (int) $this->timeCookies;
}
$this->criarCookies($getInfo->Email, $timeCookie);
createCookie("Logged", TRUE, $timeCookie);
return TRUE;
} else {
return FALSE;
}
}
exit;
}
public function getBindValues($createForFacebook, $type) {
/*
* Se o parâmetro " type" for:
* 1 = Return in string;
* 2 = Return in array;
*/
$this->valuesOfInser = array("bindValue" => array());
array_push($this->valuesOfInser['bindValue'], ":Id");
array_push($this->valuesOfInser['bindValue'], ":Email");
array_push($this->valuesOfInser['bindValue'], ":FullName");
array_push($this->valuesOfInser['bindValue'], ":City");
array_push($this->valuesOfInser['bindValue'], ":State");
array_push($this->valuesOfInser['bindValue'], ":Password");
array_push($this->valuesOfInser['bindValue'], ":Slug");
array_push($this->valuesOfInser['bindValue'], ":AccessIp");
array_push($this->valuesOfInser['bindValue'], ":AccessNavigator");
array_push($this->valuesOfInser['bindValue'], ":AccessDateTime");
array_push($this->valuesOfInser['bindValue'], ":AccessHostName");
array_push($this->valuesOfInser['bindValue'], ":AccessComputer");
array_push($this->valuesOfInser['bindValue'], ":AccessLocalIp");
array_push($this->valuesOfInser['bindValue'], ":AccessTotal");
array_push($this->valuesOfInser['bindValue'], ":CreateComputer");
array_push($this->valuesOfInser['bindValue'], ":CreateLocalIp");
array_push($this->valuesOfInser['bindValue'], ":CreateIp");
array_push($this->valuesOfInser['bindValue'], ":CreateNavigator");
array_push($this->valuesOfInser['bindValue'], ":CreateDateTime");
array_push($this->valuesOfInser['bindValue'], ":CreateHostName");
array_push($this->valuesOfInser['bindValue'], ":CreateByFacebook");
array_push($this->valuesOfInser['bindValue'], ":Type");
array_push($this->valuesOfInser['bindValue'], ":Active");
array_push($this->valuesOfInser['bindValue'], ":KeyId");
if ($createForFacebook != 0) {
array_push($this->valuesOfInser['bindValue'], ":FacebookId");
array_push($this->valuesOfInser['bindValue'], ":FacebookLink");
array_push($this->valuesOfInser['bindValue'], ":FacebookLocale");
array_push($this->valuesOfInser['bindValue'], ":Facebookname");
array_push($this->valuesOfInser['bindValue'], ":FacebookAccessToken");
}
$this->BindValues = "";
foreach ($this->valuesOfInser['bindValue'] as $key => $name) {
if ($key == 0) {
$this->BindValues .= "{$name}";
} else {
$this->BindValues .= ", {$name}";
}
}
if ($type == 1) {
return $this->BindValues;
} else {
return $this->valuesOfInser['bindValue'];
}
}
public function createNew($name, $email, $password, $city, $state, $type, $createForFacebook = 0) {
$dateAndHour = date("Y/m/d H:i:s");
$Id = 10;
$Ip = $_SERVER['REMOTE_ADDR'];
$IpLocal = gethostbyname($Ip);
$HostName = gethostbyaddr($Ip);
$Navigator = $_SERVER['HTTP_USER_AGENT'];
$Type = (int) $type;
$Active = 1;
$TotalAccess = 1;
$Name = $name;
$slug = generateSlugUser($name . "-" . $Id);
$KeyId = generateKeyIdUser($Id, $email);
if ($createForFacebook == 1) {
$FacebookId = "";
$FacebookEmail = "";
$FacebookLink = "";
$FacebookLocale = "";
$Facebookname = "";
$FacebookAccessToken = "";
}
$this->valuesOfBindValue = array("valuesBindValue" => array());
array_push($this->valuesOfBindValue['valuesBindValue'], $Id);
array_push($this->valuesOfBindValue['valuesBindValue'], $email);
array_push($this->valuesOfBindValue['valuesBindValue'], $Name);
array_push($this->valuesOfBindValue['valuesBindValue'], $city);
array_push($this->valuesOfBindValue['valuesBindValue'], $state);
array_push($this->valuesOfBindValue['valuesBindValue'], $password);
array_push($this->valuesOfBindValue['valuesBindValue'], $slug);
array_push($this->valuesOfBindValue['valuesBindValue'], $Ip);
array_push($this->valuesOfBindValue['valuesBindValue'], $Navigator);
array_push($this->valuesOfBindValue['valuesBindValue'], $dateAndHour);
array_push($this->valuesOfBindValue['valuesBindValue'], $HostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $HostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $IpLocal);
array_push($this->valuesOfBindValue['valuesBindValue'], $TotalAccess);
array_push($this->valuesOfBindValue['valuesBindValue'], $HostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $IpLocal);
array_push($this->valuesOfBindValue['valuesBindValue'], $Ip);
array_push($this->valuesOfBindValue['valuesBindValue'], $Navigator);
array_push($this->valuesOfBindValue['valuesBindValue'], $dateAndHour);
array_push($this->valuesOfBindValue['valuesBindValue'], $HostName);
array_push($this->valuesOfBindValue['valuesBindValue'], $createForFacebook);
array_push($this->valuesOfBindValue['valuesBindValue'], $Type);
array_push($this->valuesOfBindValue['valuesBindValue'], $Active);
array_push($this->valuesOfBindValue['valuesBindValue'], $KeyId);
$queryFacebook = "";
if ($createForFacebook != 0) {
array_push($this->valuesOfBindValue['valuesBindValue'], $FacebookId);
array_push($this->valuesOfBindValue['valuesBindValue'], $FacebookEmail);
array_push($this->valuesOfBindValue['valuesBindValue'], $FacebookLink);
array_push($this->valuesOfBindValue['valuesBindValue'], $FacebookLocale);
array_push($this->valuesOfBindValue['valuesBindValue'], $Facebookname);
array_push($this->valuesOfBindValue['valuesBindValue'], $FacebookAccessToken);
$queryFacebook = "`FacebookLink`, `FacebookId`, `FacebookLocale`, `Facebookname`, `FacebookAccesToken`, ";
}
$queryInsert = sprintf("INSERT INTO `users` (`Id`, `Email`, `FullName`, `City`, `State`, `Password`, `Slug`, `AccessIp`, `AccessNavigator`, `AccessDateTime`, `AccessHostName`, `AccessComputer`, `AccessLocalIp`, `AccessTotal`, `CreateComputer`, `CreateLocalIp`, `CreateIp`, `CreateNavigator`, `CreateDateTime`, `CreateHostName`, `CreateByFacebook`, {$queryFacebook} `Type`, `Active`, `KeyId`) VALUES({$this->getBindValues($createForFacebook, 1)})");
$insertNew = parent::insertRegister($queryInsert, $this->getBindValues($createForFacebook, 2), $this->valuesOfBindValue['valuesBindValue']);
if ($insertNew !== FALSE) {
$emailClass = new Email();
$emailClass->setSendFor($Name, $email);
$emailClass->setReplace("{nome}", $Name);
$emailClass->setReplace("{key}", $KeyId);
$emailClass->sendMail("Bem-vindo ao 88bit - Cadastro", $emailClass->setMessage("email-cadastro-confirmacao/{$email}", TRUE));
return TRUE;
} else {
return FALSE;
}
}
public function confirmEmail($KeyId, $type, $createForFacebook = 0) {
/*
* 1 = Confirmação de cadastro;
* 2 = Confirmação de alteração de senha;
*/
if ($type == 1) {
parent::$isQueryPersonalizada = TRUE;
$selectQuery = sprintf("SELECT * FROM WHERE KeyId = '%s' AND Active = 1", $KeyId);
parent::setQuery($selectQuery);
$getInfo = parent::listar();
if ($getInfo !== FALSE) :
$usuarioConfirma = new Users($getInfo->Email);
parent::$isQueryPersonalizada = TRUE;
$upadteQuery = sprintf("UPDATE SET .ConfirmEmail = 1 WHERE Email = '%s' AND Active = 1 AND KeyId = '%s'", $usuarioConfirma->Email, $usuarioConfirma->KeyId);
$atualizar = $this->executarQuery($upadteQuery);
if ($atualizar !== FALSE):
$this->loggedInSystem($getInfo->Email, $getInfo->Password);
return TRUE;
else:
return FALSE;
endif;
else:
return FALSE;
endif;
} else if ($type == 2) {
}
}
public function LogOut() {
$this->valuesOfInser = array("bindValue" => array());
array_push($this->valuesOfInser['bindValue'], "Id");
array_push($this->valuesOfInser['bindValue'], "Email");
array_push($this->valuesOfInser['bindValue'], "FullName");
array_push($this->valuesOfInser['bindValue'], "City");
array_push($this->valuesOfInser['bindValue'], "State");
array_push($this->valuesOfInser['bindValue'], "Password");
array_push($this->valuesOfInser['bindValue'], "Slug");
array_push($this->valuesOfInser['bindValue'], "AccessIp");
array_push($this->valuesOfInser['bindValue'], "AccessNavigator");
array_push($this->valuesOfInser['bindValue'], "AccessDateTime");
array_push($this->valuesOfInser['bindValue'], "AccessHostName");
array_push($this->valuesOfInser['bindValue'], "AccessComputer");
array_push($this->valuesOfInser['bindValue'], "AccessLocalIp");
array_push($this->valuesOfInser['bindValue'], "AccessTotal");
array_push($this->valuesOfInser['bindValue'], "CreateComputer");
array_push($this->valuesOfInser['bindValue'], "CreateLocalIp");
array_push($this->valuesOfInser['bindValue'], "CreateIp");
array_push($this->valuesOfInser['bindValue'], "CreateNavigator");
array_push($this->valuesOfInser['bindValue'], "CreateDateTime");
array_push($this->valuesOfInser['bindValue'], "CreateHostName");
array_push($this->valuesOfInser['bindValue'], "CreateByFacebook");
array_push($this->valuesOfInser['bindValue'], "Type");
array_push($this->valuesOfInser['bindValue'], "Active");
array_push($this->valuesOfInser['bindValue'], "Logged");
if ((isset($_COOKIE['BirthDate'])) && (isset($_COOKIE['Image'])) && (isset($_COOKIE['UpdateInfo']))) {
array_push($this->valuesOfInser ['bindValue'], " BirthDate"
);
array_push($this->valuesOfInser['bindValue'], "Image");
array_push($this->valuesOfInser ['bindValue'], " UpdateInfo
"
);
}
if ((isset($_COOKIE['FacebookId'])) && (isset($_COOKIE['FacebookLink'])) && (isset($_COOKIE['FacebookLocale'])) && (isset($_COOKIE['Facebookname'])) && (isset($_COOKIE['FacebookAccessToken']))) {
array_push($this->valuesOfInser['bindValue'], "FacebookId");
array_push($this->valuesOfInser['bindValue'], "FacebookLink");
array_push($this->valuesOfInser['bindValue'], "FacebookLocale");
array_push($this->valuesOfInser['bindValue'], "Facebookname");
array_push($this->valuesOfInser['bindValue'], "FacebookAccessToken");
}
foreach ($this->valuesOfInser['bindValue'] as $key => $name):
destroyCookie($name);
endforeach;
if (isset($_COOKIE['Logged'])) {
unset($_COOKIE['Logged']);
}
$this->Logged = FALSE;
createCookie("Logged", "0", $this->timeCookies);
if (!isset($_COOKIE['Email'])):
return TRUE;
else:
return FALSE;
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment