8 use CGI::Carp (qw(fatalsToBrowser));
12 use LiCoM::Config (qw(get_config));
18 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
23 homephone => 'Home Phone',
24 cellphone => 'Cell Phone',
25 officephone => 'Office Phone',
28 uri => 'URI (Homepage)',
32 our $MySelf = $ENV{'SCRIPT_NAME'};
34 our $Action = param ('action');
35 $Action ||= 'default';
39 browse => [\&html_start, \&action_browse, \&html_end],
40 default => [\&html_start, \&action_browse, \&html_end],
41 detail => [\&html_start, \&action_detail, \&html_end],
42 edit => [\&html_start, \&action_edit, \&html_end],
43 list => [\&html_start, \&action_list, \&html_end],
44 save => [\&html_start, \&action_save, \&html_end],
45 search => [\&html_start, \&action_search, \&html_end],
46 verify => [\&html_start, \&action_verify, \&html_end],
47 vcard => \&action_vcard
50 $Config = get_config ();
52 # make sure AuthLDAPRemoteUserIsDN is enabled.
53 die unless ($ENV{'REMOTE_USER'});
54 $Config->{'base_dn'} = $ENV{'REMOTE_USER'};
56 die unless (defined ($Config->{'uri'}) and defined ($Config->{'base_dn'})
57 and defined ($Config->{'bind_dn'}) and defined ($Config->{'password'}));
59 LiCoM::Person->connect
61 uri => $Config->{'uri'},
62 base_dn => $Config->{'base_dn'},
63 bind_dn => $Config->{'bind_dn'},
64 password => $Config->{'password'}
67 our ($UserCN, $UserID) = LiCoM::Person->get_user ($Config->{'base_dn'});
69 if (!$UserID and $Action ne 'save')
79 if (!defined ($Actions{$Action}))
84 if (ref ($Actions{$Action}) eq 'CODE')
86 $Actions{$Action}->();
88 elsif (ref ($Actions{$Action}) eq 'ARRAY')
90 for (@{$Actions{$Action}})
96 LiCoM::Person->disconnect ();
104 my $group = param ('group');
105 $group = shift if (@_);
111 @all = LiCoM::Person->search ([[group => $group]]);
115 @all = LiCoM::Person->search ();
125 my @g = $person->get ('group');
127 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
129 push (@nogroup, $person) if (!@g);
133 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
134 for (sort (keys (%groups)))
137 my $group_esc = uri_escape ($group);
138 my $num = $groups{$group};
140 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
144 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
146 print qq(\t\t</ul>\n\n);
151 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
155 print qq(\t\t<h2>Contacts without a group</h2>\n);
158 print qq(\t\t<ul class="results">\n);
159 for (sort { $a->name () cmp $b->name () } (@all))
162 my $cn = $person->name ();
163 my $cn_esc = uri_escape ($cn);
165 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
169 print "\t\t\t<li>There are no matching entries.</li>\n";
171 print qq(\t\t</ul>\n\n);
173 print qq(\t\t<div class="menu">\n);
176 my $group_esc = uri_escape ($group);
177 print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
178 qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
182 print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
184 print qq(\t\t</div>\n);
189 my $group = param ('group');
190 $group = shift if (@_);
193 my $title = $group ? "List of group "$group"" : 'List of all addresses';
194 my @fields = (qw(address homephone cellphone officephone fax mail));
199 @all = LiCoM::Person->search ([[group => $group]]);
203 @all = LiCoM::Person->search ();
215 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
217 print "\t\t\t</tr>\n";
219 for (sort { $a->name () cmp $b->name () } (@all))
222 my $sn = $person->lastname ();
223 my $gn = $person->firstname ();
225 print "\t\t\t<tr>\n",
226 "\t\t\t\t<td>$sn, $gn</td>\n";
231 my @values = $person->get ($field);
232 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
235 print "\t\t\t</tr>\n";
237 print "\t\t</table>\n\n";
241 my $group_esc = uri_escape ($group);
242 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
246 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
252 my $cn = param ('cn');
256 my $person = LiCoM::Person->load ($cn);
259 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
263 print qq(\t\t<h2>Details for $cn</h2>\n);
265 my $cn_esc = uri_escape ($cn);
268 <table class="detail">
277 my $values = $person->get ($field);
278 my $num = scalar (@$values);
279 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
283 print "\t\t\t<tr>\n";
286 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
290 print qq(\t\t\t\t<th>$print</th>\n);
293 for (my $i = 0; $i < $num; $i++)
295 my $val = $values->[$i];
297 if ($field eq 'group')
299 my $val_esc = uri_escape ($val);
300 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
302 elsif ($field eq 'uri')
305 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
306 $val = qq(<a href="$uri" class="extern">$val</a>);
308 elsif ($field eq 'mail')
310 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
313 print "\t\t\t<tr>\n" if ($i);
314 print "\t\t\t\t<td>$val</td>\n",
322 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
323 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
324 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
332 my $search = param ('search');
335 $search =~ s/[^\s\w]//g;
339 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
344 my @patterns = split (m/\s+/, $search);
350 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
353 my @matches = LiCoM::Person->search (@filter);
357 print qq(\t<div>No entries matched your search.</div>\n);
361 if (scalar (@matches) == 1)
363 my $person = shift (@matches);
364 my $cn = $person->name ();
369 print qq(\t<ul class="result">\n);
370 for (sort { $a->name () cmp $b->name () } (@matches))
373 my $cn = $person->name ();
374 my $cn_esc = uri_escape ($cn);
376 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
385 my $cn = param ('cn');
387 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
401 $contacts->{$_} = [] for (@MultiFields);
405 $person = LiCoM::Person->load ($cn);
409 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
413 $lastname = $person->lastname ();
414 $firstname = $person->firstname ();
418 $contacts->{$_} = $person->get ($_);
422 $lastname = param ('lastname') if (param ('lastname') and $UserID);
423 $firstname = param ('firstname') if (param ('firstname') and $UserID);
425 get_contacts ($contacts);
427 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
428 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
432 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
437 print "\t\t<h2>Edit contact $cn</h2>\n";
441 print "\t\t<h2>Create new contact</h2>\n";
445 <form action="$MySelf" method="post">
446 <input type="hidden" name="action" value="save" />
447 <input type="hidden" name="cn" value="$cn" />
454 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
458 print qq(\t\t\t\t<td>$lastname</td>\n);
467 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
471 print qq(\t\t\t\t<td>$firstname</td>\n);
474 print "\t\t\t</tr>\n";
479 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
480 my @values = @{$contacts->{$field}};
482 next if (!$UserID and $field eq 'group');
493 <td><input type="text" name="$field" value="$value" /></td>
501 <th colspan="2" class="menu">
506 <input type="submit" name="button" value="Cancel" />
507 <input type="submit" name="button" value="Apply" />
511 <input type="submit" name="button" value="Save" />
521 my $cn = $UserID ? param ('cn') : $UserCN;
523 if (verify_fields ())
525 action_edit (cn => $cn);
535 die unless ($UserID);
537 my $button = lc (param ('button'));
540 if ($button eq 'cancel')
546 if (!param ('lastname') or !param ('firstname'))
548 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
549 action_edit (cn => '');
553 my $lastname = param ('lastname');
554 my $firstname = param ('firstname');
556 my $contacts = get_contacts ();
558 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
562 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
566 $cn = $person->name ();
568 if ($button eq 'apply')
570 action_edit (cn => $cn);
580 my $cn = $UserID ? param ('cn') : $UserCN;
581 my $person = LiCoM::Person->load ($cn);
583 die unless ($person);
585 my $button = lc (param ('button'));
588 if ($UserID and $button eq 'cancel')
596 my $lastname = param ('lastname');
597 my $firstname = param ('firstname');
599 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
600 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
602 $cn = $person->name ();
605 my $contacts = get_contacts ();
611 next if (!$UserID and $field eq 'group');
613 if (defined ($contacts->{$field}))
615 my $values = $contacts->{$field};
616 $person->set ($field, $values);
620 $person->set ($field, []);
624 if ($button eq 'apply' or !$UserID)
626 action_edit (cn => $cn);
636 my $cn = param ('cn');
640 my $person = LiCoM::Person->load ($cn);
641 die unless ($person);
645 homephone => 'TEL;TYPE=home,voice',
646 cellphone => 'TEL;TYPE=cell',
647 officephone => 'TEL;TYPE=work,voice',
648 fax => 'TEL;TYPE=fax',
654 my $sn = $person->lastname ();
655 my $gn = $person->firstname ();
656 my $cn_esc = uri_escape ($cn);
659 Content-Type: text/x-vcard
660 Content-Disposition: attachment; filename="$cn.vcf"
671 my $vc_fld = $vcard_types{$field};
672 my $values = $person->get ($field);
674 next unless ($vc_fld);
679 print "$vc_fld:$value\n";
687 my $cn = param ('cn');
691 my $person = LiCoM::Person->load ($cn);
692 die unless ($person);
694 my ($mail) = $person->get ('mail');
698 my $password = $person->password ();
702 $password = pwgen ();
703 $person->password ($password);
706 $message = qq(The password for the record "$cn" is "$password".);
710 if (action_verify_send_mail ($person))
712 $message .= qq( A request for verification has been sent to $mail.);
717 $message .= q( There was no e-mail address, thus no verification request could be sent.);
720 print qq(\t\t<div class="message">$message</div>\n);
725 sub action_verify_send_mail
728 my $owner = LiCoM::Person->load ($UserCN);
731 my ($owner_mail) = $owner->get ('mail');
734 my $cn = uri_escape ($UserCN);
735 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);
740 for (keys %FieldNames)
742 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
746 my $person_name = $person->name ();
747 my ($person_mail) = $person->get ('mail');
748 my $person_gn = $person->firstname ();
749 my $password = $person->password ();
751 my $host = $ENV{'HTTP_HOST'};
752 my $url = 'http://' . $host . $MySelf;
754 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
756 To: $person_name <$person_mail>
757 From: $UserCN <$owner_mail>
758 Subject: Please verify our entry in my address book
762 the following is your entry in my address book:
767 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
768 my @values = $person->get ($field);
772 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
777 If this entry is outdated or incomplete, please take a minute and correct it.
779 Username: $person_name
782 Thank you very much :) Regards,
793 $title = q(Lightweight Contact Manager) unless ($title);
796 Content-Type: text/html; charset=UTF-8
800 <title>$title</title>
801 <style type="text/css">
808 background-color: inherit;
809 text-decoration: none;
814 text-decoration: underline;
820 background-color: inherit;
826 background-color: white;
832 background-color: yellow;
836 border: 2px solid red;
842 background-color: white;
852 border-top: 1px solid black;
859 background-color: inherit;
860 text-decoration: none;
865 text-decoration: underline;
870 border-top: 1px solid black;
878 background-color: transparent;
885 border-bottom: 1px solid black;
924 background-color: #cccccc;
931 background-color: #999999;
943 background-color: inherit;
944 text-decoration: underline;
947 div.topmenu, div.menu
970 border-collapse: collapse;
985 border-bottom-width: 2px;
990 border: 1px solid black;
1008 my $search = param ('search') || '';
1010 <div class="topmenu">
1011 <form action="$MySelf" method="post">
1012 <input type="hidden" name="action" value="browse" />
1013 <input type="submit" name="button" value="Browse" />
1015 <form action="$MySelf" method="post">
1016 <input type="hidden" name="action" value="search" />
1017 <input type="text" name="search" value="$search" />
1018 <input type="submit" name="button" value="Search" />
1020 <form action="$MySelf" method="post">
1021 <input type="hidden" name="action" value="edit" />
1022 <input type="hidden" name="dn" value="" />
1023 <input type="submit" name="button" value="Add New" />
1028 print "\t\t<h1>$title</h1>\n";
1035 "Lightweight Contact Manager",
1036 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1037 <octo at verplant.org>
1046 my $len = @_ ? shift : 6;
1055 while (length ($retval) < $len)
1057 my $chr = int (rand (128));
1059 if ($chr >= 48 and $chr < 58)
1063 elsif ($chr >= 65 and $chr < 91)
1067 elsif ($chr >= 97 and $chr < 123)
1075 $retval .= chr ($chr);
1078 $retval = '' if (!$numbers or !$lchars or !$uchars);
1092 if ($val !~ m#^[a-zA-Z]+://#)
1094 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1099 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1102 next unless ($number);
1104 if ($number !~ m/^\+/)
1106 push (@errors, 'Telephone numbers have to begin with the country code, e.g. "+49 911 123456"');
1111 print qq(\t\t<div class="error">\n) if (@errors);
1112 for (my $i = 0; $i < scalar (@errors); $i++)
1114 my $e = $errors[$i];
1116 print "<br />\n" if ($i);
1119 print qq(\n\t\t</div>\n\n) if (@errors);
1121 return (scalar (@errors));
1126 my $contacts = @_ ? shift : {};
1131 my @values = grep { $_ } (param ($field));
1133 next unless (@values);
1135 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1144 $contacts->{$field} = [@values] if (@values);