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 (20 years, 6 months 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.

    1: #
    2: # LON-CAPA helpers for cgi-bin scripts
    3: #
    4: # $Id: loncgi.pm,v 1.1 2003/10/09 22:04:37 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #############################################
   29: #############################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: loncgi
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Provides subroutines for checking a LON-CAPA cookie and loading the users
   40: environment.
   41: 
   42: =head1 Subroutines
   43: 
   44: =over 4 
   45: 
   46: =cut
   47: 
   48: #############################################
   49: #############################################
   50: package LONCAPA::loncgi;
   51: 
   52: use strict;
   53: use warnings FATAL=>'all';
   54: no warnings 'uninitialized';
   55: use CGI();
   56: use CGI::Cookie();
   57: use Fcntl qw(:flock);
   58: use LONCAPA::Configuration();
   59: 
   60: my $lonidsdir;
   61: 
   62: BEGIN {
   63:     my $perlvar=LONCAPA::Configuration::read_conf('loncapa.conf');
   64:     delete $perlvar->{'lonReceipt'};
   65:     $lonidsdir = $perlvar->{'lonIDsDir'};
   66: }
   67: 
   68: #############################################
   69: #############################################
   70: 
   71: =pod
   72: 
   73: =item check_cookie_and_load_env
   74: 
   75: Inputs: none
   76: 
   77: Returns: 1 if the user has a LON-CAPA cookie 0 if not.
   78: Loads the users environment into the %ENV hash if the cookie is correct.
   79: 
   80: =cut
   81: 
   82: #############################################
   83: #############################################
   84: sub check_cookie_and_load_env {
   85:     my %cookies=fetch CGI::Cookie;
   86:     if (exists($cookies{'lonID'}) && 
   87:         -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
   88:         # cookie found
   89:         &transfer_profile_to_env($cookies{'lonID'}->value);
   90:         return 1;
   91:     } else {
   92:         # No cookie found
   93:         return 0;
   94:     }
   95: }
   96: 
   97: #############################################
   98: #############################################
   99: 
  100: =pod
  101: 
  102: =item check_cookie
  103: 
  104: Inputs: none
  105: 
  106: Returns: 1 if the user has a LON-CAPA cookie and 0 if not.
  107: 
  108: =cut
  109: 
  110: #############################################
  111: #############################################
  112: sub check_cookie {
  113:     my %cookies=fetch CGI::Cookie;
  114:     if (exists($cookies{'lonID'}) && 
  115:         -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
  116:         # cookie found
  117:         return 1;
  118:     } else {
  119:         # No cookie found
  120:         return 0;
  121:     }
  122: }
  123: 
  124: #############################################
  125: #############################################
  126: 
  127: =pod
  128: 
  129: =item transfer_profile_to_env
  130: 
  131: Load the users environment into the %ENV hash.
  132: 
  133: Inputs: $handle, the name of the users LON-CAPA cookie.
  134: 
  135: Returns: undef
  136: 
  137: =cut
  138: 
  139: #############################################
  140: #############################################
  141: sub transfer_profile_to_env {
  142:     my ($handle)=@_;
  143:     my @profile;
  144:     {
  145:         open(IDFILE, "<$lonidsdir/$handle.id");
  146:         flock(IDFILE,LOCK_SH);
  147:         @profile=<IDFILE>;
  148:         close(IDFILE);
  149:     }
  150:     foreach my $envrow (@profile) {
  151:         chomp($envrow);
  152:         my ($envname,$envvalue)=split(/=/,$envrow);
  153:         $ENV{$envname} = $envvalue;
  154:     }
  155:     $ENV{'user.environment'} = "$lonidsdir/$handle.id";
  156:     return undef;
  157: }
  158: 
  159: #############################################
  160: #############################################
  161: 
  162: =pod
  163: 
  164: =back
  165: 
  166: =cut
  167: 
  168: 1;
  169: 
  170: __END__

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