/**
* Process the query string
*/
private function processQueryString()
{
// store the query string local, so we don't alter it.
$queryString = trim($this->request->getPathInfo(), '/');
// split into chunks
$chunks = (array) explode('/', $queryString);
$hasMultiLanguages = $this->getContainer()->getParameter('site.multilanguage');
// single language
if (!$hasMultiLanguages) {
// set language id
$language = $this->get('fork.settings')->get('Core', 'default_language', SITE_DEFAULT_LANGUAGE);
} else {
// multiple languages
// default value
$mustRedirect = false;
// get possible languages
$possibleLanguages = (array) Language::getActiveLanguages();
$redirectLanguages = (array) Language::getRedirectLanguages();
// the language is present in the URL
if (isset($chunks[0]) && in_array($chunks[0], $possibleLanguages)) {
// define language
$language = (string) $chunks[0];
// try to set a cookie with the language
try {
// set cookie
CommonCookie::set('frontend_language', $language);
} catch (\SpoonCookieException $e) {
// settings cookies isn't allowed, because this isn't a real problem we ignore the exception
}
// set sessions
\SpoonSession::set('frontend_language', $language);
// remove the language part
array_shift($chunks);
} elseif (CommonCookie::exists('frontend_language') && in_array(CommonCookie::get('frontend_language'), $redirectLanguages)) {
// set languageId
$language = (string) CommonCookie::get('frontend_language');
// redirect is needed
$mustRedirect = true;
} else {
// default browser language
// set languageId & abbreviation
$language = Language::getBrowserLanguage();
// try to set a cookie with the language
try {
// set cookie
CommonCookie::set('frontend_language', $language);
} catch (\SpoonCookieException $e) {
// settings cookies isn't allowed, because this isn't a real problem we ignore the exception
}
// redirect is needed
$mustRedirect = true;
}
// redirect is required
if ($mustRedirect) {
// build URL
$URL = rtrim('/' . $language . '/' . $this->getQueryString(), '/');
// when we are just adding the language to the domain, it's a temporary redirect because
// Safari keeps the 301 in cache, so the cookie to switch language doesn't work any more
$redirectCode = $URL == '/' . $language ? 302 : 301;
// set header & redirect
\SpoonHTTP::redirect($URL, $redirectCode);
}
}
// define the language
defined('FRONTEND_LANGUAGE') || define('FRONTEND_LANGUAGE', $language);
// sets the locale file
Language::setLocale($language);
// list of pageIds & their full URL
$keys = Navigation::getKeys();
// rebuild our URL, but without the language parameter. (it's tripped earlier)
$URL = implode('/', $chunks);
$startURL = $URL;
// loop until we find the URL in the list of pages
while (!in_array($URL, $keys)) {
// remove the last chunk
array_pop($chunks);
// redefine the URL
$URL = implode('/', $chunks);
}
// remove language from query string
if ($hasMultiLanguages) {
$queryString = trim(substr($queryString, strlen($language)), '/');
}
// if it's the homepage AND parameters were given (not allowed!)
if ($URL == '' && $queryString != '') {
// get 404 URL
$URL = Navigation::getURL(404);
// remove language
if ($hasMultiLanguages) {
$URL = str_replace('/' . $language, '', $URL);
}
}
// set pages
$URL = trim($URL, '/');
// currently not in the homepage
if ($URL != '') {
// explode in pages
$pages = explode('/', $URL);
// reset pages
$this->setPages($pages);
// reset parameters
$this->setParameters(array());
}
// set parameters
$parameters = trim(substr($startURL, strlen($URL)), '/');
// has at least one parameter
if ($parameters != '') {
// parameters will be separated by /
$parameters = explode('/', $parameters);
// set parameters
$this->setParameters($parameters);
}
// pageId, parentId & depth
$pageId = Navigation::getPageId(implode('/', $this->getPages()));
$pageInfo = Navigation::getPageInfo($pageId);
// invalid page, or parameters but no extra
if ($pageInfo === false || !empty($parameters) && !$pageInfo['has_extra']) {
// get 404 URL
$URL = Navigation::getURL(404);
// remove language
if ($hasMultiLanguages) {
$URL = trim(str_replace('/' . $language, '', $URL), '/');
}
// currently not in the homepage
if ($URL != '') {
// explode in pages
$pages = explode('/', $URL);
// reset pages
$this->setPages($pages);
// reset parameters
$this->setParameters(array());
}
}
// is this an internal redirect?
if (isset($pageInfo['redirect_page_id']) && $pageInfo['redirect_page_id'] != '') {
// get url for item
$newPageURL = Navigation::getURL((int) $pageInfo['redirect_page_id']);
$errorURL = Navigation::getURL(404);
// not an error?
if ($newPageURL != $errorURL) {
// redirect
\SpoonHTTP::redirect($newPageURL, $pageInfo['redirect_code']);
}
}
// is this an external redirect?
if (isset($pageInfo['redirect_url']) && $pageInfo['redirect_url'] != '') {
// redirect
\SpoonHTTP::redirect($pageInfo['redirect_url'], $pageInfo['redirect_code']);
}
}