166 lines
4.1 KiB
PHP
166 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Franzz\Spot;
|
|
use Franzz\Objects\PhpObject;
|
|
use Franzz\Objects\Db;
|
|
use \Settings;
|
|
|
|
class User extends PhpObject {
|
|
|
|
//DB Tables
|
|
const USER_TABLE = 'users';
|
|
|
|
//Clearance Levels
|
|
const USER_ACTIVE = 1;
|
|
const USER_INACTIVE = 0;
|
|
const CLEARANCE_USER = 0;
|
|
const CLEARANCE_ADMIN = 9;
|
|
|
|
//Cookie
|
|
const COOKIE_ID_USER = 'subscriber';
|
|
const COOKIE_DURATION = 60 * 60 * 24 * 365; //1 year
|
|
/**
|
|
* 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(
|
|
Db::getId(self::USER_TABLE) => 0,
|
|
'name' => '',
|
|
'email' => '',
|
|
'language' => '',
|
|
'timezone' => '',
|
|
'active' => self::USER_INACTIVE,
|
|
'clearance' => self::CLEARANCE_USER
|
|
);
|
|
$this->checkUserCookie();
|
|
}
|
|
|
|
public function getLang() {
|
|
return $this->asUserInfo['language'];
|
|
}
|
|
|
|
public function addUser($sEmail, $sLang, $sTimezone) {
|
|
$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'=>self::USER_ACTIVE));
|
|
|
|
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, 'timezone'=>$sTimezone, 'active'=>self::USER_ACTIVE),
|
|
array('email')
|
|
);
|
|
|
|
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
|
else {
|
|
$this->updateGravatar($iUserId, $sEmail);
|
|
$sDesc = 'lang:nl_subscribed';
|
|
$bSuccess = true;
|
|
}
|
|
}
|
|
|
|
//Set Cookie (valid 1 year)
|
|
if($bSuccess) {
|
|
$this->setUserId($iUserId);
|
|
$this->updateCookie(self::COOKIE_DURATION);
|
|
}
|
|
|
|
return Spot::getResult($bSuccess, $sDesc);
|
|
}
|
|
|
|
public function removeUser() {
|
|
$bSuccess = false;
|
|
$sDesc = '';
|
|
|
|
if($this->iUserId > 0) {
|
|
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $this->iUserId, array('active'=>self::USER_INACTIVE));
|
|
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
|
else {
|
|
$sDesc = 'lang:nl_unsubscribed';
|
|
$this->updateCookie(-60 * 60); //Set Cookie in the past, deleting it
|
|
$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 updateGravatar($iUserId, $sEmail) {
|
|
$sImage = ($sEmail != '')?@file_get_contents('https://www.gravatar.com/avatar/'.md5($sEmail).'.png?d=404&s=24'):'';
|
|
$this->oDb->updateRow(self::USER_TABLE, $iUserId, array('gravatar' => base64_encode($sImage)));
|
|
}
|
|
|
|
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(self::COOKIE_DURATION);
|
|
}
|
|
}
|
|
|
|
public function getUserId() {
|
|
return $this->iUserId;
|
|
}
|
|
|
|
public function setUserId($iUserId) {
|
|
$this->iUserId = 0;
|
|
|
|
$asUser = $this->getActiveUsersInfo($iUserId);
|
|
if(!empty($asUser)) {
|
|
$this->iUserId = $iUserId;
|
|
$this->asUserInfo = array_shift($asUser);
|
|
}
|
|
}
|
|
|
|
public function getUserInfo() {
|
|
return $this->asUserInfo;
|
|
}
|
|
|
|
public function getActiveUsersInfo($iUserId=-1) {
|
|
$asInfo = array(
|
|
'select' => array_keys($this->asUserInfo),
|
|
'from' => self::USER_TABLE,
|
|
'constraint'=> array('active'=>self::USER_ACTIVE)
|
|
);
|
|
if($iUserId != -1) $asInfo['constraint'][Db::getId(self::USER_TABLE)] = $iUserId;
|
|
|
|
if(!$this->checkUserClearance(self::CLEARANCE_ADMIN)) unset($asInfo['select']['clearance']);
|
|
|
|
return $this->oDb->selectRows($asInfo);
|
|
}
|
|
|
|
public function checkUserClearance($iClearance)
|
|
{
|
|
return ($this->asUserInfo['clearance'] >= $iClearance);
|
|
}
|
|
|
|
private function updateCookie($iDeltaTime) {
|
|
setcookie(self::COOKIE_ID_USER, ($iDeltaTime < 0)?'':$this->iUserId, array('samesite' => 'Lax', 'expires' => time() + $iDeltaTime));
|
|
}
|
|
}
|