#!/usr/bin/php
<?php

$virtualhost = $argv[1];

if ($virtualhost == "") {
  echo "usage:\n";
  echo "  $argv[0] virtualhost\n";
  exit;
}

if (!is_dir("/var/www/$virtualhost")) {
  echo "$virtualhost is not installed\n";
  exit;
}

$roundcubemail = array();

$dbPass = trim(`grep -m 1 password /etc/mysql/debian.cnf |cut -d = -f 2|cut -d ' ' -f 2`);
$dbLink = mysqli_connect('localhost', 'root', "$dbPass", 'vmail');


$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/www/$virtualhost/roundcubemail", serialize($roundcubemail));