Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added verifier to only allow existing languages in URL

also, pages are redirected when lang is same as default (for example "/en/about/" goes to "/about/")
  • Loading branch information...
commit bb4fcfd0b0a7851dc38430be4c99270acab7145b 1 parent 8ae3cbe
@vdepizzol authored
Showing with 65 additions and 94 deletions.
  1. +46 −6 url.php
  2. +19 −88 wppo.php
View
52 url.php
@@ -84,6 +84,13 @@ function wppo_find_lang_in_uri() {
}
function wppo_remove_lang_from_request_uri() {
+
+ global $wppo_cache;
+
+ /*
+ * Create cache of all available languages
+ */
+ wppo_get_lang();
/*
* $lang_rule, $req_uri, $home_path
@@ -96,10 +103,23 @@ function wppo_remove_lang_from_request_uri() {
* access
*/
- // We need to check if the language exists
- // FIXME
$lang = wppo_find_lang_in_uri();
+
+ if (strlen($lang) == 2) {
+ $lang_in_gettext_format = strtolower($lang);
+ } elseif(strlen($lang) == 5) {
+ $lang_in_gettext_format = strtolower(substr($lang, 0, 2)).'_'.strtoupper(substr($lang, 3, 2));
+ } else {
+ $lang_in_gettext_format = WPPO_DEFAULT_LANGUAGE_CODE;
+ }
+
+ /*
+ * We won't remove lang from request URI if the informed language doesn't exists
+ */
+ if ($lang_in_gettext_format != WPPO_DEFAULT_LANGUAGE_CODE && !array_key_exists($lang_in_gettext_format, $wppo_cache['available_lang'])) {
+ return false;
+ }
/*
* Group extra queries to put in the right place later
@@ -141,10 +161,26 @@ function wppo_remove_lang_from_request_uri() {
* /website_path/about/
*
*/
+
if (preg_match($lang_rule, $req_uri)) {
- $_SERVER['REQUEST_URI'] = '/' .$home_path . '/' . trim(preg_replace($lang_rule, '', $req_uri), '/') . $extra_queries;
+ if($home_path == '') {
+ $_SERVER['REQUEST_URI'] = '/' . trim(preg_replace($lang_rule, '', $req_uri), '/') . $extra_queries;
+ } else {
+ $_SERVER['REQUEST_URI'] = '/' .$home_path . '/' . trim(preg_replace($lang_rule, '', $req_uri), '/') . $extra_queries;
+ }
}
-
+
+ /*
+ * If the requested lang is the same as the default, we'll redirect
+ * to the page without the language permalink.
+ */
+ if($lang_in_gettext_format == WPPO_DEFAULT_LANGUAGE_CODE && $_SERVER['REQUEST_URI'] != WPPO_ABS_URI) {
+ header("Location: ".WPPO_URI_SCHEME.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
+ exit;
+ }
+
+ return true;
+
}
/*
@@ -160,6 +196,10 @@ function wppo_remove_lang_from_request_uri() {
add_filter('redirect_canonical', function($absolute_uri) {
$lang = wppo_get_lang();
+
+ if ($lang == WPPO_DEFAULT_LANGUAGE_CODE) {
+ return $absolute_uri;
+ }
$scheme = explode('://', $absolute_uri);
$scheme = $scheme[0];
@@ -243,7 +283,7 @@ function wppo_recreate_links_preg_replace_callback($matches) {
function wppo_recreate_url($url, $lang, $coverage = 'external') {
$lang = strtolower(str_replace("_", "-", $lang));
- if($lang == 'c') {
+ if($lang == WPPO_DEFAULT_LANGUAGE_CODE) {
return $url;
}
@@ -254,7 +294,7 @@ function wppo_recreate_url($url, $lang, $coverage = 'external') {
* it is pointing to current domain.
*/
if (substr($url, 0, 1) == '/') {
- $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
+ $url = WPPO_URI_SCHEME.'://' . $_SERVER['HTTP_HOST'] . $url;
}
if (substr($url, 0, 1) == '#') {
View
107 wppo.php
@@ -49,6 +49,7 @@
define('WPPO_PREFIX', $wpdb->prefix."wppo_");
define('WPPO_XML2PO_COMMAND', "/usr/bin/xml2po");
define('WPPO_ABS_URI', $_SERVER['REQUEST_URI']);
+define('WPPO_URI_SCHEME', 'http');
define('WPPO_HOME_URL', home_url());
define('WPPO_DEFAULT_LANGUAGE_NAME', 'English');
@@ -365,6 +366,19 @@ function wppo_get_lang() {
if (isset($wppo_cache['lang'])) {
return $wppo_cache['lang'];
}
+
+ /*
+ * Here we'll keep in cache all the existing languages in database.
+ * We use it here to check which desired language from browser exists and is available.
+ */
+ if (!isset($wppo_cache['available_lang'])) {
+ $all_languages = $wpdb->get_results("SELECT lang_code, lang_name FROM ".WPPO_PREFIX."languages WHERE lang_status = 'visible'", ARRAY_A);
+
+ foreach ($all_languages as $index => $array) {
+ $wppo_cache['available_lang'][$array['lang_code']] = $array['lang_name'];
+ }
+ }
+
$lang_uri = wppo_find_lang_in_uri();
@@ -386,104 +400,21 @@ function wppo_get_lang() {
if (strpos($defined_lang, '-') !== false) {
$defined_lang_array = explode('-', $defined_lang);
- $defined_lang = $defined_lang_array[0].'_'.strtoupper($defined_lang_array[1]);
+ $defined_lang = strtolower($defined_lang_array[0]).'_'.strtoupper($defined_lang_array[1]);
}
/*
* Verify if the selected language really exists
*/
- $check_lang = $wpdb->get_row(
- "SELECT lang_code, lang_name FROM ".WPPO_PREFIX."languages ".
- "WHERE lang_status = 'visible' AND lang_code = '".mysql_real_escape_string($defined_lang)."'", ARRAY_A
- );
-
- if ($wpdb->num_rows === 1) {
+ if (array_key_exists($defined_lang, $wppo_cache['available_lang'])) {
$wppo_cache['lang'] = $defined_lang;
return $defined_lang;
}
-
}
-
- /*
- * Since at this point no one told us what language the user wants to see the content,
- * we'll try to guess from the HTTP headers
- */
- if (!function_exists('get_http_accept_lang')) {
-
- function get_http_accept_lang() {
- $langs = array();
- if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
-
- $accept_lang_underscore = str_replace('-', '_', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
-
- preg_match_all('/([a-z]{1,8}(_[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $accept_lang_underscore, $lang_parse);
-
- if (count($lang_parse[1])) {
- $langs = array_combine($lang_parse[1], $lang_parse[4]);
-
- foreach ($langs as $lang => $val) {
- if ($val === '') {
- $langs[$lang] = 1;
- }
- }
-
- arsort($langs, SORT_NUMERIC);
-
- $langs = array_keys($langs);
-
- /*
- * If the first language available also contains the country code,
- * we'll automatically add as a second option the same language without the country code
- * (only if it doesn't already exists)
- */
- if ((strpos($langs[0], '_') !== false) && (!isset($langs[1]) || substr($langs[0], 0, 2) != substr($langs[1], 0, 2))) {
- $default_lang = array_shift($langs);
- $fallback_lang = explode('_', $default_lang);
- array_unshift($langs, $fallback_lang[0]);
- array_unshift($langs, $default_lang);
- }
- }
- }
-
- return $langs;
- }
- }
-
- /*
- * Here we'll keep in cache all the existing languages in database.
- * We use it here to check which desired language from browser exists and is available.
- */
- if (!isset($wppo_cache['available_lang'])) {
- $all_languages = $wpdb->get_results("SELECT lang_code, lang_name FROM ".WPPO_PREFIX."languages WHERE lang_status = 'visible'", ARRAY_A);
-
- foreach ($all_languages as $index => $array) {
- $wppo_cache['available_lang'][$array['lang_code']] = $array['lang_name'];
- }
- }
-
- $user_lang = get_http_accept_lang();
-
- foreach ($user_lang as $lang_code) {
- if (isset($wppo_cache['available_lang'][$lang_code])) {
- $defined_lang = $lang_code;
- break;
- }
- }
-
- if (isset($defined_lang)) {
- $wppo_cache['lang'] = $defined_lang;
- } else {
- /*
- * Returning this means that we won't show any translation to the user.
- * "C" disables all localization.
- */
- $wppo_cache['lang'] = 'C';
- return 'C';
- }
-
- return $defined_lang;
+ $wppo_cache['lang'] = WPPO_DEFAULT_LANGUAGE_CODE;
+ return WPPO_DEFAULT_LANGUAGE_CODE;
}
/*
@@ -508,7 +439,7 @@ function get_http_accept_lang() {
/*
* Define default language for WordPress template
*/
- if (wppo_get_lang() != 'C') {
+ if (wppo_get_lang() != WPPO_DEFAULT_LANGUAGE_CODE) {
if(!defined('WPLANG')) {
define('WPLANG', wppo_get_lang());
}
Please sign in to comment.
Something went wrong with that request. Please try again.