Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1523

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1523  ! raeburn     4: # $Id: lonnet.pm,v 1.1522 2023/12/28 18:14:09 raeburn Exp $
1.178     www         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: #
1.169     harris41   28: ###
                     29: 
1.971     jms        30: =pod
                     31: 
1.972     jms        32: =head1 NAME
                     33: 
                     34: Apache::lonnet.pm
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: This file is an interface to the lonc processes of
                     39: the LON-CAPA network as well as set of elaborated functions for handling information
                     40: necessary for navigating through a given cluster of LON-CAPA machines within a
                     41: domain. There are over 40 specialized functions in this module which handle the
                     42: reading and transmission of metadata, user information (ids, names, environments, roles,
                     43: logs), file information (storage, reading, directories, extensions, replication, embedded
                     44: styles and descriptors), educational resources (course descriptions, section names and
                     45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
                     46: and from more descriptive phrases or explanations.
                     47: 
                     48: This is part of the LearningOnline Network with CAPA project
                     49: described at http://www.lon-capa.org.
                     50: 
1.971     jms        51: =head1 Package Variables
                     52: 
                     53: These are largely undocumented, so if you decipher one please note it here.
                     54: 
                     55: =over 4
                     56: 
                     57: =item $processmarker
                     58: 
                     59: Contains the time this process was started and this servers host id.
                     60: 
                     61: =item $dumpcount
                     62: 
                     63: Counts the number of times a message log flush has been attempted (regardless
                     64: of success) by this process.  Used as part of the filename when messages are
                     65: delayed.
                     66: 
                     67: =back
                     68: 
                     69: =cut
                     70: 
1.1       albertel   71: package Apache::lonnet;
                     72: 
                     73: use strict;
1.486     www        74: use HTTP::Date;
1.977     amueller   75: use Image::Magick;
1.1389    raeburn    76: use CGI::Cookie;
1.1182    foxr       77: 
1.1173    foxr       78: use Encode;
                     79: 
1.1405    raeburn    80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
1.1138    raeburn    81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
1.1416    raeburn    82:             %managerstab $passwdmin);
1.871     albertel   83: 
                     84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        87:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        88: 
1.1       albertel   89: use IO::Socket;
1.31      www        90: use GDBM_File;
1.208     albertel   91: use HTML::LCParser;
1.88      www        92: use Fcntl qw(:flock);
1.870     albertel   93: use Storable qw(thaw nfreeze);
1.1289    damieng    94: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599     albertel   95: use Cache::Memcached;
1.676     albertel   96: use Digest::MD5;
1.790     albertel   97: use Math::Random;
1.1024    raeburn    98: use File::MMagic;
1.1434    raeburn    99: use Net::CIDR;
1.1456    raeburn   100: use Sys::Hostname::FQDN();
1.807     albertel  101: use LONCAPA qw(:DEFAULT :match);
1.740     www       102: use LONCAPA::Configuration;
1.1160    www       103: use LONCAPA::lonmetadata;
1.1167    droeschl  104: use LONCAPA::Lond;
1.1345    raeburn   105: use LONCAPA::LWPReq;
1.1406    raeburn   106: use LONCAPA::transliterate;
1.1117    foxr      107: 
1.1090    raeburn   108: use File::Copy;
1.676     albertel  109: 
1.195     www       110: my $readit;
1.1288    damieng   111: my $max_connection_retries = 20;     # Or some such value.
1.1       albertel  112: 
1.619     albertel  113: require Exporter;
                    114: 
                    115: our @ISA = qw (Exporter);
                    116: our @EXPORT = qw(%env);
                    117: 
1.449     matthew   118: 
1.1187    raeburn   119: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729     www       120: {
                    121:     my $logid;
1.1187    raeburn   122:     sub write_log {
1.1188    raeburn   123: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
1.1184    raeburn   124:         if ($context eq 'course') {
                    125:             if (($cnum eq '') || ($cdom eq '')) {
                    126:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    127:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    128:             }
1.957     raeburn   129:         }
1.1184    raeburn   130: 	$logid ++;
1.957     raeburn   131:         my $now = time();
                    132: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.1434    raeburn   133:         my $ip = &get_requestor_ip();
1.1184    raeburn   134:         my $logentry = { 
                    135:                           $id => {
                    136:                                    'exe_uname' => $env{'user.name'},
                    137:                                    'exe_udom'  => $env{'user.domain'},
                    138:                                    'exe_time'  => $now,
1.1434    raeburn   139:                                    'exe_ip'    => $ip,
1.1184    raeburn   140:                                    'delflag'   => $delflag,
                    141:                                    'logentry'  => $storehash,
                    142:                                    'uname'     => $uname,
                    143:                                    'udom'      => $udom,
                    144:                                   }
                    145:                        };
                    146: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729     www       147:     }
                    148: }
1.1       albertel  149: 
1.163     harris41  150: sub logtouch {
                    151:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  152:     unless (-e "$execdir/logs/lonnet.log") {	
1.1359    raeburn   153: 	open(my $fh,">>","$execdir/logs/lonnet.log");
1.163     harris41  154: 	close $fh;
                    155:     }
                    156:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    157:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    158: }
                    159: 
1.1       albertel  160: sub logthis {
                    161:     my $message=shift;
                    162:     my $execdir=$perlvar{'lonDaemons'};
                    163:     my $now=time;
                    164:     my $local=localtime($now);
1.1359    raeburn   165:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986     foxr      166: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    167: 	print $fh $logstring;
1.448     albertel  168: 	close($fh);
                    169:     }
1.1       albertel  170:     return 1;
                    171: }
                    172: 
                    173: sub logperm {
                    174:     my $message=shift;
                    175:     my $execdir=$perlvar{'lonDaemons'};
                    176:     my $now=time;
                    177:     my $local=localtime($now);
1.1359    raeburn   178:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448     albertel  179: 	print $fh "$now:$message:$local\n";
                    180: 	close($fh);
                    181:     }
1.1       albertel  182:     return 1;
                    183: }
                    184: 
1.850     albertel  185: sub create_connection {
1.853     albertel  186:     my ($hostname,$lonid) = @_;
1.851     albertel  187:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  188: 				     Type    => SOCK_STREAM,
                    189: 				     Timeout => 10);
                    190:     return 0 if (!$client);
1.1399    raeburn   191:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
1.850     albertel  192:     my $result = <$client>;
                    193:     chomp($result);
                    194:     return 1 if ($result eq 'done');
                    195:     return 0;
                    196: }
                    197: 
1.983     raeburn   198: sub get_server_timezone {
                    199:     my ($cnum,$cdom) = @_;
                    200:     my $home=&homeserver($cnum,$cdom);
                    201:     if ($home ne 'no_host') {
                    202:         my $cachetime = 24*3600;
                    203:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    204:         if (defined($cached)) {
                    205:             return $timezone;
                    206:         } else {
                    207:             my $timezone = &reply('servertimezone',$home);
                    208:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    209:         }
                    210:     }
                    211: }
1.850     albertel  212: 
1.1106    raeburn   213: sub get_server_distarch {
                    214:     my ($lonhost,$ignore_cache) = @_;
                    215:     if (defined($lonhost)) {
                    216:         if (!defined(&hostname($lonhost))) {
                    217:             return;
                    218:         }
                    219:         my $cachetime = 12*3600;
                    220:         if (!$ignore_cache) {
                    221:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    222:             if (defined($cached)) {
                    223:                 return $distarch;
                    224:             }
                    225:         }
                    226:         my $rep = &reply('serverdistarch',$lonhost);
                    227:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    228:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    229:                 $rep eq '') {
                    230:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    231:         }
                    232:     }
                    233:     return;
                    234: }
                    235: 
1.1315    raeburn   236: sub get_servercerts_info {
1.1383    raeburn   237:     my ($lonhost,$hostname,$context) = @_;
                    238:     return if ($lonhost eq '');
                    239:     if ($hostname eq '') {
                    240:         $hostname = &hostname($lonhost);
                    241:     }
                    242:     return if ($hostname eq '');
1.1315    raeburn   243:     my ($rep,$uselocal);
1.1388    raeburn   244:     if ($context eq 'install') {
1.1387    raeburn   245:         $uselocal = 1;
                    246:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
1.1315    raeburn   247:         $uselocal = 1;
                    248:     }
1.1387    raeburn   249:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
1.1315    raeburn   250:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
1.1323    raeburn   251:         if ($distro eq '') {
                    252:             $uselocal = 0;
                    253:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
1.1315    raeburn   254:             if ($1 < 6) {
                    255:                 $uselocal = 0;
                    256:             }
1.1346    raeburn   257:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
                    258:             if ($1 < 12) {
                    259:                 $uselocal = 0;
                    260:             }
1.1315    raeburn   261:         }
                    262:     }
                    263:     if ($uselocal) {
1.1383    raeburn   264:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
1.1315    raeburn   265:     } else {
                    266:         $rep=&reply('servercerts',$lonhost);
                    267:     }
                    268:     my ($result,%returnhash);
                    269:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    270:         ($rep eq 'unknown_cmd')) {
                    271:         $result = $rep;
                    272:     } else {
                    273:         $result = 'ok';
                    274:         my @pairs=split(/\&/,$rep);
                    275:         foreach my $item (@pairs) {
                    276:             my ($key,$value)=split(/=/,$item,2);
                    277:             my $what = &unescape($key);
                    278:             $returnhash{$what}=&thaw_unescape($value);
                    279:         }
                    280:     }
                    281:     return ($result,\%returnhash);
                    282: }
                    283: 
1.993     raeburn   284: sub get_server_loncaparev {
1.1073    raeburn   285:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   286:     if (defined($lonhost)) {
                    287:         if (!defined(&hostname($lonhost))) {
                    288:             undef($lonhost);
                    289:         }
                    290:     }
                    291:     if (!defined($lonhost)) {
                    292:         if (defined(&domain($dom,'primary'))) {
                    293:             $lonhost=&domain($dom,'primary');
                    294:             if ($lonhost eq 'no_host') {
                    295:                 undef($lonhost);
                    296:             }
                    297:         }
                    298:     }
                    299:     if (defined($lonhost)) {
1.1073    raeburn   300:         my $cachetime = 12*3600;
                    301:         if (!$ignore_cache) {
                    302:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    303:             if (defined($cached)) {
                    304:                 return $loncaparev;
                    305:             }
                    306:         }
                    307:         my ($answer,$loncaparev);
                    308:         my @ids=&current_machine_ids();
                    309:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    310:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   311:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   312:                 $loncaparev = $1;
                    313:             }
                    314:         } else {
                    315:             $answer = &reply('serverloncaparev',$lonhost);
                    316:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    317:                 if ($caller eq 'loncron') {
1.1397    raeburn   318:                     my $hostname = &hostname($lonhost);
1.1073    raeburn   319:                     my $protocol = $protocol{$lonhost};
                    320:                     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn   321:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073    raeburn   322:                     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn   323:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
1.1073    raeburn   324:                     unless ($response->is_error()) {
                    325:                         my $content = $response->content;
1.1081    raeburn   326:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   327:                             $loncaparev = $1;
                    328:                         }
                    329:                     }
                    330:                 } else {
                    331:                     $loncaparev = $loncaparevs{$lonhost};
                    332:                 }
1.1081    raeburn   333:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   334:                 $loncaparev = $1;
                    335:             }
1.993     raeburn   336:         }
1.1073    raeburn   337:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   338:     }
                    339: }
                    340: 
1.1074    raeburn   341: sub get_server_homeID {
                    342:     my ($hostname,$ignore_cache,$caller) = @_;
                    343:     unless ($ignore_cache) {
                    344:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    345:         if (defined($cached)) {
                    346:             return $serverhomeID;
                    347:         }
                    348:     }
                    349:     my $cachetime = 12*3600;
                    350:     my $serverhomeID;
                    351:     if ($caller eq 'loncron') { 
                    352:         my @machine_ids = &machine_ids($hostname);
                    353:         foreach my $id (@machine_ids) {
                    354:             my $response = &reply('serverhomeID',$id);
                    355:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    356:                 $serverhomeID = $response;
                    357:                 last;
                    358:             }
                    359:         }
                    360:         if ($serverhomeID eq '') {
                    361:             $serverhomeID = $machine_ids[-1];
                    362:         }
                    363:     } else {
                    364:         $serverhomeID = $serverhomeIDs{$hostname};
                    365:     }
                    366:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    367: }
                    368: 
1.1121    raeburn   369: sub get_remote_globals {
                    370:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   371:     my ($result,%returnhash,%whatneeded);
                    372:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   373:         foreach my $what (sort(keys(%{$whathash}))) {
                    374:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   375:             my ($response,$cached);
1.1121    raeburn   376:             unless ($ignore_cache) {
1.1125    raeburn   377:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   378:             }
                    379:             if (defined($cached)) {
1.1125    raeburn   380:                 $returnhash{$what} = $response;
1.1121    raeburn   381:             } else {
1.1125    raeburn   382:                 $whatneeded{$what} = 1;
1.1121    raeburn   383:             }
                    384:         }
1.1125    raeburn   385:         if (keys(%whatneeded) == 0) {
                    386:             $result = 'ok';
                    387:         } else {
1.1121    raeburn   388:             my $requested = &freeze_escape(\%whatneeded);
                    389:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   390:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    391:                 ($rep eq 'unknown_cmd')) {
                    392:                 $result = $rep;
                    393:             } else {
                    394:                 $result = 'ok';
1.1121    raeburn   395:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   396:                 foreach my $item (@pairs) {
                    397:                     my ($key,$value)=split(/=/,$item,2);
                    398:                     my $what = &unescape($key);
                    399:                     my $hashid = $lonhost.'-'.$what;
                    400:                     $returnhash{$what}=&thaw_unescape($value);
                    401:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   402:                 }
                    403:             }
                    404:         }
                    405:     }
1.1125    raeburn   406:     return ($result,\%returnhash);
1.1121    raeburn   407: }
                    408: 
1.1124    raeburn   409: sub remote_devalidate_cache {
1.1241    raeburn   410:     my ($lonhost,$cachekeys) = @_;
                    411:     my $items;
                    412:     return unless (ref($cachekeys) eq 'ARRAY');
                    413:     my $cachestr = join('&',@{$cachekeys});
                    414:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124    raeburn   415:     return $response;
                    416: }
                    417: 
1.1510    raeburn   418: sub sign_lti {
1.1511    raeburn   419:     my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
1.1510    raeburn   420:     my $chome;
                    421:     if (&domain($cdom) ne '') {
1.1511    raeburn   422:         if ($crsdef) {
1.1510    raeburn   423:             $chome = &homeserver($cnum,$cdom);
                    424:         } else {
                    425:             $chome = &domain($cdom,'primary');
                    426:         }
                    427:     }
                    428:     if ($cdom && $chome && ($chome ne 'no_host')) {
                    429:         if ((ref($paramsref) eq 'HASH') &&
                    430:             (ref($inforef) eq 'HASH')) {
                    431:             my $rep;
                    432:             if (grep { $_ eq $chome } &current_machine_ids()) {
                    433:                 # domain information is hosted on this machine
                    434:                 $rep =
1.1511    raeburn   435:                     &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
                    436:                                                      $context,$url,$ltinum,$keynum,
                    437:                                                      $perlvar{'lonVersion'},
                    438:                                                      $paramsref,$inforef);
                    439:                 if (ref($rep) eq 'HASH') {
1.1510    raeburn   440:                     return ('ok',$rep);
                    441:                 }
                    442:             } else {
                    443:                 my ($escurl,$params,$info);
                    444:                 $escurl = &escape($url);
                    445:                 if (ref($paramsref) eq 'HASH') {
                    446:                     $params = &freeze_escape($paramsref);
                    447:                 }
                    448:                 if (ref($inforef) eq 'HASH') {
                    449:                     $info = &freeze_escape($inforef);
                    450:                 }
1.1511    raeburn   451:                 $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
1.1510    raeburn   452:             }
                    453:             if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
                    454:                 return ();
1.1511    raeburn   455:             } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
                    456:                      ($inforef->{'respfmt'} eq 'to_authorization_header')) {
                    457:                 return ('ok',$rep);
1.1510    raeburn   458:             } else {
                    459:                 my %returnhash;
                    460:                 foreach my $item (split(/\&/,$rep)) {
                    461:                     my ($name,$value)=split(/\=/,$item);
                    462:                     $returnhash{&unescape($name)}=&thaw_unescape($value);
                    463:                 }
                    464:                 return('ok',\%returnhash);
                    465:             }
                    466:         } else {
                    467:             return ();
                    468:         }
                    469:     } else {
                    470:         return ();
                    471:         &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
                    472:     }
                    473: }
                    474: 
1.1       albertel  475: # -------------------------------------------------- Non-critical communication
                    476: sub subreply {
                    477:     my ($cmd,$server)=@_;
1.838     albertel  478:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      479:     #
                    480:     #  With loncnew process trimming, there's a timing hole between lonc server
                    481:     #  process exit and the master server picking up the listen on the AF_UNIX
                    482:     #  socket.  In that time interval, a lock file will exist:
                    483: 
                    484:     my $lockfile=$peerfile.".lock";
                    485:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
1.1289    damieng   486: 	sleep(0.1);
1.549     foxr      487:     }
                    488:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      489:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      490:     #
1.550     foxr      491:     #   We'll give the connection a few tries before abandoning it.  If
                    492:     #   connection is not possible, we'll con_lost back to the client.
                    493:     #   
                    494:     my $client;
                    495:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    496: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    497: 				      Type    => SOCK_STREAM,
                    498: 				      Timeout => 10);
1.869     albertel  499: 	if ($client) {
1.550     foxr      500: 	    last;		# Connected!
1.850     albertel  501: 	} else {
1.853     albertel  502: 	    &create_connection(&hostname($server),$server);
1.550     foxr      503: 	}
1.1289    damieng   504:         sleep(0.1);	# Try again later if failed connection.
1.550     foxr      505:     }
                    506:     my $answer;
                    507:     if ($client) {
1.704     albertel  508: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      509: 	$answer=<$client>;
                    510: 	if (!$answer) { $answer="con_lost"; }
                    511: 	chomp($answer);
                    512:     } else {
                    513: 	$answer = 'con_lost';	# Failed connection.
                    514:     }
1.1       albertel  515:     return $answer;
                    516: }
                    517: 
                    518: sub reply {
                    519:     my ($cmd,$server)=@_;
1.838     albertel  520:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  521:     my $answer=subreply($cmd,$server);
1.65      www       522:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1396    raeburn   523:         my $logged = $cmd;
                    524:         if ($cmd =~ /^encrypt:([^:]+):/) {
                    525:             my $subcmd = $1;
                    526:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
                    527:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1462    raeburn   528:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
                    529:                 ($subcmd eq 'put')) {
1.1396    raeburn   530:                 (undef,undef,my @rest) = split(/:/,$cmd);
                    531:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
                    532:                     splice(@rest,2,1,'Hidden');
                    533:                 } elsif ($subcmd eq 'passwd') {
                    534:                     splice(@rest,2,2,('Hidden','Hidden'));
                    535:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1462    raeburn   536:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
1.1396    raeburn   537:                     splice(@rest,3,1,'Hidden');
                    538:                 }
                    539:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
                    540:             }
                    541:         }
                    542:         &logthis("<font color=\"blue\">WARNING:".
                    543:                  " $logged to $server returned $answer</font>");
1.12      www       544:     }
1.1       albertel  545:     return $answer;
                    546: }
                    547: 
                    548: # ----------------------------------------------------------- Send USR1 to lonc
                    549: 
                    550: sub reconlonc {
1.891     albertel  551:     my ($lonid) = @_;
                    552:     if ($lonid) {
1.1295    raeburn   553:         my $hostname = &hostname($lonid);
1.891     albertel  554: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    555: 	if ($hostname && -e $peerfile) {
                    556: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    557: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    558: 					     Type    => SOCK_STREAM,
                    559: 					     Timeout => 10);
                    560: 	    if ($client) {
                    561: 		print $client ("reset_retries\n");
                    562: 		my $answer=<$client>;
                    563: 		#reset just this one.
                    564: 	    }
                    565: 	}
                    566: 	return;
                    567:     }
                    568: 
1.836     www       569:     &logthis("Trying to reconnect lonc");
1.1       albertel  570:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1359    raeburn   571:     if (open(my $fh,"<",$loncfile)) {
1.1       albertel  572: 	my $loncpid=<$fh>;
                    573:         chomp($loncpid);
                    574:         if (kill 0 => $loncpid) {
                    575: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    576:             kill USR1 => $loncpid;
                    577:             sleep 1;
1.1295    raeburn   578:         } else {
1.12      www       579: 	    &logthis(
1.672     albertel  580:                "<font color=\"blue\">WARNING:".
1.12      www       581:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  582:         }
                    583:     } else {
1.836     www       584: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  585:     }
                    586: }
                    587: 
                    588: # ------------------------------------------------------ Critical communication
1.12      www       589: 
1.1       albertel  590: sub critical {
                    591:     my ($cmd,$server)=@_;
1.838     albertel  592:     unless (&hostname($server)) {
1.672     albertel  593:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       594:                " Critical message to unknown server ($server)</font>");
                    595:         return 'no_such_host';
                    596:     }
1.1       albertel  597:     my $answer=reply($cmd,$server);
                    598:     if ($answer eq 'con_lost') {
1.1295    raeburn   599: 	&reconlonc($server);
1.589     albertel  600: 	my $answer=reply($cmd,$server);
1.1       albertel  601:         if ($answer eq 'con_lost') {
                    602:             my $now=time;
                    603:             my $middlename=$cmd;
1.5       www       604:             $middlename=substr($middlename,0,16);
1.1       albertel  605:             $middlename=~s/\W//g;
                    606:             my $dfilename=
1.305     www       607:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    608:             $dumpcount++;
1.1       albertel  609:             {
1.448     albertel  610: 		my $dfh;
1.1359    raeburn   611: 		if (open($dfh,">",$dfilename)) {
1.448     albertel  612: 		    print $dfh "$cmd\n"; 
                    613: 		    close($dfh);
                    614: 		}
1.1       albertel  615:             }
1.1288    damieng   616:             sleep 1;
1.1       albertel  617:             my $wcmd='';
                    618:             {
1.448     albertel  619: 		my $dfh;
1.1359    raeburn   620: 		if (open($dfh,"<",$dfilename)) {
1.448     albertel  621: 		    $wcmd=<$dfh>; 
                    622: 		    close($dfh);
                    623: 		}
1.1       albertel  624:             }
                    625:             chomp($wcmd);
1.7       www       626:             if ($wcmd eq $cmd) {
1.672     albertel  627: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       628:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  629:                 &logperm("D:$server:$cmd");
                    630: 	        return 'con_delayed';
                    631:             } else {
1.672     albertel  632:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       633:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  634:                 &logperm("F:$server:$cmd");
                    635:                 return 'con_failed';
                    636:             }
                    637:         }
                    638:     }
                    639:     return $answer;
1.405     albertel  640: }
                    641: 
1.755     albertel  642: # ------------------------------------------- check if return value is an error
                    643: 
                    644: sub error {
                    645:     my ($result) = @_;
1.756     albertel  646:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  647: 	if ($2 == 2) { return undef; }
                    648: 	return $1;
                    649:     }
                    650:     return undef;
                    651: }
                    652: 
1.783     albertel  653: sub convert_and_load_session_env {
                    654:     my ($lonidsdir,$handle)=@_;
                    655:     my @profile;
                    656:     {
1.917     albertel  657: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    658: 	if (!$opened) {
1.915     albertel  659: 	    return 0;
                    660: 	}
1.783     albertel  661: 	flock($idf,LOCK_SH);
                    662: 	@profile=<$idf>;
                    663: 	close($idf);
                    664:     }
                    665:     my %temp_env;
                    666:     foreach my $line (@profile) {
1.786     albertel  667: 	if ($line !~ m/=/) {
                    668: 	    return 0;
                    669: 	}
1.783     albertel  670: 	chomp($line);
                    671: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    672: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    673:     }
                    674:     unlink("$lonidsdir/$handle.id");
                    675:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    676: 	    0640)) {
                    677: 	%disk_env = %temp_env;
                    678: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    679: 	untie(%disk_env);
                    680:     }
1.786     albertel  681:     return 1;
1.783     albertel  682: }
                    683: 
1.374     www       684: # ------------------------------------------- Transfer profile into environment
1.780     albertel  685: my $env_loaded;
                    686: sub transfer_profile_to_env {
1.788     albertel  687:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    688:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       689: 
1.720     albertel  690:     if (!defined($lonidsdir)) {
                    691: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    692:     }
                    693:     if (!defined($handle)) {
                    694:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    695:     }
                    696: 
1.786     albertel  697:     my $convert;
                    698:     {
1.917     albertel  699:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    700: 	if (!$opened) {
1.915     albertel  701: 	    return;
                    702: 	}
1.786     albertel  703: 	flock($idf,LOCK_SH);
                    704: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    705: 		&GDBM_READER(),0640)) {
                    706: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    707: 	    untie(%disk_env);
                    708: 	} else {
                    709: 	    $convert = 1;
                    710: 	}
                    711:     }
                    712:     if ($convert) {
                    713: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    714: 	    &logthis("Failed to load session, or convert session.");
                    715: 	}
1.374     www       716:     }
1.783     albertel  717: 
1.786     albertel  718:     my %remove;
1.783     albertel  719:     while ( my $envname = each(%env) ) {
1.433     matthew   720:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    721:             if ($time < time-300) {
1.783     albertel  722:                 $remove{$key}++;
1.433     matthew   723:             }
                    724:         }
                    725:     }
1.783     albertel  726: 
1.619     albertel  727:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  728:     $env_loaded=1;
1.783     albertel  729:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   730:         &delenv($expired_key);
1.374     www       731:     }
1.1       albertel  732: }
                    733: 
1.916     albertel  734: # ---------------------------------------------------- Check for valid session 
                    735: sub check_for_valid_session {
1.1355    raeburn   736:     my ($r,$name,$userhashref,$domref) = @_;
1.916     albertel  737:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1378    raeburn   738:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
                    739:     if ($name eq 'lonDAV') {
                    740:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    741:     } else {
                    742:         $lonidsdir=$r->dir_config('lonIDsDir');
                    743:         if ($name eq '') {
                    744:             $name = 'lonID';
                    745:         }
                    746:     }
                    747:     if ($name eq 'lonID') {
                    748:         $secure = 'lonSID';
1.1337    raeburn   749:         $linkname = 'lonLinkID';
                    750:         $pubname = 'lonPubID';
1.1378    raeburn   751:         if (exists($cookies{$secure})) {
                    752:             $lonid=$cookies{$secure};
                    753:         } elsif (exists($cookies{$name})) {
                    754:             $lonid=$cookies{$name};
1.1400    raeburn   755:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
1.1337    raeburn   756:             $lonid=$cookies{$linkname};
1.1378    raeburn   757:         } elsif (exists($cookies{$pubname})) {
                    758:             $lonid=$cookies{$pubname};
1.1337    raeburn   759:         }
1.1378    raeburn   760:     } else {
                    761:         $lonid=$cookies{$name};
1.1337    raeburn   762:     }
1.916     albertel  763:     return undef if (!$lonid);
                    764: 
                    765:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1378    raeburn   766:     if (-l "$lonidsdir/$handle.id") {
                    767:         my $link = readlink("$lonidsdir/$handle.id");
                    768:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    769:             $handle = $1;
                    770:         }
1.1155    raeburn   771:     }
1.1355    raeburn   772:     if (!-e "$lonidsdir/$handle.id") {
                    773:         if ((ref($domref)) && ($name eq 'lonID') && 
                    774:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    775:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    776:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    777:                 $$domref = $possudom;
                    778:             }
                    779:         }
                    780:         return undef;
                    781:     }
1.916     albertel  782: 
1.917     albertel  783:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    784:     return undef if (!$opened);
1.916     albertel  785: 
                    786:     flock($idf,LOCK_SH);
                    787:     my %disk_env;
                    788:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    789: 	    &GDBM_READER(),0640)) {
                    790: 	return undef;	
                    791:     }
                    792: 
                    793:     if (!defined($disk_env{'user.name'})
                    794: 	|| !defined($disk_env{'user.domain'})) {
1.1394    raeburn   795:         untie(%disk_env);
1.916     albertel  796: 	return undef;
                    797:     }
1.1245    raeburn   798: 
                    799:     if (ref($userhashref) eq 'HASH') {
                    800:         $userhashref->{'name'} = $disk_env{'user.name'};
                    801:         $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1448    raeburn   802:         if ($disk_env{'request.role'}) {
                    803:             $userhashref->{'role'} = $disk_env{'request.role'};
                    804:         }
1.1361    raeburn   805:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
1.1375    raeburn   806:         if ($userhashref->{'lti'}) {
                    807:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
                    808:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
                    809:         }
1.1212    raeburn   810:     }
1.1394    raeburn   811:     untie(%disk_env);
1.1245    raeburn   812: 
1.916     albertel  813:     return $handle;
                    814: }
                    815: 
1.830     albertel  816: sub timed_flock {
                    817:     my ($file,$lock_type) = @_;
                    818:     my $failed=0;
                    819:     eval {
                    820: 	local $SIG{__DIE__}='DEFAULT';
                    821: 	local $SIG{ALRM}=sub {
                    822: 	    $failed=1;
                    823: 	    die("failed lock");
                    824: 	};
                    825: 	alarm(13);
                    826: 	flock($file,$lock_type);
                    827: 	alarm(0);
                    828:     };
                    829:     if ($failed) {
                    830: 	return undef;
                    831:     } else {
                    832: 	return 1;
                    833:     }
                    834: }
                    835: 
1.1392    raeburn   836: sub get_sessionfile_vars {
                    837:     my ($handle,$lonidsdir,$storearr) = @_;
                    838:     my %returnhash;
                    839:     unless (ref($storearr) eq 'ARRAY') {
                    840:         return %returnhash;
                    841:     }
                    842:     if (-l "$lonidsdir/$handle.id") {
                    843:         my $link = readlink("$lonidsdir/$handle.id");
                    844:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    845:             $handle = $1;
                    846:         }
                    847:     }
                    848:     if ((-e "$lonidsdir/$handle.id") &&
                    849:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    850:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    851:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    852:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
                    853:                 flock($idf,LOCK_SH);
                    854:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    855:                         &GDBM_READER(),0640)) {
                    856:                     foreach my $item (@{$storearr}) {
                    857:                         $returnhash{$item} = $disk_env{$item};
                    858:                     }
                    859:                     untie(%disk_env);
                    860:                 }
                    861:             }
                    862:         }
                    863:     }
                    864:     return %returnhash;
                    865: }
                    866: 
1.5       www       867: # ---------------------------------------------------------- Append Environment
                    868: 
                    869: sub appenv {
1.949     raeburn   870:     my ($newenv,$roles) = @_;
                    871:     if (ref($newenv) eq 'HASH') {
                    872:         foreach my $key (keys(%{$newenv})) {
                    873:             my $refused = 0;
                    874: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    875:                 $refused = 1;
                    876:                 if (ref($roles) eq 'ARRAY') {
1.1250    raeburn   877:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949     raeburn   878:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    879:                         $refused = 0;
                    880:                     }
                    881:                 }
                    882:             }
                    883:             if ($refused) {
                    884:                 &logthis("<font color=\"blue\">WARNING: ".
                    885:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    886:                          .'</font>');
                    887: 	        delete($newenv->{$key});
                    888:             } else {
                    889:                 $env{$key}=$newenv->{$key};
                    890:             }
                    891:         }
1.1376    raeburn   892:         my $lonids = $perlvar{'lonIDsDir'};
                    893:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
                    894:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    895:             if ($opened
                    896: 	        && &timed_flock($env_file,LOCK_EX)
                    897: 	        &&
                    898: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    899: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    900: 	        while (my ($key,$value) = each(%{$newenv})) {
                    901: 	            $disk_env{$key} = $value;
                    902: 	        }
                    903: 	        untie(%disk_env);
                    904:             }
1.35      www       905:         }
1.191     harris41  906:     }
1.56      www       907:     return 'ok';
                    908: }
                    909: # ----------------------------------------------------- Delete from Environment
                    910: 
                    911: sub delenv {
1.1104    raeburn   912:     my ($delthis,$regexp,$roles) = @_;
                    913:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    914:         my $refused = 1;
                    915:         if (ref($roles) eq 'ARRAY') {
                    916:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    917:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    918:                 $refused = 0;
                    919:             }
                    920:         }
                    921:         if ($refused) {
                    922:             &logthis("<font color=\"blue\">WARNING: ".
                    923:                      "Attempt to delete from environment ".$delthis);
                    924:             return 'error';
                    925:         }
1.56      www       926:     }
1.917     albertel  927:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    928:     if ($opened
1.915     albertel  929: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  930: 	&&
                    931: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    932: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  933: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   934: 	    if ($regexp) {
                    935:                 if ($key=~/^$delthis/) {
                    936:                     delete($env{$key});
                    937:                     delete($disk_env{$key});
                    938:                 } 
                    939:             } else {
                    940:                 if ($key=~/^\Q$delthis\E/) {
                    941: 		    delete($env{$key});
                    942: 		    delete($disk_env{$key});
                    943: 	        }
                    944:             }
1.448     albertel  945: 	}
1.783     albertel  946: 	untie(%disk_env);
1.5       www       947:     }
                    948:     return 'ok';
1.369     albertel  949: }
                    950: 
1.790     albertel  951: sub get_env_multiple {
                    952:     my ($name) = @_;
                    953:     my @values;
                    954:     if (defined($env{$name})) {
                    955:         # exists is it an array
                    956:         if (ref($env{$name})) {
                    957:             @values=@{ $env{$name} };
                    958:         } else {
                    959:             $values[0]=$env{$name};
                    960:         }
                    961:     }
                    962:     return(@values);
                    963: }
                    964: 
1.958     www       965: # ------------------------------------------------------------------- Locking
                    966: 
                    967: sub set_lock {
                    968:     my ($text)=@_;
                    969:     $locknum++;
                    970:     my $id=$$.'-'.$locknum;
                    971:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    972:              'session.lock.'.$id => $text});
                    973:     return $id;
                    974: }
                    975: 
                    976: sub get_locks {
                    977:     my $num=0;
                    978:     my %texts=();
                    979:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    980:        if ($lock=~/\w/) {
                    981:           $num++;
                    982:           $texts{$lock}=$env{'session.lock.'.$lock};
                    983:        }
                    984:    }
                    985:    return ($num,%texts);
                    986: }
                    987: 
                    988: sub remove_lock {
                    989:     my ($id)=@_;
                    990:     my $newlocks='';
                    991:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    992:        if (($lock=~/\w/) && ($lock ne $id)) {
                    993:           $newlocks.=','.$lock;
                    994:        }
                    995:     }
                    996:     &appenv({'session.locks' => $newlocks});
                    997:     &delenv('session.lock.'.$id);
                    998: }
                    999: 
                   1000: sub remove_all_locks {
                   1001:     my $activelocks=$env{'session.locks'};
                   1002:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                   1003:        if ($lock=~/\w/) {
                   1004:           &remove_lock($lock);
                   1005:        }
                   1006:     }
                   1007: }
                   1008: 
                   1009: 
1.369     albertel 1010: # ------------------------------------------ Find out current server userload
                   1011: sub userload {
                   1012:     my $numusers=0;
                   1013:     {
                   1014: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                   1015: 	my $filename;
                   1016: 	my $curtime=time;
                   1017: 	while ($filename=readdir(LONIDS)) {
1.925     albertel 1018: 	    next if ($filename eq '.' || $filename eq '..');
                   1019: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.1390    raeburn  1020:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404     albertel 1021: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel 1022: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel 1023: 	}
                   1024: 	closedir(LONIDS);
                   1025:     }
                   1026:     my $userloadpercent=0;
                   1027:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                   1028:     if ($maxuserload) {
1.371     albertel 1029: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel 1030:     }
1.372     albertel 1031:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel 1032:     return $userloadpercent;
1.283     www      1033: }
                   1034: 
1.1       albertel 1035: # ------------------------------ Find server with least workload from spare.tab
1.11      www      1036: 
1.1       albertel 1037: sub spareserver {
1.1451    raeburn  1038:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel 1039:     my $spare_server;
1.370     albertel 1040:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel 1041:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                   1042:                                                      :  $userloadpercent;
1.1083    raeburn  1043:     my ($uint_dom,$remotesessions);
                   1044:     if (($udom ne '') && (&domain($udom) ne '')) {
1.1494    raeburn  1045:         my $uprimary_id = &domain($udom,'primary');
                   1046:         $uint_dom = &internet_dom($uprimary_id);
                   1047:         my %udomdefaults = &get_domain_defaults($udom);
1.1083    raeburn  1048:         $remotesessions = $udomdefaults{'remotesessions'};
                   1049:     }
1.1123    raeburn  1050:     my $spareshash = &this_host_spares($udom);
                   1051:     if (ref($spareshash) eq 'HASH') {
                   1052:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1053:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279    raeburn  1054:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                   1055:                                              $try_server));
1.1123    raeburn  1056: 	        ($spare_server, $lowest_load) =
                   1057: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                   1058:             }
1.1083    raeburn  1059:         }
1.784     albertel 1060: 
1.1123    raeburn  1061:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                   1062: 
                   1063:         if (!$found_server) {
                   1064:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                   1065: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279    raeburn  1066:                     next unless (&spare_can_host($udom,$uint_dom,
                   1067:                                                  $remotesessions,$try_server));
1.1123    raeburn  1068: 	            ($spare_server, $lowest_load) =
                   1069: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                   1070:                 }
                   1071: 	    }
                   1072:         }
1.784     albertel 1073:     }
                   1074: 
                   1075:     if (!$want_server_name) {
1.1001    raeburn  1076:         if (defined($spare_server)) {
                   1077:             my $hostname = &hostname($spare_server);
1.1083    raeburn  1078:             if (defined($hostname)) {
1.1397    raeburn  1079:                 my $protocol = 'http';
                   1080:                 if ($protocol{$spare_server} eq 'https') {
                   1081:                     $protocol = $protocol{$spare_server};
                   1082:                 }
1.1494    raeburn  1083:                 my $alias = &use_proxy_alias($r,$spare_server);
1.1451    raeburn  1084:                 $hostname = $alias if ($alias ne '');
1.1001    raeburn  1085: 	        $spare_server = $protocol.'://'.$hostname;
                   1086:             }
                   1087:         }
1.784     albertel 1088:     }
                   1089:     return $spare_server;
                   1090: }
                   1091: 
                   1092: sub compare_server_load {
1.1253    raeburn  1093:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
                   1094: 
                   1095:     if ($required) {
                   1096:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                   1097:         my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1098:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   1099:         if (($major eq '' && $minor eq '') ||
                   1100:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                   1101:             return ($spare_server,$lowest_load);
                   1102:         }
                   1103:     }
1.784     albertel 1104: 
                   1105:     my $loadans     = &reply('load',    $try_server);
                   1106:     my $userloadans = &reply('userload',$try_server);
                   1107: 
                   1108:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn  1109: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel 1110:     }
                   1111: 
                   1112:     my $load;
                   1113:     if ($loadans =~ /\d/) {
                   1114: 	if ($userloadans =~ /\d/) {
                   1115: 	    #both are numbers, pick the bigger one
                   1116: 	    $load = ($loadans > $userloadans) ? $loadans 
                   1117: 		                              : $userloadans;
1.411     albertel 1118: 	} else {
1.784     albertel 1119: 	    $load = $loadans;
1.411     albertel 1120: 	}
1.784     albertel 1121:     } else {
                   1122: 	$load = $userloadans;
                   1123:     }
                   1124: 
                   1125:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                   1126: 	$spare_server = $try_server;
                   1127: 	$lowest_load  = $load;
1.370     albertel 1128:     }
1.784     albertel 1129:     return ($spare_server,$lowest_load);
1.202     matthew  1130: }
1.914     albertel 1131: 
                   1132: # --------------------------- ask offload servers if user already has a session
                   1133: sub find_existing_session {
                   1134:     my ($udom,$uname) = @_;
1.1123    raeburn  1135:     my $spareshash = &this_host_spares($udom);
                   1136:     if (ref($spareshash) eq 'HASH') {
                   1137:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1138:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                   1139:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1140:             }
                   1141:         }
                   1142:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                   1143:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                   1144:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1145:             }
                   1146:         }
1.914     albertel 1147:     }
                   1148:     return;
                   1149: }
                   1150: 
1.1411    raeburn  1151: sub delusersession {
                   1152:     my ($lonid,$udom,$uname) = @_;
                   1153:     my $uprimary_id = &domain($udom,'primary');
                   1154:     my $uintdom = &internet_dom($uprimary_id);
                   1155:     my $intdom = &internet_dom($lonid);
                   1156:     my $serverhomedom = &host_domain($lonid);
                   1157:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1158:         return &reply(join(':','delusersession',
                   1159:                             map {&escape($_)} ($udom,$uname)),$lonid);
                   1160:     }
                   1161:     return;
                   1162: }
                   1163: 
1.1389    raeburn  1164: # check if user's browser sent load balancer cookie and server still has session
                   1165: # and is not overloaded.
                   1166: sub check_for_balancer_cookie {
                   1167:     my ($r,$update_mtime) = @_;
                   1168:     my ($otherserver,$cookie);
                   1169:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                   1170:     if (exists($cookies{'balanceID'})) {
                   1171:         my $balid = $cookies{'balanceID'};
                   1172:         $cookie=&LONCAPA::clean_handle($balid->value);
                   1173:         my $balancedir=$r->dir_config('lonBalanceDir');
                   1174:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
                   1175:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
                   1176:                 my ($possudom,$possuname) = ($1,$2);
                   1177:                 my $has_session = 0;
                   1178:                 if ((&domain($possudom) ne '') &&
                   1179:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
                   1180:                     my $try_server;
                   1181:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
                   1182:                     if ($opened) {
                   1183:                         flock($idf,LOCK_SH);
                   1184:                         while (my $line = <$idf>) {
                   1185:                             chomp($line);
                   1186:                             if (&hostname($line) ne '') {
                   1187:                                 $try_server = $line;
                   1188:                                 last;
                   1189:                             }
                   1190:                         }
                   1191:                         close($idf);
                   1192:                         if (($try_server) &&
                   1193:                             (&has_user_session($try_server,$possudom,$possuname))) {
                   1194:                             my $lowest_load = 30000;
                   1195:                             ($otherserver,$lowest_load) =
                   1196:                                 &compare_server_load($try_server,undef,$lowest_load);
                   1197:                             if ($otherserver ne '' && $lowest_load < 100) {
                   1198:                                 $has_session = 1;
                   1199:                             } else {
                   1200:                                 undef($otherserver);
                   1201:                             }
                   1202:                         }
                   1203:                     }
                   1204:                 }
                   1205:                 if ($has_session) {
                   1206:                     if ($update_mtime) {
                   1207:                         my $atime = my $mtime = time;
                   1208:                         utime($atime,$mtime,"$balancedir/$cookie.id");
                   1209:                     }
                   1210:                 } else {
                   1211:                     unlink("$balancedir/$cookie.id");
                   1212:                 }
                   1213:             }
                   1214:         }
                   1215:     }
                   1216:     return ($otherserver,$cookie);
                   1217: }
                   1218: 
1.1431    raeburn  1219: sub updatebalcookie {
                   1220:     my ($cookie,$balancer,$lastentry)=@_;
                   1221:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1222:         my ($udom,$uname) = ($1,$2);
                   1223:         my $uprimary_id = &domain($udom,'primary');
                   1224:         my $uintdom = &internet_dom($uprimary_id);
                   1225:         my $intdom = &internet_dom($balancer);
                   1226:         my $serverhomedom = &host_domain($balancer);
                   1227:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1228:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
                   1229:         }
                   1230:     }
                   1231:     return;
                   1232: }
                   1233: 
1.1389    raeburn  1234: sub delbalcookie {
                   1235:     my ($cookie,$balancer) =@_;
                   1236:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1237:         my ($udom,$uname) = ($1,$2);
                   1238:         my $uprimary_id = &domain($udom,'primary');
                   1239:         my $uintdom = &internet_dom($uprimary_id);
                   1240:         my $intdom = &internet_dom($balancer);
                   1241:         my $serverhomedom = &host_domain($balancer);
                   1242:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1431    raeburn  1243:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1389    raeburn  1244:         }
                   1245:     }
                   1246: }
                   1247: 
1.914     albertel 1248: # -------------------------------- ask if server already has a session for user
                   1249: sub has_user_session {
                   1250:     my ($lonid,$udom,$uname) = @_;
                   1251:     my $result = &reply(join(':','userhassession',
                   1252: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                   1253:     return 1 if ($result eq 'ok');
                   1254: 
                   1255:     return 0;
                   1256: }
                   1257: 
1.1076    raeburn  1258: # --------- determine least loaded server in a user's domain which allows login
                   1259: 
                   1260: sub choose_server {
1.1259    raeburn  1261:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076    raeburn  1262:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn  1263:     my %servers = &get_servers($udom);
1.1076    raeburn  1264:     my $lowest_load = 30000;
1.1259    raeburn  1265:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
                   1266:     if ($skiploadbal) {
                   1267:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
                   1268:         unless (defined($cached)) {
                   1269:             my $cachetime = 60*60*24;
                   1270:             my %domconfig =
1.1494    raeburn  1271:                 &get_dom('configuration',['loadbalancing'],$udom);
1.1259    raeburn  1272:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1273:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
                   1274:                                            $cachetime);
                   1275:             }
                   1276:         }
                   1277:     }
1.1076    raeburn  1278:     foreach my $lonhost (keys(%servers)) {
1.1259    raeburn  1279:         if ($skiploadbal) {
                   1280:             if (ref($balancers) eq 'HASH') {
                   1281:                 next if (exists($balancers->{$lonhost}));
                   1282:             }
1.1389    raeburn  1283:         }
1.1115    raeburn  1284:         my $loginvia;
                   1285:         if ($checkloginvia) {
                   1286:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn  1287:             if ($loginvia) {
                   1288:                 my ($server,$path) = split(/:/,$loginvia);
                   1289:                 ($login_host, $lowest_load) =
1.1253    raeburn  1290:                     &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116    raeburn  1291:                 if ($login_host eq $server) {
                   1292:                     $portal_path = $path;
1.1151    raeburn  1293:                     $isredirect = 1;
1.1116    raeburn  1294:                 }
                   1295:             } else {
                   1296:                 ($login_host, $lowest_load) =
1.1253    raeburn  1297:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116    raeburn  1298:                 if ($login_host eq $lonhost) {
                   1299:                     $portal_path = '';
1.1151    raeburn  1300:                     $isredirect = ''; 
1.1116    raeburn  1301:                 }
                   1302:             }
                   1303:         } else {
1.1076    raeburn  1304:             ($login_host, $lowest_load) =
1.1253    raeburn  1305:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076    raeburn  1306:         }
                   1307:     }
                   1308:     if ($login_host ne '') {
1.1116    raeburn  1309:         $hostname = &hostname($login_host);
1.1076    raeburn  1310:     }
1.1331    raeburn  1311:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076    raeburn  1312: }
                   1313: 
1.1420    raeburn  1314: sub get_course_sessions {
                   1315:     my ($cnum,$cdom,$lastactivity) = @_;
                   1316:     my %servers = &internet_dom_servers($cdom);
                   1317:     my %returnhash;
                   1318:     foreach my $server (sort(keys(%servers))) {
                   1319:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
                   1320:         my @pairs=split(/\&/,$rep);
                   1321:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
                   1322:             foreach my $item (@pairs) {
                   1323:                 my ($key,$value)=split(/=/,$item,2);
                   1324:                 $key = &unescape($key);
                   1325:                 next if ($key =~ /^error: 2 /);
                   1326:                 if (exists($returnhash{$key})) {
                   1327:                     next if ($value < $returnhash{$key});
                   1328:                 }
                   1329:                 $returnhash{$key}=$value;
                   1330:             }
                   1331:         }
                   1332:     }
                   1333:     return %returnhash;
                   1334: }
                   1335: 
1.202     matthew  1336: # --------------------------------------------- Try to change a user's password
                   1337: 
                   1338: sub changepass {
1.799     raeburn  1339:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew  1340:     $currentpass = &escape($currentpass);
                   1341:     $newpass     = &escape($newpass);
1.1030    raeburn  1342:     my $lonhost = $perlvar{'lonHostID'};
                   1343:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew  1344: 		       $server);
                   1345:     if (! $answer) {
                   1346: 	&logthis("No reply on password change request to $server ".
                   1347: 		 "by $uname in domain $udom.");
                   1348:     } elsif ($answer =~ "^ok") {
                   1349:         &logthis("$uname in $udom successfully changed their password ".
                   1350: 		 "on $server.");
                   1351:     } elsif ($answer =~ "^pwchange_failure") {
                   1352: 	&logthis("$uname in $udom was unable to change their password ".
                   1353: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1354: 		 "or pwchange");
                   1355:     } elsif ($answer =~ "^non_authorized") {
                   1356:         &logthis("$uname in $udom did not get their password correct when ".
                   1357: 		 "attempting to change it on $server.");
                   1358:     } elsif ($answer =~ "^auth_mode_error") {
                   1359:         &logthis("$uname in $udom attempted to change their password despite ".
                   1360: 		 "not being locally or internally authenticated on $server.");
                   1361:     } elsif ($answer =~ "^unknown_user") {
                   1362:         &logthis("$uname in $udom attempted to change their password ".
                   1363: 		 "on $server but were unable to because $server is not ".
                   1364: 		 "their home server.");
                   1365:     } elsif ($answer =~ "^refused") {
                   1366: 	&logthis("$server refused to change $uname in $udom password because ".
                   1367: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1368:     } elsif ($answer =~ "invalid_client") {
                   1369:         &logthis("$server refused to change $uname in $udom password because ".
                   1370:                  "it was a reset by e-mail originating from an invalid server.");
1.1408    raeburn  1371:     } elsif ($answer =~ "^prioruse") {
                   1372:        &logthis("$server refused to change $uname in $udom password because ".
                   1373:                 "the password had been used before");
1.202     matthew  1374:     }
                   1375:     return $answer;
1.1       albertel 1376: }
                   1377: 
1.169     harris41 1378: # ----------------------- Try to determine user's current authentication scheme
                   1379: 
                   1380: sub queryauthenticate {
                   1381:     my ($uname,$udom)=@_;
1.456     albertel 1382:     my $uhome=&homeserver($uname,$udom);
1.1484    raeburn  1383:     if ((!$uhome) || ($uhome eq 'no_host')) {
1.456     albertel 1384: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1385: 	return 'no_host';
                   1386:     }
                   1387:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1388:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1389: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1390:     }
1.456     albertel 1391:     return $answer;
1.169     harris41 1392: }
                   1393: 
1.1       albertel 1394: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1395: 
1.1       albertel 1396: sub authenticate {
1.1073    raeburn  1397:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1398:     $upass=&escape($upass);
                   1399:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1400:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1401:     my $newhome;
1.836     www      1402:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1403: # Maybe the machine was offline and only re-appeared again recently?
                   1404:         &reconlonc();
                   1405: # One more
1.952     raeburn  1406: 	$uhome=&homeserver($uname,$udom,1);
                   1407:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1408:             if (defined(&domain($udom,'primary'))) {
                   1409:                 $newhome=&domain($udom,'primary');
                   1410:             }
                   1411:             if ($newhome ne '') {
                   1412:                 $uhome = $newhome;
                   1413:             }
                   1414:         }
1.836     www      1415: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1416: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1417: 	    return 'no_host';
                   1418:         }
1.1       albertel 1419:     }
1.1073    raeburn  1420:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1421:     if ($answer eq 'authorized') {
1.952     raeburn  1422:         if ($newhome) {
                   1423:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1424:             return 'no_account_on_host'; 
                   1425:         } else {
                   1426:             &logthis("User $uname at $udom authorized by $uhome");
                   1427:             return $uhome;
                   1428:         }
1.471     albertel 1429:     }
                   1430:     if ($answer eq 'non_authorized') {
                   1431: 	&logthis("User $uname at $udom rejected by $uhome");
1.1484    raeburn  1432: 	return 'no_host';
1.9       www      1433:     }
1.471     albertel 1434:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1435:     return 'no_host';
                   1436: }
                   1437: 
1.1491    raeburn  1438: sub can_switchserver {
                   1439:     my ($udom,$home) = @_;
                   1440:     my ($canswitch,@intdoms);
                   1441:     my $internet_names = &get_internet_names($home);
                   1442:     if (ref($internet_names) eq 'ARRAY') {
                   1443:         @intdoms = @{$internet_names};
                   1444:     }
                   1445:     my $uint_dom = &internet_dom(&domain($udom,'primary'));
                   1446:     if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1447:         $canswitch = 1;
                   1448:     } else {
                   1449:          my $serverhomeID = &get_server_homeID(&hostname($home));
                   1450:          my $serverhomedom = &host_domain($serverhomeID);
                   1451:          my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1452:          my %udomdefaults = &get_domain_defaults($udom);
                   1453:          my $remoterev = &get_server_loncaparev('',$home);
                   1454:          $canswitch = &can_host_session($udom,$home,$remoterev,
                   1455:                                         $udomdefaults{'remotesessions'},
                   1456:                                         $defdomdefaults{'hostedsessions'});
                   1457:     }
                   1458:     return $canswitch;
                   1459: }
                   1460: 
1.1073    raeburn  1461: sub can_host_session {
1.1074    raeburn  1462:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1463:     my $canhost = 1;
1.1494    raeburn  1464:     my $host_idn = &internet_dom($lonhost);
1.1073    raeburn  1465:     if (ref($remotesessions) eq 'HASH') {
                   1466:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1467:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1468:                 $canhost = 0;
                   1469:             } else {
                   1470:                 $canhost = 1;
                   1471:             }
                   1472:         }
                   1473:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1474:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1475:                 $canhost = 1;
                   1476:             } else {
                   1477:                 $canhost = 0;
                   1478:             }
                   1479:         }
                   1480:         if ($canhost) {
                   1481:             if ($remotesessions->{'version'} ne '') {
                   1482:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1483:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1484:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1485:                         my $major = $1;
                   1486:                         my $minor = $2;
                   1487:                         if (($major < $reqmajor ) ||
                   1488:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1489:                             $canhost = 0;
                   1490:                         }
                   1491:                     } else {
                   1492:                         $canhost = 0;
                   1493:                     }
                   1494:                 }
                   1495:             }
                   1496:         }
                   1497:     }
                   1498:     if ($canhost) {
                   1499:         if (ref($hostedsessions) eq 'HASH') {
1.1494    raeburn  1500:             my $uprimary_id = &domain($udom,'primary');
                   1501:             my $uint_dom = &internet_dom($uprimary_id);
1.1073    raeburn  1502:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1503:                 if (($uint_dom ne '') && 
                   1504:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1505:                     $canhost = 0;
                   1506:                 } else {
                   1507:                     $canhost = 1;
                   1508:                 }
                   1509:             }
                   1510:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1511:                 if (($uint_dom ne '') && 
                   1512:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1513:                     $canhost = 1;
                   1514:                 } else {
                   1515:                     $canhost = 0;
                   1516:                 }
                   1517:             }
                   1518:         }
                   1519:     }
                   1520:     return $canhost;
                   1521: }
                   1522: 
1.1083    raeburn  1523: sub spare_can_host {
                   1524:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1525:     my $canhost=1;
1.1279    raeburn  1526:     my $try_server_hostname = &hostname($try_server);
                   1527:     my $serverhomeID = &get_server_homeID($try_server_hostname);
                   1528:     my $serverhomedom = &host_domain($serverhomeID);
                   1529:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1530:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
                   1531:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
                   1532:             $canhost = 0;
                   1533:         }
                   1534:     }
1.1439    raeburn  1535:     if ($canhost) {
                   1536:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
                   1537:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
                   1538:                 unless (&shared_institution($udom,$try_server)) {
                   1539:                     $canhost = 0;
                   1540:                 }
                   1541:             }
                   1542:         }
                   1543:     }
1.1279    raeburn  1544:     if (($canhost) && ($uint_dom)) {
                   1545:         my @intdoms;
                   1546:         my $internet_names = &get_internet_names($try_server);
                   1547:         if (ref($internet_names) eq 'ARRAY') {
                   1548:             @intdoms = @{$internet_names};
                   1549:         }
                   1550:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1551:             my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1552:             $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1553:                                          $remotesessions,
                   1554:                                          $defdomdefaults{'hostedsessions'});
                   1555:         }
1.1083    raeburn  1556:     }
                   1557:     return $canhost;
                   1558: }
                   1559: 
1.1123    raeburn  1560: sub this_host_spares {
                   1561:     my ($dom) = @_;
1.1126    raeburn  1562:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1563:     my @hosts = &current_machine_ids();
                   1564:     foreach my $lonhost (@hosts) {
                   1565:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1566:             $dom_in_use = $dom;
                   1567:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1568:             last;
                   1569:         }
                   1570:     }
1.1126    raeburn  1571:     if ($dom_in_use ne '') {
                   1572:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1573:     }
                   1574:     if (ref($result) ne 'HASH') {
                   1575:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1576:         $dom_in_use = &host_domain($lonhost_in_use);
                   1577:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1578:         if (ref($result) ne 'HASH') {
                   1579:             $result = \%spareid;
                   1580:         }
                   1581:     }
                   1582:     return $result;
                   1583: }
                   1584: 
                   1585: sub spares_for_offload  {
                   1586:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1587:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1588:     if (defined($cached)) {
                   1589:         return $result;
                   1590:     } else {
1.1126    raeburn  1591:         my $cachetime = 60*60*24;
                   1592:         my %domconfig =
1.1494    raeburn  1593:             &get_dom('configuration',['usersessions'],$dom_in_use);
1.1126    raeburn  1594:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1595:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1596:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1597:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1598:                 }
                   1599:             }
                   1600:         }
                   1601:     }
1.1126    raeburn  1602:     return;
1.1123    raeburn  1603: }
                   1604: 
1.1129    raeburn  1605: sub get_lonbalancer_config {
                   1606:     my ($servers) = @_;
                   1607:     my ($currbalancer,$currtargets);
                   1608:     if (ref($servers) eq 'HASH') {
                   1609:         foreach my $server (keys(%{$servers})) {
                   1610:             my %what = (
                   1611:                          spareid => 1,
                   1612:                          perlvar => 1,
                   1613:                        );
                   1614:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1615:             if ($result eq 'ok') {
                   1616:                 if (ref($returnhash) eq 'HASH') {
                   1617:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1618:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1619:                             $currbalancer = $server;
                   1620:                             $currtargets = {};
                   1621:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1622:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1623:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1624:                                 }
                   1625:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1626:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1627:                                 }
                   1628:                             }
                   1629:                             last;
                   1630:                         }
                   1631:                     }
                   1632:                 }
                   1633:             }
                   1634:         }
                   1635:     }
                   1636:     return ($currbalancer,$currtargets);
                   1637: }
                   1638: 
                   1639: sub check_loadbalancing {
1.1331    raeburn  1640:     my ($uname,$udom,$caller) = @_;
1.1191    raeburn  1641:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1391    raeburn  1642:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
1.1129    raeburn  1643:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1644:     my @hosts = &current_machine_ids();
1.1494    raeburn  1645:     my $uprimary_id = &domain($udom,'primary');
                   1646:     my $uintdom = &internet_dom($uprimary_id);
                   1647:     my $intdom = &internet_dom($lonhost);
1.1129    raeburn  1648:     my $serverhomedom = &host_domain($lonhost);
1.1307    raeburn  1649:     my $domneedscache;
1.1129    raeburn  1650:     my $cachetime = 60*60*24;
                   1651: 
                   1652:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1653:         $dom_in_use = $udom;
                   1654:         $homeintdom = 1;
                   1655:     } else {
                   1656:         $dom_in_use = $serverhomedom;
                   1657:     }
                   1658:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1659:     unless (defined($cached)) {
                   1660:         my %domconfig =
1.1494    raeburn  1661:             &get_dom('configuration',['loadbalancing'],$dom_in_use);
1.1129    raeburn  1662:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1663:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307    raeburn  1664:         } else {
                   1665:             $domneedscache = $dom_in_use;
1.1129    raeburn  1666:         }
                   1667:     }
                   1668:     if (ref($result) eq 'HASH') {
1.1391    raeburn  1669:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1670:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1671:         if ($is_balancer) {
                   1672:             if (ref($currrules) eq 'HASH') {
                   1673:                 if ($homeintdom) {
                   1674:                     if ($uname ne '') {
                   1675:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1676:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1677:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1678:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1679:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1680:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1681:                             }
                   1682:                         }
                   1683:                         if ($rule_in_effect eq '') {
                   1684:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1685:                             if ($userenv{'inststatus'} ne '') {
                   1686:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1687:                                 my ($othertitle,$usertypes,$types) =
                   1688:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1689:                                 if (ref($types) eq 'ARRAY') {
                   1690:                                     foreach my $type (@{$types}) {
                   1691:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1692:                                             if (exists($currrules->{$type})) {
                   1693:                                                 $rule_in_effect = $currrules->{$type};
                   1694:                                             }
                   1695:                                         }
                   1696:                                     }
                   1697:                                 }
                   1698:                             } else {
                   1699:                                 if (exists($currrules->{'default'})) {
                   1700:                                     $rule_in_effect = $currrules->{'default'};
                   1701:                                 }
                   1702:                             }
                   1703:                         }
                   1704:                     } else {
                   1705:                         if (exists($currrules->{'default'})) {
                   1706:                             $rule_in_effect = $currrules->{'default'};
                   1707:                         }
                   1708:                     }
                   1709:                 } else {
                   1710:                     if ($currrules->{'_LC_external'} ne '') {
                   1711:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1712:                     }
                   1713:                 }
                   1714:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1715:                                                        $uname,$udom);
                   1716:             }
                   1717:         }
                   1718:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239    raeburn  1719:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129    raeburn  1720:         unless (defined($cached)) {
                   1721:             my %domconfig =
1.1494    raeburn  1722:                 &get_dom('configuration',['loadbalancing'],$serverhomedom);
1.1129    raeburn  1723:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307    raeburn  1724:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
                   1725:             } else {
                   1726:                 $domneedscache = $serverhomedom;
1.1129    raeburn  1727:             }
                   1728:         }
                   1729:         if (ref($result) eq 'HASH') {
1.1391    raeburn  1730:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1731:                 &check_balancer_result($result,@hosts);
                   1732:             if ($is_balancer) {
1.1129    raeburn  1733:                 if (ref($currrules) eq 'HASH') {
                   1734:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1735:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1736:                     }
                   1737:                 }
                   1738:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1739:                                                        $uname,$udom);
                   1740:             }
                   1741:         } else {
                   1742:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1743:                 $is_balancer = 1;
                   1744:                 $offloadto = &this_host_spares($dom_in_use);
                   1745:             }
1.1307    raeburn  1746:             unless (defined($cached)) {
                   1747:                 $domneedscache = $serverhomedom;
                   1748:             }
1.1129    raeburn  1749:         }
                   1750:     } else {
                   1751:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1752:             $is_balancer = 1;
                   1753:             $offloadto = &this_host_spares($dom_in_use);
                   1754:         }
1.1307    raeburn  1755:         unless (defined($cached)) {
                   1756:             $domneedscache = $serverhomedom;
                   1757:         }
                   1758:     }
                   1759:     if ($domneedscache) {
                   1760:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129    raeburn  1761:     }
1.1430    raeburn  1762:     if (($is_balancer) && ($caller ne 'switchserver')) {
1.1176    raeburn  1763:         my $lowest_load = 30000;
                   1764:         if (ref($offloadto) eq 'HASH') {
                   1765:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1766:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1767:                     ($otherserver,$lowest_load) =
                   1768:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1769:                 }
1.1129    raeburn  1770:             }
1.1176    raeburn  1771:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1772: 
1.1176    raeburn  1773:             if (!$found_server) {
                   1774:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1775:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1776:                         ($otherserver,$lowest_load) =
                   1777:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1778:                     }
                   1779:                 }
                   1780:             }
                   1781:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1782:             if (@{$offloadto} == 1) {
                   1783:                 $otherserver = $offloadto->[0];
                   1784:             } elsif (@{$offloadto} > 1) {
                   1785:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1786:                     ($otherserver,$lowest_load) =
                   1787:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1788:                 }
                   1789:             }
                   1790:         }
1.1331    raeburn  1791:         unless ($caller eq 'login') {
                   1792:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1793:                 $is_balancer = 0;
                   1794:                 if ($uname ne '' && $udom ne '') {
                   1795:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1389    raeburn  1796:                         &appenv({'user.loadbalexempt'     => $lonhost,
1.1331    raeburn  1797:                                  'user.loadbalcheck.time' => time});
                   1798:                     }
1.1176    raeburn  1799:                 }
1.1129    raeburn  1800:             }
                   1801:         }
1.1430    raeburn  1802:     }
                   1803:     if (($is_balancer) && (!$homeintdom)) {
                   1804:         undef($setcookie);
1.1129    raeburn  1805:     }
1.1391    raeburn  1806:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
1.1129    raeburn  1807: }
                   1808: 
1.1191    raeburn  1809: sub check_balancer_result {
                   1810:     my ($result,@hosts) = @_;
1.1391    raeburn  1811:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1812:     if (ref($result) eq 'HASH') {
                   1813:         if ($result->{'lonhost'} ne '') {
                   1814:             my $currbalancer = $result->{'lonhost'};
                   1815:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1816:                 $is_balancer = 1;
                   1817:                 $currtargets = $result->{'targets'};
                   1818:                 $currrules = $result->{'rules'};
                   1819:             }
1.1391    raeburn  1820:             $dom_balancers = $currbalancer;
1.1191    raeburn  1821:         } else {
1.1391    raeburn  1822:             if (keys(%{$result})) {
                   1823:                 foreach my $key (keys(%{$result})) {
                   1824:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1825:                         (ref($result->{$key}) eq 'HASH')) {
                   1826:                         $is_balancer = 1;
                   1827:                         $currrules = $result->{$key}{'rules'};
                   1828:                         $currtargets = $result->{$key}{'targets'};
                   1829:                         $setcookie = $result->{$key}{'cookie'};
                   1830:                         last;
                   1831:                     }
1.1191    raeburn  1832:                 }
1.1391    raeburn  1833:                 $dom_balancers = join(',',sort(keys(%{$result})));
1.1191    raeburn  1834:             }
                   1835:         }
                   1836:     }
1.1391    raeburn  1837:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1838: }
                   1839: 
1.1129    raeburn  1840: sub get_loadbalancer_targets {
                   1841:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1842:     my $offloadto;
1.1175    raeburn  1843:     if ($rule_in_effect eq 'none') {
                   1844:         return [$perlvar{'lonHostID'}];
                   1845:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1846:         $offloadto = $currtargets;
                   1847:     } else {
                   1848:         if ($rule_in_effect eq 'homeserver') {
                   1849:             my $homeserver = &homeserver($uname,$udom);
                   1850:             if ($homeserver ne 'no_host') {
                   1851:                 $offloadto = [$homeserver];
                   1852:             }
                   1853:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1854:             my %domconfig =
1.1494    raeburn  1855:                 &get_dom('configuration',['loadbalancing'],$udom);
1.1129    raeburn  1856:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1857:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1858:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1859:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1860:                     }
                   1861:                 }
                   1862:             } else {
1.1178    raeburn  1863:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1864:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1865:                 if (&hostname($remotebalancer) ne '') {
                   1866:                     $offloadto = [$remotebalancer];
                   1867:                 }
                   1868:             }
                   1869:         } elsif (&hostname($rule_in_effect) ne '') {
                   1870:             $offloadto = [$rule_in_effect];
                   1871:         }
                   1872:     }
                   1873:     return $offloadto;
                   1874: }
                   1875: 
1.1127    raeburn  1876: sub internet_dom_servers {
                   1877:     my ($dom) = @_;
                   1878:     my (%uniqservers,%servers);
                   1879:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1880:     my @machinedoms = &machine_domains($primaryserver);
                   1881:     foreach my $mdom (@machinedoms) {
                   1882:         my %currservers = %servers;
                   1883:         my %server = &get_servers($mdom);
                   1884:         %servers = (%currservers,%server);
                   1885:     }
                   1886:     my %by_hostname;
                   1887:     foreach my $id (keys(%servers)) {
                   1888:         push(@{$by_hostname{$servers{$id}}},$id);
                   1889:     }
                   1890:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1891:         if (@{$by_hostname{$hostname}} > 1) {
                   1892:             my $match = 0;
                   1893:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1894:                 if (&host_domain($id) eq $dom) {
                   1895:                     $uniqservers{$id} = $hostname;
                   1896:                     $match = 1;
                   1897:                 }
                   1898:             }
                   1899:             unless ($match) {
                   1900:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1901:             }
                   1902:         } else {
                   1903:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1904:         }
                   1905:     }
                   1906:     return %uniqservers;
                   1907: }
                   1908: 
1.1347    raeburn  1909: sub trusted_domains {
                   1910:     my ($cmdtype,$calldom) = @_;
1.1349    raeburn  1911:     my ($trusted,$untrusted);
1.1347    raeburn  1912:     if (&domain($calldom) eq '') {
1.1349    raeburn  1913:         return ($trusted,$untrusted);
1.1347    raeburn  1914:     }
1.1395    raeburn  1915:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
1.1349    raeburn  1916:         return ($trusted,$untrusted);
1.1347    raeburn  1917:     }
                   1918:     my $callprimary = &domain($calldom,'primary');
1.1494    raeburn  1919:     my $intcalldom = &internet_dom($callprimary);
1.1347    raeburn  1920:     if ($intcalldom eq '') {
1.1349    raeburn  1921:         return ($trusted,$untrusted);
1.1347    raeburn  1922:     }
                   1923: 
1.1494    raeburn  1924:     my ($trustconfig,$cached)=&is_cached_new('trust',$calldom);
1.1347    raeburn  1925:     unless (defined($cached)) {
1.1494    raeburn  1926:         my %domconfig = &get_dom('configuration',['trust'],$calldom);
                   1927:         &do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
1.1347    raeburn  1928:         $trustconfig = $domconfig{'trust'};
                   1929:     }
                   1930:     if (ref($trustconfig)) {
                   1931:         my (%possexc,%possinc,@allexc,@allinc); 
                   1932:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
                   1933:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
                   1934:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
                   1935:             }
                   1936:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
1.1395    raeburn  1937:                 $possinc{$intcalldom} = 1;
1.1347    raeburn  1938:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
                   1939:             }
                   1940:         }
                   1941:         if (keys(%possexc)) {
                   1942:             if (keys(%possinc)) {
                   1943:                 foreach my $key (sort(keys(%possexc))) {
                   1944:                     next if ($key eq $intcalldom);
                   1945:                     unless ($possinc{$key}) {
                   1946:                         push(@allexc,$key);
                   1947:                     }
                   1948:                 }
                   1949:             } else {
                   1950:                 @allexc = sort(keys(%possexc));
                   1951:             }
                   1952:         }
                   1953:         if (keys(%possinc)) {
                   1954:             $possinc{$intcalldom} = 1;
                   1955:             @allinc = sort(keys(%possinc));
                   1956:         }
                   1957:         if ((@allexc > 0) || (@allinc > 0)) {
                   1958:             my %doms_by_intdom;
                   1959:             my %allintdoms = &all_host_intdom();
                   1960:             my %alldoms = &all_host_domain();
                   1961:             foreach my $key (%allintdoms) {
                   1962:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
                   1963:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
                   1964:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
                   1965:                     }
                   1966:                 } else {
                   1967:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
                   1968:                 }
                   1969:             }
                   1970:             foreach my $exc (@allexc) {
                   1971:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
1.1395    raeburn  1972:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
1.1347    raeburn  1973:                 }
                   1974:             }
                   1975:             foreach my $inc (@allinc) {
                   1976:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
1.1395    raeburn  1977:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
1.1347    raeburn  1978:                 }
                   1979:             }
                   1980:         }
                   1981:     }
1.1349    raeburn  1982:     return ($trusted,$untrusted);
1.1347    raeburn  1983: }
                   1984: 
                   1985: sub will_trust {
                   1986:     my ($cmdtype,$domain,$possdom) = @_;
                   1987:     return 1 if ($domain eq $possdom);
                   1988:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
                   1989:     my $willtrust; 
                   1990:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
                   1991:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
                   1992:             $willtrust = 1;
                   1993:         }
                   1994:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
                   1995:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
                   1996:             $willtrust = 1;
                   1997:         }
                   1998:     } else {
                   1999:         $willtrust = 1;
                   2000:     }
                   2001:     return $willtrust;
                   2002: }
                   2003: 
1.1       albertel 2004: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      2005: 
1.599     albertel 2006: my %homecache;
1.1       albertel 2007: sub homeserver {
1.230     stredwic 2008:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 2009:     my $index="$uname:$udom";
1.426     albertel 2010: 
1.599     albertel 2011:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 2012: 
                   2013:     my %servers = &get_servers($udom,'library');
                   2014:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 2015:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 2016: 		 exists($badServerCache{$tryserver}));
1.841     albertel 2017: 
                   2018: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   2019: 	if ($answer eq 'found') {
                   2020: 	    delete($badServerCache{$tryserver}); 
                   2021: 	    return $homecache{$index}=$tryserver;
                   2022: 	} elsif ($answer eq 'no_host') {
                   2023: 	    $badServerCache{$tryserver}=1;
                   2024: 	}
1.1       albertel 2025:     }    
                   2026:     return 'no_host';
1.70      www      2027: }
                   2028: 
1.1300    raeburn  2029: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70      www      2030: 
                   2031: sub idget {
1.1300    raeburn  2032:     my ($udom,$idsref,$namespace)=@_;
1.70      www      2033:     my %returnhash=();
1.1300    raeburn  2034:     my @ids=(); 
                   2035:     if (ref($idsref) eq 'ARRAY') {
                   2036:         @ids = @{$idsref};
                   2037:     } else {
                   2038:         return %returnhash; 
                   2039:     }
                   2040:     if ($namespace eq '') {
                   2041:         $namespace = 'ids';
                   2042:     }
1.70      www      2043:     
1.841     albertel 2044:     my %servers = &get_servers($udom,'library');
                   2045:     foreach my $tryserver (keys(%servers)) {
1.1299    raeburn  2046: 	my $idlist=join('&', map { &escape($_); } @ids);
1.1300    raeburn  2047: 	if ($namespace eq 'ids') {
                   2048: 	    $idlist=~tr/A-Z/a-z/;
                   2049: 	}
                   2050: 	my $reply;
                   2051: 	if ($namespace eq 'ids') {
                   2052: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   2053: 	} else {
                   2054: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
                   2055: 	}
1.841     albertel 2056: 	my @answer=();
                   2057: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   2058: 	    @answer=split(/\&/,$reply);
                   2059: 	}                    ;
                   2060: 	my $i;
                   2061: 	for ($i=0;$i<=$#ids;$i++) {
                   2062: 	    if ($answer[$i]) {
1.1299    raeburn  2063: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300    raeburn  2064: 	    }
1.841     albertel 2065: 	}
1.1300    raeburn  2066:     }
1.70      www      2067:     return %returnhash;
                   2068: }
                   2069: 
                   2070: # ------------------------------------- Find the IDs behind a list of usernames
                   2071: 
                   2072: sub idrget {
                   2073:     my ($udom,@unames)=@_;
                   2074:     my %returnhash=();
1.800     albertel 2075:     foreach my $uname (@unames) {
                   2076:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 2077:     }
1.70      www      2078:     return %returnhash;
                   2079: }
                   2080: 
1.1300    raeburn  2081: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70      www      2082: 
                   2083: sub idput {
1.1300    raeburn  2084:     my ($udom,$idsref,$uhom,$namespace)=@_;
1.70      www      2085:     my %servers=();
1.1300    raeburn  2086:     my %ids=();
                   2087:     my %byid = ();
                   2088:     if (ref($idsref) eq 'HASH') {
                   2089:         %ids=%{$idsref};
                   2090:     }
                   2091:     if ($namespace eq '') {
                   2092:         $namespace = 'ids'; 
                   2093:     }
1.800     albertel 2094:     foreach my $uname (keys(%ids)) {
                   2095: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300    raeburn  2096:         if ($uhom eq '') {
                   2097:             $uhom=&homeserver($uname,$udom);
                   2098:         }
1.70      www      2099:         if ($uhom ne 'no_host') {
1.800     albertel 2100:             my $esc_unam=&escape($uname);
1.1300    raeburn  2101:             if ($namespace eq 'ids') {
                   2102:                 my $id=&escape($ids{$uname});
                   2103:                 $id=~tr/A-Z/a-z/;
                   2104:                 my $esc_unam=&escape($uname);
                   2105:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70      www      2106:             } else {
1.1300    raeburn  2107:                 my @currids = split(/,/,$ids{$uname});
                   2108:                 foreach my $id (@currids) {
                   2109:                     $byid{$uhom}{$id} .= $uname.',';
                   2110:                 }
                   2111:             }
                   2112:         }
                   2113:     }
                   2114:     if ($namespace eq 'clickers') {
                   2115:         foreach my $server (keys(%byid)) {
                   2116:             if (ref($byid{$server}) eq 'HASH') {
                   2117:                 foreach my $id (keys(%{$byid{$server}})) {
                   2118:                     $byid{$server} =~ s/,$//;
                   2119:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
                   2120:                 }
1.70      www      2121:             }
                   2122:         }
1.191     harris41 2123:     }
1.800     albertel 2124:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2125:         $servers{$server} =~ s/\&$//;
                   2126:         if ($namespace eq 'ids') {     
                   2127:             &critical('idput:'.$udom.':'.$servers{$server},$server);
                   2128:         } else {
                   2129:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
                   2130:         }
1.191     harris41 2131:     }
1.344     www      2132: }
                   2133: 
1.1300    raeburn  2134: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231    raeburn  2135: 
                   2136: sub iddel {
1.1300    raeburn  2137:     my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231    raeburn  2138:     my %result=();
1.1300    raeburn  2139:     my %ids=();
                   2140:     my %byid = ();
                   2141:     if (ref($idshashref) eq 'HASH') {
                   2142:         %ids=%{$idshashref};
                   2143:     } else {
1.1231    raeburn  2144:         return %result;
                   2145:     }
1.1300    raeburn  2146:     if ($namespace eq '') {
                   2147:         $namespace = 'ids';
                   2148:     }
1.1231    raeburn  2149:     my %servers=();
1.1300    raeburn  2150:     while (my ($id,$unamestr) = each(%ids)) {
                   2151:         if ($namespace eq 'ids') {
                   2152:             my $uhom = $uhome;
                   2153:             if ($uhom eq '') { 
                   2154:                 $uhom=&homeserver($unamestr,$udom);
                   2155:             }
                   2156:             if ($uhom ne 'no_host') {
                   2157:                 $servers{$uhom}.='&'.&escape($id);
                   2158:             }
                   2159:          } else {
                   2160:             my @curritems = split(/,/,$ids{$id});
                   2161:             foreach my $uname (@curritems) {
                   2162:                 my $uhom = $uhome;
                   2163:                 if ($uhom eq '') {
                   2164:                     $uhom=&homeserver($uname,$udom);
                   2165:                 }
                   2166:                 if ($uhom ne 'no_host') { 
                   2167:                     $byid{$uhom}{$id} .= $uname.',';
                   2168:                 }
                   2169:             }
1.1231    raeburn  2170:         }
1.1300    raeburn  2171:     }
                   2172:     if ($namespace eq 'clickers') {
                   2173:         foreach my $server (keys(%byid)) {
                   2174:             if (ref($byid{$server}) eq 'HASH') {
                   2175:                 foreach my $id (keys(%{$byid{$server}})) {
                   2176:                     $byid{$server}{$id} =~ s/,$//;
                   2177:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
                   2178:                 }
1.1231    raeburn  2179:             }
                   2180:         }
                   2181:     }
                   2182:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2183:         $servers{$server} =~ s/\&$//;
                   2184:         if ($namespace eq 'ids') {
                   2185:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
                   2186:         } elsif ($namespace eq 'clickers') {
                   2187:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
                   2188:         }
1.1231    raeburn  2189:     }
                   2190:     return %result;
                   2191: }
                   2192: 
1.1300    raeburn  2193: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
                   2194: 
                   2195: sub updateclickers {
                   2196:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
                   2197:     my %clickers;
                   2198:     if (ref($idshashref) eq 'HASH') {
                   2199:         %clickers=%{$idshashref};
                   2200:     } else {
                   2201:         return;
                   2202:     }
                   2203:     my $items='';
                   2204:     foreach my $item (keys(%clickers)) {
                   2205:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
                   2206:     }
                   2207:     $items=~s/\&$//;
                   2208:     my $request = "updateclickers:$udom:$action:$items";
                   2209:     if ($critical) {
                   2210:         return &critical($request,$uhome);
                   2211:     } else {
                   2212:         return &reply($request,$uhome);
                   2213:     }
                   2214: }
                   2215: 
1.1023    raeburn  2216: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  2217: sub dump_dom {
1.1165    droeschl 2218:     my ($namespace, $udom, $regexp) = @_;
                   2219: 
                   2220:     $udom ||= $env{'user.domain'};
                   2221: 
                   2222:     return () unless $udom;
                   2223: 
                   2224:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  2225: }
                   2226: 
1.1023    raeburn  2227: # ------------------------------------------ get items from domain db files   
1.806     raeburn  2228: 
                   2229: sub get_dom {
1.1462    raeburn  2230:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
1.1267    raeburn  2231:     return if ($udom eq 'public');
1.806     raeburn  2232:     my $items='';
                   2233:     foreach my $item (@$storearr) {
                   2234:         $items.=&escape($item).'&';
                   2235:     }
                   2236:     $items=~s/\&$//;
1.860     raeburn  2237:     if (!$udom) {
                   2238:         $udom=$env{'user.domain'};
1.1267    raeburn  2239:         return if ($udom eq 'public');
1.860     raeburn  2240:         if (defined(&domain($udom,'primary'))) {
                   2241:             $uhome=&domain($udom,'primary');
                   2242:         } else {
1.874     albertel 2243:             undef($uhome);
1.860     raeburn  2244:         }
                   2245:     } else {
                   2246:         if (!$uhome) {
                   2247:             if (defined(&domain($udom,'primary'))) {
                   2248:                 $uhome=&domain($udom,'primary');
                   2249:             }
                   2250:         }
                   2251:     }
                   2252:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1344    raeburn  2253:         my $rep;
1.1442    raeburn  2254:         if (grep { $_ eq $uhome } &current_machine_ids()) {
                   2255:             # domain information is hosted on this machine
1.1462    raeburn  2256:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
1.1344    raeburn  2257:         } else {
1.1462    raeburn  2258:             if ($encrypt) {
1.1442    raeburn  2259:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
                   2260:             } else {
                   2261:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
                   2262:             }
1.1344    raeburn  2263:         }
1.866     raeburn  2264:         my %returnhash;
1.875     albertel 2265:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  2266:             return %returnhash;
                   2267:         }
1.806     raeburn  2268:         my @pairs=split(/\&/,$rep);
                   2269:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   2270:             return @pairs;
                   2271:         }
                   2272:         my $i=0;
                   2273:         foreach my $item (@$storearr) {
                   2274:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   2275:             $i++;
                   2276:         }
                   2277:         return %returnhash;
                   2278:     } else {
1.880     banghart 2279:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  2280:     }
                   2281: }
                   2282: 
                   2283: # -------------------------------------------- put items in domain db files 
                   2284: 
                   2285: sub put_dom {
1.1462    raeburn  2286:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
1.860     raeburn  2287:     if (!$udom) {
                   2288:         $udom=$env{'user.domain'};
                   2289:         if (defined(&domain($udom,'primary'))) {
                   2290:             $uhome=&domain($udom,'primary');
                   2291:         } else {
1.874     albertel 2292:             undef($uhome);
1.860     raeburn  2293:         }
                   2294:     } else {
                   2295:         if (!$uhome) {
                   2296:             if (defined(&domain($udom,'primary'))) {
                   2297:                 $uhome=&domain($udom,'primary');
                   2298:             }
                   2299:         }
                   2300:     } 
                   2301:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  2302:         my $items='';
                   2303:         foreach my $item (keys(%$storehash)) {
                   2304:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   2305:         }
                   2306:         $items=~s/\&$//;
1.1462    raeburn  2307:         if ($encrypt) {
1.1344    raeburn  2308:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
                   2309:         } else {
                   2310:             return &reply("putdom:$udom:$namespace:$items",$uhome);
                   2311:         }
1.806     raeburn  2312:     } else {
1.860     raeburn  2313:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  2314:     }
                   2315: }
                   2316: 
1.1023    raeburn  2317: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  2318: sub newput_dom {
1.1023    raeburn  2319:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  2320:     my $result;
                   2321:     if (!$udom) {
                   2322:         $udom=$env{'user.domain'};
                   2323:     }
1.1023    raeburn  2324:     if ($udom) {
                   2325:         my $uname = &get_domainconfiguser($udom);
                   2326:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  2327:     }
                   2328:     return $result;
                   2329: }
                   2330: 
1.1023    raeburn  2331: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  2332: sub del_dom {
1.1023    raeburn  2333:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  2334:     if (ref($storearr) eq 'ARRAY') {
                   2335:         if (!$udom) {
                   2336:             $udom=$env{'user.domain'};
                   2337:         }
1.1023    raeburn  2338:         if ($udom) {
                   2339:             my $uname = &get_domainconfiguser($udom); 
                   2340:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  2341:         }
                   2342:     }
                   2343: }
                   2344: 
1.1481    raeburn  2345: sub store_dom {
1.1482    raeburn  2346:     my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
1.1481    raeburn  2347:     $$storehash{'ip'}=&get_requestor_ip();
                   2348:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2349:     my $namevalue='';
                   2350:     foreach my $key (keys(%{$storehash})) {
                   2351:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   2352:     }
                   2353:     $namevalue=~s/\&$//;
                   2354:     if (grep { $_ eq $home } current_machine_ids()) {
                   2355:         return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
                   2356:     } else {
                   2357:         if ($namespace eq 'private') {
                   2358:             return 'refused';
1.1482    raeburn  2359:         } elsif ($encrypt) {
                   2360:             return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
1.1481    raeburn  2361:         } else {
1.1482    raeburn  2362:             return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
1.1481    raeburn  2363:         }
                   2364:     }
                   2365: }
                   2366: 
1.1482    raeburn  2367: sub restore_dom {
                   2368:     my ($id,$namespace,$dom,$home,$encrypt) = @_;
                   2369:     my $answer;
                   2370:     if (grep { $_ eq $home } current_machine_ids()) {
                   2371:         $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
                   2372:     } elsif ($namespace ne 'private') {
                   2373:         if ($encrypt) {
                   2374:             $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
                   2375:         } else {
                   2376:             $answer=&reply("restoredom:$dom:$namespace:$id",$home);
                   2377:         }
                   2378:     }
                   2379:     my %returnhash=();
                   2380:     unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') || 
                   2381:             ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
                   2382:         foreach my $line (split(/\&/,$answer)) {
                   2383:             my ($name,$value)=split(/\=/,$line);
                   2384:             $returnhash{&unescape($name)}=&thaw_unescape($value);
                   2385:         }
                   2386:         my $version;
                   2387:         for ($version=1;$version<=$returnhash{'version'};$version++) {
                   2388:             foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   2389:                 $returnhash{$item}=$returnhash{$version.':'.$item};
                   2390:             }
                   2391:         }
                   2392:     }
                   2393:     return %returnhash;
                   2394: }
                   2395: 
1.1023    raeburn  2396: # ----------------------------------construct domainconfig user for a domain 
                   2397: sub get_domainconfiguser {
                   2398:     my ($udom) = @_;
                   2399:     return $udom.'-domainconfig';
                   2400: }
                   2401: 
1.837     raeburn  2402: sub retrieve_inst_usertypes {
                   2403:     my ($udom) = @_;
                   2404:     my (%returnhash,@order);
1.1494    raeburn  2405:     my %domdefs = &get_domain_defaults($udom);
1.989     raeburn  2406:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   2407:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256    raeburn  2408:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989     raeburn  2409:     } else {
                   2410:         if (defined(&domain($udom,'primary'))) {
                   2411:             my $uhome=&domain($udom,'primary');
                   2412:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   2413:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256    raeburn  2414:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989     raeburn  2415:                 return (\%returnhash,\@order);
                   2416:             }
                   2417:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   2418:             my @pairs=split(/\&/,$hashitems);
                   2419:             foreach my $item (@pairs) {
                   2420:                 my ($key,$value)=split(/=/,$item,2);
                   2421:                 $key = &unescape($key);
                   2422:                 next if ($key =~ /^error: 2 /);
                   2423:                 $returnhash{$key}=&thaw_unescape($value);
                   2424:             }
                   2425:             my @esc_order = split(/\&/,$orderitems);
                   2426:             foreach my $item (@esc_order) {
                   2427:                 push(@order,&unescape($item));
                   2428:             }
                   2429:         } else {
1.1256    raeburn  2430:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837     raeburn  2431:         }
1.1256    raeburn  2432:         return (\%returnhash,\@order);
1.837     raeburn  2433:     }
                   2434: }
                   2435: 
1.868     raeburn  2436: sub is_domainimage {
                   2437:     my ($url) = @_;
1.1468    raeburn  2438:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
1.868     raeburn  2439:         if (&domain($1) ne '') {
                   2440:             return '1';
                   2441:         }
                   2442:     }
                   2443:     return;
                   2444: }
                   2445: 
1.899     raeburn  2446: sub inst_directory_query {
                   2447:     my ($srch) = @_;
                   2448:     my $udom = $srch->{'srchdomain'};
                   2449:     my %results;
                   2450:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  2451:     my $outcome;
1.899     raeburn  2452:     if ($homeserver ne '') {
1.1357    raeburn  2453:         unless ($homeserver eq $perlvar{'lonHostID'}) {
                   2454:             if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2455:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1357    raeburn  2456:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2457:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2458:                     (($major == 2) && ($minor < 12))) {
                   2459:                     return;
                   2460:                 }
                   2461:             }
                   2462:         }
1.904     albertel 2463: 	my $queryid=&reply("querysend:instdirsearch:".
                   2464: 			   &escape($srch->{'srchby'}).':'.
                   2465: 			   &escape($srch->{'srchterm'}).':'.
                   2466: 			   &escape($srch->{'srchtype'}),$homeserver);
                   2467: 	my $host=&hostname($homeserver);
                   2468: 	if ($queryid !~/^\Q$host\E\_/) {
1.1343    raeburn  2469: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904     albertel 2470: 	    return;
                   2471: 	}
                   2472: 	my $response = &get_query_reply($queryid);
                   2473: 	my $maxtries = 5;
                   2474: 	my $tries = 1;
                   2475: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2476: 	    $response = &get_query_reply($queryid);
                   2477: 	    $tries ++;
                   2478: 	}
                   2479: 
                   2480:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  2481:             if ($response eq 'unavailable') {
                   2482:                 $outcome = $response;
                   2483:             } else {
                   2484:                 $outcome = 'ok';
                   2485:                 my @matches = split(/\n/,$response);
                   2486:                 foreach my $match (@matches) {
                   2487:                     my ($key,$value) = split(/=/,$match);
                   2488:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   2489:                 }
1.899     raeburn  2490:             }
                   2491:         }
                   2492:     }
1.909     raeburn  2493:     return ($outcome,%results);
1.899     raeburn  2494: }
                   2495: 
                   2496: sub usersearch {
                   2497:     my ($srch) = @_;
                   2498:     my $dom = $srch->{'srchdomain'};
                   2499:     my %results;
                   2500:     my %libserv = &all_library();
                   2501:     my $query = 'usersearch';
                   2502:     foreach my $tryserver (keys(%libserv)) {
                   2503:         if (&host_domain($tryserver) eq $dom) {
1.1357    raeburn  2504:             unless ($tryserver eq $perlvar{'lonHostID'}) {
                   2505:                 if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2506:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1357    raeburn  2507:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2508:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2509:                              (($major == 2) && ($minor < 12)));
                   2510:                 }
                   2511:             }
1.899     raeburn  2512:             my $host=&hostname($tryserver);
                   2513:             my $queryid=
1.911     raeburn  2514:                 &reply("querysend:".&escape($query).':'.
                   2515:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  2516:                        &escape($srch->{'srchtype'}).':'.
                   2517:                        &escape($srch->{'srchterm'}),$tryserver);
                   2518:             if ($queryid !~/^\Q$host\E\_/) {
                   2519:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  2520:                 next;
1.899     raeburn  2521:             }
                   2522:             my $reply = &get_query_reply($queryid);
                   2523:             my $maxtries = 1;
                   2524:             my $tries = 1;
                   2525:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   2526:                 $reply = &get_query_reply($queryid);
                   2527:                 $tries ++;
                   2528:             }
                   2529:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   2530:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   2531:             } else {
1.911     raeburn  2532:                 my @matches;
                   2533:                 if ($reply =~ /\n/) {
                   2534:                     @matches = split(/\n/,$reply);
                   2535:                 } else {
                   2536:                     @matches = split(/\&/,$reply);
                   2537:                 }
1.899     raeburn  2538:                 foreach my $match (@matches) {
                   2539:                     my ($uname,$udom,%userhash);
1.911     raeburn  2540:                     foreach my $entry (split(/:/,$match)) {
                   2541:                         my ($key,$value) =
                   2542:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  2543:                         $userhash{$key} = $value;
                   2544:                         if ($key eq 'username') {
                   2545:                             $uname = $value;
                   2546:                         } elsif ($key eq 'domain') {
                   2547:                             $udom = $value;
1.911     raeburn  2548:                         }
1.899     raeburn  2549:                     }
                   2550:                     $results{$uname.':'.$udom} = \%userhash;
                   2551:                 }
                   2552:             }
                   2553:         }
                   2554:     }
                   2555:     return %results;
                   2556: }
                   2557: 
1.912     raeburn  2558: sub get_instuser {
                   2559:     my ($udom,$uname,$id) = @_;
                   2560:     my $homeserver = &domain($udom,'primary');
                   2561:     my ($outcome,%results);
                   2562:     if ($homeserver ne '') {
                   2563:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   2564:                            &escape($id).':'.&escape($udom),$homeserver);
                   2565:         my $host=&hostname($homeserver);
                   2566:         if ($queryid !~/^\Q$host\E\_/) {
                   2567:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   2568:             return;
                   2569:         }
                   2570:         my $response = &get_query_reply($queryid);
                   2571:         my $maxtries = 5;
                   2572:         my $tries = 1;
                   2573:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2574:             $response = &get_query_reply($queryid);
                   2575:             $tries ++;
                   2576:         }
                   2577:         if (!&error($response) && $response ne 'refused') {
                   2578:             if ($response eq 'unavailable') {
                   2579:                 $outcome = $response;
                   2580:             } else {
                   2581:                 $outcome = 'ok';
                   2582:                 my @matches = split(/\n/,$response);
                   2583:                 foreach my $match (@matches) {
                   2584:                     my ($key,$value) = split(/=/,$match);
                   2585:                     $results{&unescape($key)} = &thaw_unescape($value);
                   2586:                 }
                   2587:             }
                   2588:         }
                   2589:     }
                   2590:     my %userinfo;
                   2591:     if (ref($results{$uname}) eq 'HASH') {
                   2592:         %userinfo = %{$results{$uname}};
                   2593:     } 
                   2594:     return ($outcome,%userinfo);
                   2595: }
                   2596: 
1.1290    raeburn  2597: sub get_multiple_instusers {
                   2598:     my ($udom,$users,$caller) = @_;
                   2599:     my ($outcome,$results);
                   2600:     if (ref($users) eq 'HASH') {
                   2601:         my $count = keys(%{$users}); 
                   2602:         my $requested = &freeze_escape($users);
                   2603:         my $homeserver = &domain($udom,'primary');
                   2604:         if ($homeserver ne '') {
                   2605:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
                   2606:             my $host=&hostname($homeserver);
                   2607:             if ($queryid !~/^\Q$host\E\_/) {
                   2608:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
                   2609:                          ' for host: '.$homeserver.'in domain '.$udom);
                   2610:                 return ($outcome,$results);
                   2611:             }
                   2612:             my $response = &get_query_reply($queryid);
                   2613:             my $maxtries = 5;
                   2614:             if ($count > 100) {
                   2615:                 $maxtries = 1+int($count/20);
                   2616:             }
                   2617:             my $tries = 1;
                   2618:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
                   2619:                 $response = &get_query_reply($queryid);
                   2620:                 $tries ++;
                   2621:             }
                   2622:             if ($response eq '') {
                   2623:                 $results = {};
                   2624:                 foreach my $key (keys(%{$users})) {
                   2625:                     my ($uname,$id);
                   2626:                     if ($caller eq 'id') {
                   2627:                         $id = $key;
                   2628:                     } else {
                   2629:                         $uname = $key;
                   2630:                     }
                   2631:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291    raeburn  2632:                     $outcome = $resp;
1.1290    raeburn  2633:                     if ($resp eq 'ok') {
                   2634:                         %{$results} = (%{$results}, %info);
                   2635:                     } else {
                   2636:                         last;
                   2637:                     }
                   2638:                 }
                   2639:             } elsif(!&error($response) && ($response ne 'refused')) {
                   2640:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
                   2641:                     $outcome = $response;
                   2642:                 } else {
1.1291    raeburn  2643:                     ($outcome,my $userdata) = split(/=/,$response,2);
1.1290    raeburn  2644:                     if ($outcome eq 'ok') {
                   2645:                         $results = &thaw_unescape($userdata); 
                   2646:                     }
                   2647:                 }
                   2648:             }
                   2649:         }
                   2650:     }
                   2651:     return ($outcome,$results);
                   2652: }
                   2653: 
1.912     raeburn  2654: sub inst_rulecheck {
1.923     raeburn  2655:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  2656:     my %returnhash;
                   2657:     if ($udom ne '') {
                   2658:         if (ref($rules) eq 'ARRAY') {
                   2659:             @{$rules} = map {&escape($_);} (@{$rules});
                   2660:             my $rulestr = join(':',@{$rules});
                   2661:             my $homeserver=&domain($udom,'primary');
                   2662:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2663:                 my $response;
                   2664:                 if ($item eq 'username') {                
                   2665:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   2666:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  2667:                                               $homeserver));
1.923     raeburn  2668:                 } elsif ($item eq 'id') {
                   2669:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   2670:                                               ':'.&escape($id).':'.$rulestr,
                   2671:                                               $homeserver));
1.945     raeburn  2672:                 } elsif ($item eq 'selfcreate') {
                   2673:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  2674:                                                &escape($udom).':'.&escape($uname).
                   2675:                                               ':'.$rulestr,$homeserver));
1.1484    raeburn  2676:                 } elsif ($item eq 'unamemap') {
                   2677:                     $response=&unescape(&reply('instunamemapcheck:'.
                   2678:                                                &escape($udom).':'.&escape($uname).
                   2679:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  2680:                 }
1.912     raeburn  2681:                 if ($response ne 'refused') {
                   2682:                     my @pairs=split(/\&/,$response);
                   2683:                     foreach my $item (@pairs) {
                   2684:                         my ($key,$value)=split(/=/,$item,2);
                   2685:                         $key = &unescape($key);
                   2686:                         next if ($key =~ /^error: 2 /);
                   2687:                         $returnhash{$key}=&thaw_unescape($value);
                   2688:                     }
                   2689:                 }
                   2690:             }
                   2691:         }
                   2692:     }
                   2693:     return %returnhash;
                   2694: }
                   2695: 
                   2696: sub inst_userrules {
1.923     raeburn  2697:     my ($udom,$check) = @_;
1.912     raeburn  2698:     my (%ruleshash,@ruleorder);
                   2699:     if ($udom ne '') {
                   2700:         my $homeserver=&domain($udom,'primary');
                   2701:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2702:             my $response;
                   2703:             if ($check eq 'id') {
                   2704:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  2705:                                  $homeserver);
1.943     raeburn  2706:             } elsif ($check eq 'email') {
                   2707:                 $response=&reply('instemailrules:'.&escape($udom),
                   2708:                                  $homeserver);
1.1484    raeburn  2709:             } elsif ($check eq 'unamemap') {
                   2710:                 $response=&reply('unamemaprules:'.&escape($udom),
                   2711:                                  $homeserver); 
1.923     raeburn  2712:             } else {
                   2713:                 $response=&reply('instuserrules:'.&escape($udom),
                   2714:                                  $homeserver);
                   2715:             }
1.912     raeburn  2716:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  2717:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  2718:                 ($response ne 'no_such_host')) {
                   2719:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   2720:                 my @pairs=split(/\&/,$hashitems);
                   2721:                 foreach my $item (@pairs) {
                   2722:                     my ($key,$value)=split(/=/,$item,2);
                   2723:                     $key = &unescape($key);
                   2724:                     next if ($key =~ /^error: 2 /);
                   2725:                     $ruleshash{$key}=&thaw_unescape($value);
                   2726:                 }
                   2727:                 my @esc_order = split(/\&/,$orderitems);
                   2728:                 foreach my $item (@esc_order) {
                   2729:                     push(@ruleorder,&unescape($item));
                   2730:                 }
                   2731:             }
                   2732:         }
                   2733:     }
                   2734:     return (\%ruleshash,\@ruleorder);
                   2735: }
                   2736: 
1.976     raeburn  2737: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  2738: 
                   2739: sub get_domain_defaults {
1.1240    raeburn  2740:     my ($domain,$ignore_cache) = @_;
1.1242    raeburn  2741:     return if (($domain eq '') || ($domain eq 'public'));
1.943     raeburn  2742:     my $cachetime = 60*60*24;
1.1240    raeburn  2743:     unless ($ignore_cache) {
                   2744:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   2745:         if (defined($cached)) {
                   2746:             if (ref($result) eq 'HASH') {
                   2747:                 return %{$result};
                   2748:             }
1.943     raeburn  2749:         }
                   2750:     }
                   2751:     my %domdefaults;
                   2752:     my %domconfig =
1.1494    raeburn  2753:          &get_dom('configuration',['defaults','quotas',
1.1047    raeburn  2754:                                   'requestcourses','inststatus',
1.1183    raeburn  2755:                                   'coursedefaults','usersessions',
1.1517    raeburn  2756:                                   'requestauthor','authordefaults',
                   2757:                                   'selfenrollment','coursecategories',
                   2758:                                   'ssl','autoenroll','trust',
                   2759:                                   'helpsettings','wafproxy',
1.1520    raeburn  2760:                                   'ltisec','toolsec','privacy'],$domain);
1.1305    raeburn  2761:     my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943     raeburn  2762:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   2763:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   2764:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   2765:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  2766:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  2767:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  2768:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1492    raeburn  2769:         $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
                   2770:         $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
1.1340    raeburn  2771:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
                   2772:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
                   2773:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.1484    raeburn  2774:         $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
1.943     raeburn  2775:     } else {
                   2776:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   2777:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   2778:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   2779:     }
1.976     raeburn  2780:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   2781:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   2782:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   2783:         } else {
                   2784:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229    raeburn  2785:         }
1.1521    raeburn  2786:         my @usertools = ('aboutme','blog','webdav','portfolio','portaccess');
1.976     raeburn  2787:         foreach my $item (@usertools) {
                   2788:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   2789:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   2790:             }
                   2791:         }
1.1229    raeburn  2792:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   2793:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
                   2794:         }
1.976     raeburn  2795:     }
1.985     raeburn  2796:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305    raeburn  2797:         foreach my $item ('official','unofficial','community','textbook','placement') {
1.985     raeburn  2798:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2799:         }
                   2800:     }
1.1183    raeburn  2801:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2802:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2803:     }
1.1517    raeburn  2804:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
                   2805:         foreach my $item ('nocodemirror','copyright','sourceavail','domcoordacc','editors') {
                   2806:             if ($item eq 'editors') {
                   2807:                 if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
                   2808:                     $domdefaults{$item} = join(',',@{$domconfig{'authordefaults'}{'editors'}});
                   2809:                 }
                   2810:             } else {
                   2811:                 $domdefaults{$item} = $domconfig{'authordefaults'}{$item};
                   2812:             }
                   2813:         }
                   2814:     }
1.989     raeburn  2815:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256    raeburn  2816:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989     raeburn  2817:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2818:         }
                   2819:     }
1.1047    raeburn  2820:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230    raeburn  2821:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277    raeburn  2822:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
1.1476    raeburn  2823:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
1.1277    raeburn  2824:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
                   2825:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   2826:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
                   2827:         }
1.1254    raeburn  2828:         foreach my $type (@coursetypes) {
                   2829:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2830:                 unless ($type eq 'community') {
                   2831:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
                   2832:                 }
                   2833:             }
                   2834:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   2835:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   2836:             }
1.1514    raeburn  2837:             if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
                   2838:                 $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
                   2839:             }
1.1277    raeburn  2840:             if ($domdefaults{'postsubmit'} eq 'on') {
                   2841:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   2842:                     $domdefaults{$type.'postsubtimeout'} = 
                   2843:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
                   2844:                 }
                   2845:             }
1.1508    raeburn  2846:             if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
                   2847:                 $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
                   2848:             } else {
                   2849:                 $domdefaults{$type.'domexttool'} = 1;
                   2850:             }
                   2851:             if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
                   2852:                 $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
                   2853:             } else {
                   2854:                 $domdefaults{$type.'exttool'} = 0;
                   2855:             }
1.1520    raeburn  2856:             if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
                   2857:                 $domdefaults{$type.'crsauthor'} = $domconfig{'coursedefaults'}{'crsauthor'}{$type};
                   2858:             } else {
                   2859:                 $domdefaults{$type.'crsauthor'} = 1;
                   2860:             }
1.1230    raeburn  2861:         }
1.1286    raeburn  2862:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   2863:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   2864:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
                   2865:                 if (@clonecodes) {
                   2866:                     $domdefaults{'canclone'} = join('+',@clonecodes);
                   2867:                 }
                   2868:             }
                   2869:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
                   2870:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
                   2871:         }
1.1356    raeburn  2872:         if ($domconfig{'coursedefaults'}{'texengine'}) {
                   2873:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
1.1480    raeburn  2874:         }
                   2875:         if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
                   2876:             $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
                   2877:         }
1.1047    raeburn  2878:     }
1.1073    raeburn  2879:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2880:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2881:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2882:         }
                   2883:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2884:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2885:         }
1.1279    raeburn  2886:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   2887:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
                   2888:         }
1.1440    raeburn  2889:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1439    raeburn  2890:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
                   2891:         }
1.1073    raeburn  2892:     }
1.1254    raeburn  2893:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                   2894:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   2895:             my @settings = ('types','registered','enroll_dates','access_dates','section',
                   2896:                             'approval','limit');
                   2897:             foreach my $type (@coursetypes) {
                   2898:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   2899:                     my @mgrdc = ();
                   2900:                     foreach my $item (@settings) {
                   2901:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
                   2902:                             push(@mgrdc,$item);
                   2903:                         }
                   2904:                     }
                   2905:                     if (@mgrdc) {
                   2906:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   2907:                     }
                   2908:                 }
                   2909:             }
                   2910:         }
                   2911:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   2912:             foreach my $type (@coursetypes) {
                   2913:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   2914:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
                   2915:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
                   2916:                     }
                   2917:                 }
                   2918:             }
                   2919:         }
                   2920:     }
1.1258    raeburn  2921:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2922:         $domdefaults{'catauth'} = 'std';
                   2923:         $domdefaults{'catunauth'} = 'std';
1.1413    raeburn  2924:         if ($domconfig{'coursecategories'}{'auth'}) {
1.1258    raeburn  2925:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   2926:         }
                   2927:         if ($domconfig{'coursecategories'}{'unauth'}) {
                   2928:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   2929:         }
                   2930:     }
1.1315    raeburn  2931:     if (ref($domconfig{'ssl'}) eq 'HASH') {
                   2932:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
                   2933:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
                   2934:         }
1.1338    raeburn  2935:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
                   2936:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
                   2937:         }
                   2938:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
                   2939:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315    raeburn  2940:         }
                   2941:     }
1.1320    raeburn  2942:     if (ref($domconfig{'trust'}) eq 'HASH') {
                   2943:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
                   2944:         foreach my $prefix (@prefixes) {
                   2945:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
                   2946:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
                   2947:             }
                   2948:         }
                   2949:     }
1.1314    raeburn  2950:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   2951:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
1.1477    raeburn  2952:         $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
1.1314    raeburn  2953:     }
1.1332    raeburn  2954:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   2955:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
                   2956:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   2957:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
                   2958:         }
                   2959:     }
1.1434    raeburn  2960:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
1.1449    raeburn  2961:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
1.1434    raeburn  2962:             if ($domconfig{'wafproxy'}{$item}) {
                   2963:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
                   2964:             }
                   2965:         }
1.1482    raeburn  2966:     }
                   2967:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
                   2968:         if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
                   2969:             $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
                   2970:             $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
                   2971:             $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
                   2972:         }
                   2973:         if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
                   2974:             if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
1.1504    raeburn  2975:                 $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
                   2976:             }
                   2977:         }
1.1523  ! raeburn  2978:         if (ref($domconfig{'ltisec'}{'suggested'}) eq 'HASH') {
        !          2979:             my %suggestions = %{$domconfig{'ltisec'}{'suggested'}};
        !          2980:             foreach my $item (keys(%{$domconfig{'ltisec'}{'suggested'}})) {
        !          2981:                 unless (ref($domconfig{'ltisec'}{'suggested'}{$item}) eq 'HASH') {
        !          2982:                     delete($suggestions{$item});
        !          2983:                 }
        !          2984:             }
        !          2985:             if (keys(%suggestions)) {
        !          2986:                 $domdefaults{'linkprotsuggested'} = \%suggestions;
        !          2987:             }
        !          2988:         }
1.1504    raeburn  2989:     }
                   2990:     if (ref($domconfig{'toolsec'}) eq 'HASH') {
                   2991:         if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
                   2992:             $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
                   2993:             $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
                   2994:         }
                   2995:         if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
                   2996:             if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
                   2997:                 $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
1.1482    raeburn  2998:             }
                   2999:         }
                   3000:     }
1.1512    raeburn  3001:     if (ref($domconfig{'privacy'}) eq 'HASH') {
                   3002:         if (ref($domconfig{'privacy'}{'approval'}) eq 'HASH') {
                   3003:             foreach my $domtype ('instdom','extdom') {
                   3004:                 if (ref($domconfig{'privacy'}{'approval'}{$domtype}) eq 'HASH') {
                   3005:                     foreach my $roletype ('domain','author','course','community') {
                   3006:                         if ($domconfig{'privacy'}{'approval'}{$domtype}{$roletype} eq 'user') {
                   3007:                             $domdefaults{'userapprovals'} = 1;
                   3008:                             last;
                   3009:                         }
                   3010:                     }
                   3011:                 }
                   3012:                 last if ($domdefaults{'userapprovals'});
                   3013:             }
                   3014:         }
                   3015:     }
1.1219    raeburn  3016:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  3017:     return %domdefaults;
                   3018: }
                   3019: 
1.1412    raeburn  3020: sub get_dom_cats {
                   3021:     my ($dom) = @_;
                   3022:     return unless (&domain($dom));
                   3023:     my ($cats,$cached)=&is_cached_new('cats',$dom);
                   3024:     unless (defined($cached)) {
                   3025:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
                   3026:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   3027:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
                   3028:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
                   3029:             } else {
                   3030:                 $cats = {};
                   3031:             }
                   3032:         } else {
                   3033:             $cats = {};
                   3034:         }
1.1494    raeburn  3035:         &do_cache_new('cats',$dom,$cats,3600);
1.1412    raeburn  3036:     }
1.1413    raeburn  3037:     return $cats;
                   3038: }
                   3039: 
                   3040: sub get_dom_instcats {
                   3041:     my ($dom) = @_;
                   3042:     return unless (&domain($dom));
                   3043:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
                   3044:     unless (defined($cached)) {
                   3045:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
                   3046:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
                   3047:         if ($totcodes > 0) {
                   3048:             my $caller = 'global';
                   3049:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
                   3050:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
                   3051:                 $instcats = {
1.1503    raeburn  3052:                                 totcodes => $totcodes,
1.1413    raeburn  3053:                                 codes => \%codes,
                   3054:                                 codetitles => \@codetitles,
                   3055:                                 cat_titles => \%cat_titles,
                   3056:                                 cat_order => \%cat_order,
                   3057:                             };
                   3058:                 &do_cache_new('instcats',$dom,$instcats,3600);
                   3059:             }
                   3060:         }
                   3061:     }
                   3062:     return $instcats;
                   3063: }
                   3064: 
                   3065: sub retrieve_instcodes {
                   3066:     my ($coursecodes,$dom) = @_;
                   3067:     my $totcodes;
                   3068:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
                   3069:     foreach my $course (keys(%courses)) {
                   3070:         if (ref($courses{$course}) eq 'HASH') {
                   3071:             if ($courses{$course}{'inst_code'} ne '') {
                   3072:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
                   3073:                 $totcodes ++;
                   3074:             }
                   3075:         }
                   3076:     }
                   3077:     return $totcodes;
1.1412    raeburn  3078: }
                   3079: 
1.1311    raeburn  3080: sub course_portal_url {
1.1451    raeburn  3081:     my ($cnum,$cdom,$r) = @_;
1.1311    raeburn  3082:     my $chome = &homeserver($cnum,$cdom);
                   3083:     my $hostname = &hostname($chome);
                   3084:     my $protocol = $protocol{$chome};
                   3085:     $protocol = 'http' if ($protocol ne 'https');
                   3086:     my %domdefaults = &get_domain_defaults($cdom);
                   3087:     my $firsturl;
                   3088:     if ($domdefaults{'portal_def'}) {
                   3089:         $firsturl = $domdefaults{'portal_def'};
                   3090:     } else {
1.1494    raeburn  3091:         my $alias = &use_proxy_alias($r,$chome);
1.1451    raeburn  3092:         $hostname = $alias if ($alias ne '');
1.1311    raeburn  3093:         $firsturl = $protocol.'://'.$hostname;
                   3094:     }
                   3095:     return $firsturl;
                   3096: }
                   3097: 
1.1492    raeburn  3098: sub url_prefix {
                   3099:     my ($r,$dom,$home,$context) = @_;
                   3100:     my $prefix;
                   3101:     my %domdefs = &get_domain_defaults($dom);
                   3102:     if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
                   3103:         if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
                   3104:             $prefix = $1;
                   3105:         }
                   3106:     }
                   3107:     if ($prefix eq '') {
                   3108:         my $hostname = &hostname($home);
                   3109:         my $protocol = $protocol{$home};
                   3110:         $protocol = 'http' if ($protocol{$home} ne 'https');
                   3111:         my $alias = &use_proxy_alias($r,$home);
                   3112:         $hostname = $alias if ($alias ne '');
                   3113:         $prefix = $protocol.'://'.$hostname;
                   3114:     }
                   3115:     return $prefix;
                   3116: }
                   3117: 
1.1407    raeburn  3118: # --------------------------------------------- Get domain config for passwords
                   3119: 
                   3120: sub get_passwdconf {
                   3121:     my ($dom) = @_;
                   3122:     my (%passwdconf,$gotconf,$lookup);
                   3123:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
                   3124:     if (defined($cached)) {
                   3125:         if (ref($result) eq 'HASH') {
                   3126:             %passwdconf = %{$result};
                   3127:             $gotconf = 1;
                   3128:         }
                   3129:     }
                   3130:     unless ($gotconf) {
                   3131:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
                   3132:         if (ref($domconfig{'passwords'}) eq 'HASH') {
                   3133:             %passwdconf = %{$domconfig{'passwords'}};
                   3134:         }
                   3135:         my $cachetime = 24*60*60;
                   3136:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
                   3137:     }
                   3138:     return %passwdconf;
                   3139: }
                   3140: 
1.344     www      3141: # --------------------------------------------------- Assign a key to a student
                   3142: 
                   3143: sub assign_access_key {
1.364     www      3144: #
                   3145: # a valid key looks like uname:udom#comments
                   3146: # comments are being appended
                   3147: #
1.498     www      3148:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   3149:     $kdom=
1.620     albertel 3150:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      3151:     $knum=
1.620     albertel 3152:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      3153:     $cdom=
1.620     albertel 3154:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3155:     $cnum=
1.620     albertel 3156:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   3157:     $udom=$env{'user.name'} unless (defined($udom));
                   3158:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      3159:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      3160:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 3161:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      3162:                                                   # assigned to this person
                   3163:                                                   # - this should not happen,
1.345     www      3164:                                                   # unless something went wrong
                   3165:                                                   # the first time around
                   3166: # ready to assign
1.364     www      3167:         $logentry=$1.'; '.$logentry;
1.496     www      3168:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      3169:                                                  $kdom,$knum) eq 'ok') {
1.345     www      3170: # key now belongs to user
1.346     www      3171: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      3172:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  3173:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      3174:                 return 'ok';
                   3175:             } else {
                   3176:                 return 
                   3177:   'error: Count not permanently assign key, will need to be re-entered later.';
                   3178: 	    }
                   3179:         } else {
                   3180:             return 'error: Could not assign key, try again later.';
                   3181:         }
1.364     www      3182:     } elsif (!$existing{$ckey}) {
1.345     www      3183: # the key does not exist
                   3184: 	return 'error: The key does not exist';
                   3185:     } else {
                   3186: # the key is somebody else's
                   3187: 	return 'error: The key is already in use';
                   3188:     }
1.344     www      3189: }
                   3190: 
1.364     www      3191: # ------------------------------------------ put an additional comment on a key
                   3192: 
                   3193: sub comment_access_key {
                   3194: #
                   3195: # a valid key looks like uname:udom#comments
                   3196: # comments are being appended
                   3197: #
                   3198:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   3199:     $cdom=
1.620     albertel 3200:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      3201:     $cnum=
1.620     albertel 3202:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      3203:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   3204:     if ($existing{$ckey}) {
                   3205:         $existing{$ckey}.='; '.$logentry;
                   3206: # ready to assign
1.367     www      3207:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      3208:                                                  $cdom,$cnum) eq 'ok') {
                   3209: 	    return 'ok';
                   3210:         } else {
                   3211: 	    return 'error: Count not store comment.';
                   3212:         }
                   3213:     } else {
                   3214: # the key does not exist
                   3215: 	return 'error: The key does not exist';
                   3216:     }
                   3217: }
                   3218: 
1.344     www      3219: # ------------------------------------------------------ Generate a set of keys
                   3220: 
                   3221: sub generate_access_keys {
1.364     www      3222:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      3223:     $cdom=
1.620     albertel 3224:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3225:     $cnum=
1.620     albertel 3226:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      3227:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      3228:     unless (($cdom) && ($cnum)) { return 0; }
                   3229:     if ($number>10000) { return 0; }
                   3230:     sleep(2); # make sure don't get same seed twice
                   3231:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   3232:     my $total=0;
                   3233:     for (my $i=1;$i<=$number;$i++) {
                   3234:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   3235:                   sprintf("%lx",int(100000*rand)).'-'.
                   3236:                   sprintf("%lx",int(100000*rand));
                   3237:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   3238:        $newkey=~s/0/h/g; # and also 0 and O
                   3239:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   3240:        if ($existing{$newkey}) {
                   3241:            $i--;
                   3242:        } else {
1.364     www      3243: 	  if (&put('accesskeys',
                   3244:               { $newkey => '# generated '.localtime().
1.620     albertel 3245:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      3246:                            '; '.$logentry },
                   3247: 		   $cdom,$cnum) eq 'ok') {
1.344     www      3248:               $total++;
                   3249: 	  }
                   3250:        }
                   3251:     }
1.620     albertel 3252:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      3253:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   3254:     return $total;
                   3255: }
                   3256: 
                   3257: # ------------------------------------------------------- Validate an accesskey
                   3258: 
                   3259: sub validate_access_key {
                   3260:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   3261:     $cdom=
1.620     albertel 3262:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3263:     $cnum=
1.620     albertel 3264:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   3265:     $udom=$env{'user.domain'} unless (defined($udom));
                   3266:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      3267:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 3268:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      3269: }
                   3270: 
                   3271: # ------------------------------------- Find the section of student in a course
1.652     albertel 3272: sub devalidate_getsection_cache {
                   3273:     my ($udom,$unam,$courseid)=@_;
                   3274:     my $hashid="$udom:$unam:$courseid";
                   3275:     &devalidate_cache_new('getsection',$hashid);
                   3276: }
1.298     matthew  3277: 
1.815     albertel 3278: sub courseid_to_courseurl {
                   3279:     my ($courseid) = @_;
                   3280:     #already url style courseid
                   3281:     return $courseid if ($courseid =~ m{^/});
                   3282: 
                   3283:     if (exists($env{'course.'.$courseid.'.num'})) {
                   3284: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   3285: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   3286: 	return "/$cdom/$cnum";
                   3287:     }
                   3288: 
1.1494    raeburn  3289:     my %courseinfo=&coursedescription($courseid);
1.815     albertel 3290:     if (exists($courseinfo{'num'})) {
                   3291: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   3292:     }
                   3293: 
                   3294:     return undef;
                   3295: }
                   3296: 
1.298     matthew  3297: sub getsection {
                   3298:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 3299:     my $cachetime=1800;
1.551     albertel 3300: 
                   3301:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 3302:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 3303:     if (defined($cached)) { return $result; }
                   3304: 
1.298     matthew  3305:     my %Pending; 
                   3306:     my %Expired;
                   3307:     #
                   3308:     # Each role can either have not started yet (pending), be active, 
                   3309:     #    or have expired.
                   3310:     #
                   3311:     # If there is an active role, we are done.
                   3312:     #
                   3313:     # If there is more than one role which has not started yet, 
                   3314:     #     choose the one which will start sooner
                   3315:     # If there is one role which has not started yet, return it.
                   3316:     #
                   3317:     # If there is more than one expired role, choose the one which ended last.
                   3318:     # If there is a role which has expired, return it.
                   3319:     #
1.815     albertel 3320:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  3321:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  3322:     foreach my $key (keys(%roleshash)) {
1.479     albertel 3323:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  3324:         my $section=$1;
                   3325:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  3326:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  3327:         my $now=time;
1.548     albertel 3328:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  3329:             $Expired{$end}=$section;
                   3330:             next;
                   3331:         }
1.548     albertel 3332:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  3333:             $Pending{$start}=$section;
                   3334:             next;
                   3335:         }
1.599     albertel 3336:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  3337:     }
                   3338:     #
                   3339:     # Presumedly there will be few matching roles from the above
                   3340:     # loop and the sorting time will be negligible.
                   3341:     if (scalar(keys(%Pending))) {
                   3342:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 3343:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  3344:     } 
                   3345:     if (scalar(keys(%Expired))) {
                   3346:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   3347:         my $time = pop(@sorted);
1.599     albertel 3348:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  3349:     }
1.599     albertel 3350:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  3351: }
1.70      www      3352: 
1.599     albertel 3353: sub save_cache {
                   3354:     &purge_remembered();
1.722     albertel 3355:     #&Apache::loncommon::validate_page();
1.620     albertel 3356:     undef(%env);
1.780     albertel 3357:     undef($env_loaded);
1.599     albertel 3358: }
1.452     albertel 3359: 
1.599     albertel 3360: my $to_remember=-1;
                   3361: my %remembered;
                   3362: my %accessed;
                   3363: my $kicks=0;
                   3364: my $hits=0;
1.849     albertel 3365: sub make_key {
                   3366:     my ($name,$id) = @_;
1.872     albertel 3367:     if (length($id) > 65 
                   3368: 	&& length(&escape($id)) > 200) {
                   3369: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   3370:     }
1.849     albertel 3371:     return &escape($name.':'.$id);
                   3372: }
                   3373: 
1.599     albertel 3374: sub devalidate_cache_new {
                   3375:     my ($name,$id,$debug) = @_;
                   3376:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319    damieng  3377:     my $remembered_id=$name.':'.$id;
1.849     albertel 3378:     $id=&make_key($name,$id);
1.599     albertel 3379:     $memcache->delete($id);
1.1319    damieng  3380:     delete($remembered{$remembered_id});
                   3381:     delete($accessed{$remembered_id});
1.599     albertel 3382: }
                   3383: 
                   3384: sub is_cached_new {
                   3385:     my ($name,$id,$debug) = @_;
1.1319    damieng  3386:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
                   3387:     if (exists($remembered{$remembered_id})) {
                   3388: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
                   3389: 	$accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3390: 	$hits++;
1.1319    damieng  3391: 	return ($remembered{$remembered_id},1);
1.599     albertel 3392:     }
1.1319    damieng  3393:     $id=&make_key($name,$id);
1.599     albertel 3394:     my $value = $memcache->get($id);
                   3395:     if (!(defined($value))) {
                   3396: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 3397: 	return (undef,undef);
1.416     albertel 3398:     }
1.599     albertel 3399:     if ($value eq '__undef__') {
                   3400: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   3401: 	$value=undef;
                   3402:     }
1.1319    damieng  3403:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3404:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   3405:     return ($value,1);
                   3406: }
                   3407: 
                   3408: sub do_cache_new {
                   3409:     my ($name,$id,$value,$time,$debug) = @_;
1.1319    damieng  3410:     my $remembered_id=$name.':'.$id;
1.849     albertel 3411:     $id=&make_key($name,$id);
1.599     albertel 3412:     my $setvalue=$value;
                   3413:     if (!defined($setvalue)) {
                   3414: 	$setvalue='__undef__';
                   3415:     }
1.623     albertel 3416:     if (!defined($time) ) {
                   3417: 	$time=600;
                   3418:     }
1.599     albertel 3419:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 3420:     my $result = $memcache->set($id,$setvalue,$time);
                   3421:     if (! $result) {
1.872     albertel 3422: 	&logthis("caching of id -> $id  failed");
1.910     albertel 3423: 	$memcache->disconnect_all();
1.872     albertel 3424:     }
1.600     albertel 3425:     # need to make a copy of $value
1.1319    damieng  3426:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3427:     return $value;
                   3428: }
                   3429: 
                   3430: sub make_room {
1.1319    damieng  3431:     my ($remembered_id,$value,$debug)=@_;
1.919     albertel 3432: 
1.1319    damieng  3433:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919     albertel 3434:                                     : $value;
1.599     albertel 3435:     if ($to_remember<0) { return; }
1.1319    damieng  3436:     $accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3437:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   3438:     my $to_kick;
                   3439:     my $max_time=0;
                   3440:     foreach my $other (keys(%accessed)) {
                   3441: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   3442: 	    $to_kick=$other;
                   3443: 	    $max_time=&tv_interval($accessed{$other});
                   3444: 	}
                   3445:     }
                   3446:     delete($remembered{$to_kick});
                   3447:     delete($accessed{$to_kick});
                   3448:     $kicks++;
                   3449:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 3450:     return;
                   3451: }
                   3452: 
1.599     albertel 3453: sub purge_remembered {
1.604     albertel 3454:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   3455:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 3456:     undef(%remembered);
                   3457:     undef(%accessed);
1.428     albertel 3458: }
1.70      www      3459: # ------------------------------------- Read an entry from a user's environment
                   3460: 
                   3461: sub userenvironment {
                   3462:     my ($udom,$unam,@what)=@_;
1.976     raeburn  3463:     my $items;
                   3464:     foreach my $item (@what) {
                   3465:         $items.=&escape($item).'&';
                   3466:     }
                   3467:     $items=~s/\&$//;
1.70      www      3468:     my %returnhash=();
1.1009    raeburn  3469:     my $uhome = &homeserver($unam,$udom);
                   3470:     unless ($uhome eq 'no_host') {
                   3471:         my @answer=split(/\&/, 
                   3472:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  3473:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   3474:             return %returnhash;
                   3475:         }
1.1009    raeburn  3476:         my $i;
                   3477:         for ($i=0;$i<=$#what;$i++) {
                   3478: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   3479:         }
1.70      www      3480:     }
                   3481:     return %returnhash;
1.1       albertel 3482: }
                   3483: 
1.617     albertel 3484: # ---------------------------------------------------------- Get a studentphoto
                   3485: sub studentphoto {
                   3486:     my ($udom,$unam,$ext) = @_;
1.1494    raeburn  3487:     my $home=&homeserver($unam,$udom);
1.706     raeburn  3488:     if (defined($env{'request.course.id'})) {
1.708     raeburn  3489:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  3490:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   3491:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   3492:             } else {
                   3493:                 my ($result,$perm_reqd)=
1.1494    raeburn  3494: 		    &auto_photo_permission($unam,$udom);
1.706     raeburn  3495:                 if ($result eq 'ok') {
                   3496:                     if (!($perm_reqd eq 'yes')) {
                   3497:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   3498:                     }
                   3499:                 }
                   3500:             }
                   3501:         }
                   3502:     } else {
                   3503:         my ($result,$perm_reqd) = 
1.1494    raeburn  3504: 	    &auto_photo_permission($unam,$udom);
1.706     raeburn  3505:         if ($result eq 'ok') {
                   3506:             if (!($perm_reqd eq 'yes')) {
                   3507:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   3508:             }
                   3509:         }
                   3510:     }
                   3511:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   3512: }
                   3513: 
                   3514: sub retrievestudentphoto {
                   3515:     my ($udom,$unam,$ext,$type) = @_;
1.1494    raeburn  3516:     my $home=&homeserver($unam,$udom);
                   3517:     my $ret=&reply("studentphoto:$udom:$unam:$ext:$type",$home);
1.706     raeburn  3518:     if ($ret eq 'ok') {
                   3519:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   3520:         if ($type eq 'thumbnail') {
                   3521:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   3522:         }
1.1494    raeburn  3523:         my $tokenurl=&tokenwrapper($url);
1.706     raeburn  3524:         return $tokenurl;
                   3525:     } else {
                   3526:         if ($type eq 'thumbnail') {
                   3527:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   3528:         } else { 
                   3529:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   3530:         }
1.617     albertel 3531:     }
                   3532: }
                   3533: 
1.263     www      3534: # -------------------------------------------------------------------- New chat
                   3535: 
                   3536: sub chatsend {
1.724     raeburn  3537:     my ($newentry,$anon,$group)=@_;
1.620     albertel 3538:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3539:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3540:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      3541:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 3542: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  3543: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      3544: }
                   3545: 
                   3546: # ------------------------------------------ Find current version of a resource
                   3547: 
                   3548: sub getversion {
                   3549:     my $fname=&clutter(shift);
1.1189    raeburn  3550:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      3551:     return &currentversion(&filelocation('',$fname));
                   3552: }
                   3553: 
                   3554: sub currentversion {
                   3555:     my $fname=shift;
                   3556:     my $author=$fname;
                   3557:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3558:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      3559:     my $home=&homeserver($uname,$udom);
1.292     www      3560:     if ($home eq 'no_host') { 
                   3561:         return -1; 
                   3562:     }
1.1112    www      3563:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      3564:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3565: 	return -1;
                   3566:     }
1.1112    www      3567:     return $answer;
1.263     www      3568: }
                   3569: 
1.1111    www      3570: #
                   3571: # Return special version number of resource if set by override, empty otherwise
                   3572: #
                   3573: sub usedversion {
                   3574:     my $fname=shift;
                   3575:     unless ($fname) { $fname=$env{'request.uri'}; }
                   3576:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   3577:     if ($urlversion) { return $urlversion; }
                   3578:     return '';
                   3579: }
                   3580: 
1.1       albertel 3581: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      3582: 
1.1       albertel 3583: sub subscribe {
                   3584:     my $fname=shift;
1.761     raeburn  3585:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 3586:     $fname=~s/[\n\r]//g;
1.1       albertel 3587:     my $author=$fname;
                   3588:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3589:     my ($udom,$uname)=split(/\//,$author);
                   3590:     my $home=homeserver($uname,$udom);
1.335     albertel 3591:     if ($home eq 'no_host') {
                   3592:         return 'not_found';
1.1       albertel 3593:     }
                   3594:     my $answer=reply("sub:$fname",$home);
1.64      www      3595:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3596: 	$answer.=' by '.$home;
                   3597:     }
1.1       albertel 3598:     return $answer;
                   3599: }
                   3600:     
1.8       www      3601: # -------------------------------------------------------------- Replicate file
                   3602: 
                   3603: sub repcopy {
                   3604:     my $filename=shift;
1.23      www      3605:     $filename=~s/\/+/\//g;
1.1142    raeburn  3606:     my $londocroot = $perlvar{'lonDocRoot'};
                   3607:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  3608:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  3609:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   3610: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 3611: 	return &repcopy_userfile($filename);
                   3612:     }
1.532     albertel 3613:     $filename=~s/[\n\r]//g;
1.8       www      3614:     my $transname="$filename.in.transfer";
1.828     www      3615: # FIXME: this should flock
1.607     raeburn  3616:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      3617:     my $remoteurl=subscribe($filename);
1.64      www      3618:     if ($remoteurl =~ /^con_lost by/) {
                   3619: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3620:            return 'unavailable';
1.8       www      3621:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 3622: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  3623: 	   return 'not_found';
1.64      www      3624:     } elsif ($remoteurl =~ /^rejected by/) {
                   3625: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3626:            return 'forbidden';
1.20      www      3627:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  3628:            return 'ok';
1.8       www      3629:     } else {
1.290     www      3630:         my $author=$filename;
                   3631:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3632:         my ($udom,$uname)=split(/\//,$author);
                   3633:         my $home=homeserver($uname,$udom);
                   3634:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      3635:            my @parts=split(/\//,$filename);
                   3636:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  3637:            if ($path ne "$londocroot/res") {
1.8       www      3638:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  3639: 	       return 'bad_request';
1.8       www      3640:            }
                   3641:            my $count;
                   3642:            for ($count=5;$count<$#parts;$count++) {
                   3643:                $path.="/$parts[$count]";
                   3644:                if ((-e $path)!=1) {
                   3645: 		   mkdir($path,0777);
                   3646:                }
                   3647:            }
                   3648:            my $request=new HTTP::Request('GET',"$remoteurl");
1.1345    raeburn  3649:            my $response;
                   3650:            if ($remoteurl =~ m{/raw/}) {
                   3651:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
                   3652:            } else {
                   3653:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
                   3654:            }
1.8       www      3655:            if ($response->is_error()) {
                   3656: 	       unlink($transname);
                   3657:                my $message=$response->status_line;
1.672     albertel 3658:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      3659:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  3660:                return 'unavailable';
1.8       www      3661:            } else {
1.16      www      3662: 	       if ($remoteurl!~/\.meta$/) {
                   3663:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1.1345    raeburn  3664:                   my $mresponse;
                   3665:                   if ($remoteurl =~ m{/raw/}) {
                   3666:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
                   3667:                   } else {
                   3668:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
                   3669:                   }
1.16      www      3670:                   if ($mresponse->is_error()) {
                   3671: 		      unlink($filename.'.meta');
                   3672:                       &logthis(
1.672     albertel 3673:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      3674:                   }
                   3675: 	       }
1.8       www      3676:                rename($transname,$filename);
1.607     raeburn  3677:                return 'ok';
1.8       www      3678:            }
1.290     www      3679:        }
1.8       www      3680:     }
1.330     www      3681: }
                   3682: 
1.1422    raeburn  3683: # ------------------------------------------------- Unsubscribe from a resource
                   3684: 
                   3685: sub unsubscribe {
                   3686:     my ($fname) = @_;
                   3687:     my $answer;
                   3688:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
                   3689:     $fname=~s/[\n\r]//g;
                   3690:     my $author=$fname;
                   3691:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3692:     my ($udom,$uname)=split(/\//,$author);
                   3693:     my $home=homeserver($uname,$udom);
                   3694:     if ($home eq 'no_host') {
                   3695:         $answer = 'no_host';
                   3696:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
                   3697:         $answer = 'home';
                   3698:     } else {
                   3699:         my $defdom = $perlvar{'lonDefDomain'};
                   3700:         if (&will_trust('content',$defdom,$udom)) {
                   3701:             $answer = reply("unsub:$fname",$home);
                   3702:         } else {
                   3703:             $answer = 'untrusted';
                   3704:         }
                   3705:     }
                   3706:     return $answer;
                   3707: }
                   3708: 
1.330     www      3709: # ------------------------------------------------ Get server side include body
                   3710: sub ssi_body {
1.381     albertel 3711:     my ($filelink,%form)=@_;
1.606     matthew  3712:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   3713:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   3714:     }
1.953     www      3715:     my $output='';
                   3716:     my $response;
1.980     raeburn  3717:     if ($filelink=~/^https?\:/) {
1.954     raeburn  3718:        ($output,$response)=&externalssi($filelink);
1.953     www      3719:     } else {
1.1004    droeschl 3720:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   3721:        $filelink .= 'inhibitmenu=yes';
1.953     www      3722:        ($output,$response)=&ssi($filelink,%form);
                   3723:     }
1.778     albertel 3724:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 3725:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 3726:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      3727:     if (wantarray) {
                   3728:         return ($output, $response);
                   3729:     } else {
                   3730:         return $output;
                   3731:     }
1.8       www      3732: }
                   3733: 
1.15      www      3734: # --------------------------------------------------------- Server Side Include
                   3735: 
1.782     albertel 3736: sub absolute_url {
1.1447    raeburn  3737:     my ($host_name,$unalias,$keep_proto) = @_;
1.782     albertel 3738:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   3739:     if ($host_name eq '') {
                   3740: 	$host_name = $ENV{'SERVER_NAME'};
                   3741:     }
1.1447    raeburn  3742:     if ($unalias) {
                   3743:         my $alias = &get_proxy_alias();
                   3744:         if ($alias eq $host_name) {
                   3745:             my $lonhost = $perlvar{'lonHostID'};
                   3746:             my $hostname = &hostname($lonhost);
                   3747:             my $lcproto; 
                   3748:             if (($keep_proto) || ($hostname eq '')) {
                   3749:                 $lcproto = $protocol;
                   3750:             } else {
                   3751:                 $lcproto = $protocol{$lonhost};
                   3752:                 $lcproto = 'http' if ($lcproto ne 'https');
                   3753:                 $lcproto .= '://';
                   3754:             }
                   3755:             unless ($hostname eq '') {
                   3756:                 return $lcproto.$hostname;
                   3757:             }
                   3758:         }
                   3759:     }
1.782     albertel 3760:     return $protocol.$host_name;
                   3761: }
                   3762: 
1.942     foxr     3763: #
                   3764: #   Server side include.
                   3765: # Parameters:
                   3766: #  fn     Possibly encrypted resource name/id.
                   3767: #  form   Hash that describes how the rendering should be done
                   3768: #         and other things.
1.944     foxr     3769: # Returns:
1.950     raeburn  3770: #   Scalar context: The content of the response.
                   3771: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     3772: #     
1.15      www      3773: sub ssi {
                   3774: 
1.944     foxr     3775:     my ($fn,%form)=@_;
1.1447    raeburn  3776:     my ($host,$request,$response);
                   3777:     $host = &absolute_url('',1);
1.711     albertel 3778: 
                   3779:     $form{'no_update_last_known'}=1;
1.895     albertel 3780:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      3781:     if (%form) {
1.1447    raeburn  3782:       $request=new HTTP::Request('POST',$host.$fn);
1.1272    droeschl 3783:       $request->content(join('&',map { 
                   3784:             my $name = escape($_);
                   3785:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
                   3786:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
                   3787:             : &escape($form{$_}) );    
                   3788:         } keys(%form)));
1.23      www      3789:     } else {
1.1447    raeburn  3790:       $request=new HTTP::Request('GET',$host.$fn);
1.23      www      3791:     }
                   3792: 
1.15      www      3793:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1345    raeburn  3794:     my $lonhost = $perlvar{'lonHostID'};
1.1385    raeburn  3795:     my $islocal;
                   3796:     if (($env{'request.course.id'}) &&
                   3797:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
                   3798:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
                   3799:         ($form{'grade_symb'} ne '') &&
1.1494    raeburn  3800:         (&allowed('mgr',$env{'request.course.id'}.
                   3801:                         ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
1.1385    raeburn  3802:         $islocal = 1;
                   3803:     }
1.1447    raeburn  3804:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
                   3805:                                              '','','',$islocal);
1.1182    foxr     3806: 
1.944     foxr     3807:     if (wantarray) {
1.1345    raeburn  3808: 	return ($response->content, $response);
1.944     foxr     3809:     } else {
1.1345    raeburn  3810: 	return $response->content;
1.942     foxr     3811:     }
1.324     www      3812: }
                   3813: 
                   3814: sub externalssi {
                   3815:     my ($url)=@_;
                   3816:     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn  3817:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
1.954     raeburn  3818:     if (wantarray) {
                   3819:         return ($response->content, $response);
                   3820:     } else {
                   3821:         return $response->content;
                   3822:     }
1.15      www      3823: }
1.254     www      3824: 
1.1354    raeburn  3825: 
                   3826: # If the local copy of a replicated resource is outdated, trigger a  
                   3827: # connection from the homeserver to flush the delayed queue. If no update 
                   3828: # happens, remove local copies of outdated resource (and corresponding
                   3829: # metadata file).
                   3830: 
1.1352    raeburn  3831: sub remove_stale_resfile {
                   3832:     my ($url) = @_;
1.1354    raeburn  3833:     my $removed;
1.1352    raeburn  3834:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
                   3835:         my $audom = $1;
                   3836:         my $auname = $2;
1.1353    raeburn  3837:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
1.1352    raeburn  3838:             my $homeserver = &homeserver($auname,$audom);
                   3839:             unless (($homeserver eq 'no_host') ||
                   3840:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
                   3841:                 my $fname = &filelocation('',$url);
                   3842:                 if (-e $fname) {
                   3843:                     my $hostname = &hostname($homeserver);
                   3844:                     if ($hostname) {
1.1397    raeburn  3845:                         my $protocol = $protocol{$homeserver};
                   3846:                         $protocol = 'http' if ($protocol ne 'https');
1.1352    raeburn  3847:                         my $uri = &declutter($url);
                   3848:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
                   3849:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
                   3850:                         if ($response->is_success()) {
                   3851:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
                   3852:                             my $locmodtime = (stat($fname))[9];
                   3853:                             if ($locmodtime < $remmodtime) {
1.1354    raeburn  3854:                                 my $stale;
                   3855:                                 my $answer = &reply('pong',$homeserver);
                   3856:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
                   3857:                                     sleep(0.2);
                   3858:                                     $locmodtime = (stat($fname))[9];
                   3859:                                     if ($locmodtime < $remmodtime) {
                   3860:                                         my $posstransfer = $fname.'.in.transfer';
                   3861:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
                   3862:                                             $removed = 1;
                   3863:                                         } else {
                   3864:                                             $stale = 1;
                   3865:                                         }
                   3866:                                     } else {
                   3867:                                         $removed = 1;
                   3868:                                     }
                   3869:                                 } else {
                   3870:                                     $stale = 1;
                   3871:                                 }
                   3872:                                 if ($stale) {
1.1421    raeburn  3873:                                     if (unlink($fname)) {
                   3874:                                         if ($uri!~/\.meta$/) {
                   3875:                                             if (-e $fname.'.meta') {
                   3876:                                                 unlink($fname.'.meta');
                   3877:                                             }
                   3878:                                         }
1.1422    raeburn  3879:                                         my $unsubresult = &unsubscribe($fname);
                   3880:                                         unless ($unsubresult eq 'ok') {
                   3881:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
                   3882:                                         }
1.1421    raeburn  3883:                                         $removed = 1;
1.1354    raeburn  3884:                                     }
1.1352    raeburn  3885:                                 }
                   3886:                             }
                   3887:                         }
                   3888:                     }
                   3889:                 }
                   3890:             }
                   3891:         }
                   3892:     }
1.1354    raeburn  3893:     return $removed;
1.1352    raeburn  3894: }
                   3895: 
1.492     albertel 3896: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   3897: 
                   3898: sub allowuploaded {
                   3899:     my ($srcurl,$url)=@_;
                   3900:     $url=&clutter(&declutter($url));
                   3901:     my $dir=$url;
                   3902:     $dir=~s/\/[^\/]+$//;
                   3903:     my %httpref=();
                   3904:     my $httpurl=&hreflocation('',$url);
                   3905:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  3906:     &Apache::lonnet::appenv(\%httpref);
1.254     www      3907: }
1.477     raeburn  3908: 
1.1193    raeburn  3909: #
                   3910: # Determine if the current user should be able to edit a particular resource,
                   3911: # when viewing in course context.
                   3912: # (a) When viewing resource used to determine if "Edit" item is included in 
                   3913: #     Functions.
                   3914: # (b) When displaying folder contents in course editor, used to determine if
                   3915: #     "Edit" link will be displayed alongside resource.
                   3916: #
1.1196    raeburn  3917: #  input: six args -- filename (decluttered), course number, course domain,
                   3918: #                   url, symb (if registered) and group (if this is a group
                   3919: #                   item -- e.g., bulletin board, group page etc.).
                   3920: #  output: array of five scalars -- 
1.1193    raeburn  3921: #          $cfile -- url for file editing if editable on current server
                   3922: #          $home -- homeserver of resource (i.e., for author if published,
                   3923: #                                           or course if uploaded.).
                   3924: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  3925: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   3926: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  3927: #
                   3928: 
                   3929: sub can_edit_resource {
1.1194    raeburn  3930:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   3931:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   3932: #
                   3933: # For aboutme pages user can only edit his/her own.
                   3934: #
1.1199    raeburn  3935:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  3936:         my ($sdom,$sname) = ($1,$2);
                   3937:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   3938:             $home = $env{'user.home'};
                   3939:             $cfile = $resurl;
                   3940:             if ($env{'form.forceedit'}) {
                   3941:                 $forceview = 1;
                   3942:             } else {
                   3943:                 $forceedit = 1;
                   3944:             }
                   3945:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3946:         } else {
                   3947:             return;
                   3948:         }
                   3949:     }
                   3950: 
1.1517    raeburn  3951: #
                   3952: # For /adm/viewcoauthors can only edit if author or co-author who is manager.
                   3953: #
                   3954: 
                   3955:     if (($resurl eq '/adm/viewcoauthors') && ($cnum ne '') && ($cdom ne '')) {
                   3956:         if (((&allowed('cca',"$cdom/$cnum")) ||
                   3957:              (&allowed('caa',"$cdom/$cnum"))) ||
                   3958:              ((&allowed('vca',"$cdom/$cnum") ||
                   3959:                &allowed('vaa',"$cdom/$cnum")) &&
                   3960:               ($env{"environment.internal.manager./$cdom/$cnum"}))) {
                   3961:             $home = $env{'user.home'};
                   3962:             $cfile = $resurl;
                   3963:             if ($env{'form.forceedit'}) {
                   3964:                 $forceview = 1;
                   3965:             } else {
                   3966:                 $forceedit = 1;
                   3967:             }
                   3968:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3969:         } else {
                   3970:             return;
                   3971:         }
                   3972:     }
                   3973: 
1.1194    raeburn  3974:     if ($env{'request.course.id'}) {
1.1494    raeburn  3975:         my $crsedit = &allowed('mdc',$env{'request.course.id'});
1.1194    raeburn  3976:         if ($group ne '') {
                   3977: # if this is a group homepage or group bulletin board, check group privs
                   3978:             my $allowed = 0;
1.1197    raeburn  3979:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   3980:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  3981:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  3982:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   3983:                     $allowed = 1;
                   3984:                 }
1.1197    raeburn  3985:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   3986:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   3987:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  3988:                     $allowed = 1;
                   3989:                 }
                   3990:             }
                   3991:             if ($allowed) {
                   3992:                 $home=&homeserver($cnum,$cdom);
                   3993:                 if ($env{'form.forceedit'}) {
                   3994:                     $forceview = 1;
                   3995:                 } else {
                   3996:                     $forceedit = 1;
                   3997:                 }
                   3998:                 $cfile = $resurl;
                   3999:             } else {
                   4000:                 return;
                   4001:             }
                   4002:         } else {
1.1208    raeburn  4003:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
1.1494    raeburn  4004:                 unless (&allowed('opa',$env{'request.course.id'})) {
1.1208    raeburn  4005:                     return;
                   4006:                 }
                   4007:             } elsif (!$crsedit) {
1.1507    raeburn  4008:                 if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
1.1194    raeburn  4009: #
                   4010: # No edit allowed where CC has switched to student role.
                   4011: #
1.1507    raeburn  4012:                     return;
                   4013:                 } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
                   4014:                          ($resurl =~ m{^/res/lib/templates/})) {
                   4015:                     return;
                   4016:                 }
1.1194    raeburn  4017:             }
                   4018:         }
                   4019:     }
                   4020: 
1.1193    raeburn  4021:     if ($file ne '') {
                   4022:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  4023:             if (&is_course_upload($file,$cnum,$cdom)) {
                   4024:                 $uploaded = 1;
                   4025:                 $incourse = 1;
1.1193    raeburn  4026:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   4027:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  4028:                     if ($env{'form.forceedit'}) {
                   4029:                         $forceview = 1;
                   4030:                     } else {
                   4031:                         $forceedit = 1;
                   4032:                     }
1.1194    raeburn  4033:                 }
                   4034:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   4035:                 $incourse = 1;
                   4036:                 if ($env{'form.forceedit'}) {
                   4037:                     $forceview = 1;
                   4038:                 } else {
                   4039:                     $forceedit = 1;
                   4040:                 }
                   4041:                 $cfile = $resurl;
1.1507    raeburn  4042:             } elsif (($resurl ne '') && (&is_on_map($resurl))) {
1.1194    raeburn  4043:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   4044:                     $incourse = 1;
                   4045:                     if ($env{'form.forceedit'}) {
                   4046:                         $forceview = 1;
                   4047:                     } else {
                   4048:                         $forceedit = 1;
                   4049:                     }
                   4050:                     $cfile = $resurl;
1.1199    raeburn  4051:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  4052:                     $incourse = 1;
                   4053:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  4054:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  4055:                     $incourse = 1;
1.1199    raeburn  4056:                     if ($env{'form.forceedit'}) {
                   4057:                         $forceview = 1;
                   4058:                     } else {
                   4059:                         $forceedit = 1;
                   4060:                     }
                   4061:                     $cfile = $resurl;
1.1419    raeburn  4062:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
                   4063:                     my ($map,$id,$res) = &decode_symb($symb);
                   4064:                     if ($map =~ /\.page$/) {
                   4065:                         $incourse = 1;
                   4066:                         if ($env{'form.forceedit'}) {
                   4067:                             $forceview = 1;
                   4068:                             $cfile = $map;
                   4069:                         } else {
                   4070:                             $forceedit = 1;
                   4071:                             $cfile =  '/adm/wrapper'.$resurl;
                   4072:                         }
                   4073:                     }
1.1343    raeburn  4074:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  4075:                     $incourse = 1;
                   4076:                     if ($env{'form.forceedit'}) {
                   4077:                         $forceview = 1;
                   4078:                     } else {
                   4079:                         $forceedit = 1;
                   4080:                     }
                   4081:                     $cfile = $resurl;
1.1208    raeburn  4082:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   4083:                     $incourse = 1;
                   4084:                     if ($env{'form.forceedit'}) {
                   4085:                         $forceview = 1;
                   4086:                     } else {
                   4087:                         $forceedit = 1;
                   4088:                     }
                   4089:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  4090:                 }
                   4091:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   4092:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   4093:                 if (&is_on_map($template)) { 
                   4094:                     $incourse = 1;
                   4095:                     $forceview = 1;
                   4096:                     $cfile = $template;
1.1193    raeburn  4097:                 }
1.1199    raeburn  4098:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1418    raeburn  4099:                 $incourse = 1;
                   4100:                 if ($env{'form.forceedit'}) {
                   4101:                     $forceview = 1;
                   4102:                 } else {
                   4103:                     $forceedit = 1;
                   4104:                 }
                   4105:                 $cfile = $resurl;
1.1343    raeburn  4106:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298    raeburn  4107:                 $incourse = 1;
                   4108:                 if ($env{'form.forceedit'}) {
                   4109:                     $forceview = 1;
                   4110:                 } else {
                   4111:                     $forceedit = 1;
                   4112:                 }
                   4113:                 $cfile = $resurl;
1.1199    raeburn  4114:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   4115:                 $incourse = 1;
                   4116:                 $forceview = 1;
                   4117:                 if ($symb) {
                   4118:                     my ($map,$id,$res)=&decode_symb($symb);
                   4119:                     $env{'request.symb'} = $symb;
                   4120:                     $cfile = &clutter($res);
                   4121:                 } else {
                   4122:                     $cfile = $env{'form.suppurl'};
1.1298    raeburn  4123:                     my $escfile = &unescape($cfile);
1.1343    raeburn  4124:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  4125:                         $cfile = '/adm/wrapper'.$escfile;
                   4126:                     } else {
                   4127:                         $escfile =~ s{^http://}{};
                   4128:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
                   4129:                     }
1.1199    raeburn  4130:                 }
1.1234    raeburn  4131:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   4132:                 if ($env{'form.forceedit'}) {
                   4133:                     $forceview = 1;
                   4134:                 } else {
                   4135:                     $forceedit = 1;
                   4136:                 }
                   4137:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193    raeburn  4138:             }
                   4139:         }
1.1194    raeburn  4140:         if ($uploaded || $incourse) {
                   4141:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  4142:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  4143:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   4144:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   4145:             # Check that the user has permission to edit this resource
                   4146:             my $setpriv = 1;
                   4147:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   4148:             if (defined($cfudom)) {
                   4149:                 $home=&homeserver($cfuname,$cfudom);
                   4150:                 $cfile=$file;
                   4151:             }
                   4152:         }
1.1194    raeburn  4153:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   4154:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  4155:             my @ids=&current_machine_ids();
                   4156:             unless (grep(/^\Q$home\E$/,@ids)) {
                   4157:                 $switchserver=1;
                   4158:             }
                   4159:         }
                   4160:     }
1.1194    raeburn  4161:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  4162: }
                   4163: 
                   4164: sub is_course_upload {
                   4165:     my ($file,$cnum,$cdom) = @_;
                   4166:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   4167:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  4168:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   4169:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  4170:         return 1;
                   4171:     }
                   4172:     return;
                   4173: }
                   4174: 
1.1194    raeburn  4175: sub in_course {
1.1195    raeburn  4176:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   4177:     if ($hideprivileged) {
                   4178:         my $skipuser;
1.1219    raeburn  4179:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   4180:         my @possdoms = ($cdom);  
                   4181:         if ($coursehash{'checkforpriv'}) { 
                   4182:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   4183:         }
                   4184:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  4185:             $skipuser = 1;
                   4186:             if ($coursehash{'nothideprivileged'}) {
                   4187:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   4188:                     my $user;
                   4189:                     if ($item =~ /:/) {
                   4190:                         $user = $item;
                   4191:                     } else {
                   4192:                         $user = join(':',split(/[\@]/,$item));
                   4193:                     }
                   4194:                     if ($user eq $uname.':'.$udom) {
                   4195:                         undef($skipuser);
                   4196:                         last;
                   4197:                     }
                   4198:                 }
                   4199:             }
                   4200:             if ($skipuser) {
                   4201:                 return 0;
                   4202:             }
                   4203:         }
                   4204:     }
1.1194    raeburn  4205:     $type ||= 'any';
                   4206:     if (!defined($cdom) || !defined($cnum)) {
                   4207:         my $cid  = $env{'request.course.id'};
                   4208:         $cdom = $env{'course.'.$cid.'.domain'};
                   4209:         $cnum = $env{'course.'.$cid.'.num'};
                   4210:     }
                   4211:     my $typesref;
1.1195    raeburn  4212:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  4213:         $typesref = ['active','previous','future'];
                   4214:     } elsif ($type eq 'previous' || $type eq 'future') {
                   4215:         $typesref = [$type];
                   4216:     }
                   4217:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   4218:                               $typesref,undef,[$cdom]);
                   4219:     my ($tmp) = keys(%roles);
                   4220:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   4221:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   4222:     if (@course_roles > 0) {
                   4223:         return 1;
                   4224:     }
                   4225:     return 0;
                   4226: }
                   4227: 
1.478     albertel 4228: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 4229: # input: action, courseID, current domain, intended
1.637     raeburn  4230: #        path to file, source of file, instruction to parse file for objects,
                   4231: #        ref to hash for embedded objects,
                   4232: #        ref to hash for codebase of java objects.
1.1095    raeburn  4233: #        reference to scalar to accommodate mime type determined
                   4234: #          from File::MMagic if $parser = parse.
1.637     raeburn  4235: #
1.485     raeburn  4236: # output: url to file (if action was uploaddoc), 
                   4237: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  4238: #
1.478     albertel 4239: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   4240: # course.
1.477     raeburn  4241: #
1.478     albertel 4242: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4243: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   4244: #          course's home server.
1.477     raeburn  4245: #
1.478     albertel 4246: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   4247: #          be copied from $source (current location) to 
                   4248: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4249: #         and will then be copied to
                   4250: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   4251: #         course's home server.
1.485     raeburn  4252: #
1.481     raeburn  4253: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 4254: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  4255: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4256: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   4257: #         in course's home server.
1.637     raeburn  4258: #
1.477     raeburn  4259: 
                   4260: sub process_coursefile {
1.1095    raeburn  4261:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   4262:         $mimetype)=@_;
1.477     raeburn  4263:     my $fetchresult;
1.638     albertel 4264:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  4265:     if ($action eq 'propagate') {
1.638     albertel 4266:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   4267: 			     $home);
1.481     raeburn  4268:     } else {
1.477     raeburn  4269:         my $fpath = '';
                   4270:         my $fname = $file;
1.478     albertel 4271:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  4272:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  4273:         my $filepath = &build_filepath($fpath);
1.481     raeburn  4274:         if ($action eq 'copy') {
                   4275:             if ($source eq '') {
                   4276:                 $fetchresult = 'no source file';
                   4277:                 return $fetchresult;
                   4278:             } else {
                   4279:                 my $destination = $filepath.'/'.$fname;
                   4280:                 rename($source,$destination);
                   4281:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4282:                                  $home);
1.481     raeburn  4283:             }
                   4284:         } elsif ($action eq 'uploaddoc') {
1.1359    raeburn  4285:             open(my $fh,'>',$filepath.'/'.$fname);
1.620     albertel 4286:             print $fh $env{'form.'.$source};
1.481     raeburn  4287:             close($fh);
1.637     raeburn  4288:             if ($parser eq 'parse') {
1.1024    raeburn  4289:                 my $mm = new File::MMagic;
1.1095    raeburn  4290:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   4291:                 if ($type eq 'text/html') {
1.1024    raeburn  4292:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   4293:                     unless ($parse_result eq 'ok') {
                   4294:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   4295:                     }
1.637     raeburn  4296:                 }
1.1095    raeburn  4297:                 if (ref($mimetype)) {
                   4298:                     $$mimetype = $type;
                   4299:                 } 
1.637     raeburn  4300:             }
1.477     raeburn  4301:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4302:                                  $home);
1.481     raeburn  4303:             if ($fetchresult eq 'ok') {
                   4304:                 return '/uploaded/'.$fpath.'/'.$fname;
                   4305:             } else {
                   4306:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4307:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  4308:                 return '/adm/notfound.html';
                   4309:             }
1.477     raeburn  4310:         }
                   4311:     }
1.485     raeburn  4312:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  4313:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4314:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  4315:     }
                   4316:     return $fetchresult;
                   4317: }
                   4318: 
1.637     raeburn  4319: sub build_filepath {
                   4320:     my ($fpath) = @_;
                   4321:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   4322:     unless ($fpath eq '') {
                   4323:         my @parts=split('/',$fpath);
                   4324:         foreach my $part (@parts) {
                   4325:             $filepath.= '/'.$part;
                   4326:             if ((-e $filepath)!=1) {
                   4327:                 mkdir($filepath,0777);
                   4328:             }
                   4329:         }
                   4330:     }
                   4331:     return $filepath;
                   4332: }
                   4333: 
                   4334: sub store_edited_file {
1.638     albertel 4335:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  4336:     my $file = $primary_url;
                   4337:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   4338:     my $fpath = '';
                   4339:     my $fname = $file;
                   4340:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   4341:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   4342:     my $filepath = &build_filepath($fpath);
1.1359    raeburn  4343:     open(my $fh,'>',$filepath.'/'.$fname);
1.637     raeburn  4344:     print $fh $content;
                   4345:     close($fh);
1.638     albertel 4346:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  4347:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4348: 			  $home);
1.637     raeburn  4349:     if ($$fetchresult eq 'ok') {
                   4350:         return '/uploaded/'.$fpath.'/'.$fname;
                   4351:     } else {
1.638     albertel 4352:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   4353: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  4354:         return '/adm/notfound.html';
                   4355:     }
                   4356: }
                   4357: 
1.531     albertel 4358: sub clean_filename {
1.831     albertel 4359:     my ($fname,$args)=@_;
1.315     www      4360: # Replace Windows backslashes by forward slashes
1.257     www      4361:     $fname=~s/\\/\//g;
1.831     albertel 4362:     if (!$args->{'keep_path'}) {
                   4363:         # Get rid of everything but the actual filename
                   4364: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   4365:     }
1.315     www      4366: # Replace spaces by underscores
                   4367:     $fname=~s/\s+/\_/g;
1.1406    raeburn  4368: # Transliterate non-ascii text to ascii
                   4369:     my $lang = &Apache::lonlocal::current_language();
                   4370:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315     www      4371: # Replace all other weird characters by nothing
1.831     albertel 4372:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 4373: # Replace all .\d. sequences with _\d. so they no longer look like version
                   4374: # numbers
                   4375:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1443    raeburn  4376: # Replace three or more adjacent underscores with one for consistency 
                   4377: # with loncfile::filename_check() so complete url can be extracted by
                   4378: # lonnet::decode_symb()
                   4379:     $fname=~s/_{3,}/_/g;
1.531     albertel 4380:     return $fname;
                   4381: }
1.1406    raeburn  4382: 
1.1051    raeburn  4383: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   4384: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   4385: # image with the same aspect ratio as the original, but with dimensions which do 
                   4386: # not exceed $resizewidth and $resizeheight.
                   4387:  
1.984     neumanie 4388: sub resizeImage {
1.1051    raeburn  4389:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   4390:     my $ima = Image::Magick->new;
                   4391:     my $resized;
                   4392:     if (-e $img_path) {
                   4393:         $ima->Read($img_path);
                   4394:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   4395:             my $width = $ima->Get('width');
                   4396:             my $height = $ima->Get('height');
                   4397:             if ($width > $resizewidth) {
                   4398: 	        my $factor = $width/$resizewidth;
                   4399:                 my $newheight = $height/$factor;
                   4400:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   4401:                 $resized = 1;
                   4402:             }
                   4403:         }
                   4404:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   4405:             my $width = $ima->Get('width');
                   4406:             my $height = $ima->Get('height');
                   4407:             if ($height > $resizeheight) {
                   4408:                 my $factor = $height/$resizeheight;
                   4409:                 my $newwidth = $width/$factor;
                   4410:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   4411:                 $resized = 1;
                   4412:             }
                   4413:         }
                   4414:         if ($resized) {
                   4415:             $ima->Write($img_path);
                   4416:         }
                   4417:     }
                   4418:     return;
1.977     amueller 4419: }
                   4420: 
1.608     albertel 4421: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 4422: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  4423: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  4424: #        $context - possible values: coursedoc, existingfile, overwrite, 
1.1504    raeburn  4425: #                                    canceloverwrite, scantron, toollogo  or ''.
1.1090    raeburn  4426: #                   if 'coursedoc': upload to the current course
                   4427: #                   if 'existingfile': write file to tmp/overwrites directory 
                   4428: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   4429: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 4430: #        $subdir - directory in userfile to store the file into
1.1401    raeburn  4431: #        $parser - instruction to parse file for objects ($parser = parse) or
                   4432: #                  if context is 'scantron', $parser is hashref of csv column mapping
                   4433: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
                   4434: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858     raeburn  4435: #        $allfiles - reference to hash for embedded objects
                   4436: #        $codebase - reference to hash for codebase of java objects
1.1504    raeburn  4437: #        $destuname - username for permanent storage of uploaded file
                   4438: #        $destudom - domain for permanaent storage of uploaded file
1.860     raeburn  4439: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   4440: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  4441: #        $resizewidth - width (pixels) to which to resize uploaded image
                   4442: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  4443: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  4444: #                    from File::MMagic.
1.858     raeburn  4445: # 
1.686     albertel 4446: # output: url of file in userspace, or error: <message> 
                   4447: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 4448: 
1.531     albertel 4449: sub userfileupload {
1.1090    raeburn  4450:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  4451:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 4452:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 4453:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 4454:     $fname=&clean_filename($fname);
1.1090    raeburn  4455:     # See if there is anything left
1.257     www      4456:     unless ($fname) { return 'error: no uploaded file'; }
1.1406    raeburn  4457:     # If filename now begins with a . prepend unix timestamp _ milliseconds
                   4458:     if ($fname =~ /^\./) {
                   4459:         my ($s,$usec) = &gettimeofday();
                   4460:         while (length($usec) < 6) {
                   4461:             $usec = '0'.$usec;
                   4462:         }
                   4463:         $fname = $s.'_'.substr($usec,0,3).$fname;
                   4464:     }
1.1090    raeburn  4465:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   4466:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   4467:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   4468:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  4469:         my $now = time;
1.1090    raeburn  4470:         my $filepath;
1.1095    raeburn  4471:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  4472:              $filepath = 'tmp/helprequests/'.$now;
                   4473:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   4474:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   4475:                          '_'.$env{'user.domain'}.'/pending';
                   4476:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   4477:             my ($docuname,$docudom);
1.1350    raeburn  4478:             if ($destudom =~ /^$match_domain$/) {
1.1090    raeburn  4479:                 $docudom = $destudom;
                   4480:             } else {
                   4481:                 $docudom = $env{'user.domain'};
                   4482:             }
1.1350    raeburn  4483:             if ($destuname =~ /^$match_username$/) {
1.1090    raeburn  4484:                 $docuname = $destuname;
                   4485:             } else {
                   4486:                 $docuname = $env{'user.name'};
                   4487:             }
                   4488:             if (exists($env{'form.group'})) {
                   4489:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4490:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4491:             }
                   4492:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   4493:             if ($context eq 'canceloverwrite') {
                   4494:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   4495:                 if (-e  $tempfile) {
                   4496:                     my @info = stat($tempfile);
                   4497:                     if ($info[9] eq $env{'form.timestamp'}) {
                   4498:                         unlink($tempfile);
                   4499:                     }
                   4500:                 }
                   4501:                 return;
1.523     raeburn  4502:             }
                   4503:         }
1.1090    raeburn  4504:         # Create the directory if not present
1.741     raeburn  4505:         my @parts=split(/\//,$filepath);
                   4506:         my $fullpath = $perlvar{'lonDaemons'};
                   4507:         for (my $i=0;$i<@parts;$i++) {
                   4508:             $fullpath .= '/'.$parts[$i];
                   4509:             if ((-e $fullpath)!=1) {
                   4510:                 mkdir($fullpath,0777);
                   4511:             }
                   4512:         }
1.1359    raeburn  4513:         open(my $fh,'>',$fullpath.'/'.$fname);
1.741     raeburn  4514:         print $fh $env{'form.'.$formname};
                   4515:         close($fh);
1.1090    raeburn  4516:         if ($context eq 'existingfile') {
                   4517:             my @info = stat($fullpath.'/'.$fname);
                   4518:             return ($fullpath.'/'.$fname,$info[9]);
                   4519:         } else {
                   4520:             return $fullpath.'/'.$fname;
                   4521:         }
1.523     raeburn  4522:     }
1.995     raeburn  4523:     if ($subdir eq 'scantron') {
                   4524:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  4525:     } else {
1.995     raeburn  4526:         $fname="$subdir/$fname";
                   4527:     }
1.1090    raeburn  4528:     if ($context eq 'coursedoc') {
1.638     albertel 4529: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4530: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  4531:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 4532:             return &finishuserfileupload($docuname,$docudom,
                   4533: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  4534: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  4535:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  4536:         } else {
1.1218    raeburn  4537:             if ($env{'form.folder'}) {
                   4538:                 $fname=$env{'form.folder'}.'/'.$fname;
                   4539:             }
1.638     albertel 4540:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   4541: 				       $fname,$formname,$parser,
1.1095    raeburn  4542: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  4543:         }
1.719     banghart 4544:     } elsif (defined($destuname)) {
                   4545:         my $docuname=$destuname;
                   4546:         my $docudom=$destudom;
1.860     raeburn  4547: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4548: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4549:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4550:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4551:     } else {
1.638     albertel 4552:         my $docuname=$env{'user.name'};
                   4553:         my $docudom=$env{'user.domain'};
1.1220    raeburn  4554:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  4555:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4556:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4557:         }
1.860     raeburn  4558: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4559: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4560:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4561:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4562:     }
1.271     www      4563: }
                   4564: 
                   4565: sub finishuserfileupload {
1.860     raeburn  4566:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  4567:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  4568:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      4569:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 4570:   
1.860     raeburn  4571:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 4572:     $file=$fname;
                   4573:     if ($fname=~m|/|) {
                   4574:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   4575: 	$path.=$fnamepath.'/';
                   4576:     }
1.259     www      4577:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      4578:     my $count;
                   4579:     for ($count=4;$count<=$#parts;$count++) {
                   4580:         $filepath.="/$parts[$count]";
                   4581:         if ((-e $filepath)!=1) {
                   4582: 	    mkdir($filepath,0777);
                   4583:         }
                   4584:     }
1.984     neumanie 4585: 
1.258     www      4586: # Save the file
                   4587:     {
1.1359    raeburn  4588: 	if (!open(FH,'>',$filepath.'/'.$file)) {
1.701     albertel 4589: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   4590: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   4591: 	    return '/adm/notfound.html';
                   4592: 	}
1.1090    raeburn  4593:         if ($context eq 'overwrite') {
1.1117    foxr     4594:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  4595:             my $target = $filepath.'/'.$file;
                   4596:             if (-e $source) {
                   4597:                 my @info = stat($source);
                   4598:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   4599:                     unless (&File::Copy::move($source,$target)) {
                   4600:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   4601:                         return "Moving from $source failed";
                   4602:                     }
                   4603:                 } else {
                   4604:                     return "Temporary file: $source had unexpected date/time for last modification";
                   4605:                 }
                   4606:             } else {
                   4607:                 return "Temporary file: $source missing";
                   4608:             }
                   4609:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 4610: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   4611: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   4612: 	    return '/adm/notfound.html';
                   4613: 	}
1.570     albertel 4614: 	close(FH);
1.1051    raeburn  4615:         if ($resizewidth && $resizeheight) {
                   4616:             my $mm = new File::MMagic;
                   4617:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   4618:             if ($mime_type =~ m{^image/}) {
                   4619: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   4620:             }  
1.977     amueller 4621: 	}
1.258     www      4622:     }
1.1152    raeburn  4623:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   4624:         if (ref($mimetype)) {
                   4625:             if ($$mimetype eq '') {
                   4626:                 my $mm = new File::MMagic;
                   4627:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   4628:                 $$mimetype = $type;
                   4629:             }
                   4630:         }
                   4631:     }
1.1401    raeburn  4632:     if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152    raeburn  4633:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  4634:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   4635:                                                        $allfiles,$codebase);
                   4636:             unless ($parse_result eq 'ok') {
                   4637:                 &logthis('Failed to parse '.$filepath.$file.
                   4638: 	   	         ' for embedded media: '.$parse_result); 
                   4639:             }
1.637     raeburn  4640:         }
1.1401    raeburn  4641:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
                   4642:         my $format = $env{'form.scantron_format'};
                   4643:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637     raeburn  4644:     }
1.860     raeburn  4645:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   4646:         my $input = $filepath.'/'.$file;
                   4647:         my $output = $filepath.'/'.'tn-'.$file;
1.1504    raeburn  4648:         my $makethumb; 
1.860     raeburn  4649:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1504    raeburn  4650:         if ($context eq 'toollogo') {
                   4651:             my ($fullwidth,$fullheight) = &check_dimensions($input);
                   4652:             if ($fullwidth ne '' && $fullheight ne '') {
                   4653:                 if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
                   4654:                     $makethumb = 1;
                   4655:                 }
                   4656:             }
                   4657:         } else {
                   4658:             $makethumb = 1;
                   4659:         }
                   4660:         if ($makethumb) {
                   4661:             my @args = ('convert','-sample',$thumbsize,$input,$output);
                   4662:             system({$args[0]} @args);
                   4663:             if (-e $filepath.'/'.'tn-'.$file) {
                   4664:                 $fetchthumb  = 1; 
                   4665:             }
1.860     raeburn  4666:         }
                   4667:     }
1.858     raeburn  4668:  
1.259     www      4669: # Notify homeserver to grep it
                   4670: #
1.984     neumanie 4671:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 4672:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      4673:     if ($fetchresult eq 'ok') {
1.860     raeburn  4674:         if ($fetchthumb) {
                   4675:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   4676:             if ($thumbresult ne 'ok') {
                   4677:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   4678:                          $docuhome.': '.$thumbresult);
                   4679:             }
                   4680:         }
1.259     www      4681: #
1.258     www      4682: # Return the URL to it
1.494     albertel 4683:         return '/uploaded/'.$path.$file;
1.263     www      4684:     } else {
1.494     albertel 4685:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   4686: 		 ': '.$fetchresult);
1.263     www      4687:         return '/adm/notfound.html';
1.858     raeburn  4688:     }
1.493     albertel 4689: }
                   4690: 
1.637     raeburn  4691: sub extract_embedded_items {
1.961     raeburn  4692:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  4693:     my @state = ();
1.1164    raeburn  4694:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  4695:     my %javafiles = (
                   4696:                       codebase => '',
                   4697:                       code => '',
                   4698:                       archive => ''
                   4699:                     );
                   4700:     my %mediafiles = (
                   4701:                       src => '',
                   4702:                       movie => '',
                   4703:                      );
1.648     raeburn  4704:     my $p;
                   4705:     if ($content) {
                   4706:         $p = HTML::LCParser->new($content);
                   4707:     } else {
1.961     raeburn  4708:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  4709:     }
1.641     albertel 4710:     while (my $t=$p->get_token()) {
1.640     albertel 4711: 	if ($t->[0] eq 'S') {
                   4712: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 4713: 	    push(@state, $tagname);
1.648     raeburn  4714:             if (lc($tagname) eq 'allow') {
                   4715:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   4716:             }
1.640     albertel 4717: 	    if (lc($tagname) eq 'img') {
                   4718: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   4719: 	    }
1.886     albertel 4720: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  4721:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  4722:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4723:                 }
1.886     albertel 4724: 	    }
1.645     raeburn  4725:             if (lc($tagname) eq 'script') {
1.1164    raeburn  4726:                 my $src;
1.645     raeburn  4727:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   4728:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   4729:                 } else {
1.1164    raeburn  4730:                     if ($attr->{'src'} ne '') {
                   4731:                         $src = $attr->{'src'};
                   4732:                         &add_filetype($allfiles,$src,'src');
                   4733:                     }
                   4734:                 }
                   4735:                 my $text = $p->get_trimmed_text();
                   4736:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   4737:                     my @swfargs = split(/,/,$1);
                   4738:                     foreach my $item (@swfargs) {
                   4739:                         $item =~ s/["']//g;
                   4740:                         $item =~ s/^\s+//;
                   4741:                         $item =~ s/\s+$//;
                   4742:                     }
                   4743:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   4744:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   4745:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   4746:                         } else {
                   4747:                             $related{$swfargs[0]} = [$swfargs[2]];
                   4748:                         }
                   4749:                     }
1.645     raeburn  4750:                 }
                   4751:             }
                   4752:             if (lc($tagname) eq 'link') {
                   4753:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   4754:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4755:                 }
                   4756:             }
1.640     albertel 4757: 	    if (lc($tagname) eq 'object' ||
                   4758: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   4759: 		foreach my $item (keys(%javafiles)) {
                   4760: 		    $javafiles{$item} = '';
                   4761: 		}
1.1164    raeburn  4762:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   4763:                     $lastids{lc($tagname)} = $attr->{'id'};
                   4764:                 }
1.640     albertel 4765: 	    }
                   4766: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   4767: 		my $name = lc($attr->{'name'});
                   4768: 		foreach my $item (keys(%javafiles)) {
                   4769: 		    if ($name eq $item) {
                   4770: 			$javafiles{$item} = $attr->{'value'};
                   4771: 			last;
                   4772: 		    }
                   4773: 		}
1.1164    raeburn  4774:                 my $pathfrom;
1.640     albertel 4775: 		foreach my $item (keys(%mediafiles)) {
                   4776: 		    if ($name eq $item) {
1.1164    raeburn  4777:                         $pathfrom = $attr->{'value'};
                   4778:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   4779: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 4780: 			last;
                   4781: 		    }
                   4782: 		}
1.1164    raeburn  4783:                 if ($name eq 'flashvars') {
                   4784:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   4785:                 }
                   4786:                 if ($pathfrom ne '') {
                   4787:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   4788:                                          $pathfrom);
                   4789:                 }
1.640     albertel 4790: 	    }
                   4791: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   4792: 		foreach my $item (keys(%javafiles)) {
                   4793: 		    if ($attr->{$item}) {
                   4794: 			$javafiles{$item} = $attr->{$item};
                   4795: 			last;
                   4796: 		    }
                   4797: 		}
                   4798: 		foreach my $item (keys(%mediafiles)) {
                   4799: 		    if ($attr->{$item}) {
                   4800: 			&add_filetype($allfiles,$attr->{$item},$item);
                   4801: 			last;
                   4802: 		    }
                   4803: 		}
1.1164    raeburn  4804:                 if (lc($tagname) eq 'embed') {
                   4805:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   4806:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   4807:                                              $attr->{'src'});
                   4808:                     }
                   4809:                 }
1.640     albertel 4810: 	    }
1.1243    raeburn  4811:             if (lc($tagname) eq 'iframe') {
                   4812:                 my $src = $attr->{'src'} ;
                   4813:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
                   4814:                     &add_filetype($allfiles,$src,'src');
                   4815:                 } elsif ($src =~ m{^/}) {
                   4816:                     if ($env{'request.course.id'}) {
                   4817:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4818:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4819:                         my $url = &hreflocation('',$fullpath);
                   4820:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
                   4821:                             my $relpath = $1;
                   4822:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
                   4823:                                 &add_filetype($allfiles,$1,'src');
                   4824:                             }
                   4825:                         }
                   4826:                     }
                   4827:                 }
                   4828:             }
1.1164    raeburn  4829:             if ($t->[4] =~ m{/>$}) {
1.1243    raeburn  4830:                 pop(@state);
1.1164    raeburn  4831:             }
1.640     albertel 4832: 	} elsif ($t->[0] eq 'E') {
                   4833: 	    my ($tagname) = ($t->[1]);
                   4834: 	    if ($javafiles{'codebase'} ne '') {
                   4835: 		$javafiles{'codebase'} .= '/';
                   4836: 	    }  
                   4837: 	    if (lc($tagname) eq 'applet' ||
                   4838: 		lc($tagname) eq 'object' ||
                   4839: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   4840: 		) {
                   4841: 		foreach my $item (keys(%javafiles)) {
                   4842: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   4843: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   4844: 			&add_filetype($allfiles,$file,$item);
                   4845: 		    }
                   4846: 		}
                   4847: 	    } 
                   4848: 	    pop @state;
                   4849: 	}
                   4850:     }
1.1164    raeburn  4851:     foreach my $id (sort(keys(%flashvars))) {
                   4852:         if ($shockwave{$id} ne '') {
                   4853:             my @pairs = split(/\&/,$flashvars{$id});
                   4854:             foreach my $pair (@pairs) {
                   4855:                 my ($key,$value) = split(/\=/,$pair);
                   4856:                 if ($key eq 'thumb') {
                   4857:                     &add_filetype($allfiles,$value,$key);
                   4858:                 } elsif ($key eq 'content') {
                   4859:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   4860:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   4861:                     if ($ext ne '') {
                   4862:                         &add_filetype($allfiles,$path.$value,$ext);
                   4863:                     }
                   4864:                 }
                   4865:             }
                   4866:         }
                   4867:     }
1.637     raeburn  4868:     return 'ok';
                   4869: }
                   4870: 
1.639     albertel 4871: sub add_filetype {
                   4872:     my ($allfiles,$file,$type)=@_;
                   4873:     if (exists($allfiles->{$file})) {
                   4874: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   4875: 	    push(@{$allfiles->{$file}}, &escape($type));
                   4876: 	}
                   4877:     } else {
                   4878: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  4879:     }
                   4880: }
                   4881: 
1.1164    raeburn  4882: sub embedded_dependency {
                   4883:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   4884:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   4885:         if (($identifier ne '') &&
                   4886:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   4887:             ($pathfrom ne '')) {
                   4888:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   4889:             foreach my $dep (@{$related->{$identifier}}) {
                   4890:                 &add_filetype($allfiles,$path.$dep,'object');
                   4891:             }
                   4892:         }
                   4893:     }
                   4894:     return;
                   4895: }
                   4896: 
1.1504    raeburn  4897: sub check_dimensions {
                   4898:     my ($inputfile) = @_;
                   4899:     my ($fullwidth,$fullheight);
                   4900:     if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
                   4901:         my $mm = new File::MMagic;
                   4902:         my $mime_type = $mm->checktype_filename($inputfile);
                   4903:         if ($mime_type =~ m{^image/}) {
                   4904:             if (open(PIPE,"identify $inputfile 2>&1 |")) {
                   4905:                 my $imageinfo = <PIPE>;
                   4906:                 if (!close(PIPE)) {
                   4907:                     &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
                   4908:                 }
                   4909:                 chomp($imageinfo);
                   4910:                 my ($fullsize) =
                   4911:                     ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
                   4912:                 if ($fullsize) {
                   4913:                     ($fullwidth,$fullheight) = split(/x/,$fullsize);
                   4914:                 }
                   4915:             }
                   4916:         }
                   4917:     }
                   4918:     return ($fullwidth,$fullheight);
                   4919: }
                   4920: 
1.1401    raeburn  4921: sub bubblesheet_converter {
                   4922:     my ($cdom,$fullpath,$config,$format) = @_;
                   4923:     if ((&domain($cdom) ne '') &&
1.1403    raeburn  4924:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
1.1401    raeburn  4925:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
1.1404    raeburn  4926:         my (%csvcols,%csvoptions);
                   4927:         if (ref($config->{'fields'}) eq 'HASH') {  
                   4928:             %csvcols = %{$config->{'fields'}};
                   4929:         }
                   4930:         if (ref($config->{'options'}) eq 'HASH') {
                   4931:             %csvoptions = %{$config->{'options'}};
                   4932:         }
1.1401    raeburn  4933:         my %csvbynum = reverse(%csvcols);
                   4934:         my %scantronconf = &get_scantron_config($format,$cdom);
                   4935:         if (keys(%scantronconf)) {
                   4936:             my %bynum = (
                   4937:                           $scantronconf{CODEstart} => 'CODEstart',
                   4938:                           $scantronconf{IDstart}   => 'IDstart',
                   4939:                           $scantronconf{PaperID}   => 'PaperID',
                   4940:                           $scantronconf{FirstName} => 'FirstName',
                   4941:                           $scantronconf{LastName}  => 'LastName',
                   4942:                           $scantronconf{Qstart}    => 'Qstart',
                   4943:                         );
                   4944:             my @ordered;
                   4945:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
1.1403    raeburn  4946:                 push(@ordered,$bynum{$item});
1.1401    raeburn  4947:             }
                   4948:             my %mapstart = (
                   4949:                               CODEstart => 'CODE',
                   4950:                               IDstart   => 'ID',
                   4951:                               PaperID   => 'PaperID',
                   4952:                               FirstName => 'FirstName',
                   4953:                               LastName  => 'LastName',
                   4954:                               Qstart    => 'FirstQuestion',
                   4955:                            );
                   4956:             my %maplength = (
                   4957:                               CODEstart => 'CODElength',
                   4958:                               IDstart   => 'IDlength',
                   4959:                               PaperID   => 'PaperIDlength',
                   4960:                               FirstName => 'FirstNamelength',
                   4961:                               LastName  => 'LastNamelength',
                   4962:             );
                   4963:             if (open(my $fh,'<',$fullpath)) {
                   4964:                 my $output;
1.1403    raeburn  4965:                 my %lettdig = &letter_to_digits();
                   4966:                 my %diglett = reverse(%lettdig);
                   4967:                 my $numletts = scalar(keys(%lettdig));
1.1404    raeburn  4968:                 my $num = 0;
1.1401    raeburn  4969:                 while (my $line=<$fh>) {
1.1404    raeburn  4970:                     $num ++;
                   4971:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
1.1401    raeburn  4972:                     $line =~ s{[\r\n]+$}{};
                   4973:                     my %found;
1.1475    raeburn  4974:                     my @values = split(/,/,$line,-1);
1.1401    raeburn  4975:                     my ($qstart,$record);
                   4976:                     for (my $i=0; $i<@values; $i++) {
1.1403    raeburn  4977:                         if ((($qstart ne '') && ($i > $qstart)) ||
                   4978:                             ($csvbynum{$i} eq 'FirstQuestion')) {
                   4979:                             if ($values[$i] eq '') {
                   4980:                                 $values[$i] = $scantronconf{'Qoff'};
                   4981:                             } elsif ($scantronconf{'Qon'} eq 'number') {
                   4982:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4983:                                     $values[$i] = $lettdig{uc($values[$i])};
                   4984:                                 }
                   4985:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
                   4986:                                 if ($values[$i] =~ /^[0-9]$/) {
                   4987:                                     $values[$i] = $diglett{$values[$i]};
                   4988:                                 }
                   4989:                             } else {
                   4990:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
                   4991:                                     my $digit;
                   4992:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4993:                                         $digit = $lettdig{uc($values[$i])}-1;
                   4994:                                         if ($values[$i] eq 'J') {
                   4995:                                             $digit += $numletts;
                   4996:                                         }
                   4997:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
                   4998:                                         $digit = $values[$i]-1;
                   4999:                                         if ($values[$i] eq '0') {
                   5000:                                             $digit += $numletts;
                   5001:                                         }
                   5002:                                     }
                   5003:                                     my $qval='';
                   5004:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
                   5005:                                         if ($j == $digit) {
                   5006:                                             $qval .= $scantronconf{'Qon'};
                   5007:                                         } else {
                   5008:                                             $qval .= $scantronconf{'Qoff'};
                   5009:                                         }
                   5010:                                     }
                   5011:                                     $values[$i] = $qval;
                   5012:                                 }
                   5013:                             }
                   5014:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
                   5015:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
                   5016:                             }
                   5017:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
                   5018:                             if ($numblank > 0) {
                   5019:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
                   5020:                             }
1.1401    raeburn  5021:                             if ($csvbynum{$i} eq 'FirstQuestion') {
                   5022:                                 $qstart = $i;
1.1403    raeburn  5023:                                 $found{$csvbynum{$i}} = $values[$i];
1.1401    raeburn  5024:                             } else {
1.1403    raeburn  5025:                                 $found{'FirstQuestion'} .= $values[$i];
                   5026:                             }
                   5027:                         } elsif (exists($csvbynum{$i})) {
1.1404    raeburn  5028:                             if ($csvoptions{'rem'}) {
                   5029:                                 $values[$i] =~ s/^\s+//;
                   5030:                             }
                   5031:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
1.1403    raeburn  5032:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
                   5033:                                     $values[$i] = '0'.$values[$i];
1.1401    raeburn  5034:                                 }
                   5035:                             }
                   5036:                             $found{$csvbynum{$i}} = $values[$i];
                   5037:                         }
                   5038:                     }
                   5039:                     foreach my $item (@ordered) {
                   5040:                         my $currlength = 1+length($record);
                   5041:                         my $numspaces = $scantronconf{$item} - $currlength;
                   5042:                         if ($numspaces > 0) {
                   5043:                             $record .= (' ' x $numspaces);
                   5044:                         }
                   5045:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
                   5046:                             unless ($item eq 'Qstart') {
                   5047:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
                   5048:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
                   5049:                                 }
                   5050:                             }
                   5051:                             $record .= $found{$mapstart{$item}};
                   5052:                         }
                   5053:                     }
                   5054:                     $output .= "$record\n";
                   5055:                 }
                   5056:                 close($fh);
                   5057:                 if ($output) {
                   5058:                     if (open(my $fh,'>',$fullpath)) {
                   5059:                         print $fh $output;
                   5060:                         close($fh);
                   5061:                     }
                   5062:                 }
                   5063:             }
                   5064:         }
                   5065:         return;
                   5066:     }
                   5067: }
                   5068: 
1.1403    raeburn  5069: sub letter_to_digits {
                   5070:     my %lettdig = (
                   5071:                     A => 1,
                   5072:                     B => 2,
                   5073:                     C => 3,
                   5074:                     D => 4,
                   5075:                     E => 5,
                   5076:                     F => 6,
                   5077:                     G => 7,
                   5078:                     H => 8,
                   5079:                     I => 9,
                   5080:                     J => 0,
                   5081:                   );
                   5082:     return %lettdig;
                   5083: }
                   5084: 
1.1401    raeburn  5085: sub get_scantron_config {
                   5086:     my ($which,$cdom) = @_;
                   5087:     my @lines = &get_scantronformat_file($cdom);
                   5088:     my %config;
                   5089:     #FIXME probably should move to XML it has already gotten a bit much now
                   5090:     foreach my $line (@lines) {
                   5091:         my ($name,$descrip)=split(/:/,$line);
                   5092:         if ($name ne $which ) { next; }
                   5093:         chomp($line);
                   5094:         my @config=split(/:/,$line);
                   5095:         $config{'name'}=$config[0];
                   5096:         $config{'description'}=$config[1];
                   5097:         $config{'CODElocation'}=$config[2];
                   5098:         $config{'CODEstart'}=$config[3];
                   5099:         $config{'CODElength'}=$config[4];
                   5100:         $config{'IDstart'}=$config[5];
                   5101:         $config{'IDlength'}=$config[6];
                   5102:         $config{'Qstart'}=$config[7];
                   5103:         $config{'Qlength'}=$config[8];
                   5104:         $config{'Qoff'}=$config[9];
                   5105:         $config{'Qon'}=$config[10];
                   5106:         $config{'PaperID'}=$config[11];
                   5107:         $config{'PaperIDlength'}=$config[12];
                   5108:         $config{'FirstName'}=$config[13];
                   5109:         $config{'FirstNamelength'}=$config[14];
                   5110:         $config{'LastName'}=$config[15];
                   5111:         $config{'LastNamelength'}=$config[16];
                   5112:         $config{'BubblesPerRow'}=$config[17];
                   5113:         last;
                   5114:     }
                   5115:     return %config;
                   5116: }
                   5117: 
                   5118: sub get_scantronformat_file {
                   5119:     my ($cdom) = @_;
                   5120:     if ($cdom eq '') {
                   5121:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5122:     }
                   5123:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
                   5124:     my $gottab = 0;
                   5125:     my @lines;
                   5126:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   5127:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   5128:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
                   5129:             if ($formatfile ne '-1') {
                   5130:                 @lines = split("\n",$formatfile,-1);
                   5131:                 $gottab = 1;
                   5132:             }
                   5133:         }
                   5134:     }
                   5135:     if (!$gottab) {
                   5136:         my $confname = $cdom.'-domainconfig';
                   5137:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
                   5138:         my $formatfile = &getfile($default);
                   5139:         if ($formatfile ne '-1') {
                   5140:             @lines = split("\n",$formatfile,-1);
                   5141:             $gottab = 1;
                   5142:         }
                   5143:     }
                   5144:     if (!$gottab) {
                   5145:         my @domains = &current_machine_domains();
                   5146:         if (grep(/^\Q$cdom\E$/,@domains)) {
                   5147:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
                   5148:                 @lines = <$fh>;
                   5149:                 close($fh);
1.1403    raeburn  5150:             }
1.1401    raeburn  5151:         } else {
                   5152:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
                   5153:                 @lines = <$fh>;
                   5154:                 close($fh);
                   5155:             }
                   5156:         }
1.1488    raeburn  5157:         chomp(@lines);
1.1401    raeburn  5158:     }
                   5159:     return @lines;
                   5160: }
                   5161: 
1.493     albertel 5162: sub removeuploadedurl {
1.984     neumanie 5163:     my ($url)=@_;	
                   5164:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 5165:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 5166: }
                   5167: 
                   5168: sub removeuserfile {
                   5169:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 5170:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  5171:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 5172:     if ($result eq 'ok') {	
1.798     raeburn  5173:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   5174:             my $metafile = $fname.'.meta';
                   5175:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 5176: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 5177:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  5178:             my $sqlresult = 
1.823     albertel 5179:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  5180:                                         'portfolio_metadata',$group,
                   5181:                                         'delete');
1.798     raeburn  5182:         }
                   5183:     }
                   5184:     return $result;
1.257     www      5185: }
1.15      www      5186: 
1.530     albertel 5187: sub mkdiruserfile {
                   5188:     my ($docuname,$docudom,$dir)=@_;
                   5189:     my $home=&homeserver($docuname,$docudom);
                   5190:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   5191: }
                   5192: 
1.531     albertel 5193: sub renameuserfile {
                   5194:     my ($docuname,$docudom,$old,$new)=@_;
                   5195:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  5196:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   5197:                         &escape("$old").':'.&escape("$new"),$home);
                   5198:     if ($result eq 'ok') {
                   5199:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   5200:             my $oldmeta = $old.'.meta';
                   5201:             my $newmeta = $new.'.meta';
                   5202:             my $metaresult = 
                   5203:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 5204: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   5205:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  5206:             my $sqlresult = 
1.823     albertel 5207:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  5208:                                         'portfolio_metadata',$group,
                   5209:                                         'delete');
1.798     raeburn  5210:         }
                   5211:     }
                   5212:     return $result;
1.531     albertel 5213: }
                   5214: 
1.14      www      5215: # ------------------------------------------------------------------------- Log
                   5216: 
                   5217: sub log {
                   5218:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      5219:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      5220: }
                   5221: 
                   5222: # ------------------------------------------------------------------ Course Log
1.352     www      5223: #
                   5224: # This routine flushes several buffers of non-mission-critical nature
                   5225: #
1.157     www      5226: 
                   5227: sub flushcourselogs {
1.352     www      5228:     &logthis('Flushing log buffers');
                   5229: #
                   5230: # course logs
                   5231: # This is a log of all transactions in a course, which can be used
                   5232: # for data mining purposes
                   5233: #
                   5234: # It also collects the courseid database, which lists last transaction
                   5235: # times and course titles for all courseids
                   5236: #
                   5237:     my %courseidbuffer=();
1.921     raeburn  5238:     foreach my $crsid (keys(%courselogs)) {
1.352     www      5239:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      5240: 		          &escape($courselogs{$crsid}),
                   5241: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      5242: 	    delete $courselogs{$crsid};
                   5243:         } else {
                   5244:             &logthis('Failed to flush log buffer for '.$crsid);
                   5245:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 5246:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      5247:                         " exceeded maximum size, deleting.</font>");
                   5248:                delete $courselogs{$crsid};
                   5249:             }
1.352     www      5250:         }
1.920     raeburn  5251:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  5252:             'description' => $coursedescrbuf{$crsid},
                   5253:             'inst_code'    => $courseinstcodebuf{$crsid},
                   5254:             'type'        => $coursetypebuf{$crsid},
                   5255:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  5256:         };
1.191     harris41 5257:     }
1.352     www      5258: #
                   5259: # Write course id database (reverse lookup) to homeserver of courses 
                   5260: # Is used in pickcourse
                   5261: #
1.840     albertel 5262:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  5263:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  5264:                                     $courseidbuffer{$crs_home},
                   5265:                                     $crs_home,'timeonly');
1.352     www      5266:     }
                   5267: #
                   5268: # File accesses
                   5269: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   5270: #
1.449     matthew  5271:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  5272:         if ($entry =~ /___count$/) {
                   5273:             my ($dom,$name);
1.807     albertel 5274:             ($dom,$name,undef)=
1.811     albertel 5275: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  5276:             if (! defined($dom) || $dom eq '' || 
                   5277:                 ! defined($name) || $name eq '') {
1.620     albertel 5278:                 my $cid = $env{'request.course.id'};
1.1472    raeburn  5279: #
                   5280: # FIXME 11/29/2021
                   5281: # Typo in rev. 1.458 (2003/12/09)??
                   5282: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
                   5283: #
1.1509    raeburn  5284: # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
1.1472    raeburn  5285: # $dom and $name will always be null, so the &inc() call will default to storing this data
                   5286: # in a nohist_accesscount.db file for the user rather than the course.
                   5287: #
                   5288: # That said there is a lot of noise in the data being stored.
                   5289: # So counts for prtspool/  and adm/ etc. are recorded.
                   5290: #
                   5291: # A review of which items ending '___count' are written to %accesshash should likely be 
                   5292: # made before deciding whether to set these to 'course.' instead of 'request.'
                   5293: #
                   5294: # Under the current scheme each user receives a nohist_accesscount.db file listing 
                   5295: # accesses for things which are not published resources, regardless of course, and
                   5296: # there is not a nohist_accesscount.db file in a course, which might log accesses from
                   5297: # anyone in the course for things which are not published resources.
                   5298: #
                   5299: # For an author, nohist_accesscount.db ends up having records for other items
                   5300: # mixed up with the legitimate access counts for the author's published resources.
                   5301: #
1.620     albertel 5302:                 $dom  = $env{'request.'.$cid.'.domain'};
                   5303:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  5304:             }
1.450     matthew  5305:             my $value = $accesshash{$entry};
                   5306:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   5307:             my %temphash=($url => $value);
1.449     matthew  5308:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   5309:             if ($result eq 'ok') {
                   5310:                 delete $accesshash{$entry};
                   5311:             }
                   5312:         } else {
1.811     albertel 5313:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      5314:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  5315:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  5316:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   5317:                 delete $accesshash{$entry};
                   5318:             }
1.185     www      5319:         }
1.191     harris41 5320:     }
1.352     www      5321: #
                   5322: # Roles
                   5323: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   5324: #
1.800     albertel 5325:     foreach my $entry (keys(%userrolehash)) {
1.351     www      5326:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      5327: 	    split(/\:/,$entry);
1.1494    raeburn  5328:         if (&put('nohist_userroles',
1.351     www      5329:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      5330:                 $rudom,$runame) eq 'ok') {
                   5331: 	    delete $userrolehash{$entry};
                   5332:         }
                   5333:     }
1.662     raeburn  5334: #
1.1334    raeburn  5335: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662     raeburn  5336: #
                   5337:     my %domrolebuffer = ();
1.1000    raeburn  5338:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 5339:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  5340:         if ($domrolebuffer{$rudom}) {
                   5341:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   5342:                       '='.&escape($domainrolehash{$entry});
                   5343:         } else {
                   5344:             $domrolebuffer{$rudom}.=&escape($entry).
                   5345:                       '='.&escape($domainrolehash{$entry});
                   5346:         }
                   5347:         delete $domainrolehash{$entry};
                   5348:     }
                   5349:     foreach my $dom (keys(%domrolebuffer)) {
1.1324    raeburn  5350: 	my %servers;
                   5351: 	if (defined(&domain($dom,'primary'))) {
                   5352: 	    my $primary=&domain($dom,'primary');
                   5353: 	    my $hostname=&hostname($primary);
                   5354: 	    $servers{$primary} = $hostname;
                   5355: 	} else { 
                   5356: 	    %servers = &get_servers($dom,'library');
                   5357: 	}
1.841     albertel 5358: 	foreach my $tryserver (keys(%servers)) {
1.1324    raeburn  5359: 	    if (&reply('domroleput:'.$dom.':'.
                   5360: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   5361: 		last;
                   5362: 	    } else {  
1.841     albertel 5363: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   5364: 	    }
1.662     raeburn  5365:         }
                   5366:     }
1.186     www      5367:     $dumpcount++;
1.157     www      5368: }
                   5369: 
                   5370: sub courselog {
                   5371:     my $what=shift;
1.158     www      5372:     $what=time.':'.$what;
1.620     albertel 5373:     unless ($env{'request.course.id'}) { return ''; }
                   5374:     $coursedombuf{$env{'request.course.id'}}=
                   5375:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5376:     $coursenumbuf{$env{'request.course.id'}}=
                   5377:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   5378:     $coursehombuf{$env{'request.course.id'}}=
                   5379:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   5380:     $coursedescrbuf{$env{'request.course.id'}}=
                   5381:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   5382:     $courseinstcodebuf{$env{'request.course.id'}}=
                   5383:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   5384:     $courseownerbuf{$env{'request.course.id'}}=
                   5385:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  5386:     $coursetypebuf{$env{'request.course.id'}}=
                   5387:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 5388:     if (defined $courselogs{$env{'request.course.id'}}) {
                   5389: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      5390:     } else {
1.620     albertel 5391: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      5392:     }
1.620     albertel 5393:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      5394: 	&flushcourselogs();
                   5395:     }
1.158     www      5396: }
                   5397: 
                   5398: sub courseacclog {
                   5399:     my $fnsymb=shift;
1.620     albertel 5400:     unless ($env{'request.course.id'}) { return ''; }
                   5401:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      5402:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      5403:         $what.=':POST';
1.583     matthew  5404:         # FIXME: Probably ought to escape things....
1.800     albertel 5405: 	foreach my $key (keys(%env)) {
                   5406:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  5407:                 my $formitem = $1;
                   5408:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   5409:                     $what.=':'.$formitem.'='.$env{$key};
                   5410:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1432    raeburn  5411:                     if ($formitem eq 'proctorpassword') {
1.1433    raeburn  5412:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1432    raeburn  5413:                     } else {
                   5414:                         $what.=':'.$formitem.'='.$env{$key};
                   5415:                     }
1.975     raeburn  5416:                 }
1.158     www      5417:             }
1.191     harris41 5418:         }
1.583     matthew  5419:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   5420:         # FIXME: We should not be depending on a form parameter that someone
                   5421:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 5422:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  5423:             $what.= ':POST';
                   5424:             # FIXME: Probably ought to escape things....
                   5425:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   5426:                                  'crsdiscuss') {
1.620     albertel 5427:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  5428:             }
                   5429:         }
1.158     www      5430:     }
                   5431:     &courselog($what);
1.149     www      5432: }
                   5433: 
1.185     www      5434: sub countacc {
                   5435:     my $url=&declutter(shift);
1.458     matthew  5436:     return if (! defined($url) || $url eq '');
1.620     albertel 5437:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      5438: #
                   5439: # Mark that this url was used in this course
                   5440: #
1.620     albertel 5441:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      5442: #
                   5443: # Increase the access count for this resource in this child process
                   5444: #
1.281     www      5445:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  5446:     $accesshash{$key}++;
1.185     www      5447: }
1.349     www      5448: 
1.361     www      5449: sub linklog {
                   5450:     my ($from,$to)=@_;
                   5451:     $from=&declutter($from);
                   5452:     $to=&declutter($to);
                   5453:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   5454:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   5455: }
1.1160    www      5456: 
                   5457: sub statslog {
                   5458:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   5459:     if ($users<2) { return; }
                   5460:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   5461:             'course'       => $env{'request.course.id'},
                   5462:             'sections'     => '"all"',
                   5463:             'num_students' => $users,
                   5464:             'part'         => $part,
                   5465:             'symb'         => $symb,
                   5466:             'mean_tries'   => $av_attempts,
                   5467:             'deg_of_diff'  => $degdiff});
                   5468:     foreach my $key (keys(%dynstore)) {
                   5469:         $accesshash{$key}=$dynstore{$key};
                   5470:     }
                   5471: }
1.361     www      5472:   
1.349     www      5473: sub userrolelog {
                   5474:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 5475:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      5476:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5477:        $userrolehash
                   5478:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      5479:                     =$tend.':'.$tstart;
1.662     raeburn  5480:     }
1.1169    droeschl 5481:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 5482:        $userrolehash
                   5483:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   5484:                     =$tend.':'.$tstart;
                   5485:     }
1.1334    raeburn  5486:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662     raeburn  5487:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5488:        $domainrolehash
                   5489:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   5490:                     = $tend.':'.$tstart;
                   5491:     }
1.351     www      5492: }
                   5493: 
1.957     raeburn  5494: sub courserolelog {
1.1512    raeburn  5495:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,
                   5496:         $context,$othdomby,$requester)=@_;
1.1184    raeburn  5497:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   5498:         my $cdom = $1;
                   5499:         my $cnum = $2;
                   5500:         my $sec = $3;
                   5501:         my $namespace = 'rolelog';
                   5502:         my %storehash = (
                   5503:                            role    => $trole,
                   5504:                            start   => $tstart,
                   5505:                            end     => $tend,
                   5506:                            selfenroll => $selfenroll,
                   5507:                            context    => $context,
                   5508:                         );
1.1512    raeburn  5509:         if ($othdomby) {
                   5510:             if ($othdomby eq 'othdombydc') {
                   5511:                 $storehash{'approval'} = 'domain';
                   5512:             } elsif ($othdomby eq 'othdombyuser') {
                   5513:                 $storehash{'approval'} = 'user'; 
                   5514:             }
                   5515:             if ($requester ne '') {
                   5516:                 $storehash{'requester'} = $requester;
                   5517:             }
                   5518:         }
1.1184    raeburn  5519:         if ($trole eq 'gr') {
                   5520:             $namespace = 'groupslog';
                   5521:             $storehash{'group'} = $sec;
                   5522:         } else {
                   5523:             $storehash{'section'} = $sec;
1.1516    raeburn  5524:             my ($curruserdomstr,$newuserdomstr);
1.1515    raeburn  5525:             if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'})) {
                   5526:                 $curruserdomstr = $env{'course.'.$env{'request.course.id'}.'.internal.userdomains'};
1.1516    raeburn  5527:             } else {
1.1515    raeburn  5528:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
                   5529:                 $curruserdomstr = $courseinfo{'internal.userdomains'};
                   5530:             }
1.1516    raeburn  5531:             if ($curruserdomstr ne '') {
                   5532:                 my @udoms = split(/,/,$curruserdomstr);
1.1515    raeburn  5533:                 unless (grep(/^\Q$domain\E/,@udoms)) {
                   5534:                     push(@udoms,$domain);
                   5535:                     $newuserdomstr = join(',',sort(@udoms));
                   5536:                 }
                   5537:             } else {
                   5538:                 $newuserdomstr = $domain;
                   5539:             }
                   5540:             if ($newuserdomstr ne '') {
                   5541:                 my $putresult = &put('environment',{ 'internal.userdomains' => $newuserdomstr },
                   5542:                                      $cdom,$cnum);
                   5543:                 if ($putresult eq 'ok') {
                   5544:                     unless (($selfenroll) || ($context eq 'selfenroll')) { 
                   5545:                         if (($context eq 'createcourse') || ($context eq 'requestcourses') ||
                   5546:                             ($context eq 'automated') || ($context eq 'domain')) {
                   5547:                             $env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'} = $newuserdomstr;
                   5548:                         } elsif ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                   5549:                             &appenv({'course.'.$cdom.'_'.$cnum.'.internal.userdomains' => $newuserdomstr});
                   5550:                         }
                   5551:                     }
                   5552:                 }
                   5553:             }
1.1184    raeburn  5554:         }
1.1188    raeburn  5555:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   5556:                    $domain,$cnum,$cdom);
1.1184    raeburn  5557:         if (($trole ne 'st') || ($sec ne '')) {
                   5558:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  5559:         }
                   5560:     }
                   5561:     return;
                   5562: }
                   5563: 
1.1184    raeburn  5564: sub domainrolelog {
1.1512    raeburn  5565:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
                   5566:         $context,$othdomby,$requester)=@_;
1.1184    raeburn  5567:     if ($area =~ m{^/($match_domain)/$}) {
                   5568:         my $cdom = $1;
1.1494    raeburn  5569:         my $domconfiguser = &get_domainconfiguser($cdom);
1.1184    raeburn  5570:         my $namespace = 'rolelog';
                   5571:         my %storehash = (
                   5572:                            role    => $trole,
                   5573:                            start   => $tstart,
                   5574:                            end     => $tend,
                   5575:                            context => $context,
                   5576:                         );
1.1512    raeburn  5577:         if ($othdomby) {
                   5578:             if ($othdomby eq 'othdombydc') {
                   5579:                 $storehash{'approval'} = 'domain';
                   5580:             } elsif ($othdomby eq 'othdombyuser') {
                   5581:                 $storehash{'approval'} = 'user';
                   5582:             }
                   5583:             if ($requester ne '') {
                   5584:                 $storehash{'requester'} = $requester;
                   5585:             }
                   5586:         }
1.1188    raeburn  5587:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   5588:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  5589:     }
                   5590:     return;
                   5591: 
                   5592: }
                   5593: 
                   5594: sub coauthorrolelog {
1.1512    raeburn  5595:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
                   5596:         $context,$othdomby,$requester)=@_;
1.1184    raeburn  5597:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   5598:         my $audom = $1;
                   5599:         my $auname = $2;
                   5600:         my $namespace = 'rolelog';
                   5601:         my %storehash = (
                   5602:                            role    => $trole,
                   5603:                            start   => $tstart,
                   5604:                            end     => $tend,
                   5605:                            context => $context,
                   5606:                         );
1.1512    raeburn  5607:         if ($othdomby) {
                   5608:             if ($othdomby eq 'othdombydc') {
                   5609:                 $storehash{'approval'} = 'domain';
                   5610:             } elsif ($othdomby eq 'othdombyuser') {
                   5611:                 $storehash{'approval'} = 'user';
                   5612:             }
                   5613:             if ($requester ne '') {
                   5614:                 $storehash{'requester'} = $requester;
                   5615:             }
                   5616:         }
1.1188    raeburn  5617:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   5618:                    $domain,$auname,$audom);
1.1184    raeburn  5619:     }
                   5620:     return;
                   5621: }
                   5622: 
1.351     www      5623: sub get_course_adv_roles {
1.948     raeburn  5624:     my ($cid,$codes) = @_;
1.620     albertel 5625:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      5626:     my %coursehash=&coursedescription($cid);
1.988     raeburn  5627:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      5628:     my %nothide=();
1.800     albertel 5629:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  5630:         if ($user !~ /:/) {
                   5631: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   5632:         } else {
                   5633:             $nothide{$user}=1;
                   5634:         }
1.470     www      5635:     }
1.1219    raeburn  5636:     my @possdoms = ($coursehash{'domain'});
                   5637:     if ($coursehash{'checkforpriv'}) {
                   5638:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   5639:     }
1.351     www      5640:     my %returnhash=();
                   5641:     my %dumphash=
                   5642:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   5643:     my $now=time;
1.997     raeburn  5644:     my %privileged;
1.1000    raeburn  5645:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 5646: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      5647:         if (($tstart) && ($tstart<0)) { next; }
                   5648:         if (($tend) && ($tend<$now)) { next; }
                   5649:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 5650:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 5651: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  5652:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  5653:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 5654: 	if ($role eq 'cr') { next; }
1.948     raeburn  5655:         if ($codes) {
                   5656:             if ($section) { $role .= ':'.$section; }
                   5657:             if ($returnhash{$role}) {
                   5658:                 $returnhash{$role}.=','.$username.':'.$domain;
                   5659:             } else {
                   5660:                 $returnhash{$role}=$username.':'.$domain;
                   5661:             }
1.351     www      5662:         } else {
1.988     raeburn  5663:             my $key=&plaintext($role,$crstype);
1.973     bisitz   5664:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  5665:             if ($returnhash{$key}) {
                   5666: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   5667:             } else {
                   5668:                 $returnhash{$key}=$username.':'.$domain;
                   5669:             }
1.351     www      5670:         }
1.948     raeburn  5671:     }
1.400     www      5672:     return %returnhash;
                   5673: }
                   5674: 
                   5675: sub get_my_roles {
1.937     raeburn  5676:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 5677:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   5678:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  5679:     my (%dumphash,%nothide);
1.1086    raeburn  5680:     if ($context eq 'userroles') {
1.1166    raeburn  5681:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  5682:     } else {
1.1219    raeburn  5683:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  5684:         if ($hidepriv) {
                   5685:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   5686:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   5687:                 if ($user !~ /:/) {
                   5688:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   5689:                 } else {
                   5690:                     $nothide{$user} = 1;
                   5691:                 }
                   5692:             }
                   5693:         }
1.858     raeburn  5694:     }
1.400     www      5695:     my %returnhash=();
                   5696:     my $now=time;
1.999     raeburn  5697:     my %privileged;
1.800     albertel 5698:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  5699:         my ($role,$tend,$tstart);
                   5700:         if ($context eq 'userroles') {
1.1149    raeburn  5701:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  5702: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   5703:         } else {
                   5704:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   5705:         }
1.400     www      5706:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  5707:         my $status = 'active';
1.939     raeburn  5708:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  5709:             $status = 'previous';
                   5710:         } 
                   5711:         if (($tstart) && ($now<$tstart)) {
                   5712:             $status = 'future';
                   5713:         }
                   5714:         if (ref($types) eq 'ARRAY') {
                   5715:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   5716:                 next;
                   5717:             } 
                   5718:         } else {
                   5719:             if ($status ne 'active') {
                   5720:                 next;
                   5721:             }
                   5722:         }
1.867     raeburn  5723:         my ($rolecode,$username,$domain,$section,$area);
                   5724:         if ($context eq 'userroles') {
1.1186    raeburn  5725:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  5726:             (undef,$domain,$username,$section) = split(/\//,$area);
                   5727:         } else {
                   5728:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   5729:         }
1.832     raeburn  5730:         if (ref($roledoms) eq 'ARRAY') {
                   5731:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   5732:                 next;
                   5733:             }
                   5734:         }
                   5735:         if (ref($roles) eq 'ARRAY') {
                   5736:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  5737:                 if ($role =~ /^cr\//) {
                   5738:                     if (!grep(/^cr$/,@{$roles})) {
                   5739:                         next;
                   5740:                     }
1.1104    raeburn  5741:                 } elsif ($role =~ /^gr\//) {
                   5742:                     if (!grep(/^gr$/,@{$roles})) {
                   5743:                         next;
                   5744:                     }
1.922     raeburn  5745:                 } else {
                   5746:                     next;
                   5747:                 }
1.832     raeburn  5748:             }
1.867     raeburn  5749:         }
1.937     raeburn  5750:         if ($hidepriv) {
1.1219    raeburn  5751:             my @privroles = ('dc','su');
1.999     raeburn  5752:             if ($context eq 'userroles') {
1.1219    raeburn  5753:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  5754:             } else {
1.1219    raeburn  5755:                 my $possdoms = [$domain];
                   5756:                 if (ref($roledoms) eq 'ARRAY') {
                   5757:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  5758:                 }
1.1219    raeburn  5759:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  5760:                     if (!$nothide{$username.':'.$domain}) {
                   5761:                         next;
                   5762:                     }
                   5763:                 }
1.937     raeburn  5764:             }
                   5765:         }
1.933     raeburn  5766:         if ($withsec) {
                   5767:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   5768:                 $tstart.':'.$tend;
                   5769:         } else {
                   5770:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   5771:         }
1.832     raeburn  5772:     }
1.373     www      5773:     return %returnhash;
1.399     www      5774: }
                   5775: 
1.1333    raeburn  5776: sub get_all_adhocroles {
                   5777:     my ($dom) = @_;
                   5778:     my @roles_by_num = ();
                   5779:     my %domdefaults = &get_domain_defaults($dom);
                   5780:     my (%description,%access_in_dom,%access_info);
                   5781:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   5782:         my $count = 0;
                   5783:         my %domcurrent = %{$domdefaults{'adhocroles'}};
                   5784:         my %ordered;
                   5785:         foreach my $role (sort(keys(%domcurrent))) {
                   5786:             my ($order,$desc,$access_in_dom);
                   5787:             if (ref($domcurrent{$role}) eq 'HASH') {
                   5788:                 $order = $domcurrent{$role}{'order'};
                   5789:                 $desc = $domcurrent{$role}{'desc'};
                   5790:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
                   5791:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
                   5792:             }
                   5793:             if ($order eq '') {
                   5794:                 $order = $count;
                   5795:             }
                   5796:             $ordered{$order} = $role;
                   5797:             if ($desc ne '') {
                   5798:                 $description{$role} = $desc;
                   5799:             } else {
                   5800:                 $description{$role}= $role;
                   5801:             }
                   5802:             $count++;
                   5803:         }
                   5804:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   5805:             push(@roles_by_num,$ordered{$item});
                   5806:         }
                   5807:     }
                   5808:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
                   5809: }
                   5810: 
1.1332    raeburn  5811: sub get_my_adhocroles {
1.1333    raeburn  5812:     my ($cid,$checkreg) = @_;
                   5813:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
                   5814:     if ($env{'request.course.id'} eq $cid) {
                   5815:         $cdom = $env{'course.'.$cid.'.domain'};
                   5816:         $cnum = $env{'course.'.$cid.'.num'};
                   5817:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
                   5818:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
                   5819:         $cdom = $1;
                   5820:         $cnum = $2;
1.1494    raeburn  5821:         %info = &get('environment',['internal.coursecode'],
                   5822:                      $cdom,$cnum);
1.1333    raeburn  5823:     }
                   5824:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
                   5825:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   5826:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
                   5827:         if ($rosterhash{$user} ne '') {
                   5828:             my $type = (split(/:/,$rosterhash{$user}))[5];
                   5829:             return ([],{}) if ($type eq 'auto');
                   5830:         }
                   5831:     }
                   5832:     if (($cdom ne '') && ($cnum ne ''))  {
1.1334    raeburn  5833:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332    raeburn  5834:             my $then=$env{'user.login.time'};
                   5835:             my $update=$env{'user.update.time'};
1.1335    raeburn  5836:             if (!$update) {
                   5837:                 $update = $then;
                   5838:             }
                   5839:             my @liveroles;
1.1334    raeburn  5840:             foreach my $role ('dh','da') {
                   5841:                 if ($env{"user.role.$role./$cdom/"}) {
1.1335    raeburn  5842:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334    raeburn  5843:                     my $limit = $update;
                   5844:                     if ($env{'request.role'} eq "$role./$cdom/") {
                   5845:                         $limit = $then;
                   5846:                     }
1.1335    raeburn  5847:                     my $activerole = 1;
                   5848:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
                   5849:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
                   5850:                     if ($activerole) {
                   5851:                         push(@liveroles,$role);
                   5852:                     }
1.1334    raeburn  5853:                 }
1.1332    raeburn  5854:             }
1.1335    raeburn  5855:             if (@liveroles) {
1.1332    raeburn  5856:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333    raeburn  5857:                     my ($accessref,$accessinfo,%access_in_dom);
                   5858:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
                   5859:                     if (ref($roles_by_num) eq 'ARRAY') {
                   5860:                         if (@{$roles_by_num}) {
1.1332    raeburn  5861:                             my %settings;
                   5862:                             if ($env{'request.course.id'} eq $cid) {
                   5863:                                 foreach my $envkey (keys(%env)) {
                   5864:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
                   5865:                                         $settings{$1} = $env{$envkey};
                   5866:                                     }
                   5867:                                 }
                   5868:                             } else {
                   5869:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
                   5870:                             }
                   5871:                             my %setincrs;
                   5872:                             if ($settings{'internal.adhocaccess'}) {
                   5873:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
                   5874:                             }
                   5875:                             my @statuses;
                   5876:                             if ($env{'environment.inststatus'}) {
                   5877:                                 @statuses = split(/,/,$env{'environment.inststatus'});
                   5878:                             }
                   5879:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333    raeburn  5880:                             if (ref($accessref) eq 'HASH') {
                   5881:                                 %access_in_dom = %{$accessref};
                   5882:                             }
                   5883:                             foreach my $role (@{$roles_by_num}) {
1.1332    raeburn  5884:                                 my ($curraccess,@okstatus,@personnel);
                   5885:                                 if ($setincrs{$role}) {
                   5886:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
                   5887:                                     if ($curraccess eq 'status') {
                   5888:                                         @okstatus = split(/\&/,$rest);
                   5889:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5890:                                         @personnel = split(/\&/,$rest);
                   5891:                                     }
                   5892:                                 } else {
                   5893:                                     $curraccess = $access_in_dom{$role};
1.1333    raeburn  5894:                                     if (ref($accessinfo) eq 'HASH') {
                   5895:                                         if ($curraccess eq 'status') {
                   5896:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5897:                                                 @okstatus = @{$accessinfo->{$role}};
                   5898:                                             }
                   5899:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5900:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5901:                                                 @personnel = @{$accessinfo->{$role}};
                   5902:                                             }
1.1332    raeburn  5903:                                         }
                   5904:                                     }
                   5905:                                 }
                   5906:                                 if ($curraccess eq 'none') {
                   5907:                                     next;
                   5908:                                 } elsif ($curraccess eq 'all') {
                   5909:                                     push(@possroles,$role);
1.1336    raeburn  5910:                                 } elsif ($curraccess eq 'dh') {
1.1335    raeburn  5911:                                     if (grep(/^dh$/,@liveroles)) {
                   5912:                                         push(@possroles,$role);
                   5913:                                     } else {
                   5914:                                         next;
                   5915:                                     }
1.1336    raeburn  5916:                                 } elsif ($curraccess eq 'da') {
1.1335    raeburn  5917:                                     if (grep(/^da$/,@liveroles)) {
                   5918:                                         push(@possroles,$role);
                   5919:                                     } else {
                   5920:                                         next;
                   5921:                                     }
1.1332    raeburn  5922:                                 } elsif ($curraccess eq 'status') {
                   5923:                                     if (@okstatus) {
                   5924:                                         if (!@statuses) {
                   5925:                                             if (grep(/^default$/,@okstatus)) {
                   5926:                                                 push(@possroles,$role);
                   5927:                                             }
                   5928:                                         } else {
                   5929:                                             foreach my $status (@okstatus) {
                   5930:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
                   5931:                                                     push(@possroles,$role);
                   5932:                                                     last;
                   5933:                                                 }
                   5934:                                             }
                   5935:                                         }
                   5936:                                     }
                   5937:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5938:                                     if (grep(/^\Q$user\E$/,@personnel)) {
                   5939:                                         if ($curraccess eq 'exc') {
                   5940:                                             push(@possroles,$role);
                   5941:                                         }
                   5942:                                     } elsif ($curraccess eq 'inc') {
                   5943:                                         push(@possroles,$role);
                   5944:                                     }
                   5945:                                 }
                   5946:                             }
                   5947:                         }
                   5948:                     }
                   5949:                 }
                   5950:             }
                   5951:         }
                   5952:     }
1.1333    raeburn  5953:     unless (ref($description) eq 'HASH') {
                   5954:         if (ref($roles_by_num) eq 'ARRAY') {
                   5955:             my %desc;
                   5956:             map { $desc{$_} = $_; } (@{$roles_by_num});
                   5957:             $description = \%desc;
                   5958:         } else {
                   5959:             $description = {};
                   5960:         }
                   5961:     }
                   5962:     return (\@possroles,$description);
1.1332    raeburn  5963: }
                   5964: 
1.399     www      5965: # ----------------------------------------------------- Frontpage Announcements
                   5966: #
                   5967: #
                   5968: 
                   5969: sub postannounce {
                   5970:     my ($server,$text)=@_;
1.844     albertel 5971:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      5972:     unless ($text=~/\w/) { $text=''; }
                   5973:     return &reply('setannounce:'.&escape($text),$server);
                   5974: }
                   5975: 
                   5976: sub getannounce {
1.448     albertel 5977: 
1.1359    raeburn  5978:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      5979: 	my $announcement='';
1.800     albertel 5980: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 5981: 	close($fh);
1.399     www      5982: 	if ($announcement=~/\w/) { 
                   5983: 	    return 
                   5984:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 5985:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      5986: 	} else {
                   5987: 	    return '';
                   5988: 	}
                   5989:     } else {
                   5990: 	return '';
                   5991:     }
1.351     www      5992: }
1.353     www      5993: 
                   5994: # ---------------------------------------------------------- Course ID routines
                   5995: # Deal with domain's nohist_courseid.db files
                   5996: #
                   5997: 
                   5998: sub courseidput {
1.921     raeburn  5999:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  6000:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  6001:     my $outcome;
                   6002:     if ($caller eq 'timeonly') {
                   6003:         my $cids = '';
                   6004:         foreach my $item (keys(%$storehash)) {
                   6005:             $cids.=&escape($item).'&';
                   6006:         }
                   6007:         $cids=~s/\&$//;
                   6008:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   6009:                           $coursehome);       
                   6010:     } else {
                   6011:         my $items = '';
                   6012:         foreach my $item (keys(%$storehash)) {
                   6013:             $items.= &escape($item).'='.
                   6014:                      &freeze_escape($$storehash{$item}).'&';
                   6015:         }
                   6016:         $items=~s/\&$//;
                   6017:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   6018:                           $coursehome);
1.918     raeburn  6019:     }
                   6020:     if ($outcome eq 'unknown_cmd') {
                   6021:         my $what;
                   6022:         foreach my $cid (keys(%$storehash)) {
                   6023:             $what .= &escape($cid).'=';
1.921     raeburn  6024:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  6025:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  6026:             }
                   6027:             $what =~ s/\:$/&/;
                   6028:         }
                   6029:         $what =~ s/\&$//;  
                   6030:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   6031:     } else {
                   6032:         return $outcome;
                   6033:     }
1.353     www      6034: }
                   6035: 
                   6036: sub courseiddump {
1.921     raeburn  6037:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  6038:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  6039:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247    raeburn  6040:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287    raeburn  6041:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918     raeburn  6042:     my $as_hash = 1;
                   6043:     my %returnhash;
                   6044:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 6045:     my %libserv = &all_library();
                   6046:     foreach my $tryserver (keys(%libserv)) {
                   6047:         if ( (  $hostidflag == 1 
                   6048: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   6049: 	     || (!defined($hostidflag)) ) {
                   6050: 
1.918     raeburn  6051: 	    if (($domfilter eq '') ||
                   6052: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 6053:                 my $rep;
                   6054:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   6055:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   6056:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   6057:                                 &escape($descfilter), &escape($instcodefilter), 
                   6058:                                 &escape($ownerfilter), &escape($coursefilter),
                   6059:                                 &escape($typefilter), &escape($regexp_ok), 
                   6060:                                 $as_hash, &escape($selfenrollonly), 
                   6061:                                 &escape($catfilter), $showhidden, $caller, 
                   6062:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   6063:                                 &escape($createdbefore), &escape($createdafter), 
1.1287    raeburn  6064:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
                   6065:                                 $reqcrsdom,&escape($reqinstcode))));
1.1180    droeschl 6066:                 } else {
                   6067:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   6068:                              $sincefilter.':'.&escape($descfilter).':'.
                   6069:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   6070:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   6071:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   6072:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   6073:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   6074:                              &escape($cc_clone).':'.$cloneonly.':'.
                   6075:                              &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287    raeburn  6076:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
                   6077:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180    droeschl 6078:                 }
                   6079:                      
1.918     raeburn  6080:                 my @pairs=split(/\&/,$rep);
                   6081:                 foreach my $item (@pairs) {
                   6082:                     my ($key,$value)=split(/\=/,$item,2);
                   6083:                     $key = &unescape($key);
                   6084:                     next if ($key =~ /^error: 2 /);
                   6085:                     my $result = &thaw_unescape($value);
                   6086:                     if (ref($result) eq 'HASH') {
                   6087:                         $returnhash{$key}=$result;
                   6088:                     } else {
1.921     raeburn  6089:                         my @responses = split(/:/,$value);
                   6090:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  6091:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  6092:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  6093:                         }
1.1008    raeburn  6094:                     }
1.353     www      6095:                 }
                   6096:             }
                   6097:         }
                   6098:     }
                   6099:     return %returnhash;
                   6100: }
                   6101: 
1.1055    raeburn  6102: sub courselastaccess {
                   6103:     my ($cdom,$cnum,$hostidref) = @_;
                   6104:     my %returnhash;
                   6105:     if ($cdom && $cnum) {
                   6106:         my $chome = &homeserver($cnum,$cdom);
                   6107:         if ($chome ne 'no_host') {
                   6108:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   6109:             &extract_lastaccess(\%returnhash,$rep);
                   6110:         }
                   6111:     } else {
                   6112:         if (!$cdom) { $cdom=''; }
                   6113:         my %libserv = &all_library();
                   6114:         foreach my $tryserver (keys(%libserv)) {
                   6115:             if (ref($hostidref) eq 'ARRAY') {
                   6116:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   6117:             } 
                   6118:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   6119:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   6120:                 &extract_lastaccess(\%returnhash,$rep);
                   6121:             }
                   6122:         }
                   6123:     }
                   6124:     return %returnhash;
                   6125: }
                   6126: 
                   6127: sub extract_lastaccess {
                   6128:     my ($returnhash,$rep) = @_;
                   6129:     if (ref($returnhash) eq 'HASH') {
                   6130:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   6131:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   6132:                  $rep eq '') {
                   6133:             my @pairs=split(/\&/,$rep);
                   6134:             foreach my $item (@pairs) {
                   6135:                 my ($key,$value)=split(/\=/,$item,2);
                   6136:                 $key = &unescape($key);
                   6137:                 next if ($key =~ /^error: 2 /);
                   6138:                 $returnhash->{$key} = &thaw_unescape($value);
                   6139:             }
                   6140:         }
                   6141:     }
                   6142:     return;
                   6143: }
                   6144: 
1.658     raeburn  6145: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  6146: 
                   6147: sub dcmailput {
1.685     raeburn  6148:     my ($domain,$msgid,$message,$server)=@_;
1.1494    raeburn  6149:     my $status = &critical(
1.740     www      6150:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   6151:        &escape($message),$server);
1.662     raeburn  6152:     return $status;
                   6153: }
                   6154: 
1.658     raeburn  6155: sub dcmaildump {
                   6156:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  6157:     my %returnhash=();
1.846     albertel 6158: 
                   6159:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  6160:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   6161:                                                          &escape($enddate).':';
                   6162: 	my @esc_senders=map { &escape($_)} @$senders;
                   6163: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 6164: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 6165:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  6166:             if (($key) && ($value)) {
                   6167:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  6168:             }
                   6169:         }
                   6170:     }
                   6171:     return %returnhash;
                   6172: }
1.662     raeburn  6173: # ---------------------------------------------------------- Domain roles
                   6174: 
                   6175: sub get_domain_roles {
                   6176:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  6177:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  6178:         $startdate = '.';
                   6179:     }
1.1018    raeburn  6180:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  6181:         $enddate = '.';
                   6182:     }
1.922     raeburn  6183:     my $rolelist;
                   6184:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  6185:         $rolelist = join('&',@{$roles});
1.922     raeburn  6186:     }
1.662     raeburn  6187:     my %personnel = ();
1.841     albertel 6188: 
                   6189:     my %servers = &get_servers($dom,'library');
                   6190:     foreach my $tryserver (keys(%servers)) {
                   6191: 	%{$personnel{$tryserver}}=();
                   6192: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   6193: 					    &escape($startdate).':'.
                   6194: 					    &escape($enddate).':'.
                   6195: 					    &escape($rolelist), $tryserver))) {
                   6196: 	    my ($key,$value) = split(/\=/,$line,2);
                   6197: 	    if (($key) && ($value)) {
                   6198: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   6199: 	    }
                   6200: 	}
1.662     raeburn  6201:     }
                   6202:     return %personnel;
                   6203: }
1.658     raeburn  6204: 
1.1332    raeburn  6205: sub get_active_domroles {
                   6206:     my ($dom,$roles) = @_;
                   6207:     return () unless (ref($roles) eq 'ARRAY');
                   6208:     my $now = time;
                   6209:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
                   6210:     my %domroles;
                   6211:     foreach my $server (keys(%dompersonnel)) {
                   6212:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   6213:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
                   6214:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
                   6215:         }
                   6216:     }
                   6217:     return %domroles;
                   6218: }
                   6219: 
1.1057    www      6220: # ----------------------------------------------------------- Interval timing 
1.149     www      6221: 
1.1153    www      6222: {
                   6223: # Caches needed for speedup of navmaps
                   6224: # We don't want to cache this for very long at all (5 seconds at most)
                   6225: # 
                   6226: # The user for whom we cache
                   6227: my $cachedkey='';
                   6228: # The cached times for this user
                   6229: my %cachedtimes=();
                   6230: # When this was last done
1.1282    raeburn  6231: my $cachedtime='';
1.1153    www      6232: 
                   6233: sub load_all_first_access {
1.1308    raeburn  6234:     my ($uname,$udom,$ignorecache)=@_;
1.1156    www      6235:     if (($cachedkey eq $uname.':'.$udom) &&
1.1308    raeburn  6236:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
                   6237:         (!$ignorecache)) {
1.1154    raeburn  6238:         return;
                   6239:     }
                   6240:     $cachedtime=time;
                   6241:     $cachedkey=$uname.':'.$udom;
                   6242:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      6243: }
                   6244: 
1.504     albertel 6245: sub get_first_access {
1.1308    raeburn  6246:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790     albertel 6247:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 6248:     if ($argsymb) { $symb=$argsymb; }
                   6249:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  6250:     if ($argmap) { $map = $argmap; }
1.926     albertel 6251:     if ($type eq 'course') {
                   6252: 	$res='course';
                   6253:     } elsif ($type eq 'map') {
1.588     albertel 6254: 	$res=&symbread($map);
                   6255:     } else {
                   6256: 	$res=$symb;
                   6257:     }
1.1308    raeburn  6258:     &load_all_first_access($uname,$udom,$ignorecache);
1.1153    www      6259:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 6260: }
                   6261: 
                   6262: sub set_first_access {
1.1162    raeburn  6263:     my ($type,$interval)=@_;
1.790     albertel 6264:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 6265:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 6266:     if ($type eq 'course') {
                   6267: 	$res='course';
                   6268:     } elsif ($type eq 'map') {
1.588     albertel 6269: 	$res=&symbread($map);
                   6270:     } else {
                   6271: 	$res=$symb;
                   6272:     }
1.1153    www      6273:     $cachedkey='';
1.1162    raeburn  6274:     my $firstaccess=&get_first_access($type,$symb,$map);
1.1386    raeburn  6275:     if ($firstaccess) {
                   6276:         &logthis("First access time already set ($firstaccess) when attempting ".
1.1393    raeburn  6277:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
                   6278:                  "in $courseid");
1.1386    raeburn  6279:         return 'already_set';
                   6280:     } else {
1.1162    raeburn  6281:         my $start = time;
                   6282: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   6283:                           $udom,$uname);
                   6284:         if ($putres eq 'ok') {
                   6285:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   6286:                  $udom,$uname); 
                   6287:             &appenv(
                   6288:                      {
                   6289:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   6290:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   6291:                      }
                   6292:                   );
1.1365    raeburn  6293:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
                   6294:                 $cachedtimes{"$courseid\0$res"} = $start;
                   6295:             }
1.1386    raeburn  6296:         } elsif ($putres ne 'refused') {
                   6297:             &logthis("Result: $putres when attempting to set first access time ".
                   6298:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162    raeburn  6299:         }
                   6300:         return $putres;
1.505     albertel 6301:     }
                   6302:     return 'already_set';
1.504     albertel 6303: }
1.1153    www      6304: }
1.1282    raeburn  6305: 
1.110     www      6306: # --------------------------------------------- Set Expire Date for Spreadsheet
                   6307: 
                   6308: sub expirespread {
                   6309:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 6310:     my $cid=$env{'request.course.id'}; 
1.110     www      6311:     if ($cid) {
                   6312:        my $now=time;
                   6313:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 6314:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   6315:                             $env{'course.'.$cid.'.num'}.
1.110     www      6316: 	        	    ':nohist_expirationdates:'.
                   6317:                             &escape($key).'='.$now,
1.620     albertel 6318:                             $env{'course.'.$cid.'.home'})
1.110     www      6319:     }
                   6320:     return 'ok';
1.14      www      6321: }
                   6322: 
1.109     www      6323: # ----------------------------------------------------- Devalidate Spreadsheets
                   6324: 
                   6325: sub devalidate {
1.325     www      6326:     my ($symb,$uname,$udom)=@_;
1.620     albertel 6327:     my $cid=$env{'request.course.id'}; 
1.109     www      6328:     if ($cid) {
1.391     matthew  6329:         # delete the stored spreadsheets for
                   6330:         # - the student level sheet of this user in course's homespace
                   6331:         # - the assessment level sheet for this resource 
                   6332:         #   for this user in user's homespace
1.553     albertel 6333: 	# - current conditional state info
1.325     www      6334: 	my $key=$uname.':'.$udom.':';
1.109     www      6335:         my $status=
1.299     matthew  6336: 	    &del('nohist_calculatedsheets',
1.391     matthew  6337: 		 [$key.'studentcalc:'],
1.620     albertel 6338: 		 $env{'course.'.$cid.'.domain'},
                   6339: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 6340: 		.' '.
                   6341: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  6342: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      6343:         unless ($status eq 'ok ok') {
                   6344:            &logthis('Could not devalidate spreadsheet '.
1.325     www      6345:                     $uname.' at '.$udom.' for '.
1.109     www      6346: 		    $symb.': '.$status);
1.133     albertel 6347:         }
1.553     albertel 6348: 	&delenv('user.state.'.$cid);
1.109     www      6349:     }
                   6350: }
                   6351: 
1.265     albertel 6352: sub get_scalar {
                   6353:     my ($string,$end) = @_;
                   6354:     my $value;
                   6355:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   6356: 	$value = $1;
                   6357:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   6358: 	$value = $1;
                   6359:     }
                   6360:     return &unescape($value);
                   6361: }
                   6362: 
                   6363: sub array2str {
                   6364:   my (@array) = @_;
                   6365:   my $result=&arrayref2str(\@array);
                   6366:   $result=~s/^__ARRAY_REF__//;
                   6367:   $result=~s/__END_ARRAY_REF__$//;
                   6368:   return $result;
                   6369: }
                   6370: 
1.204     albertel 6371: sub arrayref2str {
                   6372:   my ($arrayref) = @_;
1.265     albertel 6373:   my $result='__ARRAY_REF__';
1.204     albertel 6374:   foreach my $elem (@$arrayref) {
1.265     albertel 6375:     if(ref($elem) eq 'ARRAY') {
                   6376:       $result.=&arrayref2str($elem).'&';
                   6377:     } elsif(ref($elem) eq 'HASH') {
                   6378:       $result.=&hashref2str($elem).'&';
                   6379:     } elsif(ref($elem)) {
                   6380:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 6381:     } else {
                   6382:       $result.=&escape($elem).'&';
                   6383:     }
                   6384:   }
                   6385:   $result=~s/\&$//;
1.265     albertel 6386:   $result .= '__END_ARRAY_REF__';
1.204     albertel 6387:   return $result;
                   6388: }
                   6389: 
1.168     albertel 6390: sub hash2str {
1.204     albertel 6391:   my (%hash) = @_;
                   6392:   my $result=&hashref2str(\%hash);
1.265     albertel 6393:   $result=~s/^__HASH_REF__//;
                   6394:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 6395:   return $result;
                   6396: }
                   6397: 
                   6398: sub hashref2str {
                   6399:   my ($hashref)=@_;
1.265     albertel 6400:   my $result='__HASH_REF__';
1.800     albertel 6401:   foreach my $key (sort(keys(%$hashref))) {
                   6402:     if (ref($key) eq 'ARRAY') {
                   6403:       $result.=&arrayref2str($key).'=';
                   6404:     } elsif (ref($key) eq 'HASH') {
                   6405:       $result.=&hashref2str($key).'=';
                   6406:     } elsif (ref($key)) {
1.265     albertel 6407:       $result.='=';
1.800     albertel 6408:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 6409:     } else {
1.1132    raeburn  6410: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 6411:     }
                   6412: 
1.800     albertel 6413:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   6414:       $result.=&arrayref2str($hashref->{$key}).'&';
                   6415:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   6416:       $result.=&hashref2str($hashref->{$key}).'&';
                   6417:     } elsif(ref($hashref->{$key})) {
1.265     albertel 6418:        $result.='&';
1.800     albertel 6419:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 6420:     } else {
1.800     albertel 6421:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 6422:     }
                   6423:   }
1.168     albertel 6424:   $result=~s/\&$//;
1.265     albertel 6425:   $result .= '__END_HASH_REF__';
1.168     albertel 6426:   return $result;
                   6427: }
                   6428: 
                   6429: sub str2hash {
1.265     albertel 6430:     my ($string)=@_;
                   6431:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   6432:     return %$hash;
                   6433: }
                   6434: 
                   6435: sub str2hashref {
1.168     albertel 6436:   my ($string) = @_;
1.265     albertel 6437: 
                   6438:   my %hash;
                   6439: 
                   6440:   if($string !~ /^__HASH_REF__/) {
                   6441:       if (! ($string eq '' || !defined($string))) {
                   6442: 	  $hash{'error'}='Not hash reference';
                   6443:       }
                   6444:       return (\%hash, $string);
                   6445:   }
                   6446: 
                   6447:   $string =~ s/^__HASH_REF__//;
                   6448: 
                   6449:   while($string !~ /^__END_HASH_REF__/) {
                   6450:       #key
                   6451:       my $key='';
                   6452:       if($string =~ /^__HASH_REF__/) {
                   6453:           ($key, $string)=&str2hashref($string);
                   6454:           if(defined($key->{'error'})) {
                   6455:               $hash{'error'}='Bad data';
                   6456:               return (\%hash, $string);
                   6457:           }
                   6458:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6459:           ($key, $string)=&str2arrayref($string);
                   6460:           if($key->[0] eq 'Array reference error') {
                   6461:               $hash{'error'}='Bad data';
                   6462:               return (\%hash, $string);
                   6463:           }
                   6464:       } else {
                   6465:           $string =~ s/^(.*?)=//;
1.267     albertel 6466: 	  $key=&unescape($1);
1.265     albertel 6467:       }
                   6468:       $string =~ s/^=//;
                   6469: 
                   6470:       #value
                   6471:       my $value='';
                   6472:       if($string =~ /^__HASH_REF__/) {
                   6473:           ($value, $string)=&str2hashref($string);
                   6474:           if(defined($value->{'error'})) {
                   6475:               $hash{'error'}='Bad data';
                   6476:               return (\%hash, $string);
                   6477:           }
                   6478:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6479:           ($value, $string)=&str2arrayref($string);
                   6480:           if($value->[0] eq 'Array reference error') {
                   6481:               $hash{'error'}='Bad data';
                   6482:               return (\%hash, $string);
                   6483:           }
                   6484:       } else {
                   6485: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   6486:       }
                   6487:       $string =~ s/^&//;
                   6488: 
                   6489:       $hash{$key}=$value;
1.204     albertel 6490:   }
1.265     albertel 6491: 
                   6492:   $string =~ s/^__END_HASH_REF__//;
                   6493: 
                   6494:   return (\%hash, $string);
1.204     albertel 6495: }
                   6496: 
                   6497: sub str2array {
1.265     albertel 6498:     my ($string)=@_;
                   6499:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   6500:     return @$array;
                   6501: }
                   6502: 
                   6503: sub str2arrayref {
1.204     albertel 6504:   my ($string) = @_;
1.265     albertel 6505:   my @array;
                   6506: 
                   6507:   if($string !~ /^__ARRAY_REF__/) {
                   6508:       if (! ($string eq '' || !defined($string))) {
                   6509: 	  $array[0]='Array reference error';
                   6510:       }
                   6511:       return (\@array, $string);
                   6512:   }
                   6513: 
                   6514:   $string =~ s/^__ARRAY_REF__//;
                   6515: 
                   6516:   while($string !~ /^__END_ARRAY_REF__/) {
                   6517:       my $value='';
                   6518:       if($string =~ /^__HASH_REF__/) {
                   6519:           ($value, $string)=&str2hashref($string);
                   6520:           if(defined($value->{'error'})) {
                   6521:               $array[0] ='Array reference error';
                   6522:               return (\@array, $string);
                   6523:           }
                   6524:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6525:           ($value, $string)=&str2arrayref($string);
                   6526:           if($value->[0] eq 'Array reference error') {
                   6527:               $array[0] ='Array reference error';
                   6528:               return (\@array, $string);
                   6529:           }
                   6530:       } else {
                   6531: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   6532:       }
                   6533:       $string =~ s/^&//;
                   6534: 
                   6535:       push(@array, $value);
1.191     harris41 6536:   }
1.265     albertel 6537: 
                   6538:   $string =~ s/^__END_ARRAY_REF__//;
                   6539: 
                   6540:   return (\@array, $string);
1.168     albertel 6541: }
                   6542: 
1.167     albertel 6543: # -------------------------------------------------------------------Temp Store
                   6544: 
1.168     albertel 6545: sub tmpreset {
                   6546:   my ($symb,$namespace,$domain,$stuname) = @_;
                   6547:   if (!$symb) {
                   6548:     $symb=&symbread();
1.620     albertel 6549:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6550:   }
                   6551:   $symb=escape($symb);
                   6552: 
1.620     albertel 6553:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 6554:   $namespace=~s/\//\_/g;
                   6555:   $namespace=~s/\W//g;
                   6556: 
1.620     albertel 6557:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6558:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6559:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6560:       $stuname=&get_requestor_ip();
1.591     albertel 6561:   }
1.1117    foxr     6562:   my $path=LONCAPA::tempdir();
1.168     albertel 6563:   my %hash;
                   6564:   if (tie(%hash,'GDBM_File',
                   6565: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6566: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  6567:     foreach my $key (keys(%hash)) {
1.180     albertel 6568:       if ($key=~ /:$symb/) {
1.168     albertel 6569: 	delete($hash{$key});
                   6570:       }
                   6571:     }
                   6572:   }
                   6573: }
                   6574: 
1.167     albertel 6575: sub tmpstore {
1.168     albertel 6576:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   6577: 
                   6578:   if (!$symb) {
                   6579:     $symb=&symbread();
1.620     albertel 6580:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6581:   }
                   6582:   $symb=escape($symb);
                   6583: 
                   6584:   if (!$namespace) {
                   6585:     # I don't think we would ever want to store this for a course.
                   6586:     # it seems this will only be used if we don't have a course.
1.620     albertel 6587:     #$namespace=$env{'request.course.id'};
1.168     albertel 6588:     #if (!$namespace) {
1.620     albertel 6589:       $namespace=$env{'request.state'};
1.168     albertel 6590:     #}
                   6591:   }
                   6592:   $namespace=~s/\//\_/g;
                   6593:   $namespace=~s/\W//g;
1.620     albertel 6594:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6595:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6596:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6597:       $stuname=&get_requestor_ip();
1.591     albertel 6598:   }
1.168     albertel 6599:   my $now=time;
                   6600:   my %hash;
1.1117    foxr     6601:   my $path=LONCAPA::tempdir();
1.168     albertel 6602:   if (tie(%hash,'GDBM_File',
                   6603: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6604: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 6605:     $hash{"version:$symb"}++;
                   6606:     my $version=$hash{"version:$symb"};
                   6607:     my $allkeys=''; 
                   6608:     foreach my $key (keys(%$storehash)) {
                   6609:       $allkeys.=$key.':';
1.591     albertel 6610:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 6611:     }
                   6612:     $hash{"$version:$symb:timestamp"}=$now;
                   6613:     $allkeys.='timestamp';
                   6614:     $hash{"$version:keys:$symb"}=$allkeys;
                   6615:     if (untie(%hash)) {
                   6616:       return 'ok';
                   6617:     } else {
                   6618:       return "error:$!";
                   6619:     }
                   6620:   } else {
                   6621:     return "error:$!";
                   6622:   }
                   6623: }
1.167     albertel 6624: 
1.168     albertel 6625: # -----------------------------------------------------------------Temp Restore
1.167     albertel 6626: 
1.168     albertel 6627: sub tmprestore {
                   6628:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 6629: 
1.168     albertel 6630:   if (!$symb) {
                   6631:     $symb=&symbread();
1.620     albertel 6632:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6633:   }
                   6634:   $symb=escape($symb);
                   6635: 
1.620     albertel 6636:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 6637: 
1.620     albertel 6638:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6639:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6640:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6641:       $stuname=&get_requestor_ip();
1.591     albertel 6642:   }
1.168     albertel 6643:   my %returnhash;
                   6644:   $namespace=~s/\//\_/g;
                   6645:   $namespace=~s/\W//g;
                   6646:   my %hash;
1.1117    foxr     6647:   my $path=LONCAPA::tempdir();
1.168     albertel 6648:   if (tie(%hash,'GDBM_File',
                   6649: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6650: 	  &GDBM_READER(),0640)) {
1.168     albertel 6651:     my $version=$hash{"version:$symb"};
                   6652:     $returnhash{'version'}=$version;
                   6653:     my $scope;
                   6654:     for ($scope=1;$scope<=$version;$scope++) {
                   6655:       my $vkeys=$hash{"$scope:keys:$symb"};
                   6656:       my @keys=split(/:/,$vkeys);
                   6657:       my $key;
                   6658:       $returnhash{"$scope:keys"}=$vkeys;
                   6659:       foreach $key (@keys) {
1.591     albertel 6660: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   6661: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 6662:       }
                   6663:     }
1.168     albertel 6664:     if (!(untie(%hash))) {
                   6665:       return "error:$!";
                   6666:     }
                   6667:   } else {
                   6668:     return "error:$!";
                   6669:   }
                   6670:   return %returnhash;
1.167     albertel 6671: }
                   6672: 
1.9       www      6673: # ----------------------------------------------------------------------- Store
                   6674: 
                   6675: sub store {
1.1269    raeburn  6676:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6677:     my $home='';
                   6678: 
1.168     albertel 6679:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6680: 
1.213     www      6681:     $symb=&symbclean($symb);
1.122     albertel 6682:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6683: 
1.620     albertel 6684:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6685:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6686: 
                   6687:     &devalidate($symb,$stuname,$domain);
1.109     www      6688: 
                   6689:     $symb=escape($symb);
1.187     www      6690:     if (!$namespace) { 
1.620     albertel 6691:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6692:           return ''; 
                   6693:        } 
                   6694:     }
1.620     albertel 6695:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6696: 
1.1434    raeburn  6697:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6698:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   6699: 
1.12      www      6700:     my $namevalue='';
1.800     albertel 6701:     foreach my $key (keys(%$storehash)) {
                   6702:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6703:     }
1.12      www      6704:     $namevalue=~s/\&$//;
1.187     www      6705:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269    raeburn  6706:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9       www      6707: }
                   6708: 
1.47      www      6709: # -------------------------------------------------------------- Critical Store
                   6710: 
                   6711: sub cstore {
1.1269    raeburn  6712:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6713:     my $home='';
                   6714: 
1.168     albertel 6715:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6716: 
1.213     www      6717:     $symb=&symbclean($symb);
1.122     albertel 6718:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6719: 
1.620     albertel 6720:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6721:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6722: 
                   6723:     &devalidate($symb,$stuname,$domain);
1.109     www      6724: 
                   6725:     $symb=escape($symb);
1.187     www      6726:     if (!$namespace) { 
1.620     albertel 6727:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6728:           return ''; 
                   6729:        } 
                   6730:     }
1.620     albertel 6731:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6732: 
1.1434    raeburn  6733:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6734:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 6735: 
1.47      www      6736:     my $namevalue='';
1.800     albertel 6737:     foreach my $key (keys(%$storehash)) {
                   6738:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6739:     }
1.47      www      6740:     $namevalue=~s/\&$//;
1.187     www      6741:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      6742:     return critical
1.1269    raeburn  6743:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47      www      6744: }
                   6745: 
1.9       www      6746: # --------------------------------------------------------------------- Restore
                   6747: 
                   6748: sub restore {
1.124     www      6749:     my ($symb,$namespace,$domain,$stuname) = @_;
                   6750:     my $home='';
                   6751: 
1.168     albertel 6752:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6753: 
1.122     albertel 6754:     if (!$symb) {
1.1224    raeburn  6755:         return if ($namespace eq 'courserequests');
                   6756:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 6757:     } else {
1.1224    raeburn  6758:         unless ($namespace eq 'courserequests') {
                   6759:             $symb=&escape(&symbclean($symb));
                   6760:         }
1.122     albertel 6761:     }
1.188     www      6762:     if (!$namespace) { 
1.620     albertel 6763:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      6764:           return ''; 
                   6765:        } 
                   6766:     }
1.620     albertel 6767:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6768:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   6769:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 6770:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   6771: 
1.12      www      6772:     my %returnhash=();
1.800     albertel 6773:     foreach my $line (split(/\&/,$answer)) {
                   6774: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 6775:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 6776:     }
1.75      www      6777:     my $version;
                   6778:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 6779:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   6780:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 6781:        }
1.75      www      6782:     }
1.13      www      6783:     return %returnhash;
1.34      www      6784: }
                   6785: 
                   6786: # ---------------------------------------------------------- Course Description
1.1118    foxr     6787: #
                   6788: #  
1.34      www      6789: 
                   6790: sub coursedescription {
1.731     albertel 6791:     my ($courseid,$args)=@_;
1.34      www      6792:     $courseid=~s/^\///;
1.49      www      6793:     $courseid=~s/\_/\//g;
1.34      www      6794:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 6795:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 6796:     my $normalid=$cdomain.'_'.$cnum;
                   6797:     # need to always cache even if we get errors otherwise we keep 
                   6798:     # trying and trying and trying to get the course description.
                   6799:     my %envhash=();
                   6800:     my %returnhash=();
1.731     albertel 6801:     
                   6802:     my $expiretime=600;
                   6803:     if ($env{'request.course.id'} eq $normalid) {
                   6804: 	$expiretime=120;
                   6805:     }
                   6806: 
                   6807:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   6808:     if (!$args->{'freshen_cache'}
                   6809: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   6810: 	foreach my $key (keys(%env)) {
                   6811: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   6812: 	    my ($setting) = $1;
                   6813: 	    $returnhash{$setting} = $env{$key};
                   6814: 	}
                   6815: 	return %returnhash;
                   6816:     }
                   6817: 
1.1118    foxr     6818:     # get the data again
                   6819: 
1.731     albertel 6820:     if (!$args->{'one_time'}) {
                   6821: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   6822:     }
1.811     albertel 6823: 
1.34      www      6824:     if ($chome ne 'no_host') {
1.302     albertel 6825:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 6826:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     6827: 	   my $username = $env{'user.name'}; # Defult username
                   6828: 	   if(defined $args->{'user'}) {
                   6829: 	       $username = $args->{'user'};
                   6830: 	   }
1.129     albertel 6831:            $returnhash{'home'}= $chome;
                   6832: 	   $returnhash{'domain'} = $cdomain;
                   6833: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  6834:            if (!defined($returnhash{'type'})) {
                   6835:                $returnhash{'type'} = 'Course';
                   6836:            }
1.130     albertel 6837:            while (my ($name,$value) = each %returnhash) {
1.53      www      6838:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 6839:            }
1.270     www      6840:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     6841:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     6842: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      6843:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   6844:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   6845:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      6846:        }
                   6847:     }
1.731     albertel 6848:     if (!$args->{'one_time'}) {
1.949     raeburn  6849: 	&appenv(\%envhash);
1.731     albertel 6850:     }
1.302     albertel 6851:     return %returnhash;
1.461     www      6852: }
                   6853: 
1.1080    raeburn  6854: sub update_released_required {
                   6855:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   6856:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   6857:         $cid = $env{'request.course.id'};
                   6858:         $cdom = $env{'course.'.$cid.'.domain'};
                   6859:         $cnum = $env{'course.'.$cid.'.num'};
                   6860:         $chome = $env{'course.'.$cid.'.home'};
                   6861:     }
                   6862:     if ($needsrelease) {
                   6863:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   6864:         my $needsupdate;
                   6865:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   6866:             $needsupdate = 1;
                   6867:         } else {
                   6868:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   6869:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   6870:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   6871:                 $needsupdate = 1;
                   6872:             }
                   6873:         }
                   6874:         if ($needsupdate) {
                   6875:             my %needshash = (
                   6876:                              'internal.releaserequired' => $needsrelease,
                   6877:                             );
                   6878:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   6879:             if ($putresult eq 'ok') {
                   6880:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   6881:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6882:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   6883:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   6884:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   6885:                 }
                   6886:             }
                   6887:         }
                   6888:     }
                   6889:     return;
                   6890: }
                   6891: 
1.461     www      6892: # -------------------------------------------------See if a user is privileged
                   6893: 
                   6894: sub privileged {
1.1219    raeburn  6895:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 6896:     my $now = time;
1.1219    raeburn  6897:     my $roles;
                   6898:     if (ref($possroles) eq 'ARRAY') {
                   6899:         $roles = $possroles; 
                   6900:     } else {
                   6901:         $roles = ['dc','su'];
                   6902:     }
                   6903:     if (ref($possdomains) eq 'ARRAY') {
                   6904:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   6905:         foreach my $dom (@{$possdomains}) {
                   6906:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   6907:                 (ref($privileged{$dom}) eq 'HASH')) {
                   6908:                 foreach my $role (@{$roles}) {
                   6909:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6910:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   6911:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   6912:                             return 1 unless (($end && $end < $now) ||
                   6913:                                              ($start && $start > $now));
                   6914:                         }
                   6915:                     }
                   6916:                 }
                   6917:             }
                   6918:         }
                   6919:     } else {
                   6920:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   6921:         my $now = time;
1.1170    droeschl 6922: 
1.1275    musolffc 6923:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170    droeschl 6924:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  6925:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 6926:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  6927:                         or ($tstart && $tstart > $now);
1.1170    droeschl 6928:             }
1.1219    raeburn  6929:         }
                   6930:     }
                   6931:     return 0;
                   6932: }
1.1170    droeschl 6933: 
1.1219    raeburn  6934: sub privileged_by_domain {
                   6935:     my ($domains,$roles) = @_;
                   6936:     my %privileged = ();
                   6937:     my $cachetime = 60*60*24;
                   6938:     my $now = time;
                   6939:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   6940:         return %privileged;
                   6941:     }
                   6942:     foreach my $dom (@{$domains}) {
                   6943:         next if (ref($privileged{$dom}) eq 'HASH');
                   6944:         my $needroles;
                   6945:         foreach my $role (@{$roles}) {
                   6946:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   6947:             if (defined($cached)) {
                   6948:                 if (ref($result) eq 'HASH') {
                   6949:                     $privileged{$dom}{$role} = $result;
                   6950:                 }
                   6951:             } else {
                   6952:                 $needroles = 1;
                   6953:             }
                   6954:         }
                   6955:         if ($needroles) {
                   6956:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   6957:             $privileged{$dom} = {};
                   6958:             foreach my $server (keys(%dompersonnel)) {
                   6959:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   6960:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   6961:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   6962:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   6963:                         next if ($end && $end < $now);
                   6964:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   6965:                             $dompersonnel{$server}{$item};
                   6966:                     }
                   6967:                 }
                   6968:             }
                   6969:             if (ref($privileged{$dom}) eq 'HASH') {
                   6970:                 foreach my $role (@{$roles}) {
                   6971:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6972:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   6973:                     } else {
                   6974:                         my %hash = ();
                   6975:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   6976:                     }
                   6977:                 }
                   6978:             }
                   6979:         }
                   6980:     }
                   6981:     return %privileged;
1.9       www      6982: }
1.1       albertel 6983: 
1.103     harris41 6984: # -------------------------------------------------------- Get user privileges
1.11      www      6985: 
                   6986: sub rolesinit {
1.1169    droeschl 6987:     my ($domain, $username) = @_;
                   6988:     my %userroles = ('user.login.time' => time);
                   6989:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   6990: 
                   6991:     # firstaccess and timerinterval are related to timed maps/resources. 
                   6992:     # also, blocking can be triggered by an activating timer
                   6993:     # it's saved in the user's %env.
                   6994:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   6995:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   6996:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
1.1517    raeburn  6997:         %timerintchk, %timerintenv, %coauthorenv);
1.1169    droeschl 6998: 
1.1162    raeburn  6999:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 7000:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  7001:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   7002:     }
1.1169    droeschl 7003: 
1.1162    raeburn  7004:     foreach my $key (keys(%timerinterval)) {
                   7005:         my ($cid,$rest) = split(/\0/,$key);
                   7006:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   7007:     }
1.1169    droeschl 7008: 
1.11      www      7009:     my %allroles=();
1.1162    raeburn  7010:     my %allgroups=();
1.1517    raeburn  7011:     my %gotcoauconfig=();
1.11      www      7012: 
1.1274    raeburn  7013:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169    droeschl 7014:         my $role = $rolesdump{$area};
                   7015:         $area =~ s/\_\w\w$//;
                   7016: 
                   7017:         my ($trole, $tend, $tstart, $group_privs);
                   7018: 
                   7019:         if ($role =~ /^cr/) {
                   7020:         # Custom role, defined by a user 
                   7021:         # e.g., user.role.cr/msu/smith/mynewrole
                   7022:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   7023:                 $trole = $1;
                   7024:                 ($tend, $tstart) = split('_', $2);
                   7025:             } else {
                   7026:                 $trole = $role;
                   7027:             }
                   7028:         } elsif ($role =~ m|^gr/|) {
                   7029:         # Role of member in a group, defined within a course/community
                   7030:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   7031:             ($trole, $tend, $tstart) = split(/_/, $role);
                   7032:             next if $tstart eq '-1';
                   7033:             ($trole, $group_privs) = split(/\//, $trole);
                   7034:             $group_privs = &unescape($group_privs);
                   7035:         } else {
                   7036:         # Just a normal role, defined in roles.tab
                   7037:             ($trole, $tend, $tstart) = split(/_/,$role);
                   7038:         }
                   7039: 
                   7040:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   7041:                  $username);
                   7042:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   7043: 
                   7044:         # role expired or not available yet?
                   7045:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   7046:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   7047: 
                   7048:         next if $area eq '' or $trole eq '';
                   7049: 
                   7050:         my $spec = "$trole.$area";
                   7051:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   7052: 
                   7053:         if ($trole =~ /^cr\//) {
                   7054:         # Custom role, defined by a user
                   7055:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   7056:         } elsif ($trole eq 'gr') {
                   7057:         # Role of a member in a group, defined within a course/community
                   7058:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   7059:             next;
                   7060:         } else {
                   7061:         # Normal role, defined in roles.tab
                   7062:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.1517    raeburn  7063:             if (($trole eq 'ca') || ($trole eq 'aa')) {
                   7064:                 (undef,my ($audom,$auname)) = split(/\//,$area);
                   7065:                 unless ($gotcoauconfig{$area}) {
                   7066:                     my @ca_settings = ('authoreditors','coauthorlist','coauthoroptin');
                   7067:                     my %info = &userenvironment($audom,$auname,@ca_settings);
                   7068:                     $gotcoauconfig{$area} = 1;
                   7069:                     foreach my $item (@ca_settings) {
                   7070:                         if (exists($info{$item})) {
1.1519    raeburn  7071:                             my $name = $item;
                   7072:                             if ($item eq 'authoreditors') {
                   7073:                                 $name = 'editors';
                   7074:                             }
                   7075:                             $coauthorenv{"environment.internal.$name.$area"} = $info{$item};
1.1517    raeburn  7076:                         }
                   7077:                     }
                   7078:                 }
                   7079:             }
1.1169    droeschl 7080:         }
                   7081: 
                   7082:         my $cid = $tdomain.'_'.$trest;
                   7083:         unless ($firstaccchk{$cid}) {
                   7084:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   7085:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   7086:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   7087:                         $coursetimerstarts{$cid}{$item}; 
                   7088:                 }
                   7089:             }
                   7090:             $firstaccchk{$cid} = 1;
                   7091:         }
                   7092:         unless ($timerintchk{$cid}) {
                   7093:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   7094:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   7095:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   7096:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  7097:                 }
1.12      www      7098:             }
1.1169    droeschl 7099:             $timerintchk{$cid} = 1;
1.191     harris41 7100:         }
1.11      www      7101:     }
1.1169    droeschl 7102: 
1.1341    raeburn  7103:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
                   7104:                                                           \%allroles, \%allgroups);
1.1169    droeschl 7105:     $env{'user.adv'} = $userroles{'user.adv'};
1.1341    raeburn  7106:     $env{'user.rar'} = $userroles{'user.rar'};
1.1169    droeschl 7107: 
1.1517    raeburn  7108:     return (\%userroles,\%firstaccenv,\%timerintenv,\%coauthorenv);
1.11      www      7109: }
                   7110: 
1.567     raeburn  7111: sub set_arearole {
1.1215    raeburn  7112:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   7113:     unless ($nolog) {
1.567     raeburn  7114: # log the associated role with the area
1.1215    raeburn  7115:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   7116:     }
1.743     albertel 7117:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  7118: }
                   7119: 
                   7120: sub custom_roleprivs {
                   7121:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   7122:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250    raeburn  7123:     my $homsvr = &homeserver($rauthor,$rdomain);
1.838     albertel 7124:     if (&hostname($homsvr) ne '') {
1.567     raeburn  7125:         my ($rdummy,$roledef)=
                   7126:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   7127:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   7128:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   7129:             if (defined($syspriv)) {
1.1043    raeburn  7130:                 if ($trest =~ /^$match_community$/) {
                   7131:                     $syspriv =~ s/bre\&S//; 
                   7132:                 }
1.567     raeburn  7133:                 $$allroles{'cm./'}.=':'.$syspriv;
                   7134:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   7135:             }
                   7136:             if ($tdomain ne '') {
                   7137:                 if (defined($dompriv)) {
                   7138:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   7139:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   7140:                 }
                   7141:                 if (($trest ne '') && (defined($coursepriv))) {
1.1332    raeburn  7142:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
                   7143:                         my $rolename = $1;
                   7144:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
                   7145:                     }
1.567     raeburn  7146:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   7147:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   7148:                 }
                   7149:             }
                   7150:         }
                   7151:     }
                   7152: }
                   7153: 
1.1332    raeburn  7154: sub course_adhocrole_privs {
                   7155:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
                   7156:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
                   7157:     if ($overrides{"internal.adhocpriv.$rolename"}) {
                   7158:         my (%currprivs,%storeprivs);
                   7159:         foreach my $item (split(/:/,$coursepriv)) {
                   7160:             my ($priv,$restrict) = split(/\&/,$item);
                   7161:             $currprivs{$priv} = $restrict;
                   7162:         }
                   7163:         my (%possadd,%possremove,%full);
                   7164:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   7165:             my ($priv,$restrict)=split(/\&/,$item);
                   7166:             $full{$priv} = $restrict;
                   7167:         }
                   7168:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
1.1490    raeburn  7169:             next if ($item eq '');
                   7170:             my ($rule,$rest) = split(/=/,$item);
                   7171:             next unless (($rule eq 'off') || ($rule eq 'on'));
                   7172:             foreach my $priv (split(/:/,$rest)) {
                   7173:                 if ($priv ne '') {
                   7174:                     if ($rule eq 'off') {
                   7175:                         $possremove{$priv} = 1;
                   7176:                     } else {
                   7177:                         $possadd{$priv} = 1;
                   7178:                     }
                   7179:                 }
                   7180:             }
                   7181:         }
                   7182:         foreach my $priv (sort(keys(%full))) {
                   7183:             if (exists($currprivs{$priv})) {
                   7184:                 unless (exists($possremove{$priv})) {
                   7185:                     $storeprivs{$priv} = $currprivs{$priv};
                   7186:                 }
                   7187:             } elsif (exists($possadd{$priv})) {
                   7188:                 $storeprivs{$priv} = $full{$priv};
                   7189:             }
                   7190:         }
                   7191:         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
                   7192:     }
                   7193:     return $coursepriv;
1.1332    raeburn  7194: }
                   7195: 
1.678     raeburn  7196: sub group_roleprivs {
                   7197:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   7198:     my $access = 1;
                   7199:     my $now = time;
                   7200:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   7201:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   7202:     if ($access) {
1.811     albertel 7203:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  7204:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   7205:     }
                   7206: }
1.567     raeburn  7207: 
                   7208: sub standard_roleprivs {
                   7209:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   7210:     if (defined($pr{$trole.':s'})) {
                   7211:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   7212:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   7213:     }
                   7214:     if ($tdomain ne '') {
                   7215:         if (defined($pr{$trole.':d'})) {
                   7216:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   7217:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   7218:         }
                   7219:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   7220:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   7221:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   7222:         }
                   7223:     }
                   7224: }
                   7225: 
                   7226: sub set_userprivs {
1.1064    raeburn  7227:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  7228:     my $author=0;
                   7229:     my $adv=0;
1.1341    raeburn  7230:     my $rar=0;
1.678     raeburn  7231:     my %grouproles = ();
                   7232:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  7233:         my @groupkeys; 
1.1000    raeburn  7234:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  7235:             push(@groupkeys,$role);
                   7236:         }
                   7237:         if (ref($groups_roles) eq 'HASH') {
                   7238:             foreach my $key (keys(%{$groups_roles})) {
                   7239:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   7240:                     push(@groupkeys,$key);
                   7241:                 }
                   7242:             }
                   7243:         }
                   7244:         if (@groupkeys > 0) {
                   7245:             foreach my $role (@groupkeys) {
                   7246:                 my ($trole,$area,$sec,$extendedarea);
                   7247:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   7248:                     $trole = $1;
                   7249:                     $area = $2;
                   7250:                     $sec = $3;
                   7251:                     $extendedarea = $area.$sec;
                   7252:                     if (exists($$allgroups{$area})) {
                   7253:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   7254:                             my $spec = $trole.'.'.$extendedarea;
                   7255:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  7256:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  7257:                         }
1.678     raeburn  7258:                     }
                   7259:                 }
                   7260:             }
                   7261:         }
                   7262:     }
1.800     albertel 7263:     foreach my $group (keys(%grouproles)) {
                   7264:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  7265:     }
1.800     albertel 7266:     foreach my $role (keys(%{$allroles})) {
                   7267:         my %thesepriv;
1.941     raeburn  7268:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 7269:         foreach my $item (split(/:/,$$allroles{$role})) {
                   7270:             if ($item ne '') {
                   7271:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  7272:                 if ($restrictions eq '') {
                   7273:                     $thesepriv{$privilege}='F';
                   7274:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   7275:                     $thesepriv{$privilege}.=$restrictions;
                   7276:                 }
                   7277:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341    raeburn  7278:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567     raeburn  7279:             }
                   7280:         }
                   7281:         my $thesestr='';
1.1104    raeburn  7282:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 7283: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   7284: 	}
                   7285:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  7286:     }
1.1341    raeburn  7287:     return ($author,$adv,$rar);
1.567     raeburn  7288: }
                   7289: 
1.994     raeburn  7290: sub role_status {
1.1104    raeburn  7291:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  7292:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250    raeburn  7293:         my ($one,$two) = split(m{\./},$rolekey,2);
                   7294:         (undef,undef,$$role) = split(/\./,$one,3);
1.994     raeburn  7295:         unless (!defined($$role) || $$role eq '') {
1.1251    raeburn  7296:             $$where = '/'.$two;
1.994     raeburn  7297:             $$trolecode=$$role.'.'.$$where;
                   7298:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   7299:             $$tstatus='is';
1.1104    raeburn  7300:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  7301:                 $$tstatus='future';
1.1034    raeburn  7302:                 if ($$tstart<$now) {
                   7303:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  7304:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  7305:                             my (%allroles,%allgroups,$group_privs,
                   7306:                                 %groups_roles,@rolecodes);
1.1002    raeburn  7307:                             my %userroles = (
                   7308:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   7309:                             );
1.1064    raeburn  7310:                             @rolecodes = ('cm'); 
1.1002    raeburn  7311:                             my $spec=$$role.'.'.$$where;
                   7312:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   7313:                             if ($$role =~ /^cr\//) {
                   7314:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  7315:                                 push(@rolecodes,'cr');
1.1002    raeburn  7316:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  7317:                                 push(@rolecodes,$$role);
1.1002    raeburn  7318:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   7319:                                                     $env{'user.name'});
1.1064    raeburn  7320:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  7321:                                 (undef,my $group_privs) = split(/\//,$trole);
                   7322:                                 $group_privs = &unescape($group_privs);
                   7323:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  7324:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
1.1104    raeburn  7325:                                 &get_groups_roles($tdomain,$trest,
                   7326:                                                   \%course_roles,\@rolecodes,
                   7327:                                                   \%groups_roles);
1.1002    raeburn  7328:                             } else {
1.1064    raeburn  7329:                                 push(@rolecodes,$$role);
1.1002    raeburn  7330:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   7331:                             }
1.1341    raeburn  7332:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
                   7333:                                                                    \%groups_roles);
1.1064    raeburn  7334:                             &appenv(\%userroles,\@rolecodes);
1.1342    raeburn  7335:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002    raeburn  7336:                         }
                   7337:                     }
1.1034    raeburn  7338:                     $$tstatus = 'is';
1.1002    raeburn  7339:                 }
1.994     raeburn  7340:             }
                   7341:             if ($$tend) {
1.1104    raeburn  7342:                 if ($$tend<$update) {
1.994     raeburn  7343:                     $$tstatus='expired';
                   7344:                 } elsif ($$tend<$now) {
                   7345:                     $$tstatus='will_not';
                   7346:                 }
                   7347:             }
                   7348:         }
                   7349:     }
                   7350: }
                   7351: 
1.1104    raeburn  7352: sub get_groups_roles {
                   7353:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   7354:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   7355:                   (ref($rolecodes) eq 'ARRAY') && 
                   7356:                   (ref($groups_roles) eq 'HASH')); 
                   7357:     if (keys(%{$cdom_courseroles}) > 0) {
                   7358:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   7359:         if ($cdom ne '' && $cnum ne '') {
                   7360:             foreach my $key (keys(%{$cdom_courseroles})) {
                   7361:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   7362:                     my $crsrole = $1;
                   7363:                     my $crssec = $2;
                   7364:                     if ($crsrole =~ /^cr/) {
                   7365:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   7366:                             push(@{$rolecodes},'cr');
                   7367:                         }
                   7368:                     } else {
                   7369:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   7370:                             push(@{$rolecodes},$crsrole);
                   7371:                         }
                   7372:                     }
                   7373:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   7374:                     if ($crssec ne '') {
                   7375:                         $rolekey .= "/$crssec";
                   7376:                     }
                   7377:                     $rolekey .= './';
                   7378:                     $groups_roles->{$rolekey} = $rolecodes;
                   7379:                 }
                   7380:             }
                   7381:         }
                   7382:     }
                   7383:     return;
                   7384: }
                   7385: 
                   7386: sub delete_env_groupprivs {
                   7387:     my ($where,$courseroles,$possroles) = @_;
                   7388:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   7389:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   7390:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   7391:         %{$courseroles->{$udom}} =
                   7392:             &get_my_roles('','','userroles',['active'],
                   7393:                           $possroles,[$udom],1);
                   7394:     }
                   7395:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   7396:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   7397:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   7398:             my $area = '/'.$cdom.'/'.$cnum;
                   7399:             my $privkey = "user.priv.$crsrole.$area";
                   7400:             if ($crssec ne '') {
                   7401:                 $privkey .= '/'.$crssec;
                   7402:             }
                   7403:             $privkey .= ".$area/$group";
                   7404:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   7405:         }
                   7406:     }
                   7407:     return;
                   7408: }
                   7409: 
1.994     raeburn  7410: sub check_adhoc_privs {
1.1329    raeburn  7411:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994     raeburn  7412:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329    raeburn  7413:     if ($sec) {
                   7414:         $cckey .= '/'.$sec;
                   7415:     } 
1.1185    raeburn  7416:     my $setprivs;
1.994     raeburn  7417:     if ($env{$cckey}) {
                   7418:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  7419:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  7420:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329    raeburn  7421:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7422:             $setprivs = 1;
1.994     raeburn  7423:         }
                   7424:     } else {
1.1330    raeburn  7425:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7426:         $setprivs = 1;
1.994     raeburn  7427:     }
1.1185    raeburn  7428:     return $setprivs;
1.994     raeburn  7429: }
                   7430: 
                   7431: sub set_adhoc_privileges {
1.1326    raeburn  7432: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329    raeburn  7433:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994     raeburn  7434:     my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329    raeburn  7435:     if ($sec ne '') {
                   7436:         $area .= '/'.$sec;
                   7437:     }
1.994     raeburn  7438:     my $spec = $role.'.'.$area;
                   7439:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  7440:                                   $env{'user.name'},1);
1.1326    raeburn  7441:     my %rolehash = ();
1.1332    raeburn  7442:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
                   7443:         my $rolename = $1;
1.1326    raeburn  7444:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332    raeburn  7445:         my %domdef = &get_domain_defaults($dcdom);
                   7446:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   7447:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   7448:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
                   7449:             }
                   7450:         }
1.1326    raeburn  7451:     } else {
                   7452:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
                   7453:     }
1.1341    raeburn  7454:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994     raeburn  7455:     &appenv(\%userroles,[$role,'cm']);
1.1342    raeburn  7456:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1402    raeburn  7457:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
                   7458:             ($caller eq 'tiny')) {
1.1088    raeburn  7459:         &appenv( {'request.role'        => $spec,
                   7460:                   'request.role.domain' => $dcdom,
1.1332    raeburn  7461:                   'request.course.sec'  => $sec,
1.1088    raeburn  7462:                  }
                   7463:                );
                   7464:         my $tadv=0;
                   7465:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   7466:         &appenv({'request.role.adv'    => $tadv});
                   7467:     }
1.994     raeburn  7468: }
                   7469: 
1.12      www      7470: # --------------------------------------------------------------- get interface
                   7471: 
                   7472: sub get {
1.131     albertel 7473:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7474:    my $items='';
1.800     albertel 7475:    foreach my $item (@$storearr) {
                   7476:        $items.=&escape($item).'&';
1.191     harris41 7477:    }
1.12      www      7478:    $items=~s/\&$//;
1.620     albertel 7479:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7480:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 7481:    my $uhome=&homeserver($uname,$udomain);
                   7482: 
1.133     albertel 7483:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7484:    my @pairs=split(/\&/,$rep);
1.273     albertel 7485:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   7486:      return @pairs;
                   7487:    }
1.15      www      7488:    my %returnhash=();
1.42      www      7489:    my $i=0;
1.800     albertel 7490:    foreach my $item (@$storearr) {
                   7491:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7492:       $i++;
1.191     harris41 7493:    }
1.15      www      7494:    return %returnhash;
1.27      www      7495: }
                   7496: 
                   7497: # --------------------------------------------------------------- del interface
                   7498: 
                   7499: sub del {
1.133     albertel 7500:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      7501:    my $items='';
1.800     albertel 7502:    foreach my $item (@$storearr) {
                   7503:        $items.=&escape($item).'&';
1.191     harris41 7504:    }
1.984     neumanie 7505: 
1.27      www      7506:    $items=~s/\&$//;
1.620     albertel 7507:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7508:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7509:    my $uhome=&homeserver($uname,$udomain);
                   7510:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7511: }
                   7512: 
                   7513: # -------------------------------------------------------------- dump interface
                   7514: 
1.1180    droeschl 7515: sub unserialize {
                   7516:     my ($rep, $escapedkeys) = @_;
                   7517: 
                   7518:     return {} if $rep =~ /^error/;
                   7519: 
                   7520:     my %returnhash=();
1.1252    raeburn  7521: 	foreach my $item (split(/\&/,$rep)) {
1.1180    droeschl 7522: 	    my ($key, $value) = split(/=/, $item, 2);
                   7523: 	    $key = unescape($key) unless $escapedkeys;
                   7524: 	    next if $key =~ /^error: 2 /;
1.1252    raeburn  7525: 	    $returnhash{$key} = &thaw_unescape($value);
1.1180    droeschl 7526: 	}
                   7527:     #return %returnhash;
                   7528:     return \%returnhash;
                   7529: }        
                   7530: 
                   7531: # see Lond::dump_with_regexp
                   7532: # if $escapedkeys hash keys won't get unescaped.
1.15      www      7533: sub dump {
1.1462    raeburn  7534:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755     albertel 7535:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7536:     if (!$uname) { $uname=$env{'user.name'}; }
                   7537:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 7538: 
1.1266    raeburn  7539:     if ($regexp) {
                   7540:         $regexp=&escape($regexp);
                   7541:     } else {
                   7542:         $regexp='.';
                   7543:     }
1.1180    droeschl 7544:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   7545:         # user is hosted on this machine
1.1266    raeburn  7546:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  7547:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 7548:         return %{unserialize($reply, $escapedkeys)};
                   7549:     }
1.1462    raeburn  7550:     my $rep;
                   7551:     if ($encrypt) {
                   7552:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.1463    raeburn  7553:     } else {
1.1462    raeburn  7554:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   7555:     }
1.755     albertel 7556:     my @pairs=split(/\&/,$rep);
                   7557:     my %returnhash=();
1.1098    foxr     7558:     if (!($rep =~ /^error/ )) {
                   7559: 	foreach my $item (@pairs) {
                   7560: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 7561:         $key = unescape($key) unless $escapedkeys;
                   7562:         #$key = &unescape($key);
1.1098    foxr     7563: 	    next if ($key =~ /^error: 2 /);
                   7564: 	    $returnhash{$key}=&thaw_unescape($value);
                   7565: 	}
1.755     albertel 7566:     }
                   7567:     return %returnhash;
1.407     www      7568: }
                   7569: 
1.1098    foxr     7570: 
1.717     albertel 7571: # --------------------------------------------------------- dumpstore interface
                   7572: 
                   7573: sub dumpstore {
                   7574:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 7575:    # same as dump but keys must be escaped. They may contain colon separated
                   7576:    # lists of values that may themself contain colons (e.g. symbs).
                   7577:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 7578: }
                   7579: 
1.407     www      7580: # -------------------------------------------------------------- keys interface
                   7581: 
                   7582: sub getkeys {
                   7583:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 7584:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7585:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      7586:    my $uhome=&homeserver($uname,$udomain);
                   7587:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   7588:    my @keyarray=();
1.800     albertel 7589:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  7590:       next if ($key =~ /^error: 2 /);
1.800     albertel 7591:       push(@keyarray,&unescape($key));
1.407     www      7592:    }
                   7593:    return @keyarray;
1.318     matthew  7594: }
                   7595: 
1.319     matthew  7596: # --------------------------------------------------------------- currentdump
                   7597: sub currentdump {
1.328     matthew  7598:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 7599:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   7600:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   7601:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  7602:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 7603:    my $rep;
                   7604: 
                   7605:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   7606:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   7607:                    $courseid)));
                   7608:    } else {
                   7609:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   7610:    }
                   7611: 
1.318     matthew  7612:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  7613:    #
1.318     matthew  7614:    my %returnhash=();
1.319     matthew  7615:    #
1.1343    raeburn  7616:    if ($rep eq 'unknown_cmd') {
1.319     matthew  7617:        # an old lond will not know currentdump
                   7618:        # Do a dump and make it look like a currentdump
1.822     albertel 7619:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  7620:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   7621:        my %hash = @tmp;
                   7622:        @tmp=();
1.424     matthew  7623:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  7624:    } else {
                   7625:        my @pairs=split(/\&/,$rep);
1.800     albertel 7626:        foreach my $pair (@pairs) {
                   7627:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  7628:            my ($symb,$param) = split(/:/,$key);
                   7629:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 7630:                                                         &thaw_unescape($value);
1.319     matthew  7631:        }
1.191     harris41 7632:    }
1.12      www      7633:    return %returnhash;
1.424     matthew  7634: }
                   7635: 
                   7636: sub convert_dump_to_currentdump{
                   7637:     my %hash = %{shift()};
                   7638:     my %returnhash;
                   7639:     # Code ripped from lond, essentially.  The only difference
                   7640:     # here is the unescaping done by lonnet::dump().  Conceivably
                   7641:     # we might run in to problems with parameter names =~ /^v\./
                   7642:     while (my ($key,$value) = each(%hash)) {
                   7643:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 7644: 	$symb  = &unescape($symb);
                   7645: 	$param = &unescape($param);
1.424     matthew  7646:         next if ($v eq 'version' || $symb eq 'keys');
                   7647:         next if (exists($returnhash{$symb}) &&
                   7648:                  exists($returnhash{$symb}->{$param}) &&
                   7649:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   7650:         $returnhash{$symb}->{$param}=$value;
                   7651:         $returnhash{$symb}->{'v.'.$param}=$v;
                   7652:     }
                   7653:     #
                   7654:     # Remove all of the keys in the hashes which keep track of
                   7655:     # the version of the parameter.
                   7656:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   7657:         # use a foreach because we are going to delete from the hash.
                   7658:         foreach my $key (keys(%$param_hash)) {
                   7659:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   7660:         }
                   7661:     }
                   7662:     return \%returnhash;
1.12      www      7663: }
                   7664: 
1.627     albertel 7665: # ------------------------------------------------------ critical inc interface
                   7666: 
                   7667: sub cinc {
                   7668:     return &inc(@_,'critical');
                   7669: }
                   7670: 
1.449     matthew  7671: # --------------------------------------------------------------- inc interface
                   7672: 
                   7673: sub inc {
1.627     albertel 7674:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 7675:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7676:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  7677:     my $uhome=&homeserver($uname,$udomain);
                   7678:     my $items='';
                   7679:     if (! ref($store)) {
                   7680:         # got a single value, so use that instead
                   7681:         $items = &escape($store).'=&';
                   7682:     } elsif (ref($store) eq 'SCALAR') {
                   7683:         $items = &escape($$store).'=&';        
                   7684:     } elsif (ref($store) eq 'ARRAY') {
                   7685:         $items = join('=&',map {&escape($_);} @{$store});
                   7686:     } elsif (ref($store) eq 'HASH') {
                   7687:         while (my($key,$value) = each(%{$store})) {
                   7688:             $items.= &escape($key).'='.&escape($value).'&';
                   7689:         }
                   7690:     }
                   7691:     $items=~s/\&$//;
1.627     albertel 7692:     if ($critical) {
                   7693: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7694:     } else {
                   7695: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7696:     }
1.449     matthew  7697: }
                   7698: 
1.12      www      7699: # --------------------------------------------------------------- put interface
                   7700: 
                   7701: sub put {
1.1462    raeburn  7702:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620     albertel 7703:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7704:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7705:    my $uhome=&homeserver($uname,$udomain);
1.12      www      7706:    my $items='';
1.800     albertel 7707:    foreach my $item (keys(%$storehash)) {
                   7708:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7709:    }
1.12      www      7710:    $items=~s/\&$//;
1.1462    raeburn  7711:    if ($encrypt) {
                   7712:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
                   7713:    } else {
                   7714:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
                   7715:    }
1.47      www      7716: }
                   7717: 
1.631     albertel 7718: # ------------------------------------------------------------ newput interface
                   7719: 
                   7720: sub newput {
                   7721:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   7722:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7723:    if (!$uname) { $uname=$env{'user.name'}; }
                   7724:    my $uhome=&homeserver($uname,$udomain);
                   7725:    my $items='';
                   7726:    foreach my $key (keys(%$storehash)) {
                   7727:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7728:    }
                   7729:    $items=~s/\&$//;
                   7730:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   7731: }
                   7732: 
                   7733: # ---------------------------------------------------------  putstore interface
                   7734: 
1.524     raeburn  7735: sub putstore {
1.1269    raeburn  7736:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620     albertel 7737:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7738:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  7739:    my $uhome=&homeserver($uname,$udomain);
                   7740:    my $items='';
1.715     albertel 7741:    foreach my $key (keys(%$storehash)) {
                   7742:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  7743:    }
1.715     albertel 7744:    $items=~s/\&$//;
1.716     albertel 7745:    my $esc_symb=&escape($symb);
                   7746:    my $esc_v=&escape($version);
1.715     albertel 7747:    my $reply =
1.716     albertel 7748:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 7749: 	      $uhome);
1.1269    raeburn  7750:    if (($tolog) && ($reply eq 'ok')) {
                   7751:        my $namevalue='';
                   7752:        foreach my $key (keys(%{$storehash})) {
                   7753:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
                   7754:        }
1.1434    raeburn  7755:        my $ip = &get_requestor_ip();
                   7756:        $namevalue .= 'ip='.&escape($ip).
1.1269    raeburn  7757:                      '&host='.&escape($perlvar{'lonHostID'}).
                   7758:                      '&version='.$esc_v.
                   7759:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
1.1494    raeburn  7760:        &courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
1.1269    raeburn  7761:    }
1.715     albertel 7762:    if ($reply eq 'unknown_cmd') {
1.716     albertel 7763:        # gfall back to way things use to be done
1.715     albertel 7764:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   7765: 			    $uname);
1.524     raeburn  7766:    }
1.715     albertel 7767:    return $reply;
                   7768: }
                   7769: 
                   7770: sub old_putstore {
1.716     albertel 7771:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   7772:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7773:     if (!$uname) { $uname=$env{'user.name'}; }
                   7774:     my $uhome=&homeserver($uname,$udomain);
                   7775:     my %newstorehash;
1.800     albertel 7776:     foreach my $item (keys(%$storehash)) {
                   7777: 	my $key = $version.':'.&escape($symb).':'.$item;
                   7778: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 7779:     }
                   7780:     my $items='';
                   7781:     my %allitems = ();
1.800     albertel 7782:     foreach my $item (keys(%newstorehash)) {
                   7783: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 7784: 	    my $key = $1.':keys:'.$2;
                   7785: 	    $allitems{$key} .= $3.':';
                   7786: 	}
1.800     albertel 7787: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 7788:     }
1.800     albertel 7789:     foreach my $item (keys(%allitems)) {
                   7790: 	$allitems{$item} =~ s/\:$//;
                   7791: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 7792:     }
                   7793:     $items=~s/\&$//;
                   7794:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  7795: }
                   7796: 
1.47      www      7797: # ------------------------------------------------------ critical put interface
                   7798: 
                   7799: sub cput {
1.134     albertel 7800:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7801:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7802:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7803:    my $uhome=&homeserver($uname,$udomain);
1.47      www      7804:    my $items='';
1.800     albertel 7805:    foreach my $item (keys(%$storehash)) {
                   7806:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7807:    }
1.47      www      7808:    $items=~s/\&$//;
1.134     albertel 7809:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7810: }
                   7811: 
                   7812: # -------------------------------------------------------------- eget interface
                   7813: 
                   7814: sub eget {
1.133     albertel 7815:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7816:    my $items='';
1.800     albertel 7817:    foreach my $item (@$storearr) {
                   7818:        $items.=&escape($item).'&';
1.191     harris41 7819:    }
1.12      www      7820:    $items=~s/\&$//;
1.620     albertel 7821:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7822:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7823:    my $uhome=&homeserver($uname,$udomain);
                   7824:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7825:    my @pairs=split(/\&/,$rep);
                   7826:    my %returnhash=();
1.42      www      7827:    my $i=0;
1.800     albertel 7828:    foreach my $item (@$storearr) {
                   7829:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7830:       $i++;
1.191     harris41 7831:    }
1.12      www      7832:    return %returnhash;
                   7833: }
                   7834: 
1.667     albertel 7835: # ------------------------------------------------------------ tmpput interface
                   7836: sub tmpput {
1.802     raeburn  7837:     my ($storehash,$server,$context)=@_;
1.667     albertel 7838:     my $items='';
1.800     albertel 7839:     foreach my $item (keys(%$storehash)) {
                   7840: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 7841:     }
                   7842:     $items=~s/\&$//;
1.802     raeburn  7843:     if (defined($context)) {
                   7844:         $items .= ':'.&escape($context);
                   7845:     }
1.667     albertel 7846:     return &reply("tmpput:$items",$server);
                   7847: }
                   7848: 
                   7849: # ------------------------------------------------------------ tmpget interface
                   7850: sub tmpget {
1.688     albertel 7851:     my ($token,$server)=@_;
                   7852:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7853:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 7854:     my %returnhash;
1.1328    raeburn  7855:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
                   7856:         return %returnhash;
                   7857:     }
1.667     albertel 7858:     foreach my $item (split(/\&/,$rep)) {
                   7859: 	my ($key,$value)=split(/=/,$item);
                   7860: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   7861:     }
                   7862:     return %returnhash;
                   7863: }
                   7864: 
1.1113    raeburn  7865: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 7866: sub tmpdel {
                   7867:     my ($token,$server)=@_;
                   7868:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7869:     return &reply("tmpdel:$token",$server);
                   7870: }
                   7871: 
1.1198    raeburn  7872: # ------------------------------------------------------------ get_timebased_id 
                   7873: 
                   7874: sub get_timebased_id {
                   7875:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   7876:         $maxtries) = @_;
                   7877:     my ($newid,$error,$dellock);
                   7878:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   7879:         return ('','ok','invalid call to get suffix');
                   7880:     }
                   7881: 
                   7882: # set defaults for any optional args for which values were not supplied
                   7883:     if ($who eq '') {
                   7884:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   7885:     }
                   7886:     if (!$locktries) {
                   7887:         $locktries = 3;
                   7888:     }
                   7889:     if (!$maxtries) {
                   7890:         $maxtries = 10;
                   7891:     }
                   7892:     
                   7893:     if (($cdom eq '') || ($cnum eq '')) {
                   7894:         if ($env{'request.course.id'}) {
                   7895:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7896:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7897:         }
                   7898:         if (($cdom eq '') || ($cnum eq '')) {
                   7899:             return ('','ok','call to get suffix not in course context');
                   7900:         }
                   7901:     }
                   7902: 
                   7903: # construct locking item
                   7904:     my $lockhash = {
                   7905:                       $prefix."\0".'locked_'.$keyid => $who,
                   7906:                    };
                   7907:     my $tries = 0;
                   7908: 
                   7909: # attempt to get lock on nohist_$namespace file
1.1494    raeburn  7910:     my $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
1.1198    raeburn  7911:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   7912:         $tries ++;
                   7913:         sleep 1;
1.1494    raeburn  7914:         $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
1.1198    raeburn  7915:     }
                   7916: 
                   7917: # attempt to get unique identifier, based on current timestamp
                   7918:     if ($gotlock eq 'ok') {
1.1494    raeburn  7919:         my %inuse = &dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
1.1198    raeburn  7920:         my $id = time;
                   7921:         $newid = $id;
1.1268    raeburn  7922:         if ($idtype eq 'addcode') {
                   7923:             $newid .= &sixnum_code();
                   7924:         }
1.1198    raeburn  7925:         my $idtries = 0;
                   7926:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   7927:             if ($idtype eq 'concat') {
                   7928:                 $newid = $id.$idtries;
1.1268    raeburn  7929:             } elsif ($idtype eq 'addcode') {
                   7930:                 $newid = $newid.&sixnum_code();
1.1198    raeburn  7931:             } else {
                   7932:                 $newid ++;
                   7933:             }
                   7934:             $idtries ++;
                   7935:         }
                   7936:         if (!exists($inuse{$prefix."\0".$newid})) {
                   7937:             my %new_item =  (
                   7938:                               $prefix."\0".$newid => $who,
                   7939:                             );
1.1494    raeburn  7940:             my $putresult = &put('nohist_'.$namespace,\%new_item,
1.1198    raeburn  7941:                                                  $cdom,$cnum);
                   7942:             if ($putresult ne 'ok') {
                   7943:                 undef($newid);
                   7944:                 $error = 'error saving new item: '.$putresult;
                   7945:             }
                   7946:         } else {
1.1268    raeburn  7947:              undef($newid);
1.1198    raeburn  7948:              $error = ('error: no unique suffix available for the new item ');
                   7949:         }
                   7950: #  remove lock
                   7951:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   7952:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   7953:     } else {
                   7954:         $error = "error: could not obtain lockfile\n";
                   7955:         $dellock = 'ok';
1.1276    raeburn  7956:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
                   7957:             $dellock = 'nolock';
                   7958:         }
1.1198    raeburn  7959:     }
                   7960:     return ($newid,$dellock,$error);
                   7961: }
                   7962: 
1.1268    raeburn  7963: sub sixnum_code {
                   7964:     my $code;
                   7965:     for (0..6) {
                   7966:         $code .= int( rand(9) );
                   7967:     }
                   7968:     return $code;
                   7969: }
                   7970: 
1.765     albertel 7971: # -------------------------------------------------- portfolio access checking
                   7972: 
                   7973: sub portfolio_access {
1.1270    raeburn  7974:     my ($requrl,$clientip) = @_;
1.765     albertel 7975:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270    raeburn  7976:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814     raeburn  7977:     if ($result) {
                   7978:         my %setters;
                   7979:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1473    raeburn  7980:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
                   7981:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
                   7982:             if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  7983:                 return 'B';
                   7984:             }
                   7985:         } else {
1.1473    raeburn  7986:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
                   7987:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
                   7988:             if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  7989:                 return 'B';
                   7990:             }
                   7991:         }
                   7992:     }
1.765     albertel 7993:     if ($result eq 'ok') {
1.766     albertel 7994:        return 'F';
1.765     albertel 7995:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 7996:        return 'A';
1.765     albertel 7997:     }
1.766     albertel 7998:     return '';
1.765     albertel 7999: }
                   8000: 
                   8001: sub get_portfolio_access {
1.1521    raeburn  8002:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
1.767     albertel 8003: 
                   8004:     if (!ref($access_hash)) {
                   8005: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   8006: 	my %access_controls = &get_access_controls($current_perms,$group,
                   8007: 						   $file_name);
                   8008: 	$access_hash = $access_controls{$file_name};
                   8009:     }
                   8010: 
1.1521    raeburn  8011:     my $portaccess;
                   8012:     if (ref($portaccess) eq 'SCALAR') {
                   8013:         $portaccess = $$portaccessref;
                   8014:     } else {
                   8015:         $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
                   8016:     }
                   8017: 
1.1522    raeburn  8018:     my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
1.765     albertel 8019:     my $now = time;
                   8020:     if (ref($access_hash) eq 'HASH') {
                   8021:         foreach my $key (keys(%{$access_hash})) {
                   8022:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1521    raeburn  8023:             next if (($scope ne 'ip') && ($portaccess == 0));
1.765     albertel 8024:             if ($start > $now) {
                   8025:                 next;
                   8026:             }
                   8027:             if ($end && $end<$now) {
                   8028:                 next;
                   8029:             }
                   8030:             if ($scope eq 'public') {
                   8031:                 $public = $key;
                   8032:                 last;
                   8033:             } elsif ($scope eq 'guest') {
                   8034:                 $guest = $key;
                   8035:             } elsif ($scope eq 'domains') {
                   8036:                 push(@domains,$key);
                   8037:             } elsif ($scope eq 'users') {
                   8038:                 push(@users,$key);
                   8039:             } elsif ($scope eq 'course') {
                   8040:                 push(@courses,$key);
                   8041:             } elsif ($scope eq 'group') {
                   8042:                 push(@groups,$key);
1.1270    raeburn  8043:             } elsif ($scope eq 'ip') {
                   8044:                 push(@ips,$key);
1.1522    raeburn  8045:             } elsif ($scope eq 'userip') {
                   8046:                 push(@userips,$key);
1.765     albertel 8047:             }
                   8048:         }
                   8049:         if ($public) {
                   8050:             return 'ok';
1.1270    raeburn  8051:         } elsif (@ips > 0) {
                   8052:             my $allowed;
                   8053:             foreach my $ipkey (@ips) {
                   8054:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
                   8055:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
                   8056:                         $allowed = 1;
                   8057:                         last; 
                   8058:                     }
                   8059:                 }
                   8060:             }
                   8061:             if ($allowed) {
                   8062:                 return 'ok';
                   8063:             }
1.1522    raeburn  8064:         } elsif (@userips > 0) {
                   8065:             my $allowed;
                   8066:             foreach my $useripkey (@userips) {
                   8067:                 if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
                   8068:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
                   8069:                         $allowed = 1;
                   8070:                         last;
                   8071:                     }
                   8072:                 }
                   8073:             }
                   8074:             if ($allowed) {
                   8075:                 return 'ok';
                   8076:             }
1.765     albertel 8077:         }
                   8078:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   8079:             if ($guest) {
                   8080:                 return $guest;
                   8081:             }
                   8082:         } else {
                   8083:             if (@domains > 0) {
                   8084:                 foreach my $domkey (@domains) {
                   8085:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   8086:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   8087:                             return 'ok';
                   8088:                         }
                   8089:                     }
                   8090:                 }
                   8091:             }
                   8092:             if (@users > 0) {
                   8093:                 foreach my $userkey (@users) {
1.865     raeburn  8094:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   8095:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   8096:                             if (ref($item) eq 'HASH') {
                   8097:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   8098:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   8099:                                     return 'ok';
                   8100:                                 }
                   8101:                             }
                   8102:                         }
                   8103:                     } 
1.765     albertel 8104:                 }
                   8105:             }
                   8106:             my %roleshash;
                   8107:             my @courses_and_groups = @courses;
                   8108:             push(@courses_and_groups,@groups); 
                   8109:             if (@courses_and_groups > 0) {
                   8110:                 my (%allgroups,%allroles); 
                   8111:                 my ($start,$end,$role,$sec,$group);
                   8112:                 foreach my $envkey (%env) {
1.1060    raeburn  8113:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 8114:                         my $cid = $2.'_'.$3; 
                   8115:                         if ($1 eq 'gr') {
                   8116:                             $group = $4;
                   8117:                             $allgroups{$cid}{$group} = $env{$envkey};
                   8118:                         } else {
                   8119:                             if ($4 eq '') {
                   8120:                                 $sec = 'none';
                   8121:                             } else {
                   8122:                                 $sec = $4;
                   8123:                             }
                   8124:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   8125:                         }
1.811     albertel 8126:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 8127:                         my $cid = $2.'_'.$3;
                   8128:                         if ($4 eq '') {
                   8129:                             $sec = 'none';
                   8130:                         } else {
                   8131:                             $sec = $4;
                   8132:                         }
                   8133:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   8134:                     }
                   8135:                 }
                   8136:                 if (keys(%allroles) == 0) {
                   8137:                     return;
                   8138:                 }
                   8139:                 foreach my $key (@courses_and_groups) {
                   8140:                     my %content = %{$$access_hash{$key}};
                   8141:                     my $cnum = $content{'number'};
                   8142:                     my $cdom = $content{'domain'};
                   8143:                     my $cid = $cdom.'_'.$cnum;
                   8144:                     if (!exists($allroles{$cid})) {
                   8145:                         next;
                   8146:                     }    
                   8147:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   8148:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   8149:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   8150:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   8151:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   8152:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   8153:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   8154:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   8155:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   8156:                                         if (grep/^all$/,@sections) {
                   8157:                                             return 'ok';
                   8158:                                         } else {
                   8159:                                             if (grep/^$sec$/,@sections) {
                   8160:                                                 return 'ok';
                   8161:                                             }
                   8162:                                         }
                   8163:                                     }
                   8164:                                 }
                   8165:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   8166:                                     if (grep/^none$/,@groups) {
                   8167:                                         return 'ok';
                   8168:                                     }
                   8169:                                 } else {
                   8170:                                     if (grep/^all$/,@groups) {
                   8171:                                         return 'ok';
                   8172:                                     } 
                   8173:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   8174:                                         if (grep/^$group$/,@groups) {
                   8175:                                             return 'ok';
                   8176:                                         }
                   8177:                                     }
                   8178:                                 } 
                   8179:                             }
                   8180:                         }
                   8181:                     }
                   8182:                 }
                   8183:             }
                   8184:             if ($guest) {
                   8185:                 return $guest;
                   8186:             }
                   8187:         }
                   8188:     }
                   8189:     return;
                   8190: }
                   8191: 
                   8192: sub course_group_datechecker {
                   8193:     my ($dates,$now,$status) = @_;
                   8194:     my ($start,$end) = split(/\./,$dates);
                   8195:     if (!$start && !$end) {
                   8196:         return 'ok';
                   8197:     }
                   8198:     if (grep/^active$/,@{$status}) {
                   8199:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   8200:             return 'ok';
                   8201:         }
                   8202:     }
                   8203:     if (grep/^previous$/,@{$status}) {
                   8204:         if ($end > $now ) {
                   8205:             return 'ok';
                   8206:         }
                   8207:     }
                   8208:     if (grep/^future$/,@{$status}) {
                   8209:         if ($start > $now) {
                   8210:             return 'ok';
                   8211:         }
                   8212:     }
                   8213:     return; 
                   8214: }
                   8215: 
                   8216: sub parse_portfolio_url {
                   8217:     my ($url) = @_;
                   8218: 
                   8219:     my ($type,$udom,$unum,$group,$file_name);
                   8220:     
1.823     albertel 8221:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 8222: 	$type = 1;
                   8223:         $udom = $1;
                   8224:         $unum = $2;
                   8225:         $file_name = $3;
1.823     albertel 8226:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 8227: 	$type = 2;
                   8228:         $udom = $1;
                   8229:         $unum = $2;
                   8230:         $group = $3;
                   8231:         $file_name = $3.'/'.$4;
                   8232:     }
                   8233:     if (wantarray) {
                   8234: 	return ($type,$udom,$unum,$file_name,$group);
                   8235:     }
                   8236:     return $type;
                   8237: }
                   8238: 
                   8239: sub is_portfolio_url {
                   8240:     my ($url) = @_;
                   8241:     return scalar(&parse_portfolio_url($url));
                   8242: }
                   8243: 
1.798     raeburn  8244: sub is_portfolio_file {
                   8245:     my ($file) = @_;
1.820     raeburn  8246:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  8247:         return 1;
                   8248:     }
                   8249:     return;
                   8250: }
                   8251: 
1.1504    raeburn  8252: sub is_coursetool_logo {
                   8253:     my ($uri) = @_;
                   8254:     if ($env{'request.course.id'}) {
                   8255:         my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   8256:         if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
                   8257:             return 1;
                   8258:         }
                   8259:     }
                   8260:     return;
                   8261: }
                   8262: 
1.976     raeburn  8263: sub usertools_access {
1.1084    raeburn  8264:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  8265:     my ($access,%tools);
                   8266:     if ($context eq '') {
                   8267:         $context = 'tools';
                   8268:     }
                   8269:     if ($context eq 'requestcourses') {
                   8270:         %tools = (
                   8271:                       official   => 1,
                   8272:                       unofficial => 1,
1.1006    raeburn  8273:                       community  => 1,
1.1246    raeburn  8274:                       textbook   => 1,
1.1305    raeburn  8275:                       placement  => 1,
1.1368    raeburn  8276:                       lti        => 1,
1.985     raeburn  8277:                  );
1.1183    raeburn  8278:     } elsif ($context eq 'requestauthor') {
                   8279:         %tools = (
                   8280:                       requestauthor => 1,
                   8281:                  );
1.1517    raeburn  8282:     } elsif ($context eq 'authordefaults') {
                   8283:         %tools = (
                   8284:                       webdav    => 1,
                   8285:                  );
1.985     raeburn  8286:     } else {
                   8287:         %tools = (
                   8288:                       aboutme   => 1,
                   8289:                       blog      => 1,
                   8290:                       portfolio => 1,
1.1517    raeburn  8291:                       portaccess => 1,
1.1489    raeburn  8292:                       timezone  => 1,
1.985     raeburn  8293:                  );
                   8294:     }
1.976     raeburn  8295:     return if (!defined($tools{$tool}));
                   8296: 
1.1242    raeburn  8297:     if (($udom eq '') || ($uname eq '')) {
1.976     raeburn  8298:         $udom = $env{'user.domain'};
                   8299:         $uname = $env{'user.name'};
                   8300:     }
                   8301: 
1.978     raeburn  8302:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   8303:         if ($action ne 'reload') {
1.985     raeburn  8304:             if ($context eq 'requestcourses') {
                   8305:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  8306:             } elsif ($context eq 'requestauthor') {
                   8307:                 return $env{'environment.canrequest.author'};
1.1517    raeburn  8308:             } elsif ($context eq 'authordefaults') {
                   8309:                 if ($tool eq 'webdav') {
                   8310:                     return $env{'environment.availabletools.'.$tool};
                   8311:                 }
1.985     raeburn  8312:             } else {
                   8313:                 return $env{'environment.availabletools.'.$tool};
                   8314:             }
                   8315:         }
1.976     raeburn  8316:     }
                   8317: 
1.1183    raeburn  8318:     my ($toolstatus,$inststatus,$envkey);
                   8319:     if ($context eq 'requestauthor') {
1.1517    raeburn  8320:         $envkey = $context;
                   8321:     } elsif ($context eq 'authordefaults') {
                   8322:         if ($tool eq 'webdav') {
                   8323:             $envkey = 'tools.'.$tool;
                   8324:         }
1.1183    raeburn  8325:     } else {
                   8326:         $envkey = $context.'.'.$tool;
                   8327:     }
1.976     raeburn  8328: 
1.985     raeburn  8329:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   8330:          ($action ne 'reload')) {
1.1183    raeburn  8331:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  8332:         $inststatus = $env{'environment.inststatus'};
                   8333:     } else {
1.1084    raeburn  8334:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  8335:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  8336:             $inststatus = $userenvref->{'inststatus'};
                   8337:         } else {
1.1183    raeburn  8338:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   8339:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  8340:             $inststatus = $userenv{'inststatus'};
                   8341:         }
1.976     raeburn  8342:     }
                   8343: 
                   8344:     if ($toolstatus ne '') {
                   8345:         if ($toolstatus) {
                   8346:             $access = 1;
                   8347:         } else {
                   8348:             $access = 0;
                   8349:         }
                   8350:         return $access;
                   8351:     }
                   8352: 
1.1084    raeburn  8353:     my ($is_adv,%domdef);
                   8354:     if (ref($is_advref) eq 'HASH') {
                   8355:         $is_adv = $is_advref->{'is_adv'};
                   8356:     } else {
                   8357:         $is_adv = &is_advanced_user($udom,$uname);
                   8358:     }
                   8359:     if (ref($domdefref) eq 'HASH') {
                   8360:         %domdef = %{$domdefref};
                   8361:     } else {
                   8362:         %domdef = &get_domain_defaults($udom);
                   8363:     }
1.976     raeburn  8364:     if (ref($domdef{$tool}) eq 'HASH') {
                   8365:         if ($is_adv) {
                   8366:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   8367:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   8368:                     $access = 1;
                   8369:                 } else {
                   8370:                     $access = 0;
                   8371:                 }
                   8372:                 return $access;
                   8373:             }
                   8374:         }
                   8375:         if ($inststatus ne '') {
                   8376:             my ($hasaccess,$hasnoaccess);
                   8377:             foreach my $affiliation (split(/:/,$inststatus)) {
                   8378:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   8379:                     if ($domdef{$tool}{$affiliation}) {
                   8380:                         $hasaccess = 1;
                   8381:                     } else {
                   8382:                         $hasnoaccess = 1;
                   8383:                     }
                   8384:                 }
                   8385:             }
                   8386:             if ($hasaccess || $hasnoaccess) {
                   8387:                 if ($hasaccess) {
                   8388:                     $access = 1;
                   8389:                 } elsif ($hasnoaccess) {
                   8390:                     $access = 0; 
                   8391:                 }
                   8392:                 return $access;
                   8393:             }
                   8394:         } else {
                   8395:             if ($domdef{$tool}{'default'} ne '') {
                   8396:                 if ($domdef{$tool}{'default'}) {
                   8397:                     $access = 1;
                   8398:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   8399:                     $access = 0;
                   8400:                 }
                   8401:                 return $access;
                   8402:             }
                   8403:         }
                   8404:     } else {
1.1177    raeburn  8405:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  8406:             $access = 1;
                   8407:         } else {
                   8408:             $access = 0;
                   8409:         }
1.976     raeburn  8410:         return $access;
                   8411:     }
                   8412: }
                   8413: 
1.1050    raeburn  8414: sub is_course_owner {
                   8415:     my ($cdom,$cnum,$udom,$uname) = @_;
                   8416:     if (($udom eq '') || ($uname eq '')) {
                   8417:         $udom = $env{'user.domain'};
                   8418:         $uname = $env{'user.name'};
                   8419:     }
                   8420:     unless (($udom eq '') || ($uname eq '')) {
                   8421:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   8422:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   8423:                 return 1;
                   8424:             } else {
1.1494    raeburn  8425:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
1.1050    raeburn  8426:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   8427:                     return 1;
                   8428:                 }
                   8429:             }
                   8430:         }
                   8431:     }
                   8432:     return;
                   8433: }
                   8434: 
1.976     raeburn  8435: sub is_advanced_user {
1.1512    raeburn  8436:     my ($udom,$uname,$nocache) = @_;
                   8437:     my ($is_adv,$is_author,$use_cache,$hashid);
1.1085    raeburn  8438:     if ($udom ne '' && $uname ne '') {
                   8439:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  8440:             if (wantarray) {
                   8441:                 return ($env{'user.adv'},$env{'user.author'});
                   8442:             } else {
                   8443:                 return $env{'user.adv'};
                   8444:             }
1.1512    raeburn  8445:         } elsif (!$nocache) {
                   8446:             $use_cache = 1;
                   8447:             $hashid = "$udom:$uname";  
                   8448:             my ($info,$cached)=&is_cached_new('isadvau',$hashid);
                   8449:             if ($cached) {
                   8450:                 ($is_adv,$is_author) = split(/:/,$info);
                   8451:                 if (wantarray) {
                   8452:                     return ($is_adv,$is_author);
                   8453:                 }
                   8454:                 return $is_adv; 
                   8455:             }
1.1085    raeburn  8456:         }
                   8457:     }
1.976     raeburn  8458:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   8459:     my %allroles;
                   8460:     foreach my $role (keys(%roleshash)) {
                   8461:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   8462:         my $area = '/'.$tdomain.'/'.$trest;
                   8463:         if ($sec ne '') {
                   8464:             $area .= '/'.$sec;
                   8465:         }
                   8466:         if (($area ne '') && ($trole ne '')) {
                   8467:             my $spec=$trole.'.'.$area;
                   8468:             if ($trole =~ /^cr\//) {
                   8469:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   8470:             } elsif ($trole ne 'gr') {
                   8471:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   8472:             }
1.1128    raeburn  8473:             if ($trole eq 'au') {
                   8474:                 $is_author = 1;
                   8475:             }
1.976     raeburn  8476:         }
                   8477:     }
                   8478:     foreach my $role (keys(%allroles)) {
                   8479:         last if ($is_adv);
                   8480:         foreach my $item (split(/:/,$allroles{$role})) {
                   8481:             if ($item ne '') {
                   8482:                 my ($privilege,$restrictions)=split(/&/,$item);
                   8483:                 if ($privilege eq 'adv') {
                   8484:                     $is_adv = 1;
                   8485:                     last;
                   8486:                 }
                   8487:             }
                   8488:         }
                   8489:     }
1.1512    raeburn  8490:     if ($use_cache) {
                   8491:         my $cachetime = 600;
                   8492:         &do_cache_new('isadvau',$hashid,$is_adv.':'.$is_author,$cachetime);
                   8493:     }
1.1128    raeburn  8494:     if (wantarray) {
                   8495:         return ($is_adv,$is_author);
                   8496:     }
1.976     raeburn  8497:     return $is_adv;
                   8498: }
1.798     raeburn  8499: 
1.1035    raeburn  8500: sub check_can_request {
1.1368    raeburn  8501:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035    raeburn  8502:     my $canreq = 0;
1.1368    raeburn  8503:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   8504:         $uname = $env{'user.name'};
                   8505:         $udom = $env{'user.domain'};
                   8506:     }
1.1035    raeburn  8507:     my ($types,$typename) = &Apache::loncommon::course_types();
                   8508:     my @options = ('approval','validate','autolimit');
                   8509:     my $optregex = join('|',@options);
                   8510:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
1.1486    raeburn  8511:         my %willtrust;
1.1035    raeburn  8512:         foreach my $type (@{$types}) {
1.1368    raeburn  8513:             if (&usertools_access($uname,$udom,$type,undef,
                   8514:                                   'requestcourses')) {
1.1035    raeburn  8515:                 $canreq ++;
1.1036    raeburn  8516:                 if (ref($request_domains) eq 'HASH') {
1.1368    raeburn  8517:                     push(@{$request_domains->{$type}},$udom);
1.1036    raeburn  8518:                 }
1.1368    raeburn  8519:                 if ($dom eq $udom) {
1.1035    raeburn  8520:                     $can_request->{$type} = 1;
                   8521:                 }
                   8522:             }
1.1368    raeburn  8523:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
                   8524:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035    raeburn  8525:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   8526:                 if (@curr > 0) {
1.1036    raeburn  8527:                     foreach my $item (@curr) {
                   8528:                         if (ref($request_domains) eq 'HASH') {
                   8529:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   8530:                             if ($otherdom ne '') {
1.1486    raeburn  8531:                                 unless (exists($willtrust{$otherdom})) {
                   8532:                                     $willtrust{$otherdom} = &will_trust('reqcrs',$env{'user.domain'},$otherdom);
                   8533:                                 }
                   8534:                                 if ($willtrust{$otherdom}) {
                   8535:                                     if (ref($request_domains->{$type}) eq 'ARRAY') {
                   8536:                                         unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   8537:                                             push(@{$request_domains->{$type}},$otherdom);
                   8538:                                         }
                   8539:                                     } else {
1.1036    raeburn  8540:                                         push(@{$request_domains->{$type}},$otherdom);
                   8541:                                     }
                   8542:                                 }
                   8543:                             }
                   8544:                         }
                   8545:                     }
1.1368    raeburn  8546:                     unless ($dom eq $env{'user.domain'}) {
1.1036    raeburn  8547:                         $canreq ++;
1.1035    raeburn  8548:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   8549:                             $can_request->{$type} = 1;
                   8550:                         }
                   8551:                     }
                   8552:                 }
                   8553:             }
                   8554:         }
                   8555:     }
                   8556:     return $canreq;
                   8557: }
                   8558: 
1.341     www      8559: # ---------------------------------------------- Custom access rule evaluation
                   8560: 
                   8561: sub customaccess {
                   8562:     my ($priv,$uri)=@_;
1.807     albertel 8563:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      8564:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 8565:     $udom = &LONCAPA::clean_domain($udom);
                   8566:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      8567:     my $access=0;
1.800     albertel 8568:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 8569: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   8570: 	if ($type eq 'user') {
                   8571: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 8572: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 8573: 		if ($tdom) {
                   8574: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   8575: 		}
1.896     albertel 8576: 		if ($tuname) {
                   8577: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 8578: 		}
                   8579: 		$access=($effect eq 'allow');
                   8580: 		last;
                   8581: 	    }
                   8582: 	} else {
                   8583: 	    if ($role) {
                   8584: 		if ($role ne $urole) { next; }
                   8585: 	    }
                   8586: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   8587: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   8588: 		if ($tdom) {
                   8589: 		    if ($tdom ne $udom) { next; }
                   8590: 		}
                   8591: 		if ($tcrs) {
                   8592: 		    if ($tcrs ne $ucrs) { next; }
                   8593: 		}
                   8594: 		if ($tsec) {
                   8595: 		    if ($tsec ne $usec) { next; }
                   8596: 		}
                   8597: 		$access=($effect eq 'allow');
                   8598: 		last;
                   8599: 	    }
                   8600: 	    if ($realm eq '' && $role eq '') {
                   8601: 		$access=($effect eq 'allow');
                   8602: 	    }
1.402     bowersj2 8603: 	}
1.341     www      8604:     }
                   8605:     return $access;
                   8606: }
                   8607: 
1.103     harris41 8608: # ------------------------------------------------- Check for a user privilege
1.12      www      8609: 
                   8610: sub allowed {
1.1461    raeburn  8611:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705     albertel 8612:     my $ver_orguri=$uri;
1.439     www      8613:     $uri=&deversion($uri);
1.152     www      8614:     my $orguri=$uri;
1.52      www      8615:     $uri=&declutter($uri);
1.809     raeburn  8616: 
1.810     raeburn  8617:     if ($priv eq 'evb') {
1.1478    raeburn  8618: # Evade communication block restrictions for specified role in a course or domain
1.810     raeburn  8619:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   8620:             return $1;
                   8621:         } else {
                   8622:             return;
                   8623:         }
                   8624:     }
                   8625: 
1.620     albertel 8626:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      8627: # Free bre access to adm and meta resources
1.1436    raeburn  8628:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
1.769     albertel 8629: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   8630: 	&& ($priv eq 'bre')) {
1.14      www      8631: 	return 'F';
1.159     www      8632:     }
                   8633: 
1.545     banghart 8634: # Free bre access to user's own portfolio contents
1.714     raeburn  8635:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  8636:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  8637: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  8638:         my %setters;
1.1473    raeburn  8639:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
                   8640:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
                   8641:         if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  8642:             return 'B';
                   8643:         } else {
                   8644:             return 'F';
                   8645:         }
1.545     banghart 8646:     }
                   8647: 
1.762     raeburn  8648: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  8649:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   8650:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   8651:         if (exists($env{'request.course.id'})) {
                   8652:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8653:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8654:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   8655:                 my $courseprivid=$env{'request.course.id'};
                   8656:                 $courseprivid=~s/\_/\//;
                   8657:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   8658:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   8659:                     return $1; 
1.762     raeburn  8660:                 } else {
                   8661:                     if ($env{'request.course.sec'}) {
                   8662:                         $courseprivid.='/'.$env{'request.course.sec'};
                   8663:                     }
                   8664:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   8665:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   8666:                         return $2;
                   8667:                     }
1.714     raeburn  8668:                 }
                   8669:             }
                   8670:         }
                   8671:     }
                   8672: 
1.159     www      8673: # Free bre to public access
                   8674: 
                   8675:     if ($priv eq 'bre') {
1.1362    raeburn  8676:         my $copyright;
                   8677:         unless ($uri =~ /ext\.tool/) {
                   8678:             $copyright=&metadata($uri,'copyright');
                   8679:         }
1.620     albertel 8680: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      8681:            return 'F'; 
                   8682:         }
1.238     www      8683:         if ($copyright eq 'priv') {
                   8684:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8685: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      8686: 		return '';
                   8687:             }
                   8688:         }
                   8689:         if ($copyright eq 'domain') {
                   8690:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8691: 	    unless (($env{'user.domain'} eq $1) ||
                   8692:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      8693: 		return '';
                   8694:             }
1.262     matthew  8695:         }
1.620     albertel 8696:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  8697:             # Library role, so allow browsing of resources in this domain.
                   8698:             return 'F';
1.238     www      8699:         }
1.341     www      8700:         if ($copyright eq 'custom') {
                   8701: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   8702:         }
1.14      www      8703:     }
1.264     matthew  8704:     # Domain coordinator is trying to create a course
1.620     albertel 8705:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  8706:         # uri is the requested domain in this case.
                   8707:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  8708:         # a role of dc for the domain in question.
1.620     albertel 8709:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  8710:     }
1.29      www      8711: 
1.52      www      8712:     my $thisallowed='';
                   8713:     my $statecond=0;
                   8714:     my $courseprivid='';
                   8715: 
1.1039    raeburn  8716:     my $ownaccess;
1.1043    raeburn  8717:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  8718:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   8719:         if ($uri eq '') {
                   8720:             $ownaccess = 1;
                   8721:         } else {
                   8722:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   8723:                 my $udom = $env{'user.domain'};
                   8724:                 my $uname = $env{'user.name'};
                   8725:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   8726:                     $ownaccess = 1;
1.1040    raeburn  8727:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  8728:                     unless ($uri =~ m{\.\./}) {
                   8729:                         $ownaccess = 1;
                   8730:                     }
                   8731:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   8732:                     my $now = time;
                   8733:                     if ($uri =~ m{^([^/]+)/?$}) {
                   8734:                         my $adom = $1;
                   8735:                         foreach my $key (keys(%env)) {
1.1042    raeburn  8736:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1452    raeburn  8737:                                 my ($start,$end) = split(/\./,$env{$key});
                   8738:                                 if (($now >= $start) && (!$end || $end > $now)) {
1.1039    raeburn  8739:                                     $ownaccess = 1;
                   8740:                                     last;
                   8741:                                 }
                   8742:                             }
                   8743:                         }
                   8744:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   8745:                         my $adom = $1;
                   8746:                         my $aname = $2;
1.1042    raeburn  8747:                         foreach my $role ('ca','aa') { 
                   8748:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   8749:                                 my ($start,$end) =
1.1452    raeburn  8750:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
                   8751:                                 if (($now >= $start) && (!$end || $end > $now)) {
1.1042    raeburn  8752:                                     $ownaccess = 1;
                   8753:                                     last;
                   8754:                                 }
1.1039    raeburn  8755:                             }
                   8756:                         }
                   8757:                     }
                   8758:                 }
                   8759:             }
                   8760:         }
                   8761:     }
                   8762: 
1.52      www      8763: # Course
                   8764: 
1.620     albertel 8765:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8766:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8767:             $thisallowed.=$1;
                   8768:         }
1.52      www      8769:     }
1.29      www      8770: 
1.52      www      8771: # Domain
                   8772: 
1.620     albertel 8773:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 8774:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8775:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8776:             $thisallowed.=$1;
                   8777:         }
1.12      www      8778:     }
1.52      www      8779: 
1.1141    raeburn  8780: # User who is not author or co-author might still be able to edit
                   8781: # resource of an author in the domain (e.g., if Domain Coordinator).
                   8782:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   8783:         (&allowed('mdc',$env{'request.course.id'}))) {
                   8784:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   8785:             $thisallowed.=$1;
                   8786:         }
                   8787:     }
                   8788: 
1.52      www      8789: # Course: uri itself is a course
1.66      www      8790:     my $courseuri=$uri;
                   8791:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      8792:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      8793: 
1.620     albertel 8794:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 8795:        =~/\Q$priv\E\&([^\:]*)/) {
1.1409    raeburn  8796:         if ($priv eq 'mip') {
                   8797:             my $rem = $1;
                   8798:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
                   8799:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
                   8800:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8801:                 if ($cdom ne '') {
1.1410    raeburn  8802:                     my %passwdconf = &get_passwdconf($cdom);
                   8803:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
                   8804:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
                   8805:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
                   8806:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
                   8807:                                 unless (@inststatuses) {
                   8808:                                     @inststatuses = ('default');
                   8809:                                 }
                   8810:                                 foreach my $status (@inststatuses) {
                   8811:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
                   8812:                                         $thisallowed.=$rem;
                   8813:                                     }
                   8814:                                 }
                   8815:                             }
                   8816:                         }
1.1409    raeburn  8817:                     }
                   8818:                 }
                   8819:             }
                   8820:         } else {
                   8821:             unless (($priv eq 'bro') && (!$ownaccess)) {
                   8822:                 $thisallowed.=$1;
                   8823:             }
1.1039    raeburn  8824:         }
1.12      www      8825:     }
1.29      www      8826: 
1.665     albertel 8827: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 8828: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 8829:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 8830: 	$thisallowed='';
1.671     raeburn  8831:         my ($match)=&is_on_map($uri);
                   8832:         if ($match) {
                   8833:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   8834:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1281    raeburn  8835:                 my $value = $1;
1.1461    raeburn  8836:                 my $deeplinkblock;
                   8837:                 unless ($nodeeplinkcheck) {
                   8838:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8839:                 }
1.1402    raeburn  8840:                 if ($deeplinkblock) {
                   8841:                     $thisallowed='D';
                   8842:                 } elsif ($noblockcheck) {
1.1281    raeburn  8843:                     $thisallowed.=$value;
1.1162    raeburn  8844:                 } else {
1.1424    raeburn  8845:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8846:                     if (@blockers > 0) {
                   8847:                         $thisallowed = 'B';
                   8848:                     } else {
                   8849:                         $thisallowed.=$value;
                   8850:                     }
1.1162    raeburn  8851:                 }
1.671     raeburn  8852:             }
                   8853:         } else {
1.705     albertel 8854:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  8855:             if ($refuri) {
                   8856:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  8857:                     $thisallowed='F';
1.671     raeburn  8858:                 } else {
                   8859:                     $refuri=&declutter($refuri);
                   8860:                     my ($match) = &is_on_map($refuri);
                   8861:                     if ($match) {
1.1461    raeburn  8862:                         my $deeplinkblock;
                   8863:                         unless ($nodeeplinkcheck) {
                   8864:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8865:                         }
1.1402    raeburn  8866:                         if ($deeplinkblock) {
                   8867:                             $thisallowed='D';
                   8868:                         } elsif ($noblockcheck) {
1.1281    raeburn  8869:                             $thisallowed='F';
1.1162    raeburn  8870:                         } else {
1.1426    raeburn  8871:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  8872:                             if (@blockers > 0) {
                   8873:                                 $thisallowed = 'B';
                   8874:                             } else {
                   8875:                                 $thisallowed='F';
                   8876:                             }
1.1162    raeburn  8877:                         }
1.671     raeburn  8878:                     }
1.669     raeburn  8879:                 }
1.671     raeburn  8880:             }
                   8881:         }
1.314     www      8882:     }
1.492     albertel 8883: 
1.766     albertel 8884:     if ($priv eq 'bre'
                   8885: 	&& $thisallowed ne 'F' 
                   8886: 	&& $thisallowed ne '2'
                   8887: 	&& &is_portfolio_url($uri)) {
1.1270    raeburn  8888: 	$thisallowed = &portfolio_access($uri,$clientip);
1.766     albertel 8889:     }
1.1250    raeburn  8890: 
1.52      www      8891: # Full access at system, domain or course-wide level? Exit.
1.29      www      8892:     if ($thisallowed=~/F/) {
                   8893: 	return 'F';
                   8894:     }
                   8895: 
1.52      www      8896: # If this is generating or modifying users, exit with special codes
1.29      www      8897: 
1.1517    raeburn  8898:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643     www      8899: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 8900: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      8901: # no author name given, so this just checks on the general right to make a co-author in this domain
                   8902: 	    unless ($auname) { return $thisallowed; }
                   8903: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 8904: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   8905: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   8906: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1517    raeburn  8907: 	} elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
                   8908:             my ($audom,$auname)=split('/',$uri);
                   8909:             unless ($auname) { return $thisallowed; }
                   8910:             unless (($env{'request.role'} eq "dc./$audom") ||
                   8911:                     ($env{'request.role'} eq "ca./$uri")) {
                   8912:                 return '';
                   8913:             }
1.642     albertel 8914: 	}
1.52      www      8915: 	return $thisallowed;
                   8916:     }
                   8917: #
1.103     harris41 8918: # Gathered so far: system, domain and course wide privileges
1.52      www      8919: #
                   8920: # Course: See if uri or referer is an individual resource that is part of 
                   8921: # the course
                   8922: 
1.620     albertel 8923:     if ($env{'request.course.id'}) {
1.232     www      8924: 
1.1504    raeburn  8925:         if ($priv eq 'bre') {
                   8926:             if (&is_coursetool_logo($uri)) {
                   8927:                 return 'F';
                   8928:             }
                   8929:         }
                   8930: 
1.1409    raeburn  8931: # If this is modifying password (internal auth) domains must match for user and user's role.
                   8932: 
                   8933:         if ($priv eq 'mip') {
                   8934:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
                   8935:                 return $thisallowed;
                   8936:             } else {
                   8937:                 return '';
                   8938:             }
                   8939:         }
                   8940: 
1.620     albertel 8941:        $courseprivid=$env{'request.course.id'};
                   8942:        if ($env{'request.course.sec'}) {
                   8943:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      8944:        }
                   8945:        $courseprivid=~s/\_/\//;
                   8946:        my $checkreferer=1;
1.232     www      8947:        my ($match,$cond)=&is_on_map($uri);
                   8948:        if ($match) {
                   8949:            $statecond=$cond;
1.620     albertel 8950:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8951:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8952:                my $value = $1;
                   8953:                if ($priv eq 'bre') {
1.1461    raeburn  8954:                    my $deeplinkblock;
                   8955:                    unless ($nodeeplinkcheck) {
                   8956:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8957:                    }
1.1458    raeburn  8958:                    if ($deeplinkblock) {
                   8959:                        $thisallowed = 'D';
                   8960:                    } elsif ($noblockcheck) {
1.1281    raeburn  8961:                        $thisallowed.=$value;
1.1162    raeburn  8962:                    } else {
1.1424    raeburn  8963:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8964:                        if (@blockers > 0) {
                   8965:                            $thisallowed = 'B';
                   8966:                        } else {
                   8967:                            $thisallowed.=$value;
                   8968:                        }
1.1162    raeburn  8969:                    }
                   8970:                } else {
                   8971:                    $thisallowed.=$value;
                   8972:                }
1.52      www      8973:                $checkreferer=0;
                   8974:            }
1.29      www      8975:        }
1.1424    raeburn  8976: 
1.148     www      8977:        if ($checkreferer) {
1.620     albertel 8978: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      8979:             unless ($refuri) {
1.800     albertel 8980:                 foreach my $key (keys(%env)) {
                   8981: 		    if ($key=~/^httpref\..*\*/) {
                   8982: 			my $pattern=$key;
1.156     www      8983:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      8984:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   8985:                         $pattern=~s/\//\\\//g;
1.152     www      8986:                         if ($orguri=~/$pattern/) {
1.800     albertel 8987: 			    $refuri=$env{$key};
1.148     www      8988:                         }
                   8989:                     }
1.191     harris41 8990:                 }
1.148     www      8991:             }
1.232     www      8992: 
1.148     www      8993:          if ($refuri) { 
1.152     www      8994: 	  $refuri=&declutter($refuri);
1.232     www      8995:           my ($match,$cond)=&is_on_map($refuri);
                   8996:             if ($match) {
                   8997:               my $refstatecond=$cond;
1.620     albertel 8998:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8999:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  9000:                   my $value = $1;
                   9001:                   if ($priv eq 'bre') {
1.1461    raeburn  9002:                       my $deeplinkblock;
                   9003:                       unless ($nodeeplinkcheck) {
                   9004:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   9005:                       }
1.1402    raeburn  9006:                       if ($deeplinkblock) {
                   9007:                           $thisallowed = 'D';
                   9008:                       } elsif ($noblockcheck) {
1.1281    raeburn  9009:                           $thisallowed.=$value;
1.1162    raeburn  9010:                       } else {
1.1426    raeburn  9011:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  9012:                           if (@blockers > 0) {
                   9013:                               $thisallowed = 'B';
                   9014:                           } else {
                   9015:                               $thisallowed.=$value;
                   9016:                           }
1.1162    raeburn  9017:                       }
                   9018:                   } else {
                   9019:                       $thisallowed.=$value;
                   9020:                   }
1.53      www      9021:                   $uri=$refuri;
                   9022:                   $statecond=$refstatecond;
1.52      www      9023:               }
                   9024:           }
1.148     www      9025:         }
1.29      www      9026:        }
1.52      www      9027:    }
1.29      www      9028: 
1.52      www      9029: #
1.103     harris41 9030: # Gathered now: all privileges that could apply, and condition number
1.52      www      9031: # 
                   9032: #
                   9033: # Full or no access?
                   9034: #
1.29      www      9035: 
1.52      www      9036:     if ($thisallowed=~/F/) {
                   9037: 	return 'F';
                   9038:     }
1.29      www      9039: 
1.52      www      9040:     unless ($thisallowed) {
                   9041:         return '';
                   9042:     }
1.29      www      9043: 
1.52      www      9044: # Restrictions exist, deal with them
                   9045: #
                   9046: #   C:according to course preferences
                   9047: #   R:according to resource settings
                   9048: #   L:unless locked
                   9049: #   X:according to user session state
                   9050: #
                   9051: 
                   9052: # Possibly locked functionality, check all courses
1.1454    raeburn  9053: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54      www      9054: # Locks might take effect only after 10 minutes cache expiration for other
1.1454    raeburn  9055: # courses, and 2 minutes for current course, in which user has st or ta role
                   9056: # which is neither expired nor a future role (unless current course).
1.52      www      9057: 
1.1454    raeburn  9058:     my ($needlockcheck,$now,$crsonly);
1.52      www      9059:     if ($thisallowed=~/L/) {
1.1454    raeburn  9060:         $now = time;
                   9061:         if ($priv eq 'bre') {
                   9062:             if ($uri ne '') {
                   9063:                 if ($orguri =~ m{^/+res/}) {
                   9064:                     if ($uri =~ m{^lib/templates/}) {
                   9065:                         if ($env{'request.course.id'}) {
                   9066:                             $crsonly = 1;
                   9067:                             $needlockcheck = 1;
                   9068:                         }
                   9069:                     } else {
                   9070:                         $needlockcheck = 1;
                   9071:                     }
                   9072:                 } elsif ($env{'request.course.id'}) {
                   9073:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
                   9074:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
                   9075:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
                   9076:                         $crsonly = 1;
                   9077:                     }
                   9078:                     $needlockcheck = 1;
                   9079:                 }
                   9080:             }
                   9081:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
                   9082:             $needlockcheck = 1;
                   9083:         }
                   9084:     }
                   9085:     if ($needlockcheck) {
1.1453    raeburn  9086:         foreach my $envkey (keys(%env)) {
1.54      www      9087:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   9088:                my $courseid=$2;
                   9089:                my $roleid=$1.'.'.$2;
1.92      www      9090:                $courseid=~s/^\///;
1.1453    raeburn  9091:                unless ($env{'request.role'} eq $roleid) {
                   9092:                    my ($start,$end) = split(/\./,$env{$envkey});
                   9093:                    next unless (($now >= $start) && (!$end || $end > $now));
                   9094:                }
1.54      www      9095:                my $expiretime=600;
1.620     albertel 9096:                if ($env{'request.role'} eq $roleid) {
1.54      www      9097: 		  $expiretime=120;
                   9098:                }
                   9099: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   9100:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 9101:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 9102: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      9103:                }
1.620     albertel 9104:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   9105:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   9106: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   9107:                        &log($env{'user.domain'},$env{'user.name'},
                   9108:                             $env{'user.home'},
1.57      www      9109:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      9110:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 9111:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      9112: 		       return '';
                   9113:                    }
                   9114:                }
1.620     albertel 9115:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   9116:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1455    raeburn  9117: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620     albertel 9118:                        &log($env{'user.domain'},$env{'user.name'},
                   9119:                             $env{'user.home'},
1.57      www      9120:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      9121:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 9122:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      9123: 		       return '';
                   9124:                    }
                   9125:                }
                   9126: 	   }
1.29      www      9127:        }
1.52      www      9128:     }
1.1424    raeburn  9129: 
1.52      www      9130: #
                   9131: # Rest of the restrictions depend on selected course
                   9132: #
                   9133: 
1.620     albertel 9134:     unless ($env{'request.course.id'}) {
1.766     albertel 9135: 	if ($thisallowed eq 'A') {
                   9136: 	    return 'A';
1.814     raeburn  9137:         } elsif ($thisallowed eq 'B') {
                   9138:             return 'B';
1.766     albertel 9139: 	} else {
                   9140: 	    return '1';
                   9141: 	}
1.52      www      9142:     }
1.29      www      9143: 
1.52      www      9144: #
                   9145: # Now user is definitely in a course
                   9146: #
1.53      www      9147: 
                   9148: 
                   9149: # Course preferences
                   9150: 
                   9151:    if ($thisallowed=~/C/) {
1.620     albertel 9152:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   9153:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   9154:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 9155: 	   =~/\Q$rolecode\E/) {
1.1304    raeburn  9156: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 9157: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   9158: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   9159: 			$env{'request.course.id'});
                   9160: 	   }
1.237     www      9161:            return '';
                   9162:        }
                   9163: 
1.620     albertel 9164:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 9165: 	   =~/\Q$unamedom\E/) {
1.1304    raeburn  9166: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 9167: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   9168: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   9169: 			$env{'request.course.id'});
                   9170: 	   }
1.54      www      9171:            return '';
                   9172:        }
1.53      www      9173:    }
                   9174: 
                   9175: # Resource preferences
                   9176: 
                   9177:    if ($thisallowed=~/R/) {
1.620     albertel 9178:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 9179:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  9180: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 9181: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   9182: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   9183: 	   }
                   9184: 	   return '';
1.54      www      9185:        }
1.53      www      9186:    }
1.30      www      9187: 
1.1461    raeburn  9188: # Restricted for deeplinked session?
                   9189: 
                   9190:     if ($env{'request.deeplink.login'}) {
                   9191:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
                   9192:             if (!$symb) { $symb=&symbread($uri,1); }
                   9193:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
                   9194:                 return '';
                   9195:             }
                   9196:         }
                   9197:     }
                   9198: 
1.246     www      9199: # Restricted by state or randomout?
1.30      www      9200: 
1.52      www      9201:    if ($thisallowed=~/X/) {
1.620     albertel 9202:       if ($env{'acc.randomout'}) {
1.579     albertel 9203: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 9204:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      9205:             return ''; 
                   9206:          }
1.247     www      9207:       }
                   9208:       if (&condval($statecond)) {
1.52      www      9209: 	 return '2';
                   9210:       } else {
                   9211:          return '';
                   9212:       }
                   9213:    }
1.30      www      9214: 
1.766     albertel 9215:     if ($thisallowed eq 'A') {
                   9216: 	return 'A';
1.814     raeburn  9217:     } elsif ($thisallowed eq 'B') {
                   9218:         return 'B';
1.1402    raeburn  9219:     } elsif ($thisallowed eq 'D') {
                   9220:         return 'D';
1.766     albertel 9221:     }
1.52      www      9222:    return 'F';
1.232     www      9223: }
1.1162    raeburn  9224: 
1.1192    raeburn  9225: # ------------------------------------------- Check construction space access
                   9226: 
                   9227: sub constructaccess {
                   9228:     my ($url,$setpriv)=@_;
                   9229: 
                   9230: # We do not allow editing of previous versions of files
                   9231:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   9232: 
                   9233: # Get username and domain from URL
                   9234:     my ($ownername,$ownerdomain,$ownerhome);
                   9235: 
                   9236:     ($ownerdomain,$ownername) =
1.1325    raeburn  9237:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192    raeburn  9238: 
                   9239: # The URL does not really point to any authorspace, forget it
                   9240:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   9241: 
                   9242: # Now we need to see if the user has access to the authorspace of
                   9243: # $ownername at $ownerdomain
                   9244: 
                   9245:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   9246: # Real author for this?
                   9247:        $ownerhome = $env{'user.home'};
                   9248:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   9249:           return ($ownername,$ownerdomain,$ownerhome);
                   9250:        }
1.1506    raeburn  9251:     } elsif (&is_course($ownerdomain,$ownername)) {
                   9252: # Course Authoring Space?
1.1312    raeburn  9253:         if ($env{'request.course.id'}) {
                   9254:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
                   9255:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
                   9256:                 if (&allowed('mdc',$env{'request.course.id'})) {
1.1520    raeburn  9257:                     return if ($env{'course.'.$env{'request.course.id'}.'.internal.crsauthor'} eq '0');
                   9258:                     unless ($env{'course.'.$env{'request.course.id'}.'.internal.crsauthor'}) {
                   9259:                         my %domdefs = &get_domain_defaults($ownerdomain);
                   9260:                         my $type = lc($env{'course.'.$env{'request.course.id'}.'.type'});
                   9261:                         unless (($type eq 'community') || ($type eq 'placement')) {
                   9262:                             $type = 'unofficial';
                   9263:                             if ($env{'course.'.$env{'request.course.id'}.'internal.coursecode'} ne '') {
                   9264:                                 $type = 'official';
                   9265:                             } elsif ($env{'course.'.$env{'request.course.id'}.'internal.textbook'} ne '') {
                   9266:                                 $type = 'textbook';
                   9267:                             } else {
                   9268:                                 $type = 'unofficial';
                   9269:                             }
                   9270:                         }
                   9271:                         return if ($domdefs{$type.'crsauthor'} eq '0');
                   9272:                     }
1.1312    raeburn  9273:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
                   9274:                     return ($ownername,$ownerdomain,$ownerhome);
                   9275:                 }
                   9276:             }
                   9277:         }
1.1506    raeburn  9278:         return '';
                   9279:     } else {
                   9280: # Co-author for this?
                   9281:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   9282:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   9283:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   9284:             return ($ownername,$ownerdomain,$ownerhome);
                   9285:         }
1.1192    raeburn  9286:     }
                   9287: 
                   9288: # We don't have any access right now. If we are not possibly going to do anything about this,
                   9289: # we might as well leave
                   9290:    unless ($setpriv) { return ''; }
                   9291: 
                   9292: # Backdoor access?
                   9293:     my $allowed=&allowed('eco',$ownerdomain);
                   9294: # Nope
                   9295:     unless ($allowed) { return ''; }
                   9296: # Looks like we may have access, but could be locked by the owner of the construction space
                   9297:     if ($allowed eq 'U') {
                   9298:         my %blocked=&get('environment',['domcoord.author'],
                   9299:                          $ownerdomain,$ownername);
                   9300: # Is blocked by owner
                   9301:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   9302:     }
                   9303:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   9304: # Grant temporary access
                   9305:         my $then=$env{'user.login.time'};
1.1209    raeburn  9306:         my $update=$env{'user.update.time'};
1.1192    raeburn  9307:         if (!$update) { $update = $then; }
                   9308:         my $refresh=$env{'user.refresh.time'};
                   9309:         if (!$refresh) { $refresh = $update; }
                   9310:         my $now = time;
                   9311:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   9312:                            $now,'ca','constructaccess');
                   9313:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   9314:         return($ownername,$ownerdomain,$ownerhome);
                   9315:     }
                   9316: # No business here
                   9317:     return '';
                   9318: }
                   9319: 
1.1282    raeburn  9320: # ----------------------------------------------------------- Content Blocking
                   9321: 
                   9322: {
                   9323: # Caches for faster Course Contents display where content blocking
                   9324: # is in operation (i.e., interval param set) for timed quiz.
                   9325: #
                   9326: # User for whom data are being temporarily cached.
                   9327: my $cacheduser='';
1.1424    raeburn  9328: # Course for which data are being temporarily cached.
                   9329: my $cachedcid='';
1.1282    raeburn  9330: # Cached blockers for this user (a hash of blocking items). 
                   9331: my %cachedblockers=();
                   9332: # When the data were last cached.
                   9333: my $cachedlast='';
                   9334: 
                   9335: sub load_all_blockers {
1.1427    raeburn  9336:     my ($uname,$udom)=@_;
1.1282    raeburn  9337:     if (($uname ne '') && ($udom ne '')) { 
                   9338:         if (($cacheduser eq $uname.':'.$udom) &&
1.1424    raeburn  9339:             ($cachedcid eq $env{'request.course.id'}) &&
1.1427    raeburn  9340:             (abs($cachedlast-time)<5)) {
1.1282    raeburn  9341:             return;
                   9342:         }
                   9343:     }
                   9344:     $cachedlast=time;
                   9345:     $cacheduser=$uname.':'.$udom;
1.1424    raeburn  9346:     $cachedcid=$env{'request.course.id'};
1.1427    raeburn  9347:     %cachedblockers = &get_commblock_resources();
1.1424    raeburn  9348:     return;
1.1282    raeburn  9349: }
                   9350: 
1.1162    raeburn  9351: sub get_comm_blocks {
                   9352:     my ($cdom,$cnum) = @_;
                   9353:     if ($cdom eq '' || $cnum eq '') {
                   9354:         return unless ($env{'request.course.id'});
                   9355:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9356:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9357:     }
                   9358:     my %commblocks;
                   9359:     my $hashid=$cdom.'_'.$cnum;
                   9360:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   9361:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   9362:         %commblocks = %{$blocksref};
                   9363:     } else {
1.1494    raeburn  9364:         %commblocks = &dump('comm_block',$cdom,$cnum);
1.1162    raeburn  9365:         my $cachetime = 600;
                   9366:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   9367:     }
                   9368:     return %commblocks;
                   9369: }
                   9370: 
1.1282    raeburn  9371: sub get_commblock_resources {
                   9372:     my ($blocks) = @_;
                   9373:     my %blockers = ();
                   9374:     return %blockers unless ($env{'request.course.id'});
1.1470    raeburn  9375:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   9376:     if ($env{'request.course.sec'}) {
                   9377:         $courseurl .= '/'.$env{'request.course.sec'};
                   9378:     }
                   9379:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162    raeburn  9380:     my %commblocks;
                   9381:     if (ref($blocks) eq 'HASH') {
                   9382:         %commblocks = %{$blocks};
                   9383:     } else {
                   9384:         %commblocks = &get_comm_blocks();
                   9385:     }
1.1282    raeburn  9386:     return %blockers unless (keys(%commblocks) > 0); 
                   9387:     my $navmap = Apache::lonnavmaps::navmap->new();
                   9388:     return %blockers unless (ref($navmap));
1.1162    raeburn  9389:     my $now = time;
                   9390:     foreach my $block (keys(%commblocks)) {
                   9391:         if ($block =~ /^(\d+)____(\d+)$/) {
                   9392:             my ($start,$end) = ($1,$2);
                   9393:             if ($start <= $now && $end >= $now) {
                   9394:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   9395:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   9396:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282    raeburn  9397:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   9398:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
1.1162    raeburn  9399:                             }
                   9400:                         }
                   9401:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282    raeburn  9402:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   9403:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162    raeburn  9404:                             }
                   9405:                         }
                   9406:                     }
                   9407:                 }
                   9408:             }
                   9409:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   9410:             my $item = $1;
                   9411:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   9412:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1471    raeburn  9413:                     my (@interval,$mapname);
1.1282    raeburn  9414:                     my $type = 'map';
                   9415:                     if ($item eq 'course') {
                   9416:                         $type = 'course';
                   9417:                         @interval=&EXT("resource.0.interval");
                   9418:                     } else {
                   9419:                         if ($item =~ /___\d+___/) {
                   9420:                             $type = 'resource';
                   9421:                             @interval=&EXT("resource.0.interval",$item);
1.1162    raeburn  9422:                         } else {
1.1471    raeburn  9423:                             $mapname = &deversion($item);
                   9424:                             if (ref($navmap)) {
                   9425:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
                   9426:                                 @interval = ($timelimit,'map');
1.1162    raeburn  9427:                             }
                   9428:                         }
1.1282    raeburn  9429:                     }
1.1310    raeburn  9430:                     if ($interval[0] =~ /^(\d+)/) {
1.1308    raeburn  9431:                         my $timelimit = $1; 
1.1282    raeburn  9432:                         my $first_access;
                   9433:                         if ($type eq 'resource') {
                   9434:                             $first_access=&get_first_access($interval[1],$item);
                   9435:                         } elsif ($type eq 'map') {
                   9436:                             $first_access=&get_first_access($interval[1],undef,$item);
                   9437:                         } else {
                   9438:                             $first_access=&get_first_access($interval[1]);
                   9439:                         }
                   9440:                         if ($first_access) {
1.1292    raeburn  9441:                             my $timesup = $first_access+$timelimit;
1.1282    raeburn  9442:                             if ($timesup > $now) {
                   9443:                                 my $activeblock;
1.1471    raeburn  9444:                                 if ($type eq 'resource') {
                   9445:                                     if (ref($navmap)) {
                   9446:                                         my $res = $navmap->getBySymb($item);
                   9447:                                         if ($res->answerable()) {
                   9448:                                             $activeblock = 1;
                   9449:                                         }
                   9450:                                     }
                   9451:                                 } elsif ($type eq 'map') {
                   9452:                                     my $mapsymb = &symbread($mapname,1);
                   9453:                                     if (($mapsymb) && (ref($navmap))) {
                   9454:                                         my $mapres = $navmap->getBySymb($mapsymb);
                   9455:                                         if (ref($mapres)) {
                   9456:                                             my $first = $mapres->map_start();
                   9457:                                             my $finish = $mapres->map_finish();
                   9458:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
                   9459:                                             if (ref($it)) {
                   9460:                                                 my $res;
                   9461:                                                 while ($res = $it->next(undef,1)) {
                   9462:                                                     next unless (ref($res));
                   9463:                                                     my $symb = $res->symb();
                   9464:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
                   9465:                                                     @interval=&EXT("resource.0.interval",$symb);
                   9466:                                                     if ($interval[1] eq 'map') {
                   9467:                                                         if ($res->answerable()) {
                   9468:                                                             $activeblock = 1;
                   9469:                                                             last;
                   9470:                                                         }
                   9471:                                                     }
                   9472:                                                 }
                   9473:                                             }
                   9474:                                         }
1.1282    raeburn  9475:                                     }
                   9476:                                 }
                   9477:                                 if ($activeblock) {
                   9478:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   9479:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   9480:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
                   9481:                                          }
                   9482:                                     }
                   9483:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   9484:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   9485:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163    raeburn  9486:                                         }
1.1162    raeburn  9487:                                     }
                   9488:                                 }
                   9489:                             }
                   9490:                         }
                   9491:                     }
                   9492:                 }
                   9493:             }
                   9494:         }
                   9495:     }
1.1282    raeburn  9496:     return %blockers;
1.1162    raeburn  9497: }
                   9498: 
1.1282    raeburn  9499: sub has_comm_blocking {
1.1427    raeburn  9500:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1282    raeburn  9501:     my @blockers;
                   9502:     return unless ($env{'request.course.id'});
                   9503:     return unless ($priv eq 'bre');
                   9504:     return if ($env{'request.state'} eq 'construct');
1.1470    raeburn  9505:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   9506:     if ($env{'request.course.sec'}) {
                   9507:         $courseurl .= '/'.$env{'request.course.sec'};
                   9508:     }
                   9509:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1427    raeburn  9510:     my %blockinfo;
                   9511:     if (ref($blocks) eq 'HASH') {
                   9512:         %blockinfo = &get_commblock_resources($blocks);
                   9513:     } else {
                   9514:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
                   9515:         %blockinfo = %cachedblockers;
                   9516:     }
                   9517:     return unless (keys(%blockinfo) > 0);
1.1282    raeburn  9518:     my (%possibles,@symbs);
                   9519:     if (!$symb) {
1.1427    raeburn  9520:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162    raeburn  9521:     }
1.1282    raeburn  9522:     if ($symb) {
                   9523:         @symbs = ($symb);
                   9524:     } elsif (keys(%possibles)) { 
                   9525:         @symbs = keys(%possibles);
                   9526:     }
                   9527:     my $noblock;
                   9528:     foreach my $symb (@symbs) {
                   9529:         last if ($noblock);
                   9530:         my ($map,$resid,$resurl)=&decode_symb($symb);
1.1427    raeburn  9531:         foreach my $block (keys(%blockinfo)) {
1.1282    raeburn  9532:             if ($block =~ /^firstaccess____(.+)$/) {
                   9533:                 my $item = $1;
1.1424    raeburn  9534:                 unless ($blocked) {
                   9535:                     if (($item eq $map) || ($item eq $symb)) {
                   9536:                         $noblock = 1;
                   9537:                         last;
                   9538:                     }
1.1282    raeburn  9539:                 }
                   9540:             }
1.1427    raeburn  9541:             if (ref($blockinfo{$block}) eq 'HASH') {
                   9542:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
                   9543:                     if ($blockinfo{$block}{'resources'}{$symb}) {
1.1282    raeburn  9544:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   9545:                             push(@blockers,$block);
                   9546:                         }
                   9547:                     }
                   9548:                 }
1.1427    raeburn  9549:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
                   9550:                     if ($blockinfo{$block}{'maps'}{$map}) {
1.1424    raeburn  9551:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   9552:                             push(@blockers,$block);
                   9553:                         }
1.1282    raeburn  9554:                     }
                   9555:                 }
1.1162    raeburn  9556:             }
                   9557:         }
                   9558:     }
1.1424    raeburn  9559:     unless ($noblock) { 
                   9560:         return @blockers;
                   9561:     }
                   9562:     return;
1.1282    raeburn  9563: }
1.1162    raeburn  9564: }
                   9565: 
1.1402    raeburn  9566: sub deeplink_check {
                   9567:     my ($priv,$symb,$uri) = @_;
                   9568:     return unless ($env{'request.course.id'});
                   9569:     return unless ($priv eq 'bre');
                   9570:     return if ($env{'request.state'} eq 'construct');
                   9571:     return if ($env{'request.role.adv'});
                   9572:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9573:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9574:     my (%possibles,@symbs);
                   9575:     if (!$symb) {
                   9576:         $symb = &symbread($uri,1,1,1,\%possibles);
                   9577:     }
                   9578:     if ($symb) {
                   9579:         @symbs = ($symb);
                   9580:     } elsif (keys(%possibles)) {
                   9581:         @symbs = keys(%possibles);
                   9582:     }
                   9583: 
1.1461    raeburn  9584:     my ($deeplink_symb,$allow);
                   9585:     if ($env{'request.deeplink.login'}) {
                   9586:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
1.1402    raeburn  9587:     }
                   9588:     foreach my $symb (@symbs) {
                   9589:         last if ($allow);
                   9590:         my $deeplink = &EXT("resource.0.deeplink",$symb);
                   9591:         if ($deeplink eq '') {
                   9592:             $allow = 1;
                   9593:         } else {
1.1463    raeburn  9594:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
                   9595:             if ($state ne 'only') {
1.1402    raeburn  9596:                 $allow = 1;
1.1463    raeburn  9597:             } else {
                   9598:                 my $check_deeplink_entry;
                   9599:                 if ($protect ne 'none') {
                   9600:                     my ($acctype,$item) = split(/:/,$protect);
                   9601:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
                   9602:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
                   9603:                             $check_deeplink_entry = 1
                   9604:                         }
                   9605:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
                   9606:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
                   9607:                             $check_deeplink_entry = 1;
                   9608:                         }
                   9609:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
                   9610:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
                   9611:                             $check_deeplink_entry = 1;
                   9612:                         }
                   9613:                     }
                   9614:                 }
                   9615:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
1.1402    raeburn  9616:                     if ($scope eq 'res') {
1.1461    raeburn  9617:                         if ($symb eq $deeplink_symb) {
1.1402    raeburn  9618:                             $allow = 1;
                   9619:                         }
1.1459    raeburn  9620:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
1.1463    raeburn  9621:                         my ($map_from_symb,$map_from_login);
1.1461    raeburn  9622:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
                   9623:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
                   9624:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
                   9625:                         } else {
                   9626:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
                   9627:                         }
1.1459    raeburn  9628:                         if (($map_from_symb) && ($map_from_login)) {
                   9629:                             if ($map_from_symb eq $map_from_login) {
                   9630:                                 $allow = 1;
                   9631:                             } elsif ($scope eq 'rec') {
                   9632:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
                   9633:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
                   9634:                                     $allow = 1;
                   9635:                                 }
                   9636:                             }
                   9637:                         }
1.1402    raeburn  9638:                     }
                   9639:                 }
                   9640:             }
                   9641:         }
                   9642:     }
                   9643:     return if ($allow);
                   9644:     return 1;
                   9645: }
                   9646: 
1.1282    raeburn  9647: # -------------------------------- Deversion and split uri into path an filename   
                   9648: 
1.1133    foxr     9649: #
1.1282    raeburn  9650: #   Removes the version from a URI and
1.1133    foxr     9651: #   splits it in to its filename and path to the filename.
                   9652: #   Seems like File::Basename could have done this more clearly.
                   9653: #   Parameters:
                   9654: #      $uri   - input URI
                   9655: #   Returns:
                   9656: #     Two element list consisting of 
                   9657: #     $pathname  - the URI up to and excluding the trailing /
                   9658: #     $filename  - The part of the URI following the last /
                   9659: #  NOTE:
                   9660: #    Another realization of this is simply:
                   9661: #    use File::Basename;
                   9662: #    ...
                   9663: #    $uri = shift;
                   9664: #    $filename = basename($uri);
                   9665: #    $path     = dirname($uri);
                   9666: #    return ($filename, $path);
                   9667: #
                   9668: #     The implementation below is probably faster however.
                   9669: #
1.710     albertel 9670: sub split_uri_for_cond {
                   9671:     my $uri=&deversion(&declutter(shift));
                   9672:     my @uriparts=split(/\//,$uri);
                   9673:     my $filename=pop(@uriparts);
                   9674:     my $pathname=join('/',@uriparts);
                   9675:     return ($pathname,$filename);
                   9676: }
1.232     www      9677: # --------------------------------------------------- Is a resource on the map?
                   9678: 
                   9679: sub is_on_map {
1.710     albertel 9680:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 9681:     #Trying to find the conditional for the file
1.620     albertel 9682:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 9683: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      9684:     if ($match) {
1.289     bowersj2 9685: 	return (1,$1);
                   9686:     } else {
1.434     www      9687: 	return (0,0);
1.289     bowersj2 9688:     }
1.12      www      9689: }
                   9690: 
1.427     www      9691: # --------------------------------------------------------- Get symb from alias
                   9692: 
                   9693: sub get_symb_from_alias {
                   9694:     my $symb=shift;
                   9695:     my ($map,$resid,$url)=&decode_symb($symb);
                   9696: # Already is a symb
                   9697:     if ($url) { return $symb; }
                   9698: # Must be an alias
                   9699:     my $aliassymb='';
                   9700:     my %bighash;
1.620     albertel 9701:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      9702:                             &GDBM_READER(),0640)) {
                   9703:         my $rid=$bighash{'mapalias_'.$symb};
                   9704: 	if ($rid) {
                   9705: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 9706: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   9707: 				    $resid,$bighash{'src_'.$rid});
1.427     www      9708: 	}
                   9709:         untie %bighash;
                   9710:     }
                   9711:     return $aliassymb;
                   9712: }
                   9713: 
1.12      www      9714: # ----------------------------------------------------------------- Define Role
                   9715: 
                   9716: sub definerole {
                   9717:   if (allowed('mcr','/')) {
1.1326    raeburn  9718:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800     albertel 9719:     foreach my $role (split(':',$sysrole)) {
                   9720: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9721:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   9722:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   9723: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9724:                return "refused:s:$crole&$cqual"; 
                   9725:             }
                   9726:         }
1.191     harris41 9727:     }
1.800     albertel 9728:     foreach my $role (split(':',$domrole)) {
                   9729: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9730:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   9731:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   9732: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      9733:                return "refused:d:$crole&$cqual"; 
                   9734:             }
                   9735:         }
1.191     harris41 9736:     }
1.800     albertel 9737:     foreach my $role (split(':',$courole)) {
                   9738: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9739:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   9740:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   9741: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9742:                return "refused:c:$crole&$cqual"; 
                   9743:             }
                   9744:         }
1.191     harris41 9745:     }
1.1326    raeburn  9746:     my $uhome;
                   9747:     if (($uname ne '') && ($udom ne '')) {
                   9748:         $uhome = &homeserver($uname,$udom);
                   9749:         return $uhome if ($uhome eq 'no_host');
                   9750:     } else {
                   9751:         $uname = $env{'user.name'};
                   9752:         $udom = $env{'user.domain'};
                   9753:         $uhome = $env{'user.home'};
                   9754:     }
1.620     albertel 9755:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326    raeburn  9756:                 "$udom:$uname:rolesdef_$rolename=".
1.21      www      9757:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326    raeburn  9758:     return reply($command,$uhome);
1.12      www      9759:   } else {
                   9760:     return 'refused';
                   9761:   }
1.105     harris41 9762: }
                   9763: 
                   9764: # ---------------- Make a metadata query against the network of library servers
                   9765: 
                   9766: sub metadata_query {
1.1237    raeburn  9767:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120     harris41 9768:     my %rhash;
1.845     albertel 9769:     my %libserv = &all_library();
1.244     matthew  9770:     my @server_list = (defined($server_array) ? @$server_array
                   9771:                                               : keys(%libserv) );
                   9772:     for my $server (@server_list) {
1.1237    raeburn  9773:         my $domains = ''; 
                   9774:         if (ref($domains_hash) eq 'HASH') {
                   9775:             $domains = $domains_hash->{$server}; 
                   9776:         }
1.118     harris41 9777: 	unless ($custom or $customshow) {
1.1237    raeburn  9778: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118     harris41 9779: 	    $rhash{$server}=$reply;
                   9780: 	}
                   9781: 	else {
                   9782: 	    my $reply=&reply("querysend:".&escape($query).':'.
1.1237    raeburn  9783: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118     harris41 9784: 			     $server);
                   9785: 	    $rhash{$server}=$reply;
                   9786: 	}
1.112     harris41 9787:     }
1.118     harris41 9788:     return \%rhash;
1.240     www      9789: }
                   9790: 
                   9791: # ----------------------------------------- Send log queries and wait for reply
                   9792: 
                   9793: sub log_query {
                   9794:     my ($uname,$udom,$query,%filters)=@_;
                   9795:     my $uhome=&homeserver($uname,$udom);
                   9796:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 9797:     my $uhost=&hostname($uhome);
1.800     albertel 9798:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      9799:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   9800:                        $uhome);
1.479     albertel 9801:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      9802:     return get_query_reply($queryid);
                   9803: }
                   9804: 
1.818     raeburn  9805: # -------------------------- Update MySQL table for portfolio file
                   9806: 
                   9807: sub update_portfolio_table {
1.821     raeburn  9808:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  9809:     if ($group ne '') {
                   9810:         $file_name =~s /^\Q$group\E//;
                   9811:     }
1.818     raeburn  9812:     my $homeserver = &homeserver($uname,$udom);
                   9813:     my $queryid=
1.821     raeburn  9814:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   9815:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  9816:     my $reply = &get_query_reply($queryid);
                   9817:     return $reply;
                   9818: }
                   9819: 
1.899     raeburn  9820: # -------------------------- Update MySQL allusers table
                   9821: 
                   9822: sub update_allusers_table {
                   9823:     my ($uname,$udom,$names) = @_;
                   9824:     my $homeserver = &homeserver($uname,$udom);
                   9825:     my $queryid=
                   9826:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   9827:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   9828:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   9829:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   9830:                'generation='.&escape($names->{'generation'}).'%%'.
                   9831:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   9832:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  9833:     return;
1.899     raeburn  9834: }
                   9835: 
1.508     raeburn  9836: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  9837: 
                   9838: sub fetch_enrollment_query {
1.511     raeburn  9839:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317    raeburn  9840:     my ($homeserver,$sleep,$loopmax);
1.547     raeburn  9841:     my $maxtries = 1;
1.508     raeburn  9842:     if ($context eq 'automated') {
                   9843:         $homeserver = $perlvar{'lonHostID'};
1.1317    raeburn  9844:         $sleep = 2;
                   9845:         $loopmax = 100;
1.547     raeburn  9846:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  9847:     } else {
                   9848:         $homeserver = &homeserver($cnum,$dom);
                   9849:     }
1.838     albertel 9850:     my $host=&hostname($homeserver);
1.506     raeburn  9851:     my $cmd = '';
1.1000    raeburn  9852:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 9853:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  9854:     }
                   9855:     $cmd =~ s/%%$//;
                   9856:     $cmd = &escape($cmd);
                   9857:     my $query = 'fetchenrollment';
1.620     albertel 9858:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  9859:     unless ($queryid=~/^\Q$host\E\_/) { 
                   9860:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   9861:         return 'error: '.$queryid;
                   9862:     }
1.1317    raeburn  9863:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9864:     my $tries = 1;
                   9865:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317    raeburn  9866:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9867:         $tries ++;
                   9868:     }
1.526     raeburn  9869:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 9870:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  9871:     } else {
1.901     albertel 9872:         my @responses = split(/:/,$reply);
1.1322    raeburn  9873:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
1.800     albertel 9874:             foreach my $line (@responses) {
                   9875:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  9876:                 $$replyref{$key} = $value;
                   9877:             }
                   9878:         } else {
1.1117    foxr     9879:             my $pathname = LONCAPA::tempdir();
1.800     albertel 9880:             foreach my $line (@responses) {
                   9881:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  9882:                 $$replyref{$key} = $value;
                   9883:                 if ($value > 0) {
1.800     albertel 9884:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   9885:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  9886:                         my $destname = $pathname.'/'.$filename;
                   9887:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  9888:                         if ($xml_classlist =~ /^error/) {
                   9889:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   9890:                         } else {
1.1359    raeburn  9891:                             if ( open(FILE,">",$destname) ) {
1.506     raeburn  9892:                                 print FILE &unescape($xml_classlist);
                   9893:                                 close(FILE);
1.526     raeburn  9894:                             } else {
                   9895:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  9896:                             }
                   9897:                         }
                   9898:                     }
                   9899:                 }
                   9900:             }
                   9901:         }
                   9902:         return 'ok';
                   9903:     }
                   9904:     return 'error';
                   9905: }
                   9906: 
1.242     www      9907: sub get_query_reply {
1.1471    raeburn  9908:     my ($queryid,$sleep,$loopmax) = @_;
1.1317    raeburn  9909:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
                   9910:         $sleep = 0.2;
                   9911:     }
                   9912:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
                   9913:         $loopmax = 100;
                   9914:     }
1.1117    foxr     9915:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      9916:     my $reply='';
1.1317    raeburn  9917:     for (1..$loopmax) {
                   9918: 	sleep($sleep);
1.240     www      9919:         if (-e $replyfile.'.end') {
1.1359    raeburn  9920: 	    if (open(my $fh,"<",$replyfile)) {
1.904     albertel 9921: 		$reply = join('',<$fh>);
                   9922: 		close($fh);
1.240     www      9923: 	   } else { return 'error: reply_file_error'; }
1.242     www      9924:            return &unescape($reply);
                   9925: 	}
1.240     www      9926:     }
1.242     www      9927:     return 'timeout:'.$queryid;
1.240     www      9928: }
                   9929: 
                   9930: sub courselog_query {
1.241     www      9931: #
                   9932: # possible filters:
                   9933: # url: url or symb
                   9934: # username
                   9935: # domain
                   9936: # action: view, submit, grade
                   9937: # start: timestamp
                   9938: # end: timestamp
                   9939: #
1.240     www      9940:     my (%filters)=@_;
1.620     albertel 9941:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      9942:     if ($filters{'url'}) {
                   9943: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   9944:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   9945:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   9946:     }
1.620     albertel 9947:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   9948:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      9949:     return &log_query($cname,$cdom,'courselog',%filters);
                   9950: }
                   9951: 
                   9952: sub userlog_query {
1.858     raeburn  9953: #
                   9954: # possible filters:
                   9955: # action: log check role
                   9956: # start: timestamp
                   9957: # end: timestamp
                   9958: #
1.240     www      9959:     my ($uname,$udom,%filters)=@_;
                   9960:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      9961: }
                   9962: 
1.506     raeburn  9963: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   9964: 
                   9965: sub auto_run {
1.508     raeburn  9966:     my ($cnum,$cdom) = @_;
1.876     raeburn  9967:     my $response = 0;
                   9968:     my $settings;
                   9969:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   9970:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   9971:         $settings = $domconfig{'autoenroll'};
                   9972:         if ($settings->{'run'} eq '1') {
                   9973:             $response = 1;
                   9974:         }
                   9975:     } else {
1.934     raeburn  9976:         my $homeserver;
                   9977:         if (&is_course($cdom,$cnum)) {
                   9978:             $homeserver = &homeserver($cnum,$cdom);
                   9979:         } else {
                   9980:             $homeserver = &domain($cdom,'primary');
                   9981:         }
                   9982:         if ($homeserver ne 'no_host') {
                   9983:             $response = &reply('autorun:'.$cdom,$homeserver);
                   9984:         }
1.876     raeburn  9985:     }
1.506     raeburn  9986:     return $response;
                   9987: }
1.776     albertel 9988: 
1.506     raeburn  9989: sub auto_get_sections {
1.508     raeburn  9990:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  9991:     my $homeserver;
                   9992:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   9993:         $homeserver = &homeserver($cnum,$cdom);
                   9994:     }
                   9995:     if (!defined($homeserver)) { 
                   9996:         if ($cdom =~ /^$match_domain$/) {
                   9997:             $homeserver = &domain($cdom,'primary');
                   9998:         }
                   9999:     }
                   10000:     my @secs;
                   10001:     if (defined($homeserver)) {
                   10002:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   10003:         unless ($response eq 'refused') {
                   10004:             @secs = split(/:/,$response);
                   10005:         }
1.506     raeburn  10006:     }
                   10007:     return @secs;
                   10008: }
1.776     albertel 10009: 
1.506     raeburn  10010: sub auto_new_course {
1.1099    raeburn  10011:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  10012:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  10013:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  10014:     return $response;
                   10015: }
1.776     albertel 10016: 
1.506     raeburn  10017: sub auto_validate_courseID {
1.508     raeburn  10018:     my ($cnum,$cdom,$inst_course_id) = @_;
                   10019:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  10020:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  10021:     return $response;
                   10022: }
1.776     albertel 10023: 
1.1007    raeburn  10024: sub auto_validate_instcode {
1.1020    raeburn  10025:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  10026:     my ($homeserver,$response);
                   10027:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   10028:         $homeserver = &homeserver($cnum,$cdom);
                   10029:     }
                   10030:     if (!defined($homeserver)) {
                   10031:         if ($cdom =~ /^$match_domain$/) {
                   10032:             $homeserver = &domain($cdom,'primary');
                   10033:         }
                   10034:     }
1.1065    raeburn  10035:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   10036:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  10037:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   10038:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  10039: }
                   10040: 
1.1444    raeburn  10041: sub auto_validate_inst_crosslist {
                   10042:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
                   10043:     my ($homeserver,$response);
                   10044:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   10045:         $homeserver = &homeserver($cnum,$cdom);
                   10046:     }
                   10047:     if (!defined($homeserver)) {
                   10048:         if ($cdom =~ /^$match_domain$/) {
                   10049:             $homeserver = &domain($cdom,'primary');
                   10050:         }
                   10051:     }
                   10052:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
                   10053:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
                   10054:                          &escape($instcode).':'.&escape($inst_xlist).':'.
1.1445    raeburn  10055:                          &escape($coowner),$homeserver);
1.1444    raeburn  10056:     }
                   10057:     return $response;
                   10058: }
                   10059: 
1.506     raeburn  10060: sub auto_create_password {
1.873     raeburn  10061:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   10062:     my ($homeserver,$response);
1.506     raeburn  10063:     my $create_passwd = 0;
                   10064:     my $authchk = '';
1.873     raeburn  10065:     if ($udom =~ /^$match_domain$/) {
                   10066:         $homeserver = &domain($udom,'primary');
                   10067:     }
                   10068:     if ($homeserver eq '') {
                   10069:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   10070:             $homeserver = &homeserver($cnum,$cdom);
                   10071:         }
                   10072:     }
                   10073:     if ($homeserver eq '') {
                   10074:         $authchk = 'nodomain';
1.506     raeburn  10075:     } else {
1.873     raeburn  10076:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   10077:         if ($response eq 'refused') {
                   10078:             $authchk = 'refused';
                   10079:         } else {
1.901     albertel 10080:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  10081:         }
1.506     raeburn  10082:     }
                   10083:     return ($authparam,$create_passwd,$authchk);
                   10084: }
                   10085: 
1.706     raeburn  10086: sub auto_photo_permission {
                   10087:     my ($cnum,$cdom,$students) = @_;
                   10088:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 10089:     my ($outcome,$perm_reqd,$conditions) = 
                   10090: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 10091:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   10092: 	return (undef,undef);
                   10093:     }
1.706     raeburn  10094:     return ($outcome,$perm_reqd,$conditions);
                   10095: }
                   10096: 
                   10097: sub auto_checkphotos {
                   10098:     my ($uname,$udom,$pid) = @_;
                   10099:     my $homeserver = &homeserver($uname,$udom);
                   10100:     my ($result,$resulttype);
                   10101:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 10102: 				   &escape($uname).':'.&escape($pid),
                   10103: 				   $homeserver));
1.709     albertel 10104:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   10105: 	return (undef,undef);
                   10106:     }
1.706     raeburn  10107:     if ($outcome) {
                   10108:         ($result,$resulttype) = split(/:/,$outcome);
                   10109:     } 
                   10110:     return ($result,$resulttype);
                   10111: }
                   10112: 
                   10113: sub auto_photochoice {
                   10114:     my ($cnum,$cdom) = @_;
                   10115:     my $homeserver = &homeserver($cnum,$cdom);
                   10116:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 10117: 						       &escape($cdom),
                   10118: 						       $homeserver)));
1.709     albertel 10119:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   10120: 	return (undef,undef);
                   10121:     }
1.706     raeburn  10122:     return ($update,$comment);
                   10123: }
                   10124: 
                   10125: sub auto_photoupdate {
                   10126:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   10127:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 10128:     my $host=&hostname($homeserver);
1.706     raeburn  10129:     my $cmd = '';
                   10130:     my $maxtries = 1;
1.800     albertel 10131:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   10132:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  10133:     }
                   10134:     $cmd =~ s/%%$//;
                   10135:     $cmd = &escape($cmd);
                   10136:     my $query = 'institutionalphotos';
                   10137:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   10138:     unless ($queryid=~/^\Q$host\E\_/) {
                   10139:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   10140:         return 'error: '.$queryid;
                   10141:     }
                   10142:     my $reply = &get_query_reply($queryid);
                   10143:     my $tries = 1;
                   10144:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   10145:         $reply = &get_query_reply($queryid);
                   10146:         $tries ++;
                   10147:     }
                   10148:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   10149:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   10150:     } else {
                   10151:         my @responses = split(/:/,$reply);
                   10152:         my $outcome = shift(@responses); 
                   10153:         foreach my $item (@responses) {
                   10154:             my ($key,$value) = split(/=/,$item);
                   10155:             $$photo{$key} = $value;
                   10156:         }
                   10157:         return $outcome;
                   10158:     }
                   10159:     return 'error';
                   10160: }
                   10161: 
1.521     raeburn  10162: sub auto_instcode_format {
1.793     albertel 10163:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   10164: 	$cat_order) = @_;
1.521     raeburn  10165:     my $courses = '';
1.772     raeburn  10166:     my @homeservers;
1.521     raeburn  10167:     if ($caller eq 'global') {
1.841     albertel 10168: 	my %servers = &get_servers($codedom,'library');
                   10169: 	foreach my $tryserver (keys(%servers)) {
                   10170: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10171: 		push(@homeservers,$tryserver);
                   10172: 	    }
1.584     raeburn  10173:         }
1.1022    raeburn  10174:     } elsif ($caller eq 'requests') {
                   10175:         if ($codedom =~ /^$match_domain$/) {
                   10176:             my $chome = &domain($codedom,'primary');
                   10177:             unless ($chome eq 'no_host') {
                   10178:                 push(@homeservers,$chome);
                   10179:             }
                   10180:         }
1.521     raeburn  10181:     } else {
1.772     raeburn  10182:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  10183:     }
1.793     albertel 10184:     foreach my $code (keys(%{$instcodes})) {
                   10185:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  10186:     }
                   10187:     chop($courses);
1.772     raeburn  10188:     my $ok_response = 0;
                   10189:     my $response;
                   10190:     while (@homeservers > 0 && $ok_response == 0) {
                   10191:         my $server = shift(@homeservers); 
                   10192:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   10193:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   10194:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 10195: 		split(/:/,$response);
1.772     raeburn  10196:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   10197:             push(@{$codetitles},&str2array($codetitles_str));
                   10198:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   10199:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   10200:             $ok_response = 1;
                   10201:         }
                   10202:     }
                   10203:     if ($ok_response) {
1.521     raeburn  10204:         return 'ok';
1.772     raeburn  10205:     } else {
                   10206:         return $response;
1.521     raeburn  10207:     }
                   10208: }
                   10209: 
1.792     raeburn  10210: sub auto_instcode_defaults {
                   10211:     my ($domain,$returnhash,$code_order) = @_;
                   10212:     my @homeservers;
1.841     albertel 10213: 
                   10214:     my %servers = &get_servers($domain,'library');
                   10215:     foreach my $tryserver (keys(%servers)) {
                   10216: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10217: 	    push(@homeservers,$tryserver);
                   10218: 	}
1.792     raeburn  10219:     }
1.841     albertel 10220: 
1.792     raeburn  10221:     my $response;
1.841     albertel 10222:     foreach my $server (@homeservers) {
1.792     raeburn  10223:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 10224:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   10225: 	
                   10226: 	foreach my $pair (split(/\&/,$response)) {
                   10227: 	    my ($name,$value)=split(/\=/,$pair);
                   10228: 	    if ($name eq 'code_order') {
                   10229: 		@{$code_order} = split(/\&/,&unescape($value));
                   10230: 	    } else {
                   10231: 		$returnhash->{&unescape($name)}=&unescape($value);
                   10232: 	    }
                   10233: 	}
                   10234: 	return 'ok';
1.792     raeburn  10235:     }
1.841     albertel 10236: 
                   10237:     return $response;
1.1003    raeburn  10238: }
                   10239: 
                   10240: sub auto_possible_instcodes {
1.1007    raeburn  10241:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   10242:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   10243:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   10244:         return;
                   10245:     }
1.1003    raeburn  10246:     my (@homeservers,$uhome);
                   10247:     if (defined(&domain($domain,'primary'))) {
                   10248:         $uhome=&domain($domain,'primary');
                   10249:         push(@homeservers,&domain($domain,'primary'));
                   10250:     } else {
                   10251:         my %servers = &get_servers($domain,'library');
                   10252:         foreach my $tryserver (keys(%servers)) {
                   10253:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10254:                 push(@homeservers,$tryserver);
                   10255:             }
                   10256:         }
                   10257:     }
                   10258:     my $response;
                   10259:     foreach my $server (@homeservers) {
                   10260:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   10261:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  10262:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   10263:             split(':',$response);
                   10264:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   10265:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  10266:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  10267:             my ($name,$value)=split('=',$item);
                   10268:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  10269:         }
                   10270:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  10271:             my ($name,$value)=split('=',$item);
                   10272:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  10273:         }
                   10274:         return 'ok';
                   10275:     }
                   10276:     return $response;
                   10277: }
1.792     raeburn  10278: 
1.1010    raeburn  10279: sub auto_courserequest_checks {
                   10280:     my ($dom) = @_;
1.1020    raeburn  10281:     my ($homeserver,%validations);
                   10282:     if ($dom =~ /^$match_domain$/) {
                   10283:         $homeserver = &domain($dom,'primary');
                   10284:     }
                   10285:     unless ($homeserver eq 'no_host') {
                   10286:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   10287:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10288:             my @items = split(/&/,$response);
                   10289:             foreach my $item (@items) {
                   10290:                 my ($key,$value) = split('=',$item);
                   10291:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   10292:             }
                   10293:         }
                   10294:     }
1.1010    raeburn  10295:     return %validations; 
                   10296: }
                   10297: 
1.1020    raeburn  10298: sub auto_courserequest_validation {
1.1255    raeburn  10299:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020    raeburn  10300:     my ($homeserver,$response);
                   10301:     if ($dom =~ /^$match_domain$/) {
                   10302:         $homeserver = &domain($dom,'primary');
                   10303:     }
1.1255    raeburn  10304:     unless ($homeserver eq 'no_host') {
                   10305:         my $customdata;
                   10306:         if (ref($custominfo) eq 'HASH') {
                   10307:             $customdata = &freeze_escape($custominfo);
                   10308:         }
1.1020    raeburn  10309:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  10310:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255    raeburn  10311:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
                   10312:                                     $customdata,$homeserver));
1.1020    raeburn  10313:     }
                   10314:     return $response;
                   10315: }
                   10316: 
1.777     albertel 10317: sub auto_validate_class_sec {
1.918     raeburn  10318:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  10319:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  10320:     my $ownerlist;
                   10321:     if (ref($owners) eq 'ARRAY') {
                   10322:         $ownerlist = join(',',@{$owners});
                   10323:     } else {
                   10324:         $ownerlist = $owners;
                   10325:     }
1.773     raeburn  10326:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  10327:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  10328:     return $response;
                   10329: }
                   10330: 
1.1460    raeburn  10331: sub auto_instsec_reformat {
                   10332:     my ($cdom,$action,$instsecref) = @_;
                   10333:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
                   10334:     my @homeservers;
                   10335:     if (defined(&domain($cdom,'primary'))) {
                   10336:         push(@homeservers,&domain($cdom,'primary'));
                   10337:     } else {
                   10338:         my %servers = &get_servers($cdom,'library');
                   10339:         foreach my $tryserver (keys(%servers)) {
                   10340:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10341:                 push(@homeservers,$tryserver);
                   10342:             }
                   10343:         }
                   10344:     }
                   10345:     my $response;
                   10346:     my %reformatted = %{$instsecref};
                   10347:     foreach my $server (@homeservers) {
                   10348:         if (ref($instsecref) eq 'HASH') {
                   10349:             my $info = &freeze_escape($instsecref);
                   10350:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
                   10351:                                 $action.':'.$info,$server);
                   10352:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
                   10353:             my @items = split(/&/,$response);
                   10354:             foreach my $item (@items) {
                   10355:                 my ($key,$value) = split(/=/,$item);
                   10356:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
                   10357:             }
                   10358:         }
                   10359:     }
                   10360:     return %reformatted;
                   10361: }
                   10362: 
1.1367    raeburn  10363: sub auto_validate_instclasses {
                   10364:     my ($cdom,$cnum,$owners,$classesref) = @_;
                   10365:     my ($homeserver,%validations);
                   10366:     $homeserver = &homeserver($cnum,$cdom);
                   10367:     unless ($homeserver eq 'no_host') {
                   10368:         my $ownerlist;
                   10369:         if (ref($owners) eq 'ARRAY') {
                   10370:             $ownerlist = join(',',@{$owners});
                   10371:         } else {
                   10372:             $ownerlist = $owners;
                   10373:         }
                   10374:         if (ref($classesref) eq 'HASH') {
                   10375:             my $classes = &freeze_escape($classesref);
                   10376:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
                   10377:                                 ':'.$cdom.':'.$classes,$homeserver);
                   10378:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10379:                 my @items = split(/&/,$response);
                   10380:                 foreach my $item (@items) {
                   10381:                     my ($key,$value) = split('=',$item);
                   10382:                     $validations{&unescape($key)} = &thaw_unescape($value);
                   10383:                 }
                   10384:             }
                   10385:         }
                   10386:     }
                   10387:     return %validations;
                   10388: }
                   10389: 
1.1248    raeburn  10390: sub auto_crsreq_update {
                   10391:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257    raeburn  10392:         $code,$accessstart,$accessend,$inbound) = @_;
1.1248    raeburn  10393:     my ($homeserver,%crsreqresponse);
                   10394:     if ($cdom =~ /^$match_domain$/) {
                   10395:         $homeserver = &domain($cdom,'primary');
                   10396:     }
                   10397:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   10398:         my $info;
                   10399:         if (ref($inbound) eq 'HASH') {
                   10400:             $info = &freeze_escape($inbound);
                   10401:         }
                   10402:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
                   10403:                             ':'.&escape($action).':'.&escape($ownername).':'.
                   10404:                             &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257    raeburn  10405:                             &escape($title).':'.&escape($code).':'.
                   10406:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
                   10407:                             $homeserver);
1.1248    raeburn  10408:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10409:             my @items = split(/&/,$response);
                   10410:             foreach my $item (@items) {
                   10411:                 my ($key,$value) = split('=',$item);
                   10412:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
                   10413:             }
                   10414:         }
                   10415:     }
                   10416:     return \%crsreqresponse;
                   10417: }
                   10418: 
1.1305    raeburn  10419: sub auto_export_grades {
1.1309    raeburn  10420:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
                   10421:     my ($homeserver,%exportresponse);
                   10422:     if ($cdom =~ /^$match_domain$/) {
                   10423:         $homeserver = &domain($cdom,'primary');
                   10424:     }
                   10425:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   10426:         my $info;
                   10427:         if (ref($inforef) eq 'HASH') {
                   10428:             $info = &freeze_escape($inforef);
                   10429:         }
                   10430:         if (ref($gradesref) eq 'HASH') {
                   10431:             my $grades = &freeze_escape($gradesref);
                   10432:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
                   10433:                                 $info.':'.$grades,$homeserver);
                   10434:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
                   10435:                 my @items = split(/&/,$response);
                   10436:                 foreach my $item (@items) {
                   10437:                     my ($key,$value) = split('=',$item);
                   10438:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
                   10439:                 }
                   10440:             }
                   10441:         }
                   10442:     }
                   10443:     return \%exportresponse;
1.1305    raeburn  10444: }
                   10445: 
1.1287    raeburn  10446: sub check_instcode_cloning {
                   10447:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
                   10448:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   10449:         return;
                   10450:     }
                   10451:     my $canclone;
                   10452:     if (@{$code_order} > 0) {
                   10453:         my $instcoderegexp ='^';
                   10454:         my @clonecodes = split(/\&/,$cloner);
                   10455:         foreach my $item (@{$code_order}) {
                   10456:             if (grep(/^\Q$item\E=/,@clonecodes)) {
                   10457:                 foreach my $pair (@clonecodes) {
                   10458:                     my ($key,$val) = split(/\=/,$pair,2);
                   10459:                     $val = &unescape($val);
                   10460:                     if ($key eq $item) {
                   10461:                         $instcoderegexp .= '('.$val.')';
                   10462:                         last;
                   10463:                     }
                   10464:                 }
                   10465:             } else {
                   10466:                 $instcoderegexp .= $codedefaults->{$item};
                   10467:             }
                   10468:         }
                   10469:         $instcoderegexp .= '$';
                   10470:         my (@from,@to);
                   10471:         eval {
                   10472:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   10473:                (@to) = ($clonetocode =~ /$instcoderegexp/);
                   10474:         };
                   10475:         if ((@from > 0) && (@to > 0)) {
                   10476:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   10477:             if (!@diffs) {
                   10478:                 $canclone = 1;
                   10479:             }
                   10480:         }
                   10481:     }
                   10482:     return $canclone;
                   10483: }
                   10484: 
                   10485: sub default_instcode_cloning {
                   10486:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
                   10487:     my (%codedefaults,@code_order,$canclone);
                   10488:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
                   10489:         %codedefaults = %{$codedefaultsref};
                   10490:         @code_order = @{$codeorderref};
                   10491:     } elsif ($clonedom) {
                   10492:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
                   10493:     }
                   10494:     if (($domdefclone) && (@code_order)) {
                   10495:         my @clonecodes = split(/\+/,$domdefclone);
                   10496:         my $instcoderegexp ='^';
                   10497:         foreach my $item (@code_order) {
                   10498:             if (grep(/^\Q$item\E$/,@clonecodes)) {
                   10499:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
                   10500:             } else {
                   10501:                 $instcoderegexp .= $codedefaults{$item};
                   10502:             }
                   10503:         }
                   10504:         $instcoderegexp .= '$';
                   10505:         my (@from,@to);
                   10506:         eval {
                   10507:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   10508:             (@to) = ($clonetocode =~ /$instcoderegexp/);
                   10509:         };
                   10510:         if ((@from > 0) && (@to > 0)) {
                   10511:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   10512:             if (!@diffs) {
                   10513:                 $canclone = 1;
                   10514:             }
                   10515:         }
                   10516:     }
                   10517:     return $canclone;
                   10518: }
                   10519: 
1.679     raeburn  10520: # ------------------------------------------------------- Course Group routines
                   10521: 
                   10522: sub get_coursegroups {
1.809     raeburn  10523:     my ($cdom,$cnum,$group,$namespace) = @_;
                   10524:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  10525: }
                   10526: 
1.679     raeburn  10527: sub modify_coursegroup {
                   10528:     my ($cdom,$cnum,$groupsettings) = @_;
                   10529:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   10530: }
                   10531: 
1.809     raeburn  10532: sub toggle_coursegroup_status {
                   10533:     my ($cdom,$cnum,$group,$action) = @_;
                   10534:     my ($from_namespace,$to_namespace);
                   10535:     if ($action eq 'delete') {
                   10536:         $from_namespace = 'coursegroups';
                   10537:         $to_namespace = 'deleted_groups';
                   10538:     } else {
                   10539:         $from_namespace = 'deleted_groups';
                   10540:         $to_namespace = 'coursegroups';
                   10541:     }
                   10542:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  10543:     if (my $tmp = &error(%curr_group)) {
                   10544:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   10545:         return ('read error',$tmp);
                   10546:     } else {
                   10547:         my %savedsettings = %curr_group; 
1.809     raeburn  10548:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  10549:         my $deloutcome;
                   10550:         if ($result eq 'ok') {
1.809     raeburn  10551:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  10552:         } else {
                   10553:             return ('write error',$result);
                   10554:         }
                   10555:         if ($deloutcome eq 'ok') {
                   10556:             return 'ok';
                   10557:         } else {
                   10558:             return ('delete error',$deloutcome);
                   10559:         }
                   10560:     }
                   10561: }
                   10562: 
1.679     raeburn  10563: sub modify_group_roles {
1.1512    raeburn  10564:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context,
                   10565:         $othdomby,$requester) = @_;
1.679     raeburn  10566:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   10567:     my $role = 'gr/'.&escape($userprivs);
                   10568:     my ($uname,$udom) = split(/:/,$user);
1.1512    raeburn  10569:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context,
                   10570:                              $othdomby,$requester);
1.684     raeburn  10571:     if ($result eq 'ok') {
                   10572:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   10573:     }
1.679     raeburn  10574:     return $result;
                   10575: }
                   10576: 
                   10577: sub modify_coursegroup_membership {
                   10578:     my ($cdom,$cnum,$membership) = @_;
                   10579:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   10580:     return $result;
                   10581: }
                   10582: 
1.682     raeburn  10583: sub get_active_groups {
                   10584:     my ($udom,$uname,$cdom,$cnum) = @_;
                   10585:     my $now = time;
                   10586:     my %groups = ();
                   10587:     foreach my $key (keys(%env)) {
1.811     albertel 10588:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  10589:             my ($start,$end) = split(/\./,$env{$key});
                   10590:             if (($end!=0) && ($end<$now)) { next; }
                   10591:             if (($start!=0) && ($start>$now)) { next; }
                   10592:             if ($1 eq $cdom && $2 eq $cnum) {
                   10593:                 $groups{$3} = $env{$key} ;
                   10594:             }
                   10595:         }
                   10596:     }
                   10597:     return %groups;
                   10598: }
                   10599: 
1.683     raeburn  10600: sub get_group_membership {
                   10601:     my ($cdom,$cnum,$group) = @_;
                   10602:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   10603: }
                   10604: 
                   10605: sub get_users_groups {
                   10606:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  10607:     my @usersgroups;
1.683     raeburn  10608:     my $cachetime=1800;
                   10609: 
                   10610:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  10611:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   10612:     if (defined($cached)) {
1.734     albertel 10613:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  10614:     } else {  
                   10615:         $grouplist = '';
1.816     raeburn  10616:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  10617:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  10618:         my $access_end = $env{'course.'.$courseid.
                   10619:                               '.default_enrollment_end_date'};
                   10620:         my $now = time;
                   10621:         foreach my $key (keys(%roleshash)) {
                   10622:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   10623:                 my $group = $1;
                   10624:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   10625:                     my $start = $2;
                   10626:                     my $end = $1;
                   10627:                     if ($start == -1) { next; } # deleted from group
                   10628:                     if (($start!=0) && ($start>$now)) { next; }
                   10629:                     if (($end!=0) && ($end<$now)) {
                   10630:                         if ($access_end && $access_end < $now) {
                   10631:                             if ($access_end - $end < 86400) {
                   10632:                                 push(@usersgroups,$group);
1.733     raeburn  10633:                             }
                   10634:                         }
1.817     raeburn  10635:                         next;
1.733     raeburn  10636:                     }
1.817     raeburn  10637:                     push(@usersgroups,$group);
1.683     raeburn  10638:                 }
                   10639:             }
                   10640:         }
1.817     raeburn  10641:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   10642:         $grouplist = join(':',@usersgroups);
                   10643:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  10644:     }
1.733     raeburn  10645:     return @usersgroups;
1.683     raeburn  10646: }
                   10647: 
                   10648: sub devalidate_getgroups_cache {
                   10649:     my ($udom,$uname,$cdom,$cnum)=@_;
                   10650:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 10651: 
1.683     raeburn  10652:     my $hashid="$udom:$uname:$courseid";
                   10653:     &devalidate_cache_new('getgroups',$hashid);
                   10654: }
                   10655: 
1.12      www      10656: # ------------------------------------------------------------------ Plain Text
                   10657: 
                   10658: sub plaintext {
1.988     raeburn  10659:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  10660:     if ($short =~ m{^cr/}) {
1.758     albertel 10661: 	return (split('/',$short))[-1];
                   10662:     }
1.742     raeburn  10663:     if (!defined($cid)) {
                   10664:         $cid = $env{'request.course.id'};
                   10665:     }
                   10666:     my %rolenames = (
1.1008    raeburn  10667:                       Course    => 'std',
                   10668:                       Community => 'alt1',
1.1305    raeburn  10669:                       Placement => 'std',
1.742     raeburn  10670:                     );
1.1037    raeburn  10671:     if ($cid ne '') {
                   10672:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   10673:             unless ($forcedefault) {
                   10674:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   10675:                 &Apache::lonlocal::mt_escape(\$roletext);
                   10676:                 return &Apache::lonlocal::mt($roletext);
                   10677:             }
                   10678:         }
                   10679:     }
                   10680:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   10681:         (defined($rolenames{$type})) && 
                   10682:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  10683:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  10684:     } elsif ($cid ne '') {
                   10685:         my $crstype = $env{'course.'.$cid.'.type'};
                   10686:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   10687:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   10688:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   10689:         }
1.742     raeburn  10690:     }
1.1037    raeburn  10691:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      10692: }
                   10693: 
                   10694: # ----------------------------------------------------------------- Assign Role
                   10695: 
                   10696: sub assignrole {
1.957     raeburn  10697:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
1.1512    raeburn  10698:         $context,$othdomby,$requester,$reqsec,$reqrole)=@_;
1.1517    raeburn  10699:     my ($mrole,$rolelogcontext);
1.21      www      10700:     if ($role =~ /^cr\//) {
1.393     www      10701:         my $cwosec=$url;
1.811     albertel 10702:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.1502    raeburn  10703:         if ((!&allowed('ccr',$cwosec)) && (!&allowed('ccr',$udom))) {
1.1512    raeburn  10704:             my $refused = 1;
                   10705:             if ($context eq 'requestcourses') {
                   10706:                 if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   10707:                     if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   10708:                         if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   10709:                             my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   10710:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10711:                             if ($crsenv{'internal.courseowner'} eq
                   10712:                                 $env{'user.name'}.':'.$env{'user.domain'}) {
                   10713:                                 $refused = '';
                   10714:                             }
                   10715:                         }
                   10716:                     }
                   10717:                 }
                   10718:             } elsif (($context eq 'course') && ($othdomby eq 'othdombyuser')) {
                   10719:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   10720:                 my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
                   10721:                 my $key = "$uname:$udom:$role:$sec";
                   10722:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
                   10723:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
                   10724:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
                   10725:                         $refused = '';
                   10726:                     }
                   10727:                 }
                   10728:             }
                   10729:             if ($refused) {
                   10730:                 &logthis('Refused custom assignrole: '.
                   10731:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   10732:                          ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   10733:                 return 'refused';
                   10734:             }
1.104     www      10735:         }
1.21      www      10736:         $mrole='cr';
1.678     raeburn  10737:     } elsif ($role =~ /^gr\//) {
                   10738:         my $cwogrp=$url;
1.811     albertel 10739:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.1512    raeburn  10740:         if (!&allowed('mdg',$cwogrp)) {
                   10741:             my $refused = 1;
                   10742:             if (($refused) && ($othdomby eq 'othdombyuser') && ($requester ne '') && ($reqrole ne '')) {
                   10743:                 my ($cdom,$cnum) = ($cwogrp =~ m{^/?($match_domain)/($match_courseid)$});
                   10744:                 my $key = "$uname:$udom:$reqrole:$reqsec";
                   10745:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
                   10746:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
                   10747:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
                   10748:                         $refused = '';
                   10749:                     }
                   10750:                 }
                   10751:             }
                   10752:             if ($refused) {
                   10753:                 &logthis('Refused group assignrole: '.
                   10754:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   10755:                          $env{'user.name'}.' at '.$env{'user.domain'});
                   10756:                 return 'refused';
                   10757:             }
1.678     raeburn  10758:         }
                   10759:         $mrole='gr';
1.21      www      10760:     } else {
1.82      www      10761:         my $cwosec=$url;
1.811     albertel 10762:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  10763:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   10764:             my $refused;
                   10765:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   10766:                 if (!(&allowed('c'.$role,$url))) {
                   10767:                     $refused = 1;
                   10768:                 }
                   10769:             } else {
                   10770:                 $refused = 1;
                   10771:             }
1.947     raeburn  10772:             if ($refused) {
1.1045    raeburn  10773:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
1.1512    raeburn  10774:                 if (!$selfenroll && ($othdomby ne 'othdombyuser') &&
                   10775:                    (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
1.1045    raeburn  10776:                     my %crsenv;
                   10777:                     if ($role eq 'cc' || $role eq 'co') {
                   10778:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10779:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   10780:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   10781:                                 if ($crsenv{'internal.courseowner'} eq 
                   10782:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10783:                                     $refused = '';
                   10784:                                 }
                   10785:                             }
                   10786:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   10787:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   10788:                                 if ($crsenv{'internal.courseowner'} eq 
                   10789:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10790:                                     $refused = '';
                   10791:                                 }
                   10792:                             }
                   10793:                         }
                   10794:                     }
1.1368    raeburn  10795:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   10796:                     if ($role eq 'st') {
                   10797:                         $refused = '';
1.1377    raeburn  10798:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
1.1368    raeburn  10799:                         $refused = '';
                   10800:                     }
1.1512    raeburn  10801:                 } elsif ($othdomby eq 'othdombyuser') {
                   10802:                     my ($key,%queuedrolereq);
                   10803:                     if ($context eq 'course') {
                   10804:                         my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
                   10805:                         $key = "$uname:$udom:$role:$sec";
                   10806:                         %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
                   10807:                         if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
                   10808:                             if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
                   10809:                                 if ((($role eq 'cc') && ($cnum !~ /^$match_community$/)) || 
                   10810:                                     (($role eq 'co') && ($cnum =~ /^$match_community$/))) {
                   10811:                                     my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10812:                                     if ($crsenv{'internal.courseowner'} eq $requester) {
                   10813:                                         $refused = '';
                   10814:                                     }
                   10815:                                 } elsif ($role =~ /^(?:in|ta|ep|st)$/) {
                   10816:                                     $refused = '';
                   10817:                                 }
                   10818:                             }
                   10819:                         }
                   10820:                     } elsif (($context eq 'author') && ($role =~ /^ca|aa$/)) {
                   10821:                         my $key = "$uname:$udom:$role"; 
                   10822:                         my ($audom,$auname) = ($url =~ m{^/($match_domain)/($match_username)$});
                   10823:                         if (($audom ne '') && ($auname ne '')) {
                   10824:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$audom,$auname);
                   10825:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
                   10826:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
                   10827:                                     $refused = '';
                   10828:                                 }
                   10829:                             }
                   10830:                         }
                   10831:                     } elsif (($context eq 'domain') && ($role ne 'dc') && ($role ne 'su')) {
                   10832:                         my $key = "$uname:$udom:$role";
                   10833:                         my ($roledom) = ($url =~ m{^/($match_domain)/\Q$role\E$});
                   10834:                         if ($roledom ne '') {
                   10835:                             my $confname = $roledom.'-domainconfig';
                   10836:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$roledom,$confname);
                   10837:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
                   10838:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
                   10839:                                     $refused = '';
                   10840:                                 }
                   10841:                             }
                   10842:                         }
                   10843:                     }
1.1017    raeburn  10844:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  10845:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  10846:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  10847:                         my $wrongcc;
                   10848:                         if ($cnum =~ /^$match_community$/) {
                   10849:                             $wrongcc = 1 if ($role eq 'cc');
                   10850:                         } else {
                   10851:                             $wrongcc = 1 if ($role eq 'co');
                   10852:                         }
                   10853:                         unless ($wrongcc) {
                   10854:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10855:                             if ($crsenv{'internal.courseowner'} eq 
                   10856:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   10857:                                 $refused = '';
                   10858:                             }
1.1017    raeburn  10859:                         }
                   10860:                     }
1.1183    raeburn  10861:                 } elsif ($context eq 'requestauthor') {
                   10862:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   10863:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   10864:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   10865:                             $refused = '';
                   10866:                         } else {
                   10867:                             my %domdefaults = &get_domain_defaults($udom);
                   10868:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   10869:                                 my $checkbystatus;
                   10870:                                 if ($env{'user.adv'}) { 
                   10871:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   10872:                                     if ($disposition eq 'automatic') {
                   10873:                                         $refused = '';
                   10874:                                     } elsif ($disposition eq '') {
                   10875:                                         $checkbystatus = 1;
                   10876:                                     } 
                   10877:                                 } else {
                   10878:                                     $checkbystatus = 1;
                   10879:                                 }
                   10880:                                 if ($checkbystatus) {
                   10881:                                     if ($env{'environment.inststatus'}) {
                   10882:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   10883:                                         foreach my $type (@inststatuses) {
                   10884:                                             if (($type ne '') &&
                   10885:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   10886:                                                 $refused = '';
                   10887:                                             }
                   10888:                                         }
                   10889:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   10890:                                         $refused = '';
                   10891:                                     }
                   10892:                                 }
                   10893:                             }
                   10894:                         }
                   10895:                     }
1.1517    raeburn  10896:                 } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
                   10897:                     if ($url =~ m{^/($match_domain)/($match_username)$}) {
                   10898:                         my ($audom,$auname) = ($1,$2);
                   10899:                         if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
                   10900:                             ($env{"environment.internal.manager.$url"})) {
                   10901:                             $refused = '';
                   10902:                             $rolelogcontext = 'coauthor';
                   10903:                         }
                   10904:                     }
1.1017    raeburn  10905:                 }
                   10906:                 if ($refused) {
1.947     raeburn  10907:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   10908:                              ' '.$role.' '.$end.' '.$start.' by '.
                   10909: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   10910:                     return 'refused';
                   10911:                 }
1.932     raeburn  10912:             }
1.1131    raeburn  10913:         } elsif ($role eq 'au') {
                   10914:             if ($url ne '/'.$udom.'/') {
                   10915:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   10916:                          ' to assign author role for '.$uname.':'.$udom.
                   10917:                          ' in domain: '.$url.' refused (wrong domain).');
                   10918:                 return 'refused';
                   10919:             }
1.104     www      10920:         }
1.21      www      10921:         $mrole=$role;
                   10922:     }
1.620     albertel 10923:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      10924:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      10925:     if ($end) { $command.='_'.$end; }
1.21      www      10926:     if ($start) {
                   10927: 	if ($end) { 
1.81      www      10928:            $command.='_'.$start; 
1.21      www      10929:         } else {
1.81      www      10930:            $command.='_0_'.$start;
1.21      www      10931:         }
                   10932:     }
1.739     raeburn  10933:     my $origstart = $start;
                   10934:     my $origend = $end;
1.957     raeburn  10935:     my $delflag;
1.357     www      10936: # actually delete
                   10937:     if ($deleteflag) {
1.373     www      10938: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      10939: # modify command to delete the role
1.620     albertel 10940:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      10941:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 10942: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      10943: # set start and finish to negative values for userrolelog
                   10944:            $start=-1;
                   10945:            $end=-1;
1.957     raeburn  10946:            $delflag = 1;
1.357     www      10947:         }
                   10948:     }
                   10949: # send command
1.349     www      10950:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      10951: # log new user role if status is ok
1.349     www      10952:     if ($answer eq 'ok') {
1.663     raeburn  10953: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  10954:         if (($role eq 'cc') || ($role eq 'in') ||
                   10955:             ($role eq 'ep') || ($role eq 'ad') ||
                   10956:             ($role eq 'ta') || ($role eq 'st') ||
                   10957:             ($role=~/^cr/) || ($role eq 'gr') ||
                   10958:             ($role eq 'co')) {
1.1200    raeburn  10959: # for course roles, perform group memberships changes triggered by role change.
                   10960:             unless ($role =~ /^gr/) {
                   10961:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   10962:                                                  $origstart,$selfenroll,$context);
                   10963:             }
1.1184    raeburn  10964:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1512    raeburn  10965:                            $selfenroll,$context,$othdomby,$requester);
1.1184    raeburn  10966:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334    raeburn  10967:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
                   10968:                  ($role eq 'da')) {
1.1184    raeburn  10969:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1512    raeburn  10970:                            $context,$othdomby,$requester);
1.1184    raeburn  10971:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1517    raeburn  10972:             if ($rolelogcontext eq '') {
                   10973:                 $rolelogcontext = $context;
                   10974:             }
1.1184    raeburn  10975:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1517    raeburn  10976:                              $rolelogcontext,$othdomby,$requester); 
1.1184    raeburn  10977:         }
1.1053    raeburn  10978:         if ($role eq 'cc') {
                   10979:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   10980:         }
1.349     www      10981:     }
                   10982:     return $answer;
1.169     harris41 10983: }
                   10984: 
1.1053    raeburn  10985: sub autoupdate_coowners {
                   10986:     my ($url,$end,$start,$uname,$udom) = @_;
                   10987:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   10988:     if (($cdom ne '') && ($cnum ne '')) {
                   10989:         my $now = time;
                   10990:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   10991:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   10992:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   10993:             my $instcode = $coursehash{'internal.coursecode'};
1.1444    raeburn  10994:             my $xlists = $coursehash{'internal.crosslistings'};
1.1053    raeburn  10995:             if ($instcode ne '') {
1.1056    raeburn  10996:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   10997:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  10998:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  10999:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1444    raeburn  11000:                         unless ($result eq 'valid') {
                   11001:                             if ($xlists ne '') {
                   11002:                                 foreach my $xlist (split(',',$xlists)) {
                   11003:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
                   11004:                                     $result =
1.1446    raeburn  11005:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
                   11006:                                                                       $inst_crosslist,$uname.':'.$udom);
                   11007:                                     last if ($result eq 'valid');
1.1444    raeburn  11008:                                 }
                   11009:                             }
                   11010:                         }
1.1056    raeburn  11011:                         if ($result eq 'valid') {
                   11012:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  11013:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   11014:                                     push(@newcoowners,$coowner);
                   11015:                                 }
                   11016:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   11017:                                     push(@newcoowners,$uname.':'.$udom);
                   11018:                                 }
                   11019:                                 @newcoowners = sort(@newcoowners);
                   11020:                             } else {
                   11021:                                 push(@newcoowners,$uname.':'.$udom);
                   11022:                             }
1.1444    raeburn  11023:                         } elsif ($coursehash{'internal.co-owners'}) {
                   11024:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   11025:                                 unless ($coowner eq $uname.':'.$udom) {
                   11026:                                     push(@newcoowners,$coowner);
1.1053    raeburn  11027:                                 }
1.1444    raeburn  11028:                             }
                   11029:                             unless (@newcoowners > 0) {
                   11030:                                 $delcoowners = 1;
                   11031:                                 $coowners = '';
1.1053    raeburn  11032:                             }
                   11033:                         }
                   11034:                         if (@newcoowners || $delcoowners) {
                   11035:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   11036:                                             $delcoowners,@newcoowners);
                   11037:                         }
                   11038:                     }
                   11039:                 }
                   11040:             }
                   11041:         }
                   11042:     }
                   11043: }
                   11044: 
                   11045: sub store_coowners {
                   11046:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   11047:     my $cid = $cdom.'_'.$cnum;
                   11048:     my ($coowners,$delresult,$putresult);
                   11049:     if (@newcoowners) {
                   11050:         $coowners = join(',',@newcoowners);
                   11051:         my %coownershash = (
                   11052:                             'internal.co-owners' => $coowners,
                   11053:                            );
                   11054:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   11055:         if ($putresult eq 'ok') {
                   11056:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   11057:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   11058:             }
                   11059:         }
                   11060:     }
                   11061:     if ($delcoowners) {
                   11062:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   11063:         if ($delresult eq 'ok') {
                   11064:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   11065:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   11066:             }
                   11067:         }
                   11068:     }
                   11069:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   11070:         my %crsinfo =
1.1494    raeburn  11071:             &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
1.1053    raeburn  11072:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   11073:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
1.1494    raeburn  11074:             my $cidput = &courseidput($cdom,\%crsinfo,$chome,'notime');
1.1053    raeburn  11075:         }
                   11076:     }
                   11077: }
                   11078: 
1.169     harris41 11079: # -------------------------------------------------- Modify user authentication
1.197     www      11080: # Overrides without validation
                   11081: 
1.169     harris41 11082: sub modifyuserauth {
                   11083:     my ($udom,$uname,$umode,$upass)=@_;
                   11084:     my $uhome=&homeserver($uname,$udom);
1.1409    raeburn  11085:     my $allowed;
                   11086:     if (&allowed('mau',$udom)) {
                   11087:         $allowed = 1;
                   11088:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
                   11089:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
                   11090:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
                   11091:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   11092:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   11093:         if (($cdom ne '') && ($cnum ne '')) {
                   11094:             my $is_owner = &is_course_owner($cdom,$cnum);
                   11095:             if ($is_owner) {
                   11096:                 $allowed = 1;
                   11097:             }
                   11098:         }
                   11099:     }
                   11100:     unless ($allowed) { return 'refused'; }
1.197     www      11101:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 11102:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   11103:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 11104:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   11105: 		     &escape($upass),$uhome);
1.1434    raeburn  11106:     my $ip = &get_requestor_ip();
1.620     albertel 11107:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      11108:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1434    raeburn  11109:          '(Remote '.$ip.'): '.$reply);
1.197     www      11110:     &log($udom,,$uname,$uhome,
1.620     albertel 11111:         'Authentication changed by '.$env{'user.domain'}.', '.
                   11112:                                      $env{'user.name'}.', '.$umode.
1.1435    raeburn  11113:          '(Remote '.$ip.'): '.$reply);
1.169     harris41 11114:     unless ($reply eq 'ok') {
1.197     www      11115:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 11116: 	return 'error: '.$reply;
                   11117:     }   
1.170     harris41 11118:     return 'ok';
1.80      www      11119: }
                   11120: 
1.81      www      11121: # --------------------------------------------------------------- Modify a user
1.80      www      11122: 
1.81      www      11123: sub modifyuser {
1.206     matthew  11124:     my ($udom,    $uname, $uid,
                   11125:         $umode,   $upass, $first,
                   11126:         $middle,  $last,  $gene,
1.1058    raeburn  11127:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 11128:     $udom= &LONCAPA::clean_domain($udom);
                   11129:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  11130:     my $showcandelete = 'none';
                   11131:     if (ref($candelete) eq 'ARRAY') {
                   11132:         if (@{$candelete} > 0) {
                   11133:             $showcandelete = join(', ',@{$candelete});
                   11134:         }
                   11135:     }
1.81      www      11136:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      11137:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  11138: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  11139:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   11140:                                      ' desiredhome not specified'). 
1.620     albertel 11141:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   11142:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 11143:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  11144:     my $newuser;
                   11145:     if ($uhome eq 'no_host') {
                   11146:         $newuser = 1;
1.1368    raeburn  11147:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
                   11148:                 ($umode eq 'lti')) {
                   11149:             return 'error: more information needed to create new user';
                   11150:         }
1.1075    raeburn  11151:     }
1.80      www      11152: # ----------------------------------------------------------------- Create User
1.406     albertel 11153:     if (($uhome eq 'no_host') && 
1.1368    raeburn  11154: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80      www      11155:         my $unhome='';
1.844     albertel 11156:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  11157:             $unhome = $desiredhome;
1.620     albertel 11158: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   11159: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  11160:         } else { # load balancing routine for determining $unhome
1.81      www      11161:             my $loadm=10000000;
1.841     albertel 11162: 	    my %servers = &get_servers($udom,'library');
                   11163: 	    foreach my $tryserver (keys(%servers)) {
                   11164: 		my $answer=reply('load',$tryserver);
                   11165: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   11166: 		    $loadm=$answer;
                   11167: 		    $unhome=$tryserver;
                   11168: 		}
1.80      www      11169: 	    }
                   11170:         }
                   11171:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  11172: 	    return 'error: unable to find a home server for '.$uname.
                   11173:                    ' in domain '.$udom;
1.80      www      11174:         }
                   11175:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   11176:                          &escape($upass),$unhome);
                   11177: 	unless ($reply eq 'ok') {
                   11178:             return 'error: '.$reply;
                   11179:         }   
1.230     stredwic 11180:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      11181:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  11182: 	    return 'error: unable verify users home machine.';
1.80      www      11183:         }
1.209     matthew  11184:     }   # End of creation of new user
1.80      www      11185: # ---------------------------------------------------------------------- Add ID
                   11186:     if ($uid) {
                   11187:        $uid=~tr/A-Z/a-z/;
                   11188:        my %uidhash=&idrget($udom,$uname);
1.196     www      11189:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   11190:          && (!$forceid)) {
1.80      www      11191: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  11192: 	      return 'error: user id "'.$uid.'" does not match '.
                   11193:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      11194:           }
                   11195:        } else {
1.1300    raeburn  11196: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
1.80      www      11197:        }
                   11198:     }
                   11199: # -------------------------------------------------------------- Add names, etc
1.313     matthew  11200:     my @tmp=&get('environment',
1.899     raeburn  11201: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  11202:                     'permanentemail','inststatus'],
1.134     albertel 11203: 		   $udom,$uname);
1.1075    raeburn  11204:     my (%names,%oldnames);
1.313     matthew  11205:     if ($tmp[0] =~ m/^error:.*/) { 
                   11206:         %names=(); 
                   11207:     } else {
                   11208:         %names = @tmp;
1.1075    raeburn  11209:         %oldnames = %names;
1.313     matthew  11210:     }
1.388     www      11211: #
1.1058    raeburn  11212: # If name, email and/or uid are blank (e.g., because an uploaded file
                   11213: # of users did not contain them), do not overwrite existing values
                   11214: # unless field is in $candelete array ref.  
                   11215: #
                   11216: 
                   11217:     my @fields = ('firstname','middlename','lastname','generation',
                   11218:                   'permanentemail','id');
                   11219:     my %newvalues;
                   11220:     if (ref($candelete) eq 'ARRAY') {
                   11221:         foreach my $field (@fields) {
                   11222:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   11223:                 if ($field eq 'firstname') {
                   11224:                     $names{$field} = $first;
                   11225:                 } elsif ($field eq 'middlename') {
                   11226:                     $names{$field} = $middle;
                   11227:                 } elsif ($field eq 'lastname') {
                   11228:                     $names{$field} = $last;
                   11229:                 } elsif ($field eq 'generation') { 
                   11230:                     $names{$field} = $gene;
                   11231:                 } elsif ($field eq 'permanentemail') {
                   11232:                     $names{$field} = $email;
                   11233:                 } elsif ($field eq 'id') {
                   11234:                     $names{$field}  = $uid;
                   11235:                 }
                   11236:             }
                   11237:         }
                   11238:     }
1.388     www      11239:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  11240:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      11241:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  11242:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      11243:     if ($email) {
                   11244:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  11245:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      11246:     }
1.899     raeburn  11247:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  11248:     if (defined($inststatus)) {
                   11249:         $names{'inststatus'} = '';
                   11250:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   11251:         if (ref($usertypes) eq 'HASH') {
                   11252:             my @okstatuses; 
                   11253:             foreach my $item (split(/:/,$inststatus)) {
                   11254:                 if (defined($usertypes->{$item})) {
                   11255:                     push(@okstatuses,$item);  
                   11256:                 }
                   11257:             }
                   11258:             if (@okstatuses) {
                   11259:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   11260:             }
                   11261:         }
                   11262:     }
1.1075    raeburn  11263:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  11264:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  11265:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  11266:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   11267:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   11268:     } else {
                   11269:         $logmsg .= ' during self creation';
                   11270:     }
1.1075    raeburn  11271:     my $changed;
                   11272:     if ($newuser) {
                   11273:         $changed = 1;
                   11274:     } else {
                   11275:         foreach my $field (@fields) {
                   11276:             if ($names{$field} ne $oldnames{$field}) {
                   11277:                 $changed = 1;
                   11278:                 last;
                   11279:             }
                   11280:         }
                   11281:     }
                   11282:     unless ($changed) {
                   11283:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   11284:         &logthis($logmsg);
                   11285:         return 'ok';
                   11286:     }
                   11287:     my $reply = &put('environment', \%names, $udom,$uname);
                   11288:     if ($reply ne 'ok') { 
                   11289:         return 'error: '.$reply;
                   11290:     }
1.1087    raeburn  11291:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
1.1494    raeburn  11292:         &devalidate_cache_new('emailscache',$uname.':'.$udom);
1.1087    raeburn  11293:     }
1.1075    raeburn  11294:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   11295:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   11296:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  11297:     &logthis($logmsg);
1.134     albertel 11298:     return 'ok';
1.80      www      11299: }
                   11300: 
1.81      www      11301: # -------------------------------------------------------------- Modify student
1.80      www      11302: 
1.81      www      11303: sub modifystudent {
                   11304:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  11305:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314    raeburn  11306:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455     albertel 11307:     if (!$cid) {
1.620     albertel 11308: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 11309: 	    return 'not_in_class';
                   11310: 	}
1.80      www      11311:     }
                   11312: # --------------------------------------------------------------- Make the user
1.81      www      11313:     my $reply=&modifyuser
1.209     matthew  11314: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  11315:          $desiredhome,$email,$inststatus);
1.80      www      11316:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  11317:     # This will cause &modify_student_enrollment to get the uid from the
1.1235    raeburn  11318:     # student's environment
1.297     matthew  11319:     $uid = undef if (!$forceid);
1.455     albertel 11320:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  11321:                                         $gene,$usec,$end,$start,$type,$locktype,
1.1314    raeburn  11322:                                         $cid,$selfenroll,$context,$credits,$instsec);
1.297     matthew  11323:     return $reply;
                   11324: }
                   11325: 
                   11326: sub modify_student_enrollment {
1.1216    raeburn  11327:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1512    raeburn  11328:         $locktype,$cid,$selfenroll,$context,$credits,$instsec,$othdomby,$requester) = @_;
1.455     albertel 11329:     my ($cdom,$cnum,$chome);
                   11330:     if (!$cid) {
1.620     albertel 11331: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 11332: 	    return 'not_in_class';
                   11333: 	}
1.620     albertel 11334: 	$cdom=$env{'course.'.$cid.'.domain'};
                   11335: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 11336:     } else {
                   11337: 	($cdom,$cnum)=split(/_/,$cid);
                   11338:     }
1.620     albertel 11339:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 11340:     if (!$chome) {
1.457     raeburn  11341: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  11342:     }
1.455     albertel 11343:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  11344:     # Make sure the user exists
1.81      www      11345:     my $uhome=&homeserver($uname,$udom);
                   11346:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   11347: 	return 'error: no such user';
                   11348:     }
1.297     matthew  11349:     # Get student data if we were not given enough information
                   11350:     if (!defined($first)  || $first  eq '' || 
                   11351:         !defined($last)   || $last   eq '' || 
                   11352:         !defined($uid)    || $uid    eq '' || 
                   11353:         !defined($middle) || $middle eq '' || 
                   11354:         !defined($gene)   || $gene   eq '') {
1.294     matthew  11355:         # They did not supply us with enough data to enroll the student, so
                   11356:         # we need to pick up more information.
1.297     matthew  11357:         my %tmp = &get('environment',
1.294     matthew  11358:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  11359:                        ,$udom,$uname);
                   11360: 
1.800     albertel 11361:         #foreach my $key (keys(%tmp)) {
                   11362:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 11363:         #}
1.294     matthew  11364:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   11365:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   11366:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  11367:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  11368:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   11369:     }
1.556     albertel 11370:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  11371:     my $user = "$uname:$udom";
1.1494    raeburn  11372:     my %old_entry = &get('classlist',[$user],$cdom,$cnum);
1.487     albertel 11373:     my $reply=cput('classlist',
1.1148    raeburn  11374: 		   {$user => 
1.1314    raeburn  11375: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487     albertel 11376: 		   $cdom,$cnum);
1.1148    raeburn  11377:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   11378:         &devalidate_getsection_cache($udom,$uname,$cid);
                   11379:     } else { 
1.81      www      11380: 	return 'error: '.$reply;
                   11381:     }
1.297     matthew  11382:     # Add student role to user
1.83      www      11383:     my $uurl='/'.$cid;
1.81      www      11384:     $uurl=~s/\_/\//g;
                   11385:     if ($usec) {
                   11386: 	$uurl.='/'.$usec;
                   11387:     }
1.1148    raeburn  11388:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
1.1512    raeburn  11389:                              $selfenroll,$context,$othdomby,$requester);
1.1148    raeburn  11390:     if ($result ne 'ok') {
                   11391:         if ($old_entry{$user} ne '') {
                   11392:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   11393:         } else {
                   11394:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   11395:         }
                   11396:     }
                   11397:     return $result; 
1.21      www      11398: }
                   11399: 
1.556     albertel 11400: sub format_name {
                   11401:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   11402:     my $name;
                   11403:     if ($first ne 'lastname') {
                   11404: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   11405:     } else {
                   11406: 	if ($lastname=~/\S/) {
                   11407: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   11408: 	    $name=~s/\s+,/,/;
                   11409: 	} else {
                   11410: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   11411: 	}
                   11412:     }
                   11413:     $name=~s/^\s+//;
                   11414:     $name=~s/\s+$//;
                   11415:     $name=~s/\s+/ /g;
                   11416:     return $name;
                   11417: }
                   11418: 
1.84      www      11419: # ------------------------------------------------- Write to course preferences
                   11420: 
                   11421: sub writecoursepref {
                   11422:     my ($courseid,%prefs)=@_;
                   11423:     $courseid=~s/^\///;
                   11424:     $courseid=~s/\_/\//g;
                   11425:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   11426:     my $chome=homeserver($cnum,$cdomain);
                   11427:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   11428: 	return 'error: no such course';
                   11429:     }
                   11430:     my $cstring='';
1.800     albertel 11431:     foreach my $pref (keys(%prefs)) {
                   11432: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 11433:     }
1.84      www      11434:     $cstring=~s/\&$//;
                   11435:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   11436: }
                   11437: 
                   11438: # ---------------------------------------------------------- Make/modify course
                   11439: 
                   11440: sub createcourse {
1.741     raeburn  11441:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1423    raeburn  11442:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84      www      11443:     $url=&declutter($url);
                   11444:     my $cid='';
1.1028    raeburn  11445:     if ($context eq 'requestcourses') {
                   11446:         my $can_create = 0;
                   11447:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   11448:         if ($udom eq $ownerdom) {
1.1423    raeburn  11449:             my $reload;
                   11450:             if (($callercontext eq 'auto') &&
                   11451:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
                   11452:                 $reload = 'reload';
                   11453:             }
                   11454:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028    raeburn  11455:                                   $context)) {
                   11456:                 $can_create = 1;
                   11457:             }
                   11458:         } else {
                   11459:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   11460:                                            $category);
                   11461:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   11462:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   11463:                 if (@curr > 0) {
                   11464:                     my @options = qw(approval validate autolimit);
                   11465:                     my $optregex = join('|',@options);
                   11466:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   11467:                         $can_create = 1;
                   11468:                     }
                   11469:                 }
                   11470:             }
                   11471:         }
                   11472:         if ($can_create) {
                   11473:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   11474:                 unless (&allowed('ccc',$udom)) {
                   11475:                     return 'refused'; 
                   11476:                 }
1.1017    raeburn  11477:             }
                   11478:         } else {
                   11479:             return 'refused';
                   11480:         }
1.1028    raeburn  11481:     } elsif (!&allowed('ccc',$udom)) {
                   11482:         return 'refused';
1.84      www      11483:     }
1.1011    raeburn  11484: # --------------------------------------------------------------- Get Unique ID
                   11485:     my $uname;
                   11486:     if ($cnum =~ /^$match_courseid$/) {
                   11487:         my $chome=&homeserver($cnum,$udom,'true');
                   11488:         if (($chome eq '') || ($chome eq 'no_host')) {
                   11489:             $uname = $cnum;
                   11490:         } else {
1.1038    raeburn  11491:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  11492:         }
                   11493:     } else {
1.1038    raeburn  11494:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  11495:     }
                   11496:     return $uname if ($uname =~ /^error/);
                   11497: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  11498:     if (!defined($course_server)) {
                   11499:         if (defined(&domain($udom,'primary'))) {
                   11500:             $course_server = &domain($udom,'primary');
                   11501:         } else {
                   11502:             $course_server = $env{'user.home'}; 
                   11503:         }
                   11504:     }
                   11505:     my %host_servers =
1.1494    raeburn  11506:         &get_servers($udom,'library');
1.1052    raeburn  11507:     unless ($host_servers{$course_server}) {
                   11508:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  11509:     }
1.84      www      11510: # ------------------------------------------------------------- Make the course
                   11511:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  11512:                       $course_server);
1.84      www      11513:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  11514:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      11515:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   11516: 	return 'error: no such course';
                   11517:     }
1.271     www      11518: # ----------------------------------------------------------------- Course made
1.516     raeburn  11519: # log existence
1.1029    raeburn  11520:     my $now = time;
1.918     raeburn  11521:     my $newcourse = {
                   11522:                     $udom.'_'.$uname => {
1.921     raeburn  11523:                                      description => $description,
                   11524:                                      inst_code   => $inst_code,
                   11525:                                      owner       => $course_owner,
                   11526:                                      type        => $crstype,
1.1029    raeburn  11527:                                      creator     => $env{'user.name'}.':'.
                   11528:                                                     $env{'user.domain'},
                   11529:                                      created     => $now,
                   11530:                                      context     => $context,
1.918     raeburn  11531:                                                 },
                   11532:                     };
1.921     raeburn  11533:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      11534: # set toplevel url
1.271     www      11535:     my $topurl=$url;
                   11536:     unless ($nonstandard) {
                   11537: # ------------------------------------------ For standard courses, make top url
                   11538:         my $mapurl=&clutter($url);
1.278     www      11539:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 11540:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      11541: <map>
                   11542: <resource id="1" type="start"></resource>
                   11543: <resource id="2" src="$mapurl"></resource>
                   11544: <resource id="3" type="finish"></resource>
                   11545: <link index="1" from="1" to="2"></link>
                   11546: <link index="2" from="2" to="3"></link>
                   11547: </map>
                   11548: ENDINITMAP
                   11549:         $topurl=&declutter(
1.638     albertel 11550:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      11551:                           );
                   11552:     }
                   11553: # ----------------------------------------------------------- Write preferences
1.84      www      11554:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  11555:                      ('description'              => $description,
                   11556:                       'url'                      => $topurl,
                   11557:                       'internal.creator'         => $env{'user.name'}.':'.
                   11558:                                                     $env{'user.domain'},
                   11559:                       'internal.created'         => $now,
                   11560:                       'internal.creationcontext' => $context)
                   11561:                     );
1.84      www      11562:     return '/'.$udom.'/'.$uname;
                   11563: }
                   11564: 
1.1011    raeburn  11565: # ------------------------------------------------------------------- Create ID
                   11566: sub generate_coursenum {
1.1038    raeburn  11567:     my ($udom,$crstype) = @_;
1.1011    raeburn  11568:     my $domdesc = &domain($udom);
                   11569:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  11570:     my $first;
                   11571:     if ($crstype eq 'Community') {
                   11572:         $first = '0';
                   11573:     } else {
                   11574:         $first = int(1+rand(9)); 
                   11575:     } 
                   11576:     my $uname=$first.
1.1011    raeburn  11577:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   11578:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   11579:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   11580: # ----------------------------------------------- Make sure that does not exist
                   11581:     my $uhome=&homeserver($uname,$udom,'true');
                   11582:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  11583:         if ($crstype eq 'Community') {
                   11584:             $first = '0';
                   11585:         } else {
                   11586:             $first = int(1+rand(9));
                   11587:         }
                   11588:         $uname=$first.
1.1011    raeburn  11589:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   11590:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   11591:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   11592:         $uhome=&homeserver($uname,$udom,'true');
                   11593:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   11594:             return 'error: unable to generate unique course-ID';
                   11595:         }
                   11596:     }
                   11597:     return $uname;
                   11598: }
                   11599: 
1.813     albertel 11600: sub is_course {
1.1167    droeschl 11601:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   11602:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   11603: 
1.1381    raeburn  11604:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
                   11605:     my $uhome=&homeserver($cnum,$cdom);
                   11606:     my $iscourse;
                   11607:     if (grep { $_ eq $uhome } current_machine_ids()) {
1.1382    raeburn  11608:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
1.1381    raeburn  11609:     } else {
                   11610:         my $hashid = $cdom.':'.$cnum;
                   11611:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
                   11612:         unless (defined($cached)) {
                   11613:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
                   11614:                                         $cnum,undef,undef,'.');
                   11615:             $iscourse = 0;
                   11616:             if (exists($courses{$cdom.'_'.$cnum})) {
                   11617:                 $iscourse = 1;
                   11618:             }
                   11619:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
                   11620:         }
                   11621:     }
                   11622:     return unless ($iscourse);
1.1167    droeschl 11623:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 11624: }
                   11625: 
1.1015    raeburn  11626: sub store_userdata {
                   11627:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  11628:     my $result;
1.1016    raeburn  11629:     if ($datakey ne '') {
1.1013    raeburn  11630:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  11631:             if ($udom eq '' || $uname eq '') {
                   11632:                 $udom = $env{'user.domain'};
                   11633:                 $uname = $env{'user.name'};
                   11634:             }
                   11635:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  11636:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   11637:                 $result = 'error: no_host';
                   11638:             } else {
1.1434    raeburn  11639:                 $storehash->{'ip'} = &get_requestor_ip();
1.1013    raeburn  11640:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   11641: 
                   11642:                 my $namevalue='';
                   11643:                 foreach my $key (keys(%{$storehash})) {
                   11644:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   11645:                 }
                   11646:                 $namevalue=~s/\&$//;
1.1224    raeburn  11647:                 unless ($namespace eq 'courserequests') {
                   11648:                     $datakey = &escape($datakey);
                   11649:                 }
1.1105    raeburn  11650:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   11651:                                   $namevalue,$uhome);
1.1013    raeburn  11652:             }
                   11653:         } else {
                   11654:             $result = 'error: data to store was not a hash reference'; 
                   11655:         }
                   11656:     } else {
                   11657:         $result= 'error: invalid requestkey'; 
                   11658:     }
                   11659:     return $result;
                   11660: }
                   11661: 
1.21      www      11662: # ---------------------------------------------------------- Assign Custom Role
                   11663: 
                   11664: sub assigncustomrole {
1.1512    raeburn  11665:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,
                   11666:         $selfenroll,$context,$othdomby,$requester)=@_;
1.21      www      11667:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.1512    raeburn  11668:                        $end,$start,$deleteflag,$selfenroll,$context,$othdomby,
                   11669:                        $requester);
1.21      www      11670: }
                   11671: 
                   11672: # ----------------------------------------------------------------- Revoke Role
                   11673: 
                   11674: sub revokerole {
1.957     raeburn  11675:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      11676:     my $now=time;
1.965     raeburn  11677:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      11678: }
                   11679: 
                   11680: # ---------------------------------------------------------- Revoke Custom Role
                   11681: 
                   11682: sub revokecustomrole {
1.957     raeburn  11683:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      11684:     my $now=time;
1.357     www      11685:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  11686:            $deleteflag,$selfenroll,$context);
1.17      www      11687: }
                   11688: 
1.533     banghart 11689: # ------------------------------------------------------------ Disk usage
1.535     albertel 11690: sub diskusage {
1.955     raeburn  11691:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   11692:     $directorypath =~ s/\/$//;
                   11693:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   11694:                        .&escape($getpropath).':'.&escape($uname).':'
                   11695:                        .&escape($udom),homeserver($uname,$udom));
                   11696:     if ($listing eq 'unknown_cmd') {
                   11697:         if ($getpropath) {
                   11698:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   11699:         }
                   11700:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   11701:     }
1.514     albertel 11702:     return $listing;
1.512     banghart 11703: }
                   11704: 
1.566     banghart 11705: sub is_locked {
1.1096    raeburn  11706:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 11707:     my @check;
                   11708:     my $is_locked;
1.1093    raeburn  11709:     push (@check,$file_name);
1.613     albertel 11710:     my %locked = &get('file_permissions',\@check,
1.620     albertel 11711: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 11712:     my ($tmp)=keys(%locked);
                   11713:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  11714:     
1.566     banghart 11715:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  11716:         $is_locked = 'false';
                   11717:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  11718:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  11719:                $is_locked = 'true';
1.1096    raeburn  11720:                if (ref($which) eq 'ARRAY') {
                   11721:                    push(@{$which},$entry);
                   11722:                } else {
                   11723:                    last;
                   11724:                }
1.745     raeburn  11725:            }
                   11726:        }
1.566     banghart 11727:     } else {
                   11728:         $is_locked = 'false';
                   11729:     }
1.1093    raeburn  11730:     return $is_locked;
1.566     banghart 11731: }
                   11732: 
1.759     albertel 11733: sub declutter_portfile {
                   11734:     my ($file) = @_;
1.833     albertel 11735:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 11736:     return $file;
                   11737: }
                   11738: 
1.559     banghart 11739: # ------------------------------------------------------------- Mark as Read Only
                   11740: 
                   11741: sub mark_as_readonly {
                   11742:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 11743:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11744:     my ($tmp)=keys(%current_permissions);
                   11745:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 11746:     foreach my $file (@{$files}) {
1.759     albertel 11747: 	$file = &declutter_portfile($file);
1.561     banghart 11748:         push(@{$current_permissions{$file}},$what);
1.559     banghart 11749:     }
1.613     albertel 11750:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11751:     return;
                   11752: }
                   11753: 
1.572     banghart 11754: # ------------------------------------------------------------Save Selected Files
                   11755: 
                   11756: sub save_selected_files {
                   11757:     my ($user, $path, @files) = @_;
                   11758:     my $filename = $user."savedfiles";
1.573     banghart 11759:     my @other_files = &files_not_in_path($user, $path);
1.1359    raeburn  11760:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.573     banghart 11761:     foreach my $file (@files) {
1.620     albertel 11762:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 11763:     }
                   11764:     foreach my $file (@other_files) {
1.574     banghart 11765:         print (OUT $file."\n");
1.572     banghart 11766:     }
1.574     banghart 11767:     close (OUT);
1.572     banghart 11768:     return 'ok';
                   11769: }
                   11770: 
1.574     banghart 11771: sub clear_selected_files {
                   11772:     my ($user) = @_;
                   11773:     my $filename = $user."savedfiles";
1.1359    raeburn  11774:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.574     banghart 11775:     print (OUT undef);
                   11776:     close (OUT);
                   11777:     return ("ok");    
                   11778: }
                   11779: 
1.572     banghart 11780: sub files_in_path {
                   11781:     my ($user, $path) = @_;
                   11782:     my $filename = $user."savedfiles";
                   11783:     my %return_files;
1.1359    raeburn  11784:     open (IN,'<',LONCAPA::tempdir().$filename);
1.573     banghart 11785:     while (my $line_in = <IN>) {
1.574     banghart 11786:         chomp ($line_in);
                   11787:         my @paths_and_file = split (m!/!, $line_in);
                   11788:         my $file_part = pop (@paths_and_file);
                   11789:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 11790:         $path_part.='/';
                   11791:         my $path_and_file = $path_part.$file_part;
                   11792:         if ($path_part eq $path) {
                   11793:             $return_files{$file_part}= 'selected';
                   11794:         }
                   11795:     }
1.574     banghart 11796:     close (IN);
                   11797:     return (\%return_files);
1.572     banghart 11798: }
                   11799: 
                   11800: # called in portfolio select mode, to show files selected NOT in current directory
                   11801: sub files_not_in_path {
                   11802:     my ($user, $path) = @_;
                   11803:     my $filename = $user."savedfiles";
                   11804:     my @return_files;
                   11805:     my $path_part;
1.1359    raeburn  11806:     open(IN, '<',LONCAPA::tempdir().$filename);
1.800     albertel 11807:     while (my $line = <IN>) {
1.572     banghart 11808:         #ok, I know it's clunky, but I want it to work
1.800     albertel 11809:         my @paths_and_file = split(m|/|, $line);
                   11810:         my $file_part = pop(@paths_and_file);
                   11811:         chomp($file_part);
                   11812:         my $path_part = join('/', @paths_and_file);
1.572     banghart 11813:         $path_part .= '/';
                   11814:         my $path_and_file = $path_part.$file_part;
                   11815:         if ($path_part ne $path) {
1.800     albertel 11816:             push(@return_files, ($path_and_file));
1.572     banghart 11817:         }
                   11818:     }
1.800     albertel 11819:     close(OUT);
1.574     banghart 11820:     return (@return_files);
1.572     banghart 11821: }
                   11822: 
1.1273    raeburn  11823: #------------------------------Submitted/Handedback Portfolio Files Versioning
                   11824:  
                   11825: sub portfiles_versioning {
                   11826:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
                   11827:     my $portfolio_root = '/userfiles/portfolio';
                   11828:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
                   11829:     foreach my $file (@{$portfiles}) {
                   11830:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
                   11831:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   11832:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
                   11833:         my $getpropath = 1;
                   11834:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
                   11835:                                              $stu_name,$getpropath);
                   11836:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
                   11837:         my $new_answer = 
                   11838:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
                   11839:         if ($new_answer ne 'problem getting file') {
                   11840:             push(@{$versioned_portfiles}, $directory.$new_answer);
                   11841:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
                   11842:                               [$symb,$env{'request.course.id'},'graded']);
                   11843:         }
                   11844:     }
                   11845: }
                   11846: 
                   11847: sub get_next_version {
                   11848:     my ($answer_name, $answer_ext, $dir_list) = @_;
                   11849:     my $version;
                   11850:     if (ref($dir_list) eq 'ARRAY') {
                   11851:         foreach my $row (@{$dir_list}) {
                   11852:             my ($file) = split(/\&/,$row,2);
                   11853:             my ($file_name,$file_version,$file_ext) =
                   11854:                 &file_name_version_ext($file);
                   11855:             if (($file_name eq $answer_name) &&
                   11856:                 ($file_ext eq $answer_ext)) {
                   11857:                      # gets here if filename and extension match,
                   11858:                      # regardless of version
                   11859:                 if ($file_version ne '') {
                   11860:                     # a versioned file is found  so save it for later
                   11861:                     if ($file_version > $version) {
                   11862:                         $version = $file_version;
                   11863:                     }
                   11864:                 }
                   11865:             }
                   11866:         }
                   11867:     }
                   11868:     $version ++;
                   11869:     return($version);
                   11870: }
                   11871: 
                   11872: sub version_selected_portfile {
                   11873:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   11874:     my ($answer_name,$answer_ver,$answer_ext) =
                   11875:         &file_name_version_ext($file_name);
                   11876:     my $new_answer;
                   11877:     $env{'form.copy'} =
                   11878:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   11879:     if($env{'form.copy'} eq '-1') {
                   11880:         $new_answer = 'problem getting file';
                   11881:     } else {
                   11882:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   11883:         my $copy_result = 
                   11884:             &finishuserfileupload($stu_name,$domain,'copy',
                   11885:                                   '/portfolio'.$directory.$new_answer);
                   11886:     }
                   11887:     undef($env{'form.copy'});
                   11888:     return ($new_answer);
                   11889: }
                   11890: 
                   11891: sub file_name_version_ext {
                   11892:     my ($file)=@_;
                   11893:     my @file_parts = split(/\./, $file);
                   11894:     my ($name,$version,$ext);
                   11895:     if (@file_parts > 1) {
                   11896:         $ext=pop(@file_parts);
                   11897:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   11898:             $version=pop(@file_parts);
                   11899:         }
                   11900:         $name=join('.',@file_parts);
                   11901:     } else {
                   11902:         $name=join('.',@file_parts);
                   11903:     }
                   11904:     return($name,$version,$ext);
                   11905: }
                   11906: 
1.745     raeburn  11907: #----------------------------------------------Get portfolio file permissions
1.629     banghart 11908: 
1.745     raeburn  11909: sub get_portfile_permissions {
                   11910:     my ($domain,$user) = @_;
1.613     albertel 11911:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11912:     my ($tmp)=keys(%current_permissions);
                   11913:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11914:     return \%current_permissions;
                   11915: }
                   11916: 
                   11917: #---------------------------------------------Get portfolio file access controls
                   11918: 
1.749     raeburn  11919: sub get_access_controls {
1.745     raeburn  11920:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 11921:     my %access;
                   11922:     my $real_file = $file;
                   11923:     $file =~ s/\.meta$//;
1.745     raeburn  11924:     if (defined($file)) {
1.749     raeburn  11925:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   11926:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 11927:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  11928:             }
                   11929:         }
1.745     raeburn  11930:     } else {
1.749     raeburn  11931:         foreach my $key (keys(%{$current_permissions})) {
                   11932:             if ($key =~ /\0accesscontrol$/) {
                   11933:                 if (defined($group)) {
                   11934:                     if ($key !~ m-^\Q$group\E/-) {
                   11935:                         next;
                   11936:                     }
                   11937:                 }
                   11938:                 my ($fullpath) = split(/\0/,$key);
                   11939:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   11940:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   11941:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   11942:                     }
                   11943:                 }
                   11944:             }
                   11945:         }
                   11946:     }
                   11947:     return %access;
                   11948: }
                   11949: 
                   11950: sub modify_access_controls {
                   11951:     my ($file_name,$changes,$domain,$user)=@_;
                   11952:     my ($outcome,$deloutcome);
                   11953:     my %store_permissions;
                   11954:     my %new_values;
                   11955:     my %new_control;
                   11956:     my %translation;
                   11957:     my @deletions = ();
                   11958:     my $now = time;
                   11959:     if (exists($$changes{'activate'})) {
                   11960:         if (ref($$changes{'activate'}) eq 'HASH') {
                   11961:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   11962:             my $numnew = scalar(@newitems);
                   11963:             for (my $i=0; $i<$numnew; $i++) {
                   11964:                 my $newkey = $newitems[$i];
                   11965:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  11966:                 if ($newkey =~ /^\d+:/) { 
                   11967:                     $newkey =~ s/^(\d+)/$newid/;
                   11968:                     $translation{$1} = $newid;
                   11969:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   11970:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   11971:                     $translation{$1} = $newid;
                   11972:                 }
1.749     raeburn  11973:                 $new_values{$file_name."\0".$newkey} = 
                   11974:                                           $$changes{'activate'}{$newitems[$i]};
                   11975:                 $new_control{$newkey} = $now;
                   11976:             }
                   11977:         }
                   11978:     }
                   11979:     my %todelete;
                   11980:     my %changed_items;
                   11981:     foreach my $action ('delete','update') {
                   11982:         if (exists($$changes{$action})) {
                   11983:             if (ref($$changes{$action}) eq 'HASH') {
                   11984:                 foreach my $key (keys(%{$$changes{$action}})) {
                   11985:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   11986:                     if ($action eq 'delete') { 
                   11987:                         $todelete{$itemnum} = 1;
                   11988:                     } else {
                   11989:                         $changed_items{$itemnum} = $key;
                   11990:                     }
                   11991:                 }
1.745     raeburn  11992:             }
                   11993:         }
1.749     raeburn  11994:     }
                   11995:     # get lock on access controls for file.
                   11996:     my $lockhash = {
                   11997:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   11998:                                                        ':'.$env{'user.domain'},
                   11999:                    }; 
                   12000:     my $tries = 0;
                   12001:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   12002:    
1.1288    damieng  12003:     while (($gotlock ne 'ok') && $tries < 10) {
1.749     raeburn  12004:         $tries ++;
1.1289    damieng  12005:         sleep(0.1);
1.749     raeburn  12006:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   12007:     }
                   12008:     if ($gotlock eq 'ok') {
                   12009:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   12010:         my ($tmp)=keys(%curr_permissions);
                   12011:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   12012:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   12013:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   12014:             if (ref($curr_controls) eq 'HASH') {
                   12015:                 foreach my $control_item (keys(%{$curr_controls})) {
                   12016:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   12017:                     if (defined($todelete{$itemnum})) {
                   12018:                         push(@deletions,$file_name."\0".$control_item);
                   12019:                     } else {
                   12020:                         if (defined($changed_items{$itemnum})) {
                   12021:                             $new_control{$changed_items{$itemnum}} = $now;
                   12022:                             push(@deletions,$file_name."\0".$control_item);
                   12023:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   12024:                         } else {
                   12025:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   12026:                         }
                   12027:                     }
1.745     raeburn  12028:                 }
                   12029:             }
                   12030:         }
1.970     raeburn  12031:         my ($group);
                   12032:         if (&is_course($domain,$user)) {
                   12033:             ($group,my $file) = split(/\//,$file_name,2);
                   12034:         }
1.749     raeburn  12035:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   12036:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   12037:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   12038:         #  remove lock
                   12039:         my @del_lock = ($file_name."\0".'locked_access_records');
                   12040:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  12041:         my $sqlresult =
1.970     raeburn  12042:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  12043:                                     $group);
1.749     raeburn  12044:     } else {
                   12045:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  12046:     }
1.749     raeburn  12047:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  12048: }
                   12049: 
1.827     raeburn  12050: sub make_public_indefinitely {
1.1271    raeburn  12051:     my (@requrl) = @_;
                   12052:     return &automated_portfile_access('public',\@requrl);
                   12053: }
                   12054: 
                   12055: sub automated_portfile_access {
                   12056:     my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273    raeburn  12057:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
                   12058:         return 'invalid';
                   12059:     }
1.1271    raeburn  12060:     my %urls;
                   12061:     if (ref($addsref) eq 'ARRAY') {
                   12062:         foreach my $requrl (@{$addsref}) {
                   12063:             if (&is_portfolio_url($requrl)) {
                   12064:                 unless (exists($urls{$requrl})) {
                   12065:                     $urls{$requrl} = 'add';
                   12066:                 }
                   12067:             }
                   12068:         }
                   12069:     }
                   12070:     if (ref($delsref) eq 'ARRAY') {
                   12071:         foreach my $requrl (@{$delsref}) { 
                   12072:             if (&is_portfolio_url($requrl)) {
                   12073:                 unless (exists($urls{$requrl})) {
                   12074:                     $urls{$requrl} = 'delete'; 
                   12075:                 }
                   12076:             }
                   12077:         }
                   12078:     }
                   12079:     unless (keys(%urls)) {
                   12080:         return 'invalid';
                   12081:     }
                   12082:     my $ip;
                   12083:     if ($accesstype eq 'ip') {
                   12084:         if (ref($info) eq 'HASH') {
                   12085:             if ($info->{'ip'} ne '') {
                   12086:                 $ip = $info->{'ip'};
                   12087:             }
                   12088:         }
                   12089:         if ($ip eq '') {
                   12090:             return 'invalid';
                   12091:         }
                   12092:     }
                   12093:     my $errors;
1.827     raeburn  12094:     my $now = time;
1.1271    raeburn  12095:     my %current_perms;
                   12096:     foreach my $requrl (sort(keys(%urls))) {
                   12097:         my $action;
                   12098:         if ($urls{$requrl} eq 'add') {
                   12099:             $action = 'activate';
                   12100:         } else {
                   12101:             $action = 'none';
                   12102:         }
                   12103:         my $aclnum = 0;
1.827     raeburn  12104:         my (undef,$udom,$unum,$file_name,$group) =
                   12105:             &parse_portfolio_url($requrl);
1.1271    raeburn  12106:         unless (exists($current_perms{$unum.':'.$udom})) {
                   12107:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
                   12108:         }
                   12109:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827     raeburn  12110:                                                    $group,$file_name);
                   12111:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   12112:             my ($num,$scope,$end,$start) = 
                   12113:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271    raeburn  12114:             if ($scope eq $accesstype) {
                   12115:                 if (($start <= $now) && ($end == 0)) {
                   12116:                     if ($accesstype eq 'ip') {
                   12117:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
                   12118:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
                   12119:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
                   12120:                                     if ($urls{$requrl} eq 'add') {
                   12121:                                         $action = 'none';
                   12122:                                         last;
                   12123:                                     } else {
                   12124:                                         $action = 'delete';
                   12125:                                         $aclnum = $num;
                   12126:                                         last;
                   12127:                                     }
                   12128:                                 }
                   12129:                             }
                   12130:                         }
                   12131:                     } elsif ($accesstype eq 'public') {
                   12132:                         if ($urls{$requrl} eq 'add') {
                   12133:                             $action = 'none';
                   12134:                             last;
                   12135:                         } else {
                   12136:                             $action = 'delete';
                   12137:                             $aclnum = $num;
                   12138:                             last;
                   12139:                         }
                   12140:                     }
                   12141:                 } elsif ($accesstype eq 'public') {
1.827     raeburn  12142:                     $action = 'update';
                   12143:                     $aclnum = $num;
1.1271    raeburn  12144:                     last;
1.827     raeburn  12145:                 }
                   12146:             }
                   12147:         }
                   12148:         if ($action eq 'none') {
1.1271    raeburn  12149:             next;
1.827     raeburn  12150:         } else {
                   12151:             my %changes;
                   12152:             my $newend = 0;
                   12153:             my $newstart = $now;
1.1271    raeburn  12154:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827     raeburn  12155:             $changes{$action}{$newkey} = {
1.1271    raeburn  12156:                 type => $accesstype,
1.827     raeburn  12157:                 time => {
                   12158:                     start => $newstart,
                   12159:                     end   => $newend,
                   12160:                 },
                   12161:             };
1.1271    raeburn  12162:             if ($accesstype eq 'ip') {
                   12163:                 $changes{$action}{$newkey}{'ip'} = [$ip];
                   12164:             }
1.827     raeburn  12165:             my ($outcome,$deloutcome,$new_values,$translation) =
                   12166:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271    raeburn  12167:             unless ($outcome eq 'ok') {
                   12168:                 $errors .= $outcome.' ';
                   12169:             }
1.827     raeburn  12170:         }
1.1271    raeburn  12171:     }
                   12172:     if ($errors) {
                   12173:         $errors =~ s/\s$//;
                   12174:         return $errors;
1.827     raeburn  12175:     } else {
1.1271    raeburn  12176:         return 'ok';
1.827     raeburn  12177:     }
                   12178: }
                   12179: 
1.745     raeburn  12180: #------------------------------------------------------Get Marked as Read Only
                   12181: 
                   12182: sub get_marked_as_readonly {
                   12183:     my ($domain,$user,$what,$group) = @_;
                   12184:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 12185:     my @readonly_files;
1.629     banghart 12186:     my $cmp1=$what;
                   12187:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  12188:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   12189:         if (defined($group)) {
                   12190:             if ($file_name !~ m-^\Q$group\E/-) {
                   12191:                 next;
                   12192:             }
                   12193:         }
1.561     banghart 12194:         if (ref($value) eq "ARRAY"){
                   12195:             foreach my $stored_what (@{$value}) {
1.629     banghart 12196:                 my $cmp2=$stored_what;
1.759     albertel 12197:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  12198:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  12199:                 }
1.629     banghart 12200:                 if ($cmp1 eq $cmp2) {
1.561     banghart 12201:                     push(@readonly_files, $file_name);
1.745     raeburn  12202:                     last;
1.563     banghart 12203:                 } elsif (!defined($what)) {
                   12204:                     push(@readonly_files, $file_name);
1.745     raeburn  12205:                     last;
1.561     banghart 12206:                 }
                   12207:             }
1.745     raeburn  12208:         }
1.561     banghart 12209:     }
                   12210:     return @readonly_files;
                   12211: }
1.577     banghart 12212: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 12213: 
1.577     banghart 12214: sub get_marked_as_readonly_hash {
1.745     raeburn  12215:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 12216:     my %readonly_files;
1.745     raeburn  12217:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   12218:         if (defined($group)) {
                   12219:             if ($file_name !~ m-^\Q$group\E/-) {
                   12220:                 next;
                   12221:             }
                   12222:         }
1.577     banghart 12223:         if (ref($value) eq "ARRAY"){
                   12224:             foreach my $stored_what (@{$value}) {
1.745     raeburn  12225:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 12226:                     foreach my $lock_descriptor(@{$stored_what}) {
                   12227:                         if ($lock_descriptor eq 'graded') {
                   12228:                             $readonly_files{$file_name} = 'graded';
                   12229:                         } elsif ($lock_descriptor eq 'handback') {
                   12230:                             $readonly_files{$file_name} = 'handback';
                   12231:                         } else {
                   12232:                             if (!exists($readonly_files{$file_name})) {
                   12233:                                 $readonly_files{$file_name} = 'locked';
                   12234:                             }
                   12235:                         }
1.745     raeburn  12236:                     }
1.750     banghart 12237:                 } 
1.577     banghart 12238:             }
                   12239:         } 
                   12240:     }
                   12241:     return %readonly_files;
                   12242: }
1.559     banghart 12243: # ------------------------------------------------------------ Unmark as Read Only
                   12244: 
                   12245: sub unmark_as_readonly {
1.629     banghart 12246:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   12247:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  12248:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 12249:     $file_name = &declutter_portfile($file_name);
1.634     albertel 12250:     my $symb_crs = $what;
                   12251:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  12252:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 12253:     my ($tmp)=keys(%current_permissions);
                   12254:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  12255:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 12256:     foreach my $file (@readonly_files) {
1.759     albertel 12257: 	my $clean_file = &declutter_portfile($file);
                   12258: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 12259: 	my $current_locks = $current_permissions{$file};
1.563     banghart 12260:         my @new_locks;
                   12261:         my @del_keys;
                   12262:         if (ref($current_locks) eq "ARRAY"){
                   12263:             foreach my $locker (@{$current_locks}) {
1.632     albertel 12264:                 my $compare=$locker;
1.749     raeburn  12265:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  12266:                     $compare=join('',@{$locker});
1.746     raeburn  12267:                     if ($compare ne $symb_crs) {
                   12268:                         push(@new_locks, $locker);
                   12269:                     }
1.563     banghart 12270:                 }
                   12271:             }
1.650     albertel 12272:             if (scalar(@new_locks) > 0) {
1.563     banghart 12273:                 $current_permissions{$file} = \@new_locks;
                   12274:             } else {
                   12275:                 push(@del_keys, $file);
1.613     albertel 12276:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 12277:                 delete($current_permissions{$file});
1.563     banghart 12278:             }
                   12279:         }
1.561     banghart 12280:     }
1.613     albertel 12281:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 12282:     return;
                   12283: }
1.512     banghart 12284: 
1.17      www      12285: # ------------------------------------------------------------ Directory lister
                   12286: 
                   12287: sub dirlist {
1.955     raeburn  12288:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      12289:     $uri=~s/^\///;
                   12290:     $uri=~s/\/$//;
1.253     stredwic 12291:     my ($udom, $uname);
1.955     raeburn  12292:     if ($getuserdir) {
1.253     stredwic 12293:         $udom = $userdomain;
                   12294:         $uname = $username;
1.955     raeburn  12295:     } else {
                   12296:         (undef,$udom,$uname)=split(/\//,$uri);
                   12297:         if(defined($userdomain)) {
                   12298:             $udom = $userdomain;
                   12299:         }
                   12300:         if(defined($username)) {
                   12301:             $uname = $username;
                   12302:         }
1.253     stredwic 12303:     }
1.955     raeburn  12304:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 12305: 
1.955     raeburn  12306:     $dirRoot = $perlvar{'lonDocRoot'};
                   12307:     if (defined($getpropath)) {
                   12308:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 12309:         $dirRoot =~ s/\/$//;
1.955     raeburn  12310:     } elsif (defined($getuserdir)) {
                   12311:         my $subdir=$uname.'__';
                   12312:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   12313:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   12314:                    ."/$udom/$subdir/$uname";
                   12315:     } elsif (defined($alternateRoot)) {
                   12316:         $dirRoot = $alternateRoot;
1.751     banghart 12317:     }
1.253     stredwic 12318: 
                   12319:     if($udom) {
                   12320:         if($uname) {
1.1135    raeburn  12321:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  12322:             if ($uhome eq 'no_host') {
                   12323:                 return ([],'no_host');
                   12324:             }
1.955     raeburn  12325:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  12326:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  12327:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  12328:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  12329:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  12330:             } else {
                   12331:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12332:             }
1.605     matthew  12333:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  12334:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  12335:                 @listing_results = split(/:/,$listing);
                   12336:             } else {
                   12337:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12338:             }
1.1135    raeburn  12339:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  12340:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   12341:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   12342:                 return ([],$listing);
                   12343:             } else {
                   12344:                 return (\@listing_results);
1.1135    raeburn  12345:             }
1.955     raeburn  12346:         } elsif(!$alternateRoot) {
1.1136    raeburn  12347:             my (%allusers,%listerror);
1.841     albertel 12348: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  12349:  	    foreach my $tryserver (keys(%servers)) {
                   12350:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   12351:                                   &escape($udom),$tryserver);
                   12352:                 if ($listing eq 'unknown_cmd') {
                   12353: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   12354: 				      $udom, $tryserver);
                   12355:                 } else {
                   12356:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12357:                 }
1.841     albertel 12358: 		if ($listing eq 'unknown_cmd') {
                   12359: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   12360: 				      $udom, $tryserver);
                   12361: 		    @listing_results = split(/:/,$listing);
                   12362: 		} else {
                   12363: 		    @listing_results =
                   12364: 			map { &unescape($_); } split(/:/,$listing);
                   12365: 		}
1.1136    raeburn  12366:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   12367:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   12368:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   12369:                     $listerror{$tryserver} = $listing;
                   12370:                 } else {
1.841     albertel 12371: 		    foreach my $line (@listing_results) {
                   12372: 			my ($entry) = split(/&/,$line,2);
                   12373: 			$allusers{$entry} = 1;
                   12374: 		    }
                   12375: 		}
1.253     stredwic 12376:             }
1.1136    raeburn  12377:             my @alluserslist=();
1.800     albertel 12378:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  12379:                 push(@alluserslist,$user.'&user');
1.253     stredwic 12380:             }
1.1318    droeschl 12381: 
                   12382:             if (!%listerror) {
                   12383:                 # no errors
                   12384:                 return (\@alluserslist);
                   12385:             } elsif (scalar(keys(%servers)) == 1) {
                   12386:                 # one library server, one error 
                   12387:                 my ($key) = keys(%listerror);
                   12388:                 return (\@alluserslist, $listerror{$key});
                   12389:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
                   12390:                 # con_lost indicates that we might miss data from at least one
                   12391:                 # library server
                   12392:                 return (\@alluserslist, 'con_lost');
                   12393:             } else {
                   12394:                 # multiple library servers and no con_lost -> data should be
                   12395:                 # complete. 
                   12396:                 return (\@alluserslist);
                   12397:             }
                   12398: 
1.253     stredwic 12399:         } else {
1.1136    raeburn  12400:             return ([],'missing username');
1.253     stredwic 12401:         }
1.955     raeburn  12402:     } elsif(!defined($getpropath)) {
1.1136    raeburn  12403:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   12404:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   12405:         return (\@all_domains);
1.955     raeburn  12406:     } else {
1.1136    raeburn  12407:         return ([],'missing domain');
1.275     stredwic 12408:     }
                   12409: }
                   12410: 
                   12411: # --------------------------------------------- GetFileTimestamp
                   12412: # This function utilizes dirlist and returns the date stamp for
                   12413: # when it was last modified.  It will also return an error of -1
                   12414: # if an error occurs
                   12415: 
                   12416: sub GetFileTimestamp {
1.955     raeburn  12417:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 12418:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   12419:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  12420:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   12421:                                     undef,$getuserdir);
                   12422:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   12423:         return -1;
                   12424:     }
                   12425:     if (ref($fileref) eq 'ARRAY') {
                   12426:         my @stats = split('&',$fileref->[0]);
1.375     matthew  12427:         # @stats contains first the filename, then the stat output
                   12428:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 12429:     } else {
                   12430:         return -1;
1.253     stredwic 12431:     }
1.26      www      12432: }
                   12433: 
1.712     albertel 12434: sub stat_file {
                   12435:     my ($uri) = @_;
1.787     albertel 12436:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 12437: 
1.955     raeburn  12438:     my ($udom,$uname,$file);
1.712     albertel 12439:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   12440: 	($udom,$uname,$file) =
1.811     albertel 12441: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 12442: 	$file = 'userfiles/'.$file;
                   12443:     }
                   12444:     if ($uri =~ m-^/res/-) {
                   12445: 	($udom,$uname) = 
1.807     albertel 12446: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 12447: 	$file = $uri;
                   12448:     }
                   12449: 
                   12450:     if (!$udom || !$uname || !$file) {
                   12451: 	# unable to handle the uri
                   12452: 	return ();
                   12453:     }
1.956     raeburn  12454:     my $getpropath;
                   12455:     if ($file =~ /^userfiles\//) {
                   12456:         $getpropath = 1;
                   12457:     }
1.1136    raeburn  12458:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   12459:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   12460:         return ();
                   12461:     } else {
                   12462:         if (ref($listref) eq 'ARRAY') {
                   12463:             my @stats = split('&',$listref->[0]);
                   12464: 	    shift(@stats); #filename is first
                   12465: 	    return @stats;
                   12466:         }
1.712     albertel 12467:     }
                   12468:     return ();
                   12469: }
                   12470: 
1.1313    raeburn  12471: # --------------------------------------------------------- recursedirs
                   12472: # Recursive function to traverse either a specific user's Authoring Space
                   12473: # or corresponding Published Resource Space, and populate the hash ref:
                   12474: # $dirhashref with URLs of all directories, and if $filehashref hash
                   12475: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
1.1503    raeburn  12476: # or .rights files in resource space, and .meta, .save, .log, .bak and
                   12477: # .rights files in Authoring Space.
1.1313    raeburn  12478: #
                   12479: # Inputs:
                   12480: #
                   12481: # $is_home - true if current server is home server for user's space
1.1503    raeburn  12482: # $recurse - if true will also traverse subdirectories recursively
                   12483: # $include - reference to hash containing allowed file extensions.  If provided,
                   12484: #             files which do not have a matching extension will be ignored.
                   12485: # $exclude - reference to hash containing excluded file extensions.  If provided,
                   12486: #             files which have a matching extension will be ignored.
                   12487: # $nonemptydir - if true, will only populate $fileshashref hash entry for a particular
                   12488: #             directory with first file found (with acceptable extension).
1.1505    raeburn  12489: # $addtopdir - if true, set $dirhashref->{'/'} = 1 
1.1313    raeburn  12490: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
                   12491: # $relpath - Current path (relative to top level).
                   12492: # $dirhashref - reference to hash to populate with URLs of directories (Required)
                   12493: # $filehashref - reference to hash to populate with URLs of files (Optional)
                   12494: #
                   12495: # Returns: nothing
                   12496: #
                   12497: # Side Effects: populates $dirhashref, and $filehashref (if provided).
                   12498: #
                   12499: # Currently used by interface/londocs.pm to create linked select boxes for
                   12500: # directory and filename to import a Course "Author" resource into a course, and
                   12501: # also to create linked select boxes for Authoring Space and Directory to choose
                   12502: # save location for creation of a new "standard" problem from the Course Editor.
                   12503: #
                   12504: 
                   12505: sub recursedirs {
1.1505    raeburn  12506:     my ($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$relpath,$dirhashref,$filehashref) = @_;
1.1313    raeburn  12507:     return unless (ref($dirhashref) eq 'HASH');
1.1503    raeburn  12508:     my $docroot = $perlvar{'lonDocRoot'};
1.1313    raeburn  12509:     my $currpath = $docroot.$toppath;
1.1503    raeburn  12510:     if ($relpath ne '') {
1.1313    raeburn  12511:         $currpath .= "/$relpath";
                   12512:     }
1.1503    raeburn  12513:     my ($savefile,$checkinc,$checkexc);
1.1313    raeburn  12514:     if (ref($filehashref)) {
                   12515:         $savefile = 1;
                   12516:     }
1.1503    raeburn  12517:     if (ref($include) eq 'HASH') {
                   12518:         $checkinc = 1;
                   12519:     }
                   12520:     if (ref($exclude) eq 'HASH') {
                   12521:         $checkexc = 1;
                   12522:     }
1.1313    raeburn  12523:     if ($is_home) {
1.1505    raeburn  12524:         if ((-e $currpath) && (opendir(my $dirh,$currpath))) {
1.1503    raeburn  12525:             my $filecount = 0;
1.1313    raeburn  12526:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
                   12527:                 next if ($item eq '');
                   12528:                 if (-d "$currpath/$item") {
                   12529:                     my $newpath;
1.1503    raeburn  12530:                     if ($relpath ne '') {
1.1313    raeburn  12531:                         $newpath = "$relpath/$item";
                   12532:                     } else {
                   12533:                         $newpath = $item;
                   12534:                     }
                   12535:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
1.1503    raeburn  12536:                     if ($recurse) {
1.1505    raeburn  12537:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
1.1503    raeburn  12538:                     }
                   12539:                 } elsif (($savefile) || ($relpath eq '')) {
                   12540:                     next if ($nonemptydir && $filecount);
                   12541:                     if ($checkinc || $checkexc) {
                   12542:                         my ($extension) = ($item =~ /\.(\w+)$/);
                   12543:                         if ($checkinc) {
                   12544:                             next unless ($extension && $include->{$extension});
                   12545:                         }
                   12546:                         if ($checkexc) {
                   12547:                             next if ($extension && $exclude->{$extension});
                   12548:                         }
                   12549:                     }
1.1505    raeburn  12550:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
1.1503    raeburn  12551:                         $dirhashref->{'/'} = 1;
                   12552:                     }
                   12553:                     if ($savefile) {
                   12554:                         if ($relpath eq '') {
                   12555:                             $filehashref->{'/'}{$item} = 1;
                   12556:                         } else {
1.1313    raeburn  12557:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   12558:                         }
                   12559:                     }
1.1503    raeburn  12560:                     $filecount ++;
1.1313    raeburn  12561:                 }
                   12562:             }
                   12563:             closedir($dirh);
                   12564:         }
                   12565:     } else {
                   12566:         my ($dirlistref,$listerror) =
                   12567:             &dirlist($toppath.$relpath);
                   12568:         my @dir_lines;
                   12569:         my $dirptr=16384;
                   12570:         if (ref($dirlistref) eq 'ARRAY') {
1.1503    raeburn  12571:             my $filecount = 0;
1.1313    raeburn  12572:             foreach my $dir_line (sort
                   12573:                               {
                   12574:                                   my ($afile)=split('&',$a,2);
                   12575:                                   my ($bfile)=split('&',$b,2);
                   12576:                                   return (lc($afile) cmp lc($bfile));
                   12577:                               } (@{$dirlistref})) {
                   12578:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
                   12579:                     split(/\&/,$dir_line,16);
                   12580:                 $item =~ s/\s+$//;
                   12581:                 next if (($item =~ /^\.\.?$/) || ($obs));
                   12582:                 if ($dirptr&$testdir) {
                   12583:                     my $newpath;
                   12584:                     if ($relpath) {
                   12585:                         $newpath = "$relpath/$item";
                   12586:                     } else {
                   12587:                         $newpath = $item;
                   12588:                     }
                   12589:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
1.1503    raeburn  12590:                     if ($recurse) {
1.1505    raeburn  12591:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
1.1503    raeburn  12592:                     }
                   12593:                 } elsif (($savefile) || ($relpath eq '')) {
                   12594:                     next if ($nonemptydir && $filecount);
                   12595:                     if ($checkinc || $checkexc) {
                   12596:                         my $extension;
                   12597:                         if ($checkinc) {
                   12598:                             next unless ($extension && $include->{$extension});
                   12599:                         }
                   12600:                         if ($checkexc) {
                   12601:                             next if ($extension && $exclude->{$extension});
                   12602:                         }
                   12603:                     }
                   12604:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
                   12605:                         $dirhashref->{'/'} = 1;
                   12606:                     }
                   12607:                     if ($savefile) {
                   12608:                         if ($relpath eq '') {
                   12609:                             $filehashref->{'/'}{$item} = 1;
                   12610:                         } else {
                   12611:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
1.1313    raeburn  12612:                         }
                   12613:                     }
1.1503    raeburn  12614:                     $filecount ++; 
1.1313    raeburn  12615:                 }
                   12616:             }
                   12617:         }
                   12618:     }
1.1505    raeburn  12619:     if ($addtopdir) {
                   12620:         if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
                   12621:             $dirhashref->{'/'} = 1;
                   12622:         }
                   12623:     }
1.1313    raeburn  12624:     return;
                   12625: }
                   12626: 
1.1503    raeburn  12627: sub priv_exclude {
                   12628:     return {
                   12629:              meta => 1,
                   12630:              save => 1,
                   12631:              log => 1,
                   12632:              bak => 1,
                   12633:              rights => 1,
                   12634:              DS_Store => 1,
                   12635:            };
                   12636: }
                   12637: 
1.26      www      12638: # -------------------------------------------------------- Value of a Condition
                   12639: 
1.713     albertel 12640: # gets the value of a specific preevaluated condition
                   12641: #    stored in the string  $env{user.state.<cid>}
                   12642: # or looks up a condition reference in the bighash and if if hasn't
                   12643: # already been evaluated recurses into docondval to get the value of
                   12644: # the condition, then memoizing it to 
                   12645: #   $env{user.state.<cid>.<condition>}
1.40      www      12646: sub directcondval {
                   12647:     my $number=shift;
1.620     albertel 12648:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 12649: 	&Apache::lonuserstate::evalstate();
                   12650:     }
1.713     albertel 12651:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   12652: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   12653:     } elsif ($number =~ /^_/) {
                   12654: 	my $sub_condition;
                   12655: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   12656: 		&GDBM_READER(),0640)) {
                   12657: 	    $sub_condition=$bighash{'conditions'.$number};
                   12658: 	    untie(%bighash);
                   12659: 	}
                   12660: 	my $value = &docondval($sub_condition);
1.949     raeburn  12661: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 12662: 	return $value;
                   12663:     }
1.620     albertel 12664:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   12665:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      12666:     } else {
                   12667:        return 2;
                   12668:     }
                   12669: }
                   12670: 
1.713     albertel 12671: # get the collection of conditions for this resource
1.26      www      12672: sub condval {
                   12673:     my $condidx=shift;
1.54      www      12674:     my $allpathcond='';
1.713     albertel 12675:     foreach my $cond (split(/\|/,$condidx)) {
                   12676: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   12677: 	    $allpathcond.=
                   12678: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   12679: 	}
1.191     harris41 12680:     }
1.54      www      12681:     $allpathcond=~s/\|$//;
1.713     albertel 12682:     return &docondval($allpathcond);
                   12683: }
                   12684: 
                   12685: #evaluates an expression of conditions
                   12686: sub docondval {
                   12687:     my ($allpathcond) = @_;
                   12688:     my $result=0;
                   12689:     if ($env{'request.course.id'}
                   12690: 	&& defined($allpathcond)) {
                   12691: 	my $operand='|';
                   12692: 	my @stack;
                   12693: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   12694: 	    if ($chunk eq '(') {
                   12695: 		push @stack,($operand,$result);
                   12696: 	    } elsif ($chunk eq ')') {
                   12697: 		my $before=pop @stack;
                   12698: 		if (pop @stack eq '&') {
                   12699: 		    $result=$result>$before?$before:$result;
                   12700: 		} else {
                   12701: 		    $result=$result>$before?$result:$before;
                   12702: 		}
                   12703: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   12704: 		$operand=$chunk;
                   12705: 	    } else {
                   12706: 		my $new=directcondval($chunk);
                   12707: 		if ($operand eq '&') {
                   12708: 		    $result=$result>$new?$new:$result;
                   12709: 		} else {
                   12710: 		    $result=$result>$new?$result:$new;
                   12711: 		}
                   12712: 	    }
                   12713: 	}
1.26      www      12714:     }
                   12715:     return $result;
1.421     albertel 12716: }
                   12717: 
                   12718: # ---------------------------------------------------- Devalidate courseresdata
                   12719: 
                   12720: sub devalidatecourseresdata {
                   12721:     my ($coursenum,$coursedomain)=@_;
                   12722:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 12723:     &devalidate_cache_new('courseres',$hashid);
1.28      www      12724: }
                   12725: 
1.763     www      12726: 
1.200     www      12727: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     12728: #
                   12729: #  Parameters:
                   12730: #      $coursenum    - Number of the course.
                   12731: #      $coursedomain - Domain at which the course was created.
                   12732: #  Returns:
                   12733: #     A hash of the course parameters along (I think) with timestamps
                   12734: #     and version info.
1.877     foxr     12735: 
1.624     albertel 12736: sub get_courseresdata {
                   12737:     my ($coursenum,$coursedomain)=@_;
1.200     www      12738:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   12739:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 12740:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 12741:     my %dumpreply;
1.417     albertel 12742:     unless (defined($cached)) {
1.624     albertel 12743: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 12744: 	$result=\%dumpreply;
1.251     albertel 12745: 	my ($tmp) = keys(%dumpreply);
                   12746: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 12747: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 12748: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   12749: 	    return $tmp;
1.416     albertel 12750: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 12751: 	    $result=undef;
1.599     albertel 12752: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 12753: 	}
                   12754:     }
1.624     albertel 12755:     return $result;
                   12756: }
                   12757: 
1.633     albertel 12758: sub devalidateuserresdata {
                   12759:     my ($uname,$udom)=@_;
                   12760:     my $hashid="$udom:$uname";
                   12761:     &devalidate_cache_new('userres',$hashid);
                   12762: }
                   12763: 
1.624     albertel 12764: sub get_userresdata {
                   12765:     my ($uname,$udom)=@_;
                   12766:     #most student don\'t have any data set, check if there is some data
                   12767:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   12768: 
                   12769:     my $hashid="$udom:$uname";
                   12770:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   12771:     if (!defined($cached)) {
                   12772: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   12773: 	$result=\%resourcedata;
                   12774: 	&do_cache_new('userres',$hashid,$result,600);
                   12775:     }
                   12776:     my ($tmp)=keys(%$result);
                   12777:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   12778: 	return $result;
                   12779:     }
                   12780:     #error 2 occurs when the .db doesn't exist
                   12781:     if ($tmp!~/error: 2 /) {
1.1294    raeburn  12782:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
                   12783: 	    &logthis("<font color=\"blue\">WARNING:".
                   12784: 		     " Trying to get resource data for ".
                   12785: 		     $uname." at ".$udom.": ".
                   12786: 		     $tmp."</font>");
                   12787:         }
1.624     albertel 12788:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 12789: 	#&EXT_cache_set($udom,$uname);
                   12790: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 12791: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 12792:     }
                   12793:     return $tmp;
                   12794: }
1.879     foxr     12795: #----------------------------------------------- resdata - return resource data
                   12796: #  Purpose:
                   12797: #    Return resource data for either users or for a course.
                   12798: #  Parameters:
                   12799: #     $name      - Course/user name.
                   12800: #     $domain    - Name of the domain the user/course is registered on.
1.1311    raeburn  12801: #     $type      - Type of thing $name is (must be 'course' or 'user')
1.1301    raeburn  12802: #     $mapp      - decluttered URL of enclosing map  
                   12803: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
                   12804: #     $recurseup - Ref to array of map URLs, starting with map containing
                   12805: #                  $mapp up through hierarchy of nested maps to top level map.  
                   12806: #     $courseid  - CourseID (first part of param identifier).
                   12807: #     $modifier  - Middle part of param identifier.
                   12808: #     $what      - Last part of param identifier.
1.879     foxr     12809: #     @which     - Array of names of resources desired.
                   12810: #  Returns:
                   12811: #     The value of the first reasource in @which that is found in the
                   12812: #     resource hash.
                   12813: #  Exceptional Conditions:
                   12814: #     If the $type passed in is not valid (not the string 'course' or 
                   12815: #     'user', an undefined  reference is returned.
                   12816: #     If none of the resources are found, an undef is returned
1.624     albertel 12817: sub resdata {
1.1301    raeburn  12818:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
                   12819:         $modifier,$what,@which)=@_;
1.624     albertel 12820:     my $result;
                   12821:     if ($type eq 'course') {
                   12822: 	$result=&get_courseresdata($name,$domain);
                   12823:     } elsif ($type eq 'user') {
                   12824: 	$result=&get_userresdata($name,$domain);
                   12825:     }
                   12826:     if (!ref($result)) { return $result; }    
1.251     albertel 12827:     foreach my $item (@which) {
1.1301    raeburn  12828:         if ($item->[1] eq 'course') {
                   12829:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
                   12830:                 unless ($$recursed) {
1.1302    raeburn  12831:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301    raeburn  12832:                     $$recursed = 1;
                   12833:                 }
                   12834:                 foreach my $item (@${recurseup}) {
                   12835:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
                   12836:                     last if (defined($result->{$norecursechk}));
                   12837:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
                   12838:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
                   12839:                 }
                   12840:             }
                   12841:         }
                   12842:         if (defined($result->{$item->[0]})) {
1.927     albertel 12843: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 12844: 	}
1.250     albertel 12845:     }
1.291     albertel 12846:     return undef;
1.200     www      12847: }
                   12848: 
1.1360    raeburn  12849: sub get_domain_lti {
                   12850:     my ($cdom,$context) = @_;
1.1483    raeburn  12851:     my ($name,$cachename,%lti);
1.1360    raeburn  12852:     if ($context eq 'consumer') {
                   12853:         $name = 'ltitools';
                   12854:     } elsif ($context eq 'provider') {
                   12855:         $name = 'lti';
1.1483    raeburn  12856:     } elsif ($context eq 'linkprot') {
                   12857:         $name = 'ltisec';
1.1360    raeburn  12858:     } else {
                   12859:         return %lti;
                   12860:     }
1.1483    raeburn  12861:     if ($context eq 'linkprot') {
                   12862:         $cachename = $context;
                   12863:     } else {
                   12864:         $cachename = $name;
                   12865:     }
                   12866:     my ($result,$cached)=&is_cached_new($cachename,$cdom);
1.1298    raeburn  12867:     if (defined($cached)) {
                   12868:         if (ref($result) eq 'HASH') {
1.1360    raeburn  12869:             %lti = %{$result};
1.1298    raeburn  12870:         }
                   12871:     } else {
1.1360    raeburn  12872:         my %domconfig = &get_dom('configuration',[$name],$cdom);
                   12873:         if (ref($domconfig{$name}) eq 'HASH') {
1.1483    raeburn  12874:             if ($context eq 'linkprot') {
                   12875:                 if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
                   12876:                     %lti = %{$domconfig{$name}{'linkprot'}};
                   12877:                 }
                   12878:             } else {
                   12879:                 %lti = %{$domconfig{$name}};
                   12880:             }
1.1298    raeburn  12881:         }
                   12882:         my $cachetime = 24*60*60;
1.1483    raeburn  12883:         &do_cache_new($cachename,$cdom,\%lti,$cachetime);
1.1298    raeburn  12884:     }
1.1360    raeburn  12885:     return %lti;
1.1298    raeburn  12886: }
                   12887: 
1.1463    raeburn  12888: sub get_course_lti {
1.1510    raeburn  12889:     my ($cnum,$cdom,$context) = @_;
                   12890:     my ($name,$cachename,%lti);
                   12891:     if ($context eq 'consumer') {
                   12892:         $name = 'ltitools';
                   12893:         $cachename = 'courseltitools';
                   12894:     } elsif ($context eq 'provider') {
                   12895:         $name = 'lti';
                   12896:         $cachename = 'courselti';
                   12897:     } else {
                   12898:         return %lti;
                   12899:     }
1.1463    raeburn  12900:     my $hashid=$cdom.'_'.$cnum;
1.1510    raeburn  12901:     my ($result,$cached)=&is_cached_new($cachename,$hashid);
1.1463    raeburn  12902:     if (defined($cached)) {
                   12903:         if (ref($result) eq 'HASH') {
1.1510    raeburn  12904:             %lti = %{$result};
1.1463    raeburn  12905:         }
                   12906:     } else {
1.1510    raeburn  12907:         %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
1.1463    raeburn  12908:         my $cachetime = 24*60*60;
1.1510    raeburn  12909:         &do_cache_new($cachename,$hashid,\%lti,$cachetime);
1.1463    raeburn  12910:     }
1.1510    raeburn  12911:     return %lti;
1.1463    raeburn  12912: }
                   12913: 
1.1479    raeburn  12914: sub courselti_itemid {
                   12915:     my ($cnum,$cdom,$url,$method,$params,$context) = @_;
                   12916:     my ($chome,$itemid);
                   12917:     $chome = &homeserver($cnum,$cdom);
                   12918:     return if ($chome eq 'no_host');
                   12919:     if (ref($params) eq 'HASH') {
                   12920:         my $rep;
                   12921:         if (grep { $_ eq $chome } current_machine_ids()) {
                   12922:             $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
                   12923:         } else {
                   12924:             my $escurl = &escape($url);
                   12925:             my $escmethod = &escape($method);
                   12926:             my $items = &freeze_escape($params);
                   12927:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
                   12928:         }
                   12929:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                   12930:                 ($rep eq 'unknown_cmd')) {
                   12931:             $itemid = $rep;
                   12932:         }
                   12933:     }
                   12934:     return $itemid;
                   12935: }
                   12936: 
                   12937: sub domainlti_itemid {
                   12938:     my ($cdom,$url,$method,$params,$context) = @_;
                   12939:     my ($primary_id,$itemid);
                   12940:     $primary_id = &domain($cdom,'primary');
                   12941:     return if ($primary_id eq '');
                   12942:     if (ref($params) eq 'HASH') {
                   12943:         my $rep;
                   12944:         if (grep { $_ eq $primary_id } current_machine_ids()) {
                   12945:             $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
                   12946:         } else {
                   12947:             my $cnum = '';
                   12948:             my $escurl = &escape($url);
                   12949:             my $escmethod = &escape($method);
                   12950:             my $items = &freeze_escape($params);
                   12951:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
                   12952:         }
                   12953:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                   12954:                 ($rep eq 'unknown_cmd')) {
                   12955:             $itemid = $rep;
                   12956:         }
                   12957:     }
                   12958:     return $itemid;
                   12959: }
                   12960: 
1.1513    raeburn  12961: sub get_ltitools_id {
                   12962:     my ($context,$cdom,$cnum,$title) = @_;
                   12963:     my ($lockhash,$tries,$gotlock,$id,$error);
                   12964: 
                   12965:     # get lock on ltitools db
                   12966:     $lockhash = {
                   12967:                    lock => $env{'user.name'}.
                   12968:                            ':'.$env{'user.domain'},
                   12969:                 };
                   12970:     $tries = 0;
                   12971:     if ($context eq 'domain') {
                   12972:         $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
                   12973:     } else {
                   12974:         $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
                   12975:     }
                   12976:     while (($gotlock ne 'ok') && ($tries<10)) {
                   12977:         $tries ++;
                   12978:         sleep (0.1);
                   12979:         if ($context eq 'domain') {
                   12980:             $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
                   12981:         } else {
                   12982:             $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
                   12983:         }
                   12984:     }
                   12985:     if ($gotlock eq 'ok') {
                   12986:         my %currids;
                   12987:         if ($context eq 'domain') {
                   12988:             %currids = &dump_dom('ltitools',$cdom);
                   12989:         } else {
                   12990:             %currids = &dump('ltitools',$cdom,$cnum);
                   12991:         }
                   12992:         if ($currids{'lock'}) {
                   12993:             delete($currids{'lock'});
                   12994:             if (keys(%currids)) {
                   12995:                 my @curr = sort { $a <=> $b } keys(%currids);
                   12996:                 if ($curr[-1] =~ /^\d+$/) {
                   12997:                     $id = 1 + $curr[-1];
                   12998:                 }
                   12999:             } else {
                   13000:                 $id = 1;
                   13001:             }
                   13002:             if ($id) {
                   13003:                 if ($context eq 'domain') {
                   13004:                     unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
                   13005:                         $error = 'nostore';
                   13006:                     }
                   13007:                 } else {
                   13008:                     unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
                   13009:                         $error = 'nostore';
                   13010:                     }
                   13011:                 }
                   13012:             } else {
                   13013:                 $error = 'nonumber';
                   13014:             }
                   13015:         }
                   13016:         my $dellockoutcome;
                   13017:         if ($context eq 'domain') {
                   13018:             $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
                   13019:         } else {
                   13020:             $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
                   13021:         }
                   13022:     } else {
                   13023:         $error = 'nolock';
                   13024:     }
                   13025:     return ($id,$error);
                   13026: }
                   13027: 
1.1496    raeburn  13028: sub count_supptools {
                   13029:     my ($cnum,$cdom,$ignorecache,$reload)=@_;
                   13030:     my $hashid=$cnum.':'.$cdom;
                   13031:     my ($numexttools,$cached);
                   13032:     unless ($ignorecache) {
                   13033:         ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
                   13034:     }
                   13035:     unless (defined($cached)) {
                   13036:         my $chome=&homeserver($cnum,$cdom);
                   13037:         $numexttools = 0;
                   13038:         unless ($chome eq 'no_host') {
1.1500    raeburn  13039:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
1.1496    raeburn  13040:             if (ref($supplemental) eq 'HASH') {
                   13041:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
                   13042:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
                   13043:                         if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
                   13044:                             $numexttools ++;
                   13045:                         }
                   13046:                     }
                   13047:                 }
                   13048:             }
                   13049:         }
                   13050:         &do_cache_new('supptools',$hashid,$numexttools,600);
                   13051:     }
                   13052:     return $numexttools;
                   13053: }
                   13054: 
                   13055: sub has_unhidden_suppfiles {
1.1498    raeburn  13056:     my ($cnum,$cdom,$ignorecache,$possdel)=@_;
1.1236    raeburn  13057:     my $hashid=$cnum.':'.$cdom;
1.1496    raeburn  13058:     my ($showsupp,$cached);
1.1236    raeburn  13059:     unless ($ignorecache) {
1.1496    raeburn  13060:         ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
1.1236    raeburn  13061:     }
                   13062:     unless (defined($cached)) {
                   13063:         my $chome=&homeserver($cnum,$cdom);
                   13064:         unless ($chome eq 'no_host') {
1.1500    raeburn  13065:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
1.1496    raeburn  13066:             if (ref($supplemental) eq 'HASH') {
                   13067:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
                   13068:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
                   13069:                         next if ($key =~ /\.sequence$/);
                   13070:                         if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
                   13071:                             foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
                   13072:                                 unless ($supplemental->{'hidden'}->{$id}) {
                   13073:                                     $showsupp = 1;
                   13074:                                     last;
                   13075:                                 }
                   13076:                             }
                   13077:                         }
                   13078:                         last if ($showsupp);
                   13079:                     }
                   13080:                 }
                   13081:             }
1.1236    raeburn  13082:         }
1.1496    raeburn  13083:         &do_cache_new('showsupp',$hashid,$showsupp,600);
1.1236    raeburn  13084:     }
1.1496    raeburn  13085:     return $showsupp;
                   13086: }
                   13087: 
1.379     matthew  13088: #
                   13089: # EXT resource caching routines
                   13090: #
                   13091: 
1.1302    raeburn  13092: {
                   13093: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
                   13094: #
                   13095: # The course for which we cache
                   13096: my $cachedmapkey='';
                   13097: # The cached recursive maps for this course
                   13098: my %cachedmaps=();
                   13099: # When this was last done
                   13100: my $cachedmaptime='';
                   13101: 
1.379     matthew  13102: sub clear_EXT_cache_status {
1.383     albertel 13103:     &delenv('cache.EXT.');
1.379     matthew  13104: }
                   13105: 
                   13106: sub EXT_cache_status {
                   13107:     my ($target_domain,$target_user) = @_;
1.383     albertel 13108:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 13109:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  13110:         # We know already the user has no data
                   13111:         return 1;
                   13112:     } else {
                   13113:         return 0;
                   13114:     }
                   13115: }
                   13116: 
                   13117: sub EXT_cache_set {
                   13118:     my ($target_domain,$target_user) = @_;
1.383     albertel 13119:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  13120:     #&appenv({$cachename => time});
1.379     matthew  13121: }
                   13122: 
1.28      www      13123: # --------------------------------------------------------- Value of a Variable
1.58      www      13124: sub EXT {
1.715     albertel 13125: 
1.1461    raeburn  13126:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
1.68      www      13127:     unless ($varname) { return ''; }
1.218     albertel 13128:     #get real user name/domain, courseid and symb
                   13129:     my $courseid;
1.359     albertel 13130:     my $publicuser;
1.427     www      13131:     if ($symbparm) {
                   13132: 	$symbparm=&get_symb_from_alias($symbparm);
                   13133:     }
1.218     albertel 13134:     if (!($uname && $udom)) {
1.790     albertel 13135:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 13136:       if (!$symbparm) {	$symbparm=$cursymb; }
                   13137:     } else {
1.620     albertel 13138: 	$courseid=$env{'request.course.id'};
1.218     albertel 13139:     }
1.48      www      13140:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   13141:     my $rest;
1.320     albertel 13142:     if (defined($therest[0])) {
1.48      www      13143:        $rest=join('.',@therest);
                   13144:     } else {
                   13145:        $rest='';
                   13146:     }
1.320     albertel 13147: 
1.57      www      13148:     my $qualifierrest=$qualifier;
                   13149:     if ($rest) { $qualifierrest.='.'.$rest; }
                   13150:     my $spacequalifierrest=$space;
                   13151:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      13152:     if ($realm eq 'user') {
1.48      www      13153: # --------------------------------------------------------------- user.resource
                   13154: 	if ($space eq 'resource') {
1.651     albertel 13155: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   13156: 		  || defined($Apache::lonhomework::parsing_a_task))
                   13157: 		 &&
1.1469    raeburn  13158: 		 ($symbparm eq &symbread()) ) {
1.744     albertel 13159: 		# if we are in the middle of processing the resource the
                   13160: 		# get the value we are planning on committing
                   13161:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   13162:                     return $Apache::lonhomework::results{$qualifierrest};
                   13163:                 } else {
                   13164:                     return $Apache::lonhomework::history{$qualifierrest};
                   13165:                 }
1.335     albertel 13166: 	    } else {
1.359     albertel 13167: 		my %restored;
1.620     albertel 13168: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 13169: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   13170: 		} else {
                   13171: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   13172: 		}
1.335     albertel 13173: 		return $restored{$qualifierrest};
                   13174: 	    }
1.48      www      13175: # ----------------------------------------------------------------- user.access
                   13176:         } elsif ($space eq 'access') {
1.218     albertel 13177: 	    # FIXME - not supporting calls for a specific user
1.48      www      13178:             return &allowed($qualifier,$rest);
                   13179: # ------------------------------------------ user.preferences, user.environment
                   13180:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 13181: 	    if (($uname eq $env{'user.name'}) &&
                   13182: 		($udom eq $env{'user.domain'})) {
                   13183: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 13184: 	    } else {
1.359     albertel 13185: 		my %returnhash;
                   13186: 		if (!$publicuser) {
                   13187: 		    %returnhash=&userenvironment($udom,$uname,
                   13188: 						 $qualifierrest);
                   13189: 		}
1.218     albertel 13190: 		return $returnhash{$qualifierrest};
                   13191: 	    }
1.48      www      13192: # ----------------------------------------------------------------- user.course
                   13193:         } elsif ($space eq 'course') {
1.218     albertel 13194: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 13195:             return $env{join('.',('request.course',$qualifier))};
1.48      www      13196: # ------------------------------------------------------------------- user.role
                   13197:         } elsif ($space eq 'role') {
1.218     albertel 13198: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 13199:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      13200:             if ($qualifier eq 'value') {
                   13201: 		return $role;
                   13202:             } elsif ($qualifier eq 'extent') {
                   13203:                 return $where;
                   13204:             }
                   13205: # ----------------------------------------------------------------- user.domain
                   13206:         } elsif ($space eq 'domain') {
1.218     albertel 13207:             return $udom;
1.48      www      13208: # ------------------------------------------------------------------- user.name
                   13209:         } elsif ($space eq 'name') {
1.218     albertel 13210:             return $uname;
1.48      www      13211: # ---------------------------------------------------- Any other user namespace
1.29      www      13212:         } else {
1.359     albertel 13213: 	    my %reply;
                   13214: 	    if (!$publicuser) {
                   13215: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   13216: 	    }
                   13217: 	    return $reply{$qualifierrest};
1.48      www      13218:         }
1.236     www      13219:     } elsif ($realm eq 'query') {
                   13220: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 13221:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   13222: 						[$spacequalifierrest]);
1.620     albertel 13223: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      13224:    } elsif ($realm eq 'request') {
1.48      www      13225: # ------------------------------------------------------------- request.browser
                   13226:         if ($space eq 'browser') {
1.1145    bisitz   13227:             return $env{'browser.'.$qualifier};
1.57      www      13228: # ------------------------------------------------------------ request.filename
                   13229:         } else {
1.620     albertel 13230:             return $env{'request.'.$spacequalifierrest};
1.29      www      13231:         }
1.28      www      13232:     } elsif ($realm eq 'course') {
1.48      www      13233: # ---------------------------------------------------------- course.description
1.620     albertel 13234:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      13235:     } elsif ($realm eq 'resource') {
1.165     www      13236: 
1.620     albertel 13237: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 13238: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   13239: 	}
1.693     albertel 13240: 
1.1232    raeburn  13241:         if ($qualifier eq '') {
                   13242: 	    if ($space eq 'title') {
                   13243: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   13244: 	        return &gettitle($symbparm);
                   13245: 	    }
1.693     albertel 13246: 	
1.1232    raeburn  13247: 	    if ($space eq 'map') {
                   13248: 	        my ($map) = &decode_symb($symbparm);
                   13249: 	        return &symbread($map);
                   13250: 	    }
                   13251:             if ($space eq 'maptitle') {
                   13252:                 my ($map) = &decode_symb($symbparm);
                   13253:                 return &gettitle($map);
                   13254:             }
                   13255: 	    if ($space eq 'filename') {
                   13256: 	        if ($symbparm) {
                   13257: 		    return &clutter((&decode_symb($symbparm))[2]);
                   13258: 	        }
                   13259: 	        return &hreflocation('',$env{'request.filename'});
1.905     albertel 13260: 	    }
1.1232    raeburn  13261: 
1.1233    raeburn  13262:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
                   13263:                 if ($space eq 'visibleparts') {
                   13264:                     my $navmap = Apache::lonnavmaps::navmap->new();
                   13265:                     my $item;
                   13266:                     if (ref($navmap)) {
                   13267:                         my $res = $navmap->getBySymb($symbparm);
                   13268:                         my $parts = $res->parts();
                   13269:                         if (ref($parts) eq 'ARRAY') {
                   13270:                             $item = join(',',@{$parts});
                   13271:                         }
                   13272:                         undef($navmap);
1.1232    raeburn  13273:                     }
1.1233    raeburn  13274:                     return $item;
1.1232    raeburn  13275:                 }
                   13276:             }
                   13277:         }
1.693     albertel 13278: 
1.1301    raeburn  13279: 	my ($section, $group, @groups, @recurseup, $recursed);
1.1461    raeburn  13280:         if (ref($recurseupref) eq 'ARRAY') {
                   13281:             @recurseup = @{$recurseupref};
                   13282:             $recursed = 1;
                   13283:         }
1.1301    raeburn  13284: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228    raeburn  13285:         if (($courseid eq '') && ($cid)) {
                   13286:             $courseid = $cid;
                   13287:         }
                   13288: 	if (($symbparm && $courseid) && 
                   13289: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      13290: 
1.218     albertel 13291: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      13292: 
1.60      www      13293: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 13294: 	    my $symbp=$symbparm;
1.1301    raeburn  13295: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 13296: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301    raeburn  13297:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218     albertel 13298: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620     albertel 13299: 	    if (($env{'user.name'} eq $uname) &&
                   13300: 		($env{'user.domain'} eq $udom)) {
                   13301: 		$section=$env{'request.course.sec'};
1.733     raeburn  13302:                 @groups = split(/:/,$env{'request.course.groups'});  
                   13303:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 13304: 	    } else {
1.539     albertel 13305: 		if (! defined($usection)) {
1.551     albertel 13306: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 13307: 		} else {
                   13308: 		    $section = $usection;
                   13309: 		}
1.733     raeburn  13310:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 13311: 	    }
                   13312: 
                   13313: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   13314: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301    raeburn  13315:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218     albertel 13316: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   13317: 
1.593     albertel 13318: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 13319: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.1301    raeburn  13320:             $courseleveli=$courseid.'.'.$recurseparm;
1.593     albertel 13321: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      13322: 
1.60      www      13323: # ----------------------------------------------------------- first, check user
1.624     albertel 13324: 
1.1301    raeburn  13325: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
                   13326:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
1.927     albertel 13327: 				       ([$courselevelr,'resource'],
                   13328: 					[$courselevelm,'map'     ],
1.1301    raeburn  13329:                                         [$courseleveli,'map'     ],
1.927     albertel 13330: 					[$courselevel, 'course'  ]));
1.931     albertel 13331: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      13332: 
1.594     albertel 13333: # ------------------------------------------------ second, check some of course
1.684     raeburn  13334:             my $coursereply;
1.691     raeburn  13335:             if (@groups > 0) {
                   13336:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301    raeburn  13337:                                        $recurseparm,$mapparm,$spacequalifierrest,
                   13338:                                        $mapp,\$recursed,\@recurseup);
                   13339:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
1.684     raeburn  13340:             }
1.96      www      13341: 
1.684     raeburn  13342: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 13343: 				  $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  13344: 				  'course',$mapp,\$recursed,\@recurseup,
                   13345:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
1.927     albertel 13346: 				  ([$seclevelr,   'resource'],
                   13347: 				   [$seclevelm,   'map'     ],
1.1301    raeburn  13348:                                    [$secleveli,   'map'     ],
1.927     albertel 13349: 				   [$seclevel,    'course'  ],
                   13350: 				   [$courselevelr,'resource']));
                   13351: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      13352: 
1.60      www      13353: # ------------------------------------------------------ third, check map parms
1.218     albertel 13354: 	    my %parmhash=();
                   13355: 	    my $thisparm='';
                   13356: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 13357: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 13358: 		    &GDBM_READER(),0640)) {
1.218     albertel 13359: 		$thisparm=$parmhash{$symbparm};
                   13360: 		untie(%parmhash);
                   13361: 	    }
1.927     albertel 13362: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 13363: 	}
1.594     albertel 13364: # ------------------------------------------ fourth, look in resource metadata
1.1301    raeburn  13365:  
                   13366:         my $what = $spacequalifierrest;
                   13367: 	$what=~s/\./\_/;
1.282     albertel 13368: 	my $filename;
                   13369: 	if (!$symbparm) { $symbparm=&symbread(); }
                   13370: 	if ($symbparm) {
1.409     www      13371: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 13372: 	} else {
1.620     albertel 13373: 	    $filename=$env{'request.filename'};
1.282     albertel 13374: 	}
1.1362    raeburn  13375:         my $toolsymb;
                   13376:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
                   13377:             $toolsymb = $symbparm;
                   13378:         }
                   13379: 	my $metadata=&metadata($filename,$what,$toolsymb);
1.927     albertel 13380: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1362    raeburn  13381: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927     albertel 13382: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      13383: 
1.1301    raeburn  13384: # ----------------------------------------------- fifth, look in rest of course
1.593     albertel 13385: 	if ($symbparm && defined($courseid) && 
1.620     albertel 13386: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 13387: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   13388: 				     $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  13389: 				     'course',$mapp,\$recursed,\@recurseup,
                   13390:                                      $courseid,'.',$spacequalifierrest,
1.927     albertel 13391: 				     ([$courselevelm,'map'   ],
1.1301    raeburn  13392:                                       [$courseleveli,'map'   ],
1.927     albertel 13393: 				      [$courselevel, 'course']));
                   13394: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 13395: 	}
1.145     www      13396: # ------------------------------------------------------------------ Cascade up
1.218     albertel 13397: 	unless ($space eq '0') {
1.336     albertel 13398: 	    my @parts=split(/_/,$space);
                   13399: 	    my $id=pop(@parts);
                   13400: 	    my $part=join('_',@parts);
                   13401: 	    if ($part eq '') { $part='0'; }
1.927     albertel 13402: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 13403: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  13404: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 13405: 	}
1.395     albertel 13406: 	if ($recurse) { return undef; }
1.1362    raeburn  13407: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927     albertel 13408: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      13409: # ---------------------------------------------------- Any other user namespace
                   13410:     } elsif ($realm eq 'environment') {
                   13411: # ----------------------------------------------------------------- environment
1.620     albertel 13412: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   13413: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 13414: 	} else {
1.770     albertel 13415: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   13416: 		return '';
                   13417: 	    }
1.219     albertel 13418: 	    my %returnhash=&userenvironment($udom,$uname,
                   13419: 					    $spacequalifierrest);
                   13420: 	    return $returnhash{$spacequalifierrest};
                   13421: 	}
1.28      www      13422:     } elsif ($realm eq 'system') {
1.48      www      13423: # ----------------------------------------------------------------- system.time
                   13424: 	if ($space eq 'time') {
                   13425: 	    return time;
                   13426:         }
1.696     albertel 13427:     } elsif ($realm eq 'server') {
                   13428: # ----------------------------------------------------------------- system.time
                   13429: 	if ($space eq 'name') {
                   13430: 	    return $ENV{'SERVER_NAME'};
                   13431:         }
1.1415    raeburn  13432:     } elsif ($realm eq 'client') {
                   13433:         if ($space eq 'remote_addr') {
1.1441    raeburn  13434:             return &get_requestor_ip();
1.1415    raeburn  13435:         }
1.28      www      13436:     }
1.48      www      13437:     return '';
1.61      www      13438: }
                   13439: 
1.927     albertel 13440: sub get_reply {
                   13441:     my ($reply_value) = @_;
1.940     raeburn  13442:     if (ref($reply_value) eq 'ARRAY') {
                   13443:         if (wantarray) {
                   13444: 	    return @$reply_value;
                   13445:         }
                   13446:         return $reply_value->[0];
                   13447:     } else {
                   13448:         return $reply_value;
1.927     albertel 13449:     }
                   13450: }
                   13451: 
1.691     raeburn  13452: sub check_group_parms {
1.1301    raeburn  13453:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
                   13454:         $recursed,$recurseupref) = @_;
                   13455:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
                   13456:                   [$what,'course']);
                   13457:     my $coursereply;
1.691     raeburn  13458:     foreach my $group (@{$groups}) {
1.1301    raeburn  13459:         my @groupitems = ();
1.691     raeburn  13460:         foreach my $level (@levels) {
1.927     albertel 13461:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   13462:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  13463:         }
1.1301    raeburn  13464:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   13465:                                    $env{'course.'.$courseid.'.domain'},
                   13466:                                    'course',$mapp,$recursed,$recurseupref,
                   13467:                                    $courseid,'.['.$group.'].',$what,
                   13468:                                    @groupitems);
                   13469:         last if (defined($coursereply));
1.691     raeburn  13470:     }
                   13471:     return $coursereply;
                   13472: }
                   13473: 
1.1301    raeburn  13474: sub get_map_hierarchy {
1.1302    raeburn  13475:     my ($mapname,$courseid) = @_;
                   13476:     my @recurseup = ();
1.1301    raeburn  13477:     if ($mapname) {
1.1302    raeburn  13478:         if (($cachedmapkey eq $courseid) &&
                   13479:             (abs($cachedmaptime-time)<5)) {
                   13480:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
                   13481:                 return @{$cachedmaps{$mapname}};
                   13482:             }
                   13483:         }
1.1301    raeburn  13484:         my $navmap = Apache::lonnavmaps::navmap->new();
                   13485:         if (ref($navmap)) {
                   13486:             @recurseup = $navmap->recurseup_maps($mapname);
                   13487:             undef($navmap);
1.1302    raeburn  13488:             $cachedmaps{$mapname} = \@recurseup;
                   13489:             $cachedmaptime=time;
                   13490:             $cachedmapkey=$courseid;
1.1301    raeburn  13491:         }
                   13492:     }
                   13493:     return @recurseup;
                   13494: }
                   13495: 
1.1302    raeburn  13496: }
                   13497: 
1.691     raeburn  13498: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  13499:     my ($courseid,@groups) = @_;
                   13500:     @groups = sort(@groups);
1.691     raeburn  13501:     return @groups;
                   13502: }
                   13503: 
1.395     albertel 13504: sub packages_tab_default {
1.1362    raeburn  13505:     my ($uri,$varname,$toolsymb)=@_;
1.395     albertel 13506:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 13507: 
                   13508:     my (@extension,@specifics,$do_default);
1.1362    raeburn  13509:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395     albertel 13510: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 13511: 	if ($pack_type eq 'default') {
                   13512: 	    $do_default=1;
                   13513: 	} elsif ($pack_type eq 'extension') {
                   13514: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 13515: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 13516: 	    # only look at packages defaults for packages that this id is
1.738     albertel 13517: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   13518: 	}
                   13519:     }
                   13520:     # first look for a package that matches the requested part id
                   13521:     foreach my $package (@specifics) {
                   13522: 	my (undef,$pack_type,$pack_part)=@{$package};
                   13523: 	next if ($pack_part ne $part);
                   13524: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13525: 	    return $packagetab{"$pack_type&$name&default"};
                   13526: 	}
                   13527:     }
                   13528:     # look for any possible matching non extension_ package
                   13529:     foreach my $package (@specifics) {
                   13530: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 13531: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13532: 	    return $packagetab{"$pack_type&$name&default"};
                   13533: 	}
1.585     albertel 13534: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 13535: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   13536: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 13537: 	}
                   13538:     }
1.738     albertel 13539:     # look for any posible extension_ match
                   13540:     foreach my $package (@extension) {
                   13541: 	my ($package,$pack_type)=@{$package};
                   13542: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13543: 	    return $packagetab{"$pack_type&$name&default"};
                   13544: 	}
                   13545: 	if (defined($packagetab{$package."&$name&default"})) {
                   13546: 	    return $packagetab{$package."&$name&default"};
                   13547: 	}
                   13548:     }
                   13549:     # look for a global default setting
                   13550:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   13551: 	return $packagetab{"default&$name&default"};
                   13552:     }
1.395     albertel 13553:     return undef;
                   13554: }
                   13555: 
1.334     albertel 13556: sub add_prefix_and_part {
                   13557:     my ($prefix,$part)=@_;
                   13558:     my $keyroot;
                   13559:     if (defined($prefix) && $prefix !~ /^__/) {
                   13560: 	# prefix that has a part already
                   13561: 	$keyroot=$prefix;
                   13562:     } elsif (defined($prefix)) {
                   13563: 	# prefix that is missing a part
                   13564: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   13565:     } else {
                   13566: 	# no prefix at all
                   13567: 	if (defined($part)) { $keyroot='_'.$part; }
                   13568:     }
                   13569:     return $keyroot;
                   13570: }
                   13571: 
1.71      www      13572: # ---------------------------------------------------------------- Get metadata
                   13573: 
1.599     albertel 13574: my %metaentry;
1.1070    www      13575: my %importedpartids;
1.1369    raeburn  13576: my %importedrespids;
1.71      www      13577: sub metadata {
1.1362    raeburn  13578:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71      www      13579:     $uri=&declutter($uri);
1.288     albertel 13580:     # if it is a non metadata possible uri return quickly
1.529     albertel 13581:     if (($uri eq '') || 
                   13582: 	(($uri =~ m|^/*adm/|) && 
1.1343    raeburn  13583: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108    raeburn  13584:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 13585: 	return undef;
                   13586:     }
1.1261    raeburn  13587:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
1.924     albertel 13588: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 13589: 	return undef;
1.288     albertel 13590:     }
1.73      www      13591:     my $filename=$uri;
                   13592:     $uri=~s/\.meta$//;
1.172     www      13593: #
                   13594: # Is the metadata already cached?
1.177     www      13595: # Look at timestamp of caching
1.172     www      13596: # Everything is cached by the main uri, libraries are never directly cached
                   13597: #
1.428     albertel 13598:     if (!defined($liburi)) {
1.599     albertel 13599: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 13600: 	if (defined($cached)) { return $result->{':'.$what}; }
                   13601:     }
1.1362    raeburn  13602: 
                   13603: #
                   13604: # If the uri is for an external tool the file from
                   13605: # which metadata should be retrieved depends on whether
                   13606: # the tool had been configured to be gradable (set in the Course
                   13607: # Editor or Resource Editor).
                   13608: #
                   13609: # If a valid symb has been included as the third arg in the call
                   13610: # to &metadata() that can be used to retrieve the value of
                   13611: # parameter_0_gradable set for the resource, and included in the
                   13612: # uploaded map containing the tool. The value is retrieved via
                   13613: # &EXT(), if a valid symb is available.  Otherwise the value of
                   13614: # gradable in the exttool_$marker.db file for the tool instance
1.1364    raeburn  13615: # is retrieved via &get().
                   13616: #
                   13617: # When lonuserstate::traceroute() calls lonnet::EXT() for 
                   13618: # hiddenresource and encrypturl (during course initialization)
                   13619: # the map-level parameter for resource.0.gradable included in the 
                   13620: # uploaded map containing the tool will not yet have been stored
                   13621: # in the user_course_parms.db file for the user's session, so in 
                   13622: # this case fall back to retrieving gradable status from the
                   13623: # exttool_$marker.db file.
1.1362    raeburn  13624: #
                   13625: # In order to avoid an infinite loop, &metadata() will return
                   13626: # before a call to &EXT(), if the uri is for an external tool
                   13627: # and the $what for which metadata is being requested is
                   13628: # parameter_0_gradable or 0_gradable.
                   13629: #
                   13630: 
                   13631:     if ($uri =~ /ext\.tool$/) {
                   13632:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
                   13633:             return;
                   13634:         } else {
                   13635:             my ($checked,$use_passback);
                   13636:             if ($toolsymb ne '') {
                   13637:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
1.1364    raeburn  13638:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
1.1362    raeburn  13639:                     $checked = 1;
                   13640:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
                   13641:                         $use_passback = 1;
                   13642:                     }
                   13643:                 }
                   13644:             }
                   13645:             unless ($checked) {
                   13646:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
                   13647:                 $marker=~s/\D//g;
1.1363    raeburn  13648:                 if ($marker) {
1.1362    raeburn  13649:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
                   13650:                     $use_passback = $toolsettings{'gradable'};
                   13651:                 }
                   13652:             }
                   13653:             if ($use_passback) {
                   13654:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
                   13655:             } else {
                   13656:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
                   13657:             }
                   13658:         }
                   13659:     }
                   13660: 
1.428     albertel 13661:     {
1.1069    www      13662: # Imported parts would go here
1.1369    raeburn  13663:         my @origfiletagids=();
1.1069    www      13664:         my $importedparts=0;
1.1369    raeburn  13665: 
                   13666: # Imported responseids would go here
                   13667:         my $importedresponses=0;
1.172     www      13668: #
                   13669: # Is this a recursive call for a library?
                   13670: #
1.599     albertel 13671: #	if (! exists($metacache{$uri})) {
                   13672: #	    $metacache{$uri}={};
                   13673: #	}
1.924     albertel 13674: 	my $cachetime = 60*60;
1.171     www      13675:         if ($liburi) {
                   13676: 	    $liburi=&declutter($liburi);
                   13677:             $filename=$liburi;
1.401     bowersj2 13678:         } else {
1.599     albertel 13679: 	    &devalidate_cache_new('meta',$uri);
                   13680: 	    undef(%metaentry);
1.401     bowersj2 13681: 	}
1.140     www      13682:         my %metathesekeys=();
1.73      www      13683:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 13684: 	my $metastring;
1.1140    www      13685: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 13686: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 13687: 	    $metastring = 
1.929     albertel 13688: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 13689: 					  ('grade_target' => 'meta'));
                   13690: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  13691: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   13692:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 13693: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 13694: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 13695: 	    $metastring=&getfile($file);
1.489     albertel 13696: 	}
1.208     albertel 13697:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      13698:         my $token;
1.140     www      13699:         undef %metathesekeys;
1.71      www      13700:         while ($token=$parser->get_token) {
1.339     albertel 13701: 	    if ($token->[0] eq 'S') {
                   13702: 		if (defined($token->[2]->{'package'})) {
1.172     www      13703: #
                   13704: # This is a package - get package info
                   13705: #
1.339     albertel 13706: 		    my $package=$token->[2]->{'package'};
                   13707: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13708: 		    if (defined($token->[2]->{'id'})) { 
                   13709: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   13710: 		    }
1.599     albertel 13711: 		    if ($metaentry{':packages'}) {
                   13712: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 13713: 		    } else {
1.599     albertel 13714: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 13715: 		    }
1.736     albertel 13716: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 13717: 			my $part=$keyroot;
                   13718: 			$part=~s/^\_//;
1.736     albertel 13719: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   13720: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   13721: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 13722: 			    # ignore package.tab specified default values
                   13723:                             # here &package_tab_default() will fetch those
                   13724: 			    if ($subp eq 'default') { next; }
1.736     albertel 13725: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 13726: 			    my $unikey;
                   13727: 			    if ($pack =~ /_0$/) {
                   13728: 				$unikey='parameter_0_'.$name;
                   13729: 				$part=0;
                   13730: 			    } else {
                   13731: 				$unikey='parameter'.$keyroot.'_'.$name;
                   13732: 			    }
1.339     albertel 13733: 			    if ($subp eq 'display') {
                   13734: 				$value.=' [Part: '.$part.']';
                   13735: 			    }
1.599     albertel 13736: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 13737: 			    $metathesekeys{$unikey}=1;
1.599     albertel 13738: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   13739: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 13740: 			    }
1.599     albertel 13741: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   13742: 				$metaentry{':'.$unikey}=
                   13743: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 13744: 			    }
1.339     albertel 13745: 			}
                   13746: 		    }
                   13747: 		} else {
1.172     www      13748: #
                   13749: # This is not a package - some other kind of start tag
1.339     albertel 13750: #
                   13751: 		    my $entry=$token->[1];
1.1068    www      13752: 		    my $unikey='';
1.175     www      13753: 
1.339     albertel 13754: 		    if ($entry eq 'import') {
1.175     www      13755: #
                   13756: # Importing a library here
1.339     albertel 13757: #
1.1067    www      13758:                         my $location=$parser->get_text('/import');
                   13759:                         my $dir=$filename;
                   13760:                         $dir=~s|[^/]*$||;
                   13761:                         $location=&filelocation($dir,$location);
1.1369    raeburn  13762: 
                   13763:                         my $importid=$token->[2]->{'id'};
1.1068    www      13764:                         my $importmode=$token->[2]->{'importmode'};
1.1369    raeburn  13765: #
                   13766: # Check metadata for imported file to
                   13767: # see if it contained response items
                   13768: #
1.1372    raeburn  13769:                         my ($origfile,@libfilekeys);
1.1370    raeburn  13770:                         my %currmetaentry = %metaentry;
1.1372    raeburn  13771:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
                   13772:                                                            $depthcount+1));
                   13773:                         if (grep(/^responseorder$/,@libfilekeys)) {
                   13774:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
                   13775:                                                              undef,$depthcount+1);
                   13776:                             if ($libresponseorder ne '') {
                   13777:                                 if ($#origfiletagids<0) {
                   13778:                                     undef(%importedrespids);
                   13779:                                     undef(%importedpartids);
                   13780:                                 }
1.1373    raeburn  13781:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
                   13782:                                 if (@respids) {
                   13783:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
                   13784:                                 }
                   13785:                                 if ($importedrespids{$importid} ne '') {
1.1372    raeburn  13786:                                     $importedresponses = 1;
1.1369    raeburn  13787: # We need to get the original file and the imported file to get the response order correct
                   13788: # Load and inspect original file
1.1372    raeburn  13789:                                     if ($#origfiletagids<0) {
                   13790:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   13791:                                         $origfile=&getfile($origfilelocation);
                   13792:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13793:                                     }
1.1369    raeburn  13794:                                 }
                   13795:                             }
                   13796:                         }
1.1370    raeburn  13797: # Do not overwrite contents of %metaentry hash for resource itself with 
                   13798: # hash populated for imported library file
                   13799:                         %metaentry = %currmetaentry;
                   13800:                         undef(%currmetaentry);
1.1374    raeburn  13801:                         if ($importmode eq 'part') {
1.1068    www      13802: # Import as part(s)
1.1069    www      13803:                            $importedparts=1;
                   13804: # We need to get the original file and the imported file to get the part order correct
                   13805: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1369    raeburn  13806: # Load and inspect original file if we didn't do that already
                   13807:                            if ($#origfiletagids<0) {
                   13808:                                undef(%importedrespids);
                   13809:                                undef(%importedpartids);
                   13810:                                if ($origfile eq '') {
                   13811:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   13812:                                    $origfile=&getfile($origfilelocation);
                   13813:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13814:                                }
1.1070    www      13815:                            }
1.1372    raeburn  13816:                            my @impfilepartids;
                   13817: # If <partorder> tag is included in metadata for the imported file
                   13818: # get the parts in the imported file from that.
                   13819:                            if (grep(/^partorder$/,@libfilekeys)) {
                   13820:                                %currmetaentry = %metaentry;
                   13821:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   13822:                                                             $depthcount+1);
                   13823:                                %metaentry = %currmetaentry;
                   13824:                                undef(%currmetaentry);
                   13825:                                if ($libpartorder ne '') {
                   13826:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
                   13827:                                }
                   13828:                            } else {
                   13829: # If no <partorder> tag available, load and inspect imported file
                   13830:                                my $impfile=&getfile($location);
                   13831:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13832:                            }
1.1069    www      13833:                            if ($#impfilepartids>=0) {
                   13834: # This problem had parts
1.1070    www      13835:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      13836:                            } else {
                   13837: # Importing by turning a single problem into a problem part
                   13838: # It gets the import-tags ID as part-ID
                   13839:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      13840:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      13841:                            }
1.1068    www      13842:                         } else {
1.1374    raeburn  13843: # Import as problem or as normal import
                   13844:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13845:                             unless ($importmode eq 'problem') {
1.1068    www      13846: # Normal import
1.1374    raeburn  13847:                                 if (defined($token->[2]->{'id'})) {
                   13848:                                     $unikey.='_'.$token->[2]->{'id'};
                   13849:                                 }
                   13850:                             }
                   13851: # Check metadata for imported file to
                   13852: # see if it contained parts
                   13853:                             if (grep(/^partorder$/,@libfilekeys)) {
                   13854:                                 %currmetaentry = %metaentry;
                   13855:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   13856:                                                              $depthcount+1);
                   13857:                                 %metaentry = %currmetaentry;
                   13858:                                 undef(%currmetaentry);
                   13859:                                 if ($libpartorder ne '') {
                   13860:                                     $importedparts = 1;
                   13861:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
                   13862:                                 }
                   13863:                             }
1.1067    www      13864:                         }
1.339     albertel 13865: 			if ($depthcount<20) {
1.736     albertel 13866: 			    my $metadata = 
1.1362    raeburn  13867: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736     albertel 13868: 					  $depthcount+1);
                   13869: 			    foreach my $meta (split(',',$metadata)) {
                   13870: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   13871: 				$metathesekeys{$meta}=1;
1.339     albertel 13872: 			    }
1.1068    www      13873:                         }
1.1067    www      13874: 		    } else {
                   13875: #
                   13876: # Not importing, some other kind of non-package, non-library start tag
                   13877: # 
                   13878:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13879:                         if (defined($token->[2]->{'id'})) {
                   13880:                             $unikey.='_'.$token->[2]->{'id'};
                   13881:                         }
1.339     albertel 13882: 			if (defined($token->[2]->{'name'})) { 
                   13883: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   13884: 			}
                   13885: 			$metathesekeys{$unikey}=1;
1.736     albertel 13886: 			foreach my $param (@{$token->[3]}) {
                   13887: 			    $metaentry{':'.$unikey.'.'.$param} =
                   13888: 				$token->[2]->{$param};
1.339     albertel 13889: 			}
                   13890: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 13891: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 13892: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   13893: 		 # only ws inside the tag, and not in default, so use default
                   13894: 		 # as value
1.599     albertel 13895: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 13896: 			} elsif ( $internaltext =~ /\S/ ) {
                   13897: 		  # something interesting inside the tag
                   13898: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 13899: 			} else {
1.908     albertel 13900: 		  # no interesting values, don't set a default
1.339     albertel 13901: 			}
1.172     www      13902: # end of not-a-package not-a-library import
1.339     albertel 13903: 		    }
1.172     www      13904: # end of not-a-package start tag
1.339     albertel 13905: 		}
1.172     www      13906: # the next is the end of "start tag"
1.339     albertel 13907: 	    }
                   13908: 	}
1.483     albertel 13909: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 13910: 	$extension = lc($extension);
                   13911: 	if ($extension eq 'htm') { $extension='html'; }
                   13912: 
1.737     albertel 13913: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 13914: 	    #no specific packages #how's our extension
                   13915: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 13916: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 13917: 					 \%metathesekeys);
                   13918: 	}
1.883     albertel 13919: 
                   13920: 	if (!exists($metaentry{':packages'})
                   13921: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 13922: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 13923: 		#no specific packages well let's get default then
                   13924: 		if ($key!~/^default&/) { next; }
1.488     albertel 13925: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 13926: 					     \%metathesekeys);
                   13927: 	    }
                   13928: 	}
1.338     www      13929: # are there custom rights to evaluate
1.599     albertel 13930: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 13931: 
1.338     www      13932:     #
                   13933:     # Importing a rights file here
1.339     albertel 13934:     #
                   13935: 	    unless ($depthcount) {
1.599     albertel 13936: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 13937: 		my $dir=$filename;
                   13938: 		$dir=~s|[^/]*$||;
                   13939: 		$location=&filelocation($dir,$location);
1.736     albertel 13940: 		my $rights_metadata =
1.1362    raeburn  13941: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736     albertel 13942: 			      $depthcount+1);
                   13943: 		foreach my $rights (split(',',$rights_metadata)) {
                   13944: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   13945: 		    $metathesekeys{$rights}=1;
1.339     albertel 13946: 		}
                   13947: 	    }
                   13948: 	}
1.737     albertel 13949: 	# uniqifiy package listing
                   13950: 	my %seen;
                   13951: 	my @uniq_packages =
                   13952: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   13953: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   13954: 
1.1369    raeburn  13955:         if (($importedresponses) || ($importedparts)) {
                   13956:             if ($importedparts) {
1.1070    www      13957: # We had imported parts and need to rebuild partorder
1.1369    raeburn  13958:                 $metaentry{':partorder'}='';
                   13959:                 $metathesekeys{'partorder'}=1;
                   13960:             }
                   13961:             if ($importedresponses) {
                   13962: # We had imported responses and need to rebuil responseorder
                   13963:                 $metaentry{':responseorder'}='';
                   13964:                 $metathesekeys{'responseorder'}=1;
                   13965:             }
                   13966:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
                   13967:                 my $origid = $origfiletagids[$index+1];
                   13968:                 if ($origfiletagids[$index] eq 'part') {
                   13969: # Original part, part of the problem
                   13970:                     if ($importedparts) {
                   13971:                         $metaentry{':partorder'}.=','.$origid;
                   13972:                     }
                   13973:                 } elsif ($origfiletagids[$index] eq 'import') {
                   13974:                     if ($importedparts) {
                   13975: # We have imported parts at this position
1.1373    raeburn  13976:                         if ($importedpartids{$origid} ne '') {
                   13977:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
                   13978:                         }
1.1369    raeburn  13979:                     }
                   13980:                     if ($importedresponses) {
                   13981: # We have imported responses at this position
1.1373    raeburn  13982:                         if ($importedrespids{$origid} ne '') {
                   13983:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
1.1369    raeburn  13984:                         }
                   13985:                     }
                   13986:                 } else {
                   13987: # Original response item, part of the problem
                   13988:                     if ($importedresponses) {
                   13989:                         $metaentry{':responseorder'}.=','.$origid;
                   13990:                     }
                   13991:                 }
                   13992:             }
                   13993:             if ($importedparts) {
                   13994:                 $metaentry{':partorder'}=~s/^\,//;
                   13995:             }
                   13996:             if ($importedresponses) {
                   13997:                 $metaentry{':responseorder'}=~s/^\,//;
                   13998:             }
1.1070    www      13999:         }
1.737     albertel 14000: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 14001: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274    raeburn  14002: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.1371    raeburn  14003:         unless ($liburi) {
                   14004: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
                   14005:         }
1.177     www      14006: # this is the end of "was not already recently cached
1.71      www      14007:     }
1.599     albertel 14008:     return $metaentry{':'.$what};
1.261     albertel 14009: }
                   14010: 
1.488     albertel 14011: sub metadata_create_package_def {
1.483     albertel 14012:     my ($uri,$key,$package,$metathesekeys)=@_;
                   14013:     my ($pack,$name,$subp)=split(/\&/,$key);
                   14014:     if ($subp eq 'default') { next; }
                   14015:     
1.599     albertel 14016:     if (defined($metaentry{':packages'})) {
                   14017: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 14018:     } else {
1.599     albertel 14019: 	$metaentry{':packages'}=$package;
1.483     albertel 14020:     }
                   14021:     my $value=$packagetab{$key};
                   14022:     my $unikey;
                   14023:     $unikey='parameter_0_'.$name;
1.599     albertel 14024:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 14025:     $$metathesekeys{$unikey}=1;
1.599     albertel 14026:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   14027: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 14028:     }
1.599     albertel 14029:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   14030: 	$metaentry{':'.$unikey}=
                   14031: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 14032:     }
                   14033: }
                   14034: 
1.261     albertel 14035: sub metadata_generate_part0 {
                   14036:     my ($metadata,$metacache,$uri) = @_;
                   14037:     my %allnames;
1.737     albertel 14038:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 14039: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 14040: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   14041: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 14042: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 14043: 	    $allnames{$name}=$part;
                   14044: 	  }
                   14045: 	}
                   14046:     }
                   14047:     foreach my $name (keys(%allnames)) {
                   14048:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 14049:       my $key=":parameter_0_$name";
1.261     albertel 14050:       $$metacache{"$key.part"}='0';
                   14051:       $$metacache{"$key.name"}=$name;
1.428     albertel 14052:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 14053: 					   $allnames{$name}.'_'.$name.
                   14054: 					   '.type'};
1.428     albertel 14055:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 14056: 			     '.display'};
1.644     www      14057:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 14058:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 14059:       $$metacache{"$key.display"}=$olddis;
                   14060:     }
1.71      www      14061: }
                   14062: 
1.764     albertel 14063: # ------------------------------------------------------ Devalidate title cache
                   14064: 
                   14065: sub devalidate_title_cache {
                   14066:     my ($url)=@_;
                   14067:     if (!$env{'request.course.id'}) { return; }
                   14068:     my $symb=&symbread($url);
                   14069:     if (!$symb) { return; }
                   14070:     my $key=$env{'request.course.id'}."\0".$symb;
                   14071:     &devalidate_cache_new('title',$key);
                   14072: }
                   14073: 
1.1014    droeschl 14074: # ------------------------------------------------- Get the title of a course
                   14075: 
                   14076: sub current_course_title {
                   14077:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   14078: }
1.301     www      14079: # ------------------------------------------------- Get the title of a resource
                   14080: 
                   14081: sub gettitle {
                   14082:     my $urlsymb=shift;
                   14083:     my $symb=&symbread($urlsymb);
1.534     albertel 14084:     if ($symb) {
1.620     albertel 14085: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 14086: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 14087: 	if (defined($cached)) { 
                   14088: 	    return $result;
                   14089: 	}
1.534     albertel 14090: 	my ($map,$resid,$url)=&decode_symb($symb);
                   14091: 	my $title='';
1.907     albertel 14092: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   14093: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   14094: 	} else {
                   14095: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   14096: 		    &GDBM_READER(),0640)) {
                   14097: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   14098: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   14099: 		untie(%bighash);
                   14100: 	    }
1.534     albertel 14101: 	}
                   14102: 	$title=~s/\&colon\;/\:/gs;
                   14103: 	if ($title) {
1.1159    www      14104: # Remember both $symb and $title for dynamic metadata
                   14105:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      14106:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      14107: # Cache this title and then return it
1.599     albertel 14108: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 14109: 	}
                   14110: 	$urlsymb=$url;
                   14111:     }
                   14112:     my $title=&metadata($urlsymb,'title');
                   14113:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   14114:     return $title;
1.301     www      14115: }
1.613     albertel 14116: 
1.614     albertel 14117: sub get_slot {
                   14118:     my ($which,$cnum,$cdom)=@_;
                   14119:     if (!$cnum || !$cdom) {
1.790     albertel 14120: 	(undef,my $courseid)=&whichuser();
1.620     albertel 14121: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   14122: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 14123:     }
1.703     albertel 14124:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   14125:     my %slotinfo;
                   14126:     if (exists($remembered{$key})) {
                   14127: 	$slotinfo{$which} = $remembered{$key};
                   14128:     } else {
                   14129: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   14130: 	&Apache::lonhomework::showhash(%slotinfo);
                   14131: 	my ($tmp)=keys(%slotinfo);
                   14132: 	if ($tmp=~/^error:/) { return (); }
                   14133: 	$remembered{$key} = $slotinfo{$which};
                   14134:     }
1.616     albertel 14135:     if (ref($slotinfo{$which}) eq 'HASH') {
                   14136: 	return %{$slotinfo{$which}};
                   14137:     }
                   14138:     return $slotinfo{$which};
1.614     albertel 14139: }
1.1150    raeburn  14140: 
                   14141: sub get_reservable_slots {
                   14142:     my ($cnum,$cdom,$uname,$udom) = @_;
                   14143:     my $now = time;
                   14144:     my $reservable_info;
                   14145:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   14146:     if (exists($remembered{$key})) {
                   14147:         $reservable_info = $remembered{$key};
                   14148:     } else {
                   14149:         my %resv;
                   14150:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   14151:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   14152:         $reservable_info = \%resv;
                   14153:         $remembered{$key} = $reservable_info;
                   14154:     }
                   14155:     return $reservable_info;
                   14156: }
                   14157: 
                   14158: sub get_course_slots {
                   14159:     my ($cnum,$cdom) = @_;
                   14160:     my $hashid=$cnum.':'.$cdom;
1.1494    raeburn  14161:     my ($result,$cached) = &is_cached_new('allslots',$hashid);
1.1150    raeburn  14162:     if (defined($cached)) {
                   14163:         if (ref($result) eq 'HASH') {
                   14164:             return %{$result};
                   14165:         }
                   14166:     } else {
1.1494    raeburn  14167:         my %slots=&dump('slots',$cdom,$cnum);
1.1150    raeburn  14168:         my ($tmp) = keys(%slots);
                   14169:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  14170:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  14171:             return %slots;
                   14172:         }
                   14173:     }
                   14174:     return;
                   14175: }
                   14176: 
                   14177: sub devalidate_slots_cache {
                   14178:     my ($cnum,$cdom)=@_;
                   14179:     my $hashid=$cnum.':'.$cdom;
                   14180:     &devalidate_cache_new('allslots',$hashid);
                   14181: }
                   14182: 
1.1181    raeburn  14183: sub get_coursechange {
                   14184:     my ($cdom,$cnum) = @_;
                   14185:     if ($cdom eq '' || $cnum eq '') {
                   14186:         return unless ($env{'request.course.id'});
                   14187:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   14188:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   14189:     }
                   14190:     my $hashid=$cdom.'_'.$cnum;
                   14191:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   14192:     if ((defined($cached)) && ($change ne '')) {
                   14193:         return $change;
                   14194:     } else {
                   14195:         my %crshash;
                   14196:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   14197:         if ($crshash{'internal.contentchange'} eq '') {
                   14198:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   14199:             if ($change eq '') {
                   14200:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   14201:                 $change = $crshash{'internal.created'};
                   14202:             }
                   14203:         } else {
                   14204:             $change = $crshash{'internal.contentchange'};
                   14205:         }
                   14206:         my $cachetime = 600;
                   14207:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   14208:     }
                   14209:     return $change;
                   14210: }
                   14211: 
                   14212: sub devalidate_coursechange_cache {
1.1496    raeburn  14213:     my ($cdom,$cnum)=@_;
                   14214:     my $hashid=$cdom.'_'.$cnum;
1.1181    raeburn  14215:     &devalidate_cache_new('crschange',$hashid);
                   14216: }
                   14217: 
1.1496    raeburn  14218: sub get_suppchange {
                   14219:     my ($cdom,$cnum) = @_;
                   14220:     if ($cdom eq '' || $cnum eq '') {
                   14221:         return unless ($env{'request.course.id'});
                   14222:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   14223:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   14224:     }
                   14225:     my $hashid=$cdom.'_'.$cnum;
                   14226:     my ($change,$cached)=&is_cached_new('suppchange',$hashid);
                   14227:     if ((defined($cached)) && ($change ne '')) {
                   14228:         return $change;
                   14229:     } else {
                   14230:         my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
                   14231:         if ($crshash{'internal.supplementalchange'} eq '') {
                   14232:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   14233:             if ($change eq '') {
                   14234:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   14235:                 $change = $crshash{'internal.created'};
                   14236:             }
                   14237:         } else {
                   14238:             $change = $crshash{'internal.supplementalchange'};
                   14239:         }
                   14240:         my $cachetime = 600;
                   14241:         &do_cache_new('suppchange',$hashid,$change,$cachetime);
                   14242:     }
                   14243:     return $change;
                   14244: }
                   14245: 
                   14246: sub devalidate_suppchange_cache {
                   14247:     my ($cdom,$cnum)=@_;
                   14248:     my $hashid=$cdom.'_'.$cnum;
                   14249:     &devalidate_cache_new('suppchange',$hashid);
                   14250: }
                   14251: 
1.1497    raeburn  14252: sub update_supp_caches {
                   14253:     my ($cdom,$cnum) = @_;
                   14254:     my %servers = &internet_dom_servers($cdom);
                   14255:     my @ids=&current_machine_ids();
                   14256:     foreach my $server (keys(%servers)) {
                   14257:         next if (grep(/^\Q$server\E$/,@ids));
                   14258:         my $hashid=$cnum.':'.$cdom;
                   14259:         my $cachekey = &escape('showsupp').':'.&escape($hashid);
                   14260:         &remote_devalidate_cache($server,[$cachekey]);
                   14261:     }
                   14262:     &has_unhidden_suppfiles($cnum,$cdom,1,1);
                   14263:     &count_supptools($cnum,$cdom,1);
                   14264:     my $now = time;
                   14265:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                   14266:         &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
                   14267:     }
                   14268:     &put('environment',{'internal.supplementalchange' => $now},
                   14269:          $cdom,$cnum);
                   14270:     &Apache::lonnet::appenv(
                   14271:         {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
                   14272:     &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
                   14273: }
                   14274: 
1.31      www      14275: # ------------------------------------------------- Update symbolic store links
                   14276: 
                   14277: sub symblist {
                   14278:     my ($mapname,%newhash)=@_;
1.438     www      14279:     $mapname=&deversion(&declutter($mapname));
1.31      www      14280:     my %hash;
1.620     albertel 14281:     if (($env{'request.course.fn'}) && (%newhash)) {
                   14282:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 14283:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  14284: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 14285: 		next if ($url eq 'last_known'
                   14286: 			 && $env{'form.no_update_last_known'});
                   14287: 		$hash{declutter($url)}=&encode_symb($mapname,
                   14288: 						    $newhash{$url}->[1],
                   14289: 						    $newhash{$url}->[0]);
1.191     harris41 14290:             }
1.31      www      14291:             if (untie(%hash)) {
                   14292: 		return 'ok';
                   14293:             }
                   14294:         }
                   14295:     }
                   14296:     return 'error';
1.212     www      14297: }
                   14298: 
                   14299: # --------------------------------------------------------------- Verify a symb
                   14300: 
                   14301: sub symbverify {
1.1190    raeburn  14302:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      14303:     my $thisfn=$thisurl;
1.439     www      14304:     $thisfn=&declutter($thisfn);
1.215     www      14305: # direct jump to resource in page or to a sequence - will construct own symbs
                   14306:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   14307: # check URL part
1.409     www      14308:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      14309: 
1.431     www      14310:     unless ($url eq $thisfn) { return 0; }
1.213     www      14311: 
1.216     www      14312:     $symb=&symbclean($symb);
1.510     www      14313:     $thisurl=&deversion($thisurl);
1.439     www      14314:     $thisfn=&deversion($thisfn);
1.213     www      14315: 
                   14316:     my %bighash;
                   14317:     my $okay=0;
1.431     www      14318: 
1.620     albertel 14319:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 14320:                             &GDBM_READER(),0640)) {
1.1032    raeburn  14321:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   14322:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  14323:             if ($map =~ m{^uploaded/.+\.page$}) {
                   14324:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   14325:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   14326:             }
                   14327:         }
                   14328:         my $ids;
1.1419    raeburn  14329:         if ($map =~ m{^uploaded/.+\.page$}) {
                   14330:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1204    raeburn  14331:         } else {
                   14332:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  14333:         }
1.1102    raeburn  14334:         unless ($ids) {
                   14335:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   14336:             $ids=$bighash{$idkey};
1.216     www      14337:         }
                   14338:         if ($ids) {
                   14339: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  14340:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  14341:                 $symb =~ s/\?.+$//;
1.1202    raeburn  14342:             }
1.800     albertel 14343: 	    foreach my $id (split(/\,/,$ids)) {
                   14344: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      14345:                if (
                   14346:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  14347:    eq $symb) {
                   14348:                    if (ref($encstate)) {
                   14349:                        $$encstate = $bighash{'encrypted_'.$id};
                   14350:                    }
1.620     albertel 14351: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  14352: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   14353:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  14354: 		       $okay=1;
1.1202    raeburn  14355:                        last;
1.582     albertel 14356: 		   }
                   14357: 	       }
1.216     www      14358: 	   }
                   14359:         }
1.213     www      14360: 	untie(%bighash);
                   14361:     }
                   14362:     return $okay;
1.31      www      14363: }
                   14364: 
1.210     www      14365: # --------------------------------------------------------------- Clean-up symb
                   14366: 
                   14367: sub symbclean {
                   14368:     my $symb=shift;
1.568     albertel 14369:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      14370: # remove version from map
                   14371:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      14372: 
1.210     www      14373: # remove version from URL
                   14374:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      14375: 
1.507     www      14376: # remove wrapper
                   14377: 
1.510     www      14378:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 14379:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      14380:     return $symb;
1.409     www      14381: }
                   14382: 
                   14383: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 14384: 
                   14385: sub encode_symb {
                   14386:     my ($map,$resid,$url)=@_;
                   14387:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   14388: }
1.409     www      14389: 
                   14390: sub decode_symb {
1.568     albertel 14391:     my $symb=shift;
                   14392:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   14393:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      14394:     return (&fixversion($map),$resid,&fixversion($url));
                   14395: }
                   14396: 
                   14397: sub fixversion {
                   14398:     my $fn=shift;
1.609     banghart 14399:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      14400:     my %bighash;
                   14401:     my $uri=&clutter($fn);
1.620     albertel 14402:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      14403: # is this cached?
1.599     albertel 14404:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      14405:     if (defined($cached)) { return $result; }
                   14406: # unfortunately not cached, or expired
1.620     albertel 14407:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      14408: 	    &GDBM_READER(),0640)) {
                   14409:  	if ($bighash{'version_'.$uri}) {
                   14410:  	    my $version=$bighash{'version_'.$uri};
1.444     www      14411:  	    unless (($version eq 'mostrecent') || 
                   14412: 		    ($version==&getversion($uri))) {
1.440     www      14413:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   14414:  	    }
                   14415:  	}
                   14416:  	untie %bighash;
1.413     www      14417:     }
1.599     albertel 14418:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      14419: }
                   14420: 
                   14421: sub deversion {
                   14422:     my $url=shift;
                   14423:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   14424:     return $url;
1.210     www      14425: }
                   14426: 
1.31      www      14427: # ------------------------------------------------------ Return symb list entry
                   14428: 
                   14429: sub symbread {
1.1424    raeburn  14430:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1427    raeburn  14431:         $ignoresymbdb,$noenccheck)=@_;
1.1265    raeburn  14432:     my $cache_str='request.symbread.cached.'.$thisfn;
1.1427    raeburn  14433:     if (defined($env{$cache_str})) {
1.1424    raeburn  14434:         unless (ref($possibles) eq 'HASH') {
                   14435:             if ($ignorecachednull) {
                   14436:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
                   14437:             } else {
                   14438:                 return $env{$cache_str};
                   14439:             }
1.1282    raeburn  14440:         }
                   14441:     }
1.242     www      14442: # no filename provided? try from environment
1.1265    raeburn  14443:     unless ($thisfn) {
1.620     albertel 14444:         if ($env{'request.symb'}) {
1.1427    raeburn  14445:             return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 14446: 	}
1.620     albertel 14447: 	$thisfn=$env{'request.filename'};
1.44      www      14448:     }
1.569     albertel 14449:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      14450: # is that filename actually a symb? Verify, clean, and return
                   14451:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 14452: 	if (&symbverify($thisfn,$1)) {
1.1427    raeburn  14453: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 14454: 	}
1.242     www      14455:     }
1.44      www      14456:     $thisfn=declutter($thisfn);
1.31      www      14457:     my %hash;
1.37      www      14458:     my %bighash;
                   14459:     my $syval='';
1.620     albertel 14460:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.1427    raeburn  14461:         unless ($ignoresymbdb) {
1.1424    raeburn  14462:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                   14463:                           &GDBM_READER(),0640)) {
1.1487    raeburn  14464: 	        $syval=$hash{$thisfn};
1.1424    raeburn  14465:                 untie(%hash);
                   14466:             }
1.1427    raeburn  14467:             if ($syval && $checkforblock) {
                   14468:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1424    raeburn  14469:                 if (@blockers) {
                   14470:                     $syval='';
                   14471:                 }
                   14472:             }
1.37      www      14473:         }
                   14474: # ---------------------------------------------------------- There was an entry
                   14475:         if ($syval) {
1.601     albertel 14476: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 14477: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  14478: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 14479: 		    #return $env{$cache_str}='';
1.601     albertel 14480: 		#}    
                   14481: 		#$syval.=$1;
                   14482: 	    #}
1.37      www      14483:         } else {
                   14484: # ------------------------------------------------------- Was not in symb table
1.620     albertel 14485:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 14486:                             &GDBM_READER(),0640)) {
1.37      www      14487: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      14488:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      14489:               unless ($ids) { 
                   14490:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      14491:               }
                   14492:               unless ($ids) {
                   14493: # alias?
                   14494: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      14495:               }
1.37      www      14496:               if ($ids) {
                   14497: # ------------------------------------------------------------------- Has ID(s)
                   14498:                  my @possibilities=split(/\,/,$ids);
1.39      www      14499:                  if ($#possibilities==0) {
                   14500: # ----------------------------------------------- There is only one possibility
1.37      www      14501: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 14502: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   14503: 						    $resid,$thisfn);
1.1282    raeburn  14504:                      if (ref($possibles) eq 'HASH') {
1.1424    raeburn  14505:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
                   14506:                              $possibles->{$syval} = 1;
                   14507:                          }
1.1282    raeburn  14508:                      }
                   14509:                      if ($checkforblock) {
1.1424    raeburn  14510:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1426    raeburn  14511:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1424    raeburn  14512:                              if (@blockers) {
                   14513:                                  $syval = '';
1.1425    raeburn  14514:                                  untie(%bighash);
                   14515:                                  return $env{$cache_str}='';
1.1424    raeburn  14516:                              }
1.1282    raeburn  14517:                          }
                   14518:                      }
                   14519:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
1.39      www      14520: # ------------------------------------------ There is more than one possibility
                   14521:                      my $realpossible=0;
1.800     albertel 14522:                      foreach my $id (@possibilities) {
                   14523: 			 my $file=$bighash{'src_'.$id};
1.1282    raeburn  14524:                          my $canaccess;
                   14525:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
                   14526:                              $canaccess = 1;
                   14527:                          } else { 
                   14528:                              $canaccess = &allowed('bre',$file);
                   14529:                          }
                   14530:                          if ($canaccess) {
                   14531:          		     my ($mapid,$resid)=split(/\./,$id);
                   14532:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
                   14533:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
                   14534: 						             $resid,$thisfn);
1.1424    raeburn  14535:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1426    raeburn  14536:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1282    raeburn  14537:                                  if ($checkforblock) {
1.1426    raeburn  14538:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1424    raeburn  14539:                                      if (@blockers > 0) {
                   14540:                                          $syval = '';
                   14541:                                      } else {
1.1282    raeburn  14542:                                          $syval = $poss_syval;
                   14543:                                          $realpossible++;
                   14544:                                      }
                   14545:                                  } else {
                   14546:                                      $syval = $poss_syval;
                   14547:                                      $realpossible++;
                   14548:                                  }
1.1424    raeburn  14549:                                  if ($syval) {
                   14550:                                      if (ref($possibles) eq 'HASH') {
                   14551:                                          $possibles->{$syval} = 1;
                   14552:                                      }
                   14553:                                  }
1.1282    raeburn  14554:                              }
1.39      www      14555: 			 }
1.191     harris41 14556:                      }
1.39      www      14557: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      14558:                  } else {
                   14559:                      $syval='';
1.37      www      14560:                  }
                   14561: 	      }
1.1282    raeburn  14562:               untie(%bighash);
1.481     raeburn  14563:            }
1.31      www      14564:         }
1.62      www      14565:         if ($syval) {
1.1427    raeburn  14566: 	    return $env{$cache_str}=$syval;
1.62      www      14567:         }
1.31      www      14568:     }
1.949     raeburn  14569:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 14570:     return $env{$cache_str}='';
1.31      www      14571: }
                   14572: 
                   14573: # ---------------------------------------------------------- Return random seed
                   14574: 
1.32      www      14575: sub numval {
                   14576:     my $txt=shift;
                   14577:     $txt=~tr/A-J/0-9/;
                   14578:     $txt=~tr/a-j/0-9/;
                   14579:     $txt=~tr/K-T/0-9/;
                   14580:     $txt=~tr/k-t/0-9/;
                   14581:     $txt=~tr/U-Z/0-5/;
                   14582:     $txt=~tr/u-z/0-5/;
                   14583:     $txt=~s/\D//g;
1.564     albertel 14584:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      14585:     return int($txt);
1.368     albertel 14586: }
                   14587: 
1.484     albertel 14588: sub numval2 {
                   14589:     my $txt=shift;
                   14590:     $txt=~tr/A-J/0-9/;
                   14591:     $txt=~tr/a-j/0-9/;
                   14592:     $txt=~tr/K-T/0-9/;
                   14593:     $txt=~tr/k-t/0-9/;
                   14594:     $txt=~tr/U-Z/0-5/;
                   14595:     $txt=~tr/u-z/0-5/;
                   14596:     $txt=~s/\D//g;
                   14597:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   14598:     my $total;
                   14599:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 14600:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 14601:     return int($total);
                   14602: }
                   14603: 
1.575     albertel 14604: sub numval3 {
                   14605:     use integer;
                   14606:     my $txt=shift;
                   14607:     $txt=~tr/A-J/0-9/;
                   14608:     $txt=~tr/a-j/0-9/;
                   14609:     $txt=~tr/K-T/0-9/;
                   14610:     $txt=~tr/k-t/0-9/;
                   14611:     $txt=~tr/U-Z/0-5/;
                   14612:     $txt=~tr/u-z/0-5/;
                   14613:     $txt=~s/\D//g;
                   14614:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   14615:     my $total;
                   14616:     foreach my $val (@txts) { $total+=$val; }
                   14617:     if ($_64bit) { $total=(($total<<32)>>32); }
                   14618:     return $total;
                   14619: }
                   14620: 
1.675     albertel 14621: sub digest {
                   14622:     my ($data)=@_;
                   14623:     my $digest=&Digest::MD5::md5($data);
                   14624:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   14625:     my ($e,$f);
                   14626:     {
                   14627:         use integer;
                   14628:         $e=($a+$b);
                   14629:         $f=($c+$d);
                   14630:         if ($_64bit) {
                   14631:             $e=(($e<<32)>>32);
                   14632:             $f=(($f<<32)>>32);
                   14633:         }
                   14634:     }
                   14635:     if (wantarray) {
                   14636: 	return ($e,$f);
                   14637:     } else {
                   14638: 	my $g;
                   14639: 	{
                   14640: 	    use integer;
                   14641: 	    $g=($e+$f);
                   14642: 	    if ($_64bit) {
                   14643: 		$g=(($g<<32)>>32);
                   14644: 	    }
                   14645: 	}
                   14646: 	return $g;
                   14647:     }
                   14648: }
                   14649: 
1.368     albertel 14650: sub latest_rnd_algorithm_id {
1.675     albertel 14651:     return '64bit5';
1.366     albertel 14652: }
1.32      www      14653: 
1.503     albertel 14654: sub get_rand_alg {
                   14655:     my ($courseid)=@_;
1.790     albertel 14656:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 14657:     if ($courseid) {
1.620     albertel 14658: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 14659:     }
                   14660:     return &latest_rnd_algorithm_id();
                   14661: }
                   14662: 
1.562     albertel 14663: sub validCODE {
                   14664:     my ($CODE)=@_;
                   14665:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   14666:     return 0;
                   14667: }
                   14668: 
1.491     albertel 14669: sub getCODE {
1.620     albertel 14670:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 14671:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   14672: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   14673: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 14674: 	return $Apache::lonhomework::history{'resource.CODE'};
                   14675:     }
                   14676:     return undef;
                   14677: }
1.1133    foxr     14678: #
                   14679: #  Determines the random seed for a specific context:
                   14680: #
                   14681: # parameters:
                   14682: #   symb      - in course context the symb for the seed.
                   14683: #   course_id - The course id of the form domain_coursenum.
                   14684: #   domain    - Domain for the user.
                   14685: #   course    - Course for the user.
                   14686: #   cenv      - environment of the course.
                   14687: #
                   14688: # NOTE:
                   14689: #   All parameters are picked out of the environment if missing
                   14690: #   or not defined.
                   14691: #   If a symb cannot be determined the current time is used instead.
                   14692: #
                   14693: #  For a given well defined symb, courside, domain, username,
                   14694: #  and course environment, the seed is reproducible.
                   14695: #
1.31      www      14696: sub rndseed {
1.1133    foxr     14697:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 14698:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 14699:     if (!defined($symb)) {
1.366     albertel 14700: 	unless ($symb=$wsymb) { return time; }
                   14701:     }
1.1146    foxr     14702:     if (!defined $courseid) { 
                   14703: 	$courseid=$wcourseid; 
                   14704:     }
                   14705:     if (!defined $domain) { $domain=$wdomain; }
                   14706:     if (!defined $username) { $username=$wusername }
1.1133    foxr     14707: 
                   14708:     my $which;
                   14709:     if (defined($cenv->{'rndseed'})) {
                   14710: 	$which = $cenv->{'rndseed'};
                   14711:     } else {
                   14712: 	$which =&get_rand_alg($courseid);
                   14713:     }
1.491     albertel 14714:     if (defined(&getCODE())) {
1.1133    foxr     14715: 
1.675     albertel 14716: 	if ($which eq '64bit5') {
                   14717: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   14718: 	} elsif ($which eq '64bit4') {
1.575     albertel 14719: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   14720: 	} else {
                   14721: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   14722: 	}
1.675     albertel 14723:     } elsif ($which eq '64bit5') {
                   14724: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 14725:     } elsif ($which eq '64bit4') {
                   14726: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 14727:     } elsif ($which eq '64bit3') {
                   14728: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 14729:     } elsif ($which eq '64bit2') {
                   14730: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 14731:     } elsif ($which eq '64bit') {
                   14732: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   14733:     }
                   14734:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   14735: }
                   14736: 
                   14737: sub rndseed_32bit {
                   14738:     my ($symb,$courseid,$domain,$username)=@_;
                   14739:     {
                   14740: 	use integer;
                   14741: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   14742: 	my $symbseed=numval($symb) << 22;
                   14743: 	my $namechck=unpack("%32C*",$username) << 17;
                   14744: 	my $nameseed=numval($username) << 12;
                   14745: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   14746: 	my $courseseed=unpack("%32C*",$courseid);
                   14747: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 14748: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14749: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 14750: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 14751: 	return $num;
                   14752:     }
                   14753: }
                   14754: 
                   14755: sub rndseed_64bit {
                   14756:     my ($symb,$courseid,$domain,$username)=@_;
                   14757:     {
                   14758: 	use integer;
                   14759: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   14760: 	my $symbseed=numval($symb) << 10;
                   14761: 	my $namechck=unpack("%32S*",$username);
                   14762: 	
                   14763: 	my $nameseed=numval($username) << 21;
                   14764: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   14765: 	my $courseseed=unpack("%32S*",$courseid);
                   14766: 	
                   14767: 	my $num1=$symbchck+$symbseed+$namechck;
                   14768: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14769: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14770: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 14771: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 14772: 	return "$num1,$num2";
1.155     albertel 14773:     }
1.366     albertel 14774: }
                   14775: 
1.443     albertel 14776: sub rndseed_64bit2 {
                   14777:     my ($symb,$courseid,$domain,$username)=@_;
                   14778:     {
                   14779: 	use integer;
                   14780: 	# strings need to be an even # of cahracters long, it it is odd the
                   14781:         # last characters gets thrown away
                   14782: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14783: 	my $symbseed=numval($symb) << 10;
                   14784: 	my $namechck=unpack("%32S*",$username.' ');
                   14785: 	
                   14786: 	my $nameseed=numval($username) << 21;
1.501     albertel 14787: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14788: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14789: 	
                   14790: 	my $num1=$symbchck+$symbseed+$namechck;
                   14791: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14792: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14793: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 14794: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 14795: 	return "$num1,$num2";
                   14796:     }
                   14797: }
                   14798: 
                   14799: sub rndseed_64bit3 {
                   14800:     my ($symb,$courseid,$domain,$username)=@_;
                   14801:     {
                   14802: 	use integer;
                   14803: 	# strings need to be an even # of cahracters long, it it is odd the
                   14804:         # last characters gets thrown away
                   14805: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14806: 	my $symbseed=numval2($symb) << 10;
                   14807: 	my $namechck=unpack("%32S*",$username.' ');
                   14808: 	
                   14809: 	my $nameseed=numval2($username) << 21;
1.443     albertel 14810: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14811: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14812: 	
                   14813: 	my $num1=$symbchck+$symbseed+$namechck;
                   14814: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14815: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14816: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 14817: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      14818: 	
1.503     albertel 14819: 	return "$num1:$num2";
1.443     albertel 14820:     }
                   14821: }
                   14822: 
1.575     albertel 14823: sub rndseed_64bit4 {
                   14824:     my ($symb,$courseid,$domain,$username)=@_;
                   14825:     {
                   14826: 	use integer;
                   14827: 	# strings need to be an even # of cahracters long, it it is odd the
                   14828:         # last characters gets thrown away
                   14829: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14830: 	my $symbseed=numval3($symb) << 10;
                   14831: 	my $namechck=unpack("%32S*",$username.' ');
                   14832: 	
                   14833: 	my $nameseed=numval3($username) << 21;
                   14834: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14835: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14836: 	
                   14837: 	my $num1=$symbchck+$symbseed+$namechck;
                   14838: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14839: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14840: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 14841: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      14842: 	
1.575     albertel 14843: 	return "$num1:$num2";
                   14844:     }
                   14845: }
                   14846: 
1.675     albertel 14847: sub rndseed_64bit5 {
                   14848:     my ($symb,$courseid,$domain,$username)=@_;
                   14849:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   14850:     return "$num1:$num2";
                   14851: }
                   14852: 
1.366     albertel 14853: sub rndseed_CODE_64bit {
                   14854:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 14855:     {
1.366     albertel 14856: 	use integer;
1.443     albertel 14857: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 14858: 	my $symbseed=numval2($symb);
1.491     albertel 14859: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   14860: 	my $CODEseed=numval(&getCODE());
1.443     albertel 14861: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 14862: 	my $num1=$symbseed+$CODEchck;
                   14863: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 14864: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   14865: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 14866: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   14867: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 14868: 	return "$num1:$num2";
1.366     albertel 14869:     }
                   14870: }
                   14871: 
1.575     albertel 14872: sub rndseed_CODE_64bit4 {
                   14873:     my ($symb,$courseid,$domain,$username)=@_;
                   14874:     {
                   14875: 	use integer;
                   14876: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   14877: 	my $symbseed=numval3($symb);
                   14878: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   14879: 	my $CODEseed=numval3(&getCODE());
                   14880: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14881: 	my $num1=$symbseed+$CODEchck;
                   14882: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 14883: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   14884: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 14885: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   14886: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   14887: 	return "$num1:$num2";
                   14888:     }
                   14889: }
                   14890: 
1.675     albertel 14891: sub rndseed_CODE_64bit5 {
                   14892:     my ($symb,$courseid,$domain,$username)=@_;
                   14893:     my $code = &getCODE();
                   14894:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   14895:     return "$num1:$num2";
                   14896: }
                   14897: 
1.366     albertel 14898: sub setup_random_from_rndseed {
                   14899:     my ($rndseed)=@_;
1.503     albertel 14900:     if ($rndseed =~/([,:])/) {
1.1262    raeburn  14901:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
                   14902:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
                   14903:             &Math::Random::random_set_seed_from_phrase($rndseed);
                   14904:         } else {
                   14905:             &Math::Random::random_set_seed($num1,$num2);
                   14906:         }
1.366     albertel 14907:     } else {
                   14908: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 14909:     }
1.36      albertel 14910: }
                   14911: 
1.474     albertel 14912: sub latest_receipt_algorithm_id {
1.835     albertel 14913:     return 'receipt3';
1.474     albertel 14914: }
                   14915: 
1.480     www      14916: sub recunique {
                   14917:     my $fucourseid=shift;
                   14918:     my $unique;
1.835     albertel 14919:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   14920: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 14921: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      14922:     } else {
                   14923: 	$unique=$perlvar{'lonReceipt'};
                   14924:     }
                   14925:     return unpack("%32C*",$unique);
                   14926: }
                   14927: 
                   14928: sub recprefix {
                   14929:     my $fucourseid=shift;
                   14930:     my $prefix;
1.835     albertel 14931:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   14932: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 14933: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      14934:     } else {
                   14935: 	$prefix=$perlvar{'lonHostID'};
                   14936:     }
                   14937:     return unpack("%32C*",$prefix);
                   14938: }
                   14939: 
1.76      www      14940: sub ireceipt {
1.474     albertel 14941:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 14942: 
                   14943:     my $return =&recprefix($fucourseid).'-';
                   14944: 
                   14945:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   14946: 	$env{'request.state'} eq 'construct') {
                   14947: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   14948: 	return $return;
                   14949:     }
                   14950: 
1.76      www      14951:     my $cuname=unpack("%32C*",$funame);
                   14952:     my $cudom=unpack("%32C*",$fudom);
                   14953:     my $cucourseid=unpack("%32C*",$fucourseid);
                   14954:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      14955:     my $cunique=&recunique($fucourseid);
1.474     albertel 14956:     my $cpart=unpack("%32S*",$part);
1.835     albertel 14957:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   14958: 
1.790     albertel 14959: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 14960: 			       
                   14961: 	$return.= ($cunique%$cuname+
                   14962: 		   $cunique%$cudom+
                   14963: 		   $cusymb%$cuname+
                   14964: 		   $cusymb%$cudom+
                   14965: 		   $cucourseid%$cuname+
                   14966: 		   $cucourseid%$cudom+
                   14967: 		   $cpart%$cuname+
                   14968: 		   $cpart%$cudom);
                   14969:     } else {
                   14970: 	$return.= ($cunique%$cuname+
                   14971: 		   $cunique%$cudom+
                   14972: 		   $cusymb%$cuname+
                   14973: 		   $cusymb%$cudom+
                   14974: 		   $cucourseid%$cuname+
                   14975: 		   $cucourseid%$cudom);
                   14976:     }
                   14977:     return $return;
1.76      www      14978: }
                   14979: 
                   14980: sub receipt {
1.474     albertel 14981:     my ($part)=@_;
1.790     albertel 14982:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 14983:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      14984: }
1.260     ng       14985: 
1.790     albertel 14986: sub whichuser {
                   14987:     my ($passedsymb)=@_;
                   14988:     my ($symb,$courseid,$domain,$name,$publicuser);
                   14989:     if (defined($env{'form.grade_symb'})) {
                   14990: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   14991: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   14992: 	if (!$allowed &&
                   14993: 	    exists($env{'request.course.sec'}) &&
                   14994: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   14995: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   14996: 			      '/'.$env{'request.course.sec'});
                   14997: 	}
                   14998: 	if ($allowed) {
                   14999: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   15000: 	    $courseid=$tmp_courseid;
                   15001: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   15002: 	    ($name)=&get_env_multiple('form.grade_username');
                   15003: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   15004: 	}
                   15005:     }
                   15006:     if (!$passedsymb) {
                   15007: 	$symb=&symbread();
                   15008:     } else {
                   15009: 	$symb=$passedsymb;
                   15010:     }
                   15011:     $courseid=$env{'request.course.id'};
                   15012:     $domain=$env{'user.domain'};
                   15013:     $name=$env{'user.name'};
                   15014:     if ($name eq 'public' && $domain eq 'public') {
                   15015: 	if (!defined($env{'form.username'})) {
                   15016: 	    $env{'form.username'}.=time.rand(10000000);
                   15017: 	}
                   15018: 	$name.=$env{'form.username'};
                   15019:     }
                   15020:     return ($symb,$courseid,$domain,$name,$publicuser);
                   15021: 
                   15022: }
                   15023: 
1.36      albertel 15024: # ------------------------------------------------------------ Serves up a file
1.472     albertel 15025: # returns either the contents of the file or 
                   15026: # -1 if the file doesn't exist
1.481     raeburn  15027: #
                   15028: # if the target is a file that was uploaded via DOCS, 
                   15029: # a check will be made to see if a current copy exists on the local server,
                   15030: # if it does this will be served, otherwise a copy will be retrieved from
                   15031: # the home server for the course and stored in /home/httpd/html/userfiles on
                   15032: # the local server.   
1.472     albertel 15033: 
1.36      albertel 15034: sub getfile {
1.538     albertel 15035:     my ($file) = @_;
1.609     banghart 15036:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 15037:     &repcopy($file);
                   15038:     return &readfile($file);
                   15039: }
                   15040: 
                   15041: sub repcopy_userfile {
                   15042:     my ($file)=@_;
1.1142    raeburn  15043:     my $londocroot = $perlvar{'lonDocRoot'};
                   15044:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  15045:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 15046:     my ($cdom,$cnum,$filename) = 
1.811     albertel 15047: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 15048:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   15049:     if (-e "$file") {
1.828     www      15050: # we already have a local copy, check it out
1.538     albertel 15051: 	my @fileinfo = stat($file);
1.828     www      15052: 	my $rtncode;
                   15053: 	my $info;
1.538     albertel 15054: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 15055: 	if ($lwpresp ne 'ok') {
1.828     www      15056: # there is no such file anymore, even though we had a local copy
1.482     albertel 15057: 	    if ($rtncode eq '404') {
1.538     albertel 15058: 		unlink($file);
1.482     albertel 15059: 	    }
                   15060: 	    return -1;
                   15061: 	}
                   15062: 	if ($info < $fileinfo[9]) {
1.828     www      15063: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  15064: 	    return 'ok';
1.828     www      15065: 	} else {
                   15066: # the file is outdated, get rid of it
                   15067: 	    unlink($file);
1.482     albertel 15068: 	}
1.828     www      15069:     }
                   15070: # one way or the other, at this point, we don't have the file
                   15071: # construct the correct path for the file
                   15072:     my @parts = ($cdom,$cnum); 
                   15073:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   15074: 	push @parts, split(/\//,$1);
                   15075:     }
                   15076:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   15077:     foreach my $part (@parts) {
                   15078: 	$path .= '/'.$part;
                   15079: 	if (!-e $path) {
                   15080: 	    mkdir($path,0770);
1.482     albertel 15081: 	}
                   15082:     }
1.828     www      15083: # now the path exists for sure
                   15084: # get a user agent
                   15085:     my $transferfile=$file.'.in.transfer';
                   15086: # FIXME: this should flock
                   15087:     if (-e $transferfile) { return 'ok'; }
                   15088:     my $request;
                   15089:     $uri=~s/^\///;
1.980     raeburn  15090:     my $homeserver = &homeserver($cnum,$cdom);
1.1398    raeburn  15091:     my $hostname = &hostname($homeserver);
1.980     raeburn  15092:     my $protocol = $protocol{$homeserver};
                   15093:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  15094:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.1345    raeburn  15095:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
1.828     www      15096: # did it work?
                   15097:     if ($response->is_error()) {
                   15098: 	unlink($transferfile);
                   15099: 	&logthis("Userfile repcopy failed for $uri");
                   15100: 	return -1;
                   15101:     }
                   15102: # worked, rename the transfer file
                   15103:     rename($transferfile,$file);
1.607     raeburn  15104:     return 'ok';
1.481     raeburn  15105: }
                   15106: 
1.517     albertel 15107: sub tokenwrapper {
                   15108:     my $uri=shift;
1.980     raeburn  15109:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 15110:     $uri=~s|^/||;
1.620     albertel 15111:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 15112:     my $token=$1;
1.552     albertel 15113:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   15114:     if ($udom && $uname && $file) {
                   15115: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  15116:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  15117:         my $homeserver = &homeserver($uname,$udom);
1.1397    raeburn  15118:         my $hostname = &hostname($homeserver);
1.980     raeburn  15119:         my $protocol = $protocol{$homeserver};
                   15120:         $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  15121:         return $protocol.'://'.$hostname.'/'.$uri.
1.517     albertel 15122:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   15123:                                '&tokenissued='.$perlvar{'lonHostID'};
                   15124:     } else {
                   15125:         return '/adm/notfound.html';
                   15126:     }
                   15127: }
                   15128: 
1.828     www      15129: # call with reqtype HEAD: get last modification time
                   15130: # call with reqtype GET: get the file contents
                   15131: # Do not call this with reqtype GET for large files! It loads everything into memory
                   15132: #
1.481     raeburn  15133: sub getuploaded {
                   15134:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   15135:     $uri=~s/^\///;
1.980     raeburn  15136:     my $homeserver = &homeserver($cnum,$cdom);
1.1397    raeburn  15137:     my $hostname = &hostname($homeserver);
1.980     raeburn  15138:     my $protocol = $protocol{$homeserver};
                   15139:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  15140:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481     raeburn  15141:     my $request=new HTTP::Request($reqtype,$uri);
1.1345    raeburn  15142:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
1.481     raeburn  15143:     $$rtncode = $response->code;
1.482     albertel 15144:     if (! $response->is_success()) {
                   15145: 	return 'failed';
                   15146:     }      
                   15147:     if ($reqtype eq 'HEAD') {
1.486     www      15148: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 15149:     } elsif ($reqtype eq 'GET') {
                   15150: 	$$info = $response->content;
1.472     albertel 15151:     }
1.482     albertel 15152:     return 'ok';
1.36      albertel 15153: }
                   15154: 
1.481     raeburn  15155: sub readfile {
                   15156:     my $file = shift;
                   15157:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   15158:     my $fh;
1.1359    raeburn  15159:     open($fh,"<",$file);
1.481     raeburn  15160:     my $a='';
1.800     albertel 15161:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  15162:     return $a;
                   15163: }
                   15164: 
1.36      albertel 15165: sub filelocation {
1.590     banghart 15166:     my ($dir,$file) = @_;
                   15167:     my $location;
                   15168:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 15169: 
                   15170:     if ($file =~ m-^/adm/-) {
                   15171: 	$file=~s-^/adm/wrapper/-/-;
                   15172: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   15173:     }
1.882     albertel 15174: 
1.1139    www      15175:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  15176:         $location = $file;
1.609     banghart 15177:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 15178:         my ($udom,$uname,$filename)=
1.811     albertel 15179:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 15180:         my $home=&homeserver($uname,$udom);
                   15181:         my $is_me=0;
                   15182:         my @ids=&current_machine_ids();
                   15183:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   15184:         if ($is_me) {
1.1117    foxr     15185:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 15186:         } else {
                   15187:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   15188:   	      $udom.'/'.$uname.'/'.$filename;
                   15189:         }
1.882     albertel 15190:     } elsif ($file =~ m-^/adm/-) {
                   15191: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 15192:     } else {
                   15193:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      15194:         $file=~s:^/(res|priv)/:/:;
                   15195:         my $space=$1;
1.590     banghart 15196:         if ( !( $file =~ m:^/:) ) {
                   15197:             $location = $dir. '/'.$file;
                   15198:         } else {
1.1142    raeburn  15199:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 15200:         }
1.59      albertel 15201:     }
1.590     banghart 15202:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 15203:     while ($location=~m{/\.\./}) {
                   15204: 	if ($location =~ m{/[^/]+/\.\./}) {
                   15205: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   15206: 	} else {
                   15207: 	    $location=~ s{/\.\./}{/}g;
                   15208: 	}
                   15209:     } #remove dir/..
1.590     banghart 15210:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   15211:     return $location;
1.46      www      15212: }
1.36      albertel 15213: 
1.46      www      15214: sub hreflocation {
                   15215:     my ($dir,$file)=@_;
1.980     raeburn  15216:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 15217: 	$file=filelocation($dir,$file);
1.700     albertel 15218:     } elsif ($file=~m-^/adm/-) {
                   15219: 	$file=~s-^/adm/wrapper/-/-;
                   15220: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 15221:     }
                   15222:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   15223: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   15224:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  15225: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   15226: 	        {/uploaded/$1/$2/}x;
1.46      www      15227:     }
1.913     albertel 15228:     if ($file=~ m{^/userfiles/}) {
                   15229: 	$file =~ s{^/userfiles/}{/uploaded/};
                   15230:     }
1.462     albertel 15231:     return $file;
1.465     albertel 15232: }
                   15233: 
1.1139    www      15234: 
                   15235: 
                   15236: 
                   15237: 
1.465     albertel 15238: sub current_machine_domains {
1.853     albertel 15239:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   15240: }
                   15241: 
                   15242: sub machine_domains {
                   15243:     my ($hostname) = @_;
1.465     albertel 15244:     my @domains;
1.838     albertel 15245:     my %hostname = &all_hostnames();
1.465     albertel 15246:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  15247: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 15248: 	if ($hostname eq $name) {
1.844     albertel 15249: 	    push(@domains,&host_domain($id));
1.465     albertel 15250: 	}
                   15251:     }
                   15252:     return @domains;
                   15253: }
                   15254: 
                   15255: sub current_machine_ids {
1.853     albertel 15256:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   15257: }
                   15258: 
                   15259: sub machine_ids {
                   15260:     my ($hostname) = @_;
                   15261:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 15262:     my @ids;
1.888     albertel 15263:     my %name_to_host = &all_names();
1.889     albertel 15264:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   15265: 	return @{ $name_to_host{$hostname} };
                   15266:     }
                   15267:     return;
1.31      www      15268: }
                   15269: 
1.824     raeburn  15270: sub additional_machine_domains {
                   15271:     my @domains;
1.1466    raeburn  15272:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
                   15273:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
                   15274:             while (my $line = <$fh>) {
                   15275:                 chomp($line);           
                   15276:                 $line =~ s/\s//g;
                   15277:                 push(@domains,$line);
                   15278:             }
                   15279:             close($fh);
                   15280:         }
1.824     raeburn  15281:     }
                   15282:     return @domains;
                   15283: }
                   15284: 
                   15285: sub default_login_domain {
                   15286:     my $domain = $perlvar{'lonDefDomain'};
                   15287:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   15288:     foreach my $posdom (&current_machine_domains(),
                   15289:                         &additional_machine_domains()) {
                   15290:         if (lc($posdom) eq lc($testdomain)) {
                   15291:             $domain=$posdom;
                   15292:             last;
                   15293:         }
                   15294:     }
                   15295:     return $domain;
                   15296: }
                   15297: 
1.1414    raeburn  15298: sub shared_institution {
1.1439    raeburn  15299:     my ($dom,$lonhost) = @_;
                   15300:     if ($lonhost eq '') {
                   15301:         $lonhost = $perlvar{'lonHostID'};
                   15302:     }
1.1414    raeburn  15303:     my $same_intdom;
1.1439    raeburn  15304:     my $hostintdom = &internet_dom($lonhost);
1.1414    raeburn  15305:     if ($hostintdom ne '') {
                   15306:         my %iphost = &get_iphost();
                   15307:         my $primary_id = &domain($dom,'primary');
                   15308:         my $primary_ip = &get_host_ip($primary_id);
                   15309:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
                   15310:             foreach my $id (@{$iphost{$primary_ip}}) {
                   15311:                 my $intdom = &internet_dom($id);
                   15312:                 if ($intdom eq $hostintdom) {
                   15313:                     $same_intdom = 1;
                   15314:                     last;
                   15315:                 }
                   15316:             }
                   15317:         }
                   15318:     }
                   15319:     return $same_intdom;
                   15320: }
                   15321: 
1.1398    raeburn  15322: sub uses_sts {
                   15323:     my ($ignore_cache) = @_;
                   15324:     my $lonhost = $perlvar{'lonHostID'};
                   15325:     my $hostname = &hostname($lonhost);
                   15326:     my $sts_on;
                   15327:     if ($protocol{$lonhost} eq 'https') {
                   15328:         my $cachetime = 12*3600;
                   15329:         if (!$ignore_cache) {
                   15330:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
                   15331:             if (defined($cached)) {
                   15332:                 return $sts_on;
                   15333:             }
                   15334:         }
                   15335:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
                   15336:         my $request=new HTTP::Request('HEAD',$url);
                   15337:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
                   15338:         if ($response->is_success) {
                   15339:             my $has_sts = $response->header('Strict-Transport-Security');
                   15340:             if ($has_sts eq '') {
                   15341:                 $sts_on = 0;
                   15342:             } else {
                   15343:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
                   15344:                     my $maxage = $1;
                   15345:                     if ($maxage) {
                   15346:                         $sts_on = 1;
                   15347:                     } else {
                   15348:                         $sts_on = 0;
                   15349:                     }
                   15350:                 } else {
                   15351:                     $sts_on = 0;
                   15352:                 }
                   15353:             }
                   15354:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
                   15355:         }
                   15356:     }
                   15357:     return;
                   15358: }
                   15359: 
1.1449    raeburn  15360: sub waf_allssl {
                   15361:     my ($host_name) = @_;
                   15362:     my $alias = &get_proxy_alias();
                   15363:     if ($host_name eq '') {
                   15364:         $host_name = $ENV{'SERVER_NAME'};
                   15365:     }
                   15366:     if (($host_name ne '') && ($alias eq $host_name)) {
                   15367:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
                   15368:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   15369:         if ($defdomdefaults{'waf_sslopt'}) {
                   15370:             return $defdomdefaults{'waf_sslopt'};
                   15371:         }
                   15372:     }
                   15373:     return;
                   15374: }
                   15375: 
1.1434    raeburn  15376: sub get_requestor_ip {
                   15377:     my ($r,$nolookup,$noproxy) = @_;
                   15378:     my $from_ip;
                   15379:     if (ref($r)) {
1.1447    raeburn  15380:         if ($r->can('useragent_ip')) {
                   15381:             if ($noproxy && $r->can('client_ip')) {
                   15382:                 $from_ip = $r->client_ip();
                   15383:             } else {
                   15384:                 $from_ip = $r->useragent_ip();
                   15385:             }
                   15386:         } elsif ($r->connection->can('remote_ip')) {
                   15387:             $from_ip = $r->connection->remote_ip();
                   15388:         } else {
                   15389:             $from_ip = $r->get_remote_host($nolookup);
                   15390:         }
1.1434    raeburn  15391:     } else {
                   15392:         $from_ip = $ENV{'REMOTE_ADDR'};
                   15393:     }
                   15394:     return $from_ip if ($noproxy); 
                   15395:     # Who controls proxy settings for server
                   15396:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
                   15397:     my $proxyinfo = &get_proxy_settings($dom_in_use);
                   15398:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
1.1437    raeburn  15399:         if ($proxyinfo->{'vpnint'}) {
                   15400:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
1.1434    raeburn  15401:                 return $from_ip;
                   15402:             }
                   15403:         }
                   15404:         if ($proxyinfo->{'trusted'}) {
                   15405:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
                   15406:                 my $ipheader = $proxyinfo->{'ipheader'};
                   15407:                 my ($ip,$xfor);
                   15408:                 if (ref($r)) {
                   15409:                     if ($ipheader) {
                   15410:                         $ip = $r->headers_in->{$ipheader};
                   15411:                     }
                   15412:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
                   15413:                 } else {
                   15414:                     if ($ipheader) {
                   15415:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
                   15416:                     }
                   15417:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
                   15418:                 }
                   15419:                 if (($ip eq '') && ($xfor ne '')) {
                   15420:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
                   15421:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
                   15422:                             $ip = $poss_ip;
1.1435    raeburn  15423:                             last;
1.1434    raeburn  15424:                         }
                   15425:                     }
                   15426:                 }
                   15427:                 if ($ip ne '') {
                   15428:                     return $ip;
                   15429:                 }
                   15430:             }
                   15431:         }
                   15432:     }
                   15433:     return $from_ip;
                   15434: }
                   15435: 
                   15436: sub get_proxy_settings {
                   15437:     my ($dom_in_use) = @_;
1.1494    raeburn  15438:     my %domdefaults = &get_domain_defaults($dom_in_use);
1.1434    raeburn  15439:     my $proxyinfo = {
                   15440:                        ipheader => $domdefaults{'waf_ipheader'},
                   15441:                        trusted  => $domdefaults{'waf_trusted'},
1.1437    raeburn  15442:                        vpnint   => $domdefaults{'waf_vpnint'},
1.1438    raeburn  15443:                        vpnext   => $domdefaults{'waf_vpnext'},
1.1449    raeburn  15444:                        sslopt   => $domdefaults{'waf_sslopt'},
1.1434    raeburn  15445:                     };
                   15446:     return $proxyinfo;
                   15447: }
                   15448: 
                   15449: sub ip_match {
                   15450:     my ($ip,$pattern_str) = @_;
                   15451:     $ip=Net::CIDR::cidrvalidate($ip);
                   15452:     if ($ip) {
                   15453:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
                   15454:     }
                   15455:     return;
                   15456: }
                   15457: 
                   15458: sub get_proxy_alias {
1.1450    raeburn  15459:     my ($lonid) = @_;
                   15460:     if ($lonid eq '') {
                   15461:         $lonid = $perlvar{'lonHostID'};
                   15462:     }
                   15463:     if (!defined(&hostname($lonid))) {
                   15464:         return;
                   15465:     }
                   15466:     if ($lonid ne '') {
                   15467:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
1.1434    raeburn  15468:         if ($cached) {
                   15469:             return $alias;
                   15470:         }
1.1494    raeburn  15471:         my $dom = &host_domain($lonid);
1.1434    raeburn  15472:         if ($dom ne '') {
                   15473:             my $cachetime = 60*60*24;
                   15474:             my %domconfig =
1.1494    raeburn  15475:                 &get_dom('configuration',['wafproxy'],$dom);
1.1437    raeburn  15476:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
                   15477:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
1.1450    raeburn  15478:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
                   15479:                 }
                   15480:             }
                   15481:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
                   15482:         }
                   15483:     }
                   15484:     return;
                   15485: }
                   15486: 
                   15487: sub use_proxy_alias {
                   15488:     my ($r,$lonid) = @_;
                   15489:     my $alias = &get_proxy_alias($lonid);
                   15490:     if ($alias) {
                   15491:         my $dom = &host_domain($lonid);
                   15492:         if ($dom ne '') {
1.1467    raeburn  15493:             my $proxyinfo = &get_proxy_settings($dom);
1.1450    raeburn  15494:             my ($vpnint,$remote_ip);
                   15495:             if (ref($proxyinfo) eq 'HASH') {
                   15496:                 $vpnint = $proxyinfo->{'vpnint'};
                   15497:                 if ($vpnint) {
                   15498:                     $remote_ip = &get_requestor_ip($r,1,1);
1.1434    raeburn  15499:                 }
                   15500:             }
1.1450    raeburn  15501:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
                   15502:                 return $alias;
                   15503:             }
1.1434    raeburn  15504:         }
                   15505:     }
                   15506:     return;
                   15507: }
                   15508: 
1.1474    raeburn  15509: sub alias_sso {
1.1467    raeburn  15510:     my ($lonid) = @_;
                   15511:     if ($lonid eq '') {
                   15512:         $lonid = $perlvar{'lonHostID'};
                   15513:     }
                   15514:     if (!defined(&hostname($lonid))) {
                   15515:         return;
                   15516:     }
                   15517:     if ($lonid ne '') {
                   15518:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
                   15519:         if ($cached) {
                   15520:             return $use_alias;
                   15521:         }
1.1494    raeburn  15522:         my $dom = &host_domain($lonid);
1.1467    raeburn  15523:         if ($dom ne '') {
                   15524:             my $cachetime = 60*60*24;
                   15525:             my %domconfig =
1.1494    raeburn  15526:                 &get_dom('configuration',['wafproxy'],$dom);
1.1467    raeburn  15527:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
                   15528:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
                   15529:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
                   15530:                 }
                   15531:             }
                   15532:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
                   15533:         }
                   15534:     }
                   15535:     return;
                   15536: }
                   15537: 
1.1465    raeburn  15538: sub get_saml_landing {
                   15539:     my ($lonid) = @_;
                   15540:     if ($lonid eq '') {
                   15541:         my $defdom = &default_login_domain();
                   15542:         my @hosts = &current_machine_ids();
                   15543:         if (@hosts > 1) {
                   15544:             foreach my $hostid (@hosts) {
                   15545:                 if (&host_domain($hostid) eq $defdom) {
                   15546:                     $lonid = $hostid;
                   15547:                     last;
                   15548:                 }
                   15549:             }
                   15550:         } else {
                   15551:             $lonid = $perlvar{'lonHostID'};
                   15552:         }
                   15553:         if ($lonid) {
1.1494    raeburn  15554:             unless (&host_domain($lonid) eq $defdom) {
1.1465    raeburn  15555:                 return;
                   15556:             }
                   15557:         } else {
                   15558:             return;
                   15559:         }
                   15560:     } elsif (!defined(&hostname($lonid))) {
                   15561:         return;
                   15562:     }
                   15563:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
                   15564:     if ($cached) {
                   15565:         return $landing;
                   15566:     }
1.1494    raeburn  15567:     my $dom = &host_domain($lonid);
1.1465    raeburn  15568:     if ($dom ne '') {
                   15569:         my $cachetime = 60*60*24;
                   15570:         my %domconfig =
1.1494    raeburn  15571:             &get_dom('configuration',['login'],$dom);
1.1465    raeburn  15572:         if (ref($domconfig{'login'}) eq 'HASH') {
                   15573:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
                   15574:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
                   15575:                     $landing = 1;
                   15576:                 }
                   15577:             }
                   15578:         }
                   15579:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
                   15580:     }
                   15581:     return;
                   15582: }
                   15583: 
1.31      www      15584: # ------------------------------------------------------------- Declutters URLs
                   15585: 
                   15586: sub declutter {
                   15587:     my $thisfn=shift;
1.569     albertel 15588:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261    raeburn  15589:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
                   15590:         $thisfn=~s{^/home/httpd/html}{};
                   15591:     }
1.31      www      15592:     $thisfn=~s/^\///;
1.697     albertel 15593:     $thisfn=~s|^adm/wrapper/||;
                   15594:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      15595:     $thisfn=~s/^res\///;
1.1172    bisitz   15596:     $thisfn=~s/^priv\///;
1.1032    raeburn  15597:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   15598:         $thisfn=~s/\?.+$//;
                   15599:     }
1.268     www      15600:     return $thisfn;
                   15601: }
                   15602: 
                   15603: # ------------------------------------------------------------- Clutter up URLs
                   15604: 
                   15605: sub clutter {
                   15606:     my $thisfn='/'.&declutter(shift);
1.887     albertel 15607:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 15608: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      15609:        $thisfn='/res'.$thisfn; 
                   15610:     }
1.1031    raeburn  15611:     if ($thisfn !~m|^/adm|) {
                   15612: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 15613: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 15614: 	} else {
                   15615: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   15616: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 15617: 	    if ($embstyle eq 'ssi'
                   15618: 		|| ($embstyle eq 'hdn')
                   15619: 		|| ($embstyle eq 'rat')
                   15620: 		|| ($embstyle eq 'prv')
                   15621: 		|| ($embstyle eq 'ign')) {
                   15622: 		#do nothing with these
                   15623: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 15624: 		|| ($embstyle eq 'emb')
                   15625: 		|| ($embstyle eq 'wrp')) {
                   15626: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 15627: 	    } elsif ($embstyle eq 'unk'
                   15628: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 15629: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 15630: 	    } else {
1.718     www      15631: #		&logthis("Got a blank emb style");
1.695     albertel 15632: 	    }
1.694     albertel 15633: 	}
1.1343    raeburn  15634:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298    raeburn  15635:         $thisfn='/adm/wrapper'.$thisfn;
1.694     albertel 15636:     }
1.31      www      15637:     return $thisfn;
1.12      www      15638: }
                   15639: 
1.787     albertel 15640: sub clutter_with_no_wrapper {
                   15641:     my $uri = &clutter(shift);
                   15642:     if ($uri =~ m-^/adm/-) {
                   15643: 	$uri =~ s-^/adm/wrapper/-/-;
                   15644: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   15645:     }
                   15646:     return $uri;
                   15647: }
                   15648: 
1.557     albertel 15649: sub freeze_escape {
                   15650:     my ($value)=@_;
                   15651:     if (ref($value)) {
                   15652: 	$value=&nfreeze($value);
                   15653: 	return '__FROZEN__'.&escape($value);
                   15654:     }
                   15655:     return &escape($value);
                   15656: }
                   15657: 
1.11      www      15658: 
1.557     albertel 15659: sub thaw_unescape {
                   15660:     my ($value)=@_;
                   15661:     if ($value =~ /^__FROZEN__/) {
                   15662: 	substr($value,0,10,undef);
                   15663: 	$value=&unescape($value);
                   15664: 	return &thaw($value);
                   15665:     }
                   15666:     return &unescape($value);
                   15667: }
                   15668: 
1.436     albertel 15669: sub correct_line_ends {
                   15670:     my ($result)=@_;
                   15671:     $$result =~s/\r\n/\n/mg;
                   15672:     $$result =~s/\r/\n/mg;
1.415     albertel 15673: }
1.1       albertel 15674: # ================================================================ Main Program
                   15675: 
1.184     www      15676: sub goodbye {
1.204     albertel 15677:    &logthis("Starting Shut down");
1.443     albertel 15678: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 15679:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 15680: #converted
1.599     albertel 15681: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 15682:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   15683: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   15684: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 15685: #1.1 only
1.870     albertel 15686: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   15687: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   15688: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   15689: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   15690:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 15691:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   15692:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      15693:    &flushcourselogs();
                   15694:    &logthis("Shutting down");
                   15695: }
                   15696: 
1.852     albertel 15697: sub get_dns {
1.1210    raeburn  15698:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 15699:     if (!$ignore_cache) {
                   15700: 	my ($content,$cached)=
1.1494    raeburn  15701: 	    &is_cached_new('dns',$url);
1.869     albertel 15702: 	if ($cached) {
1.1210    raeburn  15703: 	    &$func($content,$hashref);
1.869     albertel 15704: 	    return;
                   15705: 	}
                   15706:     }
                   15707: 
                   15708:     my %alldns;
1.1379    raeburn  15709:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
                   15710:         foreach my $dns (<$config>) {
                   15711: 	    next if ($dns !~ /^\^(\S*)/x);
                   15712:             my $line = $1;
                   15713:             my ($host,$protocol) = split(/:/,$line);
                   15714:             if ($protocol ne 'https') {
                   15715:                 $protocol = 'http';
                   15716:             }
                   15717: 	    $alldns{$host} = $protocol;
1.979     raeburn  15718:         }
1.1379    raeburn  15719:         close($config);
1.869     albertel 15720:     }
                   15721:     while (%alldns) {
1.1263    raeburn  15722: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1456    raeburn  15723:         my ($contents,@content);
                   15724:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
                   15725:             my $command = (split('/',$url))[3];
                   15726:             my ($dir,$file) = &parse_getdns_url($command,$url);
                   15727:             delete($alldns{$dns});
                   15728:             next if (($dir eq '') || ($file eq ''));
                   15729:             if (open(my $config,'<',"$dir/$file")) {
1.1457    raeburn  15730:                 @content = <$config>;
1.1456    raeburn  15731:                 close($config);
                   15732:             }
                   15733:             if ($url eq '/adm/dns/loncapaCRL') {
                   15734:                 $contents = join('',@content);
                   15735:             }
                   15736:         } else {
                   15737: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
                   15738:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
                   15739:             delete($alldns{$dns});
                   15740: 	    next if ($response->is_error());
                   15741:             if ($url eq '/adm/dns/loncapaCRL') {
                   15742:                 $contents = $response->content;
                   15743:             } else {
                   15744:                 @content = split("\n",$response->content);
                   15745:             }
                   15746:         }
1.1379    raeburn  15747:         if ($url eq '/adm/dns/loncapaCRL') {
1.1456    raeburn  15748:             return &$func($contents);
1.1379    raeburn  15749:         } else {
                   15750: 	    unless ($nocache) {
                   15751: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
                   15752: 	    }
                   15753: 	    &$func(\@content,$hashref);
                   15754:             return;
                   15755:         }
                   15756:     }
                   15757:     my $which = (split('/',$url,4))[3];
                   15758:     if ($which eq 'loncapaCRL') {
                   15759:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
                   15760:         if (-e $diskfile) {
                   15761:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
                   15762:         } else {
                   15763:             &logthis("unable to contact DNS, no on disk file $diskfile available");
                   15764:         }
                   15765:     } else {
                   15766:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   15767:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
                   15768:             my @content = <$config>;
                   15769:             close($config);
                   15770:             &$func(\@content,$hashref);
                   15771:         }
1.852     albertel 15772:     }
1.1210    raeburn  15773:     return;
                   15774: }
                   15775: 
                   15776: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  15777: sub parse_dns_checksums_tab {
1.1210    raeburn  15778:     my ($lines,$hashref) = @_;
1.1264    raeburn  15779:     my $lonhost = $perlvar{'lonHostID'};
1.1494    raeburn  15780:     my $machine_dom = &host_domain($lonhost);
1.1210    raeburn  15781:     my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264    raeburn  15782:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
                   15783:     my $webconfdir = '/etc/httpd/conf';
                   15784:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
                   15785:         $webconfdir = '/etc/apache2';
                   15786:     } elsif ($distro =~ /^sles(\d+)$/) {
                   15787:         if ($1 >= 10) {
                   15788:             $webconfdir = '/etc/apache2';
                   15789:         }
                   15790:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
                   15791:         if ($1 >= 10.0) {
                   15792:             $webconfdir = '/etc/apache2';
                   15793:         }
                   15794:     }
1.1210    raeburn  15795:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   15796:     my (%chksum,%revnum);
                   15797:     if (ref($lines) eq 'ARRAY') {
                   15798:         chomp(@{$lines});
1.1238    raeburn  15799:         my $version = shift(@{$lines});
                   15800:         if ($version eq $release) {  
1.1210    raeburn  15801:             foreach my $line (@{$lines}) {
1.1238    raeburn  15802:                 my ($file,$version,$shasum) = split(/,/,$line);
1.1264    raeburn  15803:                 if ($file =~ m{^/etc/httpd/conf}) {
                   15804:                     if ($webconfdir eq '/etc/apache2') {
                   15805:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
                   15806:                     }
                   15807:                 }
1.1238    raeburn  15808:                 $chksum{$file} = $shasum;
                   15809:                 $revnum{$file} = $version;
1.1210    raeburn  15810:             }
                   15811:             if (ref($hashref) eq 'HASH') {
                   15812:                 %{$hashref} = (
                   15813:                                 sums     => \%chksum,
                   15814:                                 versions => \%revnum,
                   15815:                               );
                   15816:             }
                   15817:         }
                   15818:     }
1.869     albertel 15819:     return;
1.852     albertel 15820: }
1.1210    raeburn  15821: 
                   15822: sub fetch_dns_checksums {
1.1238    raeburn  15823:     my %checksums;
1.1494    raeburn  15824:     my $machine_dom = &host_domain($perlvar{'lonHostID'});
1.1260    raeburn  15825:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238    raeburn  15826:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   15827:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211    raeburn  15828:              \%checksums);
1.1210    raeburn  15829:     return \%checksums;
                   15830: }
                   15831: 
1.1379    raeburn  15832: sub fetch_crl_pemfile {
                   15833:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
                   15834: }
                   15835: 
                   15836: sub save_crl_pem {
1.1456    raeburn  15837:     my ($content) = @_;
1.1380    raeburn  15838:     my ($msg,$hadchanges);
1.1456    raeburn  15839:     if ($content ne '') {
1.1379    raeburn  15840:         my $now = time;
                   15841:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
                   15842:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
                   15843:         if (open(my $fh,'>',"$tmpcrl")) {
1.1456    raeburn  15844:             print $fh $content;
1.1379    raeburn  15845:             close($fh);
                   15846:             if (-e $lonca) {
                   15847:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
                   15848:                     my $check = <PIPE>;
                   15849:                     close(PIPE);
                   15850:                     chomp($check);
                   15851:                     if ($check eq 'verify OK') {
                   15852:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
1.1380    raeburn  15853:                         my $backup;
1.1379    raeburn  15854:                         if (-e $dest) {
1.1380    raeburn  15855:                             if (&File::Copy::move($dest,"$dest.bak")) {
                   15856:                                 $backup = 'ok';
                   15857:                             }
1.1379    raeburn  15858:                         }
                   15859:                         if (&File::Copy::move($tmpcrl,$dest)) {
                   15860:                             $msg = 'ok';
1.1380    raeburn  15861:                             if ($backup) {
                   15862:                                 my (%oldnums,%newnums);
                   15863:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
                   15864:                                     while (<PIPE>) {
                   15865:                                         $oldnums{(split(/:/))[1]} = 1;
                   15866:                                     }
                   15867:                                     close(PIPE);
                   15868:                                 }
                   15869:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
                   15870:                                     while(<PIPE>) {
                   15871:                                         $newnums{(split(/:/))[1]} = 1;
                   15872:                                     }
                   15873:                                     close(PIPE);
                   15874:                                 }
                   15875:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
                   15876:                                     unless (exists($oldnums{$key})) {
                   15877:                                         $hadchanges = 1;
                   15878:                                         last;
                   15879:                                     }
                   15880:                                 }
                   15881:                                 unless ($hadchanges) {
                   15882:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
                   15883:                                         unless (exists($newnums{$key})) {
                   15884:                                             $hadchanges = 1;
                   15885:                                             last;
                   15886:                                         }
                   15887:                                     }
                   15888:                                 }
                   15889:                             }
1.1379    raeburn  15890:                         }
                   15891:                     } else {
                   15892:                         unlink($tmpcrl);
                   15893:                     }
                   15894:                 } else {
                   15895:                     unlink($tmpcrl);
                   15896:                 }
                   15897:             } else {
                   15898:                 unlink($tmpcrl);
                   15899:             }
                   15900:         }
                   15901:     }
1.1380    raeburn  15902:     return ($msg,$hadchanges);
1.1379    raeburn  15903: }
                   15904: 
1.1456    raeburn  15905: sub parse_getdns_url {
                   15906:     my ($command,$url) = @_;
                   15907:     my $dir = $perlvar{'lonTabDir'};
                   15908:     my $file;
                   15909:     if ($command eq 'hosts') {
                   15910:         $file = 'dns_hosts.tab';
                   15911:     } elsif ($command eq 'domain') {
                   15912:         $file = 'dns_domain.tab';
                   15913:     } elsif ($command eq 'checksums') {
                   15914:         my $version = (split('/',$url))[4];
                   15915:         $file = "dns_checksums/$version.tab",
                   15916:     } elsif ($command eq 'loncapaCRL') {
                   15917:         $dir = $perlvar{'lonCertificateDirectory'};
                   15918:         $file = $perlvar{'lonnetCertRevocationList'};
                   15919:     }
                   15920:     return ($dir,$file);
                   15921: }
                   15922: 
1.327     albertel 15923: # ------------------------------------------------------------ Read domain file
                   15924: {
1.852     albertel 15925:     my $loaded;
1.846     albertel 15926:     my %domain;
                   15927: 
1.852     albertel 15928:     sub parse_domain_tab {
                   15929: 	my ($lines) = @_;
                   15930: 	foreach my $line (@$lines) {
                   15931: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      15932: 
1.846     albertel 15933: 	    chomp($line);
1.852     albertel 15934: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 15935: 	    my %this_domain;
                   15936: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   15937: 			       'lang_def', 'city', 'longi', 'lati',
                   15938: 			       'primary') {
                   15939: 		$this_domain{$field} = shift(@elements);
                   15940: 	    }
                   15941: 	    $domain{$name} = \%this_domain;
1.852     albertel 15942: 	}
                   15943:     }
1.864     albertel 15944: 
                   15945:     sub reset_domain_info {
                   15946: 	undef($loaded);
                   15947: 	undef(%domain);
                   15948:     }
                   15949: 
1.852     albertel 15950:     sub load_domain_tab {
1.1293    raeburn  15951: 	my ($ignore_cache,$nocache) = @_;
                   15952: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852     albertel 15953: 	my $fh;
1.1359    raeburn  15954: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852     albertel 15955: 	    my @lines = <$fh>;
                   15956: 	    &parse_domain_tab(\@lines);
1.448     albertel 15957: 	}
1.852     albertel 15958: 	close($fh);
                   15959: 	$loaded = 1;
1.327     albertel 15960:     }
1.846     albertel 15961: 
                   15962:     sub domain {
1.852     albertel 15963: 	&load_domain_tab() if (!$loaded);
                   15964: 
1.846     albertel 15965: 	my ($name,$what) = @_;
                   15966: 	return if ( !exists($domain{$name}) );
                   15967: 
                   15968: 	if (!$what) {
                   15969: 	    return $domain{$name}{'description'};
                   15970: 	}
                   15971: 	return $domain{$name}{$what};
                   15972:     }
1.974     raeburn  15973: 
                   15974:     sub domain_info {
                   15975:         &load_domain_tab() if (!$loaded);
                   15976:         return %domain;
                   15977:     }
                   15978: 
1.327     albertel 15979: }
                   15980: 
                   15981: 
1.1       albertel 15982: # ------------------------------------------------------------- Read hosts file
                   15983: {
1.838     albertel 15984:     my %hostname;
1.844     albertel 15985:     my %hostdom;
1.845     albertel 15986:     my %libserv;
1.852     albertel 15987:     my $loaded;
1.888     albertel 15988:     my %name_to_host;
1.1074    raeburn  15989:     my %internetdom;
1.1107    raeburn  15990:     my %LC_dns_serv;
1.852     albertel 15991: 
                   15992:     sub parse_hosts_tab {
                   15993: 	my ($file) = @_;
                   15994: 	foreach my $configline (@$file) {
                   15995: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  15996:             chomp($configline);
                   15997: 	    if ($configline =~ /^\^/) {
                   15998:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   15999:                     $LC_dns_serv{$1} = 1;
                   16000:                 }
                   16001:                 next;
                   16002:             }
1.1074    raeburn  16003: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 16004: 	    $name=~s/\s//g;
                   16005: 	    if ($id && $domain && $role && $name) {
1.1351    raeburn  16006:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
                   16007:                     my $curr = $hostname{$id};
                   16008:                     my $skip;
                   16009:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
                   16010:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
                   16011:                             $skip = 1;
                   16012:                         } else {
                   16013:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
                   16014:                         }
                   16015:                     }
                   16016:                     unless ($skip) {
                   16017:                         push(@{$name_to_host{$name}},$id);
                   16018:                     }
                   16019:                 } else {
                   16020:                     push(@{$name_to_host{$name}},$id);
                   16021:                 }
1.852     albertel 16022: 		$hostname{$id}=$name;
                   16023: 		$hostdom{$id}=$domain;
                   16024: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  16025:                 if (defined($protocol)) {
                   16026:                     if ($protocol eq 'https') {
                   16027:                         $protocol{$id} = $protocol;
                   16028:                     } else {
                   16029:                         $protocol{$id} = 'http'; 
                   16030:                     }
1.968     raeburn  16031:                 } else {
1.969     raeburn  16032:                     $protocol{$id} = 'http';
1.968     raeburn  16033:                 }
1.1074    raeburn  16034:                 if (defined($intdom)) {
                   16035:                     $internetdom{$id} = $intdom;
                   16036:                 }
1.852     albertel 16037: 	    }
                   16038: 	}
                   16039:     }
1.864     albertel 16040:     
                   16041:     sub reset_hosts_info {
1.897     albertel 16042: 	&purge_remembered();
1.864     albertel 16043: 	&reset_domain_info();
                   16044: 	&reset_hosts_ip_info();
1.1339    raeburn  16045:         undef(%internetdom);
1.892     albertel 16046: 	undef(%name_to_host);
1.864     albertel 16047: 	undef(%hostname);
                   16048: 	undef(%hostdom);
                   16049: 	undef(%libserv);
                   16050: 	undef($loaded);
                   16051:     }
1.1       albertel 16052: 
1.852     albertel 16053:     sub load_hosts_tab {
1.1293    raeburn  16054: 	my ($ignore_cache,$nocache) = @_;
                   16055: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1359    raeburn  16056: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852     albertel 16057: 	my @config = <$config>;
                   16058: 	&parse_hosts_tab(\@config);
                   16059: 	close($config);
                   16060: 	$loaded=1;
1.1       albertel 16061:     }
1.852     albertel 16062: 
1.838     albertel 16063:     sub hostname {
1.852     albertel 16064: 	&load_hosts_tab() if (!$loaded);
                   16065: 
1.838     albertel 16066: 	my ($lonid) = @_;
                   16067: 	return $hostname{$lonid};
                   16068:     }
1.845     albertel 16069: 
1.838     albertel 16070:     sub all_hostnames {
1.852     albertel 16071: 	&load_hosts_tab() if (!$loaded);
                   16072: 
1.838     albertel 16073: 	return %hostname;
                   16074:     }
1.845     albertel 16075: 
1.888     albertel 16076:     sub all_names {
1.1293    raeburn  16077:         my ($ignore_cache,$nocache) = @_;
                   16078: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888     albertel 16079: 
                   16080: 	return %name_to_host;
                   16081:     }
                   16082: 
1.974     raeburn  16083:     sub all_host_domain {
                   16084:         &load_hosts_tab() if (!$loaded);
                   16085:         return %hostdom;
                   16086:     }
                   16087: 
1.1339    raeburn  16088:     sub all_host_intdom {
                   16089:         &load_hosts_tab() if (!$loaded);
                   16090:         return %internetdom;
                   16091:     }
                   16092: 
1.845     albertel 16093:     sub is_library {
1.852     albertel 16094: 	&load_hosts_tab() if (!$loaded);
                   16095: 
1.845     albertel 16096: 	return exists($libserv{$_[0]});
                   16097:     }
                   16098: 
                   16099:     sub all_library {
1.852     albertel 16100: 	&load_hosts_tab() if (!$loaded);
                   16101: 
1.845     albertel 16102: 	return %libserv;
                   16103:     }
                   16104: 
1.1062    droeschl 16105:     sub unique_library {
                   16106: 	#2x reverse removes all hostnames that appear more than once
                   16107:         my %unique = reverse &all_library();
                   16108:         return reverse %unique;
                   16109:     }
                   16110: 
1.841     albertel 16111:     sub get_servers {
1.852     albertel 16112: 	&load_hosts_tab() if (!$loaded);
                   16113: 
1.841     albertel 16114: 	my ($domain,$type) = @_;
                   16115: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   16116: 	                                          : %hostname;
                   16117: 	my %result;
1.842     albertel 16118: 	if (ref($domain) eq 'ARRAY') {
                   16119: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 16120: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 16121: 		    $result{$host} = $hostname;
                   16122: 		}
                   16123: 	    }
                   16124: 	} else {
                   16125: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   16126: 		if ($hostdom{$host} eq $domain) {
                   16127: 		    $result{$host} = $hostname;
                   16128: 		}
1.841     albertel 16129: 	    }
                   16130: 	}
                   16131: 	return %result;
                   16132:     }
1.845     albertel 16133: 
1.1062    droeschl 16134:     sub get_unique_servers {
                   16135:         my %unique = reverse &get_servers(@_);
                   16136: 	return reverse %unique;
                   16137:     }
                   16138: 
1.844     albertel 16139:     sub host_domain {
1.852     albertel 16140: 	&load_hosts_tab() if (!$loaded);
                   16141: 
1.844     albertel 16142: 	my ($lonid) = @_;
                   16143: 	return $hostdom{$lonid};
                   16144:     }
                   16145: 
1.841     albertel 16146:     sub all_domains {
1.852     albertel 16147: 	&load_hosts_tab() if (!$loaded);
                   16148: 
1.841     albertel 16149: 	my %seen;
                   16150: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   16151: 	return @uniq;
                   16152:     }
1.1074    raeburn  16153: 
                   16154:     sub internet_dom {
                   16155:         &load_hosts_tab() if (!$loaded);
                   16156: 
                   16157:         my ($lonid) = @_;
                   16158:         return $internetdom{$lonid};
                   16159:     }
1.1107    raeburn  16160: 
                   16161:     sub is_LC_dns {
                   16162:         &load_hosts_tab() if (!$loaded);
                   16163: 
                   16164:         my ($hostname) = @_;
                   16165:         return exists($LC_dns_serv{$hostname});
                   16166:     }
                   16167: 
1.1       albertel 16168: }
                   16169: 
1.847     albertel 16170: { 
                   16171:     my %iphost;
1.856     albertel 16172:     my %name_to_ip;
                   16173:     my %lonid_to_ip;
1.869     albertel 16174: 
1.847     albertel 16175:     sub get_hosts_from_ip {
                   16176: 	my ($ip) = @_;
                   16177: 	my %iphosts = &get_iphost();
                   16178: 	if (ref($iphosts{$ip})) {
                   16179: 	    return @{$iphosts{$ip}};
                   16180: 	}
                   16181: 	return;
1.839     albertel 16182:     }
1.864     albertel 16183:     
                   16184:     sub reset_hosts_ip_info {
                   16185: 	undef(%iphost);
                   16186: 	undef(%name_to_ip);
                   16187: 	undef(%lonid_to_ip);
                   16188:     }
1.856     albertel 16189: 
                   16190:     sub get_host_ip {
                   16191: 	my ($lonid) = @_;
                   16192: 	if (exists($lonid_to_ip{$lonid})) {
                   16193: 	    return $lonid_to_ip{$lonid};
                   16194: 	}
                   16195: 	my $name=&hostname($lonid);
                   16196:    	my $ip = gethostbyname($name);
                   16197: 	return if (!$ip || length($ip) ne 4);
                   16198: 	$ip=inet_ntoa($ip);
                   16199: 	$name_to_ip{$name}   = $ip;
                   16200: 	$lonid_to_ip{$lonid} = $ip;
                   16201: 	return $ip;
                   16202:     }
1.847     albertel 16203:     
                   16204:     sub get_iphost {
1.1293    raeburn  16205: 	my ($ignore_cache,$nocache) = @_;
1.894     albertel 16206: 
1.869     albertel 16207: 	if (!$ignore_cache) {
                   16208: 	    if (%iphost) {
                   16209: 		return %iphost;
                   16210: 	    }
                   16211: 	    my ($ip_info,$cached)=
1.1494    raeburn  16212: 		&is_cached_new('iphost','iphost');
1.869     albertel 16213: 	    if ($cached) {
                   16214: 		%iphost      = %{$ip_info->[0]};
                   16215: 		%name_to_ip  = %{$ip_info->[1]};
                   16216: 		%lonid_to_ip = %{$ip_info->[2]};
                   16217: 		return %iphost;
                   16218: 	    }
                   16219: 	}
1.894     albertel 16220: 
                   16221: 	# get yesterday's info for fallback
                   16222: 	my %old_name_to_ip;
                   16223: 	my ($ip_info,$cached)=
1.1495    raeburn  16224: 	    &is_cached_new('iphost','iphost');
1.894     albertel 16225: 	if ($cached) {
                   16226: 	    %old_name_to_ip = %{$ip_info->[1]};
                   16227: 	}
                   16228: 
1.1293    raeburn  16229: 	my %name_to_host = &all_names($ignore_cache,$nocache);
1.888     albertel 16230: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 16231: 	    my $ip;
                   16232: 	    if (!exists($name_to_ip{$name})) {
                   16233: 		$ip = gethostbyname($name);
                   16234: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 16235: 		    if (defined($old_name_to_ip{$name})) {
                   16236: 			$ip = $old_name_to_ip{$name};
                   16237: 			&logthis("Can't find $name defaulting to old $ip");
                   16238: 		    } else {
                   16239: 			&logthis("Name $name no IP found");
                   16240: 			next;
                   16241: 		    }
                   16242: 		} else {
                   16243: 		    $ip=inet_ntoa($ip);
1.847     albertel 16244: 		}
                   16245: 		$name_to_ip{$name} = $ip;
                   16246: 	    } else {
                   16247: 		$ip = $name_to_ip{$name};
1.653     albertel 16248: 	    }
1.888     albertel 16249: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   16250: 		$lonid_to_ip{$id} = $ip;
                   16251: 	    }
                   16252: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 16253: 	}
1.1293    raeburn  16254:         unless ($nocache) {
                   16255: 	    &do_cache_new('iphost','iphost',
                   16256: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   16257: 		          48*60*60);
                   16258:         }
1.869     albertel 16259: 
1.847     albertel 16260: 	return %iphost;
1.598     albertel 16261:     }
                   16262: 
1.992     raeburn  16263:     #
                   16264:     #  Given a DNS returns the loncapa host name for that DNS 
                   16265:     # 
                   16266:     sub host_from_dns {
                   16267:         my ($dns) = @_;
                   16268:         my @hosts;
                   16269:         my $ip;
                   16270: 
1.993     raeburn  16271:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  16272:             $ip = $name_to_ip{$dns};
                   16273:         }
                   16274:         if (!$ip) {
                   16275:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   16276:             if (length($ip) == 4) { 
                   16277: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   16278:             }
                   16279:         }
                   16280:         if ($ip) {
                   16281: 	    @hosts = get_hosts_from_ip($ip);
                   16282: 	    return $hosts[0];
                   16283:         }
                   16284:         return undef;
1.986     foxr     16285:     }
1.992     raeburn  16286: 
1.1074    raeburn  16287:     sub get_internet_names {
                   16288:         my ($lonid) = @_;
                   16289:         return if ($lonid eq '');
                   16290:         my ($idnref,$cached)=
1.1494    raeburn  16291:             &is_cached_new('internetnames',$lonid);
1.1074    raeburn  16292:         if ($cached) {
                   16293:             return $idnref;
                   16294:         }
                   16295:         my $ip = &get_host_ip($lonid);
                   16296:         my @hosts = &get_hosts_from_ip($ip);
                   16297:         my %iphost = &get_iphost();
                   16298:         my (@idns,%seen);
                   16299:         foreach my $id (@hosts) {
                   16300:             my $dom = &host_domain($id);
                   16301:             my $prim_id = &domain($dom,'primary');
                   16302:             my $prim_ip = &get_host_ip($prim_id);
                   16303:             next if ($seen{$prim_ip});
                   16304:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   16305:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   16306:                     my $intdom = &internet_dom($id);
                   16307:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   16308:                         push(@idns,$intdom);
                   16309:                     }
                   16310:                 }
                   16311:             }
                   16312:             $seen{$prim_ip} = 1;
                   16313:         }
1.1219    raeburn  16314:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  16315:     }
                   16316: 
1.986     foxr     16317: }
                   16318: 
1.1079    raeburn  16319: sub all_loncaparevs {
1.1249    raeburn  16320:     return qw(1.1 1.2 1.3 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11);
1.1079    raeburn  16321: }
                   16322: 
1.1227    raeburn  16323: # ---------------------------------------------------------- Read loncaparev table
                   16324: {
                   16325:     sub load_loncaparevs { 
                   16326:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1359    raeburn  16327:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1227    raeburn  16328:                 while (my $configline=<$config>) {
                   16329:                     chomp($configline);
                   16330:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   16331:                     $loncaparevs{$hostid}=$loncaparev;
                   16332:                 }
                   16333:                 close($config);
                   16334:             }
                   16335:         }
                   16336:     }
                   16337: }
                   16338: 
                   16339: # ---------------------------------------------------------- Read serverhostID table
                   16340: {
                   16341:     sub load_serverhomeIDs {
                   16342:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1359    raeburn  16343:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1227    raeburn  16344:                 while (my $configline=<$config>) {
                   16345:                     chomp($configline);
                   16346:                     my ($name,$id)=split(/:/,$configline);
                   16347:                     $serverhomeIDs{$name}=$id;
                   16348:                 }
                   16349:                 close($config);
                   16350:             }
                   16351:         }
                   16352:     }
                   16353: }
                   16354: 
                   16355: 
1.862     albertel 16356: BEGIN {
                   16357: 
                   16358: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   16359:     unless ($readit) {
                   16360: {
                   16361:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   16362:     %perlvar = (%perlvar,%{$configvars});
                   16363: }
                   16364: 
                   16365: 
1.1       albertel 16366: # ------------------------------------------------------ Read spare server file
                   16367: {
1.1359    raeburn  16368:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 16369: 
                   16370:     while (my $configline=<$config>) {
                   16371:        chomp($configline);
1.284     matthew  16372:        if ($configline) {
1.784     albertel 16373: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 16374: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 16375: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 16376:        }
                   16377:     }
1.448     albertel 16378:     close($config);
1.1       albertel 16379: }
1.11      www      16380: # ------------------------------------------------------------ Read permissions
                   16381: {
1.1359    raeburn  16382:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11      www      16383: 
                   16384:     while (my $configline=<$config>) {
1.448     albertel 16385: 	chomp($configline);
                   16386: 	if ($configline) {
                   16387: 	    my ($role,$perm)=split(/ /,$configline);
                   16388: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   16389: 	}
1.11      www      16390:     }
1.448     albertel 16391:     close($config);
1.11      www      16392: }
                   16393: 
                   16394: # -------------------------------------------- Read plain texts for permissions
                   16395: {
1.1359    raeburn  16396:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      16397: 
                   16398:     while (my $configline=<$config>) {
1.448     albertel 16399: 	chomp($configline);
                   16400: 	if ($configline) {
1.742     raeburn  16401: 	    my ($short,@plain)=split(/:/,$configline);
                   16402:             %{$prp{$short}} = ();
                   16403: 	    if (@plain > 0) {
                   16404:                 $prp{$short}{'std'} = $plain[0];
                   16405:                 for (my $i=1; $i<@plain; $i++) {
                   16406:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   16407:                 }
                   16408:             }
1.448     albertel 16409: 	}
1.135     www      16410:     }
1.448     albertel 16411:     close($config);
1.135     www      16412: }
                   16413: 
                   16414: # ---------------------------------------------------------- Read package table
                   16415: {
1.1359    raeburn  16416:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135     www      16417: 
                   16418:     while (my $configline=<$config>) {
1.483     albertel 16419: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 16420: 	chomp($configline);
                   16421: 	my ($short,$plain)=split(/:/,$configline);
                   16422: 	my ($pack,$name)=split(/\&/,$short);
                   16423: 	if ($plain ne '') {
                   16424: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   16425: 	    $packagetab{$short}=$plain; 
                   16426: 	}
1.11      www      16427:     }
1.448     albertel 16428:     close($config);
1.329     matthew  16429: }
                   16430: 
1.1073    raeburn  16431: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  16432: 
                   16433: &load_loncaparevs();
1.1073    raeburn  16434: 
1.1074    raeburn  16435: # ---------------------------------------------------------- Read serverhostID table
                   16436: 
1.1227    raeburn  16437: &load_serverhomeIDs();
                   16438: 
                   16439: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  16440: {
                   16441:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   16442:     if (-e $file) {
                   16443:         my $parser = HTML::LCParser->new($file);
                   16444:         while (my $token = $parser->get_token()) {
                   16445:             if ($token->[0] eq 'S') {
                   16446:                 my $item = $token->[1];
                   16447:                 my $name = $token->[2]{'name'};
                   16448:                 my $value = $token->[2]{'value'};
1.1285    raeburn  16449:                 my $valuematch = $token->[2]{'valuematch'};
1.1303    raeburn  16450:                 my $namematch = $token->[2]{'namematch'};
                   16451:                 if ($item eq 'parameter') {
                   16452:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
                   16453:                         my $release = $parser->get_text();
                   16454:                         $release =~ s/(^\s*|\s*$ )//gx;
                   16455:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
                   16456:                     }
                   16457:                 } elsif ($item ne '' && $name ne '') {
1.1079    raeburn  16458:                     my $release = $parser->get_text();
                   16459:                     $release =~ s/(^\s*|\s*$ )//gx;
1.1303    raeburn  16460:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079    raeburn  16461:                 }
                   16462:             }
                   16463:         }
                   16464:     }
1.1073    raeburn  16465: }
                   16466: 
1.1138    raeburn  16467: # ---------------------------------------------------------- Read managers table
                   16468: {
                   16469:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1359    raeburn  16470:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138    raeburn  16471:             while (my $configline=<$config>) {
                   16472:                 chomp($configline);
                   16473:                 next if ($configline =~ /^\#/);
                   16474:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   16475:                     $managerstab{$configline} = 1;
                   16476:                 }
                   16477:             }
                   16478:             close($config);
                   16479:         }
                   16480:     }
                   16481: }
                   16482: 
1.329     matthew  16483: # ------------- set up temporary directory
                   16484: {
1.1117    foxr     16485:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  16486: 
1.11      www      16487: }
                   16488: 
1.1405    raeburn  16489: # ------------- set default texengine (domain default overrides this)
                   16490: {
                   16491:     $deftex = LONCAPA::texengine();
                   16492: }
                   16493: 
1.1416    raeburn  16494: # ------------- set default minimum length for passwords for internal auth users
                   16495: {
                   16496:     $passwdmin = LONCAPA::passwd_min();
                   16497: }
                   16498: 
1.794     albertel 16499: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   16500: 				'compress_threshold'=> 20_000,
                   16501:  			        });
1.185     www      16502: 
1.281     www      16503: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      16504: $dumpcount=0;
1.958     www      16505: $locknum=0;
1.22      www      16506: 
1.163     harris41 16507: &logtouch();
1.672     albertel 16508: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      16509: $readit=1;
1.564     albertel 16510:     {
                   16511: 	use integer;
                   16512: 	my $test=(2**32)+1;
1.568     albertel 16513: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 16514: 	&logthis(" Detected 64bit platform ($_64bit)");
                   16515:     }
1.195     www      16516: }
1.1       albertel 16517: }
1.179     www      16518: 
1.1       albertel 16519: 1;
1.191     harris41 16520: __END__
                   16521: 
1.243     albertel 16522: =pod
                   16523: 
1.191     harris41 16524: =head1 NAME
                   16525: 
1.243     albertel 16526: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 16527: 
                   16528: =head1 SYNOPSIS
                   16529: 
1.243     albertel 16530: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 16531: 
                   16532:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   16533: 
1.243     albertel 16534: Common parameters:
                   16535: 
                   16536: =over 4
                   16537: 
                   16538: =item *
                   16539: 
                   16540: $uname : an internal username (if $cname expecting a course Id specifically)
                   16541: 
                   16542: =item *
                   16543: 
                   16544: $udom : a domain (if $cdom expecting a course's domain specifically)
                   16545: 
                   16546: =item *
                   16547: 
                   16548: $symb : a resource instance identifier
                   16549: 
                   16550: =item *
                   16551: 
                   16552: $namespace : the name of a .db file that contains the data needed or
                   16553: being set.
                   16554: 
                   16555: =back
                   16556: 
1.394     bowersj2 16557: =head1 OVERVIEW
1.191     harris41 16558: 
1.394     bowersj2 16559: lonnet provides subroutines which interact with the
                   16560: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   16561: about classes, users, and resources.
1.243     albertel 16562: 
                   16563: For many of these objects you can also use this to store data about
                   16564: them or modify them in various ways.
1.191     harris41 16565: 
1.394     bowersj2 16566: =head2 Symbs
1.191     harris41 16567: 
1.394     bowersj2 16568: To identify a specific instance of a resource, LON-CAPA uses symbols
                   16569: or "symbs"X<symb>. These identifiers are built from the URL of the
                   16570: map, the resource number of the resource in the map, and the URL of
                   16571: the resource itself. The latter is somewhat redundant, but might help
                   16572: if maps change.
                   16573: 
                   16574: An example is
                   16575: 
                   16576:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   16577: 
                   16578: The respective map entry is
                   16579: 
                   16580:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   16581:   title="Problem 2">
                   16582:  </resource>
                   16583: 
                   16584: Symbs are used by the random number generator, as well as to store and
                   16585: restore data specific to a certain instance of for example a problem.
                   16586: 
                   16587: =head2 Storing And Retrieving Data
                   16588: 
                   16589: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   16590: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   16591: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   16592: is is the non-critical message twin of cstore. These functions are for
                   16593: handlers to store a perl hash to a user's permanent data space in an
                   16594: easy manner, and to retrieve it again on another call. It is expected
                   16595: that a handler would use this once at the beginning to retrieve data,
                   16596: and then again once at the end to send only the new data back.
                   16597: 
                   16598: The data is stored in the user's data directory on the user's
                   16599: homeserver under the ID of the course.
                   16600: 
                   16601: The hash that is returned by restore will have all of the previous
                   16602: value for all of the elements of the hash.
                   16603: 
                   16604: Example:
                   16605: 
                   16606:  #creating a hash
                   16607:  my %hash;
                   16608:  $hash{'foo'}='bar';
                   16609: 
                   16610:  #storing it
                   16611:  &Apache::lonnet::cstore(\%hash);
                   16612: 
                   16613:  #changing a value
                   16614:  $hash{'foo'}='notbar';
                   16615: 
                   16616:  #adding a new value
                   16617:  $hash{'bar'}='foo';
                   16618:  &Apache::lonnet::cstore(\%hash);
                   16619: 
                   16620:  #retrieving the hash
                   16621:  my %history=&Apache::lonnet::restore();
                   16622: 
                   16623:  #print the hash
                   16624:  foreach my $key (sort(keys(%history))) {
                   16625:    print("\%history{$key} = $history{$key}");
                   16626:  }
                   16627: 
                   16628: Will print out:
1.191     harris41 16629: 
1.394     bowersj2 16630:  %history{1:foo} = bar
                   16631:  %history{1:keys} = foo:timestamp
                   16632:  %history{1:timestamp} = 990455579
                   16633:  %history{2:bar} = foo
                   16634:  %history{2:foo} = notbar
                   16635:  %history{2:keys} = foo:bar:timestamp
                   16636:  %history{2:timestamp} = 990455580
                   16637:  %history{bar} = foo
                   16638:  %history{foo} = notbar
                   16639:  %history{timestamp} = 990455580
                   16640:  %history{version} = 2
                   16641: 
                   16642: Note that the special hash entries C<keys>, C<version> and
                   16643: C<timestamp> were added to the hash. C<version> will be equal to the
                   16644: total number of versions of the data that have been stored. The
                   16645: C<timestamp> attribute will be the UNIX time the hash was
                   16646: stored. C<keys> is available in every historical section to list which
                   16647: keys were added or changed at a specific historical revision of a
                   16648: hash.
                   16649: 
                   16650: B<Warning>: do not store the hash that restore returns directly. This
                   16651: will cause a mess since it will restore the historical keys as if the
                   16652: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 16653: 
1.394     bowersj2 16654: Calling convention:
1.191     harris41 16655: 
1.1225    raeburn  16656:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269    raeburn  16657:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191     harris41 16658: 
1.394     bowersj2 16659: For more detailed information, see lonnet specific documentation.
1.191     harris41 16660: 
1.394     bowersj2 16661: =head1 RETURN MESSAGES
1.191     harris41 16662: 
1.394     bowersj2 16663: =over 4
1.191     harris41 16664: 
1.394     bowersj2 16665: =item * B<con_lost>: unable to contact remote host
1.191     harris41 16666: 
1.394     bowersj2 16667: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   16668: when the connection is brought back up
1.191     harris41 16669: 
1.394     bowersj2 16670: =item * B<con_failed>: unable to contact remote host and unable to save message
                   16671: for later delivery
1.191     harris41 16672: 
1.967     bisitz   16673: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 16674: 
1.394     bowersj2 16675: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 16676: that was requested
1.191     harris41 16677: 
1.243     albertel 16678: =back
1.191     harris41 16679: 
1.243     albertel 16680: =head1 PUBLIC SUBROUTINES
1.191     harris41 16681: 
1.243     albertel 16682: =head2 Session Environment Functions
1.191     harris41 16683: 
1.243     albertel 16684: =over 4
1.191     harris41 16685: 
1.394     bowersj2 16686: =item * 
                   16687: X<appenv()>
1.949     raeburn  16688: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 16689: the user envirnoment file, and will be restored for each access this
1.620     albertel 16690: user makes during this session, also modifies the %env for the current
1.949     raeburn  16691: process. Optional rolesarrayref - if defined contains a reference to an array
                   16692: of roles which are exempt from the restriction on modifying user.role entries 
                   16693: in the user's environment.db and in %env.    
1.191     harris41 16694: 
                   16695: =item *
1.394     bowersj2 16696: X<delenv()>
1.987     raeburn  16697: B<delenv($delthis,$regexp)>: removes all items from the session
                   16698: environment file that begin with $delthis. If the 
                   16699: optional second arg - $regexp - is true, $delthis is treated as a 
                   16700: regular expression, otherwise \Q$delthis\E is used. 
                   16701: The values are also deleted from the current processes %env.
1.191     harris41 16702: 
1.795     albertel 16703: =item * get_env_multiple($name) 
                   16704: 
                   16705: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   16706: values may be defined and end up as an array ref.
                   16707: 
                   16708: returns an array of values
                   16709: 
1.243     albertel 16710: =back
                   16711: 
                   16712: =head2 User Information
1.191     harris41 16713: 
1.243     albertel 16714: =over 4
1.191     harris41 16715: 
                   16716: =item *
1.394     bowersj2 16717: X<queryauthenticate()>
                   16718: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 16719: authentication scheme
                   16720: 
                   16721: =item *
1.394     bowersj2 16722: X<authenticate()>
1.1073    raeburn  16723: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 16724: authenticate user from domain's lib servers (first use the current
                   16725: one). C<$upass> should be the users password.
1.1073    raeburn  16726: $checkdefauth is optional (value is 1 if a check should be made to
                   16727:    authenticate user using default authentication method, and allow
                   16728:    account creation if username does not have account in the domain).
                   16729: $clientcancheckhost is optional (value is 1 if checking whether the
                   16730:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 16731: 
                   16732: =item *
1.394     bowersj2 16733: X<homeserver()>
                   16734: B<homeserver($uname,$udom)>: find the server which has
                   16735: the user's directory and files (there must be only one), this caches
                   16736: the answer, and also caches if there is a borken connection.
1.191     harris41 16737: 
                   16738: =item *
1.394     bowersj2 16739: X<idget()>
1.1300    raeburn  16740: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
                   16741: a list of student/employee IDs or clicker IDs
                   16742: (student/employee IDs are a unique resource in a domain, there must be 
                   16743: only 1 ID per username, and only 1 username per ID in a specific domain).
                   16744: clickerIDs are not necessarily unique, as students might share clickers.
                   16745: (returns hash: id=>name,id=>name)
1.191     harris41 16746: 
                   16747: =item *
1.394     bowersj2 16748: X<idrget()>
                   16749: B<idrget($udom,@unames)>: find the IDs behind a list of
                   16750: usernames (returns hash: name=>id,name=>id)
1.191     harris41 16751: 
                   16752: =item *
1.394     bowersj2 16753: X<idput()>
1.1300    raeburn  16754: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
                   16755: names and associated student/employee IDs or clicker IDs.
                   16756: 
                   16757: =item *
                   16758: X<iddel()>
                   16759: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
                   16760: student/employee ID or clicker ID username look-ups from domain.
                   16761: The homeserver ($uhome) and namespace ($namespace) are optional.
                   16762: If no $uhome is provided, it will be determined usig &homeserver()
                   16763: for each user.  If no $namespace is provided, the default is ids.
                   16764: 
                   16765: =item *
                   16766: X<updateclickers()>
                   16767: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
                   16768: clicker ID-to-username look-ups in clickers.db on library server.
                   16769: Permitted actions are add or del (i.e., add or delete). The 
                   16770: clickers.db contains clickerID as keys (escaped), and each corresponding
                   16771: value is an escaped comma-separated list of usernames (for whom the
                   16772: library server is the homeserver), who registered that particular ID.
                   16773: If $critical is true, the update will be sent via &critical, otherwise
                   16774: &reply() will be used.
1.191     harris41 16775: 
                   16776: =item *
1.394     bowersj2 16777: X<rolesinit()>
1.1169    droeschl 16778: B<rolesinit($udom,$username)>: get user privileges.
                   16779: returns user role, first access and timer interval hashes
1.243     albertel 16780: 
                   16781: =item *
1.1171    droeschl 16782: X<privileged()>
                   16783: B<privileged($username,$domain)>: returns a true if user has a
                   16784: privileged and active role (i.e. su or dc), false otherwise.
                   16785: 
                   16786: =item *
1.551     albertel 16787: X<getsection()>
                   16788: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 16789: course $cname, return section name/number or '' for "not in course"
                   16790: and '-1' for "no section"
                   16791: 
                   16792: =item *
1.394     bowersj2 16793: X<userenvironment()>
                   16794: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 16795: passed in @what from the requested user's environment, returns a hash
                   16796: 
1.858     raeburn  16797: =item * 
                   16798: X<userlog_query()>
1.859     albertel 16799: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   16800: activity.log file. %filters defines filters applied when parsing the
                   16801: log file. These can be start or end timestamps, or the type of action
                   16802: - log to look for Login or Logout events, check for Checkin or
                   16803: Checkout, role for role selection. The response is in the form
                   16804: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   16805: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  16806: 
1.243     albertel 16807: =back
                   16808: 
                   16809: =head2 User Roles
                   16810: 
                   16811: =over 4
                   16812: 
                   16813: =item *
                   16814: 
1.1284    raeburn  16815: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
                   16816: returns codes for allowed actions.
                   16817: 
                   16818: The first argument is required, all others are optional.
                   16819: 
                   16820: $priv is the privilege being checked.
                   16821: $uri contains additional information about what is being checked for access (e.g.,
                   16822: URL, course ID etc.). 
                   16823: $symb is the unique resource instance identifier in a course; if needed,
                   16824: but not provided, it will be retrieved via a call to &symbread(). 
                   16825: $role is the role for which a priv is being checked (only used if priv is evb). 
                   16826: $clientip is the user's IP address (only used when checking for access to portfolio 
                   16827: files).
                   16828: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
                   16829: prevents recursive calls to &allowed.
                   16830: 
1.243     albertel 16831:  F: full access
                   16832:  U,I,K: authentication modes (cxx only)
                   16833:  '': forbidden
                   16834:  1: user needs to choose course
                   16835:  2: browse allowed
1.766     albertel 16836:  A: passphrase authentication needed
1.1284    raeburn  16837:  B: access temporarily blocked because of a blocking event in a course.
1.1402    raeburn  16838:  D: access blocked because access is required via session initiated via deep-link 
1.243     albertel 16839: 
                   16840: =item *
                   16841: 
1.1192    raeburn  16842: constructaccess($url,$setpriv) : check for access to construction space URL
                   16843: 
                   16844: See if the owner domain and name in the URL match those in the
                   16845: expected environment.  If so, return three element list
                   16846: ($ownername,$ownerdomain,$ownerhome).
                   16847: 
                   16848: Otherwise return the null string.
                   16849: 
                   16850: If second argument 'setpriv' is true, it assigns the privileges,
                   16851: and returns the same three element list, unless the owner has
                   16852: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   16853: in which case the null string is returned.
                   16854: 
                   16855: =item *
                   16856: 
1.1326    raeburn  16857: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
                   16858: define a custom role rolename set privileges in format of lonTabs/roles.tab
                   16859: for system, domain, and course level. $uname and $udom are optional (current
                   16860: user's username and domain will be used when either of $uname or $udom are absent.
1.243     albertel 16861: 
                   16862: =item *
                   16863: 
1.988     raeburn  16864: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   16865: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  16866: $type is Course (default) or Community.
1.988     raeburn  16867: If $forcedefault evaluates to true, text returned will be default 
                   16868: text for $type. Otherwise, if this is a course, the text returned 
                   16869: will be a custom name for the role (if defined in the course's 
                   16870: environment).  If no custom name is defined the default is returned.
                   16871:    
1.832     raeburn  16872: =item *
                   16873: 
1.1219    raeburn  16874: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  16875: All arguments are optional. Returns a hash of a roles, either for
                   16876: co-author/assistant author roles for a user's Construction Space
1.906     albertel 16877: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  16878: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   16879: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   16880: For each key, value is set to colon-separated start and end times for
                   16881: the role.  If no username and domain are specified, will default to
1.934     raeburn  16882: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  16883: of role statuses (active, future or previous), roles 
                   16884: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   16885: to restrict the list of roles reported. If no array ref is 
                   16886: provided for types, will default to return only active roles.
1.834     albertel 16887: 
1.1195    raeburn  16888: =item *
                   16889: 
                   16890: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  16891: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   16892: 
                   16893: Additional optional arguments are: $type (if role checking is to be restricted 
                   16894: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  16895: available roles) or future (roles available in the future), and
                   16896: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  16897: have active Domain Coordinator role in course's domain or in additional
                   16898: domains (specified in 'Domains to check for privileged users' in course
                   16899: environment -- set via:  Course Settings -> Classlists and staff listing).
                   16900: 
                   16901: =item *
                   16902: 
                   16903: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   16904: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   16905: $possdomains and $possroles are optional array refs -- to domains to check and
                   16906: roles to check.  If $possdomains is not specified, a dump will be done of the
                   16907: users' roles.db to check for a dc or su role in any domain. This can be
                   16908: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   16909: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   16910: this then allows &privileged_by_domain() to be used, which caches the identity
                   16911: of privileged users, eliminating the need for repeated calls to &dump().
                   16912: 
                   16913: =item *
                   16914: 
                   16915: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   16916: where the outer hash keys are domains specified in the $possdomains array ref,
                   16917: next inner hash keys are privileged roles specified in the $roles array ref,
                   16918: and the innermost hash contains key = value pairs for username:domain = end:start
                   16919: for active or future "privileged" users with that role in that domain. To avoid
                   16920: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   16921: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  16922: 
1.243     albertel 16923: =back
                   16924: 
                   16925: =head2 User Modification
                   16926: 
                   16927: =over 4
                   16928: 
                   16929: =item *
                   16930: 
1.957     raeburn  16931: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 16932: user for the level given by URL.  Optional start and end dates (leave empty
                   16933: string or zero for "no date")
1.191     harris41 16934: 
                   16935: =item *
                   16936: 
1.243     albertel 16937: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   16938: change a users, password, possible return values are: ok,
                   16939: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   16940: refused
1.191     harris41 16941: 
                   16942: =item *
                   16943: 
1.243     albertel 16944: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 16945: 
                   16946: =item *
                   16947: 
1.1058    raeburn  16948: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   16949:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   16950: 
                   16951: will update user information (firstname,middlename,lastname,generation,
                   16952: permanentemail), and if forceid is true, student/employee ID also.
                   16953: A user's institutional affiliation(s) can also be updated.
                   16954: User information fields will not be overwritten with empty entries 
                   16955: unless the field is included in the $candelete array reference.
                   16956: This array is included when a single user is modified via "Manage Users",
                   16957: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 16958: 
                   16959: =item *
                   16960: 
1.286     matthew  16961: modifystudent
                   16962: 
1.957     raeburn  16963: modify a student's enrollment and identification information.
1.1235    raeburn  16964: The course id is resolved based on the current user's environment.  
                   16965: This means the invoking user must be a course coordinator or otherwise
1.286     matthew  16966: associated with a course.
                   16967: 
1.297     matthew  16968: This call is essentially a wrapper for lonnet::modifyuser and
                   16969: lonnet::modify_student_enrollment
1.286     matthew  16970: 
                   16971: Inputs: 
                   16972: 
                   16973: =over 4
                   16974: 
1.957     raeburn  16975: =item B<$udom> Student's loncapa domain
1.286     matthew  16976: 
1.957     raeburn  16977: =item B<$uname> Student's loncapa login name
1.286     matthew  16978: 
1.964     bisitz   16979: =item B<$uid> Student/Employee ID
1.286     matthew  16980: 
1.957     raeburn  16981: =item B<$umode> Student's authentication mode
1.286     matthew  16982: 
1.957     raeburn  16983: =item B<$upass> Student's password
1.286     matthew  16984: 
1.957     raeburn  16985: =item B<$first> Student's first name
1.286     matthew  16986: 
1.957     raeburn  16987: =item B<$middle> Student's middle name
1.286     matthew  16988: 
1.957     raeburn  16989: =item B<$last> Student's last name
1.286     matthew  16990: 
1.957     raeburn  16991: =item B<$gene> Student's generation
1.286     matthew  16992: 
1.957     raeburn  16993: =item B<$usec> Student's section in course
1.286     matthew  16994: 
                   16995: =item B<$end> Unix time of the roles expiration
                   16996: 
                   16997: =item B<$start> Unix time of the roles start date
                   16998: 
                   16999: =item B<$forceid> If defined, allow $uid to be changed
                   17000: 
                   17001: =item B<$desiredhome> server to use as home server for student
                   17002: 
1.957     raeburn  17003: =item B<$email> Student's permanent e-mail address
                   17004: 
                   17005: =item B<$type> Type of enrollment (auto or manual)
                   17006: 
1.963     raeburn  17007: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   17008: 
                   17009: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  17010: 
1.963     raeburn  17011: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  17012: 
1.963     raeburn  17013: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  17014: 
1.1216    raeburn  17015: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   17016: 
                   17017: =item B<$credits> Number of credits student will earn from this class - only needs to be supplied if value needs to be different from default credits for class.
1.957     raeburn  17018: 
1.286     matthew  17019: =back
1.297     matthew  17020: 
                   17021: =item *
                   17022: 
                   17023: modify_student_enrollment
                   17024: 
1.1235    raeburn  17025: Change a student's enrollment status in a class.  The environment variable
1.297     matthew  17026: 'role.request.course' must be defined for this function to proceed.
                   17027: 
                   17028: Inputs:
                   17029: 
                   17030: =over 4
                   17031: 
1.1235    raeburn  17032: =item $udom, student's domain
1.297     matthew  17033: 
1.1235    raeburn  17034: =item $uname, student's name
1.297     matthew  17035: 
1.1235    raeburn  17036: =item $uid, student's user id
1.297     matthew  17037: 
1.1235    raeburn  17038: =item $first, student's first name
1.297     matthew  17039: 
                   17040: =item $middle
                   17041: 
                   17042: =item $last
                   17043: 
                   17044: =item $gene
                   17045: 
                   17046: =item $usec
                   17047: 
                   17048: =item $end
                   17049: 
                   17050: =item $start
                   17051: 
1.957     raeburn  17052: =item $type
                   17053: 
                   17054: =item $locktype
                   17055: 
                   17056: =item $cid
                   17057: 
                   17058: =item $selfenroll
                   17059: 
                   17060: =item $context
                   17061: 
1.1216    raeburn  17062: =item $credits, number of credits student will earn from this class
                   17063: 
1.1314    raeburn  17064: =item $instsec, institutional course section code for student
                   17065: 
1.297     matthew  17066: =back
                   17067: 
1.191     harris41 17068: 
                   17069: =item *
                   17070: 
1.243     albertel 17071: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   17072: custom role; give a custom role to a user for the level given by URL.  Specify
                   17073: name and domain of role author, and role name
1.191     harris41 17074: 
                   17075: =item *
                   17076: 
1.243     albertel 17077: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 17078: 
                   17079: =item *
                   17080: 
1.243     albertel 17081: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   17082: 
                   17083: =back
                   17084: 
                   17085: =head2 Course Infomation
                   17086: 
                   17087: =over 4
1.191     harris41 17088: 
                   17089: =item *
                   17090: 
1.1118    foxr     17091: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 17092: specified course id, including all environment settings for the
                   17093: course, the description of the course will be in the hash under the
                   17094: key 'description'
1.191     harris41 17095: 
1.1118    foxr     17096: $options is an optional parameter that if supplied is a hash reference that controls
                   17097: what how this function works.  It has the following key/values:
                   17098: 
                   17099: =over 4
                   17100: 
                   17101: =item freshen_cache
                   17102: 
                   17103: If defined, and the environment cache for the course is valid, it is 
                   17104: returned in the returned hash.
                   17105: 
                   17106: =item one_time
                   17107: 
                   17108: If defined, the last cache time is set to _now_
                   17109: 
                   17110: =item user
                   17111: 
                   17112: If defined, the supplied username is used instead of the current user.
                   17113: 
                   17114: 
                   17115: =back
                   17116: 
1.191     harris41 17117: =item *
                   17118: 
1.624     albertel 17119: resdata($name,$domain,$type,@which) : request for current parameter
                   17120: setting for a specific $type, where $type is either 'course' or 'user',
                   17121: @what should be a list of parameters to ask about. This routine caches
1.1235    raeburn  17122: answers for 10 minutes.
1.243     albertel 17123: 
1.877     foxr     17124: =item *
                   17125: 
                   17126: get_courseresdata($courseid, $domain) : dump the entire course resource
                   17127: data base, returning a hash that is keyed by the resource name and has
                   17128: values that are the resource value.  I believe that the timestamps and
                   17129: versions are also returned.
                   17130: 
1.243     albertel 17131: =back
                   17132: 
                   17133: =head2 Course Modification
                   17134: 
                   17135: =over 4
1.191     harris41 17136: 
                   17137: =item *
                   17138: 
1.243     albertel 17139: writecoursepref($courseid,%prefs) : write preferences (environment
                   17140: database) for a course
1.191     harris41 17141: 
                   17142: =item *
                   17143: 
1.1011    raeburn  17144: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   17145: 
                   17146: =item *
                   17147: 
1.1038    raeburn  17148: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 17149: 
1.1167    droeschl 17150: =item *
                   17151: 
                   17152: is_course($courseid), is_course($cdom, $cnum)
                   17153: 
                   17154: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   17155: two component version $cdom, $cnum. It checks if the specified course exists.
                   17156: 
                   17157: Returns:
                   17158:     undef if the course doesn't exist, otherwise
                   17159:     in scalar context the combined courseid.
                   17160:     in list context the two components of the course identifier, domain and 
                   17161:     courseid.    
                   17162: 
1.243     albertel 17163: =back
                   17164: 
1.1401    raeburn  17165: =head2 Bubblesheet Configuration
                   17166: 
                   17167: =over 4
                   17168: 
                   17169: =item *
                   17170: 
                   17171: get_scantron_config($which)
                   17172: 
                   17173: $which - the name of the configuration to parse from the file.
                   17174: 
                   17175: Parses and returns the bubblesheet configuration line selected as a
                   17176: hash of configuration file fields.
                   17177: 
                   17178: 
                   17179: Returns:
                   17180:     If the named configuration is not in the file, an empty
                   17181:     hash is returned.
                   17182: 
                   17183:     a hash with the fields
                   17184:       name         - internal name for the this configuration setup
                   17185:       description  - text to display to operator that describes this config
                   17186:       CODElocation - if 0 or the string 'none'
                   17187:                           - no CODE exists for this config
                   17188:                      if -1 || the string 'letter'
                   17189:                           - a CODE exists for this config and is
                   17190:                             a string of letters
                   17191:                      Unsupported value (but planned for future support)
                   17192:                           if a positive integer
                   17193:                                - The CODE exists as the first n items from
                   17194:                                  the question section of the form
                   17195:                           if the string 'number'
                   17196:                                - The CODE exists for this config and is
                   17197:                                  a string of numbers
                   17198:       CODEstart   - (only matter if a CODE exists) column in the line where
                   17199:                      the CODE starts
                   17200:       CODElength  - length of the CODE
                   17201:       IDstart     - column where the student/employee ID starts
                   17202:       IDlength    - length of the student/employee ID info
                   17203:       Qstart      - column where the information from the bubbled
                   17204:                     'questions' start
                   17205:       Qlength     - number of columns comprising a single bubble line from
                   17206:                     the sheet. (usually either 1 or 10)
                   17207:       Qon         - either a single character representing the character used
                   17208:                     to signal a bubble was chosen in the positional setup, or
                   17209:                     the string 'letter' if the letter of the chosen bubble is
                   17210:                     in the final, or 'number' if a number representing the
                   17211:                     chosen bubble is in the file (1->A 0->J)
                   17212:       Qoff        - the character used to represent that a bubble was
                   17213:                     left blank
                   17214:       PaperID     - if the scanning process generates a unique number for each
                   17215:                     sheet scanned the column that this ID number starts in
                   17216:       PaperIDlength - number of columns that comprise the unique ID number
                   17217:                       for the sheet of paper
                   17218:       FirstName   - column that the first name starts in
                   17219:       FirstNameLength - number of columns that the first name spans
                   17220:       LastName    - column that the last name starts in
                   17221:       LastNameLength - number of columns that the last name spans
                   17222:       BubblesPerRow - number of bubbles available in each row used to
                   17223:                       bubble an answer. (If not specified, 10 assumed).
                   17224: 
                   17225: 
                   17226: =item *
                   17227: 
                   17228: get_scantronformat_file($cdom)
                   17229: 
                   17230: $cdom - the course's domain (optional); if not supplied, uses
                   17231: domain for current $env{'request.course.id'}.
                   17232: 
                   17233: Returns an array containing lines from the scantron format file for
                   17234: the domain of the course.
                   17235: 
                   17236: If a url for a custom.tab file is listed in domain's configuration.db,
                   17237: lines are from this file.
                   17238: 
                   17239: Otherwise, if a default.tab has been published in RES space by the
                   17240: domainconfig user, lines are from this file.
                   17241: 
                   17242: Otherwise, fall back to getting lines from the legacy file on the
                   17243: local server:  /home/httpd/lonTabs/default_scantronformat.tab
                   17244: 
                   17245: =back
                   17246: 
1.243     albertel 17247: =head2 Resource Subroutines
                   17248: 
                   17249: =over 4
1.191     harris41 17250: 
                   17251: =item *
                   17252: 
1.243     albertel 17253: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 17254: 
                   17255: =item *
                   17256: 
1.243     albertel 17257: repcopy($filename) : subscribes to the requested file, and attempts to
                   17258: replicate from the owning library server, Might return
1.607     raeburn  17259: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   17260: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 17261: resource. Expects the local filesystem pathname
                   17262: (/home/httpd/html/res/....)
                   17263: 
                   17264: =back
                   17265: 
                   17266: =head2 Resource Information
                   17267: 
                   17268: =over 4
1.191     harris41 17269: 
                   17270: =item *
                   17271: 
1.1228    raeburn  17272: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   17273: and returns the value of a variety of different possible values,
                   17274: $varname should be a request string, and the other parameters can be
                   17275: used to specify who and what one is asking about. Ordinarily, $cid 
                   17276: does not need to be specified, as it is retrived from 
                   17277: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   17278: within lonuserstate::loadmap() when initializing a course, before
                   17279: $env{'request.course.id'} has been set, so it needs to be provided
                   17280: in that one case.
1.243     albertel 17281: 
                   17282: Possible values for $varname are environment.lastname (or other item
                   17283: from the envirnment hash), user.name (or someother aspect about the
                   17284: user), resource.0.maxtries (or some other part and parameter of a
                   17285: resource)
1.204     albertel 17286: 
                   17287: =item *
                   17288: 
1.243     albertel 17289: directcondval($number) : get current value of a condition; reads from a state
                   17290: string
1.204     albertel 17291: 
                   17292: =item *
                   17293: 
1.243     albertel 17294: condval($condidx) : value of condition index based on state
1.204     albertel 17295: 
                   17296: =item *
                   17297: 
1.1362    raeburn  17298: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243     albertel 17299: resource's metadata, $what should be either a specific key, or either
                   17300: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1362    raeburn  17301: packages that this resource currently uses, the last 3 arguments are 
                   17302: only used internally for recursive metadata.
                   17303: 
                   17304: the toolsymb is only used where the uri is for an external tool (for which
                   17305: the uri as well as the symb are guaranteed to be unique).
1.243     albertel 17306: 
1.1371    raeburn  17307: this function automatically caches all requests except any made recursively
                   17308: to retrieve a list of metadata keys for an imported library file ($liburi is 
                   17309: defined).
1.191     harris41 17310: 
                   17311: =item *
                   17312: 
1.243     albertel 17313: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   17314: network of library servers; returns file handle of where SQL and regex results
                   17315: will be stored for query
1.191     harris41 17316: 
                   17317: =item *
                   17318: 
1.1282    raeburn  17319: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
                   17320: return symbolic list entry (all arguments optional). 
                   17321: 
                   17322: Args: filename is the filename (including path) for the file for which a symb 
                   17323: is required; donotrecurse, if true will prevent calls to allowed() being made 
                   17324: to check access status if more than one resource was found in the bighash 
                   17325: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
                   17326: a randompick); ignorecachednull, if true will prevent a symb of '' being 
                   17327: returned if $env{$cache_str} is defined as ''; checkforblock if true will
                   17328: cause possible symbs to be checked to determine if they are subject to content
                   17329: blocking, if so they will not be included as possible symbs; possibles is a
                   17330: ref to a hash, which, as a side effect, will be populated with all possible 
                   17331: symbs (content blocking not tested).
                   17332:  
1.243     albertel 17333: returns the data handle
1.191     harris41 17334: 
                   17335: =item *
                   17336: 
1.1190    raeburn  17337: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   17338: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 17339: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  17340: on failure, user must be in a course, as it assumes the existence of
                   17341: the course initial hash, and uses $env('request.course.id'}.  The third
                   17342: arg is an optional reference to a scalar.  If this arg is passed in the 
                   17343: call to symbverify, it will be set to 1 if the symb has been set to be 
                   17344: encrypted; otherwise it will be null.  
1.191     harris41 17345: 
                   17346: =item *
                   17347: 
1.243     albertel 17348: symbclean($symb) : removes versions numbers from a symb, returns the
                   17349: cleaned symb
1.191     harris41 17350: 
                   17351: =item *
                   17352: 
1.243     albertel 17353: is_on_map($uri) : checks if the $uri is somewhere on the current
                   17354: course map, user must be in a course for it to work.
1.191     harris41 17355: 
                   17356: =item *
                   17357: 
1.243     albertel 17358: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 17359: 
                   17360: =item *
                   17361: 
1.243     albertel 17362: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   17363: a random seed, all arguments are optional, if they aren't sent it uses the
                   17364: environment to derive them. Note: if symb isn't sent and it can't get one
                   17365: from &symbread it will use the current time as its return value
1.191     harris41 17366: 
                   17367: =item *
                   17368: 
1.243     albertel 17369: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   17370: unfakeable, receipt
1.191     harris41 17371: 
                   17372: =item *
                   17373: 
1.620     albertel 17374: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 17375: 
                   17376: =item *
                   17377: 
1.243     albertel 17378: countacc($url) : count the number of accesses to a given URL
1.191     harris41 17379: 
                   17380: =item *
                   17381: 
1.243     albertel 17382: checkout($symb,$tuname,$tudom,$tcrsid) :  creates a record of a user having looked at an item, most likely printed out or otherwise using a resource
1.191     harris41 17383: 
                   17384: =item *
                   17385: 
1.243     albertel 17386: checkin($token) : updates that a resource has beeen returned (a hard copy version for instance) and returns the data that $token was Checkout with ($symb, $tuname, $tudom, and $tcrsid)
1.191     harris41 17387: 
                   17388: =item *
                   17389: 
1.243     albertel 17390: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 17391: 
                   17392: =item *
                   17393: 
1.243     albertel 17394: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   17395: forcing spreadsheet to reevaluate the resource scores next time.
                   17396: 
1.1195    raeburn  17397: =item * 
                   17398: 
1.1196    raeburn  17399: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   17400: when viewing in course context.
1.1195    raeburn  17401: 
1.1196    raeburn  17402:  input: six args -- filename (decluttered), course number, course domain,
                   17403:                     url, symb (if registered) and group (if this is a 
                   17404:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  17405: 
1.1196    raeburn  17406:  output: array of five scalars --
1.1195    raeburn  17407:          $cfile -- url for file editing if editable on current server
                   17408:          $home -- homeserver of resource (i.e., for author if published,
                   17409:                                           or course if uploaded.).
                   17410:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  17411:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   17412:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  17413: 
                   17414: =item *
                   17415: 
                   17416: is_course_upload($file,$cnum,$cdom)
                   17417: 
                   17418: Used in course context to determine if current file was uploaded to 
                   17419: the course (i.e., would be found in /userfiles/docs on the course's 
                   17420: homeserver.
                   17421: 
                   17422:   input: 3 args -- filename (decluttered), course number and course domain.
                   17423:   output: boolean -- 1 if file was uploaded.
                   17424: 
1.243     albertel 17425: =back
                   17426: 
                   17427: =head2 Storing/Retreiving Data
                   17428: 
                   17429: =over 4
1.191     harris41 17430: 
                   17431: =item *
                   17432: 
1.1269    raeburn  17433: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
                   17434: permanently for this url; hashref needs to be given and should be a \%hashname;
                   17435: the remaining args aren't required and if they aren't passed or are '' they will
                   17436: be derived from the env (with the exception of $laststore, which is an 
                   17437: optional arg used when a user's submission is stored in grading).
                   17438: $laststore is $version=$timestamp, where $version is the most recent version
                   17439: number retrieved for the corresponding $symb in the $namespace db file, and
                   17440: $timestamp is the timestamp for that transaction (UNIX time).
                   17441: $laststore is currently only passed when cstore() is called by 
                   17442: structuretags::finalize_storage().
1.191     harris41 17443: 
                   17444: =item *
                   17445: 
1.1269    raeburn  17446: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
                   17447: but uses critical subroutine
1.191     harris41 17448: 
                   17449: =item *
                   17450: 
1.243     albertel 17451: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   17452: all args are optional
1.191     harris41 17453: 
                   17454: =item *
                   17455: 
1.717     albertel 17456: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   17457: dumps the complete (or key matching regexp) namespace into a hash
                   17458: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   17459: normally &store()ed into
                   17460: 
                   17461: $range should be either an integer '100' (give me the first 100
                   17462:                                            matching records)
                   17463:               or be  two integers sperated by a - with no spaces
                   17464:                  '30-50' (give me the 30th through the 50th matching
                   17465:                           records)
                   17466: 
                   17467: 
                   17468: =item *
                   17469: 
1.1269    raeburn  17470: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717     albertel 17471: replaces a &store() version of data with a replacement set of data
                   17472: for a particular resource in a namespace passed in the $storehash hash 
1.1269    raeburn  17473: reference. If $tolog is true, the transaction is logged in the courselog
                   17474: with an action=PUTSTORE.
1.717     albertel 17475: 
                   17476: =item *
                   17477: 
1.243     albertel 17478: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   17479: works very similar to store/cstore, but all data is stored in a
                   17480: temporary location and can be reset using tmpreset, $storehash should
                   17481: be a hash reference, returns nothing on success
1.191     harris41 17482: 
                   17483: =item *
                   17484: 
1.243     albertel 17485: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   17486: similar to restore, but all data is stored in a temporary location and
                   17487: can be reset using tmpreset. Returns a hash of values on success,
                   17488: error string otherwise.
1.191     harris41 17489: 
                   17490: =item *
                   17491: 
1.243     albertel 17492: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   17493: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 17494: 
                   17495: =item *
                   17496: 
1.243     albertel 17497: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   17498: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 17499: 
                   17500: =item *
                   17501: 
1.243     albertel 17502: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   17503: namesp ($udom and $uname are optional)
1.191     harris41 17504: 
                   17505: =item *
                   17506: 
1.702     albertel 17507: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 17508: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 17509: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  17510: 
1.702     albertel 17511: $range should be either an integer '100' (give me the first 100
                   17512:                                            matching records)
                   17513:               or be  two integers sperated by a - with no spaces
                   17514:                  '30-50' (give me the 30th through the 50th matching
                   17515:                           records)
1.449     matthew  17516: =item *
                   17517: 
                   17518: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   17519: $store can be a scalar, an array reference, or if the amount to be 
                   17520: incremented is > 1, a hash reference.
                   17521: 
                   17522: ($udom and $uname are optional)
1.191     harris41 17523: 
                   17524: =item *
                   17525: 
1.243     albertel 17526: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   17527: ($udom and $uname are optional)
1.191     harris41 17528: 
                   17529: =item *
                   17530: 
1.243     albertel 17531: cput($namespace,$storehash,$udom,$uname) : critical put
                   17532: ($udom and $uname are optional)
1.191     harris41 17533: 
                   17534: =item *
                   17535: 
1.748     albertel 17536: newput($namespace,$storehash,$udom,$uname) :
                   17537: 
                   17538: Attempts to store the items in the $storehash, but only if they don't
                   17539: currently exist, if this succeeds you can be certain that you have 
                   17540: successfully created a new key value pair in the $namespace db.
                   17541: 
                   17542: 
                   17543: Args:
                   17544:  $namespace: name of database to store values to
                   17545:  $storehash: hashref to store to the db
                   17546:  $udom: (optional) domain of user containing the db
                   17547:  $uname: (optional) name of user caontaining the db
                   17548: 
                   17549: Returns:
                   17550:  'ok' -> succeeded in storing all keys of $storehash
                   17551:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   17552:                         least <key> already existed in the db (other
                   17553:                         requested keys may also already exist)
1.967     bisitz   17554:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 17555:  'con_lost' -> unable to contact request server
                   17556:  'refused' -> action was not allowed by remote machine
                   17557: 
                   17558: 
                   17559: =item *
                   17560: 
1.243     albertel 17561: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   17562: reference filled in from namesp (encrypts the return communication)
                   17563: ($udom and $uname are optional)
1.191     harris41 17564: 
                   17565: =item *
                   17566: 
1.243     albertel 17567: log($udom,$name,$home,$message) : write to permanent log for user; use
                   17568: critical subroutine
                   17569: 
1.806     raeburn  17570: =item *
                   17571: 
1.860     raeburn  17572: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   17573: array reference filled in from namespace found in domain level on either
                   17574: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  17575: 
                   17576: =item *
                   17577: 
1.860     raeburn  17578: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   17579: domain level either on specified domain server ($uhome) or primary domain 
                   17580: server ($udom and $uhome are optional)
1.806     raeburn  17581: 
1.943     raeburn  17582: =item * 
                   17583: 
1.1240    raeburn  17584: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
                   17585: for: authentication, language, quotas, timezone, date locale, and portal URL in
                   17586: the target domain.
                   17587: 
                   17588: May also include additional key => value pairs for the following groups:
                   17589: 
                   17590: =over
                   17591: 
                   17592: =item
                   17593: disk quotas (MB allocated by default to portfolios and authoring spaces).
                   17594: 
                   17595: =over
                   17596: 
                   17597: =item defaultquota, authorquota
                   17598: 
                   17599: =back
                   17600: 
                   17601: =item
                   17602: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
                   17603: portfolio for users).
                   17604: 
                   17605: =over
                   17606: 
                   17607: =item
                   17608: aboutme, blog, webdav, portfolio
                   17609: 
                   17610: =back
                   17611: 
                   17612: =item
                   17613: requestcourses: ability to request courses, and how requests are processed.
                   17614: 
                   17615: =over
                   17616: 
                   17617: =item
1.1305    raeburn  17618: official, unofficial, community, textbook, placement
1.1240    raeburn  17619: 
                   17620: =back
                   17621: 
                   17622: =item
                   17623: inststatus: types of institutional affiliation, and order in which they are displayed.
                   17624: 
                   17625: =over
                   17626: 
                   17627: =item
1.1256    raeburn  17628: inststatustypes, inststatusorder, inststatusguest
1.1240    raeburn  17629: 
                   17630: =back
                   17631: 
                   17632: =item
                   17633: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
                   17634: for course's uploaded content.
                   17635: 
                   17636: =over
                   17637: 
                   17638: =item
1.1246    raeburn  17639: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
1.1305    raeburn  17640: communityquota, textbookquota, placementquota
1.1240    raeburn  17641: 
                   17642: =back
                   17643: 
                   17644: =item
                   17645: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
                   17646: on your servers.
                   17647: 
                   17648: =over
                   17649: 
                   17650: =item 
                   17651: remotesessions, hostedsessions
                   17652: 
                   17653: =back
                   17654: 
                   17655: =back
                   17656: 
                   17657: In cases where a domain coordinator has never used the "Set Domain Configuration"
                   17658: utility to create a configuration.db file on a domain's primary library server 
                   17659: only the following domain defaults: auth_def, auth_arg_def, lang_def
                   17660: -- corresponding values are authentication type (internal, krb4, krb5,
                   17661: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
                   17662: will be available. Values are retrieved from cache (if current), unless the
                   17663: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
                   17664: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
                   17665: 
                   17666: Typical usage:
1.943     raeburn  17667: 
1.1240    raeburn  17668: %domdefaults = &get_domain_defaults($target_domain);
1.943     raeburn  17669: 
1.243     albertel 17670: =back
                   17671: 
                   17672: =head2 Network Status Functions
                   17673: 
                   17674: =over 4
1.191     harris41 17675: 
                   17676: =item *
                   17677: 
1.1137    raeburn  17678: dirlist() : return directory list based on URI (first arg).
                   17679: 
                   17680: Inputs: 1 required, 5 optional.
                   17681: 
                   17682: =over
                   17683: 
                   17684: =item 
                   17685: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   17686: 
                   17687: =item
                   17688: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   17689: 
                   17690: =item
                   17691: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   17692: 
                   17693: =item
                   17694: $getpropath - boolean: 1 if prepend path using &propath(). 
                   17695: 
                   17696: =item
                   17697: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   17698: 
                   17699: =item 
                   17700: $alternateRoot - path to prepend in place of path from $uri.
                   17701: 
                   17702: =back
                   17703: 
                   17704: Returns: Array of up to two items.
                   17705: 
                   17706: =over
                   17707: 
                   17708: a reference to an array of files/subdirectories
                   17709: 
                   17710: =over
                   17711: 
                   17712: Each element in the array of files/subdirectories is a & separated list of
                   17713: item name and the result of running stat on the item.  If dirlist was requested
                   17714: for a file instead of a directory, the item name will be ''. For a directory 
                   17715: listing, if the item is a metadata file, the element will end &N&M 
                   17716: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   17717: default copyright set (1).  
                   17718: 
                   17719: =back
                   17720: 
                   17721: a scalar containing error condition (if encountered).
                   17722: 
                   17723: =over
                   17724: 
                   17725: =item 
                   17726: no_host (no homeserver identified for $username:$domain).
                   17727: 
                   17728: =item 
                   17729: no_such_host (server contacted for listing not identified as valid host).
                   17730: 
                   17731: =item 
                   17732: con_lost (connection to remote server failed).
                   17733: 
                   17734: =item 
                   17735: refused (invalid $username:$domain received on lond side).
                   17736: 
                   17737: =item 
                   17738: no_such_dir (directory at specified path on lond side does not exist). 
                   17739: 
                   17740: =item 
                   17741: empty (directory at specified path on lond side is empty).
                   17742: 
                   17743: =over
                   17744: 
                   17745: This is currently not encountered because the &ls3, &ls2, 
                   17746: &ls (_handler) routines on the lond side do not filter out
                   17747: . and .. from a directory listing. 
                   17748: 
                   17749: =back
                   17750: 
                   17751: =back
                   17752: 
                   17753: =back
1.191     harris41 17754: 
                   17755: =item *
                   17756: 
1.243     albertel 17757: spareserver() : find server with least workload from spare.tab
                   17758: 
1.986     foxr     17759: 
                   17760: =item *
                   17761: 
                   17762: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   17763: if there is no corresponding loncapa host.
                   17764: 
1.243     albertel 17765: =back
                   17766: 
1.986     foxr     17767: 
1.243     albertel 17768: =head2 Apache Request
                   17769: 
                   17770: =over 4
1.191     harris41 17771: 
                   17772: =item *
                   17773: 
1.243     albertel 17774: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   17775: localhost, posts hash
                   17776: 
                   17777: =back
                   17778: 
                   17779: =head2 Data to String to Data
                   17780: 
                   17781: =over 4
1.191     harris41 17782: 
                   17783: =item *
                   17784: 
1.243     albertel 17785: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   17786: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 17787: 
                   17788: =item *
                   17789: 
1.243     albertel 17790: hashref2str($hashref) : convert a hashref into a string complete with
                   17791: escaping and '=' and '&' separators, supports elements that are
                   17792: arrayrefs and hashrefs
1.191     harris41 17793: 
                   17794: =item *
                   17795: 
1.243     albertel 17796: arrayref2str($arrayref) : convert an arrayref into a string complete
                   17797: with escaping and '&' separators, supports elements that are arrayrefs
                   17798: and hashrefs
1.191     harris41 17799: 
                   17800: =item *
                   17801: 
1.243     albertel 17802: str2hash($string) : convert string to hash using unescaping and
                   17803: splitting on '=' and '&', supports elements that are arrayrefs and
                   17804: hashrefs
1.191     harris41 17805: 
                   17806: =item *
                   17807: 
1.243     albertel 17808: str2array($string) : convert string to hash using unescaping and
                   17809: splitting on '&', supports elements that are arrayrefs and hashrefs
                   17810: 
                   17811: =back
                   17812: 
                   17813: =head2 Logging Routines
                   17814: 
                   17815: 
                   17816: These routines allow one to make log messages in the lonnet.log and
                   17817: lonnet.perm logfiles.
1.191     harris41 17818: 
1.1119    foxr     17819: =over 4
                   17820: 
1.191     harris41 17821: =item *
                   17822: 
1.243     albertel 17823: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 17824: 
                   17825: =item *
                   17826: 
1.243     albertel 17827: logthis() : append message to the normal lonnet.log file, it gets
                   17828: preiodically rolled over and deleted.
1.191     harris41 17829: 
                   17830: =item *
                   17831: 
1.243     albertel 17832: logperm() : append a permanent message to lonnet.perm.log, this log
                   17833: file never gets deleted by any automated portion of the system, only
                   17834: messages of critical importance should go in here.
                   17835: 
1.1119    foxr     17836: 
1.243     albertel 17837: =back
                   17838: 
                   17839: =head2 General File Helper Routines
                   17840: 
                   17841: =over 4
1.191     harris41 17842: 
                   17843: =item *
                   17844: 
1.481     raeburn  17845: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   17846: (a) files in /uploaded
                   17847:   (i) If a local copy of the file exists - 
                   17848:       compares modification date of local copy with last-modified date for 
                   17849:       definitive version stored on home server for course. If local copy is 
                   17850:       stale, requests a new version from the home server and stores it. 
                   17851:       If the original has been removed from the home server, then local copy 
                   17852:       is unlinked.
                   17853:   (ii) If local copy does not exist -
                   17854:       requests the file from the home server and stores it. 
                   17855:   
                   17856:   If $caller is 'uploadrep':  
                   17857:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   17858:     for request for files originally uploaded via DOCS. 
                   17859:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   17860:   
                   17861:   Otherwise:
                   17862:      This indicates a call from the content generation phase of the request.
                   17863:      -  returns the entire contents of the file or -1.
                   17864:      
                   17865: (b) files in /res
                   17866:    - returns the entire contents of a file or -1; 
                   17867:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 17868: 
1.712     albertel 17869: 
                   17870: =item *
                   17871: 
                   17872: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   17873:                   reference
                   17874: 
                   17875: returns either a stat() list of data about the file or an empty list
                   17876: if the file doesn't exist or couldn't find out about it (connection
                   17877: problems or user unknown)
                   17878: 
1.191     harris41 17879: =item *
                   17880: 
1.243     albertel 17881: filelocation($dir,$file) : returns file system location of a file
                   17882: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   17883: directory that relative $file lookups are to looked in ($dir of /a/dir
                   17884: and a file of ../bob will become /a/bob)
1.191     harris41 17885: 
                   17886: =item *
                   17887: 
                   17888: hreflocation($dir,$file) : returns file system location or a URL; same as
                   17889: filelocation except for hrefs
                   17890: 
                   17891: =item *
                   17892: 
1.1261    raeburn  17893: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
                   17894: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191     harris41 17895: 
1.243     albertel 17896: =back
                   17897: 
1.608     albertel 17898: =head2 Usererfile file routines (/uploaded*)
                   17899: 
                   17900: =over 4
                   17901: 
                   17902: =item *
                   17903: 
                   17904: userfileupload(): main rotine for putting a file in a user or course's
                   17905:                   filespace, arguments are,
                   17906: 
1.620     albertel 17907:  formname - required - this is the name of the element in $env where the
1.608     albertel 17908:            filename, and the contents of the file to create/modifed exist
1.620     albertel 17909:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   17910:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  17911:  context - if coursedoc, store the file in the course of the active role
                   17912:              of the current user; 
                   17913:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   17914:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 17915:  subdir - required - subdirectory to put the file in under ../userfiles/
                   17916:          if undefined, it will be placed in "unknown"
                   17917: 
                   17918:  (This routine calls clean_filename() to remove any dangerous
                   17919:  characters from the filename, and then calls finuserfileupload() to
                   17920:  complete the transaction)
                   17921: 
                   17922:  returns either the url of the uploaded file (/uploaded/....) if successful
                   17923:  and /adm/notfound.html if unsuccessful
                   17924: 
                   17925: =item *
                   17926: 
                   17927: clean_filename(): routine for cleaing a filename up for storage in
                   17928:                  userfile space, argument is:
                   17929: 
                   17930:  filename - proposed filename
                   17931: 
                   17932: returns: the new clean filename
                   17933: 
                   17934: =item *
                   17935: 
1.1090    raeburn  17936: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 17937: userspace, probably shouldn't be called directly
                   17938: 
                   17939:   docuname: username or courseid of destination for the file
                   17940:   docudom: domain of user/course of destination for the file
                   17941:   formname: same as for userfileupload()
1.1090    raeburn  17942:   fname: filename (including subdirectories) for the file
                   17943:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1401    raeburn  17944:           if hashref, and context is scantron, will convert csv format to standard format
1.1090    raeburn  17945:   allfiles: reference to hash used to store objects found by parser
                   17946:   codebase: reference to hash used for codebases of java objects found by parser
                   17947:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   17948:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   17949:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   17950:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   17951:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   17952:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  17953:   mimetype: reference to scalar to accommodate mime type determined
                   17954:             from File::MMagic if $parser = parse.
1.608     albertel 17955: 
                   17956:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  17957:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   17958:  was 'overwrite').
                   17959:  
1.608     albertel 17960: 
                   17961: =item *
                   17962: 
                   17963: renameuserfile(): renames an existing userfile to a new name
                   17964: 
                   17965:   Args:
                   17966:    docuname: username or courseid of destination for the file
                   17967:    docudom: domain of user/course of destination for the file
                   17968:    old: current file name (including any subdirs under userfiles)
                   17969:    new: desired file name (including any subdirs under userfiles)
                   17970: 
                   17971: =item *
                   17972: 
                   17973: mkdiruserfile(): creates a directory is a userfiles dir
                   17974: 
                   17975:   Args:
                   17976:    docuname: username or courseid of destination for the file
                   17977:    docudom: domain of user/course of destination for the file
                   17978:    dir: dir to create (including any subdirs under userfiles)
                   17979: 
                   17980: =item *
                   17981: 
                   17982: removeuserfile(): removes a file that exists in userfiles
                   17983: 
                   17984:   Args:
                   17985:    docuname: username or courseid of destination for the file
                   17986:    docudom: domain of user/course of destination for the file
                   17987:    fname: filname to delete (including any subdirs under userfiles)
                   17988: 
                   17989: =item *
                   17990: 
                   17991: removeuploadedurl(): convience function for removeuserfile()
                   17992: 
                   17993:   Args:
                   17994:    url:  a full /uploaded/... url to delete
                   17995: 
1.747     albertel 17996: =item * 
                   17997: 
                   17998: get_portfile_permissions():
                   17999:   Args:
                   18000:     domain: domain of user or course contain the portfolio files
                   18001:     user: name of user or num of course contain the portfolio files
                   18002:   Returns:
                   18003:     hashref of a dump of the proper file_permissions.db
                   18004:    
                   18005: 
                   18006: =item * 
                   18007: 
                   18008: get_access_controls():
                   18009: 
                   18010: Args:
                   18011:   current_permissions: the hash ref returned from get_portfile_permissions()
                   18012:   group: (optional) the group you want the files associated with
                   18013:   file: (optional) the file you want access info on
                   18014: 
                   18015: Returns:
1.749     raeburn  18016:     a hash (keys are file names) of hashes containing
                   18017:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   18018:         values are XML containing access control settings (see below) 
1.747     albertel 18019: 
                   18020: Internal notes:
                   18021: 
1.749     raeburn  18022:  access controls are stored in file_permissions.db as key=value pairs.
                   18023:     key -> path to file/file_name\0uniqueID:scope_end_start
                   18024:         where scope -> public,guest,course,group,domains or users.
                   18025:               end -> UNIX time for end of access (0 -> no end date)
                   18026:               start -> UNIX time for start of access
                   18027: 
                   18028:     value -> XML description of access control
                   18029:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   18030:             <start></start>
                   18031:             <end></end>
                   18032: 
                   18033:             <password></password>  for scope type = guest
                   18034: 
                   18035:             <domain></domain>     for scope type = course or group
                   18036:             <number></number>
                   18037:             <roles id="">
                   18038:              <role></role>
                   18039:              <access></access>
                   18040:              <section></section>
                   18041:              <group></group>
                   18042:             </roles>
                   18043: 
                   18044:             <dom></dom>         for scope type = domains
                   18045: 
                   18046:             <users>             for scope type = users
                   18047:              <user>
                   18048:               <uname></uname>
                   18049:               <udom></udom>
                   18050:              </user>
                   18051:             </users>
                   18052:            </scope> 
                   18053:               
                   18054:  Access data is also aggregated for each file in an additional key=value pair:
                   18055:  key -> path to file/file_name\0accesscontrol 
                   18056:  value -> reference to hash
                   18057:           hash contains key = value pairs
                   18058:           where key = uniqueID:scope_end_start
                   18059:                 value = UNIX time record was last updated
                   18060: 
                   18061:           Used to improve speed of look-ups of access controls for each file.  
                   18062:  
                   18063:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   18064: 
1.1198    raeburn  18065: =item *
                   18066: 
1.749     raeburn  18067: modify_access_controls():
                   18068: 
                   18069: Modifies access controls for a portfolio file
                   18070: Args
                   18071: 1. file name
                   18072: 2. reference to hash of required changes,
                   18073: 3. domain
                   18074: 4. username
                   18075:   where domain,username are the domain of the portfolio owner 
                   18076:   (either a user or a course) 
                   18077: 
                   18078: Returns:
                   18079: 1. result of additions or updates ('ok' or 'error', with error message). 
                   18080: 2. result of deletions ('ok' or 'error', with error message).
                   18081: 3. reference to hash of any new or updated access controls.
                   18082: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   18083:    key = integer (inbound ID)
1.1198    raeburn  18084:    value = uniqueID
                   18085: 
                   18086: =item *
                   18087: 
                   18088: get_timebased_id():
                   18089: 
                   18090: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   18091: course via the Course Editor (e.g., folders, composite pages, 
                   18092: group bulletin boards).
                   18093: 
                   18094: Args: (first three required; six others optional)
                   18095: 
                   18096: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   18097:    docssequence, or name of group
                   18098: 
                   18099: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   18100:    e.g., num, boardids
                   18101: 
                   18102: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   18103:    file will be named nohist_namespace.db
                   18104: 
                   18105: 4. cdom: domain of course; default is current course domain from %env
                   18106: 
                   18107: 5. cnum: course number; default is current course number from %env
                   18108: 
                   18109: 6. idtype: set to concat if an additional digit is to be appended to the 
                   18110:    unix timestamp to form the suffix, if the plain timestamp is already
                   18111:    in use.  Default is to not do this, but simply increment the unix 
                   18112:    timestamp by 1 until a unique key is obtained.
                   18113: 
                   18114: 7. who: holder of locking key; defaults to user:domain for user.
                   18115: 
                   18116: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   18117:    retrying); default is 3.
                   18118: 
                   18119: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   18120: 
                   18121: Returns:
                   18122: 
                   18123: 1. suffix obtained (numeric)
                   18124: 
                   18125: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   18126: 
                   18127: 3. error: contains (localized) error message if an error occurred.
                   18128: 
1.747     albertel 18129: 
1.608     albertel 18130: =back
                   18131: 
1.243     albertel 18132: =head2 HTTP Helper Routines
                   18133: 
                   18134: =over 4
                   18135: 
1.191     harris41 18136: =item *
                   18137: 
                   18138: escape() : unpack non-word characters into CGI-compatible hex codes
                   18139: 
                   18140: =item *
                   18141: 
                   18142: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   18143: 
1.243     albertel 18144: =back
                   18145: 
                   18146: =head1 PRIVATE SUBROUTINES
                   18147: 
                   18148: =head2 Underlying communication routines (Shouldn't call)
                   18149: 
                   18150: =over 4
                   18151: 
                   18152: =item *
                   18153: 
                   18154: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   18155: 
                   18156: =item *
                   18157: 
                   18158: reply() : uses subreply to send a message to remote machine, logs all failures
                   18159: 
                   18160: =item *
                   18161: 
                   18162: critical() : passes a critical message to another server; if cannot
                   18163: get through then place message in connection buffer directory and
                   18164: returns con_delayed, if incapable of saving message, returns
                   18165: con_failed
                   18166: 
                   18167: =item *
                   18168: 
                   18169: reconlonc() : tries to reconnect lonc client processes.
                   18170: 
                   18171: =back
                   18172: 
                   18173: =head2 Resource Access Logging
                   18174: 
                   18175: =over 4
                   18176: 
                   18177: =item *
                   18178: 
                   18179: flushcourselogs() : flush (save) buffer logs and access logs
                   18180: 
                   18181: =item *
                   18182: 
                   18183: courselog($what) : save message for course in hash
                   18184: 
                   18185: =item *
                   18186: 
                   18187: courseacclog($what) : save message for course using &courselog().  Perform
                   18188: special processing for specific resource types (problems, exams, quizzes, etc).
                   18189: 
1.191     harris41 18190: =item *
                   18191: 
                   18192: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   18193: as a PerlChildExitHandler
1.243     albertel 18194: 
                   18195: =back
                   18196: 
                   18197: =head2 Other
                   18198: 
                   18199: =over 4
                   18200: 
                   18201: =item *
                   18202: 
                   18203: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 18204: 
                   18205: =back
                   18206: 
                   18207: =cut
1.877     foxr     18208: 

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