#!/usr/bin/php
<?php

/**
 * 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)
 **/

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")) {
  if (is_dir("/var/vmail/$virtualhost/.exp")) {
    if (is_file("/var/vmail/$virtualhost/.exp/roundcubemail.json")) {
      unlink("/var/vmail/$virtualhost/.exp/roundcubemail.json");
    }
  } else {
    mkdir("/var/vmail/$virtualhost/.exp", 0750);
    chown("/var/vmail/$virtualhost/.exp", 'vmail');
    chgrp("/var/vmail/$virtualhost/.exp", 'vmail');
  }
} else {
  $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("/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;
}

$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/.exp/roundcubemail.json", json_encode($roundcubemail));
chmod("/var/vmail/$virtualhost/.exp/roundcubemail.json", 0640);
chown("/var/vmail/$virtualhost/.exp/roundcubemail.json", 'vmail');
chgrp("/var/vmail/$virtualhost/.exp/roundcubemail.json", 'vmail');