tiki-switch_lang.php 3.61 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
changi67's avatar
changi67 committed
3 4 5
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
6
// $Id$
7

changi67's avatar
changi67 committed
8
require_once ('tiki-setup.php');
9

10
$access->check_feature('change_language');
11

changi67's avatar
changi67 committed
12 13 14
if (isset($_GET['from'])) $orig_url = $_GET['from'];
elseif (isset($_SERVER['HTTP_REFERER'])) $orig_url = $_SERVER['HTTP_REFERER'];
else $orig_url = $prefs['tikiIndex'];
15 16

if ($prefs['feature_sefurl'] == 'y' && !strstr($orig_url, '.php')) { 
17 18 19
	if (preg_match('/cat[0-9]+-?/', $orig_url)) {
		include_once('tiki-sefurl.php');
		$orig_url = filter_out_sefurl(preg_replace('#(.*)\/cat([0-9]+)(.*)#', '/tiki-browse_categories.php?parentId=$2$3', $orig_url), $smarty, 'category');
20 21 22 23 24
	} elseif (preg_match('/article[0-9]+-?/', $orig_url)) {
		$orig_url = preg_replace('#\/article([0-9]+)(.*)#', '/tiki-read_article.php?articleId=$1', $orig_url);
	} else {
		$orig_url = preg_replace('#\/([^\/]+)$#', '/tiki-index.php?page=$1', $orig_url);
	}
25
} elseif (!strstr($orig_url, '.php')) {
26 27 28 29
        $params = parse_url($orig_url);
        if (empty($params['query']))
                $orig_url = $prefs['tikiIndex'];
}
30

31
if (strstr($orig_url, 'tiki-index.php') || strstr($orig_url, 'tiki-read_article.php')) {
changi67's avatar
changi67 committed
32 33
	global $multilinguallib;
	include_once ("lib/multilingual/multilinguallib.php");
34 35 36 37 38 39
	$orig_url = urldecode($orig_url);
	if (($txt = strstr($orig_url, '?')) == false) {
		$txt = '';
	} else {
		$txt = substr($txt, 1);
	}
40
	TikiLib::parse_str($txt, $param);
41 42 43
	if (!empty($param['page_id'])) {
		$pageId = $param['page_id'];
		$type = 'wiki page';
44 45 46
	} else if (!empty($param['articleId'])) {
                $pageId = $param['articleId'];
                $type = 'article';
47 48 49 50 51 52
	} else if (!empty($param['page'])) {
		$page = $param['page'];
		$info = $tikilib->get_page_info($page);
		$pageId = $info['page_id'];
		$type = 'wiki page';
	} else {
changi67's avatar
changi67 committed
53 54
		global $wikilib;
		include_once ('lib/wiki/wikilib.php');
55 56 57 58 59 60 61 62
		$page = $wikilib->get_default_wiki_page();
		$info = $tikilib->get_page_info($page);
		$pageId = $info['page_id'];
		$type = 'wiki page';
	}
	$bestLangPageId = $multilinguallib->selectLangObj($type, $pageId, $_REQUEST['language']);
	if ($pageId != $bestLangPageId) {
		if (!empty($param['page_id'])) {
changi67's avatar
changi67 committed
63
			$orig_url = preg_replace('/(.*[&?]page_id=)' . $pageId . '(.*)/', '${1}' . $bestLangPageId . '$2', $orig_url);
64
		} elseif (!empty($param['articleId'])) {
changi67's avatar
changi67 committed
65
			$orig_url = preg_replace('/(.*[&?]articleId=)' . $pageId . '(.*)/', '${1}' . $bestLangPageId . '$2', $orig_url);
66
		} else {
67
			$newPage = urlencode($tikilib->get_page_name_from_id($bestLangPageId));
68
			$orig_url = preg_replace('/(.*[&?]page=)'.$page.'(.*)/', '${1}'."${newPage}".'$2', $orig_url);
69
			$orig_url = preg_replace('/(.*)(tiki-index.php)$/', "$1$2?page=$newPage", $orig_url);
70 71
		}
	}
72 73
	$orig_url = preg_replace('/(.*)no_bl=y&(.*)/', '$1$2', $orig_url);
	$orig_url = preg_replace('/(.*)&no_bl=y(.*)/', '$1$2', $orig_url);
74
	if ($prefs['feature_sefurl'] == 'y') {
75 76
		include_once('tiki-sefurl.php');
		$orig_url = filter_out_sefurl($orig_url, $smarty);
77
	}
78
}
79
$orig_url = preg_replace('/(.*\?.*)switchLang=[a-zA-Z-_]*&?(.*)/', '$1$2', $orig_url);
changi67's avatar
changi67 committed
80 81
$orig_url = preg_replace('/(.*[?&]lang=)[a-zA-Z-_]*(&?.*)/', '$1' . $_REQUEST['language'] . '$2', $orig_url); // for tiki-view_lang.php?lang=en
if (isset($_GET['language'])) {
82
	$language = $_GET['language'];
changi67's avatar
changi67 committed
83
	if ($user && $prefs['change_language'] == 'y') {
84
		$tikilib->set_user_preference($user, 'language', $language);
85 86 87
	} else {
		$_SESSION['s_prefs']['language'] = $language;
		$prefs['language'] = $language;
88
	}
89
}
90 91
header("location: $orig_url");
exit;