use strict;
use warnings;
+use Carp (qw(cluck confess));
+use Encode (qw(encode decode is_utf8));
+
use LiCoM::Config (qw(get_config));
use LiCoM::Connection (qw($Ldap));
for (keys %ValidFields)
{
my $key = $_;
- $obj->{$key} = $entry->get_value ($key, asref => $ValidFields{$key});
+ my $val = $entry->get_value ($key, asref => $ValidFields{$key});
+
+ if (ref ($val))
+ {
+ $obj->{$key} = [map { decode ('UTF-8', $_) } (@$val)];
+ }
+ else
+ {
+ $obj->{$key} = decode ('UTF-8', $val);
+ }
}
return (bless ($obj, $pkg));
if (!$retval)
{
- warn ("CN '$cn' could not be found");
- return (undef);
+ cluck ("CN '$cn' could not be found");
+ return;
}
return ($retval);
{
if (ref ($val) eq 'ARRAY')
{
- $entry->add ($field => [@$val]) if (@$val);
+ $entry->add ($field => [map { encode ('UTF-8', $_) } (@$val)]) if (@$val);
}
elsif (!ref ($val))
{
- $entry->add ($field => [$val]) if ($val);
+ $entry->add ($field => [encode ('UTF-8', $val)]) if ($val);
}
else
{
my $temp;
if (ref ($val) eq 'ARRAY')
{
- $temp = $val->[0];
+ $temp = encode ('UTF-8', $val->[0]);
}
elsif (!ref ($val))
{
- $temp = $val;
+ $temp = encode ('UTF-8', $val);
}
else
{
warn ("You cannot pass ref-type " . ref ($val));
}
- $entry->add ($field => $val) if (defined ($val) and $val);
+ $entry->add ($field => $temp) if (defined ($temp) and $temp);
}
}
+ # $sn and $gn are UTF-8
my $sn = $entry->get_value ('sn');
my $gn = $entry->get_value ('givenName');
if (!defined ($sn) or !defined ($gn))
{
warn ("sn or givenName not given");
- return (undef);
+ return;
}
- $ou = 'Person';
- $dn = "cn=$sn $gn,ou=$ou," . get_config ('base_dn');
+ $ou = encode ('UTF-8', 'Person');
+ $dn = "cn=$sn $gn,ou=$ou," . encode ('UTF-8', get_config ('base_dn'));
$entry->add (cn => "$sn $gn", ou => $ou);
$entry->dn ($dn);
if ($mesg->is_error ())
{
- warn ("Error while creating entry '$dn' on LDAP server: " . $mesg->error_text ());
- return (undef);
+ my $tmp = decode ('UTF-8', $dn);
+ warn ("Error while creating entry '$tmp' on LDAP server: " . $mesg->error_text ());
+ return;
}
return (new ($pkg, $entry));
$value =~ s/([\(\)\\])/\\$1/g;
+ confess ("Value is not UTF-8 encoded: `$value'") if (!is_utf8 ($value));
+
push (@disjunc, "($field=$value)");
}
$mesg = $Ldap->search
(
base => 'ou=Person,' . get_config ('base_dn'),
- filter => $filter
+ filter => encode ('UTF-8', $filter)
);
if ($mesg->is_error ())
my $dn = shift;
my ($search) = $dn =~ m/cn\s*=\s*([^,]+)/i;
- die unless ($search);
+ return unless ($search);
my $cn = '';
my $id = '';
if ($mesg->is_error ())
{
- warn ("Error while querying LDAP server: " . $mesg->error_text ());
- return ('');
+ cluck ("Error while querying LDAP server: " . $mesg->error_text ());
+ return;
}
for ($mesg->entries ())
sub _update_dn
{
+ confess ("Wrong number of arguments") if (@_ != 3);
my $obj = shift;
+
+ my $obj_new;
+ my %hash_new;
+
+ my $sn = shift;
+ my $gn = shift;
+
my $entry = $obj->{'ldap'};
- my $sn = $obj->{'sn'};
- my $gn = $obj->{'givenName'};
- my $cn = "$sn $gn";
- my $dn = "cn=$cn,ou=Person," . get_config ('base_dn');
- $obj->{'cn'} = $cn;
+ if (($sn eq $obj->{'sn'}) && ($gn eq $obj->{'givenName'}))
+ {
+ return;
+ }
- print STDERR "This is _update_dn, trying to set dn=$dn";
+ $hash_new{$_} = $obj->{$_} for (keys %ValidFields);
+ $hash_new{'sn'} = $sn;
+ $hash_new{'givenName'} = $gn;
+ delete ($hash_new{'cn'});
- $entry->changetype ('modify');
- $entry->replace (sn => $sn, givenName => $gn, cn => $cn);
- $entry->update ($Ldap);
- $entry->dn ($dn);
- $entry->update ($Ldap);
+ $obj_new = LiCoM::Person->create (%hash_new)
+ or confess ("Cannot duplicate LDAP entry");
+
+ $obj->delete ();
+
+ %$obj = %$obj_new;
+
+ return ($obj->{'dn'});
}
sub lastname
if (@_)
{
- $obj->{'sn'} = shift;
- _update_dn ($obj);
+ _update_dn ($obj, shift, $obj->{'givenName'});
}
return ($obj->{'sn'});
if (@_)
{
- $obj->{'givenName'} = shift;
- _update_dn ($obj);
+ _update_dn ($obj, $obj->{'sn'}, shift);
}
return ($obj->{'givenName'});
if (defined ($value))
{
+ $_ = encode ('UTF-8', $_) for (@$value);
+
$entry->changetype ('modify');
if ($ValidFields{$field})
use warnings;
use lib (qw(lib));
+use Encode (qw(encode decode is_utf8));
use CGI (':cgi');
use CGI::Carp (qw(fatalsToBrowser));
use URI::Escape;
our $MySelf = $ENV{'SCRIPT_NAME'};
-our $Action = param ('action');
+our $Action = param_utf8 ('action');
$Action ||= 'default';
our %Actions =
die unless ($ENV{'REMOTE_USER'});
#set_config ('base_dn', $ENV{'REMOTE_USER'});
-die unless (defined (get_config ('uri'))
+die ("Configuration is incomplete") unless (defined (get_config ('uri'))
and defined (get_config ('base_dn'))
and defined (get_config ('bind_dn'))
and defined (get_config ('password')));
uri => get_config ('uri'),
bind_dn => get_config ('bind_dn'),
password => get_config ('password')
-) or die ("LiCoM::Connection->connect failed.");
+) or die ("Unable to connect to LDAP directory server " . get_config ('uri'));
our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
sub action_browse
{
- my $group = param ('group');
+ my $group = param_utf8 ('group');
$group = shift if (@_);
$group ||= '';
my @members = $group->get_members ();
my $members = scalar (@members);
my $group_name = $group->name ();
- my $group_uri = uri_escape ($group_name);
+ my $group_uri = uri_escape_utf8 ($group_name);
my $desc = $group->description ();
print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_uri">#,
else
{
my $group_obj = LiCoM::Group->load ($group);
- my $group_uri = uri_escape ($group_obj->name ());
+ my $group_uri = uri_escape_utf8 ($group_obj->name ());
my $group_html = encode_entities ($group_obj->name ());
my @member_names = $group_obj->get_members ();
my $desc = $group_obj->description ();
for (sort (@member_names))
{
my $cn = $_;
- my $cn_uri = uri_escape ($cn);
+ my $cn_uri = uri_escape_utf8 ($cn);
my $cn_html = encode_entities ($cn);
print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
sub action_list
{
- my $group_name = param ('group');
+ my $group_name = param_utf8 ('group');
$group_name = shift if (@_);
$group_name ||= '';
my $sn = $person->lastname ();
my $gn = $person->firstname ();
- my $cn_uri = uri_escape ($cn);
+ my $cn_uri = uri_escape_utf8 ($cn);
my $cn_html = encode_entities ("$sn, $gn");
print "\t\t\t<tr>\n",
if ($group_name)
{
- my $group_esc = uri_escape ($group_name);
+ my $group_esc = uri_escape_utf8 ($group_name);
print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
}
else
sub action_detail
{
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn = shift if (@_);
die unless ($cn);
my $cn_html = encode_entities ($cn);
- my $cn_uri = uri_escape ($cn);
+ my $cn_uri = uri_escape_utf8 ($cn);
my $person = LiCoM::Person->load ($cn);
if (!$person)
{
my $group = $groups[$i];
my $group_name = $group->name ();
- my $group_uri = uri_escape ($group_name);
+ my $group_uri = uri_escape_utf8 ($group_name);
my $group_html = encode_entities ($group_name);
print "\t\t\t<tr>\n" if ($i != 0);
sub action_search
{
- my $search = param ('search');
+ my $search = param_utf8 ('search');
$search ||= '';
$search =~ s/[^\s\w]//g;
{
my $person = $_;
my $cn = $person->name ();
- my $cn_uri = uri_escape ($cn);
+ my $cn_uri = uri_escape_utf8 ($cn);
my $cn_html = encode_entities ($cn);
print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_uri">$cn_html</a></li>\n);
{
my %opts = @_;
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn = $opts{'cn'} if (defined ($opts{'cn'}));
$cn ||= '';
my $lastname;
my $firstname;
+ my $lastname_html;
+ my $firstname_html;
+
my $contacts = {};
$contacts->{$_} = [] for (@MultiFields);
}
}
- $lastname = param ('lastname') if (param ('lastname') and $UserID);
- $firstname = param ('firstname') if (param ('firstname') and $UserID);
+ $lastname = param_utf8 ('lastname') if (param_utf8 ('lastname') and $UserID);
+ $firstname = param_utf8 ('firstname') if (param_utf8 ('firstname') and $UserID);
get_contacts ($contacts);
$lastname = $opts{'lastname'} if (defined ($opts{'lastname'}));
$firstname = $opts{'firstname'} if (defined ($opts{'firstname'}));
+ $lastname_html = encode_entities ($lastname);
+ $firstname_html = encode_entities ($firstname);
+
for (@MultiFields)
{
my $field = $_;
}
print <<EOF;
- <form action="$MySelf" method="post">
+ <form action="$MySelf" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="save" />
<input type="hidden" name="cn" value="$cn_html" />
<table class="edit">
EOF
if ($UserID)
{
- print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
+ print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname_html" /></td>\n);
}
else
{
- print qq(\t\t\t\t<td>$lastname</td>\n);
+ print qq(\t\t\t\t<td>$lastname_html</td>\n);
}
print <<EOF;
</tr>
EOF
if ($UserID)
{
- print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
+ print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname_html" /></td>\n);
}
else
{
- print qq(\t\t\t\t<td>$firstname</td>\n);
+ print qq(\t\t\t\t<td>$firstname_html</td>\n);
}
print "\t\t\t</tr>\n";
sub action_save
{
- my $cn = $UserID ? param ('cn') : $UserCN;
+ my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
if (verify_fields ())
{
die unless ($UserID);
- my $button = lc (param ('button'));
+ my $button = lc (param_utf8 ('button'));
$button ||= 'save';
if ($button eq 'cancel')
return;
}
- if (!param ('lastname') or !param ('firstname'))
+ if (!param_utf8 ('lastname') or !param_utf8 ('firstname'))
{
print qq(\t<div class="error">You have to give both, first and lastname, to identify this record.</div>\n);
action_edit (cn => '');
return;
}
- my $lastname = param ('lastname');
- my $firstname = param ('firstname');
+ my $lastname = param_utf8 ('lastname');
+ my $firstname = param_utf8 ('firstname');
my $contacts = get_contacts ();
$cn = $person->name ();
- for (param ('group'))
+ for (param_utf8 ('group'))
{
my $group_name = $_;
my $group = LiCoM::Group->load ($group_name);
}
}
- if (param ('newgroup'))
+ if (param_utf8 ('newgroup'))
{
# FIXME add error handling
- my $group_name = param ('newgroup');
+ my $group_name = param_utf8 ('newgroup');
LiCoM::Group->create ($group_name, '', $cn);
}
sub action_update
{
- my $cn = $UserID ? param ('cn') : $UserCN;
- my $person = LiCoM::Person->load ($cn);
+ my $cn = $UserID ? param_utf8 ('cn') : $UserCN;
- die unless ($person);
+ my $person = LiCoM::Person->load ($cn);
+ die ("Unable to load CN `$cn'") unless ($person);
- my $button = lc (param ('button'));
+ my $button = lc (param_utf8 ('button'));
$button ||= 'save';
if ($UserID and $button eq 'cancel')
if ($UserID)
{
- my $lastname = param ('lastname');
- my $firstname = param ('firstname');
+ my $lastname = param_utf8 ('lastname');
+ my $firstname = param_utf8 ('firstname');
+
+ my $old_cn = $person->name ();
+
+ print <<HTML;
+<div><code>
+ \$lastname = $lastname<br />
+ \$firstname = $firstname<br />
+ \$old_cn = $old_cn
+</code></div>
+HTML
$person->lastname ($lastname) if ($lastname and $lastname ne $person->lastname ());
$person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
$cn = $person->name ();
- # FIXME Fix groups:
- # Each group is one entry of type (objectClass=groupOfNames)
- # with one or more `member' attributes. These attributes are
- # the `dn' (distinguished name) of the member entries.
+
+ # Change the cn's saved in the groups
+ if ($old_cn ne $cn)
+ {
+ my @groups = LiCoM::Group->load_by_member ($old_cn);
+ for (@groups)
+ {
+ # ->del_members automatically deleted the
+ # group, if no more members exist. So this
+ # order is important.
+ print "<div><code>\$cn = " . encode_entities ($cn) . "; "
+ . "\$old_cn = " . encode_entities ($old_cn) . ";</code></div>\n";
+ $_->add_members ($cn);
+ $_->del_members ($old_cn);
+ }
+ } # if ($old_cn ne $cn)
}
my $contacts = get_contacts ();
# only `authorized' users may see and change groups
if ($UserID)
{
- my %changed_groups = map { $_ => 1 } (param ('group'));
+ my %changed_groups = map { $_ => 1 } (param_utf8 ('group'));
my @current_groups = LiCoM::Group->load_by_member ($cn);
for (@current_groups)
$group_obj->add_members ($cn);
}
- if (param ('newgroup'))
+ if (param_utf8 ('newgroup'))
{
# FIXME add error handling
- my $group_name = param ('newgroup');
+ my $group_name = param_utf8 ('newgroup');
LiCoM::Group->create ($group_name, '', $cn);
}
}
sub action_vcard
{
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn = shift if (@_);
die unless ($cn);
my $sn = $person->lastname ();
my $gn = $person->firstname ();
- my $cn_esc = uri_escape ($cn);
+ my $cn_esc = uri_escape_utf8 ($cn);
print <<EOF;
Content-Type: text/x-vcard
sub action_verify
{
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn = shift if (@_);
die unless ($cn);
my ($owner_mail) = $owner->get ('mail');
if (!$owner_mail)
{
- my $cn_uri = uri_escape ($UserCN);
+ my $cn_uri = uri_escape_utf8 ($UserCN);
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);
return (0);
}
sub action_ask_del
{
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn or die;
my $person = LiCoM::Person->load ($cn);
$person or die;
- my $cn_uri = uri_escape ($cn);
+ my $cn_uri = uri_escape_utf8 ($cn);
my $cn_html = encode_entities ($cn);
print <<EOF;
sub action_do_del
{
- my $cn = param ('cn');
+ my $cn = param_utf8 ('cn');
$cn or die;
my $cn_html = encode_entities ($cn);
sub action_edit_group
{
- my $group_name = param ('group') or die;
+ my $group_name = param_utf8 ('group') or die;
my $group_name_html = encode_entities ($group_name);
print <<HTML;
<h2>Edit contact group "$group_name_html"</h2>
- <form action="$MySelf" method="post">
+ <form action="$MySelf" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="save_group" />
<input type="hidden" name="group" value="$group_name_html" />
<table>
sub action_save_group
{
- my $group_name = param ('group') or die;
+ my $group_name = param_utf8 ('group') or die;
my $group_name_html = encode_entities ($group_name);
return;
}
- my $desc = param ('description');
+ my $desc = param_utf8 ('description');
$group_obj->description ($desc);
action_browse ();
if ($UserID)
{
- my $search = param ('search') || '';
+ my $search = param_utf8 ('search') || '';
$search = encode_entities ($search);
print <<EOF;
<div class="topmenu">
- <form action="$MySelf" method="post">
+ <form action="$MySelf" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="browse" />
<input type="submit" name="button" value="Browse" />
</form>
- <form action="$MySelf" method="post">
+ <form action="$MySelf" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="search" />
<input type="text" name="search" value="$search" />
<input type="submit" name="button" value="Search" />
</form>
- <form action="$MySelf" method="post">
+ <form action="$MySelf" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="dn" value="" />
<input type="submit" name="button" value="Add New" />
sub verify_fields
{
my @errors = ();
- for (param ('uri'))
+ for (param_utf8 ('uri'))
{
my $val = $_;
next unless ($val);
}
}
- for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
+ for (param_utf8 ('homephone'), param_utf8 ('cellphone'), param_utf8 ('officephone'), param_utf8 ('fax'))
{
my $number = $_;
next unless ($number);
my $field = shift;
my $value = shift;
- my $value_uri = uri_escape ($value);
+ my $value_uri = uri_escape_utf8 ($value);
my $value_html = encode_entities ($value);
if ($field eq 'group')
{
if ($value =~ m#^([a-z]+)://(.+)$#)
{
- $value_uri = $1 . '://' . uri_escape ($2);
+ $value_uri = $1 . '://' . uri_escape_utf8 ($2);
}
else
{
- $value_uri = 'http://' . uri_escape ($value);
+ $value_uri = 'http://' . uri_escape_utf8 ($value);
}
return (qq(<a href="$value_uri" class="extern">$value_html</a>));
}
for (@MultiFields)
{
my $field = $_;
- my @values = grep { $_ } (param ($field));
+ my @values = grep { $_ } (param_utf8 ($field));
next unless (@values);
return ($contacts);
}
+
+sub is_valid_utf8
+{
+ my $str = join ('', @_);
+
+ # Taken from here: <http://www.w3.org/International/questions/qa-forms-utf-8>
+ return ($str =~ m/^(
+ [\x09\x0A\x0D\x20-\x7E] # ASCII
+ | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
+ | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
+ | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
+ | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
+ | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
+ | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
+ | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
+ )*$/x);
+}
+
+sub param_utf8
+{
+ my @args = @_;
+ my @ret = ();
+
+ @ret = grep { is_valid_utf8 ($_) } (param (@args));
+ $_ = decode ('UTF-8', $_) for (@ret);
+ return (wantarray () ? @ret : $ret[0]);
+}
+
+sub uri_escape_utf8
+{
+ return (uri_escape (encode ('UTF-8', shift)));
+}