35274-vm/add_channel.php
Flatlogic Bot 74e14f325c wtf
2025-10-27 12:24:10 +00:00

83 lines
3.0 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$error = '';
$telegram_id = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$telegram_id = trim($_POST['telegram_id'] ?? '');
if (empty($telegram_id)) {
$error = 'Telegram Channel ID or URL is required.';
} else {
// Basic parsing to get ID from URL
if (filter_var($telegram_id, FILTER_VALIDATE_URL)) {
$path = parse_url($telegram_id, PHP_URL_PATH);
$telegram_id = basename($path);
}
// Prepend '@' if it's a public username without it
if (strpos($telegram_id, '@') !== 0 && !is_numeric($telegram_id)) {
$telegram_id = '@' . $telegram_id;
}
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO channels (telegram_id) VALUES (:telegram_id)");
$stmt->execute([':telegram_id' => $telegram_id]);
session_start();
$_SESSION['flash_message'] = "Channel '{$telegram_id}' added successfully!";
header("Location: index.php");
exit;
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$error = "Error: Channel '{$telegram_id}' already exists.";
} else {
$error = "Database error: " . $e->getMessage();
}
}
}
}
require_once __DIR__ . '/layout_header.php';
?>
<div class="container-fluid">
<h1 class="h2 mb-4">Add New Channel</h1>
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form action="add_channel.php" method="POST">
<div class="mb-3">
<label for="telegram_id" class="form-label">Telegram Channel ID or URL</label>
<input type="text" class="form-control" id="telegram_id" name="telegram_id"
placeholder="e.g., @channelname or https://t.me/channelname"
value="<?php echo htmlspecialchars($telegram_id); ?>" required>
<div class="form-text text-secondary-color">
Provide the public channel username (e.g., @durov) or full URL.
</div>
</div>
<button type="submit" class="btn btn-primary">
<i data-feather="plus" class="me-1" style="width:16px; height:16px;"></i>
Add Channel
</button>
<a href="index.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
require_once __DIR__ . '/layout_footer.php';
?>