From 02172c35864a6625960da8081ba442c45b6d7eaa Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 2 Mar 2023 12:42:17 +0300 Subject: [PATCH] =?UTF-8?q?[*]=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B0=D0=BF=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config.php | 2 +- core/lib/Core/Api/ApiController.class.php | 48 +++++++ core/lib/Core/Api/ApiException.class.php | 4 + core/lib/Core/Api/Request.class.php | 126 ++++++++++++++++++ .../Core/Helpers/SystemFunctions.class.php | 3 +- core/lib/Core/Models/User.class.php | 5 +- 6 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 core/lib/Core/Api/Request.class.php diff --git a/core/config.php b/core/config.php index 817400e..5ed093f 100644 --- a/core/config.php +++ b/core/config.php @@ -141,7 +141,7 @@ /** * Флаг использования логирования */ - const USE_LOG = false; + const USE_LOG = true; /** * E-Mail сайта diff --git a/core/lib/Core/Api/ApiController.class.php b/core/lib/Core/Api/ApiController.class.php index 3168ab2..0b03db5 100644 --- a/core/lib/Core/Api/ApiController.class.php +++ b/core/lib/Core/Api/ApiController.class.php @@ -34,9 +34,13 @@ /** @var User $userObject Объект пользователя */ private $userObject; + /** @var Request $request Объект запроса */ + private $request; + public function __construct(User $userObject) { $this->userObject = $userObject; + $this->request = new Request(); } /** @@ -55,6 +59,50 @@ ApiView::output($result); } + /** + * Получение информации о ролях текущего пользователя + * + * @return void + * @throws CoreException + * @throws \JsonException + */ + public function getUserRoles(): void + { + $result = $this->userObject->getRolesObject()->getFullRoles(); + ApiView::output($result); + } + + /** + * Отправка кода верификации + * + * @return void + * @throws CoreException + * @throws \JsonException + */ + public function sendVerificationCode(): void + { + $result = $this->userObject->sendVerificationCode(); + ApiView::output($result); + } + + /** + * Смена имени пользователя + * + * @return void + * @throws CoreException + * @throws \JsonException + * @throws ApiException + */ + public function changeName(): void + { + $name = $this->request->getProperty('name'); + if (empty(trim($name))) { + throw new ApiException('Новое имя не может быть пустым', ApiException::ERROR_INPUT_DATA); + } + $this->userObject->update(['name' => $name]); + ApiView::output(true); + } + /** * Тестовый метод (не требующий авторизации) diff --git a/core/lib/Core/Api/ApiException.class.php b/core/lib/Core/Api/ApiException.class.php index 040f8c7..1d36f23 100644 --- a/core/lib/Core/Api/ApiException.class.php +++ b/core/lib/Core/Api/ApiException.class.php @@ -23,6 +23,10 @@ class ApiException extends \Exception { + + /** @var int ERROR_INPUT_DATA Ошибка входных данных */ + const ERROR_INPUT_DATA = 10000; + /** @var int ERROR_TOKEN_UNDEFINED Токен не определен */ const ERROR_TOKEN_UNDEFINED = 10001; diff --git a/core/lib/Core/Api/Request.class.php b/core/lib/Core/Api/Request.class.php new file mode 100644 index 0000000..60d443a --- /dev/null +++ b/core/lib/Core/Api/Request.class.php @@ -0,0 +1,126 @@ +init(); + } + + /** + * Получить строковое свойство + * + * @param string $key Имя свойства + * + * @return string|null Значение + */ + public function getProperty(string $key): ?string + { + if (is_string($this->properties[$key]) === false) { + return null; + } + return $this->properties[$key] ?? null; + } + + /** + * Получить параметры запроса в виде массива + * + * @return array Данные + */ + public function getArray(): array + { + return $this->properties; + } + + /** + * Получить свойство-массив + * + * @param string $key Имя свойства + * + * @return array|null Значение + */ + public function getArrayProperty(string $key): ?array + { + if (is_array($this->properties[$key]) === false) { + return null; + } + return $this->properties[$key] ?? null; + } + + /** + * Проверка существования свойства + * + * @param string $key Имя свойство + * + * @return bool Результат проверки + */ + public function isSetProperty(string $key): bool + { + return !empty($this->properties[$key]); + } + + /** + * Получить метод, которым был отправлен запрос + * + * @return string Метод, которым был отправлен запрос + */ + public function getRequestMethod(): string + { + return $this->method; + } + + /** + * Инициализация объекта запроса + */ + protected function init(): void + { + $this->method = $_SERVER['REQUEST_METHOD']; + switch ($_SERVER['REQUEST_METHOD']) { + case self::METHOD_POST: + $this->setProperties($_POST); + break; + case self::METHOD_GET: + $this->setProperties($_GET); + break; + } + } + + /** + * Установка свойств полей запроса + * + * @param array $array Входной массив данных + */ + private function setProperties(array $array): void + { + array_walk_recursive( + $array, + static function (&$input) { + $input = strip_tags(trim($input)); + if($input === 'undefined') { + $input = null; + } + } + ); + $this->properties = $array; + } + } diff --git a/core/lib/Core/Helpers/SystemFunctions.class.php b/core/lib/Core/Helpers/SystemFunctions.class.php index 77ef9f9..88c5442 100644 --- a/core/lib/Core/Helpers/SystemFunctions.class.php +++ b/core/lib/Core/Helpers/SystemFunctions.class.php @@ -98,6 +98,7 @@ } $oses = [ + 'Postman' => '/PostmanRuntime/i', 'iOS' => '/(iPhone)|(iPad)/i', 'Windows 3.11' => '/Win16/i', 'Windows 95' => '/(Windows 95)|(Win95)|(Windows_95)/i', @@ -124,7 +125,7 @@ ]; foreach ($oses as $os => $pattern) { - if (preg_match($pattern, $_SERVER['HTTP_USER_AGENT'])) { + if (@preg_match($pattern, $_SERVER['HTTP_USER_AGENT'])) { return $os; } } diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index 911840f..5a9e822 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -450,10 +450,13 @@ $fields[$key] = Sanitize::sanitizeString($value); } $cacheId = md5('User_getAllUserData_' . $this->id); + foreach($fields as $key => $value) { + $beforeData[$key] = $this->getAllUserData()[$key]; + } /** @var $DB DB Объект базы данных */ $DB = DB::getInstance(); Log::logToFile( - 'Данные пользователя изменены', 'User.log', ['userId' => $this->id, 'before' => $this->getAllUserData(), 'after' => func_get_args()] + 'Данные пользователя изменены', 'User.log', ['userId' => $this->id, 'before' => $beforeData, 'after' => $fields] ); Cache::delete($cacheId); return $DB->update(self::TABLE, ['id' => $this->id], $fields);