3 # LiCoM - Lightweight contact manager
4 # Copyright (c) 2005-2006 Florian octo Forster <octo at verplant.org>
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; only version 2 of the License is applicable.
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # You should have received a copy of the GNU General Public License along with
16 # this program; if not, write to the Free Software # Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 use CGI::Carp (qw(fatalsToBrowser));
26 use HTML::Entities (qw(encode_entities));
28 use LiCoM::Config (qw(get_config set_config read_config));
29 use LiCoM::Connection ();
35 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
40 homephone => 'Home Phone',
41 cellphone => 'Cell Phone',
42 officephone => 'Office Phone',
45 uri => 'URI (Homepage)',
49 our $MySelf = $ENV{'SCRIPT_NAME'};
51 our $Action = param ('action');
52 $Action ||= 'default';
56 browse => [\&html_start, \&action_browse, \&html_end],
57 default => [\&html_start, \&action_browse, \&html_end],
58 detail => [\&html_start, \&action_detail, \&html_end],
59 edit => [\&html_start, \&action_edit, \&html_end],
60 list => [\&html_start, \&action_list, \&html_end],
61 save => [\&html_start, \&action_save, \&html_end],
62 search => [\&html_start, \&action_search, \&html_end],
63 verify => [\&html_start, \&action_verify, \&html_end],
64 delete => [\&html_start, \&action_ask_del, \&html_end],
65 expunge => [\&html_start, \&action_do_del, \&html_end],
66 vcard => \&action_vcard
71 # make sure AuthLDAPRemoteUserIsDN is enabled.
72 die unless ($ENV{'REMOTE_USER'});
73 set_config ('base_dn', $ENV{'REMOTE_USER'});
75 die unless (defined (get_config ('uri'))
76 and defined (get_config ('base_dn'))
77 and defined (get_config ('bind_dn'))
78 and defined (get_config ('password')));
80 LiCoM::Connection->connect
82 uri => get_config ('uri'),
83 bind_dn => get_config ('bind_dn'),
84 password => get_config ('password')
87 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
89 if (!$UserID and $Action ne 'save')
99 if (!defined ($Actions{$Action}))
104 if (ref ($Actions{$Action}) eq 'CODE')
106 $Actions{$Action}->();
108 elsif (ref ($Actions{$Action}) eq 'ARRAY')
110 for (@{$Actions{$Action}})
116 LiCoM::Connection->disconnect ();
124 my $group = param ('group');
125 $group = shift if (@_);
130 my @groups = LiCoM::Group->all ();
132 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
136 my @members = $group->get_members ();
137 my $members = scalar (@members);
138 my $group_name = $group->name ();
139 my $group_esc = uri_escape ($group_name);
140 my $desc = $group->description ();
142 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">#,
143 encode_entities ($group_name),
144 qq#</a> ($members Member#, ($members == 1 ? ')' : 's)');
145 print qq(<br />\n\t\t\t\t<span class="description">),
146 encode_entities ($desc) . '</span>' if ($desc);
151 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
156 [<a href="$MySelf?action=list">List all</a>]
162 my $group_obj = LiCoM::Group->load ($group);
163 my $group_esc = uri_escape ($group_obj->name ());
164 my @member_names = $group_obj->get_members ();
166 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
167 qq(\t\t<ul class="results">\n);
168 for (sort (@member_names))
171 my $cn_esc = uri_escape ($cn);
173 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
179 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
180 [<a href="$MySelf?action=browse">Back</a>]
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 />', map { encode_entities ($_) } (@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 $cn_html = encode_entities ($cn);
257 my $cn_uri = uri_escape ($cn);
259 my $person = LiCoM::Person->load ($cn);
262 print qq(\t<div>Entry "$cn_html" could not be loaded from DB.</div>\n);
266 print qq(\t\t<h2>Details for $cn_html</h2>\n);
269 <table class="detail">
278 my $values = $person->get ($field);
279 my $num = scalar (@$values);
280 my $field_name = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
284 $field_name = encode_entities ($field_name);
286 print "\t\t\t<tr>\n";
289 print qq(\t\t\t\t<th rowspan="$num">$field_name</th>\n);
293 print qq(\t\t\t\t<th>$field_name</th>\n);
296 for (my $i = 0; $i < $num; $i++)
298 my $val = $values->[$i];
299 my $val_uri = uri_escape ($val);
300 my $val_html = encode_entities ($val);
302 if ($field eq 'group')
304 $val = qq(<a href="$MySelf?action=browse&group=$val_uri">$val_html</a>);
306 elsif ($field eq 'uri')
308 if ($val =~ m#^([a-z]+)://(.+)$#)
310 $val_uri = $1 . '://' . uri_escape ($2);
314 $val_uri = 'http://' . uri_escape ($val);
316 $val = qq(<a href="$val_uri" class="extern">$val_html</a>);
318 elsif ($field eq 'mail')
320 $val = qq(<a href="mailto:$val_uri" class="mail">$val_html</a>);
327 print "\t\t\t<tr>\n" if ($i);
328 print "\t\t\t\t<td>$val</td>\n",
333 my @groups = LiCoM::Group->load_by_member ($cn);
336 my $num = scalar (@groups);
337 print "\t\t\t<tr>\n",
338 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
339 for (my $i = 0; $i < $num; $i++)
341 my $group = $groups[$i];
342 my $group_name = $group->name ();
343 my $group_uri = uri_escape ($group_name);
344 my $group_html = encode_entities ($group_name);
346 print "\t\t\t<tr>\n" if ($i != 0);
347 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_uri">$group_html</a></td>\n),
356 [<a href="$MySelf?action=verify&cn=$cn_uri">Verify</a>]
357 [<a href="$MySelf?action=vcard&cn=$cn_uri">vCard</a>]
358 [<a href="$MySelf?action=edit&cn=$cn_uri">Edit</a>]
359 [<a href="$MySelf?action=delete&cn=$cn_uri">Delete</a>]
367 my $search = param ('search');
370 $search =~ s/[^\s\w]//g;
374 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
379 my @patterns = split (m/\s+/, $search);
385 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
388 my @matches = LiCoM::Person->search (@filter);
392 print qq(\t<div>No entries matched your search.</div>\n);
396 if (scalar (@matches) == 1)
398 my $person = shift (@matches);
399 my $cn = $person->name ();
404 print qq(\t<ul class="result">\n);
405 for (sort { $a->name () cmp $b->name () } (@matches))
408 my $cn = $person->name ();
409 my $cn_uri = uri_escape ($cn);
410 my $cn_html = encode_entities ($cn);
412 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
421 my $cn = param ('cn');
423 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
426 my $cn_html = encode_entities ($cn);
439 $contacts->{$_} = [] for (@MultiFields);
443 $person = LiCoM::Person->load ($cn);
447 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
451 $lastname = $person->lastname ();
452 $firstname = $person->firstname ();
456 $contacts->{$_} = $person->get ($_);
460 $lastname = param ('lastname') if (param ('lastname') and $UserID);
461 $firstname = param ('firstname') if (param ('firstname') and $UserID);
463 get_contacts ($contacts);
465 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
466 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
470 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
475 print "\t\t<h2>Edit contact $cn_html</h2>\n";
479 print "\t\t<h2>Create new contact</h2>\n";
483 <form action="$MySelf" method="post">
484 <input type="hidden" name="action" value="save" />
485 <input type="hidden" name="cn" value="$cn_html" />
492 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
496 print qq(\t\t\t\t<td>$lastname</td>\n);
505 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
509 print qq(\t\t\t\t<td>$firstname</td>\n);
512 print "\t\t\t</tr>\n";
517 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
518 my @values = @{$contacts->{$field}};
520 next if ($field eq 'group');
524 $field = encode_entities ($field);
525 $print = encode_entities ($print);
529 my $value = encode_entities ($_);
534 <td><input type="text" name="$field" value="$value" /></td>
542 my @all_groups = LiCoM::Group->all ();
546 print "\t\t\t<tr>\n",
547 "\t\t\t\t<th>Group(s)</th>\n",
548 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
553 my $group_name = encode_entities ($group->name ());
556 if (grep { $cn eq $_ } ($group->get_members ()))
558 $selected = ' selected="selected"';
561 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
563 print "\t\t\t\t</select></td>\n",
567 print "\t\t\t<tr>\n",
568 "\t\t\t\t<th>New Group</th>\n",
569 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
575 <th colspan="2" class="menu">
580 <input type="submit" name="button" value="Cancel" />
581 <input type="submit" name="button" value="Apply" />
585 <input type="submit" name="button" value="Save" />
595 my $cn = $UserID ? param ('cn') : $UserCN;
597 if (verify_fields ())
599 action_edit (cn => $cn);
609 die unless ($UserID);
611 my $button = lc (param ('button'));
614 if ($button eq 'cancel')
620 if (!param ('lastname') or !param ('firstname'))
622 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
623 action_edit (cn => '');
627 my $lastname = param ('lastname');
628 my $firstname = param ('firstname');
630 my $contacts = get_contacts ();
632 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
636 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
640 $cn = $person->name ();
642 for (param ('group'))
645 my $group = LiCoM::Group->load ($group_name);
649 $group->add_members ($cn);
653 my $group_html = encode_entities ($group_name);
654 print qq(\t<div class="error">Group "$group_html" does not exist or could not be loaded.</div>\n);
658 if (param ('newgroup'))
660 # FIXME add error handling
661 my $group_name = param ('newgroup');
662 LiCoM::Group->create ($group_name, '', $cn);
665 if ($button eq 'apply')
667 action_edit (cn => $cn);
677 my $cn = $UserID ? param ('cn') : $UserCN;
678 my $person = LiCoM::Person->load ($cn);
680 die unless ($person);
682 my $button = lc (param ('button'));
685 if ($UserID and $button eq 'cancel')
693 my $lastname = param ('lastname');
694 my $firstname = param ('firstname');
696 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
697 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
699 $cn = $person->name ();
701 # Each group is one entry of type (objectClass=groupOfNames)
702 # with one or more `member' attributes. These attributes are
703 # the `dn' (distinguished name) of the member entries.
706 my $contacts = get_contacts ();
712 next if (!$UserID and $field eq 'group');
714 if (defined ($contacts->{$field}))
716 my $values = $contacts->{$field};
717 $person->set ($field, $values);
721 $person->set ($field, []);
725 my %changed_groups = map { $_ => 1 } (param ('group'));
726 my @current_groups = LiCoM::Group->load_by_member ($cn);
728 for (@current_groups)
731 my $group_name = $group_obj->name ();
733 if (!defined ($changed_groups{$group_name}))
735 $group_obj->del_members ($cn);
739 delete ($changed_groups{$group_name});
742 for (keys %changed_groups)
745 my $group_obj = LiCoM::Group->load ($group_name) or die;
747 $group_obj->add_members ($cn);
750 if (param ('newgroup'))
752 # FIXME add error handling
753 my $group_name = param ('newgroup');
754 LiCoM::Group->create ($group_name, '', $cn);
757 if ($button eq 'apply' or !$UserID)
759 action_edit (cn => $cn);
769 my $cn = param ('cn');
773 my $person = LiCoM::Person->load ($cn);
774 die unless ($person);
778 homephone => 'TEL;TYPE=home,voice',
779 cellphone => 'TEL;TYPE=cell',
780 officephone => 'TEL;TYPE=work,voice',
781 fax => 'TEL;TYPE=fax',
787 my $sn = $person->lastname ();
788 my $gn = $person->firstname ();
789 my $cn_esc = uri_escape ($cn);
792 Content-Type: text/x-vcard
793 Content-Disposition: attachment; filename="$cn.vcf"
804 my $vc_fld = $vcard_types{$field};
805 my $values = $person->get ($field);
807 next unless ($vc_fld);
812 print "$vc_fld:$value\n";
820 my $cn = param ('cn');
824 my $cn_html = encode_entities ($cn);
826 my $person = LiCoM::Person->load ($cn);
827 die unless ($person);
829 my ($mail) = $person->get ('mail');
833 my ($password) = $person->get ('password');
838 $password = pwgen ();
839 $person->set ('password', [$password]);
841 $password_html = encode_entities ($password);
843 $message = qq(The password for the record "$cn_html" is "$password_html".);
847 if (action_verify_send_mail ($person))
849 my $mail_html = encode_entities ($mail);
850 $message .= qq( A request for verification has been sent to $mail_html.);
855 $message .= q( There was no e-mail address, thus no verification request could be sent.);
858 print qq(\t\t<div class="message">$message</div>\n);
863 sub action_verify_send_mail
866 my $owner = LiCoM::Person->load ($UserCN);
869 my ($owner_mail) = $owner->get ('mail');
872 my $cn_uri = uri_escape ($UserCN);
873 print qq(\t\t<div class="error">You have no email set in your own profile. <a href="$MySelf?action=edit&cn=$cn_uri">Edit it now</a>!</div>\n);
878 for (keys %FieldNames)
880 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
884 my $person_name = $person->name ();
885 my ($person_mail) = $person->get ('mail');
886 my $person_gn = $person->firstname ();
887 my ($password) = $person->get ('password');
889 my $host = $ENV{'HTTP_HOST'};
890 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
892 open ($smh, '|-', '/usr/sbin/sendmail', '-t', '-f', $owner_mail) or die ("open (sendmail): $!");
894 To: $person_name <$person_mail>
895 From: $UserCN <$owner_mail>
896 Subject: Please verify our entry in my address book
900 the following is your entry in my address book:
905 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
906 my @values = $person->get ($field);
910 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
915 If this entry is outdated or incomplete, please take a minute and correct it.
917 Username: $person_name
920 Thank you very much :)
925 This message was automatically generated by LiCoM,
926 http://verplant.org/licom/
935 my $cn = param ('cn');
938 my $person = LiCoM::Person->load ($cn);
941 my $cn_uri = uri_escape ($cn);
942 my $cn_html = encode_entities ($cn);
945 <h2>Really delete $cn_html?</h2>
948 You are about to delete <strong>$cn_html</strong>.
949 Are you totally, absolutely sure you want to do this?
953 [<a href="$MySelf?action=expunge&cn=$cn_uri">Yes, delete</a>]
954 [<a href="$MySelf?action=detail&cn=$cn_uri">No, keep</a>]
962 my $cn = param ('cn');
965 my $cn_html = encode_entities ($cn);
967 my $person = LiCoM::Person->load ($cn);
973 <div>$cn_html has been deleted.</div>
982 $title = q(Lightweight Contact Manager) unless ($title);
984 $title = encode_entities ($title);
987 Content-Type: text/html; charset=UTF-8
991 <title>$title</title>
992 <style type="text/css">
999 background-color: inherit;
1000 text-decoration: none;
1005 text-decoration: underline;
1011 background-color: inherit;
1017 background-color: white;
1023 background-color: yellow;
1027 border: 2px solid red;
1033 background-color: white;
1043 border-top: 1px solid black;
1050 background-color: inherit;
1051 text-decoration: none;
1056 text-decoration: underline;
1061 border-top: 1px solid black;
1069 background-color: transparent;
1075 padding-bottom: 1ex;
1076 border-bottom: 1px solid black;
1115 background-color: #cccccc;
1116 vertical-align: top;
1122 background-color: #999999;
1125 vertical-align: top;
1134 background-color: inherit;
1135 text-decoration: underline;
1138 div.topmenu, div.menu
1161 border-collapse: collapse;
1176 border-bottom-width: 2px;
1181 border: 1px solid black;
1182 vertical-align: top;
1200 my $search = param ('search') || '';
1201 $search = encode_entities ($search);
1203 <div class="topmenu">
1204 <form action="$MySelf" method="post">
1205 <input type="hidden" name="action" value="browse" />
1206 <input type="submit" name="button" value="Browse" />
1208 <form action="$MySelf" method="post">
1209 <input type="hidden" name="action" value="search" />
1210 <input type="text" name="search" value="$search" />
1211 <input type="submit" name="button" value="Search" />
1213 <form action="$MySelf" method="post">
1214 <input type="hidden" name="action" value="edit" />
1215 <input type="hidden" name="dn" value="" />
1216 <input type="submit" name="button" value="Add New" />
1221 print "\t\t<h1>$title</h1>\n";
1228 "Lightweight Contact Manager",
1229 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1230 <octo at verplant.org>
1239 my $len = @_ ? shift : 6;
1248 while (length ($retval) < $len)
1250 my $chr = int (rand (128));
1252 if ($chr >= 48 and $chr < 58)
1256 elsif ($chr >= 65 and $chr < 91)
1260 elsif ($chr >= 97 and $chr < 123)
1268 $retval .= chr ($chr);
1271 $retval = '' if (!$numbers or !$lchars or !$uchars);
1285 if ($val !~ m#^[a-zA-Z]+://#)
1287 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1292 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1295 next unless ($number);
1297 if ($number !~ m/^\+[0-9 \-]+$/)
1299 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1304 print qq(\t\t<div class="error">\n) if (@errors);
1305 for (my $i = 0; $i < scalar (@errors); $i++)
1307 my $e = $errors[$i];
1309 print "<br />\n" if ($i);
1312 print qq(\n\t\t</div>\n\n) if (@errors);
1314 return (scalar (@errors));
1319 my $contacts = @_ ? shift : {};
1324 my @values = grep { $_ } (param ($field));
1326 next unless (@values);
1328 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1332 $_ =~ s/[^0-9 \-]//g;
1333 $_ = '+' . $_ if ($_);
1337 $contacts->{$field} = [@values] if (@values);