use preferred browser language if the box is not initialized yet

improved error handling of cgi
This commit is contained in:
lars 2005-10-25 10:23:22 +00:00
parent 2d0dd97a31
commit d4dbcba4f3
1 changed files with 56 additions and 4 deletions

View File

@ -12,12 +12,15 @@ use CGI;
use ClearSilver;
use ConfigFile;
my $CONFIG_FILE = '/etc/cryptobox/cryptobox.conf';
my ($pagedata, $pagename);
my ($LANGUAGE_DIR, $DEFAULT_LANGUAGE, $HTML_TEMPLATE_DIR, $DOC_DIR);
my ($CB_SCRIPT, $LOG_FILE, $IS_DEV);
my $config = ConfigFile::read_config_file('/etc/cryptobox/cryptobox.conf');
&fatal_error ("could not find configuration file ($CONFIG_FILE)") unless (-e $CONFIG_FILE);
my $config = ConfigFile::read_config_file($CONFIG_FILE);
$CB_SCRIPT = $config->{CB_SCRIPT};
$LOG_FILE = $config->{LOG_FILE};
@ -32,15 +35,29 @@ my $query = new CGI;
#################### subs ######################
# for fatal errors without the chance of clearsilver-rendering
sub fatal_error()
{
my $message = shift;
print "Content-Type: text/html\n\n";
print "<html><head><title>CryptoBox</title></head>\n";
print "<body>\n";
print '<h1 align="center">' . $message . "</h1>\n";
print "</body></html>\n";
die "[CryptoBox]: $message";
}
sub load_hdf
{
my $hdf = ClearSilver::HDF->new();
my $fname = "$HTML_TEMPLATE_DIR/main.cs";
die ("Template directory is invalid ($fname not found)!") unless (-e "$fname");
&fatal_error ("Template directory is invalid ($fname not found)!") unless (-e "$fname");
$hdf->setValue("Settings.TemplateDir","$HTML_TEMPLATE_DIR");
die ("Documentation directory ($DOC_DIR) not found!") unless (-d "$DOC_DIR");
&fatal_error ("Documentation directory ($DOC_DIR) not found!") unless (-d "$DOC_DIR");
$hdf->setValue("Settings.DocDir","$DOC_DIR");
# if it was requested as directory index (link from index.html), we should
@ -69,6 +86,14 @@ sub load_selected_language
$config_language = `$CB_SCRIPT get_config language`;
$config_language = $DEFAULT_LANGUAGE unless (&validate_language("$config_language"));
# check for preferred browser language, if the box was not initialized yet
if ( ! &check_config())
{
my $prefLang = &get_browser_language();
# take it, if a supported browser language was found
$config_language = $prefLang unless ($prefLang eq '');
}
######### temporary language setting? ############
# the default language can be overriden by the language links in the
# upper right of the page
@ -80,6 +105,7 @@ sub load_selected_language
# add the setting to every link
$data->setValue('Data.PostData.weblang', "$weblang");
} else {
# no valid language was selected - so you may ignore it
$data->setValue('Data.Warning', 'InvalidLanguage');
}
}
@ -106,7 +132,7 @@ sub get_available_languages
my $data = shift;
my ($file, @files, $hdf, $lang_name);
opendir(DIR, $LANGUAGE_DIR) or die ("Language directory ($LANGUAGE_DIR) not accessible!");
opendir(DIR, $LANGUAGE_DIR) or &fatal_error ("Language directory ($LANGUAGE_DIR) not accessible!");
@files = sort grep { /.*\.hdf$/ } readdir(DIR);
close(DIR);
@ -120,6 +146,32 @@ sub get_available_languages
}
# look for preferred browser language setting
# this code was adapted from Per Cederberg - http://www.percederberg.net/home/perl/select.perl
# it returns an empty string, if no supported language was found
sub get_browser_language
{
my ($str, @langs, @res);
# Use language preference settings
if ($ENV{'HTTP_ACCEPT_LANGUAGE'} ne '')
{
@langs = split(/,/, $ENV{'HTTP_ACCEPT_LANGUAGE'});
foreach (@langs)
{
# get the first part of the language setting
($str) = ($_ =~ m/([a-z]+)/);
# check, if it supported by the cryptobox
$res[$#res+1] = $str if validate_language($str);
}
}
# if everything fails - return empty string
$res[0] = "" if ($#res lt 0);
return $res[0];
}
sub log_msg
{
my $text = shift;