use the browsers language preferences instead of the settings page

This commit is contained in:
SJ 2012-09-21 20:13:41 +02:00
parent 3e0ad6da34
commit d68ffad8a6
5 changed files with 50 additions and 22 deletions

View File

@ -1,6 +1,6 @@
<?php
define('LANG', 'en');
define('DEFAULT_LANG', 'en');
define('THEME', 'default');
define('SITE_NAME', 'demo.mailpiler.org');

View File

@ -19,10 +19,10 @@ class ControllerUserSettings extends Controller {
$this->document->title = $this->data['text_settings'];
if(isset($this->request->post['pagelen']) && isset($this->request->post['lang']) && isset($this->request->post['theme'])) {
if(isset($this->request->post['pagelen']) && isset($this->request->post['theme'])) {
$this->model_user_prefs->set_user_preferences(Registry::get('username'), $this->request->post);
AUDIT(ACTION_CHANGE_USER_SETTINGS, '', '', '', 'lang:' . $this->request->post['lang'] . ', pagelen:' . $this->request->post['pagelen'] . ', theme:' . $this->request->post['theme']);
AUDIT(ACTION_CHANGE_USER_SETTINGS, '', '', '', 'pagelen:' . $this->request->post['pagelen'] . ', theme:' . $this->request->post['theme']);
Header("Location: settings.php");
return;

View File

@ -8,7 +8,6 @@ class ModelUserPrefs extends Model {
$query = $this->db->query("SELECT * FROM " . TABLE_USER_SETTINGS . " WHERE username=?", array($username));
if(isset($query->row['pagelen'])) { $_SESSION['pagelen'] = $query->row['pagelen']; } else { $_SESSION['pagelen'] = PAGE_LEN; }
if(isset($query->row['lang'])) { $_SESSION['lang'] = $query->row['lang']; } else { $_SESSION['lang'] = LANG; }
if(isset($query->row['theme'])) { $_SESSION['theme'] = $query->row['theme']; } else { $_SESSION['theme'] = THEME; }
return 1;
@ -18,21 +17,19 @@ class ModelUserPrefs extends Model {
public function set_user_preferences($username = '', $prefs = array() ) {
if(!isset($prefs['pagelen']) || !is_numeric($prefs['pagelen']) || $prefs['pagelen'] < 10 || $prefs['pagelen'] > 100
|| !isset($prefs['lang']) || strlen($prefs['lang']) != 2 || !file_exists(DIR_LANGUAGE . $prefs['lang'])
|| !isset($prefs['theme']) || !preg_match("/^([a-zA-Z0-9\-\_]+)$/", $prefs['theme']) || !file_exists(DIR_THEME . $prefs['theme']) ) { return 1; }
$query = $this->db->query("SELECT COUNT(*) AS num FROM " . TABLE_USER_SETTINGS . " WHERE username=?", array($username));
if((int)@$query->row['num'] == 1) {
$query = $this->db->query("UPDATE " . TABLE_USER_SETTINGS . " SET lang=?, pagelen=?, theme=? WHERE username=?", array($prefs['lang'], (int)@$prefs['pagelen'], $prefs['theme'], $username));
$query = $this->db->query("UPDATE " . TABLE_USER_SETTINGS . " SET pagelen=?, theme=? WHERE username=?", array((int)@$prefs['pagelen'], $prefs['theme'], $username));
}
else {
$query = $this->db->query("INSERT INTO " . TABLE_USER_SETTINGS . " (username, pagelen, lang, theme) VALUES(?,?,?,?)", array($username, (int)@$prefs['pagelen'], $prefs['lang'], $prefs['theme']));
$query = $this->db->query("INSERT INTO " . TABLE_USER_SETTINGS . " (username, pagelen, theme) VALUES(?,?,?)", array($username, (int)@$prefs['pagelen'], $prefs['theme']));
}
$_SESSION['pagelen'] = $prefs['pagelen'];
$_SESSION['lang'] = $prefs['lang'];
$_SESSION['theme'] = $prefs['theme'];
LOGGER("set user preference", $username);

View File

@ -5,13 +5,32 @@ class Language {
public function __construct () {
global $langs;
$lang = '';
if(isset($_SESSION['lang'])) {
$file = DIR_LANGUAGE . $_SESSION['lang'] . '/messages.php';
} else {
$file = DIR_LANGUAGE . LANG . '/messages.php';
}
else {
$pref_langs = $this->get_preferred_languages();
while(list($k, $v) = each($pref_langs)) {
if(in_array($v, $langs)) {
$lang = $v;
define('LANG', $lang);
break;
}
}
if($lang == '') { $lang = DEFAULT_LANG; }
$_SESSION['lang'] = $lang;
$file = DIR_LANGUAGE . $lang . '/messages.php';
}
if (file_exists($file)) {
if(file_exists($file)) {
$_ = array();
require($file);
@ -25,6 +44,29 @@ class Language {
}
private function get_preferred_languages() {
$langs = array();
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$l = explode(";", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
while(list($k, $v) = each($l)) {
$a = explode(",", $v);
if(isset($a[0]) && substr($a[0], 0, 2) != 'q=') {
array_push($langs, $a[0]);
}
if(isset($a[1])) { array_push($langs, $a[1]); }
}
}
array_push($langs, DEFAULT_LANG);
return $langs;
}
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}

View File

@ -19,17 +19,6 @@
</div>
</div>
<div class="row">
<div class="cell1"><?php print $text_language; ?>:</div>
<div class="cell2">
<select name="lang">
<?php foreach(Registry::get('langs') as $t) { ?>
<option value="<?php print $t; ?>"<?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == $t) { ?> selected="selected"<?php } ?>><?php print $t; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="cell1"><?php print $text_theme; ?>:</div>
<div class="cell2">