8 use CGI::Carp (qw(fatalsToBrowser));
17 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
22 homephone => 'Home Phone',
23 cellphone => 'Cell Phone',
24 officephone => 'Office Phone',
27 uri => 'URI (Homepage)',
31 our $MySelf = $ENV{'SCRIPT_NAME'};
33 our $Action = param ('action');
34 $Action ||= 'default';
38 browse => [\&html_start, \&action_browse, \&html_end],
39 default => [\&html_start, \&action_browse, \&html_end],
40 detail => [\&html_start, \&action_detail, \&html_end],
41 edit => [\&html_start, \&action_edit, \&html_end],
42 list => [\&html_start, \&action_list, \&html_end],
43 save => [\&html_start, \&action_save, \&html_end],
44 search => [\&html_start, \&action_search, \&html_end],
45 verify => [\&html_start, \&action_verify, \&html_end],
46 vcard => \&action_vcard
51 # make sure AuthLDAPRemoteUserIsDN is enabled.
52 die unless ($ENV{'REMOTE_USER'});
53 $Config{'base_dn'} = $ENV{'REMOTE_USER'};
57 uri => $Config{'uri'},
58 base_dn => $Config{'base_dn'},
59 bind_dn => $Config{'bind_dn'},
60 password => $Config{'password'}
63 our ($UserCN, $UserID) = Person->get_user ($Config{'base_dn'});
65 if (!$UserID and $Action ne 'save')
75 if (!defined ($Actions{$Action}))
80 if (ref ($Actions{$Action}) eq 'CODE')
82 $Actions{$Action}->();
84 elsif (ref ($Actions{$Action}) eq 'ARRAY')
86 for (@{$Actions{$Action}})
92 #print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
94 Person->disconnect ();
102 my $group = param ('group');
103 $group = shift if (@_);
109 @all = Person->search ([[group => $group]]);
113 @all = Person->search ();
122 my @g = $person->get ('group');
124 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
127 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
128 for (sort (keys (%groups)))
131 my $group_esc = uri_escape ($group);
132 my $num = $groups{$group};
134 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
138 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
140 print qq(\t\t</ul>\n\n);
145 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
149 print qq(\t\t<h2>All Contacts</h2>\n);
152 print qq(\t\t<ul class="results">\n);
153 for (sort { $a->name () cmp $b->name () } (@all))
156 my $cn = $person->name ();
157 my $cn_esc = uri_escape ($cn);
159 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
161 print qq(\t\t</ul>\n\n);
163 print qq(\t\t<div class="menu">\n);
166 my $group_esc = uri_escape ($group);
167 print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
168 qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
172 print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
174 print qq(\t\t</div>\n);
179 my $group = param ('group');
180 $group = shift if (@_);
183 my $title = $group ? "List of group "$group"" : 'List of all addresses';
184 my @fields = (qw(address homephone cellphone officephone fax mail));
189 @all = Person->search ([[group => $group]]);
193 @all = Person->search ();
205 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
207 print "\t\t\t</tr>\n";
209 for (sort { $a->name () cmp $b->name () } (@all))
212 my $sn = $person->lastname ();
213 my $gn = $person->firstname ();
215 print "\t\t\t<tr>\n",
216 "\t\t\t\t<td>$sn, $gn</td>\n";
221 my @values = $person->get ($field);
222 print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
225 print "\t\t\t</tr>\n";
227 print "\t\t</table>\n\n";
231 my $group_esc = uri_escape ($group);
232 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
236 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
242 my $cn = param ('cn');
246 my $person = Person->load ($cn);
249 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
253 print qq(\t\t<h2>Details for $cn</h2>\n);
255 my $cn_esc = uri_escape ($cn);
258 <table class="detail">
267 my $values = $person->get ($field);
268 my $num = scalar (@$values);
269 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
273 print "\t\t\t<tr>\n";
276 print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
280 print qq(\t\t\t\t<th>$print</th>\n);
283 for (my $i = 0; $i < $num; $i++)
285 my $val = $values->[$i];
287 if ($field eq 'group')
289 my $val_esc = uri_escape ($val);
290 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
292 elsif ($field eq 'uri')
295 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
296 $val = qq(<a href="$uri" class="extern">$val</a>);
298 elsif ($field eq 'mail')
300 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
303 print "\t\t\t<tr>\n" if ($i);
304 print "\t\t\t\t<td>$val</td>\n",
312 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
313 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
314 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
322 my $search = param ('search');
325 $search =~ s/[^\s\w]//g;
329 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
334 my @patterns = split (m/\s+/, $search);
340 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
343 my @matches = Person->search (@filter);
347 print qq(\t<div>No entries matched your search.</div>\n);
351 if (scalar (@matches) == 1)
353 my $person = shift (@matches);
354 my $cn = $person->name ();
359 print qq(\t<ul class="result">\n);
363 my $cn = $person->name ();
364 my $cn_esc = uri_escape ($cn);
366 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
375 my $cn = param ('cn');
377 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
391 $contacts->{$_} = [] for (@MultiFields);
395 $person = Person->load ($cn);
399 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
403 $lastname = $person->lastname ();
404 $firstname = $person->firstname ();
408 $contacts->{$_} = $person->get ($_);
412 $lastname = param ('lastname') if (param ('lastname') and $UserID);
413 $firstname = param ('firstname') if (param ('firstname') and $UserID);
415 get_contacts ($contacts);
417 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
418 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
422 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
427 print "\t\t<h2>Edit contact $cn</h2>\n";
431 print "\t\t<h2>Create new contact</h2>\n";
435 <form action="$MySelf" method="post">
436 <input type="hidden" name="action" value="save" />
437 <input type="hidden" name="cn" value="$cn" />
444 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
448 print qq(\t\t\t\t<td>$lastname</td>\n);
457 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
461 print qq(\t\t\t\t<td>$firstname</td>\n);
464 print "\t\t\t</tr>\n";
469 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
470 my @values = @{$contacts->{$field}};
481 <td><input type="text" name="$field" value="$value" /></td>
489 <th colspan="2" class="menu">
494 <input type="submit" name="button" value="Cancel" />
495 <input type="submit" name="button" value="Apply" />
499 <input type="submit" name="button" value="Save" />
509 my $cn = $UserID ? param ('cn') : $UserCN;
511 if (verify_fields ())
513 action_edit (cn => $cn);
523 die unless ($UserID);
525 my $button = lc (param ('button'));
528 if ($button eq 'cancel')
534 if (!param ('lastname') or !param ('firstname'))
536 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
537 action_edit (cn => '');
541 my $lastname = param ('lastname');
542 my $firstname = param ('firstname');
544 my $contacts = get_contacts ();
546 my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
550 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
554 $cn = $person->name ();
556 if ($button eq 'apply')
558 action_edit (cn => $cn);
568 my $cn = $UserID ? param ('cn') : $UserCN;
569 my $person = Person->load ($cn);
571 die unless ($person);
573 my $button = lc (param ('button'));
576 if ($UserID and $button eq 'cancel')
584 my $lastname = param ('lastname');
585 my $firstname = param ('firstname');
587 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
588 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
590 $cn = $person->name ();
593 my $contacts = get_contacts ();
599 if (defined ($contacts->{$field}))
601 my $values = $contacts->{$field};
602 $person->set ($field, $values);
606 $person->set ($field, []);
610 if ($button eq 'apply' or !$UserID)
612 action_edit (cn => $cn);
622 my $cn = param ('cn');
626 my $person = Person->load ($cn);
627 die unless ($person);
631 homephone => 'TEL;TYPE=home,voice',
632 cellphone => 'TEL;TYPE=cell',
633 officephone => 'TEL;TYPE=work,voice',
634 fax => 'TEL;TYPE=fax',
640 my $sn = $person->lastname ();
641 my $gn = $person->firstname ();
642 my $cn_esc = uri_escape ($cn);
645 Content-Type: text/x-vcard
646 Content-Disposition: attachment; filename="$cn.vcf"
657 my $vc_fld = $vcard_types{$field};
658 my $values = $person->get ($field);
663 print "$vc_fld:$value\n";
671 my $cn = param ('cn');
675 my $person = Person->load ($cn);
676 die unless ($person);
678 my ($mail) = $person->get ('mail');
682 my $password = $person->password ();
686 $password = pwgen ();
687 $person->password ($password);
690 $message = qq(The password for the record "$cn" is "$password".);
694 if (action_verify_send_mail ($person))
696 $message .= qq( A request for verification has been sent to $mail.);
701 $message .= q( There was no e-mail address, thus no verification request could be sent.);
704 print qq(\t\t<div class="message">$message</div>\n);
709 sub action_verify_send_mail
712 my $owner = Person->load ($UserCN);
715 my ($owner_mail) = $owner->get ('mail');
718 my $cn = uri_escape ($UserCN);
719 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);
724 for (keys %FieldNames)
726 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
730 my $person_name = $person->name ();
731 my ($person_mail) = $person->get ('mail');
732 my $person_gn = $person->firstname ();
733 my $password = $person->password ();
735 my $host = $ENV{'HTTP_HOST'};
736 my $url = 'http://' . $host . $MySelf;
738 open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
740 To: $person_name <$person_mail>
741 From: $UserCN <$owner_mail>
742 Subject: Please verify our entry in my address book
746 the following is your entry in my address book:
751 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
752 my @values = $person->get ($field);
756 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
761 If this entry is outdated or incomplete, please take a minute and correct it.
763 Username: $person_name
766 Thank you very much :) Regards,
777 $title = q(Lightweight Contact Manager) unless ($title);
780 Content-Type: text/html; charset=UTF-8
784 <title>$title</title>
785 <style type="text/css">
792 background-color: inherit;
793 text-decoration: none;
798 text-decoration: underline;
804 background-color: inherit;
810 background-color: white;
816 background-color: yellow;
820 border: 2px solid red;
826 background-color: white;
836 border-top: 1px solid black;
843 background-color: inherit;
844 text-decoration: none;
849 text-decoration: underline;
854 border-top: 1px solid black;
862 background-color: transparent;
869 border-bottom: 1px solid black;
908 background-color: #cccccc;
915 background-color: #999999;
927 background-color: inherit;
928 text-decoration: underline;
931 div.topmenu, div.menu
954 border-collapse: collapse;
969 border-bottom-width: 2px;
974 border: 1px solid black;
992 my $search = param ('search') || '';
994 <div class="topmenu">
995 <form action="$MySelf" method="post">
996 <input type="hidden" name="action" value="browse" />
997 <input type="submit" name="button" value="Browse" />
999 <form action="$MySelf" method="post">
1000 <input type="hidden" name="action" value="search" />
1001 <input type="text" name="search" value="$search" />
1002 <input type="submit" name="button" value="Search" />
1004 <form action="$MySelf" method="post">
1005 <input type="hidden" name="action" value="edit" />
1006 <input type="hidden" name="dn" value="" />
1007 <input type="submit" name="button" value="Add New" />
1012 print "\t\t<h1>$title</h1>\n";
1019 "Lightweight Contact Manager",
1020 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
1021 <octo at verplant.org>
1030 my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
1033 open ($fh, "< $file") or die ("open ($file): $!");
1039 if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
1044 $Config{$key} = $val;
1050 for (qw(uri bind_dn password))
1052 die ("Not defined: $_") unless (defined ($Config{$_}));
1058 my $len = @_ ? shift : 6;
1067 while (length ($retval) < $len)
1069 my $chr = int (rand (128));
1071 if ($chr >= 48 and $chr < 58)
1075 elsif ($chr >= 65 and $chr < 91)
1079 elsif ($chr >= 97 and $chr < 123)
1087 $retval .= chr ($chr);
1090 $retval = '' if (!$numbers or !$lchars or !$uchars);
1104 if ($val !~ m#^[a-zA-Z]+://#)
1106 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1111 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
1114 next unless ($number);
1116 if ($number !~ m/^\+/)
1118 push (@errors, 'Telephone numbers have to begin with the country code, e.g. "+49 911 123456"');
1123 print qq(\t\t<div class="error">\n) if (@errors);
1124 for (my $i = 0; $i < scalar (@errors); $i++)
1126 my $e = $errors[$i];
1128 print "<br />\n" if ($i);
1131 print qq(\n\t\t</div>\n\n) if (@errors);
1133 return (scalar (@errors));
1138 my $contacts = @_ ? shift : {};
1143 my @values = grep { $_ } (param ($field));
1145 next unless (@values);
1147 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1156 $contacts->{$field} = [@values] if (@values);