File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.202: download - view: text, annotated - select for diffs
Tue Dec 7 23:19:00 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Change in rev. 1.154 should also apply to xlsx, docx, pptx, xlsm, docm and
  pptm files uploaded to a user's portfolio. Eliminate extra byte (line feed),
  which would cause Excel to prompt user to "repair" file after download.

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.202 2021/12/07 23:19:00 raeburn 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: =head1 NAME
   31: 
   32: Apache::lonacc - Cookie Based Access Handler
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
   37: 
   38:  PerlAccessHandler       Apache::lonacc
   39: 
   40: =head1 INTRODUCTION
   41: 
   42: This module enables cookie based authentication and is used
   43: to control access for many different LON-CAPA URIs.
   44: 
   45: Whenever the client sends the cookie back to the server, 
   46: this cookie is handled by either lonacc.pm or loncacc.pm
   47: (see srm.conf for what is invoked when).  If
   48: the cookie is missing or invalid, the user is re-challenged
   49: for login information.
   50: 
   51: This is part of the LearningOnline Network with CAPA project
   52: described at http://www.lon-capa.org.
   53: 
   54: =head1 HANDLER SUBROUTINE
   55: 
   56: This routine is called by Apache and mod_perl.
   57: 
   58: =over 4
   59: 
   60: =item *
   61: 
   62: transfer profile into environment
   63: 
   64: =item *
   65: 
   66: load POST parameters
   67: 
   68: =item *
   69: 
   70: check access
   71: 
   72: =item *
   73: 
   74: if allowed, get symb, log, generate course statistics if applicable
   75: 
   76: =item *
   77: 
   78: otherwise return error
   79: 
   80: =item *
   81: 
   82: see if public resource
   83: 
   84: =item *
   85: 
   86: store attempted access
   87: 
   88: =back
   89: 
   90: =head1 NOTABLE SUBROUTINES
   91: 
   92: =cut
   93: 
   94: 
   95: package Apache::lonacc;
   96: 
   97: use strict;
   98: use Apache::Constants qw(:common :http :methods);
   99: use Apache::File;
  100: use Apache::lonnet;
  101: use Apache::loncommon();
  102: use Apache::lonlocal;
  103: use Apache::restrictedaccess();
  104: use Apache::blockedaccess();
  105: use Apache::lonprotected();
  106: use Fcntl qw(:flock);
  107: use LONCAPA qw(:DEFAULT :match);
  108: 
  109: sub cleanup {
  110:     my ($r)=@_;
  111:     if (! $r->is_initial_req()) { return DECLINED; }
  112:     &Apache::lonnet::save_cache();
  113:     return OK;
  114: }
  115: 
  116: sub goodbye {
  117:     my ($r)=@_;
  118:     &Apache::lonnet::goodbye();
  119:     return DONE;
  120: }
  121: 
  122: ###############################################
  123: 
  124: sub get_posted_cgi {
  125:     my ($r,$fields) = @_;
  126: 
  127:     my $buffer;
  128:     if ($r->header_in('Content-length')) {
  129: 	$r->read($buffer,$r->header_in('Content-length'),0);
  130:     }
  131:     my $content_type = $r->header_in('Content-type');
  132:     if ($content_type !~ m{^multipart/form-data}) {
  133: 	my @pairs=split(/&/,$buffer);
  134: 	my $pair;
  135: 	foreach $pair (@pairs) {
  136: 	    my ($name,$value) = split(/=/,$pair);
  137: 	    $value =~ tr/+/ /;
  138: 	    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  139: 	    $name  =~ tr/+/ /;
  140: 	    $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  141:             if (ref($fields) eq 'ARRAY') {
  142:                 next if (!grep(/^\Q$name\E$/,@{$fields}));
  143:             }
  144: 	    &Apache::loncommon::add_to_env("form.$name",$value);
  145: 	}
  146:     } else {
  147: 	my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
  148: 	my @lines = split (/\n/,$buffer);
  149: 	my $name='';
  150: 	my $value='';
  151: 	my $fname='';
  152: 	my $fmime='';
  153: 	my $i;
  154: 	for ($i=0;$i<=$#lines;$i++) {
  155: 	    if ($lines[$i]=~/^--\Q$contentsep\E/) {
  156: 		if ($name) {
  157:                     chomp($value);
  158:                     if (($r->uri eq '/adm/portfolio') && 
  159:                         ($name eq 'uploaddoc')) {
  160:                         if (length($value) == 1) {
  161:                             $value=~s/[\r\n]$//;
  162:                         }
  163:                     } 
  164:                     if ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) {
  165:                         $value=~s/[\r\n]$//;
  166:                     }
  167:                     if (ref($fields) eq 'ARRAY') {
  168:                         next if (!grep(/^\Q$name\E$/,@{$fields}));
  169:                     }
  170:                     if ($fname) {
  171:                         if ($env{'form.symb'} ne '') {
  172:                             my $size = (length($value))/(1024.0 * 1024.0);
  173:                             if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
  174:                                 $env{"form.$name.filename"}=$fname;
  175:                                 $env{"form.$name.mimetype"}=$fmime;
  176:                                 &Apache::loncommon::add_to_env("form.$name",$value);
  177:                             }
  178:                         } else {
  179:                             $env{"form.$name.filename"}=$fname;
  180:                             $env{"form.$name.mimetype"}=$fmime;
  181:                             &Apache::loncommon::add_to_env("form.$name",$value);
  182:                         }
  183:                     } else {
  184:                         $value=~s/\s+$//s;
  185:                         &Apache::loncommon::add_to_env("form.$name",$value);
  186:                     }
  187: 		}
  188: 		if ($i<$#lines) {
  189: 		    $i++;
  190: 		    $lines[$i]=~
  191: 		/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  192: 		    $name=$1;
  193: 		    $value='';
  194: 		    if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  195: 			$fname=$1;
  196: 			if 
  197:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  198: 				$fmime=$1;
  199: 				$i++;
  200: 			    } else {
  201: 				$fmime='';
  202: 			    }
  203: 		    } else {
  204: 			$fname='';
  205: 			$fmime='';
  206: 		    }
  207:                     if ($i<$#lines && $lines[$i+1]=~/^Content\-Type\:\s*([\w\-\/]+)/i) {
  208:                         # TODO: something with $1 !
  209:                         $i++;
  210:                     }
  211:                     if ($i<$#lines && $lines[$i+1]=~/^Content\-transfer\-encoding\:\s*([\w\-\/]+)/i) {
  212:                         # TODO: something with $1 !
  213:                         $i++;
  214:                     }
  215: 		    $i++;
  216: 		}
  217: 	    } else {
  218: 		$value.=$lines[$i]."\n";
  219: 	    }
  220: 	}
  221:     }
  222: #
  223: # Digested POSTed values
  224: #
  225: # Remember the way this was originally done (GET or POST)
  226: #
  227:     $env{'request.method'}=$ENV{'REQUEST_METHOD'};
  228: #
  229: # There may also be stuff in the query string
  230: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
  231: # Also, unset POSTed content length to cover all tracks.
  232: #
  233: 
  234:     $r->method_number(M_GET);
  235: 
  236:     $r->method('GET');
  237:     $r->headers_in->unset('Content-length');
  238: }
  239: 
  240: =pod
  241: 
  242: =over
  243: 
  244: =item upload_size_allowed()
  245: 
  246: 	Perform size checks for file uploads to essayresponse items in course context.
  247: 	
  248: 	Add form.HWFILESIZE.$part_$id to %env with file size (MB)
  249: 	If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
  250: 
  251: =cut
  252:  
  253: sub upload_size_allowed {
  254:     my ($name,$size,$fname) = @_;
  255:     if ($name =~ /^HWFILE(\w+)$/) {
  256:         my $ident = $1;
  257:         my $item = 'HWFILESIZE'.$ident;
  258:         my $savesize = sprintf("%.6f",$size);
  259:         &Apache::loncommon::add_to_env("form.$item",$savesize);
  260:         my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
  261:         if (!$maxsize) {
  262:             $maxsize = 10.0; # FIXME This should become a domain configuration.
  263:         }
  264:         if ($size > $maxsize) {
  265:             my $warn = 'HWFILETOOBIG'.$ident;
  266:             &Apache::loncommon::add_to_env("form.$warn",$fname);
  267:             return;
  268:         }
  269:     }
  270:     return 'ok';
  271: }
  272: 
  273: =pod
  274: 
  275: =item sso_login()
  276: 
  277: 	handle the case of the single sign on user, at this point $r->user 
  278: 	will be set and valid; now need to find the loncapa user info, and possibly
  279: 	balance them. If $r->user() is set this means either it was either set by
  280:         SSO or by checkauthen.pm, if a valid cookie was found. The latter case can
  281:         be identified by the third arg ($usename), except when lonacc is called in 
  282:         an internal redirect to /adm/switchserver (e.g., load-balancing following
  283:         successful authentication) -- no cookie set yet.  For that particular case
  284:         simply skip the call to sso_login(). 
  285: 
  286: 	returns OK if it was SSO and user was handled.
  287:         returns undef if not SSO or no means to handle the user.
  288:         
  289: =cut
  290: 
  291: sub sso_login {
  292:     my ($r,$handle,$username) = @_;
  293: 
  294:     if (($r->user eq '') || ($username ne '') || ($r->user eq 'public:public') ||
  295:         (defined($env{'user.name'}) && (defined($env{'user.domain'}))
  296: 	  && ($handle ne ''))) {
  297: 	# not an SSO case or already logged in
  298: 	return undef;
  299:     }
  300: 
  301:     my ($user) = ($r->user =~ m/^($match_username)$/);
  302:     if ($user eq '') {
  303:         return undef;
  304:     }
  305: 
  306:     my $query = $r->args;
  307:     my %form;
  308:     if ($query) {
  309:         my @items = ('role','symb','iptoken','origurl','ttoken',
  310:                      'ltoken','linkkey','logtoken','sso');
  311:         &Apache::loncommon::get_unprocessed_cgi($query,\@items);
  312:         foreach my $item (@items) {
  313:             if (defined($env{'form.'.$item})) {
  314:                 $form{$item} = $env{'form.'.$item};
  315:             }
  316:         }
  317:     }
  318: 
  319:     my %sessiondata;
  320:     if ($form{'iptoken'}) {
  321:         %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  322:         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  323:         unless ($sessiondata{'sessionserver'}) {
  324:             delete($form{'iptoken'});
  325:         }
  326:     }
  327: 
  328:     my ($linkprot,$linkkey);
  329: 
  330: #
  331: # If Shibboleth auth is in use, and a dual SSO and non-SSO login page
  332: # is in use, then the query string will contain the logtoken item with
  333: # a value set to the name of a .tmp file in /home/httpd/perl/tmp
  334: # containing the url to display after authentication, and also,
  335: # optionally, role and symb, or linkprot or linkkey (deep-link access).
  336: #
  337: # If Shibboleth auth is in use, but a dual log-in page is not in use,
  338: # and the originally requested URL was /tiny/$domain/$id (i.e.,
  339: # for deeplinking), then the query string will contain the sso item
  340: # with a value set to the name of a .tmp file in /home/httpd/perl/tmp
  341: # containing the url to display after authentication, and also,
  342: # optionally, linkprot or linkkey (deep-link access).
  343: #
  344: # Otherwise the query string may contain role and symb, or if the
  345: # originally requested URL was /tiny/$domain/$id (i.e. for deeplinking)
  346: # then the query string may contain a ttoken item with a value set
  347: # to the name of a .tmp file in /home/httpd/perl/tmp containing either
  348: # linkprot or linkkey (deep-link access).
  349: #
  350: # If deep-linked, i.e., the originally requested URL was /tiny/$domain/$id
  351: # the linkkey may have originally been sent in POSTed data, which will
  352: # have been processed in lontrans.pm
  353: #
  354: 
  355:     if ($form{'ttoken'}) {
  356:         my %info = &Apache::lonnet::tmpget($form{'ttoken'});
  357:         &Apache::lonnet::tmpdel($form{'ttoken'});
  358:         if ($info{'origurl'}) {
  359:             $form{'origurl'} = $info{'origurl'};
  360:         }
  361:         if ($info{'linkprot'}) {
  362:             $linkprot = $info{'linkprot'};
  363:         } elsif ($info{'linkkey'} ne '') {
  364:             $linkkey = $info{'linkkey'};
  365:         }
  366:     } elsif ($form{'logtoken'}) {
  367:         my ($firsturl,@rest);
  368:         my $lonhost = $r->dir_config('lonHostID');
  369:         my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost);
  370:         my $delete = &Apache::lonnet::tmpdel($form{'logtoken'});
  371:         unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
  372:                 ($tmpinfo eq 'no_such_host')) {
  373:             (undef,$firsturl,@rest) = split(/&/,$tmpinfo);
  374:             if ($firsturl ne '') {
  375:                 $firsturl = &unescape($firsturl);
  376:             }
  377:             foreach my $item (@rest) {
  378:                 my ($key,$value) = split(/=/,$item);
  379:                 $form{$key} = &unescape($value);
  380:             }
  381:             if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
  382:                 $form{'origurl'} = $firsturl;
  383:             }
  384:             if ($form{'linkprot'}) {
  385:                 $linkprot = $form{'linkprot'};
  386:             } elsif ($form{'linkkey'} ne '') {
  387:                 $linkkey = $form{'linkkey'};
  388:             }
  389:             if ($form{'iptoken'}) {
  390:                 %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  391:                 my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  392:             }
  393:         }
  394:     } elsif ($form{'sso'}) {
  395:         my $lonhost = $r->dir_config('lonHostID');
  396:         my $info = &Apache::lonnet::reply('tmpget:'.$form{'sso'},$lonhost);
  397:         &Apache::lonnet::tmpdel($form{'sso'});
  398:         unless (($info=~/^error/) || ($info eq 'con_lost') ||
  399:                 ($info eq 'no_such_host')) {
  400:             my ($firsturl,@rest)=split(/\&/,$info);
  401:             if ($firsturl ne '') {
  402:                 $form{'origurl'} = &unescape($firsturl);
  403:             }
  404:             foreach my $item (@rest) {
  405:                 my ($key,$value) = split(/=/,$item);
  406:                 $form{$key} = &unescape($value);
  407:             }
  408:             if ($form{'linkprot'}) {
  409:                 $linkprot = $form{'linkprot'};
  410:             } elsif ($form{'linkkey'} ne '') {
  411:                 $linkkey = $form{'linkkey'};
  412:             }
  413:         }
  414:     } elsif ($form{'ltoken'}) {
  415:         my %link_info = &Apache::lonnet::tmpget($form{'ltoken'});
  416:         $linkprot = $link_info{'linkprot'};
  417:         my $delete = &Apache::lonnet::tmpdel($form{'ltoken'});
  418:         delete($form{'ltoken'});
  419:     } elsif ($form{'linkkey'} ne '') {
  420:         $linkkey = $form{'linkkey'};
  421:     }
  422: 
  423:     my $domain = $r->dir_config('lonSSOUserDomain');
  424:     if ($domain eq '') {
  425:         $domain = $r->dir_config('lonDefDomain');
  426:     }
  427:     my $home=&Apache::lonnet::homeserver($user,$domain);
  428:     if ($home !~ /(con_lost|no_host|no_such_host)/) {
  429: 	&Apache::lonnet::logthis(" SSO authorized user $user ");
  430:         my ($is_balancer,$otherserver,$hosthere);
  431:         if ($form{'iptoken'}) {
  432:             if (($sessiondata{'domain'} eq $domain) &&
  433:                 ($sessiondata{'username'} eq $user)) {
  434:                 $hosthere = 1;
  435:             }
  436:         }
  437:         unless ($hosthere) {
  438:             ($is_balancer,$otherserver) =
  439:                 &Apache::lonnet::check_loadbalancing($user,$domain,'login');
  440:             if ($is_balancer) {
  441:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  442:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  443:                 if (($found_server) && ($balancer_cookie =~ /^\Q$domain\E_\Q$user\E_/)) {
  444:                     $otherserver = $found_server;
  445:                 } elsif ($otherserver eq '') {
  446:                     my $lowest_load;
  447:                     ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain);
  448:                     if ($lowest_load > 100) {
  449:                         $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain);
  450:                     }
  451:                     if ($otherserver ne '') {
  452:                         my @hosts = &Apache::lonnet::current_machine_ids();
  453:                         if (grep(/^\Q$otherserver\E$/,@hosts)) {
  454:                             $hosthere = $otherserver;
  455:                         }
  456:                     }
  457:                 }
  458:             }
  459:         }
  460: 	if (($is_balancer) && (!$hosthere)) {
  461: 	    # login but immediately go to switch server to find us a new 
  462: 	    # machine
  463: 	    &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
  464:             foreach my $item (keys(%form)) {
  465:                 $env{'form.'.$item} = $form{$item};
  466:             }
  467:             unless (($form{'symb'}) || ($form{'origurl'})) {
  468:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  469:                     $env{'form.origurl'} = $r->uri;
  470:                 }
  471:             }
  472:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  473:                 $env{'request.deeplink.login'} = $form{'origurl'};
  474:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  475:                 $env{'request.deeplink.login'} = $r->uri;
  476:             }
  477:             if ($env{'request.deeplink.login'}) {
  478:                 if ($linkprot) {
  479:                     $env{'request.linkprot'} = $linkprot;
  480:                 } elsif ($linkkey ne '') {
  481:                     $env{'request.linkkey'} = $linkkey;
  482:                 }
  483:             }
  484:             $env{'request.sso.login'} = 1;
  485:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  486:                 $env{'request.sso.reloginserver'} =
  487:                     $r->dir_config('lonSSOReloginServer');
  488:             }
  489:             my $redirecturl = '/adm/switchserver';
  490:             if ($otherserver ne '') {
  491:                 $redirecturl .= '?otherserver='.$otherserver;
  492:             }
  493: 	    $r->internal_redirect($redirecturl);
  494: 	    $r->set_handlers('PerlHandler'=> undef);
  495: 	} else {
  496: 	    # need to login them in, so generate the need data that
  497: 	    # migrate expects to do login
  498: 	    my $ip = &Apache::lonnet::get_requestor_ip($r);
  499: 	    my %info=('ip'        => $ip,
  500: 		      'domain'    => $domain,
  501: 		      'username'  => $user,
  502: 		      'server'    => $r->dir_config('lonHostID'),
  503: 		      'sso.login' => 1
  504: 		      );
  505:             foreach my $item ('role','symb','iptoken','origurl') {
  506:                 if (exists($form{$item})) {
  507:                     $info{$item} = $form{$item};
  508:                 } elsif ($sessiondata{$item} ne '') {
  509:                     $info{$item} = $sessiondata{$item};
  510:                 }
  511:             }
  512:             unless (($info{'symb'}) || ($info{'origurl'})) {
  513:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  514:                     $info{'origurl'} = $r->uri; 
  515:                 }
  516:             }
  517:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  518:                 $info{'deeplink.login'} = $form{'origurl'};
  519:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  520:                 $info{'deeplink.login'} = $r->uri;
  521:             }
  522:             if ($info{'deeplink.login'}) {
  523:                 if ($linkprot) {
  524:                     $info{'linkprot'} = $linkprot;
  525:                 } elsif ($linkkey ne '') {
  526:                     $info{'linkkey'} = $linkkey;
  527:                 }
  528:             }
  529:             if ($r->dir_config("ssodirecturl") == 1) {
  530:                 $info{'origurl'} = $r->uri;
  531:             }
  532:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  533:                 $info{'sso.reloginserver'} = 
  534:                     $r->dir_config('lonSSOReloginServer'); 
  535:             }
  536:             if (($is_balancer) && ($hosthere)) {
  537:                 $info{'noloadbalance'} = $hosthere;
  538:             }
  539: 	    my $token = 
  540: 		&Apache::lonnet::tmpput(\%info,
  541: 					$r->dir_config('lonHostID'));
  542: 	    $env{'form.token'} = $token;
  543: 	    $r->internal_redirect('/adm/migrateuser');
  544: 	    $r->set_handlers('PerlHandler'=> undef);
  545: 	}
  546: 	return OK;
  547:     } else {
  548: 	&Apache::lonnet::logthis(" SSO authorized unknown user $user ");
  549:         my @cancreate;
  550:         my %domconfig =
  551:             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
  552:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
  553:             if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
  554:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
  555:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
  556:                 } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
  557:                          ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
  558:                     @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
  559:                 }
  560:             }
  561:         }
  562:         if ((grep(/^sso$/,@cancreate)) || (defined($r->dir_config('lonSSOUserUnknownRedirect')))) {
  563:             $r->subprocess_env->set('SSOUserUnknown' => $user);
  564:             $r->subprocess_env->set('SSOUserDomain' => $domain);
  565:             if (grep(/^sso$/,@cancreate)) {
  566: #FIXME - need to preserve origurl, role and symb, or linkprot or linkkey for use after account
  567: # creation
  568:                 $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]);
  569:                 $r->handler('perl-script');
  570:             } else {
  571: 	        $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
  572:                 $r->set_handlers('PerlHandler'=> undef);
  573:             }
  574: 	    return OK;
  575:         }
  576:     }
  577:     return undef;
  578: }
  579: 
  580: sub handler {
  581:     my $r = shift;
  582:     my $requrl=$r->uri;
  583: 
  584:     if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
  585:         return OK;
  586:     }
  587: 
  588:     if (&Apache::lonnet::is_domainimage($requrl)) {
  589:         return OK;
  590:     }
  591: 
  592:     my %user;
  593:     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  594: 
  595:     unless (($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) {
  596:         my $result = &sso_login($r,$handle,$user{'name'});
  597:         if (defined($result)) {
  598: 	    return $result;
  599:         }
  600:     }
  601: 
  602:     my ($is_balancer,$otherserver);
  603: 
  604:     if ($handle eq '') {
  605:         unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) ||
  606:                 ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
  607:                 ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') ||
  608:                 ($requrl =~ m{^/res/$match_domain/$match_username/})) {
  609: 	    $r->log_reason("Cookie not valid", $r->filename);
  610:         }
  611:     } elsif ($handle ne '') {
  612: 
  613: # ------------------------------------------------------ Initialize Environment
  614: 	my $lonidsdir=$r->dir_config('lonIDsDir');
  615: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  616: 
  617: # --------------------------------------------------------- Initialize Language
  618: 
  619: 	&Apache::lonlocal::get_language_handle($r);
  620: 
  621:     }
  622: 
  623: # -------------------------------------------------- Should be a valid user now
  624:     if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
  625: # -------------------------------------------------------------- Resource State
  626: 
  627:         my ($cdom,$cnum);
  628:         if ($env{'request.course.id'}) {
  629:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  630:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  631:         }
  632: 	if ($requrl=~/^\/+(res|uploaded)\//) {
  633: 	    $env{'request.state'} = "published";
  634: 	} else {
  635: 	    $env{'request.state'} = 'unknown';
  636: 	}
  637: 	$env{'request.filename'} = $r->filename;
  638: 	$env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
  639:         my ($suppext,$checkabsolute);
  640:         if ($requrl =~ m{^/adm/wrapper/ext/}) {
  641:             my $query = $r->args;
  642:             if ($query) {
  643:                 my $preserved;
  644:                 foreach my $pair (split(/&/,$query)) {
  645:                     my ($name, $value) = split(/=/,$pair);
  646:                     unless (($name eq 'symb') || ($name eq 'usehttp')) {
  647:                         $preserved .= $pair.'&';
  648:                     }
  649:                     if (($env{'request.course.id'}) && ($name eq 'folderpath')) {
  650:                         if ($value =~ /^supplemental/) {
  651:                             $suppext = 1;
  652:                         }
  653:                     }
  654:                 }
  655:                 $preserved =~ s/\&$//;
  656:                 if ($preserved) {
  657:                     $env{'request.external.querystring'} = $preserved;
  658:                 }
  659:             }
  660:             if ($env{'request.course.id'}) {
  661:                 $checkabsolute = 1;
  662:             }
  663:         } elsif ($env{'request.course.id'} &&
  664:                  (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) ||
  665:                   ($requrl eq "/public/$cdom/$cnum/syllabus") ||
  666:                   ($requrl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}))) {
  667:             my $query = $r->args;
  668:             if ($query) {
  669:                 foreach my $pair (split(/&/,$query)) {
  670:                     my ($name, $value) = split(/=/,$pair);
  671:                     if ($name eq 'folderpath') {
  672:                         if ($value =~ /^supplemental/) {
  673:                             $suppext = 1;
  674:                         }
  675:                         last;
  676:                     }
  677:                 }
  678:             }
  679:             if ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}) {
  680:                 $checkabsolute = 1;
  681:             }
  682:         }
  683:         if ($checkabsolute) {
  684:             my $hostname = $r->hostname();
  685:             my $lonhost = &Apache::lonnet::host_from_dns($hostname);
  686:             if ($lonhost) {
  687:                 my $actual = &Apache::lonnet::absolute_url($hostname,1,1);
  688:                 my $exphostname = &Apache::lonnet::hostname($lonhost);
  689:                 my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname;
  690:                 unless ($actual eq $expected) {
  691:                     $env{'request.use_absolute'} = $expected;
  692:                 }
  693:             }
  694:         }
  695: # -------------------------------------------------------- Load POST parameters
  696: 
  697: 	&Apache::lonacc::get_posted_cgi($r);
  698: 
  699: # ------------------------------------------------------ Check if load balancer 
  700: 
  701:         my $checkexempt;
  702:         if ($env{'user.loadbalexempt'} eq $r->dir_config('lonHostID')) {
  703:             if ($env{'user.loadbalcheck.time'} + 600 > time) {
  704:                 $checkexempt = 1;
  705:             }
  706:         }
  707:         if ($env{'user.noloadbalance'} eq $r->dir_config('lonHostID')) {
  708:             $checkexempt = 1;
  709:         }
  710:         unless (($checkexempt) || (($requrl eq '/adm/switchserver') && (!$r->is_initial_req()))) {
  711:             ($is_balancer,$otherserver) =
  712:                 &Apache::lonnet::check_loadbalancing($env{'user.name'},
  713:                                                      $env{'user.domain'});
  714:             if ($is_balancer) {
  715:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  716:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  717:                 if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
  718:                     $otherserver = $found_server;
  719:                 }
  720:                 unless ($requrl eq '/adm/switchserver') {
  721:                     $r->set_handlers('PerlResponseHandler'=>
  722:                                      [\&Apache::switchserver::handler]);
  723:                 }
  724:                 if ($otherserver ne '') {
  725:                     $env{'form.otherserver'} = $otherserver;
  726:                 }
  727:                 unless (($env{'form.origurl'}) || ($r->uri eq '/adm/roles') ||
  728:                         ($r->uri eq '/adm/switchserver') || ($r->uri eq '/adm/sso')) {
  729:                     $env{'form.origurl'} = $r->uri;
  730:                 }
  731:             }
  732:         }
  733:         if ($requrl=~m{^/+tiny/+$match_domain/+\w+$}) {
  734:             if ($env{'user.name'} eq 'public' &&
  735:                 $env{'user.domain'} eq 'public') {
  736:                 $env{'request.firsturl'}=$requrl;
  737:                 return FORBIDDEN;
  738:             } else {
  739:                 return OK;
  740:             }
  741:         }
  742: # ---------------------------------------------------------------- Check access
  743: 	my $now = time;
  744:         my ($check_symb,$check_access,$check_block,$access,$poss_symb);
  745: 	if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/}
  746: 	    || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
  747:             $check_access = 1;
  748:         }
  749:         if ((!$check_access) && ($env{'request.course.id'})) {
  750:             if (($requrl eq '/adm/viewclasslist') ||
  751:                 ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) ||
  752:                 ($requrl =~ m{^/adm/.*/aboutme$}) ||
  753:                 ($requrl=~m{^/adm/coursedocs/showdoc/}) ||
  754:                 ($requrl=~m{^(/adm/wrapper|)/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  755:                 $check_block = 1;
  756:             }
  757:         }
  758:         if (($env{'request.course.id'}) && (!$suppext)) {
  759:             $requrl=~/\.(\w+)$/;
  760:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  761:                 ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) ||
  762:                 ($requrl=~/^\/adm\/wrapper\//) ||
  763:                 ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
  764:                 ($requrl=~m|\.problem/smpedit$|) ||
  765:                 ($requrl=~/^\/public\/.*\/syllabus$/) ||
  766:                 ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) ||
  767:                 ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/) ||
  768:                 ($requrl=~m{^/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  769:                 $check_symb = 1;
  770:             }
  771:         }
  772:         if (($check_access) || ($check_block)) {
  773:             if ($check_symb) {
  774:                 if ($env{'form.symb'}) {
  775:                     $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'});
  776:                 } elsif (($env{'request.course.id'}) && ($r->args ne '')) {
  777:                     my $query = $r->args;
  778:                     foreach my $pair (split(/&/,$query)) {
  779:                         my ($name, $value) = split(/=/,$pair);
  780:                         $name = &unescape($name);
  781:                         $value =~ tr/+/ /;
  782:                         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  783:                         if ($name eq 'symb') {
  784:                             $poss_symb = &Apache::lonnet::symbclean($value);
  785:                             last;
  786:                         }
  787:                     }
  788:                 }
  789:                 if ($poss_symb) {
  790:                     my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb);
  791:                     $url = &Apache::lonnet::clutter($url);
  792:                     my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'};
  793:                     unless (($url eq $requrl) && (($possmap eq $toplevelmap) ||
  794:                                                   (&Apache::lonnet::is_on_map($possmap)))) {
  795:                         undef($poss_symb);
  796:                     }
  797:                     if ($poss_symb) {
  798:                         if ((!$env{'request.role.adv'}) && ($env{'acc.randomout'}) &&
  799:                             ($env{'acc.randomout'}=~/\&\Q$poss_symb\E\&/)) {
  800:                             undef($poss_symb);
  801:                         } elsif ((!$env{'request.role.adv'}) && ($env{'acc.deeplinkout'}) &&
  802:                                  ($env{'acc.deeplinkout'}=~/\&\Q$poss_symb\E\&/)) {
  803:                             undef($poss_symb);
  804:                         }
  805:                     }
  806:                 }
  807:                 if ($poss_symb) {
  808:                     $access=&Apache::lonnet::allowed('bre',$requrl,$poss_symb);
  809:                 } else {
  810:                     $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1);
  811:                 }
  812:             } else {
  813:                 my $nodeeplinkcheck;
  814:                 if (($check_access) && ($requrl =~ /\.(sequence|page)$/)) {
  815:                     unless ($env{'form.navmap'}) {
  816:                         if ($r->args ne '') {
  817:                             &Apache::loncommon::get_unprocessed_cgi($r->args,['navmap']);
  818:                             unless ($env{'form.navmap'}) {
  819:                                 $nodeeplinkcheck = 1;
  820:                             }
  821:                         }
  822:                     }
  823:                 }
  824:                 my $clientip = &Apache::lonnet::get_requestor_ip($r);
  825:                 $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip,'','',$nodeeplinkcheck);
  826:             }
  827:         }
  828:         if ($check_block) {
  829:             if ($access eq 'B') {
  830:                 if ($poss_symb) {
  831:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
  832:                         $env{'request.symb'} = $poss_symb;
  833:                     }
  834:                 }
  835:                 &Apache::blockedaccess::setup_handler($r);
  836:                 return OK;
  837:             }
  838:         } elsif ($check_access) {
  839:             if ($handle eq '') {
  840:                 unless ($access eq 'F') {
  841:                     if ($requrl =~ m{^/res/$match_domain/$match_username/}) {
  842:                         $r->log_reason("Cookie not valid", $r->filename);
  843:                     }
  844:                 }
  845:             }
  846: 	    if ($access eq '1') {
  847: 		$env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  848: 		return HTTP_NOT_ACCEPTABLE; 
  849: 	    }
  850: 	    if ($access eq 'A') {
  851: 		&Apache::restrictedaccess::setup_handler($r);
  852: 		return OK;
  853: 	    }
  854:             if ($access eq 'B') {
  855:                 if ($poss_symb) {
  856:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
  857:                         $env{'request.symb'} = $poss_symb;
  858:                     }
  859:                 }
  860:                 &Apache::blockedaccess::setup_handler($r);
  861:                 return OK;
  862:             }
  863:             if ($access eq 'D') {
  864:                 &Apache::lonprotected::setup_handler($r);
  865:                 return OK;
  866:             }
  867: 	    if (($access ne '2') && ($access ne 'F')) {
  868:                 if ($requrl =~ m{^/res/}) {
  869:                     $access = &Apache::lonnet::allowed('bro',$requrl);
  870:                     if ($access ne 'F') {
  871:                         if ($requrl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
  872:                             $access = &Apache::lonnet::allowed('bre','/res/lib/templates/simpleproblem.problem');
  873:                             if ($access ne 'F') {
  874:                                 $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  875:                                 return HTTP_NOT_ACCEPTABLE;
  876:                             }
  877:                         } else {
  878:                             $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  879:                             return HTTP_NOT_ACCEPTABLE;
  880:                         }
  881:                     }
  882:                 } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) {
  883:                     my $clientip = &Apache::lonnet::get_requestor_ip($r);
  884:                     if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') {
  885:                         $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  886:                         return HTTP_NOT_ACCEPTABLE;
  887:                     }
  888:                 } else {
  889: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  890: 		    return HTTP_NOT_ACCEPTABLE;
  891:                 }
  892: 	    }
  893: 	}
  894: 	if ($requrl =~ m|^/prtspool/|) {
  895: 	    my $start='/prtspool/'.$env{'user.name'}.'_'.
  896: 		$env{'user.domain'};
  897: 	    if ($requrl !~ /^\Q$start\E/) {
  898: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  899: 		return HTTP_NOT_ACCEPTABLE;
  900: 	    }
  901: 	}
  902: 	if ($requrl =~ m|^/zipspool/|) {
  903: 	    my $start='/zipspool/zipout/'.$env{'user.name'}.":".
  904: 		$env{'user.domain'};
  905: 	    if ($requrl !~ /^\Q$start\E/) {
  906: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  907: 		return HTTP_NOT_ACCEPTABLE;
  908: 	    }
  909: 	}
  910: 	if ($env{'user.name'} eq 'public' && 
  911: 	    $env{'user.domain'} eq 'public' &&
  912: 	    $requrl !~ m{^/+(res|public|uploaded)/} &&
  913: 	    $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
  914:             $requrl !~ m{^/adm/blockingstatus/.*$} &&
  915: 	    $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
  916: 	    $env{'request.querystring'}=$r->args;
  917: 	    $env{'request.firsturl'}=$requrl;
  918: 	    return FORBIDDEN;
  919: 	}
  920: # ------------------------------------------------------------- This is allowed
  921: 	if ($env{'request.course.id'}) {
  922: 	    &Apache::lonnet::countacc($requrl);
  923:             my $query=$r->args;
  924:             if ($check_symb) {
  925: # ------------------------------------- This is serious stuff, get symb and log
  926: 		my $symb;
  927: 		if ($query) {
  928: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb','folderpath']);
  929: 		}
  930: 		if ($env{'form.symb'}) {
  931: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
  932:                     if (($requrl eq '/adm/navmaps') ||
  933:                         ($requrl =~ m{^/adm/wrapper/}) ||
  934:                         ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
  935:                         unless (&Apache::lonnet::symbverify($symb,$requrl)) {
  936:                             if (&Apache::lonnet::is_on_map($requrl)) {
  937:                                 $symb = &Apache::lonnet::symbread($requrl);
  938:                                 unless (&Apache::lonnet::symbverify($symb,$requrl)) {
  939:                                     undef($symb);
  940:                                 }
  941:                             }
  942:                         }
  943:                         if ($symb) {
  944:                             if ($requrl eq '/adm/navmaps') {
  945:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  946:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid]);
  947:                             } elsif (($requrl =~ m{^/adm/wrapper/}) ||
  948:                                      ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
  949:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  950:                                 if ($map =~ /\.page$/) {
  951:                                     my $mapsymb = &Apache::lonnet::symbread($map);
  952:                                     ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
  953:                                 }
  954:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  955:                                                           'last_known' =>[$murl,$mid]);
  956:                             }
  957:                         }
  958: 		    } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
  959: 			     (($requrl=~m|(.*)/smpedit$|) &&
  960: 			      &Apache::lonnet::symbverify($symb,$1)) ||
  961:                              (($requrl=~m|(.*/aboutme)/portfolio$|) &&
  962:                               &Apache::lonnet::symbverify($symb,$1))) {
  963: 			my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  964:                         if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
  965:                             my $mapsymb = &Apache::lonnet::symbread($map);
  966:                             ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
  967:                         }
  968: 			&Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  969: 						  'last_known' =>[$murl,$mid]);
  970: 		    } else {
  971: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  972: 				       $symb);
  973: 			$env{'user.error.msg'}=
  974: 			    "$requrl:bre:1:1:Invalid Access";
  975: 			return HTTP_NOT_ACCEPTABLE; 
  976: 		    }
  977: 		} else {
  978:                     if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) {
  979:                         $requrl = $1;
  980:                     }
  981: 		    $symb=&Apache::lonnet::symbread($requrl);
  982:                     if (&Apache::lonnet::is_on_map($requrl) && $symb) {
  983:                         my ($encstate,$invalidsymb);
  984:                         unless (&Apache::lonnet::symbverify($symb,$requrl,\$encstate)) {
  985:                             $invalidsymb = 1;
  986:                             #
  987:                             # If $env{'request.enc'} inconsistent with encryption expected for $symb
  988:                             # retrieved by lonnet::symbread(), call again to check for an instance of
  989:                             # $requrl in the course for which expected encryption matches request.enc.
  990:                             # If symb for different instance passes lonnet::symbverify(), use that as
  991:                             # the symb for $requrl and call &Apache::lonnet::allowed() for that symb.
  992:                             # Report invalid symb if there is no other symb. Redirect to /adm/ambiguous
  993:                             # if multiple possible symbs consistent with request.enc available for $requrl.
  994:                             #
  995:                             if (($env{'request.enc'} && !$encstate) || (!$env{'request.enc'} && $encstate)) {
  996:                                 my %possibles;
  997:                                 my $nocache = 1;
  998:                                 my $oldsymb = $symb;
  999:                                 $symb = &Apache::lonnet::symbread($requrl,'','','',\%possibles,$nocache);
 1000:                                 if (($symb) && ($symb ne $oldsymb)) {
 1001:                                     if (&Apache::lonnet::symbverify($symb,$requrl)) {
 1002:                                         my $access=&Apache::lonnet::allowed('bre',$requrl,$symb);
 1003:                                         if ($access eq 'B') {
 1004:                                             $env{'request.symb'} = $symb;
 1005:                                             &Apache::blockedaccess::setup_handler($r);
 1006:                                             return OK;
 1007:                                         } elsif (($access eq '2') || ($access eq 'F')) {
 1008:                                             $invalidsymb = '';
 1009:                                         }
 1010:                                     }
 1011:                                 } elsif (keys(%possibles) > 1) {
 1012:                                     $r->internal_redirect('/adm/ambiguous');
 1013:                                     return OK;
 1014:                                 }
 1015:                             }
 1016:                             if ($invalidsymb) {
 1017:                                 if ($requrl eq '/adm/navmaps') {
 1018:                                     undef($symb);
 1019:                                 } else {
 1020:                                     $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
 1021:                                     $env{'user.error.msg'}=
 1022:                                         "$requrl:bre:1:1:Invalid Access";
 1023:                                     return HTTP_NOT_ACCEPTABLE;
 1024:                                 }
 1025:                             }
 1026:                         }
 1027:                     }
 1028: 		    if ($symb) {
 1029: 			my ($map,$mid,$murl)=
 1030: 			    &Apache::lonnet::decode_symb($symb);
 1031:                         if ($requrl eq '/adm/navmaps') {
 1032:                             &Apache::lonnet::symblist($map,$murl =>[$murl,$mid]);
 1033:                         } else {
 1034:                             if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
 1035:                                 my $mapsymb = &Apache::lonnet::symbread($map);
 1036:                                 ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1037:                             }
 1038: 			    &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
 1039: 						      'last_known' =>[$murl,$mid]);
 1040:                         }
 1041: 		    }
 1042: 		}
 1043: 		$env{'request.symb'}=$symb;
 1044:                 if (($env{'request.symbread.cached.'}) && ($env{'request.symbread.cached.'} ne $symb)) {
 1045:                     $env{'request.symbread.cached.'} = $symb;
 1046:                 }
 1047: 		&Apache::lonnet::courseacclog($symb);
 1048: 	    } else {
 1049: # ------------------------------------------------------- This is other content
 1050: 		&Apache::lonnet::courseacclog($requrl);    
 1051: 	    }
 1052:             if ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/.+\.html?$}) {
 1053:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1054:                     if ($query) {
 1055:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit']);
 1056:                         if ($env{'form.forceedit'}) {
 1057:                             $env{'request.state'} = 'edit';
 1058:                         }
 1059:                     }
 1060:                 }
 1061:             } elsif ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/portfolio/syllabus/.+\.html?$}) {
 1062:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1063:                     if ($query) {
 1064:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit','editmode']);
 1065:                         if (($env{'form.forceedit'}) || ($env{'form.editmode'})) {
 1066:                             $env{'request.state'} = 'edit';
 1067:                         }
 1068:                     }
 1069:                 }
 1070:             }
 1071: 	}
 1072: 	return OK;
 1073:     } else {
 1074:         my $defdom=$r->dir_config('lonDefDomain');
 1075:         ($is_balancer,$otherserver) =
 1076:             &Apache::lonnet::check_loadbalancing(undef,$defdom);
 1077:         if ($is_balancer) {
 1078:             $r->set_handlers('PerlResponseHandler'=>
 1079:                              [\&Apache::switchserver::handler]);
 1080:             if ($otherserver ne '') {
 1081:                 $env{'form.otherserver'} = $otherserver;
 1082:             }
 1083:         }
 1084:     }
 1085: # -------------------------------------------- See if this is a public resource
 1086:     if ($requrl=~m|^/+adm/+help/+|) {
 1087:  	return OK;
 1088:     }
 1089: # ------------------------------------ See if this is a viewable portfolio file
 1090:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
 1091:         my $clientip = &Apache::lonnet::get_requestor_ip($r);
 1092:         my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip);
 1093: 	if ($access eq 'A') {
 1094: 	    &Apache::restrictedaccess::setup_handler($r);
 1095: 	    return OK;
 1096: 	}
 1097: 	if (($access ne '2') && ($access ne 'F')) {
 1098: 	    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1099: 	    return HTTP_NOT_ACCEPTABLE;
 1100: 	}
 1101:     }
 1102: 
 1103: # -------------------------------------------------------------- Not authorized
 1104:     $requrl=~/\.(\w+)$/;
 1105: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
 1106: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
 1107: #        ($requrl=~m|^/prtspool/|)) {
 1108: # -------------------------- Store where they wanted to go and get login screen
 1109: 	$env{'request.querystring'}=$r->args;
 1110: 	$env{'request.firsturl'}=$requrl;
 1111:        return FORBIDDEN;
 1112: #   } else {
 1113: # --------------------------------------------------------------------- Goodbye
 1114: #       return HTTP_BAD_REQUEST;
 1115: #   }
 1116: }
 1117: 
 1118: 1;
 1119: __END__
 1120: 
 1121: =pod
 1122: 
 1123: =back
 1124: 
 1125: =cut
 1126: 

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