From 39e18bd7b33e339617097386eba5be2566b535cc Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 24 Apr 2005 11:11:58 +0000 Subject: [PATCH] The output has been beautified. The action `verify' has been implemented. --- book.cgi | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++------- lib/Person.pm | 17 ++++ 2 files changed, 264 insertions(+), 31 deletions(-) diff --git a/book.cgi b/book.cgi index 461815b..4805dda 100755 --- 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

Contact Groups

\n\t\n\n); + } + + if ($group eq '*') + { + print qq(\t\t

All Contacts

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

Contact Group "$group"

\n\t\n); + print qq(\t\t\n\n); } sub action_detail @@ -195,11 +204,12 @@ EOF } } print < + [Verify] + [vCard] + [Edit] + - EOF } @@ -328,7 +338,7 @@ sub action_edit - + EOF if ($UserID) { @@ -341,7 +351,7 @@ EOF print < - + EOF if ($UserID) { @@ -368,7 +378,7 @@ EOF print < - + EOF @@ -377,12 +387,18 @@ EOF print < -
LastnameLastname
FirstnameFirstname$print$print
+ EOF - print qq(\t\t\t\t\t\n) if ($UserID); + if ($UserID) + { + print < + +EOF + } print < - +
@@ -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
You have to give both, first and lastname, to identify this record.
\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 "$cn" is "$password".); + + 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
$message
\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 < +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 <$title @@ -639,13 +811,17 @@ EOF
EOF } - print "\t\t

octo's Lightweight Address Book

\n"; + print "\t\t

$title

\n"; } sub html_end { print <octo's Lightweight Address Book <octo at verplant.org> +
+ "Lightweight Contact Manager", + written 2005 by Florian octo Forster + <octo at verplant.org> +
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); +} diff --git a/lib/Person.pm b/lib/Person.pm index c692e96..25df303 100644 --- a/lib/Person.pm +++ b/lib/Person.pm @@ -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 -- 2.11.0