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 delete => [\&html_start, \&action_ask_del, \&html_end],
48 expunge => [\&html_start, \&action_do_del, \&html_end],
49 vcard => \&action_vcard
52 $Config = get_config ();
54 # make sure AuthLDAPRemoteUserIsDN is enabled.
55 die unless ($ENV{'REMOTE_USER'});
56 $Config->{'base_dn'} = $ENV{'REMOTE_USER'};
58 die unless (defined ($Config->{'uri'}) and defined ($Config->{'base_dn'})
59 and defined ($Config->{'bind_dn'}) and defined ($Config->{'password'}));
61 LiCoM::Person->connect
63 uri => $Config->{'uri'},
64 base_dn => $Config->{'base_dn'},
65 bind_dn => $Config->{'bind_dn'},
66 password => $Config->{'password'}
69 our ($UserCN, $UserID) = LiCoM::Person->get_user ($Config->{'base_dn'});
71 if (!$UserID and $Action ne 'save')
81 if (!defined ($Actions{$Action}))
86 if (ref ($Actions{$Action}) eq 'CODE')
88 $Actions{$Action}->();
90 elsif (ref ($Actions{$Action}) eq 'ARRAY')
92 for (@{$Actions{$Action}})
98 LiCoM::Person->disconnect ();
106 my $group = param ('group');
107 $group = shift if (@_);
113 @all = LiCoM::Person->search ([[group => $group]]);
117 @all = LiCoM::Person->search ();
127 my @g = $person->get ('group');
129 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
131 push (@nogroup, $person) if (!@g);
135 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
136 for (sort (keys (%groups)))
139 my $group_esc = uri_escape ($group);
140 my $num = $groups{$group};
142 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
146 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
148 print qq(\t\t</ul>\n\n);
153 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
157 print qq(\t\t<h2>Contacts without a group</h2>\n);
160 print qq(\t\t<ul class="results">\n);
161 for (sort { $a->name () cmp $b->name () } (@all))
164 my $cn = $person->name ();
165 my $cn_esc = uri_escape ($cn);
167 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
171 print "\t\t\t<li>There are no matching entries.</li>\n";
173 print qq(\t\t</ul>\n\n);
175 print qq(\t\t<div class="menu">\n);
178 my $group_esc = uri_escape ($group);
179 print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
180 qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
184 print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
186 print qq(\t\t</div>\n);
191 my $group = param ('group');
192 $group = shift if (@_);
195 my $title = $group ? "List of group "$group"" : 'List of all addresses';
196 my @fields = (qw(address homephone cellphone officephone fax mail));
201 @all = LiCoM::Person->search ([[group => $group]]);
205 @all = LiCoM::Person->search ();
217 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
219 print "\t\t\t</tr>\n";
221 for (sort { $a->name () cmp $b->name () } (@all))
224 my $sn = $person->lastname ();
225 my $gn = $person->firstname ();
227 print "\t\t\t<tr>\n",
228 "\t\t\t\t<td>$sn, $gn</td>\n";
233 my @values = $person->get ($field);
234 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
237 print "\t\t\t</tr>\n";
239 print "\t\t</table>\n\n";
243 my $group_esc = uri_escape ($group);
244 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
248 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
254 my $cn = param ('cn');
258 my $person = LiCoM::Person->load ($cn);
261 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
265 print qq(\t\t<h2>Details for $cn</h2>\n);
267 my $cn_esc = uri_escape ($cn);
270 <table class="detail">
279 my $values = $person->get ($field);
280 my $num = scalar (@$values);
281 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
285 print "\t\t\t<tr>\n";
288 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
292 print qq(\t\t\t\t<th>$print</th>\n);
295 for (my $i = 0; $i < $num; $i++)
297 my $val = $values->[$i];
299 if ($field eq 'group')
301 my $val_esc = uri_escape ($val);
302 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
304 elsif ($field eq 'uri')
307 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
308 $val = qq(<a href="$uri" class="extern">$val</a>);
310 elsif ($field eq 'mail')
312 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
315 print "\t\t\t<tr>\n" if ($i);
316 print "\t\t\t\t<td>$val</td>\n",
324 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
325 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
326 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
327 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
335 my $search = param ('search');
338 $search =~ s/[^\s\w]//g;
342 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
347 my @patterns = split (m/\s+/, $search);
353 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
356 my @matches = LiCoM::Person->search (@filter);
360 print qq(\t<div>No entries matched your search.</div>\n);
364 if (scalar (@matches) == 1)
366 my $person = shift (@matches);
367 my $cn = $person->name ();
372 print qq(\t<ul class="result">\n);
373 for (sort { $a->name () cmp $b->name () } (@matches))
376 my $cn = $person->name ();
377 my $cn_esc = uri_escape ($cn);
379 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
388 my $cn = param ('cn');
390 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
404 $contacts->{$_} = [] for (@MultiFields);
408 $person = LiCoM::Person->load ($cn);
412 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
416 $lastname = $person->lastname ();
417 $firstname = $person->firstname ();
421 $contacts->{$_} = $person->get ($_);
425 $lastname = param ('lastname') if (param ('lastname') and $UserID);
426 $firstname = param ('firstname') if (param ('firstname') and $UserID);
428 get_contacts ($contacts);
430 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
431 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
435 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
440 print "\t\t<h2>Edit contact $cn</h2>\n";
444 print "\t\t<h2>Create new contact</h2>\n";
448 <form action="$MySelf" method="post">
449 <input type="hidden" name="action" value="save" />
450 <input type="hidden" name="cn" value="$cn" />
457 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
461 print qq(\t\t\t\t<td>$lastname</td>\n);
470 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
474 print qq(\t\t\t\t<td>$firstname</td>\n);
477 print "\t\t\t</tr>\n";
482 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
483 my @values = @{$contacts->{$field}};
485 next if (!$UserID and $field eq 'group');
496 <td><input type="text" name="$field" value="$value" /></td>
504 <th colspan="2" class="menu">
509 <input type="submit" name="button" value="Cancel" />
510 <input type="submit" name="button" value="Apply" />
514 <input type="submit" name="button" value="Save" />
524 my $cn = $UserID ? param ('cn') : $UserCN;
526 if (verify_fields ())
528 action_edit (cn => $cn);
538 die unless ($UserID);
540 my $button = lc (param ('button'));
543 if ($button eq 'cancel')
549 if (!param ('lastname') or !param ('firstname'))
551 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
552 action_edit (cn => '');
556 my $lastname = param ('lastname');
557 my $firstname = param ('firstname');
559 my $contacts = get_contacts ();
561 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
565 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
569 $cn = $person->name ();
571 if ($button eq 'apply')
573 action_edit (cn => $cn);
583 my $cn = $UserID ? param ('cn') : $UserCN;
584 my $person = LiCoM::Person->load ($cn);
586 die unless ($person);
588 my $button = lc (param ('button'));
591 if ($UserID and $button eq 'cancel')
599 my $lastname = param ('lastname');
600 my $firstname = param ('firstname');
602 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
603 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
605 $cn = $person->name ();
608 my $contacts = get_contacts ();
614 next if (!$UserID and $field eq 'group');
616 if (defined ($contacts->{$field}))
618 my $values = $contacts->{$field};
619 $person->set ($field, $values);
623 $person->set ($field, []);
627 if ($button eq 'apply' or !$UserID)
629 action_edit (cn => $cn);
639 my $cn = param ('cn');
643 my $person = LiCoM::Person->load ($cn);
644 die unless ($person);
648 homephone => 'TEL;TYPE=home,voice',
649 cellphone => 'TEL;TYPE=cell',
650 officephone => 'TEL;TYPE=work,voice',
651 fax => 'TEL;TYPE=fax',
657 my $sn = $person->lastname ();
658 my $gn = $person->firstname ();
659 my $cn_esc = uri_escape ($cn);
662 Content-Type: text/x-vcard
663 Content-Disposition: attachment; filename="$cn.vcf"
674 my $vc_fld = $vcard_types{$field};
675 my $values = $person->get ($field);
677 next unless ($vc_fld);
682 print "$vc_fld:$value\n";
690 my $cn = param ('cn');
694 my $person = LiCoM::Person->load ($cn);
695 die unless ($person);
697 my ($mail) = $person->get ('mail');
701 my $password = $person->password ();
705 $password = pwgen ();
706 $person->password ($password);
709 $message = qq(The password for the record "$cn" is "$password".);
713 if (action_verify_send_mail ($person))
715 $message .= qq( A request for verification has been sent to $mail.);
720 $message .= q( There was no e-mail address, thus no verification request could be sent.);
723 print qq(\t\t<div class="message">$message</div>\n);
728 sub action_verify_send_mail
731 my $owner = LiCoM::Person->load ($UserCN);
734 my ($owner_mail) = $owner->get ('mail');
737 my $cn = uri_escape ($UserCN);
738 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);
743 for (keys %FieldNames)
745 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
749 my $person_name = $person->name ();
750 my ($person_mail) = $person->get ('mail');
751 my $person_gn = $person->firstname ();
752 my $password = $person->password ();
754 my $host = $ENV{'HTTP_HOST'};
755 my $url = 'http://' . $host . $MySelf;
757 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
759 To: $person_name <$person_mail>
760 From: $UserCN <$owner_mail>
761 Subject: Please verify our entry in my address book
765 the following is your entry in my address book:
770 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
771 my @values = $person->get ($field);
775 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
780 If this entry is outdated or incomplete, please take a minute and correct it.
782 Username: $person_name
785 Thank you very much :) Regards,
795 my $cn = param ('cn');
798 my $person = LiCoM::Person->load ($cn);
801 my $cn_esc = uri_escape ($cn);
804 <h2>Really delete $cn?</h2>
807 You are about to delete <strong>$cn</strong>. Are you
808 totally, absolutely sure you want to do this?
812 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
813 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
821 my $cn = param ('cn');
824 my $person = LiCoM::Person->load ($cn);
830 <div>$cn has been deleted.</div>
839 $title = q(Lightweight Contact Manager) unless ($title);
842 Content-Type: text/html; charset=UTF-8
846 <title>$title</title>
847 <style type="text/css">
854 background-color: inherit;
855 text-decoration: none;
860 text-decoration: underline;
866 background-color: inherit;
872 background-color: white;
878 background-color: yellow;
882 border: 2px solid red;
888 background-color: white;
898 border-top: 1px solid black;
905 background-color: inherit;
906 text-decoration: none;
911 text-decoration: underline;
916 border-top: 1px solid black;
924 background-color: transparent;
931 border-bottom: 1px solid black;
970 background-color: #cccccc;
977 background-color: #999999;
989 background-color: inherit;
990 text-decoration: underline;
993 div.topmenu, div.menu
1016 border-collapse: collapse;
1031 border-bottom-width: 2px;
1036 border: 1px solid black;
1037 vertical-align: top;
1054 my $search = param ('search') || '';
1056 <div class="topmenu">
1057 <form action="$MySelf" method="post">
1058 <input type="hidden" name="action" value="browse" />
1059 <input type="submit" name="button" value="Browse" />
1061 <form action="$MySelf" method="post">
1062 <input type="hidden" name="action" value="search" />
1063 <input type="text" name="search" value="$search" />
1064 <input type="submit" name="button" value="Search" />
1066 <form action="$MySelf" method="post">
1067 <input type="hidden" name="action" value="edit" />
1068 <input type="hidden" name="dn" value="" />
1069 <input type="submit" name="button" value="Add New" />
1074 print "\t\t<h1>$title</h1>\n";
1081 "Lightweight Contact Manager",
1082 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1083 <octo at verplant.org>
1092 my $len = @_ ? shift : 6;
1101 while (length ($retval) < $len)
1103 my $chr = int (rand (128));
1105 if ($chr >= 48 and $chr < 58)
1109 elsif ($chr >= 65 and $chr < 91)
1113 elsif ($chr >= 97 and $chr < 123)
1121 $retval .= chr ($chr);
1124 $retval = '' if (!$numbers or !$lchars or !$uchars);
1138 if ($val !~ m#^[a-zA-Z]+://#)
1140 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1145 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1148 next unless ($number);
1150 if ($number !~ m/^\+[0-9 \-]+$/)
1152 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1157 print qq(\t\t<div class="error">\n) if (@errors);
1158 for (my $i = 0; $i < scalar (@errors); $i++)
1160 my $e = $errors[$i];
1162 print "<br />\n" if ($i);
1165 print qq(\n\t\t</div>\n\n) if (@errors);
1167 return (scalar (@errors));
1172 my $contacts = @_ ? shift : {};
1177 my @values = grep { $_ } (param ($field));
1179 next unless (@values);
1181 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1185 $_ =~ s/[^0-9 \-]//g;
1186 $_ = '+' . $_ if ($_);
1190 $contacts->{$field} = [@values] if (@values);