|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
use App\Services\TicketTelegramConfig;
|
|
|
|
|
use App\Services\TicketTelegramNotifier;
|
|
|
|
|
|
|
|
|
|
final class TelegramController extends AdminController
|
|
|
|
|
{
|
|
|
|
|
public function save($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->canManageBinding()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$payload = $this->payload($request);
|
|
|
|
|
$config = TicketTelegramConfig::save([
|
|
|
|
|
'enabled' => $payload['telegram_enabled'] ?? false,
|
|
|
|
|
'webhook_base_url' => $this->scalar($payload, 'telegram_webhook_base_url'),
|
|
|
|
|
'bot_token' => $this->scalar($payload, 'telegram_bot_token'),
|
|
|
|
|
'rotate_secret' => $payload['telegram_rotate_webhook_secret'] ?? false,
|
|
|
|
|
]);
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => 'Telegram 配置已保存;如配置有变化,请重新注册 Webhook',
|
|
|
|
|
'config' => $config,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\InvalidArgumentException $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422);
|
|
|
|
|
} catch (\RuntimeException $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => $this->safeConfigurationMessage($exception)], 422);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Administrator Telegram configuration save failed');
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => 'Telegram 配置保存失败,请检查数据库日志'], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rotateSecret($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->canManageBinding()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$config = TicketTelegramConfig::rotateWebhookSecret();
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => 'Webhook 密钥已轮换,请立即重新注册 Webhook',
|
|
|
|
|
'config' => $config,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\InvalidArgumentException $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422);
|
|
|
|
|
} catch (\RuntimeException $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => $this->safeConfigurationMessage($exception)], 422);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Administrator Telegram webhook secret rotation failed');
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => 'Webhook 密钥轮换失败,请检查数据库日志'], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function bind($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->canManageBinding()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$binding = TicketTelegramNotifier::createBinding((int) Auth::getUser()->id);
|
|
|
|
|
return $response->withJson(['ret' => 1] + $binding);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '无法生成绑定码,请检查 Telegram 配置与数据库'], 422);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unbind($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->canManageBinding()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403);
|
|
|
|
|
}
|
|
|
|
|
TicketTelegramNotifier::unbind();
|
|
|
|
|
return $response->withJson(['ret' => 1, 'msg' => '已解绑工单通知']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function webhook($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->canManageBinding()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
TicketTelegramNotifier::registerWebhook();
|
|
|
|
|
return $response->withJson(['ret' => 1, 'msg' => 'Webhook 注册成功']);
|
|
|
|
|
} catch (\RuntimeException $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function canManageBinding(): bool
|
|
|
|
|
{
|
|
|
|
|
// The routes also apply Admin + SuperAdmin/CSRF middleware. Recheck the
|
|
|
|
|
// current account here so direct invocations cannot use a downgraded ID.
|
|
|
|
|
$user = Auth::getUser();
|
|
|
|
|
return is_object($user) && !empty($user->is_admin)
|
|
|
|
|
&& (int) ($user->enable ?? 0) === 1 && AdminPermission::isSuperAdmin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function payload($request): array
|
|
|
|
|
{
|
|
|
|
|
$payload = $request !== null && method_exists($request, 'getParsedBody')
|
|
|
|
|
? $request->getParsedBody()
|
|
|
|
|
: [];
|
|
|
|
|
return is_array($payload) ? $payload : [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function scalar(array $payload, string $key): string
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists($key, $payload) || $payload[$key] === null) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
if (!is_scalar($payload[$key])) {
|
|
|
|
|
throw new \InvalidArgumentException('Telegram 配置字段格式无效');
|
|
|
|
|
}
|
|
|
|
|
return (string) $payload[$key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function safeConfigurationMessage(\RuntimeException $exception): string
|
|
|
|
|
{
|
|
|
|
|
$message = $exception->getMessage();
|
|
|
|
|
foreach (['Telegram ', 'PHP sodium ', '请在工单系统的 Telegram '] as $safePrefix) {
|
|
|
|
|
if (str_starts_with($message, $safePrefix)) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 'Telegram 配置操作失败,请检查数据库与服务器日志';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|