<?php
// ======================================================================== //
//   KOOBI - CMS                                                            //
//   2003-2005 © DREAM4 - HTTP://WWW.DREAM4.DE                              //
//   REMOVING THIS INFO WITHOUT PERMISSION BREAKS YOUR LICENSE.             //
//   PLEASE NOTE: KOOBI IS NO FREEWARE! ANY PART OF THE SOFTWARE IS © BY    //
//   THEIR OWNER.                                                           //
//                                                                          //
//   DREAM4 - AM MICHELSGRUND 7 - 69469 WEINHEIM - GERMANY                  //    
//   TEL. +49(0) 6201 - 980442 / FAX. +49(0) 6201 - 64809                   //
// ======================================================================== //
unset($USERARRAY, $PERMS, $GROUPSINGLE, $UID, $MAXPN, $UNAME);
$phpversion = str_replace(".", "", @PHP_VERSION);

$_POST = $HTTP_POST_VARS;
$_GET = $HTTP_GET_VARS;
$_REQUEST = array_merge($_POST, $_GET);
$_COOKIE = $HTTP_COOKIE_VARS;
$_SESSION = $HTTP_SESSION_VARS;

function Add_Array($array) {
	reset($array);
	while (list($feld, $wert) = each($array)) {
		if (is_string($wert)) {
			$array[$feld] = addslashes($wert);
		} else {
			if (is_array($wert)) {
				$array[$feld] = Add_Array($wert);
			}
		}
	}
	return $array;
}

if (!get_magic_quotes_gpc()) {
	$_REQUEST = Add_Array($_REQUEST);
	$_POST = Add_Array($_POST);
	$_GET = Add_Array($_GET);
	$_COOKIE = Add_Array($_COOKIE);
}

include_once(BASEDIR . "/inc/config.php");
include_once(BASEDIR . "/inc/functions.php");
include_once(BASEDIR . "/inc/functions_post.php");
include_once(BASEDIR . "/inc/functions_post_array.php");
include_once(BASEDIR . "/inc/functions_nav.php");
include_once(BASEDIR . "/inc/version.ini.php");
include_once(BASEDIR . "/class/db.php");
include_once(BASEDIR . "/class/tpl/Koobi.class.php");
//include_once(BASEDIR . "/class/mail/Mail.php");
include_once(BASEDIR . "/class/koobimail.php");

