File:  [LON-CAPA] / loncom / cgi / loncgi.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Oct 9 22:04:37 2003 UTC (21 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Adding loncgi.pm, which enables the use of the users environment to pass data.
Reworked graph.png to give error messages in htmlese and use loncgi.pm.
This change is not backwards compatable and I will be updating the modules which
use graph.png to use the new convention.

#
# LON-CAPA helpers for cgi-bin scripts
#
# $Id: loncgi.pm,v 1.1 2003/10/09 22:04:37 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA 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.
#
# LON-CAPA 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 LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#############################################
#############################################

=pod

=head1 NAME

loncgi

=head1 SYNOPSIS

Provides subroutines for checking a LON-CAPA cookie and loading the users
environment.

=head1 Subroutines

=over 4 

=cut

#############################################
#############################################
package LONCAPA::loncgi;

use strict;
use warnings FATAL=>'all';
no warnings 'uninitialized';
use CGI();
use CGI::Cookie();
use Fcntl qw(:flock);
use LONCAPA::Configuration();

my $lonidsdir;

BEGIN {
    my $perlvar=LONCAPA::Configuration::read_conf('loncapa.conf');
    delete $perlvar->{'lonReceipt'};
    $lonidsdir = $perlvar->{'lonIDsDir'};
}

#############################################
#############################################

=pod

=item check_cookie_and_load_env

Inputs: none

Returns: 1 if the user has a LON-CAPA cookie 0 if not.
Loads the users environment into the %ENV hash if the cookie is correct.

=cut

#############################################
#############################################
sub check_cookie_and_load_env {
    my %cookies=fetch CGI::Cookie;
    if (exists($cookies{'lonID'}) && 
        -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
        # cookie found
        &transfer_profile_to_env($cookies{'lonID'}->value);
        return 1;
    } else {
        # No cookie found
        return 0;
    }
}

#############################################
#############################################

=pod

=item check_cookie

Inputs: none

Returns: 1 if the user has a LON-CAPA cookie and 0 if not.

=cut

#############################################
#############################################
sub check_cookie {
    my %cookies=fetch CGI::Cookie;
    if (exists($cookies{'lonID'}) && 
        -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
        # cookie found
        return 1;
    } else {
        # No cookie found
        return 0;
    }
}

#############################################
#############################################

=pod

=item transfer_profile_to_env

Load the users environment into the %ENV hash.

Inputs: $handle, the name of the users LON-CAPA cookie.

Returns: undef

=cut

#############################################
#############################################
sub transfer_profile_to_env {
    my ($handle)=@_;
    my @profile;
    {
        open(IDFILE, "<$lonidsdir/$handle.id");
        flock(IDFILE,LOCK_SH);
        @profile=<IDFILE>;
        close(IDFILE);
    }
    foreach my $envrow (@profile) {
        chomp($envrow);
        my ($envname,$envvalue)=split(/=/,$envrow);
        $ENV{$envname} = $envvalue;
    }
    $ENV{'user.environment'} = "$lonidsdir/$handle.id";
    return undef;
}

#############################################
#############################################

=pod

=back

=cut

1;

__END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>