Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion upload/devcraft/src/modules/DleApi/Pages/AccessSyncPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DevCraft\Modules\DleApi\Models\ApiAccessLevelGroup;
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelGroupRepository;
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelRepository;
use DevCraft\Core\Support\DleDataService;

/**
* Синхронизация групп DLE ↔ уровни доступа.
Expand All @@ -30,7 +31,7 @@ public function handle(): array {
}

$groups = [];
foreach(Application::instance()->dleData()->groups() as $id => $name) {
foreach(DleDataService::groups() as $id => $name) {
$id = (int) $id;
if($id < 1) {
continue;
Expand Down
3 changes: 2 additions & 1 deletion upload/devcraft/src/modules/DleApi/Pages/KeyRequestsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DevCraft\Modules\DleApi\Models\ApiKeyRequest;
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelRepository;
use DevCraft\Modules\DleApi\Repositories\ApiKeyRequestRepository;
use DevCraft\Core\Support\DleDataService;

/**
* Заявки пользователей на API-ключ.
Expand All @@ -30,7 +31,7 @@ public function handle(): array {
}

$userNames = [];
foreach(Application::instance()->dleData()->users() as $row) {
foreach(DleDataService::users() as $row) {
$id = (int) ($row['user_id'] ?? 0);
if($id < 1) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions upload/devcraft/src/modules/DleApi/Pages/KeysPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelRepository;
use DevCraft\Modules\DleApi\Repositories\ApiKeyRepository;
use DevCraft\Modules\DleApi\Services\ScopeTableCatalog;
use DevCraft\Core\Support\DleDataService;

/**
* Страница управления API-ключами.
Expand All @@ -31,7 +32,7 @@ public function handle(): array {
}

$userNames = [];
foreach(Application::instance()->dleData()->users() as $row) {
foreach(DleDataService::users() as $row) {
$id = (int) ($row['user_id'] ?? 0);
if($id < 1) {
continue;
Expand Down Expand Up @@ -75,7 +76,7 @@ private function userOptions(): array {
['id' => 0, 'label' => '0 — ' . __('гость')],
];

foreach(Application::instance()->dleData()->users() as $row) {
foreach(DleDataService::users() as $row) {
$id = (int) ($row['user_id'] ?? 0);

if($id < 1) {
Expand Down
14 changes: 8 additions & 6 deletions upload/devcraft/src/modules/DleApi/Pages/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace DevCraft\Modules\DleApi\Pages;

use DevCraft\Modules\DleApi\DleApiIdentity;

use DLEPlugins;
use DevCraft\Core\Application;
use DevCraft\Core\Config\Paths;
use DevCraft\Core\Support\DataManager;
use DevCraft\Core\Abstracts\AbstractPage;
use DevCraft\Core\Interfaces\SettingsPageInterface;
use DevCraft\Modules\DleApi\Services\KeyNotifyDelivery;
use DevCraft\Core\Support\DleDataService;

/**
* Настройки DLE API.
Expand All @@ -25,7 +28,7 @@ public function handle(): array {
$configFile = Paths::config() . '/dleapi.json';

if(!is_file($configFile)) {
DataManager::saveConfig('dleapi', [
DataManager::saveConfig(DleApiIdentity::code(), [
'algo' => 'sha256',
'secret' => '',
'length' => 32,
Expand All @@ -34,10 +37,10 @@ public function handle(): array {
]);
}

$current = DataManager::getConfig('dleapi');
$current = DataManager::getConfig(DleApiIdentity::code());
$merged = KeyNotifyDelivery::loadEditorConfig(is_array($current) ? $current : []);
if(!is_array($current) || $merged !== $current) {
DataManager::saveConfig('dleapi', $merged);
DataManager::saveConfig(DleApiIdentity::code(), $merged);
}

$dleHome = rtrim((string) ($config['http_home_url'] ?? '/'), '/') . '/';
Expand All @@ -57,19 +60,18 @@ public function handle(): array {
}

public function supplementFormData(): array {
$dleData = Application::instance()->dleData();
$groups = [];
$users = [];

foreach($dleData->groups() as $id => $name) {
foreach(DleDataService::groups() as $id => $name) {
$id = (int) $id;
if($id < 1) {
continue;
}
$groups[(string) $id] = (string) $name;
}

foreach($dleData->users() as $row) {
foreach(DleDataService::users() as $row) {
$id = (string) ((int) ($row['user_id'] ?? 0));
$name = trim((string) ($row['name'] ?? ''));
$email = trim((string) ($row['email'] ?? ''));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DevCraft\Modules\DleApi\Models\ApiAccessLevelGroup;
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelGroupRepository;
use DevCraft\Modules\DleApi\Repositories\ApiAccessLevelRepository;
use DevCraft\Core\Support\DleDataService;

/**
* Резолв уровня доступа: группа DLE → map → default из настроек.
Expand All @@ -30,8 +31,8 @@ public function forUserId(int $userId): ?ApiAccessLevel {
if($userId < 1) {
return $this->defaultLevel();
}
global $db;
$row = $db->super_query('SELECT user_group FROM ' . USERPREFIX . '_users WHERE user_id=' . (int) $userId);

$row = DleDataService::user(id: $userId);

return $this->forUserGroup((int) ($row['user_group'] ?? 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DevCraft\Modules\DleApi\Repositories\ApiKeyRepository;
use DevCraft\Modules\DleApi\Repositories\ApiScopeRepository;
use RuntimeException;
use DevCraft\Core\Support\DleDataService;

/**
* Создание и обновление API-ключей вместе с матрицей scope.
Expand Down Expand Up @@ -152,7 +153,7 @@ private function userLabel(int $userId): string {
if($userId < 1) {
return __('гость');
}
foreach(Application::instance()->dleData()->users() as $row) {
foreach(DleDataService::users() as $row) {
if((int) ($row['user_id'] ?? 0) !== $userId) {
continue;
}
Expand Down
41 changes: 27 additions & 14 deletions upload/devcraft/src/modules/DleApi/Services/KeyNotifyDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace DevCraft\Modules\DleApi\Services;

use DevCraft\Core\Application;
use DevCraft\Builders\QueryBuilder;
use DevCraft\Core\Support\DleDataService;
use DLEPlugins;
use Throwable;

Expand Down Expand Up @@ -79,12 +81,16 @@ public static function loadEditorConfig(array $cfg): array {
return $cfg;
}

$request = $db->super_query(
"SELECT template FROM " . PREFIX . "_email WHERE name='" . $db->safesql(self::REQUEST_TEMPLATE_NAME) . "' LIMIT 1"
);
$decision = $db->super_query(
"SELECT template FROM " . PREFIX . "_email WHERE name='" . $db->safesql(self::DECISION_TEMPLATE_NAME) . "' LIMIT 1"
);
$request = QueryBuilder::create('email')
->withColumns(['template'])
->withConditionsItem('name', self::REQUEST_TEMPLATE_NAME)
->withLimit(1)
->first();
$decision = QueryBuilder::create('email')
->withColumns(['template'])
->withConditionsItem('name', self::DECISION_TEMPLATE_NAME)
->withLimit(1)
->first();

if(isset($request['template']) && $request['template'] !== '') {
$cfg['email_request_body'] = self::decodeHtmlBody((string) $request['template']);
Expand Down Expand Up @@ -185,7 +191,10 @@ private function sendEmailTemplate(string $name, array $userIds, array $vars): v
return;
}

$row = $db->super_query("SELECT * FROM " . PREFIX . "_email WHERE name='" . $db->safesql($name) . "' LIMIT 1");
$row = QueryBuilder::create('email')
->withConditionsItem('name', $name)
->withLimit(1)
->first();
if(empty($row['template'])) {
return;
}
Expand All @@ -194,21 +203,21 @@ private function sendEmailTemplate(string $name, array $userIds, array $vars): v
}
$site = rtrim((string) ($config['http_home_url'] ?? '/'), '/');
$vars = [
'{%site_url%}' => $site,
'{%api_key%}' => (string) ($vars['{%api_key%}'] ?? ''),
] + $vars;
'{%site_url%}' => $site,
'{%api_key%}' => (string) ($vars['{%api_key%}'] ?? ''),
] + $vars;

$mail = new \dle_mail($config, !empty($row['use_html']));
$body = $this->apply($row['template'], $vars);
$subj = $this->apply((string) ($vars['{%subject%}'] ?? __('DLE API')), $vars);
foreach($userIds as $uid) {
$u = $db->super_query('SELECT email, name FROM ' . USERPREFIX . '_users WHERE user_id=' . (int) $uid);
$u = DleDataService::user(id: (int) $uid);
if(empty($u['email'])) {
continue;
}
$mail->send((string) $u['email'], $subj, $this->apply($body, [
'{%username%}' => (string) ($u['name'] ?? ''),
] + $vars));
'{%username%}' => (string) ($u['name'] ?? ''),
] + $vars));
}
}

Expand Down Expand Up @@ -330,7 +339,11 @@ private static function upsertEmailTemplate(string $name, string $template, bool

$nameSql = $db->safesql($name);
$templateSql = $db->safesql($template);
$exists = $db->super_query("SELECT id FROM " . PREFIX . "_email WHERE name='{$nameSql}' LIMIT 1");
$exists = QueryBuilder::create('email')
->withColumns(['id'])
->withConditionsItem('name', $name)
->withLimit(1)
->first();
$useHtmlSql = $useHtml ? '1' : '0';

if(!empty($exists['id'])) {
Expand Down