vmail-stack/bin/vmail-roundcubemail-settings-import.php

179 lines
7.0 KiB
PHP
Raw Normal View History

2021-02-10 16:16:23 -08:00
#!/usr/bin/php
<?php
2022-08-22 13:34:20 -07:00
/**
* vmail-stack
* https://git.stack-source.com/msb/vmail-stack
* Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
* GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
**/
2021-02-10 16:16:23 -08:00
$virtualhost = $argv[1];
if ($virtualhost == "") {
echo "usage:\n";
echo " $argv[0] virtualhost\n";
exit;
}
2021-04-13 14:13:14 -07:00
if (is_readable("/var/vmail/$virtualhost/roundcubemail")) {
$roundcubemail = unserialize(file_get_contents("/var/vmail/$virtualhost/roundcubemail"));
} else {
2021-03-25 15:37:50 -07:00
echo "$virtualhost roundcubemail file does not exist or is not readable by you\n";
2021-02-10 16:16:23 -08:00
exit;
}
2021-04-13 14:13:14 -07:00
if (is_readable("/var/vmail/.my.cnf")) {
$dbHost = trim(`grep -m 1 host /var/vmail/.my.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbDatabase = trim(`grep -m 1 database /var/vmail/.my.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbUser = trim(`grep -m 1 user /var/vmail/.my.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbPass = trim(`grep -m 1 password /var/vmail/.my.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect($dbHost, $dbUser, "$dbPass", $dbDatabase);
} else {
echo "vmail .my.cnf config file does not exist or is not readable by you\n";
exit;
}
2021-02-10 16:16:23 -08:00
$user_id_new = 0;
$identity_id_new = 0;
$contact_id_new = 0;
$contactgroup_id_new = 0;
$contact_id_array_map = array();
foreach ($roundcubemail as $virtualhost=>$users_array) {
foreach ($users_array['users'] as $user_id => $user_array) {
$username = $user_array['username'];
$dbQuery = "SELECT user_id FROM rc_users WHERE username LIKE '$username'";
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows == 0) {
echo "adding $username\n";
$dbQuery = "INSERT INTO rc_users SET";
foreach ($user_array as $kusers=>$vusers) {
if (!is_array($vusers)) {
if ($vusers == 'NULL') {
$dbQuery .= " `$kusers` = $vusers,";
} else {
$vusers = mysqli_escape_string($dbLink, $vusers);
$dbQuery .= " `$kusers` = '$vusers',";
}
}
}
$dbQuery = rtrim($dbQuery, ",");
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows != '1') {
echo "ERROR: $dbResult = $dbAffectedRows\n";
}
$user_id_new = mysqli_insert_id($dbLink);
/* identities */
if (!empty($users_array['users'][$user_id]['identities'])) {
foreach ($users_array['users'][$user_id]['identities'] as $identity_id => $identity_array) {
$identity_array['user_id'] = $user_id_new;
$dbQuery = "INSERT INTO rc_identities SET";
foreach ($identity_array as $kidentities=>$videntities) {
if (!is_array($videntities)) {
if ($videntities == 'NULL') {
$dbQuery .= " `$kidentities` = $videntities,";
} else {
$videntities = mysqli_escape_string($dbLink, $videntities);
$dbQuery .= " `$kidentities` = '$videntities',";
}
}
}
$dbQuery = rtrim($dbQuery, ",");
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows != '1') {
echo "ERROR: $dbResult = $dbAffectedRows\n";
}
$identity_id_new = mysqli_insert_id($dbLink);
}
}
/* contacts */
if (!empty($users_array['users'][$user_id]['contacts'])) {
foreach ($users_array['users'][$user_id]['contacts'] as $contacts_id => $contacts_array) {
$contacts_array['user_id'] = $user_id_new;
$dbQuery = "INSERT INTO rc_contacts SET";
foreach ($contacts_array as $kcontacts=>$vcontacts) {
if (!is_array($vcontacts)) {
if ($vcontacts == 'NULL') {
$dbQuery .= " `$kidentities` = $vcontacts,";
} else {
$vcontacts = mysqli_escape_string($dbLink, $vcontacts);
$dbQuery .= " `$kcontacts` = '$vcontacts',";
}
}
}
$dbQuery = rtrim($dbQuery, ",");
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows != '1') {
echo "ERROR: $dbResult = $dbAffectedRows\n";
}
$contact_id_new = mysqli_insert_id($dbLink);
$contact_id_array_map[$contacts_id] = $contact_id_new;
}
}
/* contactgroups */
if (!empty($users_array['users'][$user_id]['contactgroups'])) {
foreach ($users_array['users'][$user_id]['contactgroups'] as $contactgroups_id => $contactgroups_array) {
$contactgroups_array['user_id'] = $user_id_new;
$dbQuery = "INSERT INTO rc_contactgroups SET";
foreach ($contactgroups_array as $kcontactgroups=>$vcontactgroups) {
if (!is_array($vcontactgroups)) {
if ($vcontactgroups == 'NULL') {
$dbQuery .= " `$kidentities` = $vcontactgroups,";
} else {
$vcontactgroups = mysqli_escape_string($dbLink, $vcontactgroups);
$dbQuery .= " `$kcontactgroups` = \"$vcontactgroups\",";
}
}
}
$dbQuery = rtrim($dbQuery, ",");
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows != '1') {
echo "ERROR: $dbResult = $dbAffectedRows\n";
}
$contactgoup_id_new = mysqli_insert_id($dbLink);
/* contactgroupmembers */
if (!empty($users_array['users'][$user_id]['contactgroups'][$contactgroups_id]['contactgroupmembers'])) {
foreach ($users_array['users'][$user_id]['contactgroups'][$contactgroups_id]['contactgroupmembers'] as $contactgroupmembers_id => $contactgroupmembers_array) {
$contactgroupmembers_array['contactgroup_id'] = $contactgoup_id_new;
$contactgroupmembers_array['contact_id'] = $contact_id_array_map[$contactgroupmembers_array['contact_id']];
$dbQuery = "INSERT INTO rc_contactgroupmembers SET";
foreach ($contactgroupmembers_array as $kcontactgroupmembers=>$vcontactgroupmembers) {
if (!is_array($vcontactgroupmembers)) {
$vcontactgroupmembers = mysqli_escape_string($dbLink, $vcontactgroupmembers);
$dbQuery .= " `$kcontactgroupmembers` = \"$vcontactgroupmembers\",";
}
}
$dbQuery = rtrim($dbQuery, ",");
$dbResult = mysqli_query($dbLink, $dbQuery);
$dbAffectedRows = mysqli_affected_rows($dbLink);
if ($dbAffectedRows != '1') {
echo "ERROR: $dbResult = $dbAffectedRows\n";
}
}
}
}
}
} else {
echo "$username already exists, skipping\n";
}
}
}
echo "Import complete. If it's no longer need you can\n";
echo "remove the file /var/vmail/$virtualhost/roundcubemail\n";