Many more fixes.. Trying to get all this work now..
[licom.git] / licom.cgi
index 40bb8a3..f0dfd12 100755 (executable)
--- a/licom.cgi
+++ b/licom.cgi
@@ -9,13 +9,14 @@ use CGI::Carp (qw(fatalsToBrowser));
 use URI::Escape;
 use Data::Dumper;
 
-use LiCoM::Config (qw(get_config));
-use LiCoM::Person;
+use LiCoM::Config (qw(get_config set_config read_config));
+use LiCoM::Connection ();
+use LiCoM::Group ();
+use LiCoM::Person ();
 
 our $Debug = 0;
-our $Config = {};
 
-our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
+our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
 
 our %FieldNames = 
 (
@@ -44,27 +45,30 @@ our %Actions =
        save    => [\&html_start, \&action_save,    \&html_end],
        search  => [\&html_start, \&action_search,  \&html_end],
        verify  => [\&html_start, \&action_verify,  \&html_end],
+       delete  => [\&html_start, \&action_ask_del,  \&html_end],
+       expunge => [\&html_start, \&action_do_del,  \&html_end],
        vcard   => \&action_vcard
 );
 
-$Config = get_config ();
+read_config ();
 
 # make sure AuthLDAPRemoteUserIsDN is enabled.
 die unless ($ENV{'REMOTE_USER'});
-$Config->{'base_dn'} = $ENV{'REMOTE_USER'};
+set_config ('base_dn', $ENV{'REMOTE_USER'});
 
-die unless (defined ($Config->{'uri'}) and defined ($Config->{'base_dn'})
-       and defined ($Config->{'bind_dn'}) and defined ($Config->{'password'}));
+die unless (defined (get_config ('uri'))
+       and defined (get_config ('base_dn'))
+       and defined (get_config ('bind_dn'))
+       and defined (get_config ('password')));
 
-LiCoM::Person->connect
+LiCoM::Connection->connect
 (
-       uri     => $Config->{'uri'},
-       base_dn => $Config->{'base_dn'},
-       bind_dn => $Config->{'bind_dn'},
-       password => $Config->{'password'}
+       uri      => get_config ('uri'),
+       bind_dn  => get_config ('bind_dn'),
+       password => get_config ('password')
 ) or die;
 
-our ($UserCN, $UserID) = LiCoM::Person->get_user ($Config->{'base_dn'});
+our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
 
 if (!$UserID and $Action ne 'save')
 {
@@ -93,7 +97,7 @@ elsif (ref ($Actions{$Action}) eq 'ARRAY')
        }
 }
 
-LiCoM::Person->disconnect ();
+LiCoM::Connection->disconnect ();
 
 exit (0);
 
@@ -105,6 +109,8 @@ sub action_browse
        $group = shift if (@_);
        $group ||= '';
 
