<?php
define( 'CUSTOMER_PAGE', true );

ini_set( 'display_errors', 0 );
error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT );

$referer = preg_replace( '>^(https?|android-app)://(www\\.)?>', '', $_SERVER['HTTP_REFERER'] );
$current_domain = preg_replace( '|^(www\\.)?|', '', $_SERVER['HTTP_HOST'] );
if( !empty( $referer ) && !( strpos( $referer, $current_domain . '/' ) === 0 ) ){
	setcookie( 'refererinfo', base64_encode( json_encode( Array( 'r' => $referer, 't' => time(), 'u' => $_SERVER['SCRIPT_URI'] ) ) ), time()+60*60*24*365 );
}
require_once 'libraries/utils.php';
require 'configure/config_loader.php';

if( isset( $_POST['sPhrase'] ) ){
	$sAttributesList = '';
	if( is_array( $_POST['aAttributeValue'] ) ) foreach( $_POST['aAttributeValue'] as $iAttribute => $iAttributeValue ) if( !empty( $iAttributeValue ) ) $sAttributesList.= '&aAttributeValue[' . intval( $iAttribute ) . ']=' . intval( $iAttributeValue );
	header( 'Location: ' . $_SERVER['REQUEST_URI'] . '?sPhrase=' . urlencode( $_POST['sPhrase'] ) . '&iProducer=' . urlencode( $_POST['iProducer'] ) . '&sPriceFrom=' . urlencode( $_POST['sPriceFrom'] ) . '&sPriceTo=' . urlencode( $_POST['sPriceTo'] ) . '&iCategory=' . intval( $_POST['iCategory'] ) . '&sCatalogNumber=' . urlencode( $_POST['sCatalogNumber'] ) . '&sProductIndex=' . urlencode( $_POST['sProductIndex'] ) . '&iFullSearch=' . ( $_POST['iFullSearch'] ? '1' : '0' ) . $sAttributesList );
	exit;
}
header( 'Content-Type: text/html; charset=' . $config['charset'] );

//$config['google_analytics'] = changeTxt( $config['google_analytics'], 'nlNds' );
//$config['google_analytics'] = htmlspecialchars_decode( $config['google_analytics'] );
$useragent = $_SERVER['HTTP_USER_AGENT'];

if( isset( $_GET['full'] ) ) $_SESSION['iFull'] = true;
#define( 'MOBILE', true );
#$_SESSION['iFull'] = false;

