query("SELECT * FROM users_entites"); $entites = $stmt->fetchAll(PDO::FETCH_ASSOC); echo "Nombre d'entités à migrer: " . count($entites) . PHP_EOL; // Préparation de la requête d'insertion $insertQuery = "INSERT INTO entites ( id, encrypted_name, adresse1, adresse2, code_postal, ville, fk_region, fk_type, encrypted_phone, encrypted_mobile, encrypted_email, gps_lat, gps_lng, encrypted_iban, encrypted_bic, chk_demo, chk_mdp_manuel, chk_copie_mail_recu, chk_accept_sms, fk_user_creat, fk_user_modif, chk_active ) VALUES ( :id, :encrypted_name, :adresse1, :adresse2, :code_postal, :ville, :fk_region, :fk_type, :encrypted_phone, :encrypted_mobile, :encrypted_email, :gps_lat, :gps_lng, :encrypted_iban, :encrypted_bic, :chk_demo, :chk_mdp_manuel, :chk_copie_mail_recu, :chk_accept_sms, :fk_user_creat, :fk_user_modif, :chk_active ) ON DUPLICATE KEY UPDATE encrypted_name = VALUES(encrypted_name), adresse1 = VALUES(adresse1), adresse2 = VALUES(adresse2), code_postal = VALUES(code_postal), ville = VALUES(ville), fk_region = VALUES(fk_region), fk_type = VALUES(fk_type), encrypted_phone = VALUES(encrypted_phone), encrypted_mobile = VALUES(encrypted_mobile), encrypted_email = VALUES(encrypted_email), gps_lat = VALUES(gps_lat), gps_lng = VALUES(gps_lng), encrypted_iban = VALUES(encrypted_iban), encrypted_bic = VALUES(encrypted_bic), chk_demo = VALUES(chk_demo), chk_mdp_manuel = VALUES(chk_mdp_manuel), chk_copie_mail_recu = VALUES(chk_copie_mail_recu), chk_accept_sms = VALUES(chk_accept_sms), fk_user_modif = VALUES(fk_user_modif), chk_active = VALUES(chk_active)"; $insertStmt = $targetDb->prepare($insertQuery); // Compteurs $successCount = 0; $errorCount = 0; // Traitement de chaque entité foreach ($entites as $entite) { try { // Mappage des champs entre les deux structures $id = isset($entite['rowid']) ? $entite['rowid'] : $entite['id']; $chkActive = isset($entite['active']) ? $entite['active'] : (isset($entite['chk_active']) ? $entite['chk_active'] : 1); // Chiffrement des données sensibles (uniquement si non vides) $libelle = $entite['libelle'] ?? ''; $encryptedName = !empty($libelle) ? ApiService::encryptData($libelle) : ''; // Traitement des numéros de téléphone $tel1 = $entite['tel1'] ?? ''; $tel2 = $entite['tel2'] ?? ''; // Initialisation des variables $encryptedPhone = ''; $encryptedMobile = ''; // Vérification si tel1 commence par 06 ou 07 (mobile) if (preg_match('/^0[67]/', $tel1)) { $encryptedMobile = ApiService::encryptData($tel1); } elseif (!empty($tel1)) { $encryptedPhone = ApiService::encryptData($tel1); } // Vérification si tel2 commence par 06 ou 07 (mobile) if (preg_match('/^0[67]/', $tel2)) { // Si encryptedMobile est déjà rempli, on garde le premier mobile trouvé if (empty($encryptedMobile)) { $encryptedMobile = ApiService::encryptData($tel2); } elseif (empty($encryptedPhone)) { // Si on a déjà un mobile mais pas de téléphone fixe, on met le second mobile comme téléphone $encryptedPhone = ApiService::encryptData($tel2); } } elseif (!empty($tel2)) { // Si encryptedPhone est déjà rempli, on garde le premier fixe trouvé if (empty($encryptedPhone)) { $encryptedPhone = ApiService::encryptData($tel2); } } // Chiffrement des autres données sensibles (uniquement si non vides) $email = $entite['email'] ?? ''; $iban = $entite['iban'] ?? ''; $bic = $entite['bic'] ?? ''; $encryptedEmail = !empty($email) ? ApiService::encryptSearchableData($email) : ''; $encryptedIban = !empty($iban) ? ApiService::encryptData($iban) : ''; $encryptedBic = !empty($bic) ? ApiService::encryptData($bic) : ''; // Préparation des données pour l'insertion $entiteData = [ 'id' => $id, 'encrypted_name' => $encryptedName, 'adresse1' => $entite['adresse1'] ?? '', 'adresse2' => $entite['adresse2'] ?? '', 'code_postal' => $entite['cp'] ?? '', 'ville' => $entite['ville'] ?? '', 'fk_region' => $entite['fk_region'] ?? null, 'fk_type' => $entite['fk_type'] ?? 1, 'encrypted_phone' => $encryptedPhone, 'encrypted_mobile' => $encryptedMobile, 'encrypted_email' => $encryptedEmail, 'gps_lat' => $entite['gps_lat'] ?? '', 'gps_lng' => $entite['gps_lng'] ?? '', 'encrypted_iban' => $encryptedIban, 'encrypted_bic' => $encryptedBic, 'chk_demo' => 0, // Valeur par défaut à 0 comme demandé 'chk_mdp_manuel' => $entite['chk_mdp_manuel'] ?? 1, 'chk_copie_mail_recu' => $entite['chk_copie_mail_recu'] ?? 0, 'chk_accept_sms' => $entite['chk_accept_sms'] ?? 0, 'fk_user_creat' => $entite['fk_user_modif'] ?? null, 'fk_user_modif' => $entite['fk_user_modif'] ?? null, 'chk_active' => $chkActive ]; // Insertion dans la base cible $insertStmt->execute($entiteData); $successCount++; } catch (Exception $e) { $errorCount++; echo "Erreur lors de la migration de l'entité ID {$id}: " . $e->getMessage() . PHP_EOL; } } echo "Migration terminée. Succès: $successCount, Erreurs: $errorCount" . PHP_EOL; // Fermer le tunnel SSH closeSshTunnel(); } catch (Exception $e) { echo "Erreur critique: " . $e->getMessage() . PHP_EOL; // Fermer le tunnel SSH en cas d'erreur closeSshTunnel(); exit(1); }