function modifyForward($aliasId, $destination, $active)
{
    if ($active) {
        $active = 't';
    } else {
        $active = 'f';
    }
    if (!$aliasId || !$destination || !$active) {
        return FALSE;
    }
    $email = getAliasEmail($aliasId);
    if (!$email) {
        return FALSE;
    }
    if ($email != $_SESSION['user']['email']) {
        $adminDomains = getAdminDomains();
        $emailParts = split('@', $email);
        $domain = $emailParts[1];
        if (!in_array($domain, $adminDomains)) {
            return FALSE;
        }
    }
    if (!userExists($email)) {
        return FALSE;
    } else {
        if (!validEmailAddress($destination)) {
            return FALSE;
        }
    }
    $destinationParts = split('@', $destination);
    $allDomainsHash = getAllDomains();
    $allDomains = array();
    foreach ($allDomainsHash as $domain) {
        $allDomains[] = $domain['domain'];
    }
    if (in_array($destinationParts[1], $allDomains)) {
        if (!userExists($destination)) {
            return FALSE;
        }
    }
    $updates = array('destination' => $destination, 'active' => $active);
    $conditions = array('alias_id' => $aliasId);
    beginTransaction();
    $ret = db_update('virtual_aliases', $updates, $conditions);
    if ($active == 'f') {
        setUserLocalIfNecessary($email);
    }
    endTransaction();
}
function addCatchAll($domainId, $destination, $active)
{
    if ($active) {
        $active = 't';
    } else {
        $active = 'f';
    }
    $foundError = FALSE;
    $errors = array();
    if (!$domainId) {
        $errors['domain'] = 'This field is required';
        $foundError = TRUE;
    }
    if (!$destination) {
        $errors['destination'] = 'This field is required';
        $foundError = TRUE;
    }
    if (!$active) {
        $errors['active'] = 'This field is required';
        $foundError = TRUE;
    }
    if ($foundError) {
        print json_encode(array('success' => false, 'errors' => $errors));
        return FALSE;
    }
    $domain = getDomain($domainId);
    if (!$domain) {
        $errors['domain'] = 'Invalid domain';
        $foundError = TRUE;
    }
    if (!validEmailAddress($destination)) {
        $errors['destination'] = 'Invalid destination';
        $foundError = TRUE;
    }
    if ($foundError) {
        print json_encode(array('success' => false, 'errors' => $errors));
        return FALSE;
    }
    $destinationParts = split('@', $destination);
    $allDomainsHash = getAllDomains();
    $allDomains = array();
    foreach ($allDomainsHash as $tmpDomain) {
        $allDomains[] = $tmpDomain['domain'];
    }
    if (in_array($destinationParts[1], $allDomains)) {
        if (!userExists($destination)) {
            print json_encode(array('success' => false, 'errors' => array('destination' => 'User does not exist in local domain: ' . $destinationParts[1])));
            return FALSE;
        }
    }
    $adminDomains = getAdminDomains();
    if (!in_array($domain, $adminDomains)) {
        $errors['domain'] = 'Permission denied on domain: ' . $domain;
        $foundError = TRUE;
    }
    $email = '@' . $domain;
    if (aliasExists($email, $destination)) {
        $errors['destination'] = 'Catch all already exists';
        $foundError = TRUE;
    }
    if ($foundError) {
        print json_encode(array('success' => false, 'errors' => $errors));
        return FALSE;
    }
    $params = array('username' => '', 'domain_id' => $domainId, 'destination' => $destination, 'active' => $active);
    $ret = db_insert('virtual_aliases', $params, 'alias_id');
    if ($ret) {
        print json_encode(array('success' => TRUE));
        return;
    }
    print json_encode(array('success' => FALSE, 'msg' => 'Unknown error'));
}