query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
echo json_encode(["success" => true, "tables" => $tables]);
} catch (Exception $e) {
echo json_encode(["success" => false, "message" => "Errore nel recuperare le tabelle: " . $e->getMessage()]);
}
exit;
}
// Modalità di esportazione di una singola tabella in formato SQL e caricamento FTP
if (isset($_GET['table'])) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
$table = $_GET['table'];
$fileContent = '';
try {
// Connessione al database di Drupal
$conn = Database::getConnection();
// Esporta la struttura della tabella
$createTableQuery = $conn->query("SHOW CREATE TABLE `$table`")->fetch(PDO::FETCH_ASSOC);
if (!$createTableQuery) {
throw new Exception("Errore durante l'ottenimento della struttura della tabella $table.");
}
$fileContent .= "-- Struttura della tabella `$table`\n";
$fileContent .= "DROP TABLE IF EXISTS `$table`;\n";
$fileContent .= $createTableQuery['Create Table'] . ";\n\n";
// Esporta i dati della tabella
$rows = $conn->query("SELECT * FROM `$table`");
foreach ($rows as $row) {
$values = array_map([$conn, 'quote'], $row);
$valuesList = implode(", ", $values);
$fileContent .= "INSERT INTO `$table` VALUES ($valuesList);\n";
}
$fileContent .= "\n\n";
// Connessione FTP
$conn_id = ftp_connect($ftp_server);
if (!$conn_id) {
throw new Exception("Impossibile connettersi al server FTP $ftp_server.");
}
// Login FTP
if (!ftp_login($conn_id, $ftp_username, $ftp_password)) {
ftp_close($conn_id);
throw new Exception("Impossibile autenticarsi con le credenziali FTP fornite.");
}
ftp_pasv($conn_id, true);
// Verifica o creazione della cartella di destinazione
if (!@ftp_chdir($conn_id, $ftp_destination)) {
if (!ftp_mkdir($conn_id, $ftp_destination)) {
ftp_close($conn_id);
throw new Exception("Impossibile creare la cartella di destinazione FTP $ftp_destination.");
}
ftp_chdir($conn_id, $ftp_destination);
}
// Nome del file SQL sul server FTP
$remoteFile = "$table.sql";
// Carica il contenuto come file temporaneo
$tempFile = tmpfile();
fwrite($tempFile, $fileContent);
fseek($tempFile, 0);
// Carica il file temporaneo su FTP
if (ftp_fput($conn_id, $remoteFile, $tempFile, FTP_ASCII)) {
echo json_encode(["success" => true, "message" => "Esportazione e caricamento FTP della tabella $table completati."]);
} else {
throw new Exception("Errore durante il caricamento FTP della tabella $table.");
}
// Chiude la connessione FTP e il file temporaneo
fclose($tempFile);
ftp_close($conn_id);
} catch (Exception $e) {
echo json_encode(["success" => false, "message" => "Errore durante l'esportazione della tabella $table: " . $e->getMessage()]);
}
exit;
}
?>
Esportazione e Caricamento FTP Database
