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
23 use Encode (qw(encode decode is_utf8));
25 use CGI::Carp (qw(fatalsToBrowser));
27 use HTML::Entities (qw(encode_entities));
29 use LiCoM::Config (qw(get_config set_config read_config));
30 use LiCoM::Connection ();
36 our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
41 homephone => 'Home Phone',
42 cellphone => 'Cell Phone',
43 officephone => 'Office Phone',
46 uri => 'URI (Homepage)',
50 our $MySelf = $ENV{'SCRIPT_NAME'};
52 our $Action = param_utf8 ('action');
53 $Action ||= 'default';
57 browse => [\&html_start, \&action_browse, \&html_end],
58 default => [\&html_start, \&action_browse, \&html_end],
59 detail => [\&html_start, \&action_detail, \&html_end],
60 edit => [\&html_start, \&action_edit, \&html_end],
61 list => [\&html_start, \&action_list, \&html_end],
62 save => [\&html_start, \&action_save, \&html_end],
63 search => [\&html_start, \&action_search, \&html_end],
64 verify => [\&html_start, \&action_verify, \&html_end],
65 delete => [\&html_start, \&action_ask_del, \&html_end],
66 expunge => [\&html_start, \&action_do_del, \&html_end],
67 vcard => \&action_vcard,
68 edit_group => [\&html_start, \&action_edit_group, \&html_end],
69 save_group => [\&html_start, \&action_save_group, \&html_end]
74 # make sure AuthLDAPRemoteUserIsDN is enabled.
75 die unless ($ENV{'REMOTE_USER'});
76 #set_config ('base_dn', $ENV{'REMOTE_USER'});
78 die ("Configuration is incomplete") unless (defined (get_config ('uri'))
79 and defined (get_config ('base_dn'))
80 and defined (get_config ('bind_dn'))
81 and defined (get_config ('password')));
83 LiCoM::Connection->connect
85 uri => get_config ('uri'),
86 bind_dn => get_config ('bind_dn'),
87 password => get_config ('password')
88 ) or die ("Unable to connect to LDAP directory server " . get_config ('uri'));
90 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
92 if (!$UserID and $Action ne 'save')
99 die ("No such user in the LDAP directory: " . $ENV{'REMOTE_USER'});
102 if (!defined ($Actions{$Action}))
104 die ("No such action: $Action");
107 if (ref ($Actions{$Action}) eq 'CODE')
109 $Actions{$Action}->();
111 elsif (ref ($Actions{$Action}) eq 'ARRAY')
113 for (@{$Actions{$Action}})
119 LiCoM::Connection->disconnect ();
127 my $group = param_utf8 ('group');
128 $group = shift if (@_);
133 my @groups = LiCoM::Group->all ();
135 print qq(\t\t<h2>Contact groups</h2>\n\t\t<ul class="groups">\n);
139 my @members = $group->get_members ();
140 my $members = scalar (@members);
141 my $group_name = $group->name ();
142 my $group_uri = uri_escape_utf8 ($group_name);
143 my $desc = $group->description ();
145 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_uri">#,
146 encode_entities ($group_name),
147 qq#</a> ($members Member#, ($members == 1 ? ')' : 's)');
148 print qq(<br />\n\t\t\t\t<span class="description">),
149 encode_entities ($desc) . '</span>' if ($desc);
154 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
159 [<a href="$MySelf?action=list">List all</a>]
165 my $group_obj = LiCoM::Group->load ($group);
166 my $group_uri = uri_escape_utf8 ($group_obj->name ());
167 my $group_html = encode_entities ($group_obj->name ());
168 my @member_names = $group_obj->get_members ();
169 my $desc = $group_obj->description ();
170 my $desc_html = encode_entities ($desc || '');
172 print qq(\t\t<h2>Contact group "$group_html"</h2>\n);
173 print qq(\t\t<div>$desc_html</div>\n) if ($desc);
174 print qq(\t\t<ul class="results">\n);
175 for (sort (@member_names))
178 my $cn_uri = uri_escape_utf8 ($cn);
179 my $cn_html = encode_entities ($cn);
181 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
187 [<a href="$MySelf?action=list&group=$group_uri">List</a>]
188 [<a href="$MySelf?action=browse">Back</a>]
189 [<a href="$MySelf?action=edit_group&group=$group_uri">Edit</a>]
197 my $group_name = param_utf8 ('group');
198 $group_name = shift if (@_);
201 my $group_name_html = encode_entities ($group_name || '');
203 my $title = $group_name
204 ? "List of group "$group_name_html""
205 : 'List of all addresses';
206 my @fields = (qw(address homephone cellphone officephone fax mail));
211 my $group_obj = LiCoM::Group->load ($group_name);
216 Unable to load group "$group_name_html".
221 for ($group_obj->get_members ())
224 my $person_obj = LiCoM::Person->load ($cn);
228 print STDERR "Unable to load cn = $cn;\n";
231 push (@all, $person_obj);
236 @all = LiCoM::Person->search ([[group => $group_name]]);
248 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
250 print "\t\t\t</tr>\n";
252 for (sort { $a->name () cmp $b->name () } (@all))
255 my $cn = $person->name ();
256 my $sn = $person->lastname ();
257 my $gn = $person->firstname ();
259 my $cn_uri = uri_escape_utf8 ($cn);
260 my $cn_html = encode_entities ("$sn, $gn");
262 print "\t\t\t<tr>\n",
263 qq(\t\t\t\t<td><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></td>\n);
268 my @values = $person->get ($field);
269 print "\t\t\t\t<td>" . join ('<br />', map { markup_field ($field, $_) } (@values)) . "</td>\n";
272 print "\t\t\t</tr>\n";
274 print "\t\t</table>\n\n";
278 my $group_esc = uri_escape_utf8 ($group_name);
279 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
283 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
289 my $cn = param_utf8 ('cn');
293 my $cn_html = encode_entities ($cn);
294 my $cn_uri = uri_escape_utf8 ($cn);
296 my $person = LiCoM::Person->load ($cn);
299 print qq(\t<div>Entry "$cn_html" could not be loaded from DB.</div>\n);
303 print qq(\t\t<h2>Details for $cn_html</h2>\n);
306 <table class="detail">
315 my $values = $person->get ($field);
316 my $num = scalar (@$values);
317 my $field_name = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
321 $field_name = encode_entities ($field_name);
323 print "\t\t\t<tr>\n";
326 print qq(\t\t\t\t<th rowspan="$num">$field_name</th>\n);
330 print qq(\t\t\t\t<th>$field_name</th>\n);
333 for (my $i = 0; $i < $num; $i++)
335 my $val = markup_field ($field, $values->[$i]);
337 print "\t\t\t<tr>\n" if ($i);
338 print "\t\t\t\t<td>$val</td>\n",
343 my @groups = LiCoM::Group->load_by_member ($cn);
346 my $num = scalar (@groups);
347 print "\t\t\t<tr>\n",
348 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
349 for (my $i = 0; $i < $num; $i++)
351 my $group = $groups[$i];
352 my $group_name = $group->name ();
353 my $group_uri = uri_escape_utf8 ($group_name);
354 my $group_html = encode_entities ($group_name);
356 print "\t\t\t<tr>\n" if ($i != 0);
357 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_uri">$group_html</a></td>\n),
366 [<a href="$MySelf?action=verify&cn=$cn_uri">Verify</a>]
367 [<a href="$MySelf?action=vcard&cn=$cn_uri">vCard</a>]
368 [<a href="$MySelf?action=edit&cn=$cn_uri">Edit</a>]
369 [<a href="$MySelf?action=delete&cn=$cn_uri">Delete</a>]
377 my $search = param_utf8 ('search');
380 $search =~ s/[^\s\w]//g;
384 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
389 my @patterns = split (m/\s+/, $search);
395 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
398 my @matches = LiCoM::Person->search (@filter);
402 print qq(\t<div>No entries matched your search.</div>\n);
406 if (scalar (@matches) == 1)
408 my $person = shift (@matches);
409 my $cn = $person->name ();
414 print qq(\t<ul class="result">\n);
415 for (sort { $a->name () cmp $b->name () } (@matches))
418 my $cn = $person->name ();
419 my $cn_uri = uri_escape_utf8 ($cn);
420 my $cn_html = encode_entities ($cn);
422 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
431 my $cn = param_utf8 ('cn');
433 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
436 my $cn_html = encode_entities ($cn);
452 $contacts->{$_} = [] for (@MultiFields);
456 $person = LiCoM::Person->load ($cn);
460 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
464 $lastname = $person->lastname ();
465 $firstname = $person->firstname ();
469 $contacts->{$_} = $person->get ($_);
473 $lastname = param_utf8 ('lastname') if (param_utf8 ('lastname') and $UserID);
474 $firstname = param_utf8 ('firstname') if (param_utf8 ('firstname') and $UserID);
476 get_contacts ($contacts);
478 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
479 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
480 $lastname_html = encode_entities ($lastname);
481 $firstname_html = encode_entities ($firstname);
486 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
491 print "\t\t<h2>Edit contact $cn_html</h2>\n";
495 print "\t\t<h2>Create new contact</h2>\n";
499 <form action="$MySelf" method="post" accept-charset="UTF-8">
500 <input type="hidden" name="action" value="save" />
501 <input type="hidden" name="cn" value="$cn_html" />
508 print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname_html" /></td>\n);
512 print qq(\t\t\t\t<td>$lastname_html</td>\n);
521 print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname_html" /></td>\n);
525 print qq(\t\t\t\t<td>$firstname_html</td>\n);
528 print "\t\t\t</tr>\n";
533 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
534 my @values = @{$contacts->{$field}};
536 next if ($field eq 'group');
540 $field = encode_entities ($field);
541 $print = encode_entities ($print);
545 my $value = encode_entities ($_);
550 <td><input type="text" name="$field" value="$value" /></td>
558 my @all_groups = LiCoM::Group->all ();
562 print "\t\t\t<tr>\n",
563 "\t\t\t\t<th>Group(s)</th>\n",
564 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
569 my $group_name = encode_entities ($group->name ());
572 if (grep { $cn eq $_ } ($group->get_members ()))
574 $selected = ' selected="selected"';
577 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
579 print "\t\t\t\t</select></td>\n",
583 print "\t\t\t<tr>\n",
584 "\t\t\t\t<th>New Group</th>\n",
585 qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
591 <th colspan="2" class="menu">
596 <input type="submit" name="button" value="Cancel" />
597 <input type="submit" name="button" value="Apply" />
601 <input type="submit" name="button" value="Save" />
611 my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
613 if (verify_fields ())
615 action_edit (cn => $cn);
625 die unless ($UserID);
627 my $button = lc (param_utf8 ('button'));
630 if ($button eq 'cancel')
636 if (!param_utf8 ('lastname') or !param_utf8 ('firstname'))
638 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
639 action_edit (cn => '');
643 my $lastname = param_utf8 ('lastname');
644 my $firstname = param_utf8 ('firstname');
646 my $contacts = get_contacts ();
648 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
652 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
656 $cn = $person->name ();
658 for (param_utf8 ('group'))
661 my $group = LiCoM::Group->load ($group_name);
665 $group->add_members ($cn);
669 my $group_html = encode_entities ($group_name);
670 print qq(\t<div class="error">Group "$group_html" does not exist or could not be loaded.</div>\n);
674 if (param_utf8 ('newgroup'))
676 # FIXME add error handling
677 my $group_name = param_utf8 ('newgroup');
678 LiCoM::Group->create ($group_name, '', $cn);
681 if ($button eq 'apply')
683 action_edit (cn => $cn);
693 my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
695 my $person = LiCoM::Person->load ($cn);
696 die ("Unable to load CN `$cn'") unless ($person);
698 my $button = lc (param_utf8 ('button'));
701 if ($UserID and $button eq 'cancel')
709 my $lastname = param_utf8 ('lastname');
710 my $firstname = param_utf8 ('firstname');
712 my $old_cn = $person->name ();
716 \$lastname = $lastname<br />
717 \$firstname = $firstname<br />
722 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
723 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
725 $cn = $person->name ();
727 # Change the cn's saved in the groups
730 my @groups = LiCoM::Group->load_by_member ($old_cn);
733 # ->del_members automatically deleted the
734 # group, if no more members exist. So this
735 # order is important.
736 print "<div><code>\$cn = " . encode_entities ($cn) . "; "
737 . "\$old_cn = " . encode_entities ($old_cn) . ";</code></div>\n";
738 $_->add_members ($cn);
739 $_->del_members ($old_cn);
741 } # if ($old_cn ne $cn)
744 my $contacts = get_contacts ();
750 next if (!$UserID and $field eq 'group');
752 if (defined ($contacts->{$field}))
754 my $values = $contacts->{$field};
755 $person->set ($field, $values);
759 $person->set ($field, []);
763 # only `authorized' users may see and change groups
766 my %changed_groups = map { $_ => 1 } (param_utf8 ('group'));
767 my @current_groups = LiCoM::Group->load_by_member ($cn);
769 for (@current_groups)
772 my $group_name = $group_obj->name ();
774 if (!defined ($changed_groups{$group_name}))
776 $group_obj->del_members ($cn);
780 delete ($changed_groups{$group_name});
783 for (keys %changed_groups)
786 my $group_obj = LiCoM::Group->load ($group_name) or die;
788 $group_obj->add_members ($cn);
791 if (param_utf8 ('newgroup'))
793 # FIXME add error handling
794 my $group_name = param_utf8 ('newgroup');
795 LiCoM::Group->create ($group_name, '', $cn);
802 <h3>Your changes have been saved.</h3>
803 <p>Thank you very much for taking the time to keep this record up to date.</p>
808 if ($button eq 'apply' or !$UserID)
810 action_edit (cn => $cn);
820 my $cn = param_utf8 ('cn');
824 my $person = LiCoM::Person->load ($cn);
825 die unless ($person);
829 homephone => 'TEL;TYPE=home,voice',
830 cellphone => 'TEL;TYPE=cell',
831 officephone => 'TEL;TYPE=work,voice',
832 fax => 'TEL;TYPE=fax',
838 my $sn = $person->lastname ();
839 my $gn = $person->firstname ();
840 my $cn_esc = uri_escape_utf8 ($cn);
843 Content-Type: text/x-vcard
844 Content-Disposition: attachment; filename="$cn.vcf"
855 my $vc_fld = $vcard_types{$field};
856 my $values = $person->get ($field);
858 next unless ($vc_fld);
863 print "$vc_fld:$value\n";
871 my $cn = param_utf8 ('cn');
875 my $cn_html = encode_entities ($cn);
877 my $person = LiCoM::Person->load ($cn);
878 die unless ($person);
880 my ($mail) = $person->get ('mail');
884 my ($password) = $person->get ('password');
889 $password = pwgen ();
890 $person->set ('password', [$password]);
892 $password_html = encode_entities ($password);
894 $message = qq(The password for the record "$cn_html" is "$password_html".);
898 if (action_verify_send_mail ($person))
900 my $mail_html = encode_entities ($mail);
901 $message .= qq( A request for verification has been sent to $mail_html.);
906 $message .= q( There was no e-mail address, thus no verification request could be sent.);
909 print qq(\t\t<div class="message">$message</div>\n);
914 sub action_verify_send_mail
917 my $owner = LiCoM::Person->load ($UserCN);
920 my ($owner_mail) = $owner->get ('mail');
923 my $cn_uri = uri_escape_utf8 ($UserCN);
924 print qq(\t\t<div class="error">You have no email set in your own profile. <a href="$MySelf?action=edit&cn=$cn_uri">Edit it now</a>!</div>\n);
929 for (keys %FieldNames)
931 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
935 my $person_name = $person->name ();
936 my ($person_mail) = $person->get ('mail');
937 my $person_gn = $person->firstname ();
938 my ($password) = $person->get ('password');
940 my $host = $ENV{'HTTP_HOST'};
941 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
943 open ($smh, '|-', '/usr/sbin/sendmail', '-t', '-f', $owner_mail) or die ("open (sendmail): $!");
945 To: $person_name <$person_mail>
946 From: $UserCN <$owner_mail>
947 Subject: Please verify our entry in my address book
951 the following is your entry in my address book:
956 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
957 my @values = $person->get ($field);
961 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
966 If this entry is outdated or incomplete, please take a minute and correct it.
968 Username: $person_name
971 Thank you very much :)
976 This message was automatically generated by LiCoM,
977 http://verplant.org/licom/
986 my $cn = param_utf8 ('cn');
989 my $person = LiCoM::Person->load ($cn);
992 my $cn_uri = uri_escape_utf8 ($cn);
993 my $cn_html = encode_entities ($cn);
996 <h2>Really delete $cn_html?</h2>
999 You are about to delete <strong>$cn_html</strong>.
1000 Are you totally, absolutely sure you want to do this?
1004 [<a href="$MySelf?action=expunge&cn=$cn_uri">Yes, delete</a>]
1005 [<a href="$MySelf?action=detail&cn=$cn_uri">No, keep</a>]
1013 my $cn = param_utf8 ('cn');
1016 my $cn_html = encode_entities ($cn);
1018 my $person = LiCoM::Person->load ($cn);
1024 <div>$cn_html has been deleted.</div>
1030 sub action_edit_group
1032 my $group_name = param_utf8 ('group') or die;
1034 my $group_name_html = encode_entities ($group_name);
1036 my $group_obj = LiCoM::Group->load ($group_name);
1040 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1044 $group_name_html = encode_entities ($group_obj->name ());
1046 my $desc_html = encode_entities ($group_obj->description () || '');
1049 <h2>Edit contact group "$group_name_html"</h2>
1050 <form action="$MySelf" method="post" accept-charset="UTF-8">
1051 <input type="hidden" name="action" value="save_group" />
1052 <input type="hidden" name="group" value="$group_name_html" />
1056 <td>$group_name_html</td>
1059 <th>Description</th>
1060 <td><input type="text" name="description" value="$desc_html" /></td>
1063 <th colspan="2"><input type="submit" name="button" value="Save" /></th>
1070 sub action_save_group
1072 my $group_name = param_utf8 ('group') or die;
1074 my $group_name_html = encode_entities ($group_name);
1076 my $group_obj = LiCoM::Group->load ($group_name);
1080 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1084 my $desc = param_utf8 ('description');
1085 $group_obj->description ($desc);
1094 $title = q(Lightweight Contact Manager) unless ($title);
1096 $title = encode_entities ($title);
1099 Content-Type: text/html; charset=UTF-8
1103 <title>$title</title>
1104 <style type="text/css">
1111 background-color: inherit;
1112 text-decoration: none;
1117 text-decoration: underline;
1123 background-color: inherit;
1129 background-color: white;
1135 background-color: yellow;
1139 border: 2px solid red;
1145 background-color: white;
1155 border-top: 1px solid black;
1162 background-color: inherit;
1163 text-decoration: none;
1168 text-decoration: underline;
1173 border-top: 1px solid black;
1181 background-color: transparent;
1187 padding-bottom: 1ex;
1188 border-bottom: 1px solid black;
1217 border: 2px solid #d0d0d0;
1228 background-color: #e8e8e8;
1229 vertical-align: top;
1235 background-color: #d0d0d0;
1238 vertical-align: top;
1252 background-color: inherit;
1253 text-decoration: underline;
1256 div.topmenu, div.menu
1279 border-collapse: collapse;
1294 border-bottom-width: 2px;
1299 border: 1px solid black;
1300 vertical-align: top;
1318 my $search = param_utf8 ('search') || '';
1319 $search = encode_entities ($search);
1321 <div class="topmenu">
1322 <form action="$MySelf" method="post" accept-charset="UTF-8">
1323 <input type="hidden" name="action" value="browse" />
1324 <input type="submit" name="button" value="Browse" />
1326 <form action="$MySelf" method="post" accept-charset="UTF-8">
1327 <input type="hidden" name="action" value="search" />
1328 <input type="text" name="search" value="$search" />
1329 <input type="submit" name="button" value="Search" />
1331 <form action="$MySelf" method="post" accept-charset="UTF-8">
1332 <input type="hidden" name="action" value="edit" />
1333 <input type="hidden" name="dn" value="" />
1334 <input type="submit" name="button" value="Add New" />
1339 print "\t\t<h1>$title</h1>\n";
1346 "Lightweight Contact Manager",
1347 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1348 <octo at verplant.org>
1357 my $len = @_ ? shift : 6;
1366 while (length ($retval) < $len)
1368 my $chr = int (rand (128));
1370 if ($chr >= 48 and $chr < 58)
1374 elsif ($chr >= 65 and $chr < 91)
1378 elsif ($chr >= 97 and $chr < 123)
1386 $retval .= chr ($chr);
1389 $retval = '' if (!$numbers or !$lchars or !$uchars);
1398 for (param_utf8 ('uri'))
1403 if ($val !~ m#^[a-zA-Z]+://#)
1405 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1410 for (param_utf8 ('homephone'), param_utf8 ('cellphone'), param_utf8 ('officephone'), param_utf8 ('fax'))
1413 next unless ($number);
1415 if ($number !~ m/^\+[0-9 \-]+$/)
1417 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1422 print qq(\t\t<div class="error">\n) if (@errors);
1423 for (my $i = 0; $i < scalar (@errors); $i++)
1425 my $e = $errors[$i];
1427 print "<br />\n" if ($i);
1430 print qq(\n\t\t</div>\n\n) if (@errors);
1432 return (scalar (@errors));
1440 my $value_uri = uri_escape_utf8 ($value);
1441 my $value_html = encode_entities ($value);
1443 if ($field eq 'group')
1445 return (qq(<a href="$MySelf?action=browse&group=$value_uri">$value_html</a>));
1447 elsif ($field eq 'uri')
1449 if ($value =~ m#^([a-z]+)://(.+)$#)
1451 $value_uri = $1 . '://' . uri_escape_utf8 ($2);
1455 $value_uri = 'http://' . uri_escape_utf8 ($value);
1457 return (qq(<a href="$value_uri" class="extern">$value_html</a>));
1459 elsif ($field eq 'mail')
1461 return (qq(<a href="mailto:$value_uri" class="mail">$value_html</a>));
1463 return ($value_html);
1468 my $contacts = @_ ? shift : {};
1473 my @values = grep { $_ } (param_utf8 ($field));
1475 next unless (@values);
1477 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1481 $_ =~ s/[^0-9 \-]//g;
1482 $_ = '+' . $_ if ($_);
1486 $contacts->{$field} = [@values] if (@values);
1494 my $str = join ('', @_);
1496 # Taken from here: <http://www.w3.org/International/questions/qa-forms-utf-8>
1497 return ($str =~ m/^(
1498 [\x09\x0A\x0D\x20-\x7E] # ASCII
1499 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1500 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1501 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1502 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1503 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1504 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1505 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1514 @ret = grep { is_valid_utf8 ($_) } (param (@args));
1515 $_ = decode ('UTF-8', $_) for (@ret);
1516 return (wantarray () ? @ret : $ret[0]);
1521 return (uri_escape (encode ('UTF-8', shift)));