<?php
@setlocale(LC_TIME, 'de_DE');
// start the timer for the page parse time log
define('PAGE_PARSE_START_TIME', microtime());
error_reporting(E_ALL & ~E_NOTICE);

if(!is_file('includes/configure.php')){
	require('install_yes4c.php');
}

include('includes/configure.php');

// THATS STATIC
define('DIR_WS_CATALOG','/');
define('DIR_WS_ADMIN','admin/');
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/');



// set the type of request (secure or not)
$request_type = (getenv('HTTPS') == 'on') ? 'SSL' : 'NONSSL';

// set php_self in the local scope
$PHP_SELF = $_SERVER['SCRIPT_FILENAME'];

// include the list of project filenames
require(DIR_WS_INCLUDES . 'filenames.php');

// include the list of project database tables
require(DIR_WS_INCLUDES . 'database_tables.php');

// SQL caching dir
define('SQL_CACHEDIR',DIR_FS_CATALOG.'cache/');



// Store DB-Querys in a Log File
define('STORE_DB_TRANSACTIONS', 'false');

// include used functions
  require_once(DIR_WS_ADMIN.DIR_WS_INCLUDES.DIR_WS_FUNCTIONS.'html_output.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_query.inc.php');
  require_once(DIR_FS_INC . 'xtc_db_queryCached.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_not_null.inc.php');
//  require_once(DIR_FS_INC . 'xtc_href_link.inc.php');
  require_once(DIR_FS_INC . 'xtc_redirect.inc.php');
  require_once(DIR_FS_INC . 'xtc_get_all_get_params.inc.php');
//  require_once(DIR_FS_INC . 'xtc_image.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_check_agent.inc.php');
  require_once(DIR_FS_INC . 'xtc_input_validation.inc.php');
  require_once(DIR_FS_INC . 'xtc_validate_password.inc.php');
  require_once(DIR_FS_INC . 'xtc_write_user_info.inc.php');

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

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




  $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)) {
    define($configuration['cfgKey'], $configuration['cfgValue']);
  }


    // set the application parameters
  function xtDBquery($query) {
       if (DB_CACHE=='true') {
           //  echo 'cached query: '.$query.'<br>';
            $result=xtc_db_queryCached($query);
          } else {
             $result=xtc_db_query($query);
    }
    return $result;
  }
  
  // if gzip_compression is enabled, start to buffer the output
  if ( (GZIP_COMPRESSION == 'true') && ($ext_zlib_loaded = extension_loaded('zlib')) && (PHP_VERSION >= '4') ) {
    if (($ini_zlib_output_compression = (int)ini_get('zlib.output_compression')) < 1) {
      ob_start('ob_gzhandler');
    } else {
      ini_set('zlib.output_compression_level', GZIP_LEVEL);
    }
  }

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

/* -------------------------------------
NEW SESSION HANDLING
--------------------------------------*/

// set the session name and save path
session_name('YESsid');
if (STORE_SESSIONS != 'mysql') 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 ($_COOKIE['cookie_test'])) {
		session_start();
		$session_started = true;
	}
} else {
	session_start();
	$session_started = true;
}


/* -------------------------------------
NEW SESSION HANDLING
--------------------------------------*/



  // 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($_SERVER['HTTP_USER_AGENT']);
    if (!session_is_registered('SESSION_USER_AGENT')) {
      $_SESSION['SESSION_USER_AGENT'] = $http_user_agent;
    }

    if ($_SESSION['SESSION_USER_AGENT'] != $http_user_agent) {
      session_destroy();
      xtc_redirect(xtc_href_link(FILENAME_LOGIN));
    }
  }

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

    if ($_SESSION['SESSION_IP_ADDRESS'] != $ip_address) {
      session_destroy();
      xtc_redirect(xtc_href_link(FILENAME_LOGIN));
    }
  }


function CacheCheck(){
	if (USE_CACHE == 'false')
		return false;
	else
		return true;
}


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

    include(DIR_WS_CLASSES . 'language.php');
    $lng = new language(xtc_input_validation($_GET['language'],'char',''));

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

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

  // include the language translations
  require(DIR_WS_LANGUAGES . $_SESSION['language'].'/'.$_SESSION['language'] . '.php');

  // write customers status in session
  require(DIR_WS_INCLUDES . 'write_customers_status.php');

  // split-page-results
  require(DIR_WS_CLASSES . 'split_page_results.php');

  // infobox
  require(DIR_WS_CLASSES . 'boxes.php');

  // include the breadcrumb class and start the breadcrumb trail
  require(DIR_WS_CLASSES . 'breadcrumb.php');
  $breadcrumb = new breadcrumb;

  $breadcrumb->add(HEADER_TITLE_TOP, HTTP_SERVER);
  $breadcrumb->add(HEADER_TITLE_CATALOG, xtc_href_link(FILENAME_DEFAULT));

  // initialize the message stack for output messages
  require(DIR_WS_CLASSES . 'message_stack.php');
  $messageStack = new messageStack;

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

  if (isset($_SESSION['customer_id'])) {
  $account_type_query=xtc_db_query("SELECT
                                    account_type,
                                    customers_default_address_id
                                    FROM
                                    ".TABLE_CUSTOMERS."
                                    WHERE customers_id = '".(int)$_SESSION['customer_id']."'");
  $account_type=xtc_db_fetch_array($account_type_query);

  // check if zone id is unset bug #0000169
  if (!isset($_SESSION['customer_country_id'])) {
  	$zone_query=xtc_db_query("SELECT  entry_country_id
                                     FROM ".TABLE_ADDRESS_BOOK."
                                     WHERE customers_id='".(int)$_SESSION['customer_id']."'
                                     and address_book_id='".$account_type['customers_default_address_id']."'");

    $zone=xtc_db_fetch_array($zone_query);
    $_SESSION['customer_country_id']=$zone['entry_country_id'];
  }
  $_SESSION['account_type']=$account_type['account_type'];
   } else {
   $_SESSION['account_type']='0';
   }

  // modification for nre graduated system
  unset($_SESSION['actual_content']);
?>
