The output has been beautified.
[licom.git] / book.cgi
index bebf6d1..4805dda 100755 (executable)
--- a/book.cgi
+++ b/book.cgi
@@ -35,12 +35,13 @@ $Action ||= 'default';
 
 our %Actions =
 (
-       browse  => [\&html_start, \&action_browse, \&html_end],
-       default => [\&html_start, \&action_browse, \&html_end],
+       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],
        save    => [\&html_start, \&action_save,    \&html_end],
        search  => [\&html_start, \&action_search,  \&html_end],
+       verify  => [\&html_start, \&action_verify,  \&html_end],
        vcard   => \&action_vcard
 );
 
@@ -111,32 +112,44 @@ sub action_browse
                        my $person = $_;
                        my @g = $person->get ('group');
 
-                       $groups{$_} = 1 for (@g);
+                       $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
                }
 
-               print qq(\t<h2>Contact Groups</h2>\n\t<ul class="groups">\n);
+               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<li><a href="$MySelf?action=browse&group=$group_esc">$group</a></li>\n);
+                       print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
                }
-               print qq(\t</ul>\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 eq '*')
+       {
+               print qq(\t\t<h2>All Contacts</h2>\n);
        }
        else
        {
-               print qq(\t<h2>Contact Group &quot;$group&quot;</h2>\n\t<ul class="results">\n);
-               for (@all)
-               {
-                       my $person = $_;
-                       my $cn = $person->name ();
-                       my $cn_esc = uri_escape ($cn);
+               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
+       }
 
-                       print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
-               }
-               print qq(\t</ul>\n);
+       print qq(\t\t<ul class="results">\n);
+       for (@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);
 }
 
 sub action_detail
@@ -191,11 +204,12 @@ EOF
                }
        }
        print <<EOF;
+               <th colspan="2" 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>]
+               </th>
        </table>
-       <div class="detail menu">
-               [<a href="$MySelf?action=edit&cn=$cn_esc">edit</a>]
-               [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
-       </div>
 EOF
 }
 
@@ -284,20 +298,22 @@ sub action_edit
        
                $lastname    = $person->lastname ();
                $firstname   = $person->firstname ();
-               $contacts->{'address'}     = $person->address ();
-               $contacts->{'homephone'}   = $person->homephone ();
-               $contacts->{'cellphone'}   = $person->cellphone ();
-               $contacts->{'officephone'} = $person->officephone ();
-               $contacts->{'fax'}         = $person->fax ();
-               $contacts->{'mail'}        = $person->mail ();
-               $contacts->{'uri'}         = $person->uri ();
-               $contacts->{'group'}       = $person->group ();
+
+               for (@MultiFields)
+               {
+                       $contacts->{$_} = $person->get ($_);
+               }
        }
 
        $lastname    = param ('lastname')    if (param ('lastname')  and $UserID);
        $firstname   = param ('firstname')   if (param ('firstname') and $UserID);
 
-       get_contacts ($contacts);
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
        
        $lastname    =   $opts{'lastname'}     if (defined ($opts{'lastname'}));
        $firstname   =   $opts{'firstname'}    if (defined ($opts{'firstname'}));
@@ -309,99 +325,83 @@ sub action_edit
 
        if ($cn)
        {
-               print "\t<h2>Edit contact $cn</h2>\n";
+               print "\t\t<h2>Edit contact $cn</h2>\n";
        }
        else
        {
-               print "\t<h2>Create new contact</h2>\n";
+               print "\t\t<h2>Create new contact</h2>\n";
        }
 
-       my $selector = sub
-       {
-               my $selected = @_ ? shift : '';
-
-               my @options = ([none => '-- Contact --']);
-
-               for (@MultiFields)
-               {
-                       my $field = $_;
-                       my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
-                       push (@options, [$field, $print]);
-               }
-
-               print qq(<select name="c_type">\n);
-               for (@options)
-               {
-                       my ($field, $print) = @$_;
-                       my $sel = $field eq $selected ? ' selected="selected"' : '';
-                       print qq(\t\t\t\t<option value="$field"$sel>$print</option>\n);
-               }
-               print qq(\t\t\t</select>);
-       };
-
        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>
-                       <td>Lastname</td>
+               <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<td><input type="text" name="lastname" value="$lastname" /></td>\n);
+               print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
        }
        else
        {
-               print qq(\t\t\t<td>$lastname</td>\n);
+               print qq(\t\t\t\t<td>$lastname</td>\n);
        }
        print <<EOF;
-               </tr>
-               <tr>
-                       <td>Firstname</td>
+                       </tr>
+                       <tr>
+                               <th>Firstname</th>
 EOF
        if ($UserID)
        {
-               print qq(\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
+               print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
        }
        else
        {
-               print qq(\t\t\t<td>$firstname</td>\n);
+               print qq(\t\t\t\t<td>$firstname</td>\n);
        }
        
-       print "\t\t</tr>\n";
+       print "\t\t\t</tr>\n";
 
        for (@MultiFields)
        {
                my $field = $_;
+               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
                my @values = @{$contacts->{$field}};
 
-               @values = ('') unless (@values);
+               push (@values, '');
                
                for (@values)
                {
                        my $value = $_;
-                       print "\t\t<tr>\n",
-                       "\t\t\t<td>";
-                       $selector->($field);
-                       print "</td>\n", <<EOF;
-                       <td><input type="text" name="c_value" value="$value" /></td>
-               </tr>
+
+                       print <<EOF;
+                       <tr>
+                               <th>$print</th>
+                               <td><input type="text" name="$field" value="$value" /></td>
+                       </tr>
 EOF
                }
        }
 
-       print "\t\t<tr>\n",
-       "\t\t\t<td>";
-       $selector->();
-       print "</td>\n", <<EOF;
-                       <td><input type="text" name="c_value" value="" /></td>
-               </tr>
-               <tr>
-                       <td colspan="2"><input type="submit" name="button" value="Save" /></td>
-               </tr>
-       </table>
-       </form>
+       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
 }
 
