Files
sld-php-template/functions/site_settings.php
T
2026-08-19 17:26:06 +01:00

90 lines
2.9 KiB
PHP

<?php
/**
* Global Site Settings & High-Resolution Favicon Management (Owner Exclusive)
*/
/**
* Get site setting value by key.
*/
function get_site_setting($key, $default = '') {
global $conn;
if (!isset($conn)) return $default;
$stmt = $conn->prepare("SELECT setting_value FROM site_settings WHERE setting_key = ? LIMIT 1");
$stmt->bind_param("s", $key);
$stmt->execute();
$res = $stmt->get_result();
if ($row = $res->fetch_assoc()) {
$stmt->close();
return $row['setting_value'];
}
$stmt->close();
return $default;
}
/**
* Update or insert site setting.
*/
function update_site_setting($key, $value) {
global $conn;
if (!isset($conn)) return false;
$stmt = $conn->prepare("INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
$stmt->bind_param("ss", $key, $value);
$res = $stmt->execute();
$stmt->close();
return $res;
}
/**
* Upload and validate high-resolution site Favicon.
*/
function upload_site_favicon($file) {
if (empty($file) || $file['error'] !== UPLOAD_ERR_OK) {
return ['success' => false, 'message' => 'Errore nel caricamento del file favicon.'];
}
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowed_extensions = ['png', 'ico', 'webp', 'svg', 'jpeg', 'jpg'];
if (!in_array($ext, $allowed_extensions)) {
return ['success' => false, 'message' => 'Formato favicon non supportato. Formati ammessi: PNG, ICO, WEBP, SVG, JPG.'];
}
// High resolution dimension check for raster formats (PNG, WEBP, JPG, ICO)
if (in_array($ext, ['png', 'webp', 'jpeg', 'jpg', 'ico'])) {
$image_info = @getimagesize($file['tmp_name']);
if ($image_info) {
$width = $image_info[0];
$height = $image_info[1];
// Minimum high resolution requirement: 64x64px
if ($width < 64 || $height < 64) {
return [
'success' => false,
'message' => "Risoluzione favicon troppo bassa ({$width}x{$height}px). Per garantire la massima qualità visiva, carica un'immagine ad alta risoluzione (almeno 64x64px)."
];
}
}
}
// Ensure assets/img directory exists
$img_dir = ROOT_DIR . "assets/img/";
if (!is_dir($img_dir)) {
@mkdir($img_dir, 0777, true);
}
$new_filename = "favicon_" . time() . "." . $ext;
$target_file = $img_dir . $new_filename;
$relative_path = "assets/img/" . $new_filename;
if (move_uploaded_file($file['tmp_name'], $target_file)) {
update_site_setting('site_favicon', $relative_path);
return ['success' => true, 'message' => 'Favicon aggiornata con successo!'];
}
return ['success' => false, 'message' => 'Impossibile salvare il file favicon su disco.'];
}