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));
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">$group_name</a> ($members Member#, ($members == 1 ? ')' : 's)');
143 print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
148 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
153 [<a href="$MySelf?action=list">List all</a>]
159 my $group_obj = LiCoM::Group->load ($group);
160 my $group_esc = uri_escape ($group_obj->name ());
161 my @member_names = $group_obj->get_members ();
163 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
164 qq(\t\t<ul class="results">\n);
165 for (sort (@member_names))
168 my $cn_esc = uri_escape ($cn);
170 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
176 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
177 [<a href="$MySelf?action=browse">Back</a>]
186 my $group = param ('group');
187 $group = shift if (@_);
190 my $title = $group ? "List of group "$group"" : 'List of all addresses';
191 my @fields = (qw(address homephone cellphone officephone fax mail));
196 @all = LiCoM::Person->search ([[group => $group]]);
200 @all = LiCoM::Person->search ();
212 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
214 print "\t\t\t</tr>\n";
216 for (sort { $a->name () cmp $b->name () } (@all))
219 my $sn = $person->lastname ();
220 my $gn = $person->firstname ();
222 print "\t\t\t<tr>\n",
223 "\t\t\t\t<td>$sn, $gn</td>\n";
228 my @values = $person->get ($field);
229 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
232 print "\t\t\t</tr>\n";
234 print "\t\t</table>\n\n";
238 my $group_esc = uri_escape ($group);
239 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
243 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
249 my $cn = param ('cn');
253 my $person = LiCoM::Person->load ($cn);
256 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
260 print qq(\t\t<h2>Details for $cn</h2>\n);
262 my $cn_esc = uri_escape ($cn);
265 <table class="detail">
274 my $values = $person->get ($field);
275 my $num = scalar (@$values);
276 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
280 print "\t\t\t<tr>\n";
283 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
287 print qq(\t\t\t\t<th>$print</th>\n);
290 for (my $i = 0; $i < $num; $i++)
292 my $val = $values->[$i];
294 if ($field eq 'group')
296 my $val_esc = uri_escape ($val);
297 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
299 elsif ($field eq 'uri')
302 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
303 $val = qq(<a href="$uri" class="extern">$val</a>);
305 elsif ($field eq 'mail')
307 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
310 print "\t\t\t<tr>\n" if ($i);
311 print "\t\t\t\t<td>$val</td>\n",
316 my @groups = LiCoM::Group->load_by_member ($cn);
319 my $num = scalar (@groups);
320 print "\t\t\t<tr>\n",
321 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
322 for (my $i = 0; $i < $num; $i++)
324 my $group = $groups[$i];
325 my $group_name = $group->name ();
326 my $group_esc = uri_escape ($group_name);
328 print "\t\t\t<tr>\n" if ($i != 0);
329 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_esc">$group_name</a></td>\n),
338 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
339 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
340 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
341 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
349 my $search = param ('search');
352 $search =~ s/[^\s\w]//g;
356 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
361 my @patterns = split (m/\s+/, $search);
367 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
370 my @matches = LiCoM::Person->search (@filter);
374 print qq(\t<div>No entries matched your search.</div>\n);
378 if (scalar (@matches) == 1)
380 my $person = shift (@matches);
381 my $cn = $person->name ();
386 print qq(\t<ul class="result">\n);
387 for (sort { $a->name () cmp $b->name () } (@matches))
390 my $cn = $person->name ();
391 my $cn_esc = uri_escape ($cn);
393 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
402 my $cn = param ('cn');
404 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
418 $contacts->{$_} = [] for (@MultiFields);
422 $person = LiCoM::Person->load ($cn);
426 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
430 $lastname = $person->lastname ();
431 $firstname = $person->firstname ();
435 $contacts->{$_} = $person->get ($_);
439 $lastname = param ('lastname') if (param ('lastname') and $UserID);
440 $firstname = param ('firstname') if (param ('firstname') and $UserID);
442 get_contacts ($contacts);
444 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
445 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
449 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
454 print "\t\t<h2>Edit contact $cn</h2>\n";
458 print "\t\t<h2>Create new contact</h2>\n";
462 <form action="$MySelf" method="post">
463 <input type="hidden" name="action" value="save" />
464 <input type="hidden" name="cn" value="$cn" />
471 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
475 print qq(\t\t\t\t<td>$lastname</td>\n);
484 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
488 print qq(\t\t\t\t<td>$firstname</td>\n);
491 print "\t\t\t</tr>\n";
496 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
497 my @values = @{$contacts->{$field}};
499 next if ($field eq 'group');
510 <td><input type="text" name="$field" value="$value" /></td>
518 my @all_groups = LiCoM::Group->all ();
522 print "\t\t\t<tr>\n",
523 "\t\t\t\t<th>Group(s)</th>\n",
524 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
529 my $group_name = $group->name ();
532 if (grep { $cn eq $_ } ($group->get_members ()))
534 $selected = ' selected="selected"';
537 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
539 print "\t\t\t\t</select></td>\n",
543 print "\t\t\t<tr>\n",
544 "\t\t\t\t<th>New Group</th>\n",
545 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
551 <th colspan="2" class="menu">
556 <input type="submit" name="button" value="Cancel" />
557 <input type="submit" name="button" value="Apply" />
561 <input type="submit" name="button" value="Save" />
571 my $cn = $UserID ? param ('cn') : $UserCN;
573 if (verify_fields ())
575 action_edit (cn => $cn);
585 die unless ($UserID);
587 my $button = lc (param ('button'));
590 if ($button eq 'cancel')
596 if (!param ('lastname') or !param ('firstname'))
598 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
599 action_edit (cn => '');
603 my $lastname = param ('lastname');
604 my $firstname = param ('firstname');
606 my $contacts = get_contacts ();
608 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
612 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
616 $cn = $person->name ();
618 for (param ('group'))
621 my $group = LiCoM::Group->load ($group_name);
625 $group->add_members ($cn);
629 print qq(\t<div class="error">Group "$group_name" does not exist or could not be loaded.</div>\n);
633 if (param ('newgroup'))
635 # FIXME add error handling
636 my $group_name = param ('newgroup');
637 LiCoM::Group->create ($group_name, '', $cn);
640 if ($button eq 'apply')
642 action_edit (cn => $cn);
652 my $cn = $UserID ? param ('cn') : $UserCN;
653 my $person = LiCoM::Person->load ($cn);
655 die unless ($person);
657 my $button = lc (param ('button'));
660 if ($UserID and $button eq 'cancel')
668 my $lastname = param ('lastname');
669 my $firstname = param ('firstname');
671 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
672 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
674 $cn = $person->name ();
678 my $contacts = get_contacts ();
684 next if (!$UserID and $field eq 'group');
686 if (defined ($contacts->{$field}))
688 my $values = $contacts->{$field};
689 $person->set ($field, $values);
693 $person->set ($field, []);
697 my %changed_groups = map { $_ => 1 } (param ('group'));
698 my @current_groups = LiCoM::Group->load_by_member ($cn);
700 for (@current_groups)
703 my $group_name = $group_obj->name ();
705 if (!defined ($changed_groups{$group_name}))
707 $group_obj->del_members ($cn);
711 delete ($changed_groups{$group_name});
714 for (keys %changed_groups)
717 my $group_obj = LiCoM::Group->load ($group_name) or die;
719 $group_obj->add_members ($cn);
722 if (param ('newgroup'))
724 # FIXME add error handling
725 my $group_name = param ('newgroup');
726 LiCoM::Group->create ($group_name, '', $cn);
729 if ($button eq 'apply' or !$UserID)
731 action_edit (cn => $cn);
741 my $cn = param ('cn');
745 my $person = LiCoM::Person->load ($cn);
746 die unless ($person);
750 homephone => 'TEL;TYPE=home,voice',
751 cellphone => 'TEL;TYPE=cell',
752 officephone => 'TEL;TYPE=work,voice',
753 fax => 'TEL;TYPE=fax',
759 my $sn = $person->lastname ();
760 my $gn = $person->firstname ();
761 my $cn_esc = uri_escape ($cn);
764 Content-Type: text/x-vcard
765 Content-Disposition: attachment; filename="$cn.vcf"
776 my $vc_fld = $vcard_types{$field};
777 my $values = $person->get ($field);
779 next unless ($vc_fld);
784 print "$vc_fld:$value\n";
792 my $cn = param ('cn');
796 my $person = LiCoM::Person->load ($cn);
797 die unless ($person);
799 my ($mail) = $person->get ('mail');
803 my $password = $person->get ('password');
807 $password = pwgen ();
808 $person->set ('password', $password);
811 $message = qq(The password for the record "$cn" is "$password".);
815 if (action_verify_send_mail ($person))
817 $message .= qq( A request for verification has been sent to $mail.);
822 $message .= q( There was no e-mail address, thus no verification request could be sent.);
825 print qq(\t\t<div class="message">$message</div>\n);
830 sub action_verify_send_mail
833 my $owner = LiCoM::Person->load ($UserCN);
836 my ($owner_mail) = $owner->get ('mail');
839 my $cn = uri_escape ($UserCN);
840 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);
845 for (keys %FieldNames)
847 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
851 my $person_name = $person->name ();
852 my ($person_mail) = $person->get ('mail');
853 my $person_gn = $person->firstname ();
854 my $password = $person->get ('password');
856 my $host = $ENV{'HTTP_HOST'};
857 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
859 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
861 To: $person_name <$person_mail>
862 From: $UserCN <$owner_mail>
863 Subject: Please verify our entry in my address book
867 the following is your entry in my address book:
872 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
873 my @values = $person->get ($field);
877 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
882 If this entry is outdated or incomplete, please take a minute and correct it.
884 Username: $person_name
887 Thank you very much :)
892 This message was automatically generated by LiCoM,
893 http://verplant.org/licom/
902 my $cn = param ('cn');
905 my $person = LiCoM::Person->load ($cn);
908 my $cn_esc = uri_escape ($cn);
911 <h2>Really delete $cn?</h2>
914 You are about to delete <strong>$cn</strong>. Are you
915 totally, absolutely sure you want to do this?
919 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
920 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
928 my $cn = param ('cn');
931 my $person = LiCoM::Person->load ($cn);
937 <div>$cn has been deleted.</div>
946 $title = q(Lightweight Contact Manager) unless ($title);
949 Content-Type: text/html; charset=UTF-8
953 <title>$title</title>
954 <style type="text/css">
961 background-color: inherit;
962 text-decoration: none;
967 text-decoration: underline;
973 background-color: inherit;
979 background-color: white;
985 background-color: yellow;
989 border: 2px solid red;
995 background-color: white;
1005 border-top: 1px solid black;
1012 background-color: inherit;
1013 text-decoration: none;
1018 text-decoration: underline;
1023 border-top: 1px solid black;
1031 background-color: transparent;
1037 padding-bottom: 1ex;
1038 border-bottom: 1px solid black;
1077 background-color: #cccccc;
1078 vertical-align: top;
1084 background-color: #999999;
1087 vertical-align: top;
1096 background-color: inherit;
1097 text-decoration: underline;
1100 div.topmenu, div.menu
1123 border-collapse: collapse;
1138 border-bottom-width: 2px;
1143 border: 1px solid black;
1144 vertical-align: top;
1162 my $search = param ('search') || '';
1164 <div class="topmenu">
1165 <form action="$MySelf" method="post">
1166 <input type="hidden" name="action" value="browse" />
1167 <input type="submit" name="button" value="Browse" />
1169 <form action="$MySelf" method="post">
1170 <input type="hidden" name="action" value="search" />
1171 <input type="text" name="search" value="$search" />
1172 <input type="submit" name="button" value="Search" />
1174 <form action="$MySelf" method="post">
1175 <input type="hidden" name="action" value="edit" />
1176 <input type="hidden" name="dn" value="" />
1177 <input type="submit" name="button" value="Add New" />
1182 print "\t\t<h1>$title</h1>\n";
1189 "Lightweight Contact Manager",
1190 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1191 <octo at verplant.org>
1200 my $len = @_ ? shift : 6;
1209 while (length ($retval) < $len)
1211 my $chr = int (rand (128));
1213 if ($chr >= 48 and $chr < 58)
1217 elsif ($chr >= 65 and $chr < 91)
1221 elsif ($chr >= 97 and $chr < 123)
1229 $retval .= chr ($chr);
1232 $retval = '' if (!$numbers or !$lchars or !$uchars);
1246 if ($val !~ m#^[a-zA-Z]+://#)
1248 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1253 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1256 next unless ($number);
1258 if ($number !~ m/^\+[0-9 \-]+$/)
1260 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1265 print qq(\t\t<div class="error">\n) if (@errors);
1266 for (my $i = 0; $i < scalar (@errors); $i++)
1268 my $e = $errors[$i];
1270 print "<br />\n" if ($i);
1273 print qq(\n\t\t</div>\n\n) if (@errors);
1275 return (scalar (@errors));
1280 my $contacts = @_ ? shift : {};
1285 my @values = grep { $_ } (param ($field));
1287 next unless (@values);
1289 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1293 $_ =~ s/[^0-9 \-]//g;
1294 $_ = '+' . $_ if ($_);
1298 $contacts->{$field} = [@values] if (@values);