<?php
    /*
     * $Id: smarty.php 1456 2010-02-07 23:12:58Z 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.
     *
     */
 
    if (isset($smarty_path)) {
      ini_set('include_path', $smarty_path . ":" . ini_get('include_path'));
      require_once ("Smarty.class.php");
    } else {
      require_once ("Smarty/Smarty.class.php");
    }
    require_once ("./locale/$display_language/db.php");
    // define local smarty-class
    class ThemeSmarty extends Smarty {
        var $_basedir = '';
        
        function ThemeSmarty($theme = 'default', $spath = '') {
            $this->Smarty();
            $this->_basedir = 'themes/'.$theme.'/';
            $this->template_dir = $this->_basedir.'templates/';
            $this->compile_dir = $this->_basedir.'compiled/';
            $this->config_dir = $this->_basedir.'config';
            $this->cache_dir = $this->_basedir.'cached/';
            $this->caching = false;
            if (!empty($spath)) {
                $this->plugins_dir = array($this->_basedir.'code/', $spath . '/plugins');
            } else {
                $this->plugins_dir = array($this->_basedir.'code/', "Smarty/plugins");
            }
            $this->assign('template_dir', 'themes/'.$theme.'/');
        }
        
        function getBaseDir() {
           return $this->_basedir;
        }
    }
    
    $select = "SELECT banner_title, use_logo, use_icons, logo_file, logo_url, logo_alt_text, " .
              "enable_false_negative_management, enable_stats_tracking, enable_user_autocreation " .
              "FROM maia_config WHERE id = 0";
    $sth = $dbh->query($select);
    if ($row = $sth->fetchrow()) {
        $banner_title = $row["banner_title"];
        $use_logo = ($row["use_logo"] == 'Y');
        $use_icons = ($row["use_icons"] == 'Y');
        $logo_file = $row["logo_file"];
        $logo_url = $row["logo_url"];
        $logo_alt_text = $row["logo_alt_text"];
        $enable_false_negative_management = ($row["enable_false_negative_management"] == 'Y');
        $enable_stats_tracking = ($row["enable_stats_tracking"] == 'Y');
        $enable_user_autocreation = ($row['enable_user_autocreation'] == 'Y');
    }
      
    $showmenu = true;
    if (!$uid && !$euid) {
        $username = "";
        $is_a_visitor = true;
    } else {

        $username = who_am_i($uid, $euid);
        $is_a_visitor = false;
    }
    $showmail = ok_to_impersonate($euid, $uid);
    if ($showmenu) {
        $admin = is_an_administrator($uid);
        $cols = 5;
        if ($enable_stats_tracking) {
            $cols++;
        }
        if ($enable_false_negative_management) {
            $cols++;
        }
        if ($admin) {
            $cols++;
        }
//         if (!$showmail) {
//             $cols -= 2;
//         }
    } else {
        $cols = 1;
        $admin = false;
    }
    
    $select = "SELECT maia_users.id, user_name, maia_themes.path ".
              "FROM maia_users ".
              "LEFT JOIN maia_themes ON maia_users.theme_id = maia_themes.id ".
              "WHERE maia_users.id = ? OR maia_users.user_name = '@.' ".
              "ORDER BY maia_users.id DESC";
    $sth = $dbh->query($select, array($uid));
    if ($row = $sth->fetchrow()) {
        $path = $row['path'];
    
    } else { // this really should not be reachable
        $path = "default";
    }
    
    if (! (is_dir("themes/$path") && is_dir("themes/$path/templates") && is_readable("themes/$path/templates"))) {
        exit($lang['error_themes']);
    }

    if(! is_writable("themes/$path/compiled")) {
      exit($lang["error_permissions"]);
    }

    // create new smarty-instance
    //$smarty =& new ThemeSmarty('default');
    if (isset($smarty_path)) {
        $smarty = new ThemeSmarty($path, $smarty_path);
    } else {
        $smarty = new ThemeSmarty($path);
    }
    
    $include = $smarty->getBaseDir()."code/smarty.php";
    if(file_exists($include) && is_readable($include)) {
       require_once ($include);
    }
    
    $include = "./locale/$display_language/local.php";
    if (file_exists($include) && is_readable($include)) {
       require_once ($include);
    }
    
    $include = $smarty->getBaseDir(). "locale/$display_language/local.php";
    if (file_exists($include) && is_readable($include)) {
       require_once ($include);
    }
    
    list($logo_width, $logo_height, $logo_type, $logo_attr) = getimagesize($smarty->getBaseDir() . $logo_file);

    if (!isset($html_charset)) {
      $html_charset="UTF-8";
    }
    
    $smarty->assign('session_timeout', $default_session_timeout * 60);
    $smarty->assign('MAIA_VERSION', MAIA_VERSION);
    $smarty->assign('display_language', $display_language);
    $smarty->assign('html_charset', $html_charset);
    header("Content-Type: text/html; $html_charset");
    $smarty->assign("msid", $msid);
    $smarty->assign("sid", $sid);
    $smarty->assign('banner_title', $banner_title);
    $smarty->assign('use_logo', $use_logo);
    $smarty->assign('use_icons', $use_icons);
    $smarty->assign('logo_file', $logo_file);
    $smarty->assign('logo_width', $logo_width);
    $smarty->assign('logo_height', $logo_height);
    $smarty->assign('logo_url', $logo_url);
    $smarty->assign('logo_alt_text', $logo_alt_text);
    $smarty->assign('enable_false_negative_management', $enable_false_negative_management);
    $smarty->assign('system_enable_user_autocreation', $enable_user_autocreation);
    $smarty->assign('enable_stats_tracking', $enable_stats_tracking);
    $smarty->assign('cols', $cols);
    $smarty->assign('admin', $admin);
    $smarty->assign('username', $username);
    $smarty->assign('showmenu', $showmenu);
    $smarty->assign('super', is_superadmin($uid));
    $smarty->assign("lang", $lang);
    $smarty->assign("is_a_visitor", $is_a_visitor);  // added by JacobLeaver, response to ticket 	
	$smarty->assign("php_errors", isset($php_errors) ? $php_errors : "");
	// some default values, which can be overridden
	$smarty->assign("page_css", "");
	$smarty->assign("page_javascript", "");
	$message = isset($message) ? $message : "";
	$message .= isset($_SESSION["message"]) ? $_SESSION["message"] : "";
	$_SESSION["message"] = ""; // unset message, we don't want to display more than once.
	                              // if page is to be redirected again, reassign it.
	$smarty->assign("message", $message);
?>
