<?php

	//error_reporting(E_ALL^E_NOTICE);
	ini_set("register_globals", "On");
	#if (eregi('http', $_SERVER["SERVER_PROTOCOL"]))	ob_start('ob_gzhandler');
	require_once dirname(__FILE__).'/config.php';
	require_once dirname(__FILE__).'/adodb/adodb.inc.php';

	$ADODB_SESSION_DRIVER = $adodb_scheme;
	$ADODB_SESSION_CONNECT = $adodb_host;
	$ADODB_SESSION_USER = $adodb_user;
	$ADODB_SESSION_PWD = $adodb_pass;
	$ADODB_SESSION_DB = $adodb_db;
	$ADODB_SESSION_TBL = 'qb_sessions';
	$ADODB_SESSION_EXPIRE_NOTIFY = array('userid','NotifyFn');
	function NotifyFn($expireref, $sesskey) {
		global $ADODB_SESS_CONN; # the session connection object
		$user = $ADODB_SESS_CONN->qstr($expireref);
		$ADODB_SESS_CONN->Execute("UPDATE qb_changelog ".
		" SET newval = NULL WHERE newval = ?", array($sesskey));
	}
	require_once dirname(__FILE__).'/adodb/session/adodb-session.php';

	@session_start();

	
	$adodb = NewADOConnection($adodb_dsn);
	if (!$adodb) die($adodb->ErrorMsg());
	$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
	if (! function_exists('removeSlashes')) {
		function removeSlashes(&$a){
			if(count($a)>0) {
				foreach($a as $i=>$v) {
					if(is_array($a[$i])) {
						removeSlashes($a[$i]);
					} else {
						$a[$i]=stripslashes($v);
					}
				}
			}
		}
	}
	if (get_magic_quotes_gpc()) {
		if ($_SERVER['REQUEST_METHOD'] == "POST") {
			removeSlashes($_POST);
		} else if ($_SERVER['REQUEST_METHOD'] == "GET") {
			removeSlashes($_GET);
		}
	}

	/* not use anymore
	if (! function_exists('htmlTitle')) {
		function htmlTitle($title) {
			echo "
			<div class=titlebox>
			<div class=h1>
			$title
			</div>
			</div>";
		}
	}
	*/

	if (! function_exists('getUserMenu')) {
		function getUserMenu($userId) {
			global $adodb, $CACHE_userMenu;
			if (isset($CACHE_userMenu)) return $CACHE_userMenu;
			$rs = $adodb->Execute("SELECT menu FROM qb_user_right ".
			" WHERE userid = '".$userId."'");
			$userMenu = $rs->fields['menu'];
		
			$rs = $adodb->Execute("SELECT userid, group_name FROM qb_user_group ".
			" WHERE userid LIKE '%".$userId."%'");
			while (!$rs->EOF && !$groupName) {
				$listUser = $rs->fields['userid'];
				$pieUser = explode(",", $listUser);
				foreach ($pieUser as $k => $v) {
					if ($v === $userId) {
						$groupName = $rs->fields['group_name'];
						break;
					}
				}
				$rs->MoveNext();
			}
		
			$rs = $adodb->Execute("SELECT menu FROM qb_group_right ".
			" WHERE group_name = '".$groupName."'");
			$groupMenu = $rs->fields['menu'];

			if ($userMenu) $userMenu .= ",";
			if ($groupMenu) $userMenu .= $groupMenu;
			$CACHE_userMenu = $userMenu;
			return $CACHE_userMenu;
		}
	}

	if (! function_exists('getUserActions')) {
		function getUserActions($userId, $moduleId) {
			global $adodb,$CACHE_arrAction;
			if (isset($CACHE_arrAction)) return $CACHE_arrAction;
			$userMenu = getUserMenu($userId);
			$where = str_replace(",", "','", $userMenu);
			$rs = $adodb->Execute("SELECT action FROM qb_right ".
			" WHERE id IN ('".$where."') AND module = '".$moduleId."'");

			$arrAction = array();
			while (!$rs->EOF) {
				$action = $rs->fields['action'];
				$p = explode(",", $action);
				foreach ($p as $k => $v) {
					$arrAction[$v] = 1;
				}
				$rs->MoveNext();
			}
			$CACHE_arrAction = $arrAction;
			return $CACHE_arrAction;
		}
	}

	if (! function_exists('getReportActions')) {
		function getReportActions($userId, $moduleId) {
			global $adodb,$CACHE_arrReport;
			if (isset($CACHE_arrReport)) return $CACHE_arrReport;
			$rs = $adodb->Execute("SELECT action FROM qb_user_report ".
			" WHERE userid = ? AND name = ?", array($userId, $moduleId));
			$arrReport = array();
			while (!$rs->EOF) {
				$action = $rs->fields['action'];
				$p = explode(",", $action);
				foreach ($p as $k => $v) {
					$arrReport[$v] = 1;
				}
				$rs->MoveNext();
			}
			$CACHE_arrReport = $arrReport;
			return $CACHE_arrReport;
		}
	}

	if (! function_exists('getUserOpts')) {
		function getUserOpts($userId, $moduleId, $arrDefaults=array()) {
			global $adodb, $arrOptions, $CACHE_opts;
			if (isset($CACHE_opts)) return $CACHE_opts;
			$arrAction = getUserActions($userId, $moduleId);
			$opts['filters'] = '1=0';
			$opts['options'] = '';
			foreach ($arrAction as $k => $v) {
				if ($k === 'list') $opts['filters'] = '';
				else {
					if (isset($arrDefaults[$arrOptions[$k]])) {
						$opts['options'] .= $arrOptions[$k];
					}
				}
			}
			$CACHE_opts = $opts;
			return $CACHE_opts;
		}
	}

	if (! function_exists('getUserOptions')) {
		function getUserOptions($moduleId, $strDefaults='') {
			global $isSupervisor;
			$userId = $_SESSION['userid'];
			if ($isSupervisor) return $strDefaults;
			$arrDefaults = array();
			for ($i=0,$j=strlen($strDefaults);$i<$j;$i++) {
				$arrDefaults[$strDefaults[$i]] = 1;
			}
			global $adodb;
			$opts = getUserOpts($userId, $moduleId, $arrDefaults);
			return $opts['options'];
		}
	}

	if (! function_exists('getUserFilters')) {
		function getUserFilters($moduleId, $strDefaults='') {
			global $isSupervisor;
			$userId = $_SESSION['userid'];
			if ($isSupervisor) return $strDefaults;
			global $adodb;
			$opts = getUserOpts($userId, $moduleId);
			if ($strDefaults && $opts['filters']) $strDefaults .= " AND ";
			return $strDefaults.$opts['filters'];
		}
	}


	if (! function_exists('__')) {
		function __($str, $default=null, $module="GENERAL") {
			global $arrDictionary, $arrFILE;
			$oldstr = $str;
			if (!isset($arrDictionary)) {
				loadDictionary();
			}
			if (isset($arrFILE[basename($module)])) {
				$module = $arrFILE[basename($module)];
			}
			$module = strtolower($module);
			$str = strtolower($str);
			if (isset($arrDictionary[$module][$str])) {
				return $arrDictionary[$module][$str];
			}
			if ($default !== null) return $default;
			return $oldstr;
		}
	}

	if (! function_exists('loadDictionary')) {
		function loadDictionary() {
			global $adodb, $arrDictionary;
			$userLang = 'english';
			if (isset($_SESSION['userid'])) {
				$rs = $adodb->Execute("SELECT language FROM qb_user".
				" WHERE userid = '".$_SESSION['userid']."'");
				if (!$rs->EOF) $userLang = $rs->fields['language'];
			}
			$rs = $adodb->Execute("SELECT module, field, dict FROM qb_dictionary".
			" WHERE language = '".$userLang."'");
			while (!$rs->EOF) {
				$dictFrom = $rs->fields['field'];
				$dictTo = $rs->fields['dict'];
				$dictModule = $rs->fields['module'];
				$arrDictionary[strtolower($dictModule)][strtolower($dictFrom)] = $dictTo;
				$rs->MoveNext();
			}
		}
	}

	if (! function_exists('getMicrotime')) {
		function getMicrotime() {
			list($a,$b) = split(" ", microtime());
			return $a+$b;
		}
	}

	if (! function_exists('getTitle')) {
		function getTitle($file) {
			global $arrFILE;
			$file = basename($file);
			if (isset($arrFILE[$file])) {
				return __($arrFILE[$file], null, "MAIN MENU");
			}
			return $file;
		}
	}

	if (! function_exists('isSupervisor')) {
		function isSupervisor() {
			global $adodb;
			if (!isset($_SESSION['userid'])) return false;
			if ($_SESSION['userid'] === 'admin') return true;
			$rs = $adodb->Execute("SELECT * FROM qb_user ".
			" WHERE userid = '".$_SESSION['userid']."'");
			if (!$rs->EOF) {
				$userid = $rs->fields['userid'];
				$supervisor = $rs->fields['supervisor'];
				if (md5($userid."Yes"."qbuilder") === $supervisor) {
					return true;
				}
			}
			return false;
		}
	}

	if (! function_exists('isLogTable')) {
		function isLogTable($table) {
			global $adodb;
			$rs = $adodb->Execute("SELECT * FROM qb_setting ".
			" WHERE name = 'logtable' ");
			$value = $rs->fields['value'];
			$pie = explode(",", $value);
			if (is_int(array_search($table, $pie))) {
				return 'qb_changelog';
			} else {
				return false;
			}
		}
	}
	
	$isSupervisor = isSupervisor();

/*	
	if (isset($_SESSION['repid'])) {
        $rs = $adodb->Execute('SELECT pkey FROM qb_report WHERE id=' . $_SESSION['repid'] . ';');
		$pkey = $rs->fields['pkey'];

		$query3 = 'SELECT ' . $pkey . ' FROM ' . $_SESSION['reptb'] . ';';
		$result3 = mysql_query($query3);
                $row3 = @mysql_fetch_object($result3);//$result3);
                $identifier = '';

                foreach ( explode(',', $row2->pkey) as $pkey ) {
                  if ( strlen($row3->$pkey) != 0 ) {
                    $identifier .= $row3->$pkey;
                  }
                }
	}
	*/
?>
