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 (@_);
117 @all = LiCoM::Person->search ([[group => $group]]);
121 @all = LiCoM::Person->search ();
131 my @g = $person->get ('group');
133 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
135 push (@nogroup, $person) if (!@g);
139 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
140 for (sort (keys (%groups)))
143 my $group_esc = uri_escape ($group);
144 my $num = $groups{$group};
146 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
150 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
152 print qq(\t\t</ul>\n\n);
157 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
161 print qq(\t\t<h2>Contacts without a group</h2>\n);
164 print qq(\t\t<ul class="results">\n);
165 for (sort { $a->name () cmp $b->name () } (@all))
168 my $cn = $person->name ();
169 my $cn_esc = uri_escape ($cn);
171 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
175 print "\t\t\t<li>There are no matching entries.</li>\n";
177 print qq(\t\t</ul>\n\n);
179 print qq(\t\t<div class="menu">\n);
182 my $group_esc = uri_escape ($group);
183 print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
184 qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
188 print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
190 print qq(\t\t</div>\n);
195 my $group = param ('group');
196 $group = shift if (@_);
199 my $title = $group ? "List of group "$group"" : 'List of all addresses';
200 my @fields = (qw(address homephone cellphone officephone fax mail));
205 @all = LiCoM::Person->search ([[group => $group]]);
209 @all = LiCoM::Person->search ();
221 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
223 print "\t\t\t</tr>\n";
225 for (sort { $a->name () cmp $b->name () } (@all))
228 my $sn = $person->lastname ();
229 my $gn = $person->firstname ();
231 print "\t\t\t<tr>\n",
232 "\t\t\t\t<td>$sn, $gn</td>\n";
237 my @values = $person->get ($field);
238 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
241 print "\t\t\t</tr>\n";
243 print "\t\t</table>\n\n";
247 my $group_esc = uri_escape ($group);
248 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
252 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
258 my $cn = param ('cn');
262 my $person = LiCoM::Person->load ($cn);
265 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
269 print qq(\t\t<h2>Details for $cn</h2>\n);
271 my $cn_esc = uri_escape ($cn);
274 <table class="detail">
283 my $values = $person->get ($field);
284 my $num = scalar (@$values);
285 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
289 print "\t\t\t<tr>\n";
292 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
296 print qq(\t\t\t\t<th>$print</th>\n);
299 for (my $i = 0; $i < $num; $i++)
301 my $val = $values->[$i];
303 if ($field eq 'group')
305 my $val_esc = uri_escape ($val);
306 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
308 elsif ($field eq 'uri')
311 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
312 $val = qq(<a href="$uri" class="extern">$val</a>);
314 elsif ($field eq 'mail')
316 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
319 print "\t\t\t<tr>\n" if ($i);
320 print "\t\t\t\t<td>$val</td>\n",
328 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
329 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
330 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
331 [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
339 my $search = param ('search');
342 $search =~ s/[^\s\w]//g;
346 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
351 my @patterns = split (m/\s+/, $search);
357 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
360 my @matches = LiCoM::Person->search (@filter);
364 print qq(\t<div>No entries matched your search.</div>\n);
368 if (scalar (@matches) == 1)
370 my $person = shift (@matches);
371 my $cn = $person->name ();
376 print qq(\t<ul class="result">\n);
377 for (sort { $a->name () cmp $b->name () } (@matches))
380 my $cn = $person->name ();
381 my $cn_esc = uri_escape ($cn);
383 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
392 my $cn = param ('cn');
394 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
408 $contacts->{$_} = [] for (@MultiFields);
412 $person = LiCoM::Person->load ($cn);
416 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
420 $lastname = $person->lastname ();
421 $firstname = $person->firstname ();
425 $contacts->{$_} = $person->get ($_);
429 $lastname = param ('lastname') if (param ('lastname') and $UserID);
430 $firstname = param ('firstname') if (param ('firstname') and $UserID);
432 get_contacts ($contacts);
434 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
435 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
439 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
444 print "\t\t<h2>Edit contact $cn</h2>\n";
448 print "\t\t<h2>Create new contact</h2>\n";
452 <form action="$MySelf" method="post">
453 <input type="hidden" name="action" value="save" />
454 <input type="hidden" name="cn" value="$cn" />
461 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
465 print qq(\t\t\t\t<td>$lastname</td>\n);
474 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
478 print qq(\t\t\t\t<td>$firstname</td>\n);
481 print "\t\t\t</tr>\n";
486 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
487 my @values = @{$contacts->{$field}};
489 next if ($field eq 'group');
500 <td><input type="text" name="$field" value="$value" /></td>
508 my %c_groups = map { $_ => 1 } (@{$contacts->{'group'}});
510 my @a_persons = LiCoM::Person->search ();
514 $a_groups{$_} = 1 for ($_->get ('group'));
517 print "\t\t\t<tr>\n",
518 "\t\t\t\t<th>", $FieldNames{'group'}, "</th>\n",
519 qq(\t\t\t\t<td><select name="group" multiple="multiple">\n);
520 for (sort (keys %a_groups))
523 my $selec = defined ($c_groups{$group}) ? ' selected="selected"' : '';
525 print qq(\t\t\t\t\t<option value="$group"$selec>$group</option>\n);
527 print "\t\t\t\t</select></td>\n",
533 <th colspan="2" class="menu">
538 <input type="submit" name="button" value="Cancel" />
539 <input type="submit" name="button" value="Apply" />
543 <input type="submit" name="button" value="Save" />
553 my $cn = $UserID ? param ('cn') : $UserCN;
555 if (verify_fields ())
557 action_edit (cn => $cn);
567 die unless ($UserID);
569 my $button = lc (param ('button'));
572 if ($button eq 'cancel')
578 if (!param ('lastname') or !param ('firstname'))
580 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
581 action_edit (cn => '');
585 my $lastname = param ('lastname');
586 my $firstname = param ('firstname');
588 my $contacts = get_contacts ();
590 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
594 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
598 $cn = $person->name ();
600 if ($button eq 'apply')
602 action_edit (cn => $cn);
612 my $cn = $UserID ? param ('cn') : $UserCN;
613 my $person = LiCoM::Person->load ($cn);
615 die unless ($person);
617 my $button = lc (param ('button'));
620 if ($UserID and $button eq 'cancel')
628 my $lastname = param ('lastname');
629 my $firstname = param ('firstname');
631 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
632 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
634 $cn = $person->name ();
637 my $contacts = get_contacts ();
643 next if (!$UserID and $field eq 'group');
645 if (defined ($contacts->{$field}))
647 my $values = $contacts->{$field};
648 $person->set ($field, $values);
652 $person->set ($field, []);
656 if ($button eq 'apply' or !$UserID)
658 action_edit (cn => $cn);
668 my $cn = param ('cn');
672 my $person = LiCoM::Person->load ($cn);
673 die unless ($person);
677 homephone => 'TEL;TYPE=home,voice',
678 cellphone => 'TEL;TYPE=cell',
679 officephone => 'TEL;TYPE=work,voice',
680 fax => 'TEL;TYPE=fax',
686 my $sn = $person->lastname ();
687 my $gn = $person->firstname ();
688 my $cn_esc = uri_escape ($cn);
691 Content-Type: text/x-vcard
692 Content-Disposition: attachment; filename="$cn.vcf"
703 my $vc_fld = $vcard_types{$field};
704 my $values = $person->get ($field);
706 next unless ($vc_fld);
711 print "$vc_fld:$value\n";
719 my $cn = param ('cn');
723 my $person = LiCoM::Person->load ($cn);
724 die unless ($person);
726 my ($mail) = $person->get ('mail');
730 my $password = $person->get ('password');
734 $password = pwgen ();
735 $person->set ('password', $password);
738 $message = qq(The password for the record "$cn" is "$password".);
742 if (action_verify_send_mail ($person))
744 $message .= qq( A request for verification has been sent to $mail.);
749 $message .= q( There was no e-mail address, thus no verification request could be sent.);
752 print qq(\t\t<div class="message">$message</div>\n);
757 sub action_verify_send_mail
760 my $owner = LiCoM::Person->load ($UserCN);
763 my ($owner_mail) = $owner->get ('mail');
766 my $cn = uri_escape ($UserCN);
767 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);
772 for (keys %FieldNames)
774 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
778 my $person_name = $person->name ();
779 my ($person_mail) = $person->get ('mail');
780 my $person_gn = $person->firstname ();
781 my $password = $person->get ('password');
783 my $host = $ENV{'HTTP_HOST'};
784 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
786 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
788 To: $person_name <$person_mail>
789 From: $UserCN <$owner_mail>
790 Subject: Please verify our entry in my address book
794 the following is your entry in my address book:
799 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
800 my @values = $person->get ($field);
804 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
809 If this entry is outdated or incomplete, please take a minute and correct it.
811 Username: $person_name
814 Thank you very much :)
819 This message was automatically generated by LiCoM,
820 http://verplant.org/licom/
829 my $cn = param ('cn');
832 my $person = LiCoM::Person->load ($cn);
835 my $cn_esc = uri_escape ($cn);
838 <h2>Really delete $cn?</h2>
841 You are about to delete <strong>$cn</strong>. Are you
842 totally, absolutely sure you want to do this?
846 [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
847 [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
855 my $cn = param ('cn');
858 my $person = LiCoM::Person->load ($cn);
864 <div>$cn has been deleted.</div>
873 $title = q(Lightweight Contact Manager) unless ($title);
876 Content-Type: text/html; charset=UTF-8
880 <title>$title</title>
881 <style type="text/css">
888 background-color: inherit;
889 text-decoration: none;
894 text-decoration: underline;
900 background-color: inherit;
906 background-color: white;
912 background-color: yellow;
916 border: 2px solid red;
922 background-color: white;
932 border-top: 1px solid black;
939 background-color: inherit;
940 text-decoration: none;
945 text-decoration: underline;
950 border-top: 1px solid black;
958 background-color: transparent;
965 border-bottom: 1px solid black;
1004 background-color: #cccccc;
1005 vertical-align: top;
1011 background-color: #999999;
1014 vertical-align: top;
1023 background-color: inherit;
1024 text-decoration: underline;
1027 div.topmenu, div.menu
1050 border-collapse: collapse;
1065 border-bottom-width: 2px;
1070 border: 1px solid black;
1071 vertical-align: top;
1089 my $search = param ('search') || '';
1091 <div class="topmenu">
1092 <form action="$MySelf" method="post">
1093 <input type="hidden" name="action" value="browse" />
1094 <input type="submit" name="button" value="Browse" />
1096 <form action="$MySelf" method="post">
1097 <input type="hidden" name="action" value="search" />
1098 <input type="text" name="search" value="$search" />
1099 <input type="submit" name="button" value="Search" />
1101 <form action="$MySelf" method="post">
1102 <input type="hidden" name="action" value="edit" />
1103 <input type="hidden" name="dn" value="" />
1104 <input type="submit" name="button" value="Add New" />
1109 print "\t\t<h1>$title</h1>\n";
1116 "Lightweight Contact Manager",
1117 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1118 <octo at verplant.org>
1127 my $len = @_ ? shift : 6;
1136 while (length ($retval) < $len)
1138 my $chr = int (rand (128));
1140 if ($chr >= 48 and $chr < 58)
1144 elsif ($chr >= 65 and $chr < 91)
1148 elsif ($chr >= 97 and $chr < 123)
1156 $retval .= chr ($chr);
1159 $retval = '' if (!$numbers or !$lchars or !$uchars);
1173 if ($val !~ m#^[a-zA-Z]+://#)
1175 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1180 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1183 next unless ($number);
1185 if ($number !~ m/^\+[0-9 \-]+$/)
1187 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1192 print qq(\t\t<div class="error">\n) if (@errors);
1193 for (my $i = 0; $i < scalar (@errors); $i++)
1195 my $e = $errors[$i];
1197 print "<br />\n" if ($i);
1200 print qq(\n\t\t</div>\n\n) if (@errors);
1202 return (scalar (@errors));
1207 my $contacts = @_ ? shift : {};
1212 my @values = grep { $_ } (param ($field));
1214 next unless (@values);
1216 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1220 $_ =~ s/[^0-9 \-]//g;
1221 $_ = '+' . $_ if ($_);
1225 $contacts->{$field} = [@values] if (@values);