define( 'MOBILE', false );
$oTpl = & TplParser::getInstance( DIR_TEMPLATES, false );
if( $config['site_off'] && !( isset( $_SESSION['bSellSmartUser'] ) && $_SESSION['bSellSmartUser'] === true ) ){
	echo $oTpl->parse( 'site_off.tpl' );
	exit;
}
$sBackLink = ( isset( $_SESSION['sBackLink'] ) && !empty( $_SESSION['sBackLink'] ) ) ? $_SESSION['sBackLink'] : $config['index'];
$oTpl->assign( 'sBackLink', $sBackLink );
$sPriceFrom = !empty( $sPriceFrom ) ? trim( changeTxt( urldecode( normalizePrice( $sPriceFrom ) ), 'h' ) ) : null;
$sPriceTo = !empty( $sPriceTo ) ? trim( changeTxt( urldecode( normalizePrice( $sPriceTo ) ), 'h' ) ) : null;
if( isset( $sPhrase ) && !empty( $sPhrase ) ){
	$sPhrase = trim( changeTxt( urldecode( $sPhrase ), 'h' ) );
}
$aActions = isset( $p ) ? getAction( $p ) : getUrlFromGet( );
if( isset( $aActions['f'] ) && $aActions['f'] == 'p' ) $iContent = ( isset( $aActions['a'] ) && is_numeric( $aActions['a'] ) ) ? $aActions['a'] : $config['start_page'];
else $iContent = null;
$oDB = & DataBase::getInstance( );
$oDB->cacheFilesIndexes( $config_db );
$sLinkTestName = null;
switch( $aActions['f'] ){
	case 'p':
		$sLinkTestName = change2Url( $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` WHERE `iPage` = ' . intval( $aActions['a'] ), true, DataBase::SINGLE_VALUE ) );
		$sLinkNameRedirect = $sLinkTestName . ',' . $aActions['a'];
		break;
	case 'products':
		$sLinkTestName = change2Url( $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` WHERE `iProduct` = ' . intval( $aActions['a'] ), true, DataBase::SINGLE_VALUE ) );
		$sLinkNameRedirect = $aActions['a'] . ',' . $sLinkTestName;
		break;
}
if( isset( $sLinkTestName ) && $sLinkTestName != $aActions['o1'] ){
	header( 'HTTP/1.1 301 Moved Permanently' );
	header( 'Location: ' . throwPageUrl( $sLinkNameRedirect . preg_replace( '/^[^,]*,[^,]*/', '', $aActions['sLink'] ) ) );
	exit;
}
if( $aActions['f'] == 'p' && $iContent == $config['start_page'] && strpos( $_SERVER['REQUEST_URI'], $config['index'] ) === false && empty( $aActions['o2'] ) && empty( $aActions['o3'] ) ){
	header( 'HTTP/1.1 301 Moved Permanently' );
	header( 'Location: ' . $config['index'] );
	exit;
}
$content = null;
$sRssMeta = null;
$oFile = & Files::getInstance( );
$oPage = new Pages( );
$oProduct = new Products( );
$oOrder = new Orders( );
$oSalesManago = new SalesManago( );
$oBM = new BlueMedia( );
$oFD = new FirstData( );
$url = 'http' . ( !empty( $_SERVER['HTTPS'] ) ? 's' : null ) . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$oTpl->assign( 'sUrlFull', str_replace( Array( '?&', '&&' ), Array( '?', '&' ), preg_replace( '/sLang=[^&]*/', '', $url ) ) );
if( $config['users_plugin_active'] == true ){
	if( $config['facebook_login'] ){
		if( empty( $_SESSION['iUserClient'] ) ){
			//Uruchomienie logowania przez facebooka dla niezalogowanych
			$oFacebook = new Facebook($fbconfig);

			if( $_GET['fbLogin'] == 1 && $oFacebook->getUser() ){
				$aFBuser = $oFacebook->api('me');
				$iUser = $oDB->executeSQL( 'SELECT `iUser` FROM `' . $oDB->throwTableName( DB_USERS ) . '` WHERE `sLogin` = \'fb-' . $aFBuser['id'] . '\' AND `sPass` IS NULL', true, DataBase::SINGLE_VALUE );
				if( empty( $iUser ) ){
					$aUser = Array(
						'sLogin' => 'fb-' . $aFBuser['id'],
						'sFirstName' => $aFBuser['first_name'],
						'sLastName' => $aFBuser['last_name'],
						'sEmail' => $aFBuser['email'],
					);
					$iUser = saveUser( $aUser, null, true );
				}
				$_SESSION['iUserClient'] = $iUser;

				//Akcje po zalogowaniu
				$oOrder->autoGroupClient( LANGUAGE, $_SESSION['iUserClient'] );
				$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` SET `iUser` = \'' . $oDB->escape( $_SESSION['iUserClient'] ) . '\' WHERE `iCustomer` = ' . $_SESSION['iCustomer' . LANGUAGE] );
				if( isset( $_POST['bRedirectOrder'] ) ) $sUserPage = $oPage->aPages[$config['order_address_page']]['sLinkName'];
				if( isset( $_SESSION['sRedirectAfterLogin'] ) ){
					$sUserPage = $_SESSION['sRedirectAfterLogin'];
					$_SESSION['sRedirectAfterLogin'] = null;
				}
				if( !isset( $sUserPage ) ){
					if( isset( $oPage->aPages[$config['account_page']] ) ) $sUserPage = $oPage->getLinkName( $config['account_page'] );
					else $sUserPage = $oPage->getLinkName( $config['start_page'] );
				}
				header( 'Location: ' . $sUserPage . ( strpos( $sUserPage, '?' ) !== false ? '&' : '?' ) . 'mes=login_correct' );
				exit;
			} else{
				$url .= ( ( strpos( $url, '?' ) !== false ) ? '&' : '?' ) . 'fbLogin=1';
				$params = array(
					'scope' => 'email',
					'redirect_uri' => $url,
				);
				$sFacebookLoginUrl = $oFacebook->getLoginUrl( $params );
				$oTpl->assign( 'sFacebookLoginUrl', $sFacebookLoginUrl );
			}
		}
	}

	require_once DIR_ENGINE . 'users_menu.php';
}
define( 'PK', 'PK_PK_PK_PK' );
if( !isset( $_SESSION['iCustomer' . LANGUAGE] ) ){
	if( isset( $_COOKIE['sCustomer' . LANGUAGE] ) && !empty( $_COOKIE['sCustomer' . LANGUAGE] ) ){
		$iOrder = $oOrder->throwSavedOrderId( $_COOKIE['sCustomer' . LANGUAGE] );
		if( isset( $iOrder ) && is_numeric( $iOrder ) ){
			$_SESSION['iCustomer' . LANGUAGE] = $iOrder;
			$oOrder->generateBasket( );
		}
	}
	if( !isset( $_SESSION['iCustomer' . LANGUAGE] ) ) $_SESSION['iCustomer' . LANGUAGE] = time( ) . rand( 100, 999 );
	setCookie( 'sCustomer' . LANGUAGE, md5( $_SESSION['iCustomer' . LANGUAGE] ), time( ) + 2592000 ); //Pamiętaj przez 30 dni
	$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` WHERE `iTime` < ' . intval( time() - 2592000 ) ); //Kasuj wpisy starsze od 30 dni
}
$sKeywords = $config['keywords'];
$sDescription = $config['description'];
ob_start( );
if( isset( $config['page_search'] ) && is_numeric( $config['page_search'] ) && isset( $oPage->aPages[$config['page_search']] ) ){
	$sLinkSearch = $oPage->aPages[$config['page_search']]['sLinkName'];
	$oTpl->assign( 'sPhrase', $sPhrase );
} else{
	$sLinkSearch = null;
}
$oTpl->assign( 'sLinkSearch', $sLinkSearch );

if( isset( $aActions ) && is_file( 'client_actions/' . $aActions['f'] . '.php' ) ) require 'client_actions/' . $aActions['f'] . '.php';
$iStorageProducts = count( $oProduct->throwProductsStorage( ) );
$aTopBasketInfo['iOrderProducts'] = isset( $_SESSION['iOrderQuantity' . LANGUAGE] ) ? $_SESSION['iOrderQuantity' . LANGUAGE] : 0;
$aTopBasketInfo['fOrderSummary'] = isset( $_SESSION['fOrderSummary' . LANGUAGE] ) ? displayPrice( $_SESSION['fOrderSummary' . LANGUAGE] ) : displayPrice( 0 );
$oTpl->assign( 'aTopBasketInfo', $aTopBasketInfo );
if( $aActions['f'] == 'banners' && isset( $a ) && is_numeric( $a ) ){
	goToBannerLink( $a );
}
if( !empty( $config['newsletter_coupon_value'] ) ){
	$oTpl->assign( 'newsletter_value', strpos( $config['newsletter_coupon_value'], '%' ) !== false ? $config['newsletter_coupon_value'] : $config['newsletter_coupon_value'] . $config['currency_symbol'] );
}

/********************************** THEME **********************************/
if( empty( $content ) ){
	$oTpl->assign( 'aMessage', Array( 'message' => $lang['Data_not_found'], 'bError' => true ) );
	$content.= $oTpl->parse( 'message.tpl' );
}
$aPoll = throwPoll( );
$aBanners = throwBanners( );
$aBoxes = throwBoxes( );
if( isset( $config['basket_page'] ) && isset( $oPage->aPages[$config['basket_page']] ) ){
	$sBasketLink = $oPage->getLinkName( $config['basket_page'] );
} else{
	$sBasketLink = null;
}
$oTpl->assign( 'sBasketLink', $sBasketLink );
$oTpl->assign( 'sMenuLogin', $sMenuLogin );
if( !isset( $bBlockPage ) ){
	$aMenus[3] = $oPage->throwMenu( 'menu_mobile.tpl', 3, $iContent, 5, null, false, false, 0, 0 );
	$aMenus[4] = $oPage->throwMenu( 'menu_mobile.tpl', 4, $iContent, 5, null, false, false, 0, 0 );
	$aMenus[5] = $oPage->throwMenu( 'menu_mobile.tpl', 5, $iContent, 0 );
	$oTpl->assign( 'aMenusMobile', $aMenus );
	$sMenu3 = $oPage->throwMenu( 'menu_3.tpl', 3, $iContent, 1 );
	$sMenu4 = $oPage->throwMenu( 'menu_4.tpl', 4, $iContent, 5, null, $config['show_products_in_menu'], false, 5, 0 );
	$sMenu5 = $oPage->throwMenu( 'menu_bottom.tpl', 5, $iContent, 0 );
	$oTpl->assign( 'SMARTY_sMenu3', $sMenu3 );
	$oTpl->assign( 'SMARTY_sMenu4', $sMenu4 );
	$oTpl->assign( 'SMARTY_sMenu5', $sMenu5 );
	if( !isset( $aData['iType'] ) || $aData['iType'] != 50 ){
		$aBasketInfo = $oOrder->listProductsBasketInfo( );
		$oTpl->assign( 'aBasketInfo', $aBasketInfo );
	}
}
if( isset( $config['newsletter'] ) && $config['newsletter'] === true ){
	$sNewsletterLink = 'add,newsletter.html';
} else{
	$sNewsletterLink = null;
}
$oTpl->assign( 'sNewsletterLink', $sNewsletterLink );
if( isset( $config['storage_page'] ) && isset( $oPage->aPages[$config['storage_page']] ) ){
	$sStoragePageLink = $oPage->getLinkName( $config['storage_page'] );
	$oTpl->assign( 'iStorageProducts', $iStorageProducts );
} else{
	$sStoragePageLink = null;
}
$oTpl->assign( 'sStoragePageLink', $sStoragePageLink );
if( !isset( $aData['iType'] ) || $aData['iType'] != 50 ){
	$aLangs = json_decode( htmlspecialchars_decode( $config['enabled_langs'] ) );
	$oTpl->assign( 'aLangs', $aLangs );
}
$oTpl->assign( 'aBanners', $aBanners );
$oTpl->assign( 'aBoxes', $aBoxes );
$oTpl->assign( 'aFiles', $aFiles );
$oTpl->assign( 'aPoll', $aPoll );
$oTpl->assign( 'sLang', LANGUAGE );
$oTpl->assign( 'sPageContent', $content );
$oTpl->assign( 'SMARTY_sSearchForm', $sSearchForm );
$oTpl->assign( 'SMARTY_sStorage', $sStorage );
$oTpl->assign( 'SMARTY_sLangSelect', $sLangSelect );
$oTpl->assign( 'SMARTY_sPagesTree', $sPagesTree );
$oTpl->assign( 'SMARTY_sDescription', $sDescription );
$oTpl->assign( 'SMARTY_sKeywords', $sKeywords );
$oTpl->assign( 'SMARTY_sRssMeta', $sRssMeta );
$oTpl->assign( 'SMARTY_sTitle', $sTitle );
echo $oTpl->parse( 'index.tpl' );
/********************************** THEME **********************************/

if( !isset( $aUrl ) ){
	$aUrl = parse_url( $_SERVER['REQUEST_URI'] );
	if( !isset( $aUrl['host'] ) ) $aUrl['host'] = null;
	if( !isset( $aUrl['path'] ) ) $aUrl['path'] = null;
	if( isset( $aUrl['query'] ) ) $aUrl['query'] = '?' . $aUrl['query'];
	else $aUrl['query'] = null;
}
if( !empty( $config['paybynet_id'] ) ){
	$paybynet_akt = @fopen( $aUrl['scheme'] . '://' . $aUrl['host'] . $aUrl['path'] . 'index.php?p=payment-paybynet_verify', 'r' );
	@fclose( $paybynet_akt );
}
ob_end_flush( );
?>