Refactor code structure for improved readability and maintainability
This commit is contained in:
112
index.php
112
index.php
@@ -19,44 +19,98 @@ include './custom/settings/config.php';
|
||||
include './custom/settings/settings.php';
|
||||
include 'functions.php';
|
||||
|
||||
//TRANSLATION FILE LOCATION
|
||||
if (isset($_GET['language']) && $_GET['language'] !=''){
|
||||
//INCLUDE LANGUAGE FILE
|
||||
$file_language = './custom/translations/translations_'.strtoupper($_GET['language']).'.php';
|
||||
if (file_exists($file_language)){
|
||||
include $file_language; //Include the code
|
||||
//DEFINE LANGUAGE
|
||||
$_SESSION['country_code'] = trim($_GET['language']);
|
||||
}
|
||||
else {
|
||||
include './custom/translations/translations_NL.php';
|
||||
//DEFINE LANGUAGE
|
||||
$_SESSION['country_code'] = language_code;
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// 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']);
|
||||
}
|
||||
}
|
||||
elseif(isset($_SESSION['country_code'])){
|
||||
$file_language = './custom/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
|
||||
if (file_exists($file_language)){
|
||||
include $file_language; //Include the code
|
||||
}
|
||||
else {
|
||||
include './custom/translations/translations_NL.php';
|
||||
}
|
||||
|
||||
// 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 {
|
||||
include './custom/translations/translations_NL.php';
|
||||
//DEFINE LANGUAGE
|
||||
$_SESSION['country_code'] = language_code;
|
||||
$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
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$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';}
|
||||
$clientsecret = $responses['token'];
|
||||
// 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 = '';
|
||||
|
||||
Reference in New Issue
Block a user