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));
27 use LiCoM::Config (qw(get_config set_config read_config));
28 use LiCoM::Connection ();
34 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
39 homephone => 'Home Phone',
40 cellphone => 'Cell Phone',
41 officephone => 'Office Phone',
44 uri => 'URI (Homepage)',
48 our $MySelf = $ENV{'SCRIPT_NAME'};
50 our $Action = param ('action');
51 $Action ||= 'default';
55 browse => [\&html_start, \&action_browse, \&html_end],
56 default => [\&html_start, \&action_browse, \&html_end],
57 detail => [\&html_start, \&action_detail, \&html_end],
58 edit => [\&html_start, \&action_edit, \&html_end],
59 list => [\&html_start, \&action_list, \&html_end],
60 save => [\&html_start, \&action_save, \&html_end],
61 search => [\&html_start, \&action_search, \&html_end],
62 verify => [\&html_start, \&action_verify, \&html_end],
63 delete => [\&html_start, \&action_ask_del, \&html_end],
64 expunge => [\&html_start, \&action_do_del, \&html_end],
65 vcard => \&action_vcard
70 # make sure AuthLDAPRemoteUserIsDN is enabled.
71 die unless ($ENV{'REMOTE_USER'});
72 set_config ('base_dn', $ENV{'REMOTE_USER'});
74 die unless (defined (get_config ('uri'))
75 and defined (get_config ('base_dn'))
76 and defined (get_config ('bind_dn'))
77 and defined (get_config ('password')));
79 LiCoM::Connection->connect
81 uri => get_config ('uri'),
82 bind_dn => get_config ('bind_dn'),
83 password => get_config ('password')
86 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
88 if (!$UserID and $Action ne 'save')
98 if (!defined ($Actions{$Action}))
103 if (ref ($Actions{$Action}) eq 'CODE')
105 $Actions{$Action}->();
107 elsif (ref ($Actions{$Action}) eq 'ARRAY')
109 for (@{$Actions{$Action}})
115 LiCoM::Connection->disconnect ();
123 my $group = param ('group');
124 $group = shift if (@_);
129 my @groups = LiCoM::Group->all ();
131 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
135 my @members = $group->get_members ();
136 my $members = scalar (@members);
137 my $group_name = $group->name ();
138 my $group_esc = uri_escape ($group_name);
139 my $desc = $group->description ();
141 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group_name</a> ($members Member#, ($members == 1 ? ')' : 's)');
142 print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
147 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
152 [<a href="$MySelf?action=list">List all</a>]
158 my $group_obj = LiCoM::Group->load ($group);
159 my $group_esc = uri_escape ($group_obj->name ());
160 my @member_names = $group_obj->get_members ();
162 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
163 qq(\t\t<ul class="results">\n);
164 for (sort (@member_names))
167 my $cn_esc = uri_escape ($cn);
169 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
175 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
176 [<a href="$MySelf?action=browse">Back</a>]
185 my $group = param ('group');
186 $group = shift if (@_);
189 my $title = $group ? "List of group "$group"" : 'List of all addresses';
190 my @fields = (qw(address homephone cellphone officephone fax mail));
195 @all = LiCoM::Person->search ([[group => $group]]);
199 @all = LiCoM::Person->search ();
211 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
213 print "\t\t\t</tr>\n";
215 for (sort { $a->name () cmp $b->name () } (@all))
218 my $sn = $person->lastname ();
219 my $gn = $person->firstname ();
221 print "\t\t\t<tr>\n",
222 "\t\t\t\t<td>$sn, $gn</td>\n";
227 my @values = $person->get ($field);
228 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
231 print "\t\t\t</tr>\n";
233 print "\t\t</table>\n\n";
237 my $group_esc = uri_escape ($group);
238 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
242 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
248 my $cn = param ('cn');
252 my $person = LiCoM::Person->load ($cn);
255 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
259 print qq(\t\t<h2>Details for $cn</h2>\n);
261 my $cn_esc = uri_escape ($cn);
264 <table class="detail">
273 my $values = $person->get ($field);
274 my $num = scalar (@$values);
275 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
279 print "\t\t\t<tr>\n";
282 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
286 print qq(\t\t\t\t<th>$print</th>\n);
289 for (my $i = 0; $i < $num; $i++)
291 my $val = $values->[$i];
293 if ($field eq 'group')
295 my $val_esc = uri_escape ($val);
296 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
298 elsif ($field eq 'uri')
301 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
302 $val = qq(<a href="$uri" class="extern">$val</a>);
304 elsif ($field eq 'mail')
306 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
309 print "\t\t\t<tr>\n" if ($i);
310 print "\t\t\t\t<td>$val</td>\n",
315 my @groups = LiCoM::Group->load_by_member ($cn);
318 my $num = scalar (@groups);
319 print "\t\t\t<tr>\n",
320 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
321 for (my $i = 0; $i < $num; $i++)
323 my $group = $groups[$i];
324 my $group_name = $group->name ();
325 my $group_esc = uri_escape ($group_name);
327 print "\t\t\t<tr>\n" if ($i != 0);
328 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_esc">$group_name</a></td>\n),
337 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
338 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
339 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
340 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
348 my $search = param ('search');
351 $search =~ s/[^\s\w]//g;
355 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
360 my @patterns = split (m/\s+/, $search);
366 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
369 my @matches = LiCoM::Person->search (@filter);
373 print qq(\t<div>No entries matched your search.</div>\n);
377 if (scalar (@matches) == 1)
379 my $person = shift (@matches);
380 my $cn = $person->name ();
385 print qq(\t<ul class="result">\n);
386 for (sort { $a->name () cmp $b->name () } (@matches))
389 my $cn = $person->name ();
390 my $cn_esc = uri_escape ($cn);
392 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
401 my $cn = param ('cn');
403 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
417 $contacts->{$_} = [] for (@MultiFields);
421 $person = LiCoM::Person->load ($cn);
425 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
429 $lastname = $person->lastname ();
430 $firstname = $person->firstname ();
434 $contacts->{$_} = $person->get ($_);
438 $lastname = param ('lastname') if (param ('lastname') and $UserID);
439 $firstname = param ('firstname') if (param ('firstname') and $UserID);
441 get_contacts ($contacts);
443 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
444 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
448 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
453 print "\t\t<h2>Edit contact $cn</h2>\n";
457 print "\t\t<h2>Create new contact</h2>\n";
461 <form action="$MySelf" method="post">
462 <input type="hidden" name="action" value="save" />
463 <input type="hidden" name="cn" value="$cn" />
470 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
474 print qq(\t\t\t\t<td>$lastname</td>\n);
483 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
487 print qq(\t\t\t\t<td>$firstname</td>\n);
490 print "\t\t\t</tr>\n";
495 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
496 my @values = @{$contacts->{$field}};
498 next if ($field eq 'group');
509 <td><input type="text" name="$field" value="$value" /></td>
517 my @all_groups = LiCoM::Group->all ();
521 print "\t\t\t<tr>\n",
522 "\t\t\t\t<th>Group(s)</th>\n",
523 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
528 my $group_name = $group->name ();
531 if (grep { $cn eq $_ } ($group->get_members ()))
533 $selected = ' selected="selected"';
536 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
538 print "\t\t\t\t</select></td>\n",
542 print "\t\t\t<tr>\n",
543 "\t\t\t\t<th>New Group</th>\n",
544 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
550 <th colspan="2" class="menu">
555 <input type="submit" name="button" value="Cancel" />
556 <input type="submit" name="button" value="Apply" />
560 <input type="submit" name="button" value="Save" />
570 my $cn = $UserID ? param ('cn') : $UserCN;
572 if (verify_fields ())
574 action_edit (cn => $cn);
584 die unless ($UserID);
586 my $button = lc (param ('button'));
589 if ($button eq 'cancel')
595 if (!param ('lastname') or !param ('firstname'))
597 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
598 action_edit (cn => '');
602 my $lastname = param ('lastname');
603 my $firstname = param ('firstname');
605 my $contacts = get_contacts ();
607 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
611 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
615 $cn = $person->name ();
617 for (param ('group'))
620 my $group = LiCoM::Group->load ($group_name);
624 $group->add_members ($cn);
628 print qq(\t<div class="error">Group "$group_name" does not exist or could not be loaded.</div>\n);
632 if (param ('newgroup'))
634 # FIXME add error handling
635 my $group_name = param ('newgroup');
636 LiCoM::Group->create ($group_name, '', $cn);
639 if ($button eq 'apply')
641 action_edit (cn => $cn);
651 my $cn = $UserID ? param ('cn') : $UserCN;
652 my $person = LiCoM::Person->load ($cn);
654 die unless ($person);
656 my $button = lc (param ('button'));
659 if ($UserID and $button eq 'cancel')
667 my $lastname = param ('lastname');
668 my $firstname = param ('firstname');
670 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
671 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
673 $cn = $person->name ();
677 my $contacts = get_contacts ();
683 next if (!$UserID and $field eq 'group');
685 if (defined ($contacts->{$field}))
687 my $values = $contacts->{$field};
688 $person->set ($field, $values);
692 $person->set ($field, []);
696 my %changed_groups = map { $_ => 1 } (param ('group'));
697 my @current_groups = LiCoM::Group->load_by_member ($cn);
699 for (@current_groups)
702 my $group_name = $group_obj->name ();
704 if (!defined ($changed_groups{$group_name}))
706 $group_obj->del_members ($cn);
710 delete ($changed_groups{$group_name});
713 for (keys %changed_groups)
716 my $group_obj = LiCoM::Group->load ($group_name) or die;
718 $group_obj->add_members ($cn);
721 if (param ('newgroup'))
723 # FIXME add error handling
724 my $group_name = param ('newgroup');
725 LiCoM::Group->create ($group_name, '', $cn);
728 if ($button eq 'apply' or !$UserID)
730 action_edit (cn => $cn);
740 my $cn = param ('cn');
744 my $person = LiCoM::Person->load ($cn);
745 die unless ($person);
749 homephone => 'TEL;TYPE=home,voice',
750 cellphone => 'TEL;TYPE=cell',
751 officephone => 'TEL;TYPE=work,voice',
752 fax => 'TEL;TYPE=fax',
758 my $sn = $person->lastname ();
759 my $gn = $person->firstname ();
760 my $cn_esc = uri_escape ($cn);
763 Content-Type: text/x-vcard
764 Content-Disposition: attachment; filename="$cn.vcf"
775 my $vc_fld = $vcard_types{$field};
776 my $values = $person->get ($field);
778 next unless ($vc_fld);
783 print "$vc_fld:$value\n";
791 my $cn = param ('cn');
795 my $person = LiCoM::Person->load ($cn);
796 die unless ($person);
798 my ($mail) = $person->get ('mail');
802 my $password = $person->get ('password');
806 $password = pwgen ();
807 $person->set ('password', $password);
810 $message = qq(The password for the record "$cn" is "$password".);
814 if (action_verify_send_mail ($person))
816 $message .= qq( A request for verification has been sent to $mail.);
821 $message .= q( There was no e-mail address, thus no verification request could be sent.);
824 print qq(\t\t<div class="message">$message</div>\n);
829 sub action_verify_send_mail
832 my $owner = LiCoM::Person->load ($UserCN);
835 my ($owner_mail) = $owner->get ('mail');
838 my $cn = uri_escape ($UserCN);
839 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);
844 for (keys %FieldNames)
846 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
850 my $person_name = $person->name ();
851 my ($person_mail) = $person->get ('mail');
852 my $person_gn = $person->firstname ();
853 my $password = $person->get ('password');
855 my $host = $ENV{'HTTP_HOST'};
856 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
858 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
860 To: $person_name <$person_mail>
861 From: $UserCN <$owner_mail>
862 Subject: Please verify our entry in my address book
866 the following is your entry in my address book:
871 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
872 my @values = $person->get ($field);
876 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
881 If this entry is outdated or incomplete, please take a minute and correct it.
883 Username: $person_name
886 Thank you very much :)
891 This message was automatically generated by LiCoM,
892 http://verplant.org/licom/
901 my $cn = param ('cn');
904 my $person = LiCoM::Person->load ($cn);
907 my $cn_esc = uri_escape ($cn);
910 <h2>Really delete $cn?</h2>
913 You are about to delete <strong>$cn</strong>. Are you
914 totally, absolutely sure you want to do this?
918 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
919 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
927 my $cn = param ('cn');
930 my $person = LiCoM::Person->load ($cn);
936 <div>$cn has been deleted.</div>
945 $title = q(Lightweight Contact Manager) unless ($title);
948 Content-Type: text/html; charset=UTF-8
952 <title>$title</title>
953 <style type="text/css">
960 background-color: inherit;
961 text-decoration: none;
966 text-decoration: underline;
972 background-color: inherit;
978 background-color: white;
984 background-color: yellow;
988 border: 2px solid red;
994 background-color: white;
1004 border-top: 1px solid black;
1011 background-color: inherit;
1012 text-decoration: none;
1017 text-decoration: underline;
1022 border-top: 1px solid black;
1030 background-color: transparent;
1036 padding-bottom: 1ex;
1037 border-bottom: 1px solid black;
1076 background-color: #cccccc;
1077 vertical-align: top;
1083 background-color: #999999;
1086 vertical-align: top;
1095 background-color: inherit;
1096 text-decoration: underline;
1099 div.topmenu, div.menu
1122 border-collapse: collapse;
1137 border-bottom-width: 2px;
1142 border: 1px solid black;
1143 vertical-align: top;
1161 my $search = param ('search') || '';
1163 <div class="topmenu">
1164 <form action="$MySelf" method="post">
1165 <input type="hidden" name="action" value="browse" />
1166 <input type="submit" name="button" value="Browse" />
1168 <form action="$MySelf" method="post">
1169 <input type="hidden" name="action" value="search" />
1170 <input type="text" name="search" value="$search" />
1171 <input type="submit" name="button" value="Search" />
1173 <form action="$MySelf" method="post">
1174 <input type="hidden" name="action" value="edit" />
1175 <input type="hidden" name="dn" value="" />
1176 <input type="submit" name="button" value="Add New" />
1181 print "\t\t<h1>$title</h1>\n";
1188 "Lightweight Contact Manager",
1189 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1190 <octo at verplant.org>
1199 my $len = @_ ? shift : 6;
1208 while (length ($retval) < $len)
1210 my $chr = int (rand (128));
1212 if ($chr >= 48 and $chr < 58)
1216 elsif ($chr >= 65 and $chr < 91)
1220 elsif ($chr >= 97 and $chr < 123)
1228 $retval .= chr ($chr);
1231 $retval = '' if (!$numbers or !$lchars or !$uchars);
1245 if ($val !~ m#^[a-zA-Z]+://#)
1247 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1252 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1255 next unless ($number);
1257 if ($number !~ m/^\+[0-9 \-]+$/)
1259 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1264 print qq(\t\t<div class="error">\n) if (@errors);
1265 for (my $i = 0; $i < scalar (@errors); $i++)
1267 my $e = $errors[$i];
1269 print "<br />\n" if ($i);
1272 print qq(\n\t\t</div>\n\n) if (@errors);
1274 return (scalar (@errors));
1279 my $contacts = @_ ? shift : {};
1284 my @values = grep { $_ } (param ($field));
1286 next unless (@values);
1288 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1292 $_ =~ s/[^0-9 \-]//g;
1293 $_ = '+' . $_ if ($_);
1297 $contacts->{$field} = [@values] if (@values);