Store gravatar image in DB
This commit is contained in:
@@ -83,7 +83,7 @@ class Spot extends Main
|
||||
Project::PROJ_TABLE => array('name', 'codename', 'active_from', 'active_to', 'timezone'),
|
||||
self::POST_TABLE => array(Db::getId(Project::PROJ_TABLE), Db::getId(User::USER_TABLE), 'name', 'content', 'site_time'),
|
||||
Media::MEDIA_TABLE => array(Db::getId(Project::PROJ_TABLE), 'filename', 'type', 'taken_on', 'posted_on', 'rotate', 'comment'),
|
||||
User::USER_TABLE => array('name', 'email', 'language', 'timezone', 'active'),
|
||||
User::USER_TABLE => array('name', 'email', 'gravatar', 'language', 'timezone', 'active'),
|
||||
self::MAP_TABLE => array('codename', 'geo_name', 'min_zoom', 'max_zoom', 'attribution'),
|
||||
self::MAPPING_TABLE => array(Db::getId(self::MAP_TABLE) , Db::getId(Project::PROJ_TABLE))
|
||||
),
|
||||
@@ -120,7 +120,8 @@ class Spot extends Main
|
||||
'geo_name' => "VARCHAR(100)",
|
||||
'min_zoom' => "TINYINT UNSIGNED",
|
||||
'max_zoom' => "TINYINT UNSIGNED",
|
||||
'attribution' => "VARCHAR(100)"
|
||||
'attribution' => "VARCHAR(100)",
|
||||
'gravatar' => "LONGTEXT"
|
||||
),
|
||||
'constraints' => array
|
||||
(
|
||||
@@ -361,7 +362,7 @@ class Spot extends Main
|
||||
private function getPosts()
|
||||
{
|
||||
$asInfo = array(
|
||||
'select' => array(Db::getFullColumnName(self::POST_TABLE, '*'), 'MD5(email) AS email_hash'),
|
||||
'select' => array(Db::getFullColumnName(self::POST_TABLE, '*'), 'gravatar'),
|
||||
'from' => self::POST_TABLE,
|
||||
'join' => array(User::USER_TABLE => Db::getId(User::USER_TABLE)),
|
||||
'constraint'=> array(Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId()),
|
||||
|
||||
54
inc/user.php
54
inc/user.php
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
class User extends PhpObject {
|
||||
|
||||
|
||||
//DB Tables
|
||||
const USER_TABLE = 'users';
|
||||
|
||||
|
||||
//Cookie
|
||||
const COOKIE_ID_USER = 'subscriber';
|
||||
const COOKIE_DURATION = 60 * 60 * 24 * 365; //1 year
|
||||
@@ -13,11 +13,11 @@ class User extends PhpObject {
|
||||
* @var Db
|
||||
*/
|
||||
private $oDb;
|
||||
|
||||
|
||||
//User Info
|
||||
private $iUserId;
|
||||
private $asUserInfo;
|
||||
|
||||
|
||||
public function __construct(Db &$oDb) {
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
$this->oDb = &$oDb;
|
||||
@@ -25,19 +25,19 @@ class User extends PhpObject {
|
||||
$this->asUserInfo = array(Db::getId(self::USER_TABLE)=>0, 'name'=>'', 'email'=>'', 'language'=>'', 'timezone'=>'', 'active'=>'0');
|
||||
$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'=>'1'));
|
||||
|
||||
|
||||
if($iUserId > 0) {
|
||||
//Log user in
|
||||
$sDesc = 'lang:nl_email_exists';
|
||||
@@ -48,6 +48,7 @@ class User extends PhpObject {
|
||||
$iUserId = $this->oDb->insertUpdateRow(self::USER_TABLE, array('email'=>$sEmail, 'language'=>$sLang, 'timezone'=>$sTimezone, 'active'=>'1'), array('email'));
|
||||
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
||||
else {
|
||||
$this->updateGravatar($iUserId, $sEmail);
|
||||
$sDesc = 'lang:nl_subscribed';
|
||||
$bSuccess = true;
|
||||
}
|
||||
@@ -58,14 +59,14 @@ class User extends PhpObject {
|
||||
$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'=>'0'));
|
||||
if($iUserId==0) $sDesc = 'lang:error_commit_db';
|
||||
@@ -76,41 +77,46 @@ class User extends PhpObject {
|
||||
}
|
||||
}
|
||||
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);
|
||||
$this->asUserInfo = array_shift($asUser);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getUserInfo() {
|
||||
return $this->asUserInfo;
|
||||
}
|
||||
|
||||
|
||||
public function getActiveUsersInfo($iUserId=-1) {
|
||||
$asInfo = array(
|
||||
'select' => array_keys($this->asUserInfo),
|
||||
@@ -118,11 +124,11 @@ class User extends PhpObject {
|
||||
'constraint'=> array('active'=>'1')
|
||||
);
|
||||
if($iUserId != -1) $asInfo['constraint'][Db::getId(self::USER_TABLE)] = $iUserId;
|
||||
|
||||
|
||||
return $this->oDb->selectRows($asInfo);
|
||||
}
|
||||
|
||||
|
||||
private function updateCookie($iDeltaTime) {
|
||||
setcookie(self::COOKIE_ID_USER, ($iDeltaTime < 0)?'':$this->iUserId, array('samesite' => 'Lax', 'expires' => time() + $iDeltaTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user