system: user-based language, no wonder nobody wants works on this ;)

PR: https://github.com/opnsense/core/issues/1218
This commit is contained in:
Franco Fichtner 2017-09-21 22:05:04 +00:00
parent 930e6281d5
commit b605812b71
5 changed files with 146 additions and 74 deletions

View File

@ -5,7 +5,9 @@
<Root url="/" visibility="hidden"/>
</Dashboard>
<License order="1" url="/license.php" cssClass="fa fa-balance-scale fa-fw"/>
<Password order="2" url="/system_usermanager_passwordmg.php" cssClass="fa fa-key fa-fw"/>
<Password order="2" url="/system_usermanager_passwordmg.php" cssClass="fa fa-key fa-fw">
<workAround url="/system_usermanager_passwordmg.php*" visibility="hidden"/>
</Password>
<Logout order="3" url="/index.php?logout" cssClass="fa fa-sign-out fa-fw"/>
</Lobby>
<Reporting order="15" cssClass="fa fa-area-chart">

View File

@ -57,7 +57,6 @@ function html_safe($text)
header("X-Frame-Options: SAMEORIGIN");
/* set language */
$lang = 'en_US';
if (!empty($config['system']['language'])) {
$lang = $config['system']['language'];
@ -73,6 +72,22 @@ bind_textdomain_codeset($textdomain, $lang_encoding);
require_once("authgui.inc");
if (!empty($_SESSION['Username']) && array_key_exists($_SESSION['Username'], $userindex)) {
$old_lang = $lang;
$lang = $config['system']['user'][$userindex[$_SESSION['Username']]]['language'];
/* a bit silly to redo this now but ok... */
if (!empty($lang) && $lang != $old_lang) {
$lang_encoding = $lang . '.UTF-8';
$textdomain = 'OPNsense';
putenv('LANG=' . $lang_encoding);
textdomain($textdomain);
bindtextdomain($textdomain, '/usr/local/share/locale');
bind_textdomain_codeset($textdomain, $lang_encoding);
}
}
/* Reserved table names to avoid colision */
$reserved_table_names = array(
"bogons",

View File

@ -40,12 +40,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$savemsg = htmlspecialchars(gettext($_GET['savemsg']));
}
$pconfig['theme'] = null;
$pconfig['language'] = null;
$pconfig['timezone'] = 'Etc/UTC';
$pconfig['prefer_ipv4'] = isset($config['system']['prefer_ipv4']);
$pconfig['hostname'] = $config['system']['hostname'];
$pconfig['dnsallowoverride'] = isset($config['system']['dnsallowoverride']);
$pconfig['dnslocalhost'] = isset($config['system']['dnslocalhost']);
$pconfig['domain'] = $config['system']['domain'];
$pconfig['hostname'] = $config['system']['hostname'];
$pconfig['language'] = $config['system']['language'];
$pconfig['prefer_ipv4'] = isset($config['system']['prefer_ipv4']);
$pconfig['theme'] = $config['theme'];
$pconfig['timezone'] = $config['system']['timezone'];
$pconfig['timezone'] = 'Etc/UTC';
for ($dnscounter = 1; $dnscounter < 9; $dnscounter++) {
$dnsname = "dns{$dnscounter}";
@ -54,16 +57,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$dnsgwname= "dns{$dnscounter}gw";
$pconfig[$dnsgwname] = !empty($config['system'][$dnsgwname]) ? $config['system'][$dnsgwname] : 'none';
}
$pconfig['dnsallowoverride'] = isset($config['system']['dnsallowoverride']);
$pconfig['timezone'] = $config['system']['timezone'];
if (isset($config['theme'])) {
$pconfig['theme'] = $config['theme'];
}
if (isset($config['system']['language'])) {
$pconfig['language'] = $config['system']['language'];
}
$pconfig['dnslocalhost'] = isset($config['system']['dnslocalhost']);
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input_errors = array();
$pconfig = $_POST;
@ -132,15 +125,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
if (count($input_errors) == 0) {
$config['system']['hostname'] = $pconfig['hostname'];
$config['system']['domain'] = $pconfig['domain'];
$config['system']['hostname'] = $pconfig['hostname'];
$config['system']['language'] = $pconfig['language'];
$config['system']['timezone'] = $pconfig['timezone'];
$config['theme'] = $pconfig['theme'];
if (!empty($pconfig['language']) && $pconfig['language'] != $config['system']['language']) {
$config['system']['language'] = $pconfig['language'];
}
if (!empty($pconfig['prefer_ipv4'])) {
$config['system']['prefer_ipv4'] = true;
} elseif (isset($config['system']['prefer_ipv4'])) {
@ -254,7 +244,7 @@ include("head.inc");
<table class="table table-striped opnsense_standard_table_form">
<tr>
<td width="22%"><strong><?=gettext("System");?></strong></td>
<td width="78%" align="right">
<td width="78%" align="right">
<small><?=gettext("full help"); ?> </small>
<i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i>
</td>
@ -304,7 +294,7 @@ include("head.inc");
<select name="language" class="selectpicker" data-size="10" data-style="btn-default" data-width="auto">
<?php
foreach (get_locale_list() as $lcode => $ldesc):?>
<option value="<?=$lcode;?>" <?=$lcode == $pconfig['language'] ? "selected=\"selected\"" : "";?>>
<option value="<?=$lcode;?>" <?= $lcode == $pconfig['language'] ? 'selected="selected"' : '' ?>>
<?=$ldesc;?>
</option>
<?php

View File

@ -30,6 +30,7 @@
*/
require_once 'guiconfig.inc';
require_once 'system.inc';
require_once 'base32/Base32.php';
function get_user_privdesc(& $user)
@ -135,6 +136,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
} else {
$pconfig[$fieldname] = null;
}
}
foreach (get_locale_list() as $lcode => $ldesc) {
if ($a_user[$id]['language'] == $lcode) {
$pconfig['language'] = $ldesc;
break;
}
}
} else {
// set defaults
@ -150,6 +158,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
}
}
if (empty($pconfig['language'])) {
$pconfig['language'] = 'Default';
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// process post type requests
if (isset($_POST['userid']) && isset($a_user[$_POST['userid']])) {
@ -611,6 +622,13 @@ $( document ).ready(function() {
</div>
</td>
</tr>
<tr>
<td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Language");?></td>
<td>
<input name="language" type="hidden" value="<?= $pconfig['language'] ?>" />
<?= $pconfig['language'] ?>
</td>
</tr>
<tr>
<td><a id="help_for_expires" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Expiration date"); ?></td>
<td>

View File

@ -1,72 +1,93 @@
<?php
/*
Copyright (C) 2014-2015 Deciso B.V.
Copyright (C) 2011 Ermal Luçi
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
* Copyright (C) 2017 Franco Fichtner
* Copyright (C) 2014-2015 Deciso B.V.
* Copyright (C) 2011 Ermal Luçi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
require_once("guiconfig.inc");
require_once("system.inc");
$username = $_SESSION['Username'];
if (isset($_POST['save'])) {
$input_errors = array();
/* input validation */
/* determine if user is not local to system */
$userFound = false;
foreach ($config['system']['user'] as $user) {
if ($user['name'] == $username) {
$userFound = true;
break;
}
}
$reqdfields = explode(' ', 'passwordfld0 passwordfld1');
$reqdfieldsn = array(gettext('Old password'), gettext('New password'));
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$pconfig = array();
if ($_POST['passwordfld1'] != $_POST['passwordfld2'] ||
!password_verify($_POST['passwordfld0'], $config['system']['user'][$userindex[$username]]['password'])) {
$input_errors[] = gettext("The passwords do not match.");
if (isset($_GET['savemsg'])) {
$savemsg = htmlspecialchars(gettext($_GET['savemsg']));
}
/* determine if user is not local to system */
$userFound = false;
foreach ($config['system']['user'] as $user) {
if ($user['name'] == $username) {
$userFound = true;
break;
if ($userFound) {
$pconfig['language'] = $config['system']['user'][$userindex[$username]]['language'];
}
if (empty($pconfig['language'])) {
$pconfig['language'] = $config['system']['language'];
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input_errors = array();
$pconfig = $_POST;
/* we can continue without a password if nothing was provided */
if ($pconfig['passwordfld1'] !== '' || $pconfig['passwordfld2'] !== '') {
if ($pconfig['passwordfld1'] != $pconfig['passwordfld2'] ||
!password_verify($pconfig['passwordfld0'], $config['system']['user'][$userindex[$username]]['password'])) {
$input_errors[] = gettext("The passwords do not match.");
}
if (!$userFound) {
$input_errors[] = gettext("Sorry, you cannot change settings for a non-local user.");
}
}
if (!$userFound) {
$input_errors[] = gettext("Sorry, you cannot change the password for a non-local user.");
}
if (count($input_errors) == 0) {
local_user_set_password($config['system']['user'][$userindex[$username]], $_POST['passwordfld1']);
local_user_set($config['system']['user'][$userindex[$username]]);
$config['system']['user'][$userindex[$username]]['language'] = $pconfig['language'];
if ($pconfig['passwordfld1'] !== '' || $pconfig['passwordfld2'] !== '') {
local_user_set_password($config['system']['user'][$userindex[$username]], $pconfig['passwordfld1']);
local_user_set($config['system']['user'][$userindex[$username]]);
}
write_config();
$savemsg = gettext("Password successfully changed") . "<br />";
header(url_safe('Location: /system_usermanager_passwordmg.php?savemsg=%s', sprintf(gettext('Saved settings for user "%s"'), $username)));
exit;
}
}
legacy_html_escape_form_data($pconfig);
include("head.inc");
?>
@ -89,23 +110,49 @@ include("head.inc");
<div class="table-responsive">
<table class="table table-striped">
<tr>
<td><?=gettext("Old password"); ?></td>
<td width="22%"><strong><?= gettext('User Settings') ?></strong></td>
<td width="78%" align="right">
<small><?=gettext("full help"); ?> </small>
<i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i>
</td>
</tr>
<tr>
<td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Old password"); ?></td>
<td>
<input name="passwordfld0" type="password" id="passwordfld0" size="20" />
</td>
</tr>
<tr>
<td><?=gettext("New password"); ?></td>
<td><i class="fa fa-info-circle text-muted"></i> <?=gettext("New password"); ?></td>
<td>
<input name="passwordfld1" type="password" id="passwordfld1" size="20" />
</td>
</tr>
<tr>
<td><?=gettext("Confirmation");?></td>
<td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Confirmation");?></td>
<td>
<input name="passwordfld2" type="password" id="passwordfld2" size="20" />
</td>
</tr>
<tr>
<td><a id="help_for_language" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Language");?></td>
<td>
<select name="language" class="selectpicker" data-size="10" data-style="btn-default" data-width="auto">
<?php
foreach (get_locale_list() as $lcode => $ldesc):?>
<option value="<?=$lcode;?>" <?=$lcode == $pconfig['language'] ? "selected=\"selected\"" : "";?>>
<?=$ldesc;?>
</option>
<?php
endforeach;?>
</select>
<div class="hidden" for="help_for_language">
<strong>
<?= gettext('Choose a language for the web GUI.') ?>
</strong>
</div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>