Files
bookmarkly2/api/update_database.php

102 lines
4.3 KiB
PHP

<?php
require_once '../include/init.php';
require_once '../include/auth.php';
header('Content-Type: application/json');
// --- Security Check: Only Admin can access this page ---
if (!isAdmin()) {
http_response_code(403); // Forbidden
echo json_encode(['success' => false, 'message' => 'You do not have permission to perform this action.']);
exit;
}
try {
$messages = [];
// --- Check columns in 'tabs' table ---
$tabs_columns = $pdo->query("PRAGMA table_info(tabs)")->fetchAll(PDO::FETCH_COLUMN, 1);
$required_columns = [
'background_image_url' => 'TEXT DEFAULT NULL',
'background_brightness' => 'INTEGER DEFAULT 100',
'background_saturation' => 'INTEGER DEFAULT 100',
'theme' => 'TEXT DEFAULT NULL',
'theme_transparency' => 'INTEGER DEFAULT NULL',
'login_token' => 'VARCHAR(255)',
'show_search_bar' => 'INTEGER DEFAULT 1'
];
$columns_added = false;
foreach ($required_columns as $column_name => $column_definition) {
if (!in_array($column_name, $tabs_columns)) {
$pdo->exec("ALTER TABLE tabs ADD COLUMN $column_name $column_definition");
$messages[] = "Column '$column_name' added to 'tabs' table.";
$columns_added = true;
} else {
$messages[] = "Column '$column_name' already exists in 'tabs' table.";
}
}
// Check for login_token column and add unique index if not present
$current_tabs_columns = $pdo->query("PRAGMA table_info(tabs)")->fetchAll(PDO::FETCH_COLUMN, 1);
if (in_array('login_token', $current_tabs_columns)) {
$indexes = $pdo->query("PRAGMA index_list(tabs)")->fetchAll();
$index_exists = false;
foreach ($indexes as $index) {
// Check if the index is on the login_token column
$index_info = $pdo->query("PRAGMA index_info({$index['name']})")->fetchAll();
foreach ($index_info as $info) {
if ($info['name'] === 'login_token' && $index['unique']) {
$index_exists = true;
break 2;
}
}
}
if (!$index_exists) {
$tabs_without_token = $pdo->query("SELECT id FROM tabs WHERE login_token IS NULL OR login_token = ''")->fetchAll(PDO::FETCH_COLUMN);
if (!empty($tabs_without_token)) {
$update_stmt = $pdo->prepare("UPDATE tabs SET login_token = ? WHERE id = ?");
foreach ($tabs_without_token as $tab_id) {
$token = bin2hex(random_bytes(16));
$update_stmt->execute([$token, $tab_id]);
}
$messages[] = count($tabs_without_token) . " existing tabs updated with a unique login token.";
$columns_added = true;
}
try {
$pdo->exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_tabs_login_token ON tabs(login_token)");
$messages[] = "Added unique index to login_token column.";
$columns_added = true;
} catch (PDOException $e) {
$messages[] = "Could not create unique index on login_token: " . $e->getMessage();
}
} else {
$messages[] = "Unique index on login_token already exists.";
}
}
// --- Check settings table for show_search_bar setting ---
$settings_check = $pdo->prepare("SELECT value FROM settings WHERE key = 'show_search_bar'");
$settings_check->execute();
if (!$settings_check->fetch()) {
$pdo->prepare("INSERT INTO settings (key, value) VALUES (?, ?)")->execute(['show_search_bar', '1']);
$messages[] = "Added 'show_search_bar' setting to settings table.";
$columns_added = true;
} else {
$messages[] = "Setting 'show_search_bar' already exists in settings table.";
}
if ($columns_added) {
echo json_encode(['success' => true, 'message' => 'Database updated successfully!', 'details' => $messages]);
} else {
echo json_encode(['success' => true, 'message' => 'Database is already up to date.', 'details' => $messages]);
}
} catch (PDOException $e) {
http_response_code(500); // Internal Server Error
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}