<?php
    /*
     * $Id: xdomainsettings.php 1439 2009-11-17 23:31:04Z dmorton $
     *
     * MAIA MAILGUARD LICENSE v.1.0
     *
     * Copyright 2004 by Robert LeBlanc <rjl@renaissoft.com>
     *                   David Morton   <mortonda@dgrmm.net>
     * All rights reserved.
     *
     * PREAMBLE
     *
     * This License is designed for users of Maia Mailguard
     * ("the Software") who wish to support the Maia Mailguard project by
     * leaving "Maia Mailguard" branding information in the HTML output
     * of the pages generated by the Software, and providing links back
     * to the Maia Mailguard home page.  Users who wish to remove this
     * branding information should contact the copyright owner to obtain
     * a Rebranding License.
     *
     * DEFINITION OF TERMS
     *
     * The "Software" refers to Maia Mailguard, including all of the
     * associated PHP, Perl, and SQL scripts, documentation files, graphic
     * icons and logo images.
     *
     * GRANT OF LICENSE
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * 3. The end-user documentation included with the redistribution, if
     *    any, must include the following acknowledgment:
     *
     *    "This product includes software developed by Robert LeBlanc
     *    <rjl@renaissoft.com>."
     *
     *    Alternately, this acknowledgment may appear in the software itself,
     *    if and wherever such third-party acknowledgments normally appear.
     *
     * 4. At least one of the following branding conventions must be used:
     *
     *    a. The Maia Mailguard logo appears in the page-top banner of
     *       all HTML output pages in an unmodified form, and links
     *       directly to the Maia Mailguard home page; or
     *
     *    b. The "Powered by Maia Mailguard" graphic appears in the HTML
     *       output of all gateway pages that lead to this software,
     *       linking directly to the Maia Mailguard home page; or
     *
     *    c. A separate Rebranding License is obtained from the copyright
     *       owner, exempting the Licensee from 4(a) and 4(b), subject to
     *       the additional conditions laid out in that license document.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */

    require_once ("core.php");
    require_once ("authcheck.php");
    require_once ("display.php");
    require_once ("maia_db.php");
    require_once ("auth.php");
    $display_language = get_display_language($euid);
    require_once ("./locale/$display_language/display.php");
    require_once ("./locale/$display_language/db.php");
    require_once ("./locale/$display_language/auth.php");
    require_once ("./locale/$display_language/domainsettings.php"); // shared with domainsettings.php

    require_once ("smarty.php");
    
    if (isset($_POST["domain_id"])) {
        $domain_id = trim($_POST["domain_id"]);
    } else {
    	$domain_id = 0;
    }

    // Only administrators with rights to this domain should be here.
    if (!is_admin_for_domain($uid, $domain_id)) {
       header("Location: index.php" . $sid);
       exit();
    }

    // Is this administrator the superadmin, or just a
    // domain admin?
    $super = is_superadmin($uid);

    // Pressed the "Update This Domain's Settings" button
    if (isset($_POST['update_domain'])) {

        $select = "SELECT enable_charts, reminder_threshold_count, " .
                         "enable_spamtraps " .
                  "FROM maia_config WHERE id = 0";
        $sth = $dbh->query($select);
        if ($row = $sth->fetchrow()) {
            $enable_charts = ($row["enable_charts"] == 'Y');
            $reminder_threshold_count = $row["reminder_threshold_count"];
            $enable_spamtraps = ($row["enable_spamtraps"] == 'Y');
        }
        $sth->free();

        if (isset($_POST["policy"])) {
            $policy_id = trim($_POST["policy"]);
        } else {
            $policy_id = 0;
        }

    	$select = "SELECT virus_lover, " .
    	                 "spam_lover, " .
    	                 "banned_files_lover, " .
    	                 "bad_header_lover, " .
    	                 "bypass_virus_checks, " .
    	                 "bypass_spam_checks, " .
    	                 "bypass_banned_checks, " .
    	                 "bypass_header_checks, " .
                         "discard_viruses, " .
  	                 "discard_spam, " .
  	                 "discard_banned_files, " .
  	                 "discard_bad_headers, " .
    	                 "spam_modifies_subj, " .
    	                 "spam_tag_level, " .
    	                 "spam_tag2_level, " .
    	                 "spam_kill_level " .
    	          "FROM policy WHERE id = ?";
    	$sth = $dbh->query($select, array($policy_id));
    	if ($row = $sth->fetchRow()) {
    	    $default_quarantine_viruses = ($row["virus_lover"] == "N");
    	    $default_quarantine_spam = ($row["spam_lover"] == "N");
    	    $default_quarantine_banned_files = ($row["banned_files_lover"] == "N");
    	    $default_quarantine_bad_headers = ($row["bad_header_lover"] == "N");
    	    $default_virus_scanning = ($row["bypass_virus_checks"] == "N");
    	    $default_spam_filtering = ($row["bypass_spam_checks"] == "N");
    	    $default_banned_files_checking = ($row["bypass_banned_checks"] == "N");
    	    $default_bad_header_checking = ($row["bypass_header_checks"] == "N");
            $default_discard_viruses = ($row["discard_viruses"] == "Y");
  	    $default_discard_spam = ($row["discard_spam"] == "Y");
  	    $default_discard_banned_files = ($row["discard_banned_files"] == "Y");
  	    $default_discard_bad_headers = ($row["discard_bad_headers"] == "Y");
    	    $default_modify_subject = ($row["spam_modifies_subj"] == "Y");
    	    $default_score_level_1 = $row["spam_tag_level"];
    	    $default_score_level_2 = $row["spam_tag2_level"];
    	    $default_score_level_3 = $row["spam_kill_level"];
        }
        $sth->free();
        $select = "SELECT maia_users.id, maia_users.discard_ham, maia_domains.enable_user_autocreation, maia_users.theme_id " .
  	                   "FROM maia_users, maia_domains " .
  	                   "WHERE maia_domains.domain = maia_users.user_name " .
  	                   "AND maia_domains.id = ?";
  	$sth = $dbh->query($select, array($domain_id));
  	if ($row = $sth->fetchrow()) {
  	    $domain_user_id = $row["id"];
  	    $default_discard_ham = ($row["discard_ham"] == "Y");
  	    $default_enable_user_autocreation = ($row["enable_user_autocreation"] == "Y");
        $default_theme_id = $row["theme_id"];
  	}
  	$sth->free();

        if (isset($_POST["viruses"])) {
            $viruses = trim($_POST["viruses"]);
        } else {
            $viruses = ($default_virus_scanning ? "yes" : "no");
        }
        if (isset($_POST["virus_destiny"])) {
            $virus_destiny = trim($_POST["virus_destiny"]);
        } else {
            $virus_destiny = ($default_quarantine_viruses ? ($default_discard_viruses ? "discard" : "quarantine") : "label");
        }
        if (isset($_POST["spam"])) {
            $spam = trim($_POST["spam"]);
        } else {
            $spam = ($default_spam_filtering ? "yes" : "no");
        }
        if (isset($_POST["spam_destiny"])) {
            $spam_destiny = trim($_POST["spam_destiny"]);
        } else {
            $spam_destiny = ($default_quarantine_spam ? ($default_discard_spam ? "discard" : "quarantine") : "label");
        }
        if (isset($_POST["banned"])) {
            $banned = trim($_POST["banned"]);
        } else {
            $banned = ($default_banned_files_checking ? "yes" : "no");
        }
        if (isset($_POST["banned_destiny"])) {
            $banned_destiny = trim($_POST["banned_destiny"]);
        } else {
	    $banned_destiny = ($default_quarantine_banned_files ? ($default_discard_banned_files ? "discard" : "quarantine") : "label");
        }
        if (isset($_POST["headers"])) {
            $headers = trim($_POST["headers"]);
        } else {
            $headers = ($default_bad_header_checking ? "yes" : "no");
        }
        if (isset($_POST["headers_destiny"])) {
            $headers_destiny = trim($_POST["headers_destiny"]);
        } else {
	    $headers_destiny = ($default_quarantine_bad_headers ? ($default_discard_bad_headers ? "discard" : "quarantine") : "label");
        }
        if (isset($_POST["modify_subject"])) {
            $modify_subject = trim($_POST["modify_subject"]);
        } else {
            $modify_subject = ($default_modify_subject ? "yes" : "no");
        }
        if (isset($_POST["level1"])) {
            $level1 = trim($_POST["level1"]);
        } else {
            $level1 = $default_score_level_1;
        }
        if (isset($_POST["level2"])) {
            $level2 = trim($_POST["level2"]);
        } else {
            $level2 = $default_score_level_2;
        }
        if (isset($_POST["level3"])) {
            $level3 = trim($_POST["level3"]);
        } else {
            $level3 = $default_score_level_3;
        }
        if (isset($_POST["discard_ham"])) {
            $discard_ham = trim($_POST["discard_ham"]);
        } else {
            $discard_ham = ($default_discard_ham ? "discard" : "cache");
        }
        if (isset($_POST["enable_user_autocreation"])) {
  	        $enable_user_autocreation = trim($_POST["enable_user_autocreation"]);
        } else {
            $enable_user_autocreation = ($default_enable_user_autocreation ? "yes" : "no");
        }
        if (isset($_POST["theme_id"])) {
            $theme_id = (trim($_POST["theme_id"]));
        } else {
            $theme_id = $default_theme_id;
        }

        $bypass_virus_checks = ($viruses == "yes" ? "N" : "Y");
	$virus_lover = ($virus_destiny == "label" ? "Y" : "N");	
	$discard_viruses = ($virus_destiny == "discard" ? "Y" : "N");
        $bypass_spam_checks = ($spam == "yes" ? "N" : "Y");
        $spam_lover = ($spam_destiny == "label" ? "Y" : "N");
  	$discard_spam = ($spam_destiny == "discard" ? "Y" : "N");
        $bypass_banned_checks = ($banned == "yes" ? "N" : "Y");
        $banned_files_lover = ($banned_destiny == "label" ? "Y" : "N");
  	$discard_banned_files = ($banned_destiny == "discard" ? "Y" : "N");
        $bypass_header_checks = ($headers == "yes" ? "N" : "Y");
	$bad_header_lover = ($headers_destiny == "label" ? "Y" : "N");
  	$discard_bad_headers = ($headers_destiny == "discard" ? "Y" : "N");
        $spam_modifies_subj = ($modify_subject == "yes" ? "Y" : "N");
        $spam_tag_level = (double) $level1;
        $spam_tag2_level = (double) $level2;
        $spam_kill_level = (double) $level3;
        $discard_ham = ($discard_ham == "cache" ? "N" : "Y");
	$enable_user_autocreation = ($enable_user_autocreation == "yes" ? "Y" : "N");

        // Enforce tag_level <= tag2_level <= kill_level
        if ($spam_tag2_level > $spam_kill_level) {
            $spam_tag2_level = $spam_kill_level;
        }
        if ($spam_tag_level > $spam_tag2_level) {
            $spam_tag_level = $spam_tag2_level;
        }
        // Enforce spam kill level according to spam destiny
	if ($spam_destiny == "label") {
  	    $spam_kill_level = 999;
  	} else {
  	    $spam_kill_level = $spam_tag2_level;
  	}

        // Update the policy table with the new settings.
        $update = "UPDATE policy SET virus_lover = ?, " .
                                    "spam_lover = ?, " .
                                    "banned_files_lover = ?, " .
                                    "bad_header_lover = ?, " .
                                    "bypass_virus_checks = ?, " .
                                    "bypass_spam_checks = ?, " .
                                    "bypass_banned_checks = ?, " .
                                    "bypass_header_checks = ?, " .
                                    "discard_viruses = ?, " .
                                    "discard_spam = ?, " .
                                    "discard_banned_files = ?, " .
                                    "discard_bad_headers = ?, " .
                                    "spam_modifies_subj = ?, " .
                                    "spam_tag_level = ?, " .
                                    "spam_tag2_level = ?, " .
                                    "spam_kill_level = ? " .
                  "WHERE id = ?";
        $dbh->query($update, array($virus_lover,
                                   $spam_lover,
                                   $banned_files_lover,
                                   $bad_header_lover,
                                   $bypass_virus_checks,
                                   $bypass_spam_checks,
                                   $bypass_banned_checks,
                                   $bypass_header_checks,
                                   $discard_viruses,
                                   $discard_spam,
                                   $discard_banned_files,
                                   $discard_bad_headers,
                                   $spam_modifies_subj,
                                   $spam_tag_level,
                                   $spam_tag2_level,
                                   $spam_kill_level,
                                   $policy_id));

        $update = "UPDATE maia_users SET discard_ham = ?, theme_id = ? WHERE id = ?";
        $dbh->query($update, array($discard_ham, $theme_id, $domain_user_id));

	$update = "UPDATE maia_domains SET enable_user_autocreation = ? WHERE id = ?";
  	$dbh->query($update, array($enable_user_autocreation, $domain_id));	

        $message = $lang['text_settings_updated'];
    // Pressed the "Revoke Administrator Privileges" button
    } elseif ($super && (isset($_POST['revoke']))) {

        // Register the full set of POST variables.
        foreach($_POST as $varname => $value)
        {
            $formVars[$varname] = trim($value);
        }

        $select = "SELECT maia_users.id, maia_users.user_name " .
                  "FROM maia_users, maia_domain_admins " .
                  "WHERE maia_users.id = maia_domain_admins.admin_id " .
                  "AND maia_domain_admins.domain_id = ?";
        $sth = $dbh->query($select, array($domain_id));

        while ($row = $sth->fetchrow()) {

            $admin_name = $row["user_name"];
            $admin_var_name = str_replace(".", "_", $admin_name);
            $admin_var_name = str_replace("[", "{", $admin_var_name);
            $admin_var_name = str_replace("]", "}", $admin_var_name);
            $admin_id = $row["id"];

            if (isset($formVars[$admin_var_name]) && trim($formVars[$admin_var_name]) == $admin_id) {

            	// Remove the link between this administrator and this domain
                $delete = "DELETE FROM maia_domain_admins WHERE domain_id = ? AND admin_id = ?";
                $dbh->query($delete, array($domain_id, $admin_id));

                // If this administrator doesn't control any remaining domains,
                // demote him to a regular (U)ser.
                $select = "SELECT domain_id FROM maia_domain_admins WHERE admin_id = ?";
                $sth2 = $dbh->query($select, array($admin_id));
                if (!$sth2->fetchrow()) {
         	    $update = "UPDATE maia_users SET user_level = 'U' WHERE id = ?";
        	    $dbh->query($update, array($admin_id));
        	}
        	$sth2->free();
            }
        }
        $sth->free();

        $message = $lang['text_admins_revoked'];

    // Pressed the "Grant Administrator Privileges" button
    } elseif ($super && (isset($_POST['grant']))) {

        // Note that $admins is an array
        if (isset($_POST["administrators"])) {
            $admins = $_POST["administrators"];

            foreach ($admins as $admin_id) {

            	// Link the administrator to this domain
            	$insert = "INSERT INTO maia_domain_admins (admin_id, domain_id) VALUES (?, ?)";
            	$dbh->query($insert, array($admin_id, $domain_id));

            	// Change the user's privilege level to Domain (A)dministrator
            	$update = "UPDATE maia_users SET user_level = 'A' WHERE id = ?";
            	$dbh->query($update, array($admin_id));

            	$message = $lang['text_administrators_added'];

            }
        }
    }
    $smarty->assign('message', $message);
    $smarty->assign('domain_id', $domain_id);
    
    $smarty->display("xdomainsettings.tpl");
    
?>
