<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

// including libs

include_once './libs/smarty/libs/Smarty.class.php';
include_once './core/layout.class.php';
include_once './core/website.class.php';
include_once './core/structure.class.php';
include_once './core/metatag.class.php';
include_once './core/item.class.php';
include_once './core/shop_theme.class.php';

$layout_menu_id = false;

// start session
if (!isset($_REQUEST['nosess']) && session_status() === PHP_SESSION_NONE) {
	session_start();
}

// language selection
if (isset($_GET['lang'])) {
	$_SESSION['user_lang'] = $_GET['lang'];
}

// layout object
$layout_object = new layout();
$layout_object->start($base_object);

Registry::set('layout_object', $layout_object);

// website object
$website_object = new website($base_object);

$update_cart = 0;

// login
if (isset($_POST['shopLogin'])) {
	$logindata = $_POST['shopLogin'];
	$login_save_session = false;
	if (isset($_POST['login_save_session']) && $_POST['login_save_session'] == 1) {
		$login_save_session = true;
	}
	$request = $base_object->customer->login($logindata['user'], $logindata['pass'], $login_save_session);
	if ($request == 1 && $base_object->config->shopConfiguration['save_customer_shoppingcart'] == 1) {
		$update_cart = 1;
	}
	$layout_object->assign('login_state', $request);
	//header('Location: '.$_SERVER['REQUEST_URI']);
} else if (isset($_GET['logout'])) { // logout
	$base_object->customer->logout();
	header('Location: '.STANDARD_DIR);
}

// Create metatag object
$layout_object->metaTagObject = new Metatag($layout_object, $base_object);
$layout_object->metaTagObject->get_by_group('DEFAULT');

// customer authentification
if ($base_object->customer->auth() > 0) {
	$customer_data = $base_object->customer->get_data();
	$layout_object->assign('customer', $customer_data);
}

if ($update_cart == 1) {
	include_once './core/shoppingcart.class.php';
	$shopping_cart_object = New Shoppingcart($base_object);
	$shopping_cart_object->update_customer_cart();
}

// get customer group
if (isset($base_object->customer->group_id)) {
	$base_object->customer_group->id = $base_object->customer->group_id;
} else {
	$base_object->customer_group->id = 100;
}
$base_object->customer_group->load_data();

$show_prices_before_login = $base_object->config->shopConfiguration['show_prices_before_login'];
$no_prices = false;
if ($show_prices_before_login != '1') {
	if (!$base_object->is_customer_logged_in()) {
		$no_prices = true;
	}
}
$layout_object->assign('no_prices', $no_prices);

$structure_id = '';

//include_once(ROOT_DIR.'core/website.class.php');
$website_object = new website($base_object);

// frontend ajax
if (!empty($_GET['website_modul'])) {
	$website_modul = $_GET['website_modul'];
	$module_file = 'modules/'.$website_modul.'.php';

	if (file_exists($module_file)) {
		include_once $module_file;

		$modul_object = new $website_modul($base_object, $layout_object);

		if (property_exists($modul_object, 'website_object')) {
			$modul_object->website_object = $website_object;
		}

		if (isset($_POST['param_1'])) {
			$website_content = $modul_object->run($_POST['param_1']);
		} else {
			$website_content = $modul_object->run();
		}

		echo $website_content;
	} else {
		$maincontent = $layout_object->_fetch('404.tpl');
	}

	exit();
}

// Domainhandling
if (isset($base_object->config->shopConfiguration['has_multiple_domains']) && is_array($base_object->config->shopConfiguration['has_multiple_domains'])) {
	foreach ($base_object->config->shopConfiguration['has_multiple_domains'] as $domain_arr) {
		if ($domain_arr['domain'] == str_replace('www.', '', $_SERVER['HTTP_HOST'])) {
			$domain_dependend_layout = true;
			$layout_menu_id = $domain_arr['menu_id'];
			$_GET['menu_id'] = (is_numeric($_GET['menu_id'])) ? $_GET['menu_id'] : $layout_menu_id;
		}
	}
}

// start breadcrumb
$breadcrumb = array();

