<?php
/* $Id: appconfig.inc.php,v 1.52 2005/03/09 21:51:57 leyeti Exp $ */

if (!defined('ROOTPATH')) die("This is a library.");

if (!defined('USE_DB_SESSIONS')) define('USE_DB_SESSIONS',false);
if (!isset($appconf["forcessl"])) $appconf["forcessl"] = '0';

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.52 $", __FILE__);

// The general templates path (we need it because smarty lacks of several template paths)
$appconf["gentemplates"] = "file:".ROOTPATH."/modules/general/templates/" . $appconf["theme"] . "/".trim($myEnv["output"]);
$appconf["genpath"]=ROOTPATH."/modules/general/templates/" . $appconf["theme"];

// Path to images (module specific and general)
$appconf["imgpathmaster"] = ROOTURL ."modules/general/templates/" . $appconf["theme"] . "/media";
$appconf["localimgpathmaster"] = ROOTPATH ."/modules/general/templates/" . $appconf["theme"] . "/media";
$appconf["imgpath"] = ROOTURL ."modules/" . $myEnv["module"] . "/templates/" .$appconf["theme"] ."/media";
$appconf["localimgpath"] = ROOTPATH ."/modules/" . $myEnv["module"] . "/templates/" .$appconf["theme"] ."/media";
$appconf["imgpathgeneral"] = ROOTURL . "modules/general/templates/". $appconf["theme"] . "/media";
$appconf["localimgpathgeneral"] = ROOTPATH . "/modules/general/templates/". $appconf["theme"] . "/media";

// URL to current script folder
$appconf["loginurl"] = ROOTURL . "index.php";

// path to templates and configs (smarty)
$smarty_tmpl  = ROOTPATH ."/modules/" . $myEnv["module"] . "/templates/" .$appconf["theme"] ."/" . $myEnv["output"];
$smarty_tmplc = ROOTPATH . "/templates_c";
// $smarty_cfg   = ROOTPATH . "/configs";

$smarty = new Smarty;
// use_sub_dirs=false is the default
//if(ini_get('safe_mode')) $smarty->use_sub_dirs = false;
//@$smarty->config_dir=$smarty_cfg;

// default modifier for security
$smarty->default_modifiers = array('strip_tags');
// modifiers declared in smarty_plugins.php
require_once(INCLUDEPATH.'/smarty_plugins.php');

// define centralized storage/cache folders
define('STORE_FOLDER', "../../data_store/".$myEnv['module']."/");
define('STORE_URL', ROOTURL."/data_store/".$myEnv['module']."/");
define('CACHE_FOLDER', "../../data_cache/".$myEnv['module']."/");
define('CACHE_URL', ROOTURL."/data_cache/".$myEnv['module']."/");

foreach (array(ROOTPATH."/data_store/",ROOTPATH."/data_cache/",ROOTPATH."/data_store/".$myEnv['module']."/",ROOTPATH."/data_cache/".$myEnv['module']."/") as $dirname) {
  if (!is_dir($dirname)) {
    if (is_writeable(ROOTPATH."/")) {
	  mkdir($dirname,0777);
    } else {die("Please give write access to ".ROOTPATH."/");}
  }
		
  if (!file_exists($dirname."index.htm")) {
    $fd = fopen($dirname."index.htm", "w");
    fwrite($fd, "You are not allowed to view this folder.");
    fclose($fd);
  }
  if (!file_exists($dirname."index.html")) {
    $fd = fopen($dirname."index.html", "w");
    fwrite($fd, "You are not allowed to view this folder.");
    fclose($fd);
  }
}

session_name($appconf["session_name"]);
if(USE_DB_SESSIONS){
  // use adodb session tracking
  $ADODB_SESSION_DRIVER=$appconf['dbvendor'];
  $ADODB_SESSION_CONNECT=$appconf['dbhost'];
  $ADODB_SESSION_USER=$appconf['dbuser'];
  $ADODB_SESSION_PWD=$appconf['dbpassword'];
  $ADODB_SESSION_DB=$appconf['dbname'];
  $ADODB_SESSION_TBL = 'mgw_sessions';
  //require_once(INCLUDEPATH . '/adodb/session/adodb-cryptsession.php');
  require_once(INCLUDEPATH . '/adodb/session/adodb-session.php');
}
session_start();

// if MGW hasn't been registered, do so here.  Important for tracking the current url,
// should a url other than moregroupware/index.php be called first.
if(!isset($_SESSION["MGW"])) {
    $_SESSION["MGW"]= new MGW();
}

