<?php
    /*
     * $Id: mailtools.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");


    /*
     * get_user_from_email(): extracts the "user" portion of a "user@domain" address,
     *                        or "" if the address contains no '@'.
     */
    function get_user_from_email($email)
    {
    	global $auth_method;
    	global $address_rewriting_type;

    	if (($auth_method == "imap" || $auth_method == "pop3") && ($address_rewriting_type == 4)) {
    	    $user = $email;
    	} else {
            $user = "";
            $pos = strpos($email, '@');
            if (!($pos === false)) {
                $user = substr($email, 0, $pos);
            }
        }

        return $user;
    }


    /*
     * get_domain_from_email(): Extracts the "domain" portion of a "user@domain"
     *                          address, or "" if the address contains no '@'.
     */
    function get_domain_from_email($email)
    {
        $domain = "";
        $pos = strpos($email, '@');
        if (!($pos === false)) {
            $domain = substr($email, $pos+1);
        }

        return strtolower($domain);
    }



    /*
     * get_rewritten_email_address(): Rewrites an IMAP e-mail address to account
     *                           for a routing domain and any server-specific
     *                           conventions, if specified.
     */
    function get_rewritten_email_address($email, $format)
    {
    	global $dbh;
    	global $routing_domain;

        if ($format == 1) { // user.domain@isp

            $address = str_replace("@", ".", $email) .
                            "@" . $routing_domain;

        } elseif ($format == 2) { // user_domain@isp

            $tmp = str_replace("@", "_", $email);
            $tmp = str_replace(".", "_", $tmp);
            $address = $tmp . "@" . $routing_domain;

        } elseif ($format == 3) { // user@domain@isp

            $address = $email . "@" . $routing_domain;

        } else { // no rewriting

            $address = $email;

        }

        return $address;
    }


    /*
     * get_address_type(): Determines the "type" of an e-mail address,
     *                     according to its format:
     *
     *                     0 : "domain"
     *                     1 : "@domain"
     *                     2 : "user@domain"
     */
    function get_address_type($email)
    {
        $pos = strpos($email, '@');
        if ($pos === false) {  // "domain"
            return 0;
        } elseif ($pos > 0) {  // "user@domain"
            return 2;
        } else {               // "@domain"
            return 1;
        }
    }


    /*
     * get_email_address_priority(): Determines an appropriate priority
     *                               for the specified e-mail address,
     *                               according to the number of '.'
     *                               characters it contains.  More specific
     *                               addresses get higher priority values.
     *
     *                               Domain-class addresses start at priority 0:
     *
     *                               e.g. "example.com" = 2
     *                                    "mail.example.com" = 4
     *
     *                               User-class addresses start at priority 10:
     *
     *                               e.g. "user@example.com" = 12
     *                                    "user@mail.example.com" = 14
     */
    function get_email_address_priority($email)
    {
        $address_type = get_address_type($email);
        if ($address_type == 2) {   // "user@domain"
            $base = 10;
        } else {
            $base = 0;
        }

        return ($base + 2 * substr_count($email, "."));
    }


    /*
     * fix_address(): Makes sure that e-mail addresses are either in
     *                "user@domain" format or "@domain" format.
     */
    function fix_address($email)
    {
       if (get_address_type($email) == 0) {
          $my_addr = "@" . $email;
       } else {
          $my_addr = $email;
       }
       return $my_addr;
    }
?>