// item search redirect
if (isset($_POST['search_item']) && $_POST['search_item']) {
	if (isset($_POST['search_item_categories']) && $_POST['search_item_categories'] > 0) {
		header('Location: /index.php?search_item='.$_POST['search_item'].'&item_category='.$_POST['search_item_categories']);
	} else if (is_array($_POST['search_item'])) {
		$querystring = http_build_query($_POST['search_item']);
		header('Location: /index.php?'.$querystring);
	} else {
		header('Location: /index.php?search_item='.$_POST['search_item']);
	}
} else if (isset($_GET['search_item']) && $_GET['search_item']) { // item search per get
	$breadcrumb[] = array('name' => 'Artikelsuche', 'short_uri' => '');
	$search = array('search_item' => $_GET['search_item']);
	if (isset($_GET['item_category']) && $_GET['item_category'] > 0) {
		$search['item_category'] = $_GET['item_category'];
	}
	if (isset($_GET['search_item2']) && $_GET['search_item2']) {
		$search['search_item2'] = $_GET['search_item2'];
	}
	if (isset($_GET['search_item3']) && $_GET['search_item3']) {
		$search['search_item3'] = $_GET['search_item3'];
	}

	include_once(ROOT_DIR.'modules/website_item_search.php');

	$content_object = new website_item_search($base_object, $layout_object);
	$layout_object->maincontent = $content_object->run($search);
} else if (isset($_POST['manufacturer_id']) && $_POST['manufacturer_id']) { // manufacturer item list
	$breadcrumb[] = array('name' => 'Hersteller', 'short_uri' => '/manufacturers/');
	$search = array('manufacturer_id' => $_POST['manufacturer_id']);
	include_once(ROOT_DIR.'modules/website_item_search.php');
	$content_object = new website_item_search($base_object, $layout_object);
	$layout_object->maincontent = $content_object->run($search);
} else if (isset($_GET['seo_manufacturer_suri']) && $_GET['seo_manufacturer_suri']) { // manufacturer details
	$breadcrumb[] = array('name' => 'Hersteller', 'short_uri' => '/manufacturers/');
	if (isset($_GET['structureSuri']) && $_GET['structureSuri']) {
		include_once(ROOT_DIR.'modules/website_manufacturers_structure_details.php');
		$content_object = new website_manufacturers_structure_details($base_object, $layout_object);
		$layout_object->maincontent = $content_object->run($_GET['seo_manufacturer_suri'], $_GET['structureSuri']);
	} else {
		include_once(ROOT_DIR.'modules/website_manufacturers_details.php');
		$content_object = new website_manufacturers_details($base_object, $layout_object, $metatag_object);
		$layout_object->maincontent = $content_object->run($_GET['seo_manufacturer_suri']);
	}
}
// item details
else if (isset($_GET['item_id']) && $_GET['item_id'] && (!isset($_GET['action']) || $_GET['action'] != 'delete')) {
	$layout_object->assign('site_type', 'full_width');
	$item_id = $_GET['item_id'];
	include_once(ROOT_DIR.'modules/website_item.php');
	$content_object = new website_item($base_object, $layout_object);
	$layout_object->maincontent = $content_object->run($item_id);

	// set last customer interesting groups
	$group_id = (isset($_GET['menu_id']) && $_GET['menu_id']) ? $_GET['menu_id'] : $content_object->item_data->structure_id;
	$base_object->customer->set_last_interesting_item_groups($group_id);

	$layout_object->assign('menu_id', isset($_GET['menu_id']) ? $_GET['menu_id'] : null);

	// Meta tags
	$layout_object->metaTagObject->get_by_group('ARTIKEL');
	$layout_object->metaTagObject->overwriteVar('Bezeichnung', $content_object->item_data->name);
	$hersteller = Manufacturer::get_name_by_id_static($content_object->item_data->manufacturer_id);
	$layout_object->metaTagObject->overwriteVar('VonHersteller', 'von '.$hersteller);
	$layout_object->metaTagObject->overwriteVar('Hersteller', $hersteller);
	$layout_object->metaTagObject->overwriteVar('ArtikelAttribut6', str_replace(';',',',$content_object->item_data->attribute_6));
	$structure_id = (isset($_GET['menu_id']) && $_GET['menu_id']) ? $_GET['menu_id'] : $content_object->structure_data[0]->id;
	$structure_object = new Structure($base_object);
	$tree_ids = array_reverse($structure_object->get_all_tree_ids($structure_id));
	$z = 0;
	foreach ($tree_ids as $sid) {
		$z++;
		$layout_object->metaTagObject->overwriteVar('Struktur'.$z, Structure::get_name_by_id_static($sid));
	}
	$layout_object->metaTagObject->overwriteVar('Warengruppe', Structure::get_name_by_id_static($structure_id));

	// multi domain handling
	if (isset($base_object->config->shopConfiguration['multi_domain']) && $base_object->config->shopConfiguration['multi_domain']) {
		$domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
		// is domain found..
		if (isset($base_object->config->shopConfiguration['multi_domain'][$domain])) {
			// check domain with structure id
			foreach ($base_object->config->shopConfiguration['multi_domain'] as $domain_name => $domain_structure_id) {
				if (in_array($domain_structure_id, $tree_ids) && $domain != $domain_name) {
					header('HTTP/1.1 301 Moved Permanently');
					header('Location: http://www.'.$domain_name.$_SERVER['REQUEST_URI']);
				}
			}
		}
	}

	$layout_object->metaTagObject->overwriteMetas(array(
		'title' => $content_object->item_data->seo_title,
		'keywords' => $content_object->item_data->seo_keywords,
		'description' => $content_object->item_data->seo_description
	));
} else { // load main modules
	if (isset($_GET['menu_id']) && $_GET['menu_id']) {
		$structure_id = $_GET['menu_id'];

		$layout_object->assign('current_menu_id', $structure_id);
	} else {
		if (is_numeric($layout_menu_id)) {
			$structure_id = $layout_menu_id;
		} else {
			$structure_id = 6;
			$layout_object->assign('entrypage', true);
		}
	}

	// get site configuration
	$structure_object = new Structure($base_object);
	$structure_object->structure_id = $structure_id;
	$structure_data = $structure_object->get_data();

	if ($structure_data->active == 2 && $structure_data->customer_group != $base_object->customer_group->id) {
		$structure_id = 6;
		$structure_object->structure_id = $structure_id;
		$structure_data = $structure_object->get_data();
	}

	$breadcrumb[] = array('name' => $structure_data->name, 'short_uri' => $structure_data->short_uri, 'object' => $structure_data);

	$layout_object->assign('structureData', $structure_data);
	$manufacturers = Manufacturer::get_names_by_structure_id($structure_id);
	$layout_object->assign('manufacturersOfCurrentStructureId', $manufacturers);
	$content_modul = $structure_object->get_type_modul($structure_data->type);

	// get maincontent
	include_once(ROOT_DIR.'modules/'.$content_modul.'.php');
	$content_object = new $content_modul($base_object, $layout_object);
	$layout_object->maincontent = $content_object->run($structure_id);

	// multi layout with redirect to correct domain
	if (isset($base_object->config->shopConfiguration['multi_domain']) && $base_object->config->shopConfiguration['multi_domain']) {
		$domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
		// is domain found..
		if (isset($base_object->config->shopConfiguration['multi_domain'][$domain])) {
			$structure_object = new Structure($base_object);
			$tree_ids = $structure_object->get_all_tree_ids($content_object->item_data->structure_id);
			// check domain with structure id
			foreach ($base_object->config->shopConfiguration['multi_domain'] as $domain_name => $domain_structure_id) {
				if (in_array($domain_structure_id, $tree_ids) && $domain != $domain_name) {
					header('HTTP/1.1 301 Moved Permanently');
					header('Location: http://www.'.$domain_name.$_SERVER['REQUEST_URI']);
				}
			}
		}
	}

	// Meta tags
	$structure_level = $structure_object->get_level($structure_data->id, 0);
	if ($structure_level > 0 && $structure_level < 5) {
		$layout_object->metaTagObject->get_by_group('MENU'.$structure_level);
	} else {
		$layout_object->metaTagObject->get_by_group('MENU');
	}
	$layout_object->metaTagObject->overwriteVar('Warengruppe', $structure_data->name);
	$tree_ids = array_reverse($structure_object->get_all_tree_ids($structure_data->id));
	$z = 0;
	foreach ($tree_ids as $sid) {
		$z++;
		$layout_object->metaTagObject->overwriteVar('Struktur'.$z, Structure::get_name_by_id_static($sid));
	}

	$usedManus = array();
	if ($manufacturers) {
		$layout_object->metaTagObject->overwriteVar('Hersteller', implode(', ', $manufacturers));
		foreach ($manufacturers as $manuName) {
			$usedManus[] = $structure_data->name.' '.$manuName;
		}
	}

	$usedSubs = $structure_object->get_subcategories($structure_id);
	if ($usedSubs) {
		$layout_object->metaTagObject->overwriteVar('Unterwarengruppen', implode(', ', $usedSubs));
	}

	$layout_object->metaTagObject->overwriteMetas(array(
		'title' => $structure_data->seo_title,
		'keywords' => ($structure_data->seo_keywords) ? $structure_data->seo_keywords : implode(', ', $usedManus),
		'description' => $structure_data->seo_description
	));
}

