<?php
    /*
     * $Id: settings.php 1519 2011-05-21 21:53:25Z 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 ("auth.php");
    require_once ("display.php");
    require_once ("maia_db.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/settings.php");
    require_once ("./locale/$display_language/domainsettings.php");

    $message = ""; //initialize variable

    // get some system config items - these control whether or not to show certain options
    $select = "SELECT enable_charts, reminder_threshold_count, " .
                     "enable_spamtraps, enable_username_changes, " .
                     "enable_address_linking " .
              "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');
        $enable_username_changes = ($row["enable_username_changes"] == 'Y');
        $enable_address_linking = ($row["enable_address_linking"] == 'Y');
    }
    $sth->free();
    $super = is_superadmin($uid);
    
    require_once ("smarty.php");
    $smarty->assign('enable_charts', $enable_charts);
    $smarty->assign('reminder_threshold_count', $reminder_threshold_count);
    $smarty->assign('enable_spamtraps', $enable_spamtraps);
    $smarty->assign('enable_username_changes', $enable_username_changes);
    $smarty->assign('enable_address_linking', $enable_address_linking);

    $smarty->assign("message", $message);

    // verify and set up domain variables if the current focus is a domain user
    if (is_a_domain_default_user($euid)) {
        $domain_user = true;
        $domain_name = get_user_name($euid);
        $domain_id = get_domain_id($domain_name);
        
    } else {
         $domain_user = false;
         $domain_name = "";
         $domain_id ="";
    }
    $smarty->assign('domain_user', $domain_user);
    $smarty->assign('domain_name', $domain_name);
    $smarty->assign('domain_id', $domain_id);
    $smarty->assign("euid", $euid);
    $smarty->assign('super', $super);


    //verify that the supplied address is valid for the current user
    if (isset($_GET["addid"])) {
       $address_id = trim($_GET["addid"]);
       if (get_email_address_owner($address_id) != $euid) {
           header("Location: index.php$sid");
           exit;
       }
    } else {
        if ($domain_user) {
           $address_id = get_primary_email_id($euid);
        } else {
           $address_id = 0;
        }
    }
    $smarty->assign('address_id', $address_id);

    if (isset($_GET['tab'])) {
        $smarty->assign('tab', $_GET['tab']);
    } else {
        $smarty->assign('tab', 0);
    }

    if ($address_id == 0) {  // for user addresslist display only - domains now have this set nonzero, ie, this also means domain_user == false
        $smarty->assign("auth_method", $auth_method);
        if (($auth_method == "pop3" && !empty($routing_domain)) ||
            $auth_method == "ldap" || $auth_method == "exchange" ||
            $auth_method == "sql" || $auth_method == "internal" || 
            $auth_method == "external") {
            $smarty->assign('login', $lang['text_username']);
        } else {
            $smarty->assign('login', $lang['text_email_address']);
        }

        // get primary address
        $primary_email_id = get_primary_email_id($euid);
        $smarty->assign('primary_email_id', $primary_email_id);
        $smarty->assign('address', get_email_address_by_id($primary_email_id));

        // get the rest of the addresses
        $select = "SELECT id, email FROM users WHERE maia_user_id = ? AND email NOT LIKE '@%' AND id <> ?";
        $sth = $dbh->query($select, array($euid, $primary_email_id));
        $user_addr = array();
        while ($row = $sth->fetchRow()) {
            $smarty->assign('atleastone', true);
            $user_addr[] = array(
              'addid' =>  $row["id"],
              'address' => $row["email"]
            );
        }
        $smarty->assign('user_addr', $user_addr);
        $sth->free();

        if ($auth_method == "internal") {
            $smarty->assign('user_name', get_user_name($euid));
        }
    }

    if ($address_id == 0 || $domain_user) {  // run this unless only displaying only the address info, ie, one non domain address
        $select = "SELECT id, name FROM maia_themes";
        $sth = $dbh->query($select);
        $themes = array();
        while ($row = $sth->fetchrow()) {
            $themes[$row['id']] = $row['name'];
        }
        $smarty->assign("themes", $themes);
   
        $select = "SELECT charts, reminders, language, spamtrap, auto_whitelist, items_per_page, quarantine_digest_interval, discard_ham, theme_id, truncate_subject, truncate_email " .
                  "FROM maia_users WHERE maia_users.id = ?";
        $sth = $dbh->query($select, array($euid));
        if ($row = $sth->fetchRow()) {
            if ($row["charts"] == 'Y') {
                $smarty->assign('ch_y_checked', "checked");
                $smarty->assign('ch_n_checked', "");
            } else {
                $smarty->assign('ch_y_checked', "");
                $smarty->assign('ch_n_checked', "checked");
            }
            if ($row["reminders"] == 'Y') {
                $smarty->assign('rm_y_checked', "checked");
                $smarty->assign('rm_n_checked', "");
            } else {
                $smarty->assign('rm_y_checked', "");
                $smarty->assign('rm_n_checked', "checked");
            }
            if ($row["spamtrap"] == 'Y') {
                $smarty->assign('st_y_checked', "checked");
                $smarty->assign('st_n_checked', "");
            } else {
                $smarty->assign('st_y_checked', "");
                $smarty->assign('st_n_checked', "checked");
            }
            if ($row["auto_whitelist"] == 'Y') {
                $smarty->assign('wl_y_checked', "checked");
                $smarty->assign('wl_n_checked', "");
            } else {
                $smarty->assign('wl_y_checked', "");
                $smarty->assign('wl_n_checked', "checked");
            }
            $smarty->assign('language', $row["language"]);
            $smarty->assign('items_per_page', $row["items_per_page"]);
            $quarantine_digest_interval = intval($row["quarantine_digest_interval"]);
            if ($quarantine_digest_interval > 0 && $quarantine_digest_interval < 60) {  //adjusting from minutes to hours.
              $quarantine_digest_interval = 1;   // if less than an hour, make it one hour.
            } else if ($quarantine_digest_interval > 1) {
              $quarantine_digest_interval = intval($quarantine_digest_interval / 60); //otherwise round down to the nearest hour.
            }
            $smarty->assign('quarantine_digest_interval',
                             $quarantine_digest_interval);
            $smarty->assign('theme_id', $row["theme_id"]);
            $smarty->assign('discard_ham', $row['discard_ham']);
            $smarty->assign("truncate_subject", $row["truncate_subject"]);
            $smarty->assign("truncate_email", $row["truncate_email"]);
        }
        $sth->free();
        $smarty->assign('atleastone', false);


        $select = "SELECT language_name, abbreviation FROM maia_languages " .
                 "WHERE installed = 'Y' " .
                 "ORDER BY language_name ASC";
        $sth = $dbh->query($select);
        $data = array();
        $opt_lang = array();
        while ($row = $sth->fetchrow()) {
            $opt_lang[$row['abbreviation']] = $row['language_name'];
        }
        $smarty->assign('opt_lang', $opt_lang);
        $sth->free();
    }


    if (is_an_administrator($uid)) {  //admins can change these settings
            $smarty->assign('user_virus_scanning', true);
            $smarty->assign('user_spam_filtering', true);
            $smarty->assign('user_banned_files_checking', true);
            $smarty->assign('user_bad_header_checking', true);
    } else {  // but everyone else is subject to the system config
            $select = "SELECT user_virus_scanning, " .
                             "user_spam_filtering, " .
                             "user_banned_files_checking, " .
                             "user_bad_header_checking " .
                             "FROM maia_config WHERE id = 0";
            $sth = $dbh->query($select);

            if ($row = $sth->fetchrow()) {
                $smarty->assign('user_virus_scanning', ($row["user_virus_scanning"] == 'Y'));
                $smarty->assign('user_spam_filtering', ($row["user_spam_filtering"] == 'Y'));
                $smarty->assign('user_banned_files_checking', ($row["user_banned_files_checking"] == 'Y'));
                $smarty->assign('user_bad_header_checking', ($row["user_bad_header_checking"] == 'Y'));
            }
            $sth->free();
    }

    if ($domain_user) {   // the following settings apply only to domain users
        $select = "SELECT enable_user_autocreation, routing_domain, transport " .
                   "FROM maia_domains " .
                   "WHERE maia_domains.id = ?";
        $sth = $dbh->query($select, array($domain_id));
        if ($row = $sth->fetchrow()) {
            $smarty->assign('enable_user_autocreation', $row["enable_user_autocreation"]);
            $smarty->assign('routing_domain', $row["routing_domain"]);
            $smarty->assign('transport', $row["transport"]);
        }
        $sth->free();

        $select = "SELECT maia_users.user_name, maia_users.id " .
              "FROM maia_users, maia_domain_admins " .
              "WHERE maia_users.id = maia_domain_admins.admin_id " .
              "AND maia_domain_admins.domain_id = ? " .
              "ORDER BY maia_users.user_name ASC";
        $sth = $dbh->query($select, array($domain_id));
        $admins = array();
        if (($rowcount = $sth->numrows()) > 0) {
            while ($row = $sth->fetchrow()) {
                $admins[] = array(
                    'id' => $row["id"],
                    'name' => $row["user_name"],
                    'var_name' => $row["user_name"]
                );
            }
        }
        $smarty->assign('admins', $admins);

        $sth->free();

        $select = "SELECT maia_users.id " .
              "FROM maia_users, maia_domain_admins " .
              "WHERE maia_users.id = maia_domain_admins.admin_id " .
              "AND maia_domain_admins.domain_id = ?";
        $rows = $dbh->getall($select, array($domain_id));
        $id_list = "";
        foreach ($rows as $row) {
            if (!empty($id_list)) {
                $id_list .= "," . $row["id"];
            } else {
                $id_list = $row["id"];
            }
        }

        $select = "SELECT user_name, id " .
                  "FROM maia_users " .
                  "WHERE user_level <> 'S' " .
                  "AND user_name NOT like '@%' ";
        if (!empty($id_list)) {
            $select .= "AND id NOT IN (" . $id_list . ") ";
        }
        $select .= "ORDER BY user_name ASC";
        $sth = $dbh->query($select);

        if ($sth->numrows()) {
            $add_admins = array();
            while ($row = $sth->fetchrow()) {
                $add_admins[] = array(
                    'id' => $row["id"],
                    'name' => $row["user_name"]
                );
            }
            $smarty->assign('add_admins', $add_admins);
        }
        $sth->free();
    }

    // domain users or individual addresses, this is for the thresholds tab
    if ($address_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, " .
                         "email, " .
                         "policy_id " .
                  "FROM users, policy " .
                  "WHERE users.policy_id = policy.id " .
                  "AND users.id = ?";

        $sth = $dbh->query($select, array($address_id));
        if ($row = $sth->fetchRow()) {
            $smarty->assign('address', $row["email"]);
            $smarty->assign('policy_id', $row["policy_id"]);
            $smarty->assign('level1', $row["spam_tag_level"]);
            $smarty->assign('level2', $row["spam_tag2_level"]);
            $smarty->assign('level3', $row["spam_kill_level"]);
            if ($row["virus_lover"] == 'Y') {
                $smarty->assign('v_l_checked', "checked");
                $smarty->assign('v_q_checked', "");
                $smarty->assign('v_d_checked', "");
            } else {
                $smarty->assign('v_l_checked', "");
                if ($row["discard_viruses"] == 'Y') {
                    $smarty->assign('v_q_checked', "");
                    $smarty->assign('v_d_checked', "checked");
                } else {
                    $smarty->assign('v_q_checked', "checked");
                    $smarty->assign('v_d_checked', "");
                }
            }
            if ($row["spam_lover"] == 'Y') {
                $smarty->assign('s_l_checked', "checked");
                $smarty->assign('s_q_checked', "");
                $smarty->assign('s_d_checked', "");
            } else {
                $smarty->assign('s_l_checked', "checked");
                if ($row["discard_spam"] == 'Y') {
                    $smarty->assign('s_q_checked', "");
                    $smarty->assign('s_d_checked', "checked");
                } else {
                    $smarty->assign('s_q_checked', "checked");
                    $smarty->assign('s_d_checked', "");
                }
            }
            if ($row["banned_files_lover"] == 'Y') {
                $smarty->assign('b_l_checked', "checked");
                $smarty->assign('b_q_checked', "");
                $smarty->assign('b_d_checked', "");
            } else {
                $smarty->assign('b_l_checked', "checked");
                if ($row["discard_banned_files"] == 'Y') {
                    $smarty->assign('b_q_checked', "");
                    $smarty->assign('b_d_checked', "checked");
                } else {
                    $smarty->assign('b_q_checked', "checked");
                    $smarty->assign('b_d_checked', "");
                }
            }
            if ($row["bad_header_lover"] == 'Y') {
                $smarty->assign('h_l_checked', "checked");
                $smarty->assign('h_q_checked', "");
                $smarty->assign('h_d_checked', "");
            } else {
                $smarty->assign('h_l_checked', "checked");
                if ($row["discard_bad_headers"] == 'Y') {
                    $smarty->assign('h_q_checked', "");
                    $smarty->assign('h_d_checked', "checked");
                } else {
                    $smarty->assign('h_q_checked', "checked");
                    $smarty->assign('h_d_checked', "");
                }
            }
            if ($row["bypass_virus_checks"] == 'Y') {
                $smarty->assign('bv_y_checked', "");
                $smarty->assign('bv_n_checked', "checked");
            } else {
                $smarty->assign('bv_y_checked', "checked");
                $smarty->assign('bv_n_checked', "");
            }
            if ($row["bypass_spam_checks"] == 'Y') {
                $smarty->assign('bs_y_checked', "");
                $smarty->assign('bs_n_checked', "checked");
            } else {
                $smarty->assign('bs_y_checked', "checked");
                $smarty->assign('bs_n_checked', "");
            }
            if ($row["bypass_banned_checks"] == 'Y') {
                $smarty->assign('bb_y_checked', "");
                $smarty->assign('bb_n_checked', "checked");
            } else {
                $smarty->assign('bb_y_checked', "checked");
                $smarty->assign('bb_n_checked', "");
            }
            if ($row["bypass_header_checks"] == 'Y') {
                $smarty->assign('bh_y_checked', "");
                $smarty->assign('bh_n_checked', "checked");
            } else {
                $smarty->assign('bh_y_checked', "checked");
                $smarty->assign('bh_n_checked', "");
            }
            if ($row["spam_modifies_subj"] == 'Y') {
                $smarty->assign('sms_y_checked', "checked");
                $smarty->assign('sms_n_checked', "");
            } else {
                $smarty->assign('sms_y_checked', "");
                $smarty->assign('sms_n_checked', "checked");
            }
        }
        $sth->free();
    }

    $smarty->display('settings.tpl');
?>