+       return ('');
+
        my @all;
        if ($group)
        {
@@ -322,6 +328,7 @@ EOF
                        [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
                        [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
                        [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
+                       [<a href="$MySelf?action=delete&cn=$cn_esc">Delete</a>]
                </div>
 
 EOF
@@ -479,7 +486,7 @@ EOF
                my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
                my @values = @{$contacts->{$field}};
 
-               next if (!$UserID and $field eq 'group');
+               next if ($field eq 'group');
 
                push (@values, '');
                
@@ -496,6 +503,31 @@ EOF
                }
        }
 
+       if ($UserID)
+       {
+               my %c_groups = map { $_ => 1 } (@{$contacts->{'group'}});
+               my %a_groups = ();
+               my @a_persons = LiCoM::Person->search ();
+
+               for (@a_persons)
+               {
+                       $a_groups{$_} = 1 for ($_->get ('group'));
+               }
+
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<th>", $FieldNames{'group'}, "</th>\n",
+               qq(\t\t\t\t<td><select name="group" multiple="multiple">\n);
+               for (sort (keys %a_groups))
+               {
+                       my $group = $_;
+                       my $selec = defined ($c_groups{$group}) ? ' selected="selected"' : '';
+
+                       print qq(\t\t\t\t\t<option value="$group"$selec>$group</option>\n);
+               }
+               print "\t\t\t\t</select></td>\n",
+               "\t\t\t</tr>\n";
+       }
+
        print <<EOF;
                        <tr>
                                <th colspan="2" class="menu">
@@ -695,12 +727,12 @@ sub action_verify
        $mail ||= '';
 
        my $message;
-       my $password = $person->password ();
+       my $password = $person->get ('password');
 
        if (!$password)
        {
                $password = pwgen ();
-               $person->password ($password);
+               $person->set ('password', $password);
        }
 
        $message = qq(The password for the record &quot;$cn&quot; is &quot;$password&quot;.);
@@ -746,10 +778,10 @@ sub action_verify_send_mail
        my $person_name = $person->name ();
        my ($person_mail) = $person->get ('mail');
        my $person_gn = $person->firstname ();
-       my $password = $person->password ();
+       my $password = $person->get ('password');
 
        my $host = $ENV{'HTTP_HOST'};
-       my $url = 'http://' . $host . $MySelf;
+       my $url = (defined ($ENV{'HTTPS'}) ? 'https://' : 'http://') . $host . $MySelf;
        
        open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
        print $smh <<EOM;
@@ -775,18 +807,66 @@ EOM
        print $smh <<EOM;
 
 If this entry is outdated or incomplete, please take a minute and correct it.
-  Address:  $url
+  Address: $url
  Username: $person_name
  Password: $password
 
-Thank you very much :) Regards,
+Thank you very much :)
+
+Regards,
 $UserCN
+--
+This message was automatically generated by LiCoM,
+http://verplant.org/licom/
 EOM
        close ($smh);
 
        return (1);
 }
 
+sub action_ask_del
+{
+       my $cn = param ('cn');
+       $cn or die;
+
+       my $person = LiCoM::Person->load ($cn);
+       $person or die;
+
+       my $cn_esc = uri_escape ($cn);
+
+       print <<EOF;
+               <h2>Really delete $cn?</h2>
+
+               <div>
+                       You are about to delete <strong>$cn</strong>. Are you
+                       totally, absolutely sure you want to do this?
+               </div>
+
+               <div class="menu">
+                       [<a href="$MySelf?action=expunge&cn=$cn_esc">Yes, delete</a>]
+                       [<a href="$MySelf?action=detail&cn=$cn_esc">No, keep</a>]
+               </div>
+
+EOF
+}
+
+sub action_do_del
+{
+       my $cn = param ('cn');
+       $cn or die;
+
+       my $person = LiCoM::Person->load ($cn);
+       $person or die;
+
+       $person->delete ();
+
+       print <<EOF;
+               <div>$cn has been deleted.</div>
+
+EOF
+       action_browse ();
+}
+
 sub html_start
 {
        my $title = shift;
@@ -1003,6 +1083,7 @@ Content-Type: text/html; charset=UTF-8
 
        <body>
 EOF
+
        if ($UserID)
        {
                my $search = param ('search') || '';
@@ -1101,9 +1182,9 @@ sub verify_fields
                my $number = $_;
                next unless ($number);
 
-               if ($number !~ m/^\+/)
+               if ($number !~ m/^\+[0-9 \-]+$/)
                {
-                       push (@errors, 'Telephone numbers have to begin with the country code, e.g. &quot;+49 911 123456&quot;');
+                       push (@errors, 'Telephone numbers have to begin with the country code and only numbers, spaces and dashes are allowed, e.g. &quot;+49 911-123456&quot;');
                        last;
                }
        }
@@ -1136,8 +1217,8 @@ sub get_contacts
                {
                        for (@values)
                        {
-                               $_ =~ s/\D//g;
-                               $_ = '+' . $_;
+                               $_ =~ s/[^0-9 \-]//g;
+                               $_ = '+' . $_ if ($_);
                        }
                }