<?php
    /*
     * $Id: xadminviruses.php 950 2006-02-12 20:49:19Z 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");

    // Only the superadministrator should be here.
    if (!is_superadmin($uid)) {
       header("Location: index.php" . $sid);
       exit();
    }

    // Find out which button we pushed to get here
    if (isset($_POST["add"])) {

    	$virus = trim($_POST["virus"]);
    	$alias = trim($_POST["alias"]);

    	// Make sure we're not aliasing the virus to itself
    	if ($virus <> $alias) {

            // Get the name of the alias
    	    $select = "SELECT virus_name, count FROM maia_viruses WHERE id = ?";
    	    $sth = $dbh->query($select, array($alias));
    	    if ($row = $sth->fetchrow()) {
    	        $alias_name = $row["virus_name"];
    	        $alias_count = $row["count"];
    	    }
    	    $sth->free();

    	    // Make sure the alias doesn't already exist
    	    $select = "SELECT virus_id FROM maia_virus_aliases WHERE virus_alias = ?";
    	    $sth = $dbh->query($select, array($alias_name));
    	    if (!$sth->fetchrow()) {

    	    	// Create the new alias
    	    	$insert = "INSERT INTO maia_virus_aliases (virus_id, virus_alias) VALUES (?, ?)";
    	    	$dbh->query($insert, array($virus, $alias_name));

    	    	// Get the received count of the actual virus
    	    	$select = "SELECT count FROM maia_viruses WHERE id = ?";
    	    	$sth2 = $dbh->query($select, array($virus));
    	    	if ($row2 = $sth2->fetchrow()) {
    	    	    $virus_count = $row2["count"];
    	    	}
    	    	$sth2->free();

    	    	// Add the received count from the alias to the actual virus
    	    	$update = "UPDATE maia_viruses SET count = ? WHERE id = ?";
    	    	$dbh->query($update, array($virus_count + $alias_count, $virus));

    	    	// Replace any references to the alias with virus references
    	    	$update = "UPDATE maia_viruses_detected SET virus_id = ? WHERE virus_id = ?";
    	    	$dbh->query($update, array($virus, $alias));

    	    	// Delete the 'alias' from the viruses table
    	    	$delete = "DELETE FROM maia_viruses WHERE id = ?";
    	    	$dbh->query($delete, array($alias));

    	    }
    	    $sth->free();
        }

    } elseif (isset($_POST["delete"])) {

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

        $select = "SELECT virus_alias, virus_id FROM maia_virus_aliases";
        $sth = $dbh->query($select);
        while ($row = $sth->fetchrow()) {

            $alias_name = $row["virus_alias"];
            $virus_id = $row["virus_id"];

            // Replace '.' with '_' to match form submission variable names
            $alias_var_name = str_replace(".", "_", $alias_name);

            if (isset($formVars[$alias_var_name]) && trim($formVars[$alias_var_name]) == $virus_id) {

            	// Delete the alias
            	$delete = "DELETE FROM maia_virus_aliases WHERE virus_alias = ? AND virus_id = ?";
            	$dbh->query($delete, array($alias_name, $virus_id));

            }
        }
        $sth->free();

    }

    // Return to the virus administration page
    header("Location: adminviruses.php" . $sid);
    exit();
?>
