@@ -97,10 +111,30 @@
$dbname = $_POST['dbname'];
$filePath = 'actions/database.php';
$fileContent = file_get_contents($filePath);
- $fileContent = preg_replace("/\\\$dbname = '';/", "\$dbname = '$dbname';", $fileContent);
+ $fileContent = preg_replace_callback('/\\$dbname\s*=\s*\'[^\']*\';/', function($m) use ($dbname) {
+ return '$dbname = ' . var_export($dbname, true) . ';';
+ }, $fileContent);
file_put_contents($filePath, $fileContent);
- unlink('actions/bookfind.sql');
- echo '
';
+ include 'actions/database.php';
+ $sqlFilePath = 'actions/bookfind.sql';
+ if (file_exists($sqlFilePath)) {
+ $sql = file_get_contents($sqlFilePath);
+ $sql = preg_replace('/CREATE DATABASE\s+IF NOT EXISTS.*?;|CREATE DATABASE.*?;|USE `.*?`;/is', '', $sql);
+ $queries = array_filter(array_map('trim', explode(';', $sql)));
+ foreach ($queries as $query) {
+ if (!empty($query)) {
+ try {
+ $bdd->exec($query);
+ } catch (Exception $e) {
+ echo '
Erreur d\'exécution SQL : ' . htmlspecialchars($e->getMessage()) . '
';
+ }
+ }
+ }
+ unlink($sqlFilePath);
+ echo '
';
+ } else {
+ echo '
Fichier SQL introuvable.
';
+ }
} else {
echo '
Identifiants MySQL manquants. Remplissez d\'abord le formulaire plus haut.
';
}
@@ -108,27 +142,84 @@
if (isset($_POST['import'])) {
if (!empty($host) && !empty($username)) {
+ // Si l'utilisateur demande d'importer uniquement les tables
+ if (!empty($_POST['onlyTables'])) {
+ $dbnameInput = !empty($_POST['dbname_import']) ? trim($_POST['dbname_import']) : 'bookfind';
+ $filePath = 'actions/database.php';
+ $fileContent = file_get_contents($filePath);
+ $fileContent = preg_replace_callback('/\\$dbname\s*=\s*\'[^\']*\';/', function($m) use ($dbnameInput) {
+ return '$dbname = ' . var_export($dbnameInput, true) . ';';
+ }, $fileContent);
+ file_put_contents($filePath, $fileContent);
+ // Reconnecter sur la base fournie
+ include 'actions/database.php';
+ $sqlFilePath = 'actions/bookfind.sql';
+ if (file_exists($sqlFilePath)) {
+ $sql = file_get_contents($sqlFilePath);
+ $cleanSql = preg_replace('/CREATE DATABASE\\s+IF NOT EXISTS.*?;|CREATE DATABASE.*?;|USE `.*?`;/is', '', $sql);
+ $queries = array_filter(array_map('trim', explode(';', $cleanSql)));
+ foreach ($queries as $query) {
+ if (!empty($query)) {
+ try {
+ $bdd->exec($query);
+ } catch (Exception $e) {
+ echo '
Erreur d\'exécution SQL : ' . htmlspecialchars($e->getMessage()) . '
';
+ }
+ }
+ }
+ unlink($sqlFilePath);
+ echo '
';
+ } else {
+ echo '
Fichier SQL introuvable.
';
+ }
+ // Fin du flux d'import uniquement des tables
+ goto end_import;
+ }
+
$filePath = 'actions/database.php';
$fileContent = file_get_contents($filePath);
- $fileContent = preg_replace("/\\\$dbname = '';/", "\$dbname = 'bookfind';", $fileContent);
+ $fileContent = preg_replace_callback('/\\$dbname\s*=\s*\'[^\']*\';/', function($m) {
+ return '$dbname = ' . var_export('bookfind', true) . ';';
+ }, $fileContent);
file_put_contents($filePath, $fileContent);
$sqlFilePath = 'actions/bookfind.sql';
- $sql = file_get_contents($sqlFilePath);
- $bdd->exec('CREATE DATABASE bookfind');
- $queries = array_filter(array_map('trim', explode(';', $sql)));
- foreach ($queries as $query) {
- if (!empty($query)) {
- if ($bdd->query($query) === false) {
- echo '
Erreur d\'exécution SQL.
';
+ if (file_exists($sqlFilePath)) {
+ $sql = file_get_contents($sqlFilePath);
+ // Créer la base sans se connecter à une base inexistante
+ try {
+ $tmpDsn = 'mysql:host=' . $host . ';charset=utf8';
+ $tmpPdo = new PDO($tmpDsn, $username, $password);
+ $tmpPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $tmpPdo->exec('CREATE DATABASE IF NOT EXISTS `bookfind` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci');
+ } catch (Exception $e) {
+ echo '
Impossible de créer la base : ' . htmlspecialchars($e->getMessage()) . '
';
+ // On arrête ici
+ goto end_import;
+ }
+ // Recharger la config pour se connecter sur la nouvelle base
+ include 'actions/database.php';
+ // Retirer les éventuelles lignes CREATE DATABASE / USE du dump avant exécution
+ $cleanSql = preg_replace('/CREATE DATABASE\\s+IF NOT EXISTS.*?;|CREATE DATABASE.*?;|USE `.*?`;/is', '', $sql);
+ $queries = array_filter(array_map('trim', explode(';', $cleanSql)));
+ foreach ($queries as $query) {
+ if (!empty($query)) {
+ try {
+ $bdd->exec($query);
+ } catch (Exception $e) {
+ echo '
Erreur d\'exécution SQL : ' . htmlspecialchars($e->getMessage()) . '
';
+ }
}
}
+ unlink($sqlFilePath);
+ echo '
';
+ } else {
+ echo '
Fichier SQL introuvable.
';
}
- unlink('actions/bookfind.sql');
- echo '
';
} else {
echo '
Identifiants MySQL manquants. Remplissez d\'abord le formulaire plus haut.
';
}
}
+ end_import:
?>