Added copyright and license information to `licom.cgi'
[licom.git] / licom.cgi
index 4af14ad..70e1e72 100755 (executable)
--- a/licom.cgi
+++ b/licom.cgi
@@ -1,5 +1,21 @@
 #!/usr/bin/perl
 
+# LiCoM - Lightweight contact manager
+# Copyright (c) 2005-2006  Florian octo Forster <octo at verplant.org>
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation; only version 2 of the License is applicable.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software # Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
 use strict;
 use warnings;
 use lib (qw(lib));
@@ -9,12 +25,14 @@ use CGI::Carp (qw(fatalsToBrowser));
 use URI::Escape;
 use Data::Dumper;
 
-use 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 = 
 (
@@ -43,6 +61,8 @@ 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
 );
 
@@ -50,17 +70,21 @@ 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 (get_config ('uri'))
+       and defined (get_config ('base_dn'))
+       and defined (get_config ('bind_dn'))
+       and defined (get_config ('password')));
 
-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) = Person->get_user ($Config{'base_dn'});
+our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
 
 if (!$UserID and $Action ne 'save')
 {
@@ -89,9 +113,7 @@ elsif (ref ($Actions{$Action}) eq 'ARRAY')
        }
 }
 
-#print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
-
-Person->disconnect ();
+LiCoM::Connection->disconnect ();
 
 exit (0);
 
@@ -103,75 +125,60 @@ sub action_browse
        $group = shift if (@_);
        $group ||= '';
 
-       my @all;
-       if ($group)
-       {
-               @all = Person->search ([[group => $group]]);
-       }
-       else
-       {
-               @all = Person->search ();
-       }
-
        if (!$group)
        {
-               my %groups = ();
-               for (@all)
-               {
-                       my $person = $_;
-                       my @g = $person->get ('group');
-
-                       $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
-               }
+               my @groups = LiCoM::Group->all ();
 
                print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
-               for (sort (keys (%groups)))
+               for (@groups)
                {
                        my $group = $_;
-                       my $group_esc = uri_escape ($group);
-                       my $num = $groups{$group};
-
-                       print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
+                       my @members = $group->get_members ();
+                       my $members = scalar (@members);
+                       my $group_name = $group->name ();
+                       my $group_esc  = uri_escape ($group_name);
+                       my $desc = $group->description ();
+
+                       print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group_name</a> ($members Member#, ($members == 1 ? ')' : 's)');
+                       print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
+                       print "</li>\n";
                }
-               if (!%groups)
+               if (!@groups)
                {
                        print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
                }
-               print qq(\t\t</ul>\n\n);
-       }
-
-       if ($group)
-       {
-               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
+               print <<EOF;
+               </ul>
+               <div class="menu">
+                       [<a href="$MySelf?action=list">List&nbsp;all</a>]
+               </div>
+EOF
        }
        else
        {
-               print qq(\t\t<h2>All Contacts</h2>\n);
-       }
-
-       print qq(\t\t<ul class="results">\n);
-       for (sort { $a->name () cmp $b->name () } (@all))
-       {
-               my $person = $_;
-               my $cn = $person->name ();
-               my $cn_esc = uri_escape ($cn);
-
-               print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
-       }
-       print qq(\t\t</ul>\n\n);
+               my $group_obj = LiCoM::Group->load ($group);
+               my $group_esc = uri_escape ($group_obj->name ());
+               my @member_names = $group_obj->get_members ();
+               
+               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n),
+               qq(\t\t<ul class="results">\n);
+               for (sort (@member_names))
+               {
+                       my $cn = $_;
+                       my $cn_esc = uri_escape ($cn);
 
-       print qq(\t\t<div class="menu">\n);
-       if ($group)
-       {
-               my $group_esc = uri_escape ($group);
-               print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
-               qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
-       }
-       else
-       {
-               print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
+                       print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
+               }
+               
+               print <<EOF;
+               </ul>
+               <div class="menu">
+                       [<a href="$MySelf?action=list&group=$group_esc">List</a>]
+                       [<a href="$MySelf?action=browse">Back</a>]
+                       [Edit]
+               </div>
+EOF
        }
-       print qq(\t\t</div>\n);
 }
 
 sub action_list
@@ -186,11 +193,11 @@ sub action_list
        my @all = ();
        if ($group)
        {
-               @all = Person->search ([[group => $group]]);
+               @all = LiCoM::Person->search ([[group => $group]]);
        }
        else
        {
-               @all = Person->search ();
+               @all = LiCoM::Person->search ();
        }
 
        print <<EOF;
@@ -243,7 +250,7 @@ sub action_detail
        $cn = shift if (@_);
        die unless ($cn);
 
-       my $person = Person->load ($cn);
+       my $person = LiCoM::Person->load ($cn);
        if (!$person)
        {
                print qq(\t<div>Entry &quot;$cn&quot; could not be loaded from DB.</div>\n);
@@ -305,6 +312,25 @@ EOF
                        "\t\t\t</tr>\n";
                }
        }
+
+       my @groups = LiCoM::Group->load_by_member ($cn);
+       if (@groups)
+       {
+               my $num = scalar (@groups);
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
+               for (my $i = 0; $i < $num; $i++)
+               {
+                       my $group = $groups[$i];
+                       my $group_name = $group->name ();
+                       my $group_esc = uri_escape ($group_name);
+
+                       print "\t\t\t<tr>\n" if ($i != 0);
+                       print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_esc">$group_name</a></td>\n),
+                       "\t\t\t</tr>\n";
+               }
+       }
+       
        print <<EOF;
                </table>
 
@@ -312,6 +338,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
@@ -340,7 +367,7 @@ sub action_search
                push (@filter, [[lastname => $pattern], [firstname => $pattern]]);
        }
 
-       my @matches = Person->search (@filter);
+       my @matches = LiCoM::Person->search (@filter);
 
        if (!@matches)
        {
@@ -357,7 +384,7 @@ sub action_search
        }
 
        print qq(\t<ul class="result">\n);
-       for (@matches)
+       for (sort { $a->name () cmp $b->name () } (@matches))
        {
                my $person = $_;
                my $cn = $person->name ();
@@ -392,7 +419,7 @@ sub action_edit
 
        if ($cn)
        {
-               $person = Person->load ($cn);
+               $person = LiCoM::Person->load ($cn);
 
                if (!$person)
                {
@@ -469,6 +496,8 @@ EOF
                my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
                my @values = @{$contacts->{$field}};
 
+               next if ($field eq 'group');
+
                push (@values, '');
                
                for (@values)
@@ -484,6 +513,39 @@ EOF
                }
        }
 
+       if ($UserID)
+       {
+               my @all_groups = LiCoM::Group->all ();
+
+               if (@all_groups)
+               {
+                       print "\t\t\t<tr>\n",
+                       "\t\t\t\t<th>Group(s)</th>\n",
+                       qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
+
+                       for (@all_groups)
+                       {
+                               my $group = $_;
+                               my $group_name = $group->name ();
+                               my $selected = '';
+
+                               if (grep { $cn eq $_ } ($group->get_members ()))
+                               {
+                                       $selected = ' selected="selected"';
+                               }
+
+                               print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
+                       }
+                       print "\t\t\t\t</select></td>\n",
+                       "\t\t\t</tr>\n";
+               }
+                       
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<th>New Group</th>\n",
+               qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
+               "\t\t\t</tr>\n";
+       }
+
        print <<EOF;
                        <tr>
                                <th colspan="2" class="menu">
@@ -543,7 +605,7 @@ sub action_save
 
        my $contacts = get_contacts ();
 
-       my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
+       my $person = LiCoM::Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
 
        if (!$person)
        {
@@ -553,6 +615,28 @@ sub action_save
        
        $cn = $person->name ();
 
+       for (param ('group'))
+       {
+               my $group_name = $_;
+               my $group = LiCoM::Group->load ($group_name);
+
+               if ($group)
+               {
+                       $group->add_members ($cn);
+               }
+               else
+               {
+                       print qq(\t<div class="error">Group &quot;$group_name&quot; does not exist or could not be loaded.</div>\n);
+               }
+       }
+
+       if (param ('newgroup'))
+       {
+               # FIXME add error handling
+               my $group_name = param ('newgroup');
+               LiCoM::Group->create ($group_name, '', $cn);
+       }
+
        if ($button eq 'apply')
        {
                action_edit (cn => $cn);
@@ -566,7 +650,7 @@ sub action_save
 sub action_update
 {
        my $cn = $UserID ? param ('cn') : $UserCN;
-       my $person = Person->load ($cn);
+       my $person = LiCoM::Person->load ($cn);
 
        die unless ($person);
 
@@ -588,6 +672,7 @@ sub action_update
                $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
 
                $cn = $person->name ();
+               # FIXME Fix groups
        }
 
        my $contacts = get_contacts ();
@@ -596,6 +681,8 @@ sub action_update
        {
                my $field = $_;
                
+               next if (!$UserID and $field eq 'group');
+
                if (defined ($contacts->{$field}))
                {
                        my $values = $contacts->{$field};
@@ -607,6 +694,38 @@ sub action_update
                }
        }
 
+       my %changed_groups = map { $_ => 1 } (param ('group'));
+       my @current_groups = LiCoM::Group->load_by_member ($cn);
+
+       for (@current_groups)
+       {
+               my $group_obj = $_;
+               my $group_name = $group_obj->name ();
+
+               if (!defined ($changed_groups{$group_name}))
+               {
+                       $group_obj->del_members ($cn);
+               }
+               else
+               {
+                       delete ($changed_groups{$group_name});
+               }
+       }
+       for (keys %changed_groups)
+       {
+               my $group_name = $_;
+               my $group_obj = LiCoM::Group->load ($group_name) or die;
+
+               $group_obj->add_members ($cn);
+       }
+
+       if (param ('newgroup'))
+       {
+               # FIXME add error handling
+               my $group_name = param ('newgroup');
+               LiCoM::Group->create ($group_name, '', $cn);
+       }
+
        if ($button eq 'apply' or !$UserID)
        {
                action_edit (cn => $cn);
@@ -623,7 +742,7 @@ sub action_vcard
        $cn = shift if (@_);
        die unless ($cn);
 
-       my $person = Person->load ($cn);
+       my $person = LiCoM::Person->load ($cn);
        die unless ($person);
 
        my %vcard_types =
@@ -657,6 +776,8 @@ EOF
                my $vc_fld = $vcard_types{$field};
                my $values = $person->get ($field);
 
+               next unless ($vc_fld);
+
                for (@$values)
                {
                        my $value = $_;
@@ -672,19 +793,19 @@ sub action_verify
        $cn = shift if (@_);
        die unless ($cn);
 
-       my $person = Person->load ($cn);
+       my $person = LiCoM::Person->load ($cn);
        die unless ($person);
 
        my ($mail) = $person->get ('mail');
        $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;.);
@@ -709,7 +830,7 @@ sub action_verify
 sub action_verify_send_mail
 {
        my $person = shift;
-       my $owner = Person->load ($UserCN);
+       my $owner = LiCoM::Person->load ($UserCN);
        my $smh;
 
        my ($owner_mail) = $owner->get ('mail');
@@ -730,10 +851,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;
@@ -759,18 +880,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;
@@ -987,6 +1156,7 @@ Content-Type: text/html; charset=UTF-8
 
        <body>
 EOF
+
        if ($UserID)
        {
                my $search = param ('search') || '';
@@ -1017,7 +1187,7 @@ sub html_end
        print <<EOF;
                <div class="foot">
                        &quot;Lightweight Contact Manager&quot;,
-                       written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
+                       written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
                        &lt;octo at verplant.org&gt;
                </div>
        </body>
@@ -1025,34 +1195,6 @@ sub html_end
 EOF
 }
 
-sub read_config
-{
-       my $file = '/var/www/html/cgi.verplant.org/address/book.conf';
-       my $fh;
-
-       open ($fh, "< $file") or die ("open ($file): $!");
-       for (<$fh>)
-       {
-               chomp;
-               my $line = $_;
-
-               if ($line =~ m/^(\w+):\s*"(.+)"\s*$/)
-               {
-                       my $key = lc ($1);
-                       my $val = $2;
-
-                       $Config{$key} = $val;
-               }
-       }
-
-       close ($fh);
-
-       for (qw(uri bind_dn password))
-       {
-               die ("Not defined: $_") unless (defined ($Config{$_}));
-       }
-}
-
 sub pwgen
 {
        my $len = @_ ? shift : 6;
@@ -1113,9 +1255,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;
                }
        }
@@ -1148,8 +1290,8 @@ sub get_contacts
                {
                        for (@values)
                        {
-                               $_ =~ s/\D//g;
-                               $_ = '+' . $_;
+                               $_ =~ s/[^0-9 \-]//g;
+                               $_ = '+' . $_ if ($_);
                        }
                }