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,
67 edit_group => [\&html_start, \&action_edit_group, \&html_end],
68 save_group => [\&html_start, \&action_save_group, \&html_end]
73 # make sure AuthLDAPRemoteUserIsDN is enabled.
74 die unless ($ENV{'REMOTE_USER'});
75 set_config ('base_dn', $ENV{'REMOTE_USER'});
77 die unless (defined (get_config ('uri'))
78 and defined (get_config ('base_dn'))
79 and defined (get_config ('bind_dn'))
80 and defined (get_config ('password')));
82 LiCoM::Connection->connect
84 uri => get_config ('uri'),
85 bind_dn => get_config ('bind_dn'),
86 password => get_config ('password')
89 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
91 if (!$UserID and $Action ne 'save')
101 if (!defined ($Actions{$Action}))
103 die ("No such action: $Action");
106 if (ref ($Actions{$Action}) eq 'CODE')
108 $Actions{$Action}->();
110 elsif (ref ($Actions{$Action}) eq 'ARRAY')
112 for (@{$Actions{$Action}})
118 LiCoM::Connection->disconnect ();
126 my $group = param ('group');
127 $group = shift if (@_);
132 my @groups = LiCoM::Group->all ();
134 print qq(\t\t<h2>Contact groups</h2>\n\t\t<ul class="groups">\n);
138 my @members = $group->get_members ();
139 my $members = scalar (@members);
140 my $group_name = $group->name ();
141 my $group_uri = uri_escape ($group_name);
142 my $desc = $group->description ();
144 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_uri">#,
145 encode_entities ($group_name),
146 qq#</a> ($members Member#, ($members == 1 ? ')' : 's)');
147 print qq(<br />\n\t\t\t\t<span class="description">),
148 encode_entities ($desc) . '</span>' if ($desc);
153 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
158 [<a href="$MySelf?action=list">List all</a>]
164 my $group_obj = LiCoM::Group->load ($group);
165 my $group_uri = uri_escape ($group_obj->name ());
166 my $group_html = encode_entities ($group_obj->name ());
167 my @member_names = $group_obj->get_members ();
168 my $desc = $group_obj->description ();
169 my $desc_html = encode_entities ($desc || '');
171 print qq(\t\t<h2>Contact group "$group_html"</h2>\n);
172 print qq(\t\t<div>$desc_html</div>\n) if ($desc);
173 print qq(\t\t<ul class="results">\n);
174 for (sort (@member_names))
177 my $cn_uri = uri_escape ($cn);
178 my $cn_html = encode_entities ($cn);
180 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
186 [<a href="$MySelf?action=list&group=$group_uri">List</a>]
187 [<a href="$MySelf?action=browse">Back</a>]
188 [<a href="$MySelf?action=edit_group&group=$group_uri">Edit</a>]
196 my $group = param ('group');
197 $group = shift if (@_);
200 my $title = $group ? "List of group "$group"" : 'List of all addresses';
201 my @fields = (qw(address homephone cellphone officephone fax mail));
206 @all = LiCoM::Person->search ([[group => $group]]);
210 @all = LiCoM::Person->search ();
222 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
224 print "\t\t\t</tr>\n";
226 for (sort { $a->name () cmp $b->name () } (@all))
229 my $sn = $person->lastname ();
230 my $gn = $person->firstname ();
232 print "\t\t\t<tr>\n",
233 "\t\t\t\t<td>$sn, $gn</td>\n";
238 my @values = $person->get ($field);
239 print "\t\t\t\t<td>" . join ('<br />', map { encode_entities ($_) } (@values)) . "</td>\n";
242 print "\t\t\t</tr>\n";
244 print "\t\t</table>\n\n";
248 my $group_esc = uri_escape ($group);
249 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
253 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
259 my $cn = param ('cn');
263 my $cn_html = encode_entities ($cn);
264 my $cn_uri = uri_escape ($cn);
266 my $person = LiCoM::Person->load ($cn);
269 print qq(\t<div>Entry "$cn_html" could not be loaded from DB.</div>\n);
273 print qq(\t\t<h2>Details for $cn_html</h2>\n);
276 <table class="detail">
285 my $values = $person->get ($field);
286 my $num = scalar (@$values);
287 my $field_name = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
291 $field_name = encode_entities ($field_name);
293 print "\t\t\t<tr>\n";
296 print qq(\t\t\t\t<th rowspan="$num">$field_name</th>\n);
300 print qq(\t\t\t\t<th>$field_name</th>\n);
303 for (my $i = 0; $i < $num; $i++)
305 my $val = $values->[$i];
306 my $val_uri = uri_escape ($val);
307 my $val_html = encode_entities ($val);
309 if ($field eq 'group')
311 $val = qq(<a href="$MySelf?action=browse&group=$val_uri">$val_html</a>);
313 elsif ($field eq 'uri')
315 if ($val =~ m#^([a-z]+)://(.+)$#)
317 $val_uri = $1 . '://' . uri_escape ($2);
321 $val_uri = 'http://' . uri_escape ($val);
323 $val = qq(<a href="$val_uri" class="extern">$val_html</a>);
325 elsif ($field eq 'mail')
327 $val = qq(<a href="mailto:$val_uri" class="mail">$val_html</a>);
334 print "\t\t\t<tr>\n" if ($i);
335 print "\t\t\t\t<td>$val</td>\n",
340 my @groups = LiCoM::Group->load_by_member ($cn);
343 my $num = scalar (@groups);
344 print "\t\t\t<tr>\n",
345 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
346 for (my $i = 0; $i < $num; $i++)
348 my $group = $groups[$i];
349 my $group_name = $group->name ();
350 my $group_uri = uri_escape ($group_name);
351 my $group_html = encode_entities ($group_name);
353 print "\t\t\t<tr>\n" if ($i != 0);
354 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_uri">$group_html</a></td>\n),
363 [<a href="$MySelf?action=verify&cn=$cn_uri">Verify</a>]
364 [<a href="$MySelf?action=vcard&cn=$cn_uri">vCard</a>]
365 [<a href="$MySelf?action=edit&cn=$cn_uri">Edit</a>]
366 [<a href="$MySelf?action=delete&cn=$cn_uri">Delete</a>]
374 my $search = param ('search');
377 $search =~ s/[^\s\w]//g;
381 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
386 my @patterns = split (m/\s+/, $search);
392 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
395 my @matches = LiCoM::Person->search (@filter);
399 print qq(\t<div>No entries matched your search.</div>\n);
403 if (scalar (@matches) == 1)
405 my $person = shift (@matches);
406 my $cn = $person->name ();
411 print qq(\t<ul class="result">\n);
412 for (sort { $a->name () cmp $b->name () } (@matches))
415 my $cn = $person->name ();
416 my $cn_uri = uri_escape ($cn);
417 my $cn_html = encode_entities ($cn);
419 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
428 my $cn = param ('cn');
430 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
433 my $cn_html = encode_entities ($cn);
446 $contacts->{$_} = [] for (@MultiFields);
450 $person = LiCoM::Person->load ($cn);
454 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
458 $lastname = $person->lastname ();
459 $firstname = $person->firstname ();
463 $contacts->{$_} = $person->get ($_);
467 $lastname = param ('lastname') if (param ('lastname') and $UserID);
468 $firstname = param ('firstname') if (param ('firstname') and $UserID);
470 get_contacts ($contacts);
472 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
473 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
477 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
482 print "\t\t<h2>Edit contact $cn_html</h2>\n";
486 print "\t\t<h2>Create new contact</h2>\n";
490 <form action="$MySelf" method="post">
491 <input type="hidden" name="action" value="save" />
492 <input type="hidden" name="cn" value="$cn_html" />
499 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
503 print qq(\t\t\t\t<td>$lastname</td>\n);
512 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
516 print qq(\t\t\t\t<td>$firstname</td>\n);
519 print "\t\t\t</tr>\n";
524 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
525 my @values = @{$contacts->{$field}};
527 next if ($field eq 'group');
531 $field = encode_entities ($field);
532 $print = encode_entities ($print);
536 my $value = encode_entities ($_);
541 <td><input type="text" name="$field" value="$value" /></td>
549 my @all_groups = LiCoM::Group->all ();
553 print "\t\t\t<tr>\n",
554 "\t\t\t\t<th>Group(s)</th>\n",
555 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
560 my $group_name = encode_entities ($group->name ());
563 if (grep { $cn eq $_ } ($group->get_members ()))
565 $selected = ' selected="selected"';
568 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
570 print "\t\t\t\t</select></td>\n",
574 print "\t\t\t<tr>\n",
575 "\t\t\t\t<th>New Group</th>\n",
576 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
582 <th colspan="2" class="menu">
587 <input type="submit" name="button" value="Cancel" />
588 <input type="submit" name="button" value="Apply" />
592 <input type="submit" name="button" value="Save" />
602 my $cn = $UserID ? param ('cn') : $UserCN;
604 if (verify_fields ())
606 action_edit (cn => $cn);
616 die unless ($UserID);
618 my $button = lc (param ('button'));
621 if ($button eq 'cancel')
627 if (!param ('lastname') or !param ('firstname'))
629 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
630 action_edit (cn => '');
634 my $lastname = param ('lastname');
635 my $firstname = param ('firstname');
637 my $contacts = get_contacts ();
639 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
643 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
647 $cn = $person->name ();
649 for (param ('group'))
652 my $group = LiCoM::Group->load ($group_name);
656 $group->add_members ($cn);
660 my $group_html = encode_entities ($group_name);
661 print qq(\t<div class="error">Group "$group_html" does not exist or could not be loaded.</div>\n);
665 if (param ('newgroup'))
667 # FIXME add error handling
668 my $group_name = param ('newgroup');
669 LiCoM::Group->create ($group_name, '', $cn);
672 if ($button eq 'apply')
674 action_edit (cn => $cn);
684 my $cn = $UserID ? param ('cn') : $UserCN;
685 my $person = LiCoM::Person->load ($cn);
687 die unless ($person);
689 my $button = lc (param ('button'));
692 if ($UserID and $button eq 'cancel')
700 my $lastname = param ('lastname');
701 my $firstname = param ('firstname');
703 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
704 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
706 $cn = $person->name ();
708 # Each group is one entry of type (objectClass=groupOfNames)
709 # with one or more `member' attributes. These attributes are
710 # the `dn' (distinguished name) of the member entries.
713 my $contacts = get_contacts ();
719 next if (!$UserID and $field eq 'group');
721 if (defined ($contacts->{$field}))
723 my $values = $contacts->{$field};
724 $person->set ($field, $values);
728 $person->set ($field, []);
732 # only `authorized' users may see and change groups
735 my %changed_groups = map { $_ => 1 } (param ('group'));
736 my @current_groups = LiCoM::Group->load_by_member ($cn);
738 for (@current_groups)
741 my $group_name = $group_obj->name ();
743 if (!defined ($changed_groups{$group_name}))
745 $group_obj->del_members ($cn);
749 delete ($changed_groups{$group_name});
752 for (keys %changed_groups)
755 my $group_obj = LiCoM::Group->load ($group_name) or die;
757 $group_obj->add_members ($cn);
760 if (param ('newgroup'))
762 # FIXME add error handling
763 my $group_name = param ('newgroup');
764 LiCoM::Group->create ($group_name, '', $cn);
771 <h3>Your changes have been saved.</h3>
772 <p>Thank you very much for taking the time to keep this record up to date.</p>
777 if ($button eq 'apply' or !$UserID)
779 action_edit (cn => $cn);
789 my $cn = param ('cn');
793 my $person = LiCoM::Person->load ($cn);
794 die unless ($person);
798 homephone => 'TEL;TYPE=home,voice',
799 cellphone => 'TEL;TYPE=cell',
800 officephone => 'TEL;TYPE=work,voice',
801 fax => 'TEL;TYPE=fax',
807 my $sn = $person->lastname ();
808 my $gn = $person->firstname ();
809 my $cn_esc = uri_escape ($cn);
812 Content-Type: text/x-vcard
813 Content-Disposition: attachment; filename="$cn.vcf"
824 my $vc_fld = $vcard_types{$field};
825 my $values = $person->get ($field);
827 next unless ($vc_fld);
832 print "$vc_fld:$value\n";
840 my $cn = param ('cn');
844 my $cn_html = encode_entities ($cn);
846 my $person = LiCoM::Person->load ($cn);
847 die unless ($person);
849 my ($mail) = $person->get ('mail');
853 my ($password) = $person->get ('password');
858 $password = pwgen ();
859 $person->set ('password', [$password]);
861 $password_html = encode_entities ($password);
863 $message = qq(The password for the record "$cn_html" is "$password_html".);
867 if (action_verify_send_mail ($person))
869 my $mail_html = encode_entities ($mail);
870 $message .= qq( A request for verification has been sent to $mail_html.);
875 $message .= q( There was no e-mail address, thus no verification request could be sent.);
878 print qq(\t\t<div class="message">$message</div>\n);
883 sub action_verify_send_mail
886 my $owner = LiCoM::Person->load ($UserCN);
889 my ($owner_mail) = $owner->get ('mail');
892 my $cn_uri = uri_escape ($UserCN);
893 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);
898 for (keys %FieldNames)
900 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
904 my $person_name = $person->name ();
905 my ($person_mail) = $person->get ('mail');
906 my $person_gn = $person->firstname ();
907 my ($password) = $person->get ('password');
909 my $host = $ENV{'HTTP_HOST'};
910 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
912 open ($smh, '|-', '/usr/sbin/sendmail', '-t', '-f', $owner_mail) or die ("open (sendmail): $!");
914 To: $person_name <$person_mail>
915 From: $UserCN <$owner_mail>
916 Subject: Please verify our entry in my address book
920 the following is your entry in my address book:
925 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
926 my @values = $person->get ($field);
930 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
935 If this entry is outdated or incomplete, please take a minute and correct it.
937 Username: $person_name
940 Thank you very much :)
945 This message was automatically generated by LiCoM,
946 http://verplant.org/licom/
955 my $cn = param ('cn');
958 my $person = LiCoM::Person->load ($cn);
961 my $cn_uri = uri_escape ($cn);
962 my $cn_html = encode_entities ($cn);
965 <h2>Really delete $cn_html?</h2>
968 You are about to delete <strong>$cn_html</strong>.
969 Are you totally, absolutely sure you want to do this?
973 [<a href="$MySelf?action=expunge&cn=$cn_uri">Yes, delete</a>]
974 [<a href="$MySelf?action=detail&cn=$cn_uri">No, keep</a>]
982 my $cn = param ('cn');
985 my $cn_html = encode_entities ($cn);
987 my $person = LiCoM::Person->load ($cn);
993 <div>$cn_html has been deleted.</div>
999 sub action_edit_group
1001 my $group_name = param ('group') or die;
1003 my $group_name_html = encode_entities ($group_name);
1005 my $group_obj = LiCoM::Group->load ($group_name);
1009 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1013 $group_name_html = encode_entities ($group_obj->name ());
1015 my $desc_html = encode_entities ($group_obj->description () || '');
1018 <h2>Edit contact group "$group_name_html"</h2>
1019 <form action="$MySelf" method="post">
1020 <input type="hidden" name="action" value="save_group" />
1021 <input type="hidden" name="group" value="$group_name_html" />
1025 <td>$group_name_html</td>
1028 <th>Description</th>
1029 <td><input type="text" name="description" value="$desc_html" /></td>
1032 <th colspan="2"><input type="submit" name="button" value="Save" /></th>
1039 sub action_save_group
1041 my $group_name = param ('group') or die;
1043 my $group_name_html = encode_entities ($group_name);
1045 my $group_obj = LiCoM::Group->load ($group_name);
1049 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1053 my $desc = param ('description');
1054 $group_obj->description ($desc);
1063 $title = q(Lightweight Contact Manager) unless ($title);
1065 $title = encode_entities ($title);
1068 Content-Type: text/html; charset=UTF-8
1072 <title>$title</title>
1073 <style type="text/css">
1080 background-color: inherit;
1081 text-decoration: none;
1086 text-decoration: underline;
1092 background-color: inherit;
1098 background-color: white;
1104 background-color: yellow;
1108 border: 2px solid red;
1114 background-color: white;
1124 border-top: 1px solid black;
1131 background-color: inherit;
1132 text-decoration: none;
1137 text-decoration: underline;
1142 border-top: 1px solid black;
1150 background-color: transparent;
1156 padding-bottom: 1ex;
1157 border-bottom: 1px solid black;
1196 background-color: #cccccc;
1197 vertical-align: top;
1203 background-color: #999999;
1206 vertical-align: top;
1215 background-color: inherit;
1216 text-decoration: underline;
1219 div.topmenu, div.menu
1242 border-collapse: collapse;
1257 border-bottom-width: 2px;
1262 border: 1px solid black;
1263 vertical-align: top;
1281 my $search = param ('search') || '';
1282 $search = encode_entities ($search);
1284 <div class="topmenu">
1285 <form action="$MySelf" method="post">
1286 <input type="hidden" name="action" value="browse" />
1287 <input type="submit" name="button" value="Browse" />
1289 <form action="$MySelf" method="post">
1290 <input type="hidden" name="action" value="search" />
1291 <input type="text" name="search" value="$search" />
1292 <input type="submit" name="button" value="Search" />
1294 <form action="$MySelf" method="post">
1295 <input type="hidden" name="action" value="edit" />
1296 <input type="hidden" name="dn" value="" />
1297 <input type="submit" name="button" value="Add New" />
1302 print "\t\t<h1>$title</h1>\n";
1309 "Lightweight Contact Manager",
1310 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1311 <octo at verplant.org>
1320 my $len = @_ ? shift : 6;
1329 while (length ($retval) < $len)
1331 my $chr = int (rand (128));
1333 if ($chr >= 48 and $chr < 58)
1337 elsif ($chr >= 65 and $chr < 91)
1341 elsif ($chr >= 97 and $chr < 123)
1349 $retval .= chr ($chr);
1352 $retval = '' if (!$numbers or !$lchars or !$uchars);
1366 if ($val !~ m#^[a-zA-Z]+://#)
1368 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1373 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1376 next unless ($number);
1378 if ($number !~ m/^\+[0-9 \-]+$/)
1380 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1385 print qq(\t\t<div class="error">\n) if (@errors);
1386 for (my $i = 0; $i < scalar (@errors); $i++)
1388 my $e = $errors[$i];
1390 print "<br />\n" if ($i);
1393 print qq(\n\t\t</div>\n\n) if (@errors);
1395 return (scalar (@errors));
1400 my $contacts = @_ ? shift : {};
1405 my @values = grep { $_ } (param ($field));
1407 next unless (@values);
1409 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1413 $_ =~ s/[^0-9 \-]//g;
1414 $_ = '+' . $_ if ($_);
1418 $contacts->{$field} = [@values] if (@values);