$sql = new dbc("SELECT 
company_name,
company_email_reply ,
company_email,
lang ,
langadmin ,
tformat ,
tformat_h ,
bbcodesite ,
bbcodecomments ,
imgcode ,
smilies ,
smileycomments ,
commentnews ,
admintheme,
mygmt
FROM " . $dbprefix . "_settings");

$global = $sql->fetchrow();
$pref['lang'] = $global->lang;
$pref['tformat'] = $global->tformat . $global->tformat_h;
$pref['hpname'] = $global->company_name;
$pref['bbcodesite'] = $global->bbcodesite;
$pref['imgcode'] = $global->imgcode;
$pref['smilies'] = $global->smilies;
$pref['sendemail'] = $global->company_email;
$pref['langadmin'] = $global->langadmin;
$pref['company_email_reply'] = $global->company_email_reply;
$pref['admintheme'] = $global->admintheme;
$pref['mygmt'] = $global->mygmt;



define("COMMENTNEWS", $global->commentnews);
define("COMMENTSBBCODE", $global->bbcodecomments);
define("COMMENTSMILEYS", $global->smileycomments);
define("PREFIX", $dbprefix);
define("LANG", $pref['lang']);
define("LANGADMIN", $pref['langadmin']);
define("BBCODESITE", $pref['bbcodesite']);
define("IMGCODE", $pref['imgcode']);
define("SMILIES", $pref['smilies']);



// =================================================================
// <<-- Template-Pack für Sektion ermitteln (ohne Area-Request) -->>
// =================================================================
if (!isset($_REQUEST['area']) || $_REQUEST['area'] == "" || $_REQUEST['area'] == 1) {
	$sql = new dbc("SELECT area_template,active FROM " . PREFIX . "_areas WHERE area_id='1'");
	$row = $sql->fetchrow();
	$sql->close();
	$THEME = $row->area_template;
	if(!$THEME) $THEME = "standard";
	
	// =================================================================
	// <<-- Wenn Template-Pack nicht existiert, Standard-Pack verwenden -->>
	// =================================================================
	if (!@dir(substr(dirname(__FILE__),0,-4)."/templates/$THEME")) {
		$THEME = "standard";
		if (!@dir(substr(dirname(__FILE__),0,-4)."/templates/$THEME")) {
			echo "<pre>Please make sure, that the Theme \"".$THEME."\" is available on your server. Aborting...</pre>";
			exit;
		}
	}
	$area = 1;
}

// =================================================================
// <<-- Template-Pack für Sektion ermitteln (mit Area-Request) -->>
// =================================================================
if ((isset($_REQUEST['area']) || $_REQUEST['area'] != "") && ($_REQUEST['area'] != 1)) {
	$area = strtolower($_REQUEST['area']);
	$sql = new dbc("SELECT area_template,active FROM " . PREFIX . "_areas WHERE area_id='" . escs($area) . "'");
	
	$row = $sql->fetchrow();
	$sql->close();
	$THEME = $row->area_template;
	if(!$THEME) $THEME = "standard";
	
	// =================================================================
	// <<-- Wenn Template-Pack nicht existiert, Standard-Pack verwenden -->>
	// =================================================================
	if (!@dir(substr(dirname(__FILE__),0,-4)."/templates/$THEME")) $THEME = "standard";
}

// =================================================================
// <<-- EVENTUELL Theme-Cookie berücksichtigen -->>
// =================================================================
if(ALLOW_THEMECHANGE==1){
	$cookieid = "theme_area_$area";
	
	if($_REQUEST['tcurl']!=""){ 
		setcookie($cookieid, $_REQUEST['mtheme'], time()+3600*24*12);
		@header("Location: $_REQUEST[tcurl]");
	}
		if($_COOKIE[$cookieid] != ''){
			$mytheme = $_COOKIE[$cookieid];
			$THEME = (!@dir(BASEDIR . "/templates/$mytheme")) ? $row->area_template : $mytheme;
	}
}

// =================================================================
// <<-- SPRACHE -->>
// =================================================================
$inclang = strtolower(LANG);
$sql = new dbc("SELECT name,value FROM " . $dbprefix . "_languages WHERE code = '$inclang'");
while ($row_lang = $sql->fetchrow()){
	$lang[$row_lang->name] = $row_lang->value;
}


$lct = LANG . "_" . strtoupper(LANG);
@setlocale(LC_TIME, "$lct");
if(LANG=="de") @setlocale (LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
if(LANG=="en") @setlocale (LC_ALL, 'en_EN', 'en');
if(LANG=="fr") @setlocale (LC_ALL, 'fr_FR@euro', 'fr_FR', 'fr');

$sql = new dbc("UPDATE " . PREFIX . "_user SET user_lastonline=user_lastonline_temp WHERE (user_lastonline_temp+500)<" . time());

session_start();
session_name("koobi");

if ($_COOKIE['kuid']) {
	$sql = new dbc("SELECT * FROM " . PREFIX . "_user LEFT JOIN " . PREFIX . "_usergroup USING (ugroup) WHERE ( uid='" . escs($_COOKIE['kuid']) . "' AND pass='" . escs($_COOKIE['kpass']) . "' ) LIMIT 1");
	$USERARRAY = $sql->fetchrow();
	$BENUTZER = $USERARRAY->uname;
	$MAXPN = $USERARRAY->groupmaxpn;
	$MAXSIG = $USERARRAY->groupmaxsig;
	$MAXPNLENTH = $USERARRAY->maxpnlength;
	$MAXCOMMLENGTH = $USERARRAY->maxcommlength;
	$UTIMEFORMAT = $USERARRAY->tformat.$USERARRAY->tformat_h;
	$MAXPICDOWNLOAD = $USERARRAY->maxpicdownload;
	$MAXLENGTH_POST = $USERARRAY->maxlength_post;
	$MAXATTACHMENT = $USERARRAY->max_attachments;
	$COUNTRY = $USERARRAY->country;
	$PERSON = $USERARRAY->person;
	$q_perms = new dbc("SELECT permissions FROM " . PREFIX . "_permissions WHERE group_id = '" . $USERARRAY->ugroup . "' AND section_id = '".$area."'");
	$perms = $q_perms->fetchrow();
	$control = "";
	$control = $sql->numrows();
	
	if($control){
		$HTTP_SESSION_VARS['loggedin'] = 'yes';
		$HTTP_SESSION_VARS['uname'] = $BENUTZER;
	}
	
	// =================================================================
	// <<-- BENUTZER-RECHTE -->>
	// =================================================================
	
	$USERRIGHTS = explode("|", $perms->permissions);
	$area = ($_REQUEST['area']!="") ? $_REQUEST['area']  : 1 ;
	foreach ($USERRIGHTS AS $VAL) $HTTP_SESSION_VARS[$VAL . $area] = 1;
	$sql = new dbc("UPDATE " . PREFIX . "_user SET user_lastonline_temp='" . time() . "' WHERE UID='" . $USERARRAY->uid . "'");
	//
	
}

if ($USERARRAY->uid) {
	$islogged = 1;
	$UID = $USERARRAY->uid;
	$UNAME = $USERARRAY->uname;
	$UMAIL = $USERARRAY->email;
	$INVISIBLE = $USERARRAY->invisible;
	define("INVISIBLE", $INVISIBLE);
	define("UNAME", $UNAME);
	define("MYGMT", $USERARRAY->mygmt);
	define("UID", $UID);
	define("UGROUP", $USERARRAY->ugroup);
	define("ISLOGGED", 1);
	define("MAXLENGTH_POST", $MAXLENGTH_POST);
	define("MAXATTACHMENT", $MAXATTACHMENT);
	
	
	
	if (bannedname($UNAME)) {
		setcookie("kuid", "" , time()-10000);
		setcookie("kpass", "" ,time()-10000);
		$NOOUT = 1;
		$EOUT = msg("nopermission", "banned", str_replace("__URL__", "index.php" , $lang['redirect']), "index.php");
	}
	
	if(bannedemail($UMAIL)){
		setcookie("kuid", "" , time()-10000);
		setcookie("kpass", "" ,time()-10000);
		$NOOUT = 1;
		$EOUT = msg("nopermission", "isbanned_email", str_replace("__URL__", "index.php" , $lang['redirect']), "index.php");
	}
	
	if ($USERARRAY->ugroup == 1) {
		$ADMIN = 1;
	}
	} else {
	
	$sql_nl = new dbc("SELECT uid FROM " . PREFIX . "_user WHERE ugroup = '2' AND  user_regdate < 2");
	$row_nl = $sql_nl->fetchrow();
	
	$islogged = 0;
	$UID = $row_nl->uid;
	$ADMIN = 0;
	define("UID", $UID);
	define("UGROUP", 2);
	define("REGDATE", 1);
	define("MYGMT", $pref['mygmt']);
	
	
	// =================================================================
	// <<-- GÄSTE-RECHTE -->>
	// =================================================================
	$sql = new dbc(" SELECT permissions FROM " . PREFIX . "_permissions WHERE group_id = '2' AND section_id = '".escs($area)."'");
	
	$USERARRAY = $sql->fetchrow();
	$USERRIGHTS = explode("|", $USERARRAY->permissions);
	
	$area = ($_REQUEST['area']!="") ? $_REQUEST['area']  : 1 ;
	foreach ($USERRIGHTS AS $VAL) $HTTP_SESSION_VARS[$VAL . $area] = 1;
	
	$sql = new dbc("SELECT maxlength_post,groupname_single, maxcommlength, maxpicdownload FROM ".PREFIX."_usergroup WHERE ugroup=2");
	$row = $sql->fetchrow();
	$MAXCOMMLENGTH = $row->maxcommlength;
	$GROUPSINGLE = $row->groupname_single;
	$MAXPICDOWNLOAD = $row->maxpicdownload;
	$MAXATTACHMENT = $row->max_attachments;
	$MAXLENGTH_POST = $row->maxlength_post;
	$COUNTRY = "default";
	$PERSON = "default";
	define("MAXLENGTH_POST", $MAXLENGTH_POST);
}

if (!$UTIMEFORMAT){ $TFORMAT = $pref['tformat']; } else { $TFORMAT = $UTIMEFORMAT; }

function mygmt($param){
	global $TFORMAT; 
	$mytime = date($TFORMAT, $param);
	return $mytime;
}
define("SEPARATOR", $lang['f_navi_separator']);
define("MAXATTACHMENT", $MAXATTACHMENT);
define("USERTHEME", $THEME);
define("COUNTRY", $COUNTRY);
define("PERSON", $PERSON);
define("MISCIDSINC", 1);
?>