Index: branches/5.1.x/core/units/helpers/mod_rewrite_helper.php =================================================================== diff -u -N -r13473 -r13489 --- branches/5.1.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 13473) +++ branches/5.1.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 13489) @@ -1,6 +1,6 @@ Application->VerifyThemeId(); $this->Application->VerifyLanguageId(); $this->Application->Phrases->Init('phrases'); - $this->Application->VerifyThemeId(); } /** Index: branches/5.1.x/core/install/install_data.sql =================================================================== diff -u -N -r13476 -r13489 --- branches/5.1.x/core/install/install_data.sql (.../install_data.sql) (revision 13476) +++ branches/5.1.x/core/install/install_data.sql (.../install_data.sql) (revision 13489) @@ -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: branches/5.1.x/core/kernel/utility/http_query.php =================================================================== diff -u -N -r13470 -r13489 --- branches/5.1.x/core/kernel/utility/http_query.php (.../http_query.php) (revision 13470) +++ branches/5.1.x/core/kernel/utility/http_query.php (.../http_query.php) (revision 13489) @@ -1,6 +1,6 @@ Application->VerifyLanguageId(); $this->Application->VerifyThemeId(); + $this->Application->VerifyLanguageId(); } } Index: branches/5.1.x/core/units/languages/languages_item.php =================================================================== diff -u -N -r13168 -r13489 --- branches/5.1.x/core/units/languages/languages_item.php (.../languages_item.php) (revision 13168) +++ branches/5.1.x/core/units/languages/languages_item.php (.../languages_item.php) (revision 13489) @@ -1,6 +1,6 @@ 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) + { + $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); - $res = parent::Load($id, $id_field_name, 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); + } + if ($default) { if (!$res) { if ($this->Application->isAdmin) { @@ -86,6 +195,7 @@ $this->Application->SetVar('lang.current_id', $this->GetID() ); $this->Application->SetVar('m_lang', $this->GetID() ); } + return $res; } } \ No newline at end of file Index: branches/5.1.x/core/kernel/utility/unit_config_reader.php =================================================================== diff -u -N -r13473 -r13489 --- branches/5.1.x/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 13473) +++ branches/5.1.x/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 13489) @@ -1,6 +1,6 @@ _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);