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 11, 2017
2 parents cc66f9e + 6c99281 commit 5a168af
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 176 deletions.
5 changes: 5 additions & 0 deletions plugins/staticpages/functions.inc
Expand Up @@ -1646,6 +1646,11 @@ function plugin_migrate_staticpages($old_conf)
*/
function plugin_autouninstall_staticpages()
{

// Delete multi-language support Core Config option for Staticpage Plugin
$c = config::get_instance();
$c->del('langurl_staticpages', 'Core');

$out = array(
/* give the name of the tables, without $_TABLES[] */
'tables' => array('staticpage'),
Expand Down
3 changes: 3 additions & 0 deletions plugins/staticpages/install_defaults.php
Expand Up @@ -206,6 +206,9 @@ function plugin_initconfig_staticpages()
0, 0, 0, 128, true, 'staticpages', 0);
$c->add('default_cache_time', $_SP_DEFAULT['default_cache_time'], 'text',
0, 0, null, 129, true, 'staticpages', 0);
$c->add('langurl_staticpages',array('staticpages', 'index.php', 'page'),'@hidden'
,0, 0, null, 130, true, 'staticpages', 0);
$c->add('langurl_staticpages',array('staticpages', 'index.php', 'page'),'@hidden',7,31,1,1830,TRUE, 'Core', 31); // Hidden config option for Core used to determine language of staticpage url (see _getLanguageInfoFromURL in lib-common)

$c->add('tab_whatsnew', NULL, 'tab', 0, 1, NULL, 0, true, 'staticpages', 1);
$c->add('fs_whatsnew', NULL, 'fieldset', 0, 1, NULL, 0, true, 'staticpages', 1);
Expand Down
2 changes: 2 additions & 0 deletions public_html/admin/install/config-install.php
Expand Up @@ -416,6 +416,8 @@ function install_config()
$c->add('page_navigation_max_pages',7,'text',7,31,NULL,1800,TRUE, $me, 31);
$c->add('default_cache_time_block',0,'text',7,31,NULL,1810,TRUE, $me, 31);
$c->add('titletoid',0,'select',7,31,1,1820,TRUE, $me, 31);
$c->add('langurl_topic',array('', 'index.php', 'topic'),'@hidden',7,31,1,1830,TRUE, $me, 31); // Hidden config option for Core used to determine language of topic url (see _getLanguageInfoFromURL in lib-common)
$c->add('langurl_article',array('', 'article.php', 'story'),'@hidden',7,31,1,1830,TRUE, $me, 31); // Hidden config option for Core used to determine language of article url (see _getLanguageInfoFromURL in lib-common)

$c->add('tab_debug', NULL, 'tab', 7, 32, NULL, 0, TRUE, $me, 32);
$c->add('fs_debug', NULL, 'fieldset', 7, 32, NULL, 0, TRUE, $me, 32);
Expand Down
11 changes: 0 additions & 11 deletions public_html/article.php
Expand Up @@ -111,17 +111,6 @@ 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: 0 additions & 10 deletions public_html/index.php
Expand Up @@ -118,16 +118,6 @@ 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
133 changes: 67 additions & 66 deletions public_html/lib-common.php
Expand Up @@ -6682,7 +6682,8 @@ 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 = _getLanguageFromURL();
$langURLinfo = _getLanguageInfoFromURL();
$langFile = $langURLinfo[0];

if (empty($langFile)) {
if (!empty($_USER['language'])) {
Expand Down Expand Up @@ -6711,83 +6712,80 @@ function COM_getLanguage()
}

/**
* Get language name from current URL
* Get language name and plugin name and id 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()
* @param boolean Tells function to return either language or id of plugin item if found
* @return array e.g., 'english', 'japanese', ... , plugin name, id
*/
function _getLanguageInfoFromURL()
{
global $_CONF, $_URL;

$retval = '';
$retval = array('','','');

// 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();

// Need to see if language is set for url. Supports normal, rewrite, and routing urls.
// To support multi-language items plugins need to include an extra config option for Core that is called langurl_pluginname
// This allows this function to figure out by looping through these config options langurl_ what plugin the url is for and if it is an
// item that supports multiple languages.
// This function is used by COM_getLanguage which is called for anonymous visitors when then first visit a page. It is called very early in the process
// so no plugin config options or actual functions are available so that is why we need to store the config option in Core since these are already loaded and
// we already know which plugins are enabled.
// This function also allows the switch block (phpblock_switch_language) to determine if the url is of an item of a plugin which supports multiple languages.
// This means the plugin must also support PLG_getItemInfo (specifically checking the id) so the new id with the switched language of the item can be checked to see if it exists or not.
// Below are example config values for Article, Topic and Staticpages which all support multiple languages.
// To support multiple languages, the config option langurl_pluginname must contain an array that returns the directory, the file name, and the url variable of the supported url.
// Keep the rest of the settings after the array the same.
// $c->add('langurl_topic',array('', 'index.php', 'topic'),'@hidden',7,31,1,1830,TRUE, 'Core', 31);
// $c->add('langurl_article',array('', 'article.php', 'story'),'@hidden',7,31,1,1830,TRUE, 'Core', 31);
// $c->add('langurl_staticpages',array('staticpages', 'index.php', 'page'),'@hidden',7,31,1,1830,TRUE, 'Core', 31);

//$url = COM_getCurrentURL();
$curdirectory = ltrim(ltrim(dirname($_SERVER['REQUEST_URI']), '\\'), '/');
$curfilename = basename($_SERVER['SCRIPT_NAME']);

// URL parts of array returned are: plugin name, directory, filename, id
$url_lang = PLG_getLanguageURL();

foreach ($url_lang as $value) {
$var = "";
// Found a matching directory and file
if ($_CONF['url_rewrite']) {
if ($_CONF['url_routing']) {
// url_routing = 1 - enabled with index.php
// url_routing = 2 - enabled without index.php





if (empty($value[1])) {
$checkdir = $value[2];
} 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;
}
}
$checkdir = $value[1] . "/" . $value[2];
}
} else { // URL contains '?' or '&'
$url = explode('&', $url);
$urlpart = $url[0];
$l = strrpos($urlpart, '_');
if ($l !== false) {
$langId = substr($urlpart, $l + 1);

if ($curdirectory == $checkdir OR ($_CONF['url_routing'] AND ($curfilename == $checkdir))) {
// Found a matching variable
COM_setArgNames(array($value[3]));
$var = COM_applyFilter(COM_getArgument($value[3]));
}
}
} 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
}
} else {
if ($curdirectory . "/" . $curfilename == $value[1] . "/" . $value[2]) {
// Found a matching variable
$var = Geeklog\Input::fRequest($value[3], '');
}
}

