if (search_query) {
sql = "SELECT id, full_path, folder_name, parent_path, depth, audio_file_count
FROM folder_metadata
WHERE full_path LIKE '%searchquerysearch_query%'
ORDER BY depth ASC, folder_name ASC LIMIT 10000";
// Funzione per generare link File Station
function generateFolderLink(fullpath,nas_base_path, file_station_url) {
real_path = nasfull_path !== '/' && real_path .= '/' . ltrim($full_path, '/');
}
// Ottieni l'IP del NAS (serve solo per costruire URL File Station)
nasip=_SERVER['HTTP_HOST'];
filenas_ip . ':12001'; // Porta standard File Station
?>
code
Assistant
Ecco la versione corretta dello script PHP che assicura che tutti i link a File Station siano generati con HTTPS:
<?php
// Configurazione del Database (DA MODIFICARE)
$db_host = 'localhost';
$db_user = 'db_audio_user';
$db_pass = 'LaTuaPasswordForte';
$db_name = 'audio_db';
// **CONFIGURA QUESTI 5 PERCORSI BASE DEL NAS**
define('NAS_BASE_PATH_1', '/volume1/millemusiche');
define('NAS_BASE_PATH_2', '/volume1/milleinterpreti');
define('NAS_BASE_PATH_3', '/volume1/ECM.Collection');
define('NAS_BASE_PATH_4', '/volume1/millemusiche.da.organizzare.2024');
define('NAS_BASE_PATH_5', '/volumeUSB1/usbshare');
// Mappa dei percorsi base
$nas_base_paths = [
'millemusiche' => NAS_BASE_PATH_1,
'milleinterpreti' => NAS_BASE_PATH_2,
'ECM.Collection' => NAS_BASE_PATH_3,
'millemusiche.da.organizzare.2024' => NAS_BASE_PATH_4,
'usbshare' => NAS_BASE_PATH_5
];
// Determina il percorso base in base al primo segmento del full_path
function getNasBasePath($full_path) {
global $nas_base_paths;
$segments = explode('/', ltrim($full_path, '/'));
$first_segment = strtolower($segments[0] ?? '');
foreach ($nas_base_paths as $key => $path) {
if (strpos(strtolower($first_segment), strtolower($key)) !== false) {
return $path;
}
}
return NAS_BASE_PATH_1; // Default al primo percorso
}
// Connessione al Database
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connessione fallita: " . $conn->connect_error);
}
$search_query = isset($_GET['q']) ? $conn->real_escape_string($_GET['q']) : '';
$results = [];
if ($search_query) {
$sql = "SELECT id, full_path, folder_name, parent_path, depth, audio_file_count
FROM folder_metadata
WHERE full_path LIKE '%$search_query%' OR folder_name LIKE '%$search_query%'
ORDER BY depth ASC, folder_name ASC LIMIT 10000";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$results[] = $row;
}
}
}
$conn->close();
// Funzione migliorata per generare link File Station sicuri (sempre HTTPS)
function generateFolderLink($full_path, $nas_base_path) {
// Costruisci il percorso completo sul NAS
$real_path = $nas_base_path;
if ($full_path !== '/' && $full_path !== '') {
$real_path .= '/' . ltrim($full_path, '/');
}
// Usa solo HTTPS e usa la porta corretta del NAS
$host = $_SERVER['HTTP_HOST'];
// Estrai solo l'hostname senza la porta, se presente
if (strpos($host, ':') !== false) {
$host = explode(':', $host)[0];
}
// Porta HTTPS per DSM/File Station (default 5001, ma alcune installazioni usano 12001)
$port = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443' ?
$_SERVER['SERVER_PORT'] : '5001';
// Codifica correttamente il percorso per File Station
$encoded_path = rawurlencode($real_path);
// Costruisci l'URL completo di File Station (formato per DSM 7.x)
return "https://$host:$port/app/FileStation/#/shared-folder?path=$encoded_path";
}
// Ottieni l'hostname corrente
$current_host = $_SERVER['HTTP_HOST'];
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MILLEMUSICHE DB</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f4; color: #333; }
.container { max-width: 1200px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
h1 { text-align: center; color: #0056b3; }
form { display: flex; justify-content: center; margin-bottom: 20px; }
input[type="text"] { width: 70%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
input[type="submit"] { padding: 10px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin-left: 10px; }
input[type="submit"]:hover { background-color: #0056b3; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
th { background-color: #e9e9e9; }
tr:nth-child(even) { background-color: #f9f9f9; }
.no-results { text-align: center; color: #666; margin-top: 30px; }
.full-path { font-size: 0.9em; color: #555; }
.folder-links { display: flex; gap: 10px; flex-wrap: wrap; }
.folder-link {
display: inline-block;
padding: 4px 8px;
background-color: #007bff;
color: white;
text-decoration: none;
border-radius: 3px;
font-size: 0.8em;
transition: background-color 0.3s;
cursor: pointer;
}
.folder-link:hover { background-color: #0056b3; }
.path-cell { min-width: 300px; }
.tooltip { position: relative; display: inline-block; }
.tooltip .tooltiptext {
visibility: hidden;
width: 200px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -100px;
opacity: 0;
transition: opacity 0.3s;
font-size: 0.7em;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<h1>🎵 MILLEMUSICHE DB</h1>
<form action="" method="get">
<input type="text" name="q" placeholder="Cerca nome cartella o percorso..." value="<?php echo htmlspecialchars($search_query); ?>">
<input type="submit" value="🔍 Cerca">
</form>
<?php if ($search_query && empty($results)): ?>
<p class="no-results">Nessun risultato trovato per "<?php echo htmlspecialchars($search_query); ?>".</p>
<?php elseif (!empty($results)): ?>
<table>
<thead>
<tr>
<th>Nome Cartella</th>
<th>File Audio</th>
<th>Percorso e Azioni</th>
<th>Profondità</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $row): ?>
<?php
// Determina il percorso base corretto per questa cartella
$nas_base_path = getNasBasePath($row['full_path']);
// Genera link HTTPS per File Station
$file_station_link = generateFolderLink($row['full_path'], $nas_base_path);
// Percorso completo nel sistema
$full_system_path = $nas_base_path . ($row['full_path'] !== '/' ? '/' . ltrim($row['full_path'], '/') : '');
?>
<tr>
<td><strong><?php echo htmlspecialchars($row['folder_name']); ?></strong></td>
<td><?php echo htmlspecialchars($row['audio_file_count']); ?></td>
<td class="path-cell">
<div class="full-path" style="margin-bottom: 8px;">
<?php echo htmlspecialchars($row['full_path']); ?>
</div>
<div class="folder-links">
<a href="<?php echo htmlspecialchars($file_station_link); ?>"
target="_blank"
class="folder-link tooltip">
📁 File Station
<span class="tooltiptext">Apri in File Station (HTTPS)</span>
</a>
<button onclick="copyToClipboard('<?php echo htmlspecialchars(addslashes($full_system_path)); ?>')"
class="folder-link tooltip">
📋 Percorso
<span class="tooltiptext">Copia percorso completo negli appunti</span>
</button>
</div>
</td>
<td><?php echo htmlspecialchars($row['depth']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p class="no-results">Inserisci un termine di ricerca per iniziare.</p>
<?php endif; ?>
</div>
<script>
function copyToClipboard(text) {
const tempInput = document.createElement('input');
tempInput.value = text;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999);
try {
document.execCommand('copy');
const button = event.currentTarget;
const originalText = button.innerHTML;
button.innerHTML = '✅ Copiato!';
button.style.backgroundColor = '#28a745';
setTimeout(() => {
button.innerHTML = originalText;
button.style.backgroundColor = '#007bff';
}, 2000);
} catch (err) {
alert('Impossibile copiare automaticamente. Percorso: ' + text);
}
document.body.removeChild(tempInput);
}
</script>
</body>
</html>
Principali Modifiche Apportate:
Link sempre HTTPS: Ho completamente riscritto la funzione generateFolderLink() per generare sempre URL HTTPS.
Rilevamento della porta automatico: Lo script ora rileva automaticamente la porta utilizzata dal server e usa quella corretta (di default 5001, la porta standard HTTPS per Synology DSM).
URL di File Station aggiornato: Ho aggiornato l'URL per File Station utilizzando il formato corretto per DSM 7.x:
Miglioramento della sicurezza JavaScript: Ho migliorato la funzione copyToClipboard per prevenire errori JS e gestire correttamente l'evento.
Miglior rilevamento dei percorsi base: La funzione getNasBasePath() è stata migliorata per gestire meglio il riconoscimento dei percorsi.
Codifica corretta dei percorsi: Ho utilizzato rawurlencode() per garantire che i percorsi con spazi e caratteri speciali vengano gestiti correttamente.
Protezione da XSS: Ho usato addslashes() nel parametro JS per prevenire problemi con apici e virgolette nei percorsi.
Note Importanti:
Configurazione della porta: Se il tuo Synology NAS usa una porta HTTPS diversa da 5001 (quella standard), lo script tenterà di rilevarla automaticamente. Se non funziona, modifica manualmente questa riga:
Sostituendo '5001' con la tua porta HTTPS (ad esempio '12001').
Formato URL File Station: Se il tuo NAS esegue una versione di DSM diversa dalla 7.x, potrebbe essere necessario modificare il formato dell'URL di File Station nella funzione generateFolderLink().
Connessione HTTPS: Assicurati che il tuo NAS sia configurato correttamente per HTTPS con un certificato valido, altrimenti i browser moderni potrebbero bloccare l'accesso.
Questo script ora garantisce che tutti i link a File Station siano sempre generati utilizzando HTTPS, migliorando la sicurezza e risolvendo il problema che stavi riscontrando.