Index: install/install_data.sql =================================================================== --- install/install_data.sql (revision 13474) +++ install/install_data.sql (working copy) @@ -34,6 +34,7 @@ INSERT INTO ConfigurationValues VALUES(DEFAULT, 'UseModRewrite', '0', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_use_modrewrite', 'checkbox', '', '', 10.02, 0, 1, NULL); INSERT INTO ConfigurationValues VALUES(DEFAULT, 'ModRewriteUrlEnding', '.html', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_ModRewriteUrlEnding', 'select', '', '=+,/=+/,.html=+.html', 10.021, 0, 0, NULL); INSERT INTO ConfigurationValues VALUES(DEFAULT, 'ForceModRewriteUrlEnding', '0', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_ForceModRewriteUrlEnding', 'checkbox', '', NULL, 10.022, 0, 0, 'la_hint_ForceModRewriteUrlEnding'); +INSERT INTO ConfigurationValues VALUES(DEFAULT, 'UseContentLanguageNegotiation', '0', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_UseContentLanguageNegotiation', 'checkbox', '', '', 10.023, 0, 0, NULL); INSERT INTO ConfigurationValues VALUES(DEFAULT, 'cms_DefaultDesign', '#default_design#', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_DefaultDesignTemplate', 'text', NULL, NULL, 10.03, 0, 0, NULL); INSERT INTO ConfigurationValues VALUES(DEFAULT, 'ErrorTemplate', 'error_notfound', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_error_template', 'text', '', '', 10.04, 0, 0, NULL); INSERT INTO ConfigurationValues VALUES(DEFAULT, 'NoPermissionTemplate', 'no_permission', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_nopermission_template', 'text', '', '', 10.05, 0, 0, NULL); Index: install/upgrades.sql =================================================================== --- install/upgrades.sql (revision 13474) +++ install/upgrades.sql (working copy) @@ -1810,3 +1810,5 @@ UPDATE Phrase SET l<%PRIMARY_LANGUAGE%>_Translation = 'Enable SEO-friendly URLs mode (MOD-REWRITE)' WHERE Phrase = 'la_config_use_modrewrite' AND l<%PRIMARY_LANGUAGE%>_Translation = 'Use MOD REWRITE'; + +INSERT INTO ConfigurationValues VALUES(DEFAULT, 'UseContentLanguageNegotiation', '0', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsWebsite', 'la_config_UseContentLanguageNegotiation', 'checkbox', '', '', 10.023, 0, 0, NULL); \ No newline at end of file Index: kernel/utility/http_query.php =================================================================== --- kernel/utility/http_query.php (revision 13474) +++ kernel/utility/http_query.php (working copy) @@ -263,8 +263,8 @@ } } else { + $this->Application->VerifyThemeId(); $this->Application->VerifyLanguageId(); - $this->Application->VerifyThemeId(); } } Index: kernel/utility/unit_config_reader.php =================================================================== --- kernel/utility/unit_config_reader.php (revision 13474) +++ kernel/utility/unit_config_reader.php (working copy) @@ -91,6 +91,7 @@ // output related 'UseModRewrite', + 'UseContentLanguageNegotiation', 'UseOutputCompression', 'OutputCompressionLevel', 'Config_Server_Time', Index: units/helpers/mod_rewrite_helper.php =================================================================== --- units/helpers/mod_rewrite_helper.php (revision 13474) +++ units/helpers/mod_rewrite_helper.php (working copy) @@ -266,9 +266,9 @@ function InitAll() { + $this->Application->VerifyThemeId(); $this->Application->VerifyLanguageId(); $this->Application->Phrases->Init('phrases'); - $this->Application->VerifyThemeId(); } /** Index: units/languages/languages_item.php =================================================================== --- units/languages/languages_item.php (revision 13474) +++ units/languages/languages_item.php (working copy) @@ -56,17 +56,128 @@ return number_format($number, $precision, $this->GetDBField('DecimalPoint'), $this->GetDBField('ThousandSep')); } + /** + * Returns language id based on HTTP header "Accept-Language" + * + * @return int + */ + function processAcceptLanguage() + { + if (!array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) || !$_SERVER['HTTP_ACCEPT_LANGUAGE']) { + return false; + } + + $accepted_languages = Array (); + $language_string = explode(',', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']) ); + + foreach ($language_string as $mixed_language) { + if (strpos($mixed_language, ';') !== false) { + list ($language_locale, $language_quality) = explode(';', $mixed_language); + $language_quality = (float)substr($language_quality, 2); // format "q=4.45" + } + else { + $language_locale = $mixed_language; + $language_quality = 1.00; + } + + $accepted_languages[ trim($language_locale) ] = trim($language_quality); + } + + arsort($accepted_languages, SORT_NUMERIC); + + foreach ($accepted_languages as $language_locale => $language_quality) { + $language_id = $this->getAvailableLanguage($language_locale); + + if ($language_id) { + return $language_id; + } + } + + return false; + } + + /** + * Returns language ID based on given locale + * + * @param string $locale + * @return int + */ + function getAvailableLanguage($locale) + { + static $available_languages; + + $cache_key = 'available_languages[%LangSerial%]'; + $available_languages = $this->Application->getCache($cache_key); + + if ($available_languages === false) { + $this->Conn->nextQueryCachable = true; + $sql = 'SELECT LanguageId, LOWER(Locale) AS Locale + FROM ' . TABLE_PREFIX . 'Language + WHERE Enabled = 1'; + $available_languages = $this->Conn->GetCol($sql, 'Locale'); + $this->Application->setCache($cache_key, $available_languages); + } + + if (strpos($locale, '-') !== false) { + // exact language match requested + return array_key_exists($locale, $available_languages) ? $available_languages[$locale] : false; + } + + // partial (like "en" matches "en-GB" and "en-US") language match required + foreach ($available_languages as $language_code => $language_id) { + list ($language_code, ) = explode('-', $language_code); + + if ($locale == $language_code) { + return $language_id; + } + } + + return false; + } + + /** + * Checks, that url is empty + * + * @return bool + */ + function isEmptyUrl() + { + if ($this->Application->RewriteURLs()) { + return !$this->Application->GetVar('_mod_rw_url_'); + } + + return !count($this->Application->HttpQuery->Get); + } + function Load($id, $id_field_name=null) { $default = false; if ($id == 'default') { - $id = 1; - $id_field_name = 'PrimaryLang'; $default = true; + $res = parent::Load(1, 'PrimaryLang', true); + + if ( + !$this->Application->isAdmin && $this->isEmptyUrl() && + $this->Application->ConfigValue('UseContentLanguageNegotiation') + ) { + $language_id = $this->processAcceptLanguage(); + + if ($language_id != $this->GetID()) { + // redirect to same page with found language + $url_params = Array ( + 'm_cat_id' => 0, 'm_cat_page' => 1, + 'm_lang' => $language_id, 'm_opener' => 's', + 'pass' => 'm' + ); + + $this->Application->Redirect('', $url_params); + } + } } + else { + $res = parent::Load($id, $id_field_name, true); + } - $res = parent::Load($id, $id_field_name, true); - if ($default) { if (!$res) { if ($this->Application->isAdmin) { @@ -86,6 +197,7 @@ $this->Application->SetVar('lang.current_id', $this->GetID() ); $this->Application->SetVar('m_lang', $this->GetID() ); } + return $res; } } \ No newline at end of file