182 lines
7.0 KiB
PHP
182 lines
7.0 KiB
PHP
<?php
|
|
define('MorvalWatches', true);
|
|
|
|
// Determine the base URL
|
|
$base_url = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http';
|
|
$base_url .= '://' . rtrim($_SERVER['HTTP_HOST'], '/');
|
|
$base_url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 || strpos($_SERVER['HTTP_HOST'], ':') !== false ? '' : ':' . $_SERVER['SERVER_PORT'];
|
|
$base_url .= '/' . ltrim(substr(str_replace('\\', '/', realpath(__DIR__)), strlen($_SERVER['DOCUMENT_ROOT'])), '/');
|
|
|
|
define('base_url', rtrim($base_url, '/') . '/');
|
|
|
|
// Initialize a new session
|
|
session_start();
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// Includes
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
include './custom/settings/config.php';
|
|
include './custom/settings/settings.php';
|
|
include 'functions.php';
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// Debug functions
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
if (debug){
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
|
debuglog("PHP ERROR [$errno]: $errstr in $errfile on line $errline");
|
|
return false;
|
|
});
|
|
|
|
set_exception_handler(function($exception) {
|
|
debuglog("PHP EXCEPTION: " . $exception->getMessage() . " in " . $exception->getFile() . " on line " . $exception->getLine());
|
|
});
|
|
}
|
|
|
|
//------------------------------------------
|
|
// Languages supported
|
|
//------------------------------------------
|
|
$supportedLanguages = ['US', 'NL', 'DE', 'ES','FR'];
|
|
|
|
//------------------------------------------
|
|
// Determine language to use
|
|
//------------------------------------------
|
|
// Session language expires after 30 days (adjust as needed)
|
|
$session_expiry_days = 30;
|
|
|
|
// Check if language session has expired
|
|
if (isset($_SESSION['country_code_timestamp'])) {
|
|
$days_passed = (time() - $_SESSION['country_code_timestamp']) / (60 * 60 * 24);
|
|
if ($days_passed > $session_expiry_days) {
|
|
unset($_SESSION['country_code']);
|
|
unset($_SESSION['country_code_timestamp']);
|
|
}
|
|
}
|
|
|
|
// Detect browser language first
|
|
$browser_lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtoupper(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) : country_default;
|
|
|
|
if (isset($_GET['language']) && $_GET['language'] != '') {
|
|
$selected_lang = strtoupper(trim($_GET['language']));
|
|
} elseif (isset($_SESSION['country_code'])) {
|
|
$selected_lang = strtoupper($_SESSION['country_code']);
|
|
} else {
|
|
$selected_lang = in_array($browser_lang, $supportedLanguages) ? $browser_lang : country_default;
|
|
}
|
|
|
|
// Load translation file
|
|
$file_language = './custom/translations/translations_'.$selected_lang.'.php';
|
|
if (file_exists($file_language)) {
|
|
include $file_language;
|
|
$_SESSION['country_code'] = $selected_lang;
|
|
$_SESSION['country_code_timestamp'] = time(); // Store timestamp when language is set
|
|
} else {
|
|
include './custom/translations/translations_'.strtoupper(country_default).'.php';
|
|
$_SESSION['country_code'] = country_default;
|
|
$_SESSION['country_code_timestamp'] = time();
|
|
}
|
|
|
|
$pdo = pdo_connect_mysql();
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//LOGIN TO API
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// Token refresh buffer: refresh token 5 minutes (300 seconds) before expiry
|
|
$token_refresh_buffer = 300;
|
|
|
|
// Check if API token exists and is still valid
|
|
if (!isset($_SESSION['api_token']) || !isset($_SESSION['api_token_expires']) || time() >= ($_SESSION['api_token_expires'] - $token_refresh_buffer)) {
|
|
// Token missing, expired, or about to expire - get new token
|
|
$data = json_encode(array("clientID" => clientID, "clientsecret" => clientsecret), JSON_UNESCAPED_UNICODE);
|
|
$responses = ioAPIv2('/v2/authorization', $data,'');
|
|
//Decode Payload
|
|
if (!empty($responses)){$responses = json_decode($responses,true);}else{$responses = '400';}
|
|
|
|
if (isset($responses['token']) && isset($responses['token_valid'])) {
|
|
// Store token and expiry timestamp in session
|
|
$_SESSION['api_token'] = $responses['token'];
|
|
$_SESSION['api_token_expires'] = strtotime($responses['token_valid']);
|
|
$clientsecret = $responses['token'];
|
|
} else {
|
|
// Fallback for backwards compatibility
|
|
$clientsecret = $responses['token'] ?? '';
|
|
}
|
|
} else {
|
|
// Use cached token
|
|
$clientsecret = $_SESSION['api_token'];
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//LOAD CACHED CATALOG AND CATEGORIES DATA
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// Load catalog and categories from cache files (refreshes once per 24 hours)
|
|
$GLOBALS['cached_catalog'] = getCachedData('/v2/catalog/', 'catalog.json', $clientsecret);
|
|
$GLOBALS['cached_categories'] = getCachedData('/v2/products_categories/', 'categories.json', $clientsecret);
|
|
|
|
// Output error variable
|
|
$error = '';
|
|
//error reporting
|
|
if (debug){
|
|
ini_set('display_errors', '1');
|
|
ini_set('display_startup_errors', '1');
|
|
error_reporting(E_ALL);
|
|
}
|
|
|
|
//Age consent session
|
|
if (isset($_POST['age_consent_allow'])){$_SESSION["age_consent"] = 1;}
|
|
|
|
// Banner
|
|
if (isset($_POST['banner_stay'])){$_SESSION["banner"] = 1;}
|
|
if (isset($_POST['banner_move'])){
|
|
session_destroy();
|
|
header('location: '.banner_link.'');
|
|
die();
|
|
}
|
|
|
|
// Define all the routes for all pages
|
|
$url = routes([
|
|
'/' => 'home.php',
|
|
'test' => 'test.php',
|
|
'/about' => 'custom/pages/about.php',
|
|
'/about_morval' => 'custom/pages/about_morval.php',
|
|
'/product/{id}' => 'product.php',
|
|
'/product/{id}/{option_id}' => 'product.php',
|
|
'/products' => 'products.php',
|
|
'/products/{category}' => 'products.php',
|
|
'/products/{category}/{sort}' => 'products.php',
|
|
'/products/{p}/{category}/{sort}' => 'products.php',
|
|
'/myaccount' => 'myaccount.php',
|
|
'/myaccount/{activation_key}' => 'myaccount.php',
|
|
'/myaccount/{tab}' => 'myaccount.php',
|
|
'/download/{id}' => 'download.php',
|
|
'/cart' => 'cart.php',
|
|
'/cart/{remove}' => 'cart.php',
|
|
'/checkout' => 'checkout.php',
|
|
'/placeorder' => 'placeorder.php',
|
|
'/placeorder/{order_id}' => 'placeorder.php',
|
|
'/search/{query}' => 'search.php',
|
|
'/logout' => 'logout.php',
|
|
'/termsandconditions'=> 'custom/pages/termsandconditions.php',
|
|
'/termsandconditions/{download}'=> 'custom/pages/termsandconditions.php',
|
|
'/faq'=> 'custom/pages/faq.php',
|
|
'/dealers'=> 'custom/pages/dealers.php',
|
|
'/privacy'=> 'custom/pages/privacy.php',
|
|
'/privacy/{download}'=> 'custom/pages/privacy.php',
|
|
'/cookies'=> 'custom/pages/cookies.php',
|
|
'/contact'=> 'custom/pages/contact.php',
|
|
'/returns'=> 'custom/pages/returns.php',
|
|
'/complaints'=> 'custom/pages/complaints.php',
|
|
'/instructions-for-use' => 'custom/pages/faq.php'
|
|
]);
|
|
|
|
// Check if route exists
|
|
if ($url) {
|
|
include $url;
|
|
} else {
|
|
// Page is set to home (home.php) by default, so when the visitor visits that will be the page they see.
|
|
$page = isset($_GET['page']) && file_exists($_GET['page'] . '.php') ? $_GET['page'] : 'home';
|
|
// Include the requested page
|
|
include $page . '.php';
|
|
}
|
|
|
|
|
|
?>
|