Skip to content

Commit

Permalink
Merge branch 'master' into feature/twofactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Dec 10, 2017
2 parents f42d42b + ed4e6f6 commit cc66f9e
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 146 deletions.
11 changes: 11 additions & 0 deletions public_html/article.php
Expand Up @@ -111,6 +111,17 @@ function extractExternalLinks($text) {
COM_handle404();
}

// If user is allowed to switch languages
if ($_CONF['allow_user_language'] == 1) {
// Let's figure out if page is for specific language
// If so let URL class know in case user changes language
$article_lang = COM_getLanguageIdForObject($sid);
if (!empty($sid) AND !empty($article_lang)) {
$_URL->setItemInfo($sid, $article_lang);
}
}


// Get topic
TOPIC_getTopic('article', $sid);

Expand Down
10 changes: 10 additions & 0 deletions public_html/index.php
Expand Up @@ -118,6 +118,16 @@ function fixTopic(&$A, $tid_list)
$page = (int) Geeklog\Input::get('page', 0);
}

// If user is allowed to switch languages
if ($_CONF['allow_user_language'] == 1) {
// Let's figure out if topic is for specific language
// If so let URL class know in case user changes language
$topic_lang = COM_getLanguageIdForObject($topic);
if (!empty($topic) AND !empty($topic_lang)) {
$_URL->setItemInfo($topic, $topic_lang);
}
}

if ($topic_check === '-') {
$topic_check = '';
}
Expand Down
167 changes: 93 additions & 74 deletions public_html/lib-common.php
Expand Up @@ -6682,7 +6682,7 @@ function COM_getLanguage()

// 1. Try to get language from URL
// $langFile = COM_getLanguageFromBrowser(); - Removed line as it doesn't work with the switch language block (that uses phpblock_switch_language) for some setups when a language cookie is set, need to check that first.
$langFile = COM_getLanguageFromURL();
$langFile = _getLanguageFromURL();

