118 lines
2.8 KiB
PHP
118 lines
2.8 KiB
PHP
<?php
|
|
|
|
class User extends PhpObject {
|
|
|
|
//DB Tables
|
|
const USER_TABLE = 'users';
|
|
|
|
//Cookie
|
|
const COOKIE_ID_USER = 'subscriber';
|
|
|
|
/**
|
|
* Database Handle
|
|
* @var Db
|
|
*/
|
|
private $oDb;
|
|
|
|
//User Info
|
|
private $iUserId;
|
|
private $asUserInfo;
|
|
|
|
public function __construct(Db &$oDb) {
|
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
|
$this->oDb = &$oDb;
|
|
$this->iUserId = 0;
|
|
$this->asUserInfo = array('name'=>'', 'email'=>'', 'language'=>'', 'active'=>'0');
|
|
$this->checkUserCookie();
|
|
}
|
|
|
|
public function getLang() {
|
|
return $this->asUserInfo['language'];
|
|
}
|
|
|
|
public function addUser($sEmail, $sLang) {
|
|
$bSuccess = false;
|
|
$sDesc = '';
|
|
$sEmail = trim($sEmail);
|
|
|
|
//Check Email availability
|
|
$iUserId = $this->oDb->selectValue(self::USER_TABLE, Db::getId(self::USER_TABLE), array('email'=>$sEmail, 'active'=>'1'));
|
|
|
|
if($iUserId > 0) {
|
|
//Log user in
|
|
$sDesc = 'lang:nl_email_exists';
|
|
$bSuccess = true;
|
|
}
|
|
else {
|
|
//Add/Reactivate user
|
|
$iUserId = $this->oDb->insertUpdateRow(self::USER_TABLE, array('email'=>$sEmail, 'language'=>$sLang, 'active'=>'1'), array('email'));
|
|
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
|
else {
|
|
$sDesc = 'lang:nl_subscribed';
|
|
$bSuccess = true;
|
|
}
|
|
}
|
|
|
|
//Set Cookie (valid 1 year)
|
|
if($bSuccess) {
|
|
$this->setUserId($iUserId);
|
|
$this->updateCookie();
|
|
}
|
|
|
|
return Spot::getResult($bSuccess, $sDesc, $this->getUserInfo());
|
|
}
|
|
|
|
public function removeUser() {
|
|
$bSuccess = false;
|
|
$sDesc = '';
|
|
|
|
if($this->iUserId > 0) {
|
|
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $this->iUserId, array('active'=>'0'));
|
|
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
|
else {
|
|
$sDesc = 'lang:nl_unsubscribed';
|
|
$bSuccess = true;
|
|
}
|
|
}
|
|
else $sDesc = 'lang:nl_unknown_email';
|
|
|
|
return Spot::getResult($bSuccess, $sDesc);
|
|
}
|
|
|
|
public function updateNickname($sNickname) {
|
|
if($this->iUserId > 0 && $sNickname!='') $this->oDb->updateRow(self::USER_TABLE, $this->iUserId, array('name'=>$sNickname));
|
|
}
|
|
|
|
private function checkUserCookie() {
|
|
if(isset($_COOKIE[self::COOKIE_ID_USER])){
|
|
$this->setUserId($_COOKIE[self::COOKIE_ID_USER]);
|
|
|
|
//Extend cookie life
|
|
if($this->iUserId > 0) $this->updateCookie();
|
|
}
|
|
}
|
|
|
|
public function getUserId() {
|
|
return $this->iUserId;
|
|
}
|
|
|
|
public function getUserInfo() {
|
|
$asUserInfo = $this->asUserInfo;
|
|
$asUserInfo[Db::getId(self::USER_TABLE)] = $this->iUserId;
|
|
return $asUserInfo;
|
|
}
|
|
|
|
public function setUserId($iUserId) {
|
|
$this->iUserId = 0;
|
|
|
|
$asUser = $this->oDb->selectRow(self::USER_TABLE, array(Db::getId(self::USER_TABLE)=>$iUserId, 'active'=>'1'), array_keys($this->asUserInfo));
|
|
if(!empty($asUser)) {
|
|
$this->iUserId = $iUserId;
|
|
$this->asUserInfo = $asUser;
|
|
}
|
|
}
|
|
|
|
private function updateCookie() {
|
|
setcookie(self::COOKIE_ID_USER, $this->iUserId, time() + 60 * 60 * 24 * 365);
|
|
}
|
|
} |