8 use CGI::Carp (qw(fatalsToBrowser));
12 use LiCoM::Config (qw(get_config set_config read_config));
13 use LiCoM::Connection ();
19 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
24 homephone => 'Home Phone',
25 cellphone => 'Cell Phone',
26 officephone => 'Office Phone',
29 uri => 'URI (Homepage)',
33 our $MySelf = $ENV{'SCRIPT_NAME'};
35 our $Action = param ('action');
36 $Action ||= 'default';
40 browse => [\&html_start, \&action_browse, \&html_end],
41 default => [\&html_start, \&action_browse, \&html_end],
42 detail => [\&html_start, \&action_detail, \&html_end],
43 edit => [\&html_start, \&action_edit, \&html_end],
44 list => [\&html_start, \&action_list, \&html_end],
45 save => [\&html_start, \&action_save, \&html_end],
46 search => [\&html_start, \&action_search, \&html_end],
47 verify => [\&html_start, \&action_verify, \&html_end],
48 delete => [\&html_start, \&action_ask_del, \&html_end],
49 expunge => [\&html_start, \&action_do_del, \&html_end],
50 vcard => \&action_vcard
55 # make sure AuthLDAPRemoteUserIsDN is enabled.
56 die unless ($ENV{'REMOTE_USER'});
57 set_config ('base_dn', $ENV{'REMOTE_USER'});
59 die unless (defined (get_config ('uri'))
60 and defined (get_config ('base_dn'))
61 and defined (get_config ('bind_dn'))
62 and defined (get_config ('password')));
64 LiCoM::Connection->connect
66 uri => get_config ('uri'),
67 bind_dn => get_config ('bind_dn'),
68 password => get_config ('password')
71 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
73 if (!$UserID and $Action ne 'save')
83 if (!defined ($Actions{$Action}))
88 if (ref ($Actions{$Action}) eq 'CODE')
90 $Actions{$Action}->();
92 elsif (ref ($Actions{$Action}) eq 'ARRAY')
94 for (@{$Actions{$Action}})
100 LiCoM::Connection->disconnect ();
108 my $group = param ('group');
109 $group = shift if (@_);
114 my @groups = LiCoM::Group->all ();
116 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
120 my @members = $group->get_members ();
121 my $members = scalar (@members);
122 my $group_name = $group->name ();
123 my $group_esc = uri_escape ($group_name);
124 my $desc = $group->description ();
126 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group_name</a> ($members Member#, ($members == 1 ? ')' : 's)');
127 print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
132 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
137 [<a href="$MySelf?action=list">List all</a>]
143 my $group_obj = LiCoM::Group->load ($group);
144 my $group_esc = uri_escape ($group_obj->name ());
145 my @member_names = $group_obj->get_members ();
147 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
148 qq(\t\t<ul class="results">\n);
149 for (sort (@member_names))
152 my $cn_esc = uri_escape ($cn);
154 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
160 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
161 [<a href="$MySelf?action=browse">Back</a>]
170 my $group = param ('group');
171 $group = shift if (@_);
174 my $title = $group ? "List of group "$group"" : 'List of all addresses';
175 my @fields = (qw(address homephone cellphone officephone fax mail));
180 @all = LiCoM::Person->search ([[group => $group]]);
184 @all = LiCoM::Person->search ();
196 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
198 print "\t\t\t</tr>\n";
200 for (sort { $a->name () cmp $b->name () } (@all))
203 my $sn = $person->lastname ();
204 my $gn = $person->firstname ();
206 print "\t\t\t<tr>\n",
207 "\t\t\t\t<td>$sn, $gn</td>\n";
212 my @values = $person->get ($field);
213 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
216 print "\t\t\t</tr>\n";
218 print "\t\t</table>\n\n";
222 my $group_esc = uri_escape ($group);
223 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
227 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
233 my $cn = param ('cn');
237 my $person = LiCoM::Person->load ($cn);
240 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
244 print qq(\t\t<h2>Details for $cn</h2>\n);
246 my $cn_esc = uri_escape ($cn);
249 <table class="detail">
258 my $values = $person->get ($field);
259 my $num = scalar (@$values);
260 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
264 print "\t\t\t<tr>\n";
267 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
271 print qq(\t\t\t\t<th>$print</th>\n);
274 for (my $i = 0; $i < $num; $i++)
276 my $val = $values->[$i];
278 if ($field eq 'group')
280 my $val_esc = uri_escape ($val);
281 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
283 elsif ($field eq 'uri')
286 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
287 $val = qq(<a href="$uri" class="extern">$val</a>);
289 elsif ($field eq 'mail')
291 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
294 print "\t\t\t<tr>\n" if ($i);
295 print "\t\t\t\t<td>$val</td>\n",
300 my @groups = LiCoM::Group->load_by_member ($cn);
303 my $num = scalar (@groups);
304 print "\t\t\t<tr>\n",
305 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
306 for (my $i = 0; $i < $num; $i++)
308 my $group = $groups[$i];
309 my $group_name = $group->name ();
310 my $group_esc = uri_escape ($group_name);
312 print "\t\t\t<tr>\n" if ($i != 0);
313 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_esc">$group_name</a></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>]
325 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
333 my $search = param ('search');
336 $search =~ s/[^\s\w]//g;
340 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
345 my @patterns = split (m/\s+/, $search);
351 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
354 my @matches = LiCoM::Person->search (@filter);
358 print qq(\t<div>No entries matched your search.</div>\n);
362 if (scalar (@matches) == 1)
364 my $person = shift (@matches);
365 my $cn = $person->name ();
370 print qq(\t<ul class="result">\n);
371 for (sort { $a->name () cmp $b->name () } (@matches))
374 my $cn = $person->name ();
375 my $cn_esc = uri_escape ($cn);
377 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
386 my $cn = param ('cn');
388 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
402 $contacts->{$_} = [] for (@MultiFields);
406 $person = LiCoM::Person->load ($cn);
410 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
414 $lastname = $person->lastname ();
415 $firstname = $person->firstname ();
419 $contacts->{$_} = $person->get ($_);
423 $lastname = param ('lastname') if (param ('lastname') and $UserID);
424 $firstname = param ('firstname') if (param ('firstname') and $UserID);
426 get_contacts ($contacts);
428 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
429 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
433 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
438 print "\t\t<h2>Edit contact $cn</h2>\n";
442 print "\t\t<h2>Create new contact</h2>\n";
446 <form action="$MySelf" method="post">
447 <input type="hidden" name="action" value="save" />
448 <input type="hidden" name="cn" value="$cn" />
455 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
459 print qq(\t\t\t\t<td>$lastname</td>\n);
468 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
472 print qq(\t\t\t\t<td>$firstname</td>\n);
475 print "\t\t\t</tr>\n";
480 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
481 my @values = @{$contacts->{$field}};
483 next if ($field eq 'group');
494 <td><input type="text" name="$field" value="$value" /></td>
502 my @all_groups = LiCoM::Group->all ();
506 print "\t\t\t<tr>\n",
507 "\t\t\t\t<th>Group(s)</th>\n",
508 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
513 my $group_name = $group->name ();
516 if (grep { $cn eq $_ } ($group->get_members ()))
518 $selected = ' selected="selected"';
521 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
523 print "\t\t\t\t</select></td>\n",
527 print "\t\t\t<tr>\n",
528 "\t\t\t\t<th>New Group</th>\n",
529 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
535 <th colspan="2" class="menu">
540 <input type="submit" name="button" value="Cancel" />
541 <input type="submit" name="button" value="Apply" />
545 <input type="submit" name="button" value="Save" />
555 my $cn = $UserID ? param ('cn') : $UserCN;
557 if (verify_fields ())
559 action_edit (cn => $cn);
569 die unless ($UserID);
571 my $button = lc (param ('button'));
574 if ($button eq 'cancel')
580 if (!param ('lastname') or !param ('firstname'))
582 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
583 action_edit (cn => '');
587 my $lastname = param ('lastname');
588 my $firstname = param ('firstname');
590 my $contacts = get_contacts ();
592 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
596 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
600 $cn = $person->name ();
602 for (param ('group'))
605 my $group = LiCoM::Group->load ($group_name);
609 $group->add_members ($cn);
613 print qq(\t<div class="error">Group "$group_name" does not exist or could not be loaded.</div>\n);
617 if (param ('newgroup'))
619 # FIXME add error handling
620 my $group_name = param ('newgroup');
621 LiCoM::Group->create ($group_name, '', $cn);
624 if ($button eq 'apply')
626 action_edit (cn => $cn);
636 my $cn = $UserID ? param ('cn') : $UserCN;
637 my $person = LiCoM::Person->load ($cn);
639 die unless ($person);
641 my $button = lc (param ('button'));
644 if ($UserID and $button eq 'cancel')
652 my $lastname = param ('lastname');
653 my $firstname = param ('firstname');
655 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
656 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
658 $cn = $person->name ();
662 my $contacts = get_contacts ();
668 next if (!$UserID and $field eq 'group');
670 if (defined ($contacts->{$field}))
672 my $values = $contacts->{$field};
673 $person->set ($field, $values);
677 $person->set ($field, []);
681 my %changed_groups = map { $_ => 1 } (param ('group'));
682 my @current_groups = LiCoM::Group->load_by_member ($cn);
684 for (@current_groups)
687 my $group_name = $group_obj->name ();
689 if (!defined ($changed_groups{$group_name}))
691 $group_obj->del_members ($cn);
695 delete ($changed_groups{$group_name});
698 for (keys %changed_groups)
701 my $group_obj = LiCoM::Group->load ($group_name) or die;
703 $group_obj->add_members ($cn);
706 if (param ('newgroup'))
708 # FIXME add error handling
709 my $group_name = param ('newgroup');
710 LiCoM::Group->create ($group_name, '', $cn);
713 if ($button eq 'apply' or !$UserID)
715 action_edit (cn => $cn);
725 my $cn = param ('cn');
729 my $person = LiCoM::Person->load ($cn);
730 die unless ($person);
734 homephone => 'TEL;TYPE=home,voice',
735 cellphone => 'TEL;TYPE=cell',
736 officephone => 'TEL;TYPE=work,voice',
737 fax => 'TEL;TYPE=fax',
743 my $sn = $person->lastname ();
744 my $gn = $person->firstname ();
745 my $cn_esc = uri_escape ($cn);
748 Content-Type: text/x-vcard
749 Content-Disposition: attachment; filename="$cn.vcf"
760 my $vc_fld = $vcard_types{$field};
761 my $values = $person->get ($field);
763 next unless ($vc_fld);
768 print "$vc_fld:$value\n";
776 my $cn = param ('cn');
780 my $person = LiCoM::Person->load ($cn);
781 die unless ($person);
783 my ($mail) = $person->get ('mail');
787 my $password = $person->get ('password');
791 $password = pwgen ();
792 $person->set ('password', $password);
795 $message = qq(The password for the record "$cn" is "$password".);
799 if (action_verify_send_mail ($person))
801 $message .= qq( A request for verification has been sent to $mail.);
806 $message .= q( There was no e-mail address, thus no verification request could be sent.);
809 print qq(\t\t<div class="message">$message</div>\n);
814 sub action_verify_send_mail
817 my $owner = LiCoM::Person->load ($UserCN);
820 my ($owner_mail) = $owner->get ('mail');
823 my $cn = uri_escape ($UserCN);
824 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);
829 for (keys %FieldNames)
831 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
835 my $person_name = $person->name ();
836 my ($person_mail) = $person->get ('mail');
837 my $person_gn = $person->firstname ();
838 my $password = $person->get ('password');
840 my $host = $ENV{'HTTP_HOST'};
841 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
843 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
845 To: $person_name <$person_mail>
846 From: $UserCN <$owner_mail>
847 Subject: Please verify our entry in my address book
851 the following is your entry in my address book:
856 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
857 my @values = $person->get ($field);
861 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
866 If this entry is outdated or incomplete, please take a minute and correct it.
868 Username: $person_name
871 Thank you very much :)
876 This message was automatically generated by LiCoM,
877 http://verplant.org/licom/
886 my $cn = param ('cn');
889 my $person = LiCoM::Person->load ($cn);
892 my $cn_esc = uri_escape ($cn);
895 <h2>Really delete $cn?</h2>
898 You are about to delete <strong>$cn</strong>. Are you
899 totally, absolutely sure you want to do this?
903 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
904 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
912 my $cn = param ('cn');
915 my $person = LiCoM::Person->load ($cn);
921 <div>$cn has been deleted.</div>
930 $title = q(Lightweight Contact Manager) unless ($title);
933 Content-Type: text/html; charset=UTF-8
937 <title>$title</title>
938 <style type="text/css">
945 background-color: inherit;
946 text-decoration: none;
951 text-decoration: underline;
957 background-color: inherit;
963 background-color: white;
969 background-color: yellow;
973 border: 2px solid red;
979 background-color: white;
989 border-top: 1px solid black;
996 background-color: inherit;
997 text-decoration: none;
1002 text-decoration: underline;
1007 border-top: 1px solid black;
1015 background-color: transparent;
1021 padding-bottom: 1ex;
1022 border-bottom: 1px solid black;
1061 background-color: #cccccc;
1062 vertical-align: top;
1068 background-color: #999999;
1071 vertical-align: top;
1080 background-color: inherit;
1081 text-decoration: underline;
1084 div.topmenu, div.menu
1107 border-collapse: collapse;
1122 border-bottom-width: 2px;
1127 border: 1px solid black;
1128 vertical-align: top;
1146 my $search = param ('search') || '';
1148 <div class="topmenu">
1149 <form action="$MySelf" method="post">
1150 <input type="hidden" name="action" value="browse" />
1151 <input type="submit" name="button" value="Browse" />
1153 <form action="$MySelf" method="post">
1154 <input type="hidden" name="action" value="search" />
1155 <input type="text" name="search" value="$search" />
1156 <input type="submit" name="button" value="Search" />
1158 <form action="$MySelf" method="post">
1159 <input type="hidden" name="action" value="edit" />
1160 <input type="hidden" name="dn" value="" />
1161 <input type="submit" name="button" value="Add New" />
1166 print "\t\t<h1>$title</h1>\n";
1173 "Lightweight Contact Manager",
1174 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1175 <octo at verplant.org>
1184 my $len = @_ ? shift : 6;
1193 while (length ($retval) < $len)
1195 my $chr = int (rand (128));
1197 if ($chr >= 48 and $chr < 58)
1201 elsif ($chr >= 65 and $chr < 91)
1205 elsif ($chr >= 97 and $chr < 123)
1213 $retval .= chr ($chr);
1216 $retval = '' if (!$numbers or !$lchars or !$uchars);
1230 if ($val !~ m#^[a-zA-Z]+://#)
1232 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1237 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1240 next unless ($number);
1242 if ($number !~ m/^\+[0-9 \-]+$/)
1244 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1249 print qq(\t\t<div class="error">\n) if (@errors);
1250 for (my $i = 0; $i < scalar (@errors); $i++)
1252 my $e = $errors[$i];
1254 print "<br />\n" if ($i);
1257 print qq(\n\t\t</div>\n\n) if (@errors);
1259 return (scalar (@errors));
1264 my $contacts = @_ ? shift : {};
1269 my @values = grep { $_ } (param ($field));
1271 next unless (@values);
1273 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1277 $_ =~ s/[^0-9 \-]//g;
1278 $_ = '+' . $_ if ($_);
1282 $contacts->{$field} = [@values] if (@values);