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 save => [\&html_start, \&action_save, \&html_end],
43 search => [\&html_start, \&action_search, \&html_end],
44 verify => [\&html_start, \&action_verify, \&html_end],
45 vcard => \&action_vcard
50 # make sure AuthLDAPRemoteUserIsDN is enabled.
51 die unless ($ENV{'REMOTE_USER'});
52 $Config{'base_dn'} = $ENV{'REMOTE_USER'};
56 uri => $Config{'uri'},
57 base_dn => $Config{'base_dn'},
58 bind_dn => $Config{'bind_dn'},
59 password => $Config{'password'}
62 our ($UserCN, $UserID) = Person->get_user ($Config{'base_dn'});
64 if (!$UserID and $Action ne 'save')
74 if (!defined ($Actions{$Action}))
79 if (ref ($Actions{$Action}) eq 'CODE')
81 $Actions{$Action}->();
83 elsif (ref ($Actions{$Action}) eq 'ARRAY')
85 for (@{$Actions{$Action}})
91 #print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
93 Person->disconnect ();
101 my $group = param ('group');
102 $group = shift if (@_);
108 @all = Person->search ([[group => $group]]);
112 @all = Person->search ();
121 my @g = $person->get ('group');
123 $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
126 print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
127 for (sort (keys (%groups)))
130 my $group_esc = uri_escape ($group);
131 my $num = $groups{$group};
133 print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
137 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
139 print qq(\t\t</ul>\n\n);
144 print qq(\t\t<h2>Contact Group "$group"</h2>\n);
148 print qq(\t\t<h2>All Contacts</h2>\n);
151 print qq(\t\t<ul class="results">\n);
152 for (sort { $a->name () cmp $b->name () } (@all))
155 my $cn = $person->name ();
156 my $cn_esc = uri_escape ($cn);
158 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
160 print qq(\t\t</ul>\n\n);
162 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">back</a>]</div>\n) if ($group);
167 my $cn = param ('cn');
171 my $person = Person->load ($cn);
174 print qq(\t<div>Entry "$cn" could not be loaded from DB.</div>\n);
178 print qq(\t<h2>Details for $cn</h2>\n);
180 my $cn_esc = uri_escape ($cn);
183 <table class="detail">
192 my $values = $person->get ($field);
193 my $num = scalar (@$values);
194 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
201 print qq(\t\t\t<th rowspan="$num">$print</th>\n);
205 print qq(\t\t\t<th>$print</th>\n);
208 for (my $i = 0; $i < $num; $i++)
210 my $val = $values->[$i];
212 if ($field eq 'group')
214 my $val_esc = uri_escape ($val);
215 $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
217 elsif ($field eq 'uri')
220 $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
221 $val = qq(<a href="$uri" class="extern">$val</a>);
223 elsif ($field eq 'mail')
225 $val = qq(<a href="mailto:$val" class="mail">$val</a>);
228 print "\t\t<tr>\n" if ($i);
229 print "\t\t\t<td>$val</td>\n",
234 <th colspan="2" class="menu">
235 [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
236 [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
237 [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
245 my $search = param ('search');
248 $search =~ s/[^\s\w]//g;
252 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
257 my @patterns = split (m/\s+/, $search);
263 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
266 my @matches = Person->search (@filter);
270 print qq(\t<div>No entries matched your search.</div>\n);
274 if (scalar (@matches) == 1)
276 my $person = shift (@matches);
277 my $cn = $person->name ();
282 print qq(\t<ul class="result">\n);
286 my $cn = $person->name ();
287 my $cn_esc = uri_escape ($cn);
289 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
298 my $cn = param ('cn');
300 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
314 $contacts->{$_} = [] for (@MultiFields);
318 $person = Person->load ($cn);
322 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
326 $lastname = $person->lastname ();
327 $firstname = $person->firstname ();
331 $contacts->{$_} = $person->get ($_);
335 $lastname = param ('lastname') if (param ('lastname') and $UserID);
336 $firstname = param ('firstname') if (param ('firstname') and $UserID);
338 get_contacts ($contacts);
340 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
341 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
345 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
350 print "\t\t<h2>Edit contact $cn</h2>\n";
354 print "\t\t<h2>Create new contact</h2>\n";
358 <form action="$MySelf" method="post">
359 <input type="hidden" name="action" value="save" />
360 <input type="hidden" name="cn" value="$cn" />
367 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
371 print qq(\t\t\t\t<td>$lastname</td>\n);
380 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
384 print qq(\t\t\t\t<td>$firstname</td>\n);
387 print "\t\t\t</tr>\n";
392 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
393 my @values = @{$contacts->{$field}};
404 <td><input type="text" name="$field" value="$value" /></td>
412 <th colspan="2" class="menu">
417 <input type="submit" name="button" value="Cancel" />
418 <input type="submit" name="button" value="Apply" />
422 <input type="submit" name="button" value="Save" />
432 my $cn = $UserID ? param ('cn') : $UserCN;
434 if (verify_fields ())
436 action_edit (cn => $cn);
446 die unless ($UserID);
448 my $button = lc (param ('button'));
451 if ($button eq 'cancel')
457 if (!param ('lastname') or !param ('firstname'))
459 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
460 action_edit (cn => '');
464 my $lastname = param ('lastname');
465 my $firstname = param ('firstname');
467 my $contacts = get_contacts ();
469 my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
473 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
477 $cn = $person->name ();
479 if ($button eq 'apply')
481 action_edit (cn => $cn);
491 my $cn = $UserID ? param ('cn') : $UserCN;
492 my $person = Person->load ($cn);
494 die unless ($person);
496 my $button = lc (param ('button'));
499 if ($UserID and $button eq 'cancel')
507 my $lastname = param ('lastname');
508 my $firstname = param ('firstname');
510 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
511 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
513 $cn = $person->name ();
516 my $contacts = get_contacts ();
522 if (defined ($contacts->{$field}))
524 my $values = $contacts->{$field};
525 $person->set ($field, $values);
529 $person->set ($field, []);
533 if ($button eq 'apply' or !$UserID)
535 action_edit (cn => $cn);
545 my $cn = param ('cn');
549 my $person = Person->load ($cn);
550 die unless ($person);
554 homephone => 'TEL;TYPE=home,voice',
555 cellphone => 'TEL;TYPE=cell',
556 officephone => 'TEL;TYPE=work,voice',
557 fax => 'TEL;TYPE=fax',
563 my $sn = $person->lastname ();
564 my $gn = $person->firstname ();
565 my $cn_esc = uri_escape ($cn);
568 Content-Type: text/x-vcard
569 Content-Disposition: attachment; filename="$cn.vcf"
580 my $vc_fld = $vcard_types{$field};
581 my $values = $person->get ($field);
586 print "$vc_fld:$value\n";
594 my $cn = param ('cn');
598 my $person = Person->load ($cn);
599 die unless ($person);
601 my ($mail) = $person->get ('mail');
605 my $password = $person->password ();
609 $password = pwgen ();
610 $person->password ($password);
613 $message = qq(The password for the record "$cn" is "$password".);
617 action_verify_send_mail ($person);
618 $message .= qq( A request for verification has been sent to $mail.);
622 $message .= q( There was no e-mail address, thus no verification request could be sent.);
625 print qq(\t\t<div class="message">$message</div>\n);
630 sub action_verify_send_mail
633 my $owner = Person->load ($UserCN);
637 for (keys %FieldNames)
639 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
643 my $person_name = $person->name ();
644 my ($person_mail) = $person->get ('mail');
645 my $person_gn = $person->firstname ();
646 my $password = $person->password ();
648 my $owner_name = $owner->name ();
649 my ($owner_mail) = $owner->get ('mail');
650 $owner_mail ||= $ENV{'SERVER_ADMIN'};
652 my $host = $ENV{'HTTP_HOST'};
653 my $url = 'http://' . $host . $MySelf;
655 open ($smh, '| /usr/sbin/sendmail -t') or die ("open pipe to sendmail: $!");
657 To: $person_name <$person_mail>
658 From: $owner_name <$owner_mail>
659 Subject: Please verify our entry in my address book
663 the following is your entry in my address book:
668 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
669 my @values = $person->get ($field);
673 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
678 If this entry is outdated or incomplete, please take a minute and correct it.
680 Username: $person_name
683 Thank you very much :) Regards,
692 $title = q(Lightweight Contact Manager) unless ($title);
695 Content-Type: text/html; charset=UTF-8
699 <title>$title</title>
700 <style type="text/css">
705 background-color: inherit;
706 text-decoration: none;
711 text-decoration: underline;
717 background-color: inherit;
723 background-color: white;
729 background-color: yellow;
733 border: 2px solid red;
739 background-color: white;
749 border-top: 1px solid black;
756 background-color: inherit;
757 text-decoration: none;
762 text-decoration: underline;
779 background-color: #cccccc;
785 background-color: #999999;
804 background-color: transparent;
814 my $search = param ('search') || '';
817 <form action="$MySelf" method="post">
818 <input type="hidden" name="action" value="browse" />
819 <input type="submit" name="button" value="Browse" />
821 <form action="$MySelf" method="post">
822 <input type="hidden" name="action" value="search" />
823 <input type="text" name="search" value="$search" />
824 <input type="submit" name="button" value="Search" />
826 <form action="$MySelf" method="post">
827 <input type="hidden" name="action" value="edit" />
828 <input type="hidden" name="dn" value="" />
829 <input type="submit" name="button" value="Add New" />
835 print "\t\t<h1>$title</h1>\n";
842 "Lightweight Contact Manager",
843 written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
844 <octo at verplant.org>
853 my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
856 open ($fh, "< $file") or die ("open ($file): $!");
862 if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
867 $Config{$key} = $val;
873 for (qw(uri bind_dn password))
875 die ("Not defined: $_") unless (defined ($Config{$_}));
881 my $len = @_ ? shift : 6;
890 while (length ($retval) < $len)
892 my $chr = int (rand (128));
894 if ($chr >= 48 and $chr < 58)
898 elsif ($chr >= 65 and $chr < 91)
902 elsif ($chr >= 97 and $chr < 123)
910 $retval .= chr ($chr);
913 $retval = '' if (!$numbers or !$lchars or !$uchars);
927 if ($val !~ m#^[a-zA-Z]+://#)
929 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
934 for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
937 next unless ($number);
939 if ($number !~ m/^\+/)
941 push (@errors, 'Telephone numbers have to begin with the country code, e.g. "+49 911 123456"');
946 print qq(\t\t<div class="error">\n) if (@errors);
947 for (my $i = 0; $i < scalar (@errors); $i++)
951 print "<br />\n" if ($i);
954 print qq(\n\t\t</div>\n\n) if (@errors);
956 return (scalar (@errors));
961 my $contacts = @_ ? shift : {};
966 my @values = grep { $_ } (param ($field));
968 next unless (@values);
970 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
979 $contacts->{$field} = [@values] if (@values);