<?php
    /*
     * $Id: welcome.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 ("maia_db.php");
    require_once ("authcheck.php");
    require_once ("display.php");
    $display_language = get_display_language($euid);
    require_once ("./locale/$display_language/db.php");
    require_once ("./locale/$display_language/display.php");
    require_once ("./locale/$display_language/welcome.php");
    
    require_once ("smarty.php");

    if (isset($_GET["id"])) {
        $id = trim($_GET["id"]);
        if (($id != 0) && (!is_an_administrator($uid))) {
            $id = $euid;
        }
    } else {
        $id = $euid;
    }
    
   if (isset($_POST["maxitemid"])) {
      $maxitemid = $_POST["maxitemid"];
   } else {
      $maxitemid = 0;
   }
   
   if (isset($_POST['delete_all_items']))
   {  
      $del_list = array();
      $select = "SELECT mail_id FROM maia_mail_recipients " .
                "WHERE type IN ('S','P','V','F','B','H') " .
                "AND recipient_id = ? " .
                "AND mail_id <= ?";

      $sth = $dbh->query($select, array($euid, $maxitemid));
      while ($row = $sth->fetchRow())
      {
         array_push($del_list, $row["mail_id"]);
      }
      $sth->free();
      delete_mail_reference($euid, $del_list);
   }
   
   
   if (isset($_POST['change_protection']) && isset($_POST['protection_level'])) {
    $select = "SELECT policy_id FROM users WHERE maia_user_id = ?"; 
    $sth = $dbh->query($select, $euid);
 
    $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 = ?";
    $protection_level = $_POST["protection_level"];
                  
    while ($row = $sth->fetchrow()) {
      	$dbh->query($update, array_merge($protection[$protection_level], array($row['policy_id'])));
    	
    }
    $sth->free();
    
    
    
   }
    
    update_mail_stats($id, "suspected_ham");
    update_mail_stats($id, "suspected_spam");
    
    $cache_count = count_cache_items($euid);
    
    if (array_key_exists('S', $cache_count)) { // includes 'P' labeled suspected spam
        $spamcount = $cache_count['S']["count"];
        $maxspamid = $cache_count['S']["max"];
    } else {
        $spamcount = 0;
        $maxspamid = 0;
    }
   

    if (array_key_exists('V', $cache_count)) {
        $viruscount = $cache_count['V']["count"];
        $maxvirusid = $cache_count['V']["max"];
    } else {
        $viruscount = 0;
        $maxvirusid = 0;
    }

    if (array_key_exists('F', $cache_count)) {
        $bannedcount = $cache_count['F']["count"];
        $maxbannedid = $cache_count['F']["max"];
    } else {
        $bannedcount = 0;
        $maxbannedid = 0;
    }

    if (array_key_exists('B', $cache_count)) {
        $headercount = $cache_count['B']["count"];
        $maxheaderid = $cache_count['B']["max"];
    } else {
        $headercount = 0;
        $maxheaderid = 0;
    }
    
    if (array_key_exists('H', $cache_count)) {
        $hamcount = $cache_count['H']["count"];
        $maxhamid = $cache_count['H']["max"];
    } else {
        $hamcount = 0;
        $maxhamid = 0;
    }
    
    $maxitemid = max($maxspamid, $maxvirusid, $maxbannedid, $maxheaderid, $maxhamid);
    
    // find current protection level
   $query = "SELECT DISTINCT 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 users LEFT JOIN policy ON users.policy_id=policy.id ".
             "WHERE users.maia_user_id = ?"; 
    $sth = $dbh->query($query, array($euid));
    
    
    
    
    
    if ($sth->numRows() > 1) { 
   	$protection_mode = "custom";
    } elseif ($sth->numrows() ==1) {
        $result = $sth->fetchrow();	
    	if (implode(',',$result) == implode(',',$protection['off'])){
    		$protection_mode = 'off';		
    	} elseif (implode(',',$result) == implode(',',$protection['low'])){
    		$protection_mode = 'low';		
    	} elseif(implode(',',$result) == implode(',',$protection['medium'])){
    		$protection_mode = 'medium';		
    	} elseif(implode(',',$result) == implode(',',$protection['high'])){
    		$protection_mode = 'high';		
    	} else {
    		$protection_mode = 'custom';
    	}
    } else { 
    //ERROR?
    	$protection_mode = "ERROR";	
    }

    $smarty->assign("protection", $protection_mode);
   
   
	if (isset($_SESSION["firsttime"])) {
		$smarty->assign("firsttime",true);
	} else {
		$smarty->assign("firsttime",false);
	}
	$smarty->assign("lang",$lang);
//	$smarty->assign("enable_false_negative_management", "T");
//	$smarty->assign("showmail","T");
	$smarty->assign("maxitemid",$maxitemid);
	$smarty->assign("msid",$msid);
	$smarty->assign("hamcount", $hamcount);
	$smarty->assign("spamcount", $spamcount);
	$smarty->assign("bannedcount", $bannedcount);
	$smarty->assign("headercount", $headercount);
	$smarty->assign("viruscount", $viruscount);
	$smarty->assign("hamtext", sprintf($lang['text_cache_ham'], $hamcount, "list-cache.php{$msid}cache_type=ham"));
	$smarty->assign("spamtext", sprintf($lang['text_cache_spam'], $spamcount, "list-cache.php{$msid}cache_type=spam"));
	$smarty->assign("virustext", sprintf($lang['text_cache_virus'], $viruscount, "list-cache.php{$msid}cache_type=virus"));
	$smarty->assign("bannedtext", sprintf($lang['text_cache_banned'], $bannedcount, "list-cache.php{$msid}cache_type=attachment"));
	$smarty->assign("headertext", sprintf($lang['text_cache_header'], $headercount, "list-cache.php{$msid}cache_type=header"));


	$smarty->assign("spam_for_user", count_items($euid, "spam"));
	$smarty->assign("virus_for_user", count_items($euid, "virus"));
	$smarty->assign("spam_for_system", count_items(0, "spam"));
	$smarty->assign("virus_for_system", count_items(0, "virus"));

    $smarty->display("welcome.tpl");

?>
