vmail-stack/bin/roundcubemail-settings-export.php
Matthew Saunders Brown 856f0cccff added permissions checks
2021-03-25 15:37:50 -07:00

111 lines
4.1 KiB
PHP
Executable File

#!/usr/bin/php
<?php
if (isset($argv[1])) {
$virtualhost = $argv[1];
} else {
echo "usage:\n";
echo " $argv[0] virtualhost\n";
exit;
}
if (!is_dir("/var/vmail/$virtualhost")) {
echo "$virtualhost is not installed\n";
exit;
}
if (!is_writable("/var/vmail/$virtualhost")) {
$user = posix_getpwuid(posix_geteuid());
$username = $user['name'];
echo "/var/vmail/$virtualhost is not writable by this user ($username).\n";
echo "re-run this script as user vmail or root or another user that can write to /var/vmail/$virtualhost\n";
exit;
}
$roundcubemail = array();
if (!is_readable('/usr/local/etc/vmail-db-info.conf')) {
$user = posix_getpwuid(posix_geteuid());
$username = $user['name'];
echo "/var/vmail/$virtualhost is not writable by this user ($username).\n";
echo "re-run this script as user vmail or root or another user that can read the /usr/local/etc/vmail-db-info.conf config file\n";
exit;
}
$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);
$dbQuery = "SELECT * FROM rc_users WHERE username LIKE '%@$virtualhost'";
$dbResult = mysqli_query($dbLink, $dbQuery);
while ($row_users = mysqli_fetch_assoc($dbResult)) {
$user_id=$row_users['user_id'];
unset($row_users['user_id']);
foreach($row_users as $kusers=>$vusers) {
if (is_null($vusers)) {
$vusers = 'NULL';
}
$roundcubemail["$virtualhost"]["users"][$user_id][$kusers] = $vusers;
}
/* identities */
$dbQuery_identities = "SELECT * FROM `rc_identities` WHERE `user_id` = '$user_id'";
$dbResult_identities = mysqli_query($dbLink, $dbQuery_identities);
while ($row_identities = mysqli_fetch_assoc($dbResult_identities)) {
$identity_id=$row_identities['identity_id'];
unset($row_identities['identity_id']);
foreach($row_identities as $kidentities=>$videntities) {
if (is_null($videntities)) {
$videntities = 'NULL';
}
$roundcubemail["$virtualhost"]["users"][$user_id]['identities'][$identity_id][$kidentities] = $videntities;
}
}
/* contacts */
$dbQuery_contacts = "SELECT * FROM `rc_contacts` WHERE `user_id` = '$user_id'";
$dbResult_contacts = mysqli_query($dbLink, $dbQuery_contacts);
while ($row_contacts = mysqli_fetch_assoc($dbResult_contacts)) {
$contact_id=$row_contacts['contact_id'];
unset($row_contacts['contact_id']);
foreach($row_contacts as $kcontacts=>$vcontacts) {
if (is_null($vcontacts)) {
$vcontacts = 'NULL';
}
$roundcubemail["$virtualhost"]["users"][$user_id]['contacts'][$contact_id][$kcontacts] = $vcontacts;
}
}
/* contactgroups */
$dbQuery_contactgroups = "SELECT * FROM `rc_contactgroups` WHERE `user_id` = '$user_id'";
$dbResult_contactgroups = mysqli_query($dbLink, $dbQuery_contactgroups);
while ($row_contactgroups = mysqli_fetch_assoc($dbResult_contactgroups)) {
$contactgroup_id=$row_contactgroups['contactgroup_id'];
unset($row_contactgroups['contactgroup_id']);
foreach($row_contactgroups as $kcontactgroups=>$vcontactgroups) {
if (is_null($vcontactgroups)) {
$vcontactgroups = 'NULL';
}
$roundcubemail["$virtualhost"]["users"][$user_id]['contactgroups'][$contactgroup_id][$kcontactgroups] = $vcontactgroups;
}
/* contactgroupmembers */
$dbQuery_contactgroupmembers = "SELECT * FROM `rc_contactgroupmembers` WHERE `contactgroup_id` = '$contactgroup_id'";
$dbResult_contactgroupmembers = mysqli_query($dbLink, $dbQuery_contactgroupmembers);
while ($row_contactgroupmembers = mysqli_fetch_assoc($dbResult_contactgroupmembers)) {
$roundcubemail["$virtualhost"]["users"][$user_id]['contactgroups'][$contactgroup_id]['contactgroupmembers'][] = $row_contactgroupmembers;
}
}
}
file_put_contents("/var/vmail/$virtualhost/roundcubemail", serialize($roundcubemail));