| 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/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::ENGINEER;
parent::__construct();
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["company", "email", "phone", "street", "city", "zip", "g-recaptcha-response", "officer_full_name", "officer_email", "officer_phone", "ico"]
*/
public function register() {
throw new Exception(\app\constants\ResponseCodes::METHOD_NOT_ALLOWED, \app\constants\ResponseCodes::METHOD_NOT_ALLOWED);
try {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
$user = \app\classes\BasicUser::newUser($this->urlReader->getPost());
mkdir(DOCUMENT_ROOT . "uploads/private/uploads/users/" . $user->_get("folder"));
$allowed_types = array(
"jpeg" => "image/jpeg",
"jpg" => "image/jpg",
"png" => "image/png",
"gif" => "image/gif",
"pdf" => "application/pdf",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xls" => "application/vnd.ms-excel",
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
$requiredFileParts = array(
"size" => "size",
"type" => "type",
"content" => "content",
"name" => "name"
);
$values = &$_POST["files"];
if (is_array($values)) {
try {
foreach ($values as &$value) {
if (is_array($value)) {
foreach ($requiredFileParts as $required) {
if (!isset($value[$required])) {
throw new \Exception("attachment missing " . $required, \app\constants\ResponseCodes::INVALID_INPUT);
}
}
if (intval($value["size"] > 11000000)) { // 11 MB
throw new \Exception("attachment max file size is " . "11MB", \app\constants\ResponseCodes::INVALID_INPUT);
}
if (!in_array($value["type"], $allowed_types)) {
throw new \Exception("attachment invalid file format " . $value["type"], \app\constants\ResponseCodes::INVALID_INPUT);
}
$fileName = str_replace("." . array_search($value["type"], $allowed_types), "", $value["name"]);
$content = $value["content"];
$content = str_replace('data:' . $value["type"] . ';base64,', '', $content);
$content = str_replace(' ', '+', $content);
$data = base64_decode($content);
$file_path = "/uploads/private/uploads/users/" . $user->_get("folder") . "/" . $fileName . "_" . time() . "." . array_search($value["type"], $allowed_types);
if (strpos($value["type"], "image") !== false) {
$img = new \app\classes\SimpleImage();
$img->fromDataUri($value["content"]);
if ($img->getWidth() > $img->getHeight()) {
$img->fitToHeight(1200);
} else {
$img->fitToHeight(1600);
}
$img->toFile(DOCUMENT_ROOT . $file_path);
} else if (!file_put_contents(DOCUMENT_ROOT . $file_path, $data)) {
throw new \Exception($this->translator->_get("Prílohu sa nepodarilo nahrať. Skúste znova."), \app\constants\ResponseCodes::INVALID_INPUT);
}
}
}
} catch (\Exception $ex) {
rmdir(DOCUMENT_ROOT . "uploads/private/uploads/users/" . $user->_get("folder"));
$user->delete();
throw new Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
$emails = $this->app->getNotificationEmails();
if (count($emails) !== 0) {
$domain = $this->app->getConfig(\app\constants\Config::DOMAIN);
$translator = \app\classes\Translator::getInstance();
$header = array(
'From: "' . $domain . '" <info@' . $domain . '>',
'Reply-To: "No Reply" <noreply@' . $domain . '>',
"MIME-Version: 1.0",
"Content-type:text/html;charset=UTF-8"
);
$headers = implode("\r\n", $header);
$vars = array(
"domain" => array(
"translate" => false,
"value" => $this->app->getBaseUrl()
)
);
foreach ($user->toArray() as $key => $val) {
$vars[$key] = array(
"translate" => false,
"value" => $val
);
}
$msg = $translator->convertVarsText($translator->_get("email-new_registration_request"), $vars);
foreach ($emails as $email) {
$this->app->sendEmailNative($email, $translator->convertVarsText($translator->_get("Admin - new registration request"), $vars), $msg, $headers);
}
}
$this->restApi->setCustomMessage("Registrácia prebehla úspešne. Po schválení registrácie administrátorom Vám bude na Vašu e-mailovu adresu zaslaný notifikačný e-mail o schválení registrácie.");
$this->restApi->ajaxResponse(array("redirect" => $this->app->getPageAddress(\app\constants\Config::HOME_PAGE)));
} 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(), $ex->getCode(), $ex->getPrevious());
}
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["first_name", "last_name", "email", "g-recaptcha-response", "gdpr_accepted"]
*/
public function register2() {
$t = \app\classes\Translator::getInstance();
$db = \app\classes\PDODatabase::getInstance();
try {
$this->restApi->verifyCaptcha($this->urlReader->getPost("g-recaptcha-response"));
$personalno = $db->getValue("SELECT MAX(personalno) FROM users");
if ($personalno === null) {
$personalno = 0;
}
$personalno++;
$user = \app\classes\BasicUser::newUser($this->urlReader->getPost() + array(
"state" => \app\constants\State::CREATED,
"phone" => "",
"type" => $this->userType,
"color" => "#000",
"contract" => "",
"personalno" => $personalno
));
mkdir(DOCUMENT_ROOT . "uploads/private/uploads/users/" . $user->_get("folder"));
$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-partner_registration_verification"), $vars);
$this->app->sendEmailNative($user->_get("email"), $translator->convertVarsText($translator->_get("email-partner_registration_verification-subject"), $vars), $msg);
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Registrácia prebehla úspešne."),
"text" => $t->_get("Na Vašu e-mailovú adresu bol zaslaný pre potvrdenie registrácie."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::HOME_PAGE)
));
} 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($t->_get("Používateľ s touto e-mailovou adresou už existuje."));
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Používateľ s touto e-mailovou adresou už existuje."),
"text" => "",
"redirect" => $this->app->getPageAddress(\app\constants\Config::HOME_PAGE
)));
} else {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
}
/**
* @includeInDoc false
* @requestMethod "POST"
* @RequiredPostParameters ["password", "password_retype", "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 AND state=:state", array(
"secret" => $this->urlReader->getPost("token"),
"state" => \app\constants\State::CREATED
));
$user = new \app\classes\BasicUser($email);
} catch (\Exception $ex) {
throw new \Exception($t->_get("Neplatný odkaz pre aktiváciu účtu."), \app\constants\ResponseCodes::ERROR);
}
if (strlen($this->urlReader->getPost("password")) < 8 || !preg_match("#[0-9]+#", $this->urlReader->getPost("password")) || is_numeric($this->urlReader->getPost("password"))) {
throw new \Exception(\app\constants\ResponseCodes::getMessageForCode(\app\constants\ResponseCodes::INVALID_PASSWORD), \app\constants\ResponseCodes::ERROR);
}
if ($this->urlReader->getPost("password") !== $this->urlReader->getPost("password_retype")) {
throw new \Exception(\app\constants\ResponseCodes::getMessageForCode(\app\constants\ResponseCodes::PASSWORD_MISMATCH), \app\constants\ResponseCodes::ERROR);
}
$password = $this->urlReader->getPost("password");
$user->update(array(
"password" => password_hash($password, PASSWORD_BCRYPT),
"secret" => generateRandomString(40),
"state" => \app\constants\State::ACTIVE
));
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Aktivácia účtu prebehla úspešne"),
"text" => $t->_get("Môžete sa prihlásiť pomocou e-mailovej adresy a zadaného hesla."),
"redirect" => $this->app->getPageAddress(\app\constants\Config::LOGIN_PAGE)
));
}
/**
* @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) {
$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-partner_registration_verification"), $vars);
$this->app->sendEmailNative($user->_get("email"), $translator->convertVarsText($translator->_get("email-partner_registration_verification-subject"), $vars), $msg);
$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) {
}
$this->restApi->ajaxResponse(array(
"title" => $t->_get("Žiadosť bola spracovaná."),
"text" => $t->_get("Ak bol zadaný e-mail nájdený v databáze, na príslušný e-mail Vám bola odoslaná adresa pre zmenu hesla."),
));
}
/**
* @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::ENGINEER));
$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::ENGINEER));
$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();
}
}
public function setUserType($userType) {
$this->userType = $userType;
}
}