<?php
    /*
     * $Id: login.php 1439 2009-11-17 23:31:04Z dmorton $
     *
     * MAIA MAILGUARD LICENSE v.1.0
     *
     * Copyright 2004 by Robert LeBlanc <rjl@renaissoft.com>
     *               and 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 any special variables from the GET array
   if (isset($_GET["super"])) {
      $super = trim($_GET["super"]);
      if($super != "register" && $super != "unregister") {
        $logger->err("invalid super parameter");
        $super = "";
      }
   } else {
      $super = "";
   }

   // Determine the initial language preference,
   // either from default or from manually selected link
   if (isset($_GET["lang"]) && strlen($_GET["lang"]) == 2 ) {
      $display_language = trim($_GET["lang"]);
      $display_language_is_default = false;
   } elseif (isset($_GET["prevlang"]) && strlen($_GET["prevlang"]) == 2) {
      $display_language = trim($_GET["prevlang"]);
      $display_language_is_default = true;
   } else {
      $display_language = $default_display_language;
      $display_language_is_default = true;
   }
   require_once ("./locale/$display_language/db.php");
   require_once ("./locale/$display_language/display.php");
   require_once ("./locale/$display_language/login.php");

   // some default values so that smarty initialization doesn't complain about undefined variables
   $uid = 0;
   $euid = 0;
   $msid = "";
   $sid = ""; 
   
   



   // Determine the server's clock time, in seconds since 1970-01-01 00:00:00
   // This will be compared with the client's clock to determine the offset
   $server_timestamp = getdate();
   $server_timestamp = $server_timestamp[0] - date('Z'); // Convert to UTC
   
   require_once ("smarty.php");
   $smarty->assign("super", $super);
   $smarty->assign("server_timestamp", $server_timestamp);
   
   // Check for an NTDOMAIN cookie
   if ($auth_method == "exchange") {
       if ($auth_exchange_only_one_domain) {
           $nt_domain = $auth_exchange_nt_domain;
       } else {
       	   if (isset($_COOKIE["NTDOMAIN"])) {
       	       $nt_domain = trim($_COOKIE["NTDOMAIN"]);
       	   } else {
       	       $nt_domain = $auth_exchange_nt_domain;
       	   }
       }
       $smarty->assign('nt_domain', $nt_domain);
   }
   
   $smarty->assign('auth_method', $auth_method);
   $smarty->assign('routing_domain', $routing_domain);

   $select = "SELECT abbreviation, language_name FROM maia_languages " .
              "WHERE abbreviation <> ? AND installed = 'Y' ORDER BY language_name ASC";
   $sth = $dbh->query($select, array($display_language));
   
   $languages = array(); 
   
   if ($sth->numrows() > 0) {
      while ($row = $sth->fetchrow()) {
         $languages[$row["abbreviation"]] = $row["language_name"];
      }
   } 
   
   $sth->free();
   $smarty->assign("languages", $languages);
   $smarty->assign("display_language", $display_language);
   $smarty->assign("display_language_is_default", $display_language_is_default);
   $smarty->display("login.tpl");
   
?>