@@ -417,6 +417,15 @@ sub action_save
 
        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);
@@ -427,7 +436,13 @@ sub action_save
        my $lastname  = param ('lastname');
        my $firstname = param ('firstname');
 
-       my $contacts = get_contacts ();
+       my $contacts = {};
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
 
        my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
 
@@ -439,7 +454,14 @@ sub action_save
        
        $cn = $person->name ();
 
-       action_detail ($cn);
+       if ($button eq 'apply')
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
 }
 
 sub action_update
@@ -449,6 +471,15 @@ sub action_update
 
        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');
@@ -460,7 +491,13 @@ sub action_update
                $cn = $person->name ();
        }
 
-       my $contacts = get_contacts ();
+       my $contacts = {};
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
 
        for (@MultiFields)
        {
@@ -477,13 +514,13 @@ sub action_update
                }
        }
 
-       if ($UserID)
+       if ($button eq 'apply' or !$UserID)
        {
-               action_detail ($cn);
+               action_edit (cn => $cn);
        }
        else
        {
-               action_edit (cn => $cn);
+               action_detail ($cn);
        }
 }
 
@@ -536,10 +573,107 @@ EOF
        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)
+       {
+               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 $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 $owner_name = $owner->name ();
+       my ($owner_mail) = $owner->get ('mail');
+       $owner_mail ||= $ENV{'SERVER_ADMIN'};
+
+       my $host = $ENV{'HTTP_HOST'};
+       my $url = 'http://' . $host . $MySelf;
+       
+       open ($smh, '| /usr/sbin/sendmail -t') or die ("open pipe to sendmail: $!");
+       print $smh <<EOM;
+To: $person_name <$person_mail>
+From: $owner_name <$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,
+$owner_name
+EOM
+       close ($smh);
+}
+
 sub html_start
 {
        my $title = shift;
-       $title = q(octo's Lightweight Address Book) unless ($title);
+       $title = q(Lightweight Contact Manager) unless ($title);
 
        print <<EOF;
 Content-Type: text/html; charset=UTF-8
@@ -549,6 +683,24 @@ Content-Type: text/html; charset=UTF-8
                <title>$title</title>
                <style type="text/css">
                <!--
+a
+{
+       color: blue;
+       background-color: inherit;
+       text-decoration: none;
+}
+
+a:hover
+{
+       text-decoration: underline;
+}
+
+a:visited
+{
+       color: navy;
+       background-color: inherit;
+}
+
 body
 {
        color: black;
@@ -582,6 +734,18 @@ div.foot
        width: 100%;
 }
 
+div.foot a
+{
+       color: black;
+       background-color: inherit;
+       text-decoration: none;
+}
+
+div.foot a:hover
+{
+       text-decoration: underline;
+}
+
 div.menu form
 {
        display: inline;
@@ -607,6 +771,17 @@ th
        text-align: left;
        vertical-align: top;
 }
+
+th.menu
+{
+       text-align: right;
+}
+
+th.menu a
+{
+       color: blue;
+       background-color: transparent;
+}
                //-->
                </style>
        </head>
@@ -636,13 +811,17 @@ EOF
                <hr />
 EOF
        }
-       print "\t\t<h1>octo's Lightweight Address Book</h1>\n";
+       print "\t\t<h1>$title</h1>\n";
 }
 
 sub html_end
 {
        print <<EOF;
-               <div class="foot">octo's Lightweight Address Book &lt;octo at verplant.org&gt;</div>
+               <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
@@ -676,34 +855,42 @@ sub read_config
        }
 }
 
-sub get_contacts
+sub pwgen
 {
-       my $contacts = @_ ? shift : {};
+       my $len = @_ ? shift : 6;
+       my $retval = '';
 
-       if (param ('c_value'))
+       while (!$retval)
        {
-               my @c_values = param ('c_value');
-               my @c_types  = param ('c_type');
-
-               my %cts = ();
-
-               die if (scalar (@c_values) != scalar (@c_types));
-
-               for (my $i = 0; $i < scalar (@c_values); $i++)
+               my $numbers = 0;
+               my $lchars  = 0;
+               my $uchars  = 0;
+               
+               while (length ($retval) < $len)
                {
-                       my $type  = $c_types[$i];
-                       my $value = $c_values[$i];
-
-                       $cts{$type} = [] unless (defined ($cts{$type}));
-                       push (@{$cts{$type}}, $value) if ($value);
+                       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);
                }
 
-               for (@MultiFields)
-               {
-                       my $type = $_;
-                       @{$contacts->{$type}} = @{$cts{$type}} if (defined ($cts{$type}));
-               }
+               $retval = '' if (!$numbers or !$lchars or !$uchars);
        }
 
-       return ($contacts);
+       return ($retval);
 }