if (!empty($var)) {
// Now lets see if language id
$l = strrpos($var, '_');
if ($l !== false) {
$langId = substr($var, $l + 1);
break;
}
}
}

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];
$retval = array($_CONF['language_files'][$langId], $value[0], $var);
}
}
}
Expand Down Expand Up @@ -6920,8 +6918,9 @@ function phpblock_switch_language()
$langId = COM_getLanguageId($lang);
$newLang = '';
$newLangId = '';
// Figure out if we need to include language
$itemId = $_URL->getId();
$langURLinfo = _getLanguageInfoFromURL();
$itemId = $langURLinfo[2];
$itemType = $langURLinfo[1];

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

$switchUrl = COM_buildURL($_CONF['site_url'] . '/switchlang.php?lang=' . $newLangId . '&itemid' . $itemId);
$switchUrl = COM_buildURL($_CONF['site_url'] . '/switchlang.php?lang=' . $newLangId . '&itemid' . $itemId . '&itemtype' . $itemType);
$retval .= COM_createLink($newLang, $switchUrl);
} else {
$retval .= '<form name="change" action="' . $_CONF['site_url']
Expand All @@ -6941,7 +6940,9 @@ function phpblock_switch_language()
$retval .= '<input type="hidden" name="oldlang" value="' . $langId
. '"' . XHTML . '>' . LB;
$retval .= '<input type="hidden" name="itemid" value="' . $itemId
. '"' . XHTML . '>' . LB;
. '"' . XHTML . '>' . LB;
$retval .= '<input type="hidden" name="itemtype" value="' . $itemType
. '"' . XHTML . '>' . LB;

$retval .= '<select onchange="change.submit()" name="lang">';
foreach ($_CONF['languages'] as $key => $value) {
Expand Down
10 changes: 0 additions & 10 deletions public_html/staticpages/index.php
Expand Up @@ -55,16 +55,6 @@
$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
37 changes: 20 additions & 17 deletions public_html/switchlang.php
Expand Up @@ -42,21 +42,34 @@
* @param string $oldLang old, i.e. current language
* @return string new URL after the language switch
*/
function switch_language($url, $newLang, $oldLang, $itemId)
function switch_language($url, $newLang, $oldLang, $itemId, $itemType)
{
global $_CONF, $_URL;

// 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))) {
if (empty($itemType) || empty($itemId) || empty($newLang) || empty($oldLang) || (strlen($newLang) !== strlen($oldLang))) {
return $url;
}

$retval = "";

// 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)
// Still not 100% perfect search solution as other variables in string may have the exact same id (though highly unlikely)
$pos = strrpos($itemId, "_");
if ($pos) {
$newItemId = substr($itemId, 0, $pos) . '_' . $newLang;
$retval = str_replace($itemId, $newItemId, $url);

if ($_CONF['switchlang_homepage']) {
// Figure out if new item exists or user has access
$newItemId = PLG_getItemInfo($itemType, $newItemId, 'id'); // if it does the id will be returned
}

if (!(empty($newItemId))) {
$retval = str_replace($itemId, $newItemId, $url);
} else {
// Doesn't exist so homepage
$retval = $_CONF['site_url'] . '/';
}
} else {
// Something went wrong so just return original url
$retval = $url;
Expand All @@ -80,6 +93,7 @@ function switch_language($url, $newLang, $oldLang, $itemId)
$lang = preg_replace('/[^a-z0-9\-_]/', '', $lang);
$oldLang = COM_getLanguageId();
$itemId = Geeklog\Input::fRequest('itemid', '');
$itemType = Geeklog\Input::fRequest('itemtype', '');

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

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

Expand All @@ -113,15 +127,4 @@ function switch_language($url, $newLang, $oldLang, $itemId)
$ret_url = $_CONF['site_url'] . '/';
}

// **** ISSUE with switchlang
// Article, Topics, and Staticpages (and maybe other third party plugins) support multiple languages
// If a page is already in a specific language there may not be a page for it in the new language being switched too or the user could not have access to it
// The problem is there currently is no easy way (with regular and rewritten urls) to determine if the last thing accessed was (article, topic, some plugin, admin page, whatever) or the id of it if there was one
// If there was, then we could maybe use PLG_getItemInfo (which may need modifying to work with topics) to figure out access
// Current fix is a setting in the config which allows the admin to have the switch language block to always redirect to the homepage
if ($_CONF['switchlang_homepage']) {
$ret_url = $_CONF['site_url'] . '/';
}


header("Location: $ret_url");

0 comments on commit 5a168af

Please sign in to comment.