File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.21: download - view: text, annotated - select for diffs
Wed Sep 26 14:07:45 2001 UTC (22 years, 7 months ago) by www
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
Public Access

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: # 5/21/99,5/22,5/29,5/31,6/15,16/11,22/11,
    4: # 01/06,01/13,05/31,06/01,09/06,09/25,09/28,10/30,11/6,
    5: # 12/25,12/26,
    6: # 01/06/01,05/28,8/11,9/26 Gerd Kortemeyer
    7: 
    8: package Apache::lonacc;
    9: 
   10: use strict;
   11: use Apache::Constants qw(:common :http :methods);
   12: use Apache::File;
   13: use Apache::lonnet;
   14: use CGI::Cookie();
   15: use Fcntl qw(:flock);
   16: 
   17: sub handler {
   18:     my $r = shift;
   19:     my $requrl=$r->uri;
   20:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   21:     my $lonid=$cookies{'lonID'};
   22:     my $cookie;
   23:     if ($lonid) {
   24: 	my $handle=$lonid->value;
   25:         $handle=~s/\W//g;
   26:         my $lonidsdir=$r->dir_config('lonIDsDir');
   27:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   28: 
   29: # ------------------------------------------- Transfer profile into environment
   30: 
   31:             my @profile;
   32: 	    {
   33:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
   34:              flock($idf,LOCK_SH);
   35:              @profile=<$idf>;
   36:              $idf->close();
   37: 	    }
   38:             my $envi;
   39:             for ($envi=0;$envi<=$#profile;$envi++) {
   40: 		chomp($profile[$envi]);
   41: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
   42:                 $ENV{$envname} = $envvalue;
   43:             }
   44:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
   45:             if ($requrl=~/^\/res\//) {
   46:                $ENV{'request.state'} = "published";
   47: 	    } else {
   48: 	       $ENV{'request.state'} = 'unknown';
   49:             }
   50:             $ENV{'request.filename'} = $r->filename;
   51: 
   52: # -------------------------------------------------------- Load POST parameters
   53: 
   54: 
   55:            
   56:         my $buffer;
   57: 
   58:         $r->read($buffer,$r->header_in('Content-length'));
   59: 
   60: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
   61:             my @pairs=split(/&/,$buffer);
   62:             my $pair;
   63:             foreach $pair (@pairs) {
   64:                my ($name,$value) = split(/=/,$pair);
   65:                $value =~ tr/+/ /;
   66:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   67:                $name  =~ tr/+/ /;
   68:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   69:                $ENV{"form.$name"}=$value;
   70:             }
   71:         } else {
   72: 	    my $contentsep=$1;
   73:             my @lines = split (/\n/,$buffer);
   74:             my $name='';
   75:             my $value='';
   76:             my $fname='';
   77:             my $fmime='';
   78:             my $i;
   79:             for ($i=0;$i<=$#lines;$i++) {
   80: 		if ($lines[$i]=~/^$contentsep/) {
   81: 		    if ($name) {
   82:                         chomp($value);
   83: 			if ($fname) {
   84: 			    $ENV{"form.$name.filename"}=$fname;
   85:                             $ENV{"form.$name.mimetype"}=$fmime;
   86:                         } else {
   87:                             $value=~s/\s+$//s;
   88:                         }
   89:                         $ENV{"form.$name"}=$value;
   90:                     }
   91:                     if ($i<$#lines) {
   92: 			$i++;
   93:                         $lines[$i]=~
   94: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
   95:                         $name=$1;
   96:                         $value='';
   97:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
   98: 			   $fname=$1;
   99:                            if 
  100:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  101: 			      $fmime=$1;
  102:                               $i++;
  103: 			   } else {
  104:                               $fmime='';
  105:                            }
  106:                         } else {
  107: 			    $fname='';
  108:                             $fmime='';
  109:                         }
  110:                         $i++;
  111:                     }
  112:                 } else {
  113: 		    $value.=$lines[$i]."\n";
  114:                 }
  115:             }
  116: 	}
  117:             $r->method_number(M_GET);
  118: 	    $r->method('GET');
  119:             $r->headers_in->unset('Content-length');
  120: 
  121: # ---------------------------------------------------------------- Check access
  122: 
  123:             if ($requrl!~/^\/adm\//) {
  124: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
  125:                 if ($access eq '1') {
  126: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  127: 	           return HTTP_NOT_ACCEPTABLE; 
  128:                 }
  129:                 if (($access ne '2') && ($access ne 'F')) {
  130: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  131: 	           return HTTP_NOT_ACCEPTABLE; 
  132:                 }
  133:             } 
  134:             return OK; 
  135:         } else { 
  136:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  137:         };
  138:     }
  139: 
  140: # -------------------------------------------- See if this is a public resource
  141:     if (&Apache::lonnet::metadata($requrl,'copyright') eq 'public') {
  142:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  143: 	$ENV{'user.name'}='public';
  144:         $ENV{'user.domain'}='public';
  145:         $ENV{'request.state'} = "published";
  146:         $ENV{'request.publicaccess'} = 1;
  147:         $ENV{'request.filename'} = $r->filename;
  148:         return OK;
  149:     }
  150: # ----------------------------------------------- Store where they wanted to go
  151:     
  152:     $ENV{'request.firsturl'}=$requrl;
  153:     return FORBIDDEN;
  154: }
  155: 
  156: 1;
  157: __END__

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