if (empty($langFile)) {
if (!empty($_USER['language'])) {
Expand Down Expand Up @@ -6710,6 +6710,91 @@ function COM_getLanguage()
return $langFile;
}

/**
* Get language name from current URL
* Note: This function starts with _ therefore it should only call from within core
*
*
* @return string e.g., 'english', 'japanese', ...
* @note code provided by hiroron
*/
function _getLanguageFromURL()
{
global $_CONF, $_URL;

$retval = '';

// If user is allowed to switch languages
if ($_CONF['allow_user_language'] == 1) {
$langId = '';
// Need to see if language is set for url
if (!$_URL->itemSet()) {
// THERE ARE ISSUES with this and figuring out the language from the URL may be incorrect if for example "_en" is found elsewhere in the url (see https://github.com/Geeklog-Core/geeklog/issues/819)
// This is used if the anonymous user visits the page directly to figure out what language to display the site in.
// If the user is logged in or has a language cookie already set (by the switch language block) from before then that language is taken (done at the beginning of lib-common)

// We need to figure out if there is a specific language specified in the url
// Since COM_getLanguage is called right at the beginning of lib-common we don't have the option of setting the $_URL language before hand
// so we must make our best guess based on the type of URL (normal, rewritten, routing)

// Another problem is we don't try to figure out if the item supports multiple language (article, topic, staticpage, other plugin)

$url = COM_getCurrentURL();

if ($_CONF['url_rewrite']) {
if ($_CONF['url_routing']) {
// url_routing = 1 - enabled with index.php
// url_routing = 2 - enabled without index.php





} else {
// for "rewritten" URLs we assume that the first parameter after
// [NG]the script name is the ID, e.g. /article.php/story-id-here_en
// 2017 fix hiroron /index.php/topic/home_ja or /index.php?topic=home_ja
$p = explode('/', $url);
$parts = count($p);
for ($i = 0; $i < $parts; $i++) {
if (strrpos($p[$i], '.php') !== false) {
for ($j = $i; $j < $parts; $j++) {
if (isset($p[$j])) {
$l = strrpos($p[$j], '_');
if ($l !== false) {
$langId = substr($p[$j], $l + 1);
break;
}
}
}
break;
}
}
}
} else { // URL contains '?' or '&'
$url = explode('&', $url);
$urlpart = $url[0];
$l = strrpos($urlpart, '_');
if ($l !== false) {
$langId = substr($urlpart, $l + 1);
}
}
} else {
// This is used mainly by the switch language block (after the visitor has been on the site already)
// The item also had a chance to update $_URL with the correct language since if this code is executed it has most likely been called after lib-common has been loaded
$langId = $_URL->getLanguage(); // If empty no language is assumed or item does not support multi-language
}
if (!empty($langId)) {
if (isset($_CONF['language_files']) && is_array($_CONF['language_files']) &&
array_key_exists($langId, $_CONF['language_files'])) {
$retval = $_CONF['language_files'][$langId];
}
}
}

return $retval;
}

/**
* Determine the language of the object from the id
*
Expand Down Expand Up @@ -6821,11 +6906,11 @@ function COM_getLangSQL($field, $type = 'WHERE', $table = '')
*/
function phpblock_switch_language()
{
global $_CONF;
global $_CONF, $_URL;

$retval = '';

if (empty($_CONF['languages']) || empty($_CONF['language_files']) ||
if ($_CONF['allow_user_language'] == 0 || empty($_CONF['languages']) || empty($_CONF['language_files']) ||
(count($_CONF['languages']) !== count($_CONF['language_files']))
) {
return $retval;
Expand All @@ -6835,6 +6920,8 @@ function phpblock_switch_language()
$langId = COM_getLanguageId($lang);
$newLang = '';
$newLangId = '';
// Figure out if we need to include language
$itemId = $_URL->getId();

if (count($_CONF['languages']) === 2) {
foreach ($_CONF['languages'] as $key => $value) {
Expand All @@ -6845,14 +6932,16 @@ function phpblock_switch_language()
}
}

$switchUrl = COM_buildURL($_CONF['site_url'] . '/switchlang.php?lang=' . $newLangId);
$switchUrl = COM_buildURL($_CONF['site_url'] . '/switchlang.php?lang=' . $newLangId . '&itemid' . $itemId);
$retval .= COM_createLink($newLang, $switchUrl);
} else {
$retval .= '<form name="change" action="' . $_CONF['site_url']
. '/switchlang.php" method="get">' . LB;
$retval .= '<div>' . LB;
$retval .= '<input type="hidden" name="oldlang" value="' . $langId
. '"' . XHTML . '>' . LB;
$retval .= '<input type="hidden" name="itemid" value="' . $itemId
. '"' . XHTML . '>' . LB;

$retval .= '<select onchange="change.submit()" name="lang">';
foreach ($_CONF['languages'] as $key => $value) {
Expand Down Expand Up @@ -8132,76 +8221,6 @@ function COM_getDocumentUrl($baseDirectory, $fileName)
return $retval;
}

/**
* Get language ID from a URL
*
* @param string $url
* @return string e.g., 'en', 'ja', ...
* @note code provided by hiroron
*/
function COM_getLanguageIdFromURL($url = '')
{
global $_CONF;

if (empty($url)) {
$url = COM_getCurrentURL();
}
$retval = '';
if ($_CONF['url_rewrite']) {
// for "rewritten" URLs we assume that the first parameter after
// [NG]the script name is the ID, e.g. /article.php/story-id-here_en
// 2017 fix hiroron /index.php/topic/home_ja or /index.php?topic=home_ja
$p = explode('/', $url);
$parts = count($p);
for ($i = 0; $i < $parts; $i++) {
if (strrpos($p[$i], '.php') !== false) {
for ($j = $i; $j < $parts; $j++) {
if (isset($p[$j])) {
$l = strrpos($p[$j], '_');
if ($l !== false) {
$retval = substr($p[$j], $l + 1);
break;
}
}
}
break;
}
}
} else { // URL contains '?' or '&'
$url = explode('&', $url);
$urlpart = $url[0];
$l = strrpos($urlpart, '_');
if ($l !== false) {
$retval = substr($urlpart, $l + 1);
}
}

return $retval;
}

/**
* Get language name from a URL
*
* @param string $url
* @return string e.g., 'english', 'japanese', ...
* @note code provided by hiroron
*/
function COM_getLanguageFromURL($url = '')
{
global $_CONF;

$retval = '';
$langId = COM_getLanguageIdFromURL($url);
if (!empty($langId)) {
if (isset($_CONF['language_files']) && is_array($_CONF['language_files']) &&
array_key_exists($langId, $_CONF['language_files'])) {
$retval = $_CONF['language_files'][$langId];
}
}

return $retval;
}

/**
* Return if developer mode is set
*
Expand Down
10 changes: 10 additions & 0 deletions public_html/staticpages/index.php
Expand Up @@ -55,6 +55,16 @@
$display_mode = COM_applyFilter(COM_getArgument('disp_mode'));
$query = Geeklog\Input::fRequest('query', '');

// If user is allowed to switch languages
if ($_CONF['allow_user_language'] == 1) {
// Let's figure out if page is for specific language
// If so let URL class know in case user changes language
$page_lang = COM_getLanguageIdForObject($page);
if (!empty($page) AND !empty($page_lang)) {
$_URL->setItemInfo($page, $page_lang);
}
}

TOPIC_getTopic('staticpages', $page);

// from comments display refresh:
Expand Down
89 changes: 17 additions & 72 deletions public_html/switchlang.php
Expand Up @@ -42,76 +42,26 @@
* @param string $oldLang old, i.e. current language
* @return string new URL after the language switch
*/
function switch_language($url, $newLang, $oldLang)
function switch_language($url, $newLang, $oldLang, $itemId)
{
global $_CONF;
global $_CONF, $_URL;

if (empty($newLang) || empty($oldLang) || (strlen($newLang) !== strlen($oldLang))) {
// See if we need to figure out new URL (ie is url language specific and are any variables missing)
if (empty($itemId) || empty($newLang) || empty($oldLang) || (strlen($newLang) !== strlen($oldLang))) {
return $url;
}

$lang_len = strlen($oldLang);
$url_rewrite = false;
$q = false;

if ($_CONF['url_rewrite']) {
// check for "rewritten" URLs with a '?', e.g. search query highlighting
$q = strpos($url, '?');
if (($q === false) || (substr($url, $q - 4, 4) !== '.php')) {
$url_rewrite = true;
}
}

if ($url_rewrite) {
$the_url = ($q === false) ? $url : substr($url, 0, $q);

// for "rewritten" URLs we assume that the first parameter after
// the script name is the ID, e.g. /article.php/story-id-here_en
$changed = false;
$p = explode('/', $the_url);
$parts = count($p);
for ($i = 0; $i < $parts; $i++) {
if (substr($p[$i], -4) === '.php') {
// found the script name - assume next parameter is the ID
// There is a langID somewhere after the script name (it may no longer be next with url_rewrite enabled, etc..)
for ($j = $i; $j < $parts; $j++) {
if (isset($p[$j + 1])) {
if (substr($p[$j + 1], -($lang_len + 1)) === '_' . $oldLang) {
$p[$j + 1] = substr_replace($p[$j + 1], $newLang, -$lang_len);
$changed = true;
break;
}
}
}
break;
}
}

if ($changed) {
// merge the pieces back together
$url = ($q === false)
? implode('/', $p)
: implode('/', $p) . substr($url, $q);
}


// Technically we don't care about if normal url, rewrite url, or routed url as id will be the same
// Still not 100% perfect search solution as other variables in string may have the exact same id (though highely unlikely)
$pos = strrpos($itemId, "_");
if ($pos) {
$newItemId = substr($itemId, 0, $pos) . '_' . $newLang;
$retval = str_replace($itemId, $newItemId, $url);
} else {
// Something went wrong so just return original url
$retval = $url;
} else { // URL contains '?' or '&'
$url = explode('&', $url);
$urlPart = $url[0];
if (count($url) > 1) {
array_shift($url);
$extra_vars = '&' . implode('&', $url);
} else {
$extra_vars = '';
}

if (substr($urlPart, -($lang_len + 1)) === '_' . $oldLang) {
$urlPart = substr_replace($urlPart, $newLang, -$lang_len);
}

$retval = $urlPart . $extra_vars;
}

return $retval;
}

Expand All @@ -129,13 +79,8 @@ function switch_language($url, $newLang, $oldLang)
$lang = strtolower(COM_applyFilter(COM_getArgument('lang')));
$lang = preg_replace('/[^a-z0-9\-_]/', '', $lang);
$oldLang = COM_getLanguageId();

// Code provided by hiroron
if ($lang === $oldLang) {
$langFromUrl = COM_getLanguageFromURL($ret_url);
$oldLang = empty($langFromUrl) ? $oldLang : $langFromUrl;
}

$itemId = Geeklog\Input::fRequest('itemid', '');

// do we really have a new language to switch to?
if (!empty($lang) && array_key_exists($lang, $_CONF['language_files'])) {
// does such a language file exist?
Expand All @@ -159,7 +104,7 @@ function switch_language($url, $newLang, $oldLang)

// Change the language ID if needed
if (!empty($ret_url) && !empty($lang) && !empty($oldLang)) {
$ret_url = switch_language($ret_url, $lang, $oldLang);
$ret_url = switch_language($ret_url, $lang, $oldLang, $itemId);
}
}

Expand Down

0 comments on commit cc66f9e

Please sign in to comment.