$layout_object->assign('layout_menu_id', $layout_menu_id);
$base_object->structure_id = $structure_id;

// get main widgets
if (is_array($base_object->config->shopConfiguration['widget_regions'])) {
	foreach ($base_object->config->shopConfiguration['widget_regions'] as $region) {
		if ($base_object->config->shopConfiguration['has_frontend_widget_config']) {
			${$region.'_widget_list'} = $base_object->config->shopConfiguration['widget_'.$region.'_column'];
		} else {
			${$region.'_widget_list'} = $base_object->config->shopConfiguration['default_widget_list_'.$region];
		}
		${$region.'_bar'} = '';

		if (gettype(${$region.'_widget_list'}) == 'array') {
			${$region.'_bar'} = array();
			foreach (${$region.'_widget_list'} as $widget) {

				$widget_data = '';
				if ($widget['widget']) {
					$widget_data = $website_object->{$widget['widget']}(isset($widget['config']) ? $widget['config'] : false);
				}

				$layout_object->assign($widget['widget'], $widget_data);
				${$region.'_bar'}[] = $layout_object->_fetch($widget['template']);
			}
		}
		$layout_object->assign($region.'_widgets', ${$region.'_bar'});
	}
}

// Ensure $structure_data is defined
if (!isset($structure_data)) {
	$structure_data = new stdClass();
	$structure_data->theme_id = null;
	$structure_data->header_image = null;
	$structure_data->header_image_link = null;
}