$rows_per_page=10;

// Define Output Type (wml, html,...)
if (!isset($_SESSION['content_type']) || isset($ctype)) {
    if(isset($ctype)) {
	$content_type = $ctype;
    }
    else {
	$content_type = "html";
    }
    $_SESSION["content_type"] = $content_type;
}
if(IsSet($content_type) && $content_type=="wml") {
    header('Content-Type: text/vnd.wap.wml');
    $myEnv["output"] = "wml";
    $smarty->template_dir = ROOTPATH ."/modules/" . $myEnv["module"] . "/templates/" . $appconf["theme"] . "/" . $myEnv["output"];
    $smarty->compile_dir = ROOTPATH . "/templates_wml_c";

    // redefine it because of wml (this must be changed in the future, because this is crap code)
    $app_gentemplates = "file:" . ROOTPATH ."/modules/general/templates/" .$appconf["theme"] ."/".trim($myEnv["output"]);

    $rows_per_page=5;
}
else {
    $smarty->template_dir=$smarty_tmpl;
    $smarty->compile_dir=$smarty_tmplc;
}

//allways assign path to general template w/o theme (mainly for css)
$smarty->assign("genpath",$appconf["genpath"]);

// allways assign path to images (module and general)
$smarty->assign("imgpathmaster",$appconf["imgpathmaster"]);
$smarty->assign("localimgpathmaster",$appconf["localimgpathmaster"]);
$smarty->assign("imgpath",$appconf["imgpath"]);
$smarty->assign("localimgpath",$appconf["localimgpath"]);
$smarty->assign("imgpathgeneral",$appconf["imgpathgeneral"]);
$smarty->assign("localimgpathgeneral",$appconf["localimgpathgeneral"]);

if (defined('SPONSOR_LOGO')) $smarty->assign("sponsor_logo",SPONSOR_LOGO);
if (defined('SPONSOR_LINK')) $smarty->assign("sponsor_link",SPONSOR_LINK);

// SID allways assign into templates
$smarty->assign("SESSION", SID);
$smarty->assign("SESSIONID", session_id());
$smarty->assign("SESSIONNAME", session_name());

// always assign rooturl and rootpath
$smarty->assign("rooturl", ROOTURL);
$smarty->assign("rootpath", ROOTPATH);

// assign skin (css)
if(isset($_SESSION["MGW"]->settings["css_skin_theme"])) {
    $smarty->assign("skin", $_SESSION["MGW"]->settings["css_skin_theme"]);
}

if (!isset($_SESSION["MGW"]->settings["show_navi_images"])) {
  $_SESSION["MGW"]->settings["show_navi_images"] = 0;
}

if ($_SESSION["MGW"]->browser=="msie" or $_SESSION["MGW"]->browser=="mozilla") {
    $smarty->assign("show_navi_images", $_SESSION["MGW"]->settings["show_navi_images"]);
} elseif ($_SESSION["MGW"]->browser=="opera") {
  if ($myEnv["module"]=="admin2" or $myEnv["module"]=="webmail2" or $myEnv["module"]=="phpbb") {
    $smarty->assign("show_navi_images", 4);
  } else {
    $smarty->assign("show_navi_images", $_SESSION["MGW"]->settings["show_navi_images"]);
  }
} else {
  $smarty->assign("show_navi_images", 4);
}

if(file_exists(ROOTPATH."/modules/" . $myEnv["module"] . "/templates/" .$appconf["theme"] ."/media/module.css"))
     $smarty->assign('modulecss',ROOTURL ."modules/" . $myEnv["module"] . "/templates/" .$appconf["theme"] ."/media/module.css");

// assign theme (as defined during setup)
$smarty->assign("theme", $appconf["theme"]);

// username und userfullname allways assign
if(isset($_SESSION["MGW"]->username))
     $smarty->assign("username", $_SESSION["MGW"]->username);
	
if(isset($_SESSION["MGW"]->fullusername))
     $smarty->assign("userfullname", $_SESSION["MGW"]->fullusername);

// Umgebungsvar $PHP_SELF allways assign
$smarty->assign("phpself", ROOTURL."modules/".$myEnv["module"]."/".basename($_SERVER["PHP_SELF"]));
$PHP_SELF = $_SERVER["PHP_SELF"];

// General template path allways assign
$smarty->assign("gentemplates", $appconf["gentemplates"]);

// assign actual modulename / name of folder where module resides
$smarty->assign("actmodule", $myEnv["module"]);
if(isset($myEnv["submenu"]))
     $smarty->assign("actsubmenu", $myEnv["submenu"]);
