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 ($field eq 'group');
496 <td><input type="text" name="$field" value="$value" /></td>
504 my %c_groups = map { $_ => 1 } (@{$contacts->{'group'}});
506 my @a_persons = LiCoM::Person->search ();
510 $a_groups{$_} = 1 for ($_->get ('group'));
513 print "\t\t\t<tr>\n",
514 "\t\t\t\t<th>", $FieldNames{'group'}, "</th>\n",
515 qq(\t\t\t\t<td><select name="group" multiple="multiple">\n);
516 for (sort (keys %a_groups))
519 my $selec = defined ($c_groups{$group}) ? ' selected="selected"' : '';
521 print qq(\t\t\t\t\t<option value="$group"$selec>$group</option>\n);
523 print "\t\t\t\t</select></td>\n",
529 <th colspan="2" class="menu">
534 <input type="submit" name="button" value="Cancel" />
535 <input type="submit" name="button" value="Apply" />
539 <input type="submit" name="button" value="Save" />
549 my $cn = $UserID ? param ('cn') : $UserCN;
551 if (verify_fields ())
553 action_edit (cn => $cn);
563 die unless ($UserID);
565 my $button = lc (param ('button'));
568 if ($button eq 'cancel')
574 if (!param ('lastname') or !param ('firstname'))
576 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
577 action_edit (cn => '');
581 my $lastname = param ('lastname');
582 my $firstname = param ('firstname');
584 my $contacts = get_contacts ();
586 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
590 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
594 $cn = $person->name ();
596 if ($button eq 'apply')
598 action_edit (cn => $cn);
608 my $cn = $UserID ? param ('cn') : $UserCN;
609 my $person = LiCoM::Person->load ($cn);
611 die unless ($person);
613 my $button = lc (param ('button'));
616 if ($UserID and $button eq 'cancel')
624 my $lastname = param ('lastname');
625 my $firstname = param ('firstname');
627 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
628 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
630 $cn = $person->name ();
633 my $contacts = get_contacts ();
639 next if (!$UserID and $field eq 'group');
641 if (defined ($contacts->{$field}))
643 my $values = $contacts->{$field};
644 $person->set ($field, $values);
648 $person->set ($field, []);
652 if ($button eq 'apply' or !$UserID)
654 action_edit (cn => $cn);
664 my $cn = param ('cn');
668 my $person = LiCoM::Person->load ($cn);
669 die unless ($person);
673 homephone => 'TEL;TYPE=home,voice',
674 cellphone => 'TEL;TYPE=cell',
675 officephone => 'TEL;TYPE=work,voice',
676 fax => 'TEL;TYPE=fax',
682 my $sn = $person->lastname ();
683 my $gn = $person->firstname ();
684 my $cn_esc = uri_escape ($cn);
687 Content-Type: text/x-vcard
688 Content-Disposition: attachment; filename="$cn.vcf"
699 my $vc_fld = $vcard_types{$field};
700 my $values = $person->get ($field);
702 next unless ($vc_fld);
707 print "$vc_fld:$value\n";
715 my $cn = param ('cn');
719 my $person = LiCoM::Person->load ($cn);
720 die unless ($person);
722 my ($mail) = $person->get ('mail');
726 my $password = $person->get ('password');
730 $password = pwgen ();
731 $person->set ('password', $password);
734 $message = qq(The password for the record "$cn" is "$password".);
738 if (action_verify_send_mail ($person))
740 $message .= qq( A request for verification has been sent to $mail.);
745 $message .= q( There was no e-mail address, thus no verification request could be sent.);
748 print qq(\t\t<div class="message">$message</div>\n);
753 sub action_verify_send_mail
756 my $owner = LiCoM::Person->load ($UserCN);
759 my ($owner_mail) = $owner->get ('mail');
762 my $cn = uri_escape ($UserCN);
763 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);
768 for (keys %FieldNames)
770 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
774 my $person_name = $person->name ();
775 my ($person_mail) = $person->get ('mail');
776 my $person_gn = $person->firstname ();
777 my $password = $person->get ('password');
779 my $host = $ENV{'HTTP_HOST'};
780 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
782 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
784 To: $person_name <$person_mail>
785 From: $UserCN <$owner_mail>
786 Subject: Please verify our entry in my address book
790 the following is your entry in my address book:
795 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
796 my @values = $person->get ($field);
800 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
805 If this entry is outdated or incomplete, please take a minute and correct it.
807 Username: $person_name
810 Thank you very much :)
815 This message was automatically generated by LiCoM,
816 http://verplant.org/licom/
825 my $cn = param ('cn');
828 my $person = LiCoM::Person->load ($cn);
831 my $cn_esc = uri_escape ($cn);
834 <h2>Really delete $cn?</h2>
837 You are about to delete <strong>$cn</strong>. Are you
838 totally, absolutely sure you want to do this?
842 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
843 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
851 my $cn = param ('cn');
854 my $person = LiCoM::Person->load ($cn);
860 <div>$cn has been deleted.</div>
869 $title = q(Lightweight Contact Manager) unless ($title);
872 Content-Type: text/html; charset=UTF-8
876 <title>$title</title>
877 <style type="text/css">
884 background-color: inherit;
885 text-decoration: none;
890 text-decoration: underline;
896 background-color: inherit;
902 background-color: white;
908 background-color: yellow;
912 border: 2px solid red;
918 background-color: white;
928 border-top: 1px solid black;
935 background-color: inherit;
936 text-decoration: none;
941 text-decoration: underline;
946 border-top: 1px solid black;
954 background-color: transparent;
961 border-bottom: 1px solid black;
1000 background-color: #cccccc;
1001 vertical-align: top;
1007 background-color: #999999;
1010 vertical-align: top;
1019 background-color: inherit;
1020 text-decoration: underline;
1023 div.topmenu, div.menu
1046 border-collapse: collapse;
1061 border-bottom-width: 2px;
1066 border: 1px solid black;
1067 vertical-align: top;
1085 my $search = param ('search') || '';
1087 <div class="topmenu">
1088 <form action="$MySelf" method="post">
1089 <input type="hidden" name="action" value="browse" />
1090 <input type="submit" name="button" value="Browse" />
1092 <form action="$MySelf" method="post">
1093 <input type="hidden" name="action" value="search" />
1094 <input type="text" name="search" value="$search" />
1095 <input type="submit" name="button" value="Search" />
1097 <form action="$MySelf" method="post">
1098 <input type="hidden" name="action" value="edit" />
1099 <input type="hidden" name="dn" value="" />
1100 <input type="submit" name="button" value="Add New" />
1105 print "\t\t<h1>$title</h1>\n";
1112 "Lightweight Contact Manager",
1113 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1114 <octo at verplant.org>
1123 my $len = @_ ? shift : 6;
1132 while (length ($retval) < $len)
1134 my $chr = int (rand (128));
1136 if ($chr >= 48 and $chr < 58)
1140 elsif ($chr >= 65 and $chr < 91)
1144 elsif ($chr >= 97 and $chr < 123)
1152 $retval .= chr ($chr);
1155 $retval = '' if (!$numbers or !$lchars or !$uchars);
1169 if ($val !~ m#^[a-zA-Z]+://#)
1171 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1176 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1179 next unless ($number);
1181 if ($number !~ m/^\+[0-9 \-]+$/)
1183 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1188 print qq(\t\t<div class="error">\n) if (@errors);
1189 for (my $i = 0; $i < scalar (@errors); $i++)
1191 my $e = $errors[$i];
1193 print "<br />\n" if ($i);
1196 print qq(\n\t\t</div>\n\n) if (@errors);
1198 return (scalar (@errors));
1203 my $contacts = @_ ? shift : {};
1208 my @values = grep { $_ } (param ($field));
1210 next unless (@values);
1212 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1216 $_ =~ s/[^0-9 \-]//g;
1217 $_ = '+' . $_ if ($_);
1221 $contacts->{$field} = [@values] if (@values);