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]
73 my @files = (qw(/etc/licom/licom.conf));
74 push (@files, './licom.conf') if (-r './licom.conf');
78 # make sure AuthLDAPRemoteUserIsDN is enabled.
79 die unless ($ENV{'REMOTE_USER'});
80 #set_config ('base_dn', $ENV{'REMOTE_USER'});
82 die ("Configuration is incomplete") unless (defined (get_config ('uri'))
83 and defined (get_config ('base_dn'))
84 and defined (get_config ('bind_dn'))
85 and defined (get_config ('password')));
87 LiCoM::Connection->connect
89 uri => get_config ('uri'),
90 bind_dn => get_config ('bind_dn'),
91 password => get_config ('password')
92 ) or die ("Unable to connect to LDAP directory server " . get_config ('uri'));
94 our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
96 if (!$UserID and $Action ne 'save')
103 die ("UserCN is not set. Make sure `AuthLDAPRemoteUserIsDN' in enabled.");
106 if (!defined ($Actions{$Action}))
108 die ("No such action: $Action");
111 if (ref ($Actions{$Action}) eq 'CODE')
113 $Actions{$Action}->();
115 elsif (ref ($Actions{$Action}) eq 'ARRAY')
117 for (@{$Actions{$Action}})
123 LiCoM::Connection->disconnect ();
131 my $group = param_utf8 ('group');
132 $group = shift if (@_);
137 my @groups = LiCoM::Group->all ();
139 print qq(\t\t<h2>Contact groups</h2>\n\t\t<ul class="groups">\n);
143 my @members = $group->get_members ();
144 my $members = scalar (@members);
145 my $group_name = $group->name ();
146 my $group_uri = uri_escape_utf8 ($group_name);
147 my $desc = $group->description ();
149 print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_uri">#,
150 encode_entities ($group_name),
151 qq#</a> ($members Member#, ($members == 1 ? ')' : 's)');
152 print qq(<br />\n\t\t\t\t<span class="description">),
153 encode_entities ($desc) . '</span>' if ($desc);
158 print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
163 [<a href="$MySelf?action=list">List all</a>]
169 my $group_obj = LiCoM::Group->load ($group);
170 my $group_uri = uri_escape_utf8 ($group_obj->name ());
171 my $group_html = encode_entities ($group_obj->name ());
172 my @member_names = $group_obj->get_members ();
173 my $desc = $group_obj->description ();
174 my $desc_html = encode_entities ($desc || '');
176 print qq(\t\t<h2>Contact group "$group_html"</h2>\n);
177 print qq(\t\t<div>$desc_html</div>\n) if ($desc);
178 print qq(\t\t<ul class="results">\n);
179 for (sort (@member_names))
182 my $cn_uri = uri_escape_utf8 ($cn);
183 my $cn_html = encode_entities ($cn);
185 print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
191 [<a href="$MySelf?action=list&group=$group_uri">List</a>]
192 [<a href="$MySelf?action=browse">Back</a>]
193 [<a href="$MySelf?action=edit_group&group=$group_uri">Edit</a>]
201 my $group_name = param_utf8 ('group');
202 $group_name = shift if (@_);
205 my $group_name_html = encode_entities ($group_name || '');
207 my $title = $group_name
208 ? "List of group "$group_name_html""
209 : 'List of all addresses';
210 my @fields = (qw(address homephone cellphone officephone fax mail));
215 my $group_obj = LiCoM::Group->load ($group_name);
220 Unable to load group "$group_name_html".
225 for ($group_obj->get_members ())
228 my $person_obj = LiCoM::Person->load ($cn);
232 print STDERR "Unable to load cn = $cn;\n";
235 push (@all, $person_obj);
240 @all = LiCoM::Person->search ([[group => $group_name]]);
252 print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
254 print "\t\t\t</tr>\n";
256 for (sort { $a->name () cmp $b->name () } (@all))
259 my $cn = $person->name ();
260 my $sn = $person->lastname ();
261 my $gn = $person->firstname ();
263 my $cn_uri = uri_escape_utf8 ($cn);
264 my $cn_html = encode_entities ("$sn, $gn");
266 print "\t\t\t<tr>\n",
267 qq(\t\t\t\t<td><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></td>\n);
272 my @values = $person->get ($field);
273 print "\t\t\t\t<td>" . join ('<br />', map { markup_field ($field, $_) } (@values)) . "</td>\n";
276 print "\t\t\t</tr>\n";
278 print "\t\t</table>\n\n";
282 my $group_esc = uri_escape_utf8 ($group_name);
283 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
287 print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
293 my $cn = param_utf8 ('cn');
297 my $cn_html = encode_entities ($cn);
298 my $cn_uri = uri_escape_utf8 ($cn);
300 my $person = LiCoM::Person->load ($cn);
303 print qq(\t<div>Entry "$cn_html" could not be loaded from DB.</div>\n);
307 print qq(\t\t<h2>Details for $cn_html</h2>\n);
310 <table class="detail">
319 my $values = $person->get ($field);
320 my $num = scalar (@$values);
321 my $field_name = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
325 $field_name = encode_entities ($field_name);
327 print "\t\t\t<tr>\n";
330 print qq(\t\t\t\t<th rowspan="$num">$field_name</th>\n);
334 print qq(\t\t\t\t<th>$field_name</th>\n);
337 for (my $i = 0; $i < $num; $i++)
339 my $val = markup_field ($field, $values->[$i]);
341 print "\t\t\t<tr>\n" if ($i);
342 print "\t\t\t\t<td>$val</td>\n",
347 my @groups = LiCoM::Group->load_by_member ($cn);
350 my $num = scalar (@groups);
351 print "\t\t\t<tr>\n",
352 "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
353 for (my $i = 0; $i < $num; $i++)
355 my $group = $groups[$i];
356 my $group_name = $group->name ();
357 my $group_uri = uri_escape_utf8 ($group_name);
358 my $group_html = encode_entities ($group_name);
360 print "\t\t\t<tr>\n" if ($i != 0);
361 print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_uri">$group_html</a></td>\n),
370 [<a href="$MySelf?action=verify&cn=$cn_uri">Verify</a>]
371 [<a href="$MySelf?action=vcard&cn=$cn_uri">vCard</a>]
372 [<a href="$MySelf?action=edit&cn=$cn_uri">Edit</a>]
373 [<a href="$MySelf?action=delete&cn=$cn_uri">Delete</a>]
381 my $search = param_utf8 ('search');
384 $search =~ s/[^\s\w]//g;
388 print qq(\t<div class="error">Sorry, the empty search is not allowed.</div>\n);
393 my @patterns = split (m/\s+/, $search);
399 push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
402 my @matches = LiCoM::Person->search (@filter);
406 print qq(\t<div>No entries matched your search.</div>\n);
410 if (scalar (@matches) == 1)
412 my $person = shift (@matches);
413 my $cn = $person->name ();
418 print qq(\t<ul class="result">\n);
419 for (sort { $a->name () cmp $b->name () } (@matches))
422 my $cn = $person->name ();
423 my $cn_uri = uri_escape_utf8 ($cn);
424 my $cn_html = encode_entities ($cn);
426 print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
435 my $cn = param_utf8 ('cn');
437 $cn = $opts{'cn'} if (defined ($opts{'cn'}));
440 my $cn_html = encode_entities ($cn);
456 $contacts->{$_} = [] for (@MultiFields);
460 $person = LiCoM::Person->load ($cn);
464 print qq(\t<div class="error">Unable to load CN "$cn". Sorry.</div>\n);
468 $lastname = $person->lastname ();
469 $firstname = $person->firstname ();
473 $contacts->{$_} = $person->get ($_);
477 $lastname = param_utf8 ('lastname') if (param_utf8 ('lastname') and $UserID);
478 $firstname = param_utf8 ('firstname') if (param_utf8 ('firstname') and $UserID);
480 get_contacts ($contacts);
482 $lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
483 $firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
484 $lastname_html = encode_entities ($lastname);
485 $firstname_html = encode_entities ($firstname);
490 @{$contacts->{$field}} = @{$opts{$field}} if (defined ($opts{$field}));
495 print "\t\t<h2>Edit contact $cn_html</h2>\n";
499 print "\t\t<h2>Create new contact</h2>\n";
503 <form action="$MySelf" method="post" accept-charset="UTF-8">
504 <input type="hidden" name="action" value="save" />
505 <input type="hidden" name="cn" value="$cn_html" />
513 <td><input type="text" name="lastname" value="$lastname_html"
514 onChange="updateTextbox ('lastname');"
515 onKeyUp="updateTextbox ('lastname');"
516 onBlur="updateTextbox ('lastname');"
522 print qq(\t\t\t\t<td>$lastname_html</td>\n);
532 <td><input type="text" name="firstname" value="$firstname_html"
533 onChange="updateTextbox ('firstname');"
534 onKeyUp="updateTextbox ('firstname');"
535 onBlur="updateTextbox ('firstname');"
541 print qq(\t\t\t\t<td>$firstname_html</td>\n);
544 print "\t\t\t</tr>\n";
549 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
550 my @values = @{$contacts->{$field}};
552 next if ($field eq 'group');
556 $field = encode_entities ($field);
557 $print = encode_entities ($print);
561 my $value = encode_entities ($_);
566 <td><input type="text" name="$field" value="$value"
567 onChange="updateTextbox ('$field');"
568 onKeyUp="updateTextbox ('$field');"
569 onBlur="updateTextbox ('$field');"
578 my @all_groups = LiCoM::Group->all ();
582 print "\t\t\t<tr>\n",
583 "\t\t\t\t<th>Group(s)</th>\n",
584 qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
589 my $group_name = encode_entities ($group->name ());
592 if (grep { $cn eq $_ } ($group->get_members ()))
594 $selected = ' selected="selected"';
597 print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
599 print "\t\t\t\t</select></td>\n",
606 <td><input type="text" name="newgroup" value=""
607 onChange="updateTextbox ('newgroup');"
608 onKeyUp="updateTextbox ('newgroup');"
609 onBlur="updateTextbox ('newgroup');"
617 <th colspan="2" class="menu">
622 <input type="submit" name="button" value="Cancel" />
623 <input type="submit" name="button" value="Apply" />
627 <input type="submit" name="button" value="Save" />
637 my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
639 if (verify_fields ())
641 action_edit (cn => $cn);
651 die unless ($UserID);
653 my $button = lc (param_utf8 ('button'));
656 if ($button eq 'cancel')
662 if (!param_utf8 ('lastname') or !param_utf8 ('firstname'))
664 print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
665 action_edit (cn => '');
669 my $lastname = param_utf8 ('lastname');
670 my $firstname = param_utf8 ('firstname');
672 my $contacts = get_contacts ();
674 my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
678 print qq(\t<div class="error">Unable to save entry. Sorry.</div>\n);
682 $cn = $person->name ();
684 for (param_utf8 ('group'))
687 my $group = LiCoM::Group->load ($group_name);
691 $group->add_members ($cn);
695 my $group_html = encode_entities ($group_name);
696 print qq(\t<div class="error">Group "$group_html" does not exist or could not be loaded.</div>\n);
700 if (param_utf8 ('newgroup'))
702 # FIXME add error handling
703 my $group_name = param_utf8 ('newgroup');
704 LiCoM::Group->create ($group_name, '', $cn);
707 if ($button eq 'apply')
709 action_edit (cn => $cn);
719 my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
721 my $person = LiCoM::Person->load ($cn);
722 die ("Unable to load CN `$cn'") unless ($person);
724 my $button = lc (param_utf8 ('button'));
727 if ($UserID and $button eq 'cancel')
735 my $lastname = param_utf8 ('lastname');
736 my $firstname = param_utf8 ('firstname');
738 my $old_cn = $person->name ();
742 \$lastname = $lastname<br />
743 \$firstname = $firstname<br />
748 $person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
749 $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
751 $cn = $person->name ();
753 # Change the cn's saved in the groups
756 my @groups = LiCoM::Group->load_by_member ($old_cn);
759 # ->del_members automatically deleted the
760 # group, if no more members exist. So this
761 # order is important.
762 print "<div><code>\$cn = " . encode_entities ($cn) . "; "
763 . "\$old_cn = " . encode_entities ($old_cn) . ";</code></div>\n";
764 $_->add_members ($cn);
765 $_->del_members ($old_cn);
767 } # if ($old_cn ne $cn)
770 my $contacts = get_contacts ();
776 next if (!$UserID and $field eq 'group');
778 if (defined ($contacts->{$field}))
780 my $values = $contacts->{$field};
781 $person->set ($field, $values);
785 $person->set ($field, []);
789 # only `authorized' users may see and change groups
792 my %changed_groups = map { $_ => 1 } (param_utf8 ('group'));
793 my @current_groups = LiCoM::Group->load_by_member ($cn);
795 for (@current_groups)
798 my $group_name = $group_obj->name ();
800 if (!defined ($changed_groups{$group_name}))
802 $group_obj->del_members ($cn);
806 delete ($changed_groups{$group_name});
809 for (keys %changed_groups)
812 my $group_obj = LiCoM::Group->load ($group_name) or die;
814 $group_obj->add_members ($cn);
817 if (param_utf8 ('newgroup'))
819 # FIXME add error handling
820 my $group_name = param_utf8 ('newgroup');
821 LiCoM::Group->create ($group_name, '', $cn);
828 <h3>Your changes have been saved.</h3>
829 <p>Thank you very much for taking the time to keep this record up to date.</p>
834 if ($button eq 'apply' or !$UserID)
836 action_edit (cn => $cn);
846 my $cn = param_utf8 ('cn');
850 my $person = LiCoM::Person->load ($cn);
851 die unless ($person);
855 homephone => 'TEL;TYPE=home,voice',
856 cellphone => 'TEL;TYPE=cell',
857 officephone => 'TEL;TYPE=work,voice',
858 fax => 'TEL;TYPE=fax',
864 my $sn = $person->lastname ();
865 my $gn = $person->firstname ();
866 my $cn_esc = uri_escape_utf8 ($cn);
869 Content-Type: text/x-vcard
870 Content-Disposition: attachment; filename="$cn.vcf"
881 my $vc_fld = $vcard_types{$field};
882 my $values = $person->get ($field);
884 next unless ($vc_fld);
889 print "$vc_fld:$value\n";
897 my $cn = param_utf8 ('cn');
901 my $cn_html = encode_entities ($cn);
903 my $person = LiCoM::Person->load ($cn);
904 die unless ($person);
906 my ($mail) = $person->get ('mail');
910 my ($password) = $person->get ('password');
915 $password = pwgen ();
916 $person->set ('password', [$password]);
918 $password_html = encode_entities ($password);
920 $message = qq(The password for the record "$cn_html" is "$password_html".);
924 if (action_verify_send_mail ($person))
926 my $mail_html = encode_entities ($mail);
927 $message .= qq( A request for verification has been sent to $mail_html.);
932 $message .= q( There was no e-mail address, thus no verification request could be sent.);
935 print qq(\t\t<div class="message">$message</div>\n);
940 sub action_verify_send_mail
943 my $owner = LiCoM::Person->load ($UserCN);
946 my ($owner_mail) = $owner->get ('mail');
949 my $cn_uri = uri_escape_utf8 ($UserCN);
950 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);
955 for (keys %FieldNames)
957 $max_width = length $FieldNames{$_} if ($max_width < length $FieldNames{$_});
961 my $person_name = $person->name ();
962 my ($person_mail) = $person->get ('mail');
963 my $person_gn = $person->firstname ();
964 my ($password) = $person->get ('password');
966 my $host = $ENV{'HTTP_HOST'};
967 my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
969 open ($smh, '|-', '/usr/sbin/sendmail', '-t', '-f', $owner_mail) or die ("open (sendmail): $!");
971 To: $person_name <$person_mail>
972 From: $UserCN <$owner_mail>
973 Subject: Please verify our entry in my address book
977 the following is your entry in my address book:
982 my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
983 my @values = $person->get ($field);
987 printf $smh ('%'.$max_width."s: %-s\n", $print, $_);
992 If this entry is outdated or incomplete, please take a minute and correct it.
994 Username: $person_name
997 Thank you very much :)
1002 This message was automatically generated by LiCoM,
1003 http://verplant.org/licom/
1012 my $cn = param_utf8 ('cn');
1015 my $person = LiCoM::Person->load ($cn);
1018 my $cn_uri = uri_escape_utf8 ($cn);
1019 my $cn_html = encode_entities ($cn);
1022 <h2>Really delete $cn_html?</h2>
1025 You are about to delete <strong>$cn_html</strong>.
1026 Are you totally, absolutely sure you want to do this?
1030 [<a href="$MySelf?action=expunge&cn=$cn_uri">Yes, delete</a>]
1031 [<a href="$MySelf?action=detail&cn=$cn_uri">No, keep</a>]
1039 my $cn = param_utf8 ('cn');
1042 my $cn_html = encode_entities ($cn);
1044 my $person = LiCoM::Person->load ($cn);
1050 <div>$cn_html has been deleted.</div>
1056 sub action_edit_group
1058 my $group_name = param_utf8 ('group') or die;
1060 my $group_name_html = encode_entities ($group_name);
1062 my $group_obj = LiCoM::Group->load ($group_name);
1066 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1070 $group_name_html = encode_entities ($group_obj->name ());
1072 my $desc_html = encode_entities ($group_obj->description () || '');
1075 <h2>Edit contact group "$group_name_html"</h2>
1076 <form action="$MySelf" method="post" accept-charset="UTF-8">
1077 <input type="hidden" name="action" value="save_group" />
1078 <input type="hidden" name="group" value="$group_name_html" />
1082 <td>$group_name_html</td>
1085 <th>Description</th>
1086 <td><input type="text" name="description" value="$desc_html"
1087 onChange="updateTextbox ('description');"
1088 onKeyUp="updateTextbox ('description');"
1089 onBlur="updateTextbox ('description');"
1093 <th colspan="2"><input type="submit" name="button" value="Save" /></th>
1100 sub action_save_group
1102 my $group_name = param_utf8 ('group') or die;
1104 my $group_name_html = encode_entities ($group_name);
1106 my $group_obj = LiCoM::Group->load ($group_name);
1110 print qq(\t<div class="error">Group "$group_name_html" does not exist or could not be loaded.</div>\n);
1114 my $desc = param_utf8 ('description');
1115 $group_obj->description ($desc);
1124 $title = q(Lightweight Contact Manager) unless ($title);
1126 $title = encode_entities ($title);
1129 Content-Type: text/html; charset=UTF-8
1133 <title>$title</title>
1134 <link rel="stylesheet" type="text/css" href="style.screen.css" media="screen" />
1135 <link rel="stylesheet" type="text/css" href="style.print.css" media="print" />
1136 <script type="text/javascript" src="html_entities.js"></script>
1137 <script type="text/javascript">
1138 function updateTextbox (name)
1140 var arrTb = document.getElementsByName (name);
1144 if (!arrTb || (arrTb.length < 1))
1147 for (var i = 0; i < arrTb.length; i++)
1149 objStr = decode_entities (arrTb[i].value);
1150 if (arrTb[i].value != objStr)
1152 arrTb[i].value = objStr;
1167 my $search = param_utf8 ('search') || '';
1168 $search = encode_entities ($search);
1170 <div class="topmenu">
1171 <form action="$MySelf" method="post" accept-charset="UTF-8">
1172 <input type="hidden" name="action" value="browse" />
1173 <input type="submit" name="button" value="Browse" />
1175 <form action="$MySelf" method="post" accept-charset="UTF-8">
1176 <input type="hidden" name="action" value="search" />
1177 <input type="text" name="search" value="$search"
1178 onChange="updateTextbox ('search');"
1179 onKeyUp="updateTextbox ('search');"
1180 onBlur="updateTextbox ('search');"
1182 <input type="submit" name="button" value="Search" />
1184 <form action="$MySelf" method="post" accept-charset="UTF-8">
1185 <input type="hidden" name="action" value="edit" />
1186 <input type="hidden" name="dn" value="" />
1187 <input type="submit" name="button" value="Add New" />
1192 print "\t\t<h1>$title</h1>\n";
1199 "Lightweight Contact Manager",
1200 written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
1201 <octo at verplant.org>
1210 my $len = @_ ? shift : 6;
1219 while (length ($retval) < $len)
1221 my $chr = int (rand (128));
1223 if ($chr >= 48 and $chr < 58)
1227 elsif ($chr >= 65 and $chr < 91)
1231 elsif ($chr >= 97 and $chr < 123)
1239 $retval .= chr ($chr);
1242 $retval = '' if (!$numbers or !$lchars or !$uchars);
1251 for (param_utf8 ('uri'))
1256 if ($val !~ m#^[a-zA-Z]+://#)
1258 push (@errors, 'URIs have to begin with a protocol, e.g. "http://", "ftp://" etc.');
1263 for (param_utf8 ('homephone'), param_utf8 ('cellphone'), param_utf8 ('officephone'), param_utf8 ('fax'))
1266 next unless ($number);
1268 if ($number !~ m/^\+[0-9 \-]+$/)
1270 push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. "+49 911-123456"');
1275 print qq(\t\t<div class="error">\n) if (@errors);
1276 for (my $i = 0; $i < scalar (@errors); $i++)
1278 my $e = $errors[$i];
1280 print "<br />\n" if ($i);
1283 print qq(\n\t\t</div>\n\n) if (@errors);
1285 return (scalar (@errors));
1293 my $value_uri = uri_escape_utf8 ($value);
1294 my $value_html = encode_entities ($value);
1296 if ($field eq 'group')
1298 return (qq(<a href="$MySelf?action=browse&group=$value_uri">$value_html</a>));
1300 elsif ($field eq 'uri')
1302 if ($value =~ m#^([a-z]+)://(.+)$#)
1304 $value_uri = $1 . '://' . uri_escape_utf8 ($2);
1308 $value_uri = 'http://' . uri_escape_utf8 ($value);
1310 return (qq(<a href="$value_uri" class="extern">$value_html</a>));
1312 elsif ($field eq 'mail')
1314 return (qq(<a href="mailto:$value_uri" class="mail">$value_html</a>));
1316 return ($value_html);
1321 my $contacts = @_ ? shift : {};
1326 my @values = grep { $_ } (param_utf8 ($field));
1328 next unless (@values);
1330 if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
1334 $_ =~ s/[^0-9 \-]//g;
1335 $_ = '+' . $_ if ($_);
1339 $contacts->{$field} = [@values] if (@values);
1347 my $str = join ('', @_);
1349 # Taken from here: <http://www.w3.org/International/questions/qa-forms-utf-8>
1350 return ($str =~ m/^(
1351 [\x09\x0A\x0D\x20-\x7E] # ASCII
1352 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1353 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1354 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1355 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1356 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1357 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1358 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1367 @ret = grep { is_valid_utf8 ($_) } (param (@args));
1368 $_ = decode ('UTF-8', $_) for (@ret);
1369 return (wantarray () ? @ret : $ret[0]);
1374 return (uri_escape (encode ('UTF-8', shift)));