167 lines
6.7 KiB
PHP
Executable File
167 lines
6.7 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
$virtualhost = $argv[1];
|
|
|
|
if ($virtualhost == "") {
|
|
echo "usage:\n";
|
|
echo " $argv[0] virtualhost\n";
|
|
exit;
|
|
}
|
|
|
|
if (!is_readable("/var/vmail/$virtualhost/roundcubemail")) {
|
|
echo "$virtualhost roundcubemail file does not exist or is not readable by you\n";
|
|
exit;
|
|
}
|
|
|
|
$roundcubemail = unserialize(file_get_contents("/var/vmail/$virtualhost/roundcubemail"));
|
|
|
|
$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);
|
|
|
|
$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";
|