<?php
// File: $Id: main.php,v 1.15 2004/06/28 11:49:39 roland Exp $
// +----------------------------------------------------------------------+
// | Der Dirigent 1.0                                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 the dedi-group <dedi-group@der-dirigent.de>  |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License                 |
// |                                                                      |
// | This program is subject to the GPL license, that is bundled with     |
// | this package in the file LICENSE.TXT.                                |
// | If you did not receive a copy of the GNU General Public License      |
// | along with this program write to the Free Software Foundation, Inc., |
// | 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA               |
// |                                                                      |
// | This program 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.                         |
// |                                                                      |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+

// Output buffering starten
ob_start();

// Magic Quotes ausschalten
set_magic_quotes_runtime (0);

// zeige alle Fehlermeldungen, aber keine Warnhinweise
error_reporting (E_ALL & ~E_NOTICE);
// error_reporting (E_ALL);
// Flag für Windows-Systeme, um auf Windows nicht existierende Befehle zu blocken
$is_win =  (strpos(getenv("OS"), "Win" ) >= 0);

// alle GET, POST und COOKIE wegen Globals_off parsen
ignore_user_abort(true);
if (!(bool) ini_get('register_globals')) import_request_variables('gp');
$cfg_dedi = '';$cfg_client ='';

$dedi = ( empty($dedi) ) ? $_COOKIE['dedi']: $dedi;

// notwendige Dateien includen
$this_dir = str_replace ('\\', '/', dirname(__FILE__) . '/');
require_once ($this_dir.'inc/config.php');
require_once ($this_dir.'inc/class.dedi_debug.php');
include_once ($this_dir.'external/pear/html/it.php');
include_once ($this_dir.'external/phplib/prepend.php');
include_once ($this_dir.'inc/class.user_perms.php');
include_once ($this_dir.'inc/class.values_ct.php');
require_once ($this_dir.'inc/fnc.general.php');
require_once ($this_dir.'inc/fnc.libary.php');
include_once ($this_dir.'inc/class.querybuilder_factory.php');
include_once ($this_dir.'inc/class.repository.php');

// Klassen initialisieren
$deb = &new dedi_debug;
$db = &new DB_dedi;
$db_query = &new querybuilder_factory();
$db_query = $db_query -> get_db($db, 'dedi_db', $this_dir.'inc/');
$val_ct = &new values_ct();

// Konfigurationsparameter einlesen
$cfg_dedi_temp = $val_ct -> get_cfg();
$cfg_dedi = array_merge($cfg_dedi, $cfg_dedi_temp);
unset($cfg_dedi_temp);

// dB Optimice
if ( $cfg_dedi['db_optimice_tables']['enable'] && (time() > ($cfg_dedi['db_optimice_tables']['last_run'] + $cfg_dedi['db_optimice_tables']['time']))) {
    lib_optimice_tables();
    $val_ct->set_value(array('group' => 'cfg', 'client' => 0, 'key' => 'db_optimice_tables', 'key2' => 'last_run', 'value' => time()));
}

// Template initialisieren
$tpl = &new HTML_Template_IT($this_dir.'tpl/'.$cfg_dedi['skin'].'/');

// Session starten
page_open(array('sess' => 'dedi_Backend_Session',
                'auth' => 'dedi_Backend_Auth'));

// Sessionvariablen initialisieren
$sess->register('sid_client');
$sess->register('sid_lang');
$sess->register('sid_lang_charset');
$sess->register('sid_area');
$sess->register('sid_sniffer');
$client       = (empty($client))       ? $sid_client       : $client;
$lang         = (empty($lang))         ? $sid_lang         : $lang;
$lang_charset = (empty($lang_charset)) ? $sid_lang_charset : $lang_charset;

$perm         = &new dedi_perms($client, $lang);
$client       = $perm -> get_client();
$lang         = $perm -> get_lang();
$lang_charset = $perm -> get_lang_charset();

// Projekt initialisieren
$sid_client   = $client;

// Sprache initialisieren
$sid_lang         = $lang;
$sid_lang_charset = $lang_charset;

// Multilanguage initialisieren
$val_ct->values_ct();

// Area initialisieren
if (isset($area)) $sid_area = $area;
else $area = $sid_area;

// Wenn area nicht erlaubt ist, redirecten auf erlaubte area, wenn möglich
$pos = strpos($area, '_');
$allowed_area = (!$pos) ? $area: substr( $area, 0, $pos );
if( !$perm->have_perm('area_'. $allowed_area) && $area != 'logout' && $area != 'plugin'){
        $new_area = $perm->get_first_allowed_area();
        if(! empty($new_area)){
                $area = $new_area;
                unset($new_area);
        }
}

// Sprachdatei einlesen
$lang_dir = $this_dir.'tpl/'.$cfg_dedi['skin'].'/lang/'.$cfg_dedi['backend_lang'].'_'.$lang_charset.'/';
$lang_defdir = $this_dir.'tpl/standard/lang/deutsch_iso-8859-1/';
require_once( ( file_exists($lang_dir.'lang_general.php') ? $lang_dir: $lang_defdir ) .'lang_general.php');
// Sprachdatei für Area einlesen
if (file_exists ($lang_dir."lang_$area.php")) {
        include_once($lang_dir."lang_$area.php");
} else {
        $deb -> collect('Fehlt: Sprachdatei für Area: ' . $area);
        if (file_exists ($lang_defdir."lang_$area.php")) {
                include_once($lang_defdir."lang_$area.php");
        }
}

// Rechte überprüfen
$cfg_client = $val_ct -> get_by_group('cfg_client', $client);
$deb -> collect('Projekt Id: ' . $client);
$deb -> collect('Lang Id: '    . $lang);
$deb -> collect('User Id: '    . $perm -> user_id);
$deb -> collect('Group Id: '   . $perm -> idgroup);

// Repository laden
$rep        = &new repository;
// Run init Plugins
if ( $cfg_rep['repository_init_plugins'] ) $rep->init_plugins();

// Area wählen
include("inc/inc.$area.php");

// Template ausspucken
$tpl->show();

// Output buffering beenden
if ($area != 'logout') page_close();
$output = ob_get_contents();
ob_end_clean();
$output .= $deb -> show();

//eventuelle autostarts ausführen:
if (is_array($cfg_dedi['autostart']['backend'])) {
	if (!defined('IS_DEDI')) die();
	foreach($cfg_dedi['autostart']['backend'] as $value) include_once $cfg_dedi['dedi_path'] .'plugins/'. $value;
}

// Seite komprimieren und ausgeben
if (($cfg_dedi['gzip'] == '1') && ereg("gzip",$_SERVER['HTTP_ACCEPT_ENCODING']) && (false == headers_sent())) {
     @ob_start('ob_gzhandler');
     eval($cfg_dedi['manipulate_output']);
     @ob_end_flush();
} else eval($cfg_dedi['manipulate_output']);
// phpinfo();
?>