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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1510  ! raeburn     4: # $Id: lonnet.pm,v 1.1509 2023/05/22 15:09:15 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 {
        !           419:     my ($cdom,$cnum,$crstool,$url,$idx,$keynum,$post,$paramsref,$inforef) = @_;
        !           420:     my $chome;
        !           421:     if (&domain($cdom) ne '') {
        !           422:         if ($crstool) {
        !           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 =
        !           435:                     &LONCAPA::Lond::sign_params($cdom,$cnum,$crstool,$url,
        !           436:                                                 $idx,$keynum,$post,
        !           437:                                                 $perlvar{'lonVersion'},
        !           438:                                                 $paramsref,$inforef);
        !           439:                 if ($rep ne '') {
        !           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:                 }
        !           451:                 $rep=&reply("encrypt:signlti:$cdom:$cnum:$crstool:$escurl:$idx:$keynum:$post:$params:$info",$chome);
        !           452:             }
        !           453:             if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
        !           454:                 return ();
        !           455:             } else {
        !           456:                 my %returnhash;
        !           457:                 foreach my $item (split(/\&/,$rep)) {
        !           458:                     my ($name,$value)=split(/\=/,$item);
        !           459:                     $returnhash{&unescape($name)}=&thaw_unescape($value);
        !           460:                 }
        !           461:                 return('ok',\%returnhash);
        !           462:             }
        !           463:         } else {
        !           464:             return ();
        !           465:         }
        !           466:     } else {
        !           467:         return ();
        !           468:         &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
        !           469:     }
        !           470: }
        !           471: 
1.1       albertel  472: # -------------------------------------------------- Non-critical communication
                    473: sub subreply {
                    474:     my ($cmd,$server)=@_;
1.838     albertel  475:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      476:     #
                    477:     #  With loncnew process trimming, there's a timing hole between lonc server
                    478:     #  process exit and the master server picking up the listen on the AF_UNIX
                    479:     #  socket.  In that time interval, a lock file will exist:
                    480: 
                    481:     my $lockfile=$peerfile.".lock";
                    482:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
1.1289    damieng   483: 	sleep(0.1);
1.549     foxr      484:     }
                    485:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      486:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      487:     #
1.550     foxr      488:     #   We'll give the connection a few tries before abandoning it.  If
                    489:     #   connection is not possible, we'll con_lost back to the client.
                    490:     #   
                    491:     my $client;
                    492:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    493: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    494: 				      Type    => SOCK_STREAM,
                    495: 				      Timeout => 10);
1.869     albertel  496: 	if ($client) {
1.550     foxr      497: 	    last;		# Connected!
1.850     albertel  498: 	} else {
1.853     albertel  499: 	    &create_connection(&hostname($server),$server);
1.550     foxr      500: 	}
1.1289    damieng   501:         sleep(0.1);	# Try again later if failed connection.
1.550     foxr      502:     }
                    503:     my $answer;
                    504:     if ($client) {
1.704     albertel  505: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      506: 	$answer=<$client>;
                    507: 	if (!$answer) { $answer="con_lost"; }
                    508: 	chomp($answer);
                    509:     } else {
                    510: 	$answer = 'con_lost';	# Failed connection.
                    511:     }
1.1       albertel  512:     return $answer;
                    513: }
                    514: 
                    515: sub reply {
                    516:     my ($cmd,$server)=@_;
1.838     albertel  517:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  518:     my $answer=subreply($cmd,$server);
1.65      www       519:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1396    raeburn   520:         my $logged = $cmd;
                    521:         if ($cmd =~ /^encrypt:([^:]+):/) {
                    522:             my $subcmd = $1;
                    523:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
                    524:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1462    raeburn   525:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
                    526:                 ($subcmd eq 'put')) {
1.1396    raeburn   527:                 (undef,undef,my @rest) = split(/:/,$cmd);
                    528:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
                    529:                     splice(@rest,2,1,'Hidden');
                    530:                 } elsif ($subcmd eq 'passwd') {
                    531:                     splice(@rest,2,2,('Hidden','Hidden'));
                    532:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1462    raeburn   533:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
1.1396    raeburn   534:                     splice(@rest,3,1,'Hidden');
                    535:                 }
                    536:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
                    537:             }
                    538:         }
                    539:         &logthis("<font color=\"blue\">WARNING:".
                    540:                  " $logged to $server returned $answer</font>");
1.12      www       541:     }
1.1       albertel  542:     return $answer;
                    543: }
                    544: 
                    545: # ----------------------------------------------------------- Send USR1 to lonc
                    546: 
                    547: sub reconlonc {
1.891     albertel  548:     my ($lonid) = @_;
                    549:     if ($lonid) {
1.1295    raeburn   550:         my $hostname = &hostname($lonid);
1.891     albertel  551: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    552: 	if ($hostname && -e $peerfile) {
                    553: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    554: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    555: 					     Type    => SOCK_STREAM,
                    556: 					     Timeout => 10);
                    557: 	    if ($client) {
                    558: 		print $client ("reset_retries\n");
                    559: 		my $answer=<$client>;
                    560: 		#reset just this one.
                    561: 	    }
                    562: 	}
                    563: 	return;
                    564:     }
                    565: 
1.836     www       566:     &logthis("Trying to reconnect lonc");
1.1       albertel  567:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1359    raeburn   568:     if (open(my $fh,"<",$loncfile)) {
1.1       albertel  569: 	my $loncpid=<$fh>;
                    570:         chomp($loncpid);
                    571:         if (kill 0 => $loncpid) {
                    572: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    573:             kill USR1 => $loncpid;
                    574:             sleep 1;
1.1295    raeburn   575:         } else {
1.12      www       576: 	    &logthis(
1.672     albertel  577:                "<font color=\"blue\">WARNING:".
1.12      www       578:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  579:         }
                    580:     } else {
1.836     www       581: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  582:     }
                    583: }
                    584: 
                    585: # ------------------------------------------------------ Critical communication
1.12      www       586: 
1.1       albertel  587: sub critical {
                    588:     my ($cmd,$server)=@_;
1.838     albertel  589:     unless (&hostname($server)) {
1.672     albertel  590:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       591:                " Critical message to unknown server ($server)</font>");
                    592:         return 'no_such_host';
                    593:     }
1.1       albertel  594:     my $answer=reply($cmd,$server);
                    595:     if ($answer eq 'con_lost') {
1.1295    raeburn   596: 	&reconlonc($server);
1.589     albertel  597: 	my $answer=reply($cmd,$server);
1.1       albertel  598:         if ($answer eq 'con_lost') {
                    599:             my $now=time;
                    600:             my $middlename=$cmd;
1.5       www       601:             $middlename=substr($middlename,0,16);
1.1       albertel  602:             $middlename=~s/\W//g;
                    603:             my $dfilename=
1.305     www       604:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    605:             $dumpcount++;
1.1       albertel  606:             {
1.448     albertel  607: 		my $dfh;
1.1359    raeburn   608: 		if (open($dfh,">",$dfilename)) {
1.448     albertel  609: 		    print $dfh "$cmd\n"; 
                    610: 		    close($dfh);
                    611: 		}
1.1       albertel  612:             }
1.1288    damieng   613:             sleep 1;
1.1       albertel  614:             my $wcmd='';
                    615:             {
1.448     albertel  616: 		my $dfh;
1.1359    raeburn   617: 		if (open($dfh,"<",$dfilename)) {
1.448     albertel  618: 		    $wcmd=<$dfh>; 
                    619: 		    close($dfh);
                    620: 		}
1.1       albertel  621:             }
                    622:             chomp($wcmd);
1.7       www       623:             if ($wcmd eq $cmd) {
1.672     albertel  624: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       625:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  626:                 &logperm("D:$server:$cmd");
                    627: 	        return 'con_delayed';
                    628:             } else {
1.672     albertel  629:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       630:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  631:                 &logperm("F:$server:$cmd");
                    632:                 return 'con_failed';
                    633:             }
                    634:         }
                    635:     }
                    636:     return $answer;
1.405     albertel  637: }
                    638: 
1.755     albertel  639: # ------------------------------------------- check if return value is an error
                    640: 
                    641: sub error {
                    642:     my ($result) = @_;
1.756     albertel  643:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  644: 	if ($2 == 2) { return undef; }
                    645: 	return $1;
                    646:     }
                    647:     return undef;
                    648: }
                    649: 
1.783     albertel  650: sub convert_and_load_session_env {
                    651:     my ($lonidsdir,$handle)=@_;
                    652:     my @profile;
                    653:     {
1.917     albertel  654: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    655: 	if (!$opened) {
1.915     albertel  656: 	    return 0;
                    657: 	}
1.783     albertel  658: 	flock($idf,LOCK_SH);
                    659: 	@profile=<$idf>;
                    660: 	close($idf);
                    661:     }
                    662:     my %temp_env;
                    663:     foreach my $line (@profile) {
1.786     albertel  664: 	if ($line !~ m/=/) {
                    665: 	    return 0;
                    666: 	}
1.783     albertel  667: 	chomp($line);
                    668: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    669: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    670:     }
                    671:     unlink("$lonidsdir/$handle.id");
                    672:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    673: 	    0640)) {
                    674: 	%disk_env = %temp_env;
                    675: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    676: 	untie(%disk_env);
                    677:     }
1.786     albertel  678:     return 1;
1.783     albertel  679: }
                    680: 
1.374     www       681: # ------------------------------------------- Transfer profile into environment
1.780     albertel  682: my $env_loaded;
                    683: sub transfer_profile_to_env {
1.788     albertel  684:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    685:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       686: 
1.720     albertel  687:     if (!defined($lonidsdir)) {
                    688: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    689:     }
                    690:     if (!defined($handle)) {
                    691:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    692:     }
                    693: 
1.786     albertel  694:     my $convert;
                    695:     {
1.917     albertel  696:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    697: 	if (!$opened) {
1.915     albertel  698: 	    return;
                    699: 	}
1.786     albertel  700: 	flock($idf,LOCK_SH);
                    701: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    702: 		&GDBM_READER(),0640)) {
                    703: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    704: 	    untie(%disk_env);
                    705: 	} else {
                    706: 	    $convert = 1;
                    707: 	}
                    708:     }
                    709:     if ($convert) {
                    710: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    711: 	    &logthis("Failed to load session, or convert session.");
                    712: 	}
1.374     www       713:     }
1.783     albertel  714: 
1.786     albertel  715:     my %remove;
1.783     albertel  716:     while ( my $envname = each(%env) ) {
1.433     matthew   717:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    718:             if ($time < time-300) {
1.783     albertel  719:                 $remove{$key}++;
1.433     matthew   720:             }
                    721:         }
                    722:     }
1.783     albertel  723: 
1.619     albertel  724:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  725:     $env_loaded=1;
1.783     albertel  726:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   727:         &delenv($expired_key);
1.374     www       728:     }
1.1       albertel  729: }
                    730: 
1.916     albertel  731: # ---------------------------------------------------- Check for valid session 
                    732: sub check_for_valid_session {
1.1355    raeburn   733:     my ($r,$name,$userhashref,$domref) = @_;
1.916     albertel  734:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1378    raeburn   735:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
                    736:     if ($name eq 'lonDAV') {
                    737:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    738:     } else {
                    739:         $lonidsdir=$r->dir_config('lonIDsDir');
                    740:         if ($name eq '') {
                    741:             $name = 'lonID';
                    742:         }
                    743:     }
                    744:     if ($name eq 'lonID') {
                    745:         $secure = 'lonSID';
1.1337    raeburn   746:         $linkname = 'lonLinkID';
                    747:         $pubname = 'lonPubID';
1.1378    raeburn   748:         if (exists($cookies{$secure})) {
                    749:             $lonid=$cookies{$secure};
                    750:         } elsif (exists($cookies{$name})) {
                    751:             $lonid=$cookies{$name};
1.1400    raeburn   752:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
1.1337    raeburn   753:             $lonid=$cookies{$linkname};
1.1378    raeburn   754:         } elsif (exists($cookies{$pubname})) {
                    755:             $lonid=$cookies{$pubname};
1.1337    raeburn   756:         }
1.1378    raeburn   757:     } else {
                    758:         $lonid=$cookies{$name};
1.1337    raeburn   759:     }
1.916     albertel  760:     return undef if (!$lonid);
                    761: 
                    762:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1378    raeburn   763:     if (-l "$lonidsdir/$handle.id") {
                    764:         my $link = readlink("$lonidsdir/$handle.id");
                    765:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    766:             $handle = $1;
                    767:         }
1.1155    raeburn   768:     }
1.1355    raeburn   769:     if (!-e "$lonidsdir/$handle.id") {
                    770:         if ((ref($domref)) && ($name eq 'lonID') && 
                    771:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    772:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    773:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    774:                 $$domref = $possudom;
                    775:             }
                    776:         }
                    777:         return undef;
                    778:     }
1.916     albertel  779: 
1.917     albertel  780:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    781:     return undef if (!$opened);
1.916     albertel  782: 
                    783:     flock($idf,LOCK_SH);
                    784:     my %disk_env;
                    785:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    786: 	    &GDBM_READER(),0640)) {
                    787: 	return undef;	
                    788:     }
                    789: 
                    790:     if (!defined($disk_env{'user.name'})
                    791: 	|| !defined($disk_env{'user.domain'})) {
1.1394    raeburn   792:         untie(%disk_env);
1.916     albertel  793: 	return undef;
                    794:     }
1.1245    raeburn   795: 
                    796:     if (ref($userhashref) eq 'HASH') {
                    797:         $userhashref->{'name'} = $disk_env{'user.name'};
                    798:         $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1448    raeburn   799:         if ($disk_env{'request.role'}) {
                    800:             $userhashref->{'role'} = $disk_env{'request.role'};
                    801:         }
1.1361    raeburn   802:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
1.1375    raeburn   803:         if ($userhashref->{'lti'}) {
                    804:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
                    805:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
                    806:         }
1.1212    raeburn   807:     }
1.1394    raeburn   808:     untie(%disk_env);
1.1245    raeburn   809: 
1.916     albertel  810:     return $handle;
                    811: }
                    812: 
1.830     albertel  813: sub timed_flock {
                    814:     my ($file,$lock_type) = @_;
                    815:     my $failed=0;
                    816:     eval {
                    817: 	local $SIG{__DIE__}='DEFAULT';
                    818: 	local $SIG{ALRM}=sub {
                    819: 	    $failed=1;
                    820: 	    die("failed lock");
                    821: 	};
                    822: 	alarm(13);
                    823: 	flock($file,$lock_type);
                    824: 	alarm(0);
                    825:     };
                    826:     if ($failed) {
                    827: 	return undef;
                    828:     } else {
                    829: 	return 1;
                    830:     }
                    831: }
                    832: 
1.1392    raeburn   833: sub get_sessionfile_vars {
                    834:     my ($handle,$lonidsdir,$storearr) = @_;
                    835:     my %returnhash;
                    836:     unless (ref($storearr) eq 'ARRAY') {
                    837:         return %returnhash;
                    838:     }
                    839:     if (-l "$lonidsdir/$handle.id") {
                    840:         my $link = readlink("$lonidsdir/$handle.id");
                    841:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    842:             $handle = $1;
                    843:         }
                    844:     }
                    845:     if ((-e "$lonidsdir/$handle.id") &&
                    846:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    847:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    848:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    849:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
                    850:                 flock($idf,LOCK_SH);
                    851:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    852:                         &GDBM_READER(),0640)) {
                    853:                     foreach my $item (@{$storearr}) {
                    854:                         $returnhash{$item} = $disk_env{$item};
                    855:                     }
                    856:                     untie(%disk_env);
                    857:                 }
                    858:             }
                    859:         }
                    860:     }
                    861:     return %returnhash;
                    862: }
                    863: 
1.5       www       864: # ---------------------------------------------------------- Append Environment
                    865: 
                    866: sub appenv {
1.949     raeburn   867:     my ($newenv,$roles) = @_;
                    868:     if (ref($newenv) eq 'HASH') {
                    869:         foreach my $key (keys(%{$newenv})) {
                    870:             my $refused = 0;
                    871: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    872:                 $refused = 1;
                    873:                 if (ref($roles) eq 'ARRAY') {
1.1250    raeburn   874:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949     raeburn   875:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    876:                         $refused = 0;
                    877:                     }
                    878:                 }
                    879:             }
                    880:             if ($refused) {
                    881:                 &logthis("<font color=\"blue\">WARNING: ".
                    882:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    883:                          .'</font>');
                    884: 	        delete($newenv->{$key});
                    885:             } else {
                    886:                 $env{$key}=$newenv->{$key};
                    887:             }
                    888:         }
1.1376    raeburn   889:         my $lonids = $perlvar{'lonIDsDir'};
                    890:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
                    891:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    892:             if ($opened
                    893: 	        && &timed_flock($env_file,LOCK_EX)
                    894: 	        &&
                    895: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    896: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    897: 	        while (my ($key,$value) = each(%{$newenv})) {
                    898: 	            $disk_env{$key} = $value;
                    899: 	        }
                    900: 	        untie(%disk_env);
                    901:             }
1.35      www       902:         }
1.191     harris41  903:     }
1.56      www       904:     return 'ok';
                    905: }
                    906: # ----------------------------------------------------- Delete from Environment
                    907: 
                    908: sub delenv {
1.1104    raeburn   909:     my ($delthis,$regexp,$roles) = @_;
                    910:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    911:         my $refused = 1;
                    912:         if (ref($roles) eq 'ARRAY') {
                    913:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    914:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    915:                 $refused = 0;
                    916:             }
                    917:         }
                    918:         if ($refused) {
                    919:             &logthis("<font color=\"blue\">WARNING: ".
                    920:                      "Attempt to delete from environment ".$delthis);
                    921:             return 'error';
                    922:         }
1.56      www       923:     }
1.917     albertel  924:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    925:     if ($opened
1.915     albertel  926: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  927: 	&&
                    928: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    929: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  930: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   931: 	    if ($regexp) {
                    932:                 if ($key=~/^$delthis/) {
                    933:                     delete($env{$key});
                    934:                     delete($disk_env{$key});
                    935:                 } 
                    936:             } else {
                    937:                 if ($key=~/^\Q$delthis\E/) {
                    938: 		    delete($env{$key});
                    939: 		    delete($disk_env{$key});
                    940: 	        }
                    941:             }
1.448     albertel  942: 	}
1.783     albertel  943: 	untie(%disk_env);
1.5       www       944:     }
                    945:     return 'ok';
1.369     albertel  946: }
                    947: 
1.790     albertel  948: sub get_env_multiple {
                    949:     my ($name) = @_;
                    950:     my @values;
                    951:     if (defined($env{$name})) {
                    952:         # exists is it an array
                    953:         if (ref($env{$name})) {
                    954:             @values=@{ $env{$name} };
                    955:         } else {
                    956:             $values[0]=$env{$name};
                    957:         }
                    958:     }
                    959:     return(@values);
                    960: }
                    961: 
1.958     www       962: # ------------------------------------------------------------------- Locking
                    963: 
                    964: sub set_lock {
                    965:     my ($text)=@_;
                    966:     $locknum++;
                    967:     my $id=$$.'-'.$locknum;
                    968:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    969:              'session.lock.'.$id => $text});
                    970:     return $id;
                    971: }
                    972: 
                    973: sub get_locks {
                    974:     my $num=0;
                    975:     my %texts=();
                    976:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    977:        if ($lock=~/\w/) {
                    978:           $num++;
                    979:           $texts{$lock}=$env{'session.lock.'.$lock};
                    980:        }
                    981:    }
                    982:    return ($num,%texts);
                    983: }
                    984: 
                    985: sub remove_lock {
                    986:     my ($id)=@_;
                    987:     my $newlocks='';
                    988:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    989:        if (($lock=~/\w/) && ($lock ne $id)) {
                    990:           $newlocks.=','.$lock;
                    991:        }
                    992:     }
                    993:     &appenv({'session.locks' => $newlocks});
                    994:     &delenv('session.lock.'.$id);
                    995: }
                    996: 
                    997: sub remove_all_locks {
                    998:     my $activelocks=$env{'session.locks'};
                    999:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                   1000:        if ($lock=~/\w/) {
                   1001:           &remove_lock($lock);
                   1002:        }
                   1003:     }
                   1004: }
                   1005: 
                   1006: 
1.369     albertel 1007: # ------------------------------------------ Find out current server userload
                   1008: sub userload {
                   1009:     my $numusers=0;
                   1010:     {
                   1011: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                   1012: 	my $filename;
                   1013: 	my $curtime=time;
                   1014: 	while ($filename=readdir(LONIDS)) {
1.925     albertel 1015: 	    next if ($filename eq '.' || $filename eq '..');
                   1016: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.1390    raeburn  1017:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404     albertel 1018: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel 1019: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel 1020: 	}
                   1021: 	closedir(LONIDS);
                   1022:     }
                   1023:     my $userloadpercent=0;
                   1024:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                   1025:     if ($maxuserload) {
1.371     albertel 1026: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel 1027:     }
1.372     albertel 1028:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel 1029:     return $userloadpercent;
1.283     www      1030: }
                   1031: 
1.1       albertel 1032: # ------------------------------ Find server with least workload from spare.tab
1.11      www      1033: 
1.1       albertel 1034: sub spareserver {
1.1451    raeburn  1035:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel 1036:     my $spare_server;
1.370     albertel 1037:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel 1038:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                   1039:                                                      :  $userloadpercent;
1.1083    raeburn  1040:     my ($uint_dom,$remotesessions);
                   1041:     if (($udom ne '') && (&domain($udom) ne '')) {
1.1494    raeburn  1042:         my $uprimary_id = &domain($udom,'primary');
                   1043:         $uint_dom = &internet_dom($uprimary_id);
                   1044:         my %udomdefaults = &get_domain_defaults($udom);
1.1083    raeburn  1045:         $remotesessions = $udomdefaults{'remotesessions'};
                   1046:     }
1.1123    raeburn  1047:     my $spareshash = &this_host_spares($udom);
                   1048:     if (ref($spareshash) eq 'HASH') {
                   1049:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1050:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279    raeburn  1051:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                   1052:                                              $try_server));
1.1123    raeburn  1053: 	        ($spare_server, $lowest_load) =
                   1054: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                   1055:             }
1.1083    raeburn  1056:         }
1.784     albertel 1057: 
1.1123    raeburn  1058:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                   1059: 
                   1060:         if (!$found_server) {
                   1061:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                   1062: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279    raeburn  1063:                     next unless (&spare_can_host($udom,$uint_dom,
                   1064:                                                  $remotesessions,$try_server));
1.1123    raeburn  1065: 	            ($spare_server, $lowest_load) =
                   1066: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                   1067:                 }
                   1068: 	    }
                   1069:         }
1.784     albertel 1070:     }
                   1071: 
                   1072:     if (!$want_server_name) {
1.1001    raeburn  1073:         if (defined($spare_server)) {
                   1074:             my $hostname = &hostname($spare_server);
1.1083    raeburn  1075:             if (defined($hostname)) {
1.1397    raeburn  1076:                 my $protocol = 'http';
                   1077:                 if ($protocol{$spare_server} eq 'https') {
                   1078:                     $protocol = $protocol{$spare_server};
                   1079:                 }
1.1494    raeburn  1080:                 my $alias = &use_proxy_alias($r,$spare_server);
1.1451    raeburn  1081:                 $hostname = $alias if ($alias ne '');
1.1001    raeburn  1082: 	        $spare_server = $protocol.'://'.$hostname;
                   1083:             }
                   1084:         }
1.784     albertel 1085:     }
                   1086:     return $spare_server;
                   1087: }
                   1088: 
                   1089: sub compare_server_load {
1.1253    raeburn  1090:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
                   1091: 
                   1092:     if ($required) {
                   1093:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                   1094:         my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1095:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   1096:         if (($major eq '' && $minor eq '') ||
                   1097:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                   1098:             return ($spare_server,$lowest_load);
                   1099:         }
                   1100:     }
1.784     albertel 1101: 
                   1102:     my $loadans     = &reply('load',    $try_server);
                   1103:     my $userloadans = &reply('userload',$try_server);
                   1104: 
                   1105:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn  1106: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel 1107:     }
                   1108: 
                   1109:     my $load;
                   1110:     if ($loadans =~ /\d/) {
                   1111: 	if ($userloadans =~ /\d/) {
                   1112: 	    #both are numbers, pick the bigger one
                   1113: 	    $load = ($loadans > $userloadans) ? $loadans 
                   1114: 		                              : $userloadans;
1.411     albertel 1115: 	} else {
1.784     albertel 1116: 	    $load = $loadans;
1.411     albertel 1117: 	}
1.784     albertel 1118:     } else {
                   1119: 	$load = $userloadans;
                   1120:     }
                   1121: 
                   1122:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                   1123: 	$spare_server = $try_server;
                   1124: 	$lowest_load  = $load;
1.370     albertel 1125:     }
1.784     albertel 1126:     return ($spare_server,$lowest_load);
1.202     matthew  1127: }
1.914     albertel 1128: 
                   1129: # --------------------------- ask offload servers if user already has a session
                   1130: sub find_existing_session {
                   1131:     my ($udom,$uname) = @_;
1.1123    raeburn  1132:     my $spareshash = &this_host_spares($udom);
                   1133:     if (ref($spareshash) eq 'HASH') {
                   1134:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1135:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                   1136:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1137:             }
                   1138:         }
                   1139:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                   1140:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                   1141:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1142:             }
                   1143:         }
1.914     albertel 1144:     }
                   1145:     return;
                   1146: }
                   1147: 
1.1411    raeburn  1148: sub delusersession {
                   1149:     my ($lonid,$udom,$uname) = @_;
                   1150:     my $uprimary_id = &domain($udom,'primary');
                   1151:     my $uintdom = &internet_dom($uprimary_id);
                   1152:     my $intdom = &internet_dom($lonid);
                   1153:     my $serverhomedom = &host_domain($lonid);
                   1154:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1155:         return &reply(join(':','delusersession',
                   1156:                             map {&escape($_)} ($udom,$uname)),$lonid);
                   1157:     }
                   1158:     return;
                   1159: }
                   1160: 
1.1389    raeburn  1161: # check if user's browser sent load balancer cookie and server still has session
                   1162: # and is not overloaded.
                   1163: sub check_for_balancer_cookie {
                   1164:     my ($r,$update_mtime) = @_;
                   1165:     my ($otherserver,$cookie);
                   1166:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                   1167:     if (exists($cookies{'balanceID'})) {
                   1168:         my $balid = $cookies{'balanceID'};
                   1169:         $cookie=&LONCAPA::clean_handle($balid->value);
                   1170:         my $balancedir=$r->dir_config('lonBalanceDir');
                   1171:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
                   1172:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
                   1173:                 my ($possudom,$possuname) = ($1,$2);
                   1174:                 my $has_session = 0;
                   1175:                 if ((&domain($possudom) ne '') &&
                   1176:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
                   1177:                     my $try_server;
                   1178:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
                   1179:                     if ($opened) {
                   1180:                         flock($idf,LOCK_SH);
                   1181:                         while (my $line = <$idf>) {
                   1182:                             chomp($line);
                   1183:                             if (&hostname($line) ne '') {
                   1184:                                 $try_server = $line;
                   1185:                                 last;
                   1186:                             }
                   1187:                         }
                   1188:                         close($idf);
                   1189:                         if (($try_server) &&
                   1190:                             (&has_user_session($try_server,$possudom,$possuname))) {
                   1191:                             my $lowest_load = 30000;
                   1192:                             ($otherserver,$lowest_load) =
                   1193:                                 &compare_server_load($try_server,undef,$lowest_load);
                   1194:                             if ($otherserver ne '' && $lowest_load < 100) {
                   1195:                                 $has_session = 1;
                   1196:                             } else {
                   1197:                                 undef($otherserver);
                   1198:                             }
                   1199:                         }
                   1200:                     }
                   1201:                 }
                   1202:                 if ($has_session) {
                   1203:                     if ($update_mtime) {
                   1204:                         my $atime = my $mtime = time;
                   1205:                         utime($atime,$mtime,"$balancedir/$cookie.id");
                   1206:                     }
                   1207:                 } else {
                   1208:                     unlink("$balancedir/$cookie.id");
                   1209:                 }
                   1210:             }
                   1211:         }
                   1212:     }
                   1213:     return ($otherserver,$cookie);
                   1214: }
                   1215: 
1.1431    raeburn  1216: sub updatebalcookie {
                   1217:     my ($cookie,$balancer,$lastentry)=@_;
                   1218:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1219:         my ($udom,$uname) = ($1,$2);
                   1220:         my $uprimary_id = &domain($udom,'primary');
                   1221:         my $uintdom = &internet_dom($uprimary_id);
                   1222:         my $intdom = &internet_dom($balancer);
                   1223:         my $serverhomedom = &host_domain($balancer);
                   1224:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1225:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
                   1226:         }
                   1227:     }
                   1228:     return;
                   1229: }
                   1230: 
1.1389    raeburn  1231: sub delbalcookie {
                   1232:     my ($cookie,$balancer) =@_;
                   1233:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1234:         my ($udom,$uname) = ($1,$2);
                   1235:         my $uprimary_id = &domain($udom,'primary');
                   1236:         my $uintdom = &internet_dom($uprimary_id);
                   1237:         my $intdom = &internet_dom($balancer);
                   1238:         my $serverhomedom = &host_domain($balancer);
                   1239:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1431    raeburn  1240:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1389    raeburn  1241:         }
                   1242:     }
                   1243: }
                   1244: 
1.914     albertel 1245: # -------------------------------- ask if server already has a session for user
                   1246: sub has_user_session {
                   1247:     my ($lonid,$udom,$uname) = @_;
                   1248:     my $result = &reply(join(':','userhassession',
                   1249: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                   1250:     return 1 if ($result eq 'ok');
                   1251: 
                   1252:     return 0;
                   1253: }
                   1254: 
1.1076    raeburn  1255: # --------- determine least loaded server in a user's domain which allows login
                   1256: 
                   1257: sub choose_server {
1.1259    raeburn  1258:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076    raeburn  1259:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn  1260:     my %servers = &get_servers($udom);
1.1076    raeburn  1261:     my $lowest_load = 30000;
1.1259    raeburn  1262:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
                   1263:     if ($skiploadbal) {
                   1264:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
                   1265:         unless (defined($cached)) {
                   1266:             my $cachetime = 60*60*24;
                   1267:             my %domconfig =
1.1494    raeburn  1268:                 &get_dom('configuration',['loadbalancing'],$udom);
1.1259    raeburn  1269:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1270:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
                   1271:                                            $cachetime);
                   1272:             }
                   1273:         }
                   1274:     }
1.1076    raeburn  1275:     foreach my $lonhost (keys(%servers)) {
1.1259    raeburn  1276:         if ($skiploadbal) {
                   1277:             if (ref($balancers) eq 'HASH') {
                   1278:                 next if (exists($balancers->{$lonhost}));
                   1279:             }
1.1389    raeburn  1280:         }
1.1115    raeburn  1281:         my $loginvia;
                   1282:         if ($checkloginvia) {
                   1283:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn  1284:             if ($loginvia) {
                   1285:                 my ($server,$path) = split(/:/,$loginvia);
                   1286:                 ($login_host, $lowest_load) =
1.1253    raeburn  1287:                     &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116    raeburn  1288:                 if ($login_host eq $server) {
                   1289:                     $portal_path = $path;
1.1151    raeburn  1290:                     $isredirect = 1;
1.1116    raeburn  1291:                 }
                   1292:             } else {
                   1293:                 ($login_host, $lowest_load) =
1.1253    raeburn  1294:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116    raeburn  1295:                 if ($login_host eq $lonhost) {
                   1296:                     $portal_path = '';
1.1151    raeburn  1297:                     $isredirect = ''; 
1.1116    raeburn  1298:                 }
                   1299:             }
                   1300:         } else {
1.1076    raeburn  1301:             ($login_host, $lowest_load) =
1.1253    raeburn  1302:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076    raeburn  1303:         }
                   1304:     }
                   1305:     if ($login_host ne '') {
1.1116    raeburn  1306:         $hostname = &hostname($login_host);
1.1076    raeburn  1307:     }
1.1331    raeburn  1308:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076    raeburn  1309: }
                   1310: 
1.1420    raeburn  1311: sub get_course_sessions {
                   1312:     my ($cnum,$cdom,$lastactivity) = @_;
                   1313:     my %servers = &internet_dom_servers($cdom);
                   1314:     my %returnhash;
                   1315:     foreach my $server (sort(keys(%servers))) {
                   1316:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
                   1317:         my @pairs=split(/\&/,$rep);
                   1318:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
                   1319:             foreach my $item (@pairs) {
                   1320:                 my ($key,$value)=split(/=/,$item,2);
                   1321:                 $key = &unescape($key);
                   1322:                 next if ($key =~ /^error: 2 /);
                   1323:                 if (exists($returnhash{$key})) {
                   1324:                     next if ($value < $returnhash{$key});
                   1325:                 }
                   1326:                 $returnhash{$key}=$value;
                   1327:             }
                   1328:         }
                   1329:     }
                   1330:     return %returnhash;
                   1331: }
                   1332: 
1.202     matthew  1333: # --------------------------------------------- Try to change a user's password
                   1334: 
                   1335: sub changepass {
1.799     raeburn  1336:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew  1337:     $currentpass = &escape($currentpass);
                   1338:     $newpass     = &escape($newpass);
1.1030    raeburn  1339:     my $lonhost = $perlvar{'lonHostID'};
                   1340:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew  1341: 		       $server);
                   1342:     if (! $answer) {
                   1343: 	&logthis("No reply on password change request to $server ".
                   1344: 		 "by $uname in domain $udom.");
                   1345:     } elsif ($answer =~ "^ok") {
                   1346:         &logthis("$uname in $udom successfully changed their password ".
                   1347: 		 "on $server.");
                   1348:     } elsif ($answer =~ "^pwchange_failure") {
                   1349: 	&logthis("$uname in $udom was unable to change their password ".
                   1350: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1351: 		 "or pwchange");
                   1352:     } elsif ($answer =~ "^non_authorized") {
                   1353:         &logthis("$uname in $udom did not get their password correct when ".
                   1354: 		 "attempting to change it on $server.");
                   1355:     } elsif ($answer =~ "^auth_mode_error") {
                   1356:         &logthis("$uname in $udom attempted to change their password despite ".
                   1357: 		 "not being locally or internally authenticated on $server.");
                   1358:     } elsif ($answer =~ "^unknown_user") {
                   1359:         &logthis("$uname in $udom attempted to change their password ".
                   1360: 		 "on $server but were unable to because $server is not ".
                   1361: 		 "their home server.");
                   1362:     } elsif ($answer =~ "^refused") {
                   1363: 	&logthis("$server refused to change $uname in $udom password because ".
                   1364: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1365:     } elsif ($answer =~ "invalid_client") {
                   1366:         &logthis("$server refused to change $uname in $udom password because ".
                   1367:                  "it was a reset by e-mail originating from an invalid server.");
1.1408    raeburn  1368:     } elsif ($answer =~ "^prioruse") {
                   1369:        &logthis("$server refused to change $uname in $udom password because ".
                   1370:                 "the password had been used before");
1.202     matthew  1371:     }
                   1372:     return $answer;
1.1       albertel 1373: }
                   1374: 
1.169     harris41 1375: # ----------------------- Try to determine user's current authentication scheme
                   1376: 
                   1377: sub queryauthenticate {
                   1378:     my ($uname,$udom)=@_;
1.456     albertel 1379:     my $uhome=&homeserver($uname,$udom);
1.1484    raeburn  1380:     if ((!$uhome) || ($uhome eq 'no_host')) {
1.456     albertel 1381: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1382: 	return 'no_host';
                   1383:     }
                   1384:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1385:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1386: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1387:     }
1.456     albertel 1388:     return $answer;
1.169     harris41 1389: }
                   1390: 
1.1       albertel 1391: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1392: 
1.1       albertel 1393: sub authenticate {
1.1073    raeburn  1394:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1395:     $upass=&escape($upass);
                   1396:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1397:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1398:     my $newhome;
1.836     www      1399:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1400: # Maybe the machine was offline and only re-appeared again recently?
                   1401:         &reconlonc();
                   1402: # One more
1.952     raeburn  1403: 	$uhome=&homeserver($uname,$udom,1);
                   1404:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1405:             if (defined(&domain($udom,'primary'))) {
                   1406:                 $newhome=&domain($udom,'primary');
                   1407:             }
                   1408:             if ($newhome ne '') {
                   1409:                 $uhome = $newhome;
                   1410:             }
                   1411:         }
1.836     www      1412: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1413: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1414: 	    return 'no_host';
                   1415:         }
1.1       albertel 1416:     }
1.1073    raeburn  1417:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1418:     if ($answer eq 'authorized') {
1.952     raeburn  1419:         if ($newhome) {
                   1420:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1421:             return 'no_account_on_host'; 
                   1422:         } else {
                   1423:             &logthis("User $uname at $udom authorized by $uhome");
                   1424:             return $uhome;
                   1425:         }
1.471     albertel 1426:     }
                   1427:     if ($answer eq 'non_authorized') {
                   1428: 	&logthis("User $uname at $udom rejected by $uhome");
1.1484    raeburn  1429: 	return 'no_host';
1.9       www      1430:     }
1.471     albertel 1431:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1432:     return 'no_host';
                   1433: }
                   1434: 
1.1491    raeburn  1435: sub can_switchserver {
                   1436:     my ($udom,$home) = @_;
                   1437:     my ($canswitch,@intdoms);
                   1438:     my $internet_names = &get_internet_names($home);
                   1439:     if (ref($internet_names) eq 'ARRAY') {
                   1440:         @intdoms = @{$internet_names};
                   1441:     }
                   1442:     my $uint_dom = &internet_dom(&domain($udom,'primary'));
                   1443:     if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1444:         $canswitch = 1;
                   1445:     } else {
                   1446:          my $serverhomeID = &get_server_homeID(&hostname($home));
                   1447:          my $serverhomedom = &host_domain($serverhomeID);
                   1448:          my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1449:          my %udomdefaults = &get_domain_defaults($udom);
                   1450:          my $remoterev = &get_server_loncaparev('',$home);
                   1451:          $canswitch = &can_host_session($udom,$home,$remoterev,
                   1452:                                         $udomdefaults{'remotesessions'},
                   1453:                                         $defdomdefaults{'hostedsessions'});
                   1454:     }
                   1455:     return $canswitch;
                   1456: }
                   1457: 
1.1073    raeburn  1458: sub can_host_session {
1.1074    raeburn  1459:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1460:     my $canhost = 1;
1.1494    raeburn  1461:     my $host_idn = &internet_dom($lonhost);
1.1073    raeburn  1462:     if (ref($remotesessions) eq 'HASH') {
                   1463:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1464:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1465:                 $canhost = 0;
                   1466:             } else {
                   1467:                 $canhost = 1;
                   1468:             }
                   1469:         }
                   1470:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1471:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1472:                 $canhost = 1;
                   1473:             } else {
                   1474:                 $canhost = 0;
                   1475:             }
                   1476:         }
                   1477:         if ($canhost) {
                   1478:             if ($remotesessions->{'version'} ne '') {
                   1479:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1480:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1481:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1482:                         my $major = $1;
                   1483:                         my $minor = $2;
                   1484:                         if (($major < $reqmajor ) ||
                   1485:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1486:                             $canhost = 0;
                   1487:                         }
                   1488:                     } else {
                   1489:                         $canhost = 0;
                   1490:                     }
                   1491:                 }
                   1492:             }
                   1493:         }
                   1494:     }
                   1495:     if ($canhost) {
                   1496:         if (ref($hostedsessions) eq 'HASH') {
1.1494    raeburn  1497:             my $uprimary_id = &domain($udom,'primary');
                   1498:             my $uint_dom = &internet_dom($uprimary_id);
1.1073    raeburn  1499:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1500:                 if (($uint_dom ne '') && 
                   1501:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1502:                     $canhost = 0;
                   1503:                 } else {
                   1504:                     $canhost = 1;
                   1505:                 }
                   1506:             }
                   1507:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1508:                 if (($uint_dom ne '') && 
                   1509:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1510:                     $canhost = 1;
                   1511:                 } else {
                   1512:                     $canhost = 0;
                   1513:                 }
                   1514:             }
                   1515:         }
                   1516:     }
                   1517:     return $canhost;
                   1518: }
                   1519: 
1.1083    raeburn  1520: sub spare_can_host {
                   1521:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1522:     my $canhost=1;
1.1279    raeburn  1523:     my $try_server_hostname = &hostname($try_server);
                   1524:     my $serverhomeID = &get_server_homeID($try_server_hostname);
                   1525:     my $serverhomedom = &host_domain($serverhomeID);
                   1526:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1527:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
                   1528:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
                   1529:             $canhost = 0;
                   1530:         }
                   1531:     }
1.1439    raeburn  1532:     if ($canhost) {
                   1533:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
                   1534:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
                   1535:                 unless (&shared_institution($udom,$try_server)) {
                   1536:                     $canhost = 0;
                   1537:                 }
                   1538:             }
                   1539:         }
                   1540:     }
1.1279    raeburn  1541:     if (($canhost) && ($uint_dom)) {
                   1542:         my @intdoms;
                   1543:         my $internet_names = &get_internet_names($try_server);
                   1544:         if (ref($internet_names) eq 'ARRAY') {
                   1545:             @intdoms = @{$internet_names};
                   1546:         }
                   1547:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1548:             my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1549:             $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1550:                                          $remotesessions,
                   1551:                                          $defdomdefaults{'hostedsessions'});
                   1552:         }
1.1083    raeburn  1553:     }
                   1554:     return $canhost;
                   1555: }
                   1556: 
1.1123    raeburn  1557: sub this_host_spares {
                   1558:     my ($dom) = @_;
1.1126    raeburn  1559:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1560:     my @hosts = &current_machine_ids();
                   1561:     foreach my $lonhost (@hosts) {
                   1562:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1563:             $dom_in_use = $dom;
                   1564:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1565:             last;
                   1566:         }
                   1567:     }
1.1126    raeburn  1568:     if ($dom_in_use ne '') {
                   1569:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1570:     }
                   1571:     if (ref($result) ne 'HASH') {
                   1572:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1573:         $dom_in_use = &host_domain($lonhost_in_use);
                   1574:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1575:         if (ref($result) ne 'HASH') {
                   1576:             $result = \%spareid;
                   1577:         }
                   1578:     }
                   1579:     return $result;
                   1580: }
                   1581: 
                   1582: sub spares_for_offload  {
                   1583:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1584:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1585:     if (defined($cached)) {
                   1586:         return $result;
                   1587:     } else {
1.1126    raeburn  1588:         my $cachetime = 60*60*24;
                   1589:         my %domconfig =
1.1494    raeburn  1590:             &get_dom('configuration',['usersessions'],$dom_in_use);
1.1126    raeburn  1591:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1592:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1593:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1594:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1595:                 }
                   1596:             }
                   1597:         }
                   1598:     }
1.1126    raeburn  1599:     return;
1.1123    raeburn  1600: }
                   1601: 
1.1129    raeburn  1602: sub get_lonbalancer_config {
                   1603:     my ($servers) = @_;
                   1604:     my ($currbalancer,$currtargets);
                   1605:     if (ref($servers) eq 'HASH') {
                   1606:         foreach my $server (keys(%{$servers})) {
                   1607:             my %what = (
                   1608:                          spareid => 1,
                   1609:                          perlvar => 1,
                   1610:                        );
                   1611:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1612:             if ($result eq 'ok') {
                   1613:                 if (ref($returnhash) eq 'HASH') {
                   1614:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1615:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1616:                             $currbalancer = $server;
                   1617:                             $currtargets = {};
                   1618:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1619:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1620:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1621:                                 }
                   1622:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1623:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1624:                                 }
                   1625:                             }
                   1626:                             last;
                   1627:                         }
                   1628:                     }
                   1629:                 }
                   1630:             }
                   1631:         }
                   1632:     }
                   1633:     return ($currbalancer,$currtargets);
                   1634: }
                   1635: 
                   1636: sub check_loadbalancing {
1.1331    raeburn  1637:     my ($uname,$udom,$caller) = @_;
1.1191    raeburn  1638:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1391    raeburn  1639:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
1.1129    raeburn  1640:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1641:     my @hosts = &current_machine_ids();
1.1494    raeburn  1642:     my $uprimary_id = &domain($udom,'primary');
                   1643:     my $uintdom = &internet_dom($uprimary_id);
                   1644:     my $intdom = &internet_dom($lonhost);
1.1129    raeburn  1645:     my $serverhomedom = &host_domain($lonhost);
1.1307    raeburn  1646:     my $domneedscache;
1.1129    raeburn  1647:     my $cachetime = 60*60*24;
                   1648: 
                   1649:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1650:         $dom_in_use = $udom;
                   1651:         $homeintdom = 1;
                   1652:     } else {
                   1653:         $dom_in_use = $serverhomedom;
                   1654:     }
                   1655:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1656:     unless (defined($cached)) {
                   1657:         my %domconfig =
1.1494    raeburn  1658:             &get_dom('configuration',['loadbalancing'],$dom_in_use);
1.1129    raeburn  1659:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1660:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307    raeburn  1661:         } else {
                   1662:             $domneedscache = $dom_in_use;
1.1129    raeburn  1663:         }
                   1664:     }
                   1665:     if (ref($result) eq 'HASH') {
1.1391    raeburn  1666:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1667:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1668:         if ($is_balancer) {
                   1669:             if (ref($currrules) eq 'HASH') {
                   1670:                 if ($homeintdom) {
                   1671:                     if ($uname ne '') {
                   1672:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1673:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1674:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1675:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1676:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1677:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1678:                             }
                   1679:                         }
                   1680:                         if ($rule_in_effect eq '') {
                   1681:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1682:                             if ($userenv{'inststatus'} ne '') {
                   1683:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1684:                                 my ($othertitle,$usertypes,$types) =
                   1685:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1686:                                 if (ref($types) eq 'ARRAY') {
                   1687:                                     foreach my $type (@{$types}) {
                   1688:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1689:                                             if (exists($currrules->{$type})) {
                   1690:                                                 $rule_in_effect = $currrules->{$type};
                   1691:                                             }
                   1692:                                         }
                   1693:                                     }
                   1694:                                 }
                   1695:                             } else {
                   1696:                                 if (exists($currrules->{'default'})) {
                   1697:                                     $rule_in_effect = $currrules->{'default'};
                   1698:                                 }
                   1699:                             }
                   1700:                         }
                   1701:                     } else {
                   1702:                         if (exists($currrules->{'default'})) {
                   1703:                             $rule_in_effect = $currrules->{'default'};
                   1704:                         }
                   1705:                     }
                   1706:                 } else {
                   1707:                     if ($currrules->{'_LC_external'} ne '') {
                   1708:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1709:                     }
                   1710:                 }
                   1711:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1712:                                                        $uname,$udom);
                   1713:             }
                   1714:         }
                   1715:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239    raeburn  1716:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129    raeburn  1717:         unless (defined($cached)) {
                   1718:             my %domconfig =
1.1494    raeburn  1719:                 &get_dom('configuration',['loadbalancing'],$serverhomedom);
1.1129    raeburn  1720:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307    raeburn  1721:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
                   1722:             } else {
                   1723:                 $domneedscache = $serverhomedom;
1.1129    raeburn  1724:             }
                   1725:         }
                   1726:         if (ref($result) eq 'HASH') {
1.1391    raeburn  1727:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1728:                 &check_balancer_result($result,@hosts);
                   1729:             if ($is_balancer) {
1.1129    raeburn  1730:                 if (ref($currrules) eq 'HASH') {
                   1731:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1732:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1733:                     }
                   1734:                 }
                   1735:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1736:                                                        $uname,$udom);
                   1737:             }
                   1738:         } else {
                   1739:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1740:                 $is_balancer = 1;
                   1741:                 $offloadto = &this_host_spares($dom_in_use);
                   1742:             }
1.1307    raeburn  1743:             unless (defined($cached)) {
                   1744:                 $domneedscache = $serverhomedom;
                   1745:             }
1.1129    raeburn  1746:         }
                   1747:     } else {
                   1748:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1749:             $is_balancer = 1;
                   1750:             $offloadto = &this_host_spares($dom_in_use);
                   1751:         }
1.1307    raeburn  1752:         unless (defined($cached)) {
                   1753:             $domneedscache = $serverhomedom;
                   1754:         }
                   1755:     }
                   1756:     if ($domneedscache) {
                   1757:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129    raeburn  1758:     }
1.1430    raeburn  1759:     if (($is_balancer) && ($caller ne 'switchserver')) {
1.1176    raeburn  1760:         my $lowest_load = 30000;
                   1761:         if (ref($offloadto) eq 'HASH') {
                   1762:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1763:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1764:                     ($otherserver,$lowest_load) =
                   1765:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1766:                 }
1.1129    raeburn  1767:             }
1.1176    raeburn  1768:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1769: 
1.1176    raeburn  1770:             if (!$found_server) {
                   1771:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1772:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1773:                         ($otherserver,$lowest_load) =
                   1774:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1775:                     }
                   1776:                 }
                   1777:             }
                   1778:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1779:             if (@{$offloadto} == 1) {
                   1780:                 $otherserver = $offloadto->[0];
                   1781:             } elsif (@{$offloadto} > 1) {
                   1782:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1783:                     ($otherserver,$lowest_load) =
                   1784:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1785:                 }
                   1786:             }
                   1787:         }
1.1331    raeburn  1788:         unless ($caller eq 'login') {
                   1789:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1790:                 $is_balancer = 0;
                   1791:                 if ($uname ne '' && $udom ne '') {
                   1792:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1389    raeburn  1793:                         &appenv({'user.loadbalexempt'     => $lonhost,
1.1331    raeburn  1794:                                  'user.loadbalcheck.time' => time});
                   1795:                     }
1.1176    raeburn  1796:                 }
1.1129    raeburn  1797:             }
                   1798:         }
1.1430    raeburn  1799:     }
                   1800:     if (($is_balancer) && (!$homeintdom)) {
                   1801:         undef($setcookie);
1.1129    raeburn  1802:     }
1.1391    raeburn  1803:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
1.1129    raeburn  1804: }
                   1805: 
1.1191    raeburn  1806: sub check_balancer_result {
                   1807:     my ($result,@hosts) = @_;
1.1391    raeburn  1808:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1809:     if (ref($result) eq 'HASH') {
                   1810:         if ($result->{'lonhost'} ne '') {
                   1811:             my $currbalancer = $result->{'lonhost'};
                   1812:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1813:                 $is_balancer = 1;
                   1814:                 $currtargets = $result->{'targets'};
                   1815:                 $currrules = $result->{'rules'};
                   1816:             }
1.1391    raeburn  1817:             $dom_balancers = $currbalancer;
1.1191    raeburn  1818:         } else {
1.1391    raeburn  1819:             if (keys(%{$result})) {
                   1820:                 foreach my $key (keys(%{$result})) {
                   1821:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1822:                         (ref($result->{$key}) eq 'HASH')) {
                   1823:                         $is_balancer = 1;
                   1824:                         $currrules = $result->{$key}{'rules'};
                   1825:                         $currtargets = $result->{$key}{'targets'};
                   1826:                         $setcookie = $result->{$key}{'cookie'};
                   1827:                         last;
                   1828:                     }
1.1191    raeburn  1829:                 }
1.1391    raeburn  1830:                 $dom_balancers = join(',',sort(keys(%{$result})));
1.1191    raeburn  1831:             }
                   1832:         }
                   1833:     }
1.1391    raeburn  1834:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1835: }
                   1836: 
1.1129    raeburn  1837: sub get_loadbalancer_targets {
                   1838:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1839:     my $offloadto;
1.1175    raeburn  1840:     if ($rule_in_effect eq 'none') {
                   1841:         return [$perlvar{'lonHostID'}];
                   1842:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1843:         $offloadto = $currtargets;
                   1844:     } else {
                   1845:         if ($rule_in_effect eq 'homeserver') {
                   1846:             my $homeserver = &homeserver($uname,$udom);
                   1847:             if ($homeserver ne 'no_host') {
                   1848:                 $offloadto = [$homeserver];
                   1849:             }
                   1850:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1851:             my %domconfig =
1.1494    raeburn  1852:                 &get_dom('configuration',['loadbalancing'],$udom);
1.1129    raeburn  1853:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1854:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1855:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1856:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1857:                     }
                   1858:                 }
                   1859:             } else {
1.1178    raeburn  1860:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1861:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1862:                 if (&hostname($remotebalancer) ne '') {
                   1863:                     $offloadto = [$remotebalancer];
                   1864:                 }
                   1865:             }
                   1866:         } elsif (&hostname($rule_in_effect) ne '') {
                   1867:             $offloadto = [$rule_in_effect];
                   1868:         }
                   1869:     }
                   1870:     return $offloadto;
                   1871: }
                   1872: 
1.1127    raeburn  1873: sub internet_dom_servers {
                   1874:     my ($dom) = @_;
                   1875:     my (%uniqservers,%servers);
                   1876:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1877:     my @machinedoms = &machine_domains($primaryserver);
                   1878:     foreach my $mdom (@machinedoms) {
                   1879:         my %currservers = %servers;
                   1880:         my %server = &get_servers($mdom);
                   1881:         %servers = (%currservers,%server);
                   1882:     }
                   1883:     my %by_hostname;
                   1884:     foreach my $id (keys(%servers)) {
                   1885:         push(@{$by_hostname{$servers{$id}}},$id);
                   1886:     }
                   1887:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1888:         if (@{$by_hostname{$hostname}} > 1) {
                   1889:             my $match = 0;
                   1890:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1891:                 if (&host_domain($id) eq $dom) {
                   1892:                     $uniqservers{$id} = $hostname;
                   1893:                     $match = 1;
                   1894:                 }
                   1895:             }
                   1896:             unless ($match) {
                   1897:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1898:             }
                   1899:         } else {
                   1900:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1901:         }
                   1902:     }
                   1903:     return %uniqservers;
                   1904: }
                   1905: 
1.1347    raeburn  1906: sub trusted_domains {
                   1907:     my ($cmdtype,$calldom) = @_;
1.1349    raeburn  1908:     my ($trusted,$untrusted);
1.1347    raeburn  1909:     if (&domain($calldom) eq '') {
1.1349    raeburn  1910:         return ($trusted,$untrusted);
1.1347    raeburn  1911:     }
1.1395    raeburn  1912:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
1.1349    raeburn  1913:         return ($trusted,$untrusted);
1.1347    raeburn  1914:     }
                   1915:     my $callprimary = &domain($calldom,'primary');
1.1494    raeburn  1916:     my $intcalldom = &internet_dom($callprimary);
1.1347    raeburn  1917:     if ($intcalldom eq '') {
1.1349    raeburn  1918:         return ($trusted,$untrusted);
1.1347    raeburn  1919:     }
                   1920: 
1.1494    raeburn  1921:     my ($trustconfig,$cached)=&is_cached_new('trust',$calldom);
1.1347    raeburn  1922:     unless (defined($cached)) {
1.1494    raeburn  1923:         my %domconfig = &get_dom('configuration',['trust'],$calldom);
                   1924:         &do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
1.1347    raeburn  1925:         $trustconfig = $domconfig{'trust'};
                   1926:     }
                   1927:     if (ref($trustconfig)) {
                   1928:         my (%possexc,%possinc,@allexc,@allinc); 
                   1929:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
                   1930:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
                   1931:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
                   1932:             }
                   1933:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
1.1395    raeburn  1934:                 $possinc{$intcalldom} = 1;
1.1347    raeburn  1935:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
                   1936:             }
                   1937:         }
                   1938:         if (keys(%possexc)) {
                   1939:             if (keys(%possinc)) {
                   1940:                 foreach my $key (sort(keys(%possexc))) {
                   1941:                     next if ($key eq $intcalldom);
                   1942:                     unless ($possinc{$key}) {
                   1943:                         push(@allexc,$key);
                   1944:                     }
                   1945:                 }
                   1946:             } else {
                   1947:                 @allexc = sort(keys(%possexc));
                   1948:             }
                   1949:         }
                   1950:         if (keys(%possinc)) {
                   1951:             $possinc{$intcalldom} = 1;
                   1952:             @allinc = sort(keys(%possinc));
                   1953:         }
                   1954:         if ((@allexc > 0) || (@allinc > 0)) {
                   1955:             my %doms_by_intdom;
                   1956:             my %allintdoms = &all_host_intdom();
                   1957:             my %alldoms = &all_host_domain();
                   1958:             foreach my $key (%allintdoms) {
                   1959:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
                   1960:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
                   1961:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
                   1962:                     }
                   1963:                 } else {
                   1964:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
                   1965:                 }
                   1966:             }
                   1967:             foreach my $exc (@allexc) {
                   1968:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
1.1395    raeburn  1969:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
1.1347    raeburn  1970:                 }
                   1971:             }
                   1972:             foreach my $inc (@allinc) {
                   1973:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
1.1395    raeburn  1974:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
1.1347    raeburn  1975:                 }
                   1976:             }
                   1977:         }
                   1978:     }
1.1349    raeburn  1979:     return ($trusted,$untrusted);
1.1347    raeburn  1980: }
                   1981: 
                   1982: sub will_trust {
                   1983:     my ($cmdtype,$domain,$possdom) = @_;
                   1984:     return 1 if ($domain eq $possdom);
                   1985:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
                   1986:     my $willtrust; 
                   1987:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
                   1988:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
                   1989:             $willtrust = 1;
                   1990:         }
                   1991:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
                   1992:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
                   1993:             $willtrust = 1;
                   1994:         }
                   1995:     } else {
                   1996:         $willtrust = 1;
                   1997:     }
                   1998:     return $willtrust;
                   1999: }
                   2000: 
1.1       albertel 2001: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      2002: 
1.599     albertel 2003: my %homecache;
1.1       albertel 2004: sub homeserver {
1.230     stredwic 2005:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 2006:     my $index="$uname:$udom";
1.426     albertel 2007: 
1.599     albertel 2008:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 2009: 
                   2010:     my %servers = &get_servers($udom,'library');
                   2011:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 2012:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 2013: 		 exists($badServerCache{$tryserver}));
1.841     albertel 2014: 
                   2015: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   2016: 	if ($answer eq 'found') {
                   2017: 	    delete($badServerCache{$tryserver}); 
                   2018: 	    return $homecache{$index}=$tryserver;
                   2019: 	} elsif ($answer eq 'no_host') {
                   2020: 	    $badServerCache{$tryserver}=1;
                   2021: 	}
1.1       albertel 2022:     }    
                   2023:     return 'no_host';
1.70      www      2024: }
                   2025: 
1.1300    raeburn  2026: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70      www      2027: 
                   2028: sub idget {
1.1300    raeburn  2029:     my ($udom,$idsref,$namespace)=@_;
1.70      www      2030:     my %returnhash=();
1.1300    raeburn  2031:     my @ids=(); 
                   2032:     if (ref($idsref) eq 'ARRAY') {
                   2033:         @ids = @{$idsref};
                   2034:     } else {
                   2035:         return %returnhash; 
                   2036:     }
                   2037:     if ($namespace eq '') {
                   2038:         $namespace = 'ids';
                   2039:     }
1.70      www      2040:     
1.841     albertel 2041:     my %servers = &get_servers($udom,'library');
                   2042:     foreach my $tryserver (keys(%servers)) {
1.1299    raeburn  2043: 	my $idlist=join('&', map { &escape($_); } @ids);
1.1300    raeburn  2044: 	if ($namespace eq 'ids') {
                   2045: 	    $idlist=~tr/A-Z/a-z/;
                   2046: 	}
                   2047: 	my $reply;
                   2048: 	if ($namespace eq 'ids') {
                   2049: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   2050: 	} else {
                   2051: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
                   2052: 	}
1.841     albertel 2053: 	my @answer=();
                   2054: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   2055: 	    @answer=split(/\&/,$reply);
                   2056: 	}                    ;
                   2057: 	my $i;
                   2058: 	for ($i=0;$i<=$#ids;$i++) {
                   2059: 	    if ($answer[$i]) {
1.1299    raeburn  2060: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300    raeburn  2061: 	    }
1.841     albertel 2062: 	}
1.1300    raeburn  2063:     }
1.70      www      2064:     return %returnhash;
                   2065: }
                   2066: 
                   2067: # ------------------------------------- Find the IDs behind a list of usernames
                   2068: 
                   2069: sub idrget {
                   2070:     my ($udom,@unames)=@_;
                   2071:     my %returnhash=();
1.800     albertel 2072:     foreach my $uname (@unames) {
                   2073:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 2074:     }
1.70      www      2075:     return %returnhash;
                   2076: }
                   2077: 
1.1300    raeburn  2078: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70      www      2079: 
                   2080: sub idput {
1.1300    raeburn  2081:     my ($udom,$idsref,$uhom,$namespace)=@_;
1.70      www      2082:     my %servers=();
1.1300    raeburn  2083:     my %ids=();
                   2084:     my %byid = ();
                   2085:     if (ref($idsref) eq 'HASH') {
                   2086:         %ids=%{$idsref};
                   2087:     }
                   2088:     if ($namespace eq '') {
                   2089:         $namespace = 'ids'; 
                   2090:     }
1.800     albertel 2091:     foreach my $uname (keys(%ids)) {
                   2092: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300    raeburn  2093:         if ($uhom eq '') {
                   2094:             $uhom=&homeserver($uname,$udom);
                   2095:         }
1.70      www      2096:         if ($uhom ne 'no_host') {
1.800     albertel 2097:             my $esc_unam=&escape($uname);
1.1300    raeburn  2098:             if ($namespace eq 'ids') {
                   2099:                 my $id=&escape($ids{$uname});
                   2100:                 $id=~tr/A-Z/a-z/;
                   2101:                 my $esc_unam=&escape($uname);
                   2102:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70      www      2103:             } else {
1.1300    raeburn  2104:                 my @currids = split(/,/,$ids{$uname});
                   2105:                 foreach my $id (@currids) {
                   2106:                     $byid{$uhom}{$id} .= $uname.',';
                   2107:                 }
                   2108:             }
                   2109:         }
                   2110:     }
                   2111:     if ($namespace eq 'clickers') {
                   2112:         foreach my $server (keys(%byid)) {
                   2113:             if (ref($byid{$server}) eq 'HASH') {
                   2114:                 foreach my $id (keys(%{$byid{$server}})) {
                   2115:                     $byid{$server} =~ s/,$//;
                   2116:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
                   2117:                 }
1.70      www      2118:             }
                   2119:         }
1.191     harris41 2120:     }
1.800     albertel 2121:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2122:         $servers{$server} =~ s/\&$//;
                   2123:         if ($namespace eq 'ids') {     
                   2124:             &critical('idput:'.$udom.':'.$servers{$server},$server);
                   2125:         } else {
                   2126:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
                   2127:         }
1.191     harris41 2128:     }
1.344     www      2129: }
                   2130: 
1.1300    raeburn  2131: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231    raeburn  2132: 
                   2133: sub iddel {
1.1300    raeburn  2134:     my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231    raeburn  2135:     my %result=();
1.1300    raeburn  2136:     my %ids=();
                   2137:     my %byid = ();
                   2138:     if (ref($idshashref) eq 'HASH') {
                   2139:         %ids=%{$idshashref};
                   2140:     } else {
1.1231    raeburn  2141:         return %result;
                   2142:     }
1.1300    raeburn  2143:     if ($namespace eq '') {
                   2144:         $namespace = 'ids';
                   2145:     }
1.1231    raeburn  2146:     my %servers=();
1.1300    raeburn  2147:     while (my ($id,$unamestr) = each(%ids)) {
                   2148:         if ($namespace eq 'ids') {
                   2149:             my $uhom = $uhome;
                   2150:             if ($uhom eq '') { 
                   2151:                 $uhom=&homeserver($unamestr,$udom);
                   2152:             }
                   2153:             if ($uhom ne 'no_host') {
                   2154:                 $servers{$uhom}.='&'.&escape($id);
                   2155:             }
                   2156:          } else {
                   2157:             my @curritems = split(/,/,$ids{$id});
                   2158:             foreach my $uname (@curritems) {
                   2159:                 my $uhom = $uhome;
                   2160:                 if ($uhom eq '') {
                   2161:                     $uhom=&homeserver($uname,$udom);
                   2162:                 }
                   2163:                 if ($uhom ne 'no_host') { 
                   2164:                     $byid{$uhom}{$id} .= $uname.',';
                   2165:                 }
                   2166:             }
1.1231    raeburn  2167:         }
1.1300    raeburn  2168:     }
                   2169:     if ($namespace eq 'clickers') {
                   2170:         foreach my $server (keys(%byid)) {
                   2171:             if (ref($byid{$server}) eq 'HASH') {
                   2172:                 foreach my $id (keys(%{$byid{$server}})) {
                   2173:                     $byid{$server}{$id} =~ s/,$//;
                   2174:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
                   2175:                 }
1.1231    raeburn  2176:             }
                   2177:         }
                   2178:     }
                   2179:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2180:         $servers{$server} =~ s/\&$//;
                   2181:         if ($namespace eq 'ids') {
                   2182:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
                   2183:         } elsif ($namespace eq 'clickers') {
                   2184:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
                   2185:         }
1.1231    raeburn  2186:     }
                   2187:     return %result;
                   2188: }
                   2189: 
1.1300    raeburn  2190: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
                   2191: 
                   2192: sub updateclickers {
                   2193:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
                   2194:     my %clickers;
                   2195:     if (ref($idshashref) eq 'HASH') {
                   2196:         %clickers=%{$idshashref};
                   2197:     } else {
                   2198:         return;
                   2199:     }
                   2200:     my $items='';
                   2201:     foreach my $item (keys(%clickers)) {
                   2202:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
                   2203:     }
                   2204:     $items=~s/\&$//;
                   2205:     my $request = "updateclickers:$udom:$action:$items";
                   2206:     if ($critical) {
                   2207:         return &critical($request,$uhome);
                   2208:     } else {
                   2209:         return &reply($request,$uhome);
                   2210:     }
                   2211: }
                   2212: 
1.1023    raeburn  2213: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  2214: sub dump_dom {
1.1165    droeschl 2215:     my ($namespace, $udom, $regexp) = @_;
                   2216: 
                   2217:     $udom ||= $env{'user.domain'};
                   2218: 
                   2219:     return () unless $udom;
                   2220: 
                   2221:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  2222: }
                   2223: 
1.1023    raeburn  2224: # ------------------------------------------ get items from domain db files   
1.806     raeburn  2225: 
                   2226: sub get_dom {
1.1462    raeburn  2227:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
1.1267    raeburn  2228:     return if ($udom eq 'public');
1.806     raeburn  2229:     my $items='';
                   2230:     foreach my $item (@$storearr) {
                   2231:         $items.=&escape($item).'&';
                   2232:     }
                   2233:     $items=~s/\&$//;
1.860     raeburn  2234:     if (!$udom) {
                   2235:         $udom=$env{'user.domain'};
1.1267    raeburn  2236:         return if ($udom eq 'public');
1.860     raeburn  2237:         if (defined(&domain($udom,'primary'))) {
                   2238:             $uhome=&domain($udom,'primary');
                   2239:         } else {
1.874     albertel 2240:             undef($uhome);
1.860     raeburn  2241:         }
                   2242:     } else {
                   2243:         if (!$uhome) {
                   2244:             if (defined(&domain($udom,'primary'))) {
                   2245:                 $uhome=&domain($udom,'primary');
                   2246:             }
                   2247:         }
                   2248:     }
                   2249:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1344    raeburn  2250:         my $rep;
1.1442    raeburn  2251:         if (grep { $_ eq $uhome } &current_machine_ids()) {
                   2252:             # domain information is hosted on this machine
1.1462    raeburn  2253:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
1.1344    raeburn  2254:         } else {
1.1462    raeburn  2255:             if ($encrypt) {
1.1442    raeburn  2256:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
                   2257:             } else {
                   2258:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
                   2259:             }
1.1344    raeburn  2260:         }
1.866     raeburn  2261:         my %returnhash;
1.875     albertel 2262:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  2263:             return %returnhash;
                   2264:         }
1.806     raeburn  2265:         my @pairs=split(/\&/,$rep);
                   2266:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   2267:             return @pairs;
                   2268:         }
                   2269:         my $i=0;
                   2270:         foreach my $item (@$storearr) {
                   2271:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   2272:             $i++;
                   2273:         }
                   2274:         return %returnhash;
                   2275:     } else {
1.880     banghart 2276:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  2277:     }
                   2278: }
                   2279: 
                   2280: # -------------------------------------------- put items in domain db files 
                   2281: 
                   2282: sub put_dom {
1.1462    raeburn  2283:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
1.860     raeburn  2284:     if (!$udom) {
                   2285:         $udom=$env{'user.domain'};
                   2286:         if (defined(&domain($udom,'primary'))) {
                   2287:             $uhome=&domain($udom,'primary');
                   2288:         } else {
1.874     albertel 2289:             undef($uhome);
1.860     raeburn  2290:         }
                   2291:     } else {
                   2292:         if (!$uhome) {
                   2293:             if (defined(&domain($udom,'primary'))) {
                   2294:                 $uhome=&domain($udom,'primary');
                   2295:             }
                   2296:         }
                   2297:     } 
                   2298:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  2299:         my $items='';
                   2300:         foreach my $item (keys(%$storehash)) {
                   2301:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   2302:         }
                   2303:         $items=~s/\&$//;
1.1462    raeburn  2304:         if ($encrypt) {
1.1344    raeburn  2305:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
                   2306:         } else {
                   2307:             return &reply("putdom:$udom:$namespace:$items",$uhome);
                   2308:         }
1.806     raeburn  2309:     } else {
1.860     raeburn  2310:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  2311:     }
                   2312: }
                   2313: 
1.1023    raeburn  2314: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  2315: sub newput_dom {
1.1023    raeburn  2316:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  2317:     my $result;
                   2318:     if (!$udom) {
                   2319:         $udom=$env{'user.domain'};
                   2320:     }
1.1023    raeburn  2321:     if ($udom) {
                   2322:         my $uname = &get_domainconfiguser($udom);
                   2323:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  2324:     }
                   2325:     return $result;
                   2326: }
                   2327: 
1.1023    raeburn  2328: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  2329: sub del_dom {
1.1023    raeburn  2330:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  2331:     if (ref($storearr) eq 'ARRAY') {
                   2332:         if (!$udom) {
                   2333:             $udom=$env{'user.domain'};
                   2334:         }
1.1023    raeburn  2335:         if ($udom) {
                   2336:             my $uname = &get_domainconfiguser($udom); 
                   2337:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  2338:         }
                   2339:     }
                   2340: }
                   2341: 
1.1481    raeburn  2342: sub store_dom {
1.1482    raeburn  2343:     my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
1.1481    raeburn  2344:     $$storehash{'ip'}=&get_requestor_ip();
                   2345:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2346:     my $namevalue='';
                   2347:     foreach my $key (keys(%{$storehash})) {
                   2348:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   2349:     }
                   2350:     $namevalue=~s/\&$//;
                   2351:     if (grep { $_ eq $home } current_machine_ids()) {
                   2352:         return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
                   2353:     } else {
                   2354:         if ($namespace eq 'private') {
                   2355:             return 'refused';
1.1482    raeburn  2356:         } elsif ($encrypt) {
                   2357:             return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
1.1481    raeburn  2358:         } else {
1.1482    raeburn  2359:             return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
1.1481    raeburn  2360:         }
                   2361:     }
                   2362: }
                   2363: 
1.1482    raeburn  2364: sub restore_dom {
                   2365:     my ($id,$namespace,$dom,$home,$encrypt) = @_;
                   2366:     my $answer;
                   2367:     if (grep { $_ eq $home } current_machine_ids()) {
                   2368:         $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
                   2369:     } elsif ($namespace ne 'private') {
                   2370:         if ($encrypt) {
                   2371:             $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
                   2372:         } else {
                   2373:             $answer=&reply("restoredom:$dom:$namespace:$id",$home);
                   2374:         }
                   2375:     }
                   2376:     my %returnhash=();
                   2377:     unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') || 
                   2378:             ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
                   2379:         foreach my $line (split(/\&/,$answer)) {
                   2380:             my ($name,$value)=split(/\=/,$line);
                   2381:             $returnhash{&unescape($name)}=&thaw_unescape($value);
                   2382:         }
                   2383:         my $version;
                   2384:         for ($version=1;$version<=$returnhash{'version'};$version++) {
                   2385:             foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   2386:                 $returnhash{$item}=$returnhash{$version.':'.$item};
                   2387:             }
                   2388:         }
                   2389:     }
                   2390:     return %returnhash;
                   2391: }
                   2392: 
1.1023    raeburn  2393: # ----------------------------------construct domainconfig user for a domain 
                   2394: sub get_domainconfiguser {
                   2395:     my ($udom) = @_;
                   2396:     return $udom.'-domainconfig';
                   2397: }
                   2398: 
1.837     raeburn  2399: sub retrieve_inst_usertypes {
                   2400:     my ($udom) = @_;
                   2401:     my (%returnhash,@order);
1.1494    raeburn  2402:     my %domdefs = &get_domain_defaults($udom);
1.989     raeburn  2403:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   2404:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256    raeburn  2405:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989     raeburn  2406:     } else {
                   2407:         if (defined(&domain($udom,'primary'))) {
                   2408:             my $uhome=&domain($udom,'primary');
                   2409:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   2410:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256    raeburn  2411:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989     raeburn  2412:                 return (\%returnhash,\@order);
                   2413:             }
                   2414:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   2415:             my @pairs=split(/\&/,$hashitems);
                   2416:             foreach my $item (@pairs) {
                   2417:                 my ($key,$value)=split(/=/,$item,2);
                   2418:                 $key = &unescape($key);
                   2419:                 next if ($key =~ /^error: 2 /);
                   2420:                 $returnhash{$key}=&thaw_unescape($value);
                   2421:             }
                   2422:             my @esc_order = split(/\&/,$orderitems);
                   2423:             foreach my $item (@esc_order) {
                   2424:                 push(@order,&unescape($item));
                   2425:             }
                   2426:         } else {
1.1256    raeburn  2427:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837     raeburn  2428:         }
1.1256    raeburn  2429:         return (\%returnhash,\@order);
1.837     raeburn  2430:     }
                   2431: }
                   2432: 
1.868     raeburn  2433: sub is_domainimage {
                   2434:     my ($url) = @_;
1.1468    raeburn  2435:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
1.868     raeburn  2436:         if (&domain($1) ne '') {
                   2437:             return '1';
                   2438:         }
                   2439:     }
                   2440:     return;
                   2441: }
                   2442: 
1.899     raeburn  2443: sub inst_directory_query {
                   2444:     my ($srch) = @_;
                   2445:     my $udom = $srch->{'srchdomain'};
                   2446:     my %results;
                   2447:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  2448:     my $outcome;
1.899     raeburn  2449:     if ($homeserver ne '') {
1.1357    raeburn  2450:         unless ($homeserver eq $perlvar{'lonHostID'}) {
                   2451:             if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2452:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1357    raeburn  2453:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2454:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2455:                     (($major == 2) && ($minor < 12))) {
                   2456:                     return;
                   2457:                 }
                   2458:             }
                   2459:         }
1.904     albertel 2460: 	my $queryid=&reply("querysend:instdirsearch:".
                   2461: 			   &escape($srch->{'srchby'}).':'.
                   2462: 			   &escape($srch->{'srchterm'}).':'.
                   2463: 			   &escape($srch->{'srchtype'}),$homeserver);
                   2464: 	my $host=&hostname($homeserver);
                   2465: 	if ($queryid !~/^\Q$host\E\_/) {
1.1343    raeburn  2466: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904     albertel 2467: 	    return;
                   2468: 	}
                   2469: 	my $response = &get_query_reply($queryid);
                   2470: 	my $maxtries = 5;
                   2471: 	my $tries = 1;
                   2472: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2473: 	    $response = &get_query_reply($queryid);
                   2474: 	    $tries ++;
                   2475: 	}
                   2476: 
                   2477:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  2478:             if ($response eq 'unavailable') {
                   2479:                 $outcome = $response;
                   2480:             } else {
                   2481:                 $outcome = 'ok';
                   2482:                 my @matches = split(/\n/,$response);
                   2483:                 foreach my $match (@matches) {
                   2484:                     my ($key,$value) = split(/=/,$match);
                   2485:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   2486:                 }
1.899     raeburn  2487:             }
                   2488:         }
                   2489:     }
1.909     raeburn  2490:     return ($outcome,%results);
1.899     raeburn  2491: }
                   2492: 
                   2493: sub usersearch {
                   2494:     my ($srch) = @_;
                   2495:     my $dom = $srch->{'srchdomain'};
                   2496:     my %results;
                   2497:     my %libserv = &all_library();
                   2498:     my $query = 'usersearch';
                   2499:     foreach my $tryserver (keys(%libserv)) {
                   2500:         if (&host_domain($tryserver) eq $dom) {
1.1357    raeburn  2501:             unless ($tryserver eq $perlvar{'lonHostID'}) {
                   2502:                 if ($srch->{'srchby'} eq 'email') {
1.1417    raeburn  2503:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1357    raeburn  2504:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2505:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2506:                              (($major == 2) && ($minor < 12)));
                   2507:                 }
                   2508:             }
1.899     raeburn  2509:             my $host=&hostname($tryserver);
                   2510:             my $queryid=
1.911     raeburn  2511:                 &reply("querysend:".&escape($query).':'.
                   2512:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  2513:                        &escape($srch->{'srchtype'}).':'.
                   2514:                        &escape($srch->{'srchterm'}),$tryserver);
                   2515:             if ($queryid !~/^\Q$host\E\_/) {
                   2516:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  2517:                 next;
1.899     raeburn  2518:             }
                   2519:             my $reply = &get_query_reply($queryid);
                   2520:             my $maxtries = 1;
                   2521:             my $tries = 1;
                   2522:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   2523:                 $reply = &get_query_reply($queryid);
                   2524:                 $tries ++;
                   2525:             }
                   2526:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   2527:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   2528:             } else {
1.911     raeburn  2529:                 my @matches;
                   2530:                 if ($reply =~ /\n/) {
                   2531:                     @matches = split(/\n/,$reply);
                   2532:                 } else {
                   2533:                     @matches = split(/\&/,$reply);
                   2534:                 }
1.899     raeburn  2535:                 foreach my $match (@matches) {
                   2536:                     my ($uname,$udom,%userhash);
1.911     raeburn  2537:                     foreach my $entry (split(/:/,$match)) {
                   2538:                         my ($key,$value) =
                   2539:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  2540:                         $userhash{$key} = $value;
                   2541:                         if ($key eq 'username') {
                   2542:                             $uname = $value;
                   2543:                         } elsif ($key eq 'domain') {
                   2544:                             $udom = $value;
1.911     raeburn  2545:                         }
1.899     raeburn  2546:                     }
                   2547:                     $results{$uname.':'.$udom} = \%userhash;
                   2548:                 }
                   2549:             }
                   2550:         }
                   2551:     }
                   2552:     return %results;
                   2553: }
                   2554: 
1.912     raeburn  2555: sub get_instuser {
                   2556:     my ($udom,$uname,$id) = @_;
                   2557:     my $homeserver = &domain($udom,'primary');
                   2558:     my ($outcome,%results);
                   2559:     if ($homeserver ne '') {
                   2560:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   2561:                            &escape($id).':'.&escape($udom),$homeserver);
                   2562:         my $host=&hostname($homeserver);
                   2563:         if ($queryid !~/^\Q$host\E\_/) {
                   2564:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   2565:             return;
                   2566:         }
                   2567:         my $response = &get_query_reply($queryid);
                   2568:         my $maxtries = 5;
                   2569:         my $tries = 1;
                   2570:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2571:             $response = &get_query_reply($queryid);
                   2572:             $tries ++;
                   2573:         }
                   2574:         if (!&error($response) && $response ne 'refused') {
                   2575:             if ($response eq 'unavailable') {
                   2576:                 $outcome = $response;
                   2577:             } else {
                   2578:                 $outcome = 'ok';
                   2579:                 my @matches = split(/\n/,$response);
                   2580:                 foreach my $match (@matches) {
                   2581:                     my ($key,$value) = split(/=/,$match);
                   2582:                     $results{&unescape($key)} = &thaw_unescape($value);
                   2583:                 }
                   2584:             }
                   2585:         }
                   2586:     }
                   2587:     my %userinfo;
                   2588:     if (ref($results{$uname}) eq 'HASH') {
                   2589:         %userinfo = %{$results{$uname}};
                   2590:     } 
                   2591:     return ($outcome,%userinfo);
                   2592: }
                   2593: 
1.1290    raeburn  2594: sub get_multiple_instusers {
                   2595:     my ($udom,$users,$caller) = @_;
                   2596:     my ($outcome,$results);
                   2597:     if (ref($users) eq 'HASH') {
                   2598:         my $count = keys(%{$users}); 
                   2599:         my $requested = &freeze_escape($users);
                   2600:         my $homeserver = &domain($udom,'primary');
                   2601:         if ($homeserver ne '') {
                   2602:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
                   2603:             my $host=&hostname($homeserver);
                   2604:             if ($queryid !~/^\Q$host\E\_/) {
                   2605:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
                   2606:                          ' for host: '.$homeserver.'in domain '.$udom);
                   2607:                 return ($outcome,$results);
                   2608:             }
                   2609:             my $response = &get_query_reply($queryid);
                   2610:             my $maxtries = 5;
                   2611:             if ($count > 100) {
                   2612:                 $maxtries = 1+int($count/20);
                   2613:             }
                   2614:             my $tries = 1;
                   2615:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
                   2616:                 $response = &get_query_reply($queryid);
                   2617:                 $tries ++;
                   2618:             }
                   2619:             if ($response eq '') {
                   2620:                 $results = {};
                   2621:                 foreach my $key (keys(%{$users})) {
                   2622:                     my ($uname,$id);
                   2623:                     if ($caller eq 'id') {
                   2624:                         $id = $key;
                   2625:                     } else {
                   2626:                         $uname = $key;
                   2627:                     }
                   2628:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291    raeburn  2629:                     $outcome = $resp;
1.1290    raeburn  2630:                     if ($resp eq 'ok') {
                   2631:                         %{$results} = (%{$results}, %info);
                   2632:                     } else {
                   2633:                         last;
                   2634:                     }
                   2635:                 }
                   2636:             } elsif(!&error($response) && ($response ne 'refused')) {
                   2637:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
                   2638:                     $outcome = $response;
                   2639:                 } else {
1.1291    raeburn  2640:                     ($outcome,my $userdata) = split(/=/,$response,2);
1.1290    raeburn  2641:                     if ($outcome eq 'ok') {
                   2642:                         $results = &thaw_unescape($userdata); 
                   2643:                     }
                   2644:                 }
                   2645:             }
                   2646:         }
                   2647:     }
                   2648:     return ($outcome,$results);
                   2649: }
                   2650: 
1.912     raeburn  2651: sub inst_rulecheck {
1.923     raeburn  2652:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  2653:     my %returnhash;
                   2654:     if ($udom ne '') {
                   2655:         if (ref($rules) eq 'ARRAY') {
                   2656:             @{$rules} = map {&escape($_);} (@{$rules});
                   2657:             my $rulestr = join(':',@{$rules});
                   2658:             my $homeserver=&domain($udom,'primary');
                   2659:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2660:                 my $response;
                   2661:                 if ($item eq 'username') {                
                   2662:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   2663:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  2664:                                               $homeserver));
1.923     raeburn  2665:                 } elsif ($item eq 'id') {
                   2666:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   2667:                                               ':'.&escape($id).':'.$rulestr,
                   2668:                                               $homeserver));
1.945     raeburn  2669:                 } elsif ($item eq 'selfcreate') {
                   2670:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  2671:                                                &escape($udom).':'.&escape($uname).
                   2672:                                               ':'.$rulestr,$homeserver));
1.1484    raeburn  2673:                 } elsif ($item eq 'unamemap') {
                   2674:                     $response=&unescape(&reply('instunamemapcheck:'.
                   2675:                                                &escape($udom).':'.&escape($uname).
                   2676:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  2677:                 }
1.912     raeburn  2678:                 if ($response ne 'refused') {
                   2679:                     my @pairs=split(/\&/,$response);
                   2680:                     foreach my $item (@pairs) {
                   2681:                         my ($key,$value)=split(/=/,$item,2);
                   2682:                         $key = &unescape($key);
                   2683:                         next if ($key =~ /^error: 2 /);
                   2684:                         $returnhash{$key}=&thaw_unescape($value);
                   2685:                     }
                   2686:                 }
                   2687:             }
                   2688:         }
                   2689:     }
                   2690:     return %returnhash;
                   2691: }
                   2692: 
                   2693: sub inst_userrules {
1.923     raeburn  2694:     my ($udom,$check) = @_;
1.912     raeburn  2695:     my (%ruleshash,@ruleorder);
                   2696:     if ($udom ne '') {
                   2697:         my $homeserver=&domain($udom,'primary');
                   2698:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2699:             my $response;
                   2700:             if ($check eq 'id') {
                   2701:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  2702:                                  $homeserver);
1.943     raeburn  2703:             } elsif ($check eq 'email') {
                   2704:                 $response=&reply('instemailrules:'.&escape($udom),
                   2705:                                  $homeserver);
1.1484    raeburn  2706:             } elsif ($check eq 'unamemap') {
                   2707:                 $response=&reply('unamemaprules:'.&escape($udom),
                   2708:                                  $homeserver); 
1.923     raeburn  2709:             } else {
                   2710:                 $response=&reply('instuserrules:'.&escape($udom),
                   2711:                                  $homeserver);
                   2712:             }
1.912     raeburn  2713:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  2714:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  2715:                 ($response ne 'no_such_host')) {
                   2716:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   2717:                 my @pairs=split(/\&/,$hashitems);
                   2718:                 foreach my $item (@pairs) {
                   2719:                     my ($key,$value)=split(/=/,$item,2);
                   2720:                     $key = &unescape($key);
                   2721:                     next if ($key =~ /^error: 2 /);
                   2722:                     $ruleshash{$key}=&thaw_unescape($value);
                   2723:                 }
                   2724:                 my @esc_order = split(/\&/,$orderitems);
                   2725:                 foreach my $item (@esc_order) {
                   2726:                     push(@ruleorder,&unescape($item));
                   2727:                 }
                   2728:             }
                   2729:         }
                   2730:     }
                   2731:     return (\%ruleshash,\@ruleorder);
                   2732: }
                   2733: 
1.976     raeburn  2734: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  2735: 
                   2736: sub get_domain_defaults {
1.1240    raeburn  2737:     my ($domain,$ignore_cache) = @_;
1.1242    raeburn  2738:     return if (($domain eq '') || ($domain eq 'public'));
1.943     raeburn  2739:     my $cachetime = 60*60*24;
1.1240    raeburn  2740:     unless ($ignore_cache) {
                   2741:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   2742:         if (defined($cached)) {
                   2743:             if (ref($result) eq 'HASH') {
                   2744:                 return %{$result};
                   2745:             }
1.943     raeburn  2746:         }
                   2747:     }
                   2748:     my %domdefaults;
                   2749:     my %domconfig =
1.1494    raeburn  2750:          &get_dom('configuration',['defaults','quotas',
1.1047    raeburn  2751:                                   'requestcourses','inststatus',
1.1183    raeburn  2752:                                   'coursedefaults','usersessions',
1.1258    raeburn  2753:                                   'requestauthor','selfenrollment',
1.1320    raeburn  2754:                                   'coursecategories','ssl','autoenroll',
1.1504    raeburn  2755:                                   'trust','helpsettings','wafproxy',
1.1508    raeburn  2756:                                   'ltisec','toolsec','domexttool',
1.1510  ! raeburn  2757:                                   'exttool'],$domain);
1.1305    raeburn  2758:     my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943     raeburn  2759:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   2760:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   2761:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   2762:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  2763:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  2764:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  2765:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1492    raeburn  2766:         $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
                   2767:         $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
1.1340    raeburn  2768:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
                   2769:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
                   2770:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.1484    raeburn  2771:         $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
1.943     raeburn  2772:     } else {
                   2773:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   2774:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   2775:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   2776:     }
1.976     raeburn  2777:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   2778:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   2779:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   2780:         } else {
                   2781:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229    raeburn  2782:         }
1.1177    raeburn  2783:         my @usertools = ('aboutme','blog','webdav','portfolio');
1.976     raeburn  2784:         foreach my $item (@usertools) {
                   2785:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   2786:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   2787:             }
                   2788:         }
1.1229    raeburn  2789:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   2790:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
                   2791:         }
1.976     raeburn  2792:     }
1.985     raeburn  2793:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305    raeburn  2794:         foreach my $item ('official','unofficial','community','textbook','placement') {
1.985     raeburn  2795:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2796:         }
                   2797:     }
1.1183    raeburn  2798:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2799:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2800:     }
1.989     raeburn  2801:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256    raeburn  2802:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989     raeburn  2803:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2804:         }
                   2805:     }
1.1047    raeburn  2806:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230    raeburn  2807:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277    raeburn  2808:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
1.1476    raeburn  2809:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
1.1277    raeburn  2810:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
                   2811:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   2812:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
                   2813:         }
1.1254    raeburn  2814:         foreach my $type (@coursetypes) {
                   2815:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2816:                 unless ($type eq 'community') {
                   2817:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
                   2818:                 }
                   2819:             }
                   2820:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   2821:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   2822:             }
1.1277    raeburn  2823:             if ($domdefaults{'postsubmit'} eq 'on') {
                   2824:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   2825:                     $domdefaults{$type.'postsubtimeout'} = 
                   2826:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
                   2827:                 }
                   2828:             }
1.1508    raeburn  2829:             if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
                   2830:                 $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
                   2831:             } else {
                   2832:                 $domdefaults{$type.'domexttool'} = 1;
                   2833:             }
                   2834:             if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
                   2835:                 $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
                   2836:             } else {
                   2837:                 $domdefaults{$type.'exttool'} = 0;
                   2838:             }
1.1230    raeburn  2839:         }
1.1286    raeburn  2840:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   2841:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   2842:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
                   2843:                 if (@clonecodes) {
                   2844:                     $domdefaults{'canclone'} = join('+',@clonecodes);
                   2845:                 }
                   2846:             }
                   2847:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
                   2848:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
                   2849:         }
1.1356    raeburn  2850:         if ($domconfig{'coursedefaults'}{'texengine'}) {
                   2851:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
1.1480    raeburn  2852:         }
                   2853:         if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
                   2854:             $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
                   2855:         }
1.1047    raeburn  2856:     }
1.1073    raeburn  2857:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2858:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2859:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2860:         }
                   2861:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2862:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2863:         }
1.1279    raeburn  2864:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   2865:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
                   2866:         }
1.1440    raeburn  2867:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1439    raeburn  2868:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
                   2869:         }
1.1073    raeburn  2870:     }
1.1254    raeburn  2871:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                   2872:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   2873:             my @settings = ('types','registered','enroll_dates','access_dates','section',
                   2874:                             'approval','limit');
                   2875:             foreach my $type (@coursetypes) {
                   2876:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   2877:                     my @mgrdc = ();
                   2878:                     foreach my $item (@settings) {
                   2879:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
                   2880:                             push(@mgrdc,$item);
                   2881:                         }
                   2882:                     }
                   2883:                     if (@mgrdc) {
                   2884:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   2885:                     }
                   2886:                 }
                   2887:             }
                   2888:         }
                   2889:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   2890:             foreach my $type (@coursetypes) {
                   2891:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   2892:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
                   2893:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
                   2894:                     }
                   2895:                 }
                   2896:             }
                   2897:         }
                   2898:     }
1.1258    raeburn  2899:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2900:         $domdefaults{'catauth'} = 'std';
                   2901:         $domdefaults{'catunauth'} = 'std';
1.1413    raeburn  2902:         if ($domconfig{'coursecategories'}{'auth'}) {
1.1258    raeburn  2903:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   2904:         }
                   2905:         if ($domconfig{'coursecategories'}{'unauth'}) {
                   2906:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   2907:         }
                   2908:     }
1.1315    raeburn  2909:     if (ref($domconfig{'ssl'}) eq 'HASH') {
                   2910:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
                   2911:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
                   2912:         }
1.1338    raeburn  2913:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
                   2914:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
                   2915:         }
                   2916:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
                   2917:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315    raeburn  2918:         }
                   2919:     }
1.1320    raeburn  2920:     if (ref($domconfig{'trust'}) eq 'HASH') {
                   2921:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
                   2922:         foreach my $prefix (@prefixes) {
                   2923:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
                   2924:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
                   2925:             }
                   2926:         }
                   2927:     }
1.1314    raeburn  2928:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   2929:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
1.1477    raeburn  2930:         $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
1.1314    raeburn  2931:     }
1.1332    raeburn  2932:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   2933:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
                   2934:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   2935:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
                   2936:         }
                   2937:     }
1.1434    raeburn  2938:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
1.1449    raeburn  2939:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
1.1434    raeburn  2940:             if ($domconfig{'wafproxy'}{$item}) {
                   2941:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
                   2942:             }
                   2943:         }
1.1482    raeburn  2944:     }
                   2945:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
                   2946:         if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
                   2947:             $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
                   2948:             $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
                   2949:             $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
                   2950:         }
                   2951:         if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
                   2952:             if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
1.1504    raeburn  2953:                 $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
                   2954:             }
                   2955:         }
                   2956:     }
                   2957:     if (ref($domconfig{'toolsec'}) eq 'HASH') {
                   2958:         if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
                   2959:             $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
                   2960:             $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
                   2961:         }
                   2962:         if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
                   2963:             if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
                   2964:                 $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
1.1482    raeburn  2965:             }
                   2966:         }
                   2967:     }
1.1219    raeburn  2968:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  2969:     return %domdefaults;
                   2970: }
                   2971: 
1.1412    raeburn  2972: sub get_dom_cats {
                   2973:     my ($dom) = @_;
                   2974:     return unless (&domain($dom));
                   2975:     my ($cats,$cached)=&is_cached_new('cats',$dom);
                   2976:     unless (defined($cached)) {
                   2977:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
                   2978:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2979:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
                   2980:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
                   2981:             } else {
                   2982:                 $cats = {};
                   2983:             }
                   2984:         } else {
                   2985:             $cats = {};
                   2986:         }
1.1494    raeburn  2987:         &do_cache_new('cats',$dom,$cats,3600);
1.1412    raeburn  2988:     }
1.1413    raeburn  2989:     return $cats;
                   2990: }
                   2991: 
                   2992: sub get_dom_instcats {
                   2993:     my ($dom) = @_;
                   2994:     return unless (&domain($dom));
                   2995:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
                   2996:     unless (defined($cached)) {
                   2997:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
                   2998:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
                   2999:         if ($totcodes > 0) {
                   3000:             my $caller = 'global';
                   3001:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
                   3002:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
                   3003:                 $instcats = {
1.1503    raeburn  3004:                                 totcodes => $totcodes,
1.1413    raeburn  3005:                                 codes => \%codes,
                   3006:                                 codetitles => \@codetitles,
                   3007:                                 cat_titles => \%cat_titles,
                   3008:                                 cat_order => \%cat_order,
                   3009:                             };
                   3010:                 &do_cache_new('instcats',$dom,$instcats,3600);
                   3011:             }
                   3012:         }
                   3013:     }
                   3014:     return $instcats;
                   3015: }
                   3016: 
                   3017: sub retrieve_instcodes {
                   3018:     my ($coursecodes,$dom) = @_;
                   3019:     my $totcodes;
                   3020:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
                   3021:     foreach my $course (keys(%courses)) {
                   3022:         if (ref($courses{$course}) eq 'HASH') {
                   3023:             if ($courses{$course}{'inst_code'} ne '') {
                   3024:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
                   3025:                 $totcodes ++;
                   3026:             }
                   3027:         }
                   3028:     }
                   3029:     return $totcodes;
1.1412    raeburn  3030: }
                   3031: 
1.1311    raeburn  3032: sub course_portal_url {
1.1451    raeburn  3033:     my ($cnum,$cdom,$r) = @_;
1.1311    raeburn  3034:     my $chome = &homeserver($cnum,$cdom);
                   3035:     my $hostname = &hostname($chome);
                   3036:     my $protocol = $protocol{$chome};
                   3037:     $protocol = 'http' if ($protocol ne 'https');
                   3038:     my %domdefaults = &get_domain_defaults($cdom);
                   3039:     my $firsturl;
                   3040:     if ($domdefaults{'portal_def'}) {
                   3041:         $firsturl = $domdefaults{'portal_def'};
                   3042:     } else {
1.1494    raeburn  3043:         my $alias = &use_proxy_alias($r,$chome);
1.1451    raeburn  3044:         $hostname = $alias if ($alias ne '');
1.1311    raeburn  3045:         $firsturl = $protocol.'://'.$hostname;
                   3046:     }
                   3047:     return $firsturl;
                   3048: }
                   3049: 
1.1492    raeburn  3050: sub url_prefix {
                   3051:     my ($r,$dom,$home,$context) = @_;
                   3052:     my $prefix;
                   3053:     my %domdefs = &get_domain_defaults($dom);
                   3054:     if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
                   3055:         if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
                   3056:             $prefix = $1;
                   3057:         }
                   3058:     }
                   3059:     if ($prefix eq '') {
                   3060:         my $hostname = &hostname($home);
                   3061:         my $protocol = $protocol{$home};
                   3062:         $protocol = 'http' if ($protocol{$home} ne 'https');
                   3063:         my $alias = &use_proxy_alias($r,$home);
                   3064:         $hostname = $alias if ($alias ne '');
                   3065:         $prefix = $protocol.'://'.$hostname;
                   3066:     }
                   3067:     return $prefix;
                   3068: }
                   3069: 
1.1407    raeburn  3070: # --------------------------------------------- Get domain config for passwords
                   3071: 
                   3072: sub get_passwdconf {
                   3073:     my ($dom) = @_;
                   3074:     my (%passwdconf,$gotconf,$lookup);
                   3075:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
                   3076:     if (defined($cached)) {
                   3077:         if (ref($result) eq 'HASH') {
                   3078:             %passwdconf = %{$result};
                   3079:             $gotconf = 1;
                   3080:         }
                   3081:     }
                   3082:     unless ($gotconf) {
                   3083:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
                   3084:         if (ref($domconfig{'passwords'}) eq 'HASH') {
                   3085:             %passwdconf = %{$domconfig{'passwords'}};
                   3086:         }
                   3087:         my $cachetime = 24*60*60;
                   3088:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
                   3089:     }
                   3090:     return %passwdconf;
                   3091: }
                   3092: 
1.344     www      3093: # --------------------------------------------------- Assign a key to a student
                   3094: 
                   3095: sub assign_access_key {
1.364     www      3096: #
                   3097: # a valid key looks like uname:udom#comments
                   3098: # comments are being appended
                   3099: #
1.498     www      3100:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   3101:     $kdom=
1.620     albertel 3102:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      3103:     $knum=
1.620     albertel 3104:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      3105:     $cdom=
1.620     albertel 3106:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3107:     $cnum=
1.620     albertel 3108:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   3109:     $udom=$env{'user.name'} unless (defined($udom));
                   3110:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      3111:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      3112:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 3113:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      3114:                                                   # assigned to this person
                   3115:                                                   # - this should not happen,
1.345     www      3116:                                                   # unless something went wrong
                   3117:                                                   # the first time around
                   3118: # ready to assign
1.364     www      3119:         $logentry=$1.'; '.$logentry;
1.496     www      3120:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      3121:                                                  $kdom,$knum) eq 'ok') {
1.345     www      3122: # key now belongs to user
1.346     www      3123: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      3124:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  3125:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      3126:                 return 'ok';
                   3127:             } else {
                   3128:                 return 
                   3129:   'error: Count not permanently assign key, will need to be re-entered later.';
                   3130: 	    }
                   3131:         } else {
                   3132:             return 'error: Could not assign key, try again later.';
                   3133:         }
1.364     www      3134:     } elsif (!$existing{$ckey}) {
1.345     www      3135: # the key does not exist
                   3136: 	return 'error: The key does not exist';
                   3137:     } else {
                   3138: # the key is somebody else's
                   3139: 	return 'error: The key is already in use';
                   3140:     }
1.344     www      3141: }
                   3142: 
1.364     www      3143: # ------------------------------------------ put an additional comment on a key
                   3144: 
                   3145: sub comment_access_key {
                   3146: #
                   3147: # a valid key looks like uname:udom#comments
                   3148: # comments are being appended
                   3149: #
                   3150:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   3151:     $cdom=
1.620     albertel 3152:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      3153:     $cnum=
1.620     albertel 3154:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      3155:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   3156:     if ($existing{$ckey}) {
                   3157:         $existing{$ckey}.='; '.$logentry;
                   3158: # ready to assign
1.367     www      3159:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      3160:                                                  $cdom,$cnum) eq 'ok') {
                   3161: 	    return 'ok';
                   3162:         } else {
                   3163: 	    return 'error: Count not store comment.';
                   3164:         }
                   3165:     } else {
                   3166: # the key does not exist
                   3167: 	return 'error: The key does not exist';
                   3168:     }
                   3169: }
                   3170: 
1.344     www      3171: # ------------------------------------------------------ Generate a set of keys
                   3172: 
                   3173: sub generate_access_keys {
1.364     www      3174:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      3175:     $cdom=
1.620     albertel 3176:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3177:     $cnum=
1.620     albertel 3178:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      3179:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      3180:     unless (($cdom) && ($cnum)) { return 0; }
                   3181:     if ($number>10000) { return 0; }
                   3182:     sleep(2); # make sure don't get same seed twice
                   3183:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   3184:     my $total=0;
                   3185:     for (my $i=1;$i<=$number;$i++) {
                   3186:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   3187:                   sprintf("%lx",int(100000*rand)).'-'.
                   3188:                   sprintf("%lx",int(100000*rand));
                   3189:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   3190:        $newkey=~s/0/h/g; # and also 0 and O
                   3191:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   3192:        if ($existing{$newkey}) {
                   3193:            $i--;
                   3194:        } else {
1.364     www      3195: 	  if (&put('accesskeys',
                   3196:               { $newkey => '# generated '.localtime().
1.620     albertel 3197:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      3198:                            '; '.$logentry },
                   3199: 		   $cdom,$cnum) eq 'ok') {
1.344     www      3200:               $total++;
                   3201: 	  }
                   3202:        }
                   3203:     }
1.620     albertel 3204:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      3205:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   3206:     return $total;
                   3207: }
                   3208: 
                   3209: # ------------------------------------------------------- Validate an accesskey
                   3210: 
                   3211: sub validate_access_key {
                   3212:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   3213:     $cdom=
1.620     albertel 3214:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      3215:     $cnum=
1.620     albertel 3216:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   3217:     $udom=$env{'user.domain'} unless (defined($udom));
                   3218:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      3219:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 3220:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      3221: }
                   3222: 
                   3223: # ------------------------------------- Find the section of student in a course
1.652     albertel 3224: sub devalidate_getsection_cache {
                   3225:     my ($udom,$unam,$courseid)=@_;
                   3226:     my $hashid="$udom:$unam:$courseid";
                   3227:     &devalidate_cache_new('getsection',$hashid);
                   3228: }
1.298     matthew  3229: 
1.815     albertel 3230: sub courseid_to_courseurl {
                   3231:     my ($courseid) = @_;
                   3232:     #already url style courseid
                   3233:     return $courseid if ($courseid =~ m{^/});
                   3234: 
                   3235:     if (exists($env{'course.'.$courseid.'.num'})) {
                   3236: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   3237: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   3238: 	return "/$cdom/$cnum";
                   3239:     }
                   3240: 
1.1494    raeburn  3241:     my %courseinfo=&coursedescription($courseid);
1.815     albertel 3242:     if (exists($courseinfo{'num'})) {
                   3243: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   3244:     }
                   3245: 
                   3246:     return undef;
                   3247: }
                   3248: 
1.298     matthew  3249: sub getsection {
                   3250:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 3251:     my $cachetime=1800;
1.551     albertel 3252: 
                   3253:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 3254:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 3255:     if (defined($cached)) { return $result; }
                   3256: 
1.298     matthew  3257:     my %Pending; 
                   3258:     my %Expired;
                   3259:     #
                   3260:     # Each role can either have not started yet (pending), be active, 
                   3261:     #    or have expired.
                   3262:     #
                   3263:     # If there is an active role, we are done.
                   3264:     #
                   3265:     # If there is more than one role which has not started yet, 
                   3266:     #     choose the one which will start sooner
                   3267:     # If there is one role which has not started yet, return it.
                   3268:     #
                   3269:     # If there is more than one expired role, choose the one which ended last.
                   3270:     # If there is a role which has expired, return it.
                   3271:     #
1.815     albertel 3272:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  3273:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  3274:     foreach my $key (keys(%roleshash)) {
1.479     albertel 3275:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  3276:         my $section=$1;
                   3277:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  3278:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  3279:         my $now=time;
1.548     albertel 3280:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  3281:             $Expired{$end}=$section;
                   3282:             next;
                   3283:         }
1.548     albertel 3284:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  3285:             $Pending{$start}=$section;
                   3286:             next;
                   3287:         }
1.599     albertel 3288:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  3289:     }
                   3290:     #
                   3291:     # Presumedly there will be few matching roles from the above
                   3292:     # loop and the sorting time will be negligible.
                   3293:     if (scalar(keys(%Pending))) {
                   3294:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 3295:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  3296:     } 
                   3297:     if (scalar(keys(%Expired))) {
                   3298:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   3299:         my $time = pop(@sorted);
1.599     albertel 3300:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  3301:     }
1.599     albertel 3302:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  3303: }
1.70      www      3304: 
1.599     albertel 3305: sub save_cache {
                   3306:     &purge_remembered();
1.722     albertel 3307:     #&Apache::loncommon::validate_page();
1.620     albertel 3308:     undef(%env);
1.780     albertel 3309:     undef($env_loaded);
1.599     albertel 3310: }
1.452     albertel 3311: 
1.599     albertel 3312: my $to_remember=-1;
                   3313: my %remembered;
                   3314: my %accessed;
                   3315: my $kicks=0;
                   3316: my $hits=0;
1.849     albertel 3317: sub make_key {
                   3318:     my ($name,$id) = @_;
1.872     albertel 3319:     if (length($id) > 65 
                   3320: 	&& length(&escape($id)) > 200) {
                   3321: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   3322:     }
1.849     albertel 3323:     return &escape($name.':'.$id);
                   3324: }
                   3325: 
1.599     albertel 3326: sub devalidate_cache_new {
                   3327:     my ($name,$id,$debug) = @_;
                   3328:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319    damieng  3329:     my $remembered_id=$name.':'.$id;
1.849     albertel 3330:     $id=&make_key($name,$id);
1.599     albertel 3331:     $memcache->delete($id);
1.1319    damieng  3332:     delete($remembered{$remembered_id});
                   3333:     delete($accessed{$remembered_id});
1.599     albertel 3334: }
                   3335: 
                   3336: sub is_cached_new {
                   3337:     my ($name,$id,$debug) = @_;
1.1319    damieng  3338:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
                   3339:     if (exists($remembered{$remembered_id})) {
                   3340: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
                   3341: 	$accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3342: 	$hits++;
1.1319    damieng  3343: 	return ($remembered{$remembered_id},1);
1.599     albertel 3344:     }
1.1319    damieng  3345:     $id=&make_key($name,$id);
1.599     albertel 3346:     my $value = $memcache->get($id);
                   3347:     if (!(defined($value))) {
                   3348: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 3349: 	return (undef,undef);
1.416     albertel 3350:     }
1.599     albertel 3351:     if ($value eq '__undef__') {
                   3352: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   3353: 	$value=undef;
                   3354:     }
1.1319    damieng  3355:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3356:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   3357:     return ($value,1);
                   3358: }
                   3359: 
                   3360: sub do_cache_new {
                   3361:     my ($name,$id,$value,$time,$debug) = @_;
1.1319    damieng  3362:     my $remembered_id=$name.':'.$id;
1.849     albertel 3363:     $id=&make_key($name,$id);
1.599     albertel 3364:     my $setvalue=$value;
                   3365:     if (!defined($setvalue)) {
                   3366: 	$setvalue='__undef__';
                   3367:     }
1.623     albertel 3368:     if (!defined($time) ) {
                   3369: 	$time=600;
                   3370:     }
1.599     albertel 3371:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 3372:     my $result = $memcache->set($id,$setvalue,$time);
                   3373:     if (! $result) {
1.872     albertel 3374: 	&logthis("caching of id -> $id  failed");
1.910     albertel 3375: 	$memcache->disconnect_all();
1.872     albertel 3376:     }
1.600     albertel 3377:     # need to make a copy of $value
1.1319    damieng  3378:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3379:     return $value;
                   3380: }
                   3381: 
                   3382: sub make_room {
1.1319    damieng  3383:     my ($remembered_id,$value,$debug)=@_;
1.919     albertel 3384: 
1.1319    damieng  3385:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919     albertel 3386:                                     : $value;
1.599     albertel 3387:     if ($to_remember<0) { return; }
1.1319    damieng  3388:     $accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3389:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   3390:     my $to_kick;
                   3391:     my $max_time=0;
                   3392:     foreach my $other (keys(%accessed)) {
                   3393: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   3394: 	    $to_kick=$other;
                   3395: 	    $max_time=&tv_interval($accessed{$other});
                   3396: 	}
                   3397:     }
                   3398:     delete($remembered{$to_kick});
                   3399:     delete($accessed{$to_kick});
                   3400:     $kicks++;
                   3401:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 3402:     return;
                   3403: }
                   3404: 
1.599     albertel 3405: sub purge_remembered {
1.604     albertel 3406:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   3407:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 3408:     undef(%remembered);
                   3409:     undef(%accessed);
1.428     albertel 3410: }
1.70      www      3411: # ------------------------------------- Read an entry from a user's environment
                   3412: 
                   3413: sub userenvironment {
                   3414:     my ($udom,$unam,@what)=@_;
1.976     raeburn  3415:     my $items;
                   3416:     foreach my $item (@what) {
                   3417:         $items.=&escape($item).'&';
                   3418:     }
                   3419:     $items=~s/\&$//;
1.70      www      3420:     my %returnhash=();
1.1009    raeburn  3421:     my $uhome = &homeserver($unam,$udom);
                   3422:     unless ($uhome eq 'no_host') {
                   3423:         my @answer=split(/\&/, 
                   3424:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  3425:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   3426:             return %returnhash;
                   3427:         }
1.1009    raeburn  3428:         my $i;
                   3429:         for ($i=0;$i<=$#what;$i++) {
                   3430: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   3431:         }
1.70      www      3432:     }
                   3433:     return %returnhash;
1.1       albertel 3434: }
                   3435: 
1.617     albertel 3436: # ---------------------------------------------------------- Get a studentphoto
                   3437: sub studentphoto {
                   3438:     my ($udom,$unam,$ext) = @_;
1.1494    raeburn  3439:     my $home=&homeserver($unam,$udom);
1.706     raeburn  3440:     if (defined($env{'request.course.id'})) {
1.708     raeburn  3441:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  3442:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   3443:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   3444:             } else {
                   3445:                 my ($result,$perm_reqd)=
1.1494    raeburn  3446: 		    &auto_photo_permission($unam,$udom);
1.706     raeburn  3447:                 if ($result eq 'ok') {
                   3448:                     if (!($perm_reqd eq 'yes')) {
                   3449:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   3450:                     }
                   3451:                 }
                   3452:             }
                   3453:         }
                   3454:     } else {
                   3455:         my ($result,$perm_reqd) = 
1.1494    raeburn  3456: 	    &auto_photo_permission($unam,$udom);
1.706     raeburn  3457:         if ($result eq 'ok') {
                   3458:             if (!($perm_reqd eq 'yes')) {
                   3459:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   3460:             }
                   3461:         }
                   3462:     }
                   3463:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   3464: }
                   3465: 
                   3466: sub retrievestudentphoto {
                   3467:     my ($udom,$unam,$ext,$type) = @_;
1.1494    raeburn  3468:     my $home=&homeserver($unam,$udom);
                   3469:     my $ret=&reply("studentphoto:$udom:$unam:$ext:$type",$home);
1.706     raeburn  3470:     if ($ret eq 'ok') {
                   3471:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   3472:         if ($type eq 'thumbnail') {
                   3473:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   3474:         }
1.1494    raeburn  3475:         my $tokenurl=&tokenwrapper($url);
1.706     raeburn  3476:         return $tokenurl;
                   3477:     } else {
                   3478:         if ($type eq 'thumbnail') {
                   3479:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   3480:         } else { 
                   3481:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   3482:         }
1.617     albertel 3483:     }
                   3484: }
                   3485: 
1.263     www      3486: # -------------------------------------------------------------------- New chat
                   3487: 
                   3488: sub chatsend {
1.724     raeburn  3489:     my ($newentry,$anon,$group)=@_;
1.620     albertel 3490:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3491:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3492:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      3493:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 3494: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  3495: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      3496: }
                   3497: 
                   3498: # ------------------------------------------ Find current version of a resource
                   3499: 
                   3500: sub getversion {
                   3501:     my $fname=&clutter(shift);
1.1189    raeburn  3502:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      3503:     return &currentversion(&filelocation('',$fname));
                   3504: }
                   3505: 
                   3506: sub currentversion {
                   3507:     my $fname=shift;
                   3508:     my $author=$fname;
                   3509:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3510:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      3511:     my $home=&homeserver($uname,$udom);
1.292     www      3512:     if ($home eq 'no_host') { 
                   3513:         return -1; 
                   3514:     }
1.1112    www      3515:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      3516:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3517: 	return -1;
                   3518:     }
1.1112    www      3519:     return $answer;
1.263     www      3520: }
                   3521: 
1.1111    www      3522: #
                   3523: # Return special version number of resource if set by override, empty otherwise
                   3524: #
                   3525: sub usedversion {
                   3526:     my $fname=shift;
                   3527:     unless ($fname) { $fname=$env{'request.uri'}; }
                   3528:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   3529:     if ($urlversion) { return $urlversion; }
                   3530:     return '';
                   3531: }
                   3532: 
1.1       albertel 3533: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      3534: 
1.1       albertel 3535: sub subscribe {
                   3536:     my $fname=shift;
1.761     raeburn  3537:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 3538:     $fname=~s/[\n\r]//g;
1.1       albertel 3539:     my $author=$fname;
                   3540:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3541:     my ($udom,$uname)=split(/\//,$author);
                   3542:     my $home=homeserver($uname,$udom);
1.335     albertel 3543:     if ($home eq 'no_host') {
                   3544:         return 'not_found';
1.1       albertel 3545:     }
                   3546:     my $answer=reply("sub:$fname",$home);
1.64      www      3547:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3548: 	$answer.=' by '.$home;
                   3549:     }
1.1       albertel 3550:     return $answer;
                   3551: }
                   3552:     
1.8       www      3553: # -------------------------------------------------------------- Replicate file
                   3554: 
                   3555: sub repcopy {
                   3556:     my $filename=shift;
1.23      www      3557:     $filename=~s/\/+/\//g;
1.1142    raeburn  3558:     my $londocroot = $perlvar{'lonDocRoot'};
                   3559:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  3560:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  3561:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   3562: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 3563: 	return &repcopy_userfile($filename);
                   3564:     }
1.532     albertel 3565:     $filename=~s/[\n\r]//g;
1.8       www      3566:     my $transname="$filename.in.transfer";
1.828     www      3567: # FIXME: this should flock
1.607     raeburn  3568:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      3569:     my $remoteurl=subscribe($filename);
1.64      www      3570:     if ($remoteurl =~ /^con_lost by/) {
                   3571: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3572:            return 'unavailable';
1.8       www      3573:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 3574: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  3575: 	   return 'not_found';
1.64      www      3576:     } elsif ($remoteurl =~ /^rejected by/) {
                   3577: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3578:            return 'forbidden';
1.20      www      3579:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  3580:            return 'ok';
1.8       www      3581:     } else {
1.290     www      3582:         my $author=$filename;
                   3583:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3584:         my ($udom,$uname)=split(/\//,$author);
                   3585:         my $home=homeserver($uname,$udom);
                   3586:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      3587:            my @parts=split(/\//,$filename);
                   3588:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  3589:            if ($path ne "$londocroot/res") {
1.8       www      3590:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  3591: 	       return 'bad_request';
1.8       www      3592:            }
                   3593:            my $count;
                   3594:            for ($count=5;$count<$#parts;$count++) {
                   3595:                $path.="/$parts[$count]";
                   3596:                if ((-e $path)!=1) {
                   3597: 		   mkdir($path,0777);
                   3598:                }
                   3599:            }
                   3600:            my $request=new HTTP::Request('GET',"$remoteurl");
1.1345    raeburn  3601:            my $response;
                   3602:            if ($remoteurl =~ m{/raw/}) {
                   3603:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
                   3604:            } else {
                   3605:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
                   3606:            }
1.8       www      3607:            if ($response->is_error()) {
                   3608: 	       unlink($transname);
                   3609:                my $message=$response->status_line;
1.672     albertel 3610:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      3611:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  3612:                return 'unavailable';
1.8       www      3613:            } else {
1.16      www      3614: 	       if ($remoteurl!~/\.meta$/) {
                   3615:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1.1345    raeburn  3616:                   my $mresponse;
                   3617:                   if ($remoteurl =~ m{/raw/}) {
                   3618:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
                   3619:                   } else {
                   3620:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
                   3621:                   }
1.16      www      3622:                   if ($mresponse->is_error()) {
                   3623: 		      unlink($filename.'.meta');
                   3624:                       &logthis(
1.672     albertel 3625:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      3626:                   }
                   3627: 	       }
1.8       www      3628:                rename($transname,$filename);
1.607     raeburn  3629:                return 'ok';
1.8       www      3630:            }
1.290     www      3631:        }
1.8       www      3632:     }
1.330     www      3633: }
                   3634: 
1.1422    raeburn  3635: # ------------------------------------------------- Unsubscribe from a resource
                   3636: 
                   3637: sub unsubscribe {
                   3638:     my ($fname) = @_;
                   3639:     my $answer;
                   3640:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
                   3641:     $fname=~s/[\n\r]//g;
                   3642:     my $author=$fname;
                   3643:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3644:     my ($udom,$uname)=split(/\//,$author);
                   3645:     my $home=homeserver($uname,$udom);
                   3646:     if ($home eq 'no_host') {
                   3647:         $answer = 'no_host';
                   3648:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
                   3649:         $answer = 'home';
                   3650:     } else {
                   3651:         my $defdom = $perlvar{'lonDefDomain'};
                   3652:         if (&will_trust('content',$defdom,$udom)) {
                   3653:             $answer = reply("unsub:$fname",$home);
                   3654:         } else {
                   3655:             $answer = 'untrusted';
                   3656:         }
                   3657:     }
                   3658:     return $answer;
                   3659: }
                   3660: 
1.330     www      3661: # ------------------------------------------------ Get server side include body
                   3662: sub ssi_body {
1.381     albertel 3663:     my ($filelink,%form)=@_;
1.606     matthew  3664:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   3665:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   3666:     }
1.953     www      3667:     my $output='';
                   3668:     my $response;
1.980     raeburn  3669:     if ($filelink=~/^https?\:/) {
1.954     raeburn  3670:        ($output,$response)=&externalssi($filelink);
1.953     www      3671:     } else {
1.1004    droeschl 3672:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   3673:        $filelink .= 'inhibitmenu=yes';
1.953     www      3674:        ($output,$response)=&ssi($filelink,%form);
                   3675:     }
1.778     albertel 3676:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 3677:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 3678:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      3679:     if (wantarray) {
                   3680:         return ($output, $response);
                   3681:     } else {
                   3682:         return $output;
                   3683:     }
1.8       www      3684: }
                   3685: 
1.15      www      3686: # --------------------------------------------------------- Server Side Include
                   3687: 
1.782     albertel 3688: sub absolute_url {
1.1447    raeburn  3689:     my ($host_name,$unalias,$keep_proto) = @_;
1.782     albertel 3690:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   3691:     if ($host_name eq '') {
                   3692: 	$host_name = $ENV{'SERVER_NAME'};
                   3693:     }
1.1447    raeburn  3694:     if ($unalias) {
                   3695:         my $alias = &get_proxy_alias();
                   3696:         if ($alias eq $host_name) {
                   3697:             my $lonhost = $perlvar{'lonHostID'};
                   3698:             my $hostname = &hostname($lonhost);
                   3699:             my $lcproto; 
                   3700:             if (($keep_proto) || ($hostname eq '')) {
                   3701:                 $lcproto = $protocol;
                   3702:             } else {
                   3703:                 $lcproto = $protocol{$lonhost};
                   3704:                 $lcproto = 'http' if ($lcproto ne 'https');
                   3705:                 $lcproto .= '://';
                   3706:             }
                   3707:             unless ($hostname eq '') {
                   3708:                 return $lcproto.$hostname;
                   3709:             }
                   3710:         }
                   3711:     }
1.782     albertel 3712:     return $protocol.$host_name;
                   3713: }
                   3714: 
1.942     foxr     3715: #
                   3716: #   Server side include.
                   3717: # Parameters:
                   3718: #  fn     Possibly encrypted resource name/id.
                   3719: #  form   Hash that describes how the rendering should be done
                   3720: #         and other things.
1.944     foxr     3721: # Returns:
1.950     raeburn  3722: #   Scalar context: The content of the response.
                   3723: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     3724: #     
1.15      www      3725: sub ssi {
                   3726: 
1.944     foxr     3727:     my ($fn,%form)=@_;
1.1447    raeburn  3728:     my ($host,$request,$response);
                   3729:     $host = &absolute_url('',1);
1.711     albertel 3730: 
                   3731:     $form{'no_update_last_known'}=1;
1.895     albertel 3732:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      3733:     if (%form) {
1.1447    raeburn  3734:       $request=new HTTP::Request('POST',$host.$fn);
1.1272    droeschl 3735:       $request->content(join('&',map { 
                   3736:             my $name = escape($_);
                   3737:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
                   3738:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
                   3739:             : &escape($form{$_}) );    
                   3740:         } keys(%form)));
1.23      www      3741:     } else {
1.1447    raeburn  3742:       $request=new HTTP::Request('GET',$host.$fn);
1.23      www      3743:     }
                   3744: 
1.15      www      3745:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1345    raeburn  3746:     my $lonhost = $perlvar{'lonHostID'};
1.1385    raeburn  3747:     my $islocal;
                   3748:     if (($env{'request.course.id'}) &&
                   3749:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
                   3750:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
                   3751:         ($form{'grade_symb'} ne '') &&
1.1494    raeburn  3752:         (&allowed('mgr',$env{'request.course.id'}.
                   3753:                         ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
1.1385    raeburn  3754:         $islocal = 1;
                   3755:     }
1.1447    raeburn  3756:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
                   3757:                                              '','','',$islocal);
1.1182    foxr     3758: 
1.944     foxr     3759:     if (wantarray) {
1.1345    raeburn  3760: 	return ($response->content, $response);
1.944     foxr     3761:     } else {
1.1345    raeburn  3762: 	return $response->content;
1.942     foxr     3763:     }
1.324     www      3764: }
                   3765: 
                   3766: sub externalssi {
                   3767:     my ($url)=@_;
                   3768:     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn  3769:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
1.954     raeburn  3770:     if (wantarray) {
                   3771:         return ($response->content, $response);
                   3772:     } else {
                   3773:         return $response->content;
                   3774:     }
1.15      www      3775: }
1.254     www      3776: 
1.1354    raeburn  3777: 
                   3778: # If the local copy of a replicated resource is outdated, trigger a  
                   3779: # connection from the homeserver to flush the delayed queue. If no update 
                   3780: # happens, remove local copies of outdated resource (and corresponding
                   3781: # metadata file).
                   3782: 
1.1352    raeburn  3783: sub remove_stale_resfile {
                   3784:     my ($url) = @_;
1.1354    raeburn  3785:     my $removed;
1.1352    raeburn  3786:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
                   3787:         my $audom = $1;
                   3788:         my $auname = $2;
1.1353    raeburn  3789:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
1.1352    raeburn  3790:             my $homeserver = &homeserver($auname,$audom);
                   3791:             unless (($homeserver eq 'no_host') ||
                   3792:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
                   3793:                 my $fname = &filelocation('',$url);
                   3794:                 if (-e $fname) {
                   3795:                     my $hostname = &hostname($homeserver);
                   3796:                     if ($hostname) {
1.1397    raeburn  3797:                         my $protocol = $protocol{$homeserver};
                   3798:                         $protocol = 'http' if ($protocol ne 'https');
1.1352    raeburn  3799:                         my $uri = &declutter($url);
                   3800:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
                   3801:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
                   3802:                         if ($response->is_success()) {
                   3803:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
                   3804:                             my $locmodtime = (stat($fname))[9];
                   3805:                             if ($locmodtime < $remmodtime) {
1.1354    raeburn  3806:                                 my $stale;
                   3807:                                 my $answer = &reply('pong',$homeserver);
                   3808:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
                   3809:                                     sleep(0.2);
                   3810:                                     $locmodtime = (stat($fname))[9];
                   3811:                                     if ($locmodtime < $remmodtime) {
                   3812:                                         my $posstransfer = $fname.'.in.transfer';
                   3813:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
                   3814:                                             $removed = 1;
                   3815:                                         } else {
                   3816:                                             $stale = 1;
                   3817:                                         }
                   3818:                                     } else {
                   3819:                                         $removed = 1;
                   3820:                                     }
                   3821:                                 } else {
                   3822:                                     $stale = 1;
                   3823:                                 }
                   3824:                                 if ($stale) {
1.1421    raeburn  3825:                                     if (unlink($fname)) {
                   3826:                                         if ($uri!~/\.meta$/) {
                   3827:                                             if (-e $fname.'.meta') {
                   3828:                                                 unlink($fname.'.meta');
                   3829:                                             }
                   3830:                                         }
1.1422    raeburn  3831:                                         my $unsubresult = &unsubscribe($fname);
                   3832:                                         unless ($unsubresult eq 'ok') {
                   3833:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
                   3834:                                         }
1.1421    raeburn  3835:                                         $removed = 1;
1.1354    raeburn  3836:                                     }
1.1352    raeburn  3837:                                 }
                   3838:                             }
                   3839:                         }
                   3840:                     }
                   3841:                 }
                   3842:             }
                   3843:         }
                   3844:     }
1.1354    raeburn  3845:     return $removed;
1.1352    raeburn  3846: }
                   3847: 
1.492     albertel 3848: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   3849: 
                   3850: sub allowuploaded {
                   3851:     my ($srcurl,$url)=@_;
                   3852:     $url=&clutter(&declutter($url));
                   3853:     my $dir=$url;
                   3854:     $dir=~s/\/[^\/]+$//;
                   3855:     my %httpref=();
                   3856:     my $httpurl=&hreflocation('',$url);
                   3857:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  3858:     &Apache::lonnet::appenv(\%httpref);
1.254     www      3859: }
1.477     raeburn  3860: 
1.1193    raeburn  3861: #
                   3862: # Determine if the current user should be able to edit a particular resource,
                   3863: # when viewing in course context.
                   3864: # (a) When viewing resource used to determine if "Edit" item is included in 
                   3865: #     Functions.
                   3866: # (b) When displaying folder contents in course editor, used to determine if
                   3867: #     "Edit" link will be displayed alongside resource.
                   3868: #
1.1196    raeburn  3869: #  input: six args -- filename (decluttered), course number, course domain,
                   3870: #                   url, symb (if registered) and group (if this is a group
                   3871: #                   item -- e.g., bulletin board, group page etc.).
                   3872: #  output: array of five scalars -- 
1.1193    raeburn  3873: #          $cfile -- url for file editing if editable on current server
                   3874: #          $home -- homeserver of resource (i.e., for author if published,
                   3875: #                                           or course if uploaded.).
                   3876: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  3877: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   3878: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  3879: #
                   3880: 
                   3881: sub can_edit_resource {
1.1194    raeburn  3882:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   3883:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   3884: #
                   3885: # For aboutme pages user can only edit his/her own.
                   3886: #
1.1199    raeburn  3887:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  3888:         my ($sdom,$sname) = ($1,$2);
                   3889:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   3890:             $home = $env{'user.home'};
                   3891:             $cfile = $resurl;
                   3892:             if ($env{'form.forceedit'}) {
                   3893:                 $forceview = 1;
                   3894:             } else {
                   3895:                 $forceedit = 1;
                   3896:             }
                   3897:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3898:         } else {
                   3899:             return;
                   3900:         }
                   3901:     }
                   3902: 
                   3903:     if ($env{'request.course.id'}) {
1.1494    raeburn  3904:         my $crsedit = &allowed('mdc',$env{'request.course.id'});
1.1194    raeburn  3905:         if ($group ne '') {
                   3906: # if this is a group homepage or group bulletin board, check group privs
                   3907:             my $allowed = 0;
1.1197    raeburn  3908:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   3909:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  3910:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  3911:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   3912:                     $allowed = 1;
                   3913:                 }
1.1197    raeburn  3914:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   3915:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   3916:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  3917:                     $allowed = 1;
                   3918:                 }
                   3919:             }
                   3920:             if ($allowed) {
                   3921:                 $home=&homeserver($cnum,$cdom);
                   3922:                 if ($env{'form.forceedit'}) {
                   3923:                     $forceview = 1;
                   3924:                 } else {
                   3925:                     $forceedit = 1;
                   3926:                 }
                   3927:                 $cfile = $resurl;
                   3928:             } else {
                   3929:                 return;
                   3930:             }
                   3931:         } else {
1.1208    raeburn  3932:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
1.1494    raeburn  3933:                 unless (&allowed('opa',$env{'request.course.id'})) {
1.1208    raeburn  3934:                     return;
                   3935:                 }
                   3936:             } elsif (!$crsedit) {
1.1507    raeburn  3937:                 if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
1.1194    raeburn  3938: #
                   3939: # No edit allowed where CC has switched to student role.
                   3940: #
1.1507    raeburn  3941:                     return;
                   3942:                 } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
                   3943:                          ($resurl =~ m{^/res/lib/templates/})) {
                   3944:                     return;
                   3945:                 }
1.1194    raeburn  3946:             }
                   3947:         }
                   3948:     }
                   3949: 
1.1193    raeburn  3950:     if ($file ne '') {
                   3951:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  3952:             if (&is_course_upload($file,$cnum,$cdom)) {
                   3953:                 $uploaded = 1;
                   3954:                 $incourse = 1;
1.1193    raeburn  3955:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   3956:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  3957:                     if ($env{'form.forceedit'}) {
                   3958:                         $forceview = 1;
                   3959:                     } else {
                   3960:                         $forceedit = 1;
                   3961:                     }
1.1194    raeburn  3962:                 }
                   3963:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   3964:                 $incourse = 1;
                   3965:                 if ($env{'form.forceedit'}) {
                   3966:                     $forceview = 1;
                   3967:                 } else {
                   3968:                     $forceedit = 1;
                   3969:                 }
                   3970:                 $cfile = $resurl;
1.1507    raeburn  3971:             } elsif (($resurl ne '') && (&is_on_map($resurl))) {
1.1194    raeburn  3972:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   3973:                     $incourse = 1;
                   3974:                     if ($env{'form.forceedit'}) {
                   3975:                         $forceview = 1;
                   3976:                     } else {
                   3977:                         $forceedit = 1;
                   3978:                     }
                   3979:                     $cfile = $resurl;
1.1199    raeburn  3980:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  3981:                     $incourse = 1;
                   3982:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  3983:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  3984:                     $incourse = 1;
1.1199    raeburn  3985:                     if ($env{'form.forceedit'}) {
                   3986:                         $forceview = 1;
                   3987:                     } else {
                   3988:                         $forceedit = 1;
                   3989:                     }
                   3990:                     $cfile = $resurl;
1.1419    raeburn  3991:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
                   3992:                     my ($map,$id,$res) = &decode_symb($symb);
                   3993:                     if ($map =~ /\.page$/) {
                   3994:                         $incourse = 1;
                   3995:                         if ($env{'form.forceedit'}) {
                   3996:                             $forceview = 1;
                   3997:                             $cfile = $map;
                   3998:                         } else {
                   3999:                             $forceedit = 1;
                   4000:                             $cfile =  '/adm/wrapper'.$resurl;
                   4001:                         }
                   4002:                     }
1.1343    raeburn  4003:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  4004:                     $incourse = 1;
                   4005:                     if ($env{'form.forceedit'}) {
                   4006:                         $forceview = 1;
                   4007:                     } else {
                   4008:                         $forceedit = 1;
                   4009:                     }
                   4010:                     $cfile = $resurl;
1.1208    raeburn  4011:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   4012:                     $incourse = 1;
                   4013:                     if ($env{'form.forceedit'}) {
                   4014:                         $forceview = 1;
                   4015:                     } else {
                   4016:                         $forceedit = 1;
                   4017:                     }
                   4018:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  4019:                 }
                   4020:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   4021:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   4022:                 if (&is_on_map($template)) { 
                   4023:                     $incourse = 1;
                   4024:                     $forceview = 1;
                   4025:                     $cfile = $template;
1.1193    raeburn  4026:                 }
1.1199    raeburn  4027:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1418    raeburn  4028:                 $incourse = 1;
                   4029:                 if ($env{'form.forceedit'}) {
                   4030:                     $forceview = 1;
                   4031:                 } else {
                   4032:                     $forceedit = 1;
                   4033:                 }
                   4034:                 $cfile = $resurl;
1.1343    raeburn  4035:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298    raeburn  4036:                 $incourse = 1;
                   4037:                 if ($env{'form.forceedit'}) {
                   4038:                     $forceview = 1;
                   4039:                 } else {
                   4040:                     $forceedit = 1;
                   4041:                 }
                   4042:                 $cfile = $resurl;
1.1199    raeburn  4043:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   4044:                 $incourse = 1;
                   4045:                 $forceview = 1;
                   4046:                 if ($symb) {
                   4047:                     my ($map,$id,$res)=&decode_symb($symb);
                   4048:                     $env{'request.symb'} = $symb;
                   4049:                     $cfile = &clutter($res);
                   4050:                 } else {
                   4051:                     $cfile = $env{'form.suppurl'};
1.1298    raeburn  4052:                     my $escfile = &unescape($cfile);
1.1343    raeburn  4053:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  4054:                         $cfile = '/adm/wrapper'.$escfile;
                   4055:                     } else {
                   4056:                         $escfile =~ s{^http://}{};
                   4057:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
                   4058:                     }
1.1199    raeburn  4059:                 }
1.1234    raeburn  4060:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   4061:                 if ($env{'form.forceedit'}) {
                   4062:                     $forceview = 1;
                   4063:                 } else {
                   4064:                     $forceedit = 1;
                   4065:                 }
                   4066:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193    raeburn  4067:             }
                   4068:         }
1.1194    raeburn  4069:         if ($uploaded || $incourse) {
                   4070:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  4071:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  4072:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   4073:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   4074:             # Check that the user has permission to edit this resource
                   4075:             my $setpriv = 1;
                   4076:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   4077:             if (defined($cfudom)) {
                   4078:                 $home=&homeserver($cfuname,$cfudom);
                   4079:                 $cfile=$file;
                   4080:             }
                   4081:         }
1.1194    raeburn  4082:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   4083:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  4084:             my @ids=&current_machine_ids();
                   4085:             unless (grep(/^\Q$home\E$/,@ids)) {
                   4086:                 $switchserver=1;
                   4087:             }
                   4088:         }
                   4089:     }
1.1194    raeburn  4090:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  4091: }
                   4092: 
                   4093: sub is_course_upload {
                   4094:     my ($file,$cnum,$cdom) = @_;
                   4095:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   4096:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  4097:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   4098:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  4099:         return 1;
                   4100:     }
                   4101:     return;
                   4102: }
                   4103: 
1.1194    raeburn  4104: sub in_course {
1.1195    raeburn  4105:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   4106:     if ($hideprivileged) {
                   4107:         my $skipuser;
1.1219    raeburn  4108:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   4109:         my @possdoms = ($cdom);  
                   4110:         if ($coursehash{'checkforpriv'}) { 
                   4111:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   4112:         }
                   4113:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  4114:             $skipuser = 1;
                   4115:             if ($coursehash{'nothideprivileged'}) {
                   4116:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   4117:                     my $user;
                   4118:                     if ($item =~ /:/) {
                   4119:                         $user = $item;
                   4120:                     } else {
                   4121:                         $user = join(':',split(/[\@]/,$item));
                   4122:                     }
                   4123:                     if ($user eq $uname.':'.$udom) {
                   4124:                         undef($skipuser);
                   4125:                         last;
                   4126:                     }
                   4127:                 }
                   4128:             }
                   4129:             if ($skipuser) {
                   4130:                 return 0;
                   4131:             }
                   4132:         }
                   4133:     }
1.1194    raeburn  4134:     $type ||= 'any';
                   4135:     if (!defined($cdom) || !defined($cnum)) {
                   4136:         my $cid  = $env{'request.course.id'};
                   4137:         $cdom = $env{'course.'.$cid.'.domain'};
                   4138:         $cnum = $env{'course.'.$cid.'.num'};
                   4139:     }
                   4140:     my $typesref;
1.1195    raeburn  4141:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  4142:         $typesref = ['active','previous','future'];
                   4143:     } elsif ($type eq 'previous' || $type eq 'future') {
                   4144:         $typesref = [$type];
                   4145:     }
                   4146:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   4147:                               $typesref,undef,[$cdom]);
                   4148:     my ($tmp) = keys(%roles);
                   4149:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   4150:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   4151:     if (@course_roles > 0) {
                   4152:         return 1;
                   4153:     }
                   4154:     return 0;
                   4155: }
                   4156: 
1.478     albertel 4157: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 4158: # input: action, courseID, current domain, intended
1.637     raeburn  4159: #        path to file, source of file, instruction to parse file for objects,
                   4160: #        ref to hash for embedded objects,
                   4161: #        ref to hash for codebase of java objects.
1.1095    raeburn  4162: #        reference to scalar to accommodate mime type determined
                   4163: #          from File::MMagic if $parser = parse.
1.637     raeburn  4164: #
1.485     raeburn  4165: # output: url to file (if action was uploaddoc), 
                   4166: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  4167: #
1.478     albertel 4168: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   4169: # course.
1.477     raeburn  4170: #
1.478     albertel 4171: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4172: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   4173: #          course's home server.
1.477     raeburn  4174: #
1.478     albertel 4175: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   4176: #          be copied from $source (current location) to 
                   4177: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4178: #         and will then be copied to
                   4179: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   4180: #         course's home server.
1.485     raeburn  4181: #
1.481     raeburn  4182: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 4183: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  4184: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   4185: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   4186: #         in course's home server.
1.637     raeburn  4187: #
1.477     raeburn  4188: 
                   4189: sub process_coursefile {
1.1095    raeburn  4190:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   4191:         $mimetype)=@_;
1.477     raeburn  4192:     my $fetchresult;
1.638     albertel 4193:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  4194:     if ($action eq 'propagate') {
1.638     albertel 4195:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   4196: 			     $home);
1.481     raeburn  4197:     } else {
1.477     raeburn  4198:         my $fpath = '';
                   4199:         my $fname = $file;
1.478     albertel 4200:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  4201:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  4202:         my $filepath = &build_filepath($fpath);
1.481     raeburn  4203:         if ($action eq 'copy') {
                   4204:             if ($source eq '') {
                   4205:                 $fetchresult = 'no source file';
                   4206:                 return $fetchresult;
                   4207:             } else {
                   4208:                 my $destination = $filepath.'/'.$fname;
                   4209:                 rename($source,$destination);
                   4210:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4211:                                  $home);
1.481     raeburn  4212:             }
                   4213:         } elsif ($action eq 'uploaddoc') {
1.1359    raeburn  4214:             open(my $fh,'>',$filepath.'/'.$fname);
1.620     albertel 4215:             print $fh $env{'form.'.$source};
1.481     raeburn  4216:             close($fh);
1.637     raeburn  4217:             if ($parser eq 'parse') {
1.1024    raeburn  4218:                 my $mm = new File::MMagic;
1.1095    raeburn  4219:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   4220:                 if ($type eq 'text/html') {
1.1024    raeburn  4221:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   4222:                     unless ($parse_result eq 'ok') {
                   4223:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   4224:                     }
1.637     raeburn  4225:                 }
1.1095    raeburn  4226:                 if (ref($mimetype)) {
                   4227:                     $$mimetype = $type;
                   4228:                 } 
1.637     raeburn  4229:             }
1.477     raeburn  4230:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4231:                                  $home);
1.481     raeburn  4232:             if ($fetchresult eq 'ok') {
                   4233:                 return '/uploaded/'.$fpath.'/'.$fname;
                   4234:             } else {
                   4235:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4236:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  4237:                 return '/adm/notfound.html';
                   4238:             }
1.477     raeburn  4239:         }
                   4240:     }
1.485     raeburn  4241:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  4242:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 4243:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  4244:     }
                   4245:     return $fetchresult;
                   4246: }
                   4247: 
1.637     raeburn  4248: sub build_filepath {
                   4249:     my ($fpath) = @_;
                   4250:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   4251:     unless ($fpath eq '') {
                   4252:         my @parts=split('/',$fpath);
                   4253:         foreach my $part (@parts) {
                   4254:             $filepath.= '/'.$part;
                   4255:             if ((-e $filepath)!=1) {
                   4256:                 mkdir($filepath,0777);
                   4257:             }
                   4258:         }
                   4259:     }
                   4260:     return $filepath;
                   4261: }
                   4262: 
                   4263: sub store_edited_file {
1.638     albertel 4264:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  4265:     my $file = $primary_url;
                   4266:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   4267:     my $fpath = '';
                   4268:     my $fname = $file;
                   4269:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   4270:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   4271:     my $filepath = &build_filepath($fpath);
1.1359    raeburn  4272:     open(my $fh,'>',$filepath.'/'.$fname);
1.637     raeburn  4273:     print $fh $content;
                   4274:     close($fh);
1.638     albertel 4275:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  4276:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 4277: 			  $home);
1.637     raeburn  4278:     if ($$fetchresult eq 'ok') {
                   4279:         return '/uploaded/'.$fpath.'/'.$fname;
                   4280:     } else {
1.638     albertel 4281:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   4282: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  4283:         return '/adm/notfound.html';
                   4284:     }
                   4285: }
                   4286: 
1.531     albertel 4287: sub clean_filename {
1.831     albertel 4288:     my ($fname,$args)=@_;
1.315     www      4289: # Replace Windows backslashes by forward slashes
1.257     www      4290:     $fname=~s/\\/\//g;
1.831     albertel 4291:     if (!$args->{'keep_path'}) {
                   4292:         # Get rid of everything but the actual filename
                   4293: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   4294:     }
1.315     www      4295: # Replace spaces by underscores
                   4296:     $fname=~s/\s+/\_/g;
1.1406    raeburn  4297: # Transliterate non-ascii text to ascii
                   4298:     my $lang = &Apache::lonlocal::current_language();
                   4299:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315     www      4300: # Replace all other weird characters by nothing
1.831     albertel 4301:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 4302: # Replace all .\d. sequences with _\d. so they no longer look like version
                   4303: # numbers
                   4304:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1443    raeburn  4305: # Replace three or more adjacent underscores with one for consistency 
                   4306: # with loncfile::filename_check() so complete url can be extracted by
                   4307: # lonnet::decode_symb()
                   4308:     $fname=~s/_{3,}/_/g;
1.531     albertel 4309:     return $fname;
                   4310: }
1.1406    raeburn  4311: 
1.1051    raeburn  4312: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   4313: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   4314: # image with the same aspect ratio as the original, but with dimensions which do 
                   4315: # not exceed $resizewidth and $resizeheight.
                   4316:  
1.984     neumanie 4317: sub resizeImage {
1.1051    raeburn  4318:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   4319:     my $ima = Image::Magick->new;
                   4320:     my $resized;
                   4321:     if (-e $img_path) {
                   4322:         $ima->Read($img_path);
                   4323:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   4324:             my $width = $ima->Get('width');
                   4325:             my $height = $ima->Get('height');
                   4326:             if ($width > $resizewidth) {
                   4327: 	        my $factor = $width/$resizewidth;
                   4328:                 my $newheight = $height/$factor;
                   4329:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   4330:                 $resized = 1;
                   4331:             }
                   4332:         }
                   4333:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   4334:             my $width = $ima->Get('width');
                   4335:             my $height = $ima->Get('height');
                   4336:             if ($height > $resizeheight) {
                   4337:                 my $factor = $height/$resizeheight;
                   4338:                 my $newwidth = $width/$factor;
                   4339:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   4340:                 $resized = 1;
                   4341:             }
                   4342:         }
                   4343:         if ($resized) {
                   4344:             $ima->Write($img_path);
                   4345:         }
                   4346:     }
                   4347:     return;
1.977     amueller 4348: }
                   4349: 
1.608     albertel 4350: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 4351: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  4352: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  4353: #        $context - possible values: coursedoc, existingfile, overwrite, 
1.1504    raeburn  4354: #                                    canceloverwrite, scantron, toollogo  or ''.
1.1090    raeburn  4355: #                   if 'coursedoc': upload to the current course
                   4356: #                   if 'existingfile': write file to tmp/overwrites directory 
                   4357: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   4358: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 4359: #        $subdir - directory in userfile to store the file into
1.1401    raeburn  4360: #        $parser - instruction to parse file for objects ($parser = parse) or
                   4361: #                  if context is 'scantron', $parser is hashref of csv column mapping
                   4362: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
                   4363: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858     raeburn  4364: #        $allfiles - reference to hash for embedded objects
                   4365: #        $codebase - reference to hash for codebase of java objects
1.1504    raeburn  4366: #        $destuname - username for permanent storage of uploaded file
                   4367: #        $destudom - domain for permanaent storage of uploaded file
1.860     raeburn  4368: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   4369: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  4370: #        $resizewidth - width (pixels) to which to resize uploaded image
                   4371: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  4372: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  4373: #                    from File::MMagic.
1.858     raeburn  4374: # 
1.686     albertel 4375: # output: url of file in userspace, or error: <message> 
                   4376: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 4377: 
1.531     albertel 4378: sub userfileupload {
1.1090    raeburn  4379:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  4380:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 4381:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 4382:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 4383:     $fname=&clean_filename($fname);
1.1090    raeburn  4384:     # See if there is anything left
1.257     www      4385:     unless ($fname) { return 'error: no uploaded file'; }
1.1406    raeburn  4386:     # If filename now begins with a . prepend unix timestamp _ milliseconds
                   4387:     if ($fname =~ /^\./) {
                   4388:         my ($s,$usec) = &gettimeofday();
                   4389:         while (length($usec) < 6) {
                   4390:             $usec = '0'.$usec;
                   4391:         }
                   4392:         $fname = $s.'_'.substr($usec,0,3).$fname;
                   4393:     }
1.1090    raeburn  4394:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   4395:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   4396:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   4397:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  4398:         my $now = time;
1.1090    raeburn  4399:         my $filepath;
1.1095    raeburn  4400:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  4401:              $filepath = 'tmp/helprequests/'.$now;
                   4402:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   4403:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   4404:                          '_'.$env{'user.domain'}.'/pending';
                   4405:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   4406:             my ($docuname,$docudom);
1.1350    raeburn  4407:             if ($destudom =~ /^$match_domain$/) {
1.1090    raeburn  4408:                 $docudom = $destudom;
                   4409:             } else {
                   4410:                 $docudom = $env{'user.domain'};
                   4411:             }
1.1350    raeburn  4412:             if ($destuname =~ /^$match_username$/) {
1.1090    raeburn  4413:                 $docuname = $destuname;
                   4414:             } else {
                   4415:                 $docuname = $env{'user.name'};
                   4416:             }
                   4417:             if (exists($env{'form.group'})) {
                   4418:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4419:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4420:             }
                   4421:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   4422:             if ($context eq 'canceloverwrite') {
                   4423:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   4424:                 if (-e  $tempfile) {
                   4425:                     my @info = stat($tempfile);
                   4426:                     if ($info[9] eq $env{'form.timestamp'}) {
                   4427:                         unlink($tempfile);
                   4428:                     }
                   4429:                 }
                   4430:                 return;
1.523     raeburn  4431:             }
                   4432:         }
1.1090    raeburn  4433:         # Create the directory if not present
1.741     raeburn  4434:         my @parts=split(/\//,$filepath);
                   4435:         my $fullpath = $perlvar{'lonDaemons'};
                   4436:         for (my $i=0;$i<@parts;$i++) {
                   4437:             $fullpath .= '/'.$parts[$i];
                   4438:             if ((-e $fullpath)!=1) {
                   4439:                 mkdir($fullpath,0777);
                   4440:             }
                   4441:         }
1.1359    raeburn  4442:         open(my $fh,'>',$fullpath.'/'.$fname);
1.741     raeburn  4443:         print $fh $env{'form.'.$formname};
                   4444:         close($fh);
1.1090    raeburn  4445:         if ($context eq 'existingfile') {
                   4446:             my @info = stat($fullpath.'/'.$fname);
                   4447:             return ($fullpath.'/'.$fname,$info[9]);
                   4448:         } else {
                   4449:             return $fullpath.'/'.$fname;
                   4450:         }
1.523     raeburn  4451:     }
1.995     raeburn  4452:     if ($subdir eq 'scantron') {
                   4453:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  4454:     } else {
1.995     raeburn  4455:         $fname="$subdir/$fname";
                   4456:     }
1.1090    raeburn  4457:     if ($context eq 'coursedoc') {
1.638     albertel 4458: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4459: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  4460:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 4461:             return &finishuserfileupload($docuname,$docudom,
                   4462: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  4463: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  4464:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  4465:         } else {
1.1218    raeburn  4466:             if ($env{'form.folder'}) {
                   4467:                 $fname=$env{'form.folder'}.'/'.$fname;
                   4468:             }
1.638     albertel 4469:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   4470: 				       $fname,$formname,$parser,
1.1095    raeburn  4471: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  4472:         }
1.719     banghart 4473:     } elsif (defined($destuname)) {
                   4474:         my $docuname=$destuname;
                   4475:         my $docudom=$destudom;
1.860     raeburn  4476: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4477: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4478:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4479:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4480:     } else {
1.638     albertel 4481:         my $docuname=$env{'user.name'};
                   4482:         my $docudom=$env{'user.domain'};
1.1220    raeburn  4483:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  4484:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4485:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4486:         }
1.860     raeburn  4487: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4488: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4489:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4490:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4491:     }
1.271     www      4492: }
                   4493: 
                   4494: sub finishuserfileupload {
1.860     raeburn  4495:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  4496:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  4497:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      4498:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 4499:   
1.860     raeburn  4500:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 4501:     $file=$fname;
                   4502:     if ($fname=~m|/|) {
                   4503:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   4504: 	$path.=$fnamepath.'/';
                   4505:     }
1.259     www      4506:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      4507:     my $count;
                   4508:     for ($count=4;$count<=$#parts;$count++) {
                   4509:         $filepath.="/$parts[$count]";
                   4510:         if ((-e $filepath)!=1) {
                   4511: 	    mkdir($filepath,0777);
                   4512:         }
                   4513:     }
1.984     neumanie 4514: 
1.258     www      4515: # Save the file
                   4516:     {
1.1359    raeburn  4517: 	if (!open(FH,'>',$filepath.'/'.$file)) {
1.701     albertel 4518: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   4519: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   4520: 	    return '/adm/notfound.html';
                   4521: 	}
1.1090    raeburn  4522:         if ($context eq 'overwrite') {
1.1117    foxr     4523:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  4524:             my $target = $filepath.'/'.$file;
                   4525:             if (-e $source) {
                   4526:                 my @info = stat($source);
                   4527:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   4528:                     unless (&File::Copy::move($source,$target)) {
                   4529:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   4530:                         return "Moving from $source failed";
                   4531:                     }
                   4532:                 } else {
                   4533:                     return "Temporary file: $source had unexpected date/time for last modification";
                   4534:                 }
                   4535:             } else {
                   4536:                 return "Temporary file: $source missing";
                   4537:             }
                   4538:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 4539: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   4540: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   4541: 	    return '/adm/notfound.html';
                   4542: 	}
1.570     albertel 4543: 	close(FH);
1.1051    raeburn  4544:         if ($resizewidth && $resizeheight) {
                   4545:             my $mm = new File::MMagic;
                   4546:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   4547:             if ($mime_type =~ m{^image/}) {
                   4548: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   4549:             }  
1.977     amueller 4550: 	}
1.258     www      4551:     }
1.1152    raeburn  4552:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   4553:         if (ref($mimetype)) {
                   4554:             if ($$mimetype eq '') {
                   4555:                 my $mm = new File::MMagic;
                   4556:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   4557:                 $$mimetype = $type;
                   4558:             }
                   4559:         }
                   4560:     }
1.1401    raeburn  4561:     if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152    raeburn  4562:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  4563:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   4564:                                                        $allfiles,$codebase);
                   4565:             unless ($parse_result eq 'ok') {
                   4566:                 &logthis('Failed to parse '.$filepath.$file.
                   4567: 	   	         ' for embedded media: '.$parse_result); 
                   4568:             }
1.637     raeburn  4569:         }
1.1401    raeburn  4570:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
                   4571:         my $format = $env{'form.scantron_format'};
                   4572:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637     raeburn  4573:     }
1.860     raeburn  4574:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   4575:         my $input = $filepath.'/'.$file;
                   4576:         my $output = $filepath.'/'.'tn-'.$file;
1.1504    raeburn  4577:         my $makethumb; 
1.860     raeburn  4578:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1504    raeburn  4579:         if ($context eq 'toollogo') {
                   4580:             my ($fullwidth,$fullheight) = &check_dimensions($input);
                   4581:             if ($fullwidth ne '' && $fullheight ne '') {
                   4582:                 if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
                   4583:                     $makethumb = 1;
                   4584:                 }
                   4585:             }
                   4586:         } else {
                   4587:             $makethumb = 1;
                   4588:         }
                   4589:         if ($makethumb) {
                   4590:             my @args = ('convert','-sample',$thumbsize,$input,$output);
                   4591:             system({$args[0]} @args);
                   4592:             if (-e $filepath.'/'.'tn-'.$file) {
                   4593:                 $fetchthumb  = 1; 
                   4594:             }
1.860     raeburn  4595:         }
                   4596:     }
1.858     raeburn  4597:  
1.259     www      4598: # Notify homeserver to grep it
                   4599: #
1.984     neumanie 4600:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 4601:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      4602:     if ($fetchresult eq 'ok') {
1.860     raeburn  4603:         if ($fetchthumb) {
                   4604:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   4605:             if ($thumbresult ne 'ok') {
                   4606:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   4607:                          $docuhome.': '.$thumbresult);
                   4608:             }
                   4609:         }
1.259     www      4610: #
1.258     www      4611: # Return the URL to it
1.494     albertel 4612:         return '/uploaded/'.$path.$file;
1.263     www      4613:     } else {
1.494     albertel 4614:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   4615: 		 ': '.$fetchresult);
1.263     www      4616:         return '/adm/notfound.html';
1.858     raeburn  4617:     }
1.493     albertel 4618: }
                   4619: 
1.637     raeburn  4620: sub extract_embedded_items {
1.961     raeburn  4621:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  4622:     my @state = ();
1.1164    raeburn  4623:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  4624:     my %javafiles = (
                   4625:                       codebase => '',
                   4626:                       code => '',
                   4627:                       archive => ''
                   4628:                     );
                   4629:     my %mediafiles = (
                   4630:                       src => '',
                   4631:                       movie => '',
                   4632:                      );
1.648     raeburn  4633:     my $p;
                   4634:     if ($content) {
                   4635:         $p = HTML::LCParser->new($content);
                   4636:     } else {
1.961     raeburn  4637:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  4638:     }
1.641     albertel 4639:     while (my $t=$p->get_token()) {
1.640     albertel 4640: 	if ($t->[0] eq 'S') {
                   4641: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 4642: 	    push(@state, $tagname);
1.648     raeburn  4643:             if (lc($tagname) eq 'allow') {
                   4644:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   4645:             }
1.640     albertel 4646: 	    if (lc($tagname) eq 'img') {
                   4647: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   4648: 	    }
1.886     albertel 4649: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  4650:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  4651:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4652:                 }
1.886     albertel 4653: 	    }
1.645     raeburn  4654:             if (lc($tagname) eq 'script') {
1.1164    raeburn  4655:                 my $src;
1.645     raeburn  4656:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   4657:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   4658:                 } else {
1.1164    raeburn  4659:                     if ($attr->{'src'} ne '') {
                   4660:                         $src = $attr->{'src'};
                   4661:                         &add_filetype($allfiles,$src,'src');
                   4662:                     }
                   4663:                 }
                   4664:                 my $text = $p->get_trimmed_text();
                   4665:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   4666:                     my @swfargs = split(/,/,$1);
                   4667:                     foreach my $item (@swfargs) {
                   4668:                         $item =~ s/["']//g;
                   4669:                         $item =~ s/^\s+//;
                   4670:                         $item =~ s/\s+$//;
                   4671:                     }
                   4672:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   4673:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   4674:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   4675:                         } else {
                   4676:                             $related{$swfargs[0]} = [$swfargs[2]];
                   4677:                         }
                   4678:                     }
1.645     raeburn  4679:                 }
                   4680:             }
                   4681:             if (lc($tagname) eq 'link') {
                   4682:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   4683:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4684:                 }
                   4685:             }
1.640     albertel 4686: 	    if (lc($tagname) eq 'object' ||
                   4687: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   4688: 		foreach my $item (keys(%javafiles)) {
                   4689: 		    $javafiles{$item} = '';
                   4690: 		}
1.1164    raeburn  4691:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   4692:                     $lastids{lc($tagname)} = $attr->{'id'};
                   4693:                 }
1.640     albertel 4694: 	    }
                   4695: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   4696: 		my $name = lc($attr->{'name'});
                   4697: 		foreach my $item (keys(%javafiles)) {
                   4698: 		    if ($name eq $item) {
                   4699: 			$javafiles{$item} = $attr->{'value'};
                   4700: 			last;
                   4701: 		    }
                   4702: 		}
1.1164    raeburn  4703:                 my $pathfrom;
1.640     albertel 4704: 		foreach my $item (keys(%mediafiles)) {
                   4705: 		    if ($name eq $item) {
1.1164    raeburn  4706:                         $pathfrom = $attr->{'value'};
                   4707:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   4708: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 4709: 			last;
                   4710: 		    }
                   4711: 		}
1.1164    raeburn  4712:                 if ($name eq 'flashvars') {
                   4713:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   4714:                 }
                   4715:                 if ($pathfrom ne '') {
                   4716:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   4717:                                          $pathfrom);
                   4718:                 }
1.640     albertel 4719: 	    }
                   4720: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   4721: 		foreach my $item (keys(%javafiles)) {
                   4722: 		    if ($attr->{$item}) {
                   4723: 			$javafiles{$item} = $attr->{$item};
                   4724: 			last;
                   4725: 		    }
                   4726: 		}
                   4727: 		foreach my $item (keys(%mediafiles)) {
                   4728: 		    if ($attr->{$item}) {
                   4729: 			&add_filetype($allfiles,$attr->{$item},$item);
                   4730: 			last;
                   4731: 		    }
                   4732: 		}
1.1164    raeburn  4733:                 if (lc($tagname) eq 'embed') {
                   4734:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   4735:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   4736:                                              $attr->{'src'});
                   4737:                     }
                   4738:                 }
1.640     albertel 4739: 	    }
1.1243    raeburn  4740:             if (lc($tagname) eq 'iframe') {
                   4741:                 my $src = $attr->{'src'} ;
                   4742:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
                   4743:                     &add_filetype($allfiles,$src,'src');
                   4744:                 } elsif ($src =~ m{^/}) {
                   4745:                     if ($env{'request.course.id'}) {
                   4746:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4747:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4748:                         my $url = &hreflocation('',$fullpath);
                   4749:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
                   4750:                             my $relpath = $1;
                   4751:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
                   4752:                                 &add_filetype($allfiles,$1,'src');
                   4753:                             }
                   4754:                         }
                   4755:                     }
                   4756:                 }
                   4757:             }
1.1164    raeburn  4758:             if ($t->[4] =~ m{/>$}) {
1.1243    raeburn  4759:                 pop(@state);
1.1164    raeburn  4760:             }
1.640     albertel 4761: 	} elsif ($t->[0] eq 'E') {
                   4762: 	    my ($tagname) = ($t->[1]);
                   4763: 	    if ($javafiles{'codebase'} ne '') {
                   4764: 		$javafiles{'codebase'} .= '/';
                   4765: 	    }  
                   4766: 	    if (lc($tagname) eq 'applet' ||
                   4767: 		lc($tagname) eq 'object' ||
                   4768: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   4769: 		) {
                   4770: 		foreach my $item (keys(%javafiles)) {
                   4771: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   4772: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   4773: 			&add_filetype($allfiles,$file,$item);
                   4774: 		    }
                   4775: 		}
                   4776: 	    } 
                   4777: 	    pop @state;
                   4778: 	}
                   4779:     }
1.1164    raeburn  4780:     foreach my $id (sort(keys(%flashvars))) {
                   4781:         if ($shockwave{$id} ne '') {
                   4782:             my @pairs = split(/\&/,$flashvars{$id});
                   4783:             foreach my $pair (@pairs) {
                   4784:                 my ($key,$value) = split(/\=/,$pair);
                   4785:                 if ($key eq 'thumb') {
                   4786:                     &add_filetype($allfiles,$value,$key);
                   4787:                 } elsif ($key eq 'content') {
                   4788:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   4789:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   4790:                     if ($ext ne '') {
                   4791:                         &add_filetype($allfiles,$path.$value,$ext);
                   4792:                     }
                   4793:                 }
                   4794:             }
                   4795:         }
                   4796:     }
1.637     raeburn  4797:     return 'ok';
                   4798: }
                   4799: 
1.639     albertel 4800: sub add_filetype {
                   4801:     my ($allfiles,$file,$type)=@_;
                   4802:     if (exists($allfiles->{$file})) {
                   4803: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   4804: 	    push(@{$allfiles->{$file}}, &escape($type));
                   4805: 	}
                   4806:     } else {
                   4807: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  4808:     }
                   4809: }
                   4810: 
1.1164    raeburn  4811: sub embedded_dependency {
                   4812:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   4813:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   4814:         if (($identifier ne '') &&
                   4815:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   4816:             ($pathfrom ne '')) {
                   4817:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   4818:             foreach my $dep (@{$related->{$identifier}}) {
                   4819:                 &add_filetype($allfiles,$path.$dep,'object');
                   4820:             }
                   4821:         }
                   4822:     }
                   4823:     return;
                   4824: }
                   4825: 
1.1504    raeburn  4826: sub check_dimensions {
                   4827:     my ($inputfile) = @_;
                   4828:     my ($fullwidth,$fullheight);
                   4829:     if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
                   4830:         my $mm = new File::MMagic;
                   4831:         my $mime_type = $mm->checktype_filename($inputfile);
                   4832:         if ($mime_type =~ m{^image/}) {
                   4833:             if (open(PIPE,"identify $inputfile 2>&1 |")) {
                   4834:                 my $imageinfo = <PIPE>;
                   4835:                 if (!close(PIPE)) {
                   4836:                     &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
                   4837:                 }
                   4838:                 chomp($imageinfo);
                   4839:                 my ($fullsize) =
                   4840:                     ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
                   4841:                 if ($fullsize) {
                   4842:                     ($fullwidth,$fullheight) = split(/x/,$fullsize);
                   4843:                 }
                   4844:             }
                   4845:         }
                   4846:     }
                   4847:     return ($fullwidth,$fullheight);
                   4848: }
                   4849: 
1.1401    raeburn  4850: sub bubblesheet_converter {
                   4851:     my ($cdom,$fullpath,$config,$format) = @_;
                   4852:     if ((&domain($cdom) ne '') &&
1.1403    raeburn  4853:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
1.1401    raeburn  4854:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
1.1404    raeburn  4855:         my (%csvcols,%csvoptions);
                   4856:         if (ref($config->{'fields'}) eq 'HASH') {  
                   4857:             %csvcols = %{$config->{'fields'}};
                   4858:         }
                   4859:         if (ref($config->{'options'}) eq 'HASH') {
                   4860:             %csvoptions = %{$config->{'options'}};
                   4861:         }
1.1401    raeburn  4862:         my %csvbynum = reverse(%csvcols);
                   4863:         my %scantronconf = &get_scantron_config($format,$cdom);
                   4864:         if (keys(%scantronconf)) {
                   4865:             my %bynum = (
                   4866:                           $scantronconf{CODEstart} => 'CODEstart',
                   4867:                           $scantronconf{IDstart}   => 'IDstart',
                   4868:                           $scantronconf{PaperID}   => 'PaperID',
                   4869:                           $scantronconf{FirstName} => 'FirstName',
                   4870:                           $scantronconf{LastName}  => 'LastName',
                   4871:                           $scantronconf{Qstart}    => 'Qstart',
                   4872:                         );
                   4873:             my @ordered;
                   4874:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
1.1403    raeburn  4875:                 push(@ordered,$bynum{$item});
1.1401    raeburn  4876:             }
                   4877:             my %mapstart = (
                   4878:                               CODEstart => 'CODE',
                   4879:                               IDstart   => 'ID',
                   4880:                               PaperID   => 'PaperID',
                   4881:                               FirstName => 'FirstName',
                   4882:                               LastName  => 'LastName',
                   4883:                               Qstart    => 'FirstQuestion',
                   4884:                            );
                   4885:             my %maplength = (
                   4886:                               CODEstart => 'CODElength',
                   4887:                               IDstart   => 'IDlength',
                   4888:                               PaperID   => 'PaperIDlength',
                   4889:                               FirstName => 'FirstNamelength',
                   4890:                               LastName  => 'LastNamelength',
                   4891:             );
                   4892:             if (open(my $fh,'<',$fullpath)) {
                   4893:                 my $output;
1.1403    raeburn  4894:                 my %lettdig = &letter_to_digits();
                   4895:                 my %diglett = reverse(%lettdig);
                   4896:                 my $numletts = scalar(keys(%lettdig));
1.1404    raeburn  4897:                 my $num = 0;
1.1401    raeburn  4898:                 while (my $line=<$fh>) {
1.1404    raeburn  4899:                     $num ++;
                   4900:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
1.1401    raeburn  4901:                     $line =~ s{[\r\n]+$}{};
                   4902:                     my %found;
1.1475    raeburn  4903:                     my @values = split(/,/,$line,-1);
1.1401    raeburn  4904:                     my ($qstart,$record);
                   4905:                     for (my $i=0; $i<@values; $i++) {
1.1403    raeburn  4906:                         if ((($qstart ne '') && ($i > $qstart)) ||
                   4907:                             ($csvbynum{$i} eq 'FirstQuestion')) {
                   4908:                             if ($values[$i] eq '') {
                   4909:                                 $values[$i] = $scantronconf{'Qoff'};
                   4910:                             } elsif ($scantronconf{'Qon'} eq 'number') {
                   4911:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4912:                                     $values[$i] = $lettdig{uc($values[$i])};
                   4913:                                 }
                   4914:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
                   4915:                                 if ($values[$i] =~ /^[0-9]$/) {
                   4916:                                     $values[$i] = $diglett{$values[$i]};
                   4917:                                 }
                   4918:                             } else {
                   4919:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
                   4920:                                     my $digit;
                   4921:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
                   4922:                                         $digit = $lettdig{uc($values[$i])}-1;
                   4923:                                         if ($values[$i] eq 'J') {
                   4924:                                             $digit += $numletts;
                   4925:                                         }
                   4926:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
                   4927:                                         $digit = $values[$i]-1;
                   4928:                                         if ($values[$i] eq '0') {
                   4929:                                             $digit += $numletts;
                   4930:                                         }
                   4931:                                     }
                   4932:                                     my $qval='';
                   4933:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
                   4934:                                         if ($j == $digit) {
                   4935:                                             $qval .= $scantronconf{'Qon'};
                   4936:                                         } else {
                   4937:                                             $qval .= $scantronconf{'Qoff'};
                   4938:                                         }
                   4939:                                     }
                   4940:                                     $values[$i] = $qval;
                   4941:                                 }
                   4942:                             }
                   4943:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
                   4944:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
                   4945:                             }
                   4946:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
                   4947:                             if ($numblank > 0) {
                   4948:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
                   4949:                             }
1.1401    raeburn  4950:                             if ($csvbynum{$i} eq 'FirstQuestion') {
                   4951:                                 $qstart = $i;
1.1403    raeburn  4952:                                 $found{$csvbynum{$i}} = $values[$i];
1.1401    raeburn  4953:                             } else {
1.1403    raeburn  4954:                                 $found{'FirstQuestion'} .= $values[$i];
                   4955:                             }
                   4956:                         } elsif (exists($csvbynum{$i})) {
1.1404    raeburn  4957:                             if ($csvoptions{'rem'}) {
                   4958:                                 $values[$i] =~ s/^\s+//;
                   4959:                             }
                   4960:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
1.1403    raeburn  4961:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
                   4962:                                     $values[$i] = '0'.$values[$i];
1.1401    raeburn  4963:                                 }
                   4964:                             }
                   4965:                             $found{$csvbynum{$i}} = $values[$i];
                   4966:                         }
                   4967:                     }
                   4968:                     foreach my $item (@ordered) {
                   4969:                         my $currlength = 1+length($record);
                   4970:                         my $numspaces = $scantronconf{$item} - $currlength;
                   4971:                         if ($numspaces > 0) {
                   4972:                             $record .= (' ' x $numspaces);
                   4973:                         }
                   4974:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
                   4975:                             unless ($item eq 'Qstart') {
                   4976:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
                   4977:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
                   4978:                                 }
                   4979:                             }
                   4980:                             $record .= $found{$mapstart{$item}};
                   4981:                         }
                   4982:                     }
                   4983:                     $output .= "$record\n";
                   4984:                 }
                   4985:                 close($fh);
                   4986:                 if ($output) {
                   4987:                     if (open(my $fh,'>',$fullpath)) {
                   4988:                         print $fh $output;
                   4989:                         close($fh);
                   4990:                     }
                   4991:                 }
                   4992:             }
                   4993:         }
                   4994:         return;
                   4995:     }
                   4996: }
                   4997: 
1.1403    raeburn  4998: sub letter_to_digits {
                   4999:     my %lettdig = (
                   5000:                     A => 1,
                   5001:                     B => 2,
                   5002:                     C => 3,
                   5003:                     D => 4,
                   5004:                     E => 5,
                   5005:                     F => 6,
                   5006:                     G => 7,
                   5007:                     H => 8,
                   5008:                     I => 9,
                   5009:                     J => 0,
                   5010:                   );
                   5011:     return %lettdig;
                   5012: }
                   5013: 
1.1401    raeburn  5014: sub get_scantron_config {
                   5015:     my ($which,$cdom) = @_;
                   5016:     my @lines = &get_scantronformat_file($cdom);
                   5017:     my %config;
                   5018:     #FIXME probably should move to XML it has already gotten a bit much now
                   5019:     foreach my $line (@lines) {
                   5020:         my ($name,$descrip)=split(/:/,$line);
                   5021:         if ($name ne $which ) { next; }
                   5022:         chomp($line);
                   5023:         my @config=split(/:/,$line);
                   5024:         $config{'name'}=$config[0];
                   5025:         $config{'description'}=$config[1];
                   5026:         $config{'CODElocation'}=$config[2];
                   5027:         $config{'CODEstart'}=$config[3];
                   5028:         $config{'CODElength'}=$config[4];
                   5029:         $config{'IDstart'}=$config[5];
                   5030:         $config{'IDlength'}=$config[6];
                   5031:         $config{'Qstart'}=$config[7];
                   5032:         $config{'Qlength'}=$config[8];
                   5033:         $config{'Qoff'}=$config[9];
                   5034:         $config{'Qon'}=$config[10];
                   5035:         $config{'PaperID'}=$config[11];
                   5036:         $config{'PaperIDlength'}=$config[12];
                   5037:         $config{'FirstName'}=$config[13];
                   5038:         $config{'FirstNamelength'}=$config[14];
                   5039:         $config{'LastName'}=$config[15];
                   5040:         $config{'LastNamelength'}=$config[16];
                   5041:         $config{'BubblesPerRow'}=$config[17];
                   5042:         last;
                   5043:     }
                   5044:     return %config;
                   5045: }
                   5046: 
                   5047: sub get_scantronformat_file {
                   5048:     my ($cdom) = @_;
                   5049:     if ($cdom eq '') {
                   5050:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5051:     }
                   5052:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
                   5053:     my $gottab = 0;
                   5054:     my @lines;
                   5055:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   5056:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   5057:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
                   5058:             if ($formatfile ne '-1') {
                   5059:                 @lines = split("\n",$formatfile,-1);
                   5060:                 $gottab = 1;
                   5061:             }
                   5062:         }
                   5063:     }
                   5064:     if (!$gottab) {
                   5065:         my $confname = $cdom.'-domainconfig';
                   5066:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
                   5067:         my $formatfile = &getfile($default);
                   5068:         if ($formatfile ne '-1') {
                   5069:             @lines = split("\n",$formatfile,-1);
                   5070:             $gottab = 1;
                   5071:         }
                   5072:     }
                   5073:     if (!$gottab) {
                   5074:         my @domains = &current_machine_domains();
                   5075:         if (grep(/^\Q$cdom\E$/,@domains)) {
                   5076:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
                   5077:                 @lines = <$fh>;
                   5078:                 close($fh);
1.1403    raeburn  5079:             }
1.1401    raeburn  5080:         } else {
                   5081:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
                   5082:                 @lines = <$fh>;
                   5083:                 close($fh);
                   5084:             }
                   5085:         }
1.1488    raeburn  5086:         chomp(@lines);
1.1401    raeburn  5087:     }
                   5088:     return @lines;
                   5089: }
                   5090: 
1.493     albertel 5091: sub removeuploadedurl {
1.984     neumanie 5092:     my ($url)=@_;	
                   5093:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 5094:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 5095: }
                   5096: 
                   5097: sub removeuserfile {
                   5098:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 5099:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  5100:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 5101:     if ($result eq 'ok') {	
1.798     raeburn  5102:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   5103:             my $metafile = $fname.'.meta';
                   5104:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 5105: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 5106:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  5107:             my $sqlresult = 
1.823     albertel 5108:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  5109:                                         'portfolio_metadata',$group,
                   5110:                                         'delete');
1.798     raeburn  5111:         }
                   5112:     }
                   5113:     return $result;
1.257     www      5114: }
1.15      www      5115: 
1.530     albertel 5116: sub mkdiruserfile {
                   5117:     my ($docuname,$docudom,$dir)=@_;
                   5118:     my $home=&homeserver($docuname,$docudom);
                   5119:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   5120: }
                   5121: 
1.531     albertel 5122: sub renameuserfile {
                   5123:     my ($docuname,$docudom,$old,$new)=@_;
                   5124:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  5125:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   5126:                         &escape("$old").':'.&escape("$new"),$home);
                   5127:     if ($result eq 'ok') {
                   5128:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   5129:             my $oldmeta = $old.'.meta';
                   5130:             my $newmeta = $new.'.meta';
                   5131:             my $metaresult = 
                   5132:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 5133: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   5134:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  5135:             my $sqlresult = 
1.823     albertel 5136:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  5137:                                         'portfolio_metadata',$group,
                   5138:                                         'delete');
1.798     raeburn  5139:         }
                   5140:     }
                   5141:     return $result;
1.531     albertel 5142: }
                   5143: 
1.14      www      5144: # ------------------------------------------------------------------------- Log
                   5145: 
                   5146: sub log {
                   5147:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      5148:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      5149: }
                   5150: 
                   5151: # ------------------------------------------------------------------ Course Log
1.352     www      5152: #
                   5153: # This routine flushes several buffers of non-mission-critical nature
                   5154: #
1.157     www      5155: 
                   5156: sub flushcourselogs {
1.352     www      5157:     &logthis('Flushing log buffers');
                   5158: #
                   5159: # course logs
                   5160: # This is a log of all transactions in a course, which can be used
                   5161: # for data mining purposes
                   5162: #
                   5163: # It also collects the courseid database, which lists last transaction
                   5164: # times and course titles for all courseids
                   5165: #
                   5166:     my %courseidbuffer=();
1.921     raeburn  5167:     foreach my $crsid (keys(%courselogs)) {
1.352     www      5168:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      5169: 		          &escape($courselogs{$crsid}),
                   5170: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      5171: 	    delete $courselogs{$crsid};
                   5172:         } else {
                   5173:             &logthis('Failed to flush log buffer for '.$crsid);
                   5174:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 5175:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      5176:                         " exceeded maximum size, deleting.</font>");
                   5177:                delete $courselogs{$crsid};
                   5178:             }
1.352     www      5179:         }
1.920     raeburn  5180:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  5181:             'description' => $coursedescrbuf{$crsid},
                   5182:             'inst_code'    => $courseinstcodebuf{$crsid},
                   5183:             'type'        => $coursetypebuf{$crsid},
                   5184:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  5185:         };
1.191     harris41 5186:     }
1.352     www      5187: #
                   5188: # Write course id database (reverse lookup) to homeserver of courses 
                   5189: # Is used in pickcourse
                   5190: #
1.840     albertel 5191:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  5192:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  5193:                                     $courseidbuffer{$crs_home},
                   5194:                                     $crs_home,'timeonly');
1.352     www      5195:     }
                   5196: #
                   5197: # File accesses
                   5198: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   5199: #
1.449     matthew  5200:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  5201:         if ($entry =~ /___count$/) {
                   5202:             my ($dom,$name);
1.807     albertel 5203:             ($dom,$name,undef)=
1.811     albertel 5204: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  5205:             if (! defined($dom) || $dom eq '' || 
                   5206:                 ! defined($name) || $name eq '') {
1.620     albertel 5207:                 my $cid = $env{'request.course.id'};
1.1472    raeburn  5208: #
                   5209: # FIXME 11/29/2021
                   5210: # Typo in rev. 1.458 (2003/12/09)??
                   5211: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
                   5212: #
1.1509    raeburn  5213: # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
1.1472    raeburn  5214: # $dom and $name will always be null, so the &inc() call will default to storing this data
                   5215: # in a nohist_accesscount.db file for the user rather than the course.
                   5216: #
                   5217: # That said there is a lot of noise in the data being stored.
                   5218: # So counts for prtspool/  and adm/ etc. are recorded.
                   5219: #
                   5220: # A review of which items ending '___count' are written to %accesshash should likely be 
                   5221: # made before deciding whether to set these to 'course.' instead of 'request.'
                   5222: #
                   5223: # Under the current scheme each user receives a nohist_accesscount.db file listing 
                   5224: # accesses for things which are not published resources, regardless of course, and
                   5225: # there is not a nohist_accesscount.db file in a course, which might log accesses from
                   5226: # anyone in the course for things which are not published resources.
                   5227: #
                   5228: # For an author, nohist_accesscount.db ends up having records for other items
                   5229: # mixed up with the legitimate access counts for the author's published resources.
                   5230: #
1.620     albertel 5231:                 $dom  = $env{'request.'.$cid.'.domain'};
                   5232:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  5233:             }
1.450     matthew  5234:             my $value = $accesshash{$entry};
                   5235:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   5236:             my %temphash=($url => $value);
1.449     matthew  5237:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   5238:             if ($result eq 'ok') {
                   5239:                 delete $accesshash{$entry};
                   5240:             }
                   5241:         } else {
1.811     albertel 5242:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      5243:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  5244:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  5245:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   5246:                 delete $accesshash{$entry};
                   5247:             }
1.185     www      5248:         }
1.191     harris41 5249:     }
1.352     www      5250: #
                   5251: # Roles
                   5252: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   5253: #
1.800     albertel 5254:     foreach my $entry (keys(%userrolehash)) {
1.351     www      5255:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      5256: 	    split(/\:/,$entry);
1.1494    raeburn  5257:         if (&put('nohist_userroles',
1.351     www      5258:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      5259:                 $rudom,$runame) eq 'ok') {
                   5260: 	    delete $userrolehash{$entry};
                   5261:         }
                   5262:     }
1.662     raeburn  5263: #
1.1334    raeburn  5264: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662     raeburn  5265: #
                   5266:     my %domrolebuffer = ();
1.1000    raeburn  5267:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 5268:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  5269:         if ($domrolebuffer{$rudom}) {
                   5270:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   5271:                       '='.&escape($domainrolehash{$entry});
                   5272:         } else {
                   5273:             $domrolebuffer{$rudom}.=&escape($entry).
                   5274:                       '='.&escape($domainrolehash{$entry});
                   5275:         }
                   5276:         delete $domainrolehash{$entry};
                   5277:     }
                   5278:     foreach my $dom (keys(%domrolebuffer)) {
1.1324    raeburn  5279: 	my %servers;
                   5280: 	if (defined(&domain($dom,'primary'))) {
                   5281: 	    my $primary=&domain($dom,'primary');
                   5282: 	    my $hostname=&hostname($primary);
                   5283: 	    $servers{$primary} = $hostname;
                   5284: 	} else { 
                   5285: 	    %servers = &get_servers($dom,'library');
                   5286: 	}
1.841     albertel 5287: 	foreach my $tryserver (keys(%servers)) {
1.1324    raeburn  5288: 	    if (&reply('domroleput:'.$dom.':'.
                   5289: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   5290: 		last;
                   5291: 	    } else {  
1.841     albertel 5292: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   5293: 	    }
1.662     raeburn  5294:         }
                   5295:     }
1.186     www      5296:     $dumpcount++;
1.157     www      5297: }
                   5298: 
                   5299: sub courselog {
                   5300:     my $what=shift;
1.158     www      5301:     $what=time.':'.$what;
1.620     albertel 5302:     unless ($env{'request.course.id'}) { return ''; }
                   5303:     $coursedombuf{$env{'request.course.id'}}=
                   5304:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5305:     $coursenumbuf{$env{'request.course.id'}}=
                   5306:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   5307:     $coursehombuf{$env{'request.course.id'}}=
                   5308:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   5309:     $coursedescrbuf{$env{'request.course.id'}}=
                   5310:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   5311:     $courseinstcodebuf{$env{'request.course.id'}}=
                   5312:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   5313:     $courseownerbuf{$env{'request.course.id'}}=
                   5314:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  5315:     $coursetypebuf{$env{'request.course.id'}}=
                   5316:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 5317:     if (defined $courselogs{$env{'request.course.id'}}) {
                   5318: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      5319:     } else {
1.620     albertel 5320: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      5321:     }
1.620     albertel 5322:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      5323: 	&flushcourselogs();
                   5324:     }
1.158     www      5325: }
                   5326: 
                   5327: sub courseacclog {
                   5328:     my $fnsymb=shift;
1.620     albertel 5329:     unless ($env{'request.course.id'}) { return ''; }
                   5330:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      5331:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      5332:         $what.=':POST';
1.583     matthew  5333:         # FIXME: Probably ought to escape things....
1.800     albertel 5334: 	foreach my $key (keys(%env)) {
                   5335:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  5336:                 my $formitem = $1;
                   5337:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   5338:                     $what.=':'.$formitem.'='.$env{$key};
                   5339:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1432    raeburn  5340:                     if ($formitem eq 'proctorpassword') {
1.1433    raeburn  5341:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1432    raeburn  5342:                     } else {
                   5343:                         $what.=':'.$formitem.'='.$env{$key};
                   5344:                     }
1.975     raeburn  5345:                 }
1.158     www      5346:             }
1.191     harris41 5347:         }
1.583     matthew  5348:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   5349:         # FIXME: We should not be depending on a form parameter that someone
                   5350:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 5351:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  5352:             $what.= ':POST';
                   5353:             # FIXME: Probably ought to escape things....
                   5354:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   5355:                                  'crsdiscuss') {
1.620     albertel 5356:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  5357:             }
                   5358:         }
1.158     www      5359:     }
                   5360:     &courselog($what);
1.149     www      5361: }
                   5362: 
1.185     www      5363: sub countacc {
                   5364:     my $url=&declutter(shift);
1.458     matthew  5365:     return if (! defined($url) || $url eq '');
1.620     albertel 5366:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      5367: #
                   5368: # Mark that this url was used in this course
                   5369: #
1.620     albertel 5370:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      5371: #
                   5372: # Increase the access count for this resource in this child process
                   5373: #
1.281     www      5374:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  5375:     $accesshash{$key}++;
1.185     www      5376: }
1.349     www      5377: 
1.361     www      5378: sub linklog {
                   5379:     my ($from,$to)=@_;
                   5380:     $from=&declutter($from);
                   5381:     $to=&declutter($to);
                   5382:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   5383:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   5384: }
1.1160    www      5385: 
                   5386: sub statslog {
                   5387:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   5388:     if ($users<2) { return; }
                   5389:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   5390:             'course'       => $env{'request.course.id'},
                   5391:             'sections'     => '"all"',
                   5392:             'num_students' => $users,
                   5393:             'part'         => $part,
                   5394:             'symb'         => $symb,
                   5395:             'mean_tries'   => $av_attempts,
                   5396:             'deg_of_diff'  => $degdiff});
                   5397:     foreach my $key (keys(%dynstore)) {
                   5398:         $accesshash{$key}=$dynstore{$key};
                   5399:     }
                   5400: }
1.361     www      5401:   
1.349     www      5402: sub userrolelog {
                   5403:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 5404:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      5405:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5406:        $userrolehash
                   5407:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      5408:                     =$tend.':'.$tstart;
1.662     raeburn  5409:     }
1.1169    droeschl 5410:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 5411:        $userrolehash
                   5412:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   5413:                     =$tend.':'.$tstart;
                   5414:     }
1.1334    raeburn  5415:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662     raeburn  5416:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   5417:        $domainrolehash
                   5418:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   5419:                     = $tend.':'.$tstart;
                   5420:     }
1.351     www      5421: }
                   5422: 
1.957     raeburn  5423: sub courserolelog {
                   5424:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184    raeburn  5425:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   5426:         my $cdom = $1;
                   5427:         my $cnum = $2;
                   5428:         my $sec = $3;
                   5429:         my $namespace = 'rolelog';
                   5430:         my %storehash = (
                   5431:                            role    => $trole,
                   5432:                            start   => $tstart,
                   5433:                            end     => $tend,
                   5434:                            selfenroll => $selfenroll,
                   5435:                            context    => $context,
                   5436:                         );
                   5437:         if ($trole eq 'gr') {
                   5438:             $namespace = 'groupslog';
                   5439:             $storehash{'group'} = $sec;
                   5440:         } else {
                   5441:             $storehash{'section'} = $sec;
                   5442:         }
1.1188    raeburn  5443:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   5444:                    $domain,$cnum,$cdom);
1.1184    raeburn  5445:         if (($trole ne 'st') || ($sec ne '')) {
                   5446:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  5447:         }
                   5448:     }
                   5449:     return;
                   5450: }
                   5451: 
1.1184    raeburn  5452: sub domainrolelog {
                   5453:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   5454:     if ($area =~ m{^/($match_domain)/$}) {
                   5455:         my $cdom = $1;
1.1494    raeburn  5456:         my $domconfiguser = &get_domainconfiguser($cdom);
1.1184    raeburn  5457:         my $namespace = 'rolelog';
                   5458:         my %storehash = (
                   5459:                            role    => $trole,
                   5460:                            start   => $tstart,
                   5461:                            end     => $tend,
                   5462:                            context => $context,
                   5463:                         );
1.1188    raeburn  5464:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   5465:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  5466:     }
                   5467:     return;
                   5468: 
                   5469: }
                   5470: 
                   5471: sub coauthorrolelog {
                   5472:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   5473:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   5474:         my $audom = $1;
                   5475:         my $auname = $2;
                   5476:         my $namespace = 'rolelog';
                   5477:         my %storehash = (
                   5478:                            role    => $trole,
                   5479:                            start   => $tstart,
                   5480:                            end     => $tend,
                   5481:                            context => $context,
                   5482:                         );
1.1188    raeburn  5483:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   5484:                    $domain,$auname,$audom);
1.1184    raeburn  5485:     }
                   5486:     return;
                   5487: }
                   5488: 
1.351     www      5489: sub get_course_adv_roles {
1.948     raeburn  5490:     my ($cid,$codes) = @_;
1.620     albertel 5491:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      5492:     my %coursehash=&coursedescription($cid);
1.988     raeburn  5493:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      5494:     my %nothide=();
1.800     albertel 5495:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  5496:         if ($user !~ /:/) {
                   5497: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   5498:         } else {
                   5499:             $nothide{$user}=1;
                   5500:         }
1.470     www      5501:     }
1.1219    raeburn  5502:     my @possdoms = ($coursehash{'domain'});
                   5503:     if ($coursehash{'checkforpriv'}) {
                   5504:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   5505:     }
1.351     www      5506:     my %returnhash=();
                   5507:     my %dumphash=
                   5508:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   5509:     my $now=time;
1.997     raeburn  5510:     my %privileged;
1.1000    raeburn  5511:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 5512: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      5513:         if (($tstart) && ($tstart<0)) { next; }
                   5514:         if (($tend) && ($tend<$now)) { next; }
                   5515:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 5516:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 5517: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  5518:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  5519:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 5520: 	if ($role eq 'cr') { next; }
1.948     raeburn  5521:         if ($codes) {
                   5522:             if ($section) { $role .= ':'.$section; }
                   5523:             if ($returnhash{$role}) {
                   5524:                 $returnhash{$role}.=','.$username.':'.$domain;
                   5525:             } else {
                   5526:                 $returnhash{$role}=$username.':'.$domain;
                   5527:             }
1.351     www      5528:         } else {
1.988     raeburn  5529:             my $key=&plaintext($role,$crstype);
1.973     bisitz   5530:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  5531:             if ($returnhash{$key}) {
                   5532: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   5533:             } else {
                   5534:                 $returnhash{$key}=$username.':'.$domain;
                   5535:             }
1.351     www      5536:         }
1.948     raeburn  5537:     }
1.400     www      5538:     return %returnhash;
                   5539: }
                   5540: 
                   5541: sub get_my_roles {
1.937     raeburn  5542:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 5543:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   5544:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  5545:     my (%dumphash,%nothide);
1.1086    raeburn  5546:     if ($context eq 'userroles') {
1.1166    raeburn  5547:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  5548:     } else {
1.1219    raeburn  5549:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  5550:         if ($hidepriv) {
                   5551:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   5552:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   5553:                 if ($user !~ /:/) {
                   5554:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   5555:                 } else {
                   5556:                     $nothide{$user} = 1;
                   5557:                 }
                   5558:             }
                   5559:         }
1.858     raeburn  5560:     }
1.400     www      5561:     my %returnhash=();
                   5562:     my $now=time;
1.999     raeburn  5563:     my %privileged;
1.800     albertel 5564:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  5565:         my ($role,$tend,$tstart);
                   5566:         if ($context eq 'userroles') {
1.1149    raeburn  5567:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  5568: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   5569:         } else {
                   5570:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   5571:         }
1.400     www      5572:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  5573:         my $status = 'active';
1.939     raeburn  5574:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  5575:             $status = 'previous';
                   5576:         } 
                   5577:         if (($tstart) && ($now<$tstart)) {
                   5578:             $status = 'future';
                   5579:         }
                   5580:         if (ref($types) eq 'ARRAY') {
                   5581:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   5582:                 next;
                   5583:             } 
                   5584:         } else {
                   5585:             if ($status ne 'active') {
                   5586:                 next;
                   5587:             }
                   5588:         }
1.867     raeburn  5589:         my ($rolecode,$username,$domain,$section,$area);
                   5590:         if ($context eq 'userroles') {
1.1186    raeburn  5591:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  5592:             (undef,$domain,$username,$section) = split(/\//,$area);
                   5593:         } else {
                   5594:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   5595:         }
1.832     raeburn  5596:         if (ref($roledoms) eq 'ARRAY') {
                   5597:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   5598:                 next;
                   5599:             }
                   5600:         }
                   5601:         if (ref($roles) eq 'ARRAY') {
                   5602:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  5603:                 if ($role =~ /^cr\//) {
                   5604:                     if (!grep(/^cr$/,@{$roles})) {
                   5605:                         next;
                   5606:                     }
1.1104    raeburn  5607:                 } elsif ($role =~ /^gr\//) {
                   5608:                     if (!grep(/^gr$/,@{$roles})) {
                   5609:                         next;
                   5610:                     }
1.922     raeburn  5611:                 } else {
                   5612:                     next;
                   5613:                 }
1.832     raeburn  5614:             }
1.867     raeburn  5615:         }
1.937     raeburn  5616:         if ($hidepriv) {
1.1219    raeburn  5617:             my @privroles = ('dc','su');
1.999     raeburn  5618:             if ($context eq 'userroles') {
1.1219    raeburn  5619:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  5620:             } else {
1.1219    raeburn  5621:                 my $possdoms = [$domain];
                   5622:                 if (ref($roledoms) eq 'ARRAY') {
                   5623:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  5624:                 }
1.1219    raeburn  5625:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  5626:                     if (!$nothide{$username.':'.$domain}) {
                   5627:                         next;
                   5628:                     }
                   5629:                 }
1.937     raeburn  5630:             }
                   5631:         }
1.933     raeburn  5632:         if ($withsec) {
                   5633:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   5634:                 $tstart.':'.$tend;
                   5635:         } else {
                   5636:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   5637:         }
1.832     raeburn  5638:     }
1.373     www      5639:     return %returnhash;
1.399     www      5640: }
                   5641: 
1.1333    raeburn  5642: sub get_all_adhocroles {
                   5643:     my ($dom) = @_;
                   5644:     my @roles_by_num = ();
                   5645:     my %domdefaults = &get_domain_defaults($dom);
                   5646:     my (%description,%access_in_dom,%access_info);
                   5647:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   5648:         my $count = 0;
                   5649:         my %domcurrent = %{$domdefaults{'adhocroles'}};
                   5650:         my %ordered;
                   5651:         foreach my $role (sort(keys(%domcurrent))) {
                   5652:             my ($order,$desc,$access_in_dom);
                   5653:             if (ref($domcurrent{$role}) eq 'HASH') {
                   5654:                 $order = $domcurrent{$role}{'order'};
                   5655:                 $desc = $domcurrent{$role}{'desc'};
                   5656:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
                   5657:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
                   5658:             }
                   5659:             if ($order eq '') {
                   5660:                 $order = $count;
                   5661:             }
                   5662:             $ordered{$order} = $role;
                   5663:             if ($desc ne '') {
                   5664:                 $description{$role} = $desc;
                   5665:             } else {
                   5666:                 $description{$role}= $role;
                   5667:             }
                   5668:             $count++;
                   5669:         }
                   5670:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   5671:             push(@roles_by_num,$ordered{$item});
                   5672:         }
                   5673:     }
                   5674:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
                   5675: }
                   5676: 
1.1332    raeburn  5677: sub get_my_adhocroles {
1.1333    raeburn  5678:     my ($cid,$checkreg) = @_;
                   5679:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
                   5680:     if ($env{'request.course.id'} eq $cid) {
                   5681:         $cdom = $env{'course.'.$cid.'.domain'};
                   5682:         $cnum = $env{'course.'.$cid.'.num'};
                   5683:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
                   5684:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
                   5685:         $cdom = $1;
                   5686:         $cnum = $2;
1.1494    raeburn  5687:         %info = &get('environment',['internal.coursecode'],
                   5688:                      $cdom,$cnum);
1.1333    raeburn  5689:     }
                   5690:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
                   5691:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   5692:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
                   5693:         if ($rosterhash{$user} ne '') {
                   5694:             my $type = (split(/:/,$rosterhash{$user}))[5];
                   5695:             return ([],{}) if ($type eq 'auto');
                   5696:         }
                   5697:     }
                   5698:     if (($cdom ne '') && ($cnum ne ''))  {
1.1334    raeburn  5699:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332    raeburn  5700:             my $then=$env{'user.login.time'};
                   5701:             my $update=$env{'user.update.time'};
1.1335    raeburn  5702:             if (!$update) {
                   5703:                 $update = $then;
                   5704:             }
                   5705:             my @liveroles;
1.1334    raeburn  5706:             foreach my $role ('dh','da') {
                   5707:                 if ($env{"user.role.$role./$cdom/"}) {
1.1335    raeburn  5708:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334    raeburn  5709:                     my $limit = $update;
                   5710:                     if ($env{'request.role'} eq "$role./$cdom/") {
                   5711:                         $limit = $then;
                   5712:                     }
1.1335    raeburn  5713:                     my $activerole = 1;
                   5714:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
                   5715:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
                   5716:                     if ($activerole) {
                   5717:                         push(@liveroles,$role);
                   5718:                     }
1.1334    raeburn  5719:                 }
1.1332    raeburn  5720:             }
1.1335    raeburn  5721:             if (@liveroles) {
1.1332    raeburn  5722:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333    raeburn  5723:                     my ($accessref,$accessinfo,%access_in_dom);
                   5724:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
                   5725:                     if (ref($roles_by_num) eq 'ARRAY') {
                   5726:                         if (@{$roles_by_num}) {
1.1332    raeburn  5727:                             my %settings;
                   5728:                             if ($env{'request.course.id'} eq $cid) {
                   5729:                                 foreach my $envkey (keys(%env)) {
                   5730:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
                   5731:                                         $settings{$1} = $env{$envkey};
                   5732:                                     }
                   5733:                                 }
                   5734:                             } else {
                   5735:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
                   5736:                             }
                   5737:                             my %setincrs;
                   5738:                             if ($settings{'internal.adhocaccess'}) {
                   5739:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
                   5740:                             }
                   5741:                             my @statuses;
                   5742:                             if ($env{'environment.inststatus'}) {
                   5743:                                 @statuses = split(/,/,$env{'environment.inststatus'});
                   5744:                             }
                   5745:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333    raeburn  5746:                             if (ref($accessref) eq 'HASH') {
                   5747:                                 %access_in_dom = %{$accessref};
                   5748:                             }
                   5749:                             foreach my $role (@{$roles_by_num}) {
1.1332    raeburn  5750:                                 my ($curraccess,@okstatus,@personnel);
                   5751:                                 if ($setincrs{$role}) {
                   5752:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
                   5753:                                     if ($curraccess eq 'status') {
                   5754:                                         @okstatus = split(/\&/,$rest);
                   5755:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5756:                                         @personnel = split(/\&/,$rest);
                   5757:                                     }
                   5758:                                 } else {
                   5759:                                     $curraccess = $access_in_dom{$role};
1.1333    raeburn  5760:                                     if (ref($accessinfo) eq 'HASH') {
                   5761:                                         if ($curraccess eq 'status') {
                   5762:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5763:                                                 @okstatus = @{$accessinfo->{$role}};
                   5764:                                             }
                   5765:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5766:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5767:                                                 @personnel = @{$accessinfo->{$role}};
                   5768:                                             }
1.1332    raeburn  5769:                                         }
                   5770:                                     }
                   5771:                                 }
                   5772:                                 if ($curraccess eq 'none') {
                   5773:                                     next;
                   5774:                                 } elsif ($curraccess eq 'all') {
                   5775:                                     push(@possroles,$role);
1.1336    raeburn  5776:                                 } elsif ($curraccess eq 'dh') {
1.1335    raeburn  5777:                                     if (grep(/^dh$/,@liveroles)) {
                   5778:                                         push(@possroles,$role);
                   5779:                                     } else {
                   5780:                                         next;
                   5781:                                     }
1.1336    raeburn  5782:                                 } elsif ($curraccess eq 'da') {
1.1335    raeburn  5783:                                     if (grep(/^da$/,@liveroles)) {
                   5784:                                         push(@possroles,$role);
                   5785:                                     } else {
                   5786:                                         next;
                   5787:                                     }
1.1332    raeburn  5788:                                 } elsif ($curraccess eq 'status') {
                   5789:                                     if (@okstatus) {
                   5790:                                         if (!@statuses) {
                   5791:                                             if (grep(/^default$/,@okstatus)) {
                   5792:                                                 push(@possroles,$role);
                   5793:                                             }
                   5794:                                         } else {
                   5795:                                             foreach my $status (@okstatus) {
                   5796:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
                   5797:                                                     push(@possroles,$role);
                   5798:                                                     last;
                   5799:                                                 }
                   5800:                                             }
                   5801:                                         }
                   5802:                                     }
                   5803:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5804:                                     if (grep(/^\Q$user\E$/,@personnel)) {
                   5805:                                         if ($curraccess eq 'exc') {
                   5806:                                             push(@possroles,$role);
                   5807:                                         }
                   5808:                                     } elsif ($curraccess eq 'inc') {
                   5809:                                         push(@possroles,$role);
                   5810:                                     }
                   5811:                                 }
                   5812:                             }
                   5813:                         }
                   5814:                     }
                   5815:                 }
                   5816:             }
                   5817:         }
                   5818:     }
1.1333    raeburn  5819:     unless (ref($description) eq 'HASH') {
                   5820:         if (ref($roles_by_num) eq 'ARRAY') {
                   5821:             my %desc;
                   5822:             map { $desc{$_} = $_; } (@{$roles_by_num});
                   5823:             $description = \%desc;
                   5824:         } else {
                   5825:             $description = {};
                   5826:         }
                   5827:     }
                   5828:     return (\@possroles,$description);
1.1332    raeburn  5829: }
                   5830: 
1.399     www      5831: # ----------------------------------------------------- Frontpage Announcements
                   5832: #
                   5833: #
                   5834: 
                   5835: sub postannounce {
                   5836:     my ($server,$text)=@_;
1.844     albertel 5837:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      5838:     unless ($text=~/\w/) { $text=''; }
                   5839:     return &reply('setannounce:'.&escape($text),$server);
                   5840: }
                   5841: 
                   5842: sub getannounce {
1.448     albertel 5843: 
1.1359    raeburn  5844:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      5845: 	my $announcement='';
1.800     albertel 5846: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 5847: 	close($fh);
1.399     www      5848: 	if ($announcement=~/\w/) { 
                   5849: 	    return 
                   5850:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 5851:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      5852: 	} else {
                   5853: 	    return '';
                   5854: 	}
                   5855:     } else {
                   5856: 	return '';
                   5857:     }
1.351     www      5858: }
1.353     www      5859: 
                   5860: # ---------------------------------------------------------- Course ID routines
                   5861: # Deal with domain's nohist_courseid.db files
                   5862: #
                   5863: 
                   5864: sub courseidput {
1.921     raeburn  5865:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  5866:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  5867:     my $outcome;
                   5868:     if ($caller eq 'timeonly') {
                   5869:         my $cids = '';
                   5870:         foreach my $item (keys(%$storehash)) {
                   5871:             $cids.=&escape($item).'&';
                   5872:         }
                   5873:         $cids=~s/\&$//;
                   5874:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   5875:                           $coursehome);       
                   5876:     } else {
                   5877:         my $items = '';
                   5878:         foreach my $item (keys(%$storehash)) {
                   5879:             $items.= &escape($item).'='.
                   5880:                      &freeze_escape($$storehash{$item}).'&';
                   5881:         }
                   5882:         $items=~s/\&$//;
                   5883:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   5884:                           $coursehome);
1.918     raeburn  5885:     }
                   5886:     if ($outcome eq 'unknown_cmd') {
                   5887:         my $what;
                   5888:         foreach my $cid (keys(%$storehash)) {
                   5889:             $what .= &escape($cid).'=';
1.921     raeburn  5890:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  5891:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  5892:             }
                   5893:             $what =~ s/\:$/&/;
                   5894:         }
                   5895:         $what =~ s/\&$//;  
                   5896:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   5897:     } else {
                   5898:         return $outcome;
                   5899:     }
1.353     www      5900: }
                   5901: 
                   5902: sub courseiddump {
1.921     raeburn  5903:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  5904:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  5905:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247    raeburn  5906:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287    raeburn  5907:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918     raeburn  5908:     my $as_hash = 1;
                   5909:     my %returnhash;
                   5910:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 5911:     my %libserv = &all_library();
                   5912:     foreach my $tryserver (keys(%libserv)) {
                   5913:         if ( (  $hostidflag == 1 
                   5914: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   5915: 	     || (!defined($hostidflag)) ) {
                   5916: 
1.918     raeburn  5917: 	    if (($domfilter eq '') ||
                   5918: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 5919:                 my $rep;
                   5920:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   5921:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   5922:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   5923:                                 &escape($descfilter), &escape($instcodefilter), 
                   5924:                                 &escape($ownerfilter), &escape($coursefilter),
                   5925:                                 &escape($typefilter), &escape($regexp_ok), 
                   5926:                                 $as_hash, &escape($selfenrollonly), 
                   5927:                                 &escape($catfilter), $showhidden, $caller, 
                   5928:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   5929:                                 &escape($createdbefore), &escape($createdafter), 
1.1287    raeburn  5930:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
                   5931:                                 $reqcrsdom,&escape($reqinstcode))));
1.1180    droeschl 5932:                 } else {
                   5933:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   5934:                              $sincefilter.':'.&escape($descfilter).':'.
                   5935:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   5936:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   5937:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   5938:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   5939:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   5940:                              &escape($cc_clone).':'.$cloneonly.':'.
                   5941:                              &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287    raeburn  5942:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
                   5943:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180    droeschl 5944:                 }
                   5945:                      
1.918     raeburn  5946:                 my @pairs=split(/\&/,$rep);
                   5947:                 foreach my $item (@pairs) {
                   5948:                     my ($key,$value)=split(/\=/,$item,2);
                   5949:                     $key = &unescape($key);
                   5950:                     next if ($key =~ /^error: 2 /);
                   5951:                     my $result = &thaw_unescape($value);
                   5952:                     if (ref($result) eq 'HASH') {
                   5953:                         $returnhash{$key}=$result;
                   5954:                     } else {
1.921     raeburn  5955:                         my @responses = split(/:/,$value);
                   5956:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  5957:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  5958:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  5959:                         }
1.1008    raeburn  5960:                     }
1.353     www      5961:                 }
                   5962:             }
                   5963:         }
                   5964:     }
                   5965:     return %returnhash;
                   5966: }
                   5967: 
1.1055    raeburn  5968: sub courselastaccess {
                   5969:     my ($cdom,$cnum,$hostidref) = @_;
                   5970:     my %returnhash;
                   5971:     if ($cdom && $cnum) {
                   5972:         my $chome = &homeserver($cnum,$cdom);
                   5973:         if ($chome ne 'no_host') {
                   5974:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   5975:             &extract_lastaccess(\%returnhash,$rep);
                   5976:         }
                   5977:     } else {
                   5978:         if (!$cdom) { $cdom=''; }
                   5979:         my %libserv = &all_library();
                   5980:         foreach my $tryserver (keys(%libserv)) {
                   5981:             if (ref($hostidref) eq 'ARRAY') {
                   5982:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   5983:             } 
                   5984:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   5985:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   5986:                 &extract_lastaccess(\%returnhash,$rep);
                   5987:             }
                   5988:         }
                   5989:     }
                   5990:     return %returnhash;
                   5991: }
                   5992: 
                   5993: sub extract_lastaccess {
                   5994:     my ($returnhash,$rep) = @_;
                   5995:     if (ref($returnhash) eq 'HASH') {
                   5996:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   5997:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   5998:                  $rep eq '') {
                   5999:             my @pairs=split(/\&/,$rep);
                   6000:             foreach my $item (@pairs) {
                   6001:                 my ($key,$value)=split(/\=/,$item,2);
                   6002:                 $key = &unescape($key);
                   6003:                 next if ($key =~ /^error: 2 /);
                   6004:                 $returnhash->{$key} = &thaw_unescape($value);
                   6005:             }
                   6006:         }
                   6007:     }
                   6008:     return;
                   6009: }
                   6010: 
1.658     raeburn  6011: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  6012: 
                   6013: sub dcmailput {
1.685     raeburn  6014:     my ($domain,$msgid,$message,$server)=@_;
1.1494    raeburn  6015:     my $status = &critical(
1.740     www      6016:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   6017:        &escape($message),$server);
1.662     raeburn  6018:     return $status;
                   6019: }
                   6020: 
1.658     raeburn  6021: sub dcmaildump {
                   6022:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  6023:     my %returnhash=();
1.846     albertel 6024: 
                   6025:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  6026:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   6027:                                                          &escape($enddate).':';
                   6028: 	my @esc_senders=map { &escape($_)} @$senders;
                   6029: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 6030: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 6031:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  6032:             if (($key) && ($value)) {
                   6033:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  6034:             }
                   6035:         }
                   6036:     }
                   6037:     return %returnhash;
                   6038: }
1.662     raeburn  6039: # ---------------------------------------------------------- Domain roles
                   6040: 
                   6041: sub get_domain_roles {
                   6042:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  6043:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  6044:         $startdate = '.';
                   6045:     }
1.1018    raeburn  6046:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  6047:         $enddate = '.';
                   6048:     }
1.922     raeburn  6049:     my $rolelist;
                   6050:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  6051:         $rolelist = join('&',@{$roles});
1.922     raeburn  6052:     }
1.662     raeburn  6053:     my %personnel = ();
1.841     albertel 6054: 
                   6055:     my %servers = &get_servers($dom,'library');
                   6056:     foreach my $tryserver (keys(%servers)) {
                   6057: 	%{$personnel{$tryserver}}=();
                   6058: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   6059: 					    &escape($startdate).':'.
                   6060: 					    &escape($enddate).':'.
                   6061: 					    &escape($rolelist), $tryserver))) {
                   6062: 	    my ($key,$value) = split(/\=/,$line,2);
                   6063: 	    if (($key) && ($value)) {
                   6064: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   6065: 	    }
                   6066: 	}
1.662     raeburn  6067:     }
                   6068:     return %personnel;
                   6069: }
1.658     raeburn  6070: 
1.1332    raeburn  6071: sub get_active_domroles {
                   6072:     my ($dom,$roles) = @_;
                   6073:     return () unless (ref($roles) eq 'ARRAY');
                   6074:     my $now = time;
                   6075:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
                   6076:     my %domroles;
                   6077:     foreach my $server (keys(%dompersonnel)) {
                   6078:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   6079:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
                   6080:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
                   6081:         }
                   6082:     }
                   6083:     return %domroles;
                   6084: }
                   6085: 
1.1057    www      6086: # ----------------------------------------------------------- Interval timing 
1.149     www      6087: 
1.1153    www      6088: {
                   6089: # Caches needed for speedup of navmaps
                   6090: # We don't want to cache this for very long at all (5 seconds at most)
                   6091: # 
                   6092: # The user for whom we cache
                   6093: my $cachedkey='';
                   6094: # The cached times for this user
                   6095: my %cachedtimes=();
                   6096: # When this was last done
1.1282    raeburn  6097: my $cachedtime='';
1.1153    www      6098: 
                   6099: sub load_all_first_access {
1.1308    raeburn  6100:     my ($uname,$udom,$ignorecache)=@_;
1.1156    www      6101:     if (($cachedkey eq $uname.':'.$udom) &&
1.1308    raeburn  6102:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
                   6103:         (!$ignorecache)) {
1.1154    raeburn  6104:         return;
                   6105:     }
                   6106:     $cachedtime=time;
                   6107:     $cachedkey=$uname.':'.$udom;
                   6108:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      6109: }
                   6110: 
1.504     albertel 6111: sub get_first_access {
1.1308    raeburn  6112:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790     albertel 6113:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 6114:     if ($argsymb) { $symb=$argsymb; }
                   6115:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  6116:     if ($argmap) { $map = $argmap; }
1.926     albertel 6117:     if ($type eq 'course') {
                   6118: 	$res='course';
                   6119:     } elsif ($type eq 'map') {
1.588     albertel 6120: 	$res=&symbread($map);
                   6121:     } else {
                   6122: 	$res=$symb;
                   6123:     }
1.1308    raeburn  6124:     &load_all_first_access($uname,$udom,$ignorecache);
1.1153    www      6125:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 6126: }
                   6127: 
                   6128: sub set_first_access {
1.1162    raeburn  6129:     my ($type,$interval)=@_;
1.790     albertel 6130:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 6131:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 6132:     if ($type eq 'course') {
                   6133: 	$res='course';
                   6134:     } elsif ($type eq 'map') {
1.588     albertel 6135: 	$res=&symbread($map);
                   6136:     } else {
                   6137: 	$res=$symb;
                   6138:     }
1.1153    www      6139:     $cachedkey='';
1.1162    raeburn  6140:     my $firstaccess=&get_first_access($type,$symb,$map);
1.1386    raeburn  6141:     if ($firstaccess) {
                   6142:         &logthis("First access time already set ($firstaccess) when attempting ".
1.1393    raeburn  6143:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
                   6144:                  "in $courseid");
1.1386    raeburn  6145:         return 'already_set';
                   6146:     } else {
1.1162    raeburn  6147:         my $start = time;
                   6148: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   6149:                           $udom,$uname);
                   6150:         if ($putres eq 'ok') {
                   6151:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   6152:                  $udom,$uname); 
                   6153:             &appenv(
                   6154:                      {
                   6155:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   6156:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   6157:                      }
                   6158:                   );
1.1365    raeburn  6159:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
                   6160:                 $cachedtimes{"$courseid\0$res"} = $start;
                   6161:             }
1.1386    raeburn  6162:         } elsif ($putres ne 'refused') {
                   6163:             &logthis("Result: $putres when attempting to set first access time ".
                   6164:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162    raeburn  6165:         }
                   6166:         return $putres;
1.505     albertel 6167:     }
                   6168:     return 'already_set';
1.504     albertel 6169: }
1.1153    www      6170: }
1.1282    raeburn  6171: 
1.110     www      6172: # --------------------------------------------- Set Expire Date for Spreadsheet
                   6173: 
                   6174: sub expirespread {
                   6175:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 6176:     my $cid=$env{'request.course.id'}; 
1.110     www      6177:     if ($cid) {
                   6178:        my $now=time;
                   6179:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 6180:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   6181:                             $env{'course.'.$cid.'.num'}.
1.110     www      6182: 	        	    ':nohist_expirationdates:'.
                   6183:                             &escape($key).'='.$now,
1.620     albertel 6184:                             $env{'course.'.$cid.'.home'})
1.110     www      6185:     }
                   6186:     return 'ok';
1.14      www      6187: }
                   6188: 
1.109     www      6189: # ----------------------------------------------------- Devalidate Spreadsheets
                   6190: 
                   6191: sub devalidate {
1.325     www      6192:     my ($symb,$uname,$udom)=@_;
1.620     albertel 6193:     my $cid=$env{'request.course.id'}; 
1.109     www      6194:     if ($cid) {
1.391     matthew  6195:         # delete the stored spreadsheets for
                   6196:         # - the student level sheet of this user in course's homespace
                   6197:         # - the assessment level sheet for this resource 
                   6198:         #   for this user in user's homespace
1.553     albertel 6199: 	# - current conditional state info
1.325     www      6200: 	my $key=$uname.':'.$udom.':';
1.109     www      6201:         my $status=
1.299     matthew  6202: 	    &del('nohist_calculatedsheets',
1.391     matthew  6203: 		 [$key.'studentcalc:'],
1.620     albertel 6204: 		 $env{'course.'.$cid.'.domain'},
                   6205: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 6206: 		.' '.
                   6207: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  6208: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      6209:         unless ($status eq 'ok ok') {
                   6210:            &logthis('Could not devalidate spreadsheet '.
1.325     www      6211:                     $uname.' at '.$udom.' for '.
1.109     www      6212: 		    $symb.': '.$status);
1.133     albertel 6213:         }
1.553     albertel 6214: 	&delenv('user.state.'.$cid);
1.109     www      6215:     }
                   6216: }
                   6217: 
1.265     albertel 6218: sub get_scalar {
                   6219:     my ($string,$end) = @_;
                   6220:     my $value;
                   6221:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   6222: 	$value = $1;
                   6223:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   6224: 	$value = $1;
                   6225:     }
                   6226:     return &unescape($value);
                   6227: }
                   6228: 
                   6229: sub array2str {
                   6230:   my (@array) = @_;
                   6231:   my $result=&arrayref2str(\@array);
                   6232:   $result=~s/^__ARRAY_REF__//;
                   6233:   $result=~s/__END_ARRAY_REF__$//;
                   6234:   return $result;
                   6235: }
                   6236: 
1.204     albertel 6237: sub arrayref2str {
                   6238:   my ($arrayref) = @_;
1.265     albertel 6239:   my $result='__ARRAY_REF__';
1.204     albertel 6240:   foreach my $elem (@$arrayref) {
1.265     albertel 6241:     if(ref($elem) eq 'ARRAY') {
                   6242:       $result.=&arrayref2str($elem).'&';
                   6243:     } elsif(ref($elem) eq 'HASH') {
                   6244:       $result.=&hashref2str($elem).'&';
                   6245:     } elsif(ref($elem)) {
                   6246:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 6247:     } else {
                   6248:       $result.=&escape($elem).'&';
                   6249:     }
                   6250:   }
                   6251:   $result=~s/\&$//;
1.265     albertel 6252:   $result .= '__END_ARRAY_REF__';
1.204     albertel 6253:   return $result;
                   6254: }
                   6255: 
1.168     albertel 6256: sub hash2str {
1.204     albertel 6257:   my (%hash) = @_;
                   6258:   my $result=&hashref2str(\%hash);
1.265     albertel 6259:   $result=~s/^__HASH_REF__//;
                   6260:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 6261:   return $result;
                   6262: }
                   6263: 
                   6264: sub hashref2str {
                   6265:   my ($hashref)=@_;
1.265     albertel 6266:   my $result='__HASH_REF__';
1.800     albertel 6267:   foreach my $key (sort(keys(%$hashref))) {
                   6268:     if (ref($key) eq 'ARRAY') {
                   6269:       $result.=&arrayref2str($key).'=';
                   6270:     } elsif (ref($key) eq 'HASH') {
                   6271:       $result.=&hashref2str($key).'=';
                   6272:     } elsif (ref($key)) {
1.265     albertel 6273:       $result.='=';
1.800     albertel 6274:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 6275:     } else {
1.1132    raeburn  6276: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 6277:     }
                   6278: 
1.800     albertel 6279:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   6280:       $result.=&arrayref2str($hashref->{$key}).'&';
                   6281:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   6282:       $result.=&hashref2str($hashref->{$key}).'&';
                   6283:     } elsif(ref($hashref->{$key})) {
1.265     albertel 6284:        $result.='&';
1.800     albertel 6285:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 6286:     } else {
1.800     albertel 6287:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 6288:     }
                   6289:   }
1.168     albertel 6290:   $result=~s/\&$//;
1.265     albertel 6291:   $result .= '__END_HASH_REF__';
1.168     albertel 6292:   return $result;
                   6293: }
                   6294: 
                   6295: sub str2hash {
1.265     albertel 6296:     my ($string)=@_;
                   6297:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   6298:     return %$hash;
                   6299: }
                   6300: 
                   6301: sub str2hashref {
1.168     albertel 6302:   my ($string) = @_;
1.265     albertel 6303: 
                   6304:   my %hash;
                   6305: 
                   6306:   if($string !~ /^__HASH_REF__/) {
                   6307:       if (! ($string eq '' || !defined($string))) {
                   6308: 	  $hash{'error'}='Not hash reference';
                   6309:       }
                   6310:       return (\%hash, $string);
                   6311:   }
                   6312: 
                   6313:   $string =~ s/^__HASH_REF__//;
                   6314: 
                   6315:   while($string !~ /^__END_HASH_REF__/) {
                   6316:       #key
                   6317:       my $key='';
                   6318:       if($string =~ /^__HASH_REF__/) {
                   6319:           ($key, $string)=&str2hashref($string);
                   6320:           if(defined($key->{'error'})) {
                   6321:               $hash{'error'}='Bad data';
                   6322:               return (\%hash, $string);
                   6323:           }
                   6324:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6325:           ($key, $string)=&str2arrayref($string);
                   6326:           if($key->[0] eq 'Array reference error') {
                   6327:               $hash{'error'}='Bad data';
                   6328:               return (\%hash, $string);
                   6329:           }
                   6330:       } else {
                   6331:           $string =~ s/^(.*?)=//;
1.267     albertel 6332: 	  $key=&unescape($1);
1.265     albertel 6333:       }
                   6334:       $string =~ s/^=//;
                   6335: 
                   6336:       #value
                   6337:       my $value='';
                   6338:       if($string =~ /^__HASH_REF__/) {
                   6339:           ($value, $string)=&str2hashref($string);
                   6340:           if(defined($value->{'error'})) {
                   6341:               $hash{'error'}='Bad data';
                   6342:               return (\%hash, $string);
                   6343:           }
                   6344:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6345:           ($value, $string)=&str2arrayref($string);
                   6346:           if($value->[0] eq 'Array reference error') {
                   6347:               $hash{'error'}='Bad data';
                   6348:               return (\%hash, $string);
                   6349:           }
                   6350:       } else {
                   6351: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   6352:       }
                   6353:       $string =~ s/^&//;
                   6354: 
                   6355:       $hash{$key}=$value;
1.204     albertel 6356:   }
1.265     albertel 6357: 
                   6358:   $string =~ s/^__END_HASH_REF__//;
                   6359: 
                   6360:   return (\%hash, $string);
1.204     albertel 6361: }
                   6362: 
                   6363: sub str2array {
1.265     albertel 6364:     my ($string)=@_;
                   6365:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   6366:     return @$array;
                   6367: }
                   6368: 
                   6369: sub str2arrayref {
1.204     albertel 6370:   my ($string) = @_;
1.265     albertel 6371:   my @array;
                   6372: 
                   6373:   if($string !~ /^__ARRAY_REF__/) {
                   6374:       if (! ($string eq '' || !defined($string))) {
                   6375: 	  $array[0]='Array reference error';
                   6376:       }
                   6377:       return (\@array, $string);
                   6378:   }
                   6379: 
                   6380:   $string =~ s/^__ARRAY_REF__//;
                   6381: 
                   6382:   while($string !~ /^__END_ARRAY_REF__/) {
                   6383:       my $value='';
                   6384:       if($string =~ /^__HASH_REF__/) {
                   6385:           ($value, $string)=&str2hashref($string);
                   6386:           if(defined($value->{'error'})) {
                   6387:               $array[0] ='Array reference error';
                   6388:               return (\@array, $string);
                   6389:           }
                   6390:       } elsif($string =~ /^__ARRAY_REF__/) {
                   6391:           ($value, $string)=&str2arrayref($string);
                   6392:           if($value->[0] eq 'Array reference error') {
                   6393:               $array[0] ='Array reference error';
                   6394:               return (\@array, $string);
                   6395:           }
                   6396:       } else {
                   6397: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   6398:       }
                   6399:       $string =~ s/^&//;
                   6400: 
                   6401:       push(@array, $value);
1.191     harris41 6402:   }
1.265     albertel 6403: 
                   6404:   $string =~ s/^__END_ARRAY_REF__//;
                   6405: 
                   6406:   return (\@array, $string);
1.168     albertel 6407: }
                   6408: 
1.167     albertel 6409: # -------------------------------------------------------------------Temp Store
                   6410: 
1.168     albertel 6411: sub tmpreset {
                   6412:   my ($symb,$namespace,$domain,$stuname) = @_;
                   6413:   if (!$symb) {
                   6414:     $symb=&symbread();
1.620     albertel 6415:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6416:   }
                   6417:   $symb=escape($symb);
                   6418: 
1.620     albertel 6419:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 6420:   $namespace=~s/\//\_/g;
                   6421:   $namespace=~s/\W//g;
                   6422: 
1.620     albertel 6423:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6424:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6425:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6426:       $stuname=&get_requestor_ip();
1.591     albertel 6427:   }
1.1117    foxr     6428:   my $path=LONCAPA::tempdir();
1.168     albertel 6429:   my %hash;
                   6430:   if (tie(%hash,'GDBM_File',
                   6431: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6432: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  6433:     foreach my $key (keys(%hash)) {
1.180     albertel 6434:       if ($key=~ /:$symb/) {
1.168     albertel 6435: 	delete($hash{$key});
                   6436:       }
                   6437:     }
                   6438:   }
                   6439: }
                   6440: 
1.167     albertel 6441: sub tmpstore {
1.168     albertel 6442:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   6443: 
                   6444:   if (!$symb) {
                   6445:     $symb=&symbread();
1.620     albertel 6446:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6447:   }
                   6448:   $symb=escape($symb);
                   6449: 
                   6450:   if (!$namespace) {
                   6451:     # I don't think we would ever want to store this for a course.
                   6452:     # it seems this will only be used if we don't have a course.
1.620     albertel 6453:     #$namespace=$env{'request.course.id'};
1.168     albertel 6454:     #if (!$namespace) {
1.620     albertel 6455:       $namespace=$env{'request.state'};
1.168     albertel 6456:     #}
                   6457:   }
                   6458:   $namespace=~s/\//\_/g;
                   6459:   $namespace=~s/\W//g;
1.620     albertel 6460:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6461:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6462:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6463:       $stuname=&get_requestor_ip();
1.591     albertel 6464:   }
1.168     albertel 6465:   my $now=time;
                   6466:   my %hash;
1.1117    foxr     6467:   my $path=LONCAPA::tempdir();
1.168     albertel 6468:   if (tie(%hash,'GDBM_File',
                   6469: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6470: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 6471:     $hash{"version:$symb"}++;
                   6472:     my $version=$hash{"version:$symb"};
                   6473:     my $allkeys=''; 
                   6474:     foreach my $key (keys(%$storehash)) {
                   6475:       $allkeys.=$key.':';
1.591     albertel 6476:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 6477:     }
                   6478:     $hash{"$version:$symb:timestamp"}=$now;
                   6479:     $allkeys.='timestamp';
                   6480:     $hash{"$version:keys:$symb"}=$allkeys;
                   6481:     if (untie(%hash)) {
                   6482:       return 'ok';
                   6483:     } else {
                   6484:       return "error:$!";
                   6485:     }
                   6486:   } else {
                   6487:     return "error:$!";
                   6488:   }
                   6489: }
1.167     albertel 6490: 
1.168     albertel 6491: # -----------------------------------------------------------------Temp Restore
1.167     albertel 6492: 
1.168     albertel 6493: sub tmprestore {
                   6494:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 6495: 
1.168     albertel 6496:   if (!$symb) {
                   6497:     $symb=&symbread();
1.620     albertel 6498:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 6499:   }
                   6500:   $symb=escape($symb);
                   6501: 
1.620     albertel 6502:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 6503: 
1.620     albertel 6504:   if (!$domain) { $domain=$env{'user.domain'}; }
                   6505:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 6506:   if ($domain eq 'public' && $stuname eq 'public') {
1.1434    raeburn  6507:       $stuname=&get_requestor_ip();
1.591     albertel 6508:   }
1.168     albertel 6509:   my %returnhash;
                   6510:   $namespace=~s/\//\_/g;
                   6511:   $namespace=~s/\W//g;
                   6512:   my %hash;
1.1117    foxr     6513:   my $path=LONCAPA::tempdir();
1.168     albertel 6514:   if (tie(%hash,'GDBM_File',
                   6515: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 6516: 	  &GDBM_READER(),0640)) {
1.168     albertel 6517:     my $version=$hash{"version:$symb"};
                   6518:     $returnhash{'version'}=$version;
                   6519:     my $scope;
                   6520:     for ($scope=1;$scope<=$version;$scope++) {
                   6521:       my $vkeys=$hash{"$scope:keys:$symb"};
                   6522:       my @keys=split(/:/,$vkeys);
                   6523:       my $key;
                   6524:       $returnhash{"$scope:keys"}=$vkeys;
                   6525:       foreach $key (@keys) {
1.591     albertel 6526: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   6527: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 6528:       }
                   6529:     }
1.168     albertel 6530:     if (!(untie(%hash))) {
                   6531:       return "error:$!";
                   6532:     }
                   6533:   } else {
                   6534:     return "error:$!";
                   6535:   }
                   6536:   return %returnhash;
1.167     albertel 6537: }
                   6538: 
1.9       www      6539: # ----------------------------------------------------------------------- Store
                   6540: 
                   6541: sub store {
1.1269    raeburn  6542:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6543:     my $home='';
                   6544: 
1.168     albertel 6545:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6546: 
1.213     www      6547:     $symb=&symbclean($symb);
1.122     albertel 6548:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6549: 
1.620     albertel 6550:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6551:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6552: 
                   6553:     &devalidate($symb,$stuname,$domain);
1.109     www      6554: 
                   6555:     $symb=escape($symb);
1.187     www      6556:     if (!$namespace) { 
1.620     albertel 6557:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6558:           return ''; 
                   6559:        } 
                   6560:     }
1.620     albertel 6561:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6562: 
1.1434    raeburn  6563:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6564:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   6565: 
1.12      www      6566:     my $namevalue='';
1.800     albertel 6567:     foreach my $key (keys(%$storehash)) {
                   6568:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6569:     }
1.12      www      6570:     $namevalue=~s/\&$//;
1.187     www      6571:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269    raeburn  6572:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9       www      6573: }
                   6574: 
1.47      www      6575: # -------------------------------------------------------------- Critical Store
                   6576: 
                   6577: sub cstore {
1.1269    raeburn  6578:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6579:     my $home='';
                   6580: 
1.168     albertel 6581:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6582: 
1.213     www      6583:     $symb=&symbclean($symb);
1.122     albertel 6584:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6585: 
1.620     albertel 6586:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6587:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6588: 
                   6589:     &devalidate($symb,$stuname,$domain);
1.109     www      6590: 
                   6591:     $symb=escape($symb);
1.187     www      6592:     if (!$namespace) { 
1.620     albertel 6593:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6594:           return ''; 
                   6595:        } 
                   6596:     }
1.620     albertel 6597:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6598: 
1.1434    raeburn  6599:     $$storehash{'ip'}=&get_requestor_ip();
1.447     www      6600:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 6601: 
1.47      www      6602:     my $namevalue='';
1.800     albertel 6603:     foreach my $key (keys(%$storehash)) {
                   6604:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6605:     }
1.47      www      6606:     $namevalue=~s/\&$//;
1.187     www      6607:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      6608:     return critical
1.1269    raeburn  6609:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47      www      6610: }
                   6611: 
1.9       www      6612: # --------------------------------------------------------------------- Restore
                   6613: 
                   6614: sub restore {
1.124     www      6615:     my ($symb,$namespace,$domain,$stuname) = @_;
                   6616:     my $home='';
                   6617: 
1.168     albertel 6618:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6619: 
1.122     albertel 6620:     if (!$symb) {
1.1224    raeburn  6621:         return if ($namespace eq 'courserequests');
                   6622:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 6623:     } else {
1.1224    raeburn  6624:         unless ($namespace eq 'courserequests') {
                   6625:             $symb=&escape(&symbclean($symb));
                   6626:         }
1.122     albertel 6627:     }
1.188     www      6628:     if (!$namespace) { 
1.620     albertel 6629:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      6630:           return ''; 
                   6631:        } 
                   6632:     }
1.620     albertel 6633:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6634:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   6635:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 6636:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   6637: 
1.12      www      6638:     my %returnhash=();
1.800     albertel 6639:     foreach my $line (split(/\&/,$answer)) {
                   6640: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 6641:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 6642:     }
1.75      www      6643:     my $version;
                   6644:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 6645:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   6646:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 6647:        }
1.75      www      6648:     }
1.13      www      6649:     return %returnhash;
1.34      www      6650: }
                   6651: 
                   6652: # ---------------------------------------------------------- Course Description
1.1118    foxr     6653: #
                   6654: #  
1.34      www      6655: 
                   6656: sub coursedescription {
1.731     albertel 6657:     my ($courseid,$args)=@_;
1.34      www      6658:     $courseid=~s/^\///;
1.49      www      6659:     $courseid=~s/\_/\//g;
1.34      www      6660:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 6661:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 6662:     my $normalid=$cdomain.'_'.$cnum;
                   6663:     # need to always cache even if we get errors otherwise we keep 
                   6664:     # trying and trying and trying to get the course description.
                   6665:     my %envhash=();
                   6666:     my %returnhash=();
1.731     albertel 6667:     
                   6668:     my $expiretime=600;
                   6669:     if ($env{'request.course.id'} eq $normalid) {
                   6670: 	$expiretime=120;
                   6671:     }
                   6672: 
                   6673:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   6674:     if (!$args->{'freshen_cache'}
                   6675: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   6676: 	foreach my $key (keys(%env)) {
                   6677: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   6678: 	    my ($setting) = $1;
                   6679: 	    $returnhash{$setting} = $env{$key};
                   6680: 	}
                   6681: 	return %returnhash;
                   6682:     }
                   6683: 
1.1118    foxr     6684:     # get the data again
                   6685: 
1.731     albertel 6686:     if (!$args->{'one_time'}) {
                   6687: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   6688:     }
1.811     albertel 6689: 
1.34      www      6690:     if ($chome ne 'no_host') {
1.302     albertel 6691:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 6692:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     6693: 	   my $username = $env{'user.name'}; # Defult username
                   6694: 	   if(defined $args->{'user'}) {
                   6695: 	       $username = $args->{'user'};
                   6696: 	   }
1.129     albertel 6697:            $returnhash{'home'}= $chome;
                   6698: 	   $returnhash{'domain'} = $cdomain;
                   6699: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  6700:            if (!defined($returnhash{'type'})) {
                   6701:                $returnhash{'type'} = 'Course';
                   6702:            }
1.130     albertel 6703:            while (my ($name,$value) = each %returnhash) {
1.53      www      6704:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 6705:            }
1.270     www      6706:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     6707:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     6708: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      6709:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   6710:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   6711:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      6712:        }
                   6713:     }
1.731     albertel 6714:     if (!$args->{'one_time'}) {
1.949     raeburn  6715: 	&appenv(\%envhash);
1.731     albertel 6716:     }
1.302     albertel 6717:     return %returnhash;
1.461     www      6718: }
                   6719: 
1.1080    raeburn  6720: sub update_released_required {
                   6721:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   6722:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   6723:         $cid = $env{'request.course.id'};
                   6724:         $cdom = $env{'course.'.$cid.'.domain'};
                   6725:         $cnum = $env{'course.'.$cid.'.num'};
                   6726:         $chome = $env{'course.'.$cid.'.home'};
                   6727:     }
                   6728:     if ($needsrelease) {
                   6729:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   6730:         my $needsupdate;
                   6731:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   6732:             $needsupdate = 1;
                   6733:         } else {
                   6734:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   6735:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   6736:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   6737:                 $needsupdate = 1;
                   6738:             }
                   6739:         }
                   6740:         if ($needsupdate) {
                   6741:             my %needshash = (
                   6742:                              'internal.releaserequired' => $needsrelease,
                   6743:                             );
                   6744:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   6745:             if ($putresult eq 'ok') {
                   6746:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   6747:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6748:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   6749:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   6750:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   6751:                 }
                   6752:             }
                   6753:         }
                   6754:     }
                   6755:     return;
                   6756: }
                   6757: 
1.461     www      6758: # -------------------------------------------------See if a user is privileged
                   6759: 
                   6760: sub privileged {
1.1219    raeburn  6761:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 6762:     my $now = time;
1.1219    raeburn  6763:     my $roles;
                   6764:     if (ref($possroles) eq 'ARRAY') {
                   6765:         $roles = $possroles; 
                   6766:     } else {
                   6767:         $roles = ['dc','su'];
                   6768:     }
                   6769:     if (ref($possdomains) eq 'ARRAY') {
                   6770:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   6771:         foreach my $dom (@{$possdomains}) {
                   6772:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   6773:                 (ref($privileged{$dom}) eq 'HASH')) {
                   6774:                 foreach my $role (@{$roles}) {
                   6775:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6776:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   6777:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   6778:                             return 1 unless (($end && $end < $now) ||
                   6779:                                              ($start && $start > $now));
                   6780:                         }
                   6781:                     }
                   6782:                 }
                   6783:             }
                   6784:         }
                   6785:     } else {
                   6786:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   6787:         my $now = time;
1.1170    droeschl 6788: 
1.1275    musolffc 6789:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170    droeschl 6790:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  6791:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 6792:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  6793:                         or ($tstart && $tstart > $now);
1.1170    droeschl 6794:             }
1.1219    raeburn  6795:         }
                   6796:     }
                   6797:     return 0;
                   6798: }
1.1170    droeschl 6799: 
1.1219    raeburn  6800: sub privileged_by_domain {
                   6801:     my ($domains,$roles) = @_;
                   6802:     my %privileged = ();
                   6803:     my $cachetime = 60*60*24;
                   6804:     my $now = time;
                   6805:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   6806:         return %privileged;
                   6807:     }
                   6808:     foreach my $dom (@{$domains}) {
                   6809:         next if (ref($privileged{$dom}) eq 'HASH');
                   6810:         my $needroles;
                   6811:         foreach my $role (@{$roles}) {
                   6812:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   6813:             if (defined($cached)) {
                   6814:                 if (ref($result) eq 'HASH') {
                   6815:                     $privileged{$dom}{$role} = $result;
                   6816:                 }
                   6817:             } else {
                   6818:                 $needroles = 1;
                   6819:             }
                   6820:         }
                   6821:         if ($needroles) {
                   6822:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   6823:             $privileged{$dom} = {};
                   6824:             foreach my $server (keys(%dompersonnel)) {
                   6825:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   6826:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   6827:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   6828:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   6829:                         next if ($end && $end < $now);
                   6830:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   6831:                             $dompersonnel{$server}{$item};
                   6832:                     }
                   6833:                 }
                   6834:             }
                   6835:             if (ref($privileged{$dom}) eq 'HASH') {
                   6836:                 foreach my $role (@{$roles}) {
                   6837:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6838:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   6839:                     } else {
                   6840:                         my %hash = ();
                   6841:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   6842:                     }
                   6843:                 }
                   6844:             }
                   6845:         }
                   6846:     }
                   6847:     return %privileged;
1.9       www      6848: }
1.1       albertel 6849: 
1.103     harris41 6850: # -------------------------------------------------------- Get user privileges
1.11      www      6851: 
                   6852: sub rolesinit {
1.1169    droeschl 6853:     my ($domain, $username) = @_;
                   6854:     my %userroles = ('user.login.time' => time);
                   6855:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   6856: 
                   6857:     # firstaccess and timerinterval are related to timed maps/resources. 
                   6858:     # also, blocking can be triggered by an activating timer
                   6859:     # it's saved in the user's %env.
                   6860:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   6861:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   6862:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
                   6863:         %timerintchk, %timerintenv);
                   6864: 
1.1162    raeburn  6865:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 6866:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  6867:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   6868:     }
1.1169    droeschl 6869: 
1.1162    raeburn  6870:     foreach my $key (keys(%timerinterval)) {
                   6871:         my ($cid,$rest) = split(/\0/,$key);
                   6872:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   6873:     }
1.1169    droeschl 6874: 
1.11      www      6875:     my %allroles=();
1.1162    raeburn  6876:     my %allgroups=();
1.11      www      6877: 
1.1274    raeburn  6878:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169    droeschl 6879:         my $role = $rolesdump{$area};
                   6880:         $area =~ s/\_\w\w$//;
                   6881: 
                   6882:         my ($trole, $tend, $tstart, $group_privs);
                   6883: 
                   6884:         if ($role =~ /^cr/) {
                   6885:         # Custom role, defined by a user 
                   6886:         # e.g., user.role.cr/msu/smith/mynewrole
                   6887:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   6888:                 $trole = $1;
                   6889:                 ($tend, $tstart) = split('_', $2);
                   6890:             } else {
                   6891:                 $trole = $role;
                   6892:             }
                   6893:         } elsif ($role =~ m|^gr/|) {
                   6894:         # Role of member in a group, defined within a course/community
                   6895:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   6896:             ($trole, $tend, $tstart) = split(/_/, $role);
                   6897:             next if $tstart eq '-1';
                   6898:             ($trole, $group_privs) = split(/\//, $trole);
                   6899:             $group_privs = &unescape($group_privs);
                   6900:         } else {
                   6901:         # Just a normal role, defined in roles.tab
                   6902:             ($trole, $tend, $tstart) = split(/_/,$role);
                   6903:         }
                   6904: 
                   6905:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   6906:                  $username);
                   6907:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   6908: 
                   6909:         # role expired or not available yet?
                   6910:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   6911:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   6912: 
                   6913:         next if $area eq '' or $trole eq '';
                   6914: 
                   6915:         my $spec = "$trole.$area";
                   6916:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   6917: 
                   6918:         if ($trole =~ /^cr\//) {
                   6919:         # Custom role, defined by a user
                   6920:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   6921:         } elsif ($trole eq 'gr') {
                   6922:         # Role of a member in a group, defined within a course/community
                   6923:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   6924:             next;
                   6925:         } else {
                   6926:         # Normal role, defined in roles.tab
                   6927:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   6928:         }
                   6929: 
                   6930:         my $cid = $tdomain.'_'.$trest;
                   6931:         unless ($firstaccchk{$cid}) {
                   6932:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   6933:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   6934:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   6935:                         $coursetimerstarts{$cid}{$item}; 
                   6936:                 }
                   6937:             }
                   6938:             $firstaccchk{$cid} = 1;
                   6939:         }
                   6940:         unless ($timerintchk{$cid}) {
                   6941:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   6942:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   6943:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   6944:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  6945:                 }
1.12      www      6946:             }
1.1169    droeschl 6947:             $timerintchk{$cid} = 1;
1.191     harris41 6948:         }
1.11      www      6949:     }
1.1169    droeschl 6950: 
1.1341    raeburn  6951:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
                   6952:                                                           \%allroles, \%allgroups);
1.1169    droeschl 6953:     $env{'user.adv'} = $userroles{'user.adv'};
1.1341    raeburn  6954:     $env{'user.rar'} = $userroles{'user.rar'};
1.1169    droeschl 6955: 
1.1162    raeburn  6956:     return (\%userroles,\%firstaccenv,\%timerintenv);
1.11      www      6957: }
                   6958: 
1.567     raeburn  6959: sub set_arearole {
1.1215    raeburn  6960:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   6961:     unless ($nolog) {
1.567     raeburn  6962: # log the associated role with the area
1.1215    raeburn  6963:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   6964:     }
1.743     albertel 6965:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  6966: }
                   6967: 
                   6968: sub custom_roleprivs {
                   6969:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   6970:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250    raeburn  6971:     my $homsvr = &homeserver($rauthor,$rdomain);
1.838     albertel 6972:     if (&hostname($homsvr) ne '') {
1.567     raeburn  6973:         my ($rdummy,$roledef)=
                   6974:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   6975:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   6976:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   6977:             if (defined($syspriv)) {
1.1043    raeburn  6978:                 if ($trest =~ /^$match_community$/) {
                   6979:                     $syspriv =~ s/bre\&S//; 
                   6980:                 }
1.567     raeburn  6981:                 $$allroles{'cm./'}.=':'.$syspriv;
                   6982:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   6983:             }
                   6984:             if ($tdomain ne '') {
                   6985:                 if (defined($dompriv)) {
                   6986:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   6987:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   6988:                 }
                   6989:                 if (($trest ne '') && (defined($coursepriv))) {
1.1332    raeburn  6990:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
                   6991:                         my $rolename = $1;
                   6992:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
                   6993:                     }
1.567     raeburn  6994:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   6995:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   6996:                 }
                   6997:             }
                   6998:         }
                   6999:     }
                   7000: }
                   7001: 
1.1332    raeburn  7002: sub course_adhocrole_privs {
                   7003:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
                   7004:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
                   7005:     if ($overrides{"internal.adhocpriv.$rolename"}) {
                   7006:         my (%currprivs,%storeprivs);
                   7007:         foreach my $item (split(/:/,$coursepriv)) {
                   7008:             my ($priv,$restrict) = split(/\&/,$item);
                   7009:             $currprivs{$priv} = $restrict;
                   7010:         }
                   7011:         my (%possadd,%possremove,%full);
                   7012:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   7013:             my ($priv,$restrict)=split(/\&/,$item);
                   7014:             $full{$priv} = $restrict;
                   7015:         }
                   7016:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
1.1490    raeburn  7017:             next if ($item eq '');
                   7018:             my ($rule,$rest) = split(/=/,$item);
                   7019:             next unless (($rule eq 'off') || ($rule eq 'on'));
                   7020:             foreach my $priv (split(/:/,$rest)) {
                   7021:                 if ($priv ne '') {
                   7022:                     if ($rule eq 'off') {
                   7023:                         $possremove{$priv} = 1;
                   7024:                     } else {
                   7025:                         $possadd{$priv} = 1;
                   7026:                     }
                   7027:                 }
                   7028:             }
                   7029:         }
                   7030:         foreach my $priv (sort(keys(%full))) {
                   7031:             if (exists($currprivs{$priv})) {
                   7032:                 unless (exists($possremove{$priv})) {
                   7033:                     $storeprivs{$priv} = $currprivs{$priv};
                   7034:                 }
                   7035:             } elsif (exists($possadd{$priv})) {
                   7036:                 $storeprivs{$priv} = $full{$priv};
                   7037:             }
                   7038:         }
                   7039:         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
                   7040:     }
                   7041:     return $coursepriv;
1.1332    raeburn  7042: }
                   7043: 
1.678     raeburn  7044: sub group_roleprivs {
                   7045:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   7046:     my $access = 1;
                   7047:     my $now = time;
                   7048:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   7049:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   7050:     if ($access) {
1.811     albertel 7051:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  7052:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   7053:     }
                   7054: }
1.567     raeburn  7055: 
                   7056: sub standard_roleprivs {
                   7057:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   7058:     if (defined($pr{$trole.':s'})) {
                   7059:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   7060:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   7061:     }
                   7062:     if ($tdomain ne '') {
                   7063:         if (defined($pr{$trole.':d'})) {
                   7064:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   7065:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   7066:         }
                   7067:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   7068:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   7069:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   7070:         }
                   7071:     }
                   7072: }
                   7073: 
                   7074: sub set_userprivs {
1.1064    raeburn  7075:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  7076:     my $author=0;
                   7077:     my $adv=0;
1.1341    raeburn  7078:     my $rar=0;
1.678     raeburn  7079:     my %grouproles = ();
                   7080:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  7081:         my @groupkeys; 
1.1000    raeburn  7082:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  7083:             push(@groupkeys,$role);
                   7084:         }
                   7085:         if (ref($groups_roles) eq 'HASH') {
                   7086:             foreach my $key (keys(%{$groups_roles})) {
                   7087:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   7088:                     push(@groupkeys,$key);
                   7089:                 }
                   7090:             }
                   7091:         }
                   7092:         if (@groupkeys > 0) {
                   7093:             foreach my $role (@groupkeys) {
                   7094:                 my ($trole,$area,$sec,$extendedarea);
                   7095:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   7096:                     $trole = $1;
                   7097:                     $area = $2;
                   7098:                     $sec = $3;
                   7099:                     $extendedarea = $area.$sec;
                   7100:                     if (exists($$allgroups{$area})) {
                   7101:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   7102:                             my $spec = $trole.'.'.$extendedarea;
                   7103:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  7104:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  7105:                         }
1.678     raeburn  7106:                     }
                   7107:                 }
                   7108:             }
                   7109:         }
                   7110:     }
1.800     albertel 7111:     foreach my $group (keys(%grouproles)) {
                   7112:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  7113:     }
1.800     albertel 7114:     foreach my $role (keys(%{$allroles})) {
                   7115:         my %thesepriv;
1.941     raeburn  7116:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 7117:         foreach my $item (split(/:/,$$allroles{$role})) {
                   7118:             if ($item ne '') {
                   7119:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  7120:                 if ($restrictions eq '') {
                   7121:                     $thesepriv{$privilege}='F';
                   7122:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   7123:                     $thesepriv{$privilege}.=$restrictions;
                   7124:                 }
                   7125:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341    raeburn  7126:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567     raeburn  7127:             }
                   7128:         }
                   7129:         my $thesestr='';
1.1104    raeburn  7130:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 7131: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   7132: 	}
                   7133:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  7134:     }
1.1341    raeburn  7135:     return ($author,$adv,$rar);
1.567     raeburn  7136: }
                   7137: 
1.994     raeburn  7138: sub role_status {
1.1104    raeburn  7139:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  7140:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250    raeburn  7141:         my ($one,$two) = split(m{\./},$rolekey,2);
                   7142:         (undef,undef,$$role) = split(/\./,$one,3);
1.994     raeburn  7143:         unless (!defined($$role) || $$role eq '') {
1.1251    raeburn  7144:             $$where = '/'.$two;
1.994     raeburn  7145:             $$trolecode=$$role.'.'.$$where;
                   7146:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   7147:             $$tstatus='is';
1.1104    raeburn  7148:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  7149:                 $$tstatus='future';
1.1034    raeburn  7150:                 if ($$tstart<$now) {
                   7151:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  7152:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  7153:                             my (%allroles,%allgroups,$group_privs,
                   7154:                                 %groups_roles,@rolecodes);
1.1002    raeburn  7155:                             my %userroles = (
                   7156:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   7157:                             );
1.1064    raeburn  7158:                             @rolecodes = ('cm'); 
1.1002    raeburn  7159:                             my $spec=$$role.'.'.$$where;
                   7160:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   7161:                             if ($$role =~ /^cr\//) {
                   7162:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  7163:                                 push(@rolecodes,'cr');
1.1002    raeburn  7164:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  7165:                                 push(@rolecodes,$$role);
1.1002    raeburn  7166:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   7167:                                                     $env{'user.name'});
1.1064    raeburn  7168:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  7169:                                 (undef,my $group_privs) = split(/\//,$trole);
                   7170:                                 $group_privs = &unescape($group_privs);
                   7171:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  7172:                                 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  7173:                                 &get_groups_roles($tdomain,$trest,
                   7174:                                                   \%course_roles,\@rolecodes,
                   7175:                                                   \%groups_roles);
1.1002    raeburn  7176:                             } else {
1.1064    raeburn  7177:                                 push(@rolecodes,$$role);
1.1002    raeburn  7178:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   7179:                             }
1.1341    raeburn  7180:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
                   7181:                                                                    \%groups_roles);
1.1064    raeburn  7182:                             &appenv(\%userroles,\@rolecodes);
1.1342    raeburn  7183:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002    raeburn  7184:                         }
                   7185:                     }
1.1034    raeburn  7186:                     $$tstatus = 'is';
1.1002    raeburn  7187:                 }
1.994     raeburn  7188:             }
                   7189:             if ($$tend) {
1.1104    raeburn  7190:                 if ($$tend<$update) {
1.994     raeburn  7191:                     $$tstatus='expired';
                   7192:                 } elsif ($$tend<$now) {
                   7193:                     $$tstatus='will_not';
                   7194:                 }
                   7195:             }
                   7196:         }
                   7197:     }
                   7198: }
                   7199: 
1.1104    raeburn  7200: sub get_groups_roles {
                   7201:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   7202:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   7203:                   (ref($rolecodes) eq 'ARRAY') && 
                   7204:                   (ref($groups_roles) eq 'HASH')); 
                   7205:     if (keys(%{$cdom_courseroles}) > 0) {
                   7206:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   7207:         if ($cdom ne '' && $cnum ne '') {
                   7208:             foreach my $key (keys(%{$cdom_courseroles})) {
                   7209:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   7210:                     my $crsrole = $1;
                   7211:                     my $crssec = $2;
                   7212:                     if ($crsrole =~ /^cr/) {
                   7213:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   7214:                             push(@{$rolecodes},'cr');
                   7215:                         }
                   7216:                     } else {
                   7217:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   7218:                             push(@{$rolecodes},$crsrole);
                   7219:                         }
                   7220:                     }
                   7221:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   7222:                     if ($crssec ne '') {
                   7223:                         $rolekey .= "/$crssec";
                   7224:                     }
                   7225:                     $rolekey .= './';
                   7226:                     $groups_roles->{$rolekey} = $rolecodes;
                   7227:                 }
                   7228:             }
                   7229:         }
                   7230:     }
                   7231:     return;
                   7232: }
                   7233: 
                   7234: sub delete_env_groupprivs {
                   7235:     my ($where,$courseroles,$possroles) = @_;
                   7236:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   7237:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   7238:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   7239:         %{$courseroles->{$udom}} =
                   7240:             &get_my_roles('','','userroles',['active'],
                   7241:                           $possroles,[$udom],1);
                   7242:     }
                   7243:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   7244:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   7245:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   7246:             my $area = '/'.$cdom.'/'.$cnum;
                   7247:             my $privkey = "user.priv.$crsrole.$area";
                   7248:             if ($crssec ne '') {
                   7249:                 $privkey .= '/'.$crssec;
                   7250:             }
                   7251:             $privkey .= ".$area/$group";
                   7252:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   7253:         }
                   7254:     }
                   7255:     return;
                   7256: }
                   7257: 
1.994     raeburn  7258: sub check_adhoc_privs {
1.1329    raeburn  7259:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994     raeburn  7260:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329    raeburn  7261:     if ($sec) {
                   7262:         $cckey .= '/'.$sec;
                   7263:     } 
1.1185    raeburn  7264:     my $setprivs;
1.994     raeburn  7265:     if ($env{$cckey}) {
                   7266:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  7267:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  7268:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329    raeburn  7269:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7270:             $setprivs = 1;
1.994     raeburn  7271:         }
                   7272:     } else {
1.1330    raeburn  7273:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  7274:         $setprivs = 1;
1.994     raeburn  7275:     }
1.1185    raeburn  7276:     return $setprivs;
1.994     raeburn  7277: }
                   7278: 
                   7279: sub set_adhoc_privileges {
1.1326    raeburn  7280: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329    raeburn  7281:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994     raeburn  7282:     my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329    raeburn  7283:     if ($sec ne '') {
                   7284:         $area .= '/'.$sec;
                   7285:     }
1.994     raeburn  7286:     my $spec = $role.'.'.$area;
                   7287:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  7288:                                   $env{'user.name'},1);
1.1326    raeburn  7289:     my %rolehash = ();
1.1332    raeburn  7290:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
                   7291:         my $rolename = $1;
1.1326    raeburn  7292:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332    raeburn  7293:         my %domdef = &get_domain_defaults($dcdom);
                   7294:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   7295:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   7296:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
                   7297:             }
                   7298:         }
1.1326    raeburn  7299:     } else {
                   7300:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
                   7301:     }
1.1341    raeburn  7302:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994     raeburn  7303:     &appenv(\%userroles,[$role,'cm']);
1.1342    raeburn  7304:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1402    raeburn  7305:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
                   7306:             ($caller eq 'tiny')) {
1.1088    raeburn  7307:         &appenv( {'request.role'        => $spec,
                   7308:                   'request.role.domain' => $dcdom,
1.1332    raeburn  7309:                   'request.course.sec'  => $sec,
1.1088    raeburn  7310:                  }
                   7311:                );
                   7312:         my $tadv=0;
                   7313:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   7314:         &appenv({'request.role.adv'    => $tadv});
                   7315:     }
1.994     raeburn  7316: }
                   7317: 
1.12      www      7318: # --------------------------------------------------------------- get interface
                   7319: 
                   7320: sub get {
1.131     albertel 7321:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7322:    my $items='';
1.800     albertel 7323:    foreach my $item (@$storearr) {
                   7324:        $items.=&escape($item).'&';
1.191     harris41 7325:    }
1.12      www      7326:    $items=~s/\&$//;
1.620     albertel 7327:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7328:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 7329:    my $uhome=&homeserver($uname,$udomain);
                   7330: 
1.133     albertel 7331:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7332:    my @pairs=split(/\&/,$rep);
1.273     albertel 7333:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   7334:      return @pairs;
                   7335:    }
1.15      www      7336:    my %returnhash=();
1.42      www      7337:    my $i=0;
1.800     albertel 7338:    foreach my $item (@$storearr) {
                   7339:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7340:       $i++;
1.191     harris41 7341:    }
1.15      www      7342:    return %returnhash;
1.27      www      7343: }
                   7344: 
                   7345: # --------------------------------------------------------------- del interface
                   7346: 
                   7347: sub del {
1.133     albertel 7348:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      7349:    my $items='';
1.800     albertel 7350:    foreach my $item (@$storearr) {
                   7351:        $items.=&escape($item).'&';
1.191     harris41 7352:    }
1.984     neumanie 7353: 
1.27      www      7354:    $items=~s/\&$//;
1.620     albertel 7355:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7356:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7357:    my $uhome=&homeserver($uname,$udomain);
                   7358:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      7359: }
                   7360: 
                   7361: # -------------------------------------------------------------- dump interface
                   7362: 
1.1180    droeschl 7363: sub unserialize {
                   7364:     my ($rep, $escapedkeys) = @_;
                   7365: 
                   7366:     return {} if $rep =~ /^error/;
                   7367: 
                   7368:     my %returnhash=();
1.1252    raeburn  7369: 	foreach my $item (split(/\&/,$rep)) {
1.1180    droeschl 7370: 	    my ($key, $value) = split(/=/, $item, 2);
                   7371: 	    $key = unescape($key) unless $escapedkeys;
                   7372: 	    next if $key =~ /^error: 2 /;
1.1252    raeburn  7373: 	    $returnhash{$key} = &thaw_unescape($value);
1.1180    droeschl 7374: 	}
                   7375:     #return %returnhash;
                   7376:     return \%returnhash;
                   7377: }        
                   7378: 
                   7379: # see Lond::dump_with_regexp
                   7380: # if $escapedkeys hash keys won't get unescaped.
1.15      www      7381: sub dump {
1.1462    raeburn  7382:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755     albertel 7383:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7384:     if (!$uname) { $uname=$env{'user.name'}; }
                   7385:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 7386: 
1.1266    raeburn  7387:     if ($regexp) {
                   7388:         $regexp=&escape($regexp);
                   7389:     } else {
                   7390:         $regexp='.';
                   7391:     }
1.1180    droeschl 7392:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   7393:         # user is hosted on this machine
1.1266    raeburn  7394:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  7395:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 7396:         return %{unserialize($reply, $escapedkeys)};
                   7397:     }
1.1462    raeburn  7398:     my $rep;
                   7399:     if ($encrypt) {
                   7400:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.1463    raeburn  7401:     } else {
1.1462    raeburn  7402:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   7403:     }
1.755     albertel 7404:     my @pairs=split(/\&/,$rep);
                   7405:     my %returnhash=();
1.1098    foxr     7406:     if (!($rep =~ /^error/ )) {
                   7407: 	foreach my $item (@pairs) {
                   7408: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 7409:         $key = unescape($key) unless $escapedkeys;
                   7410:         #$key = &unescape($key);
1.1098    foxr     7411: 	    next if ($key =~ /^error: 2 /);
                   7412: 	    $returnhash{$key}=&thaw_unescape($value);
                   7413: 	}
1.755     albertel 7414:     }
                   7415:     return %returnhash;
1.407     www      7416: }
                   7417: 
1.1098    foxr     7418: 
1.717     albertel 7419: # --------------------------------------------------------- dumpstore interface
                   7420: 
                   7421: sub dumpstore {
                   7422:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 7423:    # same as dump but keys must be escaped. They may contain colon separated
                   7424:    # lists of values that may themself contain colons (e.g. symbs).
                   7425:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 7426: }
                   7427: 
1.407     www      7428: # -------------------------------------------------------------- keys interface
                   7429: 
                   7430: sub getkeys {
                   7431:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 7432:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7433:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      7434:    my $uhome=&homeserver($uname,$udomain);
                   7435:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   7436:    my @keyarray=();
1.800     albertel 7437:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  7438:       next if ($key =~ /^error: 2 /);
1.800     albertel 7439:       push(@keyarray,&unescape($key));
1.407     www      7440:    }
                   7441:    return @keyarray;
1.318     matthew  7442: }
                   7443: 
1.319     matthew  7444: # --------------------------------------------------------------- currentdump
                   7445: sub currentdump {
1.328     matthew  7446:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 7447:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   7448:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   7449:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  7450:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 7451:    my $rep;
                   7452: 
                   7453:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   7454:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   7455:                    $courseid)));
                   7456:    } else {
                   7457:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   7458:    }
                   7459: 
1.318     matthew  7460:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  7461:    #
1.318     matthew  7462:    my %returnhash=();
1.319     matthew  7463:    #
1.1343    raeburn  7464:    if ($rep eq 'unknown_cmd') {
1.319     matthew  7465:        # an old lond will not know currentdump
                   7466:        # Do a dump and make it look like a currentdump
1.822     albertel 7467:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  7468:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   7469:        my %hash = @tmp;
                   7470:        @tmp=();
1.424     matthew  7471:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  7472:    } else {
                   7473:        my @pairs=split(/\&/,$rep);
1.800     albertel 7474:        foreach my $pair (@pairs) {
                   7475:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  7476:            my ($symb,$param) = split(/:/,$key);
                   7477:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 7478:                                                         &thaw_unescape($value);
1.319     matthew  7479:        }
1.191     harris41 7480:    }
1.12      www      7481:    return %returnhash;
1.424     matthew  7482: }
                   7483: 
                   7484: sub convert_dump_to_currentdump{
                   7485:     my %hash = %{shift()};
                   7486:     my %returnhash;
                   7487:     # Code ripped from lond, essentially.  The only difference
                   7488:     # here is the unescaping done by lonnet::dump().  Conceivably
                   7489:     # we might run in to problems with parameter names =~ /^v\./
                   7490:     while (my ($key,$value) = each(%hash)) {
                   7491:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 7492: 	$symb  = &unescape($symb);
                   7493: 	$param = &unescape($param);
1.424     matthew  7494:         next if ($v eq 'version' || $symb eq 'keys');
                   7495:         next if (exists($returnhash{$symb}) &&
                   7496:                  exists($returnhash{$symb}->{$param}) &&
                   7497:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   7498:         $returnhash{$symb}->{$param}=$value;
                   7499:         $returnhash{$symb}->{'v.'.$param}=$v;
                   7500:     }
                   7501:     #
                   7502:     # Remove all of the keys in the hashes which keep track of
                   7503:     # the version of the parameter.
                   7504:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   7505:         # use a foreach because we are going to delete from the hash.
                   7506:         foreach my $key (keys(%$param_hash)) {
                   7507:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   7508:         }
                   7509:     }
                   7510:     return \%returnhash;
1.12      www      7511: }
                   7512: 
1.627     albertel 7513: # ------------------------------------------------------ critical inc interface
                   7514: 
                   7515: sub cinc {
                   7516:     return &inc(@_,'critical');
                   7517: }
                   7518: 
1.449     matthew  7519: # --------------------------------------------------------------- inc interface
                   7520: 
                   7521: sub inc {
1.627     albertel 7522:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 7523:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7524:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  7525:     my $uhome=&homeserver($uname,$udomain);
                   7526:     my $items='';
                   7527:     if (! ref($store)) {
                   7528:         # got a single value, so use that instead
                   7529:         $items = &escape($store).'=&';
                   7530:     } elsif (ref($store) eq 'SCALAR') {
                   7531:         $items = &escape($$store).'=&';        
                   7532:     } elsif (ref($store) eq 'ARRAY') {
                   7533:         $items = join('=&',map {&escape($_);} @{$store});
                   7534:     } elsif (ref($store) eq 'HASH') {
                   7535:         while (my($key,$value) = each(%{$store})) {
                   7536:             $items.= &escape($key).'='.&escape($value).'&';
                   7537:         }
                   7538:     }
                   7539:     $items=~s/\&$//;
1.627     albertel 7540:     if ($critical) {
                   7541: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7542:     } else {
                   7543: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7544:     }
1.449     matthew  7545: }
                   7546: 
1.12      www      7547: # --------------------------------------------------------------- put interface
                   7548: 
                   7549: sub put {
1.1462    raeburn  7550:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620     albertel 7551:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7552:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7553:    my $uhome=&homeserver($uname,$udomain);
1.12      www      7554:    my $items='';
1.800     albertel 7555:    foreach my $item (keys(%$storehash)) {
                   7556:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7557:    }
1.12      www      7558:    $items=~s/\&$//;
1.1462    raeburn  7559:    if ($encrypt) {
                   7560:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
                   7561:    } else {
                   7562:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
                   7563:    }
1.47      www      7564: }
                   7565: 
1.631     albertel 7566: # ------------------------------------------------------------ newput interface
                   7567: 
                   7568: sub newput {
                   7569:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   7570:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7571:    if (!$uname) { $uname=$env{'user.name'}; }
                   7572:    my $uhome=&homeserver($uname,$udomain);
                   7573:    my $items='';
                   7574:    foreach my $key (keys(%$storehash)) {
                   7575:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7576:    }
                   7577:    $items=~s/\&$//;
                   7578:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   7579: }
                   7580: 
                   7581: # ---------------------------------------------------------  putstore interface
                   7582: 
1.524     raeburn  7583: sub putstore {
1.1269    raeburn  7584:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620     albertel 7585:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7586:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  7587:    my $uhome=&homeserver($uname,$udomain);
                   7588:    my $items='';
1.715     albertel 7589:    foreach my $key (keys(%$storehash)) {
                   7590:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  7591:    }
1.715     albertel 7592:    $items=~s/\&$//;
1.716     albertel 7593:    my $esc_symb=&escape($symb);
                   7594:    my $esc_v=&escape($version);
1.715     albertel 7595:    my $reply =
1.716     albertel 7596:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 7597: 	      $uhome);
1.1269    raeburn  7598:    if (($tolog) && ($reply eq 'ok')) {
                   7599:        my $namevalue='';
                   7600:        foreach my $key (keys(%{$storehash})) {
                   7601:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
                   7602:        }
1.1434    raeburn  7603:        my $ip = &get_requestor_ip();
                   7604:        $namevalue .= 'ip='.&escape($ip).
1.1269    raeburn  7605:                      '&host='.&escape($perlvar{'lonHostID'}).
                   7606:                      '&version='.$esc_v.
                   7607:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
1.1494    raeburn  7608:        &courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
1.1269    raeburn  7609:    }
1.715     albertel 7610:    if ($reply eq 'unknown_cmd') {
1.716     albertel 7611:        # gfall back to way things use to be done
1.715     albertel 7612:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   7613: 			    $uname);
1.524     raeburn  7614:    }
1.715     albertel 7615:    return $reply;
                   7616: }
                   7617: 
                   7618: sub old_putstore {
1.716     albertel 7619:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   7620:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7621:     if (!$uname) { $uname=$env{'user.name'}; }
                   7622:     my $uhome=&homeserver($uname,$udomain);
                   7623:     my %newstorehash;
1.800     albertel 7624:     foreach my $item (keys(%$storehash)) {
                   7625: 	my $key = $version.':'.&escape($symb).':'.$item;
                   7626: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 7627:     }
                   7628:     my $items='';
                   7629:     my %allitems = ();
1.800     albertel 7630:     foreach my $item (keys(%newstorehash)) {
                   7631: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 7632: 	    my $key = $1.':keys:'.$2;
                   7633: 	    $allitems{$key} .= $3.':';
                   7634: 	}
1.800     albertel 7635: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 7636:     }
1.800     albertel 7637:     foreach my $item (keys(%allitems)) {
                   7638: 	$allitems{$item} =~ s/\:$//;
                   7639: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 7640:     }
                   7641:     $items=~s/\&$//;
                   7642:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  7643: }
                   7644: 
1.47      www      7645: # ------------------------------------------------------ critical put interface
                   7646: 
                   7647: sub cput {
1.134     albertel 7648:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7649:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7650:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7651:    my $uhome=&homeserver($uname,$udomain);
1.47      www      7652:    my $items='';
1.800     albertel 7653:    foreach my $item (keys(%$storehash)) {
                   7654:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7655:    }
1.47      www      7656:    $items=~s/\&$//;
1.134     albertel 7657:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7658: }
                   7659: 
                   7660: # -------------------------------------------------------------- eget interface
                   7661: 
                   7662: sub eget {
1.133     albertel 7663:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7664:    my $items='';
1.800     albertel 7665:    foreach my $item (@$storearr) {
                   7666:        $items.=&escape($item).'&';
1.191     harris41 7667:    }
1.12      www      7668:    $items=~s/\&$//;
1.620     albertel 7669:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7670:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7671:    my $uhome=&homeserver($uname,$udomain);
                   7672:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7673:    my @pairs=split(/\&/,$rep);
                   7674:    my %returnhash=();
1.42      www      7675:    my $i=0;
1.800     albertel 7676:    foreach my $item (@$storearr) {
                   7677:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7678:       $i++;
1.191     harris41 7679:    }
1.12      www      7680:    return %returnhash;
                   7681: }
                   7682: 
1.667     albertel 7683: # ------------------------------------------------------------ tmpput interface
                   7684: sub tmpput {
1.802     raeburn  7685:     my ($storehash,$server,$context)=@_;
1.667     albertel 7686:     my $items='';
1.800     albertel 7687:     foreach my $item (keys(%$storehash)) {
                   7688: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 7689:     }
                   7690:     $items=~s/\&$//;
1.802     raeburn  7691:     if (defined($context)) {
                   7692:         $items .= ':'.&escape($context);
                   7693:     }
1.667     albertel 7694:     return &reply("tmpput:$items",$server);
                   7695: }
                   7696: 
                   7697: # ------------------------------------------------------------ tmpget interface
                   7698: sub tmpget {
1.688     albertel 7699:     my ($token,$server)=@_;
                   7700:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7701:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 7702:     my %returnhash;
1.1328    raeburn  7703:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
                   7704:         return %returnhash;
                   7705:     }
1.667     albertel 7706:     foreach my $item (split(/\&/,$rep)) {
                   7707: 	my ($key,$value)=split(/=/,$item);
                   7708: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   7709:     }
                   7710:     return %returnhash;
                   7711: }
                   7712: 
1.1113    raeburn  7713: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 7714: sub tmpdel {
                   7715:     my ($token,$server)=@_;
                   7716:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7717:     return &reply("tmpdel:$token",$server);
                   7718: }
                   7719: 
1.1198    raeburn  7720: # ------------------------------------------------------------ get_timebased_id 
                   7721: 
                   7722: sub get_timebased_id {
                   7723:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   7724:         $maxtries) = @_;
                   7725:     my ($newid,$error,$dellock);
                   7726:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   7727:         return ('','ok','invalid call to get suffix');
                   7728:     }
                   7729: 
                   7730: # set defaults for any optional args for which values were not supplied
                   7731:     if ($who eq '') {
                   7732:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   7733:     }
                   7734:     if (!$locktries) {
                   7735:         $locktries = 3;
                   7736:     }
                   7737:     if (!$maxtries) {
                   7738:         $maxtries = 10;
                   7739:     }
                   7740:     
                   7741:     if (($cdom eq '') || ($cnum eq '')) {
                   7742:         if ($env{'request.course.id'}) {
                   7743:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7744:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7745:         }
                   7746:         if (($cdom eq '') || ($cnum eq '')) {
                   7747:             return ('','ok','call to get suffix not in course context');
                   7748:         }
                   7749:     }
                   7750: 
                   7751: # construct locking item
                   7752:     my $lockhash = {
                   7753:                       $prefix."\0".'locked_'.$keyid => $who,
                   7754:                    };
                   7755:     my $tries = 0;
                   7756: 
                   7757: # attempt to get lock on nohist_$namespace file
1.1494    raeburn  7758:     my $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
1.1198    raeburn  7759:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   7760:         $tries ++;
                   7761:         sleep 1;
1.1494    raeburn  7762:         $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
1.1198    raeburn  7763:     }
                   7764: 
                   7765: # attempt to get unique identifier, based on current timestamp
                   7766:     if ($gotlock eq 'ok') {
1.1494    raeburn  7767:         my %inuse = &dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
1.1198    raeburn  7768:         my $id = time;
                   7769:         $newid = $id;
1.1268    raeburn  7770:         if ($idtype eq 'addcode') {
                   7771:             $newid .= &sixnum_code();
                   7772:         }
1.1198    raeburn  7773:         my $idtries = 0;
                   7774:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   7775:             if ($idtype eq 'concat') {
                   7776:                 $newid = $id.$idtries;
1.1268    raeburn  7777:             } elsif ($idtype eq 'addcode') {
                   7778:                 $newid = $newid.&sixnum_code();
1.1198    raeburn  7779:             } else {
                   7780:                 $newid ++;
                   7781:             }
                   7782:             $idtries ++;
                   7783:         }
                   7784:         if (!exists($inuse{$prefix."\0".$newid})) {
                   7785:             my %new_item =  (
                   7786:                               $prefix."\0".$newid => $who,
                   7787:                             );
1.1494    raeburn  7788:             my $putresult = &put('nohist_'.$namespace,\%new_item,
1.1198    raeburn  7789:                                                  $cdom,$cnum);
                   7790:             if ($putresult ne 'ok') {
                   7791:                 undef($newid);
                   7792:                 $error = 'error saving new item: '.$putresult;
                   7793:             }
                   7794:         } else {
1.1268    raeburn  7795:              undef($newid);
1.1198    raeburn  7796:              $error = ('error: no unique suffix available for the new item ');
                   7797:         }
                   7798: #  remove lock
                   7799:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   7800:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   7801:     } else {
                   7802:         $error = "error: could not obtain lockfile\n";
                   7803:         $dellock = 'ok';
1.1276    raeburn  7804:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
                   7805:             $dellock = 'nolock';
                   7806:         }
1.1198    raeburn  7807:     }
                   7808:     return ($newid,$dellock,$error);
                   7809: }
                   7810: 
1.1268    raeburn  7811: sub sixnum_code {
                   7812:     my $code;
                   7813:     for (0..6) {
                   7814:         $code .= int( rand(9) );
                   7815:     }
                   7816:     return $code;
                   7817: }
                   7818: 
1.765     albertel 7819: # -------------------------------------------------- portfolio access checking
                   7820: 
                   7821: sub portfolio_access {
1.1270    raeburn  7822:     my ($requrl,$clientip) = @_;
1.765     albertel 7823:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270    raeburn  7824:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814     raeburn  7825:     if ($result) {
                   7826:         my %setters;
                   7827:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1473    raeburn  7828:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
                   7829:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
                   7830:             if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  7831:                 return 'B';
                   7832:             }
                   7833:         } else {
1.1473    raeburn  7834:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
                   7835:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
                   7836:             if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  7837:                 return 'B';
                   7838:             }
                   7839:         }
                   7840:     }
1.765     albertel 7841:     if ($result eq 'ok') {
1.766     albertel 7842:        return 'F';
1.765     albertel 7843:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 7844:        return 'A';
1.765     albertel 7845:     }
1.766     albertel 7846:     return '';
1.765     albertel 7847: }
                   7848: 
                   7849: sub get_portfolio_access {
1.1270    raeburn  7850:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767     albertel 7851: 
                   7852:     if (!ref($access_hash)) {
                   7853: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   7854: 	my %access_controls = &get_access_controls($current_perms,$group,
                   7855: 						   $file_name);
                   7856: 	$access_hash = $access_controls{$file_name};
                   7857:     }
                   7858: 
1.1270    raeburn  7859:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765     albertel 7860:     my $now = time;
                   7861:     if (ref($access_hash) eq 'HASH') {
                   7862:         foreach my $key (keys(%{$access_hash})) {
                   7863:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   7864:             if ($start > $now) {
                   7865:                 next;
                   7866:             }
                   7867:             if ($end && $end<$now) {
                   7868:                 next;
                   7869:             }
                   7870:             if ($scope eq 'public') {
                   7871:                 $public = $key;
                   7872:                 last;
                   7873:             } elsif ($scope eq 'guest') {
                   7874:                 $guest = $key;
                   7875:             } elsif ($scope eq 'domains') {
                   7876:                 push(@domains,$key);
                   7877:             } elsif ($scope eq 'users') {
                   7878:                 push(@users,$key);
                   7879:             } elsif ($scope eq 'course') {
                   7880:                 push(@courses,$key);
                   7881:             } elsif ($scope eq 'group') {
                   7882:                 push(@groups,$key);
1.1270    raeburn  7883:             } elsif ($scope eq 'ip') {
                   7884:                 push(@ips,$key);
1.765     albertel 7885:             }
                   7886:         }
                   7887:         if ($public) {
                   7888:             return 'ok';
1.1270    raeburn  7889:         } elsif (@ips > 0) {
                   7890:             my $allowed;
                   7891:             foreach my $ipkey (@ips) {
                   7892:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
                   7893:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
                   7894:                         $allowed = 1;
                   7895:                         last; 
                   7896:                     }
                   7897:                 }
                   7898:             }
                   7899:             if ($allowed) {
                   7900:                 return 'ok';
                   7901:             }
1.765     albertel 7902:         }
                   7903:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   7904:             if ($guest) {
                   7905:                 return $guest;
                   7906:             }
                   7907:         } else {
                   7908:             if (@domains > 0) {
                   7909:                 foreach my $domkey (@domains) {
                   7910:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   7911:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   7912:                             return 'ok';
                   7913:                         }
                   7914:                     }
                   7915:                 }
                   7916:             }
                   7917:             if (@users > 0) {
                   7918:                 foreach my $userkey (@users) {
1.865     raeburn  7919:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   7920:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   7921:                             if (ref($item) eq 'HASH') {
                   7922:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   7923:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   7924:                                     return 'ok';
                   7925:                                 }
                   7926:                             }
                   7927:                         }
                   7928:                     } 
1.765     albertel 7929:                 }
                   7930:             }
                   7931:             my %roleshash;
                   7932:             my @courses_and_groups = @courses;
                   7933:             push(@courses_and_groups,@groups); 
                   7934:             if (@courses_and_groups > 0) {
                   7935:                 my (%allgroups,%allroles); 
                   7936:                 my ($start,$end,$role,$sec,$group);
                   7937:                 foreach my $envkey (%env) {
1.1060    raeburn  7938:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7939:                         my $cid = $2.'_'.$3; 
                   7940:                         if ($1 eq 'gr') {
                   7941:                             $group = $4;
                   7942:                             $allgroups{$cid}{$group} = $env{$envkey};
                   7943:                         } else {
                   7944:                             if ($4 eq '') {
                   7945:                                 $sec = 'none';
                   7946:                             } else {
                   7947:                                 $sec = $4;
                   7948:                             }
                   7949:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7950:                         }
1.811     albertel 7951:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7952:                         my $cid = $2.'_'.$3;
                   7953:                         if ($4 eq '') {
                   7954:                             $sec = 'none';
                   7955:                         } else {
                   7956:                             $sec = $4;
                   7957:                         }
                   7958:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7959:                     }
                   7960:                 }
                   7961:                 if (keys(%allroles) == 0) {
                   7962:                     return;
                   7963:                 }
                   7964:                 foreach my $key (@courses_and_groups) {
                   7965:                     my %content = %{$$access_hash{$key}};
                   7966:                     my $cnum = $content{'number'};
                   7967:                     my $cdom = $content{'domain'};
                   7968:                     my $cid = $cdom.'_'.$cnum;
                   7969:                     if (!exists($allroles{$cid})) {
                   7970:                         next;
                   7971:                     }    
                   7972:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   7973:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   7974:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   7975:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   7976:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   7977:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   7978:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   7979:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   7980:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   7981:                                         if (grep/^all$/,@sections) {
                   7982:                                             return 'ok';
                   7983:                                         } else {
                   7984:                                             if (grep/^$sec$/,@sections) {
                   7985:                                                 return 'ok';
                   7986:                                             }
                   7987:                                         }
                   7988:                                     }
                   7989:                                 }
                   7990:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   7991:                                     if (grep/^none$/,@groups) {
                   7992:                                         return 'ok';
                   7993:                                     }
                   7994:                                 } else {
                   7995:                                     if (grep/^all$/,@groups) {
                   7996:                                         return 'ok';
                   7997:                                     } 
                   7998:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   7999:                                         if (grep/^$group$/,@groups) {
                   8000:                                             return 'ok';
                   8001:                                         }
                   8002:                                     }
                   8003:                                 } 
                   8004:                             }
                   8005:                         }
                   8006:                     }
                   8007:                 }
                   8008:             }
                   8009:             if ($guest) {
                   8010:                 return $guest;
                   8011:             }
                   8012:         }
                   8013:     }
                   8014:     return;
                   8015: }
                   8016: 
                   8017: sub course_group_datechecker {
                   8018:     my ($dates,$now,$status) = @_;
                   8019:     my ($start,$end) = split(/\./,$dates);
                   8020:     if (!$start && !$end) {
                   8021:         return 'ok';
                   8022:     }
                   8023:     if (grep/^active$/,@{$status}) {
                   8024:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   8025:             return 'ok';
                   8026:         }
                   8027:     }
                   8028:     if (grep/^previous$/,@{$status}) {
                   8029:         if ($end > $now ) {
                   8030:             return 'ok';
                   8031:         }
                   8032:     }
                   8033:     if (grep/^future$/,@{$status}) {
                   8034:         if ($start > $now) {
                   8035:             return 'ok';
                   8036:         }
                   8037:     }
                   8038:     return; 
                   8039: }
                   8040: 
                   8041: sub parse_portfolio_url {
                   8042:     my ($url) = @_;
                   8043: 
                   8044:     my ($type,$udom,$unum,$group,$file_name);
                   8045:     
1.823     albertel 8046:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 8047: 	$type = 1;
                   8048:         $udom = $1;
                   8049:         $unum = $2;
                   8050:         $file_name = $3;
1.823     albertel 8051:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 8052: 	$type = 2;
                   8053:         $udom = $1;
                   8054:         $unum = $2;
                   8055:         $group = $3;
                   8056:         $file_name = $3.'/'.$4;
                   8057:     }
                   8058:     if (wantarray) {
                   8059: 	return ($type,$udom,$unum,$file_name,$group);
                   8060:     }
                   8061:     return $type;
                   8062: }
                   8063: 
                   8064: sub is_portfolio_url {
                   8065:     my ($url) = @_;
                   8066:     return scalar(&parse_portfolio_url($url));
                   8067: }
                   8068: 
1.798     raeburn  8069: sub is_portfolio_file {
                   8070:     my ($file) = @_;
1.820     raeburn  8071:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  8072:         return 1;
                   8073:     }
                   8074:     return;
                   8075: }
                   8076: 
1.1504    raeburn  8077: sub is_coursetool_logo {
                   8078:     my ($uri) = @_;
                   8079:     if ($env{'request.course.id'}) {
                   8080:         my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   8081:         if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
                   8082:             return 1;
                   8083:         }
                   8084:     }
                   8085:     return;
                   8086: }
                   8087: 
1.976     raeburn  8088: sub usertools_access {
1.1084    raeburn  8089:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  8090:     my ($access,%tools);
                   8091:     if ($context eq '') {
                   8092:         $context = 'tools';
                   8093:     }
                   8094:     if ($context eq 'requestcourses') {
                   8095:         %tools = (
                   8096:                       official   => 1,
                   8097:                       unofficial => 1,
1.1006    raeburn  8098:                       community  => 1,
1.1246    raeburn  8099:                       textbook   => 1,
1.1305    raeburn  8100:                       placement  => 1,
1.1368    raeburn  8101:                       lti        => 1,
1.985     raeburn  8102:                  );
1.1183    raeburn  8103:     } elsif ($context eq 'requestauthor') {
                   8104:         %tools = (
                   8105:                       requestauthor => 1,
                   8106:                  );
1.985     raeburn  8107:     } else {
                   8108:         %tools = (
                   8109:                       aboutme   => 1,
                   8110:                       blog      => 1,
1.1177    raeburn  8111:                       webdav    => 1,
1.985     raeburn  8112:                       portfolio => 1,
1.1489    raeburn  8113:                       timezone  => 1,
1.985     raeburn  8114:                  );
                   8115:     }
1.976     raeburn  8116:     return if (!defined($tools{$tool}));
                   8117: 
1.1242    raeburn  8118:     if (($udom eq '') || ($uname eq '')) {
1.976     raeburn  8119:         $udom = $env{'user.domain'};
                   8120:         $uname = $env{'user.name'};
                   8121:     }
                   8122: 
1.978     raeburn  8123:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   8124:         if ($action ne 'reload') {
1.985     raeburn  8125:             if ($context eq 'requestcourses') {
                   8126:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  8127:             } elsif ($context eq 'requestauthor') {
                   8128:                 return $env{'environment.canrequest.author'};
1.985     raeburn  8129:             } else {
                   8130:                 return $env{'environment.availabletools.'.$tool};
                   8131:             }
                   8132:         }
1.976     raeburn  8133:     }
                   8134: 
1.1183    raeburn  8135:     my ($toolstatus,$inststatus,$envkey);
                   8136:     if ($context eq 'requestauthor') {
                   8137:         $envkey = $context; 
                   8138:     } else {
                   8139:         $envkey = $context.'.'.$tool;
                   8140:     }
1.976     raeburn  8141: 
1.985     raeburn  8142:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   8143:          ($action ne 'reload')) {
1.1183    raeburn  8144:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  8145:         $inststatus = $env{'environment.inststatus'};
                   8146:     } else {
1.1084    raeburn  8147:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  8148:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  8149:             $inststatus = $userenvref->{'inststatus'};
                   8150:         } else {
1.1183    raeburn  8151:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   8152:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  8153:             $inststatus = $userenv{'inststatus'};
                   8154:         }
1.976     raeburn  8155:     }
                   8156: 
                   8157:     if ($toolstatus ne '') {
                   8158:         if ($toolstatus) {
                   8159:             $access = 1;
                   8160:         } else {
                   8161:             $access = 0;
                   8162:         }
                   8163:         return $access;
                   8164:     }
                   8165: 
1.1084    raeburn  8166:     my ($is_adv,%domdef);
                   8167:     if (ref($is_advref) eq 'HASH') {
                   8168:         $is_adv = $is_advref->{'is_adv'};
                   8169:     } else {
                   8170:         $is_adv = &is_advanced_user($udom,$uname);
                   8171:     }
                   8172:     if (ref($domdefref) eq 'HASH') {
                   8173:         %domdef = %{$domdefref};
                   8174:     } else {
                   8175:         %domdef = &get_domain_defaults($udom);
                   8176:     }
1.976     raeburn  8177:     if (ref($domdef{$tool}) eq 'HASH') {
                   8178:         if ($is_adv) {
                   8179:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   8180:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   8181:                     $access = 1;
                   8182:                 } else {
                   8183:                     $access = 0;
                   8184:                 }
                   8185:                 return $access;
                   8186:             }
                   8187:         }
                   8188:         if ($inststatus ne '') {
                   8189:             my ($hasaccess,$hasnoaccess);
                   8190:             foreach my $affiliation (split(/:/,$inststatus)) {
                   8191:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   8192:                     if ($domdef{$tool}{$affiliation}) {
                   8193:                         $hasaccess = 1;
                   8194:                     } else {
                   8195:                         $hasnoaccess = 1;
                   8196:                     }
                   8197:                 }
                   8198:             }
                   8199:             if ($hasaccess || $hasnoaccess) {
                   8200:                 if ($hasaccess) {
                   8201:                     $access = 1;
                   8202:                 } elsif ($hasnoaccess) {
                   8203:                     $access = 0; 
                   8204:                 }
                   8205:                 return $access;
                   8206:             }
                   8207:         } else {
                   8208:             if ($domdef{$tool}{'default'} ne '') {
                   8209:                 if ($domdef{$tool}{'default'}) {
                   8210:                     $access = 1;
                   8211:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   8212:                     $access = 0;
                   8213:                 }
                   8214:                 return $access;
                   8215:             }
                   8216:         }
                   8217:     } else {
1.1177    raeburn  8218:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  8219:             $access = 1;
                   8220:         } else {
                   8221:             $access = 0;
                   8222:         }
1.976     raeburn  8223:         return $access;
                   8224:     }
                   8225: }
                   8226: 
1.1050    raeburn  8227: sub is_course_owner {
                   8228:     my ($cdom,$cnum,$udom,$uname) = @_;
                   8229:     if (($udom eq '') || ($uname eq '')) {
                   8230:         $udom = $env{'user.domain'};
                   8231:         $uname = $env{'user.name'};
                   8232:     }
                   8233:     unless (($udom eq '') || ($uname eq '')) {
                   8234:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   8235:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   8236:                 return 1;
                   8237:             } else {
1.1494    raeburn  8238:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
1.1050    raeburn  8239:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   8240:                     return 1;
                   8241:                 }
                   8242:             }
                   8243:         }
                   8244:     }
                   8245:     return;
                   8246: }
                   8247: 
1.976     raeburn  8248: sub is_advanced_user {
                   8249:     my ($udom,$uname) = @_;
1.1085    raeburn  8250:     if ($udom ne '' && $uname ne '') {
                   8251:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  8252:             if (wantarray) {
                   8253:                 return ($env{'user.adv'},$env{'user.author'});
                   8254:             } else {
                   8255:                 return $env{'user.adv'};
                   8256:             }
1.1085    raeburn  8257:         }
                   8258:     }
1.976     raeburn  8259:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   8260:     my %allroles;
1.1128    raeburn  8261:     my ($is_adv,$is_author);
1.976     raeburn  8262:     foreach my $role (keys(%roleshash)) {
                   8263:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   8264:         my $area = '/'.$tdomain.'/'.$trest;
                   8265:         if ($sec ne '') {
                   8266:             $area .= '/'.$sec;
                   8267:         }
                   8268:         if (($area ne '') && ($trole ne '')) {
                   8269:             my $spec=$trole.'.'.$area;
                   8270:             if ($trole =~ /^cr\//) {
                   8271:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   8272:             } elsif ($trole ne 'gr') {
                   8273:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   8274:             }
1.1128    raeburn  8275:             if ($trole eq 'au') {
                   8276:                 $is_author = 1;
                   8277:             }
1.976     raeburn  8278:         }
                   8279:     }
                   8280:     foreach my $role (keys(%allroles)) {
                   8281:         last if ($is_adv);
                   8282:         foreach my $item (split(/:/,$allroles{$role})) {
                   8283:             if ($item ne '') {
                   8284:                 my ($privilege,$restrictions)=split(/&/,$item);
                   8285:                 if ($privilege eq 'adv') {
                   8286:                     $is_adv = 1;
                   8287:                     last;
                   8288:                 }
                   8289:             }
                   8290:         }
                   8291:     }
1.1128    raeburn  8292:     if (wantarray) {
                   8293:         return ($is_adv,$is_author);
                   8294:     }
1.976     raeburn  8295:     return $is_adv;
                   8296: }
1.798     raeburn  8297: 
1.1035    raeburn  8298: sub check_can_request {
1.1368    raeburn  8299:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035    raeburn  8300:     my $canreq = 0;
1.1368    raeburn  8301:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   8302:         $uname = $env{'user.name'};
                   8303:         $udom = $env{'user.domain'};
                   8304:     }
1.1035    raeburn  8305:     my ($types,$typename) = &Apache::loncommon::course_types();
                   8306:     my @options = ('approval','validate','autolimit');
                   8307:     my $optregex = join('|',@options);
                   8308:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
1.1486    raeburn  8309:         my %willtrust;
1.1035    raeburn  8310:         foreach my $type (@{$types}) {
1.1368    raeburn  8311:             if (&usertools_access($uname,$udom,$type,undef,
                   8312:                                   'requestcourses')) {
1.1035    raeburn  8313:                 $canreq ++;
1.1036    raeburn  8314:                 if (ref($request_domains) eq 'HASH') {
1.1368    raeburn  8315:                     push(@{$request_domains->{$type}},$udom);
1.1036    raeburn  8316:                 }
1.1368    raeburn  8317:                 if ($dom eq $udom) {
1.1035    raeburn  8318:                     $can_request->{$type} = 1;
                   8319:                 }
                   8320:             }
1.1368    raeburn  8321:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
                   8322:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035    raeburn  8323:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   8324:                 if (@curr > 0) {
1.1036    raeburn  8325:                     foreach my $item (@curr) {
                   8326:                         if (ref($request_domains) eq 'HASH') {
                   8327:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   8328:                             if ($otherdom ne '') {
1.1486    raeburn  8329:                                 unless (exists($willtrust{$otherdom})) {
                   8330:                                     $willtrust{$otherdom} = &will_trust('reqcrs',$env{'user.domain'},$otherdom);
                   8331:                                 }
                   8332:                                 if ($willtrust{$otherdom}) {
                   8333:                                     if (ref($request_domains->{$type}) eq 'ARRAY') {
                   8334:                                         unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   8335:                                             push(@{$request_domains->{$type}},$otherdom);
                   8336:                                         }
                   8337:                                     } else {
1.1036    raeburn  8338:                                         push(@{$request_domains->{$type}},$otherdom);
                   8339:                                     }
                   8340:                                 }
                   8341:                             }
                   8342:                         }
                   8343:                     }
1.1368    raeburn  8344:                     unless ($dom eq $env{'user.domain'}) {
1.1036    raeburn  8345:                         $canreq ++;
1.1035    raeburn  8346:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   8347:                             $can_request->{$type} = 1;
                   8348:                         }
                   8349:                     }
                   8350:                 }
                   8351:             }
                   8352:         }
                   8353:     }
                   8354:     return $canreq;
                   8355: }
                   8356: 
1.341     www      8357: # ---------------------------------------------- Custom access rule evaluation
                   8358: 
                   8359: sub customaccess {
                   8360:     my ($priv,$uri)=@_;
1.807     albertel 8361:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      8362:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 8363:     $udom = &LONCAPA::clean_domain($udom);
                   8364:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      8365:     my $access=0;
1.800     albertel 8366:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 8367: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   8368: 	if ($type eq 'user') {
                   8369: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 8370: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 8371: 		if ($tdom) {
                   8372: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   8373: 		}
1.896     albertel 8374: 		if ($tuname) {
                   8375: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 8376: 		}
                   8377: 		$access=($effect eq 'allow');
                   8378: 		last;
                   8379: 	    }
                   8380: 	} else {
                   8381: 	    if ($role) {
                   8382: 		if ($role ne $urole) { next; }
                   8383: 	    }
                   8384: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   8385: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   8386: 		if ($tdom) {
                   8387: 		    if ($tdom ne $udom) { next; }
                   8388: 		}
                   8389: 		if ($tcrs) {
                   8390: 		    if ($tcrs ne $ucrs) { next; }
                   8391: 		}
                   8392: 		if ($tsec) {
                   8393: 		    if ($tsec ne $usec) { next; }
                   8394: 		}
                   8395: 		$access=($effect eq 'allow');
                   8396: 		last;
                   8397: 	    }
                   8398: 	    if ($realm eq '' && $role eq '') {
                   8399: 		$access=($effect eq 'allow');
                   8400: 	    }
1.402     bowersj2 8401: 	}
1.341     www      8402:     }
                   8403:     return $access;
                   8404: }
                   8405: 
1.103     harris41 8406: # ------------------------------------------------- Check for a user privilege
1.12      www      8407: 
                   8408: sub allowed {
1.1461    raeburn  8409:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705     albertel 8410:     my $ver_orguri=$uri;
1.439     www      8411:     $uri=&deversion($uri);
1.152     www      8412:     my $orguri=$uri;
1.52      www      8413:     $uri=&declutter($uri);
1.809     raeburn  8414: 
1.810     raeburn  8415:     if ($priv eq 'evb') {
1.1478    raeburn  8416: # Evade communication block restrictions for specified role in a course or domain
1.810     raeburn  8417:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   8418:             return $1;
                   8419:         } else {
                   8420:             return;
                   8421:         }
                   8422:     }
                   8423: 
1.620     albertel 8424:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      8425: # Free bre access to adm and meta resources
1.1436    raeburn  8426:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
1.769     albertel 8427: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   8428: 	&& ($priv eq 'bre')) {
1.14      www      8429: 	return 'F';
1.159     www      8430:     }
                   8431: 
1.545     banghart 8432: # Free bre access to user's own portfolio contents
1.714     raeburn  8433:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  8434:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  8435: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  8436:         my %setters;
1.1473    raeburn  8437:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
                   8438:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
                   8439:         if (($startblock && $endblock) || ($by_ip)) {
1.814     raeburn  8440:             return 'B';
                   8441:         } else {
                   8442:             return 'F';
                   8443:         }
1.545     banghart 8444:     }
                   8445: 
1.762     raeburn  8446: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  8447:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   8448:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   8449:         if (exists($env{'request.course.id'})) {
                   8450:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8451:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8452:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   8453:                 my $courseprivid=$env{'request.course.id'};
                   8454:                 $courseprivid=~s/\_/\//;
                   8455:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   8456:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   8457:                     return $1; 
1.762     raeburn  8458:                 } else {
                   8459:                     if ($env{'request.course.sec'}) {
                   8460:                         $courseprivid.='/'.$env{'request.course.sec'};
                   8461:                     }
                   8462:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   8463:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   8464:                         return $2;
                   8465:                     }
1.714     raeburn  8466:                 }
                   8467:             }
                   8468:         }
                   8469:     }
                   8470: 
1.159     www      8471: # Free bre to public access
                   8472: 
                   8473:     if ($priv eq 'bre') {
1.1362    raeburn  8474:         my $copyright;
                   8475:         unless ($uri =~ /ext\.tool/) {
                   8476:             $copyright=&metadata($uri,'copyright');
                   8477:         }
1.620     albertel 8478: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      8479:            return 'F'; 
                   8480:         }
1.238     www      8481:         if ($copyright eq 'priv') {
                   8482:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8483: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      8484: 		return '';
                   8485:             }
                   8486:         }
                   8487:         if ($copyright eq 'domain') {
                   8488:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 8489: 	    unless (($env{'user.domain'} eq $1) ||
                   8490:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      8491: 		return '';
                   8492:             }
1.262     matthew  8493:         }
1.620     albertel 8494:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  8495:             # Library role, so allow browsing of resources in this domain.
                   8496:             return 'F';
1.238     www      8497:         }
1.341     www      8498:         if ($copyright eq 'custom') {
                   8499: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   8500:         }
1.14      www      8501:     }
1.264     matthew  8502:     # Domain coordinator is trying to create a course
1.620     albertel 8503:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  8504:         # uri is the requested domain in this case.
                   8505:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  8506:         # a role of dc for the domain in question.
1.620     albertel 8507:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  8508:     }
1.29      www      8509: 
1.52      www      8510:     my $thisallowed='';
                   8511:     my $statecond=0;
                   8512:     my $courseprivid='';
                   8513: 
1.1039    raeburn  8514:     my $ownaccess;
1.1043    raeburn  8515:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  8516:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   8517:         if ($uri eq '') {
                   8518:             $ownaccess = 1;
                   8519:         } else {
                   8520:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   8521:                 my $udom = $env{'user.domain'};
                   8522:                 my $uname = $env{'user.name'};
                   8523:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   8524:                     $ownaccess = 1;
1.1040    raeburn  8525:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  8526:                     unless ($uri =~ m{\.\./}) {
                   8527:                         $ownaccess = 1;
                   8528:                     }
                   8529:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   8530:                     my $now = time;
                   8531:                     if ($uri =~ m{^([^/]+)/?$}) {
                   8532:                         my $adom = $1;
                   8533:                         foreach my $key (keys(%env)) {
1.1042    raeburn  8534:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1452    raeburn  8535:                                 my ($start,$end) = split(/\./,$env{$key});
                   8536:                                 if (($now >= $start) && (!$end || $end > $now)) {
1.1039    raeburn  8537:                                     $ownaccess = 1;
                   8538:                                     last;
                   8539:                                 }
                   8540:                             }
                   8541:                         }
                   8542:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   8543:                         my $adom = $1;
                   8544:                         my $aname = $2;
1.1042    raeburn  8545:                         foreach my $role ('ca','aa') { 
                   8546:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   8547:                                 my ($start,$end) =
1.1452    raeburn  8548:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
                   8549:                                 if (($now >= $start) && (!$end || $end > $now)) {
1.1042    raeburn  8550:                                     $ownaccess = 1;
                   8551:                                     last;
                   8552:                                 }
1.1039    raeburn  8553:                             }
                   8554:                         }
                   8555:                     }
                   8556:                 }
                   8557:             }
                   8558:         }
                   8559:     }
                   8560: 
1.52      www      8561: # Course
                   8562: 
1.620     albertel 8563:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8564:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8565:             $thisallowed.=$1;
                   8566:         }
1.52      www      8567:     }
1.29      www      8568: 
1.52      www      8569: # Domain
                   8570: 
1.620     albertel 8571:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 8572:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8573:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8574:             $thisallowed.=$1;
                   8575:         }
1.12      www      8576:     }
1.52      www      8577: 
1.1141    raeburn  8578: # User who is not author or co-author might still be able to edit
                   8579: # resource of an author in the domain (e.g., if Domain Coordinator).
                   8580:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   8581:         (&allowed('mdc',$env{'request.course.id'}))) {
                   8582:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   8583:             $thisallowed.=$1;
                   8584:         }
                   8585:     }
                   8586: 
1.52      www      8587: # Course: uri itself is a course
1.66      www      8588:     my $courseuri=$uri;
                   8589:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      8590:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      8591: 
1.620     albertel 8592:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 8593:        =~/\Q$priv\E\&([^\:]*)/) {
1.1409    raeburn  8594:         if ($priv eq 'mip') {
                   8595:             my $rem = $1;
                   8596:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
                   8597:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
                   8598:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8599:                 if ($cdom ne '') {
1.1410    raeburn  8600:                     my %passwdconf = &get_passwdconf($cdom);
                   8601:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
                   8602:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
                   8603:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
                   8604:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
                   8605:                                 unless (@inststatuses) {
                   8606:                                     @inststatuses = ('default');
                   8607:                                 }
                   8608:                                 foreach my $status (@inststatuses) {
                   8609:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
                   8610:                                         $thisallowed.=$rem;
                   8611:                                     }
                   8612:                                 }
                   8613:                             }
                   8614:                         }
1.1409    raeburn  8615:                     }
                   8616:                 }
                   8617:             }
                   8618:         } else {
                   8619:             unless (($priv eq 'bro') && (!$ownaccess)) {
                   8620:                 $thisallowed.=$1;
                   8621:             }
1.1039    raeburn  8622:         }
1.12      www      8623:     }
1.29      www      8624: 
1.665     albertel 8625: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 8626: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 8627:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 8628: 	$thisallowed='';
1.671     raeburn  8629:         my ($match)=&is_on_map($uri);
                   8630:         if ($match) {
                   8631:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   8632:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1281    raeburn  8633:                 my $value = $1;
1.1461    raeburn  8634:                 my $deeplinkblock;
                   8635:                 unless ($nodeeplinkcheck) {
                   8636:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8637:                 }
1.1402    raeburn  8638:                 if ($deeplinkblock) {
                   8639:                     $thisallowed='D';
                   8640:                 } elsif ($noblockcheck) {
1.1281    raeburn  8641:                     $thisallowed.=$value;
1.1162    raeburn  8642:                 } else {
1.1424    raeburn  8643:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8644:                     if (@blockers > 0) {
                   8645:                         $thisallowed = 'B';
                   8646:                     } else {
                   8647:                         $thisallowed.=$value;
                   8648:                     }
1.1162    raeburn  8649:                 }
1.671     raeburn  8650:             }
                   8651:         } else {
1.705     albertel 8652:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  8653:             if ($refuri) {
                   8654:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  8655:                     $thisallowed='F';
1.671     raeburn  8656:                 } else {
                   8657:                     $refuri=&declutter($refuri);
                   8658:                     my ($match) = &is_on_map($refuri);
                   8659:                     if ($match) {
1.1461    raeburn  8660:                         my $deeplinkblock;
                   8661:                         unless ($nodeeplinkcheck) {
                   8662:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8663:                         }
1.1402    raeburn  8664:                         if ($deeplinkblock) {
                   8665:                             $thisallowed='D';
                   8666:                         } elsif ($noblockcheck) {
1.1281    raeburn  8667:                             $thisallowed='F';
1.1162    raeburn  8668:                         } else {
1.1426    raeburn  8669:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  8670:                             if (@blockers > 0) {
                   8671:                                 $thisallowed = 'B';
                   8672:                             } else {
                   8673:                                 $thisallowed='F';
                   8674:                             }
1.1162    raeburn  8675:                         }
1.671     raeburn  8676:                     }
1.669     raeburn  8677:                 }
1.671     raeburn  8678:             }
                   8679:         }
1.314     www      8680:     }
1.492     albertel 8681: 
1.766     albertel 8682:     if ($priv eq 'bre'
                   8683: 	&& $thisallowed ne 'F' 
                   8684: 	&& $thisallowed ne '2'
                   8685: 	&& &is_portfolio_url($uri)) {
1.1270    raeburn  8686: 	$thisallowed = &portfolio_access($uri,$clientip);
1.766     albertel 8687:     }
1.1250    raeburn  8688: 
1.52      www      8689: # Full access at system, domain or course-wide level? Exit.
1.29      www      8690:     if ($thisallowed=~/F/) {
                   8691: 	return 'F';
                   8692:     }
                   8693: 
1.52      www      8694: # If this is generating or modifying users, exit with special codes
1.29      www      8695: 
1.643     www      8696:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   8697: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 8698: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      8699: # no author name given, so this just checks on the general right to make a co-author in this domain
                   8700: 	    unless ($auname) { return $thisallowed; }
                   8701: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 8702: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   8703: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   8704: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   8705: 	}
1.52      www      8706: 	return $thisallowed;
                   8707:     }
                   8708: #
1.103     harris41 8709: # Gathered so far: system, domain and course wide privileges
1.52      www      8710: #
                   8711: # Course: See if uri or referer is an individual resource that is part of 
                   8712: # the course
                   8713: 
1.620     albertel 8714:     if ($env{'request.course.id'}) {
1.232     www      8715: 
1.1504    raeburn  8716:         if ($priv eq 'bre') {
                   8717:             if (&is_coursetool_logo($uri)) {
                   8718:                 return 'F';
                   8719:             }
                   8720:         }
                   8721: 
1.1409    raeburn  8722: # If this is modifying password (internal auth) domains must match for user and user's role.
                   8723: 
                   8724:         if ($priv eq 'mip') {
                   8725:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
                   8726:                 return $thisallowed;
                   8727:             } else {
                   8728:                 return '';
                   8729:             }
                   8730:         }
                   8731: 
1.620     albertel 8732:        $courseprivid=$env{'request.course.id'};
                   8733:        if ($env{'request.course.sec'}) {
                   8734:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      8735:        }
                   8736:        $courseprivid=~s/\_/\//;
                   8737:        my $checkreferer=1;
1.232     www      8738:        my ($match,$cond)=&is_on_map($uri);
                   8739:        if ($match) {
                   8740:            $statecond=$cond;
1.620     albertel 8741:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8742:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8743:                my $value = $1;
                   8744:                if ($priv eq 'bre') {
1.1461    raeburn  8745:                    my $deeplinkblock;
                   8746:                    unless ($nodeeplinkcheck) {
                   8747:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8748:                    }
1.1458    raeburn  8749:                    if ($deeplinkblock) {
                   8750:                        $thisallowed = 'D';
                   8751:                    } elsif ($noblockcheck) {
1.1281    raeburn  8752:                        $thisallowed.=$value;
1.1162    raeburn  8753:                    } else {
1.1424    raeburn  8754:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1281    raeburn  8755:                        if (@blockers > 0) {
                   8756:                            $thisallowed = 'B';
                   8757:                        } else {
                   8758:                            $thisallowed.=$value;
                   8759:                        }
1.1162    raeburn  8760:                    }
                   8761:                } else {
                   8762:                    $thisallowed.=$value;
                   8763:                }
1.52      www      8764:                $checkreferer=0;
                   8765:            }
1.29      www      8766:        }
1.1424    raeburn  8767: 
1.148     www      8768:        if ($checkreferer) {
1.620     albertel 8769: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      8770:             unless ($refuri) {
1.800     albertel 8771:                 foreach my $key (keys(%env)) {
                   8772: 		    if ($key=~/^httpref\..*\*/) {
                   8773: 			my $pattern=$key;
1.156     www      8774:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      8775:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   8776:                         $pattern=~s/\//\\\//g;
1.152     www      8777:                         if ($orguri=~/$pattern/) {
1.800     albertel 8778: 			    $refuri=$env{$key};
1.148     www      8779:                         }
                   8780:                     }
1.191     harris41 8781:                 }
1.148     www      8782:             }
1.232     www      8783: 
1.148     www      8784:          if ($refuri) { 
1.152     www      8785: 	  $refuri=&declutter($refuri);
1.232     www      8786:           my ($match,$cond)=&is_on_map($refuri);
                   8787:             if ($match) {
                   8788:               my $refstatecond=$cond;
1.620     albertel 8789:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8790:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8791:                   my $value = $1;
                   8792:                   if ($priv eq 'bre') {
1.1461    raeburn  8793:                       my $deeplinkblock;
                   8794:                       unless ($nodeeplinkcheck) {
                   8795:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8796:                       }
1.1402    raeburn  8797:                       if ($deeplinkblock) {
                   8798:                           $thisallowed = 'D';
                   8799:                       } elsif ($noblockcheck) {
1.1281    raeburn  8800:                           $thisallowed.=$value;
1.1162    raeburn  8801:                       } else {
1.1426    raeburn  8802:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1281    raeburn  8803:                           if (@blockers > 0) {
                   8804:                               $thisallowed = 'B';
                   8805:                           } else {
                   8806:                               $thisallowed.=$value;
                   8807:                           }
1.1162    raeburn  8808:                       }
                   8809:                   } else {
                   8810:                       $thisallowed.=$value;
                   8811:                   }
1.53      www      8812:                   $uri=$refuri;
                   8813:                   $statecond=$refstatecond;
1.52      www      8814:               }
                   8815:           }
1.148     www      8816:         }
1.29      www      8817:        }
1.52      www      8818:    }
1.29      www      8819: 
1.52      www      8820: #
1.103     harris41 8821: # Gathered now: all privileges that could apply, and condition number
1.52      www      8822: # 
                   8823: #
                   8824: # Full or no access?
                   8825: #
1.29      www      8826: 
1.52      www      8827:     if ($thisallowed=~/F/) {
                   8828: 	return 'F';
                   8829:     }
1.29      www      8830: 
1.52      www      8831:     unless ($thisallowed) {
                   8832:         return '';
                   8833:     }
1.29      www      8834: 
1.52      www      8835: # Restrictions exist, deal with them
                   8836: #
                   8837: #   C:according to course preferences
                   8838: #   R:according to resource settings
                   8839: #   L:unless locked
                   8840: #   X:according to user session state
                   8841: #
                   8842: 
                   8843: # Possibly locked functionality, check all courses
1.1454    raeburn  8844: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54      www      8845: # Locks might take effect only after 10 minutes cache expiration for other
1.1454    raeburn  8846: # courses, and 2 minutes for current course, in which user has st or ta role
                   8847: # which is neither expired nor a future role (unless current course).
1.52      www      8848: 
1.1454    raeburn  8849:     my ($needlockcheck,$now,$crsonly);
1.52      www      8850:     if ($thisallowed=~/L/) {
1.1454    raeburn  8851:         $now = time;
                   8852:         if ($priv eq 'bre') {
                   8853:             if ($uri ne '') {
                   8854:                 if ($orguri =~ m{^/+res/}) {
                   8855:                     if ($uri =~ m{^lib/templates/}) {
                   8856:                         if ($env{'request.course.id'}) {
                   8857:                             $crsonly = 1;
                   8858:                             $needlockcheck = 1;
                   8859:                         }
                   8860:                     } else {
                   8861:                         $needlockcheck = 1;
                   8862:                     }
                   8863:                 } elsif ($env{'request.course.id'}) {
                   8864:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
                   8865:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
                   8866:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
                   8867:                         $crsonly = 1;
                   8868:                     }
                   8869:                     $needlockcheck = 1;
                   8870:                 }
                   8871:             }
                   8872:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
                   8873:             $needlockcheck = 1;
                   8874:         }
                   8875:     }
                   8876:     if ($needlockcheck) {
1.1453    raeburn  8877:         foreach my $envkey (keys(%env)) {
1.54      www      8878:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   8879:                my $courseid=$2;
                   8880:                my $roleid=$1.'.'.$2;
1.92      www      8881:                $courseid=~s/^\///;
1.1453    raeburn  8882:                unless ($env{'request.role'} eq $roleid) {
                   8883:                    my ($start,$end) = split(/\./,$env{$envkey});
                   8884:                    next unless (($now >= $start) && (!$end || $end > $now));
                   8885:                }
1.54      www      8886:                my $expiretime=600;
1.620     albertel 8887:                if ($env{'request.role'} eq $roleid) {
1.54      www      8888: 		  $expiretime=120;
                   8889:                }
                   8890: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   8891:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 8892:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 8893: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      8894:                }
1.620     albertel 8895:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8896:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   8897: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   8898:                        &log($env{'user.domain'},$env{'user.name'},
                   8899:                             $env{'user.home'},
1.57      www      8900:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      8901:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8902:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8903: 		       return '';
                   8904:                    }
                   8905:                }
1.620     albertel 8906:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8907:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1455    raeburn  8908: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620     albertel 8909:                        &log($env{'user.domain'},$env{'user.name'},
                   8910:                             $env{'user.home'},
1.57      www      8911:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      8912:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8913:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8914: 		       return '';
                   8915:                    }
                   8916:                }
                   8917: 	   }
1.29      www      8918:        }
1.52      www      8919:     }
1.1424    raeburn  8920: 
1.52      www      8921: #
                   8922: # Rest of the restrictions depend on selected course
                   8923: #
                   8924: 
1.620     albertel 8925:     unless ($env{'request.course.id'}) {
1.766     albertel 8926: 	if ($thisallowed eq 'A') {
                   8927: 	    return 'A';
1.814     raeburn  8928:         } elsif ($thisallowed eq 'B') {
                   8929:             return 'B';
1.766     albertel 8930: 	} else {
                   8931: 	    return '1';
                   8932: 	}
1.52      www      8933:     }
1.29      www      8934: 
1.52      www      8935: #
                   8936: # Now user is definitely in a course
                   8937: #
1.53      www      8938: 
                   8939: 
                   8940: # Course preferences
                   8941: 
                   8942:    if ($thisallowed=~/C/) {
1.620     albertel 8943:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   8944:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   8945:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 8946: 	   =~/\Q$rolecode\E/) {
1.1304    raeburn  8947: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8948: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8949: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   8950: 			$env{'request.course.id'});
                   8951: 	   }
1.237     www      8952:            return '';
                   8953:        }
                   8954: 
1.620     albertel 8955:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 8956: 	   =~/\Q$unamedom\E/) {
1.1304    raeburn  8957: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8958: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   8959: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   8960: 			$env{'request.course.id'});
                   8961: 	   }
1.54      www      8962:            return '';
                   8963:        }
1.53      www      8964:    }
                   8965: 
                   8966: # Resource preferences
                   8967: 
                   8968:    if ($thisallowed=~/R/) {
1.620     albertel 8969:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 8970:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  8971: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 8972: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8973: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   8974: 	   }
                   8975: 	   return '';
1.54      www      8976:        }
1.53      www      8977:    }
1.30      www      8978: 
1.1461    raeburn  8979: # Restricted for deeplinked session?
                   8980: 
                   8981:     if ($env{'request.deeplink.login'}) {
                   8982:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
                   8983:             if (!$symb) { $symb=&symbread($uri,1); }
                   8984:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
                   8985:                 return '';
                   8986:             }
                   8987:         }
                   8988:     }
                   8989: 
1.246     www      8990: # Restricted by state or randomout?
1.30      www      8991: 
1.52      www      8992:    if ($thisallowed=~/X/) {
1.620     albertel 8993:       if ($env{'acc.randomout'}) {
1.579     albertel 8994: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 8995:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      8996:             return ''; 
                   8997:          }
1.247     www      8998:       }
                   8999:       if (&condval($statecond)) {
1.52      www      9000: 	 return '2';
                   9001:       } else {
                   9002:          return '';
                   9003:       }
                   9004:    }
1.30      www      9005: 
1.766     albertel 9006:     if ($thisallowed eq 'A') {
                   9007: 	return 'A';
1.814     raeburn  9008:     } elsif ($thisallowed eq 'B') {
                   9009:         return 'B';
1.1402    raeburn  9010:     } elsif ($thisallowed eq 'D') {
                   9011:         return 'D';
1.766     albertel 9012:     }
1.52      www      9013:    return 'F';
1.232     www      9014: }
1.1162    raeburn  9015: 
1.1192    raeburn  9016: # ------------------------------------------- Check construction space access
                   9017: 
                   9018: sub constructaccess {
                   9019:     my ($url,$setpriv)=@_;
                   9020: 
                   9021: # We do not allow editing of previous versions of files
                   9022:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   9023: 
                   9024: # Get username and domain from URL
                   9025:     my ($ownername,$ownerdomain,$ownerhome);
                   9026: 
                   9027:     ($ownerdomain,$ownername) =
1.1325    raeburn  9028:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192    raeburn  9029: 
                   9030: # The URL does not really point to any authorspace, forget it
                   9031:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   9032: 
                   9033: # Now we need to see if the user has access to the authorspace of
                   9034: # $ownername at $ownerdomain
                   9035: 
                   9036:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   9037: # Real author for this?
                   9038:        $ownerhome = $env{'user.home'};
                   9039:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   9040:           return ($ownername,$ownerdomain,$ownerhome);
                   9041:        }
1.1506    raeburn  9042:     } elsif (&is_course($ownerdomain,$ownername)) {
                   9043: # Course Authoring Space?
1.1312    raeburn  9044:         if ($env{'request.course.id'}) {
                   9045:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
                   9046:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
                   9047:                 if (&allowed('mdc',$env{'request.course.id'})) {
                   9048:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
                   9049:                     return ($ownername,$ownerdomain,$ownerhome);
                   9050:                 }
                   9051:             }
                   9052:         }
1.1506    raeburn  9053:         return '';
                   9054:     } else {
                   9055: # Co-author for this?
                   9056:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   9057:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   9058:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   9059:             return ($ownername,$ownerdomain,$ownerhome);
                   9060:         }
1.1192    raeburn  9061:     }
                   9062: 
                   9063: # We don't have any access right now. If we are not possibly going to do anything about this,
                   9064: # we might as well leave
                   9065:    unless ($setpriv) { return ''; }
                   9066: 
                   9067: # Backdoor access?
                   9068:     my $allowed=&allowed('eco',$ownerdomain);
                   9069: # Nope
                   9070:     unless ($allowed) { return ''; }
                   9071: # Looks like we may have access, but could be locked by the owner of the construction space
                   9072:     if ($allowed eq 'U') {
                   9073:         my %blocked=&get('environment',['domcoord.author'],
                   9074:                          $ownerdomain,$ownername);
                   9075: # Is blocked by owner
                   9076:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   9077:     }
                   9078:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   9079: # Grant temporary access
                   9080:         my $then=$env{'user.login.time'};
1.1209    raeburn  9081:         my $update=$env{'user.update.time'};
1.1192    raeburn  9082:         if (!$update) { $update = $then; }
                   9083:         my $refresh=$env{'user.refresh.time'};
                   9084:         if (!$refresh) { $refresh = $update; }
                   9085:         my $now = time;
                   9086:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   9087:                            $now,'ca','constructaccess');
                   9088:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   9089:         return($ownername,$ownerdomain,$ownerhome);
                   9090:     }
                   9091: # No business here
                   9092:     return '';
                   9093: }
                   9094: 
1.1282    raeburn  9095: # ----------------------------------------------------------- Content Blocking
                   9096: 
                   9097: {
                   9098: # Caches for faster Course Contents display where content blocking
                   9099: # is in operation (i.e., interval param set) for timed quiz.
                   9100: #
                   9101: # User for whom data are being temporarily cached.
                   9102: my $cacheduser='';
1.1424    raeburn  9103: # Course for which data are being temporarily cached.
                   9104: my $cachedcid='';
1.1282    raeburn  9105: # Cached blockers for this user (a hash of blocking items). 
                   9106: my %cachedblockers=();
                   9107: # When the data were last cached.
                   9108: my $cachedlast='';
                   9109: 
                   9110: sub load_all_blockers {
1.1427    raeburn  9111:     my ($uname,$udom)=@_;
1.1282    raeburn  9112:     if (($uname ne '') && ($udom ne '')) { 
                   9113:         if (($cacheduser eq $uname.':'.$udom) &&
1.1424    raeburn  9114:             ($cachedcid eq $env{'request.course.id'}) &&
1.1427    raeburn  9115:             (abs($cachedlast-time)<5)) {
1.1282    raeburn  9116:             return;
                   9117:         }
                   9118:     }
                   9119:     $cachedlast=time;
                   9120:     $cacheduser=$uname.':'.$udom;
1.1424    raeburn  9121:     $cachedcid=$env{'request.course.id'};
1.1427    raeburn  9122:     %cachedblockers = &get_commblock_resources();
1.1424    raeburn  9123:     return;
1.1282    raeburn  9124: }
                   9125: 
1.1162    raeburn  9126: sub get_comm_blocks {
                   9127:     my ($cdom,$cnum) = @_;
                   9128:     if ($cdom eq '' || $cnum eq '') {
                   9129:         return unless ($env{'request.course.id'});
                   9130:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9131:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9132:     }
                   9133:     my %commblocks;
                   9134:     my $hashid=$cdom.'_'.$cnum;
                   9135:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   9136:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   9137:         %commblocks = %{$blocksref};
                   9138:     } else {
1.1494    raeburn  9139:         %commblocks = &dump('comm_block',$cdom,$cnum);
1.1162    raeburn  9140:         my $cachetime = 600;
                   9141:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   9142:     }
                   9143:     return %commblocks;
                   9144: }
                   9145: 
1.1282    raeburn  9146: sub get_commblock_resources {
                   9147:     my ($blocks) = @_;
                   9148:     my %blockers = ();
                   9149:     return %blockers unless ($env{'request.course.id'});
1.1470    raeburn  9150:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   9151:     if ($env{'request.course.sec'}) {
                   9152:         $courseurl .= '/'.$env{'request.course.sec'};
                   9153:     }
                   9154:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162    raeburn  9155:     my %commblocks;
                   9156:     if (ref($blocks) eq 'HASH') {
                   9157:         %commblocks = %{$blocks};
                   9158:     } else {
                   9159:         %commblocks = &get_comm_blocks();
                   9160:     }
1.1282    raeburn  9161:     return %blockers unless (keys(%commblocks) > 0); 
                   9162:     my $navmap = Apache::lonnavmaps::navmap->new();
                   9163:     return %blockers unless (ref($navmap));
1.1162    raeburn  9164:     my $now = time;
                   9165:     foreach my $block (keys(%commblocks)) {
                   9166:         if ($block =~ /^(\d+)____(\d+)$/) {
                   9167:             my ($start,$end) = ($1,$2);
                   9168:             if ($start <= $now && $end >= $now) {
                   9169:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   9170:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   9171:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282    raeburn  9172:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   9173:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
1.1162    raeburn  9174:                             }
                   9175:                         }
                   9176:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282    raeburn  9177:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   9178:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162    raeburn  9179:                             }
                   9180:                         }
                   9181:                     }
                   9182:                 }
                   9183:             }
                   9184:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   9185:             my $item = $1;
                   9186:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   9187:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1471    raeburn  9188:                     my (@interval,$mapname);
1.1282    raeburn  9189:                     my $type = 'map';
                   9190:                     if ($item eq 'course') {
                   9191:                         $type = 'course';
                   9192:                         @interval=&EXT("resource.0.interval");
                   9193:                     } else {
                   9194:                         if ($item =~ /___\d+___/) {
                   9195:                             $type = 'resource';
                   9196:                             @interval=&EXT("resource.0.interval",$item);
1.1162    raeburn  9197:                         } else {
1.1471    raeburn  9198:                             $mapname = &deversion($item);
                   9199:                             if (ref($navmap)) {
                   9200:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
                   9201:                                 @interval = ($timelimit,'map');
1.1162    raeburn  9202:                             }
                   9203:                         }
1.1282    raeburn  9204:                     }
1.1310    raeburn  9205:                     if ($interval[0] =~ /^(\d+)/) {
1.1308    raeburn  9206:                         my $timelimit = $1; 
1.1282    raeburn  9207:                         my $first_access;
                   9208:                         if ($type eq 'resource') {
                   9209:                             $first_access=&get_first_access($interval[1],$item);
                   9210:                         } elsif ($type eq 'map') {
                   9211:                             $first_access=&get_first_access($interval[1],undef,$item);
                   9212:                         } else {
                   9213:                             $first_access=&get_first_access($interval[1]);
                   9214:                         }
                   9215:                         if ($first_access) {
1.1292    raeburn  9216:                             my $timesup = $first_access+$timelimit;
1.1282    raeburn  9217:                             if ($timesup > $now) {
                   9218:                                 my $activeblock;
1.1471    raeburn  9219:                                 if ($type eq 'resource') {
                   9220:                                     if (ref($navmap)) {
                   9221:                                         my $res = $navmap->getBySymb($item);
                   9222:                                         if ($res->answerable()) {
                   9223:                                             $activeblock = 1;
                   9224:                                         }
                   9225:                                     }
                   9226:                                 } elsif ($type eq 'map') {
                   9227:                                     my $mapsymb = &symbread($mapname,1);
                   9228:                                     if (($mapsymb) && (ref($navmap))) {
                   9229:                                         my $mapres = $navmap->getBySymb($mapsymb);
                   9230:                                         if (ref($mapres)) {
                   9231:                                             my $first = $mapres->map_start();
                   9232:                                             my $finish = $mapres->map_finish();
                   9233:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
                   9234:                                             if (ref($it)) {
                   9235:                                                 my $res;
                   9236:                                                 while ($res = $it->next(undef,1)) {
                   9237:                                                     next unless (ref($res));
                   9238:                                                     my $symb = $res->symb();
                   9239:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
                   9240:                                                     @interval=&EXT("resource.0.interval",$symb);
                   9241:                                                     if ($interval[1] eq 'map') {
                   9242:                                                         if ($res->answerable()) {
                   9243:                                                             $activeblock = 1;
                   9244:                                                             last;
                   9245:                                                         }
                   9246:                                                     }
                   9247:                                                 }
                   9248:                                             }
                   9249:                                         }
1.1282    raeburn  9250:                                     }
                   9251:                                 }
                   9252:                                 if ($activeblock) {
                   9253:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   9254:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   9255:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
                   9256:                                          }
                   9257:                                     }
                   9258:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   9259:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   9260:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163    raeburn  9261:                                         }
1.1162    raeburn  9262:                                     }
                   9263:                                 }
                   9264:                             }
                   9265:                         }
                   9266:                     }
                   9267:                 }
                   9268:             }
                   9269:         }
                   9270:     }
1.1282    raeburn  9271:     return %blockers;
1.1162    raeburn  9272: }
                   9273: 
1.1282    raeburn  9274: sub has_comm_blocking {
1.1427    raeburn  9275:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1282    raeburn  9276:     my @blockers;
                   9277:     return unless ($env{'request.course.id'});
                   9278:     return unless ($priv eq 'bre');
                   9279:     return if ($env{'request.state'} eq 'construct');
1.1470    raeburn  9280:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
                   9281:     if ($env{'request.course.sec'}) {
                   9282:         $courseurl .= '/'.$env{'request.course.sec'};
                   9283:     }
                   9284:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1427    raeburn  9285:     my %blockinfo;
                   9286:     if (ref($blocks) eq 'HASH') {
                   9287:         %blockinfo = &get_commblock_resources($blocks);
                   9288:     } else {
                   9289:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
                   9290:         %blockinfo = %cachedblockers;
                   9291:     }
                   9292:     return unless (keys(%blockinfo) > 0);
1.1282    raeburn  9293:     my (%possibles,@symbs);
                   9294:     if (!$symb) {
1.1427    raeburn  9295:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162    raeburn  9296:     }
1.1282    raeburn  9297:     if ($symb) {
                   9298:         @symbs = ($symb);
                   9299:     } elsif (keys(%possibles)) { 
                   9300:         @symbs = keys(%possibles);
                   9301:     }
                   9302:     my $noblock;
                   9303:     foreach my $symb (@symbs) {
                   9304:         last if ($noblock);
                   9305:         my ($map,$resid,$resurl)=&decode_symb($symb);
1.1427    raeburn  9306:         foreach my $block (keys(%blockinfo)) {
1.1282    raeburn  9307:             if ($block =~ /^firstaccess____(.+)$/) {
                   9308:                 my $item = $1;
1.1424    raeburn  9309:                 unless ($blocked) {
                   9310:                     if (($item eq $map) || ($item eq $symb)) {
                   9311:                         $noblock = 1;
                   9312:                         last;
                   9313:                     }
1.1282    raeburn  9314:                 }
                   9315:             }
1.1427    raeburn  9316:             if (ref($blockinfo{$block}) eq 'HASH') {
                   9317:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
                   9318:                     if ($blockinfo{$block}{'resources'}{$symb}) {
1.1282    raeburn  9319:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   9320:                             push(@blockers,$block);
                   9321:                         }
                   9322:                     }
                   9323:                 }
1.1427    raeburn  9324:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
                   9325:                     if ($blockinfo{$block}{'maps'}{$map}) {
1.1424    raeburn  9326:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   9327:                             push(@blockers,$block);
                   9328:                         }
1.1282    raeburn  9329:                     }
                   9330:                 }
1.1162    raeburn  9331:             }
                   9332:         }
                   9333:     }
1.1424    raeburn  9334:     unless ($noblock) { 
                   9335:         return @blockers;
                   9336:     }
                   9337:     return;
1.1282    raeburn  9338: }
1.1162    raeburn  9339: }
                   9340: 
1.1402    raeburn  9341: sub deeplink_check {
                   9342:     my ($priv,$symb,$uri) = @_;
                   9343:     return unless ($env{'request.course.id'});
                   9344:     return unless ($priv eq 'bre');
                   9345:     return if ($env{'request.state'} eq 'construct');
                   9346:     return if ($env{'request.role.adv'});
                   9347:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9348:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9349:     my (%possibles,@symbs);
                   9350:     if (!$symb) {
                   9351:         $symb = &symbread($uri,1,1,1,\%possibles);
                   9352:     }
                   9353:     if ($symb) {
                   9354:         @symbs = ($symb);
                   9355:     } elsif (keys(%possibles)) {
                   9356:         @symbs = keys(%possibles);
                   9357:     }
                   9358: 
1.1461    raeburn  9359:     my ($deeplink_symb,$allow);
                   9360:     if ($env{'request.deeplink.login'}) {
                   9361:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
1.1402    raeburn  9362:     }
                   9363:     foreach my $symb (@symbs) {
                   9364:         last if ($allow);
                   9365:         my $deeplink = &EXT("resource.0.deeplink",$symb);
                   9366:         if ($deeplink eq '') {
                   9367:             $allow = 1;
                   9368:         } else {
1.1463    raeburn  9369:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
                   9370:             if ($state ne 'only') {
1.1402    raeburn  9371:                 $allow = 1;
1.1463    raeburn  9372:             } else {
                   9373:                 my $check_deeplink_entry;
                   9374:                 if ($protect ne 'none') {
                   9375:                     my ($acctype,$item) = split(/:/,$protect);
                   9376:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
                   9377:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
                   9378:                             $check_deeplink_entry = 1
                   9379:                         }
                   9380:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
                   9381:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
                   9382:                             $check_deeplink_entry = 1;
                   9383:                         }
                   9384:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
                   9385:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
                   9386:                             $check_deeplink_entry = 1;
                   9387:                         }
                   9388:                     }
                   9389:                 }
                   9390:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
1.1402    raeburn  9391:                     if ($scope eq 'res') {
1.1461    raeburn  9392:                         if ($symb eq $deeplink_symb) {
1.1402    raeburn  9393:                             $allow = 1;
                   9394:                         }
1.1459    raeburn  9395:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
1.1463    raeburn  9396:                         my ($map_from_symb,$map_from_login);
1.1461    raeburn  9397:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
                   9398:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
                   9399:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
                   9400:                         } else {
                   9401:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
                   9402:                         }
1.1459    raeburn  9403:                         if (($map_from_symb) && ($map_from_login)) {
                   9404:                             if ($map_from_symb eq $map_from_login) {
                   9405:                                 $allow = 1;
                   9406:                             } elsif ($scope eq 'rec') {
                   9407:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
                   9408:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
                   9409:                                     $allow = 1;
                   9410:                                 }
                   9411:                             }
                   9412:                         }
1.1402    raeburn  9413:                     }
                   9414:                 }
                   9415:             }
                   9416:         }
                   9417:     }
                   9418:     return if ($allow);
                   9419:     return 1;
                   9420: }
                   9421: 
1.1282    raeburn  9422: # -------------------------------- Deversion and split uri into path an filename   
                   9423: 
1.1133    foxr     9424: #
1.1282    raeburn  9425: #   Removes the version from a URI and
1.1133    foxr     9426: #   splits it in to its filename and path to the filename.
                   9427: #   Seems like File::Basename could have done this more clearly.
                   9428: #   Parameters:
                   9429: #      $uri   - input URI
                   9430: #   Returns:
                   9431: #     Two element list consisting of 
                   9432: #     $pathname  - the URI up to and excluding the trailing /
                   9433: #     $filename  - The part of the URI following the last /
                   9434: #  NOTE:
                   9435: #    Another realization of this is simply:
                   9436: #    use File::Basename;
                   9437: #    ...
                   9438: #    $uri = shift;
                   9439: #    $filename = basename($uri);
                   9440: #    $path     = dirname($uri);
                   9441: #    return ($filename, $path);
                   9442: #
                   9443: #     The implementation below is probably faster however.
                   9444: #
1.710     albertel 9445: sub split_uri_for_cond {
                   9446:     my $uri=&deversion(&declutter(shift));
                   9447:     my @uriparts=split(/\//,$uri);
                   9448:     my $filename=pop(@uriparts);
                   9449:     my $pathname=join('/',@uriparts);
                   9450:     return ($pathname,$filename);
                   9451: }
1.232     www      9452: # --------------------------------------------------- Is a resource on the map?
                   9453: 
                   9454: sub is_on_map {
1.710     albertel 9455:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 9456:     #Trying to find the conditional for the file
1.620     albertel 9457:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 9458: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      9459:     if ($match) {
1.289     bowersj2 9460: 	return (1,$1);
                   9461:     } else {
1.434     www      9462: 	return (0,0);
1.289     bowersj2 9463:     }
1.12      www      9464: }
                   9465: 
1.427     www      9466: # --------------------------------------------------------- Get symb from alias
                   9467: 
                   9468: sub get_symb_from_alias {
                   9469:     my $symb=shift;
                   9470:     my ($map,$resid,$url)=&decode_symb($symb);
                   9471: # Already is a symb
                   9472:     if ($url) { return $symb; }
                   9473: # Must be an alias
                   9474:     my $aliassymb='';
                   9475:     my %bighash;
1.620     albertel 9476:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      9477:                             &GDBM_READER(),0640)) {
                   9478:         my $rid=$bighash{'mapalias_'.$symb};
                   9479: 	if ($rid) {
                   9480: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 9481: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   9482: 				    $resid,$bighash{'src_'.$rid});
1.427     www      9483: 	}
                   9484:         untie %bighash;
                   9485:     }
                   9486:     return $aliassymb;
                   9487: }
                   9488: 
1.12      www      9489: # ----------------------------------------------------------------- Define Role
                   9490: 
                   9491: sub definerole {
                   9492:   if (allowed('mcr','/')) {
1.1326    raeburn  9493:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800     albertel 9494:     foreach my $role (split(':',$sysrole)) {
                   9495: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9496:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   9497:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   9498: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9499:                return "refused:s:$crole&$cqual"; 
                   9500:             }
                   9501:         }
1.191     harris41 9502:     }
1.800     albertel 9503:     foreach my $role (split(':',$domrole)) {
                   9504: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9505:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   9506:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   9507: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      9508:                return "refused:d:$crole&$cqual"; 
                   9509:             }
                   9510:         }
1.191     harris41 9511:     }
1.800     albertel 9512:     foreach my $role (split(':',$courole)) {
                   9513: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 9514:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   9515:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   9516: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      9517:                return "refused:c:$crole&$cqual"; 
                   9518:             }
                   9519:         }
1.191     harris41 9520:     }
1.1326    raeburn  9521:     my $uhome;
                   9522:     if (($uname ne '') && ($udom ne '')) {
                   9523:         $uhome = &homeserver($uname,$udom);
                   9524:         return $uhome if ($uhome eq 'no_host');
                   9525:     } else {
                   9526:         $uname = $env{'user.name'};
                   9527:         $udom = $env{'user.domain'};
                   9528:         $uhome = $env{'user.home'};
                   9529:     }
1.620     albertel 9530:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326    raeburn  9531:                 "$udom:$uname:rolesdef_$rolename=".
1.21      www      9532:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326    raeburn  9533:     return reply($command,$uhome);
1.12      www      9534:   } else {
                   9535:     return 'refused';
                   9536:   }
1.105     harris41 9537: }
                   9538: 
                   9539: # ---------------- Make a metadata query against the network of library servers
                   9540: 
                   9541: sub metadata_query {
1.1237    raeburn  9542:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120     harris41 9543:     my %rhash;
1.845     albertel 9544:     my %libserv = &all_library();
1.244     matthew  9545:     my @server_list = (defined($server_array) ? @$server_array
                   9546:                                               : keys(%libserv) );
                   9547:     for my $server (@server_list) {
1.1237    raeburn  9548:         my $domains = ''; 
                   9549:         if (ref($domains_hash) eq 'HASH') {
                   9550:             $domains = $domains_hash->{$server}; 
                   9551:         }
1.118     harris41 9552: 	unless ($custom or $customshow) {
1.1237    raeburn  9553: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118     harris41 9554: 	    $rhash{$server}=$reply;
                   9555: 	}
                   9556: 	else {
                   9557: 	    my $reply=&reply("querysend:".&escape($query).':'.
1.1237    raeburn  9558: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118     harris41 9559: 			     $server);
                   9560: 	    $rhash{$server}=$reply;
                   9561: 	}
1.112     harris41 9562:     }
1.118     harris41 9563:     return \%rhash;
1.240     www      9564: }
                   9565: 
                   9566: # ----------------------------------------- Send log queries and wait for reply
                   9567: 
                   9568: sub log_query {
                   9569:     my ($uname,$udom,$query,%filters)=@_;
                   9570:     my $uhome=&homeserver($uname,$udom);
                   9571:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 9572:     my $uhost=&hostname($uhome);
1.800     albertel 9573:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      9574:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   9575:                        $uhome);
1.479     albertel 9576:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      9577:     return get_query_reply($queryid);
                   9578: }
                   9579: 
1.818     raeburn  9580: # -------------------------- Update MySQL table for portfolio file
                   9581: 
                   9582: sub update_portfolio_table {
1.821     raeburn  9583:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  9584:     if ($group ne '') {
                   9585:         $file_name =~s /^\Q$group\E//;
                   9586:     }
1.818     raeburn  9587:     my $homeserver = &homeserver($uname,$udom);
                   9588:     my $queryid=
1.821     raeburn  9589:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   9590:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  9591:     my $reply = &get_query_reply($queryid);
                   9592:     return $reply;
                   9593: }
                   9594: 
1.899     raeburn  9595: # -------------------------- Update MySQL allusers table
                   9596: 
                   9597: sub update_allusers_table {
                   9598:     my ($uname,$udom,$names) = @_;
                   9599:     my $homeserver = &homeserver($uname,$udom);
                   9600:     my $queryid=
                   9601:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   9602:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   9603:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   9604:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   9605:                'generation='.&escape($names->{'generation'}).'%%'.
                   9606:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   9607:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  9608:     return;
1.899     raeburn  9609: }
                   9610: 
1.508     raeburn  9611: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  9612: 
                   9613: sub fetch_enrollment_query {
1.511     raeburn  9614:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317    raeburn  9615:     my ($homeserver,$sleep,$loopmax);
1.547     raeburn  9616:     my $maxtries = 1;
1.508     raeburn  9617:     if ($context eq 'automated') {
                   9618:         $homeserver = $perlvar{'lonHostID'};
1.1317    raeburn  9619:         $sleep = 2;
                   9620:         $loopmax = 100;
1.547     raeburn  9621:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  9622:     } else {
                   9623:         $homeserver = &homeserver($cnum,$dom);
                   9624:     }
1.838     albertel 9625:     my $host=&hostname($homeserver);
1.506     raeburn  9626:     my $cmd = '';
1.1000    raeburn  9627:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 9628:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  9629:     }
                   9630:     $cmd =~ s/%%$//;
                   9631:     $cmd = &escape($cmd);
                   9632:     my $query = 'fetchenrollment';
1.620     albertel 9633:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  9634:     unless ($queryid=~/^\Q$host\E\_/) { 
                   9635:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   9636:         return 'error: '.$queryid;
                   9637:     }
1.1317    raeburn  9638:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9639:     my $tries = 1;
                   9640:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317    raeburn  9641:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  9642:         $tries ++;
                   9643:     }
1.526     raeburn  9644:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 9645:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  9646:     } else {
1.901     albertel 9647:         my @responses = split(/:/,$reply);
1.1322    raeburn  9648:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
1.800     albertel 9649:             foreach my $line (@responses) {
                   9650:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  9651:                 $$replyref{$key} = $value;
                   9652:             }
                   9653:         } else {
1.1117    foxr     9654:             my $pathname = LONCAPA::tempdir();
1.800     albertel 9655:             foreach my $line (@responses) {
                   9656:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  9657:                 $$replyref{$key} = $value;
                   9658:                 if ($value > 0) {
1.800     albertel 9659:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   9660:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  9661:                         my $destname = $pathname.'/'.$filename;
                   9662:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  9663:                         if ($xml_classlist =~ /^error/) {
                   9664:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   9665:                         } else {
1.1359    raeburn  9666:                             if ( open(FILE,">",$destname) ) {
1.506     raeburn  9667:                                 print FILE &unescape($xml_classlist);
                   9668:                                 close(FILE);
1.526     raeburn  9669:                             } else {
                   9670:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  9671:                             }
                   9672:                         }
                   9673:                     }
                   9674:                 }
                   9675:             }
                   9676:         }
                   9677:         return 'ok';
                   9678:     }
                   9679:     return 'error';
                   9680: }
                   9681: 
1.242     www      9682: sub get_query_reply {
1.1471    raeburn  9683:     my ($queryid,$sleep,$loopmax) = @_;
1.1317    raeburn  9684:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
                   9685:         $sleep = 0.2;
                   9686:     }
                   9687:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
                   9688:         $loopmax = 100;
                   9689:     }
1.1117    foxr     9690:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      9691:     my $reply='';
1.1317    raeburn  9692:     for (1..$loopmax) {
                   9693: 	sleep($sleep);
1.240     www      9694:         if (-e $replyfile.'.end') {
1.1359    raeburn  9695: 	    if (open(my $fh,"<",$replyfile)) {
1.904     albertel 9696: 		$reply = join('',<$fh>);
                   9697: 		close($fh);
1.240     www      9698: 	   } else { return 'error: reply_file_error'; }
1.242     www      9699:            return &unescape($reply);
                   9700: 	}
1.240     www      9701:     }
1.242     www      9702:     return 'timeout:'.$queryid;
1.240     www      9703: }
                   9704: 
                   9705: sub courselog_query {
1.241     www      9706: #
                   9707: # possible filters:
                   9708: # url: url or symb
                   9709: # username
                   9710: # domain
                   9711: # action: view, submit, grade
                   9712: # start: timestamp
                   9713: # end: timestamp
                   9714: #
1.240     www      9715:     my (%filters)=@_;
1.620     albertel 9716:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      9717:     if ($filters{'url'}) {
                   9718: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   9719:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   9720:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   9721:     }
1.620     albertel 9722:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   9723:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      9724:     return &log_query($cname,$cdom,'courselog',%filters);
                   9725: }
                   9726: 
                   9727: sub userlog_query {
1.858     raeburn  9728: #
                   9729: # possible filters:
                   9730: # action: log check role
                   9731: # start: timestamp
                   9732: # end: timestamp
                   9733: #
1.240     www      9734:     my ($uname,$udom,%filters)=@_;
                   9735:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      9736: }
                   9737: 
1.506     raeburn  9738: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   9739: 
                   9740: sub auto_run {
1.508     raeburn  9741:     my ($cnum,$cdom) = @_;
1.876     raeburn  9742:     my $response = 0;
                   9743:     my $settings;
                   9744:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   9745:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   9746:         $settings = $domconfig{'autoenroll'};
                   9747:         if ($settings->{'run'} eq '1') {
                   9748:             $response = 1;
                   9749:         }
                   9750:     } else {
1.934     raeburn  9751:         my $homeserver;
                   9752:         if (&is_course($cdom,$cnum)) {
                   9753:             $homeserver = &homeserver($cnum,$cdom);
                   9754:         } else {
                   9755:             $homeserver = &domain($cdom,'primary');
                   9756:         }
                   9757:         if ($homeserver ne 'no_host') {
                   9758:             $response = &reply('autorun:'.$cdom,$homeserver);
                   9759:         }
1.876     raeburn  9760:     }
1.506     raeburn  9761:     return $response;
                   9762: }
1.776     albertel 9763: 
1.506     raeburn  9764: sub auto_get_sections {
1.508     raeburn  9765:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  9766:     my $homeserver;
                   9767:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   9768:         $homeserver = &homeserver($cnum,$cdom);
                   9769:     }
                   9770:     if (!defined($homeserver)) { 
                   9771:         if ($cdom =~ /^$match_domain$/) {
                   9772:             $homeserver = &domain($cdom,'primary');
                   9773:         }
                   9774:     }
                   9775:     my @secs;
                   9776:     if (defined($homeserver)) {
                   9777:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   9778:         unless ($response eq 'refused') {
                   9779:             @secs = split(/:/,$response);
                   9780:         }
1.506     raeburn  9781:     }
                   9782:     return @secs;
                   9783: }
1.776     albertel 9784: 
1.506     raeburn  9785: sub auto_new_course {
1.1099    raeburn  9786:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  9787:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  9788:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  9789:     return $response;
                   9790: }
1.776     albertel 9791: 
1.506     raeburn  9792: sub auto_validate_courseID {
1.508     raeburn  9793:     my ($cnum,$cdom,$inst_course_id) = @_;
                   9794:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  9795:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  9796:     return $response;
                   9797: }
1.776     albertel 9798: 
1.1007    raeburn  9799: sub auto_validate_instcode {
1.1020    raeburn  9800:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  9801:     my ($homeserver,$response);
                   9802:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9803:         $homeserver = &homeserver($cnum,$cdom);
                   9804:     }
                   9805:     if (!defined($homeserver)) {
                   9806:         if ($cdom =~ /^$match_domain$/) {
                   9807:             $homeserver = &domain($cdom,'primary');
                   9808:         }
                   9809:     }
1.1065    raeburn  9810:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   9811:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  9812:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   9813:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  9814: }
                   9815: 
1.1444    raeburn  9816: sub auto_validate_inst_crosslist {
                   9817:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
                   9818:     my ($homeserver,$response);
                   9819:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9820:         $homeserver = &homeserver($cnum,$cdom);
                   9821:     }
                   9822:     if (!defined($homeserver)) {
                   9823:         if ($cdom =~ /^$match_domain$/) {
                   9824:             $homeserver = &domain($cdom,'primary');
                   9825:         }
                   9826:     }
                   9827:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
                   9828:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
                   9829:                          &escape($instcode).':'.&escape($inst_xlist).':'.
1.1445    raeburn  9830:                          &escape($coowner),$homeserver);
1.1444    raeburn  9831:     }
                   9832:     return $response;
                   9833: }
                   9834: 
1.506     raeburn  9835: sub auto_create_password {
1.873     raeburn  9836:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   9837:     my ($homeserver,$response);
1.506     raeburn  9838:     my $create_passwd = 0;
                   9839:     my $authchk = '';
1.873     raeburn  9840:     if ($udom =~ /^$match_domain$/) {
                   9841:         $homeserver = &domain($udom,'primary');
                   9842:     }
                   9843:     if ($homeserver eq '') {
                   9844:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9845:             $homeserver = &homeserver($cnum,$cdom);
                   9846:         }
                   9847:     }
                   9848:     if ($homeserver eq '') {
                   9849:         $authchk = 'nodomain';
1.506     raeburn  9850:     } else {
1.873     raeburn  9851:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   9852:         if ($response eq 'refused') {
                   9853:             $authchk = 'refused';
                   9854:         } else {
1.901     albertel 9855:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  9856:         }
1.506     raeburn  9857:     }
                   9858:     return ($authparam,$create_passwd,$authchk);
                   9859: }
                   9860: 
1.706     raeburn  9861: sub auto_photo_permission {
                   9862:     my ($cnum,$cdom,$students) = @_;
                   9863:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 9864:     my ($outcome,$perm_reqd,$conditions) = 
                   9865: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 9866:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9867: 	return (undef,undef);
                   9868:     }
1.706     raeburn  9869:     return ($outcome,$perm_reqd,$conditions);
                   9870: }
                   9871: 
                   9872: sub auto_checkphotos {
                   9873:     my ($uname,$udom,$pid) = @_;
                   9874:     my $homeserver = &homeserver($uname,$udom);
                   9875:     my ($result,$resulttype);
                   9876:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 9877: 				   &escape($uname).':'.&escape($pid),
                   9878: 				   $homeserver));
1.709     albertel 9879:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9880: 	return (undef,undef);
                   9881:     }
1.706     raeburn  9882:     if ($outcome) {
                   9883:         ($result,$resulttype) = split(/:/,$outcome);
                   9884:     } 
                   9885:     return ($result,$resulttype);
                   9886: }
                   9887: 
                   9888: sub auto_photochoice {
                   9889:     my ($cnum,$cdom) = @_;
                   9890:     my $homeserver = &homeserver($cnum,$cdom);
                   9891:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 9892: 						       &escape($cdom),
                   9893: 						       $homeserver)));
1.709     albertel 9894:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9895: 	return (undef,undef);
                   9896:     }
1.706     raeburn  9897:     return ($update,$comment);
                   9898: }
                   9899: 
                   9900: sub auto_photoupdate {
                   9901:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   9902:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 9903:     my $host=&hostname($homeserver);
1.706     raeburn  9904:     my $cmd = '';
                   9905:     my $maxtries = 1;
1.800     albertel 9906:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   9907:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  9908:     }
                   9909:     $cmd =~ s/%%$//;
                   9910:     $cmd = &escape($cmd);
                   9911:     my $query = 'institutionalphotos';
                   9912:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   9913:     unless ($queryid=~/^\Q$host\E\_/) {
                   9914:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   9915:         return 'error: '.$queryid;
                   9916:     }
                   9917:     my $reply = &get_query_reply($queryid);
                   9918:     my $tries = 1;
                   9919:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   9920:         $reply = &get_query_reply($queryid);
                   9921:         $tries ++;
                   9922:     }
                   9923:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   9924:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   9925:     } else {
                   9926:         my @responses = split(/:/,$reply);
                   9927:         my $outcome = shift(@responses); 
                   9928:         foreach my $item (@responses) {
                   9929:             my ($key,$value) = split(/=/,$item);
                   9930:             $$photo{$key} = $value;
                   9931:         }
                   9932:         return $outcome;
                   9933:     }
                   9934:     return 'error';
                   9935: }
                   9936: 
1.521     raeburn  9937: sub auto_instcode_format {
1.793     albertel 9938:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   9939: 	$cat_order) = @_;
1.521     raeburn  9940:     my $courses = '';
1.772     raeburn  9941:     my @homeservers;
1.521     raeburn  9942:     if ($caller eq 'global') {
1.841     albertel 9943: 	my %servers = &get_servers($codedom,'library');
                   9944: 	foreach my $tryserver (keys(%servers)) {
                   9945: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9946: 		push(@homeservers,$tryserver);
                   9947: 	    }
1.584     raeburn  9948:         }
1.1022    raeburn  9949:     } elsif ($caller eq 'requests') {
                   9950:         if ($codedom =~ /^$match_domain$/) {
                   9951:             my $chome = &domain($codedom,'primary');
                   9952:             unless ($chome eq 'no_host') {
                   9953:                 push(@homeservers,$chome);
                   9954:             }
                   9955:         }
1.521     raeburn  9956:     } else {
1.772     raeburn  9957:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  9958:     }
1.793     albertel 9959:     foreach my $code (keys(%{$instcodes})) {
                   9960:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  9961:     }
                   9962:     chop($courses);
1.772     raeburn  9963:     my $ok_response = 0;
                   9964:     my $response;
                   9965:     while (@homeservers > 0 && $ok_response == 0) {
                   9966:         my $server = shift(@homeservers); 
                   9967:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   9968:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   9969:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 9970: 		split(/:/,$response);
1.772     raeburn  9971:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   9972:             push(@{$codetitles},&str2array($codetitles_str));
                   9973:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   9974:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   9975:             $ok_response = 1;
                   9976:         }
                   9977:     }
                   9978:     if ($ok_response) {
1.521     raeburn  9979:         return 'ok';
1.772     raeburn  9980:     } else {
                   9981:         return $response;
1.521     raeburn  9982:     }
                   9983: }
                   9984: 
1.792     raeburn  9985: sub auto_instcode_defaults {
                   9986:     my ($domain,$returnhash,$code_order) = @_;
                   9987:     my @homeservers;
1.841     albertel 9988: 
                   9989:     my %servers = &get_servers($domain,'library');
                   9990:     foreach my $tryserver (keys(%servers)) {
                   9991: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9992: 	    push(@homeservers,$tryserver);
                   9993: 	}
1.792     raeburn  9994:     }
1.841     albertel 9995: 
1.792     raeburn  9996:     my $response;
1.841     albertel 9997:     foreach my $server (@homeservers) {
1.792     raeburn  9998:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 9999:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   10000: 	
                   10001: 	foreach my $pair (split(/\&/,$response)) {
                   10002: 	    my ($name,$value)=split(/\=/,$pair);
                   10003: 	    if ($name eq 'code_order') {
                   10004: 		@{$code_order} = split(/\&/,&unescape($value));
                   10005: 	    } else {
                   10006: 		$returnhash->{&unescape($name)}=&unescape($value);
                   10007: 	    }
                   10008: 	}
                   10009: 	return 'ok';
1.792     raeburn  10010:     }
1.841     albertel 10011: 
                   10012:     return $response;
1.1003    raeburn  10013: }
                   10014: 
                   10015: sub auto_possible_instcodes {
1.1007    raeburn  10016:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   10017:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   10018:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   10019:         return;
                   10020:     }
1.1003    raeburn  10021:     my (@homeservers,$uhome);
                   10022:     if (defined(&domain($domain,'primary'))) {
                   10023:         $uhome=&domain($domain,'primary');
                   10024:         push(@homeservers,&domain($domain,'primary'));
                   10025:     } else {
                   10026:         my %servers = &get_servers($domain,'library');
                   10027:         foreach my $tryserver (keys(%servers)) {
                   10028:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10029:                 push(@homeservers,$tryserver);
                   10030:             }
                   10031:         }
                   10032:     }
                   10033:     my $response;
                   10034:     foreach my $server (@homeservers) {
                   10035:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   10036:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  10037:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   10038:             split(':',$response);
                   10039:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   10040:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  10041:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  10042:             my ($name,$value)=split('=',$item);
                   10043:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  10044:         }
                   10045:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  10046:             my ($name,$value)=split('=',$item);
                   10047:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  10048:         }
                   10049:         return 'ok';
                   10050:     }
                   10051:     return $response;
                   10052: }
1.792     raeburn  10053: 
1.1010    raeburn  10054: sub auto_courserequest_checks {
                   10055:     my ($dom) = @_;
1.1020    raeburn  10056:     my ($homeserver,%validations);
                   10057:     if ($dom =~ /^$match_domain$/) {
                   10058:         $homeserver = &domain($dom,'primary');
                   10059:     }
                   10060:     unless ($homeserver eq 'no_host') {
                   10061:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   10062:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10063:             my @items = split(/&/,$response);
                   10064:             foreach my $item (@items) {
                   10065:                 my ($key,$value) = split('=',$item);
                   10066:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   10067:             }
                   10068:         }
                   10069:     }
1.1010    raeburn  10070:     return %validations; 
                   10071: }
                   10072: 
1.1020    raeburn  10073: sub auto_courserequest_validation {
1.1255    raeburn  10074:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020    raeburn  10075:     my ($homeserver,$response);
                   10076:     if ($dom =~ /^$match_domain$/) {
                   10077:         $homeserver = &domain($dom,'primary');
                   10078:     }
1.1255    raeburn  10079:     unless ($homeserver eq 'no_host') {
                   10080:         my $customdata;
                   10081:         if (ref($custominfo) eq 'HASH') {
                   10082:             $customdata = &freeze_escape($custominfo);
                   10083:         }
1.1020    raeburn  10084:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  10085:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255    raeburn  10086:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
                   10087:                                     $customdata,$homeserver));
1.1020    raeburn  10088:     }
                   10089:     return $response;
                   10090: }
                   10091: 
1.777     albertel 10092: sub auto_validate_class_sec {
1.918     raeburn  10093:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  10094:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  10095:     my $ownerlist;
                   10096:     if (ref($owners) eq 'ARRAY') {
                   10097:         $ownerlist = join(',',@{$owners});
                   10098:     } else {
                   10099:         $ownerlist = $owners;
                   10100:     }
1.773     raeburn  10101:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  10102:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  10103:     return $response;
                   10104: }
                   10105: 
1.1460    raeburn  10106: sub auto_instsec_reformat {
                   10107:     my ($cdom,$action,$instsecref) = @_;
                   10108:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
                   10109:     my @homeservers;
                   10110:     if (defined(&domain($cdom,'primary'))) {
                   10111:         push(@homeservers,&domain($cdom,'primary'));
                   10112:     } else {
                   10113:         my %servers = &get_servers($cdom,'library');
                   10114:         foreach my $tryserver (keys(%servers)) {
                   10115:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   10116:                 push(@homeservers,$tryserver);
                   10117:             }
                   10118:         }
                   10119:     }
                   10120:     my $response;
                   10121:     my %reformatted = %{$instsecref};
                   10122:     foreach my $server (@homeservers) {
                   10123:         if (ref($instsecref) eq 'HASH') {
                   10124:             my $info = &freeze_escape($instsecref);
                   10125:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
                   10126:                                 $action.':'.$info,$server);
                   10127:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
                   10128:             my @items = split(/&/,$response);
                   10129:             foreach my $item (@items) {
                   10130:                 my ($key,$value) = split(/=/,$item);
                   10131:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
                   10132:             }
                   10133:         }
                   10134:     }
                   10135:     return %reformatted;
                   10136: }
                   10137: 
1.1367    raeburn  10138: sub auto_validate_instclasses {
                   10139:     my ($cdom,$cnum,$owners,$classesref) = @_;
                   10140:     my ($homeserver,%validations);
                   10141:     $homeserver = &homeserver($cnum,$cdom);
                   10142:     unless ($homeserver eq 'no_host') {
                   10143:         my $ownerlist;
                   10144:         if (ref($owners) eq 'ARRAY') {
                   10145:             $ownerlist = join(',',@{$owners});
                   10146:         } else {
                   10147:             $ownerlist = $owners;
                   10148:         }
                   10149:         if (ref($classesref) eq 'HASH') {
                   10150:             my $classes = &freeze_escape($classesref);
                   10151:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
                   10152:                                 ':'.$cdom.':'.$classes,$homeserver);
                   10153:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10154:                 my @items = split(/&/,$response);
                   10155:                 foreach my $item (@items) {
                   10156:                     my ($key,$value) = split('=',$item);
                   10157:                     $validations{&unescape($key)} = &thaw_unescape($value);
                   10158:                 }
                   10159:             }
                   10160:         }
                   10161:     }
                   10162:     return %validations;
                   10163: }
                   10164: 
1.1248    raeburn  10165: sub auto_crsreq_update {
                   10166:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257    raeburn  10167:         $code,$accessstart,$accessend,$inbound) = @_;
1.1248    raeburn  10168:     my ($homeserver,%crsreqresponse);
                   10169:     if ($cdom =~ /^$match_domain$/) {
                   10170:         $homeserver = &domain($cdom,'primary');
                   10171:     }
                   10172:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   10173:         my $info;
                   10174:         if (ref($inbound) eq 'HASH') {
                   10175:             $info = &freeze_escape($inbound);
                   10176:         }
                   10177:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
                   10178:                             ':'.&escape($action).':'.&escape($ownername).':'.
                   10179:                             &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257    raeburn  10180:                             &escape($title).':'.&escape($code).':'.
                   10181:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
                   10182:                             $homeserver);
1.1248    raeburn  10183:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   10184:             my @items = split(/&/,$response);
                   10185:             foreach my $item (@items) {
                   10186:                 my ($key,$value) = split('=',$item);
                   10187:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
                   10188:             }
                   10189:         }
                   10190:     }
                   10191:     return \%crsreqresponse;
                   10192: }
                   10193: 
1.1305    raeburn  10194: sub auto_export_grades {
1.1309    raeburn  10195:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
                   10196:     my ($homeserver,%exportresponse);
                   10197:     if ($cdom =~ /^$match_domain$/) {
                   10198:         $homeserver = &domain($cdom,'primary');
                   10199:     }
                   10200:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   10201:         my $info;
                   10202:         if (ref($inforef) eq 'HASH') {
                   10203:             $info = &freeze_escape($inforef);
                   10204:         }
                   10205:         if (ref($gradesref) eq 'HASH') {
                   10206:             my $grades = &freeze_escape($gradesref);
                   10207:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
                   10208:                                 $info.':'.$grades,$homeserver);
                   10209:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
                   10210:                 my @items = split(/&/,$response);
                   10211:                 foreach my $item (@items) {
                   10212:                     my ($key,$value) = split('=',$item);
                   10213:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
                   10214:                 }
                   10215:             }
                   10216:         }
                   10217:     }
                   10218:     return \%exportresponse;
1.1305    raeburn  10219: }
                   10220: 
1.1287    raeburn  10221: sub check_instcode_cloning {
                   10222:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
                   10223:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   10224:         return;
                   10225:     }
                   10226:     my $canclone;
                   10227:     if (@{$code_order} > 0) {
                   10228:         my $instcoderegexp ='^';
                   10229:         my @clonecodes = split(/\&/,$cloner);
                   10230:         foreach my $item (@{$code_order}) {
                   10231:             if (grep(/^\Q$item\E=/,@clonecodes)) {
                   10232:                 foreach my $pair (@clonecodes) {
                   10233:                     my ($key,$val) = split(/\=/,$pair,2);
                   10234:                     $val = &unescape($val);
                   10235:                     if ($key eq $item) {
                   10236:                         $instcoderegexp .= '('.$val.')';
                   10237:                         last;
                   10238:                     }
                   10239:                 }
                   10240:             } else {
                   10241:                 $instcoderegexp .= $codedefaults->{$item};
                   10242:             }
                   10243:         }
                   10244:         $instcoderegexp .= '$';
                   10245:         my (@from,@to);
                   10246:         eval {
                   10247:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   10248:                (@to) = ($clonetocode =~ /$instcoderegexp/);
                   10249:         };
                   10250:         if ((@from > 0) && (@to > 0)) {
                   10251:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   10252:             if (!@diffs) {
                   10253:                 $canclone = 1;
                   10254:             }
                   10255:         }
                   10256:     }
                   10257:     return $canclone;
                   10258: }
                   10259: 
                   10260: sub default_instcode_cloning {
                   10261:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
                   10262:     my (%codedefaults,@code_order,$canclone);
                   10263:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
                   10264:         %codedefaults = %{$codedefaultsref};
                   10265:         @code_order = @{$codeorderref};
                   10266:     } elsif ($clonedom) {
                   10267:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
                   10268:     }
                   10269:     if (($domdefclone) && (@code_order)) {
                   10270:         my @clonecodes = split(/\+/,$domdefclone);
                   10271:         my $instcoderegexp ='^';
                   10272:         foreach my $item (@code_order) {
                   10273:             if (grep(/^\Q$item\E$/,@clonecodes)) {
                   10274:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
                   10275:             } else {
                   10276:                 $instcoderegexp .= $codedefaults{$item};
                   10277:             }
                   10278:         }
                   10279:         $instcoderegexp .= '$';
                   10280:         my (@from,@to);
                   10281:         eval {
                   10282:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   10283:             (@to) = ($clonetocode =~ /$instcoderegexp/);
                   10284:         };
                   10285:         if ((@from > 0) && (@to > 0)) {
                   10286:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   10287:             if (!@diffs) {
                   10288:                 $canclone = 1;
                   10289:             }
                   10290:         }
                   10291:     }
                   10292:     return $canclone;
                   10293: }
                   10294: 
1.679     raeburn  10295: # ------------------------------------------------------- Course Group routines
                   10296: 
                   10297: sub get_coursegroups {
1.809     raeburn  10298:     my ($cdom,$cnum,$group,$namespace) = @_;
                   10299:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  10300: }
                   10301: 
1.679     raeburn  10302: sub modify_coursegroup {
                   10303:     my ($cdom,$cnum,$groupsettings) = @_;
                   10304:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   10305: }
                   10306: 
1.809     raeburn  10307: sub toggle_coursegroup_status {
                   10308:     my ($cdom,$cnum,$group,$action) = @_;
                   10309:     my ($from_namespace,$to_namespace);
                   10310:     if ($action eq 'delete') {
                   10311:         $from_namespace = 'coursegroups';
                   10312:         $to_namespace = 'deleted_groups';
                   10313:     } else {
                   10314:         $from_namespace = 'deleted_groups';
                   10315:         $to_namespace = 'coursegroups';
                   10316:     }
                   10317:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  10318:     if (my $tmp = &error(%curr_group)) {
                   10319:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   10320:         return ('read error',$tmp);
                   10321:     } else {
                   10322:         my %savedsettings = %curr_group; 
1.809     raeburn  10323:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  10324:         my $deloutcome;
                   10325:         if ($result eq 'ok') {
1.809     raeburn  10326:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  10327:         } else {
                   10328:             return ('write error',$result);
                   10329:         }
                   10330:         if ($deloutcome eq 'ok') {
                   10331:             return 'ok';
                   10332:         } else {
                   10333:             return ('delete error',$deloutcome);
                   10334:         }
                   10335:     }
                   10336: }
                   10337: 
1.679     raeburn  10338: sub modify_group_roles {
1.957     raeburn  10339:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  10340:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   10341:     my $role = 'gr/'.&escape($userprivs);
                   10342:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  10343:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  10344:     if ($result eq 'ok') {
                   10345:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   10346:     }
1.679     raeburn  10347:     return $result;
                   10348: }
                   10349: 
                   10350: sub modify_coursegroup_membership {
                   10351:     my ($cdom,$cnum,$membership) = @_;
                   10352:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   10353:     return $result;
                   10354: }
                   10355: 
1.682     raeburn  10356: sub get_active_groups {
                   10357:     my ($udom,$uname,$cdom,$cnum) = @_;
                   10358:     my $now = time;
                   10359:     my %groups = ();
                   10360:     foreach my $key (keys(%env)) {
1.811     albertel 10361:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  10362:             my ($start,$end) = split(/\./,$env{$key});
                   10363:             if (($end!=0) && ($end<$now)) { next; }
                   10364:             if (($start!=0) && ($start>$now)) { next; }
                   10365:             if ($1 eq $cdom && $2 eq $cnum) {
                   10366:                 $groups{$3} = $env{$key} ;
                   10367:             }
                   10368:         }
                   10369:     }
                   10370:     return %groups;
                   10371: }
                   10372: 
1.683     raeburn  10373: sub get_group_membership {
                   10374:     my ($cdom,$cnum,$group) = @_;
                   10375:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   10376: }
                   10377: 
                   10378: sub get_users_groups {
                   10379:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  10380:     my @usersgroups;
1.683     raeburn  10381:     my $cachetime=1800;
                   10382: 
                   10383:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  10384:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   10385:     if (defined($cached)) {
1.734     albertel 10386:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  10387:     } else {  
                   10388:         $grouplist = '';
1.816     raeburn  10389:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  10390:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  10391:         my $access_end = $env{'course.'.$courseid.
                   10392:                               '.default_enrollment_end_date'};
                   10393:         my $now = time;
                   10394:         foreach my $key (keys(%roleshash)) {
                   10395:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   10396:                 my $group = $1;
                   10397:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   10398:                     my $start = $2;
                   10399:                     my $end = $1;
                   10400:                     if ($start == -1) { next; } # deleted from group
                   10401:                     if (($start!=0) && ($start>$now)) { next; }
                   10402:                     if (($end!=0) && ($end<$now)) {
                   10403:                         if ($access_end && $access_end < $now) {
                   10404:                             if ($access_end - $end < 86400) {
                   10405:                                 push(@usersgroups,$group);
1.733     raeburn  10406:                             }
                   10407:                         }
1.817     raeburn  10408:                         next;
1.733     raeburn  10409:                     }
1.817     raeburn  10410:                     push(@usersgroups,$group);
1.683     raeburn  10411:                 }
                   10412:             }
                   10413:         }
1.817     raeburn  10414:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   10415:         $grouplist = join(':',@usersgroups);
                   10416:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  10417:     }
1.733     raeburn  10418:     return @usersgroups;
1.683     raeburn  10419: }
                   10420: 
                   10421: sub devalidate_getgroups_cache {
                   10422:     my ($udom,$uname,$cdom,$cnum)=@_;
                   10423:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 10424: 
1.683     raeburn  10425:     my $hashid="$udom:$uname:$courseid";
                   10426:     &devalidate_cache_new('getgroups',$hashid);
                   10427: }
                   10428: 
1.12      www      10429: # ------------------------------------------------------------------ Plain Text
                   10430: 
                   10431: sub plaintext {
1.988     raeburn  10432:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  10433:     if ($short =~ m{^cr/}) {
1.758     albertel 10434: 	return (split('/',$short))[-1];
                   10435:     }
1.742     raeburn  10436:     if (!defined($cid)) {
                   10437:         $cid = $env{'request.course.id'};
                   10438:     }
                   10439:     my %rolenames = (
1.1008    raeburn  10440:                       Course    => 'std',
                   10441:                       Community => 'alt1',
1.1305    raeburn  10442:                       Placement => 'std',
1.742     raeburn  10443:                     );
1.1037    raeburn  10444:     if ($cid ne '') {
                   10445:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   10446:             unless ($forcedefault) {
                   10447:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   10448:                 &Apache::lonlocal::mt_escape(\$roletext);
                   10449:                 return &Apache::lonlocal::mt($roletext);
                   10450:             }
                   10451:         }
                   10452:     }
                   10453:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   10454:         (defined($rolenames{$type})) && 
                   10455:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  10456:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  10457:     } elsif ($cid ne '') {
                   10458:         my $crstype = $env{'course.'.$cid.'.type'};
                   10459:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   10460:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   10461:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   10462:         }
1.742     raeburn  10463:     }
1.1037    raeburn  10464:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      10465: }
                   10466: 
                   10467: # ----------------------------------------------------------------- Assign Role
                   10468: 
                   10469: sub assignrole {
1.957     raeburn  10470:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   10471:         $context)=@_;
1.21      www      10472:     my $mrole;
                   10473:     if ($role =~ /^cr\//) {
1.393     www      10474:         my $cwosec=$url;
1.811     albertel 10475:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.1502    raeburn  10476:         if ((!&allowed('ccr',$cwosec)) && (!&allowed('ccr',$udom))) {
1.1026    raeburn  10477:            my $refused = 1;
                   10478:            if ($context eq 'requestcourses') {
                   10479:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   10480:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   10481:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   10482:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   10483:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10484:                            if ($crsenv{'internal.courseowner'} eq
                   10485:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   10486:                                $refused = '';
                   10487:                            }
                   10488:                        }
                   10489:                    }
                   10490:                }
                   10491:            }
                   10492:            if ($refused) {
                   10493:                &logthis('Refused custom assignrole: '.
                   10494:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   10495:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   10496:                return 'refused';
                   10497:            }
1.104     www      10498:         }
1.21      www      10499:         $mrole='cr';
1.678     raeburn  10500:     } elsif ($role =~ /^gr\//) {
                   10501:         my $cwogrp=$url;
1.811     albertel 10502:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  10503:         unless (&allowed('mdg',$cwogrp)) {
                   10504:             &logthis('Refused group assignrole: '.
                   10505:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   10506:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   10507:             return 'refused';
                   10508:         }
                   10509:         $mrole='gr';
1.21      www      10510:     } else {
1.82      www      10511:         my $cwosec=$url;
1.811     albertel 10512:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  10513:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   10514:             my $refused;
                   10515:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   10516:                 if (!(&allowed('c'.$role,$url))) {
                   10517:                     $refused = 1;
                   10518:                 }
                   10519:             } else {
                   10520:                 $refused = 1;
                   10521:             }
1.947     raeburn  10522:             if ($refused) {
1.1045    raeburn  10523:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
1.1377    raeburn  10524:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
1.1045    raeburn  10525:                     my %crsenv;
                   10526:                     if ($role eq 'cc' || $role eq 'co') {
                   10527:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10528:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   10529:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   10530:                                 if ($crsenv{'internal.courseowner'} eq 
                   10531:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10532:                                     $refused = '';
                   10533:                                 }
                   10534:                             }
                   10535:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   10536:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   10537:                                 if ($crsenv{'internal.courseowner'} eq 
                   10538:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   10539:                                     $refused = '';
                   10540:                                 }
                   10541:                             }
                   10542:                         }
                   10543:                     }
1.1368    raeburn  10544:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   10545:                     if ($role eq 'st') {
                   10546:                         $refused = '';
1.1377    raeburn  10547:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
1.1368    raeburn  10548:                         $refused = '';
                   10549:                     }
1.1017    raeburn  10550:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  10551:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  10552:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  10553:                         my $wrongcc;
                   10554:                         if ($cnum =~ /^$match_community$/) {
                   10555:                             $wrongcc = 1 if ($role eq 'cc');
                   10556:                         } else {
                   10557:                             $wrongcc = 1 if ($role eq 'co');
                   10558:                         }
                   10559:                         unless ($wrongcc) {
                   10560:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   10561:                             if ($crsenv{'internal.courseowner'} eq 
                   10562:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   10563:                                 $refused = '';
                   10564:                             }
1.1017    raeburn  10565:                         }
                   10566:                     }
1.1183    raeburn  10567:                 } elsif ($context eq 'requestauthor') {
                   10568:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   10569:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   10570:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   10571:                             $refused = '';
                   10572:                         } else {
                   10573:                             my %domdefaults = &get_domain_defaults($udom);
                   10574:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   10575:                                 my $checkbystatus;
                   10576:                                 if ($env{'user.adv'}) { 
                   10577:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   10578:                                     if ($disposition eq 'automatic') {
                   10579:                                         $refused = '';
                   10580:                                     } elsif ($disposition eq '') {
                   10581:                                         $checkbystatus = 1;
                   10582:                                     } 
                   10583:                                 } else {
                   10584:                                     $checkbystatus = 1;
                   10585:                                 }
                   10586:                                 if ($checkbystatus) {
                   10587:                                     if ($env{'environment.inststatus'}) {
                   10588:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   10589:                                         foreach my $type (@inststatuses) {
                   10590:                                             if (($type ne '') &&
                   10591:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   10592:                                                 $refused = '';
                   10593:                                             }
                   10594:                                         }
                   10595:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   10596:                                         $refused = '';
                   10597:                                     }
                   10598:                                 }
                   10599:                             }
                   10600:                         }
                   10601:                     }
1.1017    raeburn  10602:                 }
                   10603:                 if ($refused) {
1.947     raeburn  10604:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   10605:                              ' '.$role.' '.$end.' '.$start.' by '.
                   10606: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   10607:                     return 'refused';
                   10608:                 }
1.932     raeburn  10609:             }
1.1131    raeburn  10610:         } elsif ($role eq 'au') {
                   10611:             if ($url ne '/'.$udom.'/') {
                   10612:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   10613:                          ' to assign author role for '.$uname.':'.$udom.
                   10614:                          ' in domain: '.$url.' refused (wrong domain).');
                   10615:                 return 'refused';
                   10616:             }
1.104     www      10617:         }
1.21      www      10618:         $mrole=$role;
                   10619:     }
1.620     albertel 10620:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      10621:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      10622:     if ($end) { $command.='_'.$end; }
1.21      www      10623:     if ($start) {
                   10624: 	if ($end) { 
1.81      www      10625:            $command.='_'.$start; 
1.21      www      10626:         } else {
1.81      www      10627:            $command.='_0_'.$start;
1.21      www      10628:         }
                   10629:     }
1.739     raeburn  10630:     my $origstart = $start;
                   10631:     my $origend = $end;
1.957     raeburn  10632:     my $delflag;
1.357     www      10633: # actually delete
                   10634:     if ($deleteflag) {
1.373     www      10635: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      10636: # modify command to delete the role
1.620     albertel 10637:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      10638:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 10639: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      10640: # set start and finish to negative values for userrolelog
                   10641:            $start=-1;
                   10642:            $end=-1;
1.957     raeburn  10643:            $delflag = 1;
1.357     www      10644:         }
                   10645:     }
                   10646: # send command
1.349     www      10647:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      10648: # log new user role if status is ok
1.349     www      10649:     if ($answer eq 'ok') {
1.663     raeburn  10650: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  10651:         if (($role eq 'cc') || ($role eq 'in') ||
                   10652:             ($role eq 'ep') || ($role eq 'ad') ||
                   10653:             ($role eq 'ta') || ($role eq 'st') ||
                   10654:             ($role=~/^cr/) || ($role eq 'gr') ||
                   10655:             ($role eq 'co')) {
1.1200    raeburn  10656: # for course roles, perform group memberships changes triggered by role change.
                   10657:             unless ($role =~ /^gr/) {
                   10658:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   10659:                                                  $origstart,$selfenroll,$context);
                   10660:             }
1.1184    raeburn  10661:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10662:                            $selfenroll,$context);
                   10663:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334    raeburn  10664:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
                   10665:                  ($role eq 'da')) {
1.1184    raeburn  10666:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10667:                            $context);
                   10668:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
                   10669:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   10670:                              $context); 
                   10671:         }
1.1053    raeburn  10672:         if ($role eq 'cc') {
                   10673:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   10674:         }
1.349     www      10675:     }
                   10676:     return $answer;
1.169     harris41 10677: }
                   10678: 
1.1053    raeburn  10679: sub autoupdate_coowners {
                   10680:     my ($url,$end,$start,$uname,$udom) = @_;
                   10681:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   10682:     if (($cdom ne '') && ($cnum ne '')) {
                   10683:         my $now = time;
                   10684:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   10685:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   10686:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   10687:             my $instcode = $coursehash{'internal.coursecode'};
1.1444    raeburn  10688:             my $xlists = $coursehash{'internal.crosslistings'};
1.1053    raeburn  10689:             if ($instcode ne '') {
1.1056    raeburn  10690:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   10691:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  10692:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  10693:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1444    raeburn  10694:                         unless ($result eq 'valid') {
                   10695:                             if ($xlists ne '') {
                   10696:                                 foreach my $xlist (split(',',$xlists)) {
                   10697:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
                   10698:                                     $result =
1.1446    raeburn  10699:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
                   10700:                                                                       $inst_crosslist,$uname.':'.$udom);
                   10701:                                     last if ($result eq 'valid');
1.1444    raeburn  10702:                                 }
                   10703:                             }
                   10704:                         }
1.1056    raeburn  10705:                         if ($result eq 'valid') {
                   10706:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  10707:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   10708:                                     push(@newcoowners,$coowner);
                   10709:                                 }
                   10710:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   10711:                                     push(@newcoowners,$uname.':'.$udom);
                   10712:                                 }
                   10713:                                 @newcoowners = sort(@newcoowners);
                   10714:                             } else {
                   10715:                                 push(@newcoowners,$uname.':'.$udom);
                   10716:                             }
1.1444    raeburn  10717:                         } elsif ($coursehash{'internal.co-owners'}) {
                   10718:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   10719:                                 unless ($coowner eq $uname.':'.$udom) {
                   10720:                                     push(@newcoowners,$coowner);
1.1053    raeburn  10721:                                 }
1.1444    raeburn  10722:                             }
                   10723:                             unless (@newcoowners > 0) {
                   10724:                                 $delcoowners = 1;
                   10725:                                 $coowners = '';
1.1053    raeburn  10726:                             }
                   10727:                         }
                   10728:                         if (@newcoowners || $delcoowners) {
                   10729:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   10730:                                             $delcoowners,@newcoowners);
                   10731:                         }
                   10732:                     }
                   10733:                 }
                   10734:             }
                   10735:         }
                   10736:     }
                   10737: }
                   10738: 
                   10739: sub store_coowners {
                   10740:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   10741:     my $cid = $cdom.'_'.$cnum;
                   10742:     my ($coowners,$delresult,$putresult);
                   10743:     if (@newcoowners) {
                   10744:         $coowners = join(',',@newcoowners);
                   10745:         my %coownershash = (
                   10746:                             'internal.co-owners' => $coowners,
                   10747:                            );
                   10748:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   10749:         if ($putresult eq 'ok') {
                   10750:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   10751:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   10752:             }
                   10753:         }
                   10754:     }
                   10755:     if ($delcoowners) {
                   10756:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   10757:         if ($delresult eq 'ok') {
                   10758:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   10759:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   10760:             }
                   10761:         }
                   10762:     }
                   10763:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   10764:         my %crsinfo =
1.1494    raeburn  10765:             &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
1.1053    raeburn  10766:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   10767:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
1.1494    raeburn  10768:             my $cidput = &courseidput($cdom,\%crsinfo,$chome,'notime');
1.1053    raeburn  10769:         }
                   10770:     }
                   10771: }
                   10772: 
1.169     harris41 10773: # -------------------------------------------------- Modify user authentication
1.197     www      10774: # Overrides without validation
                   10775: 
1.169     harris41 10776: sub modifyuserauth {
                   10777:     my ($udom,$uname,$umode,$upass)=@_;
                   10778:     my $uhome=&homeserver($uname,$udom);
1.1409    raeburn  10779:     my $allowed;
                   10780:     if (&allowed('mau',$udom)) {
                   10781:         $allowed = 1;
                   10782:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
                   10783:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
                   10784:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
                   10785:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   10786:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   10787:         if (($cdom ne '') && ($cnum ne '')) {
                   10788:             my $is_owner = &is_course_owner($cdom,$cnum);
                   10789:             if ($is_owner) {
                   10790:                 $allowed = 1;
                   10791:             }
                   10792:         }
                   10793:     }
                   10794:     unless ($allowed) { return 'refused'; }
1.197     www      10795:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 10796:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10797:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 10798:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   10799: 		     &escape($upass),$uhome);
1.1434    raeburn  10800:     my $ip = &get_requestor_ip();
1.620     albertel 10801:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      10802:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1434    raeburn  10803:          '(Remote '.$ip.'): '.$reply);
1.197     www      10804:     &log($udom,,$uname,$uhome,
1.620     albertel 10805:         'Authentication changed by '.$env{'user.domain'}.', '.
                   10806:                                      $env{'user.name'}.', '.$umode.
1.1435    raeburn  10807:          '(Remote '.$ip.'): '.$reply);
1.169     harris41 10808:     unless ($reply eq 'ok') {
1.197     www      10809:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 10810: 	return 'error: '.$reply;
                   10811:     }   
1.170     harris41 10812:     return 'ok';
1.80      www      10813: }
                   10814: 
1.81      www      10815: # --------------------------------------------------------------- Modify a user
1.80      www      10816: 
1.81      www      10817: sub modifyuser {
1.206     matthew  10818:     my ($udom,    $uname, $uid,
                   10819:         $umode,   $upass, $first,
                   10820:         $middle,  $last,  $gene,
1.1058    raeburn  10821:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 10822:     $udom= &LONCAPA::clean_domain($udom);
                   10823:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  10824:     my $showcandelete = 'none';
                   10825:     if (ref($candelete) eq 'ARRAY') {
                   10826:         if (@{$candelete} > 0) {
                   10827:             $showcandelete = join(', ',@{$candelete});
                   10828:         }
                   10829:     }
1.81      www      10830:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      10831:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  10832: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  10833:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   10834:                                      ' desiredhome not specified'). 
1.620     albertel 10835:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10836:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 10837:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  10838:     my $newuser;
                   10839:     if ($uhome eq 'no_host') {
                   10840:         $newuser = 1;
1.1368    raeburn  10841:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
                   10842:                 ($umode eq 'lti')) {
                   10843:             return 'error: more information needed to create new user';
                   10844:         }
1.1075    raeburn  10845:     }
1.80      www      10846: # ----------------------------------------------------------------- Create User
1.406     albertel 10847:     if (($uhome eq 'no_host') && 
1.1368    raeburn  10848: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80      www      10849:         my $unhome='';
1.844     albertel 10850:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  10851:             $unhome = $desiredhome;
1.620     albertel 10852: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   10853: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  10854:         } else { # load balancing routine for determining $unhome
1.81      www      10855:             my $loadm=10000000;
1.841     albertel 10856: 	    my %servers = &get_servers($udom,'library');
                   10857: 	    foreach my $tryserver (keys(%servers)) {
                   10858: 		my $answer=reply('load',$tryserver);
                   10859: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   10860: 		    $loadm=$answer;
                   10861: 		    $unhome=$tryserver;
                   10862: 		}
1.80      www      10863: 	    }
                   10864:         }
                   10865:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  10866: 	    return 'error: unable to find a home server for '.$uname.
                   10867:                    ' in domain '.$udom;
1.80      www      10868:         }
                   10869:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   10870:                          &escape($upass),$unhome);
                   10871: 	unless ($reply eq 'ok') {
                   10872:             return 'error: '.$reply;
                   10873:         }   
1.230     stredwic 10874:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      10875:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  10876: 	    return 'error: unable verify users home machine.';
1.80      www      10877:         }
1.209     matthew  10878:     }   # End of creation of new user
1.80      www      10879: # ---------------------------------------------------------------------- Add ID
                   10880:     if ($uid) {
                   10881:        $uid=~tr/A-Z/a-z/;
                   10882:        my %uidhash=&idrget($udom,$uname);
1.196     www      10883:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   10884:          && (!$forceid)) {
1.80      www      10885: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  10886: 	      return 'error: user id "'.$uid.'" does not match '.
                   10887:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      10888:           }
                   10889:        } else {
1.1300    raeburn  10890: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
1.80      www      10891:        }
                   10892:     }
                   10893: # -------------------------------------------------------------- Add names, etc
1.313     matthew  10894:     my @tmp=&get('environment',
1.899     raeburn  10895: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  10896:                     'permanentemail','inststatus'],
1.134     albertel 10897: 		   $udom,$uname);
1.1075    raeburn  10898:     my (%names,%oldnames);
1.313     matthew  10899:     if ($tmp[0] =~ m/^error:.*/) { 
                   10900:         %names=(); 
                   10901:     } else {
                   10902:         %names = @tmp;
1.1075    raeburn  10903:         %oldnames = %names;
1.313     matthew  10904:     }
1.388     www      10905: #
1.1058    raeburn  10906: # If name, email and/or uid are blank (e.g., because an uploaded file
                   10907: # of users did not contain them), do not overwrite existing values
                   10908: # unless field is in $candelete array ref.  
                   10909: #
                   10910: 
                   10911:     my @fields = ('firstname','middlename','lastname','generation',
                   10912:                   'permanentemail','id');
                   10913:     my %newvalues;
                   10914:     if (ref($candelete) eq 'ARRAY') {
                   10915:         foreach my $field (@fields) {
                   10916:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   10917:                 if ($field eq 'firstname') {
                   10918:                     $names{$field} = $first;
                   10919:                 } elsif ($field eq 'middlename') {
                   10920:                     $names{$field} = $middle;
                   10921:                 } elsif ($field eq 'lastname') {
                   10922:                     $names{$field} = $last;
                   10923:                 } elsif ($field eq 'generation') { 
                   10924:                     $names{$field} = $gene;
                   10925:                 } elsif ($field eq 'permanentemail') {
                   10926:                     $names{$field} = $email;
                   10927:                 } elsif ($field eq 'id') {
                   10928:                     $names{$field}  = $uid;
                   10929:                 }
                   10930:             }
                   10931:         }
                   10932:     }
1.388     www      10933:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  10934:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      10935:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  10936:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      10937:     if ($email) {
                   10938:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  10939:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      10940:     }
1.899     raeburn  10941:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  10942:     if (defined($inststatus)) {
                   10943:         $names{'inststatus'} = '';
                   10944:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   10945:         if (ref($usertypes) eq 'HASH') {
                   10946:             my @okstatuses; 
                   10947:             foreach my $item (split(/:/,$inststatus)) {
                   10948:                 if (defined($usertypes->{$item})) {
                   10949:                     push(@okstatuses,$item);  
                   10950:                 }
                   10951:             }
                   10952:             if (@okstatuses) {
                   10953:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   10954:             }
                   10955:         }
                   10956:     }
1.1075    raeburn  10957:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  10958:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  10959:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  10960:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   10961:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   10962:     } else {
                   10963:         $logmsg .= ' during self creation';
                   10964:     }
1.1075    raeburn  10965:     my $changed;
                   10966:     if ($newuser) {
                   10967:         $changed = 1;
                   10968:     } else {
                   10969:         foreach my $field (@fields) {
                   10970:             if ($names{$field} ne $oldnames{$field}) {
                   10971:                 $changed = 1;
                   10972:                 last;
                   10973:             }
                   10974:         }
                   10975:     }
                   10976:     unless ($changed) {
                   10977:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   10978:         &logthis($logmsg);
                   10979:         return 'ok';
                   10980:     }
                   10981:     my $reply = &put('environment', \%names, $udom,$uname);
                   10982:     if ($reply ne 'ok') { 
                   10983:         return 'error: '.$reply;
                   10984:     }
1.1087    raeburn  10985:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
1.1494    raeburn  10986:         &devalidate_cache_new('emailscache',$uname.':'.$udom);
1.1087    raeburn  10987:     }
1.1075    raeburn  10988:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   10989:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   10990:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  10991:     &logthis($logmsg);
1.134     albertel 10992:     return 'ok';
1.80      www      10993: }
                   10994: 
1.81      www      10995: # -------------------------------------------------------------- Modify student
1.80      www      10996: 
1.81      www      10997: sub modifystudent {
                   10998:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  10999:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314    raeburn  11000:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455     albertel 11001:     if (!$cid) {
1.620     albertel 11002: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 11003: 	    return 'not_in_class';
                   11004: 	}
1.80      www      11005:     }
                   11006: # --------------------------------------------------------------- Make the user
1.81      www      11007:     my $reply=&modifyuser
1.209     matthew  11008: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  11009:          $desiredhome,$email,$inststatus);
1.80      www      11010:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  11011:     # This will cause &modify_student_enrollment to get the uid from the
1.1235    raeburn  11012:     # student's environment
1.297     matthew  11013:     $uid = undef if (!$forceid);
1.455     albertel 11014:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  11015:                                         $gene,$usec,$end,$start,$type,$locktype,
1.1314    raeburn  11016:                                         $cid,$selfenroll,$context,$credits,$instsec);
1.297     matthew  11017:     return $reply;
                   11018: }
                   11019: 
                   11020: sub modify_student_enrollment {
1.1216    raeburn  11021:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314    raeburn  11022:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455     albertel 11023:     my ($cdom,$cnum,$chome);
                   11024:     if (!$cid) {
1.620     albertel 11025: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 11026: 	    return 'not_in_class';
                   11027: 	}
1.620     albertel 11028: 	$cdom=$env{'course.'.$cid.'.domain'};
                   11029: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 11030:     } else {
                   11031: 	($cdom,$cnum)=split(/_/,$cid);
                   11032:     }
1.620     albertel 11033:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 11034:     if (!$chome) {
1.457     raeburn  11035: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  11036:     }
1.455     albertel 11037:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  11038:     # Make sure the user exists
1.81      www      11039:     my $uhome=&homeserver($uname,$udom);
                   11040:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   11041: 	return 'error: no such user';
                   11042:     }
1.297     matthew  11043:     # Get student data if we were not given enough information
                   11044:     if (!defined($first)  || $first  eq '' || 
                   11045:         !defined($last)   || $last   eq '' || 
                   11046:         !defined($uid)    || $uid    eq '' || 
                   11047:         !defined($middle) || $middle eq '' || 
                   11048:         !defined($gene)   || $gene   eq '') {
1.294     matthew  11049:         # They did not supply us with enough data to enroll the student, so
                   11050:         # we need to pick up more information.
1.297     matthew  11051:         my %tmp = &get('environment',
1.294     matthew  11052:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  11053:                        ,$udom,$uname);
                   11054: 
1.800     albertel 11055:         #foreach my $key (keys(%tmp)) {
                   11056:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 11057:         #}
1.294     matthew  11058:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   11059:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   11060:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  11061:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  11062:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   11063:     }
1.556     albertel 11064:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  11065:     my $user = "$uname:$udom";
1.1494    raeburn  11066:     my %old_entry = &get('classlist',[$user],$cdom,$cnum);
1.487     albertel 11067:     my $reply=cput('classlist',
1.1148    raeburn  11068: 		   {$user => 
1.1314    raeburn  11069: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487     albertel 11070: 		   $cdom,$cnum);
1.1148    raeburn  11071:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   11072:         &devalidate_getsection_cache($udom,$uname,$cid);
                   11073:     } else { 
1.81      www      11074: 	return 'error: '.$reply;
                   11075:     }
1.297     matthew  11076:     # Add student role to user
1.83      www      11077:     my $uurl='/'.$cid;
1.81      www      11078:     $uurl=~s/\_/\//g;
                   11079:     if ($usec) {
                   11080: 	$uurl.='/'.$usec;
                   11081:     }
1.1148    raeburn  11082:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   11083:                              $selfenroll,$context);
                   11084:     if ($result ne 'ok') {
                   11085:         if ($old_entry{$user} ne '') {
                   11086:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   11087:         } else {
                   11088:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   11089:         }
                   11090:     }
                   11091:     return $result; 
1.21      www      11092: }
                   11093: 
1.556     albertel 11094: sub format_name {
                   11095:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   11096:     my $name;
                   11097:     if ($first ne 'lastname') {
                   11098: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   11099:     } else {
                   11100: 	if ($lastname=~/\S/) {
                   11101: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   11102: 	    $name=~s/\s+,/,/;
                   11103: 	} else {
                   11104: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   11105: 	}
                   11106:     }
                   11107:     $name=~s/^\s+//;
                   11108:     $name=~s/\s+$//;
                   11109:     $name=~s/\s+/ /g;
                   11110:     return $name;
                   11111: }
                   11112: 
1.84      www      11113: # ------------------------------------------------- Write to course preferences
                   11114: 
                   11115: sub writecoursepref {
                   11116:     my ($courseid,%prefs)=@_;
                   11117:     $courseid=~s/^\///;
                   11118:     $courseid=~s/\_/\//g;
                   11119:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   11120:     my $chome=homeserver($cnum,$cdomain);
                   11121:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   11122: 	return 'error: no such course';
                   11123:     }
                   11124:     my $cstring='';
1.800     albertel 11125:     foreach my $pref (keys(%prefs)) {
                   11126: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 11127:     }
1.84      www      11128:     $cstring=~s/\&$//;
                   11129:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   11130: }
                   11131: 
                   11132: # ---------------------------------------------------------- Make/modify course
                   11133: 
                   11134: sub createcourse {
1.741     raeburn  11135:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1423    raeburn  11136:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84      www      11137:     $url=&declutter($url);
                   11138:     my $cid='';
1.1028    raeburn  11139:     if ($context eq 'requestcourses') {
                   11140:         my $can_create = 0;
                   11141:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   11142:         if ($udom eq $ownerdom) {
1.1423    raeburn  11143:             my $reload;
                   11144:             if (($callercontext eq 'auto') &&
                   11145:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
                   11146:                 $reload = 'reload';
                   11147:             }
                   11148:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028    raeburn  11149:                                   $context)) {
                   11150:                 $can_create = 1;
                   11151:             }
                   11152:         } else {
                   11153:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   11154:                                            $category);
                   11155:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   11156:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   11157:                 if (@curr > 0) {
                   11158:                     my @options = qw(approval validate autolimit);
                   11159:                     my $optregex = join('|',@options);
                   11160:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   11161:                         $can_create = 1;
                   11162:                     }
                   11163:                 }
                   11164:             }
                   11165:         }
                   11166:         if ($can_create) {
                   11167:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   11168:                 unless (&allowed('ccc',$udom)) {
                   11169:                     return 'refused'; 
                   11170:                 }
1.1017    raeburn  11171:             }
                   11172:         } else {
                   11173:             return 'refused';
                   11174:         }
1.1028    raeburn  11175:     } elsif (!&allowed('ccc',$udom)) {
                   11176:         return 'refused';
1.84      www      11177:     }
1.1011    raeburn  11178: # --------------------------------------------------------------- Get Unique ID
                   11179:     my $uname;
                   11180:     if ($cnum =~ /^$match_courseid$/) {
                   11181:         my $chome=&homeserver($cnum,$udom,'true');
                   11182:         if (($chome eq '') || ($chome eq 'no_host')) {
                   11183:             $uname = $cnum;
                   11184:         } else {
1.1038    raeburn  11185:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  11186:         }
                   11187:     } else {
1.1038    raeburn  11188:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  11189:     }
                   11190:     return $uname if ($uname =~ /^error/);
                   11191: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  11192:     if (!defined($course_server)) {
                   11193:         if (defined(&domain($udom,'primary'))) {
                   11194:             $course_server = &domain($udom,'primary');
                   11195:         } else {
                   11196:             $course_server = $env{'user.home'}; 
                   11197:         }
                   11198:     }
                   11199:     my %host_servers =
1.1494    raeburn  11200:         &get_servers($udom,'library');
1.1052    raeburn  11201:     unless ($host_servers{$course_server}) {
                   11202:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  11203:     }
1.84      www      11204: # ------------------------------------------------------------- Make the course
                   11205:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  11206:                       $course_server);
1.84      www      11207:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  11208:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      11209:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   11210: 	return 'error: no such course';
                   11211:     }
1.271     www      11212: # ----------------------------------------------------------------- Course made
1.516     raeburn  11213: # log existence
1.1029    raeburn  11214:     my $now = time;
1.918     raeburn  11215:     my $newcourse = {
                   11216:                     $udom.'_'.$uname => {
1.921     raeburn  11217:                                      description => $description,
                   11218:                                      inst_code   => $inst_code,
                   11219:                                      owner       => $course_owner,
                   11220:                                      type        => $crstype,
1.1029    raeburn  11221:                                      creator     => $env{'user.name'}.':'.
                   11222:                                                     $env{'user.domain'},
                   11223:                                      created     => $now,
                   11224:                                      context     => $context,
1.918     raeburn  11225:                                                 },
                   11226:                     };
1.921     raeburn  11227:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      11228: # set toplevel url
1.271     www      11229:     my $topurl=$url;
                   11230:     unless ($nonstandard) {
                   11231: # ------------------------------------------ For standard courses, make top url
                   11232:         my $mapurl=&clutter($url);
1.278     www      11233:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 11234:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      11235: <map>
                   11236: <resource id="1" type="start"></resource>
                   11237: <resource id="2" src="$mapurl"></resource>
                   11238: <resource id="3" type="finish"></resource>
                   11239: <link index="1" from="1" to="2"></link>
                   11240: <link index="2" from="2" to="3"></link>
                   11241: </map>
                   11242: ENDINITMAP
                   11243:         $topurl=&declutter(
1.638     albertel 11244:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      11245:                           );
                   11246:     }
                   11247: # ----------------------------------------------------------- Write preferences
1.84      www      11248:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  11249:                      ('description'              => $description,
                   11250:                       'url'                      => $topurl,
                   11251:                       'internal.creator'         => $env{'user.name'}.':'.
                   11252:                                                     $env{'user.domain'},
                   11253:                       'internal.created'         => $now,
                   11254:                       'internal.creationcontext' => $context)
                   11255:                     );
1.84      www      11256:     return '/'.$udom.'/'.$uname;
                   11257: }
                   11258: 
1.1011    raeburn  11259: # ------------------------------------------------------------------- Create ID
                   11260: sub generate_coursenum {
1.1038    raeburn  11261:     my ($udom,$crstype) = @_;
1.1011    raeburn  11262:     my $domdesc = &domain($udom);
                   11263:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  11264:     my $first;
                   11265:     if ($crstype eq 'Community') {
                   11266:         $first = '0';
                   11267:     } else {
                   11268:         $first = int(1+rand(9)); 
                   11269:     } 
                   11270:     my $uname=$first.
1.1011    raeburn  11271:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   11272:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   11273:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   11274: # ----------------------------------------------- Make sure that does not exist
                   11275:     my $uhome=&homeserver($uname,$udom,'true');
                   11276:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  11277:         if ($crstype eq 'Community') {
                   11278:             $first = '0';
                   11279:         } else {
                   11280:             $first = int(1+rand(9));
                   11281:         }
                   11282:         $uname=$first.
1.1011    raeburn  11283:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   11284:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   11285:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   11286:         $uhome=&homeserver($uname,$udom,'true');
                   11287:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   11288:             return 'error: unable to generate unique course-ID';
                   11289:         }
                   11290:     }
                   11291:     return $uname;
                   11292: }
                   11293: 
1.813     albertel 11294: sub is_course {
1.1167    droeschl 11295:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   11296:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   11297: 
1.1381    raeburn  11298:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
                   11299:     my $uhome=&homeserver($cnum,$cdom);
                   11300:     my $iscourse;
                   11301:     if (grep { $_ eq $uhome } current_machine_ids()) {
1.1382    raeburn  11302:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
1.1381    raeburn  11303:     } else {
                   11304:         my $hashid = $cdom.':'.$cnum;
                   11305:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
                   11306:         unless (defined($cached)) {
                   11307:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
                   11308:                                         $cnum,undef,undef,'.');
                   11309:             $iscourse = 0;
                   11310:             if (exists($courses{$cdom.'_'.$cnum})) {
                   11311:                 $iscourse = 1;
                   11312:             }
                   11313:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
                   11314:         }
                   11315:     }
                   11316:     return unless ($iscourse);
1.1167    droeschl 11317:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 11318: }
                   11319: 
1.1015    raeburn  11320: sub store_userdata {
                   11321:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  11322:     my $result;
1.1016    raeburn  11323:     if ($datakey ne '') {
1.1013    raeburn  11324:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  11325:             if ($udom eq '' || $uname eq '') {
                   11326:                 $udom = $env{'user.domain'};
                   11327:                 $uname = $env{'user.name'};
                   11328:             }
                   11329:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  11330:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   11331:                 $result = 'error: no_host';
                   11332:             } else {
1.1434    raeburn  11333:                 $storehash->{'ip'} = &get_requestor_ip();
1.1013    raeburn  11334:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   11335: 
                   11336:                 my $namevalue='';
                   11337:                 foreach my $key (keys(%{$storehash})) {
                   11338:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   11339:                 }
                   11340:                 $namevalue=~s/\&$//;
1.1224    raeburn  11341:                 unless ($namespace eq 'courserequests') {
                   11342:                     $datakey = &escape($datakey);
                   11343:                 }
1.1105    raeburn  11344:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   11345:                                   $namevalue,$uhome);
1.1013    raeburn  11346:             }
                   11347:         } else {
                   11348:             $result = 'error: data to store was not a hash reference'; 
                   11349:         }
                   11350:     } else {
                   11351:         $result= 'error: invalid requestkey'; 
                   11352:     }
                   11353:     return $result;
                   11354: }
                   11355: 
1.21      www      11356: # ---------------------------------------------------------- Assign Custom Role
                   11357: 
                   11358: sub assigncustomrole {
1.957     raeburn  11359:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      11360:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  11361:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      11362: }
                   11363: 
                   11364: # ----------------------------------------------------------------- Revoke Role
                   11365: 
                   11366: sub revokerole {
1.957     raeburn  11367:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      11368:     my $now=time;
1.965     raeburn  11369:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      11370: }
                   11371: 
                   11372: # ---------------------------------------------------------- Revoke Custom Role
                   11373: 
                   11374: sub revokecustomrole {
1.957     raeburn  11375:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      11376:     my $now=time;
1.357     www      11377:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  11378:            $deleteflag,$selfenroll,$context);
1.17      www      11379: }
                   11380: 
1.533     banghart 11381: # ------------------------------------------------------------ Disk usage
1.535     albertel 11382: sub diskusage {
1.955     raeburn  11383:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   11384:     $directorypath =~ s/\/$//;
                   11385:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   11386:                        .&escape($getpropath).':'.&escape($uname).':'
                   11387:                        .&escape($udom),homeserver($uname,$udom));
                   11388:     if ($listing eq 'unknown_cmd') {
                   11389:         if ($getpropath) {
                   11390:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   11391:         }
                   11392:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   11393:     }
1.514     albertel 11394:     return $listing;
1.512     banghart 11395: }
                   11396: 
1.566     banghart 11397: sub is_locked {
1.1096    raeburn  11398:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 11399:     my @check;
                   11400:     my $is_locked;
1.1093    raeburn  11401:     push (@check,$file_name);
1.613     albertel 11402:     my %locked = &get('file_permissions',\@check,
1.620     albertel 11403: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 11404:     my ($tmp)=keys(%locked);
                   11405:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  11406:     
1.566     banghart 11407:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  11408:         $is_locked = 'false';
                   11409:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  11410:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  11411:                $is_locked = 'true';
1.1096    raeburn  11412:                if (ref($which) eq 'ARRAY') {
                   11413:                    push(@{$which},$entry);
                   11414:                } else {
                   11415:                    last;
                   11416:                }
1.745     raeburn  11417:            }
                   11418:        }
1.566     banghart 11419:     } else {
                   11420:         $is_locked = 'false';
                   11421:     }
1.1093    raeburn  11422:     return $is_locked;
1.566     banghart 11423: }
                   11424: 
1.759     albertel 11425: sub declutter_portfile {
                   11426:     my ($file) = @_;
1.833     albertel 11427:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 11428:     return $file;
                   11429: }
                   11430: 
1.559     banghart 11431: # ------------------------------------------------------------- Mark as Read Only
                   11432: 
                   11433: sub mark_as_readonly {
                   11434:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 11435:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11436:     my ($tmp)=keys(%current_permissions);
                   11437:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 11438:     foreach my $file (@{$files}) {
1.759     albertel 11439: 	$file = &declutter_portfile($file);
1.561     banghart 11440:         push(@{$current_permissions{$file}},$what);
1.559     banghart 11441:     }
1.613     albertel 11442:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11443:     return;
                   11444: }
                   11445: 
1.572     banghart 11446: # ------------------------------------------------------------Save Selected Files
                   11447: 
                   11448: sub save_selected_files {
                   11449:     my ($user, $path, @files) = @_;
                   11450:     my $filename = $user."savedfiles";
1.573     banghart 11451:     my @other_files = &files_not_in_path($user, $path);
1.1359    raeburn  11452:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.573     banghart 11453:     foreach my $file (@files) {
1.620     albertel 11454:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 11455:     }
                   11456:     foreach my $file (@other_files) {
1.574     banghart 11457:         print (OUT $file."\n");
1.572     banghart 11458:     }
1.574     banghart 11459:     close (OUT);
1.572     banghart 11460:     return 'ok';
                   11461: }
                   11462: 
1.574     banghart 11463: sub clear_selected_files {
                   11464:     my ($user) = @_;
                   11465:     my $filename = $user."savedfiles";
1.1359    raeburn  11466:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.574     banghart 11467:     print (OUT undef);
                   11468:     close (OUT);
                   11469:     return ("ok");    
                   11470: }
                   11471: 
1.572     banghart 11472: sub files_in_path {
                   11473:     my ($user, $path) = @_;
                   11474:     my $filename = $user."savedfiles";
                   11475:     my %return_files;
1.1359    raeburn  11476:     open (IN,'<',LONCAPA::tempdir().$filename);
1.573     banghart 11477:     while (my $line_in = <IN>) {
1.574     banghart 11478:         chomp ($line_in);
                   11479:         my @paths_and_file = split (m!/!, $line_in);
                   11480:         my $file_part = pop (@paths_and_file);
                   11481:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 11482:         $path_part.='/';
                   11483:         my $path_and_file = $path_part.$file_part;
                   11484:         if ($path_part eq $path) {
                   11485:             $return_files{$file_part}= 'selected';
                   11486:         }
                   11487:     }
1.574     banghart 11488:     close (IN);
                   11489:     return (\%return_files);
1.572     banghart 11490: }
                   11491: 
                   11492: # called in portfolio select mode, to show files selected NOT in current directory
                   11493: sub files_not_in_path {
                   11494:     my ($user, $path) = @_;
                   11495:     my $filename = $user."savedfiles";
                   11496:     my @return_files;
                   11497:     my $path_part;
1.1359    raeburn  11498:     open(IN, '<',LONCAPA::tempdir().$filename);
1.800     albertel 11499:     while (my $line = <IN>) {
1.572     banghart 11500:         #ok, I know it's clunky, but I want it to work
1.800     albertel 11501:         my @paths_and_file = split(m|/|, $line);
                   11502:         my $file_part = pop(@paths_and_file);
                   11503:         chomp($file_part);
                   11504:         my $path_part = join('/', @paths_and_file);
1.572     banghart 11505:         $path_part .= '/';
                   11506:         my $path_and_file = $path_part.$file_part;
                   11507:         if ($path_part ne $path) {
1.800     albertel 11508:             push(@return_files, ($path_and_file));
1.572     banghart 11509:         }
                   11510:     }
1.800     albertel 11511:     close(OUT);
1.574     banghart 11512:     return (@return_files);
1.572     banghart 11513: }
                   11514: 
1.1273    raeburn  11515: #------------------------------Submitted/Handedback Portfolio Files Versioning
                   11516:  
                   11517: sub portfiles_versioning {
                   11518:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
                   11519:     my $portfolio_root = '/userfiles/portfolio';
                   11520:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
                   11521:     foreach my $file (@{$portfiles}) {
                   11522:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
                   11523:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   11524:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
                   11525:         my $getpropath = 1;
                   11526:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
                   11527:                                              $stu_name,$getpropath);
                   11528:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
                   11529:         my $new_answer = 
                   11530:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
                   11531:         if ($new_answer ne 'problem getting file') {
                   11532:             push(@{$versioned_portfiles}, $directory.$new_answer);
                   11533:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
                   11534:                               [$symb,$env{'request.course.id'},'graded']);
                   11535:         }
                   11536:     }
                   11537: }
                   11538: 
                   11539: sub get_next_version {
                   11540:     my ($answer_name, $answer_ext, $dir_list) = @_;
                   11541:     my $version;
                   11542:     if (ref($dir_list) eq 'ARRAY') {
                   11543:         foreach my $row (@{$dir_list}) {
                   11544:             my ($file) = split(/\&/,$row,2);
                   11545:             my ($file_name,$file_version,$file_ext) =
                   11546:                 &file_name_version_ext($file);
                   11547:             if (($file_name eq $answer_name) &&
                   11548:                 ($file_ext eq $answer_ext)) {
                   11549:                      # gets here if filename and extension match,
                   11550:                      # regardless of version
                   11551:                 if ($file_version ne '') {
                   11552:                     # a versioned file is found  so save it for later
                   11553:                     if ($file_version > $version) {
                   11554:                         $version = $file_version;
                   11555:                     }
                   11556:                 }
                   11557:             }
                   11558:         }
                   11559:     }
                   11560:     $version ++;
                   11561:     return($version);
                   11562: }
                   11563: 
                   11564: sub version_selected_portfile {
                   11565:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   11566:     my ($answer_name,$answer_ver,$answer_ext) =
                   11567:         &file_name_version_ext($file_name);
                   11568:     my $new_answer;
                   11569:     $env{'form.copy'} =
                   11570:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   11571:     if($env{'form.copy'} eq '-1') {
                   11572:         $new_answer = 'problem getting file';
                   11573:     } else {
                   11574:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   11575:         my $copy_result = 
                   11576:             &finishuserfileupload($stu_name,$domain,'copy',
                   11577:                                   '/portfolio'.$directory.$new_answer);
                   11578:     }
                   11579:     undef($env{'form.copy'});
                   11580:     return ($new_answer);
                   11581: }
                   11582: 
                   11583: sub file_name_version_ext {
                   11584:     my ($file)=@_;
                   11585:     my @file_parts = split(/\./, $file);
                   11586:     my ($name,$version,$ext);
                   11587:     if (@file_parts > 1) {
                   11588:         $ext=pop(@file_parts);
                   11589:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   11590:             $version=pop(@file_parts);
                   11591:         }
                   11592:         $name=join('.',@file_parts);
                   11593:     } else {
                   11594:         $name=join('.',@file_parts);
                   11595:     }
                   11596:     return($name,$version,$ext);
                   11597: }
                   11598: 
1.745     raeburn  11599: #----------------------------------------------Get portfolio file permissions
1.629     banghart 11600: 
1.745     raeburn  11601: sub get_portfile_permissions {
                   11602:     my ($domain,$user) = @_;
1.613     albertel 11603:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 11604:     my ($tmp)=keys(%current_permissions);
                   11605:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11606:     return \%current_permissions;
                   11607: }
                   11608: 
                   11609: #---------------------------------------------Get portfolio file access controls
                   11610: 
1.749     raeburn  11611: sub get_access_controls {
1.745     raeburn  11612:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 11613:     my %access;
                   11614:     my $real_file = $file;
                   11615:     $file =~ s/\.meta$//;
1.745     raeburn  11616:     if (defined($file)) {
1.749     raeburn  11617:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   11618:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 11619:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  11620:             }
                   11621:         }
1.745     raeburn  11622:     } else {
1.749     raeburn  11623:         foreach my $key (keys(%{$current_permissions})) {
                   11624:             if ($key =~ /\0accesscontrol$/) {
                   11625:                 if (defined($group)) {
                   11626:                     if ($key !~ m-^\Q$group\E/-) {
                   11627:                         next;
                   11628:                     }
                   11629:                 }
                   11630:                 my ($fullpath) = split(/\0/,$key);
                   11631:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   11632:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   11633:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   11634:                     }
                   11635:                 }
                   11636:             }
                   11637:         }
                   11638:     }
                   11639:     return %access;
                   11640: }
                   11641: 
                   11642: sub modify_access_controls {
                   11643:     my ($file_name,$changes,$domain,$user)=@_;
                   11644:     my ($outcome,$deloutcome);
                   11645:     my %store_permissions;
                   11646:     my %new_values;
                   11647:     my %new_control;
                   11648:     my %translation;
                   11649:     my @deletions = ();
                   11650:     my $now = time;
                   11651:     if (exists($$changes{'activate'})) {
                   11652:         if (ref($$changes{'activate'}) eq 'HASH') {
                   11653:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   11654:             my $numnew = scalar(@newitems);
                   11655:             for (my $i=0; $i<$numnew; $i++) {
                   11656:                 my $newkey = $newitems[$i];
                   11657:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  11658:                 if ($newkey =~ /^\d+:/) { 
                   11659:                     $newkey =~ s/^(\d+)/$newid/;
                   11660:                     $translation{$1} = $newid;
                   11661:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   11662:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   11663:                     $translation{$1} = $newid;
                   11664:                 }
1.749     raeburn  11665:                 $new_values{$file_name."\0".$newkey} = 
                   11666:                                           $$changes{'activate'}{$newitems[$i]};
                   11667:                 $new_control{$newkey} = $now;
                   11668:             }
                   11669:         }
                   11670:     }
                   11671:     my %todelete;
                   11672:     my %changed_items;
                   11673:     foreach my $action ('delete','update') {
                   11674:         if (exists($$changes{$action})) {
                   11675:             if (ref($$changes{$action}) eq 'HASH') {
                   11676:                 foreach my $key (keys(%{$$changes{$action}})) {
                   11677:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   11678:                     if ($action eq 'delete') { 
                   11679:                         $todelete{$itemnum} = 1;
                   11680:                     } else {
                   11681:                         $changed_items{$itemnum} = $key;
                   11682:                     }
                   11683:                 }
1.745     raeburn  11684:             }
                   11685:         }
1.749     raeburn  11686:     }
                   11687:     # get lock on access controls for file.
                   11688:     my $lockhash = {
                   11689:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   11690:                                                        ':'.$env{'user.domain'},
                   11691:                    }; 
                   11692:     my $tries = 0;
                   11693:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   11694:    
1.1288    damieng  11695:     while (($gotlock ne 'ok') && $tries < 10) {
1.749     raeburn  11696:         $tries ++;
1.1289    damieng  11697:         sleep(0.1);
1.749     raeburn  11698:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   11699:     }
                   11700:     if ($gotlock eq 'ok') {
                   11701:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   11702:         my ($tmp)=keys(%curr_permissions);
                   11703:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   11704:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   11705:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   11706:             if (ref($curr_controls) eq 'HASH') {
                   11707:                 foreach my $control_item (keys(%{$curr_controls})) {
                   11708:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   11709:                     if (defined($todelete{$itemnum})) {
                   11710:                         push(@deletions,$file_name."\0".$control_item);
                   11711:                     } else {
                   11712:                         if (defined($changed_items{$itemnum})) {
                   11713:                             $new_control{$changed_items{$itemnum}} = $now;
                   11714:                             push(@deletions,$file_name."\0".$control_item);
                   11715:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   11716:                         } else {
                   11717:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   11718:                         }
                   11719:                     }
1.745     raeburn  11720:                 }
                   11721:             }
                   11722:         }
1.970     raeburn  11723:         my ($group);
                   11724:         if (&is_course($domain,$user)) {
                   11725:             ($group,my $file) = split(/\//,$file_name,2);
                   11726:         }
1.749     raeburn  11727:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   11728:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   11729:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   11730:         #  remove lock
                   11731:         my @del_lock = ($file_name."\0".'locked_access_records');
                   11732:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  11733:         my $sqlresult =
1.970     raeburn  11734:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  11735:                                     $group);
1.749     raeburn  11736:     } else {
                   11737:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  11738:     }
1.749     raeburn  11739:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  11740: }
                   11741: 
1.827     raeburn  11742: sub make_public_indefinitely {
1.1271    raeburn  11743:     my (@requrl) = @_;
                   11744:     return &automated_portfile_access('public',\@requrl);
                   11745: }
                   11746: 
                   11747: sub automated_portfile_access {
                   11748:     my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273    raeburn  11749:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
                   11750:         return 'invalid';
                   11751:     }
1.1271    raeburn  11752:     my %urls;
                   11753:     if (ref($addsref) eq 'ARRAY') {
                   11754:         foreach my $requrl (@{$addsref}) {
                   11755:             if (&is_portfolio_url($requrl)) {
                   11756:                 unless (exists($urls{$requrl})) {
                   11757:                     $urls{$requrl} = 'add';
                   11758:                 }
                   11759:             }
                   11760:         }
                   11761:     }
                   11762:     if (ref($delsref) eq 'ARRAY') {
                   11763:         foreach my $requrl (@{$delsref}) { 
                   11764:             if (&is_portfolio_url($requrl)) {
                   11765:                 unless (exists($urls{$requrl})) {
                   11766:                     $urls{$requrl} = 'delete'; 
                   11767:                 }
                   11768:             }
                   11769:         }
                   11770:     }
                   11771:     unless (keys(%urls)) {
                   11772:         return 'invalid';
                   11773:     }
                   11774:     my $ip;
                   11775:     if ($accesstype eq 'ip') {
                   11776:         if (ref($info) eq 'HASH') {
                   11777:             if ($info->{'ip'} ne '') {
                   11778:                 $ip = $info->{'ip'};
                   11779:             }
                   11780:         }
                   11781:         if ($ip eq '') {
                   11782:             return 'invalid';
                   11783:         }
                   11784:     }
                   11785:     my $errors;
1.827     raeburn  11786:     my $now = time;
1.1271    raeburn  11787:     my %current_perms;
                   11788:     foreach my $requrl (sort(keys(%urls))) {
                   11789:         my $action;
                   11790:         if ($urls{$requrl} eq 'add') {
                   11791:             $action = 'activate';
                   11792:         } else {
                   11793:             $action = 'none';
                   11794:         }
                   11795:         my $aclnum = 0;
1.827     raeburn  11796:         my (undef,$udom,$unum,$file_name,$group) =
                   11797:             &parse_portfolio_url($requrl);
1.1271    raeburn  11798:         unless (exists($current_perms{$unum.':'.$udom})) {
                   11799:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
                   11800:         }
                   11801:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827     raeburn  11802:                                                    $group,$file_name);
                   11803:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   11804:             my ($num,$scope,$end,$start) = 
                   11805:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271    raeburn  11806:             if ($scope eq $accesstype) {
                   11807:                 if (($start <= $now) && ($end == 0)) {
                   11808:                     if ($accesstype eq 'ip') {
                   11809:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
                   11810:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
                   11811:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
                   11812:                                     if ($urls{$requrl} eq 'add') {
                   11813:                                         $action = 'none';
                   11814:                                         last;
                   11815:                                     } else {
                   11816:                                         $action = 'delete';
                   11817:                                         $aclnum = $num;
                   11818:                                         last;
                   11819:                                     }
                   11820:                                 }
                   11821:                             }
                   11822:                         }
                   11823:                     } elsif ($accesstype eq 'public') {
                   11824:                         if ($urls{$requrl} eq 'add') {
                   11825:                             $action = 'none';
                   11826:                             last;
                   11827:                         } else {
                   11828:                             $action = 'delete';
                   11829:                             $aclnum = $num;
                   11830:                             last;
                   11831:                         }
                   11832:                     }
                   11833:                 } elsif ($accesstype eq 'public') {
1.827     raeburn  11834:                     $action = 'update';
                   11835:                     $aclnum = $num;
1.1271    raeburn  11836:                     last;
1.827     raeburn  11837:                 }
                   11838:             }
                   11839:         }
                   11840:         if ($action eq 'none') {
1.1271    raeburn  11841:             next;
1.827     raeburn  11842:         } else {
                   11843:             my %changes;
                   11844:             my $newend = 0;
                   11845:             my $newstart = $now;
1.1271    raeburn  11846:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827     raeburn  11847:             $changes{$action}{$newkey} = {
1.1271    raeburn  11848:                 type => $accesstype,
1.827     raeburn  11849:                 time => {
                   11850:                     start => $newstart,
                   11851:                     end   => $newend,
                   11852:                 },
                   11853:             };
1.1271    raeburn  11854:             if ($accesstype eq 'ip') {
                   11855:                 $changes{$action}{$newkey}{'ip'} = [$ip];
                   11856:             }
1.827     raeburn  11857:             my ($outcome,$deloutcome,$new_values,$translation) =
                   11858:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271    raeburn  11859:             unless ($outcome eq 'ok') {
                   11860:                 $errors .= $outcome.' ';
                   11861:             }
1.827     raeburn  11862:         }
1.1271    raeburn  11863:     }
                   11864:     if ($errors) {
                   11865:         $errors =~ s/\s$//;
                   11866:         return $errors;
1.827     raeburn  11867:     } else {
1.1271    raeburn  11868:         return 'ok';
1.827     raeburn  11869:     }
                   11870: }
                   11871: 
1.745     raeburn  11872: #------------------------------------------------------Get Marked as Read Only
                   11873: 
                   11874: sub get_marked_as_readonly {
                   11875:     my ($domain,$user,$what,$group) = @_;
                   11876:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 11877:     my @readonly_files;
1.629     banghart 11878:     my $cmp1=$what;
                   11879:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  11880:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11881:         if (defined($group)) {
                   11882:             if ($file_name !~ m-^\Q$group\E/-) {
                   11883:                 next;
                   11884:             }
                   11885:         }
1.561     banghart 11886:         if (ref($value) eq "ARRAY"){
                   11887:             foreach my $stored_what (@{$value}) {
1.629     banghart 11888:                 my $cmp2=$stored_what;
1.759     albertel 11889:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  11890:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  11891:                 }
1.629     banghart 11892:                 if ($cmp1 eq $cmp2) {
1.561     banghart 11893:                     push(@readonly_files, $file_name);
1.745     raeburn  11894:                     last;
1.563     banghart 11895:                 } elsif (!defined($what)) {
                   11896:                     push(@readonly_files, $file_name);
1.745     raeburn  11897:                     last;
1.561     banghart 11898:                 }
                   11899:             }
1.745     raeburn  11900:         }
1.561     banghart 11901:     }
                   11902:     return @readonly_files;
                   11903: }
1.577     banghart 11904: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 11905: 
1.577     banghart 11906: sub get_marked_as_readonly_hash {
1.745     raeburn  11907:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 11908:     my %readonly_files;
1.745     raeburn  11909:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11910:         if (defined($group)) {
                   11911:             if ($file_name !~ m-^\Q$group\E/-) {
                   11912:                 next;
                   11913:             }
                   11914:         }
1.577     banghart 11915:         if (ref($value) eq "ARRAY"){
                   11916:             foreach my $stored_what (@{$value}) {
1.745     raeburn  11917:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 11918:                     foreach my $lock_descriptor(@{$stored_what}) {
                   11919:                         if ($lock_descriptor eq 'graded') {
                   11920:                             $readonly_files{$file_name} = 'graded';
                   11921:                         } elsif ($lock_descriptor eq 'handback') {
                   11922:                             $readonly_files{$file_name} = 'handback';
                   11923:                         } else {
                   11924:                             if (!exists($readonly_files{$file_name})) {
                   11925:                                 $readonly_files{$file_name} = 'locked';
                   11926:                             }
                   11927:                         }
1.745     raeburn  11928:                     }
1.750     banghart 11929:                 } 
1.577     banghart 11930:             }
                   11931:         } 
                   11932:     }
                   11933:     return %readonly_files;
                   11934: }
1.559     banghart 11935: # ------------------------------------------------------------ Unmark as Read Only
                   11936: 
                   11937: sub unmark_as_readonly {
1.629     banghart 11938:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   11939:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  11940:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 11941:     $file_name = &declutter_portfile($file_name);
1.634     albertel 11942:     my $symb_crs = $what;
                   11943:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  11944:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 11945:     my ($tmp)=keys(%current_permissions);
                   11946:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11947:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 11948:     foreach my $file (@readonly_files) {
1.759     albertel 11949: 	my $clean_file = &declutter_portfile($file);
                   11950: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 11951: 	my $current_locks = $current_permissions{$file};
1.563     banghart 11952:         my @new_locks;
                   11953:         my @del_keys;
                   11954:         if (ref($current_locks) eq "ARRAY"){
                   11955:             foreach my $locker (@{$current_locks}) {
1.632     albertel 11956:                 my $compare=$locker;
1.749     raeburn  11957:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  11958:                     $compare=join('',@{$locker});
1.746     raeburn  11959:                     if ($compare ne $symb_crs) {
                   11960:                         push(@new_locks, $locker);
                   11961:                     }
1.563     banghart 11962:                 }
                   11963:             }
1.650     albertel 11964:             if (scalar(@new_locks) > 0) {
1.563     banghart 11965:                 $current_permissions{$file} = \@new_locks;
                   11966:             } else {
                   11967:                 push(@del_keys, $file);
1.613     albertel 11968:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 11969:                 delete($current_permissions{$file});
1.563     banghart 11970:             }
                   11971:         }
1.561     banghart 11972:     }
1.613     albertel 11973:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11974:     return;
                   11975: }
1.512     banghart 11976: 
1.17      www      11977: # ------------------------------------------------------------ Directory lister
                   11978: 
                   11979: sub dirlist {
1.955     raeburn  11980:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      11981:     $uri=~s/^\///;
                   11982:     $uri=~s/\/$//;
1.253     stredwic 11983:     my ($udom, $uname);
1.955     raeburn  11984:     if ($getuserdir) {
1.253     stredwic 11985:         $udom = $userdomain;
                   11986:         $uname = $username;
1.955     raeburn  11987:     } else {
                   11988:         (undef,$udom,$uname)=split(/\//,$uri);
                   11989:         if(defined($userdomain)) {
                   11990:             $udom = $userdomain;
                   11991:         }
                   11992:         if(defined($username)) {
                   11993:             $uname = $username;
                   11994:         }
1.253     stredwic 11995:     }
1.955     raeburn  11996:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 11997: 
1.955     raeburn  11998:     $dirRoot = $perlvar{'lonDocRoot'};
                   11999:     if (defined($getpropath)) {
                   12000:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 12001:         $dirRoot =~ s/\/$//;
1.955     raeburn  12002:     } elsif (defined($getuserdir)) {
                   12003:         my $subdir=$uname.'__';
                   12004:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   12005:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   12006:                    ."/$udom/$subdir/$uname";
                   12007:     } elsif (defined($alternateRoot)) {
                   12008:         $dirRoot = $alternateRoot;
1.751     banghart 12009:     }
1.253     stredwic 12010: 
                   12011:     if($udom) {
                   12012:         if($uname) {
1.1135    raeburn  12013:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  12014:             if ($uhome eq 'no_host') {
                   12015:                 return ([],'no_host');
                   12016:             }
1.955     raeburn  12017:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  12018:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  12019:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  12020:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  12021:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  12022:             } else {
                   12023:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12024:             }
1.605     matthew  12025:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  12026:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  12027:                 @listing_results = split(/:/,$listing);
                   12028:             } else {
                   12029:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12030:             }
1.1135    raeburn  12031:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  12032:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   12033:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   12034:                 return ([],$listing);
                   12035:             } else {
                   12036:                 return (\@listing_results);
1.1135    raeburn  12037:             }
1.955     raeburn  12038:         } elsif(!$alternateRoot) {
1.1136    raeburn  12039:             my (%allusers,%listerror);
1.841     albertel 12040: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  12041:  	    foreach my $tryserver (keys(%servers)) {
                   12042:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   12043:                                   &escape($udom),$tryserver);
                   12044:                 if ($listing eq 'unknown_cmd') {
                   12045: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   12046: 				      $udom, $tryserver);
                   12047:                 } else {
                   12048:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   12049:                 }
1.841     albertel 12050: 		if ($listing eq 'unknown_cmd') {
                   12051: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   12052: 				      $udom, $tryserver);
                   12053: 		    @listing_results = split(/:/,$listing);
                   12054: 		} else {
                   12055: 		    @listing_results =
                   12056: 			map { &unescape($_); } split(/:/,$listing);
                   12057: 		}
1.1136    raeburn  12058:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   12059:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   12060:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   12061:                     $listerror{$tryserver} = $listing;
                   12062:                 } else {
1.841     albertel 12063: 		    foreach my $line (@listing_results) {
                   12064: 			my ($entry) = split(/&/,$line,2);
                   12065: 			$allusers{$entry} = 1;
                   12066: 		    }
                   12067: 		}
1.253     stredwic 12068:             }
1.1136    raeburn  12069:             my @alluserslist=();
1.800     albertel 12070:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  12071:                 push(@alluserslist,$user.'&user');
1.253     stredwic 12072:             }
1.1318    droeschl 12073: 
                   12074:             if (!%listerror) {
                   12075:                 # no errors
                   12076:                 return (\@alluserslist);
                   12077:             } elsif (scalar(keys(%servers)) == 1) {
                   12078:                 # one library server, one error 
                   12079:                 my ($key) = keys(%listerror);
                   12080:                 return (\@alluserslist, $listerror{$key});
                   12081:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
                   12082:                 # con_lost indicates that we might miss data from at least one
                   12083:                 # library server
                   12084:                 return (\@alluserslist, 'con_lost');
                   12085:             } else {
                   12086:                 # multiple library servers and no con_lost -> data should be
                   12087:                 # complete. 
                   12088:                 return (\@alluserslist);
                   12089:             }
                   12090: 
1.253     stredwic 12091:         } else {
1.1136    raeburn  12092:             return ([],'missing username');
1.253     stredwic 12093:         }
1.955     raeburn  12094:     } elsif(!defined($getpropath)) {
1.1136    raeburn  12095:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   12096:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   12097:         return (\@all_domains);
1.955     raeburn  12098:     } else {
1.1136    raeburn  12099:         return ([],'missing domain');
1.275     stredwic 12100:     }
                   12101: }
                   12102: 
                   12103: # --------------------------------------------- GetFileTimestamp
                   12104: # This function utilizes dirlist and returns the date stamp for
                   12105: # when it was last modified.  It will also return an error of -1
                   12106: # if an error occurs
                   12107: 
                   12108: sub GetFileTimestamp {
1.955     raeburn  12109:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 12110:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   12111:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  12112:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   12113:                                     undef,$getuserdir);
                   12114:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   12115:         return -1;
                   12116:     }
                   12117:     if (ref($fileref) eq 'ARRAY') {
                   12118:         my @stats = split('&',$fileref->[0]);
1.375     matthew  12119:         # @stats contains first the filename, then the stat output
                   12120:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 12121:     } else {
                   12122:         return -1;
1.253     stredwic 12123:     }
1.26      www      12124: }
                   12125: 
1.712     albertel 12126: sub stat_file {
                   12127:     my ($uri) = @_;
1.787     albertel 12128:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 12129: 
1.955     raeburn  12130:     my ($udom,$uname,$file);
1.712     albertel 12131:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   12132: 	($udom,$uname,$file) =
1.811     albertel 12133: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 12134: 	$file = 'userfiles/'.$file;
                   12135:     }
                   12136:     if ($uri =~ m-^/res/-) {
                   12137: 	($udom,$uname) = 
1.807     albertel 12138: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 12139: 	$file = $uri;
                   12140:     }
                   12141: 
                   12142:     if (!$udom || !$uname || !$file) {
                   12143: 	# unable to handle the uri
                   12144: 	return ();
                   12145:     }
1.956     raeburn  12146:     my $getpropath;
                   12147:     if ($file =~ /^userfiles\//) {
                   12148:         $getpropath = 1;
                   12149:     }
1.1136    raeburn  12150:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   12151:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   12152:         return ();
                   12153:     } else {
                   12154:         if (ref($listref) eq 'ARRAY') {
                   12155:             my @stats = split('&',$listref->[0]);
                   12156: 	    shift(@stats); #filename is first
                   12157: 	    return @stats;
                   12158:         }
1.712     albertel 12159:     }
                   12160:     return ();
                   12161: }
                   12162: 
1.1313    raeburn  12163: # --------------------------------------------------------- recursedirs
                   12164: # Recursive function to traverse either a specific user's Authoring Space
                   12165: # or corresponding Published Resource Space, and populate the hash ref:
                   12166: # $dirhashref with URLs of all directories, and if $filehashref hash
                   12167: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
1.1503    raeburn  12168: # or .rights files in resource space, and .meta, .save, .log, .bak and
                   12169: # .rights files in Authoring Space.
1.1313    raeburn  12170: #
                   12171: # Inputs:
                   12172: #
                   12173: # $is_home - true if current server is home server for user's space
1.1503    raeburn  12174: # $recurse - if true will also traverse subdirectories recursively
                   12175: # $include - reference to hash containing allowed file extensions.  If provided,
                   12176: #             files which do not have a matching extension will be ignored.
                   12177: # $exclude - reference to hash containing excluded file extensions.  If provided,
                   12178: #             files which have a matching extension will be ignored.
                   12179: # $nonemptydir - if true, will only populate $fileshashref hash entry for a particular
                   12180: #             directory with first file found (with acceptable extension).
1.1505    raeburn  12181: # $addtopdir - if true, set $dirhashref->{'/'} = 1 
1.1313    raeburn  12182: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
                   12183: # $relpath - Current path (relative to top level).
                   12184: # $dirhashref - reference to hash to populate with URLs of directories (Required)
                   12185: # $filehashref - reference to hash to populate with URLs of files (Optional)
                   12186: #
                   12187: # Returns: nothing
                   12188: #
                   12189: # Side Effects: populates $dirhashref, and $filehashref (if provided).
                   12190: #
                   12191: # Currently used by interface/londocs.pm to create linked select boxes for
                   12192: # directory and filename to import a Course "Author" resource into a course, and
                   12193: # also to create linked select boxes for Authoring Space and Directory to choose
                   12194: # save location for creation of a new "standard" problem from the Course Editor.
                   12195: #
                   12196: 
                   12197: sub recursedirs {
1.1505    raeburn  12198:     my ($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$relpath,$dirhashref,$filehashref) = @_;
1.1313    raeburn  12199:     return unless (ref($dirhashref) eq 'HASH');
1.1503    raeburn  12200:     my $docroot = $perlvar{'lonDocRoot'};
1.1313    raeburn  12201:     my $currpath = $docroot.$toppath;
1.1503    raeburn  12202:     if ($relpath ne '') {
1.1313    raeburn  12203:         $currpath .= "/$relpath";
                   12204:     }
1.1503    raeburn  12205:     my ($savefile,$checkinc,$checkexc);
1.1313    raeburn  12206:     if (ref($filehashref)) {
                   12207:         $savefile = 1;
                   12208:     }
1.1503    raeburn  12209:     if (ref($include) eq 'HASH') {
                   12210:         $checkinc = 1;
                   12211:     }
                   12212:     if (ref($exclude) eq 'HASH') {
                   12213:         $checkexc = 1;
                   12214:     }
1.1313    raeburn  12215:     if ($is_home) {
1.1505    raeburn  12216:         if ((-e $currpath) && (opendir(my $dirh,$currpath))) {
1.1503    raeburn  12217:             my $filecount = 0;
1.1313    raeburn  12218:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
                   12219:                 next if ($item eq '');
                   12220:                 if (-d "$currpath/$item") {
                   12221:                     my $newpath;
1.1503    raeburn  12222:                     if ($relpath ne '') {
1.1313    raeburn  12223:                         $newpath = "$relpath/$item";
                   12224:                     } else {
                   12225:                         $newpath = $item;
                   12226:                     }
                   12227:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
1.1503    raeburn  12228:                     if ($recurse) {
1.1505    raeburn  12229:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
1.1503    raeburn  12230:                     }
                   12231:                 } elsif (($savefile) || ($relpath eq '')) {
                   12232:                     next if ($nonemptydir && $filecount);
                   12233:                     if ($checkinc || $checkexc) {
                   12234:                         my ($extension) = ($item =~ /\.(\w+)$/);
                   12235:                         if ($checkinc) {
                   12236:                             next unless ($extension && $include->{$extension});
                   12237:                         }
                   12238:                         if ($checkexc) {
                   12239:                             next if ($extension && $exclude->{$extension});
                   12240:                         }
                   12241:                     }
1.1505    raeburn  12242:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
1.1503    raeburn  12243:                         $dirhashref->{'/'} = 1;
                   12244:                     }
                   12245:                     if ($savefile) {
                   12246:                         if ($relpath eq '') {
                   12247:                             $filehashref->{'/'}{$item} = 1;
                   12248:                         } else {
1.1313    raeburn  12249:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   12250:                         }
                   12251:                     }
1.1503    raeburn  12252:                     $filecount ++;
1.1313    raeburn  12253:                 }
                   12254:             }
                   12255:             closedir($dirh);
                   12256:         }
                   12257:     } else {
                   12258:         my ($dirlistref,$listerror) =
                   12259:             &dirlist($toppath.$relpath);
                   12260:         my @dir_lines;
                   12261:         my $dirptr=16384;
                   12262:         if (ref($dirlistref) eq 'ARRAY') {
1.1503    raeburn  12263:             my $filecount = 0;
1.1313    raeburn  12264:             foreach my $dir_line (sort
                   12265:                               {
                   12266:                                   my ($afile)=split('&',$a,2);
                   12267:                                   my ($bfile)=split('&',$b,2);
                   12268:                                   return (lc($afile) cmp lc($bfile));
                   12269:                               } (@{$dirlistref})) {
                   12270:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
                   12271:                     split(/\&/,$dir_line,16);
                   12272:                 $item =~ s/\s+$//;
                   12273:                 next if (($item =~ /^\.\.?$/) || ($obs));
                   12274:                 if ($dirptr&$testdir) {
                   12275:                     my $newpath;
                   12276:                     if ($relpath) {
                   12277:                         $newpath = "$relpath/$item";
                   12278:                     } else {
                   12279:                         $newpath = $item;
                   12280:                     }
                   12281:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
1.1503    raeburn  12282:                     if ($recurse) {
1.1505    raeburn  12283:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
1.1503    raeburn  12284:                     }
                   12285:                 } elsif (($savefile) || ($relpath eq '')) {
                   12286:                     next if ($nonemptydir && $filecount);
                   12287:                     if ($checkinc || $checkexc) {
                   12288:                         my $extension;
                   12289:                         if ($checkinc) {
                   12290:                             next unless ($extension && $include->{$extension});
                   12291:                         }
                   12292:                         if ($checkexc) {
                   12293:                             next if ($extension && $exclude->{$extension});
                   12294:                         }
                   12295:                     }
                   12296:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
                   12297:                         $dirhashref->{'/'} = 1;
                   12298:                     }
                   12299:                     if ($savefile) {
                   12300:                         if ($relpath eq '') {
                   12301:                             $filehashref->{'/'}{$item} = 1;
                   12302:                         } else {
                   12303:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
1.1313    raeburn  12304:                         }
                   12305:                     }
1.1503    raeburn  12306:                     $filecount ++; 
1.1313    raeburn  12307:                 }
                   12308:             }
                   12309:         }
                   12310:     }
1.1505    raeburn  12311:     if ($addtopdir) {
                   12312:         if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
                   12313:             $dirhashref->{'/'} = 1;
                   12314:         }
                   12315:     }
1.1313    raeburn  12316:     return;
                   12317: }
                   12318: 
1.1503    raeburn  12319: sub priv_exclude {
                   12320:     return {
                   12321:              meta => 1,
                   12322:              save => 1,
                   12323:              log => 1,
                   12324:              bak => 1,
                   12325:              rights => 1,
                   12326:              DS_Store => 1,
                   12327:            };
                   12328: }
                   12329: 
1.26      www      12330: # -------------------------------------------------------- Value of a Condition
                   12331: 
1.713     albertel 12332: # gets the value of a specific preevaluated condition
                   12333: #    stored in the string  $env{user.state.<cid>}
                   12334: # or looks up a condition reference in the bighash and if if hasn't
                   12335: # already been evaluated recurses into docondval to get the value of
                   12336: # the condition, then memoizing it to 
                   12337: #   $env{user.state.<cid>.<condition>}
1.40      www      12338: sub directcondval {
                   12339:     my $number=shift;
1.620     albertel 12340:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 12341: 	&Apache::lonuserstate::evalstate();
                   12342:     }
1.713     albertel 12343:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   12344: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   12345:     } elsif ($number =~ /^_/) {
                   12346: 	my $sub_condition;
                   12347: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   12348: 		&GDBM_READER(),0640)) {
                   12349: 	    $sub_condition=$bighash{'conditions'.$number};
                   12350: 	    untie(%bighash);
                   12351: 	}
                   12352: 	my $value = &docondval($sub_condition);
1.949     raeburn  12353: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 12354: 	return $value;
                   12355:     }
1.620     albertel 12356:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   12357:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      12358:     } else {
                   12359:        return 2;
                   12360:     }
                   12361: }
                   12362: 
1.713     albertel 12363: # get the collection of conditions for this resource
1.26      www      12364: sub condval {
                   12365:     my $condidx=shift;
1.54      www      12366:     my $allpathcond='';
1.713     albertel 12367:     foreach my $cond (split(/\|/,$condidx)) {
                   12368: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   12369: 	    $allpathcond.=
                   12370: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   12371: 	}
1.191     harris41 12372:     }
1.54      www      12373:     $allpathcond=~s/\|$//;
1.713     albertel 12374:     return &docondval($allpathcond);
                   12375: }
                   12376: 
                   12377: #evaluates an expression of conditions
                   12378: sub docondval {
                   12379:     my ($allpathcond) = @_;
                   12380:     my $result=0;
                   12381:     if ($env{'request.course.id'}
                   12382: 	&& defined($allpathcond)) {
                   12383: 	my $operand='|';
                   12384: 	my @stack;
                   12385: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   12386: 	    if ($chunk eq '(') {
                   12387: 		push @stack,($operand,$result);
                   12388: 	    } elsif ($chunk eq ')') {
                   12389: 		my $before=pop @stack;
                   12390: 		if (pop @stack eq '&') {
                   12391: 		    $result=$result>$before?$before:$result;
                   12392: 		} else {
                   12393: 		    $result=$result>$before?$result:$before;
                   12394: 		}
                   12395: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   12396: 		$operand=$chunk;
                   12397: 	    } else {
                   12398: 		my $new=directcondval($chunk);
                   12399: 		if ($operand eq '&') {
                   12400: 		    $result=$result>$new?$new:$result;
                   12401: 		} else {
                   12402: 		    $result=$result>$new?$result:$new;
                   12403: 		}
                   12404: 	    }
                   12405: 	}
1.26      www      12406:     }
                   12407:     return $result;
1.421     albertel 12408: }
                   12409: 
                   12410: # ---------------------------------------------------- Devalidate courseresdata
                   12411: 
                   12412: sub devalidatecourseresdata {
                   12413:     my ($coursenum,$coursedomain)=@_;
                   12414:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 12415:     &devalidate_cache_new('courseres',$hashid);
1.28      www      12416: }
                   12417: 
1.763     www      12418: 
1.200     www      12419: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     12420: #
                   12421: #  Parameters:
                   12422: #      $coursenum    - Number of the course.
                   12423: #      $coursedomain - Domain at which the course was created.
                   12424: #  Returns:
                   12425: #     A hash of the course parameters along (I think) with timestamps
                   12426: #     and version info.
1.877     foxr     12427: 
1.624     albertel 12428: sub get_courseresdata {
                   12429:     my ($coursenum,$coursedomain)=@_;
1.200     www      12430:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   12431:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 12432:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 12433:     my %dumpreply;
1.417     albertel 12434:     unless (defined($cached)) {
1.624     albertel 12435: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 12436: 	$result=\%dumpreply;
1.251     albertel 12437: 	my ($tmp) = keys(%dumpreply);
                   12438: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 12439: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 12440: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   12441: 	    return $tmp;
1.416     albertel 12442: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 12443: 	    $result=undef;
1.599     albertel 12444: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 12445: 	}
                   12446:     }
1.624     albertel 12447:     return $result;
                   12448: }
                   12449: 
1.633     albertel 12450: sub devalidateuserresdata {
                   12451:     my ($uname,$udom)=@_;
                   12452:     my $hashid="$udom:$uname";
                   12453:     &devalidate_cache_new('userres',$hashid);
                   12454: }
                   12455: 
1.624     albertel 12456: sub get_userresdata {
                   12457:     my ($uname,$udom)=@_;
                   12458:     #most student don\'t have any data set, check if there is some data
                   12459:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   12460: 
                   12461:     my $hashid="$udom:$uname";
                   12462:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   12463:     if (!defined($cached)) {
                   12464: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   12465: 	$result=\%resourcedata;
                   12466: 	&do_cache_new('userres',$hashid,$result,600);
                   12467:     }
                   12468:     my ($tmp)=keys(%$result);
                   12469:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   12470: 	return $result;
                   12471:     }
                   12472:     #error 2 occurs when the .db doesn't exist
                   12473:     if ($tmp!~/error: 2 /) {
1.1294    raeburn  12474:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
                   12475: 	    &logthis("<font color=\"blue\">WARNING:".
                   12476: 		     " Trying to get resource data for ".
                   12477: 		     $uname." at ".$udom.": ".
                   12478: 		     $tmp."</font>");
                   12479:         }
1.624     albertel 12480:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 12481: 	#&EXT_cache_set($udom,$uname);
                   12482: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 12483: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 12484:     }
                   12485:     return $tmp;
                   12486: }
1.879     foxr     12487: #----------------------------------------------- resdata - return resource data
                   12488: #  Purpose:
                   12489: #    Return resource data for either users or for a course.
                   12490: #  Parameters:
                   12491: #     $name      - Course/user name.
                   12492: #     $domain    - Name of the domain the user/course is registered on.
1.1311    raeburn  12493: #     $type      - Type of thing $name is (must be 'course' or 'user')
1.1301    raeburn  12494: #     $mapp      - decluttered URL of enclosing map  
                   12495: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
                   12496: #     $recurseup - Ref to array of map URLs, starting with map containing
                   12497: #                  $mapp up through hierarchy of nested maps to top level map.  
                   12498: #     $courseid  - CourseID (first part of param identifier).
                   12499: #     $modifier  - Middle part of param identifier.
                   12500: #     $what      - Last part of param identifier.
1.879     foxr     12501: #     @which     - Array of names of resources desired.
                   12502: #  Returns:
                   12503: #     The value of the first reasource in @which that is found in the
                   12504: #     resource hash.
                   12505: #  Exceptional Conditions:
                   12506: #     If the $type passed in is not valid (not the string 'course' or 
                   12507: #     'user', an undefined  reference is returned.
                   12508: #     If none of the resources are found, an undef is returned
1.624     albertel 12509: sub resdata {
1.1301    raeburn  12510:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
                   12511:         $modifier,$what,@which)=@_;
1.624     albertel 12512:     my $result;
                   12513:     if ($type eq 'course') {
                   12514: 	$result=&get_courseresdata($name,$domain);
                   12515:     } elsif ($type eq 'user') {
                   12516: 	$result=&get_userresdata($name,$domain);
                   12517:     }
                   12518:     if (!ref($result)) { return $result; }    
1.251     albertel 12519:     foreach my $item (@which) {
1.1301    raeburn  12520:         if ($item->[1] eq 'course') {
                   12521:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
                   12522:                 unless ($$recursed) {
1.1302    raeburn  12523:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301    raeburn  12524:                     $$recursed = 1;
                   12525:                 }
                   12526:                 foreach my $item (@${recurseup}) {
                   12527:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
                   12528:                     last if (defined($result->{$norecursechk}));
                   12529:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
                   12530:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
                   12531:                 }
                   12532:             }
                   12533:         }
                   12534:         if (defined($result->{$item->[0]})) {
1.927     albertel 12535: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 12536: 	}
1.250     albertel 12537:     }
1.291     albertel 12538:     return undef;
1.200     www      12539: }
                   12540: 
1.1360    raeburn  12541: sub get_domain_lti {
                   12542:     my ($cdom,$context) = @_;
1.1483    raeburn  12543:     my ($name,$cachename,%lti);
1.1360    raeburn  12544:     if ($context eq 'consumer') {
                   12545:         $name = 'ltitools';
                   12546:     } elsif ($context eq 'provider') {
                   12547:         $name = 'lti';
1.1483    raeburn  12548:     } elsif ($context eq 'linkprot') {
                   12549:         $name = 'ltisec';
1.1360    raeburn  12550:     } else {
                   12551:         return %lti;
                   12552:     }
1.1483    raeburn  12553: 
                   12554:     if ($context eq 'linkprot') {
                   12555:         $cachename = $context;
                   12556:     } else {
                   12557:         $cachename = $name;
                   12558:     }
                   12559:     
                   12560:     my ($result,$cached)=&is_cached_new($cachename,$cdom);
1.1298    raeburn  12561:     if (defined($cached)) {
                   12562:         if (ref($result) eq 'HASH') {
1.1360    raeburn  12563:             %lti = %{$result};
1.1298    raeburn  12564:         }
                   12565:     } else {
1.1360    raeburn  12566:         my %domconfig = &get_dom('configuration',[$name],$cdom);
                   12567:         if (ref($domconfig{$name}) eq 'HASH') {
1.1483    raeburn  12568:             if ($context eq 'linkprot') {
                   12569:                 if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
                   12570:                     %lti = %{$domconfig{$name}{'linkprot'}};
                   12571:                 }
                   12572:             } else {
                   12573:                 %lti = %{$domconfig{$name}};
                   12574:             }
                   12575:             if (($context eq 'consumer') && (keys(%lti))) {
                   12576:                 my %encdomconfig = &get_dom('encconfig',[$name],$cdom,undef,1);
                   12577:                 if (ref($encdomconfig{$name}) eq 'HASH') {
                   12578:                     foreach my $id (keys(%lti)) {
                   12579:                         if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
                   12580:                             foreach my $item ('key','secret') {
                   12581:                                 $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
                   12582:                             }
1.1344    raeburn  12583:                         }
                   12584:                     }
                   12585:                 }
                   12586:             }
1.1298    raeburn  12587:         }
                   12588:         my $cachetime = 24*60*60;
1.1483    raeburn  12589:         &do_cache_new($cachename,$cdom,\%lti,$cachetime);
1.1298    raeburn  12590:     }
1.1360    raeburn  12591:     return %lti;
1.1298    raeburn  12592: }
                   12593: 
1.1463    raeburn  12594: sub get_course_lti {
1.1510  ! raeburn  12595:     my ($cnum,$cdom,$context) = @_;
        !          12596:     my ($name,$cachename,%lti);
        !          12597:     if ($context eq 'consumer') {
        !          12598:         $name = 'ltitools';
        !          12599:         $cachename = 'courseltitools';
        !          12600:     } elsif ($context eq 'provider') {
        !          12601:         $name = 'lti';
        !          12602:         $cachename = 'courselti';
        !          12603:     } else {
        !          12604:         return %lti;
        !          12605:     }
1.1463    raeburn  12606:     my $hashid=$cdom.'_'.$cnum;
1.1510  ! raeburn  12607:     my ($result,$cached)=&is_cached_new($cachename,$hashid);
1.1463    raeburn  12608:     if (defined($cached)) {
                   12609:         if (ref($result) eq 'HASH') {
1.1510  ! raeburn  12610:             %lti = %{$result};
1.1463    raeburn  12611:         }
                   12612:     } else {
1.1510  ! raeburn  12613:         %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
1.1463    raeburn  12614:         my $cachetime = 24*60*60;
1.1510  ! raeburn  12615:         &do_cache_new($cachename,$hashid,\%lti,$cachetime);
1.1463    raeburn  12616:     }
1.1510  ! raeburn  12617:     return %lti;
1.1463    raeburn  12618: }
                   12619: 
1.1479    raeburn  12620: sub courselti_itemid {
                   12621:     my ($cnum,$cdom,$url,$method,$params,$context) = @_;
                   12622:     my ($chome,$itemid);
                   12623:     $chome = &homeserver($cnum,$cdom);
                   12624:     return if ($chome eq 'no_host');
                   12625:     if (ref($params) eq 'HASH') {
                   12626:         my $rep;
                   12627:         if (grep { $_ eq $chome } current_machine_ids()) {
                   12628:             $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
                   12629:         } else {
                   12630:             my $escurl = &escape($url);
                   12631:             my $escmethod = &escape($method);
                   12632:             my $items = &freeze_escape($params);
                   12633:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
                   12634:         }
                   12635:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                   12636:                 ($rep eq 'unknown_cmd')) {
                   12637:             $itemid = $rep;
                   12638:         }
                   12639:     }
                   12640:     return $itemid;
                   12641: }
                   12642: 
                   12643: sub domainlti_itemid {
                   12644:     my ($cdom,$url,$method,$params,$context) = @_;
                   12645:     my ($primary_id,$itemid);
                   12646:     $primary_id = &domain($cdom,'primary');
                   12647:     return if ($primary_id eq '');
                   12648:     if (ref($params) eq 'HASH') {
                   12649:         my $rep;
                   12650:         if (grep { $_ eq $primary_id } current_machine_ids()) {
                   12651:             $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
                   12652:         } else {
                   12653:             my $cnum = '';
                   12654:             my $escurl = &escape($url);
                   12655:             my $escmethod = &escape($method);
                   12656:             my $items = &freeze_escape($params);
                   12657:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
                   12658:         }
                   12659:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                   12660:                 ($rep eq 'unknown_cmd')) {
                   12661:             $itemid = $rep;
                   12662:         }
                   12663:     }
                   12664:     return $itemid;
                   12665: }
                   12666: 
1.1496    raeburn  12667: sub count_supptools {
                   12668:     my ($cnum,$cdom,$ignorecache,$reload)=@_;
                   12669:     my $hashid=$cnum.':'.$cdom;
                   12670:     my ($numexttools,$cached);
                   12671:     unless ($ignorecache) {
                   12672:         ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
                   12673:     }
                   12674:     unless (defined($cached)) {
                   12675:         my $chome=&homeserver($cnum,$cdom);
                   12676:         $numexttools = 0;
                   12677:         unless ($chome eq 'no_host') {
1.1500    raeburn  12678:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
1.1496    raeburn  12679:             if (ref($supplemental) eq 'HASH') {
                   12680:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
                   12681:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
                   12682:                         if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
                   12683:                             $numexttools ++;
                   12684:                         }
                   12685:                     }
                   12686:                 }
                   12687:             }
                   12688:         }
                   12689:         &do_cache_new('supptools',$hashid,$numexttools,600);
                   12690:     }
                   12691:     return $numexttools;
                   12692: }
                   12693: 
                   12694: sub has_unhidden_suppfiles {
1.1498    raeburn  12695:     my ($cnum,$cdom,$ignorecache,$possdel)=@_;
1.1236    raeburn  12696:     my $hashid=$cnum.':'.$cdom;
1.1496    raeburn  12697:     my ($showsupp,$cached);
1.1236    raeburn  12698:     unless ($ignorecache) {
1.1496    raeburn  12699:         ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
1.1236    raeburn  12700:     }
                   12701:     unless (defined($cached)) {
                   12702:         my $chome=&homeserver($cnum,$cdom);
                   12703:         unless ($chome eq 'no_host') {
1.1500    raeburn  12704:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
1.1496    raeburn  12705:             if (ref($supplemental) eq 'HASH') {
                   12706:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
                   12707:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
                   12708:                         next if ($key =~ /\.sequence$/);
                   12709:                         if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
                   12710:                             foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
                   12711:                                 unless ($supplemental->{'hidden'}->{$id}) {
                   12712:                                     $showsupp = 1;
                   12713:                                     last;
                   12714:                                 }
                   12715:                             }
                   12716:                         }
                   12717:                         last if ($showsupp);
                   12718:                     }
                   12719:                 }
                   12720:             }
1.1236    raeburn  12721:         }
1.1496    raeburn  12722:         &do_cache_new('showsupp',$hashid,$showsupp,600);
1.1236    raeburn  12723:     }
1.1496    raeburn  12724:     return $showsupp;
                   12725: }
                   12726: 
1.379     matthew  12727: #
                   12728: # EXT resource caching routines
                   12729: #
                   12730: 
1.1302    raeburn  12731: {
                   12732: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
                   12733: #
                   12734: # The course for which we cache
                   12735: my $cachedmapkey='';
                   12736: # The cached recursive maps for this course
                   12737: my %cachedmaps=();
                   12738: # When this was last done
                   12739: my $cachedmaptime='';
                   12740: 
1.379     matthew  12741: sub clear_EXT_cache_status {
1.383     albertel 12742:     &delenv('cache.EXT.');
1.379     matthew  12743: }
                   12744: 
                   12745: sub EXT_cache_status {
                   12746:     my ($target_domain,$target_user) = @_;
1.383     albertel 12747:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 12748:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  12749:         # We know already the user has no data
                   12750:         return 1;
                   12751:     } else {
                   12752:         return 0;
                   12753:     }
                   12754: }
                   12755: 
                   12756: sub EXT_cache_set {
                   12757:     my ($target_domain,$target_user) = @_;
1.383     albertel 12758:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  12759:     #&appenv({$cachename => time});
1.379     matthew  12760: }
                   12761: 
1.28      www      12762: # --------------------------------------------------------- Value of a Variable
1.58      www      12763: sub EXT {
1.715     albertel 12764: 
1.1461    raeburn  12765:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
1.68      www      12766:     unless ($varname) { return ''; }
1.218     albertel 12767:     #get real user name/domain, courseid and symb
                   12768:     my $courseid;
1.359     albertel 12769:     my $publicuser;
1.427     www      12770:     if ($symbparm) {
                   12771: 	$symbparm=&get_symb_from_alias($symbparm);
                   12772:     }
1.218     albertel 12773:     if (!($uname && $udom)) {
1.790     albertel 12774:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 12775:       if (!$symbparm) {	$symbparm=$cursymb; }
                   12776:     } else {
1.620     albertel 12777: 	$courseid=$env{'request.course.id'};
1.218     albertel 12778:     }
1.48      www      12779:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   12780:     my $rest;
1.320     albertel 12781:     if (defined($therest[0])) {
1.48      www      12782:        $rest=join('.',@therest);
                   12783:     } else {
                   12784:        $rest='';
                   12785:     }
1.320     albertel 12786: 
1.57      www      12787:     my $qualifierrest=$qualifier;
                   12788:     if ($rest) { $qualifierrest.='.'.$rest; }
                   12789:     my $spacequalifierrest=$space;
                   12790:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      12791:     if ($realm eq 'user') {
1.48      www      12792: # --------------------------------------------------------------- user.resource
                   12793: 	if ($space eq 'resource') {
1.651     albertel 12794: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   12795: 		  || defined($Apache::lonhomework::parsing_a_task))
                   12796: 		 &&
1.1469    raeburn  12797: 		 ($symbparm eq &symbread()) ) {
1.744     albertel 12798: 		# if we are in the middle of processing the resource the
                   12799: 		# get the value we are planning on committing
                   12800:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   12801:                     return $Apache::lonhomework::results{$qualifierrest};
                   12802:                 } else {
                   12803:                     return $Apache::lonhomework::history{$qualifierrest};
                   12804:                 }
1.335     albertel 12805: 	    } else {
1.359     albertel 12806: 		my %restored;
1.620     albertel 12807: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 12808: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   12809: 		} else {
                   12810: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   12811: 		}
1.335     albertel 12812: 		return $restored{$qualifierrest};
                   12813: 	    }
1.48      www      12814: # ----------------------------------------------------------------- user.access
                   12815:         } elsif ($space eq 'access') {
1.218     albertel 12816: 	    # FIXME - not supporting calls for a specific user
1.48      www      12817:             return &allowed($qualifier,$rest);
                   12818: # ------------------------------------------ user.preferences, user.environment
                   12819:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 12820: 	    if (($uname eq $env{'user.name'}) &&
                   12821: 		($udom eq $env{'user.domain'})) {
                   12822: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 12823: 	    } else {
1.359     albertel 12824: 		my %returnhash;
                   12825: 		if (!$publicuser) {
                   12826: 		    %returnhash=&userenvironment($udom,$uname,
                   12827: 						 $qualifierrest);
                   12828: 		}
1.218     albertel 12829: 		return $returnhash{$qualifierrest};
                   12830: 	    }
1.48      www      12831: # ----------------------------------------------------------------- user.course
                   12832:         } elsif ($space eq 'course') {
1.218     albertel 12833: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 12834:             return $env{join('.',('request.course',$qualifier))};
1.48      www      12835: # ------------------------------------------------------------------- user.role
                   12836:         } elsif ($space eq 'role') {
1.218     albertel 12837: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 12838:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      12839:             if ($qualifier eq 'value') {
                   12840: 		return $role;
                   12841:             } elsif ($qualifier eq 'extent') {
                   12842:                 return $where;
                   12843:             }
                   12844: # ----------------------------------------------------------------- user.domain
                   12845:         } elsif ($space eq 'domain') {
1.218     albertel 12846:             return $udom;
1.48      www      12847: # ------------------------------------------------------------------- user.name
                   12848:         } elsif ($space eq 'name') {
1.218     albertel 12849:             return $uname;
1.48      www      12850: # ---------------------------------------------------- Any other user namespace
1.29      www      12851:         } else {
1.359     albertel 12852: 	    my %reply;
                   12853: 	    if (!$publicuser) {
                   12854: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   12855: 	    }
                   12856: 	    return $reply{$qualifierrest};
1.48      www      12857:         }
1.236     www      12858:     } elsif ($realm eq 'query') {
                   12859: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 12860:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   12861: 						[$spacequalifierrest]);
1.620     albertel 12862: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      12863:    } elsif ($realm eq 'request') {
1.48      www      12864: # ------------------------------------------------------------- request.browser
                   12865:         if ($space eq 'browser') {
1.1145    bisitz   12866:             return $env{'browser.'.$qualifier};
1.57      www      12867: # ------------------------------------------------------------ request.filename
                   12868:         } else {
1.620     albertel 12869:             return $env{'request.'.$spacequalifierrest};
1.29      www      12870:         }
1.28      www      12871:     } elsif ($realm eq 'course') {
1.48      www      12872: # ---------------------------------------------------------- course.description
1.620     albertel 12873:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      12874:     } elsif ($realm eq 'resource') {
1.165     www      12875: 
1.620     albertel 12876: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 12877: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   12878: 	}
1.693     albertel 12879: 
1.1232    raeburn  12880:         if ($qualifier eq '') {
                   12881: 	    if ($space eq 'title') {
                   12882: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   12883: 	        return &gettitle($symbparm);
                   12884: 	    }
1.693     albertel 12885: 	
1.1232    raeburn  12886: 	    if ($space eq 'map') {
                   12887: 	        my ($map) = &decode_symb($symbparm);
                   12888: 	        return &symbread($map);
                   12889: 	    }
                   12890:             if ($space eq 'maptitle') {
                   12891:                 my ($map) = &decode_symb($symbparm);
                   12892:                 return &gettitle($map);
                   12893:             }
                   12894: 	    if ($space eq 'filename') {
                   12895: 	        if ($symbparm) {
                   12896: 		    return &clutter((&decode_symb($symbparm))[2]);
                   12897: 	        }
                   12898: 	        return &hreflocation('',$env{'request.filename'});
1.905     albertel 12899: 	    }
1.1232    raeburn  12900: 
1.1233    raeburn  12901:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
                   12902:                 if ($space eq 'visibleparts') {
                   12903:                     my $navmap = Apache::lonnavmaps::navmap->new();
                   12904:                     my $item;
                   12905:                     if (ref($navmap)) {
                   12906:                         my $res = $navmap->getBySymb($symbparm);
                   12907:                         my $parts = $res->parts();
                   12908:                         if (ref($parts) eq 'ARRAY') {
                   12909:                             $item = join(',',@{$parts});
                   12910:                         }
                   12911:                         undef($navmap);
1.1232    raeburn  12912:                     }
1.1233    raeburn  12913:                     return $item;
1.1232    raeburn  12914:                 }
                   12915:             }
                   12916:         }
1.693     albertel 12917: 
1.1301    raeburn  12918: 	my ($section, $group, @groups, @recurseup, $recursed);
1.1461    raeburn  12919:         if (ref($recurseupref) eq 'ARRAY') {
                   12920:             @recurseup = @{$recurseupref};
                   12921:             $recursed = 1;
                   12922:         }
1.1301    raeburn  12923: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228    raeburn  12924:         if (($courseid eq '') && ($cid)) {
                   12925:             $courseid = $cid;
                   12926:         }
                   12927: 	if (($symbparm && $courseid) && 
                   12928: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      12929: 
1.218     albertel 12930: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      12931: 
1.60      www      12932: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 12933: 	    my $symbp=$symbparm;
1.1301    raeburn  12934: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 12935: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301    raeburn  12936:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218     albertel 12937: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620     albertel 12938: 	    if (($env{'user.name'} eq $uname) &&
                   12939: 		($env{'user.domain'} eq $udom)) {
                   12940: 		$section=$env{'request.course.sec'};
1.733     raeburn  12941:                 @groups = split(/:/,$env{'request.course.groups'});  
                   12942:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 12943: 	    } else {
1.539     albertel 12944: 		if (! defined($usection)) {
1.551     albertel 12945: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 12946: 		} else {
                   12947: 		    $section = $usection;
                   12948: 		}
1.733     raeburn  12949:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 12950: 	    }
                   12951: 
                   12952: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   12953: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301    raeburn  12954:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218     albertel 12955: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   12956: 
1.593     albertel 12957: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 12958: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.1301    raeburn  12959:             $courseleveli=$courseid.'.'.$recurseparm;
1.593     albertel 12960: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      12961: 
1.60      www      12962: # ----------------------------------------------------------- first, check user
1.624     albertel 12963: 
1.1301    raeburn  12964: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
                   12965:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
1.927     albertel 12966: 				       ([$courselevelr,'resource'],
                   12967: 					[$courselevelm,'map'     ],
1.1301    raeburn  12968:                                         [$courseleveli,'map'     ],
1.927     albertel 12969: 					[$courselevel, 'course'  ]));
1.931     albertel 12970: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      12971: 
1.594     albertel 12972: # ------------------------------------------------ second, check some of course
1.684     raeburn  12973:             my $coursereply;
1.691     raeburn  12974:             if (@groups > 0) {
                   12975:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301    raeburn  12976:                                        $recurseparm,$mapparm,$spacequalifierrest,
                   12977:                                        $mapp,\$recursed,\@recurseup);
                   12978:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
1.684     raeburn  12979:             }
1.96      www      12980: 
1.684     raeburn  12981: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 12982: 				  $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  12983: 				  'course',$mapp,\$recursed,\@recurseup,
                   12984:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
1.927     albertel 12985: 				  ([$seclevelr,   'resource'],
                   12986: 				   [$seclevelm,   'map'     ],
1.1301    raeburn  12987:                                    [$secleveli,   'map'     ],
1.927     albertel 12988: 				   [$seclevel,    'course'  ],
                   12989: 				   [$courselevelr,'resource']));
                   12990: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      12991: 
1.60      www      12992: # ------------------------------------------------------ third, check map parms
1.218     albertel 12993: 	    my %parmhash=();
                   12994: 	    my $thisparm='';
                   12995: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 12996: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 12997: 		    &GDBM_READER(),0640)) {
1.218     albertel 12998: 		$thisparm=$parmhash{$symbparm};
                   12999: 		untie(%parmhash);
                   13000: 	    }
1.927     albertel 13001: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 13002: 	}
1.594     albertel 13003: # ------------------------------------------ fourth, look in resource metadata
1.1301    raeburn  13004:  
                   13005:         my $what = $spacequalifierrest;
                   13006: 	$what=~s/\./\_/;
1.282     albertel 13007: 	my $filename;
                   13008: 	if (!$symbparm) { $symbparm=&symbread(); }
                   13009: 	if ($symbparm) {
1.409     www      13010: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 13011: 	} else {
1.620     albertel 13012: 	    $filename=$env{'request.filename'};
1.282     albertel 13013: 	}
1.1362    raeburn  13014:         my $toolsymb;
                   13015:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
                   13016:             $toolsymb = $symbparm;
                   13017:         }
                   13018: 	my $metadata=&metadata($filename,$what,$toolsymb);
1.927     albertel 13019: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1362    raeburn  13020: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927     albertel 13021: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      13022: 
1.1301    raeburn  13023: # ----------------------------------------------- fifth, look in rest of course
1.593     albertel 13024: 	if ($symbparm && defined($courseid) && 
1.620     albertel 13025: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 13026: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   13027: 				     $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  13028: 				     'course',$mapp,\$recursed,\@recurseup,
                   13029:                                      $courseid,'.',$spacequalifierrest,
1.927     albertel 13030: 				     ([$courselevelm,'map'   ],
1.1301    raeburn  13031:                                       [$courseleveli,'map'   ],
1.927     albertel 13032: 				      [$courselevel, 'course']));
                   13033: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 13034: 	}
1.145     www      13035: # ------------------------------------------------------------------ Cascade up
1.218     albertel 13036: 	unless ($space eq '0') {
1.336     albertel 13037: 	    my @parts=split(/_/,$space);
                   13038: 	    my $id=pop(@parts);
                   13039: 	    my $part=join('_',@parts);
                   13040: 	    if ($part eq '') { $part='0'; }
1.927     albertel 13041: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 13042: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  13043: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 13044: 	}
1.395     albertel 13045: 	if ($recurse) { return undef; }
1.1362    raeburn  13046: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927     albertel 13047: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      13048: # ---------------------------------------------------- Any other user namespace
                   13049:     } elsif ($realm eq 'environment') {
                   13050: # ----------------------------------------------------------------- environment
1.620     albertel 13051: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   13052: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 13053: 	} else {
1.770     albertel 13054: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   13055: 		return '';
                   13056: 	    }
1.219     albertel 13057: 	    my %returnhash=&userenvironment($udom,$uname,
                   13058: 					    $spacequalifierrest);
                   13059: 	    return $returnhash{$spacequalifierrest};
                   13060: 	}
1.28      www      13061:     } elsif ($realm eq 'system') {
1.48      www      13062: # ----------------------------------------------------------------- system.time
                   13063: 	if ($space eq 'time') {
                   13064: 	    return time;
                   13065:         }
1.696     albertel 13066:     } elsif ($realm eq 'server') {
                   13067: # ----------------------------------------------------------------- system.time
                   13068: 	if ($space eq 'name') {
                   13069: 	    return $ENV{'SERVER_NAME'};
                   13070:         }
1.1415    raeburn  13071:     } elsif ($realm eq 'client') {
                   13072:         if ($space eq 'remote_addr') {
1.1441    raeburn  13073:             return &get_requestor_ip();
1.1415    raeburn  13074:         }
1.28      www      13075:     }
1.48      www      13076:     return '';
1.61      www      13077: }
                   13078: 
1.927     albertel 13079: sub get_reply {
                   13080:     my ($reply_value) = @_;
1.940     raeburn  13081:     if (ref($reply_value) eq 'ARRAY') {
                   13082:         if (wantarray) {
                   13083: 	    return @$reply_value;
                   13084:         }
                   13085:         return $reply_value->[0];
                   13086:     } else {
                   13087:         return $reply_value;
1.927     albertel 13088:     }
                   13089: }
                   13090: 
1.691     raeburn  13091: sub check_group_parms {
1.1301    raeburn  13092:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
                   13093:         $recursed,$recurseupref) = @_;
                   13094:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
                   13095:                   [$what,'course']);
                   13096:     my $coursereply;
1.691     raeburn  13097:     foreach my $group (@{$groups}) {
1.1301    raeburn  13098:         my @groupitems = ();
1.691     raeburn  13099:         foreach my $level (@levels) {
1.927     albertel 13100:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   13101:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  13102:         }
1.1301    raeburn  13103:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   13104:                                    $env{'course.'.$courseid.'.domain'},
                   13105:                                    'course',$mapp,$recursed,$recurseupref,
                   13106:                                    $courseid,'.['.$group.'].',$what,
                   13107:                                    @groupitems);
                   13108:         last if (defined($coursereply));
1.691     raeburn  13109:     }
                   13110:     return $coursereply;
                   13111: }
                   13112: 
1.1301    raeburn  13113: sub get_map_hierarchy {
1.1302    raeburn  13114:     my ($mapname,$courseid) = @_;
                   13115:     my @recurseup = ();
1.1301    raeburn  13116:     if ($mapname) {
1.1302    raeburn  13117:         if (($cachedmapkey eq $courseid) &&
                   13118:             (abs($cachedmaptime-time)<5)) {
                   13119:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
                   13120:                 return @{$cachedmaps{$mapname}};
                   13121:             }
                   13122:         }
1.1301    raeburn  13123:         my $navmap = Apache::lonnavmaps::navmap->new();
                   13124:         if (ref($navmap)) {
                   13125:             @recurseup = $navmap->recurseup_maps($mapname);
                   13126:             undef($navmap);
1.1302    raeburn  13127:             $cachedmaps{$mapname} = \@recurseup;
                   13128:             $cachedmaptime=time;
                   13129:             $cachedmapkey=$courseid;
1.1301    raeburn  13130:         }
                   13131:     }
                   13132:     return @recurseup;
                   13133: }
                   13134: 
1.1302    raeburn  13135: }
                   13136: 
1.691     raeburn  13137: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  13138:     my ($courseid,@groups) = @_;
                   13139:     @groups = sort(@groups);
1.691     raeburn  13140:     return @groups;
                   13141: }
                   13142: 
1.395     albertel 13143: sub packages_tab_default {
1.1362    raeburn  13144:     my ($uri,$varname,$toolsymb)=@_;
1.395     albertel 13145:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 13146: 
                   13147:     my (@extension,@specifics,$do_default);
1.1362    raeburn  13148:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395     albertel 13149: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 13150: 	if ($pack_type eq 'default') {
                   13151: 	    $do_default=1;
                   13152: 	} elsif ($pack_type eq 'extension') {
                   13153: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 13154: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 13155: 	    # only look at packages defaults for packages that this id is
1.738     albertel 13156: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   13157: 	}
                   13158:     }
                   13159:     # first look for a package that matches the requested part id
                   13160:     foreach my $package (@specifics) {
                   13161: 	my (undef,$pack_type,$pack_part)=@{$package};
                   13162: 	next if ($pack_part ne $part);
                   13163: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13164: 	    return $packagetab{"$pack_type&$name&default"};
                   13165: 	}
                   13166:     }
                   13167:     # look for any possible matching non extension_ package
                   13168:     foreach my $package (@specifics) {
                   13169: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 13170: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13171: 	    return $packagetab{"$pack_type&$name&default"};
                   13172: 	}
1.585     albertel 13173: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 13174: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   13175: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 13176: 	}
                   13177:     }
1.738     albertel 13178:     # look for any posible extension_ match
                   13179:     foreach my $package (@extension) {
                   13180: 	my ($package,$pack_type)=@{$package};
                   13181: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   13182: 	    return $packagetab{"$pack_type&$name&default"};
                   13183: 	}
                   13184: 	if (defined($packagetab{$package."&$name&default"})) {
                   13185: 	    return $packagetab{$package."&$name&default"};
                   13186: 	}
                   13187:     }
                   13188:     # look for a global default setting
                   13189:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   13190: 	return $packagetab{"default&$name&default"};
                   13191:     }
1.395     albertel 13192:     return undef;
                   13193: }
                   13194: 
1.334     albertel 13195: sub add_prefix_and_part {
                   13196:     my ($prefix,$part)=@_;
                   13197:     my $keyroot;
                   13198:     if (defined($prefix) && $prefix !~ /^__/) {
                   13199: 	# prefix that has a part already
                   13200: 	$keyroot=$prefix;
                   13201:     } elsif (defined($prefix)) {
                   13202: 	# prefix that is missing a part
                   13203: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   13204:     } else {
                   13205: 	# no prefix at all
                   13206: 	if (defined($part)) { $keyroot='_'.$part; }
                   13207:     }
                   13208:     return $keyroot;
                   13209: }
                   13210: 
1.71      www      13211: # ---------------------------------------------------------------- Get metadata
                   13212: 
1.599     albertel 13213: my %metaentry;
1.1070    www      13214: my %importedpartids;
1.1369    raeburn  13215: my %importedrespids;
1.71      www      13216: sub metadata {
1.1362    raeburn  13217:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71      www      13218:     $uri=&declutter($uri);
1.288     albertel 13219:     # if it is a non metadata possible uri return quickly
1.529     albertel 13220:     if (($uri eq '') || 
                   13221: 	(($uri =~ m|^/*adm/|) && 
1.1343    raeburn  13222: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108    raeburn  13223:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 13224: 	return undef;
                   13225:     }
1.1261    raeburn  13226:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
1.924     albertel 13227: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 13228: 	return undef;
1.288     albertel 13229:     }
1.73      www      13230:     my $filename=$uri;
                   13231:     $uri=~s/\.meta$//;
1.172     www      13232: #
                   13233: # Is the metadata already cached?
1.177     www      13234: # Look at timestamp of caching
1.172     www      13235: # Everything is cached by the main uri, libraries are never directly cached
                   13236: #
1.428     albertel 13237:     if (!defined($liburi)) {
1.599     albertel 13238: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 13239: 	if (defined($cached)) { return $result->{':'.$what}; }
                   13240:     }
1.1362    raeburn  13241: 
                   13242: #
                   13243: # If the uri is for an external tool the file from
                   13244: # which metadata should be retrieved depends on whether
                   13245: # the tool had been configured to be gradable (set in the Course
                   13246: # Editor or Resource Editor).
                   13247: #
                   13248: # If a valid symb has been included as the third arg in the call
                   13249: # to &metadata() that can be used to retrieve the value of
                   13250: # parameter_0_gradable set for the resource, and included in the
                   13251: # uploaded map containing the tool. The value is retrieved via
                   13252: # &EXT(), if a valid symb is available.  Otherwise the value of
                   13253: # gradable in the exttool_$marker.db file for the tool instance
1.1364    raeburn  13254: # is retrieved via &get().
                   13255: #
                   13256: # When lonuserstate::traceroute() calls lonnet::EXT() for 
                   13257: # hiddenresource and encrypturl (during course initialization)
                   13258: # the map-level parameter for resource.0.gradable included in the 
                   13259: # uploaded map containing the tool will not yet have been stored
                   13260: # in the user_course_parms.db file for the user's session, so in 
                   13261: # this case fall back to retrieving gradable status from the
                   13262: # exttool_$marker.db file.
1.1362    raeburn  13263: #
                   13264: # In order to avoid an infinite loop, &metadata() will return
                   13265: # before a call to &EXT(), if the uri is for an external tool
                   13266: # and the $what for which metadata is being requested is
                   13267: # parameter_0_gradable or 0_gradable.
                   13268: #
                   13269: 
                   13270:     if ($uri =~ /ext\.tool$/) {
                   13271:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
                   13272:             return;
                   13273:         } else {
                   13274:             my ($checked,$use_passback);
                   13275:             if ($toolsymb ne '') {
                   13276:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
1.1364    raeburn  13277:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
1.1362    raeburn  13278:                     $checked = 1;
                   13279:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
                   13280:                         $use_passback = 1;
                   13281:                     }
                   13282:                 }
                   13283:             }
                   13284:             unless ($checked) {
                   13285:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
                   13286:                 $marker=~s/\D//g;
1.1363    raeburn  13287:                 if ($marker) {
1.1362    raeburn  13288:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
                   13289:                     $use_passback = $toolsettings{'gradable'};
                   13290:                 }
                   13291:             }
                   13292:             if ($use_passback) {
                   13293:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
                   13294:             } else {
                   13295:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
                   13296:             }
                   13297:         }
                   13298:     }
                   13299: 
1.428     albertel 13300:     {
1.1069    www      13301: # Imported parts would go here
1.1369    raeburn  13302:         my @origfiletagids=();
1.1069    www      13303:         my $importedparts=0;
1.1369    raeburn  13304: 
                   13305: # Imported responseids would go here
                   13306:         my $importedresponses=0;
1.172     www      13307: #
                   13308: # Is this a recursive call for a library?
                   13309: #
1.599     albertel 13310: #	if (! exists($metacache{$uri})) {
                   13311: #	    $metacache{$uri}={};
                   13312: #	}
1.924     albertel 13313: 	my $cachetime = 60*60;
1.171     www      13314:         if ($liburi) {
                   13315: 	    $liburi=&declutter($liburi);
                   13316:             $filename=$liburi;
1.401     bowersj2 13317:         } else {
1.599     albertel 13318: 	    &devalidate_cache_new('meta',$uri);
                   13319: 	    undef(%metaentry);
1.401     bowersj2 13320: 	}
1.140     www      13321:         my %metathesekeys=();
1.73      www      13322:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 13323: 	my $metastring;
1.1140    www      13324: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 13325: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 13326: 	    $metastring = 
1.929     albertel 13327: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 13328: 					  ('grade_target' => 'meta'));
                   13329: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  13330: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   13331:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 13332: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 13333: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 13334: 	    $metastring=&getfile($file);
1.489     albertel 13335: 	}
1.208     albertel 13336:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      13337:         my $token;
1.140     www      13338:         undef %metathesekeys;
1.71      www      13339:         while ($token=$parser->get_token) {
1.339     albertel 13340: 	    if ($token->[0] eq 'S') {
                   13341: 		if (defined($token->[2]->{'package'})) {
1.172     www      13342: #
                   13343: # This is a package - get package info
                   13344: #
1.339     albertel 13345: 		    my $package=$token->[2]->{'package'};
                   13346: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13347: 		    if (defined($token->[2]->{'id'})) { 
                   13348: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   13349: 		    }
1.599     albertel 13350: 		    if ($metaentry{':packages'}) {
                   13351: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 13352: 		    } else {
1.599     albertel 13353: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 13354: 		    }
1.736     albertel 13355: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 13356: 			my $part=$keyroot;
                   13357: 			$part=~s/^\_//;
1.736     albertel 13358: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   13359: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   13360: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 13361: 			    # ignore package.tab specified default values
                   13362:                             # here &package_tab_default() will fetch those
                   13363: 			    if ($subp eq 'default') { next; }
1.736     albertel 13364: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 13365: 			    my $unikey;
                   13366: 			    if ($pack =~ /_0$/) {
                   13367: 				$unikey='parameter_0_'.$name;
                   13368: 				$part=0;
                   13369: 			    } else {
                   13370: 				$unikey='parameter'.$keyroot.'_'.$name;
                   13371: 			    }
1.339     albertel 13372: 			    if ($subp eq 'display') {
                   13373: 				$value.=' [Part: '.$part.']';
                   13374: 			    }
1.599     albertel 13375: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 13376: 			    $metathesekeys{$unikey}=1;
1.599     albertel 13377: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   13378: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 13379: 			    }
1.599     albertel 13380: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   13381: 				$metaentry{':'.$unikey}=
                   13382: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 13383: 			    }
1.339     albertel 13384: 			}
                   13385: 		    }
                   13386: 		} else {
1.172     www      13387: #
                   13388: # This is not a package - some other kind of start tag
1.339     albertel 13389: #
                   13390: 		    my $entry=$token->[1];
1.1068    www      13391: 		    my $unikey='';
1.175     www      13392: 
1.339     albertel 13393: 		    if ($entry eq 'import') {
1.175     www      13394: #
                   13395: # Importing a library here
1.339     albertel 13396: #
1.1067    www      13397:                         my $location=$parser->get_text('/import');
                   13398:                         my $dir=$filename;
                   13399:                         $dir=~s|[^/]*$||;
                   13400:                         $location=&filelocation($dir,$location);
1.1369    raeburn  13401: 
                   13402:                         my $importid=$token->[2]->{'id'};
1.1068    www      13403:                         my $importmode=$token->[2]->{'importmode'};
1.1369    raeburn  13404: #
                   13405: # Check metadata for imported file to
                   13406: # see if it contained response items
                   13407: #
1.1372    raeburn  13408:                         my ($origfile,@libfilekeys);
1.1370    raeburn  13409:                         my %currmetaentry = %metaentry;
1.1372    raeburn  13410:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
                   13411:                                                            $depthcount+1));
                   13412:                         if (grep(/^responseorder$/,@libfilekeys)) {
                   13413:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
                   13414:                                                              undef,$depthcount+1);
                   13415:                             if ($libresponseorder ne '') {
                   13416:                                 if ($#origfiletagids<0) {
                   13417:                                     undef(%importedrespids);
                   13418:                                     undef(%importedpartids);
                   13419:                                 }
1.1373    raeburn  13420:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
                   13421:                                 if (@respids) {
                   13422:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
                   13423:                                 }
                   13424:                                 if ($importedrespids{$importid} ne '') {
1.1372    raeburn  13425:                                     $importedresponses = 1;
1.1369    raeburn  13426: # We need to get the original file and the imported file to get the response order correct
                   13427: # Load and inspect original file
1.1372    raeburn  13428:                                     if ($#origfiletagids<0) {
                   13429:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   13430:                                         $origfile=&getfile($origfilelocation);
                   13431:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13432:                                     }
1.1369    raeburn  13433:                                 }
                   13434:                             }
                   13435:                         }
1.1370    raeburn  13436: # Do not overwrite contents of %metaentry hash for resource itself with 
                   13437: # hash populated for imported library file
                   13438:                         %metaentry = %currmetaentry;
                   13439:                         undef(%currmetaentry);
1.1374    raeburn  13440:                         if ($importmode eq 'part') {
1.1068    www      13441: # Import as part(s)
1.1069    www      13442:                            $importedparts=1;
                   13443: # We need to get the original file and the imported file to get the part order correct
                   13444: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1369    raeburn  13445: # Load and inspect original file if we didn't do that already
                   13446:                            if ($#origfiletagids<0) {
                   13447:                                undef(%importedrespids);
                   13448:                                undef(%importedpartids);
                   13449:                                if ($origfile eq '') {
                   13450:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   13451:                                    $origfile=&getfile($origfilelocation);
                   13452:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13453:                                }
1.1070    www      13454:                            }
1.1372    raeburn  13455:                            my @impfilepartids;
                   13456: # If <partorder> tag is included in metadata for the imported file
                   13457: # get the parts in the imported file from that.
                   13458:                            if (grep(/^partorder$/,@libfilekeys)) {
                   13459:                                %currmetaentry = %metaentry;
                   13460:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   13461:                                                             $depthcount+1);
                   13462:                                %metaentry = %currmetaentry;
                   13463:                                undef(%currmetaentry);
                   13464:                                if ($libpartorder ne '') {
                   13465:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
                   13466:                                }
                   13467:                            } else {
                   13468: # If no <partorder> tag available, load and inspect imported file
                   13469:                                my $impfile=&getfile($location);
                   13470:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   13471:                            }
1.1069    www      13472:                            if ($#impfilepartids>=0) {
                   13473: # This problem had parts
1.1070    www      13474:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      13475:                            } else {
                   13476: # Importing by turning a single problem into a problem part
                   13477: # It gets the import-tags ID as part-ID
                   13478:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      13479:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      13480:                            }
1.1068    www      13481:                         } else {
1.1374    raeburn  13482: # Import as problem or as normal import
                   13483:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13484:                             unless ($importmode eq 'problem') {
1.1068    www      13485: # Normal import
1.1374    raeburn  13486:                                 if (defined($token->[2]->{'id'})) {
                   13487:                                     $unikey.='_'.$token->[2]->{'id'};
                   13488:                                 }
                   13489:                             }
                   13490: # Check metadata for imported file to
                   13491: # see if it contained parts
                   13492:                             if (grep(/^partorder$/,@libfilekeys)) {
                   13493:                                 %currmetaentry = %metaentry;
                   13494:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   13495:                                                              $depthcount+1);
                   13496:                                 %metaentry = %currmetaentry;
                   13497:                                 undef(%currmetaentry);
                   13498:                                 if ($libpartorder ne '') {
                   13499:                                     $importedparts = 1;
                   13500:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
                   13501:                                 }
                   13502:                             }
1.1067    www      13503:                         }
1.339     albertel 13504: 			if ($depthcount<20) {
1.736     albertel 13505: 			    my $metadata = 
1.1362    raeburn  13506: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736     albertel 13507: 					  $depthcount+1);
                   13508: 			    foreach my $meta (split(',',$metadata)) {
                   13509: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   13510: 				$metathesekeys{$meta}=1;
1.339     albertel 13511: 			    }
1.1068    www      13512:                         }
1.1067    www      13513: 		    } else {
                   13514: #
                   13515: # Not importing, some other kind of non-package, non-library start tag
                   13516: # 
                   13517:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   13518:                         if (defined($token->[2]->{'id'})) {
                   13519:                             $unikey.='_'.$token->[2]->{'id'};
                   13520:                         }
1.339     albertel 13521: 			if (defined($token->[2]->{'name'})) { 
                   13522: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   13523: 			}
                   13524: 			$metathesekeys{$unikey}=1;
1.736     albertel 13525: 			foreach my $param (@{$token->[3]}) {
                   13526: 			    $metaentry{':'.$unikey.'.'.$param} =
                   13527: 				$token->[2]->{$param};
1.339     albertel 13528: 			}
                   13529: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 13530: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 13531: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   13532: 		 # only ws inside the tag, and not in default, so use default
                   13533: 		 # as value
1.599     albertel 13534: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 13535: 			} elsif ( $internaltext =~ /\S/ ) {
                   13536: 		  # something interesting inside the tag
                   13537: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 13538: 			} else {
1.908     albertel 13539: 		  # no interesting values, don't set a default
1.339     albertel 13540: 			}
1.172     www      13541: # end of not-a-package not-a-library import
1.339     albertel 13542: 		    }
1.172     www      13543: # end of not-a-package start tag
1.339     albertel 13544: 		}
1.172     www      13545: # the next is the end of "start tag"
1.339     albertel 13546: 	    }
                   13547: 	}
1.483     albertel 13548: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 13549: 	$extension = lc($extension);
                   13550: 	if ($extension eq 'htm') { $extension='html'; }
                   13551: 
1.737     albertel 13552: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 13553: 	    #no specific packages #how's our extension
                   13554: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 13555: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 13556: 					 \%metathesekeys);
                   13557: 	}
1.883     albertel 13558: 
                   13559: 	if (!exists($metaentry{':packages'})
                   13560: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 13561: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 13562: 		#no specific packages well let's get default then
                   13563: 		if ($key!~/^default&/) { next; }
1.488     albertel 13564: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 13565: 					     \%metathesekeys);
                   13566: 	    }
                   13567: 	}
1.338     www      13568: # are there custom rights to evaluate
1.599     albertel 13569: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 13570: 
1.338     www      13571:     #
                   13572:     # Importing a rights file here
1.339     albertel 13573:     #
                   13574: 	    unless ($depthcount) {
1.599     albertel 13575: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 13576: 		my $dir=$filename;
                   13577: 		$dir=~s|[^/]*$||;
                   13578: 		$location=&filelocation($dir,$location);
1.736     albertel 13579: 		my $rights_metadata =
1.1362    raeburn  13580: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736     albertel 13581: 			      $depthcount+1);
                   13582: 		foreach my $rights (split(',',$rights_metadata)) {
                   13583: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   13584: 		    $metathesekeys{$rights}=1;
1.339     albertel 13585: 		}
                   13586: 	    }
                   13587: 	}
1.737     albertel 13588: 	# uniqifiy package listing
                   13589: 	my %seen;
                   13590: 	my @uniq_packages =
                   13591: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   13592: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   13593: 
1.1369    raeburn  13594:         if (($importedresponses) || ($importedparts)) {
                   13595:             if ($importedparts) {
1.1070    www      13596: # We had imported parts and need to rebuild partorder
1.1369    raeburn  13597:                 $metaentry{':partorder'}='';
                   13598:                 $metathesekeys{'partorder'}=1;
                   13599:             }
                   13600:             if ($importedresponses) {
                   13601: # We had imported responses and need to rebuil responseorder
                   13602:                 $metaentry{':responseorder'}='';
                   13603:                 $metathesekeys{'responseorder'}=1;
                   13604:             }
                   13605:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
                   13606:                 my $origid = $origfiletagids[$index+1];
                   13607:                 if ($origfiletagids[$index] eq 'part') {
                   13608: # Original part, part of the problem
                   13609:                     if ($importedparts) {
                   13610:                         $metaentry{':partorder'}.=','.$origid;
                   13611:                     }
                   13612:                 } elsif ($origfiletagids[$index] eq 'import') {
                   13613:                     if ($importedparts) {
                   13614: # We have imported parts at this position
1.1373    raeburn  13615:                         if ($importedpartids{$origid} ne '') {
                   13616:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
                   13617:                         }
1.1369    raeburn  13618:                     }
                   13619:                     if ($importedresponses) {
                   13620: # We have imported responses at this position
1.1373    raeburn  13621:                         if ($importedrespids{$origid} ne '') {
                   13622:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
1.1369    raeburn  13623:                         }
                   13624:                     }
                   13625:                 } else {
                   13626: # Original response item, part of the problem
                   13627:                     if ($importedresponses) {
                   13628:                         $metaentry{':responseorder'}.=','.$origid;
                   13629:                     }
                   13630:                 }
                   13631:             }
                   13632:             if ($importedparts) {
                   13633:                 $metaentry{':partorder'}=~s/^\,//;
                   13634:             }
                   13635:             if ($importedresponses) {
                   13636:                 $metaentry{':responseorder'}=~s/^\,//;
                   13637:             }
1.1070    www      13638:         }
1.737     albertel 13639: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 13640: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274    raeburn  13641: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.1371    raeburn  13642:         unless ($liburi) {
                   13643: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
                   13644:         }
1.177     www      13645: # this is the end of "was not already recently cached
1.71      www      13646:     }
1.599     albertel 13647:     return $metaentry{':'.$what};
1.261     albertel 13648: }
                   13649: 
1.488     albertel 13650: sub metadata_create_package_def {
1.483     albertel 13651:     my ($uri,$key,$package,$metathesekeys)=@_;
                   13652:     my ($pack,$name,$subp)=split(/\&/,$key);
                   13653:     if ($subp eq 'default') { next; }
                   13654:     
1.599     albertel 13655:     if (defined($metaentry{':packages'})) {
                   13656: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 13657:     } else {
1.599     albertel 13658: 	$metaentry{':packages'}=$package;
1.483     albertel 13659:     }
                   13660:     my $value=$packagetab{$key};
                   13661:     my $unikey;
                   13662:     $unikey='parameter_0_'.$name;
1.599     albertel 13663:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 13664:     $$metathesekeys{$unikey}=1;
1.599     albertel 13665:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   13666: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 13667:     }
1.599     albertel 13668:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   13669: 	$metaentry{':'.$unikey}=
                   13670: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 13671:     }
                   13672: }
                   13673: 
1.261     albertel 13674: sub metadata_generate_part0 {
                   13675:     my ($metadata,$metacache,$uri) = @_;
                   13676:     my %allnames;
1.737     albertel 13677:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 13678: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 13679: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   13680: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 13681: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 13682: 	    $allnames{$name}=$part;
                   13683: 	  }
                   13684: 	}
                   13685:     }
                   13686:     foreach my $name (keys(%allnames)) {
                   13687:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 13688:       my $key=":parameter_0_$name";
1.261     albertel 13689:       $$metacache{"$key.part"}='0';
                   13690:       $$metacache{"$key.name"}=$name;
1.428     albertel 13691:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 13692: 					   $allnames{$name}.'_'.$name.
                   13693: 					   '.type'};
1.428     albertel 13694:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 13695: 			     '.display'};
1.644     www      13696:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 13697:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 13698:       $$metacache{"$key.display"}=$olddis;
                   13699:     }
1.71      www      13700: }
                   13701: 
1.764     albertel 13702: # ------------------------------------------------------ Devalidate title cache
                   13703: 
                   13704: sub devalidate_title_cache {
                   13705:     my ($url)=@_;
                   13706:     if (!$env{'request.course.id'}) { return; }
                   13707:     my $symb=&symbread($url);
                   13708:     if (!$symb) { return; }
                   13709:     my $key=$env{'request.course.id'}."\0".$symb;
                   13710:     &devalidate_cache_new('title',$key);
                   13711: }
                   13712: 
1.1014    droeschl 13713: # ------------------------------------------------- Get the title of a course
                   13714: 
                   13715: sub current_course_title {
                   13716:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   13717: }
1.301     www      13718: # ------------------------------------------------- Get the title of a resource
                   13719: 
                   13720: sub gettitle {
                   13721:     my $urlsymb=shift;
                   13722:     my $symb=&symbread($urlsymb);
1.534     albertel 13723:     if ($symb) {
1.620     albertel 13724: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 13725: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 13726: 	if (defined($cached)) { 
                   13727: 	    return $result;
                   13728: 	}
1.534     albertel 13729: 	my ($map,$resid,$url)=&decode_symb($symb);
                   13730: 	my $title='';
1.907     albertel 13731: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   13732: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   13733: 	} else {
                   13734: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   13735: 		    &GDBM_READER(),0640)) {
                   13736: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   13737: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   13738: 		untie(%bighash);
                   13739: 	    }
1.534     albertel 13740: 	}
                   13741: 	$title=~s/\&colon\;/\:/gs;
                   13742: 	if ($title) {
1.1159    www      13743: # Remember both $symb and $title for dynamic metadata
                   13744:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      13745:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      13746: # Cache this title and then return it
1.599     albertel 13747: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 13748: 	}
                   13749: 	$urlsymb=$url;
                   13750:     }
                   13751:     my $title=&metadata($urlsymb,'title');
                   13752:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   13753:     return $title;
1.301     www      13754: }
1.613     albertel 13755: 
1.614     albertel 13756: sub get_slot {
                   13757:     my ($which,$cnum,$cdom)=@_;
                   13758:     if (!$cnum || !$cdom) {
1.790     albertel 13759: 	(undef,my $courseid)=&whichuser();
1.620     albertel 13760: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   13761: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 13762:     }
1.703     albertel 13763:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   13764:     my %slotinfo;
                   13765:     if (exists($remembered{$key})) {
                   13766: 	$slotinfo{$which} = $remembered{$key};
                   13767:     } else {
                   13768: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   13769: 	&Apache::lonhomework::showhash(%slotinfo);
                   13770: 	my ($tmp)=keys(%slotinfo);
                   13771: 	if ($tmp=~/^error:/) { return (); }
                   13772: 	$remembered{$key} = $slotinfo{$which};
                   13773:     }
1.616     albertel 13774:     if (ref($slotinfo{$which}) eq 'HASH') {
                   13775: 	return %{$slotinfo{$which}};
                   13776:     }
                   13777:     return $slotinfo{$which};
1.614     albertel 13778: }
1.1150    raeburn  13779: 
                   13780: sub get_reservable_slots {
                   13781:     my ($cnum,$cdom,$uname,$udom) = @_;
                   13782:     my $now = time;
                   13783:     my $reservable_info;
                   13784:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   13785:     if (exists($remembered{$key})) {
                   13786:         $reservable_info = $remembered{$key};
                   13787:     } else {
                   13788:         my %resv;
                   13789:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   13790:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   13791:         $reservable_info = \%resv;
                   13792:         $remembered{$key} = $reservable_info;
                   13793:     }
                   13794:     return $reservable_info;
                   13795: }
                   13796: 
                   13797: sub get_course_slots {
                   13798:     my ($cnum,$cdom) = @_;
                   13799:     my $hashid=$cnum.':'.$cdom;
1.1494    raeburn  13800:     my ($result,$cached) = &is_cached_new('allslots',$hashid);
1.1150    raeburn  13801:     if (defined($cached)) {
                   13802:         if (ref($result) eq 'HASH') {
                   13803:             return %{$result};
                   13804:         }
                   13805:     } else {
1.1494    raeburn  13806:         my %slots=&dump('slots',$cdom,$cnum);
1.1150    raeburn  13807:         my ($tmp) = keys(%slots);
                   13808:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  13809:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  13810:             return %slots;
                   13811:         }
                   13812:     }
                   13813:     return;
                   13814: }
                   13815: 
                   13816: sub devalidate_slots_cache {
                   13817:     my ($cnum,$cdom)=@_;
                   13818:     my $hashid=$cnum.':'.$cdom;
                   13819:     &devalidate_cache_new('allslots',$hashid);
                   13820: }
                   13821: 
1.1181    raeburn  13822: sub get_coursechange {
                   13823:     my ($cdom,$cnum) = @_;
                   13824:     if ($cdom eq '' || $cnum eq '') {
                   13825:         return unless ($env{'request.course.id'});
                   13826:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   13827:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   13828:     }
                   13829:     my $hashid=$cdom.'_'.$cnum;
                   13830:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   13831:     if ((defined($cached)) && ($change ne '')) {
                   13832:         return $change;
                   13833:     } else {
                   13834:         my %crshash;
                   13835:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   13836:         if ($crshash{'internal.contentchange'} eq '') {
                   13837:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   13838:             if ($change eq '') {
                   13839:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   13840:                 $change = $crshash{'internal.created'};
                   13841:             }
                   13842:         } else {
                   13843:             $change = $crshash{'internal.contentchange'};
                   13844:         }
                   13845:         my $cachetime = 600;
                   13846:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   13847:     }
                   13848:     return $change;
                   13849: }
                   13850: 
                   13851: sub devalidate_coursechange_cache {
1.1496    raeburn  13852:     my ($cdom,$cnum)=@_;
                   13853:     my $hashid=$cdom.'_'.$cnum;
1.1181    raeburn  13854:     &devalidate_cache_new('crschange',$hashid);
                   13855: }
                   13856: 
1.1496    raeburn  13857: sub get_suppchange {
                   13858:     my ($cdom,$cnum) = @_;
                   13859:     if ($cdom eq '' || $cnum eq '') {
                   13860:         return unless ($env{'request.course.id'});
                   13861:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   13862:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   13863:     }
                   13864:     my $hashid=$cdom.'_'.$cnum;
                   13865:     my ($change,$cached)=&is_cached_new('suppchange',$hashid);
                   13866:     if ((defined($cached)) && ($change ne '')) {
                   13867:         return $change;
                   13868:     } else {
                   13869:         my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
                   13870:         if ($crshash{'internal.supplementalchange'} eq '') {
                   13871:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   13872:             if ($change eq '') {
                   13873:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   13874:                 $change = $crshash{'internal.created'};
                   13875:             }
                   13876:         } else {
                   13877:             $change = $crshash{'internal.supplementalchange'};
                   13878:         }
                   13879:         my $cachetime = 600;
                   13880:         &do_cache_new('suppchange',$hashid,$change,$cachetime);
                   13881:     }
                   13882:     return $change;
                   13883: }
                   13884: 
                   13885: sub devalidate_suppchange_cache {
                   13886:     my ($cdom,$cnum)=@_;
                   13887:     my $hashid=$cdom.'_'.$cnum;
                   13888:     &devalidate_cache_new('suppchange',$hashid);
                   13889: }
                   13890: 
1.1497    raeburn  13891: sub update_supp_caches {
                   13892:     my ($cdom,$cnum) = @_;
                   13893:     my %servers = &internet_dom_servers($cdom);
                   13894:     my @ids=&current_machine_ids();
                   13895:     foreach my $server (keys(%servers)) {
                   13896:         next if (grep(/^\Q$server\E$/,@ids));
                   13897:         my $hashid=$cnum.':'.$cdom;
                   13898:         my $cachekey = &escape('showsupp').':'.&escape($hashid);
                   13899:         &remote_devalidate_cache($server,[$cachekey]);
                   13900:     }
                   13901:     &has_unhidden_suppfiles($cnum,$cdom,1,1);
                   13902:     &count_supptools($cnum,$cdom,1);
                   13903:     my $now = time;
                   13904:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                   13905:         &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
                   13906:     }
                   13907:     &put('environment',{'internal.supplementalchange' => $now},
                   13908:          $cdom,$cnum);
                   13909:     &Apache::lonnet::appenv(
                   13910:         {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
                   13911:     &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
                   13912: }
                   13913: 
1.31      www      13914: # ------------------------------------------------- Update symbolic store links
                   13915: 
                   13916: sub symblist {
                   13917:     my ($mapname,%newhash)=@_;
1.438     www      13918:     $mapname=&deversion(&declutter($mapname));
1.31      www      13919:     my %hash;
1.620     albertel 13920:     if (($env{'request.course.fn'}) && (%newhash)) {
                   13921:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 13922:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  13923: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 13924: 		next if ($url eq 'last_known'
                   13925: 			 && $env{'form.no_update_last_known'});
                   13926: 		$hash{declutter($url)}=&encode_symb($mapname,
                   13927: 						    $newhash{$url}->[1],
                   13928: 						    $newhash{$url}->[0]);
1.191     harris41 13929:             }
1.31      www      13930:             if (untie(%hash)) {
                   13931: 		return 'ok';
                   13932:             }
                   13933:         }
                   13934:     }
                   13935:     return 'error';
1.212     www      13936: }
                   13937: 
                   13938: # --------------------------------------------------------------- Verify a symb
                   13939: 
                   13940: sub symbverify {
1.1190    raeburn  13941:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      13942:     my $thisfn=$thisurl;
1.439     www      13943:     $thisfn=&declutter($thisfn);
1.215     www      13944: # direct jump to resource in page or to a sequence - will construct own symbs
                   13945:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   13946: # check URL part
1.409     www      13947:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      13948: 
1.431     www      13949:     unless ($url eq $thisfn) { return 0; }
1.213     www      13950: 
1.216     www      13951:     $symb=&symbclean($symb);
1.510     www      13952:     $thisurl=&deversion($thisurl);
1.439     www      13953:     $thisfn=&deversion($thisfn);
1.213     www      13954: 
                   13955:     my %bighash;
                   13956:     my $okay=0;
1.431     www      13957: 
1.620     albertel 13958:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 13959:                             &GDBM_READER(),0640)) {
1.1032    raeburn  13960:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   13961:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  13962:             if ($map =~ m{^uploaded/.+\.page$}) {
                   13963:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   13964:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   13965:             }
                   13966:         }
                   13967:         my $ids;
1.1419    raeburn  13968:         if ($map =~ m{^uploaded/.+\.page$}) {
                   13969:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1204    raeburn  13970:         } else {
                   13971:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  13972:         }
1.1102    raeburn  13973:         unless ($ids) {
                   13974:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   13975:             $ids=$bighash{$idkey};
1.216     www      13976:         }
                   13977:         if ($ids) {
                   13978: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  13979:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  13980:                 $symb =~ s/\?.+$//;
1.1202    raeburn  13981:             }
1.800     albertel 13982: 	    foreach my $id (split(/\,/,$ids)) {
                   13983: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      13984:                if (
                   13985:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  13986:    eq $symb) {
                   13987:                    if (ref($encstate)) {
                   13988:                        $$encstate = $bighash{'encrypted_'.$id};
                   13989:                    }
1.620     albertel 13990: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  13991: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   13992:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  13993: 		       $okay=1;
1.1202    raeburn  13994:                        last;
1.582     albertel 13995: 		   }
                   13996: 	       }
1.216     www      13997: 	   }
                   13998:         }
1.213     www      13999: 	untie(%bighash);
                   14000:     }
                   14001:     return $okay;
1.31      www      14002: }
                   14003: 
1.210     www      14004: # --------------------------------------------------------------- Clean-up symb
                   14005: 
                   14006: sub symbclean {
                   14007:     my $symb=shift;
1.568     albertel 14008:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      14009: # remove version from map
                   14010:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      14011: 
1.210     www      14012: # remove version from URL
                   14013:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      14014: 
1.507     www      14015: # remove wrapper
                   14016: 
1.510     www      14017:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 14018:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      14019:     return $symb;
1.409     www      14020: }
                   14021: 
                   14022: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 14023: 
                   14024: sub encode_symb {
                   14025:     my ($map,$resid,$url)=@_;
                   14026:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   14027: }
1.409     www      14028: 
                   14029: sub decode_symb {
1.568     albertel 14030:     my $symb=shift;
                   14031:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   14032:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      14033:     return (&fixversion($map),$resid,&fixversion($url));
                   14034: }
                   14035: 
                   14036: sub fixversion {
                   14037:     my $fn=shift;
1.609     banghart 14038:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      14039:     my %bighash;
                   14040:     my $uri=&clutter($fn);
1.620     albertel 14041:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      14042: # is this cached?
1.599     albertel 14043:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      14044:     if (defined($cached)) { return $result; }
                   14045: # unfortunately not cached, or expired
1.620     albertel 14046:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      14047: 	    &GDBM_READER(),0640)) {
                   14048:  	if ($bighash{'version_'.$uri}) {
                   14049:  	    my $version=$bighash{'version_'.$uri};
1.444     www      14050:  	    unless (($version eq 'mostrecent') || 
                   14051: 		    ($version==&getversion($uri))) {
1.440     www      14052:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   14053:  	    }
                   14054:  	}
                   14055:  	untie %bighash;
1.413     www      14056:     }
1.599     albertel 14057:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      14058: }
                   14059: 
                   14060: sub deversion {
                   14061:     my $url=shift;
                   14062:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   14063:     return $url;
1.210     www      14064: }
                   14065: 
1.31      www      14066: # ------------------------------------------------------ Return symb list entry
                   14067: 
                   14068: sub symbread {
1.1424    raeburn  14069:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1427    raeburn  14070:         $ignoresymbdb,$noenccheck)=@_;
1.1265    raeburn  14071:     my $cache_str='request.symbread.cached.'.$thisfn;
1.1427    raeburn  14072:     if (defined($env{$cache_str})) {
1.1424    raeburn  14073:         unless (ref($possibles) eq 'HASH') {
                   14074:             if ($ignorecachednull) {
                   14075:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
                   14076:             } else {
                   14077:                 return $env{$cache_str};
                   14078:             }
1.1282    raeburn  14079:         }
                   14080:     }
1.242     www      14081: # no filename provided? try from environment
1.1265    raeburn  14082:     unless ($thisfn) {
1.620     albertel 14083:         if ($env{'request.symb'}) {
1.1427    raeburn  14084:             return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 14085: 	}
1.620     albertel 14086: 	$thisfn=$env{'request.filename'};
1.44      www      14087:     }
1.569     albertel 14088:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      14089: # is that filename actually a symb? Verify, clean, and return
                   14090:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 14091: 	if (&symbverify($thisfn,$1)) {
1.1427    raeburn  14092: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 14093: 	}
1.242     www      14094:     }
1.44      www      14095:     $thisfn=declutter($thisfn);
1.31      www      14096:     my %hash;
1.37      www      14097:     my %bighash;
                   14098:     my $syval='';
1.620     albertel 14099:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.1427    raeburn  14100:         unless ($ignoresymbdb) {
1.1424    raeburn  14101:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                   14102:                           &GDBM_READER(),0640)) {
1.1487    raeburn  14103: 	        $syval=$hash{$thisfn};
1.1424    raeburn  14104:                 untie(%hash);
                   14105:             }
1.1427    raeburn  14106:             if ($syval && $checkforblock) {
                   14107:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1424    raeburn  14108:                 if (@blockers) {
                   14109:                     $syval='';
                   14110:                 }
                   14111:             }
1.37      www      14112:         }
                   14113: # ---------------------------------------------------------- There was an entry
                   14114:         if ($syval) {
1.601     albertel 14115: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 14116: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  14117: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 14118: 		    #return $env{$cache_str}='';
1.601     albertel 14119: 		#}    
                   14120: 		#$syval.=$1;
                   14121: 	    #}
1.37      www      14122:         } else {
                   14123: # ------------------------------------------------------- Was not in symb table
1.620     albertel 14124:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 14125:                             &GDBM_READER(),0640)) {
1.37      www      14126: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      14127:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      14128:               unless ($ids) { 
                   14129:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      14130:               }
                   14131:               unless ($ids) {
                   14132: # alias?
                   14133: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      14134:               }
1.37      www      14135:               if ($ids) {
                   14136: # ------------------------------------------------------------------- Has ID(s)
                   14137:                  my @possibilities=split(/\,/,$ids);
1.39      www      14138:                  if ($#possibilities==0) {
                   14139: # ----------------------------------------------- There is only one possibility
1.37      www      14140: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 14141: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   14142: 						    $resid,$thisfn);
1.1282    raeburn  14143:                      if (ref($possibles) eq 'HASH') {
1.1424    raeburn  14144:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
                   14145:                              $possibles->{$syval} = 1;
                   14146:                          }
1.1282    raeburn  14147:                      }
                   14148:                      if ($checkforblock) {
1.1424    raeburn  14149:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1426    raeburn  14150:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1424    raeburn  14151:                              if (@blockers) {
                   14152:                                  $syval = '';
1.1425    raeburn  14153:                                  untie(%bighash);
                   14154:                                  return $env{$cache_str}='';
1.1424    raeburn  14155:                              }
1.1282    raeburn  14156:                          }
                   14157:                      }
                   14158:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
1.39      www      14159: # ------------------------------------------ There is more than one possibility
                   14160:                      my $realpossible=0;
1.800     albertel 14161:                      foreach my $id (@possibilities) {
                   14162: 			 my $file=$bighash{'src_'.$id};
1.1282    raeburn  14163:                          my $canaccess;
                   14164:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
                   14165:                              $canaccess = 1;
                   14166:                          } else { 
                   14167:                              $canaccess = &allowed('bre',$file);
                   14168:                          }
                   14169:                          if ($canaccess) {
                   14170:          		     my ($mapid,$resid)=split(/\./,$id);
                   14171:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
                   14172:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
                   14173: 						             $resid,$thisfn);
1.1424    raeburn  14174:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1426    raeburn  14175:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1282    raeburn  14176:                                  if ($checkforblock) {
1.1426    raeburn  14177:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1424    raeburn  14178:                                      if (@blockers > 0) {
                   14179:                                          $syval = '';
                   14180:                                      } else {
1.1282    raeburn  14181:                                          $syval = $poss_syval;
                   14182:                                          $realpossible++;
                   14183:                                      }
                   14184:                                  } else {
                   14185:                                      $syval = $poss_syval;
                   14186:                                      $realpossible++;
                   14187:                                  }
1.1424    raeburn  14188:                                  if ($syval) {
                   14189:                                      if (ref($possibles) eq 'HASH') {
                   14190:                                          $possibles->{$syval} = 1;
                   14191:                                      }
                   14192:                                  }
1.1282    raeburn  14193:                              }
1.39      www      14194: 			 }
1.191     harris41 14195:                      }
1.39      www      14196: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      14197:                  } else {
                   14198:                      $syval='';
1.37      www      14199:                  }
                   14200: 	      }
1.1282    raeburn  14201:               untie(%bighash);
1.481     raeburn  14202:            }
1.31      www      14203:         }
1.62      www      14204:         if ($syval) {
1.1427    raeburn  14205: 	    return $env{$cache_str}=$syval;
1.62      www      14206:         }
1.31      www      14207:     }
1.949     raeburn  14208:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 14209:     return $env{$cache_str}='';
1.31      www      14210: }
                   14211: 
                   14212: # ---------------------------------------------------------- Return random seed
                   14213: 
1.32      www      14214: sub numval {
                   14215:     my $txt=shift;
                   14216:     $txt=~tr/A-J/0-9/;
                   14217:     $txt=~tr/a-j/0-9/;
                   14218:     $txt=~tr/K-T/0-9/;
                   14219:     $txt=~tr/k-t/0-9/;
                   14220:     $txt=~tr/U-Z/0-5/;
                   14221:     $txt=~tr/u-z/0-5/;
                   14222:     $txt=~s/\D//g;
1.564     albertel 14223:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      14224:     return int($txt);
1.368     albertel 14225: }
                   14226: 
1.484     albertel 14227: sub numval2 {
                   14228:     my $txt=shift;
                   14229:     $txt=~tr/A-J/0-9/;
                   14230:     $txt=~tr/a-j/0-9/;
                   14231:     $txt=~tr/K-T/0-9/;
                   14232:     $txt=~tr/k-t/0-9/;
                   14233:     $txt=~tr/U-Z/0-5/;
                   14234:     $txt=~tr/u-z/0-5/;
                   14235:     $txt=~s/\D//g;
                   14236:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   14237:     my $total;
                   14238:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 14239:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 14240:     return int($total);
                   14241: }
                   14242: 
1.575     albertel 14243: sub numval3 {
                   14244:     use integer;
                   14245:     my $txt=shift;
                   14246:     $txt=~tr/A-J/0-9/;
                   14247:     $txt=~tr/a-j/0-9/;
                   14248:     $txt=~tr/K-T/0-9/;
                   14249:     $txt=~tr/k-t/0-9/;
                   14250:     $txt=~tr/U-Z/0-5/;
                   14251:     $txt=~tr/u-z/0-5/;
                   14252:     $txt=~s/\D//g;
                   14253:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   14254:     my $total;
                   14255:     foreach my $val (@txts) { $total+=$val; }
                   14256:     if ($_64bit) { $total=(($total<<32)>>32); }
                   14257:     return $total;
                   14258: }
                   14259: 
1.675     albertel 14260: sub digest {
                   14261:     my ($data)=@_;
                   14262:     my $digest=&Digest::MD5::md5($data);
                   14263:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   14264:     my ($e,$f);
                   14265:     {
                   14266:         use integer;
                   14267:         $e=($a+$b);
                   14268:         $f=($c+$d);
                   14269:         if ($_64bit) {
                   14270:             $e=(($e<<32)>>32);
                   14271:             $f=(($f<<32)>>32);
                   14272:         }
                   14273:     }
                   14274:     if (wantarray) {
                   14275: 	return ($e,$f);
                   14276:     } else {
                   14277: 	my $g;
                   14278: 	{
                   14279: 	    use integer;
                   14280: 	    $g=($e+$f);
                   14281: 	    if ($_64bit) {
                   14282: 		$g=(($g<<32)>>32);
                   14283: 	    }
                   14284: 	}
                   14285: 	return $g;
                   14286:     }
                   14287: }
                   14288: 
1.368     albertel 14289: sub latest_rnd_algorithm_id {
1.675     albertel 14290:     return '64bit5';
1.366     albertel 14291: }
1.32      www      14292: 
1.503     albertel 14293: sub get_rand_alg {
                   14294:     my ($courseid)=@_;
1.790     albertel 14295:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 14296:     if ($courseid) {
1.620     albertel 14297: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 14298:     }
                   14299:     return &latest_rnd_algorithm_id();
                   14300: }
                   14301: 
1.562     albertel 14302: sub validCODE {
                   14303:     my ($CODE)=@_;
                   14304:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   14305:     return 0;
                   14306: }
                   14307: 
1.491     albertel 14308: sub getCODE {
1.620     albertel 14309:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 14310:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   14311: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   14312: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 14313: 	return $Apache::lonhomework::history{'resource.CODE'};
                   14314:     }
                   14315:     return undef;
                   14316: }
1.1133    foxr     14317: #
                   14318: #  Determines the random seed for a specific context:
                   14319: #
                   14320: # parameters:
                   14321: #   symb      - in course context the symb for the seed.
                   14322: #   course_id - The course id of the form domain_coursenum.
                   14323: #   domain    - Domain for the user.
                   14324: #   course    - Course for the user.
                   14325: #   cenv      - environment of the course.
                   14326: #
                   14327: # NOTE:
                   14328: #   All parameters are picked out of the environment if missing
                   14329: #   or not defined.
                   14330: #   If a symb cannot be determined the current time is used instead.
                   14331: #
                   14332: #  For a given well defined symb, courside, domain, username,
                   14333: #  and course environment, the seed is reproducible.
                   14334: #
1.31      www      14335: sub rndseed {
1.1133    foxr     14336:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 14337:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 14338:     if (!defined($symb)) {
1.366     albertel 14339: 	unless ($symb=$wsymb) { return time; }
                   14340:     }
1.1146    foxr     14341:     if (!defined $courseid) { 
                   14342: 	$courseid=$wcourseid; 
                   14343:     }
                   14344:     if (!defined $domain) { $domain=$wdomain; }
                   14345:     if (!defined $username) { $username=$wusername }
1.1133    foxr     14346: 
                   14347:     my $which;
                   14348:     if (defined($cenv->{'rndseed'})) {
                   14349: 	$which = $cenv->{'rndseed'};
                   14350:     } else {
                   14351: 	$which =&get_rand_alg($courseid);
                   14352:     }
1.491     albertel 14353:     if (defined(&getCODE())) {
1.1133    foxr     14354: 
1.675     albertel 14355: 	if ($which eq '64bit5') {
                   14356: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   14357: 	} elsif ($which eq '64bit4') {
1.575     albertel 14358: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   14359: 	} else {
                   14360: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   14361: 	}
1.675     albertel 14362:     } elsif ($which eq '64bit5') {
                   14363: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 14364:     } elsif ($which eq '64bit4') {
                   14365: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 14366:     } elsif ($which eq '64bit3') {
                   14367: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 14368:     } elsif ($which eq '64bit2') {
                   14369: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 14370:     } elsif ($which eq '64bit') {
                   14371: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   14372:     }
                   14373:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   14374: }
                   14375: 
                   14376: sub rndseed_32bit {
                   14377:     my ($symb,$courseid,$domain,$username)=@_;
                   14378:     {
                   14379: 	use integer;
                   14380: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   14381: 	my $symbseed=numval($symb) << 22;
                   14382: 	my $namechck=unpack("%32C*",$username) << 17;
                   14383: 	my $nameseed=numval($username) << 12;
                   14384: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   14385: 	my $courseseed=unpack("%32C*",$courseid);
                   14386: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 14387: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14388: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 14389: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 14390: 	return $num;
                   14391:     }
                   14392: }
                   14393: 
                   14394: sub rndseed_64bit {
                   14395:     my ($symb,$courseid,$domain,$username)=@_;
                   14396:     {
                   14397: 	use integer;
                   14398: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   14399: 	my $symbseed=numval($symb) << 10;
                   14400: 	my $namechck=unpack("%32S*",$username);
                   14401: 	
                   14402: 	my $nameseed=numval($username) << 21;
                   14403: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   14404: 	my $courseseed=unpack("%32S*",$courseid);
                   14405: 	
                   14406: 	my $num1=$symbchck+$symbseed+$namechck;
                   14407: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14408: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14409: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 14410: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 14411: 	return "$num1,$num2";
1.155     albertel 14412:     }
1.366     albertel 14413: }
                   14414: 
1.443     albertel 14415: sub rndseed_64bit2 {
                   14416:     my ($symb,$courseid,$domain,$username)=@_;
                   14417:     {
                   14418: 	use integer;
                   14419: 	# strings need to be an even # of cahracters long, it it is odd the
                   14420:         # last characters gets thrown away
                   14421: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14422: 	my $symbseed=numval($symb) << 10;
                   14423: 	my $namechck=unpack("%32S*",$username.' ');
                   14424: 	
                   14425: 	my $nameseed=numval($username) << 21;
1.501     albertel 14426: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14427: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14428: 	
                   14429: 	my $num1=$symbchck+$symbseed+$namechck;
                   14430: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14431: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14432: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 14433: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 14434: 	return "$num1,$num2";
                   14435:     }
                   14436: }
                   14437: 
                   14438: sub rndseed_64bit3 {
                   14439:     my ($symb,$courseid,$domain,$username)=@_;
                   14440:     {
                   14441: 	use integer;
                   14442: 	# strings need to be an even # of cahracters long, it it is odd the
                   14443:         # last characters gets thrown away
                   14444: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14445: 	my $symbseed=numval2($symb) << 10;
                   14446: 	my $namechck=unpack("%32S*",$username.' ');
                   14447: 	
                   14448: 	my $nameseed=numval2($username) << 21;
1.443     albertel 14449: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14450: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14451: 	
                   14452: 	my $num1=$symbchck+$symbseed+$namechck;
                   14453: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14454: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14455: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 14456: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      14457: 	
1.503     albertel 14458: 	return "$num1:$num2";
1.443     albertel 14459:     }
                   14460: }
                   14461: 
1.575     albertel 14462: sub rndseed_64bit4 {
                   14463:     my ($symb,$courseid,$domain,$username)=@_;
                   14464:     {
                   14465: 	use integer;
                   14466: 	# strings need to be an even # of cahracters long, it it is odd the
                   14467:         # last characters gets thrown away
                   14468: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   14469: 	my $symbseed=numval3($symb) << 10;
                   14470: 	my $namechck=unpack("%32S*",$username.' ');
                   14471: 	
                   14472: 	my $nameseed=numval3($username) << 21;
                   14473: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   14474: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14475: 	
                   14476: 	my $num1=$symbchck+$symbseed+$namechck;
                   14477: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 14478: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   14479: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 14480: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      14481: 	
1.575     albertel 14482: 	return "$num1:$num2";
                   14483:     }
                   14484: }
                   14485: 
1.675     albertel 14486: sub rndseed_64bit5 {
                   14487:     my ($symb,$courseid,$domain,$username)=@_;
                   14488:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   14489:     return "$num1:$num2";
                   14490: }
                   14491: 
1.366     albertel 14492: sub rndseed_CODE_64bit {
                   14493:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 14494:     {
1.366     albertel 14495: 	use integer;
1.443     albertel 14496: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 14497: 	my $symbseed=numval2($symb);
1.491     albertel 14498: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   14499: 	my $CODEseed=numval(&getCODE());
1.443     albertel 14500: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 14501: 	my $num1=$symbseed+$CODEchck;
                   14502: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 14503: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   14504: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 14505: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   14506: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 14507: 	return "$num1:$num2";
1.366     albertel 14508:     }
                   14509: }
                   14510: 
1.575     albertel 14511: sub rndseed_CODE_64bit4 {
                   14512:     my ($symb,$courseid,$domain,$username)=@_;
                   14513:     {
                   14514: 	use integer;
                   14515: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   14516: 	my $symbseed=numval3($symb);
                   14517: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   14518: 	my $CODEseed=numval3(&getCODE());
                   14519: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   14520: 	my $num1=$symbseed+$CODEchck;
                   14521: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 14522: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   14523: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 14524: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   14525: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   14526: 	return "$num1:$num2";
                   14527:     }
                   14528: }
                   14529: 
1.675     albertel 14530: sub rndseed_CODE_64bit5 {
                   14531:     my ($symb,$courseid,$domain,$username)=@_;
                   14532:     my $code = &getCODE();
                   14533:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   14534:     return "$num1:$num2";
                   14535: }
                   14536: 
1.366     albertel 14537: sub setup_random_from_rndseed {
                   14538:     my ($rndseed)=@_;
1.503     albertel 14539:     if ($rndseed =~/([,:])/) {
1.1262    raeburn  14540:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
                   14541:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
                   14542:             &Math::Random::random_set_seed_from_phrase($rndseed);
                   14543:         } else {
                   14544:             &Math::Random::random_set_seed($num1,$num2);
                   14545:         }
1.366     albertel 14546:     } else {
                   14547: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 14548:     }
1.36      albertel 14549: }
                   14550: 
1.474     albertel 14551: sub latest_receipt_algorithm_id {
1.835     albertel 14552:     return 'receipt3';
1.474     albertel 14553: }
                   14554: 
1.480     www      14555: sub recunique {
                   14556:     my $fucourseid=shift;
                   14557:     my $unique;
1.835     albertel 14558:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   14559: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 14560: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      14561:     } else {
                   14562: 	$unique=$perlvar{'lonReceipt'};
                   14563:     }
                   14564:     return unpack("%32C*",$unique);
                   14565: }
                   14566: 
                   14567: sub recprefix {
                   14568:     my $fucourseid=shift;
                   14569:     my $prefix;
1.835     albertel 14570:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   14571: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 14572: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      14573:     } else {
                   14574: 	$prefix=$perlvar{'lonHostID'};
                   14575:     }
                   14576:     return unpack("%32C*",$prefix);
                   14577: }
                   14578: 
1.76      www      14579: sub ireceipt {
1.474     albertel 14580:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 14581: 
                   14582:     my $return =&recprefix($fucourseid).'-';
                   14583: 
                   14584:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   14585: 	$env{'request.state'} eq 'construct') {
                   14586: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   14587: 	return $return;
                   14588:     }
                   14589: 
1.76      www      14590:     my $cuname=unpack("%32C*",$funame);
                   14591:     my $cudom=unpack("%32C*",$fudom);
                   14592:     my $cucourseid=unpack("%32C*",$fucourseid);
                   14593:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      14594:     my $cunique=&recunique($fucourseid);
1.474     albertel 14595:     my $cpart=unpack("%32S*",$part);
1.835     albertel 14596:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   14597: 
1.790     albertel 14598: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 14599: 			       
                   14600: 	$return.= ($cunique%$cuname+
                   14601: 		   $cunique%$cudom+
                   14602: 		   $cusymb%$cuname+
                   14603: 		   $cusymb%$cudom+
                   14604: 		   $cucourseid%$cuname+
                   14605: 		   $cucourseid%$cudom+
                   14606: 		   $cpart%$cuname+
                   14607: 		   $cpart%$cudom);
                   14608:     } else {
                   14609: 	$return.= ($cunique%$cuname+
                   14610: 		   $cunique%$cudom+
                   14611: 		   $cusymb%$cuname+
                   14612: 		   $cusymb%$cudom+
                   14613: 		   $cucourseid%$cuname+
                   14614: 		   $cucourseid%$cudom);
                   14615:     }
                   14616:     return $return;
1.76      www      14617: }
                   14618: 
                   14619: sub receipt {
1.474     albertel 14620:     my ($part)=@_;
1.790     albertel 14621:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 14622:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      14623: }
1.260     ng       14624: 
1.790     albertel 14625: sub whichuser {
                   14626:     my ($passedsymb)=@_;
                   14627:     my ($symb,$courseid,$domain,$name,$publicuser);
                   14628:     if (defined($env{'form.grade_symb'})) {
                   14629: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   14630: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   14631: 	if (!$allowed &&
                   14632: 	    exists($env{'request.course.sec'}) &&
                   14633: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   14634: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   14635: 			      '/'.$env{'request.course.sec'});
                   14636: 	}
                   14637: 	if ($allowed) {
                   14638: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   14639: 	    $courseid=$tmp_courseid;
                   14640: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   14641: 	    ($name)=&get_env_multiple('form.grade_username');
                   14642: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   14643: 	}
                   14644:     }
                   14645:     if (!$passedsymb) {
                   14646: 	$symb=&symbread();
                   14647:     } else {
                   14648: 	$symb=$passedsymb;
                   14649:     }
                   14650:     $courseid=$env{'request.course.id'};
                   14651:     $domain=$env{'user.domain'};
                   14652:     $name=$env{'user.name'};
                   14653:     if ($name eq 'public' && $domain eq 'public') {
                   14654: 	if (!defined($env{'form.username'})) {
                   14655: 	    $env{'form.username'}.=time.rand(10000000);
                   14656: 	}
                   14657: 	$name.=$env{'form.username'};
                   14658:     }
                   14659:     return ($symb,$courseid,$domain,$name,$publicuser);
                   14660: 
                   14661: }
                   14662: 
1.36      albertel 14663: # ------------------------------------------------------------ Serves up a file
1.472     albertel 14664: # returns either the contents of the file or 
                   14665: # -1 if the file doesn't exist
1.481     raeburn  14666: #
                   14667: # if the target is a file that was uploaded via DOCS, 
                   14668: # a check will be made to see if a current copy exists on the local server,
                   14669: # if it does this will be served, otherwise a copy will be retrieved from
                   14670: # the home server for the course and stored in /home/httpd/html/userfiles on
                   14671: # the local server.   
1.472     albertel 14672: 
1.36      albertel 14673: sub getfile {
1.538     albertel 14674:     my ($file) = @_;
1.609     banghart 14675:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 14676:     &repcopy($file);
                   14677:     return &readfile($file);
                   14678: }
                   14679: 
                   14680: sub repcopy_userfile {
                   14681:     my ($file)=@_;
1.1142    raeburn  14682:     my $londocroot = $perlvar{'lonDocRoot'};
                   14683:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  14684:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 14685:     my ($cdom,$cnum,$filename) = 
1.811     albertel 14686: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 14687:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   14688:     if (-e "$file") {
1.828     www      14689: # we already have a local copy, check it out
1.538     albertel 14690: 	my @fileinfo = stat($file);
1.828     www      14691: 	my $rtncode;
                   14692: 	my $info;
1.538     albertel 14693: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 14694: 	if ($lwpresp ne 'ok') {
1.828     www      14695: # there is no such file anymore, even though we had a local copy
1.482     albertel 14696: 	    if ($rtncode eq '404') {
1.538     albertel 14697: 		unlink($file);
1.482     albertel 14698: 	    }
                   14699: 	    return -1;
                   14700: 	}
                   14701: 	if ($info < $fileinfo[9]) {
1.828     www      14702: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  14703: 	    return 'ok';
1.828     www      14704: 	} else {
                   14705: # the file is outdated, get rid of it
                   14706: 	    unlink($file);
1.482     albertel 14707: 	}
1.828     www      14708:     }
                   14709: # one way or the other, at this point, we don't have the file
                   14710: # construct the correct path for the file
                   14711:     my @parts = ($cdom,$cnum); 
                   14712:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   14713: 	push @parts, split(/\//,$1);
                   14714:     }
                   14715:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   14716:     foreach my $part (@parts) {
                   14717: 	$path .= '/'.$part;
                   14718: 	if (!-e $path) {
                   14719: 	    mkdir($path,0770);
1.482     albertel 14720: 	}
                   14721:     }
1.828     www      14722: # now the path exists for sure
                   14723: # get a user agent
                   14724:     my $transferfile=$file.'.in.transfer';
                   14725: # FIXME: this should flock
                   14726:     if (-e $transferfile) { return 'ok'; }
                   14727:     my $request;
                   14728:     $uri=~s/^\///;
1.980     raeburn  14729:     my $homeserver = &homeserver($cnum,$cdom);
1.1398    raeburn  14730:     my $hostname = &hostname($homeserver);
1.980     raeburn  14731:     my $protocol = $protocol{$homeserver};
                   14732:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14733:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.1345    raeburn  14734:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
1.828     www      14735: # did it work?
                   14736:     if ($response->is_error()) {
                   14737: 	unlink($transferfile);
                   14738: 	&logthis("Userfile repcopy failed for $uri");
                   14739: 	return -1;
                   14740:     }
                   14741: # worked, rename the transfer file
                   14742:     rename($transferfile,$file);
1.607     raeburn  14743:     return 'ok';
1.481     raeburn  14744: }
                   14745: 
1.517     albertel 14746: sub tokenwrapper {
                   14747:     my $uri=shift;
1.980     raeburn  14748:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 14749:     $uri=~s|^/||;
1.620     albertel 14750:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 14751:     my $token=$1;
1.552     albertel 14752:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   14753:     if ($udom && $uname && $file) {
                   14754: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  14755:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  14756:         my $homeserver = &homeserver($uname,$udom);
1.1397    raeburn  14757:         my $hostname = &hostname($homeserver);
1.980     raeburn  14758:         my $protocol = $protocol{$homeserver};
                   14759:         $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14760:         return $protocol.'://'.$hostname.'/'.$uri.
1.517     albertel 14761:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   14762:                                '&tokenissued='.$perlvar{'lonHostID'};
                   14763:     } else {
                   14764:         return '/adm/notfound.html';
                   14765:     }
                   14766: }
                   14767: 
1.828     www      14768: # call with reqtype HEAD: get last modification time
                   14769: # call with reqtype GET: get the file contents
                   14770: # Do not call this with reqtype GET for large files! It loads everything into memory
                   14771: #
1.481     raeburn  14772: sub getuploaded {
                   14773:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   14774:     $uri=~s/^\///;
1.980     raeburn  14775:     my $homeserver = &homeserver($cnum,$cdom);
1.1397    raeburn  14776:     my $hostname = &hostname($homeserver);
1.980     raeburn  14777:     my $protocol = $protocol{$homeserver};
                   14778:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  14779:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481     raeburn  14780:     my $request=new HTTP::Request($reqtype,$uri);
1.1345    raeburn  14781:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
1.481     raeburn  14782:     $$rtncode = $response->code;
1.482     albertel 14783:     if (! $response->is_success()) {
                   14784: 	return 'failed';
                   14785:     }      
                   14786:     if ($reqtype eq 'HEAD') {
1.486     www      14787: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 14788:     } elsif ($reqtype eq 'GET') {
                   14789: 	$$info = $response->content;
1.472     albertel 14790:     }
1.482     albertel 14791:     return 'ok';
1.36      albertel 14792: }
                   14793: 
1.481     raeburn  14794: sub readfile {
                   14795:     my $file = shift;
                   14796:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   14797:     my $fh;
1.1359    raeburn  14798:     open($fh,"<",$file);
1.481     raeburn  14799:     my $a='';
1.800     albertel 14800:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  14801:     return $a;
                   14802: }
                   14803: 
1.36      albertel 14804: sub filelocation {
1.590     banghart 14805:     my ($dir,$file) = @_;
                   14806:     my $location;
                   14807:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 14808: 
                   14809:     if ($file =~ m-^/adm/-) {
                   14810: 	$file=~s-^/adm/wrapper/-/-;
                   14811: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   14812:     }
1.882     albertel 14813: 
1.1139    www      14814:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  14815:         $location = $file;
1.609     banghart 14816:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 14817:         my ($udom,$uname,$filename)=
1.811     albertel 14818:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 14819:         my $home=&homeserver($uname,$udom);
                   14820:         my $is_me=0;
                   14821:         my @ids=&current_machine_ids();
                   14822:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   14823:         if ($is_me) {
1.1117    foxr     14824:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 14825:         } else {
                   14826:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   14827:   	      $udom.'/'.$uname.'/'.$filename;
                   14828:         }
1.882     albertel 14829:     } elsif ($file =~ m-^/adm/-) {
                   14830: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 14831:     } else {
                   14832:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      14833:         $file=~s:^/(res|priv)/:/:;
                   14834:         my $space=$1;
1.590     banghart 14835:         if ( !( $file =~ m:^/:) ) {
                   14836:             $location = $dir. '/'.$file;
                   14837:         } else {
1.1142    raeburn  14838:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 14839:         }
1.59      albertel 14840:     }
1.590     banghart 14841:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 14842:     while ($location=~m{/\.\./}) {
                   14843: 	if ($location =~ m{/[^/]+/\.\./}) {
                   14844: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   14845: 	} else {
                   14846: 	    $location=~ s{/\.\./}{/}g;
                   14847: 	}
                   14848:     } #remove dir/..
1.590     banghart 14849:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   14850:     return $location;
1.46      www      14851: }
1.36      albertel 14852: 
1.46      www      14853: sub hreflocation {
                   14854:     my ($dir,$file)=@_;
1.980     raeburn  14855:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 14856: 	$file=filelocation($dir,$file);
1.700     albertel 14857:     } elsif ($file=~m-^/adm/-) {
                   14858: 	$file=~s-^/adm/wrapper/-/-;
                   14859: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 14860:     }
                   14861:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   14862: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   14863:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  14864: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   14865: 	        {/uploaded/$1/$2/}x;
1.46      www      14866:     }
1.913     albertel 14867:     if ($file=~ m{^/userfiles/}) {
                   14868: 	$file =~ s{^/userfiles/}{/uploaded/};
                   14869:     }
1.462     albertel 14870:     return $file;
1.465     albertel 14871: }
                   14872: 
1.1139    www      14873: 
                   14874: 
                   14875: 
                   14876: 
1.465     albertel 14877: sub current_machine_domains {
1.853     albertel 14878:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   14879: }
                   14880: 
                   14881: sub machine_domains {
                   14882:     my ($hostname) = @_;
1.465     albertel 14883:     my @domains;
1.838     albertel 14884:     my %hostname = &all_hostnames();
1.465     albertel 14885:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  14886: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 14887: 	if ($hostname eq $name) {
1.844     albertel 14888: 	    push(@domains,&host_domain($id));
1.465     albertel 14889: 	}
                   14890:     }
                   14891:     return @domains;
                   14892: }
                   14893: 
                   14894: sub current_machine_ids {
1.853     albertel 14895:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   14896: }
                   14897: 
                   14898: sub machine_ids {
                   14899:     my ($hostname) = @_;
                   14900:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 14901:     my @ids;
1.888     albertel 14902:     my %name_to_host = &all_names();
1.889     albertel 14903:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   14904: 	return @{ $name_to_host{$hostname} };
                   14905:     }
                   14906:     return;
1.31      www      14907: }
                   14908: 
1.824     raeburn  14909: sub additional_machine_domains {
                   14910:     my @domains;
1.1466    raeburn  14911:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
                   14912:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
                   14913:             while (my $line = <$fh>) {
                   14914:                 chomp($line);           
                   14915:                 $line =~ s/\s//g;
                   14916:                 push(@domains,$line);
                   14917:             }
                   14918:             close($fh);
                   14919:         }
1.824     raeburn  14920:     }
                   14921:     return @domains;
                   14922: }
                   14923: 
                   14924: sub default_login_domain {
                   14925:     my $domain = $perlvar{'lonDefDomain'};
                   14926:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   14927:     foreach my $posdom (&current_machine_domains(),
                   14928:                         &additional_machine_domains()) {
                   14929:         if (lc($posdom) eq lc($testdomain)) {
                   14930:             $domain=$posdom;
                   14931:             last;
                   14932:         }
                   14933:     }
                   14934:     return $domain;
                   14935: }
                   14936: 
1.1414    raeburn  14937: sub shared_institution {
1.1439    raeburn  14938:     my ($dom,$lonhost) = @_;
                   14939:     if ($lonhost eq '') {
                   14940:         $lonhost = $perlvar{'lonHostID'};
                   14941:     }
1.1414    raeburn  14942:     my $same_intdom;
1.1439    raeburn  14943:     my $hostintdom = &internet_dom($lonhost);
1.1414    raeburn  14944:     if ($hostintdom ne '') {
                   14945:         my %iphost = &get_iphost();
                   14946:         my $primary_id = &domain($dom,'primary');
                   14947:         my $primary_ip = &get_host_ip($primary_id);
                   14948:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
                   14949:             foreach my $id (@{$iphost{$primary_ip}}) {
                   14950:                 my $intdom = &internet_dom($id);
                   14951:                 if ($intdom eq $hostintdom) {
                   14952:                     $same_intdom = 1;
                   14953:                     last;
                   14954:                 }
                   14955:             }
                   14956:         }
                   14957:     }
                   14958:     return $same_intdom;
                   14959: }
                   14960: 
1.1398    raeburn  14961: sub uses_sts {
                   14962:     my ($ignore_cache) = @_;
                   14963:     my $lonhost = $perlvar{'lonHostID'};
                   14964:     my $hostname = &hostname($lonhost);
                   14965:     my $sts_on;
                   14966:     if ($protocol{$lonhost} eq 'https') {
                   14967:         my $cachetime = 12*3600;
                   14968:         if (!$ignore_cache) {
                   14969:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
                   14970:             if (defined($cached)) {
                   14971:                 return $sts_on;
                   14972:             }
                   14973:         }
                   14974:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
                   14975:         my $request=new HTTP::Request('HEAD',$url);
                   14976:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
                   14977:         if ($response->is_success) {
                   14978:             my $has_sts = $response->header('Strict-Transport-Security');
                   14979:             if ($has_sts eq '') {
                   14980:                 $sts_on = 0;
                   14981:             } else {
                   14982:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
                   14983:                     my $maxage = $1;
                   14984:                     if ($maxage) {
                   14985:                         $sts_on = 1;
                   14986:                     } else {
                   14987:                         $sts_on = 0;
                   14988:                     }
                   14989:                 } else {
                   14990:                     $sts_on = 0;
                   14991:                 }
                   14992:             }
                   14993:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
                   14994:         }
                   14995:     }
                   14996:     return;
                   14997: }
                   14998: 
1.1449    raeburn  14999: sub waf_allssl {
                   15000:     my ($host_name) = @_;
                   15001:     my $alias = &get_proxy_alias();
                   15002:     if ($host_name eq '') {
                   15003:         $host_name = $ENV{'SERVER_NAME'};
                   15004:     }
                   15005:     if (($host_name ne '') && ($alias eq $host_name)) {
                   15006:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
                   15007:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   15008:         if ($defdomdefaults{'waf_sslopt'}) {
                   15009:             return $defdomdefaults{'waf_sslopt'};
                   15010:         }
                   15011:     }
                   15012:     return;
                   15013: }
                   15014: 
1.1434    raeburn  15015: sub get_requestor_ip {
                   15016:     my ($r,$nolookup,$noproxy) = @_;
                   15017:     my $from_ip;
                   15018:     if (ref($r)) {
1.1447    raeburn  15019:         if ($r->can('useragent_ip')) {
                   15020:             if ($noproxy && $r->can('client_ip')) {
                   15021:                 $from_ip = $r->client_ip();
                   15022:             } else {
                   15023:                 $from_ip = $r->useragent_ip();
                   15024:             }
                   15025:         } elsif ($r->connection->can('remote_ip')) {
                   15026:             $from_ip = $r->connection->remote_ip();
                   15027:         } else {
                   15028:             $from_ip = $r->get_remote_host($nolookup);
                   15029:         }
1.1434    raeburn  15030:     } else {
                   15031:         $from_ip = $ENV{'REMOTE_ADDR'};
                   15032:     }
                   15033:     return $from_ip if ($noproxy); 
                   15034:     # Who controls proxy settings for server
                   15035:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
                   15036:     my $proxyinfo = &get_proxy_settings($dom_in_use);
                   15037:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
1.1437    raeburn  15038:         if ($proxyinfo->{'vpnint'}) {
                   15039:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
1.1434    raeburn  15040:                 return $from_ip;
                   15041:             }
                   15042:         }
                   15043:         if ($proxyinfo->{'trusted'}) {
                   15044:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
                   15045:                 my $ipheader = $proxyinfo->{'ipheader'};
                   15046:                 my ($ip,$xfor);
                   15047:                 if (ref($r)) {
                   15048:                     if ($ipheader) {
                   15049:                         $ip = $r->headers_in->{$ipheader};
                   15050:                     }
                   15051:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
                   15052:                 } else {
                   15053:                     if ($ipheader) {
                   15054:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
                   15055:                     }
                   15056:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
                   15057:                 }
                   15058:                 if (($ip eq '') && ($xfor ne '')) {
                   15059:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
                   15060:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
                   15061:                             $ip = $poss_ip;
1.1435    raeburn  15062:                             last;
1.1434    raeburn  15063:                         }
                   15064:                     }
                   15065:                 }
                   15066:                 if ($ip ne '') {
                   15067:                     return $ip;
                   15068:                 }
                   15069:             }
                   15070:         }
                   15071:     }
                   15072:     return $from_ip;
                   15073: }
                   15074: 
                   15075: sub get_proxy_settings {
                   15076:     my ($dom_in_use) = @_;
1.1494    raeburn  15077:     my %domdefaults = &get_domain_defaults($dom_in_use);
1.1434    raeburn  15078:     my $proxyinfo = {
                   15079:                        ipheader => $domdefaults{'waf_ipheader'},
                   15080:                        trusted  => $domdefaults{'waf_trusted'},
1.1437    raeburn  15081:                        vpnint   => $domdefaults{'waf_vpnint'},
1.1438    raeburn  15082:                        vpnext   => $domdefaults{'waf_vpnext'},
1.1449    raeburn  15083:                        sslopt   => $domdefaults{'waf_sslopt'},
1.1434    raeburn  15084:                     };
                   15085:     return $proxyinfo;
                   15086: }
                   15087: 
                   15088: sub ip_match {
                   15089:     my ($ip,$pattern_str) = @_;
                   15090:     $ip=Net::CIDR::cidrvalidate($ip);
                   15091:     if ($ip) {
                   15092:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
                   15093:     }
                   15094:     return;
                   15095: }
                   15096: 
                   15097: sub get_proxy_alias {
1.1450    raeburn  15098:     my ($lonid) = @_;
                   15099:     if ($lonid eq '') {
                   15100:         $lonid = $perlvar{'lonHostID'};
                   15101:     }
                   15102:     if (!defined(&hostname($lonid))) {
                   15103:         return;
                   15104:     }
                   15105:     if ($lonid ne '') {
                   15106:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
1.1434    raeburn  15107:         if ($cached) {
                   15108:             return $alias;
                   15109:         }
1.1494    raeburn  15110:         my $dom = &host_domain($lonid);
1.1434    raeburn  15111:         if ($dom ne '') {
                   15112:             my $cachetime = 60*60*24;
                   15113:             my %domconfig =
1.1494    raeburn  15114:                 &get_dom('configuration',['wafproxy'],$dom);
1.1437    raeburn  15115:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
                   15116:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
1.1450    raeburn  15117:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
                   15118:                 }
                   15119:             }
                   15120:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
                   15121:         }
                   15122:     }
                   15123:     return;
                   15124: }
                   15125: 
                   15126: sub use_proxy_alias {
                   15127:     my ($r,$lonid) = @_;
                   15128:     my $alias = &get_proxy_alias($lonid);
                   15129:     if ($alias) {
                   15130:         my $dom = &host_domain($lonid);
                   15131:         if ($dom ne '') {
1.1467    raeburn  15132:             my $proxyinfo = &get_proxy_settings($dom);
1.1450    raeburn  15133:             my ($vpnint,$remote_ip);
                   15134:             if (ref($proxyinfo) eq 'HASH') {
                   15135:                 $vpnint = $proxyinfo->{'vpnint'};
                   15136:                 if ($vpnint) {
                   15137:                     $remote_ip = &get_requestor_ip($r,1,1);
1.1434    raeburn  15138:                 }
                   15139:             }
1.1450    raeburn  15140:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
                   15141:                 return $alias;
                   15142:             }
1.1434    raeburn  15143:         }
                   15144:     }
                   15145:     return;
                   15146: }
                   15147: 
1.1474    raeburn  15148: sub alias_sso {
1.1467    raeburn  15149:     my ($lonid) = @_;
                   15150:     if ($lonid eq '') {
                   15151:         $lonid = $perlvar{'lonHostID'};
                   15152:     }
                   15153:     if (!defined(&hostname($lonid))) {
                   15154:         return;
                   15155:     }
                   15156:     if ($lonid ne '') {
                   15157:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
                   15158:         if ($cached) {
                   15159:             return $use_alias;
                   15160:         }
1.1494    raeburn  15161:         my $dom = &host_domain($lonid);
1.1467    raeburn  15162:         if ($dom ne '') {
                   15163:             my $cachetime = 60*60*24;
                   15164:             my %domconfig =
1.1494    raeburn  15165:                 &get_dom('configuration',['wafproxy'],$dom);
1.1467    raeburn  15166:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
                   15167:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
                   15168:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
                   15169:                 }
                   15170:             }
                   15171:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
                   15172:         }
                   15173:     }
                   15174:     return;
                   15175: }
                   15176: 
1.1465    raeburn  15177: sub get_saml_landing {
                   15178:     my ($lonid) = @_;
                   15179:     if ($lonid eq '') {
                   15180:         my $defdom = &default_login_domain();
                   15181:         my @hosts = &current_machine_ids();
                   15182:         if (@hosts > 1) {
                   15183:             foreach my $hostid (@hosts) {
                   15184:                 if (&host_domain($hostid) eq $defdom) {
                   15185:                     $lonid = $hostid;
                   15186:                     last;
                   15187:                 }
                   15188:             }
                   15189:         } else {
                   15190:             $lonid = $perlvar{'lonHostID'};
                   15191:         }
                   15192:         if ($lonid) {
1.1494    raeburn  15193:             unless (&host_domain($lonid) eq $defdom) {
1.1465    raeburn  15194:                 return;
                   15195:             }
                   15196:         } else {
                   15197:             return;
                   15198:         }
                   15199:     } elsif (!defined(&hostname($lonid))) {
                   15200:         return;
                   15201:     }
                   15202:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
                   15203:     if ($cached) {
                   15204:         return $landing;
                   15205:     }
1.1494    raeburn  15206:     my $dom = &host_domain($lonid);
1.1465    raeburn  15207:     if ($dom ne '') {
                   15208:         my $cachetime = 60*60*24;
                   15209:         my %domconfig =
1.1494    raeburn  15210:             &get_dom('configuration',['login'],$dom);
1.1465    raeburn  15211:         if (ref($domconfig{'login'}) eq 'HASH') {
                   15212:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
                   15213:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
                   15214:                     $landing = 1;
                   15215:                 }
                   15216:             }
                   15217:         }
                   15218:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
                   15219:     }
                   15220:     return;
                   15221: }
                   15222: 
1.31      www      15223: # ------------------------------------------------------------- Declutters URLs
                   15224: 
                   15225: sub declutter {
                   15226:     my $thisfn=shift;
1.569     albertel 15227:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261    raeburn  15228:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
                   15229:         $thisfn=~s{^/home/httpd/html}{};
                   15230:     }
1.31      www      15231:     $thisfn=~s/^\///;
1.697     albertel 15232:     $thisfn=~s|^adm/wrapper/||;
                   15233:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      15234:     $thisfn=~s/^res\///;
1.1172    bisitz   15235:     $thisfn=~s/^priv\///;
1.1032    raeburn  15236:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   15237:         $thisfn=~s/\?.+$//;
                   15238:     }
1.268     www      15239:     return $thisfn;
                   15240: }
                   15241: 
                   15242: # ------------------------------------------------------------- Clutter up URLs
                   15243: 
                   15244: sub clutter {
                   15245:     my $thisfn='/'.&declutter(shift);
1.887     albertel 15246:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 15247: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      15248:        $thisfn='/res'.$thisfn; 
                   15249:     }
1.1031    raeburn  15250:     if ($thisfn !~m|^/adm|) {
                   15251: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 15252: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 15253: 	} else {
                   15254: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   15255: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 15256: 	    if ($embstyle eq 'ssi'
                   15257: 		|| ($embstyle eq 'hdn')
                   15258: 		|| ($embstyle eq 'rat')
                   15259: 		|| ($embstyle eq 'prv')
                   15260: 		|| ($embstyle eq 'ign')) {
                   15261: 		#do nothing with these
                   15262: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 15263: 		|| ($embstyle eq 'emb')
                   15264: 		|| ($embstyle eq 'wrp')) {
                   15265: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 15266: 	    } elsif ($embstyle eq 'unk'
                   15267: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 15268: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 15269: 	    } else {
1.718     www      15270: #		&logthis("Got a blank emb style");
1.695     albertel 15271: 	    }
1.694     albertel 15272: 	}
1.1343    raeburn  15273:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298    raeburn  15274:         $thisfn='/adm/wrapper'.$thisfn;
1.694     albertel 15275:     }
1.31      www      15276:     return $thisfn;
1.12      www      15277: }
                   15278: 
1.787     albertel 15279: sub clutter_with_no_wrapper {
                   15280:     my $uri = &clutter(shift);
                   15281:     if ($uri =~ m-^/adm/-) {
                   15282: 	$uri =~ s-^/adm/wrapper/-/-;
                   15283: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   15284:     }
                   15285:     return $uri;
                   15286: }
                   15287: 
1.557     albertel 15288: sub freeze_escape {
                   15289:     my ($value)=@_;
                   15290:     if (ref($value)) {
                   15291: 	$value=&nfreeze($value);
                   15292: 	return '__FROZEN__'.&escape($value);
                   15293:     }
                   15294:     return &escape($value);
                   15295: }
                   15296: 
1.11      www      15297: 
1.557     albertel 15298: sub thaw_unescape {
                   15299:     my ($value)=@_;
                   15300:     if ($value =~ /^__FROZEN__/) {
                   15301: 	substr($value,0,10,undef);
                   15302: 	$value=&unescape($value);
                   15303: 	return &thaw($value);
                   15304:     }
                   15305:     return &unescape($value);
                   15306: }
                   15307: 
1.436     albertel 15308: sub correct_line_ends {
                   15309:     my ($result)=@_;
                   15310:     $$result =~s/\r\n/\n/mg;
                   15311:     $$result =~s/\r/\n/mg;
1.415     albertel 15312: }
1.1       albertel 15313: # ================================================================ Main Program
                   15314: 
1.184     www      15315: sub goodbye {
1.204     albertel 15316:    &logthis("Starting Shut down");
1.443     albertel 15317: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 15318:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 15319: #converted
1.599     albertel 15320: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 15321:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   15322: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   15323: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 15324: #1.1 only
1.870     albertel 15325: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   15326: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   15327: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   15328: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   15329:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 15330:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   15331:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      15332:    &flushcourselogs();
                   15333:    &logthis("Shutting down");
                   15334: }
                   15335: 
1.852     albertel 15336: sub get_dns {
1.1210    raeburn  15337:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 15338:     if (!$ignore_cache) {
                   15339: 	my ($content,$cached)=
1.1494    raeburn  15340: 	    &is_cached_new('dns',$url);
1.869     albertel 15341: 	if ($cached) {
1.1210    raeburn  15342: 	    &$func($content,$hashref);
1.869     albertel 15343: 	    return;
                   15344: 	}
                   15345:     }
                   15346: 
                   15347:     my %alldns;
1.1379    raeburn  15348:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
                   15349:         foreach my $dns (<$config>) {
                   15350: 	    next if ($dns !~ /^\^(\S*)/x);
                   15351:             my $line = $1;
                   15352:             my ($host,$protocol) = split(/:/,$line);
                   15353:             if ($protocol ne 'https') {
                   15354:                 $protocol = 'http';
                   15355:             }
                   15356: 	    $alldns{$host} = $protocol;
1.979     raeburn  15357:         }
1.1379    raeburn  15358:         close($config);
1.869     albertel 15359:     }
                   15360:     while (%alldns) {
1.1263    raeburn  15361: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1456    raeburn  15362:         my ($contents,@content);
                   15363:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
                   15364:             my $command = (split('/',$url))[3];
                   15365:             my ($dir,$file) = &parse_getdns_url($command,$url);
                   15366:             delete($alldns{$dns});
                   15367:             next if (($dir eq '') || ($file eq ''));
                   15368:             if (open(my $config,'<',"$dir/$file")) {
1.1457    raeburn  15369:                 @content = <$config>;
1.1456    raeburn  15370:                 close($config);
                   15371:             }
                   15372:             if ($url eq '/adm/dns/loncapaCRL') {
                   15373:                 $contents = join('',@content);
                   15374:             }
                   15375:         } else {
                   15376: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
                   15377:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
                   15378:             delete($alldns{$dns});
                   15379: 	    next if ($response->is_error());
                   15380:             if ($url eq '/adm/dns/loncapaCRL') {
                   15381:                 $contents = $response->content;
                   15382:             } else {
                   15383:                 @content = split("\n",$response->content);
                   15384:             }
                   15385:         }
1.1379    raeburn  15386:         if ($url eq '/adm/dns/loncapaCRL') {
1.1456    raeburn  15387:             return &$func($contents);
1.1379    raeburn  15388:         } else {
                   15389: 	    unless ($nocache) {
                   15390: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
                   15391: 	    }
                   15392: 	    &$func(\@content,$hashref);
                   15393:             return;
                   15394:         }
                   15395:     }
                   15396:     my $which = (split('/',$url,4))[3];
                   15397:     if ($which eq 'loncapaCRL') {
                   15398:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
                   15399:         if (-e $diskfile) {
                   15400:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
                   15401:         } else {
                   15402:             &logthis("unable to contact DNS, no on disk file $diskfile available");
                   15403:         }
                   15404:     } else {
                   15405:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   15406:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
                   15407:             my @content = <$config>;
                   15408:             close($config);
                   15409:             &$func(\@content,$hashref);
                   15410:         }
1.852     albertel 15411:     }
1.1210    raeburn  15412:     return;
                   15413: }
                   15414: 
                   15415: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  15416: sub parse_dns_checksums_tab {
1.1210    raeburn  15417:     my ($lines,$hashref) = @_;
1.1264    raeburn  15418:     my $lonhost = $perlvar{'lonHostID'};
1.1494    raeburn  15419:     my $machine_dom = &host_domain($lonhost);
1.1210    raeburn  15420:     my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264    raeburn  15421:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
                   15422:     my $webconfdir = '/etc/httpd/conf';
                   15423:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
                   15424:         $webconfdir = '/etc/apache2';
                   15425:     } elsif ($distro =~ /^sles(\d+)$/) {
                   15426:         if ($1 >= 10) {
                   15427:             $webconfdir = '/etc/apache2';
                   15428:         }
                   15429:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
                   15430:         if ($1 >= 10.0) {
                   15431:             $webconfdir = '/etc/apache2';
                   15432:         }
                   15433:     }
1.1210    raeburn  15434:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   15435:     my (%chksum,%revnum);
                   15436:     if (ref($lines) eq 'ARRAY') {
                   15437:         chomp(@{$lines});
1.1238    raeburn  15438:         my $version = shift(@{$lines});
                   15439:         if ($version eq $release) {  
1.1210    raeburn  15440:             foreach my $line (@{$lines}) {
1.1238    raeburn  15441:                 my ($file,$version,$shasum) = split(/,/,$line);
1.1264    raeburn  15442:                 if ($file =~ m{^/etc/httpd/conf}) {
                   15443:                     if ($webconfdir eq '/etc/apache2') {
                   15444:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
                   15445:                     }
                   15446:                 }
1.1238    raeburn  15447:                 $chksum{$file} = $shasum;
                   15448:                 $revnum{$file} = $version;
1.1210    raeburn  15449:             }
                   15450:             if (ref($hashref) eq 'HASH') {
                   15451:                 %{$hashref} = (
                   15452:                                 sums     => \%chksum,
                   15453:                                 versions => \%revnum,
                   15454:                               );
                   15455:             }
                   15456:         }
                   15457:     }
1.869     albertel 15458:     return;
1.852     albertel 15459: }
1.1210    raeburn  15460: 
                   15461: sub fetch_dns_checksums {
1.1238    raeburn  15462:     my %checksums;
1.1494    raeburn  15463:     my $machine_dom = &host_domain($perlvar{'lonHostID'});
1.1260    raeburn  15464:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238    raeburn  15465:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   15466:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211    raeburn  15467:              \%checksums);
1.1210    raeburn  15468:     return \%checksums;
                   15469: }
                   15470: 
1.1379    raeburn  15471: sub fetch_crl_pemfile {
                   15472:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
                   15473: }
                   15474: 
                   15475: sub save_crl_pem {
1.1456    raeburn  15476:     my ($content) = @_;
1.1380    raeburn  15477:     my ($msg,$hadchanges);
1.1456    raeburn  15478:     if ($content ne '') {
1.1379    raeburn  15479:         my $now = time;
                   15480:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
                   15481:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
                   15482:         if (open(my $fh,'>',"$tmpcrl")) {
1.1456    raeburn  15483:             print $fh $content;
1.1379    raeburn  15484:             close($fh);
                   15485:             if (-e $lonca) {
                   15486:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
                   15487:                     my $check = <PIPE>;
                   15488:                     close(PIPE);
                   15489:                     chomp($check);
                   15490:                     if ($check eq 'verify OK') {
                   15491:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
1.1380    raeburn  15492:                         my $backup;
1.1379    raeburn  15493:                         if (-e $dest) {
1.1380    raeburn  15494:                             if (&File::Copy::move($dest,"$dest.bak")) {
                   15495:                                 $backup = 'ok';
                   15496:                             }
1.1379    raeburn  15497:                         }
                   15498:                         if (&File::Copy::move($tmpcrl,$dest)) {
                   15499:                             $msg = 'ok';
1.1380    raeburn  15500:                             if ($backup) {
                   15501:                                 my (%oldnums,%newnums);
                   15502:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
                   15503:                                     while (<PIPE>) {
                   15504:                                         $oldnums{(split(/:/))[1]} = 1;
                   15505:                                     }
                   15506:                                     close(PIPE);
                   15507:                                 }
                   15508:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
                   15509:                                     while(<PIPE>) {
                   15510:                                         $newnums{(split(/:/))[1]} = 1;
                   15511:                                     }
                   15512:                                     close(PIPE);
                   15513:                                 }
                   15514:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
                   15515:                                     unless (exists($oldnums{$key})) {
                   15516:                                         $hadchanges = 1;
                   15517:                                         last;
                   15518:                                     }
                   15519:                                 }
                   15520:                                 unless ($hadchanges) {
                   15521:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
                   15522:                                         unless (exists($newnums{$key})) {
                   15523:                                             $hadchanges = 1;
                   15524:                                             last;
                   15525:                                         }
                   15526:                                     }
                   15527:                                 }
                   15528:                             }
1.1379    raeburn  15529:                         }
                   15530:                     } else {
                   15531:                         unlink($tmpcrl);
                   15532:                     }
                   15533:                 } else {
                   15534:                     unlink($tmpcrl);
                   15535:                 }
                   15536:             } else {
                   15537:                 unlink($tmpcrl);
                   15538:             }
                   15539:         }
                   15540:     }
1.1380    raeburn  15541:     return ($msg,$hadchanges);
1.1379    raeburn  15542: }
                   15543: 
1.1456    raeburn  15544: sub parse_getdns_url {
                   15545:     my ($command,$url) = @_;
                   15546:     my $dir = $perlvar{'lonTabDir'};
                   15547:     my $file;
                   15548:     if ($command eq 'hosts') {
                   15549:         $file = 'dns_hosts.tab';
                   15550:     } elsif ($command eq 'domain') {
                   15551:         $file = 'dns_domain.tab';
                   15552:     } elsif ($command eq 'checksums') {
                   15553:         my $version = (split('/',$url))[4];
                   15554:         $file = "dns_checksums/$version.tab",
                   15555:     } elsif ($command eq 'loncapaCRL') {
                   15556:         $dir = $perlvar{'lonCertificateDirectory'};
                   15557:         $file = $perlvar{'lonnetCertRevocationList'};
                   15558:     }
                   15559:     return ($dir,$file);
                   15560: }
                   15561: 
1.327     albertel 15562: # ------------------------------------------------------------ Read domain file
                   15563: {
1.852     albertel 15564:     my $loaded;
1.846     albertel 15565:     my %domain;
                   15566: 
1.852     albertel 15567:     sub parse_domain_tab {
                   15568: 	my ($lines) = @_;
                   15569: 	foreach my $line (@$lines) {
                   15570: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      15571: 
1.846     albertel 15572: 	    chomp($line);
1.852     albertel 15573: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 15574: 	    my %this_domain;
                   15575: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   15576: 			       'lang_def', 'city', 'longi', 'lati',
                   15577: 			       'primary') {
                   15578: 		$this_domain{$field} = shift(@elements);
                   15579: 	    }
                   15580: 	    $domain{$name} = \%this_domain;
1.852     albertel 15581: 	}
                   15582:     }
1.864     albertel 15583: 
                   15584:     sub reset_domain_info {
                   15585: 	undef($loaded);
                   15586: 	undef(%domain);
                   15587:     }
                   15588: 
1.852     albertel 15589:     sub load_domain_tab {
1.1293    raeburn  15590: 	my ($ignore_cache,$nocache) = @_;
                   15591: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852     albertel 15592: 	my $fh;
1.1359    raeburn  15593: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852     albertel 15594: 	    my @lines = <$fh>;
                   15595: 	    &parse_domain_tab(\@lines);
1.448     albertel 15596: 	}
1.852     albertel 15597: 	close($fh);
                   15598: 	$loaded = 1;
1.327     albertel 15599:     }
1.846     albertel 15600: 
                   15601:     sub domain {
1.852     albertel 15602: 	&load_domain_tab() if (!$loaded);
                   15603: 
1.846     albertel 15604: 	my ($name,$what) = @_;
                   15605: 	return if ( !exists($domain{$name}) );
                   15606: 
                   15607: 	if (!$what) {
                   15608: 	    return $domain{$name}{'description'};
                   15609: 	}
                   15610: 	return $domain{$name}{$what};
                   15611:     }
1.974     raeburn  15612: 
                   15613:     sub domain_info {
                   15614:         &load_domain_tab() if (!$loaded);
                   15615:         return %domain;
                   15616:     }
                   15617: 
1.327     albertel 15618: }
                   15619: 
                   15620: 
1.1       albertel 15621: # ------------------------------------------------------------- Read hosts file
                   15622: {
1.838     albertel 15623:     my %hostname;
1.844     albertel 15624:     my %hostdom;
1.845     albertel 15625:     my %libserv;
1.852     albertel 15626:     my $loaded;
1.888     albertel 15627:     my %name_to_host;
1.1074    raeburn  15628:     my %internetdom;
1.1107    raeburn  15629:     my %LC_dns_serv;
1.852     albertel 15630: 
                   15631:     sub parse_hosts_tab {
                   15632: 	my ($file) = @_;
                   15633: 	foreach my $configline (@$file) {
                   15634: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  15635:             chomp($configline);
                   15636: 	    if ($configline =~ /^\^/) {
                   15637:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   15638:                     $LC_dns_serv{$1} = 1;
                   15639:                 }
                   15640:                 next;
                   15641:             }
1.1074    raeburn  15642: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 15643: 	    $name=~s/\s//g;
                   15644: 	    if ($id && $domain && $role && $name) {
1.1351    raeburn  15645:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
                   15646:                     my $curr = $hostname{$id};
                   15647:                     my $skip;
                   15648:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
                   15649:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
                   15650:                             $skip = 1;
                   15651:                         } else {
                   15652:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
                   15653:                         }
                   15654:                     }
                   15655:                     unless ($skip) {
                   15656:                         push(@{$name_to_host{$name}},$id);
                   15657:                     }
                   15658:                 } else {
                   15659:                     push(@{$name_to_host{$name}},$id);
                   15660:                 }
1.852     albertel 15661: 		$hostname{$id}=$name;
                   15662: 		$hostdom{$id}=$domain;
                   15663: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  15664:                 if (defined($protocol)) {
                   15665:                     if ($protocol eq 'https') {
                   15666:                         $protocol{$id} = $protocol;
                   15667:                     } else {
                   15668:                         $protocol{$id} = 'http'; 
                   15669:                     }
1.968     raeburn  15670:                 } else {
1.969     raeburn  15671:                     $protocol{$id} = 'http';
1.968     raeburn  15672:                 }
1.1074    raeburn  15673:                 if (defined($intdom)) {
                   15674:                     $internetdom{$id} = $intdom;
                   15675:                 }
1.852     albertel 15676: 	    }
                   15677: 	}
                   15678:     }
1.864     albertel 15679:     
                   15680:     sub reset_hosts_info {
1.897     albertel 15681: 	&purge_remembered();
1.864     albertel 15682: 	&reset_domain_info();
                   15683: 	&reset_hosts_ip_info();
1.1339    raeburn  15684:         undef(%internetdom);
1.892     albertel 15685: 	undef(%name_to_host);
1.864     albertel 15686: 	undef(%hostname);
                   15687: 	undef(%hostdom);
                   15688: 	undef(%libserv);
                   15689: 	undef($loaded);
                   15690:     }
1.1       albertel 15691: 
1.852     albertel 15692:     sub load_hosts_tab {
1.1293    raeburn  15693: 	my ($ignore_cache,$nocache) = @_;
                   15694: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1359    raeburn  15695: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852     albertel 15696: 	my @config = <$config>;
                   15697: 	&parse_hosts_tab(\@config);
                   15698: 	close($config);
                   15699: 	$loaded=1;
1.1       albertel 15700:     }
1.852     albertel 15701: 
1.838     albertel 15702:     sub hostname {
1.852     albertel 15703: 	&load_hosts_tab() if (!$loaded);
                   15704: 
1.838     albertel 15705: 	my ($lonid) = @_;
                   15706: 	return $hostname{$lonid};
                   15707:     }
1.845     albertel 15708: 
1.838     albertel 15709:     sub all_hostnames {
1.852     albertel 15710: 	&load_hosts_tab() if (!$loaded);
                   15711: 
1.838     albertel 15712: 	return %hostname;
                   15713:     }
1.845     albertel 15714: 
1.888     albertel 15715:     sub all_names {
1.1293    raeburn  15716:         my ($ignore_cache,$nocache) = @_;
                   15717: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888     albertel 15718: 
                   15719: 	return %name_to_host;
                   15720:     }
                   15721: 
1.974     raeburn  15722:     sub all_host_domain {
                   15723:         &load_hosts_tab() if (!$loaded);
                   15724:         return %hostdom;
                   15725:     }
                   15726: 
1.1339    raeburn  15727:     sub all_host_intdom {
                   15728:         &load_hosts_tab() if (!$loaded);
                   15729:         return %internetdom;
                   15730:     }
                   15731: 
1.845     albertel 15732:     sub is_library {
1.852     albertel 15733: 	&load_hosts_tab() if (!$loaded);
                   15734: 
1.845     albertel 15735: 	return exists($libserv{$_[0]});
                   15736:     }
                   15737: 
                   15738:     sub all_library {
1.852     albertel 15739: 	&load_hosts_tab() if (!$loaded);
                   15740: 
1.845     albertel 15741: 	return %libserv;
                   15742:     }
                   15743: 
1.1062    droeschl 15744:     sub unique_library {
                   15745: 	#2x reverse removes all hostnames that appear more than once
                   15746:         my %unique = reverse &all_library();
                   15747:         return reverse %unique;
                   15748:     }
                   15749: 
1.841     albertel 15750:     sub get_servers {
1.852     albertel 15751: 	&load_hosts_tab() if (!$loaded);
                   15752: 
1.841     albertel 15753: 	my ($domain,$type) = @_;
                   15754: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   15755: 	                                          : %hostname;
                   15756: 	my %result;
1.842     albertel 15757: 	if (ref($domain) eq 'ARRAY') {
                   15758: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 15759: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 15760: 		    $result{$host} = $hostname;
                   15761: 		}
                   15762: 	    }
                   15763: 	} else {
                   15764: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   15765: 		if ($hostdom{$host} eq $domain) {
                   15766: 		    $result{$host} = $hostname;
                   15767: 		}
1.841     albertel 15768: 	    }
                   15769: 	}
                   15770: 	return %result;
                   15771:     }
1.845     albertel 15772: 
1.1062    droeschl 15773:     sub get_unique_servers {
                   15774:         my %unique = reverse &get_servers(@_);
                   15775: 	return reverse %unique;
                   15776:     }
                   15777: 
1.844     albertel 15778:     sub host_domain {
1.852     albertel 15779: 	&load_hosts_tab() if (!$loaded);
                   15780: 
1.844     albertel 15781: 	my ($lonid) = @_;
                   15782: 	return $hostdom{$lonid};
                   15783:     }
                   15784: 
1.841     albertel 15785:     sub all_domains {
1.852     albertel 15786: 	&load_hosts_tab() if (!$loaded);
                   15787: 
1.841     albertel 15788: 	my %seen;
                   15789: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   15790: 	return @uniq;
                   15791:     }
1.1074    raeburn  15792: 
                   15793:     sub internet_dom {
                   15794:         &load_hosts_tab() if (!$loaded);
                   15795: 
                   15796:         my ($lonid) = @_;
                   15797:         return $internetdom{$lonid};
                   15798:     }
1.1107    raeburn  15799: 
                   15800:     sub is_LC_dns {
                   15801:         &load_hosts_tab() if (!$loaded);
                   15802: 
                   15803:         my ($hostname) = @_;
                   15804:         return exists($LC_dns_serv{$hostname});
                   15805:     }
                   15806: 
1.1       albertel 15807: }
                   15808: 
1.847     albertel 15809: { 
                   15810:     my %iphost;
1.856     albertel 15811:     my %name_to_ip;
                   15812:     my %lonid_to_ip;
1.869     albertel 15813: 
1.847     albertel 15814:     sub get_hosts_from_ip {
                   15815: 	my ($ip) = @_;
                   15816: 	my %iphosts = &get_iphost();
                   15817: 	if (ref($iphosts{$ip})) {
                   15818: 	    return @{$iphosts{$ip}};
                   15819: 	}
                   15820: 	return;
1.839     albertel 15821:     }
1.864     albertel 15822:     
                   15823:     sub reset_hosts_ip_info {
                   15824: 	undef(%iphost);
                   15825: 	undef(%name_to_ip);
                   15826: 	undef(%lonid_to_ip);
                   15827:     }
1.856     albertel 15828: 
                   15829:     sub get_host_ip {
                   15830: 	my ($lonid) = @_;
                   15831: 	if (exists($lonid_to_ip{$lonid})) {
                   15832: 	    return $lonid_to_ip{$lonid};
                   15833: 	}
                   15834: 	my $name=&hostname($lonid);
                   15835:    	my $ip = gethostbyname($name);
                   15836: 	return if (!$ip || length($ip) ne 4);
                   15837: 	$ip=inet_ntoa($ip);
                   15838: 	$name_to_ip{$name}   = $ip;
                   15839: 	$lonid_to_ip{$lonid} = $ip;
                   15840: 	return $ip;
                   15841:     }
1.847     albertel 15842:     
                   15843:     sub get_iphost {
1.1293    raeburn  15844: 	my ($ignore_cache,$nocache) = @_;
1.894     albertel 15845: 
1.869     albertel 15846: 	if (!$ignore_cache) {
                   15847: 	    if (%iphost) {
                   15848: 		return %iphost;
                   15849: 	    }
                   15850: 	    my ($ip_info,$cached)=
1.1494    raeburn  15851: 		&is_cached_new('iphost','iphost');
1.869     albertel 15852: 	    if ($cached) {
                   15853: 		%iphost      = %{$ip_info->[0]};
                   15854: 		%name_to_ip  = %{$ip_info->[1]};
                   15855: 		%lonid_to_ip = %{$ip_info->[2]};
                   15856: 		return %iphost;
                   15857: 	    }
                   15858: 	}
1.894     albertel 15859: 
                   15860: 	# get yesterday's info for fallback
                   15861: 	my %old_name_to_ip;
                   15862: 	my ($ip_info,$cached)=
1.1495    raeburn  15863: 	    &is_cached_new('iphost','iphost');
1.894     albertel 15864: 	if ($cached) {
                   15865: 	    %old_name_to_ip = %{$ip_info->[1]};
                   15866: 	}
                   15867: 
1.1293    raeburn  15868: 	my %name_to_host = &all_names($ignore_cache,$nocache);
1.888     albertel 15869: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 15870: 	    my $ip;
                   15871: 	    if (!exists($name_to_ip{$name})) {
                   15872: 		$ip = gethostbyname($name);
                   15873: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 15874: 		    if (defined($old_name_to_ip{$name})) {
                   15875: 			$ip = $old_name_to_ip{$name};
                   15876: 			&logthis("Can't find $name defaulting to old $ip");
                   15877: 		    } else {
                   15878: 			&logthis("Name $name no IP found");
                   15879: 			next;
                   15880: 		    }
                   15881: 		} else {
                   15882: 		    $ip=inet_ntoa($ip);
1.847     albertel 15883: 		}
                   15884: 		$name_to_ip{$name} = $ip;
                   15885: 	    } else {
                   15886: 		$ip = $name_to_ip{$name};
1.653     albertel 15887: 	    }
1.888     albertel 15888: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   15889: 		$lonid_to_ip{$id} = $ip;
                   15890: 	    }
                   15891: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 15892: 	}
1.1293    raeburn  15893:         unless ($nocache) {
                   15894: 	    &do_cache_new('iphost','iphost',
                   15895: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   15896: 		          48*60*60);
                   15897:         }
1.869     albertel 15898: 
1.847     albertel 15899: 	return %iphost;
1.598     albertel 15900:     }
                   15901: 
1.992     raeburn  15902:     #
                   15903:     #  Given a DNS returns the loncapa host name for that DNS 
                   15904:     # 
                   15905:     sub host_from_dns {
                   15906:         my ($dns) = @_;
                   15907:         my @hosts;
                   15908:         my $ip;
                   15909: 
1.993     raeburn  15910:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  15911:             $ip = $name_to_ip{$dns};
                   15912:         }
                   15913:         if (!$ip) {
                   15914:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   15915:             if (length($ip) == 4) { 
                   15916: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   15917:             }
                   15918:         }
                   15919:         if ($ip) {
                   15920: 	    @hosts = get_hosts_from_ip($ip);
                   15921: 	    return $hosts[0];
                   15922:         }
                   15923:         return undef;
1.986     foxr     15924:     }
1.992     raeburn  15925: 
1.1074    raeburn  15926:     sub get_internet_names {
                   15927:         my ($lonid) = @_;
                   15928:         return if ($lonid eq '');
                   15929:         my ($idnref,$cached)=
1.1494    raeburn  15930:             &is_cached_new('internetnames',$lonid);
1.1074    raeburn  15931:         if ($cached) {
                   15932:             return $idnref;
                   15933:         }
                   15934:         my $ip = &get_host_ip($lonid);
                   15935:         my @hosts = &get_hosts_from_ip($ip);
                   15936:         my %iphost = &get_iphost();
                   15937:         my (@idns,%seen);
                   15938:         foreach my $id (@hosts) {
                   15939:             my $dom = &host_domain($id);
                   15940:             my $prim_id = &domain($dom,'primary');
                   15941:             my $prim_ip = &get_host_ip($prim_id);
                   15942:             next if ($seen{$prim_ip});
                   15943:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   15944:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   15945:                     my $intdom = &internet_dom($id);
                   15946:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   15947:                         push(@idns,$intdom);
                   15948:                     }
                   15949:                 }
                   15950:             }
                   15951:             $seen{$prim_ip} = 1;
                   15952:         }
1.1219    raeburn  15953:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  15954:     }
                   15955: 
1.986     foxr     15956: }
                   15957: 
1.1079    raeburn  15958: sub all_loncaparevs {
1.1249    raeburn  15959:     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  15960: }
                   15961: 
1.1227    raeburn  15962: # ---------------------------------------------------------- Read loncaparev table
                   15963: {
                   15964:     sub load_loncaparevs { 
                   15965:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1359    raeburn  15966:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1227    raeburn  15967:                 while (my $configline=<$config>) {
                   15968:                     chomp($configline);
                   15969:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   15970:                     $loncaparevs{$hostid}=$loncaparev;
                   15971:                 }
                   15972:                 close($config);
                   15973:             }
                   15974:         }
                   15975:     }
                   15976: }
                   15977: 
                   15978: # ---------------------------------------------------------- Read serverhostID table
                   15979: {
                   15980:     sub load_serverhomeIDs {
                   15981:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1359    raeburn  15982:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1227    raeburn  15983:                 while (my $configline=<$config>) {
                   15984:                     chomp($configline);
                   15985:                     my ($name,$id)=split(/:/,$configline);
                   15986:                     $serverhomeIDs{$name}=$id;
                   15987:                 }
                   15988:                 close($config);
                   15989:             }
                   15990:         }
                   15991:     }
                   15992: }
                   15993: 
                   15994: 
1.862     albertel 15995: BEGIN {
                   15996: 
                   15997: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   15998:     unless ($readit) {
                   15999: {
                   16000:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   16001:     %perlvar = (%perlvar,%{$configvars});
                   16002: }
                   16003: 
                   16004: 
1.1       albertel 16005: # ------------------------------------------------------ Read spare server file
                   16006: {
1.1359    raeburn  16007:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 16008: 
                   16009:     while (my $configline=<$config>) {
                   16010:        chomp($configline);
1.284     matthew  16011:        if ($configline) {
1.784     albertel 16012: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 16013: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 16014: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 16015:        }
                   16016:     }
1.448     albertel 16017:     close($config);
1.1       albertel 16018: }
1.11      www      16019: # ------------------------------------------------------------ Read permissions
                   16020: {
1.1359    raeburn  16021:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11      www      16022: 
                   16023:     while (my $configline=<$config>) {
1.448     albertel 16024: 	chomp($configline);
                   16025: 	if ($configline) {
                   16026: 	    my ($role,$perm)=split(/ /,$configline);
                   16027: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   16028: 	}
1.11      www      16029:     }
1.448     albertel 16030:     close($config);
1.11      www      16031: }
                   16032: 
                   16033: # -------------------------------------------- Read plain texts for permissions
                   16034: {
1.1359    raeburn  16035:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      16036: 
                   16037:     while (my $configline=<$config>) {
1.448     albertel 16038: 	chomp($configline);
                   16039: 	if ($configline) {
1.742     raeburn  16040: 	    my ($short,@plain)=split(/:/,$configline);
                   16041:             %{$prp{$short}} = ();
                   16042: 	    if (@plain > 0) {
                   16043:                 $prp{$short}{'std'} = $plain[0];
                   16044:                 for (my $i=1; $i<@plain; $i++) {
                   16045:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   16046:                 }
                   16047:             }
1.448     albertel 16048: 	}
1.135     www      16049:     }
1.448     albertel 16050:     close($config);
1.135     www      16051: }
                   16052: 
                   16053: # ---------------------------------------------------------- Read package table
                   16054: {
1.1359    raeburn  16055:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135     www      16056: 
                   16057:     while (my $configline=<$config>) {
1.483     albertel 16058: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 16059: 	chomp($configline);
                   16060: 	my ($short,$plain)=split(/:/,$configline);
                   16061: 	my ($pack,$name)=split(/\&/,$short);
                   16062: 	if ($plain ne '') {
                   16063: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   16064: 	    $packagetab{$short}=$plain; 
                   16065: 	}
1.11      www      16066:     }
1.448     albertel 16067:     close($config);
1.329     matthew  16068: }
                   16069: 
1.1073    raeburn  16070: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  16071: 
                   16072: &load_loncaparevs();
1.1073    raeburn  16073: 
1.1074    raeburn  16074: # ---------------------------------------------------------- Read serverhostID table
                   16075: 
1.1227    raeburn  16076: &load_serverhomeIDs();
                   16077: 
                   16078: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  16079: {
                   16080:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   16081:     if (-e $file) {
                   16082:         my $parser = HTML::LCParser->new($file);
                   16083:         while (my $token = $parser->get_token()) {
                   16084:             if ($token->[0] eq 'S') {
                   16085:                 my $item = $token->[1];
                   16086:                 my $name = $token->[2]{'name'};
                   16087:                 my $value = $token->[2]{'value'};
1.1285    raeburn  16088:                 my $valuematch = $token->[2]{'valuematch'};
1.1303    raeburn  16089:                 my $namematch = $token->[2]{'namematch'};
                   16090:                 if ($item eq 'parameter') {
                   16091:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
                   16092:                         my $release = $parser->get_text();
                   16093:                         $release =~ s/(^\s*|\s*$ )//gx;
                   16094:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
                   16095:                     }
                   16096:                 } elsif ($item ne '' && $name ne '') {
1.1079    raeburn  16097:                     my $release = $parser->get_text();
                   16098:                     $release =~ s/(^\s*|\s*$ )//gx;
1.1303    raeburn  16099:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079    raeburn  16100:                 }
                   16101:             }
                   16102:         }
                   16103:     }
1.1073    raeburn  16104: }
                   16105: 
1.1138    raeburn  16106: # ---------------------------------------------------------- Read managers table
                   16107: {
                   16108:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1359    raeburn  16109:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138    raeburn  16110:             while (my $configline=<$config>) {
                   16111:                 chomp($configline);
                   16112:                 next if ($configline =~ /^\#/);
                   16113:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   16114:                     $managerstab{$configline} = 1;
                   16115:                 }
                   16116:             }
                   16117:             close($config);
                   16118:         }
                   16119:     }
                   16120: }
                   16121: 
1.329     matthew  16122: # ------------- set up temporary directory
                   16123: {
1.1117    foxr     16124:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  16125: 
1.11      www      16126: }
                   16127: 
1.1405    raeburn  16128: # ------------- set default texengine (domain default overrides this)
                   16129: {
                   16130:     $deftex = LONCAPA::texengine();
                   16131: }
                   16132: 
1.1416    raeburn  16133: # ------------- set default minimum length for passwords for internal auth users
                   16134: {
                   16135:     $passwdmin = LONCAPA::passwd_min();
                   16136: }
                   16137: 
1.794     albertel 16138: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   16139: 				'compress_threshold'=> 20_000,
                   16140:  			        });
1.185     www      16141: 
1.281     www      16142: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      16143: $dumpcount=0;
1.958     www      16144: $locknum=0;
1.22      www      16145: 
1.163     harris41 16146: &logtouch();
1.672     albertel 16147: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      16148: $readit=1;
1.564     albertel 16149:     {
                   16150: 	use integer;
                   16151: 	my $test=(2**32)+1;
1.568     albertel 16152: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 16153: 	&logthis(" Detected 64bit platform ($_64bit)");
                   16154:     }
1.195     www      16155: }
1.1       albertel 16156: }
1.179     www      16157: 
1.1       albertel 16158: 1;
1.191     harris41 16159: __END__
                   16160: 
1.243     albertel 16161: =pod
                   16162: 
1.191     harris41 16163: =head1 NAME
                   16164: 
1.243     albertel 16165: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 16166: 
                   16167: =head1 SYNOPSIS
                   16168: 
1.243     albertel 16169: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 16170: 
                   16171:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   16172: 
1.243     albertel 16173: Common parameters:
                   16174: 
                   16175: =over 4
                   16176: 
                   16177: =item *
                   16178: 
                   16179: $uname : an internal username (if $cname expecting a course Id specifically)
                   16180: 
                   16181: =item *
                   16182: 
                   16183: $udom : a domain (if $cdom expecting a course's domain specifically)
                   16184: 
                   16185: =item *
                   16186: 
                   16187: $symb : a resource instance identifier
                   16188: 
                   16189: =item *
                   16190: 
                   16191: $namespace : the name of a .db file that contains the data needed or
                   16192: being set.
                   16193: 
                   16194: =back
                   16195: 
1.394     bowersj2 16196: =head1 OVERVIEW
1.191     harris41 16197: 
1.394     bowersj2 16198: lonnet provides subroutines which interact with the
                   16199: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   16200: about classes, users, and resources.
1.243     albertel 16201: 
                   16202: For many of these objects you can also use this to store data about
                   16203: them or modify them in various ways.
1.191     harris41 16204: 
1.394     bowersj2 16205: =head2 Symbs
1.191     harris41 16206: 
1.394     bowersj2 16207: To identify a specific instance of a resource, LON-CAPA uses symbols
                   16208: or "symbs"X<symb>. These identifiers are built from the URL of the
                   16209: map, the resource number of the resource in the map, and the URL of
                   16210: the resource itself. The latter is somewhat redundant, but might help
                   16211: if maps change.
                   16212: 
                   16213: An example is
                   16214: 
                   16215:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   16216: 
                   16217: The respective map entry is
                   16218: 
                   16219:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   16220:   title="Problem 2">
                   16221:  </resource>
                   16222: 
                   16223: Symbs are used by the random number generator, as well as to store and
                   16224: restore data specific to a certain instance of for example a problem.
                   16225: 
                   16226: =head2 Storing And Retrieving Data
                   16227: 
                   16228: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   16229: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   16230: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   16231: is is the non-critical message twin of cstore. These functions are for
                   16232: handlers to store a perl hash to a user's permanent data space in an
                   16233: easy manner, and to retrieve it again on another call. It is expected
                   16234: that a handler would use this once at the beginning to retrieve data,
                   16235: and then again once at the end to send only the new data back.
                   16236: 
                   16237: The data is stored in the user's data directory on the user's
                   16238: homeserver under the ID of the course.
                   16239: 
                   16240: The hash that is returned by restore will have all of the previous
                   16241: value for all of the elements of the hash.
                   16242: 
                   16243: Example:
                   16244: 
                   16245:  #creating a hash
                   16246:  my %hash;
                   16247:  $hash{'foo'}='bar';
                   16248: 
                   16249:  #storing it
                   16250:  &Apache::lonnet::cstore(\%hash);
                   16251: 
                   16252:  #changing a value
                   16253:  $hash{'foo'}='notbar';
                   16254: 
                   16255:  #adding a new value
                   16256:  $hash{'bar'}='foo';
                   16257:  &Apache::lonnet::cstore(\%hash);
                   16258: 
                   16259:  #retrieving the hash
                   16260:  my %history=&Apache::lonnet::restore();
                   16261: 
                   16262:  #print the hash
                   16263:  foreach my $key (sort(keys(%history))) {
                   16264:    print("\%history{$key} = $history{$key}");
                   16265:  }
                   16266: 
                   16267: Will print out:
1.191     harris41 16268: 
1.394     bowersj2 16269:  %history{1:foo} = bar
                   16270:  %history{1:keys} = foo:timestamp
                   16271:  %history{1:timestamp} = 990455579
                   16272:  %history{2:bar} = foo
                   16273:  %history{2:foo} = notbar
                   16274:  %history{2:keys} = foo:bar:timestamp
                   16275:  %history{2:timestamp} = 990455580
                   16276:  %history{bar} = foo
                   16277:  %history{foo} = notbar
                   16278:  %history{timestamp} = 990455580
                   16279:  %history{version} = 2
                   16280: 
                   16281: Note that the special hash entries C<keys>, C<version> and
                   16282: C<timestamp> were added to the hash. C<version> will be equal to the
                   16283: total number of versions of the data that have been stored. The
                   16284: C<timestamp> attribute will be the UNIX time the hash was
                   16285: stored. C<keys> is available in every historical section to list which
                   16286: keys were added or changed at a specific historical revision of a
                   16287: hash.
                   16288: 
                   16289: B<Warning>: do not store the hash that restore returns directly. This
                   16290: will cause a mess since it will restore the historical keys as if the
                   16291: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 16292: 
1.394     bowersj2 16293: Calling convention:
1.191     harris41 16294: 
1.1225    raeburn  16295:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269    raeburn  16296:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191     harris41 16297: 
1.394     bowersj2 16298: For more detailed information, see lonnet specific documentation.
1.191     harris41 16299: 
1.394     bowersj2 16300: =head1 RETURN MESSAGES
1.191     harris41 16301: 
1.394     bowersj2 16302: =over 4
1.191     harris41 16303: 
1.394     bowersj2 16304: =item * B<con_lost>: unable to contact remote host
1.191     harris41 16305: 
1.394     bowersj2 16306: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   16307: when the connection is brought back up
1.191     harris41 16308: 
1.394     bowersj2 16309: =item * B<con_failed>: unable to contact remote host and unable to save message
                   16310: for later delivery
1.191     harris41 16311: 
1.967     bisitz   16312: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 16313: 
1.394     bowersj2 16314: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 16315: that was requested
1.191     harris41 16316: 
1.243     albertel 16317: =back
1.191     harris41 16318: 
1.243     albertel 16319: =head1 PUBLIC SUBROUTINES
1.191     harris41 16320: 
1.243     albertel 16321: =head2 Session Environment Functions
1.191     harris41 16322: 
1.243     albertel 16323: =over 4
1.191     harris41 16324: 
1.394     bowersj2 16325: =item * 
                   16326: X<appenv()>
1.949     raeburn  16327: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 16328: the user envirnoment file, and will be restored for each access this
1.620     albertel 16329: user makes during this session, also modifies the %env for the current
1.949     raeburn  16330: process. Optional rolesarrayref - if defined contains a reference to an array
                   16331: of roles which are exempt from the restriction on modifying user.role entries 
                   16332: in the user's environment.db and in %env.    
1.191     harris41 16333: 
                   16334: =item *
1.394     bowersj2 16335: X<delenv()>
1.987     raeburn  16336: B<delenv($delthis,$regexp)>: removes all items from the session
                   16337: environment file that begin with $delthis. If the 
                   16338: optional second arg - $regexp - is true, $delthis is treated as a 
                   16339: regular expression, otherwise \Q$delthis\E is used. 
                   16340: The values are also deleted from the current processes %env.
1.191     harris41 16341: 
1.795     albertel 16342: =item * get_env_multiple($name) 
                   16343: 
                   16344: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   16345: values may be defined and end up as an array ref.
                   16346: 
                   16347: returns an array of values
                   16348: 
1.243     albertel 16349: =back
                   16350: 
                   16351: =head2 User Information
1.191     harris41 16352: 
1.243     albertel 16353: =over 4
1.191     harris41 16354: 
                   16355: =item *
1.394     bowersj2 16356: X<queryauthenticate()>
                   16357: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 16358: authentication scheme
                   16359: 
                   16360: =item *
1.394     bowersj2 16361: X<authenticate()>
1.1073    raeburn  16362: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 16363: authenticate user from domain's lib servers (first use the current
                   16364: one). C<$upass> should be the users password.
1.1073    raeburn  16365: $checkdefauth is optional (value is 1 if a check should be made to
                   16366:    authenticate user using default authentication method, and allow
                   16367:    account creation if username does not have account in the domain).
                   16368: $clientcancheckhost is optional (value is 1 if checking whether the
                   16369:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 16370: 
                   16371: =item *
1.394     bowersj2 16372: X<homeserver()>
                   16373: B<homeserver($uname,$udom)>: find the server which has
                   16374: the user's directory and files (there must be only one), this caches
                   16375: the answer, and also caches if there is a borken connection.
1.191     harris41 16376: 
                   16377: =item *
1.394     bowersj2 16378: X<idget()>
1.1300    raeburn  16379: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
                   16380: a list of student/employee IDs or clicker IDs
                   16381: (student/employee IDs are a unique resource in a domain, there must be 
                   16382: only 1 ID per username, and only 1 username per ID in a specific domain).
                   16383: clickerIDs are not necessarily unique, as students might share clickers.
                   16384: (returns hash: id=>name,id=>name)
1.191     harris41 16385: 
                   16386: =item *
1.394     bowersj2 16387: X<idrget()>
                   16388: B<idrget($udom,@unames)>: find the IDs behind a list of
                   16389: usernames (returns hash: name=>id,name=>id)
1.191     harris41 16390: 
                   16391: =item *
1.394     bowersj2 16392: X<idput()>
1.1300    raeburn  16393: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
                   16394: names and associated student/employee IDs or clicker IDs.
                   16395: 
                   16396: =item *
                   16397: X<iddel()>
                   16398: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
                   16399: student/employee ID or clicker ID username look-ups from domain.
                   16400: The homeserver ($uhome) and namespace ($namespace) are optional.
                   16401: If no $uhome is provided, it will be determined usig &homeserver()
                   16402: for each user.  If no $namespace is provided, the default is ids.
                   16403: 
                   16404: =item *
                   16405: X<updateclickers()>
                   16406: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
                   16407: clicker ID-to-username look-ups in clickers.db on library server.
                   16408: Permitted actions are add or del (i.e., add or delete). The 
                   16409: clickers.db contains clickerID as keys (escaped), and each corresponding
                   16410: value is an escaped comma-separated list of usernames (for whom the
                   16411: library server is the homeserver), who registered that particular ID.
                   16412: If $critical is true, the update will be sent via &critical, otherwise
                   16413: &reply() will be used.
1.191     harris41 16414: 
                   16415: =item *
1.394     bowersj2 16416: X<rolesinit()>
1.1169    droeschl 16417: B<rolesinit($udom,$username)>: get user privileges.
                   16418: returns user role, first access and timer interval hashes
1.243     albertel 16419: 
                   16420: =item *
1.1171    droeschl 16421: X<privileged()>
                   16422: B<privileged($username,$domain)>: returns a true if user has a
                   16423: privileged and active role (i.e. su or dc), false otherwise.
                   16424: 
                   16425: =item *
1.551     albertel 16426: X<getsection()>
                   16427: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 16428: course $cname, return section name/number or '' for "not in course"
                   16429: and '-1' for "no section"
                   16430: 
                   16431: =item *
1.394     bowersj2 16432: X<userenvironment()>
                   16433: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 16434: passed in @what from the requested user's environment, returns a hash
                   16435: 
1.858     raeburn  16436: =item * 
                   16437: X<userlog_query()>
1.859     albertel 16438: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   16439: activity.log file. %filters defines filters applied when parsing the
                   16440: log file. These can be start or end timestamps, or the type of action
                   16441: - log to look for Login or Logout events, check for Checkin or
                   16442: Checkout, role for role selection. The response is in the form
                   16443: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   16444: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  16445: 
1.243     albertel 16446: =back
                   16447: 
                   16448: =head2 User Roles
                   16449: 
                   16450: =over 4
                   16451: 
                   16452: =item *
                   16453: 
1.1284    raeburn  16454: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
                   16455: returns codes for allowed actions.
                   16456: 
                   16457: The first argument is required, all others are optional.
                   16458: 
                   16459: $priv is the privilege being checked.
                   16460: $uri contains additional information about what is being checked for access (e.g.,
                   16461: URL, course ID etc.). 
                   16462: $symb is the unique resource instance identifier in a course; if needed,
                   16463: but not provided, it will be retrieved via a call to &symbread(). 
                   16464: $role is the role for which a priv is being checked (only used if priv is evb). 
                   16465: $clientip is the user's IP address (only used when checking for access to portfolio 
                   16466: files).
                   16467: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
                   16468: prevents recursive calls to &allowed.
                   16469: 
1.243     albertel 16470:  F: full access
                   16471:  U,I,K: authentication modes (cxx only)
                   16472:  '': forbidden
                   16473:  1: user needs to choose course
                   16474:  2: browse allowed
1.766     albertel 16475:  A: passphrase authentication needed
1.1284    raeburn  16476:  B: access temporarily blocked because of a blocking event in a course.
1.1402    raeburn  16477:  D: access blocked because access is required via session initiated via deep-link 
1.243     albertel 16478: 
                   16479: =item *
                   16480: 
1.1192    raeburn  16481: constructaccess($url,$setpriv) : check for access to construction space URL
                   16482: 
                   16483: See if the owner domain and name in the URL match those in the
                   16484: expected environment.  If so, return three element list
                   16485: ($ownername,$ownerdomain,$ownerhome).
                   16486: 
                   16487: Otherwise return the null string.
                   16488: 
                   16489: If second argument 'setpriv' is true, it assigns the privileges,
                   16490: and returns the same three element list, unless the owner has
                   16491: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   16492: in which case the null string is returned.
                   16493: 
                   16494: =item *
                   16495: 
1.1326    raeburn  16496: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
                   16497: define a custom role rolename set privileges in format of lonTabs/roles.tab
                   16498: for system, domain, and course level. $uname and $udom are optional (current
                   16499: user's username and domain will be used when either of $uname or $udom are absent.
1.243     albertel 16500: 
                   16501: =item *
                   16502: 
1.988     raeburn  16503: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   16504: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  16505: $type is Course (default) or Community.
1.988     raeburn  16506: If $forcedefault evaluates to true, text returned will be default 
                   16507: text for $type. Otherwise, if this is a course, the text returned 
                   16508: will be a custom name for the role (if defined in the course's 
                   16509: environment).  If no custom name is defined the default is returned.
                   16510:    
1.832     raeburn  16511: =item *
                   16512: 
1.1219    raeburn  16513: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  16514: All arguments are optional. Returns a hash of a roles, either for
                   16515: co-author/assistant author roles for a user's Construction Space
1.906     albertel 16516: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  16517: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   16518: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   16519: For each key, value is set to colon-separated start and end times for
                   16520: the role.  If no username and domain are specified, will default to
1.934     raeburn  16521: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  16522: of role statuses (active, future or previous), roles 
                   16523: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   16524: to restrict the list of roles reported. If no array ref is 
                   16525: provided for types, will default to return only active roles.
1.834     albertel 16526: 
1.1195    raeburn  16527: =item *
                   16528: 
                   16529: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  16530: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   16531: 
                   16532: Additional optional arguments are: $type (if role checking is to be restricted 
                   16533: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  16534: available roles) or future (roles available in the future), and
                   16535: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  16536: have active Domain Coordinator role in course's domain or in additional
                   16537: domains (specified in 'Domains to check for privileged users' in course
                   16538: environment -- set via:  Course Settings -> Classlists and staff listing).
                   16539: 
                   16540: =item *
                   16541: 
                   16542: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   16543: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   16544: $possdomains and $possroles are optional array refs -- to domains to check and
                   16545: roles to check.  If $possdomains is not specified, a dump will be done of the
                   16546: users' roles.db to check for a dc or su role in any domain. This can be
                   16547: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   16548: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   16549: this then allows &privileged_by_domain() to be used, which caches the identity
                   16550: of privileged users, eliminating the need for repeated calls to &dump().
                   16551: 
                   16552: =item *
                   16553: 
                   16554: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   16555: where the outer hash keys are domains specified in the $possdomains array ref,
                   16556: next inner hash keys are privileged roles specified in the $roles array ref,
                   16557: and the innermost hash contains key = value pairs for username:domain = end:start
                   16558: for active or future "privileged" users with that role in that domain. To avoid
                   16559: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   16560: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  16561: 
1.243     albertel 16562: =back
                   16563: 
                   16564: =head2 User Modification
                   16565: 
                   16566: =over 4
                   16567: 
                   16568: =item *
                   16569: 
1.957     raeburn  16570: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 16571: user for the level given by URL.  Optional start and end dates (leave empty
                   16572: string or zero for "no date")
1.191     harris41 16573: 
                   16574: =item *
                   16575: 
1.243     albertel 16576: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   16577: change a users, password, possible return values are: ok,
                   16578: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   16579: refused
1.191     harris41 16580: 
                   16581: =item *
                   16582: 
1.243     albertel 16583: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 16584: 
                   16585: =item *
                   16586: 
1.1058    raeburn  16587: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   16588:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   16589: 
                   16590: will update user information (firstname,middlename,lastname,generation,
                   16591: permanentemail), and if forceid is true, student/employee ID also.
                   16592: A user's institutional affiliation(s) can also be updated.
                   16593: User information fields will not be overwritten with empty entries 
                   16594: unless the field is included in the $candelete array reference.
                   16595: This array is included when a single user is modified via "Manage Users",
                   16596: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 16597: 
                   16598: =item *
                   16599: 
1.286     matthew  16600: modifystudent
                   16601: 
1.957     raeburn  16602: modify a student's enrollment and identification information.
1.1235    raeburn  16603: The course id is resolved based on the current user's environment.  
                   16604: This means the invoking user must be a course coordinator or otherwise
1.286     matthew  16605: associated with a course.
                   16606: 
1.297     matthew  16607: This call is essentially a wrapper for lonnet::modifyuser and
                   16608: lonnet::modify_student_enrollment
1.286     matthew  16609: 
                   16610: Inputs: 
                   16611: 
                   16612: =over 4
                   16613: 
1.957     raeburn  16614: =item B<$udom> Student's loncapa domain
1.286     matthew  16615: 
1.957     raeburn  16616: =item B<$uname> Student's loncapa login name
1.286     matthew  16617: 
1.964     bisitz   16618: =item B<$uid> Student/Employee ID
1.286     matthew  16619: 
1.957     raeburn  16620: =item B<$umode> Student's authentication mode
1.286     matthew  16621: 
1.957     raeburn  16622: =item B<$upass> Student's password
1.286     matthew  16623: 
1.957     raeburn  16624: =item B<$first> Student's first name
1.286     matthew  16625: 
1.957     raeburn  16626: =item B<$middle> Student's middle name
1.286     matthew  16627: 
1.957     raeburn  16628: =item B<$last> Student's last name
1.286     matthew  16629: 
1.957     raeburn  16630: =item B<$gene> Student's generation
1.286     matthew  16631: 
1.957     raeburn  16632: =item B<$usec> Student's section in course
1.286     matthew  16633: 
                   16634: =item B<$end> Unix time of the roles expiration
                   16635: 
                   16636: =item B<$start> Unix time of the roles start date
                   16637: 
                   16638: =item B<$forceid> If defined, allow $uid to be changed
                   16639: 
                   16640: =item B<$desiredhome> server to use as home server for student
                   16641: 
1.957     raeburn  16642: =item B<$email> Student's permanent e-mail address
                   16643: 
                   16644: =item B<$type> Type of enrollment (auto or manual)
                   16645: 
1.963     raeburn  16646: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   16647: 
                   16648: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  16649: 
1.963     raeburn  16650: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  16651: 
1.963     raeburn  16652: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  16653: 
1.1216    raeburn  16654: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   16655: 
                   16656: =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  16657: 
1.286     matthew  16658: =back
1.297     matthew  16659: 
                   16660: =item *
                   16661: 
                   16662: modify_student_enrollment
                   16663: 
1.1235    raeburn  16664: Change a student's enrollment status in a class.  The environment variable
1.297     matthew  16665: 'role.request.course' must be defined for this function to proceed.
                   16666: 
                   16667: Inputs:
                   16668: 
                   16669: =over 4
                   16670: 
1.1235    raeburn  16671: =item $udom, student's domain
1.297     matthew  16672: 
1.1235    raeburn  16673: =item $uname, student's name
1.297     matthew  16674: 
1.1235    raeburn  16675: =item $uid, student's user id
1.297     matthew  16676: 
1.1235    raeburn  16677: =item $first, student's first name
1.297     matthew  16678: 
                   16679: =item $middle
                   16680: 
                   16681: =item $last
                   16682: 
                   16683: =item $gene
                   16684: 
                   16685: =item $usec
                   16686: 
                   16687: =item $end
                   16688: 
                   16689: =item $start
                   16690: 
1.957     raeburn  16691: =item $type
                   16692: 
                   16693: =item $locktype
                   16694: 
                   16695: =item $cid
                   16696: 
                   16697: =item $selfenroll
                   16698: 
                   16699: =item $context
                   16700: 
1.1216    raeburn  16701: =item $credits, number of credits student will earn from this class
                   16702: 
1.1314    raeburn  16703: =item $instsec, institutional course section code for student
                   16704: 
1.297     matthew  16705: =back
                   16706: 
1.191     harris41 16707: 
                   16708: =item *
                   16709: 
1.243     albertel 16710: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   16711: custom role; give a custom role to a user for the level given by URL.  Specify
                   16712: name and domain of role author, and role name
1.191     harris41 16713: 
                   16714: =item *
                   16715: 
1.243     albertel 16716: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 16717: 
                   16718: =item *
                   16719: 
1.243     albertel 16720: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   16721: 
                   16722: =back
                   16723: 
                   16724: =head2 Course Infomation
                   16725: 
                   16726: =over 4
1.191     harris41 16727: 
                   16728: =item *
                   16729: 
1.1118    foxr     16730: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 16731: specified course id, including all environment settings for the
                   16732: course, the description of the course will be in the hash under the
                   16733: key 'description'
1.191     harris41 16734: 
1.1118    foxr     16735: $options is an optional parameter that if supplied is a hash reference that controls
                   16736: what how this function works.  It has the following key/values:
                   16737: 
                   16738: =over 4
                   16739: 
                   16740: =item freshen_cache
                   16741: 
                   16742: If defined, and the environment cache for the course is valid, it is 
                   16743: returned in the returned hash.
                   16744: 
                   16745: =item one_time
                   16746: 
                   16747: If defined, the last cache time is set to _now_
                   16748: 
                   16749: =item user
                   16750: 
                   16751: If defined, the supplied username is used instead of the current user.
                   16752: 
                   16753: 
                   16754: =back
                   16755: 
1.191     harris41 16756: =item *
                   16757: 
1.624     albertel 16758: resdata($name,$domain,$type,@which) : request for current parameter
                   16759: setting for a specific $type, where $type is either 'course' or 'user',
                   16760: @what should be a list of parameters to ask about. This routine caches
1.1235    raeburn  16761: answers for 10 minutes.
1.243     albertel 16762: 
1.877     foxr     16763: =item *
                   16764: 
                   16765: get_courseresdata($courseid, $domain) : dump the entire course resource
                   16766: data base, returning a hash that is keyed by the resource name and has
                   16767: values that are the resource value.  I believe that the timestamps and
                   16768: versions are also returned.
                   16769: 
1.243     albertel 16770: =back
                   16771: 
                   16772: =head2 Course Modification
                   16773: 
                   16774: =over 4
1.191     harris41 16775: 
                   16776: =item *
                   16777: 
1.243     albertel 16778: writecoursepref($courseid,%prefs) : write preferences (environment
                   16779: database) for a course
1.191     harris41 16780: 
                   16781: =item *
                   16782: 
1.1011    raeburn  16783: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   16784: 
                   16785: =item *
                   16786: 
1.1038    raeburn  16787: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 16788: 
1.1167    droeschl 16789: =item *
                   16790: 
                   16791: is_course($courseid), is_course($cdom, $cnum)
                   16792: 
                   16793: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   16794: two component version $cdom, $cnum. It checks if the specified course exists.
                   16795: 
                   16796: Returns:
                   16797:     undef if the course doesn't exist, otherwise
                   16798:     in scalar context the combined courseid.
                   16799:     in list context the two components of the course identifier, domain and 
                   16800:     courseid.    
                   16801: 
1.243     albertel 16802: =back
                   16803: 
1.1401    raeburn  16804: =head2 Bubblesheet Configuration
                   16805: 
                   16806: =over 4
                   16807: 
                   16808: =item *
                   16809: 
                   16810: get_scantron_config($which)
                   16811: 
                   16812: $which - the name of the configuration to parse from the file.
                   16813: 
                   16814: Parses and returns the bubblesheet configuration line selected as a
                   16815: hash of configuration file fields.
                   16816: 
                   16817: 
                   16818: Returns:
                   16819:     If the named configuration is not in the file, an empty
                   16820:     hash is returned.
                   16821: 
                   16822:     a hash with the fields
                   16823:       name         - internal name for the this configuration setup
                   16824:       description  - text to display to operator that describes this config
                   16825:       CODElocation - if 0 or the string 'none'
                   16826:                           - no CODE exists for this config
                   16827:                      if -1 || the string 'letter'
                   16828:                           - a CODE exists for this config and is
                   16829:                             a string of letters
                   16830:                      Unsupported value (but planned for future support)
                   16831:                           if a positive integer
                   16832:                                - The CODE exists as the first n items from
                   16833:                                  the question section of the form
                   16834:                           if the string 'number'
                   16835:                                - The CODE exists for this config and is
                   16836:                                  a string of numbers
                   16837:       CODEstart   - (only matter if a CODE exists) column in the line where
                   16838:                      the CODE starts
                   16839:       CODElength  - length of the CODE
                   16840:       IDstart     - column where the student/employee ID starts
                   16841:       IDlength    - length of the student/employee ID info
                   16842:       Qstart      - column where the information from the bubbled
                   16843:                     'questions' start
                   16844:       Qlength     - number of columns comprising a single bubble line from
                   16845:                     the sheet. (usually either 1 or 10)
                   16846:       Qon         - either a single character representing the character used
                   16847:                     to signal a bubble was chosen in the positional setup, or
                   16848:                     the string 'letter' if the letter of the chosen bubble is
                   16849:                     in the final, or 'number' if a number representing the
                   16850:                     chosen bubble is in the file (1->A 0->J)
                   16851:       Qoff        - the character used to represent that a bubble was
                   16852:                     left blank
                   16853:       PaperID     - if the scanning process generates a unique number for each
                   16854:                     sheet scanned the column that this ID number starts in
                   16855:       PaperIDlength - number of columns that comprise the unique ID number
                   16856:                       for the sheet of paper
                   16857:       FirstName   - column that the first name starts in
                   16858:       FirstNameLength - number of columns that the first name spans
                   16859:       LastName    - column that the last name starts in
                   16860:       LastNameLength - number of columns that the last name spans
                   16861:       BubblesPerRow - number of bubbles available in each row used to
                   16862:                       bubble an answer. (If not specified, 10 assumed).
                   16863: 
                   16864: 
                   16865: =item *
                   16866: 
                   16867: get_scantronformat_file($cdom)
                   16868: 
                   16869: $cdom - the course's domain (optional); if not supplied, uses
                   16870: domain for current $env{'request.course.id'}.
                   16871: 
                   16872: Returns an array containing lines from the scantron format file for
                   16873: the domain of the course.
                   16874: 
                   16875: If a url for a custom.tab file is listed in domain's configuration.db,
                   16876: lines are from this file.
                   16877: 
                   16878: Otherwise, if a default.tab has been published in RES space by the
                   16879: domainconfig user, lines are from this file.
                   16880: 
                   16881: Otherwise, fall back to getting lines from the legacy file on the
                   16882: local server:  /home/httpd/lonTabs/default_scantronformat.tab
                   16883: 
                   16884: =back
                   16885: 
1.243     albertel 16886: =head2 Resource Subroutines
                   16887: 
                   16888: =over 4
1.191     harris41 16889: 
                   16890: =item *
                   16891: 
1.243     albertel 16892: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 16893: 
                   16894: =item *
                   16895: 
1.243     albertel 16896: repcopy($filename) : subscribes to the requested file, and attempts to
                   16897: replicate from the owning library server, Might return
1.607     raeburn  16898: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   16899: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 16900: resource. Expects the local filesystem pathname
                   16901: (/home/httpd/html/res/....)
                   16902: 
                   16903: =back
                   16904: 
                   16905: =head2 Resource Information
                   16906: 
                   16907: =over 4
1.191     harris41 16908: 
                   16909: =item *
                   16910: 
1.1228    raeburn  16911: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   16912: and returns the value of a variety of different possible values,
                   16913: $varname should be a request string, and the other parameters can be
                   16914: used to specify who and what one is asking about. Ordinarily, $cid 
                   16915: does not need to be specified, as it is retrived from 
                   16916: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   16917: within lonuserstate::loadmap() when initializing a course, before
                   16918: $env{'request.course.id'} has been set, so it needs to be provided
                   16919: in that one case.
1.243     albertel 16920: 
                   16921: Possible values for $varname are environment.lastname (or other item
                   16922: from the envirnment hash), user.name (or someother aspect about the
                   16923: user), resource.0.maxtries (or some other part and parameter of a
                   16924: resource)
1.204     albertel 16925: 
                   16926: =item *
                   16927: 
1.243     albertel 16928: directcondval($number) : get current value of a condition; reads from a state
                   16929: string
1.204     albertel 16930: 
                   16931: =item *
                   16932: 
1.243     albertel 16933: condval($condidx) : value of condition index based on state
1.204     albertel 16934: 
                   16935: =item *
                   16936: 
1.1362    raeburn  16937: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243     albertel 16938: resource's metadata, $what should be either a specific key, or either
                   16939: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1362    raeburn  16940: packages that this resource currently uses, the last 3 arguments are 
                   16941: only used internally for recursive metadata.
                   16942: 
                   16943: the toolsymb is only used where the uri is for an external tool (for which
                   16944: the uri as well as the symb are guaranteed to be unique).
1.243     albertel 16945: 
1.1371    raeburn  16946: this function automatically caches all requests except any made recursively
                   16947: to retrieve a list of metadata keys for an imported library file ($liburi is 
                   16948: defined).
1.191     harris41 16949: 
                   16950: =item *
                   16951: 
1.243     albertel 16952: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   16953: network of library servers; returns file handle of where SQL and regex results
                   16954: will be stored for query
1.191     harris41 16955: 
                   16956: =item *
                   16957: 
1.1282    raeburn  16958: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
                   16959: return symbolic list entry (all arguments optional). 
                   16960: 
                   16961: Args: filename is the filename (including path) for the file for which a symb 
                   16962: is required; donotrecurse, if true will prevent calls to allowed() being made 
                   16963: to check access status if more than one resource was found in the bighash 
                   16964: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
                   16965: a randompick); ignorecachednull, if true will prevent a symb of '' being 
                   16966: returned if $env{$cache_str} is defined as ''; checkforblock if true will
                   16967: cause possible symbs to be checked to determine if they are subject to content
                   16968: blocking, if so they will not be included as possible symbs; possibles is a
                   16969: ref to a hash, which, as a side effect, will be populated with all possible 
                   16970: symbs (content blocking not tested).
                   16971:  
1.243     albertel 16972: returns the data handle
1.191     harris41 16973: 
                   16974: =item *
                   16975: 
1.1190    raeburn  16976: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   16977: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 16978: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  16979: on failure, user must be in a course, as it assumes the existence of
                   16980: the course initial hash, and uses $env('request.course.id'}.  The third
                   16981: arg is an optional reference to a scalar.  If this arg is passed in the 
                   16982: call to symbverify, it will be set to 1 if the symb has been set to be 
                   16983: encrypted; otherwise it will be null.  
1.191     harris41 16984: 
                   16985: =item *
                   16986: 
1.243     albertel 16987: symbclean($symb) : removes versions numbers from a symb, returns the
                   16988: cleaned symb
1.191     harris41 16989: 
                   16990: =item *
                   16991: 
1.243     albertel 16992: is_on_map($uri) : checks if the $uri is somewhere on the current
                   16993: course map, user must be in a course for it to work.
1.191     harris41 16994: 
                   16995: =item *
                   16996: 
1.243     albertel 16997: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 16998: 
                   16999: =item *
                   17000: 
1.243     albertel 17001: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   17002: a random seed, all arguments are optional, if they aren't sent it uses the
                   17003: environment to derive them. Note: if symb isn't sent and it can't get one
                   17004: from &symbread it will use the current time as its return value
1.191     harris41 17005: 
                   17006: =item *
                   17007: 
1.243     albertel 17008: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   17009: unfakeable, receipt
1.191     harris41 17010: 
                   17011: =item *
                   17012: 
1.620     albertel 17013: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 17014: 
                   17015: =item *
                   17016: 
1.243     albertel 17017: countacc($url) : count the number of accesses to a given URL
1.191     harris41 17018: 
                   17019: =item *
                   17020: 
1.243     albertel 17021: 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 17022: 
                   17023: =item *
                   17024: 
1.243     albertel 17025: 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 17026: 
                   17027: =item *
                   17028: 
1.243     albertel 17029: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 17030: 
                   17031: =item *
                   17032: 
1.243     albertel 17033: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   17034: forcing spreadsheet to reevaluate the resource scores next time.
                   17035: 
1.1195    raeburn  17036: =item * 
                   17037: 
1.1196    raeburn  17038: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   17039: when viewing in course context.
1.1195    raeburn  17040: 
1.1196    raeburn  17041:  input: six args -- filename (decluttered), course number, course domain,
                   17042:                     url, symb (if registered) and group (if this is a 
                   17043:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  17044: 
1.1196    raeburn  17045:  output: array of five scalars --
1.1195    raeburn  17046:          $cfile -- url for file editing if editable on current server
                   17047:          $home -- homeserver of resource (i.e., for author if published,
                   17048:                                           or course if uploaded.).
                   17049:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  17050:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   17051:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  17052: 
                   17053: =item *
                   17054: 
                   17055: is_course_upload($file,$cnum,$cdom)
                   17056: 
                   17057: Used in course context to determine if current file was uploaded to 
                   17058: the course (i.e., would be found in /userfiles/docs on the course's 
                   17059: homeserver.
                   17060: 
                   17061:   input: 3 args -- filename (decluttered), course number and course domain.
                   17062:   output: boolean -- 1 if file was uploaded.
                   17063: 
1.243     albertel 17064: =back
                   17065: 
                   17066: =head2 Storing/Retreiving Data
                   17067: 
                   17068: =over 4
1.191     harris41 17069: 
                   17070: =item *
                   17071: 
1.1269    raeburn  17072: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
                   17073: permanently for this url; hashref needs to be given and should be a \%hashname;
                   17074: the remaining args aren't required and if they aren't passed or are '' they will
                   17075: be derived from the env (with the exception of $laststore, which is an 
                   17076: optional arg used when a user's submission is stored in grading).
                   17077: $laststore is $version=$timestamp, where $version is the most recent version
                   17078: number retrieved for the corresponding $symb in the $namespace db file, and
                   17079: $timestamp is the timestamp for that transaction (UNIX time).
                   17080: $laststore is currently only passed when cstore() is called by 
                   17081: structuretags::finalize_storage().
1.191     harris41 17082: 
                   17083: =item *
                   17084: 
1.1269    raeburn  17085: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
                   17086: but uses critical subroutine
1.191     harris41 17087: 
                   17088: =item *
                   17089: 
1.243     albertel 17090: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   17091: all args are optional
1.191     harris41 17092: 
                   17093: =item *
                   17094: 
1.717     albertel 17095: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   17096: dumps the complete (or key matching regexp) namespace into a hash
                   17097: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   17098: normally &store()ed into
                   17099: 
                   17100: $range should be either an integer '100' (give me the first 100
                   17101:                                            matching records)
                   17102:               or be  two integers sperated by a - with no spaces
                   17103:                  '30-50' (give me the 30th through the 50th matching
                   17104:                           records)
                   17105: 
                   17106: 
                   17107: =item *
                   17108: 
1.1269    raeburn  17109: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717     albertel 17110: replaces a &store() version of data with a replacement set of data
                   17111: for a particular resource in a namespace passed in the $storehash hash 
1.1269    raeburn  17112: reference. If $tolog is true, the transaction is logged in the courselog
                   17113: with an action=PUTSTORE.
1.717     albertel 17114: 
                   17115: =item *
                   17116: 
1.243     albertel 17117: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   17118: works very similar to store/cstore, but all data is stored in a
                   17119: temporary location and can be reset using tmpreset, $storehash should
                   17120: be a hash reference, returns nothing on success
1.191     harris41 17121: 
                   17122: =item *
                   17123: 
1.243     albertel 17124: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   17125: similar to restore, but all data is stored in a temporary location and
                   17126: can be reset using tmpreset. Returns a hash of values on success,
                   17127: error string otherwise.
1.191     harris41 17128: 
                   17129: =item *
                   17130: 
1.243     albertel 17131: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   17132: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 17133: 
                   17134: =item *
                   17135: 
1.243     albertel 17136: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   17137: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 17138: 
                   17139: =item *
                   17140: 
1.243     albertel 17141: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   17142: namesp ($udom and $uname are optional)
1.191     harris41 17143: 
                   17144: =item *
                   17145: 
1.702     albertel 17146: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 17147: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 17148: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  17149: 
1.702     albertel 17150: $range should be either an integer '100' (give me the first 100
                   17151:                                            matching records)
                   17152:               or be  two integers sperated by a - with no spaces
                   17153:                  '30-50' (give me the 30th through the 50th matching
                   17154:                           records)
1.449     matthew  17155: =item *
                   17156: 
                   17157: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   17158: $store can be a scalar, an array reference, or if the amount to be 
                   17159: incremented is > 1, a hash reference.
                   17160: 
                   17161: ($udom and $uname are optional)
1.191     harris41 17162: 
                   17163: =item *
                   17164: 
1.243     albertel 17165: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   17166: ($udom and $uname are optional)
1.191     harris41 17167: 
                   17168: =item *
                   17169: 
1.243     albertel 17170: cput($namespace,$storehash,$udom,$uname) : critical put
                   17171: ($udom and $uname are optional)
1.191     harris41 17172: 
                   17173: =item *
                   17174: 
1.748     albertel 17175: newput($namespace,$storehash,$udom,$uname) :
                   17176: 
                   17177: Attempts to store the items in the $storehash, but only if they don't
                   17178: currently exist, if this succeeds you can be certain that you have 
                   17179: successfully created a new key value pair in the $namespace db.
                   17180: 
                   17181: 
                   17182: Args:
                   17183:  $namespace: name of database to store values to
                   17184:  $storehash: hashref to store to the db
                   17185:  $udom: (optional) domain of user containing the db
                   17186:  $uname: (optional) name of user caontaining the db
                   17187: 
                   17188: Returns:
                   17189:  'ok' -> succeeded in storing all keys of $storehash
                   17190:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   17191:                         least <key> already existed in the db (other
                   17192:                         requested keys may also already exist)
1.967     bisitz   17193:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 17194:  'con_lost' -> unable to contact request server
                   17195:  'refused' -> action was not allowed by remote machine
                   17196: 
                   17197: 
                   17198: =item *
                   17199: 
1.243     albertel 17200: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   17201: reference filled in from namesp (encrypts the return communication)
                   17202: ($udom and $uname are optional)
1.191     harris41 17203: 
                   17204: =item *
                   17205: 
1.243     albertel 17206: log($udom,$name,$home,$message) : write to permanent log for user; use
                   17207: critical subroutine
                   17208: 
1.806     raeburn  17209: =item *
                   17210: 
1.860     raeburn  17211: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   17212: array reference filled in from namespace found in domain level on either
                   17213: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  17214: 
                   17215: =item *
                   17216: 
1.860     raeburn  17217: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   17218: domain level either on specified domain server ($uhome) or primary domain 
                   17219: server ($udom and $uhome are optional)
1.806     raeburn  17220: 
1.943     raeburn  17221: =item * 
                   17222: 
1.1240    raeburn  17223: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
                   17224: for: authentication, language, quotas, timezone, date locale, and portal URL in
                   17225: the target domain.
                   17226: 
                   17227: May also include additional key => value pairs for the following groups:
                   17228: 
                   17229: =over
                   17230: 
                   17231: =item
                   17232: disk quotas (MB allocated by default to portfolios and authoring spaces).
                   17233: 
                   17234: =over
                   17235: 
                   17236: =item defaultquota, authorquota
                   17237: 
                   17238: =back
                   17239: 
                   17240: =item
                   17241: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
                   17242: portfolio for users).
                   17243: 
                   17244: =over
                   17245: 
                   17246: =item
                   17247: aboutme, blog, webdav, portfolio
                   17248: 
                   17249: =back
                   17250: 
                   17251: =item
                   17252: requestcourses: ability to request courses, and how requests are processed.
                   17253: 
                   17254: =over
                   17255: 
                   17256: =item
1.1305    raeburn  17257: official, unofficial, community, textbook, placement
1.1240    raeburn  17258: 
                   17259: =back
                   17260: 
                   17261: =item
                   17262: inststatus: types of institutional affiliation, and order in which they are displayed.
                   17263: 
                   17264: =over
                   17265: 
                   17266: =item
1.1256    raeburn  17267: inststatustypes, inststatusorder, inststatusguest
1.1240    raeburn  17268: 
                   17269: =back
                   17270: 
                   17271: =item
                   17272: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
                   17273: for course's uploaded content.
                   17274: 
                   17275: =over
                   17276: 
                   17277: =item
1.1246    raeburn  17278: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
1.1305    raeburn  17279: communityquota, textbookquota, placementquota
1.1240    raeburn  17280: 
                   17281: =back
                   17282: 
                   17283: =item
                   17284: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
                   17285: on your servers.
                   17286: 
                   17287: =over
                   17288: 
                   17289: =item 
                   17290: remotesessions, hostedsessions
                   17291: 
                   17292: =back
                   17293: 
                   17294: =back
                   17295: 
                   17296: In cases where a domain coordinator has never used the "Set Domain Configuration"
                   17297: utility to create a configuration.db file on a domain's primary library server 
                   17298: only the following domain defaults: auth_def, auth_arg_def, lang_def
                   17299: -- corresponding values are authentication type (internal, krb4, krb5,
                   17300: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
                   17301: will be available. Values are retrieved from cache (if current), unless the
                   17302: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
                   17303: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
                   17304: 
                   17305: Typical usage:
1.943     raeburn  17306: 
1.1240    raeburn  17307: %domdefaults = &get_domain_defaults($target_domain);
1.943     raeburn  17308: 
1.243     albertel 17309: =back
                   17310: 
                   17311: =head2 Network Status Functions
                   17312: 
                   17313: =over 4
1.191     harris41 17314: 
                   17315: =item *
                   17316: 
1.1137    raeburn  17317: dirlist() : return directory list based on URI (first arg).
                   17318: 
                   17319: Inputs: 1 required, 5 optional.
                   17320: 
                   17321: =over
                   17322: 
                   17323: =item 
                   17324: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   17325: 
                   17326: =item
                   17327: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   17328: 
                   17329: =item
                   17330: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   17331: 
                   17332: =item
                   17333: $getpropath - boolean: 1 if prepend path using &propath(). 
                   17334: 
                   17335: =item
                   17336: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   17337: 
                   17338: =item 
                   17339: $alternateRoot - path to prepend in place of path from $uri.
                   17340: 
                   17341: =back
                   17342: 
                   17343: Returns: Array of up to two items.
                   17344: 
                   17345: =over
                   17346: 
                   17347: a reference to an array of files/subdirectories
                   17348: 
                   17349: =over
                   17350: 
                   17351: Each element in the array of files/subdirectories is a & separated list of
                   17352: item name and the result of running stat on the item.  If dirlist was requested
                   17353: for a file instead of a directory, the item name will be ''. For a directory 
                   17354: listing, if the item is a metadata file, the element will end &N&M 
                   17355: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   17356: default copyright set (1).  
                   17357: 
                   17358: =back
                   17359: 
                   17360: a scalar containing error condition (if encountered).
                   17361: 
                   17362: =over
                   17363: 
                   17364: =item 
                   17365: no_host (no homeserver identified for $username:$domain).
                   17366: 
                   17367: =item 
                   17368: no_such_host (server contacted for listing not identified as valid host).
                   17369: 
                   17370: =item 
                   17371: con_lost (connection to remote server failed).
                   17372: 
                   17373: =item 
                   17374: refused (invalid $username:$domain received on lond side).
                   17375: 
                   17376: =item 
                   17377: no_such_dir (directory at specified path on lond side does not exist). 
                   17378: 
                   17379: =item 
                   17380: empty (directory at specified path on lond side is empty).
                   17381: 
                   17382: =over
                   17383: 
                   17384: This is currently not encountered because the &ls3, &ls2, 
                   17385: &ls (_handler) routines on the lond side do not filter out
                   17386: . and .. from a directory listing. 
                   17387: 
                   17388: =back
                   17389: 
                   17390: =back
                   17391: 
                   17392: =back
1.191     harris41 17393: 
                   17394: =item *
                   17395: 
1.243     albertel 17396: spareserver() : find server with least workload from spare.tab
                   17397: 
1.986     foxr     17398: 
                   17399: =item *
                   17400: 
                   17401: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   17402: if there is no corresponding loncapa host.
                   17403: 
1.243     albertel 17404: =back
                   17405: 
1.986     foxr     17406: 
1.243     albertel 17407: =head2 Apache Request
                   17408: 
                   17409: =over 4
1.191     harris41 17410: 
                   17411: =item *
                   17412: 
1.243     albertel 17413: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   17414: localhost, posts hash
                   17415: 
                   17416: =back
                   17417: 
                   17418: =head2 Data to String to Data
                   17419: 
                   17420: =over 4
1.191     harris41 17421: 
                   17422: =item *
                   17423: 
1.243     albertel 17424: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   17425: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 17426: 
                   17427: =item *
                   17428: 
1.243     albertel 17429: hashref2str($hashref) : convert a hashref into a string complete with
                   17430: escaping and '=' and '&' separators, supports elements that are
                   17431: arrayrefs and hashrefs
1.191     harris41 17432: 
                   17433: =item *
                   17434: 
1.243     albertel 17435: arrayref2str($arrayref) : convert an arrayref into a string complete
                   17436: with escaping and '&' separators, supports elements that are arrayrefs
                   17437: and hashrefs
1.191     harris41 17438: 
                   17439: =item *
                   17440: 
1.243     albertel 17441: str2hash($string) : convert string to hash using unescaping and
                   17442: splitting on '=' and '&', supports elements that are arrayrefs and
                   17443: hashrefs
1.191     harris41 17444: 
                   17445: =item *
                   17446: 
1.243     albertel 17447: str2array($string) : convert string to hash using unescaping and
                   17448: splitting on '&', supports elements that are arrayrefs and hashrefs
                   17449: 
                   17450: =back
                   17451: 
                   17452: =head2 Logging Routines
                   17453: 
                   17454: 
                   17455: These routines allow one to make log messages in the lonnet.log and
                   17456: lonnet.perm logfiles.
1.191     harris41 17457: 
1.1119    foxr     17458: =over 4
                   17459: 
1.191     harris41 17460: =item *
                   17461: 
1.243     albertel 17462: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 17463: 
                   17464: =item *
                   17465: 
1.243     albertel 17466: logthis() : append message to the normal lonnet.log file, it gets
                   17467: preiodically rolled over and deleted.
1.191     harris41 17468: 
                   17469: =item *
                   17470: 
1.243     albertel 17471: logperm() : append a permanent message to lonnet.perm.log, this log
                   17472: file never gets deleted by any automated portion of the system, only
                   17473: messages of critical importance should go in here.
                   17474: 
1.1119    foxr     17475: 
1.243     albertel 17476: =back
                   17477: 
                   17478: =head2 General File Helper Routines
                   17479: 
                   17480: =over 4
1.191     harris41 17481: 
                   17482: =item *
                   17483: 
1.481     raeburn  17484: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   17485: (a) files in /uploaded
                   17486:   (i) If a local copy of the file exists - 
                   17487:       compares modification date of local copy with last-modified date for 
                   17488:       definitive version stored on home server for course. If local copy is 
                   17489:       stale, requests a new version from the home server and stores it. 
                   17490:       If the original has been removed from the home server, then local copy 
                   17491:       is unlinked.
                   17492:   (ii) If local copy does not exist -
                   17493:       requests the file from the home server and stores it. 
                   17494:   
                   17495:   If $caller is 'uploadrep':  
                   17496:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   17497:     for request for files originally uploaded via DOCS. 
                   17498:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   17499:   
                   17500:   Otherwise:
                   17501:      This indicates a call from the content generation phase of the request.
                   17502:      -  returns the entire contents of the file or -1.
                   17503:      
                   17504: (b) files in /res
                   17505:    - returns the entire contents of a file or -1; 
                   17506:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 17507: 
1.712     albertel 17508: 
                   17509: =item *
                   17510: 
                   17511: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   17512:                   reference
                   17513: 
                   17514: returns either a stat() list of data about the file or an empty list
                   17515: if the file doesn't exist or couldn't find out about it (connection
                   17516: problems or user unknown)
                   17517: 
1.191     harris41 17518: =item *
                   17519: 
1.243     albertel 17520: filelocation($dir,$file) : returns file system location of a file
                   17521: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   17522: directory that relative $file lookups are to looked in ($dir of /a/dir
                   17523: and a file of ../bob will become /a/bob)
1.191     harris41 17524: 
                   17525: =item *
                   17526: 
                   17527: hreflocation($dir,$file) : returns file system location or a URL; same as
                   17528: filelocation except for hrefs
                   17529: 
                   17530: =item *
                   17531: 
1.1261    raeburn  17532: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
                   17533: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191     harris41 17534: 
1.243     albertel 17535: =back
                   17536: 
1.608     albertel 17537: =head2 Usererfile file routines (/uploaded*)
                   17538: 
                   17539: =over 4
                   17540: 
                   17541: =item *
                   17542: 
                   17543: userfileupload(): main rotine for putting a file in a user or course's
                   17544:                   filespace, arguments are,
                   17545: 
1.620     albertel 17546:  formname - required - this is the name of the element in $env where the
1.608     albertel 17547:            filename, and the contents of the file to create/modifed exist
1.620     albertel 17548:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   17549:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  17550:  context - if coursedoc, store the file in the course of the active role
                   17551:              of the current user; 
                   17552:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   17553:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 17554:  subdir - required - subdirectory to put the file in under ../userfiles/
                   17555:          if undefined, it will be placed in "unknown"
                   17556: 
                   17557:  (This routine calls clean_filename() to remove any dangerous
                   17558:  characters from the filename, and then calls finuserfileupload() to
                   17559:  complete the transaction)
                   17560: 
                   17561:  returns either the url of the uploaded file (/uploaded/....) if successful
                   17562:  and /adm/notfound.html if unsuccessful
                   17563: 
                   17564: =item *
                   17565: 
                   17566: clean_filename(): routine for cleaing a filename up for storage in
                   17567:                  userfile space, argument is:
                   17568: 
                   17569:  filename - proposed filename
                   17570: 
                   17571: returns: the new clean filename
                   17572: 
                   17573: =item *
                   17574: 
1.1090    raeburn  17575: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 17576: userspace, probably shouldn't be called directly
                   17577: 
                   17578:   docuname: username or courseid of destination for the file
                   17579:   docudom: domain of user/course of destination for the file
                   17580:   formname: same as for userfileupload()
1.1090    raeburn  17581:   fname: filename (including subdirectories) for the file
                   17582:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1401    raeburn  17583:           if hashref, and context is scantron, will convert csv format to standard format
1.1090    raeburn  17584:   allfiles: reference to hash used to store objects found by parser
                   17585:   codebase: reference to hash used for codebases of java objects found by parser
                   17586:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   17587:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   17588:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   17589:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   17590:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   17591:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  17592:   mimetype: reference to scalar to accommodate mime type determined
                   17593:             from File::MMagic if $parser = parse.
1.608     albertel 17594: 
                   17595:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  17596:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   17597:  was 'overwrite').
                   17598:  
1.608     albertel 17599: 
                   17600: =item *
                   17601: 
                   17602: renameuserfile(): renames an existing userfile to a new name
                   17603: 
                   17604:   Args:
                   17605:    docuname: username or courseid of destination for the file
                   17606:    docudom: domain of user/course of destination for the file
                   17607:    old: current file name (including any subdirs under userfiles)
                   17608:    new: desired file name (including any subdirs under userfiles)
                   17609: 
                   17610: =item *
                   17611: 
                   17612: mkdiruserfile(): creates a directory is a userfiles dir
                   17613: 
                   17614:   Args:
                   17615:    docuname: username or courseid of destination for the file
                   17616:    docudom: domain of user/course of destination for the file
                   17617:    dir: dir to create (including any subdirs under userfiles)
                   17618: 
                   17619: =item *
                   17620: 
                   17621: removeuserfile(): removes a file that exists in userfiles
                   17622: 
                   17623:   Args:
                   17624:    docuname: username or courseid of destination for the file
                   17625:    docudom: domain of user/course of destination for the file
                   17626:    fname: filname to delete (including any subdirs under userfiles)
                   17627: 
                   17628: =item *
                   17629: 
                   17630: removeuploadedurl(): convience function for removeuserfile()
                   17631: 
                   17632:   Args:
                   17633:    url:  a full /uploaded/... url to delete
                   17634: 
1.747     albertel 17635: =item * 
                   17636: 
                   17637: get_portfile_permissions():
                   17638:   Args:
                   17639:     domain: domain of user or course contain the portfolio files
                   17640:     user: name of user or num of course contain the portfolio files
                   17641:   Returns:
                   17642:     hashref of a dump of the proper file_permissions.db
                   17643:    
                   17644: 
                   17645: =item * 
                   17646: 
                   17647: get_access_controls():
                   17648: 
                   17649: Args:
                   17650:   current_permissions: the hash ref returned from get_portfile_permissions()
                   17651:   group: (optional) the group you want the files associated with
                   17652:   file: (optional) the file you want access info on
                   17653: 
                   17654: Returns:
1.749     raeburn  17655:     a hash (keys are file names) of hashes containing
                   17656:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   17657:         values are XML containing access control settings (see below) 
1.747     albertel 17658: 
                   17659: Internal notes:
                   17660: 
1.749     raeburn  17661:  access controls are stored in file_permissions.db as key=value pairs.
                   17662:     key -> path to file/file_name\0uniqueID:scope_end_start
                   17663:         where scope -> public,guest,course,group,domains or users.
                   17664:               end -> UNIX time for end of access (0 -> no end date)
                   17665:               start -> UNIX time for start of access
                   17666: 
                   17667:     value -> XML description of access control
                   17668:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   17669:             <start></start>
                   17670:             <end></end>
                   17671: 
                   17672:             <password></password>  for scope type = guest
                   17673: 
                   17674:             <domain></domain>     for scope type = course or group
                   17675:             <number></number>
                   17676:             <roles id="">
                   17677:              <role></role>
                   17678:              <access></access>
                   17679:              <section></section>
                   17680:              <group></group>
                   17681:             </roles>
                   17682: 
                   17683:             <dom></dom>         for scope type = domains
                   17684: 
                   17685:             <users>             for scope type = users
                   17686:              <user>
                   17687:               <uname></uname>
                   17688:               <udom></udom>
                   17689:              </user>
                   17690:             </users>
                   17691:            </scope> 
                   17692:               
                   17693:  Access data is also aggregated for each file in an additional key=value pair:
                   17694:  key -> path to file/file_name\0accesscontrol 
                   17695:  value -> reference to hash
                   17696:           hash contains key = value pairs
                   17697:           where key = uniqueID:scope_end_start
                   17698:                 value = UNIX time record was last updated
                   17699: 
                   17700:           Used to improve speed of look-ups of access controls for each file.  
                   17701:  
                   17702:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   17703: 
1.1198    raeburn  17704: =item *
                   17705: 
1.749     raeburn  17706: modify_access_controls():
                   17707: 
                   17708: Modifies access controls for a portfolio file
                   17709: Args
                   17710: 1. file name
                   17711: 2. reference to hash of required changes,
                   17712: 3. domain
                   17713: 4. username
                   17714:   where domain,username are the domain of the portfolio owner 
                   17715:   (either a user or a course) 
                   17716: 
                   17717: Returns:
                   17718: 1. result of additions or updates ('ok' or 'error', with error message). 
                   17719: 2. result of deletions ('ok' or 'error', with error message).
                   17720: 3. reference to hash of any new or updated access controls.
                   17721: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   17722:    key = integer (inbound ID)
1.1198    raeburn  17723:    value = uniqueID
                   17724: 
                   17725: =item *
                   17726: 
                   17727: get_timebased_id():
                   17728: 
                   17729: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   17730: course via the Course Editor (e.g., folders, composite pages, 
                   17731: group bulletin boards).
                   17732: 
                   17733: Args: (first three required; six others optional)
                   17734: 
                   17735: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   17736:    docssequence, or name of group
                   17737: 
                   17738: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   17739:    e.g., num, boardids
                   17740: 
                   17741: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   17742:    file will be named nohist_namespace.db
                   17743: 
                   17744: 4. cdom: domain of course; default is current course domain from %env
                   17745: 
                   17746: 5. cnum: course number; default is current course number from %env
                   17747: 
                   17748: 6. idtype: set to concat if an additional digit is to be appended to the 
                   17749:    unix timestamp to form the suffix, if the plain timestamp is already
                   17750:    in use.  Default is to not do this, but simply increment the unix 
                   17751:    timestamp by 1 until a unique key is obtained.
                   17752: 
                   17753: 7. who: holder of locking key; defaults to user:domain for user.
                   17754: 
                   17755: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   17756:    retrying); default is 3.
                   17757: 
                   17758: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   17759: 
                   17760: Returns:
                   17761: 
                   17762: 1. suffix obtained (numeric)
                   17763: 
                   17764: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   17765: 
                   17766: 3. error: contains (localized) error message if an error occurred.
                   17767: 
1.747     albertel 17768: 
1.608     albertel 17769: =back
                   17770: 
1.243     albertel 17771: =head2 HTTP Helper Routines
                   17772: 
                   17773: =over 4
                   17774: 
1.191     harris41 17775: =item *
                   17776: 
                   17777: escape() : unpack non-word characters into CGI-compatible hex codes
                   17778: 
                   17779: =item *
                   17780: 
                   17781: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   17782: 
1.243     albertel 17783: =back
                   17784: 
                   17785: =head1 PRIVATE SUBROUTINES
                   17786: 
                   17787: =head2 Underlying communication routines (Shouldn't call)
                   17788: 
                   17789: =over 4
                   17790: 
                   17791: =item *
                   17792: 
                   17793: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   17794: 
                   17795: =item *
                   17796: 
                   17797: reply() : uses subreply to send a message to remote machine, logs all failures
                   17798: 
                   17799: =item *
                   17800: 
                   17801: critical() : passes a critical message to another server; if cannot
                   17802: get through then place message in connection buffer directory and
                   17803: returns con_delayed, if incapable of saving message, returns
                   17804: con_failed
                   17805: 
                   17806: =item *
                   17807: 
                   17808: reconlonc() : tries to reconnect lonc client processes.
                   17809: 
                   17810: =back
                   17811: 
                   17812: =head2 Resource Access Logging
                   17813: 
                   17814: =over 4
                   17815: 
                   17816: =item *
                   17817: 
                   17818: flushcourselogs() : flush (save) buffer logs and access logs
                   17819: 
                   17820: =item *
                   17821: 
                   17822: courselog($what) : save message for course in hash
                   17823: 
                   17824: =item *
                   17825: 
                   17826: courseacclog($what) : save message for course using &courselog().  Perform
                   17827: special processing for specific resource types (problems, exams, quizzes, etc).
                   17828: 
1.191     harris41 17829: =item *
                   17830: 
                   17831: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   17832: as a PerlChildExitHandler
1.243     albertel 17833: 
                   17834: =back
                   17835: 
                   17836: =head2 Other
                   17837: 
                   17838: =over 4
                   17839: 
                   17840: =item *
                   17841: 
                   17842: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 17843: 
                   17844: =back
                   17845: 
                   17846: =cut
1.877     foxr     17847: 

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