From d4dbcba4f3698d7bcc014542ecde0f85052aa2b8 Mon Sep 17 00:00:00 2001 From: lars Date: Tue, 25 Oct 2005 10:23:22 +0000 Subject: [PATCH] use preferred browser language if the box is not initialized yet improved error handling of cgi --- cbox-tree.d/var/www/cgi-bin/cryptobox.pl | 60 ++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/cbox-tree.d/var/www/cgi-bin/cryptobox.pl b/cbox-tree.d/var/www/cgi-bin/cryptobox.pl index 5fe9489..6e306f3 100755 --- a/cbox-tree.d/var/www/cgi-bin/cryptobox.pl +++ b/cbox-tree.d/var/www/cgi-bin/cryptobox.pl @@ -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 "CryptoBox\n"; + print "\n"; + print '

' . $message . "

\n"; + print "\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;