2021-02-10 16:16:23 -08:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$virtualhost = $argv[1];
|
|
|
|
|
|
|
|
if ($virtualhost == "") {
|
|
|
|
echo "usage:\n";
|
|
|
|
echo " $argv[0] virtualhost\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:25:44 -07:00
|
|
|
if (!is_file("/var/vmail/$virtualhost/roundcubemail")) {
|
2021-02-10 16:16:23 -08:00
|
|
|
echo "$virtualhost roundcubemail file does not exist\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:25:44 -07:00
|
|
|
$roundcubemail = unserialize(file_get_contents("/var/vmail/$virtualhost/roundcubemail"));
|
2021-02-10 16:16:23 -08:00
|
|
|
|
2021-03-17 13:25:44 -07:00
|
|
|
$dbHost = trim(`grep -m 1 host /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
|
|
|
|
$dbDatabase = trim(`grep -m 1 database /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
|
|
|
|
$dbUser = trim(`grep -m 1 user /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
|
|
|
|
$dbPass = trim(`grep -m 1 password /usr/local/etc/vmail-db-info.conf |cut -d = -f 2|cut -d ' ' -f 2`);
|
|
|
|
$dbLink = mysqli_connect($dbHost, $dbUser, "$dbPass", $dbDatabase);
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 13:25:44 -07:00
|
|
|
|
|
|
|
echo "Import complete. If it's no longer need you can\n";
|
|
|
|
echo "remove the file /var/vmail/$virtualhost/roundcubemail\n";
|