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}};
491 <td><input type="text" name="$field" value="$value" /></td>
499 <th colspan="2" class="menu">
504 <input type="submit" name="button" value="Cancel" />
505 <input type="submit" name="button" value="Apply" />
509 <input type="submit" name="button" value="Save" />
519 my $cn = $UserID ? param ('cn') : $UserCN;
521 if (verify_fields ())
523 action_edit (cn => $cn);
533 die unless ($UserID);
535 my $button = lc (param ('button'));
538 if ($button eq 'cancel')
544 if (!param ('lastname') or !param ('firstname'))
546 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
547 action_edit (cn => '');
551 my $lastname = param ('lastname');
552 my $firstname = param ('firstname');
554 my $contacts = get_contacts ();
556 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
560 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
564 $cn = $person->name ();
566 if ($button eq 'apply')
568 action_edit (cn => $cn);
578 my $cn = $UserID ? param ('cn') : $UserCN;
579 my $person = LiCoM::Person->load ($cn);
581 die unless ($person);
583 my $button = lc (param ('button'));
586 if ($UserID and $button eq 'cancel')
594 my $lastname = param ('lastname');
595 my $firstname = param ('firstname');
597 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
598 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
600 $cn = $person->name ();
603 my $contacts = get_contacts ();
609 if (defined ($contacts->{$field}))
611 my $values = $contacts->{$field};
612 $person->set ($field, $values);
616 $person->set ($field, []);
620 if ($button eq 'apply' or !$UserID)
622 action_edit (cn => $cn);
632 my $cn = param ('cn');
636 my $person = LiCoM::Person->load ($cn);
637 die unless ($person);
641 homephone => 'TEL;TYPE=home,voice',
642 cellphone => 'TEL;TYPE=cell',
643 officephone => 'TEL;TYPE=work,voice',
644 fax => 'TEL;TYPE=fax',
650 my $sn = $person->lastname ();
651 my $gn = $person->firstname ();
652 my $cn_esc = uri_escape ($cn);
655 Content-Type: text/x-vcard
656 Content-Disposition: attachment; filename="$cn.vcf"
667 my $vc_fld = $vcard_types{$field};
668 my $values = $person->get ($field);
673 print "$vc_fld:$value\n";
681 my $cn = param ('cn');
685 my $person = LiCoM::Person->load ($cn);
686 die unless ($person);
688 my ($mail) = $person->get ('mail');
692 my $password = $person->password ();
696 $password = pwgen ();
697 $person->password ($password);
700 $message = qq(The password for the record "$cn" is "$password".);
704 if (action_verify_send_mail ($person))
706 $message .= qq( A request for verification has been sent to $mail.);
711 $message .= q( There was no e-mail address, thus no verification request could be sent.);
714 print qq(\t\t<div class="message">$message</div>\n);
719 sub action_verify_send_mail
722 my $owner = LiCoM::Person->load ($UserCN);
725 my ($owner_mail) = $owner->get ('mail');
728 my $cn = uri_escape ($UserCN);
729 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);
734 for (keys %FieldNames)
736 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
740 my $person_name = $person->name ();
741 my ($person_mail) = $person->get ('mail');
742 my $person_gn = $person->firstname ();
743 my $password = $person->password ();
745 my $host = $ENV{'HTTP_HOST'};
746 my $url = 'http://' . $host . $MySelf;
748 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
750 To: $person_name <$person_mail>
751 From: $UserCN <$owner_mail>
752 Subject: Please verify our entry in my address book
756 the following is your entry in my address book:
761 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
762 my @values = $person->get ($field);
766 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
771 If this entry is outdated or incomplete, please take a minute and correct it.
773 Username: $person_name
776 Thank you very much :) Regards,
787 $title = q(Lightweight Contact Manager) unless ($title);
790 Content-Type: text/html; charset=UTF-8
794 <title>$title</title>
795 <style type="text/css">
802 background-color: inherit;
803 text-decoration: none;
808 text-decoration: underline;
814 background-color: inherit;
820 background-color: white;
826 background-color: yellow;
830 border: 2px solid red;
836 background-color: white;
846 border-top: 1px solid black;
853 background-color: inherit;
854 text-decoration: none;
859 text-decoration: underline;
864 border-top: 1px solid black;
872 background-color: transparent;
879 border-bottom: 1px solid black;
918 background-color: #cccccc;
925 background-color: #999999;
937 background-color: inherit;
938 text-decoration: underline;
941 div.topmenu, div.menu
964 border-collapse: collapse;
979 border-bottom-width: 2px;
984 border: 1px solid black;
1002 my $search = param ('search') || '';
1004 <div class="topmenu">
1005 <form action="$MySelf" method="post">
1006 <input type="hidden" name="action" value="browse" />
1007 <input type="submit" name="button" value="Browse" />
1009 <form action="$MySelf" method="post">
1010 <input type="hidden" name="action" value="search" />
1011 <input type="text" name="search" value="$search" />
1012 <input type="submit" name="button" value="Search" />
1014 <form action="$MySelf" method="post">
1015 <input type="hidden" name="action" value="edit" />
1016 <input type="hidden" name="dn" value="" />
1017 <input type="submit" name="button" value="Add New" />
1022 print "\t\t<h1>$title</h1>\n";
1029 "Lightweight Contact Manager",
1030 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1031 <octo at verplant.org>
1040 my $len = @_ ? shift : 6;
1049 while (length ($retval) < $len)
1051 my $chr = int (rand (128));
1053 if ($chr >= 48 and $chr < 58)
1057 elsif ($chr >= 65 and $chr < 91)
1061 elsif ($chr >= 97 and $chr < 123)
1069 $retval .= chr ($chr);
1072 $retval = '' if (!$numbers or !$lchars or !$uchars);
1086 if ($val !~ m#^[a-zA-Z]+://#)
1088 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1093 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1096 next unless ($number);
1098 if ($number !~ m/^\+/)
1100 push (@errors, 'Telephone numbers have to begin with the country code, e.g. "+49 911 123456"');
1105 print qq(\t\t<div class="error">\n) if (@errors);
1106 for (my $i = 0; $i < scalar (@errors); $i++)
1108 my $e = $errors[$i];
1110 print "<br />\n" if ($i);
1113 print qq(\n\t\t</div>\n\n) if (@errors);
1115 return (scalar (@errors));
1120 my $contacts = @_ ? shift : {};
1125 my @values = grep { $_ } (param ($field));
1127 next unless (@values);
1129 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1138 $contacts->{$field} = [@values] if (@values);