#!/usr/bin/perl
#use lib "/home/uninet/perl/i686-linux"; 
use lib "/home/uninet/perl/i386-linux";
use lib "/home/uninet/perl"; 
# /perl/i386-linux
############################################################################
# -*- Mode:Perl -*-                                                        
#                                                                          
# $Id: index.pl,v 1.41 2001/03/07 21:38:57 mikesaenz Exp $
# $Revision: 1.41 $
# $Author: mikesaenz $
# Copyright (C) 1999-2000 Daniel "supercobra"  Guermeur
# supercobra@metadot.com
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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 Portal;
use Glo;

use CGI qw(:standard :html3);
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);
use strict;
#################################################

use Data::Dumper;

# Increment hit count
&Glo::incr_hit_count();

# Create a Metadot portal instance
my $Mdot = Portal->new();

# we have to determine $iid
my $iid ;
if    (defined $FORM{id})   { $iid = $FORM{id}; }
elsif (defined $FORM{iid})  { $iid = $FORM{iid}; }
elsif (!defined $FORM{isa}) { $iid = 0; }             # display main portal by default
$FORM{iid} = $FORM{id} = $iid;

# we have to determine $isa
my $isa ;
if    (defined $FORM{isa}) { $isa = $FORM{isa} ; }
elsif (defined $iid)       { ($isa) = sqlSelect("isa", "instance", "iid=$iid") ; }
$FORM{isa} = $isa = $isa || 'Category';



# we have to determine $op
my $op;
if (defined $FORM{op})     { $op = $FORM{op} ; }
else                       { $op = "show" ; }   # by default we try to display the object
# handle old operator
if (lc($op) eq "showusers"){ $op = "show_users" ; $isa = "Portal" ;}
if (lc($op) eq "myportal") { $op = "myportal"  ; $isa = "Portal" ; }
$FORM{op} = $op = lc($op) ;

# display debug information
$Mdot->debug_msg(3, "index.pl: iid=" . ($iid||"") . " isa=$isa op=$op");


### MAIN OPERATOR SWITCH ###

# instanciate right object
my $is_gizmo = 0;
my $object;
if    ($isa eq 'Channel') { require "Channel.pm"; $object = Channel->new($FORM{cid}); }
elsif ($isa eq 'Message') { require "Message.pm"; $object = Message->new($FORM{mid}); $is_gizmo = 1;}
elsif ($isa eq 'Portal')  { $object = $Mdot; }
elsif ($isa eq 'Session') { $object = $SESSION; }
elsif ($isa eq 'User')    { $object = User->new($SESSION->uid()); }
else                      { require "Gizmo.pm"; 
			    require "GizmoTool.pm"; 
			    $object = $isa->new($iid);
			    $is_gizmo = 1;
			  }

if ($is_gizmo) {
    # enforce AC
    unless($object->is_allowed_to_do($op, $USER, 1)) {
        $object->print_click_back( "Error", "index.pl:<BR>Sorry, you are not allowed to do operation:<BR> -$op-<BR>$0");	
        exit(0);      
    }
} else {
    # check OLD security
    # XXX [claudio] this should be removed when all object implement new AC
    my %security = ($op, $object->security->{$op}) ;
    $object->debug_msg(4, "index.pl: op=$op lvl=$security{$op}");
    $object->check_security(\%security,$op);
}

# order the object to handle the operation

$object->handle($op);

