File: //opt/proundcontra.mosmuc.de/user.php
<?php
class User
{
public function __construct()
{
$this->isLoggedIn = false;
$this->userId = 0;
$this->voteData = 0;
$this->groups = null;
$this->ownGroups = null;
$this->adminGroups = null;
}
public function create($username, $email, $password, $sendConfirmation = true, $group = USER_GROUP_DEFAULT_SIGNUP)
{
global $sDB, $sTemplate;
$salt = salt();
$passwordHash = crypt($password, '$6$rounds=5000$'.$salt.'$');
$dateAdded = time();
$sDB->execUsers("
INSERT INTO `users`
(`userId`, `userName`, `email`, `group`, `password`, `salt`, `dateAdded`)
VALUES
(NULL,
'".mysqli_real_escape_string($sDB->getLink(), $username)."',
'".mysqli_real_escape_string($sDB->getLink(), $email)."',
'".i($group)."',
'".mysqli_real_escape_string($sDB->getLink(), $passwordHash)."',
'".mysqli_real_escape_string($sDB->getLink(), $salt)."',
'".i($dateAdded)."');
");
if (mysqli_affected_rows($sDB->getLink())) {
$this->userId = mysqli_insert_id($sDB->getLink());
$this->userName = $username;
$this->email = $email;
$this->password = $passwordHash;
$this->salt = $salt;
$this->dateAdded = $dateAdded;
$this->group = $group;
if ($sendConfirmation) {
$confirmationCode = md5(time()+rand());
$confirmationLink = $sTemplate->getRoot()."confirmation.php?userId=".$this->userId."&confirmationCode=".$confirmationCode;
$this->addConfirmationCode("CONFIRMATION_TYPE_EMAIL", $confirmationCode);
$subject = $sTemplate->getString("SIGNUP_CONFIRMATION_EMAIL_SUBJECT");
$message = $sTemplate->getString(
"SIGNUP_CONFIRMATION_EMAIL_BODY",
["[USERNAME]", "[PASSWORD]", "[CONFIRMATION_LINK]"],
[$this->userName, $password, $confirmationLink]
);
$mail = new HTMLMail($this->email, $this->email, SENDMAIL_FROM_NAME, SENDMAIL_FROM);
$mail->buildMessage($subject, $message);
$mail->sendmail();
}
return true;
} else {
return false;
}
}
public function load($userId)
{
global $sDB, $sSession;
$res = $sDB->execUsers("
SELECT *
FROM `users`
WHERE `userId` = '".i($userId)."'
LIMIT 1;
");
if (!mysqli_num_rows($res)) {
return false;
}
$row = mysqli_fetch_object($res);
$this->userId = $userId;
$this->userName = $row->userName;
$this->email = $row->email;
$this->group = $row->group;
//$this->entitled = $row->entitled;
//$this->verified = $row->verified;
$this->password = $row->password;
$this->salt = $row->salt;
$this->dateAdded = $row->dateAdded;
$this->scoreQuestions = $row->scoreQuestions;
$this->scoreArguments = $row->scoreArguments;
if ($userId == $sSession->getVal('userId')) {
$this->isLoggedIn = true;
}
return true;
}
public function login($password, $forceLogin = false)
{
global $sSession;
if (!$this->userId) {
return false;
}
$passwordHash = crypt($password, '$6$rounds=5000$'.$this->salt.'$');
if ($passwordHash === $this->password || $forceLogin) {
$this->isLoggedIn = true;
$sSession->setVal('userId', $this->userId);
return true;
}
return false;
}
public function isLoggedIn()
{
return $this->isLoggedIn;
}
public function getSignupDate()
{
return date("d.m.Y", $this->dateAdded);
}
public function getUserName()
{
if (POSTS_ANON) {
return "XXXXX";
}
return $this->userName;
}
public function getUserStatus()
{
global $sTemplate;
if (!$this->entitled && !$this->verified) {
return $sTemplate->getString("NOT_VERIFIED_NOT_ENTITLED");
} elseif (!$this->entitled && $this->verified) {
return $sTemplate->getString("VERIFIED_NOT_ENTITLED")." ".$this->verified;
} elseif ($this->entitled && !$this->verified) {
return $sTemplate->getString("NOT_VERIFIED_ENTITLED");
} else {
return $sTemplate->getString("VERIFIED_ENTITLED")." ".$this->verified;
}
}
public function getUserId()
{
return $this->userId;
}
public function getScoreQuestions()
{
return $this->scoreQuestions;
}
public function getScoreArguments()
{
return $this->scoreArguments;
}
public function getVoteState($questionId, $argumentId)
{
global $sDB;
// If user is not logged in, no votes
if (!$this->isLoggedIn()) {
return VOTE_NONE;
}
// If user vote data is not loaded yet, load it
if (!$this->voteData) {
$this->voteData = [];
$res = $sDB->exec("
SELECT *
FROM `user_votes`
WHERE `userId` = '".i($this->userId)."';
");
while ($row = mysqli_fetch_object($res)) {
if (!isset($this->voteData[$row->questionId]) || !is_array($this->voteData[$row->questionId])) {
$this->voteData[$row->questionId] = [];
}
$this->voteData[$row->questionId][$row->argumentId] = $row->vote;
}
}
if (isset($this->voteData[$questionId]) && isset($this->voteData[$questionId][$argumentId])) {
return $this->voteData[$questionId][$argumentId];
}
return VOTE_NONE;
}
public function recentQuestions($limit = 3)
{
global $sDB;
$ret = [];
$res = $sDB->exec("
SELECT *
FROM `questions`
WHERE `userId` = '".i($this->userId)."'
LIMIT ".i($limit).";
");
while ($row = mysqli_fetch_object($res)) {
$q = new Question($row->questionId, $row);
$ret[] = $q;
}
return $ret;
}
public function recentArguments($limit = 3)
{
global $sDB;
$ret = [];
$res = $sDB->exec("
SELECT *
FROM `arguments`
WHERE `userId` = '".i($this->userId)."'
LIMIT ".i($limit).";
");
while ($row = mysqli_fetch_object($res)) {
$a = new Argument($row->argumentId, $row);
$ret[] = $a;
}
return $ret;
}
public function setPassword($old, $new, $forceUpdate = false)
{
global $sDB;
$passwordHash = crypt($old, '$6$rounds=5000$'.$this->salt.'$');
$passwordHashNew = crypt($new, '$6$rounds=5000$'.$this->salt.'$');
if ($passwordHash === $this->password || $forceUpdate) {
$this->password = $passwordHashNew;
$sDB->execUsers("
UPDATE `users`
SET `password` = '".mysqli_real_escape_string($sDB->getLink(), $passwordHashNew)."'
WHERE `userId` = '".i($this->userId)."'
LIMIT 1;
");
return true;
}
return false;
}
public function setUserGroup($group)
{
global $sDB;
$sDB->execUsers("
UPDATE `users`
SET `group` = '".i($group)."'
WHERE `userId` = '".i($this->userId)."'
LIMIT 1;
");
return true;
}
public function group()
{
return $this->group;
}
public function url()
{
global $sTemplate;
return $sTemplate->getProfileLink($this->userId);
}
public function shortUrl()
{
global $sTemplate;
$id = new BaseConvert($this->userId);
return $sTemplate->getShortUrlBase()."u".$id->val();
}
public function addConfirmationCode($type, $code)
{
global $sDB;
$sDB->exec("
DELETE FROM `confirmation_codes`
WHERE `userId` = '".i($this->userId)."'
AND `type` = '".mysqli_real_escape_string($sDB->getLink(), $type)."'
LIMIT 1;
");
$sDB->exec("
INSERT INTO `confirmation_codes`
(`confirmationId`, `userId`, `type`, `code`, `dateAdded`)
VALUES
(NULL,
'".i($this->userId)."',
'".mysqli_real_escape_string($sDB->getLink(), $type)."',
'".mysqli_real_escape_string($sDB->getLink(), $code)."',
'".time()."');
");
}
public function reqPass()
{
global $sTemplate;
$confirmationCode = md5(time()+rand());
$confirmationLink = $sTemplate->getRoot()."confirmation.php?userId=".$this->userId."&confirmationCode=".$confirmationCode;
$confirmationLink = "<a href='".$confirmationLink."'>".$confirmationLink."</a>";
$this->addConfirmationCode("CONFIRMATION_TYPE_PASS_REQUEST", $confirmationCode);
$subject = $sTemplate->getString("ACCOUNT_FORGOT_PASSWORD_EMAIL_HEADER_STEP1");
$message = $sTemplate->getString(
"ACCOUNT_FORGOT_PASSWORD_EMAIL_BODY_STEP1",
["[NEW_PASS_CONFIRM_LINK]"],
[$confirmationLink]
);
$mail = new HTMLMail($this->email, $this->email, SENDMAIL_FROM_NAME, SENDMAIL_FROM);
$mail->buildMessage($subject, $message);
$mail->sendmail();
}
public function reqPassStep2()
{
global $sDB, $sTemplate;
$password = generatePassword();
$this->setPassword('', $password, true);
$subject = $sTemplate->getString("ACCOUNT_FORGOT_PASSWORD_EMAIL_HEADER_STEP2");
$message = $sTemplate->getString(
"ACCOUNT_FORGOT_PASSWORD_EMAIL_BODY_STEP2",
["[PASSWORD]"],
[$password]
);
$mail = new HTMLMail($this->email, $this->email, SENDMAIL_FROM_NAME, SENDMAIL_FROM);
$mail->buildMessage($subject, $message);
$mail->sendmail();
}
public function follow($qId)
{
global $sDB;
$res = $sDB->exec("
SELECT *
FROM `notifications`
WHERE `questionId` = '".i($qId)."'
AND `userId` = '".i($this->getUserId())."'
LIMIT 1;
");
if (mysqli_num_rows($res)) {
return false;
}
$sDB->exec("
INSERT INTO `notifications`
(`notificationId`, `userId`, `questionId`, `flags`, `dateAdded`)
VALUES
(NULL,
'".i($this->getUserId())."',
'".i($qId)."',
'0',
'".i(time())."');
");
return true;
}
public function unfollow($qId)
{
global $sDB;
$sDB->exec("
DELETE FROM `notifications`
WHERE `questionId` = '".i($qId)."'
AND `userId` = '".i($this->getUserId())."'
LIMIT 1;
");
return true;
}
public function isFollowing($qId)
{
global $sDB;
$res = $sDB->exec("
SELECT *
FROM `notifications`
WHERE `questionId` = '".i($qId)."'
AND `userId` = '".i($this->getUserId())."'
LIMIT 1;
");
return (mysqli_num_rows($res) > 0);
}
private $isLoggedIn;
private $userId;
private $userName;
private $entitled;
private $verified;
private $email;
private $password;
private $salt;
private $dateAdded;
private $group;
private $groups;
private $ownGroups;
private $adminGroups;
private $voteData;
private $scoreQuestions;
private $scoreArguments;
}
?>