From c94c22edda1bf1b9caca9901dc2480f48858407a Mon Sep 17 00:00:00 2001 From: Dries Date: Mon, 5 Mar 2012 05:36:39 -0500 Subject: [PATCH] =?UTF-8?q?-=20Patch=20#1272840=20by=20attiks,=20G=C3=A1bo?= =?UTF-8?q?r=20Hojtsy,=20pp,=20Dave=20Reid,=20catch:=20upgrade=20path=20fo?= =?UTF-8?q?r=20language=20domains=20and=20validation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/includes/locale.inc | 2 +- core/modules/locale/locale.admin.inc | 13 ++++++++ core/modules/locale/locale.install | 32 +++++++++++++++++++ core/modules/locale/locale.test | 4 +-- .../tests/upgrade/upgrade.language.test | 14 ++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/core/includes/locale.inc b/core/includes/locale.inc index 43bac8d3093..cf322456518 100644 --- a/core/includes/locale.inc +++ b/core/includes/locale.inc @@ -214,7 +214,7 @@ function locale_language_from_user($languages) { // User preference (only for logged users). global $user; - if ($user->uid) { + if ($user->uid && !empty($user->preferred_langcode)) { return $user->preferred_langcode; } diff --git a/core/modules/locale/locale.admin.inc b/core/modules/locale/locale.admin.inc index 2c78e17699e..ff20d56b821 100644 --- a/core/modules/locale/locale.admin.inc +++ b/core/modules/locale/locale.admin.inc @@ -330,6 +330,19 @@ function language_negotiation_configure_url_form_validate($form, &$form_state) { form_error($form['domain'][$langcode], t('The domain for %language, %value, is not unique.', array('%language' => $language->name, '%value' => $value))); } } + + // Domain names should not contain protocol and/or ports. + foreach ($languages as $langcode => $name) { + $value = $form_state['values']['domain'][$langcode]; + if (!empty($value)) { + // Ensure we have a protocol but only one protocol in the setting for + // parse_url() checking against the hostname. + $host = 'http://' . str_replace(array('http://', 'https://'), '', $value); + if (parse_url($host, PHP_URL_HOST) != $value) { + form_error($form['domain'][$langcode], t('The domain for %language may only contain the domain name, not a protocol and/or port.', array( '%language' => $name))); + } + } + } } /** diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index d3f0152dae0..eb11e4fcb39 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -316,6 +316,38 @@ function locale_update_8002() { db_delete('locales_target')->condition('lid', $subquery, 'NOT IN')->execute(); } +/** + * Converts language domains to new format. + */ +function locale_update_8003() { + $message = ''; + $domains = variable_get('locale_language_negotiation_url_domains', array()); + // $used_domains keeps track of the domain names in use. + $used_domains = array(); + foreach ($domains as $langcode => $domain) { + // Domain names can not contain protocol and/or ports. + if (!empty($domain)) { + $host = 'http://' . str_replace(array('http://', 'https://'), '', $domain); + if (parse_url($host, PHP_URL_HOST) != $domain) { + $domains[$langcode] = parse_url($host, PHP_URL_HOST); + } + if (array_key_exists($domain, $used_domains)) { + if (empty($message)) { + $message = 'Some languages are using the same domain name, you should change these domain names at ' . l('URL language detection configuration', 'admin/config/regional/language/configure/url' . '.'); + } + } + else { + $used_domains[$domain] = $domain; + } + } + } + variable_set('locale_language_negotiation_url_domains', $domains); + + if (!empty($message)) { + return $message; + } +} + /** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. diff --git a/core/modules/locale/locale.test b/core/modules/locale/locale.test index 7d1e55cd6e0..4c58e91851d 100644 --- a/core/modules/locale/locale.test +++ b/core/modules/locale/locale.test @@ -2280,8 +2280,8 @@ class LocaleUILanguageNegotiationTest extends DrupalWebTestCase { $this->assertResponse(404, "Unknown language path prefix should return 404"); // Setup for domain negotiation, first configure the language to have domain - // URL. We use https and a port to make sure that only the domain name is used. - $edit = array("domain[$langcode]" => "https://$language_domain:99"); + // URL. + $edit = array("domain[$langcode]" => $language_domain); $this->drupalPost("admin/config/regional/language/detection/url", $edit, t('Save configuration')); // Set the site to use domain language negotiation. diff --git a/core/modules/simpletest/tests/upgrade/upgrade.language.test b/core/modules/simpletest/tests/upgrade/upgrade.language.test index 0dbbf2e99c6..9edf03a71bb 100644 --- a/core/modules/simpletest/tests/upgrade/upgrade.language.test +++ b/core/modules/simpletest/tests/upgrade/upgrade.language.test @@ -97,4 +97,18 @@ class LanguageUpgradePathTestCase extends UpgradePathTestCase { $file = db_query('SELECT * FROM {file_managed} WHERE fid = :fid', array(':fid' => 1))->fetchObject(); $this->assertEqual($file->langcode, LANGUAGE_NONE); } + + /** + * Tests language domain upgrade path. + */ + public function testLanguageUrlUpgrade() { + $language_domain = 'ca.example.com'; + db_update('languages')->fields(array('domain' => 'http://' . $language_domain . ':8888'))->condition('language', 'ca')->execute(); + variable_set('locale_language_negotiation_url_part', 1); + + $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.')); + + $domains = locale_language_negotiation_url_domains(); + $this->assertTrue($domains['ca'] == $language_domain, t('Language domain for Catalan properly upgraded.')); + } }