| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/app/classes/ |
Upload File : |
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of User
*
* @author Maroš
*/
namespace app\classes;
abstract class User extends PDOElement {
protected static $loggedUser;
protected $accessibleUsers;
public function __construct($table, $email, $id_column = "email", $allow_deleted = false, $allow_disabled = false, $types = array()) {
$this->uniqueColumn = "email";
if (count($types) > 0) {
$typesSql = " AND type IN('" . implode("', '", $types) . "')";
} else {
$typesSql = "";
}
parent::__construct($email, $table, array($id_column => $email), $id_column, ($allow_disabled ? ($typesSql === "" ? false : $typesSql) : "(state='" . \app\constants\State::ACTIVE . "' OR state='" . \app\constants\State::CREATED . "' OR state='" . \app\constants\State::APPROVED . "' OR state='" . \app\constants\State::DISMISSED . "') " . $typesSql), false, $allow_deleted);
if (key_exists('public_id', $this->element) && strlen($this->element['public_id']) === 0) {
$this->generatePublicId();
}
}
public static function getLoggedUser($types = array()) {
if (self::$loggedUser instanceof User) {
throw new \Exception(\app\constants\ResponseCodes::getMessageForCode(\app\constants\ResponseCodes::UNAUTHORIZED), \app\constants\ResponseCodes::UNAUTHORIZED);
}
return self::$loggedUser;
}
public function getAuthToken() {
$token = \App::getSecret() . substr(time(), 0, strlen(time()) - 4) . $this->_get("secret") . $this->_get("password") . $this->_get("type");
return $this->_get("email") . "|" . hash("sha256", $token);
}
private function getPrevAuthToken() {
$token = \App::getSecret() . (intval(substr(time(), 0, strlen(time()) - 4)) - 1) . $this->_get("secret") . $this->_get("password") . $this->_get("type");
return $this->_get("email") . "|" . hash("sha256", $token);
}
public function login($password) {
if (password_verify($password, $this->_get("password"))) {
if (key_exists("last_login", $this->element)) {
$this->update(array("last_login" => date("Y-m-d H:i:s")));
}
return $this->getAuthToken();
}
return false;
}
public function authorize($token) {
if (hash_equals($this->getAuthToken(), $token) || hash_equals($this->getPrevAuthToken(), $token)) {
return true;
}
return false;
}
protected static function prepareNewUser($data) {
if (!filter_var($data["email"], FILTER_VALIDATE_EMAIL)) {
throw new \Exception("Neplatný e-mail", \app\constants\ResponseCodes::INVALID_EMAIL);
}
if (strlen($data["password"]) < 8 || !preg_match("#[0-9]+#", $data["password"]) || is_numeric($data["password"])) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_PASSWORD, \app\constants\ResponseCodes::INVALID_PASSWORD);
}
if ($data["password"] !== $data["password_retype"]) {
throw new \Exception(\app\constants\ResponseCodes::PASSWORD_MISMATCH, \app\constants\ResponseCodes::PASSWORD_MISMATCH);
}
$userData = array(
"email" => isset($data["email"]) ? $data["email"] : null,
"password" => password_hash($data["password"], PASSWORD_BCRYPT),
"secret" => generateRandomString(40),
"created" => date("Y-m-d H:i:s"),
"last_update" => date("Y-m-d H:i:s"),
"first_name" => isset($data["first_name"]) ? $data["first_name"] : null,
"last_name" => isset($data["last_name"]) ? $data["last_name"] : null,
"registration_ip" => get_ip_address()
);
return $userData;
}
public function isAdmin() {
return $this->_get("type") === \app\constants\UserType::ADMIN;
}
public function isSuperAdmin() {
return $this->isAdmin() && intval($this->_get('super_admin')) === 1;
}
public function isCustomer() {
return $this->_get("type") === \app\constants\UserType::CUSTOMER;
}
public function getAdminRightsSQL($leftOr = false, $rightOr = false) {
if ($this->isAdmin()) {
return " " . ($leftOr ? "OR " : "") . "1=1 " . ($rightOr ? "OR " : "");
}
return "";
}
public function checkUserType($acceptableTypes, $throwEx = true) {
if (is_array($acceptableTypes)) {
if (!in_array($this->_get("type"), $acceptableTypes)) {
if ($throwEx) {
throw new \Exception("inappropriate user type", \app\constants\ResponseCodes::INVALID_INPUT);
}
return false;
}
return true;
}
if ($acceptableTypes !== $this->_get("type")) {
if ($throwEx) {
throw new \Exception("inappropriate user type", \app\constants\ResponseCodes::INVALID_INPUT);
}
return false;
}
return true;
}
/**
* @return PDOCollection
*/
public function getAccessibleUsers() {
if ($this->accessibleUsers === null) {
$cnf = new \modules\users\code\UsersConfig;
$accessibleRoles = $cnf->getConfig(\modules\users\code\UsersConfig::CONF_USERS_TYPES_MANAGEMENT)[$this->_get("type")];
$this->accessibleUsers = new PDOCollection("users", "id", false, array(
"ACTIVE" => \app\constants\State::ACTIVE,
"user_id" => $this->_get("id")
), " WHERE state=:ACTIVE "
. " AND ("
. " type IN('" . implode("', '", $accessibleRoles) . "') "
. $this->getAdminRightsSQL(true)
. ") OR id=:user_id ORDER BY last_name, type ASC"
);
}
return $this->accessibleUsers;
}
/**
* @return PDOCollection
*/
public function getAccessibleUsersSwapAccount() {
$allowedTypes = array_values(array_filter(
\app\constants\UserType::getConstants(),
static function ($typeCode) {
return $typeCode !== \app\constants\UserType::ADMIN;
}
));
$placeholders = [];
$bind = ['ACTIVE' => \app\constants\State::ACTIVE];
foreach ($allowedTypes as $index => $typeCode) {
$key = 'type_' . $index;
$placeholders[] = ':' . $key;
$bind[$key] = $typeCode;
}
return new PDOCollection(
'users',
'id',
false,
$bind,
' WHERE state=:ACTIVE AND type IN (' . implode(', ', $placeholders) . ') ORDER BY type ASC, last_name ASC, first_name ASC, email ASC'
);
}
public function toArray($exclude = array()) {
$data = parent::toArray($exclude);
unset($data["password"], $data["secret"], $data["metadata"]);
return $data;
}
public function update(array $dataArray, array $exclude = array()) {
if (isset($dataArray["state"])) {
if ($dataArray["state"] === \app\constants\State::ACTIVE) {
if (intval($this->getMetadata("enableNewsletter")) === 1) {
\modules\newsletter\code\Newsletter::add($this->_get("email"));
} else {
\modules\newsletter\code\Newsletter::remove($this->_get("email"));
}
} else {
\modules\newsletter\code\Newsletter::remove($this->_get("email"));
}
}
parent::update($dataArray, $exclude);
}
public function generatePublicId() {
try {
$this->update(['public_id' => uuidPrettyUnsafe()]);
} catch (\Exception $ex) {
$this->generatePublicId();
}
}
}