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));
29 use LiCoM::Config (qw(get_config set_config read_config));
30 use LiCoM::Connection ();
36 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
41 homephone => 'Home Phone',
42 cellphone => 'Cell Phone',
43 officephone => 'Office Phone',
46 uri => 'URI (Homepage)',
50 our $MySelf = $ENV{'SCRIPT_NAME'};
52 our $Action = param ('action');
53 $Action ||= 'default';
57 browse => [\&html_start, \&action_browse, \&html_end],
58 default => [\&html_start, \&action_browse, \&html_end],
59 detail => [\&html_start, \&action_detail, \&html_end],
60 edit => [\&html_start, \&action_edit, \&html_end],
61 list => [\&html_start, \&action_list, \&html_end],
62 save => [\&html_start, \&action_save, \&html_end],
63 search => [\&html_start, \&action_search, \&html_end],
64 verify => [\&html_start, \&action_verify, \&html_end],
65 delete => [\&html_start, \&action_ask_del, \&html_end],
66 expunge => [\&html_start, \&action_do_del, \&html_end],
67 vcard => \&action_vcard
72 # make sure AuthLDAPRemoteUserIsDN is enabled.
73 die unless ($ENV{'REMOTE_USER'});
74 set_config ('base_dn', $ENV{'REMOTE_USER'});
76 die unless (defined (get_config ('uri'))
77 and defined (get_config ('base_dn'))
78 and defined (get_config ('bind_dn'))
79 and defined (get_config ('password')));
81 LiCoM::Connection->connect
83 uri => get_config ('uri'),
84 bind_dn => get_config ('bind_dn'),
85 password => get_config ('password')
88 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
90 if (!$UserID and $Action ne 'save')
100 if (!defined ($Actions{$Action}))
105 if (ref ($Actions{$Action}) eq 'CODE')
107 $Actions{$Action}->();
109 elsif (ref ($Actions{$Action}) eq 'ARRAY')
111 for (@{$Actions{$Action}})
117 LiCoM::Connection->disconnect ();
125 my $group = param ('group');
126 $group = shift if (@_);
131 my @groups = LiCoM::Group->all ();
133 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
137 my @members = $group->get_members ();
138 my $members = scalar (@members);
139 my $group_name = $group->name ();
140 my $group_esc = uri_escape ($group_name);
141 my $desc = $group->description ();
143 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">#,
144 encode_entities ($group_name),
145 qq#</a> ($members Member#, ($members == 1 ? ')' : 's)');
146 print qq(<br />\n\t\t\t\t<span class="description">),
147 encode_entities ($desc) . '</span>' if ($desc);
152 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
157 [<a href="$MySelf?action=list">List all</a>]
163 my $group_obj = LiCoM::Group->load ($group);
164 my $group_esc = uri_escape ($group_obj->name ());
165 my @member_names = $group_obj->get_members ();
167 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
168 qq(\t\t<ul class="results">\n);
169 for (sort (@member_names))
172 my $cn_esc = uri_escape ($cn);
174 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
180 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
181 [<a href="$MySelf?action=browse">Back</a>]
190 my $group = param ('group');
191 $group = shift if (@_);
194 my $title = $group ? "List of group "$group"" : 'List of all addresses';
195 my @fields = (qw(address homephone cellphone officephone fax mail));
200 @all = LiCoM::Person->search ([[group => $group]]);
204 @all = LiCoM::Person->search ();
216 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
218 print "\t\t\t</tr>\n";
220 for (sort { $a->name () cmp $b->name () } (@all))
223 my $sn = $person->lastname ();
224 my $gn = $person->firstname ();
226 print "\t\t\t<tr>\n",
227 "\t\t\t\t<td>$sn, $gn</td>\n";
232 my @values = $person->get ($field);
233 print "\t\t\t\t<td>" . join ('<br />', map { encode_entities ($_) } (@values)) . "</td>\n";
236 print "\t\t\t</tr>\n";
238 print "\t\t</table>\n\n";
242 my $group_esc = uri_escape ($group);
243 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
247 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
253 my $cn = param ('cn');
257 my $cn_html = encode_entities ($cn);
258 my $cn_uri = uri_escape ($cn);
260 my $person = LiCoM::Person->load ($cn);
263 print qq(\t<div>Entry "$cn_html" could not be loaded from DB.</div>\n);
267 print qq(\t\t<h2>Details for $cn_html</h2>\n);
270 <table class="detail">
279 my $values = $person->get ($field);
280 my $num = scalar (@$values);
281 my $field_name = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
285 $field_name = encode_entities ($field_name);
287 print "\t\t\t<tr>\n";
290 print qq(\t\t\t\t<th rowspan="$num">$field_name</th>\n);
294 print qq(\t\t\t\t<th>$field_name</th>\n);
297 for (my $i = 0; $i < $num; $i++)
299 my $val = $values->[$i];
300 my $val_uri = uri_escape ($val);
301 my $val_html = encode_entities ($val);
303 if ($field eq 'group')
305 $val = qq(<a href="$MySelf?action=browse&group=$val_uri">$val_html</a>);
307 elsif ($field eq 'uri')
309 if ($val =~ m#^([a-z]+)://(.+)$#)
311 $val_uri = $1 . '://' . uri_escape ($2);
315 $val_uri = 'http://' . uri_escape ($val);
317 $val = qq(<a href="$val_uri" class="extern">$val_html</a>);
319 elsif ($field eq 'mail')
321 $val = qq(<a href="mailto:$val_uri" class="mail">$val_html</a>);
328 print "\t\t\t<tr>\n" if ($i);
329 print "\t\t\t\t<td>$val</td>\n",
334 my @groups = LiCoM::Group->load_by_member ($cn);
337 my $num = scalar (@groups);
338 print "\t\t\t<tr>\n",
339 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
340 for (my $i = 0; $i < $num; $i++)
342 my $group = $groups[$i];
343 my $group_name = $group->name ();
344 my $group_uri = uri_escape ($group_name);
345 my $group_html = encode_entities ($group_name);
347 print "\t\t\t<tr>\n" if ($i != 0);
348 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_uri">$group_html</a></td>\n),
357 [<a href="$MySelf?action=verify&cn=$cn_uri">Verify</a>]
358 [<a href="$MySelf?action=vcard&cn=$cn_uri">vCard</a>]
359 [<a href="$MySelf?action=edit&cn=$cn_uri">Edit</a>]
360 [<a href="$MySelf?action=delete&cn=$cn_uri">Delete</a>]
368 my $search = param ('search');
371 $search =~ s/[^\s\w]//g;
375 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
380 my @patterns = split (m/\s+/, $search);
386 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
389 my @matches = LiCoM::Person->search (@filter);
393 print qq(\t<div>No entries matched your search.</div>\n);
397 if (scalar (@matches) == 1)
399 my $person = shift (@matches);
400 my $cn = $person->name ();
405 print qq(\t<ul class="result">\n);
406 for (sort { $a->name () cmp $b->name () } (@matches))
409 my $cn = $person->name ();
410 my $cn_uri = uri_escape ($cn);
411 my $cn_html = encode_entities ($cn);
413 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
422 my $cn = param ('cn');
424 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
427 my $cn_html = encode_entities ($cn);
440 $contacts->{$_} = [] for (@MultiFields);
444 $person = LiCoM::Person->load ($cn);
448 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
452 $lastname = $person->lastname ();
453 $firstname = $person->firstname ();
457 $contacts->{$_} = $person->get ($_);
461 $lastname = param ('lastname') if (param ('lastname') and $UserID);
462 $firstname = param ('firstname') if (param ('firstname') and $UserID);
464 get_contacts ($contacts);
466 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
467 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
471 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
476 print "\t\t<h2>Edit contact $cn_html</h2>\n";
480 print "\t\t<h2>Create new contact</h2>\n";
484 <form action="$MySelf" method="post">
485 <input type="hidden" name="action" value="save" />
486 <input type="hidden" name="cn" value="$cn_html" />
493 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
497 print qq(\t\t\t\t<td>$lastname</td>\n);
506 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
510 print qq(\t\t\t\t<td>$firstname</td>\n);
513 print "\t\t\t</tr>\n";
518 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
519 my @values = @{$contacts->{$field}};
521 next if ($field eq 'group');
525 $field = encode_entities ($field);
526 $print = encode_entities ($print);
530 my $value = encode_entities ($_);
535 <td><input type="text" name="$field" value="$value" /></td>
543 my @all_groups = LiCoM::Group->all ();
547 print "\t\t\t<tr>\n",
548 "\t\t\t\t<th>Group(s)</th>\n",
549 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
554 my $group_name = encode_entities ($group->name ());
557 if (grep { $cn eq $_ } ($group->get_members ()))
559 $selected = ' selected="selected"';
562 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
564 print "\t\t\t\t</select></td>\n",
568 print "\t\t\t<tr>\n",
569 "\t\t\t\t<th>New Group</th>\n",
570 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
576 <th colspan="2" class="menu">
581 <input type="submit" name="button" value="Cancel" />
582 <input type="submit" name="button" value="Apply" />
586 <input type="submit" name="button" value="Save" />
596 my $cn = $UserID ? param ('cn') : $UserCN;
598 if (verify_fields ())
600 action_edit (cn => $cn);
610 die unless ($UserID);
612 my $button = lc (param ('button'));
615 if ($button eq 'cancel')
621 if (!param ('lastname') or !param ('firstname'))
623 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
624 action_edit (cn => '');
628 my $lastname = param ('lastname');
629 my $firstname = param ('firstname');
631 my $contacts = get_contacts ();
633 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
637 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
641 $cn = $person->name ();
643 for (param ('group'))
646 my $group = LiCoM::Group->load ($group_name);
650 $group->add_members ($cn);
654 my $group_html = encode_entities ($group_name);
655 print qq(\t<div class="error">Group "$group_html" does not exist or could not be loaded.</div>\n);
659 if (param ('newgroup'))
661 # FIXME add error handling
662 my $group_name = param ('newgroup');
663 LiCoM::Group->create ($group_name, '', $cn);
666 if ($button eq 'apply')
668 action_edit (cn => $cn);
678 my $cn = $UserID ? param ('cn') : $UserCN;
679 my $person = LiCoM::Person->load ($cn);
681 die unless ($person);
683 my $button = lc (param ('button'));
686 if ($UserID and $button eq 'cancel')
694 my $lastname = param ('lastname');
695 my $firstname = param ('firstname');
697 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
698 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
700 $cn = $person->name ();
702 # Each group is one entry of type (objectClass=groupOfNames)
703 # with one or more `member' attributes. These attributes are
704 # the `dn' (distinguished name) of the member entries.
707 my $contacts = get_contacts ();
713 next if (!$UserID and $field eq 'group');
715 if (defined ($contacts->{$field}))
717 my $values = $contacts->{$field};
718 $person->set ($field, $values);
722 $person->set ($field, []);
726 my %changed_groups = map { $_ => 1 } (param ('group'));
727 my @current_groups = LiCoM::Group->load_by_member ($cn);
729 for (@current_groups)
732 my $group_name = $group_obj->name ();
734 if (!defined ($changed_groups{$group_name}))
736 $group_obj->del_members ($cn);
740 delete ($changed_groups{$group_name});
743 for (keys %changed_groups)
746 my $group_obj = LiCoM::Group->load ($group_name) or die;
748 $group_obj->add_members ($cn);
751 if (param ('newgroup'))
753 # FIXME add error handling
754 my $group_name = param ('newgroup');
755 LiCoM::Group->create ($group_name, '', $cn);
758 if ($button eq 'apply' or !$UserID)
760 action_edit (cn => $cn);
770 my $cn = param ('cn');
774 my $person = LiCoM::Person->load ($cn);
775 die unless ($person);
779 homephone => 'TEL;TYPE=home,voice',
780 cellphone => 'TEL;TYPE=cell',
781 officephone => 'TEL;TYPE=work,voice',
782 fax => 'TEL;TYPE=fax',
788 my $sn = $person->lastname ();
789 my $gn = $person->firstname ();
790 my $cn_esc = uri_escape ($cn);
793 Content-Type: text/x-vcard
794 Content-Disposition: attachment; filename="$cn.vcf"
805 my $vc_fld = $vcard_types{$field};
806 my $values = $person->get ($field);
808 next unless ($vc_fld);
813 print "$vc_fld:$value\n";
821 my $cn = param ('cn');
825 my $cn_html = encode_entities ($cn);
827 my $person = LiCoM::Person->load ($cn);
828 die unless ($person);
830 my ($mail) = $person->get ('mail');
834 my ($password) = $person->get ('password');
839 $password = pwgen ();
840 $person->set ('password', [$password]);
842 $password_html = encode_entities ($password);
844 $message = qq(The password for the record "$cn_html" is "$password_html".);
848 if (action_verify_send_mail ($person))
850 my $mail_html = encode_entities ($mail);
851 $message .= qq( A request for verification has been sent to $mail_html.);
856 $message .= q( There was no e-mail address, thus no verification request could be sent.);
859 print qq(\t\t<div class="message">$message</div>\n);
864 sub action_verify_send_mail
867 my $owner = LiCoM::Person->load ($UserCN);
870 my ($owner_mail) = $owner->get ('mail');
873 my $cn_uri = uri_escape ($UserCN);
874 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);
879 for (keys %FieldNames)
881 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
885 my $person_name = $person->name ();
886 my ($person_mail) = $person->get ('mail');
887 my $person_gn = $person->firstname ();
888 my ($password) = $person->get ('password');
890 my $host = $ENV{'HTTP_HOST'};
891 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
893 open ($smh, '|-', '/usr/sbin/sendmail', '-t', '-f', $owner_mail) or die ("open (sendmail): $!");
895 To: $person_name <$person_mail>
896 From: $UserCN <$owner_mail>
897 Subject: Please verify our entry in my address book
901 the following is your entry in my address book:
906 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
907 my @values = $person->get ($field);
911 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
916 If this entry is outdated or incomplete, please take a minute and correct it.
918 Username: $person_name
921 Thank you very much :)
926 This message was automatically generated by LiCoM,
927 http://verplant.org/licom/
936 my $cn = param ('cn');
939 my $person = LiCoM::Person->load ($cn);
942 my $cn_uri = uri_escape ($cn);
943 my $cn_html = encode_entities ($cn);
946 <h2>Really delete $cn_html?</h2>
949 You are about to delete <strong>$cn_html</strong>.
950 Are you totally, absolutely sure you want to do this?
954 [<a href="$MySelf?action=expunge&cn=$cn_uri">Yes, delete</a>]
955 [<a href="$MySelf?action=detail&cn=$cn_uri">No, keep</a>]
963 my $cn = param ('cn');
966 my $cn_html = encode_entities ($cn);
968 my $person = LiCoM::Person->load ($cn);
974 <div>$cn_html has been deleted.</div>
983 $title = q(Lightweight Contact Manager) unless ($title);
985 $title = encode_entities ($title);
988 Content-Type: text/html; charset=UTF-8
992 <title>$title</title>
993 <style type="text/css">
1000 background-color: inherit;
1001 text-decoration: none;
1006 text-decoration: underline;
1012 background-color: inherit;
1018 background-color: white;
1024 background-color: yellow;
1028 border: 2px solid red;
1034 background-color: white;
1044 border-top: 1px solid black;
1051 background-color: inherit;
1052 text-decoration: none;
1057 text-decoration: underline;
1062 border-top: 1px solid black;
1070 background-color: transparent;
1076 padding-bottom: 1ex;
1077 border-bottom: 1px solid black;
1116 background-color: #cccccc;
1117 vertical-align: top;
1123 background-color: #999999;
1126 vertical-align: top;
1135 background-color: inherit;
1136 text-decoration: underline;
1139 div.topmenu, div.menu
1162 border-collapse: collapse;
1177 border-bottom-width: 2px;
1182 border: 1px solid black;
1183 vertical-align: top;
1201 my $search = param ('search') || '';
1202 $search = encode_entities ($search);
1204 <div class="topmenu">
1205 <form action="$MySelf" method="post">
1206 <input type="hidden" name="action" value="browse" />
1207 <input type="submit" name="button" value="Browse" />
1209 <form action="$MySelf" method="post">
1210 <input type="hidden" name="action" value="search" />
1211 <input type="text" name="search" value="$search" />
1212 <input type="submit" name="button" value="Search" />
1214 <form action="$MySelf" method="post">
1215 <input type="hidden" name="action" value="edit" />
1216 <input type="hidden" name="dn" value="" />
1217 <input type="submit" name="button" value="Add New" />
1222 print "\t\t<h1>$title</h1>\n";
1229 "Lightweight Contact Manager",
1230 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1231 <octo at verplant.org>
1240 my $len = @_ ? shift : 6;
1249 while (length ($retval) < $len)
1251 my $chr = int (rand (128));
1253 if ($chr >= 48 and $chr < 58)
1257 elsif ($chr >= 65 and $chr < 91)
1261 elsif ($chr >= 97 and $chr < 123)
1269 $retval .= chr ($chr);
1272 $retval = '' if (!$numbers or !$lchars or !$uchars);
1286 if ($val !~ m#^[a-zA-Z]+://#)
1288 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1293 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1296 next unless ($number);
1298 if ($number !~ m/^\+[0-9 \-]+$/)
1300 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1305 print qq(\t\t<div class="error">\n) if (@errors);
1306 for (my $i = 0; $i < scalar (@errors); $i++)
1308 my $e = $errors[$i];
1310 print "<br />\n" if ($i);
1313 print qq(\n\t\t</div>\n\n) if (@errors);
1315 return (scalar (@errors));
1320 my $contacts = @_ ? shift : {};
1325 my @values = grep { $_ } (param ($field));
1327 next unless (@values);
1329 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1333 $_ =~ s/[^0-9 \-]//g;
1334 $_ = '+' . $_ if ($_);
1338 $contacts->{$field} = [@values] if (@values);