From 4848efe591a981a35d2de5e916524bea73d13814 Mon Sep 17 00:00:00 2001 From: Maxim Harder Date: Tue, 19 Sep 2023 20:06:36 +0200 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D1=8B=20=D1=81=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BE?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [FIX] - Исправлена заявленная ошибка: https://github.com/DevCraftClub/dle_api/issues/11 --- install_archive.bat | 4 +- install_archive.sh | 4 +- manifest.json | 2 +- upload/api/includes/functions.php | 61 ++++-- upload/api/routes/admin_logs.php | 4 +- upload/api/routes/admin_sections.php | 4 +- upload/api/routes/banned.php | 4 +- upload/api/routes/banners.php | 4 +- upload/api/routes/banners_logs.php | 4 +- upload/api/routes/banners_rubrics.php | 4 +- upload/api/routes/category.php | 4 +- upload/api/routes/comment_rating_log.php | 4 +- upload/api/routes/comments.php | 4 +- upload/api/routes/comments_files.php | 4 +- upload/api/routes/complaint.php | 4 +- upload/api/routes/email.php | 4 +- upload/api/routes/files.php | 4 +- upload/api/routes/flood.php | 4 +- upload/api/routes/ignore_list.php | 4 +- upload/api/routes/images.php | 4 +- upload/api/routes/links.php | 4 +- upload/api/routes/login_log.php | 4 +- upload/api/routes/logs.php | 4 +- upload/api/routes/lostdb.php | 4 +- upload/api/routes/mail_log.php | 4 +- upload/api/routes/metatags.php | 4 +- upload/api/routes/notice.php | 4 +- upload/api/routes/plugins.php | 4 +- upload/api/routes/plugins_files.php | 4 +- upload/api/routes/plugins_logs.php | 4 +- upload/api/routes/pm.php | 4 +- upload/api/routes/poll.php | 4 +- upload/api/routes/poll_log.php | 4 +- upload/api/routes/post.php | 24 ++- upload/api/routes/post_extras.php | 4 +- upload/api/routes/post_extras_cats.php | 4 +- upload/api/routes/post_log.php | 4 +- upload/api/routes/post_pass.php | 4 +- upload/api/routes/question.php | 4 +- upload/api/routes/read_log.php | 4 +- upload/api/routes/redirects.php | 4 +- upload/api/routes/rss.php | 4 +- upload/api/routes/rssinform.php | 4 +- upload/api/routes/sendlog.php | 4 +- upload/api/routes/social_login.php | 4 +- upload/api/routes/spam_log.php | 4 +- upload/api/routes/static.php | 4 +- upload/api/routes/static_files.php | 4 +- upload/api/routes/subscribe.php | 4 +- upload/api/routes/tags.php | 4 +- upload/api/routes/twofactor.php | 4 +- upload/api/routes/usergroups.php | 4 +- upload/api/routes/users.php | 4 +- upload/api/routes/views.php | 4 +- upload/api/routes/vote.php | 4 +- upload/api/routes/vote_result.php | 4 +- upload/api/routes/xfsearch.php | 4 +- .../Monolog/Handler/ElasticSearchHandler.php | 194 +++++++++++++----- upload/engine/inc/dleapi.php | 6 +- upload/install.xml | 2 +- 60 files changed, 317 insertions(+), 188 deletions(-) diff --git a/install_archive.bat b/install_archive.bat index e4478928..c8c725dc 100644 --- a/install_archive.bat +++ b/install_archive.bat @@ -3,8 +3,8 @@ mkdir temp robocopy upload temp /E cd temp set PATH=%PATH%;%ProgramFiles%\7-Zip\ -7z a -mx0 -r -tzip -aoa maharder_assets.zip * +7z a -mx0 -r -tzip -aoa dle_api.zip * cd .. -copy /Y temp\maharder_assets.zip install.zip +copy /Y temp\dle_api.zip install.zip rd /s /q temp exit; \ No newline at end of file diff --git a/install_archive.sh b/install_archive.sh index fd7a1569..66b63c68 100755 --- a/install_archive.sh +++ b/install_archive.sh @@ -3,8 +3,8 @@ mkdir temp rsync -avz upload/ temp/ cd temp -zip -r maharder_assets.zip * +zip -r dle_api.zip * cd .. -cp -f temp/maharder_assets.zip install.zip +cp -f temp/dle_api.zip install.zip rm -rf temp exit 0 \ No newline at end of file diff --git a/manifest.json b/manifest.json index d90a6eed..0a52b82f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,4 +1,4 @@ { - "version": "160.0.34", + "version": "160.0.35", "name": "DLE API" } diff --git a/upload/api/includes/functions.php b/upload/api/includes/functions.php index b90836b7..1a7b36af 100644 --- a/upload/api/includes/functions.php +++ b/upload/api/includes/functions.php @@ -11,13 +11,13 @@ include_once DLEPlugins::Check(ENGINE_DIR . '/inc/includes/functions.inc.php'); include_once DLEPlugins::Check(__DIR__ . '/PDO.class.php'); include_once DLEPlugins::Check(ENGINE_DIR . '/api/api.class.php'); -$dleapi = json_decode(file_get_contents(DLEPlugins::Check(ENGINE_DIR . '/data/dleapi.json')), true); -$dle_api = new DLE_API(); +$dleapi = json_decode(file_get_contents(DLEPlugins::Check(ENGINE_DIR . '/data/dleapi.json')), true); +$dle_api = new DLE_API(); $dle_api->db = $db; $dbHostPort = explode(':', DBHOST); -$dbHost = $dbHostPort[0] ?: 'localhost'; -$dbPort = (isset($dbHostPort[1])) ? (int) $dbHostPort[1] : 3306; +$dbHost = $dbHostPort[0] ?: 'localhost'; +$dbPort = (isset($dbHostPort[1])) ? (int) $dbHostPort[1] : 3306; $connect = new database($dbHost, $dbPort, DBNAME, DBUSER, DBPASS); $DLEprefix = PREFIX; @@ -28,6 +28,7 @@ * * @param $value * @param $type + * * @return string */ function getComparer($value, $type = null) : string { @@ -66,6 +67,7 @@ function getComparer($value, $type = null) : string { * * @param $key * @param $name + * * @return array|false[] */ function checkAPI($key, $name) : array { @@ -80,7 +82,12 @@ function checkAPI($key, $name) : array { try { if (!empty($key) && !empty($name)) { - $keyCheck = $connect->query("SELECT k.id, k.api, k.is_admin, k.active, u.user_id, k.own_only, u.name FROM {$DLEprefix}_api_keys k, {$USERprefix}_users u WHERE u.user_id = k.user_id and k.api = :key", array('key' => $key)); + $keyCheck = $connect->query( + "SELECT k.id, k.api, k.is_admin, k.active, k.user_id, k.own_only, u.name FROM {$DLEprefix}_api_keys k, {$USERprefix}_users u WHERE k.api = :key", + array('key' => $key) + ); + + $username = (int) $keyCheck[0]['user_id'] > 0 ? $keyCheck[0]['name'] : 'Гость'; if (!empty($keyCheck)) { if ($keyCheck[0]['is_admin'] && $keyCheck[0]['active'] === 1) { @@ -92,15 +99,15 @@ function checkAPI($key, $name) : array { 'own' => [ 'access' => true, 'user_id' => $keyCheck[0]['user_id'], - 'user_name' => $keyCheck[0]['name'] + 'user_name' => $username ], ); } else { - $tablesCheck = $connect->query("SELECT * FROM {$DLEprefix}_api_scope - WHERE table = :name and key_id = :api", array('name' => $name, - 'api' => $keyCheck[0]['api'] - )); + $tablesCheck = $connect->query( + "SELECT * FROM {$DLEprefix}_api_scope das WHERE das.table = :name and das.key_id = :api_id", + array('name' => $name, 'api_id' => $keyCheck[0]['id']) + ); if (count($tablesCheck) > 0) { if ($keyCheck[0]['active'] === 1) { @@ -110,7 +117,7 @@ function checkAPI($key, $name) : array { if ($tablesCheck[0]['delete'] === 1) $antwort['delete'] = true; if ($keyCheck[0]['own_only'] === 1) $antwort['own']['access'] = true; $antwort['own']['user_id'] = $keyCheck[0]['user_id']; - $antwort['own']['user_name'] = $keyCheck[0]['name']; + $antwort['own']['user_name'] = $username; } else $antwort['error'] = 'API-ключ не активен!'; } else $antwort['error'] = 'API-ключ не действителен!'; } @@ -132,6 +139,7 @@ function checkAPI($key, $name) : array { * * @param $value * @param $type + * * @return bool|float|int|string */ function defType($value, $type = null) : float|bool|int|string { @@ -149,6 +157,7 @@ function defType($value, $type = null) : float|bool|int|string { * * @param $text * @param $max + * * @return string */ function checkLength($text, $max) : string { @@ -162,17 +171,17 @@ class CacheSystem { private string $cachePath; private string $module; private string $id; - private mixed $data; + private mixed $data; private string $app; /** * CacheSystem constructor. * - * @param $module // Название таблицы - * @param string $id // Идентификационный набор символов - * @param mixed $data // Передаваемые и сохраняемые данные - * @param string $app // Тип кеша - * @param string $path // Путь кеша + * @param $module // Название таблицы + * @param string $id // Идентификационный набор символов + * @param mixed $data // Передаваемые и сохраняемые данные + * @param string $app // Тип кеша + * @param string $path // Путь кеша */ public function __construct(string $module, string $id = '', mixed $data = '', string $app = 'api', string $path = ENGINE_DIR . '/cache') { $this->data = $data; @@ -184,7 +193,7 @@ public function __construct(string $module, string $id = '', mixed $data = '', s } /** - * @param string $cachePath + * @param string $cachePath */ public function setCachePath(string $cachePath) : void { if (!mkdir($cachePath) && !is_dir($cachePath)) { @@ -209,7 +218,7 @@ public function create() : bool|string { * @return string */ public function get() : string { - $file_name = "{$this->app}_{$this->module}_" . md5($this->id) . '.json'; + $file_name = "{$this->app}_{$this->module}_" . md5($this->id) . '.json'; $cache_file = $this->cachePath . DIRECTORY_SEPARATOR . $file_name; if (file_exists($cache_file)) { $return_data = json_decode(file_get_contents($cache_file), true); @@ -226,7 +235,7 @@ public function get() : string { /** * Очищает файлы кеша * - * @param string $app + * @param string $app */ public function clear(string $app = '') : void { $pattern = (empty($app)) ? '*' : $this->app . '_' . $app . '_*'; @@ -241,7 +250,7 @@ public function clear(string $app = '') : void { } /** - * @param mixed $data + * @param mixed $data */ public function setData(mixed $data) : void { $this->data = $data; @@ -270,3 +279,13 @@ private function secureData($data, $value) : mixed { } } + +function check_response(mixed $data) : bool { + if (is_array($data)) return count($data) > 0; + else { + if (!empty($data)) { + return str_contains($data, '[]'); + } + } + return false; +} diff --git a/upload/api/routes/admin_logs.php b/upload/api/routes/admin_logs.php index 708a63c2..91324367 100644 --- a/upload/api/routes/admin_logs.php +++ b/upload/api/routes/admin_logs.php @@ -128,8 +128,8 @@ $sql = "SELECT * FROM " . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/admin_sections.php b/upload/api/routes/admin_sections.php index 5bba1e2e..18a06e62 100644 --- a/upload/api/routes/admin_sections.php +++ b/upload/api/routes/admin_sections.php @@ -122,8 +122,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/banned.php b/upload/api/routes/banned.php index 0d9a3924..157166ef 100644 --- a/upload/api/routes/banned.php +++ b/upload/api/routes/banned.php @@ -122,8 +122,8 @@ $sql = "SELECT * FROM " . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/banners.php b/upload/api/routes/banners.php index 14eeab9f..98b62b06 100644 --- a/upload/api/routes/banners.php +++ b/upload/api/routes/banners.php @@ -226,8 +226,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/banners_logs.php b/upload/api/routes/banners_logs.php index 6b8b624d..595423b1 100644 --- a/upload/api/routes/banners_logs.php +++ b/upload/api/routes/banners_logs.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/banners_rubrics.php b/upload/api/routes/banners_rubrics.php index 49ccf262..8102458f 100644 --- a/upload/api/routes/banners_rubrics.php +++ b/upload/api/routes/banners_rubrics.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/category.php b/upload/api/routes/category.php index b59009ba..f73390ac 100644 --- a/upload/api/routes/category.php +++ b/upload/api/routes/category.php @@ -261,8 +261,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/comment_rating_log.php b/upload/api/routes/comment_rating_log.php index 749a4b9f..1bb11ca6 100644 --- a/upload/api/routes/comment_rating_log.php +++ b/upload/api/routes/comment_rating_log.php @@ -114,8 +114,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/comments.php b/upload/api/routes/comments.php index 6ca9ef30..9c59246a 100644 --- a/upload/api/routes/comments.php +++ b/upload/api/routes/comments.php @@ -171,8 +171,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/comments_files.php b/upload/api/routes/comments_files.php index c44d58b8..b2e2ef70 100644 --- a/upload/api/routes/comments_files.php +++ b/upload/api/routes/comments_files.php @@ -123,8 +123,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/complaint.php b/upload/api/routes/complaint.php index 8ca7a30e..d8385de2 100644 --- a/upload/api/routes/complaint.php +++ b/upload/api/routes/complaint.php @@ -144,8 +144,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/email.php b/upload/api/routes/email.php index dba3b9d5..7bb18cb8 100644 --- a/upload/api/routes/email.php +++ b/upload/api/routes/email.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/files.php b/upload/api/routes/files.php index 0f45eb2b..f0e9acf4 100644 --- a/upload/api/routes/files.php +++ b/upload/api/routes/files.php @@ -158,8 +158,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/flood.php b/upload/api/routes/flood.php index 4f457e14..c3fdd316 100644 --- a/upload/api/routes/flood.php +++ b/upload/api/routes/flood.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/ignore_list.php b/upload/api/routes/ignore_list.php index 6568d733..04eb18d0 100644 --- a/upload/api/routes/ignore_list.php +++ b/upload/api/routes/ignore_list.php @@ -101,8 +101,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/images.php b/upload/api/routes/images.php index 6f9a6b1d..a508144b 100644 --- a/upload/api/routes/images.php +++ b/upload/api/routes/images.php @@ -116,8 +116,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/links.php b/upload/api/routes/links.php index 753da10d..afd11def 100644 --- a/upload/api/routes/links.php +++ b/upload/api/routes/links.php @@ -128,8 +128,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/login_log.php b/upload/api/routes/login_log.php index 94f3e6ce..1c65f0ee 100644 --- a/upload/api/routes/login_log.php +++ b/upload/api/routes/login_log.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/logs.php b/upload/api/routes/logs.php index 426b6143..58430140 100644 --- a/upload/api/routes/logs.php +++ b/upload/api/routes/logs.php @@ -114,8 +114,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/lostdb.php b/upload/api/routes/lostdb.php index 2962c116..c18d0661 100644 --- a/upload/api/routes/lostdb.php +++ b/upload/api/routes/lostdb.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/mail_log.php b/upload/api/routes/mail_log.php index b9f33bd3..dd78cee4 100644 --- a/upload/api/routes/mail_log.php +++ b/upload/api/routes/mail_log.php @@ -107,8 +107,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/metatags.php b/upload/api/routes/metatags.php index cf3184d1..a1449de7 100644 --- a/upload/api/routes/metatags.php +++ b/upload/api/routes/metatags.php @@ -127,8 +127,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/notice.php b/upload/api/routes/notice.php index bb93ae8c..597622ec 100644 --- a/upload/api/routes/notice.php +++ b/upload/api/routes/notice.php @@ -100,8 +100,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/plugins.php b/upload/api/routes/plugins.php index 59ce55ad..22ee0522 100644 --- a/upload/api/routes/plugins.php +++ b/upload/api/routes/plugins.php @@ -238,8 +238,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/plugins_files.php b/upload/api/routes/plugins_files.php index 8bc79e40..4b716b03 100644 --- a/upload/api/routes/plugins_files.php +++ b/upload/api/routes/plugins_files.php @@ -147,8 +147,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/plugins_logs.php b/upload/api/routes/plugins_logs.php index 7d84e605..7c75db6e 100644 --- a/upload/api/routes/plugins_logs.php +++ b/upload/api/routes/plugins_logs.php @@ -105,8 +105,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/pm.php b/upload/api/routes/pm.php index db924a13..0e6a92f4 100644 --- a/upload/api/routes/pm.php +++ b/upload/api/routes/pm.php @@ -149,8 +149,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/poll.php b/upload/api/routes/poll.php index c24b5ccb..b4a6d21f 100644 --- a/upload/api/routes/poll.php +++ b/upload/api/routes/poll.php @@ -127,8 +127,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/poll_log.php b/upload/api/routes/poll_log.php index 52de36c5..3ac2e666 100644 --- a/upload/api/routes/poll_log.php +++ b/upload/api/routes/poll_log.php @@ -99,8 +99,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/post.php b/upload/api/routes/post.php index 9632395d..8abc7093 100644 --- a/upload/api/routes/post.php +++ b/upload/api/routes/post.php @@ -209,16 +209,18 @@ } } if (!$access['full']) { - if (strlen($possibleParams) === 0 && $access['own_only']['access']) - $possibleParams .= " WHERE autor = '{$access['own_only']['user_name']}'"; - else $possibleParams .= " AND autor = '{$access['own_only']['user_name']}'"; + if (strlen($possibleParams) === 0) { + if ($access['own_only']['access']) $possibleParams .= " WHERE autor = '{$access['own_only']['user_name']}'"; + } else { + if ($access['own_only']['access']) $possibleParams .= " AND autor = '{$access['own_only']['user_name']}'"; + } } $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { @@ -279,16 +281,18 @@ } if (!$access['full']) { - if (strlen($possibleParams) === 0 && $access['own_only']['access']) - $possibleParams .= " WHERE autor = '{$access['own_only']['user_name']}' OR user_id = {$access['own_only']['user_id']}"; - else $possibleParams .= " AND (autor = '{$access['own_only']['user_name']}' OR user_id = {$access['own_only']['user_id']})"; + if (strlen($possibleParams) === 0) { + if ($access['own_only']['access']) $possibleParams .= " WHERE autor = '{$access['own_only']['user_name']}' OR user_id = {$access['own_only']['user_id']}"; + } else { + if ($access['own_only']['access']) $possibleParams .= " AND (autor = '{$access['own_only']['user_name']}' OR user_id = {$access['own_only']['user_id']})"; + } } $sql = 'SELECT * FROM ' . PREFIX . '_post p INNER JOIN ' . PREFIX . "_post_extras e on p.id = e.news_id {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/post_extras.php b/upload/api/routes/post_extras.php index 44994b48..11f5b3bf 100644 --- a/upload/api/routes/post_extras.php +++ b/upload/api/routes/post_extras.php @@ -225,8 +225,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/post_extras_cats.php b/upload/api/routes/post_extras_cats.php index 14529ce3..7c9cc8c8 100644 --- a/upload/api/routes/post_extras_cats.php +++ b/upload/api/routes/post_extras_cats.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/post_log.php b/upload/api/routes/post_log.php index 3a685ff2..39194317 100644 --- a/upload/api/routes/post_log.php +++ b/upload/api/routes/post_log.php @@ -106,8 +106,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/post_pass.php b/upload/api/routes/post_pass.php index e30f1875..76fca375 100644 --- a/upload/api/routes/post_pass.php +++ b/upload/api/routes/post_pass.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/question.php b/upload/api/routes/question.php index b2f05193..ca2e8a9b 100644 --- a/upload/api/routes/question.php +++ b/upload/api/routes/question.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/read_log.php b/upload/api/routes/read_log.php index c937eda0..eb9fc648 100644 --- a/upload/api/routes/read_log.php +++ b/upload/api/routes/read_log.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/redirects.php b/upload/api/routes/redirects.php index fec937ec..9734b5b4 100644 --- a/upload/api/routes/redirects.php +++ b/upload/api/routes/redirects.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/rss.php b/upload/api/routes/rss.php index 82740b62..5e8de073 100644 --- a/upload/api/routes/rss.php +++ b/upload/api/routes/rss.php @@ -162,8 +162,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/rssinform.php b/upload/api/routes/rssinform.php index 22ac2a99..1e6ad29e 100644 --- a/upload/api/routes/rssinform.php +++ b/upload/api/routes/rssinform.php @@ -148,8 +148,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/sendlog.php b/upload/api/routes/sendlog.php index ea8b20c1..d059de2c 100644 --- a/upload/api/routes/sendlog.php +++ b/upload/api/routes/sendlog.php @@ -106,8 +106,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/social_login.php b/upload/api/routes/social_login.php index fba2a959..2708e74b 100644 --- a/upload/api/routes/social_login.php +++ b/upload/api/routes/social_login.php @@ -128,8 +128,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/spam_log.php b/upload/api/routes/spam_log.php index af22d99b..5ea119e5 100644 --- a/upload/api/routes/spam_log.php +++ b/upload/api/routes/spam_log.php @@ -106,8 +106,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/static.php b/upload/api/routes/static.php index 9f32f6c6..ca57b736 100644 --- a/upload/api/routes/static.php +++ b/upload/api/routes/static.php @@ -204,8 +204,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/static_files.php b/upload/api/routes/static_files.php index e22ed36f..47a26c71 100644 --- a/upload/api/routes/static_files.php +++ b/upload/api/routes/static_files.php @@ -155,8 +155,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/subscribe.php b/upload/api/routes/subscribe.php index 931ce76b..d491b8ec 100644 --- a/upload/api/routes/subscribe.php +++ b/upload/api/routes/subscribe.php @@ -120,8 +120,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/tags.php b/upload/api/routes/tags.php index 7e9c3b13..a57ba707 100644 --- a/upload/api/routes/tags.php +++ b/upload/api/routes/tags.php @@ -92,8 +92,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/twofactor.php b/upload/api/routes/twofactor.php index 51aa36c8..5237f489 100644 --- a/upload/api/routes/twofactor.php +++ b/upload/api/routes/twofactor.php @@ -114,8 +114,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/usergroups.php b/upload/api/routes/usergroups.php index dc4d52e0..d9527dd0 100644 --- a/upload/api/routes/usergroups.php +++ b/upload/api/routes/usergroups.php @@ -919,8 +919,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/users.php b/upload/api/routes/users.php index a2bbc45d..fdaadcd8 100644 --- a/upload/api/routes/users.php +++ b/upload/api/routes/users.php @@ -312,8 +312,8 @@ $sql = 'SELECT * FROM ' . USERPREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/views.php b/upload/api/routes/views.php index 6713bc38..58ce4917 100644 --- a/upload/api/routes/views.php +++ b/upload/api/routes/views.php @@ -85,8 +85,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/vote.php b/upload/api/routes/vote.php index ece12b5a..013e0289 100644 --- a/upload/api/routes/vote.php +++ b/upload/api/routes/vote.php @@ -141,8 +141,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/vote_result.php b/upload/api/routes/vote_result.php index 622d63ef..158a25a2 100644 --- a/upload/api/routes/vote_result.php +++ b/upload/api/routes/vote_result.php @@ -114,8 +114,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/routes/xfsearch.php b/upload/api/routes/xfsearch.php index d5356fdf..39e69ad7 100644 --- a/upload/api/routes/xfsearch.php +++ b/upload/api/routes/xfsearch.php @@ -99,8 +99,8 @@ $sql = 'SELECT * FROM ' . PREFIX . "_{$api_name} {$possibleParams} ORDER by {$orderBy} {$sort} {$limit}"; $getData = new CacheSystem($api_name, $sql); - if (empty($getData->get())) { - $data = $connect->query($sql); + if (check_response($getData->get())) { + $data = $connect->query($sql, []); $getData->setData($data); $data = $getData->create(); } else { diff --git a/upload/api/vendor/monolog/monolog/src/Monolog/Handler/ElasticSearchHandler.php b/upload/api/vendor/monolog/monolog/src/Monolog/Handler/ElasticSearchHandler.php index bb0f83eb..74cc7b6e 100644 --- a/upload/api/vendor/monolog/monolog/src/Monolog/Handler/ElasticSearchHandler.php +++ b/upload/api/vendor/monolog/monolog/src/Monolog/Handler/ElasticSearchHandler.php @@ -1,4 +1,4 @@ -setHosts($hosts) + * ->build(); * - * $client = new \Elastica\Client(); * $options = array( * 'index' => 'elastic_index_name', - * 'type' => 'elastic_doc_type', + * 'type' => 'elastic_doc_type', * ); - * $handler = new ElasticSearchHandler($client, $options); + * $handler = new ElasticsearchHandler($client, $options); * $log = new Logger('application'); * $log->pushHandler($handler); * - * @author Jelle Vink + * @author Avtandil Kikabidze + * @phpstan-type Options array{ + * index: string, + * type: string, + * ignore_error: bool, + * op_type: 'index'|'create' + * } + * @phpstan-type InputOptions array{ + * index?: string, + * type?: string, + * ignore_error?: bool, + * op_type?: 'index'|'create' + * } */ -class ElasticSearchHandler extends AbstractProcessingHandler +class ElasticsearchHandler extends AbstractProcessingHandler { + protected Client|Client8 $client; + /** - * @var Client + * @var mixed[] Handler config options + * @phpstan-var Options */ - protected $client; + protected array $options; /** - * @var array Handler config options + * @var bool */ - protected $options = array(); + private $needsType; /** - * @param Client $client Elastica Client object - * @param array $options Handler configuration - * @param int $level The minimum logging level at which this handler will be triggered - * @param bool $bubble Whether the messages that are handled can bubble up the stack or not + * @param Client|Client8 $client Elasticsearch Client object + * @param mixed[] $options Handler configuration + * + * @phpstan-param InputOptions $options */ - public function __construct(Client $client, array $options = array(), $level = Logger::DEBUG, $bubble = true) + public function __construct(Client|Client8 $client, array $options = [], int|string|Level $level = Level::Debug, bool $bubble = true) { parent::__construct($level, $bubble); $this->client = $client; $this->options = array_merge( - array( - 'index' => 'monolog', // Elastic index name - 'type' => 'record', // Elastic document type - 'ignore_error' => false, // Suppress Elastica exceptions - ), + [ + 'index' => 'monolog', // Elastic index name + 'type' => '_doc', // Elastic document type + 'ignore_error' => false, // Suppress Elasticsearch exceptions + 'op_type' => 'index', // Elastic op_type (index or create) (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-op_type) + ], $options ); + + if ($client instanceof Client8 || $client::VERSION[0] === '7') { + $this->needsType = false; + // force the type to _doc for ES8/ES7 + $this->options['type'] = '_doc'; + } else { + $this->needsType = true; + } } /** - * {@inheritDoc} + * @inheritDoc */ - protected function write(array $record) + protected function write(LogRecord $record): void { - $this->bulkSend(array($record['formatted'])); + $this->bulkSend([$record->formatted]); } /** - * {@inheritdoc} + * @inheritDoc */ - public function setFormatter(FormatterInterface $formatter) + public function setFormatter(FormatterInterface $formatter): HandlerInterface { - if ($formatter instanceof ElasticaFormatter) { + if ($formatter instanceof ElasticsearchFormatter) { return parent::setFormatter($formatter); } - throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter'); + + throw new InvalidArgumentException('ElasticsearchHandler is only compatible with ElasticsearchFormatter'); } /** * Getter options - * @return array + * + * @return mixed[] + * + * @phpstan-return Options */ - public function getOptions() + public function getOptions(): array { return $this->options; } /** - * {@inheritDoc} + * @inheritDoc */ - protected function getDefaultFormatter() + protected function getDefaultFormatter(): FormatterInterface { - return new ElasticaFormatter($this->options['index'], $this->options['type']); + return new ElasticsearchFormatter($this->options['index'], $this->options['type']); } /** - * {@inheritdoc} + * @inheritDoc */ - public function handleBatch(array $records) + public function handleBatch(array $records): void { $documents = $this->getFormatter()->formatBatch($records); $this->bulkSend($documents); @@ -112,17 +152,79 @@ public function handleBatch(array $records) /** * Use Elasticsearch bulk API to send list of documents - * @param array $documents + * + * @param array> $records Records + _index/_type keys * @throws \RuntimeException */ - protected function bulkSend(array $documents) + protected function bulkSend(array $records): void { try { - $this->client->addDocuments($documents); - } catch (ExceptionInterface $e) { - if (!$this->options['ignore_error']) { - throw new \RuntimeException("Error sending messages to Elasticsearch", 0, $e); + $params = [ + 'body' => [], + ]; + + foreach ($records as $record) { + $params['body'][] = [ + $this->options['op_type'] => $this->needsType ? [ + '_index' => $record['_index'], + '_type' => $record['_type'], + ] : [ + '_index' => $record['_index'], + ], + ]; + unset($record['_index'], $record['_type']); + + $params['body'][] = $record; + } + + /** @var Elasticsearch */ + $responses = $this->client->bulk($params); + + if ($responses['errors'] === true) { + throw $this->createExceptionFromResponses($responses); + } + } catch (Throwable $e) { + if (! $this->options['ignore_error']) { + throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e); + } + } + } + + /** + * Creates elasticsearch exception from responses array + * + * Only the first error is converted into an exception. + * + * @param mixed[]|Elasticsearch $responses returned by $this->client->bulk() + */ + protected function createExceptionFromResponses($responses): Throwable + { + foreach ($responses['items'] ?? [] as $item) { + if (isset($item['index']['error'])) { + return $this->createExceptionFromError($item['index']['error']); } } + + if (class_exists(ElasticInvalidArgumentException::class)) { + return new ElasticInvalidArgumentException('Elasticsearch failed to index one or more records.'); + } + + return new ElasticsearchRuntimeException('Elasticsearch failed to index one or more records.'); + } + + /** + * Creates elasticsearch exception from error array + * + * @param mixed[] $error + */ + protected function createExceptionFromError(array $error): Throwable + { + $previous = isset($error['caused_by']) ? $this->createExceptionFromError($error['caused_by']) : null; + + if (class_exists(ElasticInvalidArgumentException::class)) { + return new ElasticInvalidArgumentException($error['type'] . ': ' . $error['reason'], 0, $previous); + } + + return new ElasticsearchRuntimeException($error['type'] . ': ' . $error['reason'], 0, $previous); } } diff --git a/upload/engine/inc/dleapi.php b/upload/engine/inc/dleapi.php index 930b4cd2..dd0d574a 100644 --- a/upload/engine/inc/dleapi.php +++ b/upload/engine/inc/dleapi.php @@ -22,8 +22,12 @@ $version = [ 'name' => 'DLE-API', 'descr' => 'Неофициальное API', - 'version' => '160.0.34', + 'version' => '160.0.35', 'changelog' => [ + '160.0.35' => [ + '[FIX] Исправлена заявленная ошибка', + '[FIX] Исправлены функции проверки доступа для других пользователей и гостевых ключей', + ], '160.0.34' => [ '[FIX] Исправлена заявленная ошибка', ], diff --git a/upload/install.xml b/upload/install.xml index e6c324b4..ac2898ea 100644 --- a/upload/install.xml +++ b/upload/install.xml @@ -3,7 +3,7 @@ DLE-API Неофициальное API для CMS DLE - 160.0.33 + 160.0.35 16.0 greater