else
     $smarty->assign("actsubmenu", 0);

// Version und Name allways assign
$smarty->assign("gwversion", "[Release $gwversion]");
$smarty->assign("gwtitle", $gwtitle);

if(isset($_SESSION["MGW"]->charset))
     $smarty->assign("charset", $_SESSION["MGW"]->charset);

$conn = connect_database();
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

$lu_date = $conn->DBTimeStamp(time());

//start lgging facilities (all levels for core modules)
$GLOBALS["log"] = new log(MGW_FATAL+MGW_ERROR+MGW_WARNING+MGW_NOTICE+MGW_INFO);
$smarty->assign("mgw_log_id", $GLOBALS["log"]->logId);

//logging on?
$sql = "SELECT value_int FROM mgw_config WHERE configtagname='enable_logging' ORDER BY userid DESC";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$row = $res->fetchRow();
if ((int)$row["value_int"]!=1) {
    $GLOBALS["log"]->off();
    $smarty->assign("mgw_log_enable", 0);
} else {
    $GLOBALS["log"]->on();
    $smarty->assign("mgw_log_enable", 1);
}
$GLOBALS["log"]->message(MGW_INFO, "Logging started", __LINE__, __FILE__, "general");

//start notification
$GLOBALS["notify"] = new notify();

$myEnv["label"] = "";
$myEnv["admin"] = 0;
if(!isset($_SESSION["MGW"]->spkz) || $_SESSION["MGW"]->spkz=="") $_SESSION["MGW"]->spkz = $appconf["def_language"];

if ($myEnv["module"]!="general" && isset($_SESSION["MGW"]->userid) && $_SESSION["MGW"]->userid) {
$store = $myEnv["module"];

$myEnv["module"] = "general";
$menulabels = Lang::readLanguageFile($_SESSION["MGW"]->spkz, "_navi");
  $myEnv["module"] = $store;

  $sql = "SELECT * FROM mgw_modules WHERE active=1 ORDER BY modulename!='general',modulename!='overview',modulename";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

while($row = $res->fetchRow()){
    $mname = $row["modulename"];
	if (query_access_module($mname)) {
	  $_modulename = $row["modulename"];
	
	  // assign "*" after modulename in menu if its not translated yet
	  if(!isset($menulabels[$_modulename]) || $menulabels[$_modulename]=="")
	    $menustring = $_modulename. " *";
	  else
	    $menustring = $menulabels[$_modulename];

	  $row["label"] = $menustring;
	  if ($myEnv["module"] == $_modulename) { $myEnv["label"] = $menustring; }
	  if ($_modulename == "admin") { $myEnv["admin"] = 1; }

	  // WARNING !!! I (rabol) have made a small patch to smarty.class.php, line 239 to make this work
	  // I have also written a mail to the authors of Smarty about the issue
	  $smarty->append("mdata", $row);
    }
}
$smarty->assign("actlabel", $myEnv["label"]);
$smarty->assign("showadmin", $myEnv["admin"]);

if (query_access_module("webmail2") and isModuleActive("webmail2")) {
  $smarty->assign("show_webmail_iframe", 1);
} else {
  $smarty->assign("show_webmail_iframe", 0);
}
}

// resize the browser size when width is added to the url / only for testing - emailtotom
if (isset($_GET["width"])) {
  $addon = $_GET["width"];
  if ($_GET["width"]=="800") { $addon = "100%\" onmouseover=\"window.resizeTo(800,600);\""; }
  if ($_GET["width"]=="1024") { $addon = "100%\" onmouseover=\"window.resizeTo(1024,768);\""; }
  $smarty->assign("page_width", $addon);
} else {
  $smarty->assign("page_width", "100%");
}

//log level for the current module (DITTY HACK)
$mylevel = 0;
$sql = "SELECT value_int,userid FROM mgw_config WHERE configtagname='log_".$myEnv["module"]."' ORDER BY userid DESC";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$changed = false;
while ($row = $res->fetchRow()) {
    $val = (int)$row["value_int"];
    $usr = (int)$row["userid"];
    if ($usr!=0) $changed = true;
    if ($changed) { if ($usr!=0) $mylevel += $val; } else { if ($usr==0) $mylevel += $val; }
}

$GLOBALS["log"]->level($mylevel);
$GLOBALS["log"]->message(MGW_NOTICE, "Going to module: ".$myEnv["module"]." (".$GLOBALS["log"]->levelStr().")", __LINE__, __FILE__, "general", 1);
?>
