Telegram drin
This commit is contained in:
@@ -32,18 +32,29 @@ $pdo = new PDO(
|
||||
$userId = (int) $_SESSION['user_id'];
|
||||
$errors = [];
|
||||
$successMessage = '';
|
||||
$telegramConfig = require __DIR__ . '/../config/telegram.php';
|
||||
|
||||
function h(?string $value): string
|
||||
{
|
||||
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function generateTelegramLinkToken(int $length = 32): string
|
||||
{
|
||||
return bin2hex(random_bytes(max(16, (int) ceil($length / 2))));
|
||||
}
|
||||
|
||||
$stmtUser = $pdo->prepare("
|
||||
SELECT
|
||||
`id`,
|
||||
`username`,
|
||||
`email`,
|
||||
`display_name`,
|
||||
`telegram_username`,
|
||||
`telegram_chat_id`,
|
||||
`telegram_link_token`,
|
||||
`telegram_link_token_expires_at`,
|
||||
`telegram_connected_at`,
|
||||
`role`,
|
||||
`is_active`
|
||||
FROM `app_users`
|
||||
@@ -113,6 +124,46 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
}
|
||||
|
||||
if ($action === 'generate_telegram_link') {
|
||||
$token = generateTelegramLinkToken(32);
|
||||
$expiresAt = (new DateTimeImmutable('now'))->modify('+30 minutes')->format('Y-m-d H:i:s');
|
||||
|
||||
$stmtLink = $pdo->prepare("
|
||||
UPDATE `app_users`
|
||||
SET
|
||||
`telegram_link_token` = :token,
|
||||
`telegram_link_token_expires_at` = :expires_at,
|
||||
`updated_at` = NOW()
|
||||
WHERE `id` = :id
|
||||
");
|
||||
$stmtLink->execute([
|
||||
':token' => $token,
|
||||
':expires_at' => $expiresAt,
|
||||
':id' => $userId,
|
||||
]);
|
||||
|
||||
$successMessage = 'Ein neuer Telegram-Verbindungscode wurde erzeugt.';
|
||||
}
|
||||
|
||||
if ($action === 'disconnect_telegram') {
|
||||
$stmtDisconnect = $pdo->prepare("
|
||||
UPDATE `app_users`
|
||||
SET
|
||||
`telegram_username` = NULL,
|
||||
`telegram_chat_id` = NULL,
|
||||
`telegram_link_token` = NULL,
|
||||
`telegram_link_token_expires_at` = NULL,
|
||||
`telegram_connected_at` = NULL,
|
||||
`updated_at` = NOW()
|
||||
WHERE `id` = :id
|
||||
");
|
||||
$stmtDisconnect->execute([
|
||||
':id' => $userId,
|
||||
]);
|
||||
|
||||
$successMessage = 'Die Telegram-Verknuepfung wurde entfernt.';
|
||||
}
|
||||
|
||||
if ($action === 'change_password') {
|
||||
$currentPassword = $_POST['current_password'] ?? '';
|
||||
$newPassword = $_POST['new_password'] ?? '';
|
||||
@@ -165,6 +216,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
$loggedIn = true;
|
||||
$publicBasePath = './';
|
||||
$telegramBotUsername = trim((string) ($telegramConfig['bot_username'] ?? ''));
|
||||
$telegramHasConnection = !empty($user['telegram_chat_id']);
|
||||
$telegramLinkToken = trim((string) ($user['telegram_link_token'] ?? ''));
|
||||
$telegramLinkExpiresAt = !empty($user['telegram_link_token_expires_at'])
|
||||
? new DateTimeImmutable((string) $user['telegram_link_token_expires_at'])
|
||||
: null;
|
||||
$telegramLinkIsActive = $telegramLinkToken !== ''
|
||||
&& $telegramLinkExpiresAt instanceof DateTimeImmutable
|
||||
&& $telegramLinkExpiresAt > new DateTimeImmutable('now');
|
||||
$telegramDeepLink = $telegramLinkIsActive && $telegramBotUsername !== ''
|
||||
? 'https://t.me/' . $telegramBotUsername . '?start=connect_' . $telegramLinkToken
|
||||
: null;
|
||||
?>
|
||||
<?php require __DIR__ . '/header.php'; ?>
|
||||
|
||||
@@ -214,6 +278,60 @@ $loggedIn = true;
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>Telegram verbinden</h2>
|
||||
<div class="form-stack">
|
||||
<p class="hint">
|
||||
Spaetere Bot-Nachrichten funktionieren nur mit einer verknuepften Chat-ID. Der Telegram-Username allein reicht Telegram fuer aktive Nachrichten an einen Nutzer nicht aus.
|
||||
</p>
|
||||
<table class="data-table mb-lg">
|
||||
<tr><td>Status</td><td><?= $telegramHasConnection ? 'Verbunden' : 'Nicht verbunden' ?></td></tr>
|
||||
<tr><td>Telegram-Username</td><td><?= h(($user['telegram_username'] ?? '') !== '' ? '@' . ltrim((string) $user['telegram_username'], '@') : '—') ?></td></tr>
|
||||
<tr><td>Chat-ID</td><td><?= h($user['telegram_chat_id'] ?? '—') ?></td></tr>
|
||||
<tr><td>Verbunden seit</td><td><?= h($user['telegram_connected_at'] ?? '—') ?></td></tr>
|
||||
</table>
|
||||
|
||||
<?php if ($telegramLinkIsActive): ?>
|
||||
<p class="hint">
|
||||
Aktiver Verbindungscode bis <?= h($telegramLinkExpiresAt?->format('d.m.Y H:i')) ?>:
|
||||
<strong><?= h($telegramLinkToken) ?></strong>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="input-row">
|
||||
<form method="post" action="">
|
||||
<input type="hidden" name="action" value="generate_telegram_link">
|
||||
<button type="submit" class="btn btn-primary"><?= $telegramLinkIsActive ? 'Neuen Verbindungscode erzeugen' : 'Telegram verbinden' ?></button>
|
||||
</form>
|
||||
|
||||
<?php if ($telegramHasConnection): ?>
|
||||
<form method="post" action="" onsubmit="return confirm('Telegram-Verknuepfung wirklich entfernen?');">
|
||||
<input type="hidden" name="action" value="disconnect_telegram">
|
||||
<button type="submit" class="btn btn-danger">Verknuepfung loesen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<?php if ($telegramLinkIsActive): ?>
|
||||
<?php if ($telegramDeepLink !== null): ?>
|
||||
<div class="telegram-qr-block">
|
||||
<div class="telegram-qr-image" id="telegramConnectQr" data-telegram-link="<?= h($telegramDeepLink) ?>"></div>
|
||||
<p class="hint">QR-Code mit dem Handy in Telegram scannen und dann im Bot nur noch auf <strong>Start</strong> tippen.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<p class="hint">
|
||||
Verbindung starten: <a href="<?= h($publicBasePath . 'telegram/connect.php') ?>">Telegram-Verknuepfung oeffnen</a>
|
||||
<?php if ($telegramDeepLink !== null): ?>
|
||||
oder direkt im Bot:
|
||||
<a href="<?= h($telegramDeepLink) ?>" target="_blank" rel="noopener">@<?= h($telegramBotUsername) ?></a>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid-2">
|
||||
<div class="card">
|
||||
<h2>Passwort aendern</h2>
|
||||
<form method="post" action="" class="form-stack">
|
||||
@@ -235,4 +353,28 @@ $loggedIn = true;
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if ($telegramDeepLink !== null): ?>
|
||||
<script src="<?= h($publicBasePath . 'js/qrcode.min.js') ?>"></script>
|
||||
<script>
|
||||
(function () {
|
||||
const qrEl = document.getElementById('telegramConnectQr');
|
||||
if (!qrEl || typeof QRCode === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
const qrLink = qrEl.getAttribute('data-telegram-link');
|
||||
if (!qrLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
new QRCode(qrEl, {
|
||||
text: qrLink,
|
||||
width: 220,
|
||||
height: 220,
|
||||
correctLevel: QRCode.CorrectLevel.M
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php require __DIR__ . '/footer.php'; ?>
|
||||
|
||||
Reference in New Issue
Block a user