| 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/modules/users/api/ |
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.
*/
namespace modules\users\api;
/**
* Description of Users
*
* @author Maroš
*/
/**
* @includeInDoc true
* @description "Unauthenticated users endpoints."
* @requiresAuthorization "No."
*/
class Users extends \app\classes\ApiClass {
protected $userType;
public function __construct() {
$this->requiresAuth = false;
$this->userType = \app\constants\UserType::CUSTOMER;
parent::__construct();
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["first_name", "last_name", "email", "password", "password_retype", "gdpr_accept", "g-recaptcha-response"]
*/
public function register($api = true) {
$t = \app\classes\Translator::getInstance();
if ($api) {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
}
if (intval($this->urlReader->getPost("gdpr_accept")) !== 1) {
throw new \Exception("gdpr_accept", \app\constants\ResponseCodes::INVALID_INPUT);
}
$_POST["type"] = $this->userType;
$_POST["state"] = \app\constants\State::CREATED;
try {
$postData = $this->urlReader->getPost();
array_map("toNonHtml", $postData);
$invitationToken = trim((string) ($postData['token'] ?? ''));
if ($invitationToken !== '') {
$user = \app\classes\UserInvitationRegistration::completeRegistration($invitationToken, $postData);
if ($api) {
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Registrácia prebehla úspešne."),
"text" => $t->_get("Môžete sa prihlásiť."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
} else {
return $user;
}
return;
}
$pairedUser = \app\classes\UserInvitationRegistration::tryCompleteRegistrationByEmail($postData);
if ($pairedUser !== null) {
if ($api) {
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Registrácia prebehla úspešne."),
"text" => $t->_get("Môžete sa prihlásiť."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
} else {
return $pairedUser;
}
return;
}
$user = \app\classes\BasicUser::newUser($postData, []);
mkdir(DOCUMENT_ROOT . "uploads/private/uploads/users/" . $user->_get("folder"));
$this->sendRegistrationConfirmationEmail($user);
if ($api) {
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Registrácia prebehla úspešne."),
"text" => $t->_get("Registrácia prebehla úspešne. Na Vašu e-mailovú adresu bol zaslaný e-mail s odkazom pre aktiváciu účtu."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
} else {
return $user;
}
} catch (\InvalidArgumentException $ex) {
throw new \Exception($ex->getMessage(), \app\constants\HttpReponseCodes::HTTP_BAD_REQUEST, $ex);
} catch (\Exception $ex) {
if ($ex->getCode() === 1062 || intval($ex->getCode()) === 23000) {
$this->restApi->setCode(\app\constants\ResponseCodes::ALREADY_EXISTS);
$this->restApi->setHttpResponseCode(\app\constants\ResponseCodes::ALREADY_EXISTS);
$this->restApi->setCustomMessage("Používateľ s touto e-mailovou adresou už existuje.");
} else {
throw new \Exception($ex->getMessage(), intval($ex->getCode()), $ex->getPrevious());
}
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["token", "g-recaptcha-response"]
*/
public function verifyRegistration() {
$t = \app\classes\Translator::getInstance();
$db = \app\classes\PDODatabase::getInstance();
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
try {
$email = $db->getValue("SELECT email FROM users WHERE secret=:secret", array(
"secret" => $this->urlReader->getPost("token"),
));
$user = new \app\classes\BasicUser($email);
$expectedState = $user->getType() === \app\constants\UserType::CUSTOMER ? \app\constants\State::CREATED : \app\constants\State::CREATED;
if ($user->_get("state") !== $expectedState) {
throw new \Exception($t->_get("Neplatný odkaz pre aktiváciu účtu."), \app\constants\ResponseCodes::ERROR);
}
} catch (\Exception $ex) {
throw new \Exception($t->_get("Neplatný odkaz pre aktiváciu účtu."), \app\constants\ResponseCodes::ERROR);
}
$user->update(array(
"state" => \app\constants\State::ACTIVE,
"secret" => generateRandomString(40)
));
if ($user->getType() === \app\constants\UserType::CUSTOMER) {
try {
$db->sqlCommand("UPDATE dpfob SET users_id=:users_id WHERE users_id IS NULL AND TRIM(email)=TRIM(:email)", [
"users_id" => $user->_get("id"),
"email" => $user->_get("email")
]);
} catch (\Exception $ex) {
}
}
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Aktivácia účtu prebehla úspešne"),
"text" => $t->_get("Môžete sa prihlásiť."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
}
protected function notifyAdminNewRegistration(\app\classes\BasicUser $user) {
$translator = \app\classes\Translator::getInstance();
$vars = array(
"verification_link" => array(
"translate" => false,
"value" => $this->app->getBaseUrl() . "pouzivatelia/?approveRegistration=" . $user->_get("email")
),
"dismiss_link" => array(
"translate" => false,
"value" => $this->app->getBaseUrl() . "pouzivatelia/?dismissRegistration=" . $user->_get("email")
),
"page_name" => array(
"translate" => false,
"value" => $this->app->getConfig(\app\constants\Config::PAGE_NAME)
)
);
foreach ($user->toArray() as $key => $val) {
$vars[$key] = array(
"translate" => false,
"value" => $val
);
}
$vars["type"] = array("translate" => false, "value" => \app\constants\UserType::getStateMessage($user->_get("type")));
$msg = $translator->convertVarsText($translator->_get("email-registration_notification"), $vars);
$this->app->sendMultipleEmails(implode(",", $this->app->getNotificationEmails()), $translator->convertVarsText($translator->_get("email-registration_notification-subject"), $vars), $msg);
}
protected function sendRegistrationConfirmationEmail(\app\classes\BasicUser $user) {
$translator = \app\classes\Translator::getInstance();
$vars = array(
"verification_link" => array(
"translate" => false,
"value" => $this->app->getBaseUrl() . $this->app->getPageAddress(\app\constants\Config::REGISTRATION_VERIFICATION_PAGE) . "/" . $user->_get("secret") . "/"
),
"page_name" => array(
"translate" => false,
"value" => $this->app->getConfig(\app\constants\Config::PAGE_NAME)
)
);
foreach ($user->toArray() as $key => $val) {
$vars[$key] = array(
"translate" => false,
"value" => $val
);
}
$vars["type"] = array("translate" => false, "value" => \app\constants\UserType::getStateMessage($user->_get("type")));
$msg = $translator->convertVarsText($translator->_get("email-registration_verification"), $vars);
$this->app->sendEmailNative($user->_get("email"), $translator->convertVarsText($translator->_get("email-registration_verification-subject"), $vars), $msg);
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["email", "password", "g-recaptcha-response"]
*/
public function login() {
try {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
$user = new \app\classes\BasicUser($this->urlReader->getPost("email"));
if ($user->_get("state") !== \app\constants\State::ACTIVE) {
throw new \Exception("Login failed");
}
$login = $user->login($this->urlReader->getPost("password"));
if ($login === false) {
throw new \Exception("Login failed");
}
$this->restApi->ajaxResponse(array("logged" => true));
} catch (\Exception $ex) {
$this->restApi->setCode(\app\constants\ResponseCodes::LOGIN_FAILED);
$this->restApi->setHttpResponseCode(\app\constants\ResponseCodes::LOGIN_FAILED);
$this->restApi->ajaxResponse(array("logged" => false));
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["email"]
*/
public function forgottenPassword() {
$t = \app\classes\Translator::getInstance();
try {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
$user = new \app\classes\BasicUser($this->urlReader->getPost("email"));
if ($user->_get('state') === \app\constants\State::CREATED) {
$this->sendRegistrationConfirmationEmail($user);
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Váš účet nie je aktívny!"),
"text" => $t->_get("Na Vašu e-mailovú adresu Vám bol zaslaný odkaz pre aktiváciu účtu."),
));
}
$domain = $this->app->getConfig(\app\constants\Config::DOMAIN);
$translator = \app\classes\Translator::getInstance();
$header = array(
'From: "' . $domain . '" <forgotten.password@' . $domain . '>',
'Reply-To: "No Reply" <noreply@' . $domain . '>',
"MIME-Version: 1.0",
"Content-type:text/html;charset=UTF-8"
);
$headers = implode("\r\n", $header);
$msg = $translator->convertVarsText($translator->_get("email-user_forgotten_password_confirmation"), array(
"link" => array(
"translate" => false,
"value" => $this->app->getBaseUrl() . $this->app->getPageAddress(\app\constants\Config::FORGOTTEN_PASSWORD_CONFIRM_PAGE) . "/" . $user->_get("email") . "/" . md5($user->_get("last_login") . $this->app->getSecret() . $user->_get("email")) . md5($user->_get("secret") . $this->app->getSecret()) . "/"
)
));
$this->app->sendEmailNative($user->_get("email"), $translator->_get("User - forgotten password"), $msg, $headers);
} catch (\Exception $ex) {
if ($ex->getCode() === \app\constants\HttpReponseCodes::HTTP_BAD_REQUEST) {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters []
*/
public function forgottenPasswordConfirm() {
$t = \app\classes\Translator::getInstance();
try {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
$user = new \app\classes\BasicUser($this->urlReader->getPost("email"));
if ($this->urlReader->getPost("email") === false || $this->urlReader->getPost("token") === false || md5($user->_get("last_login") . $this->app->getSecret() . $user->_get("email")) . md5($user->_get("secret") . $this->app->getSecret()) !== $this->urlReader->getPost("token")) {
throw new \Exception(\app\constants\ResponseCodes::UNAUTHORIZED, \app\constants\ResponseCodes::UNAUTHORIZED);
}
$password = generateRandomString(12) . rand(0, 9) . rand(0, 9);
$user->update(array(
"password" => password_hash($password, PASSWORD_BCRYPT),
"secret" => generateRandomString(40)
));
$domain = $this->app->getConfig(\app\constants\Config::DOMAIN);
$translator = \app\classes\Translator::getInstance();
$header = array(
'From: "' . $domain . '" <forgotten.password@' . $domain . '>',
'Reply-To: "No Reply" <noreply@' . $domain . '>',
"MIME-Version: 1.0",
"Content-type:text/html;charset=UTF-8"
);
$headers = implode("\r\n", $header);
$msg = $translator->convertVarsText($translator->_get("email-user_new_password"), array(
"password" => array(
"translate" => false,
"value" => $password
)
));
$this->app->sendEmailNative($user->_get("email"), $translator->_get("User - new password"), $msg, $headers);
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Heslo bolo obnovené"),
"text" => $t->_get("Na Vašu e-mailovu adresu Vám bolo zaslané nové heslo."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
} catch (\Exception $ex) {
$this->restApi->setHttpResponseCode(\app\constants\HttpReponseCodes::HTTP_UNAUTHORIZED);
$this->restApi->setCode(\app\constants\HttpReponseCodes::HTTP_UNAUTHORIZED);
$this->restApi->setErrorReason("Error");
$this->restApi->setCustomMessage($t->_get("Odkaz pre obnovenie hesla je neplatný. Heslo sa nepodarilo obnoviť. Opätovne požiadajte o zmenu hesla a skúste znova."));
$this->restApi->ajaxResponse();
}
}
/**
* @includeInDoc true
* @requestMethod "POST"
* @RequiredPostParameters ["email", "password"]
* @Description "<i>Get Authorization Token<hr></i>"
*
* @stringEmail "user e-mail"
* @stringPassword "user password<hr>"
*
* @Returns "<b><i>on success authToken otherwise empty data set</i></b>"
*/
public function getAuthToken() {
usleep(rand(1000, 2000) * 1000);
try {
$user = new \app\classes\BasicUser($this->urlReader->getPost("email"), false, false, false, array(\app\constants\UserType::CUSTOMER));
$token = $user->login($this->urlReader->getPost("password"));
if ($token === false) {
throw new \Exception("Login failed");
}
$this->restApi->ajaxResponse(array("authToken" => $token));
} catch (\Exception $ex) {
$this->restApi->setCode(\app\constants\ResponseCodes::LOGIN_FAILED);
$this->restApi->setHttpResponseCode(\app\constants\HttpReponseCodes::HTTP_UNAUTHORIZED);
$this->restApi->ajaxResponse();
}
}
/**
* @includeInDoc true
* @requestMethod "POST"
* @RequiredPostParameters ["email"]
* @Description "<i>Sends e-mail to user with forgotten password confirmation link<hr></i>"
*
* @stringEmail "user email<hr>"
*
* @Returns "<b><i>empty data set</i></b>"
*/
public function forgotPassword() {
$this->restApi->setCustomMessage($this->translator->_get("Žiadosť bola spracovaná. Ak bol zadaný e-mail nájdený v databáze, bola Vám odoslaná adresa pre zmenu hesla."));
try {
usleep(rand(1000, 2000) * 1000);
$user = new \app\classes\BasicUser($this->urlReader->getPost("email"), false, false, false, array(\app\constants\UserType::CUSTOMER));
$domain = $this->app->getConfig(\app\constants\Config::DOMAIN);
$translator = \app\classes\Translator::getInstance();
$header = array(
'From: "' . $domain . '" <forgotten.password@' . $domain . '>',
'Reply-To: "No Reply" <noreply@' . $domain . '>',
"MIME-Version: 1.0",
"Content-type:text/html;charset=UTF-8"
);
$headers = implode("\r\n", $header);
$msg = $translator->convertVarsText($translator->_get("email-user_forgotten_password_confirmation"), array(
"link" => array(
"translate" => false,
"value" => $this->app->getBaseUrl() . $this->app->getPageAddress(\app\constants\Config::FORGOTTEN_PASSWORD_CONFIRM_PAGE) . "/" . $user->_get("email") . "/" . md5($user->_get("last_login") . $this->app->getSecret() . $user->_get("email")) . md5($user->_get("secret") . $this->app->getSecret()) . "/"
)
));
$this->app->sendEmailNative($user->_get("email"), $translator->_get("User - forgotten password"), $msg, $headers);
$this->restApi->ajaxResponse();
} catch (\Exception $ex) {
$this->restApi->ajaxResponse();
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters []
* @VerifyPermissions []
*
*/
public function checkEmail() {
usleep(rand(100, 500) * 1000);
$email = toNonHtml($this->urlReader->getPost('email'));
try {
new \app\classes\BasicUser($email);
$this->restApi->ajaxResponse(['result' => true]);
} catch (\Exception $ex) {
$this->restApi->ajaxResponse(['result' => false]);
}
}
public function setUserType($userType) {
$this->userType = $userType;
}
}