| 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/ttjs.cz/dev/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;
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 . "') " . $typesSql), false, $allow_deleted);
}
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 toArray($exclude = array()) {
$data = parent::toArray($exclude);
unset($data["password"], $data["secret"], $data["metadata"]);
return $data;
}
}