v8
This commit is contained in:
parent
1e73419ffb
commit
9c07e1ee23
@ -24,6 +24,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$channel_id = $_POST['channel_id'] ?? 0;
|
||||
$name = $_POST['name'] ?? '';
|
||||
$allow_file_sharing = isset($_POST['allow_file_sharing']) ? 1 : 0;
|
||||
$message_limit = !empty($_POST['message_limit']) ? (int)$_POST['message_limit'] : null;
|
||||
$theme_color = $_POST['theme_color'] ?? null;
|
||||
if ($theme_color === '') $theme_color = null;
|
||||
|
||||
@ -34,8 +35,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
if ($server && $server['owner_id'] == $user_id) {
|
||||
$name = strtolower(preg_replace('/[^a-zA-Z0-9\-]/', '-', $name));
|
||||
$stmt = db()->prepare("UPDATE channels SET name = ?, allow_file_sharing = ?, theme_color = ? WHERE id = ?");
|
||||
$stmt->execute([$name, $allow_file_sharing, $theme_color, $channel_id]);
|
||||
$stmt = db()->prepare("UPDATE channels SET name = ?, allow_file_sharing = ?, theme_color = ?, message_limit = ? WHERE id = ?");
|
||||
$stmt->execute([$name, $allow_file_sharing, $theme_color, $message_limit, $channel_id]);
|
||||
}
|
||||
header('Location: index.php?server_id=' . $server_id . '&channel_id=' . $channel_id);
|
||||
exit;
|
||||
@ -69,11 +70,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Basic sanitization for channel name
|
||||
$name = strtolower(preg_replace('/[^a-zA-Z0-9\-]/', '-', $name));
|
||||
$allow_file_sharing = isset($_POST['allow_file_sharing']) ? 1 : 0;
|
||||
$message_limit = !empty($_POST['message_limit']) ? (int)$_POST['message_limit'] : null;
|
||||
$theme_color = $_POST['theme_color'] ?? null;
|
||||
if ($theme_color === '') $theme_color = null;
|
||||
|
||||
$stmt = db()->prepare("INSERT INTO channels (server_id, name, type, allow_file_sharing, theme_color) VALUES (?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$server_id, $name, $type, $allow_file_sharing, $theme_color]);
|
||||
$stmt = db()->prepare("INSERT INTO channels (server_id, name, type, allow_file_sharing, theme_color, message_limit) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$server_id, $name, $type, $allow_file_sharing, $theme_color, $message_limit]);
|
||||
$channel_id = db()->lastInsertId();
|
||||
|
||||
header('Location: index.php?server_id=' . $server_id . '&channel_id=' . $channel_id);
|
||||
|
||||
46
api_v1_clear_channel.php
Normal file
46
api_v1_clear_channel.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/db/config.php";
|
||||
require_once __DIR__ . "/includes/permissions.php";
|
||||
session_start();
|
||||
header("Content-Type: application/json");
|
||||
|
||||
if (!isset($_SESSION["user_id"])) {
|
||||
echo json_encode(["success" => false, "error" => "Unauthorized"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$channel_id = $_POST["channel_id"] ?? null;
|
||||
if (!$channel_id) {
|
||||
echo json_encode(["success" => false, "error" => "Missing channel ID"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get server_id for this channel
|
||||
$stmt = db()->prepare("SELECT server_id FROM channels WHERE id = ?");
|
||||
$stmt->execute([$channel_id]);
|
||||
$channel = $stmt->fetch();
|
||||
|
||||
if (!$channel) {
|
||||
echo json_encode(["success" => false, "error" => "Channel not found"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$server_id = $channel["server_id"];
|
||||
|
||||
// Check if user is owner or admin (minimal check for now)
|
||||
$stmt = db()->prepare("SELECT owner_id FROM servers WHERE id = ?");
|
||||
$stmt->execute([$server_id]);
|
||||
$server = $stmt->fetch();
|
||||
|
||||
if ($server["owner_id"] != $_SESSION["user_id"]) {
|
||||
echo json_encode(["success" => false, "error" => "Only the server owner can clear history"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = db()->prepare("DELETE FROM messages WHERE channel_id = ?");
|
||||
$stmt->execute([$channel_id]);
|
||||
echo json_encode(["success" => true]);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(["success" => false, "error" => $e->getMessage()]);
|
||||
}
|
||||
@ -189,6 +189,28 @@ try {
|
||||
$stmt->execute([$channel_id, $user_id, $content, $attachment_url, $metadata]);
|
||||
$last_id = db()->lastInsertId();
|
||||
|
||||
// Enforce message limit if set
|
||||
$stmt = db()->prepare("SELECT message_limit FROM channels WHERE id = ?");
|
||||
$stmt->execute([$channel_id]);
|
||||
$channel = $stmt->fetch();
|
||||
if ($channel && !empty($channel['message_limit'])) {
|
||||
$limit = (int)$channel['message_limit'];
|
||||
// Delete oldest messages that exceed the limit
|
||||
$stmt = db()->prepare("
|
||||
DELETE FROM messages
|
||||
WHERE channel_id = ?
|
||||
AND id NOT IN (
|
||||
SELECT id FROM (
|
||||
SELECT id FROM messages
|
||||
WHERE channel_id = ?
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT ?
|
||||
) as tmp
|
||||
)
|
||||
");
|
||||
$stmt->execute([$channel_id, $channel_id, $limit]);
|
||||
}
|
||||
|
||||
// Fetch message with username for the response
|
||||
$stmt = db()->prepare("SELECT m.*, u.username, u.avatar_url FROM messages m JOIN users u ON m.user_id = u.id WHERE m.id = ?");
|
||||
$stmt->execute([$last_id]);
|
||||
|
||||
@ -592,11 +592,34 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
modal.querySelector('#edit-channel-id').value = btn.dataset.id;
|
||||
modal.querySelector('#edit-channel-name').value = btn.dataset.name;
|
||||
modal.querySelector('#edit-channel-files').checked = btn.dataset.files == '1';
|
||||
modal.querySelector('#edit-channel-limit').value = btn.dataset.limit || '';
|
||||
modal.querySelector('#edit-channel-theme').value = btn.dataset.theme || '#5865f2';
|
||||
modal.querySelector('#delete-channel-id').value = btn.dataset.id;
|
||||
});
|
||||
});
|
||||
|
||||
// Clear Channel History
|
||||
const clearHistoryBtn = document.getElementById('clear-channel-history-btn');
|
||||
clearHistoryBtn?.addEventListener('click', async () => {
|
||||
const channelId = document.getElementById('edit-channel-id').value;
|
||||
if (!confirm('Voulez-vous vraiment vider tout l\'historique de ce salon ? Cette action est irréversible.')) return;
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('channel_id', channelId);
|
||||
const resp = await fetch('api_v1_clear_channel.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
const result = await resp.json();
|
||||
if (result.success) {
|
||||
location.reload();
|
||||
} else {
|
||||
alert(result.error || 'Erreur lors du nettoyage de l\'historique');
|
||||
}
|
||||
} catch (e) { console.error(e); }
|
||||
});
|
||||
|
||||
// Roles Management
|
||||
const rolesTabBtn = document.getElementById('roles-tab-btn');
|
||||
const rolesList = document.getElementById('roles-list');
|
||||
|
||||
17
index.php
17
index.php
@ -79,13 +79,14 @@ if ($is_dm_view) {
|
||||
$channel_theme = $active_channel['theme_color'] ?? null;
|
||||
|
||||
// Fetch messages
|
||||
$display_limit = !empty($active_channel['message_limit']) ? (int)$active_channel['message_limit'] : 50;
|
||||
$stmt = db()->prepare("
|
||||
SELECT m.*, u.username, u.avatar_url
|
||||
FROM messages m
|
||||
JOIN users u ON m.user_id = u.id
|
||||
WHERE m.channel_id = ?
|
||||
ORDER BY m.created_at ASC
|
||||
LIMIT 50
|
||||
LIMIT " . $display_limit . "
|
||||
");
|
||||
$stmt->execute([$active_channel_id]);
|
||||
$messages = $stmt->fetchAll();
|
||||
@ -224,6 +225,7 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
||||
data-id="<?php echo $c['id']; ?>"
|
||||
data-name="<?php echo htmlspecialchars($c['name']); ?>"
|
||||
data-files="<?php echo $c['allow_file_sharing']; ?>"
|
||||
data-limit="<?php echo $c['message_limit']; ?>"
|
||||
data-theme="<?php echo $c['theme_color']; ?>">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"></circle><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33 1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82 1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path></svg>
|
||||
</span>
|
||||
@ -246,6 +248,7 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
||||
data-id="<?php echo $c['id']; ?>"
|
||||
data-name="<?php echo htmlspecialchars($c['name']); ?>"
|
||||
data-files="<?php echo $c['allow_file_sharing']; ?>"
|
||||
data-limit="<?php echo $c['message_limit']; ?>"
|
||||
data-theme="<?php echo $c['theme_color']; ?>">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"></circle><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33 1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82 1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path></svg>
|
||||
</span>
|
||||
@ -705,6 +708,11 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
||||
<input class="form-check-input" type="checkbox" name="allow_file_sharing" id="add-channel-files" value="1" checked>
|
||||
<label class="form-check-label text-white" for="add-channel-files">Allow File Sharing</label>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label text-uppercase fw-bold" style="font-size: 0.7em; color: var(--text-muted);">Message Limit</label>
|
||||
<input type="number" name="message_limit" class="form-control" placeholder="e.g. 50 (Leave empty for no limit)">
|
||||
<div class="form-text text-muted" style="font-size: 0.8em;">Automatically keeps only the last X messages in this channel.</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label text-uppercase fw-bold" style="font-size: 0.7em; color: var(--text-muted);">Theme Color</label>
|
||||
<input type="color" name="theme_color" class="form-control form-control-color w-100" value="#5865f2" title="Choose channel theme color">
|
||||
@ -754,6 +762,12 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
||||
<div class="form-text text-muted" style="font-size: 0.8em;">When disabled, users cannot upload files in this channel.</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label text-uppercase fw-bold" style="font-size: 0.7em; color: var(--text-muted);">Message Limit</label>
|
||||
<input type="number" name="message_limit" id="edit-channel-limit" class="form-control" placeholder="No limit">
|
||||
<div class="form-text text-muted" style="font-size: 0.8em;">Keep only the most recent messages.</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label text-uppercase fw-bold" style="font-size: 0.7em; color: var(--text-muted);">Theme Color</label>
|
||||
<input type="color" name="theme_color" id="edit-channel-theme" class="form-control form-control-color w-100" value="#5865f2" title="Choose channel theme color">
|
||||
@ -761,6 +775,7 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
||||
|
||||
<button type="submit" class="btn btn-primary w-100 mb-2">Save Changes</button>
|
||||
</form>
|
||||
<button type="button" id="clear-channel-history-btn" class="btn btn-warning w-100 mb-2">Vider l'historique</button>
|
||||
<form action="api_v1_channels.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this channel?');">
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="server_id" value="<?php echo $active_server_id; ?>">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user