<?php
$sys['afianVersion'] = "Afian Document Manager";
$time_start = microtime(true);

error_reporting(E_ALL & ~E_NOTICE);
session_start();

/* get afian's root folder */
$sys['root'] =  str_replace("\\","/",getcwd());

if (
//cache thumbnails
($_GET['public'] && ($_GET['sub'] == "thumb") && !$_GET['clearCache']) || 
//cache components
($_GET['components']) || 
//cache when downloading files ?!
(($_GET['file']=="myfilesacting" || $_GET['file']=="bshareacting") && $_GET['act'] == "download")
) {
	session_cache_limiter('public');
} else {
	session_cache_limiter('nocache');
}

/* checking GPCS data */
require_once($sys['root']."/system/functions/misc.inc");
require_once($sys['root']."/system/class/lib.strings.php");
$str = new strings;
/* dependecies: none */
require_once($sys['root']."/system/class/lib.settings.php");
$settings = new settings();
/* dependecies: $str, $_SESSION */
require_once($sys['root']."/system/includes/inputchk.inc");
require_once($sys['root']."/system/class/lib.csv.php");
require_once($sys['root']."/system/class/lib.list.php");
require_once($sys['root']."/system/class/lib.fileman.php");
/* utils */
$fm = new fileman;

$sys['baseURL'] = getBaseURL();

/* gziping output buffer*/
/* dependecies: $fm (fileman) */
require_once($sys['root']."/system/class/lib.obgzip.php");

/* multilanguage support */
require_once($sys['root']."/system/class/lib.lang.php");
$lang = new language();
$lang->getLanguageData();
/************************** BEGIN OUTPUT *******************/


if ($settings->config['need_setup'] == "1") {
	/* *** INSTALLATION  *** */
	if ($_GET['folder_browser_data_feeder']) {
		require_once($sys['root']."/system/includes/folder_browser_data_feeder.inc");
	} else {
		require_once($sys['root']."/system/includes/setup/index.inc");
	}
	exit();
}


define('ADODB_DIR',gluePath($sys['root'],"/system/class/adodb"));
require_once($sys['root']."/system/class/adodb/adodb.inc.php");
require_once($sys['root']."/system/class/adodb/adodb-errorhandler.inc.php");
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$ADODB_CACHE_DIR = $sys['root']."/system/data/temp/adodbcache";
$db = NewADOConnection($settings->config['dbtype']);

if (!$db->Connect($settings->config['server'], $settings->config['dbuser'], $settings->config['dbpass'], $settings->config['dbname'])) {
	exit("Database connection failed!");
}

require_once($sys['root']."/system/class/lib.config.php");
$config = new config();
require_once($sys['root']."/system/class/lib.thumb.php");
require_once($sys['root']."/system/class/lib.debug.php");
require_once($sys['root']."/system/class/lib.quota.php");
require_once($sys['root']."/system/class/lib.sharing.php");
require_once($sys['root']."/system/class/lib.trash.php");
require_once($sys['root']."/system/class/lib.zip.php");
require_once($sys['root']."/system/class/lib.comments.php");
require_once($sys['root']."/system/class/lib.img.php");
require_once($sys['root']."/system/class/lib.template.php");
require_once($sys['root']."/system/class/lib.jstemplate.php");
$tpl = new template();
require_once($sys['root']."/system/class/lib.calendar.php");
require_once($sys['root']."/system/class/lib.log.php");
require_once($sys['root']."/system/class/email/htmlmimemail.inc");
require_once($sys['root']."/system/includes/checkimages.inc");
require_once($sys['root']."/system/class/lib.flags.php");
require_once($sys['root']."/system/class/auth/lib.auth.sql.php");



/* login */
$auth = new Auth;
if ($_POST['login'] || $_GET['login']) {
	$u = ($_POST['username'])?$_POST['username']:$_GET['username'];
	$p = ($_POST['password'])?$_POST['password']:$_GET['password'];
	$loginResult = $auth->login($str->fromHTML($u), $str->fromHTML($p));
	if ($loginResult == "ok") {
		$logTheLogin = true;
	}
}

$img = new Images;
$thumb = new Thumbs;
$fm->loadFileTypesInfo();
$flags = new flags;


if ($auth->logged()){
	$config->settings['adminemail'] = $db->GetOne("SELECT email FROM ".$settings->config['optinfo_tablename']." WHERE uid='1'");
	/* user permissions */
	require_once($sys['root']."/system/includes/permissions.inc");
	
	$share = 	new share;
	$log = 		new logging($auth->currentUserInfo['username']);
	$trash = 	new Trash;
	$comments = new comments;
	$quota = 	new Quota($auth, $auth->currentUserInfo['username']);
	$forms = 	new HTMLForms;
	if ($logTheLogin) {
		$IP = getenv("REMOTE_ADDR");
		if (getenv("HTTP_X_FORWARDED_FOR")){
			$IP .= ";";
			$IP .= getenv("HTTP_X_FORWARDED_FOR"); 
		}
		$log->act("0", "login", "USERNAME: ".$str->fromHTML($_POST['username'])."\rPASSWORD: ".$str->fromHTML($_POST['password'])."\rIP ADDRESS: ".$IP."");
		}
	$quota->resetTrafficIfMonth();
	
	
	require_once($sys['root']."/system/getlist.php");
} else {
	if ($_GET['file'] || $_GET['admin']) {
		echo "You are not logged in, or the session expired.";
	} else {
		require_once($sys['root']."/system/templates/login/header.inc");
		require_once($sys['root']."/system/includes/login.inc");
		require_once($sys['root']."/system/templates/login/footer.inc");
	}
}

$lang->updateLanguageData();

GzDocOut(9,0,$dontGzip); // END OUTPUT