The output has been beautified.
authorocto <octo>
Sun, 24 Apr 2005 11:11:58 +0000 (11:11 +0000)
committerocto <octo>
Sun, 24 Apr 2005 11:11:58 +0000 (11:11 +0000)
The action `verify' has been implemented.

book.cgi
lib/Person.pm

index 461815b..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,36 +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);
                }
                if (!%groups)
                {
-                       print qq(\t\t<li class="empty">There are no groups yet.</li>\n);
+                       print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
                }
-               print qq(\t</ul>\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
@@ -195,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
 }
 
@@ -328,7 +338,7 @@ sub action_edit
                <input type="hidden" name="cn" value="$cn" />
                <table class="edit">
                        <tr>
-                               <td>Lastname</td>
+                               <th>Lastname</th>
 EOF
        if ($UserID)
        {
@@ -341,7 +351,7 @@ EOF
        print <<EOF;
                        </tr>
                        <tr>
-                               <td>Firstname</td>
+                               <th>Firstname</th>
 EOF
        if ($UserID)
        {
@@ -368,7 +378,7 @@ EOF
 
                        print <<EOF;
                        <tr>
-                               <td>$print</td>
+                               <th>$print</th>
                                <td><input type="text" name="$field" value="$value" /></td>
                        </tr>
 EOF
@@ -377,12 +387,18 @@ EOF
 
        print <<EOF;
                        <tr>
-                               <td colspan="2">
+                               <th colspan="2" class="menu">
 EOF
-       print qq(\t\t\t\t\t<input type="submit" name="button" value="Update" />\n) if ($UserID);
+       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" />
-                               </td>
+                               </th>
                        </tr>
                </table>
                </form>
@@ -401,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);
@@ -429,7 +454,7 @@ sub action_save
        
        $cn = $person->name ();
 
-       if (param ('button') eq 'Update')
+       if ($button eq 'apply')
        {
                action_edit (cn => $cn);
        }
@@ -446,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');
@@ -480,7 +514,7 @@ sub action_update
                }
        }
 
-       if (param ('button') eq 'Update' or !$UserID)
+       if ($button eq 'apply' or !$UserID)
        {
                action_edit (cn => $cn);
        }
@@ -539,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
@@ -552,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;
@@ -585,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;
@@ -610,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>
@@ -639,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
@@ -678,3 +854,43 @@ sub read_config
                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);
+}
index c692e96..25df303 100644 (file)
@@ -541,6 +541,23 @@ sub get_user
        return ($cn, $id);
 }
 
+sub password
+{
+       my $obj = shift;
+       my $entry = $obj->{'ldap'};
+       my $pwd;
+
+       if (@_)
+       {
+               $pwd = shift;
+               $entry->changetype ('modify');
+               $entry->replace (userPassword => $pwd);
+               $entry->update ($Ldap);
+       }
+
+       $pwd = $entry->get_value ('userPassword');
+}
+
 =back
 
 =head1 AUTHOR