From 0d79adae1190187e6738efa8115dd3214f9f794a Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 24 Apr 2005 12:13:08 +0000 Subject: [PATCH] Implemented checks for certain fields (telephone numbers and uris). --- book.cgi | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 29 deletions(-) diff --git a/book.cgi b/book.cgi index 4805dda..cf4653e 100755 --- a/book.cgi +++ b/book.cgi @@ -100,11 +100,19 @@ sub action_browse { my $group = param ('group'); $group = shift if (@_); - $group ||= '*'; + $group ||= ''; - my @all = Person->search ([[group => $group]]); + my @all; + if ($group) + { + @all = Person->search ([[group => $group]]); + } + else + { + @all = Person->search (); + } - if ($group eq '*') + if (!$group) { my %groups = (); for (@all) @@ -131,17 +139,17 @@ sub action_browse print qq(\t\t\n\n); } - if ($group eq '*') + if ($group) { - print qq(\t\t

All Contacts

\n); + print qq(\t\t

Contact Group "$group"

\n); } else { - print qq(\t\t

Contact Group "$group"

\n); + print qq(\t\t

All Contacts

\n); } print qq(\t\t\n\n); + + print qq(\t\t\n) if ($group); } sub action_detail @@ -198,6 +208,23 @@ EOF for (my $i = 0; $i < $num; $i++) { my $val = $values->[$i]; + + if ($field eq 'group') + { + my $val_esc = uri_escape ($val); + $val = qq($val); + } + elsif ($field eq 'uri') + { + my $uri = $val; + $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#); + $val = qq($val); + } + elsif ($field eq 'mail') + { + $val = qq($val); + } + print "\t\t\n" if ($i); print "\t\t\t$val\n", "\t\t\n"; @@ -308,12 +335,7 @@ sub action_edit $lastname = param ('lastname') if (param ('lastname') and $UserID); $firstname = param ('firstname') if (param ('firstname') and $UserID); - for (@MultiFields) - { - my $field = $_; - my @values = grep { $_ } (param ($field)); - $contacts->{$field} = [@values] if (@values); - } + get_contacts ($contacts); $lastname = $opts{'lastname'} if (defined ($opts{'lastname'})); $firstname = $opts{'firstname'} if (defined ($opts{'firstname'})); @@ -409,6 +431,12 @@ sub action_save { my $cn = $UserID ? param ('cn') : $UserCN; + if (verify_fields ()) + { + action_edit (cn => $cn); + return; + } + if ($cn) { action_update (); @@ -436,13 +464,7 @@ sub action_save my $lastname = param ('lastname'); my $firstname = param ('firstname'); - my $contacts = {}; - for (@MultiFields) - { - my $field = $_; - my @values = grep { $_ } (param ($field)); - $contacts->{$field} = [@values] if (@values); - } + my $contacts = get_contacts (); my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts); @@ -491,13 +513,7 @@ sub action_update $cn = $person->name (); } - my $contacts = {}; - for (@MultiFields) - { - my $field = $_; - my @values = grep { $_ } (param ($field)); - $contacts->{$field} = [@values] if (@values); - } + my $contacts = get_contacts (); for (@MultiFields) { @@ -722,7 +738,7 @@ div.foot color: gray; background-color: white; - position: absolute; + position: fixed; top: auto; right: 0px; bottom: 0px; @@ -777,7 +793,12 @@ th.menu text-align: right; } -th.menu a +.menu +{ + font-weight: bold; +} + +.menu a { color: blue; background-color: transparent; @@ -894,3 +915,69 @@ sub pwgen return ($retval); } + +sub verify_fields +{ + my @errors = (); + for (param ('uri')) + { + my $val = $_; + next unless ($val); + + if ($val !~ m#^[a-zA-Z]+://#) + { + push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.'); + last; + } + } + + for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax')) + { + my $number = $_; + next unless ($number); + + if ($number !~ m/^\+/) + { + push (@errors, 'Telephone numbers have to begin with the country code, e.g. "+49 911 123456"'); + last; + } + } + + print qq(\t\t
\n) if (@errors); + for (my $i = 0; $i < scalar (@errors); $i++) + { + my $e = $errors[$i]; + + print "
\n" if ($i); + print "\t\t\t$e"; + } + print qq(\n\t\t
\n\n) if (@errors); + + return (scalar (@errors)); +} + +sub get_contacts +{ + my $contacts = @_ ? shift : {}; + + for (@MultiFields) + { + my $field = $_; + my @values = grep { $_ } (param ($field)); + + next unless (@values); + + if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax') + { + for (@values) + { + $_ =~ s/\D//g; + $_ = '+' . $_; + } + } + + $contacts->{$field} = [@values] if (@values); + } + + return ($contacts); +} -- 2.11.0