Renamed book.cgi to licom.cgi
authorocto <octo>
Sun, 24 Apr 2005 15:54:23 +0000 (15:54 +0000)
committerocto <octo>
Sun, 24 Apr 2005 15:54:23 +0000 (15:54 +0000)
book.cgi [deleted file]
licom.cgi [new file with mode: 0755]

diff --git a/book.cgi b/book.cgi
deleted file mode 100755 (executable)
index 4af14ad..0000000
--- a/book.cgi
+++ /dev/null
@@ -1,1160 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-use lib (qw(lib));
-
-use CGI (':cgi');
-use CGI::Carp (qw(fatalsToBrowser));
-use URI::Escape;
-use Data::Dumper;
-
-use Person;
-
-our $Debug = 0;
-our %Config = ();
-
-our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
-
-our %FieldNames = 
-(
-       address         => 'Address',
-       homephone       => 'Home Phone',
-       cellphone       => 'Cell Phone',
-       officephone     => 'Office Phone',
-       fax             => 'FAX',
-       mail            => 'E-Mail',
-       uri             => 'URI (Homepage)',
-       group           => 'Group'
-);
-
-our $MySelf = $ENV{'SCRIPT_NAME'};
-
-our $Action = param ('action');
-$Action ||= 'default';
-
-our %Actions =
-(
-       browse  => [\&html_start, \&action_browse,  \&html_end],
-       default => [\&html_start, \&action_browse,  \&html_end],
-       detail  => [\&html_start, \&action_detail,  \&html_end],
-       edit    => [\&html_start, \&action_edit,    \&html_end],
-       list    => [\&html_start, \&action_list,    \&html_end],
-       save    => [\&html_start, \&action_save,    \&html_end],
-       search  => [\&html_start, \&action_search,  \&html_end],
-       verify  => [\&html_start, \&action_verify,  \&html_end],
-       vcard   => \&action_vcard
-);
-
-read_config ();
-
-# make sure AuthLDAPRemoteUserIsDN is enabled.
-die unless ($ENV{'REMOTE_USER'});
-$Config{'base_dn'} = $ENV{'REMOTE_USER'};
-
-Person->connect
-(
-       uri     => $Config{'uri'},
-       base_dn => $Config{'base_dn'},
-       bind_dn => $Config{'bind_dn'},
-       password => $Config{'password'}
-) or die;
-
-our ($UserCN, $UserID) = Person->get_user ($Config{'base_dn'});
-
-if (!$UserID and $Action ne 'save')
-{
-       $Action = 'edit';
-}
-
-if (!$UserCN)
-{
-       die;
-}
-
-if (!defined ($Actions{$Action}))
-{
-       die;
-}
-
-if (ref ($Actions{$Action}) eq 'CODE')
-{
-       $Actions{$Action}->();
-}
-elsif (ref ($Actions{$Action}) eq 'ARRAY')
-{
-       for (@{$Actions{$Action}})
-       {
-               $_->();
-       }
-}
-
-#print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
-
-Person->disconnect ();
-
-exit (0);
-
-###
-
-sub action_browse
-{
-       my $group = param ('group');
-       $group = shift if (@_);
-       $group ||= '';
-
-       my @all;
-       if ($group)
-       {
-               @all = Person->search ([[group => $group]]);
-       }
-       else
-       {
-               @all = Person->search ();
-       }
-
-       if (!$group)
-       {
-               my %groups = ();
-               for (@all)
-               {
-                       my $person = $_;
-                       my @g = $person->get ('group');
-
-                       $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
-               }
-
-               print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
-               for (sort (keys (%groups)))
-               {
-                       my $group = $_;
-                       my $group_esc = uri_escape ($group);
-                       my $num = $groups{$group};
-
-                       print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
-               }
-               if (!%groups)
-               {
-                       print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
-               }
-               print qq(\t\t</ul>\n\n);
-       }
-
-       if ($group)
-       {
-               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
-       }
-       else
-       {
-               print qq(\t\t<h2>All Contacts</h2>\n);
-       }
-
-       print qq(\t\t<ul class="results">\n);
-       for (sort { $a->name () cmp $b->name () } (@all))
-       {
-               my $person = $_;
-               my $cn = $person->name ();
-               my $cn_esc = uri_escape ($cn);
-
-               print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
-       }
-       print qq(\t\t</ul>\n\n);
-
-       print qq(\t\t<div class="menu">\n);
-       if ($group)
-       {
-               my $group_esc = uri_escape ($group);
-               print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
-               qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
-       }
-       else
-       {
-               print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
-       }
-       print qq(\t\t</div>\n);
-}
-
-sub action_list
-{
-       my $group = param ('group');
-       $group = shift if (@_);
-       $group ||= '';
-
-       my $title = $group ? "List of group &quot;$group&quot;" : 'List of all addresses';
-       my @fields = (qw(address homephone cellphone officephone fax mail));
-
-       my @all = ();
-       if ($group)
-       {
-               @all = Person->search ([[group => $group]]);
-       }
-       else
-       {
-               @all = Person->search ();
-       }
-
-       print <<EOF;
-               <h2>$title</h2>
-
-               <table class="list">
-                       <tr>
-                               <th>Name</th>
-EOF
-       for (@fields)
-       {
-               print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
-       }
-       print "\t\t\t</tr>\n";
-
-       for (sort { $a->name () cmp $b->name () } (@all))
-       {
-               my $person = $_;
-               my $sn = $person->lastname ();
-               my $gn = $person->firstname ();
-
-               print "\t\t\t<tr>\n",
-               "\t\t\t\t<td>$sn, $gn</td>\n";
-
-               for (@fields)
-               {
-                       my $field = $_;
-                       my @values = $person->get ($field);
-                       print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
-               }
-
-               print "\t\t\t</tr>\n";
-       }
-       print "\t\t</table>\n\n";
-
-       if ($group)
-       {
-               my $group_esc = uri_escape ($group);
-               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
-       }
-       else
-       {
-               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
-       }
-}
-
-sub action_detail
-{
-       my $cn = param ('cn');
-       $cn = shift if (@_);
-       die unless ($cn);
-
-       my $person = Person->load ($cn);
-       if (!$person)
-       {
-               print qq(\t<div>Entry &quot;$cn&quot; could not be loaded from DB.</div>\n);
-               return;
-       }
-
-       print qq(\t\t<h2>Details for $cn</h2>\n);
-
-       my $cn_esc = uri_escape ($cn);
-
-       print <<EOF;
-               <table class="detail">
-                       <tr>
-                               <th>Name</th>
-                               <td>$cn</td>
-                       </tr>
-EOF
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my $values = $person->get ($field);
-               my $num = scalar (@$values);
-               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
-
-               next unless ($num);
-
-               print "\t\t\t<tr>\n";
-               if ($num > 1)
-               {
-                       print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
-               }
-               else
-               {
-                       print qq(\t\t\t\t<th>$print</th>\n);
-               }
-
-               for (my $i = 0; $i < $num; $i++)
-               {
-                       my $val = $values->[$i];
-
-                       if ($field eq 'group')
-                       {
-                               my $val_esc = uri_escape ($val);
-                               $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
-                       }
-                       elsif ($field eq 'uri')
-                       {
-                               my $uri = $val;
-                               $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
-                               $val = qq(<a href="$uri" class="extern">$val</a>);
-                       }
-                       elsif ($field eq 'mail')
-                       {
-                               $val = qq(<a href="mailto:$val" class="mail">$val</a>);
-                       }
-                       
-                       print "\t\t\t<tr>\n" if ($i);
-                       print "\t\t\t\t<td>$val</td>\n",
-                       "\t\t\t</tr>\n";
-               }
-       }
-       print <<EOF;
-               </table>
-
-               <div class="menu">
-                       [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
-                       [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
-                       [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
-               </div>
-
-EOF
-}
-
-sub action_search
-{
-       my $search = param ('search');
-
-       $search ||= '';
-       $search =~ s/[^\s\w]//g;
-
-       if (!$search)
-       {
-               print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
-               action_default ();
-               return;
-       }
-
-       my @patterns = split (m/\s+/, $search);
-       my @filter = ();
-
-       for (@patterns)
-       {
-               my $pattern = "$_*";
-               push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
-       }
-
-       my @matches = Person->search (@filter);
-
-       if (!@matches)
-       {
-               print qq(\t<div>No entries matched your search.</div>\n);
-               return;
-       }
-
-       if (scalar (@matches) == 1)
-       {
-               my $person = shift (@matches);
-               my $cn = $person->name ();
-               action_detail ($cn);
-               return;
-       }
-
-       print qq(\t<ul class="result">\n);
-       for (@matches)
-       {
-               my $person = $_;
-               my $cn = $person->name ();
-               my $cn_esc = uri_escape ($cn);
-
-               print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
-       }
-       print qq(\t</ul>\n);
-}
-
-sub action_edit
-{
-       my %opts = @_;
-
-       my $cn = param ('cn');
-
-       $cn = $opts{'cn'} if (defined ($opts{'cn'}));
-       $cn ||= '';
-
-       if (!$UserID)
-       {
-               $cn = $UserCN;
-       }
-
-       my $person;
-
-       my $lastname;
-       my $firstname;
-
-       my $contacts = {};
-       $contacts->{$_} = [] for (@MultiFields);
-
-       if ($cn)
-       {
-               $person = Person->load ($cn);
-
-               if (!$person)
-               {
-                       print qq(\t<div class="error">Unable to load CN &quot;$cn&quot;. Sorry.</div>\n);
-                       return;
-               }
-       
-               $lastname    = $person->lastname ();
-               $firstname   = $person->firstname ();
-
-               for (@MultiFields)
-               {
-                       $contacts->{$_} = $person->get ($_);
-               }
-       }
-
-       $lastname    = param ('lastname')    if (param ('lastname')  and $UserID);
-       $firstname   = param ('firstname')   if (param ('firstname') and $UserID);
-
-       get_contacts ($contacts);
-       
-       $lastname    =   $opts{'lastname'}     if (defined ($opts{'lastname'}));
-       $firstname   =   $opts{'firstname'}    if (defined ($opts{'firstname'}));
-       for (@MultiFields)
-       {
-               my $field = $_;
-               @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
-       }
-
-       if ($cn)
-       {
-               print "\t\t<h2>Edit contact $cn</h2>\n";
-       }
-       else
-       {
-               print "\t\t<h2>Create new contact</h2>\n";
-       }
-
-       print <<EOF;
-               <form action="$MySelf" method="post">
-               <input type="hidden" name="action" value="save" />
-               <input type="hidden" name="cn" value="$cn" />
-               <table class="edit">
-                       <tr>
-                               <th>Lastname</th>
-EOF
-       if ($UserID)
-       {
-               print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
-       }
-       else
-       {
-               print qq(\t\t\t\t<td>$lastname</td>\n);
-       }
-       print <<EOF;
-                       </tr>
-                       <tr>
-                               <th>Firstname</th>
-EOF
-       if ($UserID)
-       {
-               print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
-       }
-       else
-       {
-               print qq(\t\t\t\t<td>$firstname</td>\n);
-       }
-       
-       print "\t\t\t</tr>\n";
-
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
-               my @values = @{$contacts->{$field}};
-
-               push (@values, '');
-               
-               for (@values)
-               {
-                       my $value = $_;
-
-                       print <<EOF;
-                       <tr>
-                               <th>$print</th>
-                               <td><input type="text" name="$field" value="$value" /></td>
-                       </tr>
-EOF
-               }
-       }
-
-       print <<EOF;
-                       <tr>
-                               <th colspan="2" class="menu">
-EOF
-       if ($UserID)
-       {
-               print <<EOF;
-                                       <input type="submit" name="button" value="Cancel" />
-                                       <input type="submit" name="button" value="Apply" />
-EOF
-       }
-       print <<EOF;
-                                       <input type="submit" name="button" value="Save" />
-                               </th>
-                       </tr>
-               </table>
-               </form>
-EOF
-}
-
-sub action_save
-{
-       my $cn = $UserID ? param ('cn') : $UserCN;
-
-       if (verify_fields ())
-       {
-               action_edit (cn => $cn);
-               return;
-       }
-
-       if ($cn)
-       {
-               action_update ();
-               return;
-       }
-
-       die unless ($UserID);
-
-       my $button = lc (param ('button'));
-       $button ||= 'save';
-
-       if ($button eq 'cancel')
-       {
-               action_browse ();
-               return;
-       }
-
-       if (!param ('lastname') or !param ('firstname'))
-       {
-               print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
-               action_edit (cn => '');
-               return;
-       }
-
-       my $lastname  = param ('lastname');
-       my $firstname = param ('firstname');
-
-       my $contacts = get_contacts ();
-
-       my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
-
-       if (!$person)
-       {
-               print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
-               return;
-       }
-       
-       $cn = $person->name ();
-
-       if ($button eq 'apply')
-       {
-               action_edit (cn => $cn);
-       }
-       else
-       {
-               action_detail ($cn);
-       }
-}
-
-sub action_update
-{
-       my $cn = $UserID ? param ('cn') : $UserCN;
-       my $person = Person->load ($cn);
-
-       die unless ($person);
-
-       my $button = lc (param ('button'));
-       $button ||= 'save';
-
-       if ($UserID and $button eq 'cancel')
-       {
-               action_detail ($cn);
-               return;
-       }
-
-       if ($UserID)
-       {
-               my $lastname  = param ('lastname');
-               my $firstname = param ('firstname');
-
-               $person->lastname  ($lastname)  if ($lastname  and $lastname  ne $person->lastname ());
-               $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
-
-               $cn = $person->name ();
-       }
-
-       my $contacts = get_contacts ();
-
-       for (@MultiFields)
-       {
-               my $field = $_;
-               
-               if (defined ($contacts->{$field}))
-               {
-                       my $values = $contacts->{$field};
-                       $person->set ($field, $values);
-               }
-               else
-               {
-                       $person->set ($field, []);
-               }
-       }
-
-       if ($button eq 'apply' or !$UserID)
-       {
-               action_edit (cn => $cn);
-       }
-       else
-       {
-               action_detail ($cn);
-       }
-}
-
-sub action_vcard
-{
-       my $cn = param ('cn');
-       $cn = shift if (@_);
-       die unless ($cn);
-
-       my $person = Person->load ($cn);
-       die unless ($person);
-
-       my %vcard_types =
-       (
-               homephone       => 'TEL;TYPE=home,voice',
-               cellphone       => 'TEL;TYPE=cell',
-               officephone     => 'TEL;TYPE=work,voice',
-               fax             => 'TEL;TYPE=fax',
-               mail            => 'EMAIL',
-               uri             => 'URL',
-               group           => 'ORG'
-       );
-
-       my $sn = $person->lastname ();
-       my $gn = $person->firstname ();
-       my $cn_esc = uri_escape ($cn);
-
-       print <<EOF;
-Content-Type: text/x-vcard
-Content-Disposition: attachment; filename="$cn.vcf"
-
-BEGIN:VCARD
-VERSION:3.0
-FN: $cn
-N: $sn;$gn
-EOF
-
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my $vc_fld = $vcard_types{$field};
-               my $values = $person->get ($field);
-
-               for (@$values)
-               {
-                       my $value = $_;
-                       print "$vc_fld:$value\n";
-               }
-       }
-       print "END:VCARD\n";
-}
-
-sub action_verify
-{
-       my $cn = param ('cn');
-       $cn = shift if (@_);
-       die unless ($cn);
-
-       my $person = Person->load ($cn);
-       die unless ($person);
-
-       my ($mail) = $person->get ('mail');
-       $mail ||= '';
-
-       my $message;
-       my $password = $person->password ();
-
-       if (!$password)
-       {
-               $password = pwgen ();
-               $person->password ($password);
-       }
-
-       $message = qq(The password for the record &quot;$cn&quot; is &quot;$password&quot;.);
-
-       if ($mail)
-       {
-               if (action_verify_send_mail ($person))
-               {
-                       $message .= qq( A request for verification has been sent to $mail.);
-               }
-       }
-       else
-       {
-               $message .= q( There was no e-mail address, thus no verification request could be sent.);
-       }
-
-       print qq(\t\t<div class="message">$message</div>\n);
-
-       action_detail ($cn);
-}
-
-sub action_verify_send_mail
-{
-       my $person = shift;
-       my $owner = Person->load ($UserCN);
-       my $smh;
-
-       my ($owner_mail) = $owner->get ('mail');
-       if (!$owner_mail)
-       {
-               my $cn = uri_escape ($UserCN);
-               print qq(\t\t<div class="error">You have no email set in your own profile. <a href="$MySelf?action=edit&cn=$cn">Edit it now</a>!</div>\n);
-               return (0);
-       }
-
-       my $max_width = 0;
-       for (keys %FieldNames)
-       {
-               $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
-       }
-       $max_width++;
-
-       my $person_name = $person->name ();
-       my ($person_mail) = $person->get ('mail');
-       my $person_gn = $person->firstname ();
-       my $password = $person->password ();
-
-       my $host = $ENV{'HTTP_HOST'};
-       my $url = 'http://' . $host . $MySelf;
-       
-       open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
-       print $smh <<EOM;
-To: $person_name <$person_mail>
-From: $UserCN <$owner_mail>
-Subject: Please verify our entry in my address book
-
-Hello $person_gn,
-
-the following is your entry in my address book:
-EOM
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
-               my @values = $person->get ($field);
-
-               for (@values)
-               {
-                       printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
-               }
-       }
-       print $smh <<EOM;
-
-If this entry is outdated or incomplete, please take a minute and correct it.
-  Address:  $url
- Username: $person_name
- Password: $password
-
-Thank you very much :) Regards,
-$UserCN
-EOM
-       close ($smh);
-
-       return (1);
-}
-
-sub html_start
-{
-       my $title = shift;
-       $title = q(Lightweight Contact Manager) unless ($title);
-
-       print <<EOF;
-Content-Type: text/html; charset=UTF-8
-
-<html>
-       <head>
-               <title>$title</title>
-               <style type="text/css">
-               <!--
-               \@media screen
-               {
-                       a
-                       {
-                               color: blue;
-                               background-color: inherit;
-                               text-decoration: none;
-                       }
-
-                       a:hover
-                       {
-                               text-decoration: underline;
-                       }
-
-                       a:visited
-                       {
-                               color: navy;
-                               background-color: inherit;
-                       }
-
-                       body
-                       {
-                               color: black;
-                               background-color: white;
-                       }
-
-                       div.error
-                       {
-                               color: red;
-                               background-color: yellow;
-
-                               font-weight: bold;
-                               padding: 1ex;
-                               border: 2px solid red;
-                       }
-
-                       div.foot
-                       {
-                               color: gray;
-                               background-color: white;
-
-                               position: fixed;
-                               top: auto;
-                               right: 0px;
-                               bottom: 0px;
-                               left: 0px;
-
-                               font-size: x-small;
-                               text-align: right;
-                               border-top: 1px solid black;
-                               width: 100%;
-                       }
-
-                       div.foot a
-                       {
-                               color: black;
-                               background-color: inherit;
-                               text-decoration: none;
-                       }
-
-                       div.foot a:hover
-                       {
-                               text-decoration: underline;
-                       }
-
-                       div.menu
-                       {
-                               border-top: 1px solid black;
-                               margin-top: 1ex;
-                               font-weight: bold;
-                       }
-
-                       div.menu a
-                       {
-                               color: blue;
-                               background-color: transparent;
-                       }
-
-                       div.topmenu
-                       {
-                               margin-bottom: 1ex;
-                               padding-bottom: 1ex;
-                               border-bottom: 1px solid black;
-                       }
-
-                       div.topmenu form
-                       {
-                               display: inline;
-                               margin-right: 5ex;
-                       }
-
-                       h1
-                       {
-                               position: absolute;
-                               top: 1ex;
-                               right: 1ex;
-                               bottom: auto;
-                               left: auto;
-
-                               font-size: 100%;
-                               font-weight: bold;
-                       }
-
-                       img
-                       {
-                               border: none;
-                       }
-
-                       table.list
-                       {
-                               width: 100%;
-                       }
-
-                       table.list td
-                       {
-                               empty-cells: show;
-                       }
-
-                       td
-                       {
-                               color: black;
-                               background-color: #cccccc;
-                               vertical-align: top;
-                       }
-
-                       th
-                       {
-                               color: black;
-                               background-color: #999999;
-                               padding: 0.3ex;
-                               text-align: left;
-                               vertical-align: top;
-                       }
-               }
-
-               \@media print
-               {
-                       a
-                       {
-                               color: inherit;
-                               background-color: inherit;
-                               text-decoration: underline;
-                       }
-                       
-                       div.topmenu, div.menu
-                       {
-                               display: none;
-                       }
-
-                       div.foot
-                       {
-                               font-size: 50%;
-                               text-align: right;
-                       }
-
-                       h1
-                       {
-                               display: none;
-                       }
-
-                       h2
-                       {
-                               font-size: 100%;
-                       }
-
-                       table
-                       {
-                               border-collapse: collapse;
-                       }
-
-                       table.list
-                       {
-                               width: 100%;
-                       }
-
-                       table.list td
-                       {
-                               empty-cells: show;
-                       }
-
-                       table.list th
-                       {
-                               border-bottom-width: 2px;
-                       }
-
-                       td, th
-                       {
-                               border: 1px solid black;
-                               vertical-align: top;
-                       }
-
-                       th
-                       {
-                               font-weight: bold;
-                               text-align: center;
-                       }
-               }
-               //-->
-               </style>
-       </head>
-
-       <body>
-EOF
-       if ($UserID)
-       {
-               my $search = param ('search') || '';
-               print <<EOF;
-               <div class="topmenu">
-                       <form action="$MySelf" method="post">
-                               <input type="hidden" name="action" value="browse" />
-                               <input type="submit" name="button" value="Browse" />
-                       </form>
-                       <form action="$MySelf" method="post">
-                               <input type="hidden" name="action" value="search" />
-                               <input type="text" name="search" value="$search" />
-                               <input type="submit" name="button" value="Search" />
-                       </form>
-                       <form action="$MySelf" method="post">
-                               <input type="hidden" name="action" value="edit" />
-                               <input type="hidden" name="dn" value="" />
-                               <input type="submit" name="button" value="Add New" />
-                       </form>
-               </div>
-EOF
-       }
-       print "\t\t<h1>$title</h1>\n";
-}
-
-sub html_end
-{
-       print <<EOF;
-               <div class="foot">
-                       &quot;Lightweight Contact Manager&quot;,
-                       written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
-                       &lt;octo at verplant.org&gt;
-               </div>
-       </body>
-</html>
-EOF
-}
-
-sub read_config
-{
-       my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
-       my $fh;
-
-       open ($fh, "< $file") or die ("open ($file): $!");
-       for (<$fh>)
-       {
-               chomp;
-               my $line = $_;
-
-               if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
-               {
-                       my $key = lc ($1);
-                       my $val = $2;
-
-                       $Config{$key} = $val;
-               }
-       }
-
-       close ($fh);
-
-       for (qw(uri bind_dn password))
-       {
-               die ("Not defined: $_") unless (defined ($Config{$_}));
-       }
-}
-
-sub pwgen
-{
-       my $len = @_ ? shift : 6;
-       my $retval = '';
-
-       while (!$retval)
-       {
-               my $numbers = 0;
-               my $lchars  = 0;
-               my $uchars  = 0;
-               
-               while (length ($retval) < $len)
-               {
-                       my $chr = int (rand (128));
-
-                       if ($chr >= 48 and $chr < 58)
-                       {
-                               $numbers++;
-                       }
-                       elsif ($chr >= 65 and $chr < 91)
-                       {
-                               $uchars++;
-                       }
-                       elsif ($chr >= 97 and $chr < 123)
-                       {
-                               $lchars++;
-                       }
-                       else
-                       {
-                               next;
-                       }
-                       $retval .= chr ($chr);
-               }
-
-               $retval = '' if (!$numbers or !$lchars or !$uchars);
-       }
-
-       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. &quot;http://&quot;, &quot;ftp://&quot; 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. &quot;+49 911 123456&quot;');
-                       last;
-               }
-       }
-
-       print qq(\t\t<div class="error">\n) if (@errors);
-       for (my $i = 0; $i < scalar (@errors); $i++)
-       {
-               my $e = $errors[$i];
-
-               print "<br />\n" if ($i);
-               print "\t\t\t$e";
-       }
-       print qq(\n\t\t</div>\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);
-}
diff --git a/licom.cgi b/licom.cgi
new file mode 100755 (executable)
index 0000000..4af14ad
--- /dev/null
+++ b/licom.cgi
@@ -0,0 +1,1160 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use lib (qw(lib));
+
+use CGI (':cgi');
+use CGI::Carp (qw(fatalsToBrowser));
+use URI::Escape;
+use Data::Dumper;
+
+use Person;
+
+our $Debug = 0;
+our %Config = ();
+
+our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
+
+our %FieldNames = 
+(
+       address         => 'Address',
+       homephone       => 'Home Phone',
+       cellphone       => 'Cell Phone',
+       officephone     => 'Office Phone',
+       fax             => 'FAX',
+       mail            => 'E-Mail',
+       uri             => 'URI (Homepage)',
+       group           => 'Group'
+);
+
+our $MySelf = $ENV{'SCRIPT_NAME'};
+
+our $Action = param ('action');
+$Action ||= 'default';
+
+our %Actions =
+(
+       browse  => [\&html_start, \&action_browse,  \&html_end],
+       default => [\&html_start, \&action_browse,  \&html_end],
+       detail  => [\&html_start, \&action_detail,  \&html_end],
+       edit    => [\&html_start, \&action_edit,    \&html_end],
+       list    => [\&html_start, \&action_list,    \&html_end],
+       save    => [\&html_start, \&action_save,    \&html_end],
+       search  => [\&html_start, \&action_search,  \&html_end],
+       verify  => [\&html_start, \&action_verify,  \&html_end],
+       vcard   => \&action_vcard
+);
+
+read_config ();
+
+# make sure AuthLDAPRemoteUserIsDN is enabled.
+die unless ($ENV{'REMOTE_USER'});
+$Config{'base_dn'} = $ENV{'REMOTE_USER'};
+
+Person->connect
+(
+       uri     => $Config{'uri'},
+       base_dn => $Config{'base_dn'},
+       bind_dn => $Config{'bind_dn'},
+       password => $Config{'password'}
+) or die;
+
+our ($UserCN, $UserID) = Person->get_user ($Config{'base_dn'});
+
+if (!$UserID and $Action ne 'save')
+{
+       $Action = 'edit';
+}
+
+if (!$UserCN)
+{
+       die;
+}
+
+if (!defined ($Actions{$Action}))
+{
+       die;
+}
+
+if (ref ($Actions{$Action}) eq 'CODE')
+{
+       $Actions{$Action}->();
+}
+elsif (ref ($Actions{$Action}) eq 'ARRAY')
+{
+       for (@{$Actions{$Action}})
+       {
+               $_->();
+       }
+}
+
+#print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
+
+Person->disconnect ();
+
+exit (0);
+
+###
+
+sub action_browse
+{
+       my $group = param ('group');
+       $group = shift if (@_);
+       $group ||= '';
+
+       my @all;
+       if ($group)
+       {
+               @all = Person->search ([[group => $group]]);
+       }
+       else
+       {
+               @all = Person->search ();
+       }
+
+       if (!$group)
+       {
+               my %groups = ();
+               for (@all)
+               {
+                       my $person = $_;
+                       my @g = $person->get ('group');
+
+                       $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
+               }
+
+               print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
+               for (sort (keys (%groups)))
+               {
+                       my $group = $_;
+                       my $group_esc = uri_escape ($group);
+                       my $num = $groups{$group};
+
+                       print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
+               }
+               if (!%groups)
+               {
+                       print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
+               }
+               print qq(\t\t</ul>\n\n);
+       }
+
+       if ($group)
+       {
+               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
+       }
+       else
+       {
+               print qq(\t\t<h2>All Contacts</h2>\n);
+       }
+
+       print qq(\t\t<ul class="results">\n);
+       for (sort { $a->name () cmp $b->name () } (@all))
+       {
+               my $person = $_;
+               my $cn = $person->name ();
+               my $cn_esc = uri_escape ($cn);
+
+               print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
+       }
+       print qq(\t\t</ul>\n\n);
+
+       print qq(\t\t<div class="menu">\n);
+       if ($group)
+       {
+               my $group_esc = uri_escape ($group);
+               print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
+               qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
+       }
+       else
+       {
+               print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
+       }
+       print qq(\t\t</div>\n);
+}
+
+sub action_list
+{
+       my $group = param ('group');
+       $group = shift if (@_);
+       $group ||= '';
+
+       my $title = $group ? "List of group &quot;$group&quot;" : 'List of all addresses';
+       my @fields = (qw(address homephone cellphone officephone fax mail));
+
+       my @all = ();
+       if ($group)
+       {
+               @all = Person->search ([[group => $group]]);
+       }
+       else
+       {
+               @all = Person->search ();
+       }
+
+       print <<EOF;
+               <h2>$title</h2>
+
+               <table class="list">
+                       <tr>
+                               <th>Name</th>
+EOF
+       for (@fields)
+       {
+               print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
+       }
+       print "\t\t\t</tr>\n";
+
+       for (sort { $a->name () cmp $b->name () } (@all))
+       {
+               my $person = $_;
+               my $sn = $person->lastname ();
+               my $gn = $person->firstname ();
+
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<td>$sn, $gn</td>\n";
+
+               for (@fields)
+               {
+                       my $field = $_;
+                       my @values = $person->get ($field);
+                       print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
+               }
+
+               print "\t\t\t</tr>\n";
+       }
+       print "\t\t</table>\n\n";
+
+       if ($group)
+       {
+               my $group_esc = uri_escape ($group);
+               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
+       }
+       else
+       {
+               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
+       }
+}
+
+sub action_detail
+{
+       my $cn = param ('cn');
+       $cn = shift if (@_);
+       die unless ($cn);
+
+       my $person = Person->load ($cn);
+       if (!$person)
+       {
+               print qq(\t<div>Entry &quot;$cn&quot; could not be loaded from DB.</div>\n);
+               return;
+       }
+
+       print qq(\t\t<h2>Details for $cn</h2>\n);
+
+       my $cn_esc = uri_escape ($cn);
+
+       print <<EOF;
+               <table class="detail">
+                       <tr>
+                               <th>Name</th>
+                               <td>$cn</td>
+                       </tr>
+EOF
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my $values = $person->get ($field);
+               my $num = scalar (@$values);
+               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
+
+               next unless ($num);
+
+               print "\t\t\t<tr>\n";
+               if ($num > 1)
+               {
+                       print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
+               }
+               else
+               {
+                       print qq(\t\t\t\t<th>$print</th>\n);
+               }
+
+               for (my $i = 0; $i < $num; $i++)
+               {
+                       my $val = $values->[$i];
+
+                       if ($field eq 'group')
+                       {
+                               my $val_esc = uri_escape ($val);
+                               $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
+                       }
+                       elsif ($field eq 'uri')
+                       {
+                               my $uri = $val;
+                               $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
+                               $val = qq(<a href="$uri" class="extern">$val</a>);
+                       }
+                       elsif ($field eq 'mail')
+                       {
+                               $val = qq(<a href="mailto:$val" class="mail">$val</a>);
+                       }
+                       
+                       print "\t\t\t<tr>\n" if ($i);
+                       print "\t\t\t\t<td>$val</td>\n",
+                       "\t\t\t</tr>\n";
+               }
+       }
+       print <<EOF;
+               </table>
+
+               <div class="menu">
+                       [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
+                       [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
+                       [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
+               </div>
+
+EOF
+}
+
+sub action_search
+{
+       my $search = param ('search');
+
+       $search ||= '';
+       $search =~ s/[^\s\w]//g;
+
+       if (!$search)
+       {
+               print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
+               action_default ();
+               return;
+       }
+
+       my @patterns = split (m/\s+/, $search);
+       my @filter = ();
+
+       for (@patterns)
+       {
+               my $pattern = "$_*";
+               push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
+       }
+
+       my @matches = Person->search (@filter);
+
+       if (!@matches)
+       {
+               print qq(\t<div>No entries matched your search.</div>\n);
+               return;
+       }
+
+       if (scalar (@matches) == 1)
+       {
+               my $person = shift (@matches);
+               my $cn = $person->name ();
+               action_detail ($cn);
+               return;
+       }
+
+       print qq(\t<ul class="result">\n);
+       for (@matches)
+       {
+               my $person = $_;
+               my $cn = $person->name ();
+               my $cn_esc = uri_escape ($cn);
+
+               print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
+       }
+       print qq(\t</ul>\n);
+}
+
+sub action_edit
+{
+       my %opts = @_;
+
+       my $cn = param ('cn');
+
+       $cn = $opts{'cn'} if (defined ($opts{'cn'}));
+       $cn ||= '';
+
+       if (!$UserID)
+       {
+               $cn = $UserCN;
+       }
+
+       my $person;
+
+       my $lastname;
+       my $firstname;
+
+       my $contacts = {};
+       $contacts->{$_} = [] for (@MultiFields);
+
+       if ($cn)
+       {
+               $person = Person->load ($cn);
+
+               if (!$person)
+               {
+                       print qq(\t<div class="error">Unable to load CN &quot;$cn&quot;. Sorry.</div>\n);
+                       return;
+               }
+       
+               $lastname    = $person->lastname ();
+               $firstname   = $person->firstname ();
+
+               for (@MultiFields)
+               {
+                       $contacts->{$_} = $person->get ($_);
+               }
+       }
+
+       $lastname    = param ('lastname')    if (param ('lastname')  and $UserID);
+       $firstname   = param ('firstname')   if (param ('firstname') and $UserID);
+
+       get_contacts ($contacts);
+       
+       $lastname    =   $opts{'lastname'}     if (defined ($opts{'lastname'}));
+       $firstname   =   $opts{'firstname'}    if (defined ($opts{'firstname'}));
+       for (@MultiFields)
+       {
+               my $field = $_;
+               @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
+       }
+
+       if ($cn)
+       {
+               print "\t\t<h2>Edit contact $cn</h2>\n";
+       }
+       else
+       {
+               print "\t\t<h2>Create new contact</h2>\n";
+       }
+
+       print <<EOF;
+               <form action="$MySelf" method="post">
+               <input type="hidden" name="action" value="save" />
+               <input type="hidden" name="cn" value="$cn" />
+               <table class="edit">
+                       <tr>
+                               <th>Lastname</th>
+EOF
+       if ($UserID)
+       {
+               print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
+       }
+       else
+       {
+               print qq(\t\t\t\t<td>$lastname</td>\n);
+       }
+       print <<EOF;
+                       </tr>
+                       <tr>
+                               <th>Firstname</th>
+EOF
+       if ($UserID)
+       {
+               print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
+       }
+       else
+       {
+               print qq(\t\t\t\t<td>$firstname</td>\n);
+       }
+       
+       print "\t\t\t</tr>\n";
+
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
+               my @values = @{$contacts->{$field}};
+
+               push (@values, '');
+               
+               for (@values)
+               {
+                       my $value = $_;
+
+                       print <<EOF;
+                       <tr>
+                               <th>$print</th>
+                               <td><input type="text" name="$field" value="$value" /></td>
+                       </tr>
+EOF
+               }
+       }
+
+       print <<EOF;
+                       <tr>
+                               <th colspan="2" class="menu">
+EOF
+       if ($UserID)
+       {
+               print <<EOF;
+                                       <input type="submit" name="button" value="Cancel" />
+                                       <input type="submit" name="button" value="Apply" />
+EOF
+       }
+       print <<EOF;
+                                       <input type="submit" name="button" value="Save" />
+                               </th>
+                       </tr>
+               </table>
+               </form>
+EOF
+}
+
+sub action_save
+{
+       my $cn = $UserID ? param ('cn') : $UserCN;
+
+       if (verify_fields ())
+       {
+               action_edit (cn => $cn);
+               return;
+       }
+
+       if ($cn)
+       {
+               action_update ();
+               return;
+       }
+
+       die unless ($UserID);
+
+       my $button = lc (param ('button'));
+       $button ||= 'save';
+
+       if ($button eq 'cancel')
+       {
+               action_browse ();
+               return;
+       }
+
+       if (!param ('lastname') or !param ('firstname'))
+       {
+               print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
+               action_edit (cn => '');
+               return;
+       }
+
+       my $lastname  = param ('lastname');
+       my $firstname = param ('firstname');
+
+       my $contacts = get_contacts ();
+
+       my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
+
+       if (!$person)
+       {
+               print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
+               return;
+       }
+       
+       $cn = $person->name ();
+
+       if ($button eq 'apply')
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
+}
+
+sub action_update
+{
+       my $cn = $UserID ? param ('cn') : $UserCN;
+       my $person = Person->load ($cn);
+
+       die unless ($person);
+
+       my $button = lc (param ('button'));
+       $button ||= 'save';
+
+       if ($UserID and $button eq 'cancel')
+       {
+               action_detail ($cn);
+               return;
+       }
+
+       if ($UserID)
+       {
+               my $lastname  = param ('lastname');
+               my $firstname = param ('firstname');
+
+               $person->lastname  ($lastname)  if ($lastname  and $lastname  ne $person->lastname ());
+               $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
+
+               $cn = $person->name ();
+       }
+
+       my $contacts = get_contacts ();
+
+       for (@MultiFields)
+       {
+               my $field = $_;
+               
+               if (defined ($contacts->{$field}))
+               {
+                       my $values = $contacts->{$field};
+                       $person->set ($field, $values);
+               }
+               else
+               {
+                       $person->set ($field, []);
+               }
+       }
+
+       if ($button eq 'apply' or !$UserID)
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
+}
+
+sub action_vcard
+{
+       my $cn = param ('cn');
+       $cn = shift if (@_);
+       die unless ($cn);
+
+       my $person = Person->load ($cn);
+       die unless ($person);
+
+       my %vcard_types =
+       (
+               homephone       => 'TEL;TYPE=home,voice',
+               cellphone       => 'TEL;TYPE=cell',
+               officephone     => 'TEL;TYPE=work,voice',
+               fax             => 'TEL;TYPE=fax',
+               mail            => 'EMAIL',
+               uri             => 'URL',
+               group           => 'ORG'
+       );
+
+       my $sn = $person->lastname ();
+       my $gn = $person->firstname ();
+       my $cn_esc = uri_escape ($cn);
+
+       print <<EOF;
+Content-Type: text/x-vcard
+Content-Disposition: attachment; filename="$cn.vcf"
+
+BEGIN:VCARD
+VERSION:3.0
+FN: $cn
+N: $sn;$gn
+EOF
+
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my $vc_fld = $vcard_types{$field};
+               my $values = $person->get ($field);
+
+               for (@$values)
+               {
+                       my $value = $_;
+                       print "$vc_fld:$value\n";
+               }
+       }
+       print "END:VCARD\n";
+}
+
+sub action_verify
+{
+       my $cn = param ('cn');
+       $cn = shift if (@_);
+       die unless ($cn);
+
+       my $person = Person->load ($cn);
+       die unless ($person);
+
+       my ($mail) = $person->get ('mail');
+       $mail ||= '';
+
+       my $message;
+       my $password = $person->password ();
+
+       if (!$password)
+       {
+               $password = pwgen ();
+               $person->password ($password);
+       }
+
+       $message = qq(The password for the record &quot;$cn&quot; is &quot;$password&quot;.);
+
+       if ($mail)
+       {
+               if (action_verify_send_mail ($person))
+               {
+                       $message .= qq( A request for verification has been sent to $mail.);
+               }
+       }
+       else
+       {
+               $message .= q( There was no e-mail address, thus no verification request could be sent.);
+       }
+
+       print qq(\t\t<div class="message">$message</div>\n);
+
+       action_detail ($cn);
+}
+
+sub action_verify_send_mail
+{
+       my $person = shift;
+       my $owner = Person->load ($UserCN);
+       my $smh;
+
+       my ($owner_mail) = $owner->get ('mail');
+       if (!$owner_mail)
+       {
+               my $cn = uri_escape ($UserCN);
+               print qq(\t\t<div class="error">You have no email set in your own profile. <a href="$MySelf?action=edit&cn=$cn">Edit it now</a>!</div>\n);
+               return (0);
+       }
+
+       my $max_width = 0;
+       for (keys %FieldNames)
+       {
+               $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
+       }
+       $max_width++;
+
+       my $person_name = $person->name ();
+       my ($person_mail) = $person->get ('mail');
+       my $person_gn = $person->firstname ();
+       my $password = $person->password ();
+
+       my $host = $ENV{'HTTP_HOST'};
+       my $url = 'http://' . $host . $MySelf;
+       
+       open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
+       print $smh <<EOM;
+To: $person_name <$person_mail>
+From: $UserCN <$owner_mail>
+Subject: Please verify our entry in my address book
+
+Hello $person_gn,
+
+the following is your entry in my address book:
+EOM
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
+               my @values = $person->get ($field);
+
+               for (@values)
+               {
+                       printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
+               }
+       }
+       print $smh <<EOM;
+
+If this entry is outdated or incomplete, please take a minute and correct it.
+  Address:  $url
+ Username: $person_name
+ Password: $password
+
+Thank you very much :) Regards,
+$UserCN
+EOM
+       close ($smh);
+
+       return (1);
+}
+
+sub html_start
+{
+       my $title = shift;
+       $title = q(Lightweight Contact Manager) unless ($title);
+
+       print <<EOF;
+Content-Type: text/html; charset=UTF-8
+
+<html>
+       <head>
+               <title>$title</title>
+               <style type="text/css">
+               <!--
+               \@media screen
+               {
+                       a
+                       {
+                               color: blue;
+                               background-color: inherit;
+                               text-decoration: none;
+                       }
+
+                       a:hover
+                       {
+                               text-decoration: underline;
+                       }
+
+                       a:visited
+                       {
+                               color: navy;
+                               background-color: inherit;
+                       }
+
+                       body
+                       {
+                               color: black;
+                               background-color: white;
+                       }
+
+                       div.error
+                       {
+                               color: red;
+                               background-color: yellow;
+
+                               font-weight: bold;
+                               padding: 1ex;
+                               border: 2px solid red;
+                       }
+
+                       div.foot
+                       {
+                               color: gray;
+                               background-color: white;
+
+                               position: fixed;
+                               top: auto;
+                               right: 0px;
+                               bottom: 0px;
+                               left: 0px;
+
+                               font-size: x-small;
+                               text-align: right;
+                               border-top: 1px solid black;
+                               width: 100%;
+                       }
+
+                       div.foot a
+                       {
+                               color: black;
+                               background-color: inherit;
+                               text-decoration: none;
+                       }
+
+                       div.foot a:hover
+                       {
+                               text-decoration: underline;
+                       }
+
+                       div.menu
+                       {
+                               border-top: 1px solid black;
+                               margin-top: 1ex;
+                               font-weight: bold;
+                       }
+
+                       div.menu a
+                       {
+                               color: blue;
+                               background-color: transparent;
+                       }
+
+                       div.topmenu
+                       {
+                               margin-bottom: 1ex;
+                               padding-bottom: 1ex;
+                               border-bottom: 1px solid black;
+                       }
+
+                       div.topmenu form
+                       {
+                               display: inline;
+                               margin-right: 5ex;
+                       }
+
+                       h1
+                       {
+                               position: absolute;
+                               top: 1ex;
+                               right: 1ex;
+                               bottom: auto;
+                               left: auto;
+
+                               font-size: 100%;
+                               font-weight: bold;
+                       }
+
+                       img
+                       {
+                               border: none;
+                       }
+
+                       table.list
+                       {
+                               width: 100%;
+                       }
+
+                       table.list td
+                       {
+                               empty-cells: show;
+                       }
+
+                       td
+                       {
+                               color: black;
+                               background-color: #cccccc;
+                               vertical-align: top;
+                       }
+
+                       th
+                       {
+                               color: black;
+                               background-color: #999999;
+                               padding: 0.3ex;
+                               text-align: left;
+                               vertical-align: top;
+                       }
+               }
+
+               \@media print
+               {
+                       a
+                       {
+                               color: inherit;
+                               background-color: inherit;
+                               text-decoration: underline;
+                       }
+                       
+                       div.topmenu, div.menu
+                       {
+                               display: none;
+                       }
+
+                       div.foot
+                       {
+                               font-size: 50%;
+                               text-align: right;
+                       }
+
+                       h1
+                       {
+                               display: none;
+                       }
+
+                       h2
+                       {
+                               font-size: 100%;
+                       }
+
+                       table
+                       {
+                               border-collapse: collapse;
+                       }
+
+                       table.list
+                       {
+                               width: 100%;
+                       }
+
+                       table.list td
+                       {
+                               empty-cells: show;
+                       }
+
+                       table.list th
+                       {
+                               border-bottom-width: 2px;
+                       }
+
+                       td, th
+                       {
+                               border: 1px solid black;
+                               vertical-align: top;
+                       }
+
+                       th
+                       {
+                               font-weight: bold;
+                               text-align: center;
+                       }
+               }
+               //-->
+               </style>
+       </head>
+
+       <body>
+EOF
+       if ($UserID)
+       {
+               my $search = param ('search') || '';
+               print <<EOF;
+               <div class="topmenu">
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="browse" />
+                               <input type="submit" name="button" value="Browse" />
+                       </form>
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="search" />
+                               <input type="text" name="search" value="$search" />
+                               <input type="submit" name="button" value="Search" />
+                       </form>
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="edit" />
+                               <input type="hidden" name="dn" value="" />
+                               <input type="submit" name="button" value="Add New" />
+                       </form>
+               </div>
+EOF
+       }
+       print "\t\t<h1>$title</h1>\n";
+}
+
+sub html_end
+{
+       print <<EOF;
+               <div class="foot">
+                       &quot;Lightweight Contact Manager&quot;,
+                       written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
+                       &lt;octo at verplant.org&gt;
+               </div>
+       </body>
+</html>
+EOF
+}
+
+sub read_config
+{
+       my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
+       my $fh;
+
+       open ($fh, "< $file") or die ("open ($file): $!");
+       for (<$fh>)
+       {
+               chomp;
+               my $line = $_;
+
+               if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
+               {
+                       my $key = lc ($1);
+                       my $val = $2;
+
+                       $Config{$key} = $val;
+               }
+       }
+
+       close ($fh);
+
+       for (qw(uri bind_dn password))
+       {
+               die ("Not defined: $_") unless (defined ($Config{$_}));
+       }
+}
+
+sub pwgen
+{
+       my $len = @_ ? shift : 6;
+       my $retval = '';
+
+       while (!$retval)
+       {
+               my $numbers = 0;
+               my $lchars  = 0;
+               my $uchars  = 0;
+               
+               while (length ($retval) < $len)
+               {
+                       my $chr = int (rand (128));
+
+                       if ($chr >= 48 and $chr < 58)
+                       {
+                               $numbers++;
+                       }
+                       elsif ($chr >= 65 and $chr < 91)
+                       {
+                               $uchars++;
+                       }
+                       elsif ($chr >= 97 and $chr < 123)
+                       {
+                               $lchars++;
+                       }
+                       else
+                       {
+                               next;
+                       }
+                       $retval .= chr ($chr);
+               }
+
+               $retval = '' if (!$numbers or !$lchars or !$uchars);
+       }
+
+       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. &quot;http://&quot;, &quot;ftp://&quot; 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. &quot;+49 911 123456&quot;');
+                       last;
+               }
+       }
+
+       print qq(\t\t<div class="error">\n) if (@errors);
+       for (my $i = 0; $i < scalar (@errors); $i++)
+       {
+               my $e = $errors[$i];
+
+               print "<br />\n" if ($i);
+               print "\t\t\t$e";
+       }
+       print qq(\n\t\t</div>\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);
+}