8 use CGI::Carp (qw(fatalsToBrowser));
17 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
22 homephone => 'Home Phone',
23 cellphone => 'Cell Phone',
24 officephone => 'Office Phone',
27 uri => 'URI (Homepage)',
31 our $MySelf = $ENV{'SCRIPT_NAME'};
33 our $Action = param ('action');
34 $Action ||= 'default';
38 browse => [\&html_start, \&action_browse, \&html_end],
39 default => [\&html_start, \&action_browse, \&html_end],
40 detail => [\&html_start, \&action_detail, \&html_end],
41 edit => [\&html_start, \&action_edit, \&html_end],
42 save => [\&html_start, \&action_save, \&html_end],
43 search => [\&html_start, \&action_search, \&html_end],
44 verify => [\&html_start, \&action_verify, \&html_end],
45 vcard => \&action_vcard
50 # make sure AuthLDAPRemoteUserIsDN is enabled.
51 die unless ($ENV{'REMOTE_USER'});
52 $Config{'base_dn'} = $ENV{'REMOTE_USER'};
56 uri => $Config{'uri'},
57 base_dn => $Config{'base_dn'},
58 bind_dn => $Config{'bind_dn'},
59 password => $Config{'password'}
62 our ($UserCN, $UserID) = Person->get_user ($Config{'base_dn'});
64 if (!$UserID and $Action ne 'save')
74 if (!defined ($Actions{$Action}))
79 if (ref ($Actions{$Action}) eq 'CODE')
81 $Actions{$Action}->();
83 elsif (ref ($Actions{$Action}) eq 'ARRAY')
85 for (@{$Actions{$Action}})
91 #print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
93 Person->disconnect ();
101 my $group = param ('group');
102 $group = shift if (@_);
105 my @all = Person->search ([[group => $group]]);
113 my @g = $person->get ('group');
115 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
118 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
119 for (sort (keys (%groups)))
122 my $group_esc = uri_escape ($group);
123 my $num = $groups{$group};
125 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
129 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
131 print qq(\t\t</ul>\n\n);
136 print qq(\t\t<h2>All Contacts</h2>\n);
140 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
143 print qq(\t\t<ul class="results">\n);
147 my $cn = $person->name ();
148 my $cn_esc = uri_escape ($cn);
150 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
152 print qq(\t\t</ul>\n\n);
157 my $cn = param ('cn');
161 my $person = Person->load ($cn);
164 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
168 print qq(\t<h2>Details for $cn</h2>\n);
170 my $cn_esc = uri_escape ($cn);
173 <table class="detail">
182 my $values = $person->get ($field);
183 my $num = scalar (@$values);
184 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
191 print qq(\t\t\t<th rowspan="$num">$print</th>\n);
195 print qq(\t\t\t<th>$print</th>\n);
198 for (my $i = 0; $i < $num; $i++)
200 my $val = $values->[$i];
201 print "\t\t<tr>\n" if ($i);
202 print "\t\t\t<td>$val</td>\n",
207 <th colspan="2" class="menu">
208 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
209 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
210 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
218 my $search = param ('search');
221 $search =~ s/[^\s\w]//g;
225 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
230 my @patterns = split (m/\s+/, $search);
236 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
239 my @matches = Person->search (@filter);
243 print qq(\t<div>No entries matched your search.</div>\n);
247 if (scalar (@matches) == 1)
249 my $person = shift (@matches);
250 my $cn = $person->name ();
255 print qq(\t<ul class="result">\n);
259 my $cn = $person->name ();
260 my $cn_esc = uri_escape ($cn);
262 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
271 my $cn = param ('cn');
273 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
287 $contacts->{$_} = [] for (@MultiFields);
291 $person = Person->load ($cn);
295 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
299 $lastname = $person->lastname ();
300 $firstname = $person->firstname ();
304 $contacts->{$_} = $person->get ($_);
308 $lastname = param ('lastname') if (param ('lastname') and $UserID);
309 $firstname = param ('firstname') if (param ('firstname') and $UserID);
314 my @values = grep { $_ } (param ($field));
315 $contacts->{$field} = [@values] if (@values);
318 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
319 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
323 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
328 print "\t\t<h2>Edit contact $cn</h2>\n";
332 print "\t\t<h2>Create new contact</h2>\n";
336 <form action="$MySelf" method="post">
337 <input type="hidden" name="action" value="save" />
338 <input type="hidden" name="cn" value="$cn" />
345 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
349 print qq(\t\t\t\t<td>$lastname</td>\n);
358 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
362 print qq(\t\t\t\t<td>$firstname</td>\n);
365 print "\t\t\t</tr>\n";
370 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
371 my @values = @{$contacts->{$field}};
382 <td><input type="text" name="$field" value="$value" /></td>
390 <th colspan="2" class="menu">
395 <input type="submit" name="button" value="Cancel" />
396 <input type="submit" name="button" value="Apply" />
400 <input type="submit" name="button" value="Save" />
410 my $cn = $UserID ? param ('cn') : $UserCN;
418 die unless ($UserID);
420 my $button = lc (param ('button'));
423 if ($button eq 'cancel')
429 if (!param ('lastname') or !param ('firstname'))
431 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
432 action_edit (cn => '');
436 my $lastname = param ('lastname');
437 my $firstname = param ('firstname');
443 my @values = grep { $_ } (param ($field));
444 $contacts->{$field} = [@values] if (@values);
447 my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
451 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
455 $cn = $person->name ();
457 if ($button eq 'apply')
459 action_edit (cn => $cn);
469 my $cn = $UserID ? param ('cn') : $UserCN;
470 my $person = Person->load ($cn);
472 die unless ($person);
474 my $button = lc (param ('button'));
477 if ($UserID and $button eq 'cancel')
485 my $lastname = param ('lastname');
486 my $firstname = param ('firstname');
488 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
489 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
491 $cn = $person->name ();
498 my @values = grep { $_ } (param ($field));
499 $contacts->{$field} = [@values] if (@values);
506 if (defined ($contacts->{$field}))
508 my $values = $contacts->{$field};
509 $person->set ($field, $values);
513 $person->set ($field, []);
517 if ($button eq 'apply' or !$UserID)
519 action_edit (cn => $cn);
529 my $cn = param ('cn');
533 my $person = Person->load ($cn);
534 die unless ($person);
538 homephone => 'TEL;TYPE=home,voice',
539 cellphone => 'TEL;TYPE=cell',
540 officephone => 'TEL;TYPE=work,voice',
541 fax => 'TEL;TYPE=fax',
547 my $sn = $person->lastname ();
548 my $gn = $person->firstname ();
549 my $cn_esc = uri_escape ($cn);
552 Content-Type: text/x-vcard
553 Content-Disposition: attachment; filename="$cn.vcf"
564 my $vc_fld = $vcard_types{$field};
565 my $values = $person->get ($field);
570 print "$vc_fld:$value\n";
578 my $cn = param ('cn');
582 my $person = Person->load ($cn);
583 die unless ($person);
585 my ($mail) = $person->get ('mail');
589 my $password = $person->password ();
593 $password = pwgen ();
594 $person->password ($password);
597 $message = qq(The password for the record "$cn" is "$password".);
601 action_verify_send_mail ($person);
602 $message .= qq( A request for verification has been sent to $mail.);
606 $message .= q( There was no e-mail address, thus no verification request could be sent.);
609 print qq(\t\t<div class="message">$message</div>\n);
614 sub action_verify_send_mail
617 my $owner = Person->load ($UserCN);
621 for (keys %FieldNames)
623 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
627 my $person_name = $person->name ();
628 my ($person_mail) = $person->get ('mail');
629 my $person_gn = $person->firstname ();
630 my $password = $person->password ();
632 my $owner_name = $owner->name ();
633 my ($owner_mail) = $owner->get ('mail');
634 $owner_mail ||= $ENV{'SERVER_ADMIN'};
636 my $host = $ENV{'HTTP_HOST'};
637 my $url = 'http://' . $host . $MySelf;
639 open ($smh, '| /usr/sbin/sendmail -t') or die ("open pipe to sendmail: $!");
641 To: $person_name <$person_mail>
642 From: $owner_name <$owner_mail>
643 Subject: Please verify our entry in my address book
647 the following is your entry in my address book:
652 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
653 my @values = $person->get ($field);
657 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
662 If this entry is outdated or incomplete, please take a minute and correct it.
664 Username: $person_name
667 Thank you very much :) Regards,
676 $title = q(Lightweight Contact Manager) unless ($title);
679 Content-Type: text/html; charset=UTF-8
683 <title>$title</title>
684 <style type="text/css">
689 background-color: inherit;
690 text-decoration: none;
695 text-decoration: underline;
701 background-color: inherit;
707 background-color: white;
713 background-color: yellow;
717 border: 2px solid red;
723 background-color: white;
733 border-top: 1px solid black;
740 background-color: inherit;
741 text-decoration: none;
746 text-decoration: underline;
763 background-color: #cccccc;
769 background-color: #999999;
783 background-color: transparent;
793 my $search = param ('search') || '';
796 <form action="$MySelf" method="post">
797 <input type="hidden" name="action" value="browse" />
798 <input type="submit" name="button" value="Browse" />
800 <form action="$MySelf" method="post">
801 <input type="hidden" name="action" value="search" />
802 <input type="text" name="search" value="$search" />
803 <input type="submit" name="button" value="Search" />
805 <form action="$MySelf" method="post">
806 <input type="hidden" name="action" value="edit" />
807 <input type="hidden" name="dn" value="" />
808 <input type="submit" name="button" value="Add New" />
814 print "\t\t<h1>$title</h1>\n";
821 "Lightweight Contact Manager",
822 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
823 <octo at verplant.org>
832 my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
835 open ($fh, "< $file") or die ("open ($file): $!");
841 if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
846 $Config{$key} = $val;
852 for (qw(uri bind_dn password))
854 die ("Not defined: $_") unless (defined ($Config{$_}));
860 my $len = @_ ? shift : 6;
869 while (length ($retval) < $len)
871 my $chr = int (rand (128));
873 if ($chr >= 48 and $chr < 58)
877 elsif ($chr >= 65 and $chr < 91)
881 elsif ($chr >= 97 and $chr < 123)
889 $retval .= chr ($chr);
892 $retval = '' if (!$numbers or !$lchars or !$uchars);