<?php
/**
 * Init file of PHPGEN system.
 *
 * PHPGEN is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHPGEN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHPGEN; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @copyright   EasySoft R&D Team (C) 2006
 * @author      Chunsheng Wang <wwccss@263.net>
 * @link        http://www.phpgen.com
 * @package     Include
 * @version     $Id: Init.php,v 1.10 2006/08/17 13:52:56 wangcs Exp $
 */
/* Report all errors except E_NOTICE. */
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
session_start();
header("Cache-control: private");

/* Set the include path and BaseURL */
$_CFG["SysRoot"]     = str_replace("\\", "/", str_replace("Include", "", dirname(__FILE__)));
$_CFG["PathDivider"] = eregi("WIN",PHP_OS) ? ";" : ":";
ini_set("include_path", $_CFG["SysRoot"]                   . $_CFG["PathDivider"] .
                        $_CFG["SysRoot"] . "Include/"      . $_CFG["PathDivider"] .
                        $_CFG["SysRoot"] . "Include/Class" . $_CFG["PathDivider"]);

/* Require needed files. */
require_once("Config.php");
require_once("FuncMain.php");
require_once("FuncGEN.php");
require_once("FuncPRJ.php");
require_once("FuncTable.php");
require_once("FuncPage.php");
require_once("FuncCode.php");
require_once("FuncHtml.php");
require_once("Smarty/Smarty.class.php");
require_once("ADO/adodb.inc.php");
require_once("JS.class.php");
require_once("Generator/Generator.class.php");
require_once("Generator/Beautify.class.php");
require_once("Cpaint/Cpaint.class.php");
              
/* Init smarty class. */
$MyTPL = new Smarty;
$MyTPL->caching       = false;
$MyTPL->template_dir  = $_CFG["SysRoot"] . 'Templates';
$MyTPL->compile_dir   = $_CFG["SysRoot"] . 'Compile';
$MyTPL->compile_check = true;

/* Init JS classes. */
$MyJS = new JS;
$MyCP = new cpaint();

/* Set the language and style. */
$_CFG["UserLang"]  = isset($_COOKIE["UserLang"])  ? $_COOKIE["UserLang"]  : key($_CFG["Lang"]);
$_CFG["UserStyle"] = isset($_COOKIE["UserStyle"]) ? $_COOKIE["UserStyle"] : "Default";
@setcookie("UserLang",  $_CFG["UserLang"],  time()+360000, $_CFG["CookieDomain"]);
@setcookie("UserStyle", $_CFG["UserStyle"], time()+360000, $_CFG["CookieDomain"]);

$LangMain = "Language/" . $_CFG["UserLang"] . "/_COMMON.php";
$LangSelf = $_CFG["SysRoot"] . "Language/" . $_CFG["UserLang"] . $_SERVER["PHP_SELF"];
$LangSelf = $_CFG["SysRoot"] . "Language/" . $_CFG["UserLang"] . str_replace($_CFG["BasePath"], "", $_SERVER["PHP_SELF"]);
require_once($LangMain);
file_exists($LangSelf) ? require_once($LangSelf) : "";

/* Add slashes to REQUEST, GET, POST, COOKIE, FILES vars. */
if(!get_magic_quotes_gpc())
{
    $_REQUEST = sysAddSlash($_REQUEST);
    $_GET     = sysAddSlash($_GET);
    $_POST    = sysAddSlash($_POST);
    $_COOKIE  = sysAddSlash($_COOKIE);
    $_FILES   = sysAddSlash($_FILES);
}

/* Judge whether the user is a valid user. */
if(!eregi("Login.php|Logout.php", $_SERVER["PHP_SELF"]))
{
    genJudgeUser($_REQUEST["UserName"], $_REQUEST["Password"]);
}

/* Init some variables. */
$_TPL["TplFile"] = substr($_SERVER["PHP_SELF"], 1);
$_TPL["TplFile"] = str_replace(".php", ".htm", $_TPL["TplFile"]);

/* Assign $Config defined in the Lang file to the template. */
$MyTPL->assign("CFG",  $_CFG);
$MyTPL->assign("TPL",  $_TPL);

/* Save current ProjectID, TableName vars to session. */
isset($_REQUEST["ProjectID"]) ? $_SESSION["ProjectID"] = $_REQUEST["ProjectID"] : "";
isset($_REQUEST["TableName"]) ? $_SESSION["TableName"] = $_REQUEST["TableName"] : "";

/* Assign TableName and IdTag to template. */
$MyTPL->assign("TableName", $_SESSION["TableName"]);
$MyTPL->assign("IdTag",     '$' . 'Id' . '$');
?>