$main_theme_id = $structure_data->theme_id;
$header_image = $structure_data->header_image;
$header_link = $structure_data->header_image_link;

// full path breadcrump
if (isset($website_object->activated_structures) && $website_object->activated_structures) {
	$breadcrumb = array();
	foreach ($website_object->activated_structures as $structure_obj) {
		$breadcrumb[] = array('name' => $structure_obj->name, 'short_uri' => $structure_obj->full_short_uri, 'object' => $structure_obj);

		// get theme configuration from parent structures
		if ($structure_obj->theme_id) {
			$main_theme_id = $structure_obj->theme_id;
		}

		// get header image from parent structures
		if ($structure_obj->header_image) {
			$header_image = $structure_obj->header_image;
			$header_link = $structure_obj->header_image_link;
		}
	}
}

// get dynamic theme data
$theme_data = new stdClass();
if ($main_theme_id) {
	$shop_theme_object = new ShopTheme($base_object);
	$theme_data = $shop_theme_object->get_data($main_theme_id);
}

$theme_data->header_image = $header_image;
$theme_data->header_image_link = $header_link;
$layout_object->assign('theme_data', $theme_data);

if ($layout_menu_id) {
	$layout_object->assign('menu_items_allsubs', Structure::getLayoutMenu($layout_menu_id));
}


// multi layout with redirect to correct domain
if (isset($base_object->config->shopConfiguration['multi_layout']) && $base_object->config->shopConfiguration['multi_layout']) {
	if (!isset($_SESSION['layout_id'])) {
		$layout_id = $base_object->config->shopConfiguration['multi_layout_default'];
	} else {
		$layout_id = $_SESSION['layout_id'];
	}
	foreach ($base_object->config->shopConfiguration['multi_layout'] as $structure_id => $l_id) {
		if (in_array($structure_id, $_GET['parent_ids'])) {
			$layout_id = $l_id;
		}
	}
	$_SESSION['layout_id'] = $layout_id;
	$layout_object->assign('layout_id', $layout_id);
}

// assign meta tags to template / layout object
$layout_object->metaTagObject->assign();

// show breadcrumb
$layout_object->assign('breadcrumb', $breadcrumb);

// feature for sellavi
/*if ($structure_id == 75 && $_SESSION['load_first_page'] != 'false') {
	$_SESSION['load_first_page'] = 'false';
	$layout_object->assign('load_first_page', 1);
}*/


// _fetch animated header images from directory
/*if ($layout_menu_id) {
	$directory = false;

	if ($layout_menu_id == 11) {
		$directory = 'trolly';
	} else if ($layout_menu_id == 16) {
		$directory = 'shower';
	} else if ($layout_menu_id == 17) {
		$directory = 'thera';
	}

	if ($directory) {
		$images = array();
		$directory = '/web/'.SHOP_SYSTEM.'/themes/'.$base_object->config->shopConfiguration['THEME'].'/media/images/header/'.$directory;

		if ($handle = opendir(ROOT_DIR.$directory)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != '.' && $file != '..' && is_file(ROOT_DIR.$directory.'/'.$file)) {
					$images[] = array(
						'imagefile' => $directory.'/'.$file,
						'animation' => 'RANDOM',
						'direction' => 'RANDOM',
						'speed' => 2500,
						'pause' => 2500
					);
				}
			}

			closedir($handle);
		}

		shuffle($images);
		$layout_object->assign('header_animated_images', json_encode($images));
	}
}*/

// read main template
$layout_object->show_site();

?>