<?php
//ini_set('register_globals','On');
@setlocale(LC_TIME, 'de_DE');
  define('PAGE_PARSE_START_TIME', microtime());
  @ini_set("session.cache_expire", 120);
  // Set the level of error reporting
  error_reporting(E_ALL & ~E_NOTICE);

  // Disable use_trans_sid as xtc_href_link() does this manually
  if (function_exists('ini_set')) {
    ini_set('session.use_trans_sid', 0);
  }

  require('../includes/configure.php');
// THATS STATIC
define('DIR_WS_CATALOG','../');
define('DIR_WS_ADMIN','admin/');
define('DIR_WS_ICONS','images/icons/');
define('DIR_WS_INCLUDES','includes/');
define('DIR_WS_MODULES',DIR_WS_INCLUDES.'modules/');
define('DIR_WS_LANGUAGES','lang/');
define('DIR_WS_FUNCTIONS','functions/');
define('DIR_WS_CLASSES',DIR_WS_INCLUDES.'classes/');
define('DIR_FS_INC',DIR_FS_CATALOG.'inc/');
define('DIR_FS_ADMIN',DIR_FS_CATALOG.'admin/');
define('DIR_FS_LANGUAGES',DIR_FS_CATALOG.'lang/');
define('SQL_CACHEDIR',DIR_FS_CATALOG.'cache/');

  define('SQL_CACHEDIR',DIR_FS_CATALOG.'cache/');

  // Set the length of the redeem code, the longer the more secure
  define('SECURITY_CODE_LENGTH', '6');

  // define the filenames used in the project
  define('FILENAME_START', 'start.php');
  define('FILENAME_CUSTOMERS', 'customers.php');
  define('FILENAME_CUSTOMERS_EDIT', 'customers_edit.php');
  define('FILENAME_COMPANIES', 'companies.php');
  define('FILENAME_COMPANIES_EDIT', 'companies_edit.php');
  define('FILENAME_MAILMASTER', 'mailmaster.php');
  define('FILENAME_EMAIL_ACCOUNTS', 'email_accounts.php');
  define('FILENAME_EMAIL_ACCOUNTS_EDIT','email_accounts_edit.php');
  
  define('TABLE_ADDRESS_BOOK', 'address_book');
  define('TABLE_ADMIN_ACCESS', 'admin_access');
  define('TABLE_AIRPORTS', 'airports');
  define('TABLE_CONFIGURATION', 'configuration');
  define('TABLE_CUSTOMERS', 'customers');
  define('TABLE_COUNTRIES', 'countries');
  define('TABLE_COMPANIES', 'companies');
  define('TABLE_CUSTOMERS_INFO', 'customers_info');
  define('TABLE_CUSTOMERS_IP', 'customers_ip');
  define('TABLE_CUSTOMERS_TO_COMPANIES','customers_to_companies');
  define('TABLE_CUSTOMERS_STATUS', 'customers_status');
  define('TABLE_CONTENT_MANAGER','content_manager');
  define('TABLE_HISTORY','history');
  define('TABLE_LANGUAGES', 'languages');
  define('TABLE_EMAILS', 'emails');
  define('TABLE_EMAIL_ACCOUNTS', 'email_accounts');
  define('TABLE_AIRLINES','airlines');
  
  
  require_once(DIR_FS_INC . 'xtc_db_query.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_queryCached.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_perform.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_fetch_array.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_num_rows.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_data_seek.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_insert_id.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_free_result.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_fetch_fields.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_output.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_input.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_prepare_input.inc.php');
  require_once(DIR_FS_INC . 'xtc_get_ip_address.inc.php');
  require_once(DIR_FS_INC . 'xtc_setcookie.inc.php');
  require_once(DIR_FS_INC . 'xtc_validate_email.inc.php');
  require_once(DIR_FS_INC . 'xtc_not_null.inc.php');
  require_once(DIR_FS_INC . 'yes_get_menu_link.inc.php');
  require_once(DIR_FS_INC . 'yes_get_customers_status_name.inc.php');
  require_once(DIR_FS_INC . 'yes_get_customers_gender_name.inc.php');
  require_once(DIR_FS_INC . 'yes_get_php_self.inc.php');

  require(DIR_FS_CATALOG.DIR_WS_CLASSES.'adodb/adodb.inc.php');
  $ADODB_CACHE_DIR = DIR_FS_CATALOG.'cache/';
  $conn = &ADONewConnection('mysql');

  $conn->Connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
  $conn->debug = false;
  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;


  // set application wide parameters
  $configuration_query = xtc_db_query('select configuration_key as cfgKey, configuration_value as cfgValue from ' . TABLE_CONFIGURATION . '');
  while ($configuration = xtc_db_fetch_array($configuration_query)) {
	if($configuration['cfgKey'] != 'MAX_DISPLAY_SEARCH_RESULTS')
		define($configuration['cfgKey'], $configuration['cfgValue']);
  }

  define('MAX_DISPLAY_SEARCH_RESULTS',MAX_DISPLAY_SEARCH_RESULTS_ADMIN);

  define('FILENAME_IMAGEMANIPULATOR',IMAGE_MANIPULATOR);
    function xtDBquery($query) {
       if (DB_CACHE=='true') {
             $result=xtc_db_queryCached($query);
             //echo 'cached query: '.$query.'<br>';
          } else {
             $result=xtc_db_query($query);
    }
    return $result;
  }



  // some code to solve compatibility issues
  require(DIR_WS_FUNCTIONS . 'compatibility.php');

  require(DIR_WS_FUNCTIONS . 'general.php');


  // define how the session functions will be used
  require(DIR_WS_FUNCTIONS . 'sessions.php');

  define('YES_HREF_CONNECTION_TYPE',((ENABLE_SSL == 'true') ? 'SSL':'NONSSL') );
    // define our general functions used application-wide
  require(DIR_WS_FUNCTIONS . 'html_output.php');

  // set the session name and save path
  session_name('YESsid');
  session_save_path(SESSION_WRITE_DIRECTORY);
  // set the session cookie parameters
  if (function_exists('session_set_cookie_params')) {
    session_set_cookie_params(0, '/', (xtc_not_null($current_domain) ? '.' . $current_domain : ''));
  } elseif (function_exists('ini_set')) {
    ini_set('session.cookie_lifetime', '0');
    ini_set('session.cookie_path', '/');
    ini_set('session.cookie_domain', (xtc_not_null($current_domain) ? '.' . $current_domain : ''));
  }


  // set the session ID if it exists
  if (isset($_POST[session_name()])) {
    session_id($_POST[session_name()]);
  } elseif ( ($request_type == 'SSL') && isset($_GET[session_name()]) ) {
    session_id($_GET[session_name()]);
  }

  // start the session
  $session_started = false;
  if (SESSION_FORCE_COOKIE_USE == 'True') {
    xtc_setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, '/', $current_domain);

    if (isset($HTTP_COOKIE_VARS['cookie_test'])) {
      session_start();
      $session_started = true;
    }
  } elseif (CHECK_CLIENT_AGENT == 'True') {
    $user_agent = strtolower(getenv('HTTP_USER_AGENT'));
    $spider_flag = false;

    if ($spider_flag == false) {
      session_start();
      $session_started = true;
    }
  } else {
    session_start();
    $session_started = true;
  }


  // verify the ssl_session_id if the feature is enabled
  if ( ($request_type == 'SSL') && (SESSION_CHECK_SSL_SESSION_ID == 'True') && (ENABLE_SSL == true) && ($session_started == true) ) {
    $ssl_session_id = getenv('SSL_SESSION_ID');
    if (!session_is_registered('SSL_SESSION_ID')) {
      $_SESSION['SESSION_SSL_ID'] = $ssl_session_id;
    }

    if ($_SESSION['SESSION_SSL_ID'] != $ssl_session_id) {
      session_destroy();
      xtc_redirect(xtc_href_link(FILENAME_SSL_CHECK));
    }
  }

  // verify the browser user agent if the feature is enabled
  if (SESSION_CHECK_USER_AGENT == 'True') {
    $http_user_agent = getenv('HTTP_USER_AGENT');
    if (!session_is_registered('SESSION_USER_AGENT')) {
      $_SESSION['SESSION_USER_AGENT'] = $http_user_agent;
    }

  }

  // verify the IP address if the feature is enabled
  if (SESSION_CHECK_IP_ADDRESS == 'True') {
    $ip_address = xtc_get_ip_address();
    if (!xtc_session_is_registered('SESSION_IP_ADDRESS')) {
      $_SESSION['SESSION_IP_ADDRESS'] = $ip_address;
    }

  }

  // set the language
  if (!isset($_SESSION['language']) || isset($_GET['language'])) {

    include(DIR_WS_CLASSES . 'language.php');
    $lng = new language('german');

    //if (!isset($_GET['language'])) $lng->get_browser_language();

    $_SESSION['language'] = $lng->language['directory'];
    $_SESSION['languages_id'] = $lng->language['id'];
  }


  // include the language translations
  require(DIR_FS_LANGUAGES . $_SESSION['language'] . '/admin/'.$_SESSION['language'] . '.php');
  $current_page = explode('?', basename($_SERVER['PHP_SELF'])); $current_page = $current_page[0]; // for BadBlue(Win32) webserver compatibility
  if (file_exists(DIR_FS_LANGUAGES . $_SESSION['language'] . '/admin/'.$current_page)) {
    include(DIR_FS_LANGUAGES . $_SESSION['language'] . '/admin/'.  $current_page);
  }

  // define our localization functions
  require(DIR_WS_FUNCTIONS . 'localization.php');

  // Include validation functions (right now only email address)
  //require(DIR_WS_FUNCTIONS . 'validations.php');

  // entry/item info classes
  require(DIR_WS_CLASSES . 'object_info.php');

    // Include Template Engine
  require(DIR_FS_CATALOG.DIR_WS_CLASSES . 'Smarty_2.6.3/Smarty.class.php');
  require(DIR_FS_CATALOG.DIR_WS_CLASSES . 'class.yesSmarty.php');


  // global databasetableobject
  require(DIR_WS_CLASSES.'databasetable.php');
  require(DIR_WS_CLASSES.'db_cache.php');
  
  // HISTORY CLASS
  require(DIR_WS_CLASSES.'history.php');
  $YES_HISTORY = new history;
  
  // ACCESS ARRAY BERECHTIGUNGEN
  $YES_USER_PERMISSIONS = array();
  $admin_access_query_raw = sprintf("SELECT * FROM %s WHERE customers_id='%s'",TABLE_ADMIN_ACCESS,$_SESSION['customer_id']);
  $admin_access = $conn->CacheExecute( 3600,$admin_access_query_raw );
  if($admin_access){
  	$YES_USER_PERMISSIONS = $admin_access->fields;
  }