35274-vm/settings.php
Flatlogic Bot d2f005da56 nu norm
2025-10-27 13:55:31 +00:00

148 lines
6.7 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'layout_header.php';
$pdo = db();
$success_message = '';
// List of recognized settings keys
$allowed_settings = [
'webhook_new_post',
'webhook_post_sent',
'webhook_new_channel',
'webhook_token' // Add the token to the allowed list
];
// Handle form submission for saving webhook URLs
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_settings'])) {
$stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (:key, :value) ON DUPLICATE KEY UPDATE setting_value = :value");
foreach ($allowed_settings as $key) {
if (isset($_POST[$key])) {
$stmt->execute([':key' => $key, ':value' => $_POST[$key]]);
}
}
$success_message = "Settings saved successfully!";
}
// Handle token regeneration
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['regenerate_token'])) {
$new_token = bin2hex(random_bytes(16));
$stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('webhook_token', :token) ON DUPLICATE KEY UPDATE setting_value = :token");
$stmt->execute([':token' => $new_token]);
$success_message = "New webhook token generated successfully!";
}
// Fetch all settings from the database
$stmt = $pdo->query("SELECT * FROM settings");
$db_settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// Merge with defaults to ensure all keys exist
$settings = array_merge(
array_fill_keys($allowed_settings, ''),
$db_settings
);
// Generate a token if it doesn't exist
if (empty($settings['webhook_token'])) {
$settings['webhook_token'] = bin2hex(random_bytes(16));
$stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('webhook_token', :token) ON DUPLICATE KEY UPDATE setting_value = :token");
$stmt->execute([':token' => $settings['webhook_token']]);
}
// Construct the full webhook URL
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$incoming_webhook_url = sprintf('%s://%s/webhook_receiver.php?token=%s', $scheme, $host, $settings['webhook_token']);
?>
<div class="container-fluid p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 text-white mb-0">Settings</h1>
<a href="webhook_docs.php" class="btn btn-info">View Webhook Documentation</a>
</div>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<div class="row">
<div class="col-lg-8">
<div class="card bg-dark-surface mb-4">
<div class="card-header">
<h5 class="card-title mb-0">Outgoing Webhooks <a href="webhook_docs.php#outgoing" class="small">(See Docs)</a></h5>
</div>
<div class="card-body">
<p class="card-text text-muted mb-4">
Configure webhooks to send notifications to external services based on events in the application.
Enter the full URL for each endpoint.
</p>
<form method="POST">
<div class="mb-4">
<label for="webhook_new_post" class="form-label">New Post Scheduled</label>
<input type="url" class="form-control" id="webhook_new_post" name="webhook_new_post"
placeholder="https://your-service.com/webhook/new-post"
value="<?php echo htmlspecialchars($settings['webhook_new_post']); ?>">
<div class="form-text">This webhook will be triggered when a new post is added to the scheduler.</div>
</div>
<div class="mb-4">
<label for="webhook_post_sent" class="form-label">Post Successfully Sent</label>
<input type="url" class="form-control" id="webhook_post_sent" name="webhook_post_sent"
placeholder="https://your-service.com/webhook/post-sent"
value="<?php echo htmlspecialchars($settings['webhook_post_sent']); ?>">
<div class="form-text">This webhook will be triggered when a scheduled post is successfully sent. (Not yet implemented)</div>
</div>
<div class="mb-4">
<label for="webhook_new_channel" class="form-label">New Channel Added</label>
<input type="url" class="form-control" id="webhook_new_channel" name="webhook_new_channel"
placeholder="https://your-service.com/webhook/new-channel"
value="<?php echo htmlspecialchars($settings['webhook_new_channel']); ?>">
<div class="form-text">This webhook will be triggered when a new channel is added to the system. (Not yet implemented)</div>
</div>
<button type="submit" name="save_settings" class="btn btn-primary">Save Settings</button>
</form>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card bg-dark-surface">
<div class="card-header">
<h5 class="card-title mb-0">Incoming Webhook URL <a href="webhook_docs.php#incoming" class="small">(See Docs)</a></h5>
</div>
<div class="card-body">
<p class="text-muted">
Use this URL in your external services (like n8n) to send data back to this application.
</p>
<div class="input-group mb-3">
<input type="text" id="webhook-url-input" class="form-control" value="<?php echo htmlspecialchars($incoming_webhook_url); ?>" readonly>
<button class="btn btn-secondary" type="button" onclick="copyToClipboard()">Copy</button>
</div>
<form method="POST" class="d-inline">
<button type="submit" name="regenerate_token" class="btn btn-warning">Regenerate Token</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
function copyToClipboard() {
var copyText = document.getElementById("webhook-url-input");
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
document.execCommand("copy");
alert("Copied the URL: " + copyText.value);
}
</script>
<?php
require_once 'layout_footer.php';
?>