8 use CGI::Carp (qw(fatalsToBrowser));
12 use LiCoM::Config (qw(get_config set_config read_config));
13 use LiCoM::Connection ();
19 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
24 homephone => 'Home Phone',
25 cellphone => 'Cell Phone',
26 officephone => 'Office Phone',
29 uri => 'URI (Homepage)',
33 our $MySelf = $ENV{'SCRIPT_NAME'};
35 our $Action = param ('action');
36 $Action ||= 'default';
40 browse => [\&html_start, \&action_browse, \&html_end],
41 default => [\&html_start, \&action_browse, \&html_end],
42 detail => [\&html_start, \&action_detail, \&html_end],
43 edit => [\&html_start, \&action_edit, \&html_end],
44 list => [\&html_start, \&action_list, \&html_end],
45 save => [\&html_start, \&action_save, \&html_end],
46 search => [\&html_start, \&action_search, \&html_end],
47 verify => [\&html_start, \&action_verify, \&html_end],
48 delete => [\&html_start, \&action_ask_del, \&html_end],
49 expunge => [\&html_start, \&action_do_del, \&html_end],
50 vcard => \&action_vcard
55 # make sure AuthLDAPRemoteUserIsDN is enabled.
56 die unless ($ENV{'REMOTE_USER'});
57 set_config ('base_dn', $ENV{'REMOTE_USER'});
59 die unless (defined (get_config ('uri'))
60 and defined (get_config ('base_dn'))
61 and defined (get_config ('bind_dn'))
62 and defined (get_config ('password')));
64 LiCoM::Connection->connect
66 uri => get_config ('uri'),
67 bind_dn => get_config ('bind_dn'),
68 password => get_config ('password')
71 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
73 if (!$UserID and $Action ne 'save')
83 if (!defined ($Actions{$Action}))
88 if (ref ($Actions{$Action}) eq 'CODE')
90 $Actions{$Action}->();
92 elsif (ref ($Actions{$Action}) eq 'ARRAY')
94 for (@{$Actions{$Action}})
100 LiCoM::Connection->disconnect ();
108 my $group = param ('group');
109 $group = shift if (@_);
114 my @groups = LiCoM::Group->all ();
116 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
120 my @members = $group->get_members ();
121 my $members = scalar (@members);
122 my $group_name = $group->name ();
123 my $group_esc = uri_escape ($group_name);
124 my $desc = $group->description ();
126 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group_name</a> ($members Members)#;
127 print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
132 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
137 [<a href="$MySelf?action=list">List all</a>]
143 my $group_obj = LiCoM::Group->load ($group);
144 my $group_esc = uri_escape ($group_obj->name ());
145 my @member_names = $group_obj->get_members ();
147 print qq(\t\t<h2>Contact Group "$group"</h2>\n),
148 qq(\t\t<ul class="results">\n);
149 for (sort (@member_names))
152 my $cn_esc = uri_escape ($cn);
154 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
160 [<a href="$MySelf?action=list&group=$group_esc">List</a>]
161 [<a href="$MySelf?action=browse">Back</a>]
170 my $group = param ('group');
171 $group = shift if (@_);
174 my $title = $group ? "List of group "$group"" : 'List of all addresses';
175 my @fields = (qw(address homephone cellphone officephone fax mail));
180 @all = LiCoM::Person->search ([[group => $group]]);
184 @all = LiCoM::Person->search ();
196 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
198 print "\t\t\t</tr>\n";
200 for (sort { $a->name () cmp $b->name () } (@all))
203 my $sn = $person->lastname ();
204 my $gn = $person->firstname ();
206 print "\t\t\t<tr>\n",
207 "\t\t\t\t<td>$sn, $gn</td>\n";
212 my @values = $person->get ($field);
213 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
216 print "\t\t\t</tr>\n";
218 print "\t\t</table>\n\n";
222 my $group_esc = uri_escape ($group);
223 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
227 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
233 my $cn = param ('cn');
237 my $person = LiCoM::Person->load ($cn);
240 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
244 print qq(\t\t<h2>Details for $cn</h2>\n);
246 my $cn_esc = uri_escape ($cn);
249 <table class="detail">
258 my $values = $person->get ($field);
259 my $num = scalar (@$values);
260 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
264 print "\t\t\t<tr>\n";
267 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
271 print qq(\t\t\t\t<th>$print</th>\n);
274 for (my $i = 0; $i < $num; $i++)
276 my $val = $values->[$i];
278 if ($field eq 'group')
280 my $val_esc = uri_escape ($val);
281 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
283 elsif ($field eq 'uri')
286 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
287 $val = qq(<a href="$uri" class="extern">$val</a>);
289 elsif ($field eq 'mail')
291 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
294 print "\t\t\t<tr>\n" if ($i);
295 print "\t\t\t\t<td>$val</td>\n",
303 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
304 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
305 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
306 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
314 my $search = param ('search');
317 $search =~ s/[^\s\w]//g;
321 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
326 my @patterns = split (m/\s+/, $search);
332 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
335 my @matches = LiCoM::Person->search (@filter);
339 print qq(\t<div>No entries matched your search.</div>\n);
343 if (scalar (@matches) == 1)
345 my $person = shift (@matches);
346 my $cn = $person->name ();
351 print qq(\t<ul class="result">\n);
352 for (sort { $a->name () cmp $b->name () } (@matches))
355 my $cn = $person->name ();
356 my $cn_esc = uri_escape ($cn);
358 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
367 my $cn = param ('cn');
369 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
383 $contacts->{$_} = [] for (@MultiFields);
387 $person = LiCoM::Person->load ($cn);
391 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
395 $lastname = $person->lastname ();
396 $firstname = $person->firstname ();
400 $contacts->{$_} = $person->get ($_);
404 $lastname = param ('lastname') if (param ('lastname') and $UserID);
405 $firstname = param ('firstname') if (param ('firstname') and $UserID);
407 get_contacts ($contacts);
409 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
410 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
414 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
419 print "\t\t<h2>Edit contact $cn</h2>\n";
423 print "\t\t<h2>Create new contact</h2>\n";
427 <form action="$MySelf" method="post">
428 <input type="hidden" name="action" value="save" />
429 <input type="hidden" name="cn" value="$cn" />
436 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
440 print qq(\t\t\t\t<td>$lastname</td>\n);
449 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
453 print qq(\t\t\t\t<td>$firstname</td>\n);
456 print "\t\t\t</tr>\n";
461 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
462 my @values = @{$contacts->{$field}};
464 next if ($field eq 'group');
475 <td><input type="text" name="$field" value="$value" /></td>
483 my @all_groups = LiCoM::Group->all ();
487 print "\t\t\t<tr>\n",
488 "\t\t\t\t<th>Group(s)</th>\n",
489 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
494 my $group_name = $group->name ();
497 if (grep { $cn eq $_ } ($group->get_members ()))
499 $selected = ' selected="selected"';
502 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
504 print "\t\t\t\t</select></td>\n",
508 print "\t\t\t<tr>\n",
509 "\t\t\t\t<th>New Group</th>\n",
510 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
516 <th colspan="2" class="menu">
521 <input type="submit" name="button" value="Cancel" />
522 <input type="submit" name="button" value="Apply" />
526 <input type="submit" name="button" value="Save" />
536 my $cn = $UserID ? param ('cn') : $UserCN;
538 if (verify_fields ())
540 action_edit (cn => $cn);
550 die unless ($UserID);
552 my $button = lc (param ('button'));
555 if ($button eq 'cancel')
561 if (!param ('lastname') or !param ('firstname'))
563 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
564 action_edit (cn => '');
568 my $lastname = param ('lastname');
569 my $firstname = param ('firstname');
571 my $contacts = get_contacts ();
573 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
577 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
581 $cn = $person->name ();
583 for (param ('group'))
586 my $group = LiCoM::Group->load ($group_name);
590 $group->add_members ($cn);
594 print qq(\t<div class="error">Group "$group_name" does not exist or could not be loaded.</div>\n);
598 if (param ('newgroup'))
600 # FIXME add error handling
601 my $group_name = param ('newgroup');
602 LiCoM::Group->create ($group_name, '', $cn);
605 if ($button eq 'apply')
607 action_edit (cn => $cn);
617 my $cn = $UserID ? param ('cn') : $UserCN;
618 my $person = LiCoM::Person->load ($cn);
620 die unless ($person);
622 my $button = lc (param ('button'));
625 if ($UserID and $button eq 'cancel')
633 my $lastname = param ('lastname');
634 my $firstname = param ('firstname');
636 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
637 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
639 $cn = $person->name ();
643 my $contacts = get_contacts ();
649 next if (!$UserID and $field eq 'group');
651 if (defined ($contacts->{$field}))
653 my $values = $contacts->{$field};
654 $person->set ($field, $values);
658 $person->set ($field, []);
662 my %changed_groups = map { $_ => 1 } (param ('group'));
663 my @current_groups = LiCoM::Group->load_by_member ($cn);
665 for (@current_groups)
668 my $group_name = $group_obj->name ();
670 if (!defined ($changed_groups{$group_name}))
672 $group_obj->del_members ($cn);
676 delete ($changed_groups{$group_name});
679 for (keys %changed_groups)
682 my $group_obj = LiCoM::Group->load ($group_name) or die;
684 $group_obj->add_members ($cn);
687 if (param ('newgroup'))
689 # FIXME add error handling
690 my $group_name = param ('newgroup');
691 LiCoM::Group->create ($group_name, '', $cn);
694 if ($button eq 'apply' or !$UserID)
696 action_edit (cn => $cn);
706 my $cn = param ('cn');
710 my $person = LiCoM::Person->load ($cn);
711 die unless ($person);
715 homephone => 'TEL;TYPE=home,voice',
716 cellphone => 'TEL;TYPE=cell',
717 officephone => 'TEL;TYPE=work,voice',
718 fax => 'TEL;TYPE=fax',
724 my $sn = $person->lastname ();
725 my $gn = $person->firstname ();
726 my $cn_esc = uri_escape ($cn);
729 Content-Type: text/x-vcard
730 Content-Disposition: attachment; filename="$cn.vcf"
741 my $vc_fld = $vcard_types{$field};
742 my $values = $person->get ($field);
744 next unless ($vc_fld);
749 print "$vc_fld:$value\n";
757 my $cn = param ('cn');
761 my $person = LiCoM::Person->load ($cn);
762 die unless ($person);
764 my ($mail) = $person->get ('mail');
768 my $password = $person->get ('password');
772 $password = pwgen ();
773 $person->set ('password', $password);
776 $message = qq(The password for the record "$cn" is "$password".);
780 if (action_verify_send_mail ($person))
782 $message .= qq( A request for verification has been sent to $mail.);
787 $message .= q( There was no e-mail address, thus no verification request could be sent.);
790 print qq(\t\t<div class="message">$message</div>\n);
795 sub action_verify_send_mail
798 my $owner = LiCoM::Person->load ($UserCN);
801 my ($owner_mail) = $owner->get ('mail');
804 my $cn = uri_escape ($UserCN);
805 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);
810 for (keys %FieldNames)
812 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
816 my $person_name = $person->name ();
817 my ($person_mail) = $person->get ('mail');
818 my $person_gn = $person->firstname ();
819 my $password = $person->get ('password');
821 my $host = $ENV{'HTTP_HOST'};
822 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
824 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
826 To: $person_name <$person_mail>
827 From: $UserCN <$owner_mail>
828 Subject: Please verify our entry in my address book
832 the following is your entry in my address book:
837 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
838 my @values = $person->get ($field);
842 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
847 If this entry is outdated or incomplete, please take a minute and correct it.
849 Username: $person_name
852 Thank you very much :)
857 This message was automatically generated by LiCoM,
858 http://verplant.org/licom/
867 my $cn = param ('cn');
870 my $person = LiCoM::Person->load ($cn);
873 my $cn_esc = uri_escape ($cn);
876 <h2>Really delete $cn?</h2>
879 You are about to delete <strong>$cn</strong>. Are you
880 totally, absolutely sure you want to do this?
884 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
885 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
893 my $cn = param ('cn');
896 my $person = LiCoM::Person->load ($cn);
902 <div>$cn has been deleted.</div>
911 $title = q(Lightweight Contact Manager) unless ($title);
914 Content-Type: text/html; charset=UTF-8
918 <title>$title</title>
919 <style type="text/css">
926 background-color: inherit;
927 text-decoration: none;
932 text-decoration: underline;
938 background-color: inherit;
944 background-color: white;
950 background-color: yellow;
954 border: 2px solid red;
960 background-color: white;
970 border-top: 1px solid black;
977 background-color: inherit;
978 text-decoration: none;
983 text-decoration: underline;
988 border-top: 1px solid black;
996 background-color: transparent;
1002 padding-bottom: 1ex;
1003 border-bottom: 1px solid black;
1042 background-color: #cccccc;
1043 vertical-align: top;
1049 background-color: #999999;
1052 vertical-align: top;
1061 background-color: inherit;
1062 text-decoration: underline;
1065 div.topmenu, div.menu
1088 border-collapse: collapse;
1103 border-bottom-width: 2px;
1108 border: 1px solid black;
1109 vertical-align: top;
1127 my $search = param ('search') || '';
1129 <div class="topmenu">
1130 <form action="$MySelf" method="post">
1131 <input type="hidden" name="action" value="browse" />
1132 <input type="submit" name="button" value="Browse" />
1134 <form action="$MySelf" method="post">
1135 <input type="hidden" name="action" value="search" />
1136 <input type="text" name="search" value="$search" />
1137 <input type="submit" name="button" value="Search" />
1139 <form action="$MySelf" method="post">
1140 <input type="hidden" name="action" value="edit" />
1141 <input type="hidden" name="dn" value="" />
1142 <input type="submit" name="button" value="Add New" />
1147 print "\t\t<h1>$title</h1>\n";
1154 "Lightweight Contact Manager",
1155 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1156 <octo at verplant.org>
1165 my $len = @_ ? shift : 6;
1174 while (length ($retval) < $len)
1176 my $chr = int (rand (128));
1178 if ($chr >= 48 and $chr < 58)
1182 elsif ($chr >= 65 and $chr < 91)
1186 elsif ($chr >= 97 and $chr < 123)
1194 $retval .= chr ($chr);
1197 $retval = '' if (!$numbers or !$lchars or !$uchars);
1211 if ($val !~ m#^[a-zA-Z]+://#)
1213 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1218 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1221 next unless ($number);
1223 if ($number !~ m/^\+[0-9 \-]+$/)
1225 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1230 print qq(\t\t<div class="error">\n) if (@errors);
1231 for (my $i = 0; $i < scalar (@errors); $i++)
1233 my $e = $errors[$i];
1235 print "<br />\n" if ($i);
1238 print qq(\n\t\t</div>\n\n) if (@errors);
1240 return (scalar (@errors));
1245 my $contacts = @_ ? shift : {};
1250 my @values = grep { $_ } (param ($field));
1252 next unless (@values);
1254 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1258 $_ =~ s/[^0-9 \-]//g;
1259 $_ = '+' . $_ if ($_);
1263 $contacts->{$field} = [@values] if (@values);