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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1403  ! raeburn     4: # $Id: lonnet.pm,v 1.1402 2019/01/27 16:02:58 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.1245    raeburn    80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1138    raeburn    81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
                     82:             %managerstab);
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.807     albertel   99: use LONCAPA qw(:DEFAULT :match);
1.740     www       100: use LONCAPA::Configuration;
1.1160    www       101: use LONCAPA::lonmetadata;
1.1167    droeschl  102: use LONCAPA::Lond;
1.1345    raeburn   103: use LONCAPA::LWPReq;
1.1117    foxr      104: 
1.1090    raeburn   105: use File::Copy;
1.676     albertel  106: 
1.195     www       107: my $readit;
1.1288    damieng   108: my $max_connection_retries = 20;     # Or some such value.
1.1       albertel  109: 
1.619     albertel  110: require Exporter;
                    111: 
                    112: our @ISA = qw (Exporter);
                    113: our @EXPORT = qw(%env);
                    114: 
1.449     matthew   115: 
1.1187    raeburn   116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729     www       117: {
                    118:     my $logid;
1.1187    raeburn   119:     sub write_log {
1.1188    raeburn   120: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
1.1184    raeburn   121:         if ($context eq 'course') {
                    122:             if (($cnum eq '') || ($cdom eq '')) {
                    123:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    124:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    125:             }
1.957     raeburn   126:         }
1.1184    raeburn   127: 	$logid ++;
1.957     raeburn   128:         my $now = time();
                    129: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.1184    raeburn   130:         my $logentry = { 
                    131:                           $id => {
                    132:                                    'exe_uname' => $env{'user.name'},
                    133:                                    'exe_udom'  => $env{'user.domain'},
                    134:                                    'exe_time'  => $now,
                    135:                                    'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    136:                                    'delflag'   => $delflag,
                    137:                                    'logentry'  => $storehash,
                    138:                                    'uname'     => $uname,
                    139:                                    'udom'      => $udom,
                    140:                                   }
                    141:                        };
                    142: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729     www       143:     }
                    144: }
1.1       albertel  145: 
1.163     harris41  146: sub logtouch {
                    147:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  148:     unless (-e "$execdir/logs/lonnet.log") {	
1.1359    raeburn   149: 	open(my $fh,">>","$execdir/logs/lonnet.log");
1.163     harris41  150: 	close $fh;
                    151:     }
                    152:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    153:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    154: }
                    155: 
1.1       albertel  156: sub logthis {
                    157:     my $message=shift;
                    158:     my $execdir=$perlvar{'lonDaemons'};
                    159:     my $now=time;
                    160:     my $local=localtime($now);
1.1359    raeburn   161:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986     foxr      162: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    163: 	print $fh $logstring;
1.448     albertel  164: 	close($fh);
                    165:     }
1.1       albertel  166:     return 1;
                    167: }
                    168: 
                    169: sub logperm {
                    170:     my $message=shift;
                    171:     my $execdir=$perlvar{'lonDaemons'};
                    172:     my $now=time;
                    173:     my $local=localtime($now);
1.1359    raeburn   174:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448     albertel  175: 	print $fh "$now:$message:$local\n";
                    176: 	close($fh);
                    177:     }
1.1       albertel  178:     return 1;
                    179: }
                    180: 
1.850     albertel  181: sub create_connection {
1.853     albertel  182:     my ($hostname,$lonid) = @_;
1.851     albertel  183:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  184: 				     Type    => SOCK_STREAM,
                    185: 				     Timeout => 10);
                    186:     return 0 if (!$client);
1.1399    raeburn   187:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
1.850     albertel  188:     my $result = <$client>;
                    189:     chomp($result);
                    190:     return 1 if ($result eq 'done');
                    191:     return 0;
                    192: }
                    193: 
1.983     raeburn   194: sub get_server_timezone {
                    195:     my ($cnum,$cdom) = @_;
                    196:     my $home=&homeserver($cnum,$cdom);
                    197:     if ($home ne 'no_host') {
                    198:         my $cachetime = 24*3600;
                    199:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    200:         if (defined($cached)) {
                    201:             return $timezone;
                    202:         } else {
                    203:             my $timezone = &reply('servertimezone',$home);
                    204:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    205:         }
                    206:     }
                    207: }
1.850     albertel  208: 
1.1106    raeburn   209: sub get_server_distarch {
                    210:     my ($lonhost,$ignore_cache) = @_;
                    211:     if (defined($lonhost)) {
                    212:         if (!defined(&hostname($lonhost))) {
                    213:             return;
                    214:         }
                    215:         my $cachetime = 12*3600;
                    216:         if (!$ignore_cache) {
                    217:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    218:             if (defined($cached)) {
                    219:                 return $distarch;
                    220:             }
                    221:         }
                    222:         my $rep = &reply('serverdistarch',$lonhost);
                    223:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    224:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    225:                 $rep eq '') {
                    226:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    227:         }
                    228:     }
                    229:     return;
                    230: }
                    231: 
1.1315    raeburn   232: sub get_servercerts_info {
1.1383    raeburn   233:     my ($lonhost,$hostname,$context) = @_;
                    234:     return if ($lonhost eq '');
                    235:     if ($hostname eq '') {
                    236:         $hostname = &hostname($lonhost);
                    237:     }
                    238:     return if ($hostname eq '');
1.1315    raeburn   239:     my ($rep,$uselocal);
1.1388    raeburn   240:     if ($context eq 'install') {
1.1387    raeburn   241:         $uselocal = 1;
                    242:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
1.1315    raeburn   243:         $uselocal = 1;
                    244:     }
1.1387    raeburn   245:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
1.1315    raeburn   246:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
1.1323    raeburn   247:         if ($distro eq '') {
                    248:             $uselocal = 0;
                    249:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
1.1315    raeburn   250:             if ($1 < 6) {
                    251:                 $uselocal = 0;
                    252:             }
1.1346    raeburn   253:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
                    254:             if ($1 < 12) {
                    255:                 $uselocal = 0;
                    256:             }
1.1315    raeburn   257:         }
                    258:     }
                    259:     if ($uselocal) {
1.1383    raeburn   260:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
1.1315    raeburn   261:     } else {
                    262:         $rep=&reply('servercerts',$lonhost);
                    263:     }
                    264:     my ($result,%returnhash);
                    265:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    266:         ($rep eq 'unknown_cmd')) {
                    267:         $result = $rep;
                    268:     } else {
                    269:         $result = 'ok';
                    270:         my @pairs=split(/\&/,$rep);
                    271:         foreach my $item (@pairs) {
                    272:             my ($key,$value)=split(/=/,$item,2);
                    273:             my $what = &unescape($key);
                    274:             $returnhash{$what}=&thaw_unescape($value);
                    275:         }
                    276:     }
                    277:     return ($result,\%returnhash);
                    278: }
                    279: 
1.993     raeburn   280: sub get_server_loncaparev {
1.1073    raeburn   281:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   282:     if (defined($lonhost)) {
                    283:         if (!defined(&hostname($lonhost))) {
                    284:             undef($lonhost);
                    285:         }
                    286:     }
                    287:     if (!defined($lonhost)) {
                    288:         if (defined(&domain($dom,'primary'))) {
                    289:             $lonhost=&domain($dom,'primary');
                    290:             if ($lonhost eq 'no_host') {
                    291:                 undef($lonhost);
                    292:             }
                    293:         }
                    294:     }
                    295:     if (defined($lonhost)) {
1.1073    raeburn   296:         my $cachetime = 12*3600;
                    297:         if (!$ignore_cache) {
                    298:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    299:             if (defined($cached)) {
                    300:                 return $loncaparev;
                    301:             }
                    302:         }
                    303:         my ($answer,$loncaparev);
                    304:         my @ids=&current_machine_ids();
                    305:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    306:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   307:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   308:                 $loncaparev = $1;
                    309:             }
                    310:         } else {
                    311:             $answer = &reply('serverloncaparev',$lonhost);
                    312:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    313:                 if ($caller eq 'loncron') {
1.1397    raeburn   314:                     my $hostname = &hostname($lonhost);
1.1073    raeburn   315:                     my $protocol = $protocol{$lonhost};
                    316:                     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn   317:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073    raeburn   318:                     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn   319:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
1.1073    raeburn   320:                     unless ($response->is_error()) {
                    321:                         my $content = $response->content;
1.1081    raeburn   322:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   323:                             $loncaparev = $1;
                    324:                         }
                    325:                     }
                    326:                 } else {
                    327:                     $loncaparev = $loncaparevs{$lonhost};
                    328:                 }
1.1081    raeburn   329:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   330:                 $loncaparev = $1;
                    331:             }
1.993     raeburn   332:         }
1.1073    raeburn   333:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   334:     }
                    335: }
                    336: 
1.1074    raeburn   337: sub get_server_homeID {
                    338:     my ($hostname,$ignore_cache,$caller) = @_;
                    339:     unless ($ignore_cache) {
                    340:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    341:         if (defined($cached)) {
                    342:             return $serverhomeID;
                    343:         }
                    344:     }
                    345:     my $cachetime = 12*3600;
                    346:     my $serverhomeID;
                    347:     if ($caller eq 'loncron') { 
                    348:         my @machine_ids = &machine_ids($hostname);
                    349:         foreach my $id (@machine_ids) {
                    350:             my $response = &reply('serverhomeID',$id);
                    351:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    352:                 $serverhomeID = $response;
                    353:                 last;
                    354:             }
                    355:         }
                    356:         if ($serverhomeID eq '') {
                    357:             $serverhomeID = $machine_ids[-1];
                    358:         }
                    359:     } else {
                    360:         $serverhomeID = $serverhomeIDs{$hostname};
                    361:     }
                    362:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    363: }
                    364: 
1.1121    raeburn   365: sub get_remote_globals {
                    366:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   367:     my ($result,%returnhash,%whatneeded);
                    368:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   369:         foreach my $what (sort(keys(%{$whathash}))) {
                    370:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   371:             my ($response,$cached);
1.1121    raeburn   372:             unless ($ignore_cache) {
1.1125    raeburn   373:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   374:             }
                    375:             if (defined($cached)) {
1.1125    raeburn   376:                 $returnhash{$what} = $response;
1.1121    raeburn   377:             } else {
1.1125    raeburn   378:                 $whatneeded{$what} = 1;
1.1121    raeburn   379:             }
                    380:         }
1.1125    raeburn   381:         if (keys(%whatneeded) == 0) {
                    382:             $result = 'ok';
                    383:         } else {
1.1121    raeburn   384:             my $requested = &freeze_escape(\%whatneeded);
                    385:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   386:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    387:                 ($rep eq 'unknown_cmd')) {
                    388:                 $result = $rep;
                    389:             } else {
                    390:                 $result = 'ok';
1.1121    raeburn   391:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   392:                 foreach my $item (@pairs) {
                    393:                     my ($key,$value)=split(/=/,$item,2);
                    394:                     my $what = &unescape($key);
                    395:                     my $hashid = $lonhost.'-'.$what;
                    396:                     $returnhash{$what}=&thaw_unescape($value);
                    397:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   398:                 }
                    399:             }
                    400:         }
                    401:     }
1.1125    raeburn   402:     return ($result,\%returnhash);
1.1121    raeburn   403: }
                    404: 
1.1124    raeburn   405: sub remote_devalidate_cache {
1.1241    raeburn   406:     my ($lonhost,$cachekeys) = @_;
                    407:     my $items;
                    408:     return unless (ref($cachekeys) eq 'ARRAY');
                    409:     my $cachestr = join('&',@{$cachekeys});
                    410:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124    raeburn   411:     return $response;
                    412: }
                    413: 
1.1       albertel  414: # -------------------------------------------------- Non-critical communication
                    415: sub subreply {
                    416:     my ($cmd,$server)=@_;
1.838     albertel  417:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      418:     #
                    419:     #  With loncnew process trimming, there's a timing hole between lonc server
                    420:     #  process exit and the master server picking up the listen on the AF_UNIX
                    421:     #  socket.  In that time interval, a lock file will exist:
                    422: 
                    423:     my $lockfile=$peerfile.".lock";
                    424:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
1.1289    damieng   425: 	sleep(0.1);
1.549     foxr      426:     }
                    427:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      428:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      429:     #
1.550     foxr      430:     #   We'll give the connection a few tries before abandoning it.  If
                    431:     #   connection is not possible, we'll con_lost back to the client.
                    432:     #   
                    433:     my $client;
                    434:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    435: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    436: 				      Type    => SOCK_STREAM,
                    437: 				      Timeout => 10);
1.869     albertel  438: 	if ($client) {
1.550     foxr      439: 	    last;		# Connected!
1.850     albertel  440: 	} else {
1.853     albertel  441: 	    &create_connection(&hostname($server),$server);
1.550     foxr      442: 	}
1.1289    damieng   443:         sleep(0.1);	# Try again later if failed connection.
1.550     foxr      444:     }
                    445:     my $answer;
                    446:     if ($client) {
1.704     albertel  447: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      448: 	$answer=<$client>;
                    449: 	if (!$answer) { $answer="con_lost"; }
                    450: 	chomp($answer);
                    451:     } else {
                    452: 	$answer = 'con_lost';	# Failed connection.
                    453:     }
1.1       albertel  454:     return $answer;
                    455: }
                    456: 
                    457: sub reply {
                    458:     my ($cmd,$server)=@_;
1.838     albertel  459:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  460:     my $answer=subreply($cmd,$server);
1.65      www       461:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1396    raeburn   462:         my $logged = $cmd;
                    463:         if ($cmd =~ /^encrypt:([^:]+):/) {
                    464:             my $subcmd = $1;
                    465:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
                    466:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
                    467:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
                    468:                 (undef,undef,my @rest) = split(/:/,$cmd);
                    469:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
                    470:                     splice(@rest,2,1,'Hidden');
                    471:                 } elsif ($subcmd eq 'passwd') {
                    472:                     splice(@rest,2,2,('Hidden','Hidden'));
                    473:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
                    474:                          ($subcmd eq 'autoexportgrades')) {
                    475:                     splice(@rest,3,1,'Hidden');
                    476:                 }
                    477:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
                    478:             }
                    479:         }
                    480:         &logthis("<font color=\"blue\">WARNING:".
                    481:                  " $logged to $server returned $answer</font>");
1.12      www       482:     }
1.1       albertel  483:     return $answer;
                    484: }
                    485: 
                    486: # ----------------------------------------------------------- Send USR1 to lonc
                    487: 
                    488: sub reconlonc {
1.891     albertel  489:     my ($lonid) = @_;
                    490:     if ($lonid) {
1.1295    raeburn   491:         my $hostname = &hostname($lonid);
1.891     albertel  492: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    493: 	if ($hostname && -e $peerfile) {
                    494: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    495: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    496: 					     Type    => SOCK_STREAM,
                    497: 					     Timeout => 10);
                    498: 	    if ($client) {
                    499: 		print $client ("reset_retries\n");
                    500: 		my $answer=<$client>;
                    501: 		#reset just this one.
                    502: 	    }
                    503: 	}
                    504: 	return;
                    505:     }
                    506: 
1.836     www       507:     &logthis("Trying to reconnect lonc");
1.1       albertel  508:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1359    raeburn   509:     if (open(my $fh,"<",$loncfile)) {
1.1       albertel  510: 	my $loncpid=<$fh>;
                    511:         chomp($loncpid);
                    512:         if (kill 0 => $loncpid) {
                    513: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    514:             kill USR1 => $loncpid;
                    515:             sleep 1;
1.1295    raeburn   516:         } else {
1.12      www       517: 	    &logthis(
1.672     albertel  518:                "<font color=\"blue\">WARNING:".
1.12      www       519:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  520:         }
                    521:     } else {
1.836     www       522: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  523:     }
                    524: }
                    525: 
                    526: # ------------------------------------------------------ Critical communication
1.12      www       527: 
1.1       albertel  528: sub critical {
                    529:     my ($cmd,$server)=@_;
1.838     albertel  530:     unless (&hostname($server)) {
1.672     albertel  531:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       532:                " Critical message to unknown server ($server)</font>");
                    533:         return 'no_such_host';
                    534:     }
1.1       albertel  535:     my $answer=reply($cmd,$server);
                    536:     if ($answer eq 'con_lost') {
1.1295    raeburn   537: 	&reconlonc($server);
1.589     albertel  538: 	my $answer=reply($cmd,$server);
1.1       albertel  539:         if ($answer eq 'con_lost') {
                    540:             my $now=time;
                    541:             my $middlename=$cmd;
1.5       www       542:             $middlename=substr($middlename,0,16);
1.1       albertel  543:             $middlename=~s/\W//g;
                    544:             my $dfilename=
1.305     www       545:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    546:             $dumpcount++;
1.1       albertel  547:             {
1.448     albertel  548: 		my $dfh;
1.1359    raeburn   549: 		if (open($dfh,">",$dfilename)) {
1.448     albertel  550: 		    print $dfh "$cmd\n"; 
                    551: 		    close($dfh);
                    552: 		}
1.1       albertel  553:             }
1.1288    damieng   554:             sleep 1;
1.1       albertel  555:             my $wcmd='';
                    556:             {
1.448     albertel  557: 		my $dfh;
1.1359    raeburn   558: 		if (open($dfh,"<",$dfilename)) {
1.448     albertel  559: 		    $wcmd=<$dfh>; 
                    560: 		    close($dfh);
                    561: 		}
1.1       albertel  562:             }
                    563:             chomp($wcmd);
1.7       www       564:             if ($wcmd eq $cmd) {
1.672     albertel  565: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       566:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  567:                 &logperm("D:$server:$cmd");
                    568: 	        return 'con_delayed';
                    569:             } else {
1.672     albertel  570:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       571:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  572:                 &logperm("F:$server:$cmd");
                    573:                 return 'con_failed';
                    574:             }
                    575:         }
                    576:     }
                    577:     return $answer;
1.405     albertel  578: }
                    579: 
1.755     albertel  580: # ------------------------------------------- check if return value is an error
                    581: 
                    582: sub error {
                    583:     my ($result) = @_;
1.756     albertel  584:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  585: 	if ($2 == 2) { return undef; }
                    586: 	return $1;
                    587:     }
                    588:     return undef;
                    589: }
                    590: 
1.783     albertel  591: sub convert_and_load_session_env {
                    592:     my ($lonidsdir,$handle)=@_;
                    593:     my @profile;
                    594:     {
1.917     albertel  595: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    596: 	if (!$opened) {
1.915     albertel  597: 	    return 0;
                    598: 	}
1.783     albertel  599: 	flock($idf,LOCK_SH);
                    600: 	@profile=<$idf>;
                    601: 	close($idf);
                    602:     }
                    603:     my %temp_env;
                    604:     foreach my $line (@profile) {
1.786     albertel  605: 	if ($line !~ m/=/) {
                    606: 	    return 0;
                    607: 	}
1.783     albertel  608: 	chomp($line);
                    609: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    610: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    611:     }
                    612:     unlink("$lonidsdir/$handle.id");
                    613:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    614: 	    0640)) {
                    615: 	%disk_env = %temp_env;
                    616: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    617: 	untie(%disk_env);
                    618:     }
1.786     albertel  619:     return 1;
1.783     albertel  620: }
                    621: 
1.374     www       622: # ------------------------------------------- Transfer profile into environment
1.780     albertel  623: my $env_loaded;
                    624: sub transfer_profile_to_env {
1.788     albertel  625:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    626:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       627: 
1.720     albertel  628:     if (!defined($lonidsdir)) {
                    629: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    630:     }
                    631:     if (!defined($handle)) {
                    632:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    633:     }
                    634: 
1.786     albertel  635:     my $convert;
                    636:     {
1.917     albertel  637:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    638: 	if (!$opened) {
1.915     albertel  639: 	    return;
                    640: 	}
1.786     albertel  641: 	flock($idf,LOCK_SH);
                    642: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    643: 		&GDBM_READER(),0640)) {
                    644: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    645: 	    untie(%disk_env);
                    646: 	} else {
                    647: 	    $convert = 1;
                    648: 	}
                    649:     }
                    650:     if ($convert) {
                    651: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    652: 	    &logthis("Failed to load session, or convert session.");
                    653: 	}
1.374     www       654:     }
1.783     albertel  655: 
1.786     albertel  656:     my %remove;
1.783     albertel  657:     while ( my $envname = each(%env) ) {
1.433     matthew   658:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    659:             if ($time < time-300) {
1.783     albertel  660:                 $remove{$key}++;
1.433     matthew   661:             }
                    662:         }
                    663:     }
1.783     albertel  664: 
1.619     albertel  665:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  666:     $env_loaded=1;
1.783     albertel  667:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   668:         &delenv($expired_key);
1.374     www       669:     }
1.1       albertel  670: }
                    671: 
1.916     albertel  672: # ---------------------------------------------------- Check for valid session 
                    673: sub check_for_valid_session {
1.1355    raeburn   674:     my ($r,$name,$userhashref,$domref) = @_;
1.916     albertel  675:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1378    raeburn   676:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
                    677:     if ($name eq 'lonDAV') {
                    678:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    679:     } else {
                    680:         $lonidsdir=$r->dir_config('lonIDsDir');
                    681:         if ($name eq '') {
                    682:             $name = 'lonID';
                    683:         }
                    684:     }
                    685:     if ($name eq 'lonID') {
                    686:         $secure = 'lonSID';
1.1337    raeburn   687:         $linkname = 'lonLinkID';
                    688:         $pubname = 'lonPubID';
1.1378    raeburn   689:         if (exists($cookies{$secure})) {
                    690:             $lonid=$cookies{$secure};
                    691:         } elsif (exists($cookies{$name})) {
                    692:             $lonid=$cookies{$name};
1.1400    raeburn   693:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
1.1337    raeburn   694:             $lonid=$cookies{$linkname};
1.1378    raeburn   695:         } elsif (exists($cookies{$pubname})) {
                    696:             $lonid=$cookies{$pubname};
1.1337    raeburn   697:         }
1.1378    raeburn   698:     } else {
                    699:         $lonid=$cookies{$name};
1.1337    raeburn   700:     }
1.916     albertel  701:     return undef if (!$lonid);
                    702: 
                    703:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1378    raeburn   704:     if (-l "$lonidsdir/$handle.id") {
                    705:         my $link = readlink("$lonidsdir/$handle.id");
                    706:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    707:             $handle = $1;
                    708:         }
1.1155    raeburn   709:     }
1.1355    raeburn   710:     if (!-e "$lonidsdir/$handle.id") {
                    711:         if ((ref($domref)) && ($name eq 'lonID') && 
                    712:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    713:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    714:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    715:                 $$domref = $possudom;
                    716:             }
                    717:         }
                    718:         return undef;
                    719:     }
1.916     albertel  720: 
1.917     albertel  721:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    722:     return undef if (!$opened);
1.916     albertel  723: 
                    724:     flock($idf,LOCK_SH);
                    725:     my %disk_env;
                    726:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    727: 	    &GDBM_READER(),0640)) {
                    728: 	return undef;	
                    729:     }
                    730: 
                    731:     if (!defined($disk_env{'user.name'})
                    732: 	|| !defined($disk_env{'user.domain'})) {
1.1394    raeburn   733:         untie(%disk_env);
1.916     albertel  734: 	return undef;
                    735:     }
1.1245    raeburn   736: 
                    737:     if (ref($userhashref) eq 'HASH') {
                    738:         $userhashref->{'name'} = $disk_env{'user.name'};
                    739:         $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1361    raeburn   740:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
1.1375    raeburn   741:         if ($userhashref->{'lti'}) {
                    742:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
                    743:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
                    744:         }
1.1212    raeburn   745:     }
1.1394    raeburn   746:     untie(%disk_env);
1.1245    raeburn   747: 
1.916     albertel  748:     return $handle;
                    749: }
                    750: 
1.830     albertel  751: sub timed_flock {
                    752:     my ($file,$lock_type) = @_;
                    753:     my $failed=0;
                    754:     eval {
                    755: 	local $SIG{__DIE__}='DEFAULT';
                    756: 	local $SIG{ALRM}=sub {
                    757: 	    $failed=1;
                    758: 	    die("failed lock");
                    759: 	};
                    760: 	alarm(13);
                    761: 	flock($file,$lock_type);
                    762: 	alarm(0);
                    763:     };
                    764:     if ($failed) {
                    765: 	return undef;
                    766:     } else {
                    767: 	return 1;
                    768:     }
                    769: }
                    770: 
1.1392    raeburn   771: sub get_sessionfile_vars {
                    772:     my ($handle,$lonidsdir,$storearr) = @_;
                    773:     my %returnhash;
                    774:     unless (ref($storearr) eq 'ARRAY') {
                    775:         return %returnhash;
                    776:     }
                    777:     if (-l "$lonidsdir/$handle.id") {
                    778:         my $link = readlink("$lonidsdir/$handle.id");
                    779:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
                    780:             $handle = $1;
                    781:         }
                    782:     }
                    783:     if ((-e "$lonidsdir/$handle.id") &&
                    784:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
                    785:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
                    786:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
                    787:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
                    788:                 flock($idf,LOCK_SH);
                    789:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    790:                         &GDBM_READER(),0640)) {
                    791:                     foreach my $item (@{$storearr}) {
                    792:                         $returnhash{$item} = $disk_env{$item};
                    793:                     }
                    794:                     untie(%disk_env);
                    795:                 }
                    796:             }
                    797:         }
                    798:     }
                    799:     return %returnhash;
                    800: }
                    801: 
1.5       www       802: # ---------------------------------------------------------- Append Environment
                    803: 
                    804: sub appenv {
1.949     raeburn   805:     my ($newenv,$roles) = @_;
                    806:     if (ref($newenv) eq 'HASH') {
                    807:         foreach my $key (keys(%{$newenv})) {
                    808:             my $refused = 0;
                    809: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    810:                 $refused = 1;
                    811:                 if (ref($roles) eq 'ARRAY') {
1.1250    raeburn   812:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949     raeburn   813:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    814:                         $refused = 0;
                    815:                     }
                    816:                 }
                    817:             }
                    818:             if ($refused) {
                    819:                 &logthis("<font color=\"blue\">WARNING: ".
                    820:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    821:                          .'</font>');
                    822: 	        delete($newenv->{$key});
                    823:             } else {
                    824:                 $env{$key}=$newenv->{$key};
                    825:             }
                    826:         }
1.1376    raeburn   827:         my $lonids = $perlvar{'lonIDsDir'};
                    828:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
                    829:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    830:             if ($opened
                    831: 	        && &timed_flock($env_file,LOCK_EX)
                    832: 	        &&
                    833: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    834: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    835: 	        while (my ($key,$value) = each(%{$newenv})) {
                    836: 	            $disk_env{$key} = $value;
                    837: 	        }
                    838: 	        untie(%disk_env);
                    839:             }
1.35      www       840:         }
1.191     harris41  841:     }
1.56      www       842:     return 'ok';
                    843: }
                    844: # ----------------------------------------------------- Delete from Environment
                    845: 
                    846: sub delenv {
1.1104    raeburn   847:     my ($delthis,$regexp,$roles) = @_;
                    848:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    849:         my $refused = 1;
                    850:         if (ref($roles) eq 'ARRAY') {
                    851:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    852:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    853:                 $refused = 0;
                    854:             }
                    855:         }
                    856:         if ($refused) {
                    857:             &logthis("<font color=\"blue\">WARNING: ".
                    858:                      "Attempt to delete from environment ".$delthis);
                    859:             return 'error';
                    860:         }
1.56      www       861:     }
1.917     albertel  862:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    863:     if ($opened
1.915     albertel  864: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  865: 	&&
                    866: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    867: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  868: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   869: 	    if ($regexp) {
                    870:                 if ($key=~/^$delthis/) {
                    871:                     delete($env{$key});
                    872:                     delete($disk_env{$key});
                    873:                 } 
                    874:             } else {
                    875:                 if ($key=~/^\Q$delthis\E/) {
                    876: 		    delete($env{$key});
                    877: 		    delete($disk_env{$key});
                    878: 	        }
                    879:             }
1.448     albertel  880: 	}
1.783     albertel  881: 	untie(%disk_env);
1.5       www       882:     }
                    883:     return 'ok';
1.369     albertel  884: }
                    885: 
1.790     albertel  886: sub get_env_multiple {
                    887:     my ($name) = @_;
                    888:     my @values;
                    889:     if (defined($env{$name})) {
                    890:         # exists is it an array
                    891:         if (ref($env{$name})) {
                    892:             @values=@{ $env{$name} };
                    893:         } else {
                    894:             $values[0]=$env{$name};
                    895:         }
                    896:     }
                    897:     return(@values);
                    898: }
                    899: 
1.958     www       900: # ------------------------------------------------------------------- Locking
                    901: 
                    902: sub set_lock {
                    903:     my ($text)=@_;
                    904:     $locknum++;
                    905:     my $id=$$.'-'.$locknum;
                    906:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    907:              'session.lock.'.$id => $text});
                    908:     return $id;
                    909: }
                    910: 
                    911: sub get_locks {
                    912:     my $num=0;
                    913:     my %texts=();
                    914:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    915:        if ($lock=~/\w/) {
                    916:           $num++;
                    917:           $texts{$lock}=$env{'session.lock.'.$lock};
                    918:        }
                    919:    }
                    920:    return ($num,%texts);
                    921: }
                    922: 
                    923: sub remove_lock {
                    924:     my ($id)=@_;
                    925:     my $newlocks='';
                    926:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    927:        if (($lock=~/\w/) && ($lock ne $id)) {
                    928:           $newlocks.=','.$lock;
                    929:        }
                    930:     }
                    931:     &appenv({'session.locks' => $newlocks});
                    932:     &delenv('session.lock.'.$id);
                    933: }
                    934: 
                    935: sub remove_all_locks {
                    936:     my $activelocks=$env{'session.locks'};
                    937:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    938:        if ($lock=~/\w/) {
                    939:           &remove_lock($lock);
                    940:        }
                    941:     }
                    942: }
                    943: 
                    944: 
1.369     albertel  945: # ------------------------------------------ Find out current server userload
                    946: sub userload {
                    947:     my $numusers=0;
                    948:     {
                    949: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    950: 	my $filename;
                    951: 	my $curtime=time;
                    952: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  953: 	    next if ($filename eq '.' || $filename eq '..');
                    954: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.1390    raeburn   955:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404     albertel  956: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  957: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  958: 	}
                    959: 	closedir(LONIDS);
                    960:     }
                    961:     my $userloadpercent=0;
                    962:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    963:     if ($maxuserload) {
1.371     albertel  964: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  965:     }
1.372     albertel  966:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  967:     return $userloadpercent;
1.283     www       968: }
                    969: 
1.1       albertel  970: # ------------------------------ Find server with least workload from spare.tab
1.11      www       971: 
1.1       albertel  972: sub spareserver {
1.1083    raeburn   973:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  974:     my $spare_server;
1.370     albertel  975:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  976:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    977:                                                      :  $userloadpercent;
1.1083    raeburn   978:     my ($uint_dom,$remotesessions);
                    979:     if (($udom ne '') && (&domain($udom) ne '')) {
                    980:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    981:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    982:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    983:         $remotesessions = $udomdefaults{'remotesessions'};
                    984:     }
1.1123    raeburn   985:     my $spareshash = &this_host_spares($udom);
                    986:     if (ref($spareshash) eq 'HASH') {
                    987:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    988:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279    raeburn   989:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    990:                                              $try_server));
1.1123    raeburn   991: 	        ($spare_server, $lowest_load) =
                    992: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                    993:             }
1.1083    raeburn   994:         }
1.784     albertel  995: 
1.1123    raeburn   996:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    997: 
                    998:         if (!$found_server) {
                    999:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                   1000: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279    raeburn  1001:                     next unless (&spare_can_host($udom,$uint_dom,
                   1002:                                                  $remotesessions,$try_server));
1.1123    raeburn  1003: 	            ($spare_server, $lowest_load) =
                   1004: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                   1005:                 }
                   1006: 	    }
                   1007:         }
1.784     albertel 1008:     }
                   1009: 
                   1010:     if (!$want_server_name) {
1.1001    raeburn  1011:         if (defined($spare_server)) {
                   1012:             my $hostname = &hostname($spare_server);
1.1083    raeburn  1013:             if (defined($hostname)) {
1.1397    raeburn  1014:                 my $protocol = 'http';
                   1015:                 if ($protocol{$spare_server} eq 'https') {
                   1016:                     $protocol = $protocol{$spare_server};
                   1017:                 }
1.1001    raeburn  1018: 	        $spare_server = $protocol.'://'.$hostname;
                   1019:             }
                   1020:         }
1.784     albertel 1021:     }
                   1022:     return $spare_server;
                   1023: }
                   1024: 
                   1025: sub compare_server_load {
1.1253    raeburn  1026:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
                   1027: 
                   1028:     if ($required) {
                   1029:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                   1030:         my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1031:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   1032:         if (($major eq '' && $minor eq '') ||
                   1033:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                   1034:             return ($spare_server,$lowest_load);
                   1035:         }
                   1036:     }
1.784     albertel 1037: 
                   1038:     my $loadans     = &reply('load',    $try_server);
                   1039:     my $userloadans = &reply('userload',$try_server);
                   1040: 
                   1041:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn  1042: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel 1043:     }
                   1044: 
                   1045:     my $load;
                   1046:     if ($loadans =~ /\d/) {
                   1047: 	if ($userloadans =~ /\d/) {
                   1048: 	    #both are numbers, pick the bigger one
                   1049: 	    $load = ($loadans > $userloadans) ? $loadans 
                   1050: 		                              : $userloadans;
1.411     albertel 1051: 	} else {
1.784     albertel 1052: 	    $load = $loadans;
1.411     albertel 1053: 	}
1.784     albertel 1054:     } else {
                   1055: 	$load = $userloadans;
                   1056:     }
                   1057: 
                   1058:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                   1059: 	$spare_server = $try_server;
                   1060: 	$lowest_load  = $load;
1.370     albertel 1061:     }
1.784     albertel 1062:     return ($spare_server,$lowest_load);
1.202     matthew  1063: }
1.914     albertel 1064: 
                   1065: # --------------------------- ask offload servers if user already has a session
                   1066: sub find_existing_session {
                   1067:     my ($udom,$uname) = @_;
1.1123    raeburn  1068:     my $spareshash = &this_host_spares($udom);
                   1069:     if (ref($spareshash) eq 'HASH') {
                   1070:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                   1071:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                   1072:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1073:             }
                   1074:         }
                   1075:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                   1076:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                   1077:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                   1078:             }
                   1079:         }
1.914     albertel 1080:     }
                   1081:     return;
                   1082: }
                   1083: 
1.1389    raeburn  1084: # check if user's browser sent load balancer cookie and server still has session
                   1085: # and is not overloaded.
                   1086: sub check_for_balancer_cookie {
                   1087:     my ($r,$update_mtime) = @_;
                   1088:     my ($otherserver,$cookie);
                   1089:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                   1090:     if (exists($cookies{'balanceID'})) {
                   1091:         my $balid = $cookies{'balanceID'};
                   1092:         $cookie=&LONCAPA::clean_handle($balid->value);
                   1093:         my $balancedir=$r->dir_config('lonBalanceDir');
                   1094:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
                   1095:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
                   1096:                 my ($possudom,$possuname) = ($1,$2);
                   1097:                 my $has_session = 0;
                   1098:                 if ((&domain($possudom) ne '') &&
                   1099:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
                   1100:                     my $try_server;
                   1101:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
                   1102:                     if ($opened) {
                   1103:                         flock($idf,LOCK_SH);
                   1104:                         while (my $line = <$idf>) {
                   1105:                             chomp($line);
                   1106:                             if (&hostname($line) ne '') {
                   1107:                                 $try_server = $line;
                   1108:                                 last;
                   1109:                             }
                   1110:                         }
                   1111:                         close($idf);
                   1112:                         if (($try_server) &&
                   1113:                             (&has_user_session($try_server,$possudom,$possuname))) {
                   1114:                             my $lowest_load = 30000;
                   1115:                             ($otherserver,$lowest_load) =
                   1116:                                 &compare_server_load($try_server,undef,$lowest_load);
                   1117:                             if ($otherserver ne '' && $lowest_load < 100) {
                   1118:                                 $has_session = 1;
                   1119:                             } else {
                   1120:                                 undef($otherserver);
                   1121:                             }
                   1122:                         }
                   1123:                     }
                   1124:                 }
                   1125:                 if ($has_session) {
                   1126:                     if ($update_mtime) {
                   1127:                         my $atime = my $mtime = time;
                   1128:                         utime($atime,$mtime,"$balancedir/$cookie.id");
                   1129:                     }
                   1130:                 } else {
                   1131:                     unlink("$balancedir/$cookie.id");
                   1132:                 }
                   1133:             }
                   1134:         }
                   1135:     }
                   1136:     return ($otherserver,$cookie);
                   1137: }
                   1138: 
                   1139: sub delbalcookie {
                   1140:     my ($cookie,$balancer) =@_;
                   1141:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
                   1142:         my ($udom,$uname) = ($1,$2);
                   1143:         my $uprimary_id = &domain($udom,'primary');
                   1144:         my $uintdom = &internet_dom($uprimary_id);
                   1145:         my $intdom = &internet_dom($balancer);
                   1146:         my $serverhomedom = &host_domain($balancer);
                   1147:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1148:             return &reply("delbalcookie:$cookie",$balancer);
                   1149:         }
                   1150:     }
                   1151: }
                   1152: 
1.914     albertel 1153: # -------------------------------- ask if server already has a session for user
                   1154: sub has_user_session {
                   1155:     my ($lonid,$udom,$uname) = @_;
                   1156:     my $result = &reply(join(':','userhassession',
                   1157: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                   1158:     return 1 if ($result eq 'ok');
                   1159: 
                   1160:     return 0;
                   1161: }
                   1162: 
1.1076    raeburn  1163: # --------- determine least loaded server in a user's domain which allows login
                   1164: 
                   1165: sub choose_server {
1.1259    raeburn  1166:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076    raeburn  1167:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn  1168:     my %servers = &get_servers($udom);
1.1076    raeburn  1169:     my $lowest_load = 30000;
1.1259    raeburn  1170:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
                   1171:     if ($skiploadbal) {
                   1172:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
                   1173:         unless (defined($cached)) {
                   1174:             my $cachetime = 60*60*24;
                   1175:             my %domconfig =
                   1176:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1177:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1178:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
                   1179:                                            $cachetime);
                   1180:             }
                   1181:         }
                   1182:     }
1.1076    raeburn  1183:     foreach my $lonhost (keys(%servers)) {
1.1259    raeburn  1184:         if ($skiploadbal) {
                   1185:             if (ref($balancers) eq 'HASH') {
                   1186:                 next if (exists($balancers->{$lonhost}));
                   1187:             }
1.1389    raeburn  1188:         }
1.1115    raeburn  1189:         my $loginvia;
                   1190:         if ($checkloginvia) {
                   1191:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn  1192:             if ($loginvia) {
                   1193:                 my ($server,$path) = split(/:/,$loginvia);
                   1194:                 ($login_host, $lowest_load) =
1.1253    raeburn  1195:                     &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116    raeburn  1196:                 if ($login_host eq $server) {
                   1197:                     $portal_path = $path;
1.1151    raeburn  1198:                     $isredirect = 1;
1.1116    raeburn  1199:                 }
                   1200:             } else {
                   1201:                 ($login_host, $lowest_load) =
1.1253    raeburn  1202:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116    raeburn  1203:                 if ($login_host eq $lonhost) {
                   1204:                     $portal_path = '';
1.1151    raeburn  1205:                     $isredirect = ''; 
1.1116    raeburn  1206:                 }
                   1207:             }
                   1208:         } else {
1.1076    raeburn  1209:             ($login_host, $lowest_load) =
1.1253    raeburn  1210:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076    raeburn  1211:         }
                   1212:     }
                   1213:     if ($login_host ne '') {
1.1116    raeburn  1214:         $hostname = &hostname($login_host);
1.1076    raeburn  1215:     }
1.1331    raeburn  1216:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076    raeburn  1217: }
                   1218: 
1.202     matthew  1219: # --------------------------------------------- Try to change a user's password
                   1220: 
                   1221: sub changepass {
1.799     raeburn  1222:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew  1223:     $currentpass = &escape($currentpass);
                   1224:     $newpass     = &escape($newpass);
1.1030    raeburn  1225:     my $lonhost = $perlvar{'lonHostID'};
                   1226:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew  1227: 		       $server);
                   1228:     if (! $answer) {
                   1229: 	&logthis("No reply on password change request to $server ".
                   1230: 		 "by $uname in domain $udom.");
                   1231:     } elsif ($answer =~ "^ok") {
                   1232:         &logthis("$uname in $udom successfully changed their password ".
                   1233: 		 "on $server.");
                   1234:     } elsif ($answer =~ "^pwchange_failure") {
                   1235: 	&logthis("$uname in $udom was unable to change their password ".
                   1236: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1237: 		 "or pwchange");
                   1238:     } elsif ($answer =~ "^non_authorized") {
                   1239:         &logthis("$uname in $udom did not get their password correct when ".
                   1240: 		 "attempting to change it on $server.");
                   1241:     } elsif ($answer =~ "^auth_mode_error") {
                   1242:         &logthis("$uname in $udom attempted to change their password despite ".
                   1243: 		 "not being locally or internally authenticated on $server.");
                   1244:     } elsif ($answer =~ "^unknown_user") {
                   1245:         &logthis("$uname in $udom attempted to change their password ".
                   1246: 		 "on $server but were unable to because $server is not ".
                   1247: 		 "their home server.");
                   1248:     } elsif ($answer =~ "^refused") {
                   1249: 	&logthis("$server refused to change $uname in $udom password because ".
                   1250: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1251:     } elsif ($answer =~ "invalid_client") {
                   1252:         &logthis("$server refused to change $uname in $udom password because ".
                   1253:                  "it was a reset by e-mail originating from an invalid server.");
1.202     matthew  1254:     }
                   1255:     return $answer;
1.1       albertel 1256: }
                   1257: 
1.169     harris41 1258: # ----------------------- Try to determine user's current authentication scheme
                   1259: 
                   1260: sub queryauthenticate {
                   1261:     my ($uname,$udom)=@_;
1.456     albertel 1262:     my $uhome=&homeserver($uname,$udom);
                   1263:     if (!$uhome) {
                   1264: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1265: 	return 'no_host';
                   1266:     }
                   1267:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1268:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1269: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1270:     }
1.456     albertel 1271:     return $answer;
1.169     harris41 1272: }
                   1273: 
1.1       albertel 1274: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1275: 
1.1       albertel 1276: sub authenticate {
1.1073    raeburn  1277:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1278:     $upass=&escape($upass);
                   1279:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1280:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1281:     my $newhome;
1.836     www      1282:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1283: # Maybe the machine was offline and only re-appeared again recently?
                   1284:         &reconlonc();
                   1285: # One more
1.952     raeburn  1286: 	$uhome=&homeserver($uname,$udom,1);
                   1287:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1288:             if (defined(&domain($udom,'primary'))) {
                   1289:                 $newhome=&domain($udom,'primary');
                   1290:             }
                   1291:             if ($newhome ne '') {
                   1292:                 $uhome = $newhome;
                   1293:             }
                   1294:         }
1.836     www      1295: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1296: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1297: 	    return 'no_host';
                   1298:         }
1.1       albertel 1299:     }
1.1073    raeburn  1300:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1301:     if ($answer eq 'authorized') {
1.952     raeburn  1302:         if ($newhome) {
                   1303:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1304:             return 'no_account_on_host'; 
                   1305:         } else {
                   1306:             &logthis("User $uname at $udom authorized by $uhome");
                   1307:             return $uhome;
                   1308:         }
1.471     albertel 1309:     }
                   1310:     if ($answer eq 'non_authorized') {
                   1311: 	&logthis("User $uname at $udom rejected by $uhome");
                   1312: 	return 'no_host'; 
1.9       www      1313:     }
1.471     albertel 1314:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1315:     return 'no_host';
                   1316: }
                   1317: 
1.1073    raeburn  1318: sub can_host_session {
1.1074    raeburn  1319:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1320:     my $canhost = 1;
1.1074    raeburn  1321:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn  1322:     if (ref($remotesessions) eq 'HASH') {
                   1323:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1324:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1325:                 $canhost = 0;
                   1326:             } else {
                   1327:                 $canhost = 1;
                   1328:             }
                   1329:         }
                   1330:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1331:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1332:                 $canhost = 1;
                   1333:             } else {
                   1334:                 $canhost = 0;
                   1335:             }
                   1336:         }
                   1337:         if ($canhost) {
                   1338:             if ($remotesessions->{'version'} ne '') {
                   1339:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1340:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1341:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1342:                         my $major = $1;
                   1343:                         my $minor = $2;
                   1344:                         if (($major < $reqmajor ) ||
                   1345:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1346:                             $canhost = 0;
                   1347:                         }
                   1348:                     } else {
                   1349:                         $canhost = 0;
                   1350:                     }
                   1351:                 }
                   1352:             }
                   1353:         }
                   1354:     }
                   1355:     if ($canhost) {
                   1356:         if (ref($hostedsessions) eq 'HASH') {
1.1120    raeburn  1357:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1358:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073    raeburn  1359:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1360:                 if (($uint_dom ne '') && 
                   1361:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1362:                     $canhost = 0;
                   1363:                 } else {
                   1364:                     $canhost = 1;
                   1365:                 }
                   1366:             }
                   1367:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1368:                 if (($uint_dom ne '') && 
                   1369:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1370:                     $canhost = 1;
                   1371:                 } else {
                   1372:                     $canhost = 0;
                   1373:                 }
                   1374:             }
                   1375:         }
                   1376:     }
                   1377:     return $canhost;
                   1378: }
                   1379: 
1.1083    raeburn  1380: sub spare_can_host {
                   1381:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1382:     my $canhost=1;
1.1279    raeburn  1383:     my $try_server_hostname = &hostname($try_server);
                   1384:     my $serverhomeID = &get_server_homeID($try_server_hostname);
                   1385:     my $serverhomedom = &host_domain($serverhomeID);
                   1386:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1387:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
                   1388:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
                   1389:             $canhost = 0;
                   1390:         }
                   1391:     }
                   1392:     if (($canhost) && ($uint_dom)) {
                   1393:         my @intdoms;
                   1394:         my $internet_names = &get_internet_names($try_server);
                   1395:         if (ref($internet_names) eq 'ARRAY') {
                   1396:             @intdoms = @{$internet_names};
                   1397:         }
                   1398:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1399:             my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1400:             $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1401:                                          $remotesessions,
                   1402:                                          $defdomdefaults{'hostedsessions'});
                   1403:         }
1.1083    raeburn  1404:     }
                   1405:     return $canhost;
                   1406: }
                   1407: 
1.1123    raeburn  1408: sub this_host_spares {
                   1409:     my ($dom) = @_;
1.1126    raeburn  1410:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1411:     my @hosts = &current_machine_ids();
                   1412:     foreach my $lonhost (@hosts) {
                   1413:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1414:             $dom_in_use = $dom;
                   1415:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1416:             last;
                   1417:         }
                   1418:     }
1.1126    raeburn  1419:     if ($dom_in_use ne '') {
                   1420:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1421:     }
                   1422:     if (ref($result) ne 'HASH') {
                   1423:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1424:         $dom_in_use = &host_domain($lonhost_in_use);
                   1425:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1426:         if (ref($result) ne 'HASH') {
                   1427:             $result = \%spareid;
                   1428:         }
                   1429:     }
                   1430:     return $result;
                   1431: }
                   1432: 
                   1433: sub spares_for_offload  {
                   1434:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1435:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1436:     if (defined($cached)) {
                   1437:         return $result;
                   1438:     } else {
1.1126    raeburn  1439:         my $cachetime = 60*60*24;
                   1440:         my %domconfig =
                   1441:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
                   1442:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1443:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1444:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1445:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1446:                 }
                   1447:             }
                   1448:         }
                   1449:     }
1.1126    raeburn  1450:     return;
1.1123    raeburn  1451: }
                   1452: 
1.1129    raeburn  1453: sub get_lonbalancer_config {
                   1454:     my ($servers) = @_;
                   1455:     my ($currbalancer,$currtargets);
                   1456:     if (ref($servers) eq 'HASH') {
                   1457:         foreach my $server (keys(%{$servers})) {
                   1458:             my %what = (
                   1459:                          spareid => 1,
                   1460:                          perlvar => 1,
                   1461:                        );
                   1462:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1463:             if ($result eq 'ok') {
                   1464:                 if (ref($returnhash) eq 'HASH') {
                   1465:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1466:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1467:                             $currbalancer = $server;
                   1468:                             $currtargets = {};
                   1469:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1470:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1471:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1472:                                 }
                   1473:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1474:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1475:                                 }
                   1476:                             }
                   1477:                             last;
                   1478:                         }
                   1479:                     }
                   1480:                 }
                   1481:             }
                   1482:         }
                   1483:     }
                   1484:     return ($currbalancer,$currtargets);
                   1485: }
                   1486: 
                   1487: sub check_loadbalancing {
1.1331    raeburn  1488:     my ($uname,$udom,$caller) = @_;
1.1191    raeburn  1489:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1391    raeburn  1490:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
1.1129    raeburn  1491:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1492:     my @hosts = &current_machine_ids();
1.1129    raeburn  1493:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1494:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
                   1495:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                   1496:     my $serverhomedom = &host_domain($lonhost);
1.1307    raeburn  1497:     my $domneedscache;
1.1129    raeburn  1498:     my $cachetime = 60*60*24;
                   1499: 
                   1500:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1501:         $dom_in_use = $udom;
                   1502:         $homeintdom = 1;
                   1503:     } else {
                   1504:         $dom_in_use = $serverhomedom;
                   1505:     }
                   1506:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1507:     unless (defined($cached)) {
                   1508:         my %domconfig =
                   1509:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
                   1510:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1511:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307    raeburn  1512:         } else {
                   1513:             $domneedscache = $dom_in_use;
1.1129    raeburn  1514:         }
                   1515:     }
                   1516:     if (ref($result) eq 'HASH') {
1.1391    raeburn  1517:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1518:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1519:         if ($is_balancer) {
                   1520:             if (ref($currrules) eq 'HASH') {
                   1521:                 if ($homeintdom) {
                   1522:                     if ($uname ne '') {
                   1523:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1524:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1525:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1526:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1527:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1528:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1529:                             }
                   1530:                         }
                   1531:                         if ($rule_in_effect eq '') {
                   1532:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1533:                             if ($userenv{'inststatus'} ne '') {
                   1534:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1535:                                 my ($othertitle,$usertypes,$types) =
                   1536:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1537:                                 if (ref($types) eq 'ARRAY') {
                   1538:                                     foreach my $type (@{$types}) {
                   1539:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1540:                                             if (exists($currrules->{$type})) {
                   1541:                                                 $rule_in_effect = $currrules->{$type};
                   1542:                                             }
                   1543:                                         }
                   1544:                                     }
                   1545:                                 }
                   1546:                             } else {
                   1547:                                 if (exists($currrules->{'default'})) {
                   1548:                                     $rule_in_effect = $currrules->{'default'};
                   1549:                                 }
                   1550:                             }
                   1551:                         }
                   1552:                     } else {
                   1553:                         if (exists($currrules->{'default'})) {
                   1554:                             $rule_in_effect = $currrules->{'default'};
                   1555:                         }
                   1556:                     }
                   1557:                 } else {
                   1558:                     if ($currrules->{'_LC_external'} ne '') {
                   1559:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1560:                     }
                   1561:                 }
                   1562:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1563:                                                        $uname,$udom);
                   1564:             }
                   1565:         }
                   1566:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239    raeburn  1567:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129    raeburn  1568:         unless (defined($cached)) {
                   1569:             my %domconfig =
                   1570:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
                   1571:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307    raeburn  1572:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
                   1573:             } else {
                   1574:                 $domneedscache = $serverhomedom;
1.1129    raeburn  1575:             }
                   1576:         }
                   1577:         if (ref($result) eq 'HASH') {
1.1391    raeburn  1578:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
1.1191    raeburn  1579:                 &check_balancer_result($result,@hosts);
                   1580:             if ($is_balancer) {
1.1129    raeburn  1581:                 if (ref($currrules) eq 'HASH') {
                   1582:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1583:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1584:                     }
                   1585:                 }
                   1586:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1587:                                                        $uname,$udom);
                   1588:             }
                   1589:         } else {
                   1590:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1591:                 $is_balancer = 1;
                   1592:                 $offloadto = &this_host_spares($dom_in_use);
                   1593:             }
1.1307    raeburn  1594:             unless (defined($cached)) {
                   1595:                 $domneedscache = $serverhomedom;
                   1596:             }
1.1129    raeburn  1597:         }
                   1598:     } else {
                   1599:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1600:             $is_balancer = 1;
                   1601:             $offloadto = &this_host_spares($dom_in_use);
                   1602:         }
1.1307    raeburn  1603:         unless (defined($cached)) {
                   1604:             $domneedscache = $serverhomedom;
                   1605:         }
                   1606:     }
                   1607:     if ($domneedscache) {
                   1608:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129    raeburn  1609:     }
1.1176    raeburn  1610:     if ($is_balancer) {
                   1611:         my $lowest_load = 30000;
                   1612:         if (ref($offloadto) eq 'HASH') {
                   1613:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1614:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1615:                     ($otherserver,$lowest_load) =
                   1616:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1617:                 }
1.1129    raeburn  1618:             }
1.1176    raeburn  1619:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1620: 
1.1176    raeburn  1621:             if (!$found_server) {
                   1622:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1623:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1624:                         ($otherserver,$lowest_load) =
                   1625:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1626:                     }
                   1627:                 }
                   1628:             }
                   1629:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1630:             if (@{$offloadto} == 1) {
                   1631:                 $otherserver = $offloadto->[0];
                   1632:             } elsif (@{$offloadto} > 1) {
                   1633:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1634:                     ($otherserver,$lowest_load) =
                   1635:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1636:                 }
                   1637:             }
                   1638:         }
1.1331    raeburn  1639:         unless ($caller eq 'login') {
                   1640:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1641:                 $is_balancer = 0;
                   1642:                 if ($uname ne '' && $udom ne '') {
                   1643:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1389    raeburn  1644:                         &appenv({'user.loadbalexempt'     => $lonhost,
1.1331    raeburn  1645:                                  'user.loadbalcheck.time' => time});
                   1646:                     }
1.1176    raeburn  1647:                 }
1.1129    raeburn  1648:             }
                   1649:         }
1.1389    raeburn  1650:         unless ($homeintdom) {
                   1651:             undef($setcookie);
                   1652:         }
1.1129    raeburn  1653:     }
1.1391    raeburn  1654:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
1.1129    raeburn  1655: }
                   1656: 
1.1191    raeburn  1657: sub check_balancer_result {
                   1658:     my ($result,@hosts) = @_;
1.1391    raeburn  1659:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1660:     if (ref($result) eq 'HASH') {
                   1661:         if ($result->{'lonhost'} ne '') {
                   1662:             my $currbalancer = $result->{'lonhost'};
                   1663:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1664:                 $is_balancer = 1;
                   1665:                 $currtargets = $result->{'targets'};
                   1666:                 $currrules = $result->{'rules'};
                   1667:             }
1.1391    raeburn  1668:             $dom_balancers = $currbalancer;
1.1191    raeburn  1669:         } else {
1.1391    raeburn  1670:             if (keys(%{$result})) {
                   1671:                 foreach my $key (keys(%{$result})) {
                   1672:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1673:                         (ref($result->{$key}) eq 'HASH')) {
                   1674:                         $is_balancer = 1;
                   1675:                         $currrules = $result->{$key}{'rules'};
                   1676:                         $currtargets = $result->{$key}{'targets'};
                   1677:                         $setcookie = $result->{$key}{'cookie'};
                   1678:                         last;
                   1679:                     }
1.1191    raeburn  1680:                 }
1.1391    raeburn  1681:                 $dom_balancers = join(',',sort(keys(%{$result})));
1.1191    raeburn  1682:             }
                   1683:         }
                   1684:     }
1.1391    raeburn  1685:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
1.1191    raeburn  1686: }
                   1687: 
1.1129    raeburn  1688: sub get_loadbalancer_targets {
                   1689:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1690:     my $offloadto;
1.1175    raeburn  1691:     if ($rule_in_effect eq 'none') {
                   1692:         return [$perlvar{'lonHostID'}];
                   1693:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1694:         $offloadto = $currtargets;
                   1695:     } else {
                   1696:         if ($rule_in_effect eq 'homeserver') {
                   1697:             my $homeserver = &homeserver($uname,$udom);
                   1698:             if ($homeserver ne 'no_host') {
                   1699:                 $offloadto = [$homeserver];
                   1700:             }
                   1701:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1702:             my %domconfig =
                   1703:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1704:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1705:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1706:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1707:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1708:                     }
                   1709:                 }
                   1710:             } else {
1.1178    raeburn  1711:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1712:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1713:                 if (&hostname($remotebalancer) ne '') {
                   1714:                     $offloadto = [$remotebalancer];
                   1715:                 }
                   1716:             }
                   1717:         } elsif (&hostname($rule_in_effect) ne '') {
                   1718:             $offloadto = [$rule_in_effect];
                   1719:         }
                   1720:     }
                   1721:     return $offloadto;
                   1722: }
                   1723: 
1.1127    raeburn  1724: sub internet_dom_servers {
                   1725:     my ($dom) = @_;
                   1726:     my (%uniqservers,%servers);
                   1727:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1728:     my @machinedoms = &machine_domains($primaryserver);
                   1729:     foreach my $mdom (@machinedoms) {
                   1730:         my %currservers = %servers;
                   1731:         my %server = &get_servers($mdom);
                   1732:         %servers = (%currservers,%server);
                   1733:     }
                   1734:     my %by_hostname;
                   1735:     foreach my $id (keys(%servers)) {
                   1736:         push(@{$by_hostname{$servers{$id}}},$id);
                   1737:     }
                   1738:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1739:         if (@{$by_hostname{$hostname}} > 1) {
                   1740:             my $match = 0;
                   1741:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1742:                 if (&host_domain($id) eq $dom) {
                   1743:                     $uniqservers{$id} = $hostname;
                   1744:                     $match = 1;
                   1745:                 }
                   1746:             }
                   1747:             unless ($match) {
                   1748:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1749:             }
                   1750:         } else {
                   1751:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1752:         }
                   1753:     }
                   1754:     return %uniqservers;
                   1755: }
                   1756: 
1.1347    raeburn  1757: sub trusted_domains {
                   1758:     my ($cmdtype,$calldom) = @_;
1.1349    raeburn  1759:     my ($trusted,$untrusted);
1.1347    raeburn  1760:     if (&domain($calldom) eq '') {
1.1349    raeburn  1761:         return ($trusted,$untrusted);
1.1347    raeburn  1762:     }
1.1395    raeburn  1763:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
1.1349    raeburn  1764:         return ($trusted,$untrusted);
1.1347    raeburn  1765:     }
                   1766:     my $callprimary = &domain($calldom,'primary');
                   1767:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
                   1768:     if ($intcalldom eq '') {
1.1349    raeburn  1769:         return ($trusted,$untrusted);
1.1347    raeburn  1770:     }
                   1771: 
                   1772:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
                   1773:     unless (defined($cached)) {
                   1774:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
                   1775:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
                   1776:         $trustconfig = $domconfig{'trust'};
                   1777:     }
                   1778:     if (ref($trustconfig)) {
                   1779:         my (%possexc,%possinc,@allexc,@allinc); 
                   1780:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
                   1781:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
                   1782:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
                   1783:             }
                   1784:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
1.1395    raeburn  1785:                 $possinc{$intcalldom} = 1;
1.1347    raeburn  1786:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
                   1787:             }
                   1788:         }
                   1789:         if (keys(%possexc)) {
                   1790:             if (keys(%possinc)) {
                   1791:                 foreach my $key (sort(keys(%possexc))) {
                   1792:                     next if ($key eq $intcalldom);
                   1793:                     unless ($possinc{$key}) {
                   1794:                         push(@allexc,$key);
                   1795:                     }
                   1796:                 }
                   1797:             } else {
                   1798:                 @allexc = sort(keys(%possexc));
                   1799:             }
                   1800:         }
                   1801:         if (keys(%possinc)) {
                   1802:             $possinc{$intcalldom} = 1;
                   1803:             @allinc = sort(keys(%possinc));
                   1804:         }
                   1805:         if ((@allexc > 0) || (@allinc > 0)) {
                   1806:             my %doms_by_intdom;
                   1807:             my %allintdoms = &all_host_intdom();
                   1808:             my %alldoms = &all_host_domain();
                   1809:             foreach my $key (%allintdoms) {
                   1810:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
                   1811:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
                   1812:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
                   1813:                     }
                   1814:                 } else {
                   1815:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
                   1816:                 }
                   1817:             }
                   1818:             foreach my $exc (@allexc) {
                   1819:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
1.1395    raeburn  1820:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
1.1347    raeburn  1821:                 }
                   1822:             }
                   1823:             foreach my $inc (@allinc) {
                   1824:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
1.1395    raeburn  1825:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
1.1347    raeburn  1826:                 }
                   1827:             }
                   1828:         }
                   1829:     }
1.1349    raeburn  1830:     return ($trusted,$untrusted);
1.1347    raeburn  1831: }
                   1832: 
                   1833: sub will_trust {
                   1834:     my ($cmdtype,$domain,$possdom) = @_;
                   1835:     return 1 if ($domain eq $possdom);
                   1836:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
                   1837:     my $willtrust; 
                   1838:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
                   1839:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
                   1840:             $willtrust = 1;
                   1841:         }
                   1842:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
                   1843:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
                   1844:             $willtrust = 1;
                   1845:         }
                   1846:     } else {
                   1847:         $willtrust = 1;
                   1848:     }
                   1849:     return $willtrust;
                   1850: }
                   1851: 
1.1       albertel 1852: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1853: 
1.599     albertel 1854: my %homecache;
1.1       albertel 1855: sub homeserver {
1.230     stredwic 1856:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1857:     my $index="$uname:$udom";
1.426     albertel 1858: 
1.599     albertel 1859:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1860: 
                   1861:     my %servers = &get_servers($udom,'library');
                   1862:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1863:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1864: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1865: 
                   1866: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1867: 	if ($answer eq 'found') {
                   1868: 	    delete($badServerCache{$tryserver}); 
                   1869: 	    return $homecache{$index}=$tryserver;
                   1870: 	} elsif ($answer eq 'no_host') {
                   1871: 	    $badServerCache{$tryserver}=1;
                   1872: 	}
1.1       albertel 1873:     }    
                   1874:     return 'no_host';
1.70      www      1875: }
                   1876: 
1.1300    raeburn  1877: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70      www      1878: 
                   1879: sub idget {
1.1300    raeburn  1880:     my ($udom,$idsref,$namespace)=@_;
1.70      www      1881:     my %returnhash=();
1.1300    raeburn  1882:     my @ids=(); 
                   1883:     if (ref($idsref) eq 'ARRAY') {
                   1884:         @ids = @{$idsref};
                   1885:     } else {
                   1886:         return %returnhash; 
                   1887:     }
                   1888:     if ($namespace eq '') {
                   1889:         $namespace = 'ids';
                   1890:     }
1.70      www      1891:     
1.841     albertel 1892:     my %servers = &get_servers($udom,'library');
                   1893:     foreach my $tryserver (keys(%servers)) {
1.1299    raeburn  1894: 	my $idlist=join('&', map { &escape($_); } @ids);
1.1300    raeburn  1895: 	if ($namespace eq 'ids') {
                   1896: 	    $idlist=~tr/A-Z/a-z/;
                   1897: 	}
                   1898: 	my $reply;
                   1899: 	if ($namespace eq 'ids') {
                   1900: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1901: 	} else {
                   1902: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
                   1903: 	}
1.841     albertel 1904: 	my @answer=();
                   1905: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1906: 	    @answer=split(/\&/,$reply);
                   1907: 	}                    ;
                   1908: 	my $i;
                   1909: 	for ($i=0;$i<=$#ids;$i++) {
                   1910: 	    if ($answer[$i]) {
1.1299    raeburn  1911: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300    raeburn  1912: 	    }
1.841     albertel 1913: 	}
1.1300    raeburn  1914:     }
1.70      www      1915:     return %returnhash;
                   1916: }
                   1917: 
                   1918: # ------------------------------------- Find the IDs behind a list of usernames
                   1919: 
                   1920: sub idrget {
                   1921:     my ($udom,@unames)=@_;
                   1922:     my %returnhash=();
1.800     albertel 1923:     foreach my $uname (@unames) {
                   1924:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1925:     }
1.70      www      1926:     return %returnhash;
                   1927: }
                   1928: 
1.1300    raeburn  1929: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70      www      1930: 
                   1931: sub idput {
1.1300    raeburn  1932:     my ($udom,$idsref,$uhom,$namespace)=@_;
1.70      www      1933:     my %servers=();
1.1300    raeburn  1934:     my %ids=();
                   1935:     my %byid = ();
                   1936:     if (ref($idsref) eq 'HASH') {
                   1937:         %ids=%{$idsref};
                   1938:     }
                   1939:     if ($namespace eq '') {
                   1940:         $namespace = 'ids'; 
                   1941:     }
1.800     albertel 1942:     foreach my $uname (keys(%ids)) {
                   1943: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300    raeburn  1944:         if ($uhom eq '') {
                   1945:             $uhom=&homeserver($uname,$udom);
                   1946:         }
1.70      www      1947:         if ($uhom ne 'no_host') {
1.800     albertel 1948:             my $esc_unam=&escape($uname);
1.1300    raeburn  1949:             if ($namespace eq 'ids') {
                   1950:                 my $id=&escape($ids{$uname});
                   1951:                 $id=~tr/A-Z/a-z/;
                   1952:                 my $esc_unam=&escape($uname);
                   1953:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70      www      1954:             } else {
1.1300    raeburn  1955:                 my @currids = split(/,/,$ids{$uname});
                   1956:                 foreach my $id (@currids) {
                   1957:                     $byid{$uhom}{$id} .= $uname.',';
                   1958:                 }
                   1959:             }
                   1960:         }
                   1961:     }
                   1962:     if ($namespace eq 'clickers') {
                   1963:         foreach my $server (keys(%byid)) {
                   1964:             if (ref($byid{$server}) eq 'HASH') {
                   1965:                 foreach my $id (keys(%{$byid{$server}})) {
                   1966:                     $byid{$server} =~ s/,$//;
                   1967:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
                   1968:                 }
1.70      www      1969:             }
                   1970:         }
1.191     harris41 1971:     }
1.800     albertel 1972:     foreach my $server (keys(%servers)) {
1.1300    raeburn  1973:         $servers{$server} =~ s/\&$//;
                   1974:         if ($namespace eq 'ids') {     
                   1975:             &critical('idput:'.$udom.':'.$servers{$server},$server);
                   1976:         } else {
                   1977:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
                   1978:         }
1.191     harris41 1979:     }
1.344     www      1980: }
                   1981: 
1.1300    raeburn  1982: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231    raeburn  1983: 
                   1984: sub iddel {
1.1300    raeburn  1985:     my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231    raeburn  1986:     my %result=();
1.1300    raeburn  1987:     my %ids=();
                   1988:     my %byid = ();
                   1989:     if (ref($idshashref) eq 'HASH') {
                   1990:         %ids=%{$idshashref};
                   1991:     } else {
1.1231    raeburn  1992:         return %result;
                   1993:     }
1.1300    raeburn  1994:     if ($namespace eq '') {
                   1995:         $namespace = 'ids';
                   1996:     }
1.1231    raeburn  1997:     my %servers=();
1.1300    raeburn  1998:     while (my ($id,$unamestr) = each(%ids)) {
                   1999:         if ($namespace eq 'ids') {
                   2000:             my $uhom = $uhome;
                   2001:             if ($uhom eq '') { 
                   2002:                 $uhom=&homeserver($unamestr,$udom);
                   2003:             }
                   2004:             if ($uhom ne 'no_host') {
                   2005:                 $servers{$uhom}.='&'.&escape($id);
                   2006:             }
                   2007:          } else {
                   2008:             my @curritems = split(/,/,$ids{$id});
                   2009:             foreach my $uname (@curritems) {
                   2010:                 my $uhom = $uhome;
                   2011:                 if ($uhom eq '') {
                   2012:                     $uhom=&homeserver($uname,$udom);
                   2013:                 }
                   2014:                 if ($uhom ne 'no_host') { 
                   2015:                     $byid{$uhom}{$id} .= $uname.',';
                   2016:                 }
                   2017:             }
1.1231    raeburn  2018:         }
1.1300    raeburn  2019:     }
                   2020:     if ($namespace eq 'clickers') {
                   2021:         foreach my $server (keys(%byid)) {
                   2022:             if (ref($byid{$server}) eq 'HASH') {
                   2023:                 foreach my $id (keys(%{$byid{$server}})) {
                   2024:                     $byid{$server}{$id} =~ s/,$//;
                   2025:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
                   2026:                 }
1.1231    raeburn  2027:             }
                   2028:         }
                   2029:     }
                   2030:     foreach my $server (keys(%servers)) {
1.1300    raeburn  2031:         $servers{$server} =~ s/\&$//;
                   2032:         if ($namespace eq 'ids') {
                   2033:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
                   2034:         } elsif ($namespace eq 'clickers') {
                   2035:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
                   2036:         }
1.1231    raeburn  2037:     }
                   2038:     return %result;
                   2039: }
                   2040: 
1.1300    raeburn  2041: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
                   2042: 
                   2043: sub updateclickers {
                   2044:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
                   2045:     my %clickers;
                   2046:     if (ref($idshashref) eq 'HASH') {
                   2047:         %clickers=%{$idshashref};
                   2048:     } else {
                   2049:         return;
                   2050:     }
                   2051:     my $items='';
                   2052:     foreach my $item (keys(%clickers)) {
                   2053:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
                   2054:     }
                   2055:     $items=~s/\&$//;
                   2056:     my $request = "updateclickers:$udom:$action:$items";
                   2057:     if ($critical) {
                   2058:         return &critical($request,$uhome);
                   2059:     } else {
                   2060:         return &reply($request,$uhome);
                   2061:     }
                   2062: }
                   2063: 
1.1023    raeburn  2064: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  2065: sub dump_dom {
1.1165    droeschl 2066:     my ($namespace, $udom, $regexp) = @_;
                   2067: 
                   2068:     $udom ||= $env{'user.domain'};
                   2069: 
                   2070:     return () unless $udom;
                   2071: 
                   2072:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  2073: }
                   2074: 
1.1023    raeburn  2075: # ------------------------------------------ get items from domain db files   
1.806     raeburn  2076: 
                   2077: sub get_dom {
1.860     raeburn  2078:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.1267    raeburn  2079:     return if ($udom eq 'public');
1.806     raeburn  2080:     my $items='';
                   2081:     foreach my $item (@$storearr) {
                   2082:         $items.=&escape($item).'&';
                   2083:     }
                   2084:     $items=~s/\&$//;
1.860     raeburn  2085:     if (!$udom) {
                   2086:         $udom=$env{'user.domain'};
1.1267    raeburn  2087:         return if ($udom eq 'public');
1.860     raeburn  2088:         if (defined(&domain($udom,'primary'))) {
                   2089:             $uhome=&domain($udom,'primary');
                   2090:         } else {
1.874     albertel 2091:             undef($uhome);
1.860     raeburn  2092:         }
                   2093:     } else {
                   2094:         if (!$uhome) {
                   2095:             if (defined(&domain($udom,'primary'))) {
                   2096:                 $uhome=&domain($udom,'primary');
                   2097:             }
                   2098:         }
                   2099:     }
                   2100:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1344    raeburn  2101:         my $rep;
                   2102:         if ($namespace =~ /^enc/) {
                   2103:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
                   2104:         } else {
                   2105:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
                   2106:         }
1.866     raeburn  2107:         my %returnhash;
1.875     albertel 2108:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  2109:             return %returnhash;
                   2110:         }
1.806     raeburn  2111:         my @pairs=split(/\&/,$rep);
                   2112:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   2113:             return @pairs;
                   2114:         }
                   2115:         my $i=0;
                   2116:         foreach my $item (@$storearr) {
                   2117:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   2118:             $i++;
                   2119:         }
                   2120:         return %returnhash;
                   2121:     } else {
1.880     banghart 2122:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  2123:     }
                   2124: }
                   2125: 
                   2126: # -------------------------------------------- put items in domain db files 
                   2127: 
                   2128: sub put_dom {
1.860     raeburn  2129:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   2130:     if (!$udom) {
                   2131:         $udom=$env{'user.domain'};
                   2132:         if (defined(&domain($udom,'primary'))) {
                   2133:             $uhome=&domain($udom,'primary');
                   2134:         } else {
1.874     albertel 2135:             undef($uhome);
1.860     raeburn  2136:         }
                   2137:     } else {
                   2138:         if (!$uhome) {
                   2139:             if (defined(&domain($udom,'primary'))) {
                   2140:                 $uhome=&domain($udom,'primary');
                   2141:             }
                   2142:         }
                   2143:     } 
                   2144:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  2145:         my $items='';
                   2146:         foreach my $item (keys(%$storehash)) {
                   2147:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   2148:         }
                   2149:         $items=~s/\&$//;
1.1344    raeburn  2150:         if ($namespace =~ /^enc/) {
                   2151:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
                   2152:         } else {
                   2153:             return &reply("putdom:$udom:$namespace:$items",$uhome);
                   2154:         }
1.806     raeburn  2155:     } else {
1.860     raeburn  2156:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  2157:     }
                   2158: }
                   2159: 
1.1023    raeburn  2160: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  2161: sub newput_dom {
1.1023    raeburn  2162:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  2163:     my $result;
                   2164:     if (!$udom) {
                   2165:         $udom=$env{'user.domain'};
                   2166:     }
1.1023    raeburn  2167:     if ($udom) {
                   2168:         my $uname = &get_domainconfiguser($udom);
                   2169:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  2170:     }
                   2171:     return $result;
                   2172: }
                   2173: 
1.1023    raeburn  2174: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  2175: sub del_dom {
1.1023    raeburn  2176:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  2177:     if (ref($storearr) eq 'ARRAY') {
                   2178:         if (!$udom) {
                   2179:             $udom=$env{'user.domain'};
                   2180:         }
1.1023    raeburn  2181:         if ($udom) {
                   2182:             my $uname = &get_domainconfiguser($udom); 
                   2183:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  2184:         }
                   2185:     }
                   2186: }
                   2187: 
1.1023    raeburn  2188: # ----------------------------------construct domainconfig user for a domain 
                   2189: sub get_domainconfiguser {
                   2190:     my ($udom) = @_;
                   2191:     return $udom.'-domainconfig';
                   2192: }
                   2193: 
1.837     raeburn  2194: sub retrieve_inst_usertypes {
                   2195:     my ($udom) = @_;
                   2196:     my (%returnhash,@order);
1.989     raeburn  2197:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   2198:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   2199:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256    raeburn  2200:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989     raeburn  2201:     } else {
                   2202:         if (defined(&domain($udom,'primary'))) {
                   2203:             my $uhome=&domain($udom,'primary');
                   2204:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   2205:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256    raeburn  2206:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989     raeburn  2207:                 return (\%returnhash,\@order);
                   2208:             }
                   2209:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   2210:             my @pairs=split(/\&/,$hashitems);
                   2211:             foreach my $item (@pairs) {
                   2212:                 my ($key,$value)=split(/=/,$item,2);
                   2213:                 $key = &unescape($key);
                   2214:                 next if ($key =~ /^error: 2 /);
                   2215:                 $returnhash{$key}=&thaw_unescape($value);
                   2216:             }
                   2217:             my @esc_order = split(/\&/,$orderitems);
                   2218:             foreach my $item (@esc_order) {
                   2219:                 push(@order,&unescape($item));
                   2220:             }
                   2221:         } else {
1.1256    raeburn  2222:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837     raeburn  2223:         }
1.1256    raeburn  2224:         return (\%returnhash,\@order);
1.837     raeburn  2225:     }
                   2226: }
                   2227: 
1.868     raeburn  2228: sub is_domainimage {
                   2229:     my ($url) = @_;
1.1306    raeburn  2230:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
1.868     raeburn  2231:         if (&domain($1) ne '') {
                   2232:             return '1';
                   2233:         }
                   2234:     }
                   2235:     return;
                   2236: }
                   2237: 
1.899     raeburn  2238: sub inst_directory_query {
                   2239:     my ($srch) = @_;
                   2240:     my $udom = $srch->{'srchdomain'};
                   2241:     my %results;
                   2242:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  2243:     my $outcome;
1.899     raeburn  2244:     if ($homeserver ne '') {
1.1357    raeburn  2245:         unless ($homeserver eq $perlvar{'lonHostID'}) {
                   2246:             if ($srch->{'srchby'} eq 'email') {
                   2247:                 my $lcrev = &get_server_loncaparev(undef,$homeserver);
                   2248:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2249:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2250:                     (($major == 2) && ($minor < 12))) {
                   2251:                     return;
                   2252:                 }
                   2253:             }
                   2254:         }
1.904     albertel 2255: 	my $queryid=&reply("querysend:instdirsearch:".
                   2256: 			   &escape($srch->{'srchby'}).':'.
                   2257: 			   &escape($srch->{'srchterm'}).':'.
                   2258: 			   &escape($srch->{'srchtype'}),$homeserver);
                   2259: 	my $host=&hostname($homeserver);
                   2260: 	if ($queryid !~/^\Q$host\E\_/) {
1.1343    raeburn  2261: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904     albertel 2262: 	    return;
                   2263: 	}
                   2264: 	my $response = &get_query_reply($queryid);
                   2265: 	my $maxtries = 5;
                   2266: 	my $tries = 1;
                   2267: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2268: 	    $response = &get_query_reply($queryid);
                   2269: 	    $tries ++;
                   2270: 	}
                   2271: 
                   2272:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  2273:             if ($response eq 'unavailable') {
                   2274:                 $outcome = $response;
                   2275:             } else {
                   2276:                 $outcome = 'ok';
                   2277:                 my @matches = split(/\n/,$response);
                   2278:                 foreach my $match (@matches) {
                   2279:                     my ($key,$value) = split(/=/,$match);
                   2280:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   2281:                 }
1.899     raeburn  2282:             }
                   2283:         }
                   2284:     }
1.909     raeburn  2285:     return ($outcome,%results);
1.899     raeburn  2286: }
                   2287: 
                   2288: sub usersearch {
                   2289:     my ($srch) = @_;
                   2290:     my $dom = $srch->{'srchdomain'};
                   2291:     my %results;
                   2292:     my %libserv = &all_library();
                   2293:     my $query = 'usersearch';
                   2294:     foreach my $tryserver (keys(%libserv)) {
                   2295:         if (&host_domain($tryserver) eq $dom) {
1.1357    raeburn  2296:             unless ($tryserver eq $perlvar{'lonHostID'}) {
                   2297:                 if ($srch->{'srchby'} eq 'email') {
                   2298:                     my $lcrev = &get_server_loncaparev(undef,$tryserver);
                   2299:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   2300:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
                   2301:                              (($major == 2) && ($minor < 12)));
                   2302:                 }
                   2303:             }
1.899     raeburn  2304:             my $host=&hostname($tryserver);
                   2305:             my $queryid=
1.911     raeburn  2306:                 &reply("querysend:".&escape($query).':'.
                   2307:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  2308:                        &escape($srch->{'srchtype'}).':'.
                   2309:                        &escape($srch->{'srchterm'}),$tryserver);
                   2310:             if ($queryid !~/^\Q$host\E\_/) {
                   2311:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  2312:                 next;
1.899     raeburn  2313:             }
                   2314:             my $reply = &get_query_reply($queryid);
                   2315:             my $maxtries = 1;
                   2316:             my $tries = 1;
                   2317:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   2318:                 $reply = &get_query_reply($queryid);
                   2319:                 $tries ++;
                   2320:             }
                   2321:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   2322:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   2323:             } else {
1.911     raeburn  2324:                 my @matches;
                   2325:                 if ($reply =~ /\n/) {
                   2326:                     @matches = split(/\n/,$reply);
                   2327:                 } else {
                   2328:                     @matches = split(/\&/,$reply);
                   2329:                 }
1.899     raeburn  2330:                 foreach my $match (@matches) {
                   2331:                     my ($uname,$udom,%userhash);
1.911     raeburn  2332:                     foreach my $entry (split(/:/,$match)) {
                   2333:                         my ($key,$value) =
                   2334:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  2335:                         $userhash{$key} = $value;
                   2336:                         if ($key eq 'username') {
                   2337:                             $uname = $value;
                   2338:                         } elsif ($key eq 'domain') {
                   2339:                             $udom = $value;
1.911     raeburn  2340:                         }
1.899     raeburn  2341:                     }
                   2342:                     $results{$uname.':'.$udom} = \%userhash;
                   2343:                 }
                   2344:             }
                   2345:         }
                   2346:     }
                   2347:     return %results;
                   2348: }
                   2349: 
1.912     raeburn  2350: sub get_instuser {
                   2351:     my ($udom,$uname,$id) = @_;
                   2352:     my $homeserver = &domain($udom,'primary');
                   2353:     my ($outcome,%results);
                   2354:     if ($homeserver ne '') {
                   2355:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   2356:                            &escape($id).':'.&escape($udom),$homeserver);
                   2357:         my $host=&hostname($homeserver);
                   2358:         if ($queryid !~/^\Q$host\E\_/) {
                   2359:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   2360:             return;
                   2361:         }
                   2362:         my $response = &get_query_reply($queryid);
                   2363:         my $maxtries = 5;
                   2364:         my $tries = 1;
                   2365:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2366:             $response = &get_query_reply($queryid);
                   2367:             $tries ++;
                   2368:         }
                   2369:         if (!&error($response) && $response ne 'refused') {
                   2370:             if ($response eq 'unavailable') {
                   2371:                 $outcome = $response;
                   2372:             } else {
                   2373:                 $outcome = 'ok';
                   2374:                 my @matches = split(/\n/,$response);
                   2375:                 foreach my $match (@matches) {
                   2376:                     my ($key,$value) = split(/=/,$match);
                   2377:                     $results{&unescape($key)} = &thaw_unescape($value);
                   2378:                 }
                   2379:             }
                   2380:         }
                   2381:     }
                   2382:     my %userinfo;
                   2383:     if (ref($results{$uname}) eq 'HASH') {
                   2384:         %userinfo = %{$results{$uname}};
                   2385:     } 
                   2386:     return ($outcome,%userinfo);
                   2387: }
                   2388: 
1.1290    raeburn  2389: sub get_multiple_instusers {
                   2390:     my ($udom,$users,$caller) = @_;
                   2391:     my ($outcome,$results);
                   2392:     if (ref($users) eq 'HASH') {
                   2393:         my $count = keys(%{$users}); 
                   2394:         my $requested = &freeze_escape($users);
                   2395:         my $homeserver = &domain($udom,'primary');
                   2396:         if ($homeserver ne '') {
                   2397:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
                   2398:             my $host=&hostname($homeserver);
                   2399:             if ($queryid !~/^\Q$host\E\_/) {
                   2400:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
                   2401:                          ' for host: '.$homeserver.'in domain '.$udom);
                   2402:                 return ($outcome,$results);
                   2403:             }
                   2404:             my $response = &get_query_reply($queryid);
                   2405:             my $maxtries = 5;
                   2406:             if ($count > 100) {
                   2407:                 $maxtries = 1+int($count/20);
                   2408:             }
                   2409:             my $tries = 1;
                   2410:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
                   2411:                 $response = &get_query_reply($queryid);
                   2412:                 $tries ++;
                   2413:             }
                   2414:             if ($response eq '') {
                   2415:                 $results = {};
                   2416:                 foreach my $key (keys(%{$users})) {
                   2417:                     my ($uname,$id);
                   2418:                     if ($caller eq 'id') {
                   2419:                         $id = $key;
                   2420:                     } else {
                   2421:                         $uname = $key;
                   2422:                     }
                   2423:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291    raeburn  2424:                     $outcome = $resp;
1.1290    raeburn  2425:                     if ($resp eq 'ok') {
                   2426:                         %{$results} = (%{$results}, %info);
                   2427:                     } else {
                   2428:                         last;
                   2429:                     }
                   2430:                 }
                   2431:             } elsif(!&error($response) && ($response ne 'refused')) {
                   2432:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
                   2433:                     $outcome = $response;
                   2434:                 } else {
1.1291    raeburn  2435:                     ($outcome,my $userdata) = split(/=/,$response,2);
1.1290    raeburn  2436:                     if ($outcome eq 'ok') {
                   2437:                         $results = &thaw_unescape($userdata); 
                   2438:                     }
                   2439:                 }
                   2440:             }
                   2441:         }
                   2442:     }
                   2443:     return ($outcome,$results);
                   2444: }
                   2445: 
1.912     raeburn  2446: sub inst_rulecheck {
1.923     raeburn  2447:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  2448:     my %returnhash;
                   2449:     if ($udom ne '') {
                   2450:         if (ref($rules) eq 'ARRAY') {
                   2451:             @{$rules} = map {&escape($_);} (@{$rules});
                   2452:             my $rulestr = join(':',@{$rules});
                   2453:             my $homeserver=&domain($udom,'primary');
                   2454:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2455:                 my $response;
                   2456:                 if ($item eq 'username') {                
                   2457:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   2458:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  2459:                                               $homeserver));
1.923     raeburn  2460:                 } elsif ($item eq 'id') {
                   2461:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   2462:                                               ':'.&escape($id).':'.$rulestr,
                   2463:                                               $homeserver));
1.945     raeburn  2464:                 } elsif ($item eq 'selfcreate') {
                   2465:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  2466:                                                &escape($udom).':'.&escape($uname).
                   2467:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  2468:                 }
1.912     raeburn  2469:                 if ($response ne 'refused') {
                   2470:                     my @pairs=split(/\&/,$response);
                   2471:                     foreach my $item (@pairs) {
                   2472:                         my ($key,$value)=split(/=/,$item,2);
                   2473:                         $key = &unescape($key);
                   2474:                         next if ($key =~ /^error: 2 /);
                   2475:                         $returnhash{$key}=&thaw_unescape($value);
                   2476:                     }
                   2477:                 }
                   2478:             }
                   2479:         }
                   2480:     }
                   2481:     return %returnhash;
                   2482: }
                   2483: 
                   2484: sub inst_userrules {
1.923     raeburn  2485:     my ($udom,$check) = @_;
1.912     raeburn  2486:     my (%ruleshash,@ruleorder);
                   2487:     if ($udom ne '') {
                   2488:         my $homeserver=&domain($udom,'primary');
                   2489:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2490:             my $response;
                   2491:             if ($check eq 'id') {
                   2492:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  2493:                                  $homeserver);
1.943     raeburn  2494:             } elsif ($check eq 'email') {
                   2495:                 $response=&reply('instemailrules:'.&escape($udom),
                   2496:                                  $homeserver);
1.923     raeburn  2497:             } else {
                   2498:                 $response=&reply('instuserrules:'.&escape($udom),
                   2499:                                  $homeserver);
                   2500:             }
1.912     raeburn  2501:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  2502:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  2503:                 ($response ne 'no_such_host')) {
                   2504:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   2505:                 my @pairs=split(/\&/,$hashitems);
                   2506:                 foreach my $item (@pairs) {
                   2507:                     my ($key,$value)=split(/=/,$item,2);
                   2508:                     $key = &unescape($key);
                   2509:                     next if ($key =~ /^error: 2 /);
                   2510:                     $ruleshash{$key}=&thaw_unescape($value);
                   2511:                 }
                   2512:                 my @esc_order = split(/\&/,$orderitems);
                   2513:                 foreach my $item (@esc_order) {
                   2514:                     push(@ruleorder,&unescape($item));
                   2515:                 }
                   2516:             }
                   2517:         }
                   2518:     }
                   2519:     return (\%ruleshash,\@ruleorder);
                   2520: }
                   2521: 
1.976     raeburn  2522: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  2523: 
                   2524: sub get_domain_defaults {
1.1240    raeburn  2525:     my ($domain,$ignore_cache) = @_;
1.1242    raeburn  2526:     return if (($domain eq '') || ($domain eq 'public'));
1.943     raeburn  2527:     my $cachetime = 60*60*24;
1.1240    raeburn  2528:     unless ($ignore_cache) {
                   2529:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   2530:         if (defined($cached)) {
                   2531:             if (ref($result) eq 'HASH') {
                   2532:                 return %{$result};
                   2533:             }
1.943     raeburn  2534:         }
                   2535:     }
                   2536:     my %domdefaults;
                   2537:     my %domconfig =
1.989     raeburn  2538:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  2539:                                   'requestcourses','inststatus',
1.1183    raeburn  2540:                                   'coursedefaults','usersessions',
1.1258    raeburn  2541:                                   'requestauthor','selfenrollment',
1.1320    raeburn  2542:                                   'coursecategories','ssl','autoenroll',
1.1332    raeburn  2543:                                   'trust','helpsettings'],$domain);
1.1305    raeburn  2544:     my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943     raeburn  2545:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   2546:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   2547:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   2548:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  2549:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  2550:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  2551:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1340    raeburn  2552:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
                   2553:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
                   2554:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.943     raeburn  2555:     } else {
                   2556:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   2557:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   2558:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   2559:     }
1.976     raeburn  2560:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   2561:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   2562:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   2563:         } else {
                   2564:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229    raeburn  2565:         }
1.1177    raeburn  2566:         my @usertools = ('aboutme','blog','webdav','portfolio');
1.976     raeburn  2567:         foreach my $item (@usertools) {
                   2568:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   2569:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   2570:             }
                   2571:         }
1.1229    raeburn  2572:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   2573:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
                   2574:         }
1.976     raeburn  2575:     }
1.985     raeburn  2576:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305    raeburn  2577:         foreach my $item ('official','unofficial','community','textbook','placement') {
1.985     raeburn  2578:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2579:         }
                   2580:     }
1.1183    raeburn  2581:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2582:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2583:     }
1.989     raeburn  2584:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256    raeburn  2585:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989     raeburn  2586:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2587:         }
                   2588:     }
1.1047    raeburn  2589:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230    raeburn  2590:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277    raeburn  2591:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
                   2592:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
                   2593:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   2594:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
                   2595:         }
1.1254    raeburn  2596:         foreach my $type (@coursetypes) {
                   2597:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2598:                 unless ($type eq 'community') {
                   2599:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
                   2600:                 }
                   2601:             }
                   2602:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   2603:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   2604:             }
1.1277    raeburn  2605:             if ($domdefaults{'postsubmit'} eq 'on') {
                   2606:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   2607:                     $domdefaults{$type.'postsubtimeout'} = 
                   2608:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
                   2609:                 }
                   2610:             }
1.1230    raeburn  2611:         }
1.1286    raeburn  2612:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   2613:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   2614:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
                   2615:                 if (@clonecodes) {
                   2616:                     $domdefaults{'canclone'} = join('+',@clonecodes);
                   2617:                 }
                   2618:             }
                   2619:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
                   2620:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
                   2621:         }
1.1356    raeburn  2622:         if ($domconfig{'coursedefaults'}{'texengine'}) {
                   2623:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
                   2624:         } 
1.1047    raeburn  2625:     }
1.1073    raeburn  2626:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2627:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2628:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2629:         }
                   2630:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2631:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2632:         }
1.1279    raeburn  2633:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   2634:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
                   2635:         }
1.1073    raeburn  2636:     }
1.1254    raeburn  2637:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                   2638:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   2639:             my @settings = ('types','registered','enroll_dates','access_dates','section',
                   2640:                             'approval','limit');
                   2641:             foreach my $type (@coursetypes) {
                   2642:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   2643:                     my @mgrdc = ();
                   2644:                     foreach my $item (@settings) {
                   2645:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
                   2646:                             push(@mgrdc,$item);
                   2647:                         }
                   2648:                     }
                   2649:                     if (@mgrdc) {
                   2650:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   2651:                     }
                   2652:                 }
                   2653:             }
                   2654:         }
                   2655:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   2656:             foreach my $type (@coursetypes) {
                   2657:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   2658:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
                   2659:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
                   2660:                     }
                   2661:                 }
                   2662:             }
                   2663:         }
                   2664:     }
1.1258    raeburn  2665:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2666:         $domdefaults{'catauth'} = 'std';
                   2667:         $domdefaults{'catunauth'} = 'std';
                   2668:         if ($domconfig{'coursecategories'}{'auth'}) { 
                   2669:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   2670:         }
                   2671:         if ($domconfig{'coursecategories'}{'unauth'}) {
                   2672:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   2673:         }
                   2674:     }
1.1315    raeburn  2675:     if (ref($domconfig{'ssl'}) eq 'HASH') {
                   2676:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
                   2677:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
                   2678:         }
1.1338    raeburn  2679:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
                   2680:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
                   2681:         }
                   2682:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
                   2683:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315    raeburn  2684:         }
                   2685:     }
1.1320    raeburn  2686:     if (ref($domconfig{'trust'}) eq 'HASH') {
                   2687:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
                   2688:         foreach my $prefix (@prefixes) {
                   2689:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
                   2690:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
                   2691:             }
                   2692:         }
                   2693:     }
1.1314    raeburn  2694:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   2695:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
                   2696:     }
1.1332    raeburn  2697:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   2698:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
                   2699:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   2700:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
                   2701:         }
                   2702:     }
1.1219    raeburn  2703:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  2704:     return %domdefaults;
                   2705: }
                   2706: 
1.1311    raeburn  2707: sub course_portal_url {
                   2708:     my ($cnum,$cdom) = @_;
                   2709:     my $chome = &homeserver($cnum,$cdom);
                   2710:     my $hostname = &hostname($chome);
                   2711:     my $protocol = $protocol{$chome};
                   2712:     $protocol = 'http' if ($protocol ne 'https');
                   2713:     my %domdefaults = &get_domain_defaults($cdom);
                   2714:     my $firsturl;
                   2715:     if ($domdefaults{'portal_def'}) {
                   2716:         $firsturl = $domdefaults{'portal_def'};
                   2717:     } else {
                   2718:         $firsturl = $protocol.'://'.$hostname;
                   2719:     }
                   2720:     return $firsturl;
                   2721: }
                   2722: 
1.344     www      2723: # --------------------------------------------------- Assign a key to a student
                   2724: 
                   2725: sub assign_access_key {
1.364     www      2726: #
                   2727: # a valid key looks like uname:udom#comments
                   2728: # comments are being appended
                   2729: #
1.498     www      2730:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   2731:     $kdom=
1.620     albertel 2732:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      2733:     $knum=
1.620     albertel 2734:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      2735:     $cdom=
1.620     albertel 2736:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2737:     $cnum=
1.620     albertel 2738:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2739:     $udom=$env{'user.name'} unless (defined($udom));
                   2740:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      2741:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      2742:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 2743:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      2744:                                                   # assigned to this person
                   2745:                                                   # - this should not happen,
1.345     www      2746:                                                   # unless something went wrong
                   2747:                                                   # the first time around
                   2748: # ready to assign
1.364     www      2749:         $logentry=$1.'; '.$logentry;
1.496     www      2750:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      2751:                                                  $kdom,$knum) eq 'ok') {
1.345     www      2752: # key now belongs to user
1.346     www      2753: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      2754:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  2755:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      2756:                 return 'ok';
                   2757:             } else {
                   2758:                 return 
                   2759:   'error: Count not permanently assign key, will need to be re-entered later.';
                   2760: 	    }
                   2761:         } else {
                   2762:             return 'error: Could not assign key, try again later.';
                   2763:         }
1.364     www      2764:     } elsif (!$existing{$ckey}) {
1.345     www      2765: # the key does not exist
                   2766: 	return 'error: The key does not exist';
                   2767:     } else {
                   2768: # the key is somebody else's
                   2769: 	return 'error: The key is already in use';
                   2770:     }
1.344     www      2771: }
                   2772: 
1.364     www      2773: # ------------------------------------------ put an additional comment on a key
                   2774: 
                   2775: sub comment_access_key {
                   2776: #
                   2777: # a valid key looks like uname:udom#comments
                   2778: # comments are being appended
                   2779: #
                   2780:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   2781:     $cdom=
1.620     albertel 2782:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      2783:     $cnum=
1.620     albertel 2784:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      2785:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   2786:     if ($existing{$ckey}) {
                   2787:         $existing{$ckey}.='; '.$logentry;
                   2788: # ready to assign
1.367     www      2789:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      2790:                                                  $cdom,$cnum) eq 'ok') {
                   2791: 	    return 'ok';
                   2792:         } else {
                   2793: 	    return 'error: Count not store comment.';
                   2794:         }
                   2795:     } else {
                   2796: # the key does not exist
                   2797: 	return 'error: The key does not exist';
                   2798:     }
                   2799: }
                   2800: 
1.344     www      2801: # ------------------------------------------------------ Generate a set of keys
                   2802: 
                   2803: sub generate_access_keys {
1.364     www      2804:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      2805:     $cdom=
1.620     albertel 2806:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2807:     $cnum=
1.620     albertel 2808:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      2809:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      2810:     unless (($cdom) && ($cnum)) { return 0; }
                   2811:     if ($number>10000) { return 0; }
                   2812:     sleep(2); # make sure don't get same seed twice
                   2813:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   2814:     my $total=0;
                   2815:     for (my $i=1;$i<=$number;$i++) {
                   2816:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   2817:                   sprintf("%lx",int(100000*rand)).'-'.
                   2818:                   sprintf("%lx",int(100000*rand));
                   2819:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   2820:        $newkey=~s/0/h/g; # and also 0 and O
                   2821:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   2822:        if ($existing{$newkey}) {
                   2823:            $i--;
                   2824:        } else {
1.364     www      2825: 	  if (&put('accesskeys',
                   2826:               { $newkey => '# generated '.localtime().
1.620     albertel 2827:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      2828:                            '; '.$logentry },
                   2829: 		   $cdom,$cnum) eq 'ok') {
1.344     www      2830:               $total++;
                   2831: 	  }
                   2832:        }
                   2833:     }
1.620     albertel 2834:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      2835:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   2836:     return $total;
                   2837: }
                   2838: 
                   2839: # ------------------------------------------------------- Validate an accesskey
                   2840: 
                   2841: sub validate_access_key {
                   2842:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   2843:     $cdom=
1.620     albertel 2844:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2845:     $cnum=
1.620     albertel 2846:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2847:     $udom=$env{'user.domain'} unless (defined($udom));
                   2848:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      2849:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 2850:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      2851: }
                   2852: 
                   2853: # ------------------------------------- Find the section of student in a course
1.652     albertel 2854: sub devalidate_getsection_cache {
                   2855:     my ($udom,$unam,$courseid)=@_;
                   2856:     my $hashid="$udom:$unam:$courseid";
                   2857:     &devalidate_cache_new('getsection',$hashid);
                   2858: }
1.298     matthew  2859: 
1.815     albertel 2860: sub courseid_to_courseurl {
                   2861:     my ($courseid) = @_;
                   2862:     #already url style courseid
                   2863:     return $courseid if ($courseid =~ m{^/});
                   2864: 
                   2865:     if (exists($env{'course.'.$courseid.'.num'})) {
                   2866: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   2867: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   2868: 	return "/$cdom/$cnum";
                   2869:     }
                   2870: 
                   2871:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   2872:     if (exists($courseinfo{'num'})) {
                   2873: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   2874:     }
                   2875: 
                   2876:     return undef;
                   2877: }
                   2878: 
1.298     matthew  2879: sub getsection {
                   2880:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 2881:     my $cachetime=1800;
1.551     albertel 2882: 
                   2883:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 2884:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 2885:     if (defined($cached)) { return $result; }
                   2886: 
1.298     matthew  2887:     my %Pending; 
                   2888:     my %Expired;
                   2889:     #
                   2890:     # Each role can either have not started yet (pending), be active, 
                   2891:     #    or have expired.
                   2892:     #
                   2893:     # If there is an active role, we are done.
                   2894:     #
                   2895:     # If there is more than one role which has not started yet, 
                   2896:     #     choose the one which will start sooner
                   2897:     # If there is one role which has not started yet, return it.
                   2898:     #
                   2899:     # If there is more than one expired role, choose the one which ended last.
                   2900:     # If there is a role which has expired, return it.
                   2901:     #
1.815     albertel 2902:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  2903:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  2904:     foreach my $key (keys(%roleshash)) {
1.479     albertel 2905:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  2906:         my $section=$1;
                   2907:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  2908:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  2909:         my $now=time;
1.548     albertel 2910:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  2911:             $Expired{$end}=$section;
                   2912:             next;
                   2913:         }
1.548     albertel 2914:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  2915:             $Pending{$start}=$section;
                   2916:             next;
                   2917:         }
1.599     albertel 2918:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  2919:     }
                   2920:     #
                   2921:     # Presumedly there will be few matching roles from the above
                   2922:     # loop and the sorting time will be negligible.
                   2923:     if (scalar(keys(%Pending))) {
                   2924:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 2925:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  2926:     } 
                   2927:     if (scalar(keys(%Expired))) {
                   2928:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   2929:         my $time = pop(@sorted);
1.599     albertel 2930:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  2931:     }
1.599     albertel 2932:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  2933: }
1.70      www      2934: 
1.599     albertel 2935: sub save_cache {
                   2936:     &purge_remembered();
1.722     albertel 2937:     #&Apache::loncommon::validate_page();
1.620     albertel 2938:     undef(%env);
1.780     albertel 2939:     undef($env_loaded);
1.599     albertel 2940: }
1.452     albertel 2941: 
1.599     albertel 2942: my $to_remember=-1;
                   2943: my %remembered;
                   2944: my %accessed;
                   2945: my $kicks=0;
                   2946: my $hits=0;
1.849     albertel 2947: sub make_key {
                   2948:     my ($name,$id) = @_;
1.872     albertel 2949:     if (length($id) > 65 
                   2950: 	&& length(&escape($id)) > 200) {
                   2951: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   2952:     }
1.849     albertel 2953:     return &escape($name.':'.$id);
                   2954: }
                   2955: 
1.599     albertel 2956: sub devalidate_cache_new {
                   2957:     my ($name,$id,$debug) = @_;
                   2958:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319    damieng  2959:     my $remembered_id=$name.':'.$id;
1.849     albertel 2960:     $id=&make_key($name,$id);
1.599     albertel 2961:     $memcache->delete($id);
1.1319    damieng  2962:     delete($remembered{$remembered_id});
                   2963:     delete($accessed{$remembered_id});
1.599     albertel 2964: }
                   2965: 
                   2966: sub is_cached_new {
                   2967:     my ($name,$id,$debug) = @_;
1.1319    damieng  2968:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
                   2969:     if (exists($remembered{$remembered_id})) {
                   2970: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
                   2971: 	$accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 2972: 	$hits++;
1.1319    damieng  2973: 	return ($remembered{$remembered_id},1);
1.599     albertel 2974:     }
1.1319    damieng  2975:     $id=&make_key($name,$id);
1.599     albertel 2976:     my $value = $memcache->get($id);
                   2977:     if (!(defined($value))) {
                   2978: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 2979: 	return (undef,undef);
1.416     albertel 2980:     }
1.599     albertel 2981:     if ($value eq '__undef__') {
                   2982: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   2983: 	$value=undef;
                   2984:     }
1.1319    damieng  2985:     &make_room($remembered_id,$value,$debug);
1.599     albertel 2986:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   2987:     return ($value,1);
                   2988: }
                   2989: 
                   2990: sub do_cache_new {
                   2991:     my ($name,$id,$value,$time,$debug) = @_;
1.1319    damieng  2992:     my $remembered_id=$name.':'.$id;
1.849     albertel 2993:     $id=&make_key($name,$id);
1.599     albertel 2994:     my $setvalue=$value;
                   2995:     if (!defined($setvalue)) {
                   2996: 	$setvalue='__undef__';
                   2997:     }
1.623     albertel 2998:     if (!defined($time) ) {
                   2999: 	$time=600;
                   3000:     }
1.599     albertel 3001:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 3002:     my $result = $memcache->set($id,$setvalue,$time);
                   3003:     if (! $result) {
1.872     albertel 3004: 	&logthis("caching of id -> $id  failed");
1.910     albertel 3005: 	$memcache->disconnect_all();
1.872     albertel 3006:     }
1.600     albertel 3007:     # need to make a copy of $value
1.1319    damieng  3008:     &make_room($remembered_id,$value,$debug);
1.599     albertel 3009:     return $value;
                   3010: }
                   3011: 
                   3012: sub make_room {
1.1319    damieng  3013:     my ($remembered_id,$value,$debug)=@_;
1.919     albertel 3014: 
1.1319    damieng  3015:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919     albertel 3016:                                     : $value;
1.599     albertel 3017:     if ($to_remember<0) { return; }
1.1319    damieng  3018:     $accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 3019:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   3020:     my $to_kick;
                   3021:     my $max_time=0;
                   3022:     foreach my $other (keys(%accessed)) {
                   3023: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   3024: 	    $to_kick=$other;
                   3025: 	    $max_time=&tv_interval($accessed{$other});
                   3026: 	}
                   3027:     }
                   3028:     delete($remembered{$to_kick});
                   3029:     delete($accessed{$to_kick});
                   3030:     $kicks++;
                   3031:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 3032:     return;
                   3033: }
                   3034: 
1.599     albertel 3035: sub purge_remembered {
1.604     albertel 3036:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   3037:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 3038:     undef(%remembered);
                   3039:     undef(%accessed);
1.428     albertel 3040: }
1.70      www      3041: # ------------------------------------- Read an entry from a user's environment
                   3042: 
                   3043: sub userenvironment {
                   3044:     my ($udom,$unam,@what)=@_;
1.976     raeburn  3045:     my $items;
                   3046:     foreach my $item (@what) {
                   3047:         $items.=&escape($item).'&';
                   3048:     }
                   3049:     $items=~s/\&$//;
1.70      www      3050:     my %returnhash=();
1.1009    raeburn  3051:     my $uhome = &homeserver($unam,$udom);
                   3052:     unless ($uhome eq 'no_host') {
                   3053:         my @answer=split(/\&/, 
                   3054:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  3055:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   3056:             return %returnhash;
                   3057:         }
1.1009    raeburn  3058:         my $i;
                   3059:         for ($i=0;$i<=$#what;$i++) {
                   3060: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   3061:         }
1.70      www      3062:     }
                   3063:     return %returnhash;
1.1       albertel 3064: }
                   3065: 
1.617     albertel 3066: # ---------------------------------------------------------- Get a studentphoto
                   3067: sub studentphoto {
                   3068:     my ($udom,$unam,$ext) = @_;
                   3069:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  3070:     if (defined($env{'request.course.id'})) {
1.708     raeburn  3071:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  3072:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   3073:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   3074:             } else {
                   3075:                 my ($result,$perm_reqd)=
1.707     albertel 3076: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  3077:                 if ($result eq 'ok') {
                   3078:                     if (!($perm_reqd eq 'yes')) {
                   3079:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   3080:                     }
                   3081:                 }
                   3082:             }
                   3083:         }
                   3084:     } else {
                   3085:         my ($result,$perm_reqd) = 
1.707     albertel 3086: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  3087:         if ($result eq 'ok') {
                   3088:             if (!($perm_reqd eq 'yes')) {
                   3089:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   3090:             }
                   3091:         }
                   3092:     }
                   3093:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   3094: }
                   3095: 
                   3096: sub retrievestudentphoto {
                   3097:     my ($udom,$unam,$ext,$type) = @_;
                   3098:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   3099:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   3100:     if ($ret eq 'ok') {
                   3101:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   3102:         if ($type eq 'thumbnail') {
                   3103:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   3104:         }
                   3105:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   3106:         return $tokenurl;
                   3107:     } else {
                   3108:         if ($type eq 'thumbnail') {
                   3109:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   3110:         } else { 
                   3111:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   3112:         }
1.617     albertel 3113:     }
                   3114: }
                   3115: 
1.263     www      3116: # -------------------------------------------------------------------- New chat
                   3117: 
                   3118: sub chatsend {
1.724     raeburn  3119:     my ($newentry,$anon,$group)=@_;
1.620     albertel 3120:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3121:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3122:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      3123:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 3124: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  3125: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      3126: }
                   3127: 
                   3128: # ------------------------------------------ Find current version of a resource
                   3129: 
                   3130: sub getversion {
                   3131:     my $fname=&clutter(shift);
1.1189    raeburn  3132:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      3133:     return &currentversion(&filelocation('',$fname));
                   3134: }
                   3135: 
                   3136: sub currentversion {
                   3137:     my $fname=shift;
                   3138:     my $author=$fname;
                   3139:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3140:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      3141:     my $home=&homeserver($uname,$udom);
1.292     www      3142:     if ($home eq 'no_host') { 
                   3143:         return -1; 
                   3144:     }
1.1112    www      3145:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      3146:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3147: 	return -1;
                   3148:     }
1.1112    www      3149:     return $answer;
1.263     www      3150: }
                   3151: 
1.1111    www      3152: #
                   3153: # Return special version number of resource if set by override, empty otherwise
                   3154: #
                   3155: sub usedversion {
                   3156:     my $fname=shift;
                   3157:     unless ($fname) { $fname=$env{'request.uri'}; }
                   3158:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   3159:     if ($urlversion) { return $urlversion; }
                   3160:     return '';
                   3161: }
                   3162: 
1.1       albertel 3163: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      3164: 
1.1       albertel 3165: sub subscribe {
                   3166:     my $fname=shift;
1.761     raeburn  3167:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 3168:     $fname=~s/[\n\r]//g;
1.1       albertel 3169:     my $author=$fname;
                   3170:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3171:     my ($udom,$uname)=split(/\//,$author);
                   3172:     my $home=homeserver($uname,$udom);
1.335     albertel 3173:     if ($home eq 'no_host') {
                   3174:         return 'not_found';
1.1       albertel 3175:     }
                   3176:     my $answer=reply("sub:$fname",$home);
1.64      www      3177:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   3178: 	$answer.=' by '.$home;
                   3179:     }
1.1       albertel 3180:     return $answer;
                   3181: }
                   3182:     
1.8       www      3183: # -------------------------------------------------------------- Replicate file
                   3184: 
                   3185: sub repcopy {
                   3186:     my $filename=shift;
1.23      www      3187:     $filename=~s/\/+/\//g;
1.1142    raeburn  3188:     my $londocroot = $perlvar{'lonDocRoot'};
                   3189:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  3190:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  3191:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   3192: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 3193: 	return &repcopy_userfile($filename);
                   3194:     }
1.532     albertel 3195:     $filename=~s/[\n\r]//g;
1.8       www      3196:     my $transname="$filename.in.transfer";
1.828     www      3197: # FIXME: this should flock
1.607     raeburn  3198:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      3199:     my $remoteurl=subscribe($filename);
1.64      www      3200:     if ($remoteurl =~ /^con_lost by/) {
                   3201: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3202:            return 'unavailable';
1.8       www      3203:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 3204: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  3205: 	   return 'not_found';
1.64      www      3206:     } elsif ($remoteurl =~ /^rejected by/) {
                   3207: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  3208:            return 'forbidden';
1.20      www      3209:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  3210:            return 'ok';
1.8       www      3211:     } else {
1.290     www      3212:         my $author=$filename;
                   3213:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   3214:         my ($udom,$uname)=split(/\//,$author);
                   3215:         my $home=homeserver($uname,$udom);
                   3216:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      3217:            my @parts=split(/\//,$filename);
                   3218:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  3219:            if ($path ne "$londocroot/res") {
1.8       www      3220:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  3221: 	       return 'bad_request';
1.8       www      3222:            }
                   3223:            my $count;
                   3224:            for ($count=5;$count<$#parts;$count++) {
                   3225:                $path.="/$parts[$count]";
                   3226:                if ((-e $path)!=1) {
                   3227: 		   mkdir($path,0777);
                   3228:                }
                   3229:            }
                   3230:            my $request=new HTTP::Request('GET',"$remoteurl");
1.1345    raeburn  3231:            my $response;
                   3232:            if ($remoteurl =~ m{/raw/}) {
                   3233:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
                   3234:            } else {
                   3235:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
                   3236:            }
1.8       www      3237:            if ($response->is_error()) {
                   3238: 	       unlink($transname);
                   3239:                my $message=$response->status_line;
1.672     albertel 3240:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      3241:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  3242:                return 'unavailable';
1.8       www      3243:            } else {
1.16      www      3244: 	       if ($remoteurl!~/\.meta$/) {
                   3245:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1.1345    raeburn  3246:                   my $mresponse;
                   3247:                   if ($remoteurl =~ m{/raw/}) {
                   3248:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
                   3249:                   } else {
                   3250:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
                   3251:                   }
1.16      www      3252:                   if ($mresponse->is_error()) {
                   3253: 		      unlink($filename.'.meta');
                   3254:                       &logthis(
1.672     albertel 3255:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      3256:                   }
                   3257: 	       }
1.8       www      3258:                rename($transname,$filename);
1.607     raeburn  3259:                return 'ok';
1.8       www      3260:            }
1.290     www      3261:        }
1.8       www      3262:     }
1.330     www      3263: }
                   3264: 
                   3265: # ------------------------------------------------ Get server side include body
                   3266: sub ssi_body {
1.381     albertel 3267:     my ($filelink,%form)=@_;
1.606     matthew  3268:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   3269:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   3270:     }
1.953     www      3271:     my $output='';
                   3272:     my $response;
1.980     raeburn  3273:     if ($filelink=~/^https?\:/) {
1.954     raeburn  3274:        ($output,$response)=&externalssi($filelink);
1.953     www      3275:     } else {
1.1004    droeschl 3276:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   3277:        $filelink .= 'inhibitmenu=yes';
1.953     www      3278:        ($output,$response)=&ssi($filelink,%form);
                   3279:     }
1.778     albertel 3280:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 3281:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 3282:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      3283:     if (wantarray) {
                   3284:         return ($output, $response);
                   3285:     } else {
                   3286:         return $output;
                   3287:     }
1.8       www      3288: }
                   3289: 
1.15      www      3290: # --------------------------------------------------------- Server Side Include
                   3291: 
1.782     albertel 3292: sub absolute_url {
                   3293:     my ($host_name) = @_;
                   3294:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   3295:     if ($host_name eq '') {
                   3296: 	$host_name = $ENV{'SERVER_NAME'};
                   3297:     }
                   3298:     return $protocol.$host_name;
                   3299: }
                   3300: 
1.942     foxr     3301: #
                   3302: #   Server side include.
                   3303: # Parameters:
                   3304: #  fn     Possibly encrypted resource name/id.
                   3305: #  form   Hash that describes how the rendering should be done
                   3306: #         and other things.
1.944     foxr     3307: # Returns:
1.950     raeburn  3308: #   Scalar context: The content of the response.
                   3309: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     3310: #     
1.15      www      3311: sub ssi {
                   3312: 
1.944     foxr     3313:     my ($fn,%form)=@_;
1.23      www      3314:     my $request;
1.711     albertel 3315: 
                   3316:     $form{'no_update_last_known'}=1;
1.895     albertel 3317:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      3318:     if (%form) {
1.782     albertel 3319:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1272    droeschl 3320:       $request->content(join('&',map { 
                   3321:             my $name = escape($_);
                   3322:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
                   3323:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
                   3324:             : &escape($form{$_}) );    
                   3325:         } keys(%form)));
1.23      www      3326:     } else {
1.782     albertel 3327:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      3328:     }
                   3329: 
1.15      www      3330:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1345    raeburn  3331:     my $lonhost = $perlvar{'lonHostID'};
1.1385    raeburn  3332:     my $islocal;
                   3333:     if (($env{'request.course.id'}) &&
                   3334:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
                   3335:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
                   3336:         ($form{'grade_symb'} ne '') &&
                   3337:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
                   3338:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
                   3339:         $islocal = 1;
                   3340:     }
1.1384    raeburn  3341:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
1.1385    raeburn  3342:                                                 '','','',$islocal);
1.1182    foxr     3343: 
1.944     foxr     3344:     if (wantarray) {
1.1345    raeburn  3345: 	return ($response->content, $response);
1.944     foxr     3346:     } else {
1.1345    raeburn  3347: 	return $response->content;
1.942     foxr     3348:     }
1.324     www      3349: }
                   3350: 
                   3351: sub externalssi {
                   3352:     my ($url)=@_;
                   3353:     my $request=new HTTP::Request('GET',$url);
1.1345    raeburn  3354:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
1.954     raeburn  3355:     if (wantarray) {
                   3356:         return ($response->content, $response);
                   3357:     } else {
                   3358:         return $response->content;
                   3359:     }
1.15      www      3360: }
1.254     www      3361: 
1.1354    raeburn  3362: 
                   3363: # If the local copy of a replicated resource is outdated, trigger a  
                   3364: # connection from the homeserver to flush the delayed queue. If no update 
                   3365: # happens, remove local copies of outdated resource (and corresponding
                   3366: # metadata file).
                   3367: 
1.1352    raeburn  3368: sub remove_stale_resfile {
                   3369:     my ($url) = @_;
1.1354    raeburn  3370:     my $removed;
1.1352    raeburn  3371:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
                   3372:         my $audom = $1;
                   3373:         my $auname = $2;
1.1353    raeburn  3374:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
1.1352    raeburn  3375:             my $homeserver = &homeserver($auname,$audom);
                   3376:             unless (($homeserver eq 'no_host') ||
                   3377:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
                   3378:                 my $fname = &filelocation('',$url);
                   3379:                 if (-e $fname) {
                   3380:                     my $hostname = &hostname($homeserver);
                   3381:                     if ($hostname) {
1.1397    raeburn  3382:                         my $protocol = $protocol{$homeserver};
                   3383:                         $protocol = 'http' if ($protocol ne 'https');
1.1352    raeburn  3384:                         my $uri = &declutter($url);
                   3385:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
                   3386:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
                   3387:                         if ($response->is_success()) {
                   3388:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
                   3389:                             my $locmodtime = (stat($fname))[9];
                   3390:                             if ($locmodtime < $remmodtime) {
1.1354    raeburn  3391:                                 my $stale;
                   3392:                                 my $answer = &reply('pong',$homeserver);
                   3393:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
                   3394:                                     sleep(0.2);
                   3395:                                     $locmodtime = (stat($fname))[9];
                   3396:                                     if ($locmodtime < $remmodtime) {
                   3397:                                         my $posstransfer = $fname.'.in.transfer';
                   3398:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
                   3399:                                             $removed = 1;
                   3400:                                         } else {
                   3401:                                             $stale = 1;
                   3402:                                         }
                   3403:                                     } else {
                   3404:                                         $removed = 1;
                   3405:                                     }
                   3406:                                 } else {
                   3407:                                     $stale = 1;
                   3408:                                 }
                   3409:                                 if ($stale) {
                   3410:                                     unlink($fname);
                   3411:                                     if ($uri!~/\.meta$/) {
                   3412:                                         unlink($fname.'.meta');
                   3413:                                     }
                   3414:                                     &reply("unsub:$fname",$homeserver);
                   3415:                                     $removed = 1;
1.1352    raeburn  3416:                                 }
                   3417:                             }
                   3418:                         }
                   3419:                     }
                   3420:                 }
                   3421:             }
                   3422:         }
                   3423:     }
1.1354    raeburn  3424:     return $removed;
1.1352    raeburn  3425: }
                   3426: 
1.492     albertel 3427: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   3428: 
                   3429: sub allowuploaded {
                   3430:     my ($srcurl,$url)=@_;
                   3431:     $url=&clutter(&declutter($url));
                   3432:     my $dir=$url;
                   3433:     $dir=~s/\/[^\/]+$//;
                   3434:     my %httpref=();
                   3435:     my $httpurl=&hreflocation('',$url);
                   3436:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  3437:     &Apache::lonnet::appenv(\%httpref);
1.254     www      3438: }
1.477     raeburn  3439: 
1.1193    raeburn  3440: #
                   3441: # Determine if the current user should be able to edit a particular resource,
                   3442: # when viewing in course context.
                   3443: # (a) When viewing resource used to determine if "Edit" item is included in 
                   3444: #     Functions.
                   3445: # (b) When displaying folder contents in course editor, used to determine if
                   3446: #     "Edit" link will be displayed alongside resource.
                   3447: #
1.1196    raeburn  3448: #  input: six args -- filename (decluttered), course number, course domain,
                   3449: #                   url, symb (if registered) and group (if this is a group
                   3450: #                   item -- e.g., bulletin board, group page etc.).
                   3451: #  output: array of five scalars -- 
1.1193    raeburn  3452: #          $cfile -- url for file editing if editable on current server
                   3453: #          $home -- homeserver of resource (i.e., for author if published,
                   3454: #                                           or course if uploaded.).
                   3455: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  3456: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   3457: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  3458: #
                   3459: 
                   3460: sub can_edit_resource {
1.1194    raeburn  3461:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   3462:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   3463: #
                   3464: # For aboutme pages user can only edit his/her own.
                   3465: #
1.1199    raeburn  3466:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  3467:         my ($sdom,$sname) = ($1,$2);
                   3468:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   3469:             $home = $env{'user.home'};
                   3470:             $cfile = $resurl;
                   3471:             if ($env{'form.forceedit'}) {
                   3472:                 $forceview = 1;
                   3473:             } else {
                   3474:                 $forceedit = 1;
                   3475:             }
                   3476:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3477:         } else {
                   3478:             return;
                   3479:         }
                   3480:     }
                   3481: 
                   3482:     if ($env{'request.course.id'}) {
                   3483:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
                   3484:         if ($group ne '') {
                   3485: # if this is a group homepage or group bulletin board, check group privs
                   3486:             my $allowed = 0;
1.1197    raeburn  3487:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   3488:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  3489:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  3490:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   3491:                     $allowed = 1;
                   3492:                 }
1.1197    raeburn  3493:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   3494:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   3495:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  3496:                     $allowed = 1;
                   3497:                 }
                   3498:             }
                   3499:             if ($allowed) {
                   3500:                 $home=&homeserver($cnum,$cdom);
                   3501:                 if ($env{'form.forceedit'}) {
                   3502:                     $forceview = 1;
                   3503:                 } else {
                   3504:                     $forceedit = 1;
                   3505:                 }
                   3506:                 $cfile = $resurl;
                   3507:             } else {
                   3508:                 return;
                   3509:             }
                   3510:         } else {
1.1208    raeburn  3511:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3512:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
                   3513:                     return;
                   3514:                 }
                   3515:             } elsif (!$crsedit) {
1.1194    raeburn  3516: #
                   3517: # No edit allowed where CC has switched to student role.
                   3518: #
                   3519:                 return;
                   3520:             }
                   3521:         }
                   3522:     }
                   3523: 
1.1193    raeburn  3524:     if ($file ne '') {
                   3525:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  3526:             if (&is_course_upload($file,$cnum,$cdom)) {
                   3527:                 $uploaded = 1;
                   3528:                 $incourse = 1;
1.1193    raeburn  3529:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   3530:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  3531:                     if ($env{'form.forceedit'}) {
                   3532:                         $forceview = 1;
                   3533:                     } else {
                   3534:                         $forceedit = 1;
                   3535:                     }
1.1194    raeburn  3536:                 }
                   3537:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   3538:                 $incourse = 1;
                   3539:                 if ($env{'form.forceedit'}) {
                   3540:                     $forceview = 1;
                   3541:                 } else {
                   3542:                     $forceedit = 1;
                   3543:                 }
                   3544:                 $cfile = $resurl;
                   3545:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
                   3546:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   3547:                     $incourse = 1;
                   3548:                     if ($env{'form.forceedit'}) {
                   3549:                         $forceview = 1;
                   3550:                     } else {
                   3551:                         $forceedit = 1;
                   3552:                     }
                   3553:                     $cfile = $resurl;
1.1199    raeburn  3554:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  3555:                     $incourse = 1;
                   3556:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  3557:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  3558:                     $incourse = 1;
1.1199    raeburn  3559:                     if ($env{'form.forceedit'}) {
                   3560:                         $forceview = 1;
                   3561:                     } else {
                   3562:                         $forceedit = 1;
                   3563:                     }
                   3564:                     $cfile = $resurl;
1.1343    raeburn  3565:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3566:                     $incourse = 1;
                   3567:                     if ($env{'form.forceedit'}) {
                   3568:                         $forceview = 1;
                   3569:                     } else {
                   3570:                         $forceedit = 1;
                   3571:                     }
                   3572:                     $cfile = $resurl;
1.1208    raeburn  3573:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3574:                     $incourse = 1;
                   3575:                     if ($env{'form.forceedit'}) {
                   3576:                         $forceview = 1;
                   3577:                     } else {
                   3578:                         $forceedit = 1;
                   3579:                     }
                   3580:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  3581:                 }
                   3582:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   3583:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   3584:                 if (&is_on_map($template)) { 
                   3585:                     $incourse = 1;
                   3586:                     $forceview = 1;
                   3587:                     $cfile = $template;
1.1193    raeburn  3588:                 }
1.1199    raeburn  3589:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
                   3590:                     $incourse = 1;
                   3591:                     if ($env{'form.forceedit'}) {
                   3592:                         $forceview = 1;
                   3593:                     } else {
                   3594:                         $forceedit = 1;
                   3595:                     }
                   3596:                     $cfile = $resurl;
1.1343    raeburn  3597:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298    raeburn  3598:                 $incourse = 1;
                   3599:                 if ($env{'form.forceedit'}) {
                   3600:                     $forceview = 1;
                   3601:                 } else {
                   3602:                     $forceedit = 1;
                   3603:                 }
                   3604:                 $cfile = $resurl;
1.1199    raeburn  3605:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   3606:                 $incourse = 1;
                   3607:                 $forceview = 1;
                   3608:                 if ($symb) {
                   3609:                     my ($map,$id,$res)=&decode_symb($symb);
                   3610:                     $env{'request.symb'} = $symb;
                   3611:                     $cfile = &clutter($res);
                   3612:                 } else {
                   3613:                     $cfile = $env{'form.suppurl'};
1.1298    raeburn  3614:                     my $escfile = &unescape($cfile);
1.1343    raeburn  3615:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3616:                         $cfile = '/adm/wrapper'.$escfile;
                   3617:                     } else {
                   3618:                         $escfile =~ s{^http://}{};
                   3619:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
                   3620:                     }
1.1199    raeburn  3621:                 }
1.1234    raeburn  3622:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3623:                 if ($env{'form.forceedit'}) {
                   3624:                     $forceview = 1;
                   3625:                 } else {
                   3626:                     $forceedit = 1;
                   3627:                 }
                   3628:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193    raeburn  3629:             }
                   3630:         }
1.1194    raeburn  3631:         if ($uploaded || $incourse) {
                   3632:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  3633:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  3634:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   3635:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   3636:             # Check that the user has permission to edit this resource
                   3637:             my $setpriv = 1;
                   3638:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   3639:             if (defined($cfudom)) {
                   3640:                 $home=&homeserver($cfuname,$cfudom);
                   3641:                 $cfile=$file;
                   3642:             }
                   3643:         }
1.1194    raeburn  3644:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   3645:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  3646:             my @ids=&current_machine_ids();
                   3647:             unless (grep(/^\Q$home\E$/,@ids)) {
                   3648:                 $switchserver=1;
                   3649:             }
                   3650:         }
                   3651:     }
1.1194    raeburn  3652:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  3653: }
                   3654: 
                   3655: sub is_course_upload {
                   3656:     my ($file,$cnum,$cdom) = @_;
                   3657:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   3658:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  3659:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   3660:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  3661:         return 1;
                   3662:     }
                   3663:     return;
                   3664: }
                   3665: 
1.1194    raeburn  3666: sub in_course {
1.1195    raeburn  3667:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   3668:     if ($hideprivileged) {
                   3669:         my $skipuser;
1.1219    raeburn  3670:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   3671:         my @possdoms = ($cdom);  
                   3672:         if ($coursehash{'checkforpriv'}) { 
                   3673:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   3674:         }
                   3675:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  3676:             $skipuser = 1;
                   3677:             if ($coursehash{'nothideprivileged'}) {
                   3678:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3679:                     my $user;
                   3680:                     if ($item =~ /:/) {
                   3681:                         $user = $item;
                   3682:                     } else {
                   3683:                         $user = join(':',split(/[\@]/,$item));
                   3684:                     }
                   3685:                     if ($user eq $uname.':'.$udom) {
                   3686:                         undef($skipuser);
                   3687:                         last;
                   3688:                     }
                   3689:                 }
                   3690:             }
                   3691:             if ($skipuser) {
                   3692:                 return 0;
                   3693:             }
                   3694:         }
                   3695:     }
1.1194    raeburn  3696:     $type ||= 'any';
                   3697:     if (!defined($cdom) || !defined($cnum)) {
                   3698:         my $cid  = $env{'request.course.id'};
                   3699:         $cdom = $env{'course.'.$cid.'.domain'};
                   3700:         $cnum = $env{'course.'.$cid.'.num'};
                   3701:     }
                   3702:     my $typesref;
1.1195    raeburn  3703:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  3704:         $typesref = ['active','previous','future'];
                   3705:     } elsif ($type eq 'previous' || $type eq 'future') {
                   3706:         $typesref = [$type];
                   3707:     }
                   3708:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   3709:                               $typesref,undef,[$cdom]);
                   3710:     my ($tmp) = keys(%roles);
                   3711:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   3712:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   3713:     if (@course_roles > 0) {
                   3714:         return 1;
                   3715:     }
                   3716:     return 0;
                   3717: }
                   3718: 
1.478     albertel 3719: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 3720: # input: action, courseID, current domain, intended
1.637     raeburn  3721: #        path to file, source of file, instruction to parse file for objects,
                   3722: #        ref to hash for embedded objects,
                   3723: #        ref to hash for codebase of java objects.
1.1095    raeburn  3724: #        reference to scalar to accommodate mime type determined
                   3725: #          from File::MMagic if $parser = parse.
1.637     raeburn  3726: #
1.485     raeburn  3727: # output: url to file (if action was uploaddoc), 
                   3728: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  3729: #
1.478     albertel 3730: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   3731: # course.
1.477     raeburn  3732: #
1.478     albertel 3733: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3734: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   3735: #          course's home server.
1.477     raeburn  3736: #
1.478     albertel 3737: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   3738: #          be copied from $source (current location) to 
                   3739: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3740: #         and will then be copied to
                   3741: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   3742: #         course's home server.
1.485     raeburn  3743: #
1.481     raeburn  3744: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 3745: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  3746: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3747: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   3748: #         in course's home server.
1.637     raeburn  3749: #
1.477     raeburn  3750: 
                   3751: sub process_coursefile {
1.1095    raeburn  3752:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   3753:         $mimetype)=@_;
1.477     raeburn  3754:     my $fetchresult;
1.638     albertel 3755:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  3756:     if ($action eq 'propagate') {
1.638     albertel 3757:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   3758: 			     $home);
1.481     raeburn  3759:     } else {
1.477     raeburn  3760:         my $fpath = '';
                   3761:         my $fname = $file;
1.478     albertel 3762:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  3763:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  3764:         my $filepath = &build_filepath($fpath);
1.481     raeburn  3765:         if ($action eq 'copy') {
                   3766:             if ($source eq '') {
                   3767:                 $fetchresult = 'no source file';
                   3768:                 return $fetchresult;
                   3769:             } else {
                   3770:                 my $destination = $filepath.'/'.$fname;
                   3771:                 rename($source,$destination);
                   3772:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3773:                                  $home);
1.481     raeburn  3774:             }
                   3775:         } elsif ($action eq 'uploaddoc') {
1.1359    raeburn  3776:             open(my $fh,'>',$filepath.'/'.$fname);
1.620     albertel 3777:             print $fh $env{'form.'.$source};
1.481     raeburn  3778:             close($fh);
1.637     raeburn  3779:             if ($parser eq 'parse') {
1.1024    raeburn  3780:                 my $mm = new File::MMagic;
1.1095    raeburn  3781:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   3782:                 if ($type eq 'text/html') {
1.1024    raeburn  3783:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   3784:                     unless ($parse_result eq 'ok') {
                   3785:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   3786:                     }
1.637     raeburn  3787:                 }
1.1095    raeburn  3788:                 if (ref($mimetype)) {
                   3789:                     $$mimetype = $type;
                   3790:                 } 
1.637     raeburn  3791:             }
1.477     raeburn  3792:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3793:                                  $home);
1.481     raeburn  3794:             if ($fetchresult eq 'ok') {
                   3795:                 return '/uploaded/'.$fpath.'/'.$fname;
                   3796:             } else {
                   3797:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 3798:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  3799:                 return '/adm/notfound.html';
                   3800:             }
1.477     raeburn  3801:         }
                   3802:     }
1.485     raeburn  3803:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  3804:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 3805:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  3806:     }
                   3807:     return $fetchresult;
                   3808: }
                   3809: 
1.637     raeburn  3810: sub build_filepath {
                   3811:     my ($fpath) = @_;
                   3812:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   3813:     unless ($fpath eq '') {
                   3814:         my @parts=split('/',$fpath);
                   3815:         foreach my $part (@parts) {
                   3816:             $filepath.= '/'.$part;
                   3817:             if ((-e $filepath)!=1) {
                   3818:                 mkdir($filepath,0777);
                   3819:             }
                   3820:         }
                   3821:     }
                   3822:     return $filepath;
                   3823: }
                   3824: 
                   3825: sub store_edited_file {
1.638     albertel 3826:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  3827:     my $file = $primary_url;
                   3828:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   3829:     my $fpath = '';
                   3830:     my $fname = $file;
                   3831:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   3832:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   3833:     my $filepath = &build_filepath($fpath);
1.1359    raeburn  3834:     open(my $fh,'>',$filepath.'/'.$fname);
1.637     raeburn  3835:     print $fh $content;
                   3836:     close($fh);
1.638     albertel 3837:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  3838:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3839: 			  $home);
1.637     raeburn  3840:     if ($$fetchresult eq 'ok') {
                   3841:         return '/uploaded/'.$fpath.'/'.$fname;
                   3842:     } else {
1.638     albertel 3843:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   3844: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  3845:         return '/adm/notfound.html';
                   3846:     }
                   3847: }
                   3848: 
1.531     albertel 3849: sub clean_filename {
1.831     albertel 3850:     my ($fname,$args)=@_;
1.315     www      3851: # Replace Windows backslashes by forward slashes
1.257     www      3852:     $fname=~s/\\/\//g;
1.831     albertel 3853:     if (!$args->{'keep_path'}) {
                   3854:         # Get rid of everything but the actual filename
                   3855: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   3856:     }
1.315     www      3857: # Replace spaces by underscores
                   3858:     $fname=~s/\s+/\_/g;
                   3859: # Replace all other weird characters by nothing
1.831     albertel 3860:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 3861: # Replace all .\d. sequences with _\d. so they no longer look like version
                   3862: # numbers
                   3863:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 3864:     return $fname;
                   3865: }
1.1051    raeburn  3866: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   3867: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   3868: # image with the same aspect ratio as the original, but with dimensions which do 
                   3869: # not exceed $resizewidth and $resizeheight.
                   3870:  
1.984     neumanie 3871: sub resizeImage {
1.1051    raeburn  3872:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   3873:     my $ima = Image::Magick->new;
                   3874:     my $resized;
                   3875:     if (-e $img_path) {
                   3876:         $ima->Read($img_path);
                   3877:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   3878:             my $width = $ima->Get('width');
                   3879:             my $height = $ima->Get('height');
                   3880:             if ($width > $resizewidth) {
                   3881: 	        my $factor = $width/$resizewidth;
                   3882:                 my $newheight = $height/$factor;
                   3883:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   3884:                 $resized = 1;
                   3885:             }
                   3886:         }
                   3887:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   3888:             my $width = $ima->Get('width');
                   3889:             my $height = $ima->Get('height');
                   3890:             if ($height > $resizeheight) {
                   3891:                 my $factor = $height/$resizeheight;
                   3892:                 my $newwidth = $width/$factor;
                   3893:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   3894:                 $resized = 1;
                   3895:             }
                   3896:         }
                   3897:         if ($resized) {
                   3898:             $ima->Write($img_path);
                   3899:         }
                   3900:     }
                   3901:     return;
1.977     amueller 3902: }
                   3903: 
1.608     albertel 3904: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 3905: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  3906: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  3907: #        $context - possible values: coursedoc, existingfile, overwrite, 
1.1401    raeburn  3908: #                                    canceloverwrite, scantron or ''.
1.1090    raeburn  3909: #                   if 'coursedoc': upload to the current course
                   3910: #                   if 'existingfile': write file to tmp/overwrites directory 
                   3911: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   3912: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 3913: #        $subdir - directory in userfile to store the file into
1.1401    raeburn  3914: #        $parser - instruction to parse file for objects ($parser = parse) or
                   3915: #                  if context is 'scantron', $parser is hashref of csv column mapping
                   3916: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
                   3917: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858     raeburn  3918: #        $allfiles - reference to hash for embedded objects
                   3919: #        $codebase - reference to hash for codebase of java objects
                   3920: #        $desuname - username for permanent storage of uploaded file
                   3921: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  3922: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   3923: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  3924: #        $resizewidth - width (pixels) to which to resize uploaded image
                   3925: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  3926: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  3927: #                    from File::MMagic.
1.858     raeburn  3928: # 
1.686     albertel 3929: # output: url of file in userspace, or error: <message> 
                   3930: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 3931: 
1.531     albertel 3932: sub userfileupload {
1.1090    raeburn  3933:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  3934:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 3935:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 3936:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 3937:     $fname=&clean_filename($fname);
1.1090    raeburn  3938:     # See if there is anything left
1.257     www      3939:     unless ($fname) { return 'error: no uploaded file'; }
1.1090    raeburn  3940:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   3941:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   3942:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   3943:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  3944:         my $now = time;
1.1090    raeburn  3945:         my $filepath;
1.1095    raeburn  3946:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  3947:              $filepath = 'tmp/helprequests/'.$now;
                   3948:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   3949:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   3950:                          '_'.$env{'user.domain'}.'/pending';
                   3951:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   3952:             my ($docuname,$docudom);
1.1350    raeburn  3953:             if ($destudom =~ /^$match_domain$/) {
1.1090    raeburn  3954:                 $docudom = $destudom;
                   3955:             } else {
                   3956:                 $docudom = $env{'user.domain'};
                   3957:             }
1.1350    raeburn  3958:             if ($destuname =~ /^$match_username$/) {
1.1090    raeburn  3959:                 $docuname = $destuname;
                   3960:             } else {
                   3961:                 $docuname = $env{'user.name'};
                   3962:             }
                   3963:             if (exists($env{'form.group'})) {
                   3964:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3965:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3966:             }
                   3967:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   3968:             if ($context eq 'canceloverwrite') {
                   3969:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   3970:                 if (-e  $tempfile) {
                   3971:                     my @info = stat($tempfile);
                   3972:                     if ($info[9] eq $env{'form.timestamp'}) {
                   3973:                         unlink($tempfile);
                   3974:                     }
                   3975:                 }
                   3976:                 return;
1.523     raeburn  3977:             }
                   3978:         }
1.1090    raeburn  3979:         # Create the directory if not present
1.741     raeburn  3980:         my @parts=split(/\//,$filepath);
                   3981:         my $fullpath = $perlvar{'lonDaemons'};
                   3982:         for (my $i=0;$i<@parts;$i++) {
                   3983:             $fullpath .= '/'.$parts[$i];
                   3984:             if ((-e $fullpath)!=1) {
                   3985:                 mkdir($fullpath,0777);
                   3986:             }
                   3987:         }
1.1359    raeburn  3988:         open(my $fh,'>',$fullpath.'/'.$fname);
1.741     raeburn  3989:         print $fh $env{'form.'.$formname};
                   3990:         close($fh);
1.1090    raeburn  3991:         if ($context eq 'existingfile') {
                   3992:             my @info = stat($fullpath.'/'.$fname);
                   3993:             return ($fullpath.'/'.$fname,$info[9]);
                   3994:         } else {
                   3995:             return $fullpath.'/'.$fname;
                   3996:         }
1.523     raeburn  3997:     }
1.995     raeburn  3998:     if ($subdir eq 'scantron') {
                   3999:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  4000:     } else {
1.995     raeburn  4001:         $fname="$subdir/$fname";
                   4002:     }
1.1090    raeburn  4003:     if ($context eq 'coursedoc') {
1.638     albertel 4004: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4005: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  4006:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 4007:             return &finishuserfileupload($docuname,$docudom,
                   4008: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  4009: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  4010:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  4011:         } else {
1.1218    raeburn  4012:             if ($env{'form.folder'}) {
                   4013:                 $fname=$env{'form.folder'}.'/'.$fname;
                   4014:             }
1.638     albertel 4015:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   4016: 				       $fname,$formname,$parser,
1.1095    raeburn  4017: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  4018:         }
1.719     banghart 4019:     } elsif (defined($destuname)) {
                   4020:         my $docuname=$destuname;
                   4021:         my $docudom=$destudom;
1.860     raeburn  4022: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4023: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4024:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4025:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4026:     } else {
1.638     albertel 4027:         my $docuname=$env{'user.name'};
                   4028:         my $docudom=$env{'user.domain'};
1.1220    raeburn  4029:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  4030:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4031:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4032:         }
1.860     raeburn  4033: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   4034: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  4035:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  4036:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      4037:     }
1.271     www      4038: }
                   4039: 
                   4040: sub finishuserfileupload {
1.860     raeburn  4041:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  4042:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  4043:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      4044:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 4045:   
1.860     raeburn  4046:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 4047:     $file=$fname;
                   4048:     if ($fname=~m|/|) {
                   4049:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   4050: 	$path.=$fnamepath.'/';
                   4051:     }
1.259     www      4052:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      4053:     my $count;
                   4054:     for ($count=4;$count<=$#parts;$count++) {
                   4055:         $filepath.="/$parts[$count]";
                   4056:         if ((-e $filepath)!=1) {
                   4057: 	    mkdir($filepath,0777);
                   4058:         }
                   4059:     }
1.984     neumanie 4060: 
1.258     www      4061: # Save the file
                   4062:     {
1.1359    raeburn  4063: 	if (!open(FH,'>',$filepath.'/'.$file)) {
1.701     albertel 4064: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   4065: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   4066: 	    return '/adm/notfound.html';
                   4067: 	}
1.1090    raeburn  4068:         if ($context eq 'overwrite') {
1.1117    foxr     4069:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  4070:             my $target = $filepath.'/'.$file;
                   4071:             if (-e $source) {
                   4072:                 my @info = stat($source);
                   4073:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   4074:                     unless (&File::Copy::move($source,$target)) {
                   4075:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   4076:                         return "Moving from $source failed";
                   4077:                     }
                   4078:                 } else {
                   4079:                     return "Temporary file: $source had unexpected date/time for last modification";
                   4080:                 }
                   4081:             } else {
                   4082:                 return "Temporary file: $source missing";
                   4083:             }
                   4084:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 4085: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   4086: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   4087: 	    return '/adm/notfound.html';
                   4088: 	}
1.570     albertel 4089: 	close(FH);
1.1051    raeburn  4090:         if ($resizewidth && $resizeheight) {
                   4091:             my $mm = new File::MMagic;
                   4092:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   4093:             if ($mime_type =~ m{^image/}) {
                   4094: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   4095:             }  
1.977     amueller 4096: 	}
1.258     www      4097:     }
1.1152    raeburn  4098:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   4099:         if (ref($mimetype)) {
                   4100:             if ($$mimetype eq '') {
                   4101:                 my $mm = new File::MMagic;
                   4102:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   4103:                 $$mimetype = $type;
                   4104:             }
                   4105:         }
                   4106:     }
1.1401    raeburn  4107:     if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152    raeburn  4108:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  4109:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   4110:                                                        $allfiles,$codebase);
                   4111:             unless ($parse_result eq 'ok') {
                   4112:                 &logthis('Failed to parse '.$filepath.$file.
                   4113: 	   	         ' for embedded media: '.$parse_result); 
                   4114:             }
1.637     raeburn  4115:         }
1.1401    raeburn  4116:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
                   4117:         my $format = $env{'form.scantron_format'};
                   4118:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637     raeburn  4119:     }
1.860     raeburn  4120:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   4121:         my $input = $filepath.'/'.$file;
                   4122:         my $output = $filepath.'/'.'tn-'.$file;
                   4123:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1359    raeburn  4124:         my @args = ('convert','-sample',$thumbsize,$input,$output);
                   4125:         system({$args[0]} @args);
1.860     raeburn  4126:         if (-e $filepath.'/'.'tn-'.$file) {
                   4127:             $fetchthumb  = 1; 
                   4128:         }
                   4129:     }
1.858     raeburn  4130:  
1.259     www      4131: # Notify homeserver to grep it
                   4132: #
1.984     neumanie 4133:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 4134:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      4135:     if ($fetchresult eq 'ok') {
1.860     raeburn  4136:         if ($fetchthumb) {
                   4137:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   4138:             if ($thumbresult ne 'ok') {
                   4139:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   4140:                          $docuhome.': '.$thumbresult);
                   4141:             }
                   4142:         }
1.259     www      4143: #
1.258     www      4144: # Return the URL to it
1.494     albertel 4145:         return '/uploaded/'.$path.$file;
1.263     www      4146:     } else {
1.494     albertel 4147:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   4148: 		 ': '.$fetchresult);
1.263     www      4149:         return '/adm/notfound.html';
1.858     raeburn  4150:     }
1.493     albertel 4151: }
                   4152: 
1.637     raeburn  4153: sub extract_embedded_items {
1.961     raeburn  4154:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  4155:     my @state = ();
1.1164    raeburn  4156:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  4157:     my %javafiles = (
                   4158:                       codebase => '',
                   4159:                       code => '',
                   4160:                       archive => ''
                   4161:                     );
                   4162:     my %mediafiles = (
                   4163:                       src => '',
                   4164:                       movie => '',
                   4165:                      );
1.648     raeburn  4166:     my $p;
                   4167:     if ($content) {
                   4168:         $p = HTML::LCParser->new($content);
                   4169:     } else {
1.961     raeburn  4170:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  4171:     }
1.641     albertel 4172:     while (my $t=$p->get_token()) {
1.640     albertel 4173: 	if ($t->[0] eq 'S') {
                   4174: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 4175: 	    push(@state, $tagname);
1.648     raeburn  4176:             if (lc($tagname) eq 'allow') {
                   4177:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   4178:             }
1.640     albertel 4179: 	    if (lc($tagname) eq 'img') {
                   4180: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   4181: 	    }
1.886     albertel 4182: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  4183:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  4184:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4185:                 }
1.886     albertel 4186: 	    }
1.645     raeburn  4187:             if (lc($tagname) eq 'script') {
1.1164    raeburn  4188:                 my $src;
1.645     raeburn  4189:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   4190:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   4191:                 } else {
1.1164    raeburn  4192:                     if ($attr->{'src'} ne '') {
                   4193:                         $src = $attr->{'src'};
                   4194:                         &add_filetype($allfiles,$src,'src');
                   4195:                     }
                   4196:                 }
                   4197:                 my $text = $p->get_trimmed_text();
                   4198:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   4199:                     my @swfargs = split(/,/,$1);
                   4200:                     foreach my $item (@swfargs) {
                   4201:                         $item =~ s/["']//g;
                   4202:                         $item =~ s/^\s+//;
                   4203:                         $item =~ s/\s+$//;
                   4204:                     }
                   4205:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   4206:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   4207:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   4208:                         } else {
                   4209:                             $related{$swfargs[0]} = [$swfargs[2]];
                   4210:                         }
                   4211:                     }
1.645     raeburn  4212:                 }
                   4213:             }
                   4214:             if (lc($tagname) eq 'link') {
                   4215:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   4216:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   4217:                 }
                   4218:             }
1.640     albertel 4219: 	    if (lc($tagname) eq 'object' ||
                   4220: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   4221: 		foreach my $item (keys(%javafiles)) {
                   4222: 		    $javafiles{$item} = '';
                   4223: 		}
1.1164    raeburn  4224:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   4225:                     $lastids{lc($tagname)} = $attr->{'id'};
                   4226:                 }
1.640     albertel 4227: 	    }
                   4228: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   4229: 		my $name = lc($attr->{'name'});
                   4230: 		foreach my $item (keys(%javafiles)) {
                   4231: 		    if ($name eq $item) {
                   4232: 			$javafiles{$item} = $attr->{'value'};
                   4233: 			last;
                   4234: 		    }
                   4235: 		}
1.1164    raeburn  4236:                 my $pathfrom;
1.640     albertel 4237: 		foreach my $item (keys(%mediafiles)) {
                   4238: 		    if ($name eq $item) {
1.1164    raeburn  4239:                         $pathfrom = $attr->{'value'};
                   4240:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   4241: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 4242: 			last;
                   4243: 		    }
                   4244: 		}
1.1164    raeburn  4245:                 if ($name eq 'flashvars') {
                   4246:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   4247:                 }
                   4248:                 if ($pathfrom ne '') {
                   4249:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   4250:                                          $pathfrom);
                   4251:                 }
1.640     albertel 4252: 	    }
                   4253: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   4254: 		foreach my $item (keys(%javafiles)) {
                   4255: 		    if ($attr->{$item}) {
                   4256: 			$javafiles{$item} = $attr->{$item};
                   4257: 			last;
                   4258: 		    }
                   4259: 		}
                   4260: 		foreach my $item (keys(%mediafiles)) {
                   4261: 		    if ($attr->{$item}) {
                   4262: 			&add_filetype($allfiles,$attr->{$item},$item);
                   4263: 			last;
                   4264: 		    }
                   4265: 		}
1.1164    raeburn  4266:                 if (lc($tagname) eq 'embed') {
                   4267:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   4268:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   4269:                                              $attr->{'src'});
                   4270:                     }
                   4271:                 }
1.640     albertel 4272: 	    }
1.1243    raeburn  4273:             if (lc($tagname) eq 'iframe') {
                   4274:                 my $src = $attr->{'src'} ;
                   4275:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
                   4276:                     &add_filetype($allfiles,$src,'src');
                   4277:                 } elsif ($src =~ m{^/}) {
                   4278:                     if ($env{'request.course.id'}) {
                   4279:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4280:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4281:                         my $url = &hreflocation('',$fullpath);
                   4282:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
                   4283:                             my $relpath = $1;
                   4284:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
                   4285:                                 &add_filetype($allfiles,$1,'src');
                   4286:                             }
                   4287:                         }
                   4288:                     }
                   4289:                 }
                   4290:             }
1.1164    raeburn  4291:             if ($t->[4] =~ m{/>$}) {
1.1243    raeburn  4292:                 pop(@state);
1.1164    raeburn  4293:             }
1.640     albertel 4294: 	} elsif ($t->[0] eq 'E') {
                   4295: 	    my ($tagname) = ($t->[1]);
                   4296: 	    if ($javafiles{'codebase'} ne '') {
                   4297: 		$javafiles{'codebase'} .= '/';
                   4298: 	    }  
                   4299: 	    if (lc($tagname) eq 'applet' ||
                   4300: 		lc($tagname) eq 'object' ||
                   4301: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   4302: 		) {
                   4303: 		foreach my $item (keys(%javafiles)) {
                   4304: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   4305: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   4306: 			&add_filetype($allfiles,$file,$item);
                   4307: 		    }
                   4308: 		}
                   4309: 	    } 
                   4310: 	    pop @state;
                   4311: 	}
                   4312:     }
1.1164    raeburn  4313:     foreach my $id (sort(keys(%flashvars))) {
                   4314:         if ($shockwave{$id} ne '') {
                   4315:             my @pairs = split(/\&/,$flashvars{$id});
                   4316:             foreach my $pair (@pairs) {
                   4317:                 my ($key,$value) = split(/\=/,$pair);
                   4318:                 if ($key eq 'thumb') {
                   4319:                     &add_filetype($allfiles,$value,$key);
                   4320:                 } elsif ($key eq 'content') {
                   4321:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   4322:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   4323:                     if ($ext ne '') {
                   4324:                         &add_filetype($allfiles,$path.$value,$ext);
                   4325:                     }
                   4326:                 }
                   4327:             }
                   4328:         }
                   4329:     }
1.637     raeburn  4330:     return 'ok';
                   4331: }
                   4332: 
1.639     albertel 4333: sub add_filetype {
                   4334:     my ($allfiles,$file,$type)=@_;
                   4335:     if (exists($allfiles->{$file})) {
                   4336: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   4337: 	    push(@{$allfiles->{$file}}, &escape($type));
                   4338: 	}
                   4339:     } else {
                   4340: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  4341:     }
                   4342: }
                   4343: 
1.1164    raeburn  4344: sub embedded_dependency {
                   4345:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   4346:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   4347:         if (($identifier ne '') &&
                   4348:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   4349:             ($pathfrom ne '')) {
                   4350:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   4351:             foreach my $dep (@{$related->{$identifier}}) {
                   4352:                 &add_filetype($allfiles,$path.$dep,'object');
                   4353:             }
                   4354:         }
                   4355:     }
                   4356:     return;
                   4357: }
                   4358: 
1.1401    raeburn  4359: sub bubblesheet_converter {
                   4360:     my ($cdom,$fullpath,$config,$format) = @_;
                   4361:     if ((&domain($cdom) ne '') &&
1.1403  ! raeburn  4362:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
1.1401    raeburn  4363:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
                   4364:         my %csvcols = %{$config};
                   4365:         my %csvbynum = reverse(%csvcols);
                   4366:         my %scantronconf = &get_scantron_config($format,$cdom);
                   4367:         if (keys(%scantronconf)) {
                   4368:             my %bynum = (
                   4369:                           $scantronconf{CODEstart} => 'CODEstart',
                   4370:                           $scantronconf{IDstart}   => 'IDstart',
                   4371:                           $scantronconf{PaperID}   => 'PaperID',
                   4372:                           $scantronconf{FirstName} => 'FirstName',
                   4373:                           $scantronconf{LastName}  => 'LastName',
                   4374:                           $scantronconf{Qstart}    => 'Qstart',
                   4375:                         );
                   4376:             my @ordered;
                   4377:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
1.1403  ! raeburn  4378:                 push(@ordered,$bynum{$item});
1.1401    raeburn  4379:             }
                   4380:             my %mapstart = (
                   4381:                               CODEstart => 'CODE',
                   4382:                               IDstart   => 'ID',
                   4383:                               PaperID   => 'PaperID',
                   4384:                               FirstName => 'FirstName',
                   4385:                               LastName  => 'LastName',
                   4386:                               Qstart    => 'FirstQuestion',
                   4387:                            );
                   4388:             my %maplength = (
                   4389:                               CODEstart => 'CODElength',
                   4390:                               IDstart   => 'IDlength',
                   4391:                               PaperID   => 'PaperIDlength',
                   4392:                               FirstName => 'FirstNamelength',
                   4393:                               LastName  => 'LastNamelength',
                   4394:             );
                   4395:             if (open(my $fh,'<',$fullpath)) {
                   4396:                 my $output;
1.1403  ! raeburn  4397:                 my %lettdig = &letter_to_digits();
        !          4398:                 my %diglett = reverse(%lettdig);
        !          4399:                 my $numletts = scalar(keys(%lettdig));
1.1401    raeburn  4400:                 while (my $line=<$fh>) {
                   4401:                     $line =~ s{[\r\n]+$}{};
                   4402:                     my %found;
                   4403:                     my @values = split(/,/,$line);
                   4404:                     my ($qstart,$record);
                   4405:                     for (my $i=0; $i<@values; $i++) {
1.1403  ! raeburn  4406:                         if ((($qstart ne '') && ($i > $qstart)) ||
        !          4407:                             ($csvbynum{$i} eq 'FirstQuestion')) {
        !          4408:                             if ($values[$i] eq '') {
        !          4409:                                 $values[$i] = $scantronconf{'Qoff'};
        !          4410:                             } elsif ($scantronconf{'Qon'} eq 'number') {
        !          4411:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
        !          4412:                                     $values[$i] = $lettdig{uc($values[$i])};
        !          4413:                                 }
        !          4414:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
        !          4415:                                 if ($values[$i] =~ /^[0-9]$/) {
        !          4416:                                     $values[$i] = $diglett{$values[$i]};
        !          4417:                                 }
        !          4418:                             } else {
        !          4419:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
        !          4420:                                     my $digit;
        !          4421:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
        !          4422:                                         $digit = $lettdig{uc($values[$i])}-1;
        !          4423:                                         if ($values[$i] eq 'J') {
        !          4424:                                             $digit += $numletts;
        !          4425:                                         }
        !          4426:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
        !          4427:                                         $digit = $values[$i]-1;
        !          4428:                                         if ($values[$i] eq '0') {
        !          4429:                                             $digit += $numletts;
        !          4430:                                         }
        !          4431:                                     }
        !          4432:                                     my $qval='';
        !          4433:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
        !          4434:                                         if ($j == $digit) {
        !          4435:                                             $qval .= $scantronconf{'Qon'};
        !          4436:                                         } else {
        !          4437:                                             $qval .= $scantronconf{'Qoff'};
        !          4438:                                         }
        !          4439:                                     }
        !          4440:                                     $values[$i] = $qval;
        !          4441:                                 }
        !          4442:                             }
        !          4443:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
        !          4444:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
        !          4445:                             }
        !          4446:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
        !          4447:                             if ($numblank > 0) {
        !          4448:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
        !          4449:                             }
1.1401    raeburn  4450:                             if ($csvbynum{$i} eq 'FirstQuestion') {
                   4451:                                 $qstart = $i;
1.1403  ! raeburn  4452:                                 $found{$csvbynum{$i}} = $values[$i];
1.1401    raeburn  4453:                             } else {
1.1403  ! raeburn  4454:                                 $found{'FirstQuestion'} .= $values[$i];
        !          4455:                             }
        !          4456:                         } elsif (exists($csvbynum{$i})) {
        !          4457:                             $values[$i] =~ s/^\s+//;
        !          4458:                             if ($csvbynum{$i} eq 'PaperID') {
        !          4459:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
        !          4460:                                     $values[$i] = '0'.$values[$i];
1.1401    raeburn  4461:                                 }
                   4462:                             }
                   4463:                             $found{$csvbynum{$i}} = $values[$i];
                   4464:                         }
                   4465:                     }
                   4466:                     foreach my $item (@ordered) {
                   4467:                         my $currlength = 1+length($record);
                   4468:                         my $numspaces = $scantronconf{$item} - $currlength;
                   4469:                         if ($numspaces > 0) {
                   4470:                             $record .= (' ' x $numspaces);
                   4471:                         }
                   4472:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
                   4473:                             unless ($item eq 'Qstart') {
                   4474:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
                   4475:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
                   4476:                                 }
                   4477:                             }
                   4478:                             $record .= $found{$mapstart{$item}};
                   4479:                         }
                   4480:                     }
                   4481:                     $output .= "$record\n";
                   4482:                 }
                   4483:                 close($fh);
                   4484:                 if ($output) {
                   4485:                     if (open(my $fh,'>',$fullpath)) {
                   4486:                         print $fh $output;
                   4487:                         close($fh);
                   4488:                     }
                   4489:                 }
                   4490:             }
                   4491:         }
                   4492:         return;
                   4493:     }
                   4494: }
                   4495: 
1.1403  ! raeburn  4496: sub letter_to_digits {
        !          4497:     my %lettdig = (
        !          4498:                     A => 1,
        !          4499:                     B => 2,
        !          4500:                     C => 3,
        !          4501:                     D => 4,
        !          4502:                     E => 5,
        !          4503:                     F => 6,
        !          4504:                     G => 7,
        !          4505:                     H => 8,
        !          4506:                     I => 9,
        !          4507:                     J => 0,
        !          4508:                   );
        !          4509:     return %lettdig;
        !          4510: }
        !          4511: 
1.1401    raeburn  4512: sub get_scantron_config {
                   4513:     my ($which,$cdom) = @_;
                   4514:     my @lines = &get_scantronformat_file($cdom);
                   4515:     my %config;
                   4516:     #FIXME probably should move to XML it has already gotten a bit much now
                   4517:     foreach my $line (@lines) {
                   4518:         my ($name,$descrip)=split(/:/,$line);
                   4519:         if ($name ne $which ) { next; }
                   4520:         chomp($line);
                   4521:         my @config=split(/:/,$line);
                   4522:         $config{'name'}=$config[0];
                   4523:         $config{'description'}=$config[1];
                   4524:         $config{'CODElocation'}=$config[2];
                   4525:         $config{'CODEstart'}=$config[3];
                   4526:         $config{'CODElength'}=$config[4];
                   4527:         $config{'IDstart'}=$config[5];
                   4528:         $config{'IDlength'}=$config[6];
                   4529:         $config{'Qstart'}=$config[7];
                   4530:         $config{'Qlength'}=$config[8];
                   4531:         $config{'Qoff'}=$config[9];
                   4532:         $config{'Qon'}=$config[10];
                   4533:         $config{'PaperID'}=$config[11];
                   4534:         $config{'PaperIDlength'}=$config[12];
                   4535:         $config{'FirstName'}=$config[13];
                   4536:         $config{'FirstNamelength'}=$config[14];
                   4537:         $config{'LastName'}=$config[15];
                   4538:         $config{'LastNamelength'}=$config[16];
                   4539:         $config{'BubblesPerRow'}=$config[17];
                   4540:         last;
                   4541:     }
                   4542:     return %config;
                   4543: }
                   4544: 
                   4545: sub get_scantronformat_file {
                   4546:     my ($cdom) = @_;
                   4547:     if ($cdom eq '') {
                   4548:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4549:     }
                   4550:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
                   4551:     my $gottab = 0;
                   4552:     my @lines;
                   4553:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   4554:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   4555:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
                   4556:             if ($formatfile ne '-1') {
                   4557:                 @lines = split("\n",$formatfile,-1);
                   4558:                 $gottab = 1;
                   4559:             }
                   4560:         }
                   4561:     }
                   4562:     if (!$gottab) {
                   4563:         my $confname = $cdom.'-domainconfig';
                   4564:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
                   4565:         my $formatfile = &getfile($default);
                   4566:         if ($formatfile ne '-1') {
                   4567:             @lines = split("\n",$formatfile,-1);
                   4568:             $gottab = 1;
                   4569:         }
                   4570:     }
                   4571:     if (!$gottab) {
                   4572:         my @domains = &current_machine_domains();
                   4573:         if (grep(/^\Q$cdom\E$/,@domains)) {
                   4574:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
                   4575:                 @lines = <$fh>;
                   4576:                 close($fh);
1.1403  ! raeburn  4577:             }
1.1401    raeburn  4578:         } else {
                   4579:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
                   4580:                 @lines = <$fh>;
                   4581:                 close($fh);
                   4582:             }
                   4583:         }
                   4584:     }
                   4585:     return @lines;
                   4586: }
                   4587: 
1.493     albertel 4588: sub removeuploadedurl {
1.984     neumanie 4589:     my ($url)=@_;	
                   4590:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 4591:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 4592: }
                   4593: 
                   4594: sub removeuserfile {
                   4595:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 4596:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  4597:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 4598:     if ($result eq 'ok') {	
1.798     raeburn  4599:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   4600:             my $metafile = $fname.'.meta';
                   4601:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 4602: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 4603:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  4604:             my $sqlresult = 
1.823     albertel 4605:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4606:                                         'portfolio_metadata',$group,
                   4607:                                         'delete');
1.798     raeburn  4608:         }
                   4609:     }
                   4610:     return $result;
1.257     www      4611: }
1.15      www      4612: 
1.530     albertel 4613: sub mkdiruserfile {
                   4614:     my ($docuname,$docudom,$dir)=@_;
                   4615:     my $home=&homeserver($docuname,$docudom);
                   4616:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   4617: }
                   4618: 
1.531     albertel 4619: sub renameuserfile {
                   4620:     my ($docuname,$docudom,$old,$new)=@_;
                   4621:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  4622:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   4623:                         &escape("$old").':'.&escape("$new"),$home);
                   4624:     if ($result eq 'ok') {
                   4625:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   4626:             my $oldmeta = $old.'.meta';
                   4627:             my $newmeta = $new.'.meta';
                   4628:             my $metaresult = 
                   4629:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 4630: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   4631:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  4632:             my $sqlresult = 
1.823     albertel 4633:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4634:                                         'portfolio_metadata',$group,
                   4635:                                         'delete');
1.798     raeburn  4636:         }
                   4637:     }
                   4638:     return $result;
1.531     albertel 4639: }
                   4640: 
1.14      www      4641: # ------------------------------------------------------------------------- Log
                   4642: 
                   4643: sub log {
                   4644:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      4645:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      4646: }
                   4647: 
                   4648: # ------------------------------------------------------------------ Course Log
1.352     www      4649: #
                   4650: # This routine flushes several buffers of non-mission-critical nature
                   4651: #
1.157     www      4652: 
                   4653: sub flushcourselogs {
1.352     www      4654:     &logthis('Flushing log buffers');
                   4655: #
                   4656: # course logs
                   4657: # This is a log of all transactions in a course, which can be used
                   4658: # for data mining purposes
                   4659: #
                   4660: # It also collects the courseid database, which lists last transaction
                   4661: # times and course titles for all courseids
                   4662: #
                   4663:     my %courseidbuffer=();
1.921     raeburn  4664:     foreach my $crsid (keys(%courselogs)) {
1.352     www      4665:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      4666: 		          &escape($courselogs{$crsid}),
                   4667: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      4668: 	    delete $courselogs{$crsid};
                   4669:         } else {
                   4670:             &logthis('Failed to flush log buffer for '.$crsid);
                   4671:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 4672:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      4673:                         " exceeded maximum size, deleting.</font>");
                   4674:                delete $courselogs{$crsid};
                   4675:             }
1.352     www      4676:         }
1.920     raeburn  4677:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  4678:             'description' => $coursedescrbuf{$crsid},
                   4679:             'inst_code'    => $courseinstcodebuf{$crsid},
                   4680:             'type'        => $coursetypebuf{$crsid},
                   4681:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  4682:         };
1.191     harris41 4683:     }
1.352     www      4684: #
                   4685: # Write course id database (reverse lookup) to homeserver of courses 
                   4686: # Is used in pickcourse
                   4687: #
1.840     albertel 4688:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  4689:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  4690:                                     $courseidbuffer{$crs_home},
                   4691:                                     $crs_home,'timeonly');
1.352     www      4692:     }
                   4693: #
                   4694: # File accesses
                   4695: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   4696: #
1.449     matthew  4697:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  4698:         if ($entry =~ /___count$/) {
                   4699:             my ($dom,$name);
1.807     albertel 4700:             ($dom,$name,undef)=
1.811     albertel 4701: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  4702:             if (! defined($dom) || $dom eq '' || 
                   4703:                 ! defined($name) || $name eq '') {
1.620     albertel 4704:                 my $cid = $env{'request.course.id'};
                   4705:                 $dom  = $env{'request.'.$cid.'.domain'};
                   4706:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  4707:             }
1.450     matthew  4708:             my $value = $accesshash{$entry};
                   4709:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   4710:             my %temphash=($url => $value);
1.449     matthew  4711:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   4712:             if ($result eq 'ok') {
                   4713:                 delete $accesshash{$entry};
                   4714:             }
                   4715:         } else {
1.811     albertel 4716:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      4717:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  4718:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  4719:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   4720:                 delete $accesshash{$entry};
                   4721:             }
1.185     www      4722:         }
1.191     harris41 4723:     }
1.352     www      4724: #
                   4725: # Roles
                   4726: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   4727: #
1.800     albertel 4728:     foreach my $entry (keys(%userrolehash)) {
1.351     www      4729:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      4730: 	    split(/\:/,$entry);
                   4731:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      4732:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      4733:                 $rudom,$runame) eq 'ok') {
                   4734: 	    delete $userrolehash{$entry};
                   4735:         }
                   4736:     }
1.662     raeburn  4737: #
1.1334    raeburn  4738: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662     raeburn  4739: #
                   4740:     my %domrolebuffer = ();
1.1000    raeburn  4741:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 4742:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  4743:         if ($domrolebuffer{$rudom}) {
                   4744:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   4745:                       '='.&escape($domainrolehash{$entry});
                   4746:         } else {
                   4747:             $domrolebuffer{$rudom}.=&escape($entry).
                   4748:                       '='.&escape($domainrolehash{$entry});
                   4749:         }
                   4750:         delete $domainrolehash{$entry};
                   4751:     }
                   4752:     foreach my $dom (keys(%domrolebuffer)) {
1.1324    raeburn  4753: 	my %servers;
                   4754: 	if (defined(&domain($dom,'primary'))) {
                   4755: 	    my $primary=&domain($dom,'primary');
                   4756: 	    my $hostname=&hostname($primary);
                   4757: 	    $servers{$primary} = $hostname;
                   4758: 	} else { 
                   4759: 	    %servers = &get_servers($dom,'library');
                   4760: 	}
1.841     albertel 4761: 	foreach my $tryserver (keys(%servers)) {
1.1324    raeburn  4762: 	    if (&reply('domroleput:'.$dom.':'.
                   4763: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   4764: 		last;
                   4765: 	    } else {  
1.841     albertel 4766: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   4767: 	    }
1.662     raeburn  4768:         }
                   4769:     }
1.186     www      4770:     $dumpcount++;
1.157     www      4771: }
                   4772: 
                   4773: sub courselog {
                   4774:     my $what=shift;
1.158     www      4775:     $what=time.':'.$what;
1.620     albertel 4776:     unless ($env{'request.course.id'}) { return ''; }
                   4777:     $coursedombuf{$env{'request.course.id'}}=
                   4778:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4779:     $coursenumbuf{$env{'request.course.id'}}=
                   4780:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   4781:     $coursehombuf{$env{'request.course.id'}}=
                   4782:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   4783:     $coursedescrbuf{$env{'request.course.id'}}=
                   4784:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   4785:     $courseinstcodebuf{$env{'request.course.id'}}=
                   4786:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   4787:     $courseownerbuf{$env{'request.course.id'}}=
                   4788:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  4789:     $coursetypebuf{$env{'request.course.id'}}=
                   4790:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 4791:     if (defined $courselogs{$env{'request.course.id'}}) {
                   4792: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      4793:     } else {
1.620     albertel 4794: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      4795:     }
1.620     albertel 4796:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      4797: 	&flushcourselogs();
                   4798:     }
1.158     www      4799: }
                   4800: 
                   4801: sub courseacclog {
                   4802:     my $fnsymb=shift;
1.620     albertel 4803:     unless ($env{'request.course.id'}) { return ''; }
                   4804:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      4805:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      4806:         $what.=':POST';
1.583     matthew  4807:         # FIXME: Probably ought to escape things....
1.800     albertel 4808: 	foreach my $key (keys(%env)) {
                   4809:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  4810:                 my $formitem = $1;
                   4811:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   4812:                     $what.=':'.$formitem.'='.$env{$key};
                   4813:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
                   4814:                     $what.=':'.$formitem.'='.$env{$key};
                   4815:                 }
1.158     www      4816:             }
1.191     harris41 4817:         }
1.583     matthew  4818:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   4819:         # FIXME: We should not be depending on a form parameter that someone
                   4820:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 4821:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  4822:             $what.= ':POST';
                   4823:             # FIXME: Probably ought to escape things....
                   4824:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   4825:                                  'crsdiscuss') {
1.620     albertel 4826:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  4827:             }
                   4828:         }
1.158     www      4829:     }
                   4830:     &courselog($what);
1.149     www      4831: }
                   4832: 
1.185     www      4833: sub countacc {
                   4834:     my $url=&declutter(shift);
1.458     matthew  4835:     return if (! defined($url) || $url eq '');
1.620     albertel 4836:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      4837: #
                   4838: # Mark that this url was used in this course
                   4839: #
1.620     albertel 4840:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      4841: #
                   4842: # Increase the access count for this resource in this child process
                   4843: #
1.281     www      4844:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  4845:     $accesshash{$key}++;
1.185     www      4846: }
1.349     www      4847: 
1.361     www      4848: sub linklog {
                   4849:     my ($from,$to)=@_;
                   4850:     $from=&declutter($from);
                   4851:     $to=&declutter($to);
                   4852:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   4853:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   4854: }
1.1160    www      4855: 
                   4856: sub statslog {
                   4857:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   4858:     if ($users<2) { return; }
                   4859:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   4860:             'course'       => $env{'request.course.id'},
                   4861:             'sections'     => '"all"',
                   4862:             'num_students' => $users,
                   4863:             'part'         => $part,
                   4864:             'symb'         => $symb,
                   4865:             'mean_tries'   => $av_attempts,
                   4866:             'deg_of_diff'  => $degdiff});
                   4867:     foreach my $key (keys(%dynstore)) {
                   4868:         $accesshash{$key}=$dynstore{$key};
                   4869:     }
                   4870: }
1.361     www      4871:   
1.349     www      4872: sub userrolelog {
                   4873:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 4874:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      4875:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   4876:        $userrolehash
                   4877:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      4878:                     =$tend.':'.$tstart;
1.662     raeburn  4879:     }
1.1169    droeschl 4880:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 4881:        $userrolehash
                   4882:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   4883:                     =$tend.':'.$tstart;
                   4884:     }
1.1334    raeburn  4885:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662     raeburn  4886:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   4887:        $domainrolehash
                   4888:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   4889:                     = $tend.':'.$tstart;
                   4890:     }
1.351     www      4891: }
                   4892: 
1.957     raeburn  4893: sub courserolelog {
                   4894:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184    raeburn  4895:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   4896:         my $cdom = $1;
                   4897:         my $cnum = $2;
                   4898:         my $sec = $3;
                   4899:         my $namespace = 'rolelog';
                   4900:         my %storehash = (
                   4901:                            role    => $trole,
                   4902:                            start   => $tstart,
                   4903:                            end     => $tend,
                   4904:                            selfenroll => $selfenroll,
                   4905:                            context    => $context,
                   4906:                         );
                   4907:         if ($trole eq 'gr') {
                   4908:             $namespace = 'groupslog';
                   4909:             $storehash{'group'} = $sec;
                   4910:         } else {
                   4911:             $storehash{'section'} = $sec;
                   4912:         }
1.1188    raeburn  4913:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   4914:                    $domain,$cnum,$cdom);
1.1184    raeburn  4915:         if (($trole ne 'st') || ($sec ne '')) {
                   4916:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  4917:         }
                   4918:     }
                   4919:     return;
                   4920: }
                   4921: 
1.1184    raeburn  4922: sub domainrolelog {
                   4923:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   4924:     if ($area =~ m{^/($match_domain)/$}) {
                   4925:         my $cdom = $1;
                   4926:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   4927:         my $namespace = 'rolelog';
                   4928:         my %storehash = (
                   4929:                            role    => $trole,
                   4930:                            start   => $tstart,
                   4931:                            end     => $tend,
                   4932:                            context => $context,
                   4933:                         );
1.1188    raeburn  4934:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   4935:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  4936:     }
                   4937:     return;
                   4938: 
                   4939: }
                   4940: 
                   4941: sub coauthorrolelog {
                   4942:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   4943:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   4944:         my $audom = $1;
                   4945:         my $auname = $2;
                   4946:         my $namespace = 'rolelog';
                   4947:         my %storehash = (
                   4948:                            role    => $trole,
                   4949:                            start   => $tstart,
                   4950:                            end     => $tend,
                   4951:                            context => $context,
                   4952:                         );
1.1188    raeburn  4953:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   4954:                    $domain,$auname,$audom);
1.1184    raeburn  4955:     }
                   4956:     return;
                   4957: }
                   4958: 
1.351     www      4959: sub get_course_adv_roles {
1.948     raeburn  4960:     my ($cid,$codes) = @_;
1.620     albertel 4961:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      4962:     my %coursehash=&coursedescription($cid);
1.988     raeburn  4963:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      4964:     my %nothide=();
1.800     albertel 4965:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  4966:         if ($user !~ /:/) {
                   4967: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   4968:         } else {
                   4969:             $nothide{$user}=1;
                   4970:         }
1.470     www      4971:     }
1.1219    raeburn  4972:     my @possdoms = ($coursehash{'domain'});
                   4973:     if ($coursehash{'checkforpriv'}) {
                   4974:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   4975:     }
1.351     www      4976:     my %returnhash=();
                   4977:     my %dumphash=
                   4978:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   4979:     my $now=time;
1.997     raeburn  4980:     my %privileged;
1.1000    raeburn  4981:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 4982: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      4983:         if (($tstart) && ($tstart<0)) { next; }
                   4984:         if (($tend) && ($tend<$now)) { next; }
                   4985:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 4986:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 4987: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  4988:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  4989:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 4990: 	if ($role eq 'cr') { next; }
1.948     raeburn  4991:         if ($codes) {
                   4992:             if ($section) { $role .= ':'.$section; }
                   4993:             if ($returnhash{$role}) {
                   4994:                 $returnhash{$role}.=','.$username.':'.$domain;
                   4995:             } else {
                   4996:                 $returnhash{$role}=$username.':'.$domain;
                   4997:             }
1.351     www      4998:         } else {
1.988     raeburn  4999:             my $key=&plaintext($role,$crstype);
1.973     bisitz   5000:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  5001:             if ($returnhash{$key}) {
                   5002: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   5003:             } else {
                   5004:                 $returnhash{$key}=$username.':'.$domain;
                   5005:             }
1.351     www      5006:         }
1.948     raeburn  5007:     }
1.400     www      5008:     return %returnhash;
                   5009: }
                   5010: 
                   5011: sub get_my_roles {
1.937     raeburn  5012:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 5013:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   5014:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  5015:     my (%dumphash,%nothide);
1.1086    raeburn  5016:     if ($context eq 'userroles') {
1.1166    raeburn  5017:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  5018:     } else {
1.1219    raeburn  5019:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  5020:         if ($hidepriv) {
                   5021:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   5022:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   5023:                 if ($user !~ /:/) {
                   5024:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   5025:                 } else {
                   5026:                     $nothide{$user} = 1;
                   5027:                 }
                   5028:             }
                   5029:         }
1.858     raeburn  5030:     }
1.400     www      5031:     my %returnhash=();
                   5032:     my $now=time;
1.999     raeburn  5033:     my %privileged;
1.800     albertel 5034:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  5035:         my ($role,$tend,$tstart);
                   5036:         if ($context eq 'userroles') {
1.1149    raeburn  5037:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  5038: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   5039:         } else {
                   5040:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   5041:         }
1.400     www      5042:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  5043:         my $status = 'active';
1.939     raeburn  5044:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  5045:             $status = 'previous';
                   5046:         } 
                   5047:         if (($tstart) && ($now<$tstart)) {
                   5048:             $status = 'future';
                   5049:         }
                   5050:         if (ref($types) eq 'ARRAY') {
                   5051:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   5052:                 next;
                   5053:             } 
                   5054:         } else {
                   5055:             if ($status ne 'active') {
                   5056:                 next;
                   5057:             }
                   5058:         }
1.867     raeburn  5059:         my ($rolecode,$username,$domain,$section,$area);
                   5060:         if ($context eq 'userroles') {
1.1186    raeburn  5061:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  5062:             (undef,$domain,$username,$section) = split(/\//,$area);
                   5063:         } else {
                   5064:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   5065:         }
1.832     raeburn  5066:         if (ref($roledoms) eq 'ARRAY') {
                   5067:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   5068:                 next;
                   5069:             }
                   5070:         }
                   5071:         if (ref($roles) eq 'ARRAY') {
                   5072:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  5073:                 if ($role =~ /^cr\//) {
                   5074:                     if (!grep(/^cr$/,@{$roles})) {
                   5075:                         next;
                   5076:                     }
1.1104    raeburn  5077:                 } elsif ($role =~ /^gr\//) {
                   5078:                     if (!grep(/^gr$/,@{$roles})) {
                   5079:                         next;
                   5080:                     }
1.922     raeburn  5081:                 } else {
                   5082:                     next;
                   5083:                 }
1.832     raeburn  5084:             }
1.867     raeburn  5085:         }
1.937     raeburn  5086:         if ($hidepriv) {
1.1219    raeburn  5087:             my @privroles = ('dc','su');
1.999     raeburn  5088:             if ($context eq 'userroles') {
1.1219    raeburn  5089:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  5090:             } else {
1.1219    raeburn  5091:                 my $possdoms = [$domain];
                   5092:                 if (ref($roledoms) eq 'ARRAY') {
                   5093:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  5094:                 }
1.1219    raeburn  5095:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  5096:                     if (!$nothide{$username.':'.$domain}) {
                   5097:                         next;
                   5098:                     }
                   5099:                 }
1.937     raeburn  5100:             }
                   5101:         }
1.933     raeburn  5102:         if ($withsec) {
                   5103:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   5104:                 $tstart.':'.$tend;
                   5105:         } else {
                   5106:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   5107:         }
1.832     raeburn  5108:     }
1.373     www      5109:     return %returnhash;
1.399     www      5110: }
                   5111: 
1.1333    raeburn  5112: sub get_all_adhocroles {
                   5113:     my ($dom) = @_;
                   5114:     my @roles_by_num = ();
                   5115:     my %domdefaults = &get_domain_defaults($dom);
                   5116:     my (%description,%access_in_dom,%access_info);
                   5117:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   5118:         my $count = 0;
                   5119:         my %domcurrent = %{$domdefaults{'adhocroles'}};
                   5120:         my %ordered;
                   5121:         foreach my $role (sort(keys(%domcurrent))) {
                   5122:             my ($order,$desc,$access_in_dom);
                   5123:             if (ref($domcurrent{$role}) eq 'HASH') {
                   5124:                 $order = $domcurrent{$role}{'order'};
                   5125:                 $desc = $domcurrent{$role}{'desc'};
                   5126:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
                   5127:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
                   5128:             }
                   5129:             if ($order eq '') {
                   5130:                 $order = $count;
                   5131:             }
                   5132:             $ordered{$order} = $role;
                   5133:             if ($desc ne '') {
                   5134:                 $description{$role} = $desc;
                   5135:             } else {
                   5136:                 $description{$role}= $role;
                   5137:             }
                   5138:             $count++;
                   5139:         }
                   5140:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   5141:             push(@roles_by_num,$ordered{$item});
                   5142:         }
                   5143:     }
                   5144:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
                   5145: }
                   5146: 
1.1332    raeburn  5147: sub get_my_adhocroles {
1.1333    raeburn  5148:     my ($cid,$checkreg) = @_;
                   5149:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
                   5150:     if ($env{'request.course.id'} eq $cid) {
                   5151:         $cdom = $env{'course.'.$cid.'.domain'};
                   5152:         $cnum = $env{'course.'.$cid.'.num'};
                   5153:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
                   5154:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
                   5155:         $cdom = $1;
                   5156:         $cnum = $2;
                   5157:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
                   5158:                                      $cdom,$cnum);
                   5159:     }
                   5160:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
                   5161:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   5162:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
                   5163:         if ($rosterhash{$user} ne '') {
                   5164:             my $type = (split(/:/,$rosterhash{$user}))[5];
                   5165:             return ([],{}) if ($type eq 'auto');
                   5166:         }
                   5167:     }
                   5168:     if (($cdom ne '') && ($cnum ne ''))  {
1.1334    raeburn  5169:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332    raeburn  5170:             my $then=$env{'user.login.time'};
                   5171:             my $update=$env{'user.update.time'};
1.1335    raeburn  5172:             if (!$update) {
                   5173:                 $update = $then;
                   5174:             }
                   5175:             my @liveroles;
1.1334    raeburn  5176:             foreach my $role ('dh','da') {
                   5177:                 if ($env{"user.role.$role./$cdom/"}) {
1.1335    raeburn  5178:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334    raeburn  5179:                     my $limit = $update;
                   5180:                     if ($env{'request.role'} eq "$role./$cdom/") {
                   5181:                         $limit = $then;
                   5182:                     }
1.1335    raeburn  5183:                     my $activerole = 1;
                   5184:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
                   5185:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
                   5186:                     if ($activerole) {
                   5187:                         push(@liveroles,$role);
                   5188:                     }
1.1334    raeburn  5189:                 }
1.1332    raeburn  5190:             }
1.1335    raeburn  5191:             if (@liveroles) {
1.1332    raeburn  5192:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333    raeburn  5193:                     my ($accessref,$accessinfo,%access_in_dom);
                   5194:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
                   5195:                     if (ref($roles_by_num) eq 'ARRAY') {
                   5196:                         if (@{$roles_by_num}) {
1.1332    raeburn  5197:                             my %settings;
                   5198:                             if ($env{'request.course.id'} eq $cid) {
                   5199:                                 foreach my $envkey (keys(%env)) {
                   5200:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
                   5201:                                         $settings{$1} = $env{$envkey};
                   5202:                                     }
                   5203:                                 }
                   5204:                             } else {
                   5205:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
                   5206:                             }
                   5207:                             my %setincrs;
                   5208:                             if ($settings{'internal.adhocaccess'}) {
                   5209:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
                   5210:                             }
                   5211:                             my @statuses;
                   5212:                             if ($env{'environment.inststatus'}) {
                   5213:                                 @statuses = split(/,/,$env{'environment.inststatus'});
                   5214:                             }
                   5215:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333    raeburn  5216:                             if (ref($accessref) eq 'HASH') {
                   5217:                                 %access_in_dom = %{$accessref};
                   5218:                             }
                   5219:                             foreach my $role (@{$roles_by_num}) {
1.1332    raeburn  5220:                                 my ($curraccess,@okstatus,@personnel);
                   5221:                                 if ($setincrs{$role}) {
                   5222:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
                   5223:                                     if ($curraccess eq 'status') {
                   5224:                                         @okstatus = split(/\&/,$rest);
                   5225:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5226:                                         @personnel = split(/\&/,$rest);
                   5227:                                     }
                   5228:                                 } else {
                   5229:                                     $curraccess = $access_in_dom{$role};
1.1333    raeburn  5230:                                     if (ref($accessinfo) eq 'HASH') {
                   5231:                                         if ($curraccess eq 'status') {
                   5232:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5233:                                                 @okstatus = @{$accessinfo->{$role}};
                   5234:                                             }
                   5235:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5236:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   5237:                                                 @personnel = @{$accessinfo->{$role}};
                   5238:                                             }
1.1332    raeburn  5239:                                         }
                   5240:                                     }
                   5241:                                 }
                   5242:                                 if ($curraccess eq 'none') {
                   5243:                                     next;
                   5244:                                 } elsif ($curraccess eq 'all') {
                   5245:                                     push(@possroles,$role);
1.1336    raeburn  5246:                                 } elsif ($curraccess eq 'dh') {
1.1335    raeburn  5247:                                     if (grep(/^dh$/,@liveroles)) {
                   5248:                                         push(@possroles,$role);
                   5249:                                     } else {
                   5250:                                         next;
                   5251:                                     }
1.1336    raeburn  5252:                                 } elsif ($curraccess eq 'da') {
1.1335    raeburn  5253:                                     if (grep(/^da$/,@liveroles)) {
                   5254:                                         push(@possroles,$role);
                   5255:                                     } else {
                   5256:                                         next;
                   5257:                                     }
1.1332    raeburn  5258:                                 } elsif ($curraccess eq 'status') {
                   5259:                                     if (@okstatus) {
                   5260:                                         if (!@statuses) {
                   5261:                                             if (grep(/^default$/,@okstatus)) {
                   5262:                                                 push(@possroles,$role);
                   5263:                                             }
                   5264:                                         } else {
                   5265:                                             foreach my $status (@okstatus) {
                   5266:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
                   5267:                                                     push(@possroles,$role);
                   5268:                                                     last;
                   5269:                                                 }
                   5270:                                             }
                   5271:                                         }
                   5272:                                     }
                   5273:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   5274:                                     if (grep(/^\Q$user\E$/,@personnel)) {
                   5275:                                         if ($curraccess eq 'exc') {
                   5276:                                             push(@possroles,$role);
                   5277:                                         }
                   5278:                                     } elsif ($curraccess eq 'inc') {
                   5279:                                         push(@possroles,$role);
                   5280:                                     }
                   5281:                                 }
                   5282:                             }
                   5283:                         }
                   5284:                     }
                   5285:                 }
                   5286:             }
                   5287:         }
                   5288:     }
1.1333    raeburn  5289:     unless (ref($description) eq 'HASH') {
                   5290:         if (ref($roles_by_num) eq 'ARRAY') {
                   5291:             my %desc;
                   5292:             map { $desc{$_} = $_; } (@{$roles_by_num});
                   5293:             $description = \%desc;
                   5294:         } else {
                   5295:             $description = {};
                   5296:         }
                   5297:     }
                   5298:     return (\@possroles,$description);
1.1332    raeburn  5299: }
                   5300: 
1.399     www      5301: # ----------------------------------------------------- Frontpage Announcements
                   5302: #
                   5303: #
                   5304: 
                   5305: sub postannounce {
                   5306:     my ($server,$text)=@_;
1.844     albertel 5307:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      5308:     unless ($text=~/\w/) { $text=''; }
                   5309:     return &reply('setannounce:'.&escape($text),$server);
                   5310: }
                   5311: 
                   5312: sub getannounce {
1.448     albertel 5313: 
1.1359    raeburn  5314:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      5315: 	my $announcement='';
1.800     albertel 5316: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 5317: 	close($fh);
1.399     www      5318: 	if ($announcement=~/\w/) { 
                   5319: 	    return 
                   5320:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 5321:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      5322: 	} else {
                   5323: 	    return '';
                   5324: 	}
                   5325:     } else {
                   5326: 	return '';
                   5327:     }
1.351     www      5328: }
1.353     www      5329: 
                   5330: # ---------------------------------------------------------- Course ID routines
                   5331: # Deal with domain's nohist_courseid.db files
                   5332: #
                   5333: 
                   5334: sub courseidput {
1.921     raeburn  5335:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  5336:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  5337:     my $outcome;
                   5338:     if ($caller eq 'timeonly') {
                   5339:         my $cids = '';
                   5340:         foreach my $item (keys(%$storehash)) {
                   5341:             $cids.=&escape($item).'&';
                   5342:         }
                   5343:         $cids=~s/\&$//;
                   5344:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   5345:                           $coursehome);       
                   5346:     } else {
                   5347:         my $items = '';
                   5348:         foreach my $item (keys(%$storehash)) {
                   5349:             $items.= &escape($item).'='.
                   5350:                      &freeze_escape($$storehash{$item}).'&';
                   5351:         }
                   5352:         $items=~s/\&$//;
                   5353:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   5354:                           $coursehome);
1.918     raeburn  5355:     }
                   5356:     if ($outcome eq 'unknown_cmd') {
                   5357:         my $what;
                   5358:         foreach my $cid (keys(%$storehash)) {
                   5359:             $what .= &escape($cid).'=';
1.921     raeburn  5360:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  5361:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  5362:             }
                   5363:             $what =~ s/\:$/&/;
                   5364:         }
                   5365:         $what =~ s/\&$//;  
                   5366:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   5367:     } else {
                   5368:         return $outcome;
                   5369:     }
1.353     www      5370: }
                   5371: 
                   5372: sub courseiddump {
1.921     raeburn  5373:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  5374:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  5375:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247    raeburn  5376:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287    raeburn  5377:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918     raeburn  5378:     my $as_hash = 1;
                   5379:     my %returnhash;
                   5380:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 5381:     my %libserv = &all_library();
                   5382:     foreach my $tryserver (keys(%libserv)) {
                   5383:         if ( (  $hostidflag == 1 
                   5384: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   5385: 	     || (!defined($hostidflag)) ) {
                   5386: 
1.918     raeburn  5387: 	    if (($domfilter eq '') ||
                   5388: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 5389:                 my $rep;
                   5390:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   5391:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   5392:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   5393:                                 &escape($descfilter), &escape($instcodefilter), 
                   5394:                                 &escape($ownerfilter), &escape($coursefilter),
                   5395:                                 &escape($typefilter), &escape($regexp_ok), 
                   5396:                                 $as_hash, &escape($selfenrollonly), 
                   5397:                                 &escape($catfilter), $showhidden, $caller, 
                   5398:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   5399:                                 &escape($createdbefore), &escape($createdafter), 
1.1287    raeburn  5400:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
                   5401:                                 $reqcrsdom,&escape($reqinstcode))));
1.1180    droeschl 5402:                 } else {
                   5403:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   5404:                              $sincefilter.':'.&escape($descfilter).':'.
                   5405:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   5406:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   5407:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   5408:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   5409:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   5410:                              &escape($cc_clone).':'.$cloneonly.':'.
                   5411:                              &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287    raeburn  5412:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
                   5413:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180    droeschl 5414:                 }
                   5415:                      
1.918     raeburn  5416:                 my @pairs=split(/\&/,$rep);
                   5417:                 foreach my $item (@pairs) {
                   5418:                     my ($key,$value)=split(/\=/,$item,2);
                   5419:                     $key = &unescape($key);
                   5420:                     next if ($key =~ /^error: 2 /);
                   5421:                     my $result = &thaw_unescape($value);
                   5422:                     if (ref($result) eq 'HASH') {
                   5423:                         $returnhash{$key}=$result;
                   5424:                     } else {
1.921     raeburn  5425:                         my @responses = split(/:/,$value);
                   5426:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  5427:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  5428:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  5429:                         }
1.1008    raeburn  5430:                     }
1.353     www      5431:                 }
                   5432:             }
                   5433:         }
                   5434:     }
                   5435:     return %returnhash;
                   5436: }
                   5437: 
1.1055    raeburn  5438: sub courselastaccess {
                   5439:     my ($cdom,$cnum,$hostidref) = @_;
                   5440:     my %returnhash;
                   5441:     if ($cdom && $cnum) {
                   5442:         my $chome = &homeserver($cnum,$cdom);
                   5443:         if ($chome ne 'no_host') {
                   5444:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   5445:             &extract_lastaccess(\%returnhash,$rep);
                   5446:         }
                   5447:     } else {
                   5448:         if (!$cdom) { $cdom=''; }
                   5449:         my %libserv = &all_library();
                   5450:         foreach my $tryserver (keys(%libserv)) {
                   5451:             if (ref($hostidref) eq 'ARRAY') {
                   5452:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   5453:             } 
                   5454:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   5455:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   5456:                 &extract_lastaccess(\%returnhash,$rep);
                   5457:             }
                   5458:         }
                   5459:     }
                   5460:     return %returnhash;
                   5461: }
                   5462: 
                   5463: sub extract_lastaccess {
                   5464:     my ($returnhash,$rep) = @_;
                   5465:     if (ref($returnhash) eq 'HASH') {
                   5466:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   5467:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   5468:                  $rep eq '') {
                   5469:             my @pairs=split(/\&/,$rep);
                   5470:             foreach my $item (@pairs) {
                   5471:                 my ($key,$value)=split(/\=/,$item,2);
                   5472:                 $key = &unescape($key);
                   5473:                 next if ($key =~ /^error: 2 /);
                   5474:                 $returnhash->{$key} = &thaw_unescape($value);
                   5475:             }
                   5476:         }
                   5477:     }
                   5478:     return;
                   5479: }
                   5480: 
1.658     raeburn  5481: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  5482: 
                   5483: sub dcmailput {
1.685     raeburn  5484:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  5485:     my $status = &Apache::lonnet::critical(
1.740     www      5486:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   5487:        &escape($message),$server);
1.662     raeburn  5488:     return $status;
                   5489: }
                   5490: 
1.658     raeburn  5491: sub dcmaildump {
                   5492:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  5493:     my %returnhash=();
1.846     albertel 5494: 
                   5495:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  5496:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   5497:                                                          &escape($enddate).':';
                   5498: 	my @esc_senders=map { &escape($_)} @$senders;
                   5499: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 5500: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 5501:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  5502:             if (($key) && ($value)) {
                   5503:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  5504:             }
                   5505:         }
                   5506:     }
                   5507:     return %returnhash;
                   5508: }
1.662     raeburn  5509: # ---------------------------------------------------------- Domain roles
                   5510: 
                   5511: sub get_domain_roles {
                   5512:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  5513:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  5514:         $startdate = '.';
                   5515:     }
1.1018    raeburn  5516:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  5517:         $enddate = '.';
                   5518:     }
1.922     raeburn  5519:     my $rolelist;
                   5520:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  5521:         $rolelist = join('&',@{$roles});
1.922     raeburn  5522:     }
1.662     raeburn  5523:     my %personnel = ();
1.841     albertel 5524: 
                   5525:     my %servers = &get_servers($dom,'library');
                   5526:     foreach my $tryserver (keys(%servers)) {
                   5527: 	%{$personnel{$tryserver}}=();
                   5528: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   5529: 					    &escape($startdate).':'.
                   5530: 					    &escape($enddate).':'.
                   5531: 					    &escape($rolelist), $tryserver))) {
                   5532: 	    my ($key,$value) = split(/\=/,$line,2);
                   5533: 	    if (($key) && ($value)) {
                   5534: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   5535: 	    }
                   5536: 	}
1.662     raeburn  5537:     }
                   5538:     return %personnel;
                   5539: }
1.658     raeburn  5540: 
1.1332    raeburn  5541: sub get_active_domroles {
                   5542:     my ($dom,$roles) = @_;
                   5543:     return () unless (ref($roles) eq 'ARRAY');
                   5544:     my $now = time;
                   5545:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
                   5546:     my %domroles;
                   5547:     foreach my $server (keys(%dompersonnel)) {
                   5548:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   5549:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
                   5550:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
                   5551:         }
                   5552:     }
                   5553:     return %domroles;
                   5554: }
                   5555: 
1.1057    www      5556: # ----------------------------------------------------------- Interval timing 
1.149     www      5557: 
1.1153    www      5558: {
                   5559: # Caches needed for speedup of navmaps
                   5560: # We don't want to cache this for very long at all (5 seconds at most)
                   5561: # 
                   5562: # The user for whom we cache
                   5563: my $cachedkey='';
                   5564: # The cached times for this user
                   5565: my %cachedtimes=();
                   5566: # When this was last done
1.1282    raeburn  5567: my $cachedtime='';
1.1153    www      5568: 
                   5569: sub load_all_first_access {
1.1308    raeburn  5570:     my ($uname,$udom,$ignorecache)=@_;
1.1156    www      5571:     if (($cachedkey eq $uname.':'.$udom) &&
1.1308    raeburn  5572:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
                   5573:         (!$ignorecache)) {
1.1154    raeburn  5574:         return;
                   5575:     }
                   5576:     $cachedtime=time;
                   5577:     $cachedkey=$uname.':'.$udom;
                   5578:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      5579: }
                   5580: 
1.504     albertel 5581: sub get_first_access {
1.1308    raeburn  5582:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790     albertel 5583:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 5584:     if ($argsymb) { $symb=$argsymb; }
                   5585:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  5586:     if ($argmap) { $map = $argmap; }
1.926     albertel 5587:     if ($type eq 'course') {
                   5588: 	$res='course';
                   5589:     } elsif ($type eq 'map') {
1.588     albertel 5590: 	$res=&symbread($map);
                   5591:     } else {
                   5592: 	$res=$symb;
                   5593:     }
1.1308    raeburn  5594:     &load_all_first_access($uname,$udom,$ignorecache);
1.1153    www      5595:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 5596: }
                   5597: 
                   5598: sub set_first_access {
1.1162    raeburn  5599:     my ($type,$interval)=@_;
1.790     albertel 5600:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 5601:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 5602:     if ($type eq 'course') {
                   5603: 	$res='course';
                   5604:     } elsif ($type eq 'map') {
1.588     albertel 5605: 	$res=&symbread($map);
                   5606:     } else {
                   5607: 	$res=$symb;
                   5608:     }
1.1153    www      5609:     $cachedkey='';
1.1162    raeburn  5610:     my $firstaccess=&get_first_access($type,$symb,$map);
1.1386    raeburn  5611:     if ($firstaccess) {
                   5612:         &logthis("First access time already set ($firstaccess) when attempting ".
1.1393    raeburn  5613:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
                   5614:                  "in $courseid");
1.1386    raeburn  5615:         return 'already_set';
                   5616:     } else {
1.1162    raeburn  5617:         my $start = time;
                   5618: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   5619:                           $udom,$uname);
                   5620:         if ($putres eq 'ok') {
                   5621:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   5622:                  $udom,$uname); 
                   5623:             &appenv(
                   5624:                      {
                   5625:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   5626:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   5627:                      }
                   5628:                   );
1.1365    raeburn  5629:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
                   5630:                 $cachedtimes{"$courseid\0$res"} = $start;
                   5631:             }
1.1386    raeburn  5632:         } elsif ($putres ne 'refused') {
                   5633:             &logthis("Result: $putres when attempting to set first access time ".
                   5634:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162    raeburn  5635:         }
                   5636:         return $putres;
1.505     albertel 5637:     }
                   5638:     return 'already_set';
1.504     albertel 5639: }
1.1153    www      5640: }
1.1282    raeburn  5641: 
1.110     www      5642: # --------------------------------------------- Set Expire Date for Spreadsheet
                   5643: 
                   5644: sub expirespread {
                   5645:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 5646:     my $cid=$env{'request.course.id'}; 
1.110     www      5647:     if ($cid) {
                   5648:        my $now=time;
                   5649:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 5650:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   5651:                             $env{'course.'.$cid.'.num'}.
1.110     www      5652: 	        	    ':nohist_expirationdates:'.
                   5653:                             &escape($key).'='.$now,
1.620     albertel 5654:                             $env{'course.'.$cid.'.home'})
1.110     www      5655:     }
                   5656:     return 'ok';
1.14      www      5657: }
                   5658: 
1.109     www      5659: # ----------------------------------------------------- Devalidate Spreadsheets
                   5660: 
                   5661: sub devalidate {
1.325     www      5662:     my ($symb,$uname,$udom)=@_;
1.620     albertel 5663:     my $cid=$env{'request.course.id'}; 
1.109     www      5664:     if ($cid) {
1.391     matthew  5665:         # delete the stored spreadsheets for
                   5666:         # - the student level sheet of this user in course's homespace
                   5667:         # - the assessment level sheet for this resource 
                   5668:         #   for this user in user's homespace
1.553     albertel 5669: 	# - current conditional state info
1.325     www      5670: 	my $key=$uname.':'.$udom.':';
1.109     www      5671:         my $status=
1.299     matthew  5672: 	    &del('nohist_calculatedsheets',
1.391     matthew  5673: 		 [$key.'studentcalc:'],
1.620     albertel 5674: 		 $env{'course.'.$cid.'.domain'},
                   5675: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 5676: 		.' '.
                   5677: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  5678: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      5679:         unless ($status eq 'ok ok') {
                   5680:            &logthis('Could not devalidate spreadsheet '.
1.325     www      5681:                     $uname.' at '.$udom.' for '.
1.109     www      5682: 		    $symb.': '.$status);
1.133     albertel 5683:         }
1.553     albertel 5684: 	&delenv('user.state.'.$cid);
1.109     www      5685:     }
                   5686: }
                   5687: 
1.265     albertel 5688: sub get_scalar {
                   5689:     my ($string,$end) = @_;
                   5690:     my $value;
                   5691:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   5692: 	$value = $1;
                   5693:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   5694: 	$value = $1;
                   5695:     }
                   5696:     return &unescape($value);
                   5697: }
                   5698: 
                   5699: sub array2str {
                   5700:   my (@array) = @_;
                   5701:   my $result=&arrayref2str(\@array);
                   5702:   $result=~s/^__ARRAY_REF__//;
                   5703:   $result=~s/__END_ARRAY_REF__$//;
                   5704:   return $result;
                   5705: }
                   5706: 
1.204     albertel 5707: sub arrayref2str {
                   5708:   my ($arrayref) = @_;
1.265     albertel 5709:   my $result='__ARRAY_REF__';
1.204     albertel 5710:   foreach my $elem (@$arrayref) {
1.265     albertel 5711:     if(ref($elem) eq 'ARRAY') {
                   5712:       $result.=&arrayref2str($elem).'&';
                   5713:     } elsif(ref($elem) eq 'HASH') {
                   5714:       $result.=&hashref2str($elem).'&';
                   5715:     } elsif(ref($elem)) {
                   5716:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 5717:     } else {
                   5718:       $result.=&escape($elem).'&';
                   5719:     }
                   5720:   }
                   5721:   $result=~s/\&$//;
1.265     albertel 5722:   $result .= '__END_ARRAY_REF__';
1.204     albertel 5723:   return $result;
                   5724: }
                   5725: 
1.168     albertel 5726: sub hash2str {
1.204     albertel 5727:   my (%hash) = @_;
                   5728:   my $result=&hashref2str(\%hash);
1.265     albertel 5729:   $result=~s/^__HASH_REF__//;
                   5730:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 5731:   return $result;
                   5732: }
                   5733: 
                   5734: sub hashref2str {
                   5735:   my ($hashref)=@_;
1.265     albertel 5736:   my $result='__HASH_REF__';
1.800     albertel 5737:   foreach my $key (sort(keys(%$hashref))) {
                   5738:     if (ref($key) eq 'ARRAY') {
                   5739:       $result.=&arrayref2str($key).'=';
                   5740:     } elsif (ref($key) eq 'HASH') {
                   5741:       $result.=&hashref2str($key).'=';
                   5742:     } elsif (ref($key)) {
1.265     albertel 5743:       $result.='=';
1.800     albertel 5744:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 5745:     } else {
1.1132    raeburn  5746: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 5747:     }
                   5748: 
1.800     albertel 5749:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   5750:       $result.=&arrayref2str($hashref->{$key}).'&';
                   5751:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   5752:       $result.=&hashref2str($hashref->{$key}).'&';
                   5753:     } elsif(ref($hashref->{$key})) {
1.265     albertel 5754:        $result.='&';
1.800     albertel 5755:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 5756:     } else {
1.800     albertel 5757:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 5758:     }
                   5759:   }
1.168     albertel 5760:   $result=~s/\&$//;
1.265     albertel 5761:   $result .= '__END_HASH_REF__';
1.168     albertel 5762:   return $result;
                   5763: }
                   5764: 
                   5765: sub str2hash {
1.265     albertel 5766:     my ($string)=@_;
                   5767:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   5768:     return %$hash;
                   5769: }
                   5770: 
                   5771: sub str2hashref {
1.168     albertel 5772:   my ($string) = @_;
1.265     albertel 5773: 
                   5774:   my %hash;
                   5775: 
                   5776:   if($string !~ /^__HASH_REF__/) {
                   5777:       if (! ($string eq '' || !defined($string))) {
                   5778: 	  $hash{'error'}='Not hash reference';
                   5779:       }
                   5780:       return (\%hash, $string);
                   5781:   }
                   5782: 
                   5783:   $string =~ s/^__HASH_REF__//;
                   5784: 
                   5785:   while($string !~ /^__END_HASH_REF__/) {
                   5786:       #key
                   5787:       my $key='';
                   5788:       if($string =~ /^__HASH_REF__/) {
                   5789:           ($key, $string)=&str2hashref($string);
                   5790:           if(defined($key->{'error'})) {
                   5791:               $hash{'error'}='Bad data';
                   5792:               return (\%hash, $string);
                   5793:           }
                   5794:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5795:           ($key, $string)=&str2arrayref($string);
                   5796:           if($key->[0] eq 'Array reference error') {
                   5797:               $hash{'error'}='Bad data';
                   5798:               return (\%hash, $string);
                   5799:           }
                   5800:       } else {
                   5801:           $string =~ s/^(.*?)=//;
1.267     albertel 5802: 	  $key=&unescape($1);
1.265     albertel 5803:       }
                   5804:       $string =~ s/^=//;
                   5805: 
                   5806:       #value
                   5807:       my $value='';
                   5808:       if($string =~ /^__HASH_REF__/) {
                   5809:           ($value, $string)=&str2hashref($string);
                   5810:           if(defined($value->{'error'})) {
                   5811:               $hash{'error'}='Bad data';
                   5812:               return (\%hash, $string);
                   5813:           }
                   5814:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5815:           ($value, $string)=&str2arrayref($string);
                   5816:           if($value->[0] eq 'Array reference error') {
                   5817:               $hash{'error'}='Bad data';
                   5818:               return (\%hash, $string);
                   5819:           }
                   5820:       } else {
                   5821: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   5822:       }
                   5823:       $string =~ s/^&//;
                   5824: 
                   5825:       $hash{$key}=$value;
1.204     albertel 5826:   }
1.265     albertel 5827: 
                   5828:   $string =~ s/^__END_HASH_REF__//;
                   5829: 
                   5830:   return (\%hash, $string);
1.204     albertel 5831: }
                   5832: 
                   5833: sub str2array {
1.265     albertel 5834:     my ($string)=@_;
                   5835:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   5836:     return @$array;
                   5837: }
                   5838: 
                   5839: sub str2arrayref {
1.204     albertel 5840:   my ($string) = @_;
1.265     albertel 5841:   my @array;
                   5842: 
                   5843:   if($string !~ /^__ARRAY_REF__/) {
                   5844:       if (! ($string eq '' || !defined($string))) {
                   5845: 	  $array[0]='Array reference error';
                   5846:       }
                   5847:       return (\@array, $string);
                   5848:   }
                   5849: 
                   5850:   $string =~ s/^__ARRAY_REF__//;
                   5851: 
                   5852:   while($string !~ /^__END_ARRAY_REF__/) {
                   5853:       my $value='';
                   5854:       if($string =~ /^__HASH_REF__/) {
                   5855:           ($value, $string)=&str2hashref($string);
                   5856:           if(defined($value->{'error'})) {
                   5857:               $array[0] ='Array reference error';
                   5858:               return (\@array, $string);
                   5859:           }
                   5860:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5861:           ($value, $string)=&str2arrayref($string);
                   5862:           if($value->[0] eq 'Array reference error') {
                   5863:               $array[0] ='Array reference error';
                   5864:               return (\@array, $string);
                   5865:           }
                   5866:       } else {
                   5867: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   5868:       }
                   5869:       $string =~ s/^&//;
                   5870: 
                   5871:       push(@array, $value);
1.191     harris41 5872:   }
1.265     albertel 5873: 
                   5874:   $string =~ s/^__END_ARRAY_REF__//;
                   5875: 
                   5876:   return (\@array, $string);
1.168     albertel 5877: }
                   5878: 
1.167     albertel 5879: # -------------------------------------------------------------------Temp Store
                   5880: 
1.168     albertel 5881: sub tmpreset {
                   5882:   my ($symb,$namespace,$domain,$stuname) = @_;
                   5883:   if (!$symb) {
                   5884:     $symb=&symbread();
1.620     albertel 5885:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5886:   }
                   5887:   $symb=escape($symb);
                   5888: 
1.620     albertel 5889:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 5890:   $namespace=~s/\//\_/g;
                   5891:   $namespace=~s/\W//g;
                   5892: 
1.620     albertel 5893:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5894:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5895:   if ($domain eq 'public' && $stuname eq 'public') {
                   5896:       $stuname=$ENV{'REMOTE_ADDR'};
                   5897:   }
1.1117    foxr     5898:   my $path=LONCAPA::tempdir();
1.168     albertel 5899:   my %hash;
                   5900:   if (tie(%hash,'GDBM_File',
                   5901: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5902: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  5903:     foreach my $key (keys(%hash)) {
1.180     albertel 5904:       if ($key=~ /:$symb/) {
1.168     albertel 5905: 	delete($hash{$key});
                   5906:       }
                   5907:     }
                   5908:   }
                   5909: }
                   5910: 
1.167     albertel 5911: sub tmpstore {
1.168     albertel 5912:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   5913: 
                   5914:   if (!$symb) {
                   5915:     $symb=&symbread();
1.620     albertel 5916:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5917:   }
                   5918:   $symb=escape($symb);
                   5919: 
                   5920:   if (!$namespace) {
                   5921:     # I don't think we would ever want to store this for a course.
                   5922:     # it seems this will only be used if we don't have a course.
1.620     albertel 5923:     #$namespace=$env{'request.course.id'};
1.168     albertel 5924:     #if (!$namespace) {
1.620     albertel 5925:       $namespace=$env{'request.state'};
1.168     albertel 5926:     #}
                   5927:   }
                   5928:   $namespace=~s/\//\_/g;
                   5929:   $namespace=~s/\W//g;
1.620     albertel 5930:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5931:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5932:   if ($domain eq 'public' && $stuname eq 'public') {
                   5933:       $stuname=$ENV{'REMOTE_ADDR'};
                   5934:   }
1.168     albertel 5935:   my $now=time;
                   5936:   my %hash;
1.1117    foxr     5937:   my $path=LONCAPA::tempdir();
1.168     albertel 5938:   if (tie(%hash,'GDBM_File',
                   5939: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5940: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 5941:     $hash{"version:$symb"}++;
                   5942:     my $version=$hash{"version:$symb"};
                   5943:     my $allkeys=''; 
                   5944:     foreach my $key (keys(%$storehash)) {
                   5945:       $allkeys.=$key.':';
1.591     albertel 5946:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 5947:     }
                   5948:     $hash{"$version:$symb:timestamp"}=$now;
                   5949:     $allkeys.='timestamp';
                   5950:     $hash{"$version:keys:$symb"}=$allkeys;
                   5951:     if (untie(%hash)) {
                   5952:       return 'ok';
                   5953:     } else {
                   5954:       return "error:$!";
                   5955:     }
                   5956:   } else {
                   5957:     return "error:$!";
                   5958:   }
                   5959: }
1.167     albertel 5960: 
1.168     albertel 5961: # -----------------------------------------------------------------Temp Restore
1.167     albertel 5962: 
1.168     albertel 5963: sub tmprestore {
                   5964:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 5965: 
1.168     albertel 5966:   if (!$symb) {
                   5967:     $symb=&symbread();
1.620     albertel 5968:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5969:   }
                   5970:   $symb=escape($symb);
                   5971: 
1.620     albertel 5972:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 5973: 
1.620     albertel 5974:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5975:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5976:   if ($domain eq 'public' && $stuname eq 'public') {
                   5977:       $stuname=$ENV{'REMOTE_ADDR'};
                   5978:   }
1.168     albertel 5979:   my %returnhash;
                   5980:   $namespace=~s/\//\_/g;
                   5981:   $namespace=~s/\W//g;
                   5982:   my %hash;
1.1117    foxr     5983:   my $path=LONCAPA::tempdir();
1.168     albertel 5984:   if (tie(%hash,'GDBM_File',
                   5985: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5986: 	  &GDBM_READER(),0640)) {
1.168     albertel 5987:     my $version=$hash{"version:$symb"};
                   5988:     $returnhash{'version'}=$version;
                   5989:     my $scope;
                   5990:     for ($scope=1;$scope<=$version;$scope++) {
                   5991:       my $vkeys=$hash{"$scope:keys:$symb"};
                   5992:       my @keys=split(/:/,$vkeys);
                   5993:       my $key;
                   5994:       $returnhash{"$scope:keys"}=$vkeys;
                   5995:       foreach $key (@keys) {
1.591     albertel 5996: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   5997: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 5998:       }
                   5999:     }
1.168     albertel 6000:     if (!(untie(%hash))) {
                   6001:       return "error:$!";
                   6002:     }
                   6003:   } else {
                   6004:     return "error:$!";
                   6005:   }
                   6006:   return %returnhash;
1.167     albertel 6007: }
                   6008: 
1.9       www      6009: # ----------------------------------------------------------------------- Store
                   6010: 
                   6011: sub store {
1.1269    raeburn  6012:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6013:     my $home='';
                   6014: 
1.168     albertel 6015:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6016: 
1.213     www      6017:     $symb=&symbclean($symb);
1.122     albertel 6018:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6019: 
1.620     albertel 6020:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6021:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6022: 
                   6023:     &devalidate($symb,$stuname,$domain);
1.109     www      6024: 
                   6025:     $symb=escape($symb);
1.187     www      6026:     if (!$namespace) { 
1.620     albertel 6027:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6028:           return ''; 
                   6029:        } 
                   6030:     }
1.620     albertel 6031:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6032: 
                   6033:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   6034:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   6035: 
1.12      www      6036:     my $namevalue='';
1.800     albertel 6037:     foreach my $key (keys(%$storehash)) {
                   6038:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6039:     }
1.12      www      6040:     $namevalue=~s/\&$//;
1.187     www      6041:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269    raeburn  6042:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9       www      6043: }
                   6044: 
1.47      www      6045: # -------------------------------------------------------------- Critical Store
                   6046: 
                   6047: sub cstore {
1.1269    raeburn  6048:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      6049:     my $home='';
                   6050: 
1.168     albertel 6051:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6052: 
1.213     www      6053:     $symb=&symbclean($symb);
1.122     albertel 6054:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      6055: 
1.620     albertel 6056:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6057:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      6058: 
                   6059:     &devalidate($symb,$stuname,$domain);
1.109     www      6060: 
                   6061:     $symb=escape($symb);
1.187     www      6062:     if (!$namespace) { 
1.620     albertel 6063:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      6064:           return ''; 
                   6065:        } 
                   6066:     }
1.620     albertel 6067:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      6068: 
                   6069:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   6070:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 6071: 
1.47      www      6072:     my $namevalue='';
1.800     albertel 6073:     foreach my $key (keys(%$storehash)) {
                   6074:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 6075:     }
1.47      www      6076:     $namevalue=~s/\&$//;
1.187     www      6077:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      6078:     return critical
1.1269    raeburn  6079:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47      www      6080: }
                   6081: 
1.9       www      6082: # --------------------------------------------------------------------- Restore
                   6083: 
                   6084: sub restore {
1.124     www      6085:     my ($symb,$namespace,$domain,$stuname) = @_;
                   6086:     my $home='';
                   6087: 
1.168     albertel 6088:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      6089: 
1.122     albertel 6090:     if (!$symb) {
1.1224    raeburn  6091:         return if ($namespace eq 'courserequests');
                   6092:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 6093:     } else {
1.1224    raeburn  6094:         unless ($namespace eq 'courserequests') {
                   6095:             $symb=&escape(&symbclean($symb));
                   6096:         }
1.122     albertel 6097:     }
1.188     www      6098:     if (!$namespace) { 
1.620     albertel 6099:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      6100:           return ''; 
                   6101:        } 
                   6102:     }
1.620     albertel 6103:     if (!$domain) { $domain=$env{'user.domain'}; }
                   6104:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   6105:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 6106:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   6107: 
1.12      www      6108:     my %returnhash=();
1.800     albertel 6109:     foreach my $line (split(/\&/,$answer)) {
                   6110: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 6111:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 6112:     }
1.75      www      6113:     my $version;
                   6114:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 6115:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   6116:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 6117:        }
1.75      www      6118:     }
1.13      www      6119:     return %returnhash;
1.34      www      6120: }
                   6121: 
                   6122: # ---------------------------------------------------------- Course Description
1.1118    foxr     6123: #
                   6124: #  
1.34      www      6125: 
                   6126: sub coursedescription {
1.731     albertel 6127:     my ($courseid,$args)=@_;
1.34      www      6128:     $courseid=~s/^\///;
1.49      www      6129:     $courseid=~s/\_/\//g;
1.34      www      6130:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 6131:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 6132:     my $normalid=$cdomain.'_'.$cnum;
                   6133:     # need to always cache even if we get errors otherwise we keep 
                   6134:     # trying and trying and trying to get the course description.
                   6135:     my %envhash=();
                   6136:     my %returnhash=();
1.731     albertel 6137:     
                   6138:     my $expiretime=600;
                   6139:     if ($env{'request.course.id'} eq $normalid) {
                   6140: 	$expiretime=120;
                   6141:     }
                   6142: 
                   6143:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   6144:     if (!$args->{'freshen_cache'}
                   6145: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   6146: 	foreach my $key (keys(%env)) {
                   6147: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   6148: 	    my ($setting) = $1;
                   6149: 	    $returnhash{$setting} = $env{$key};
                   6150: 	}
                   6151: 	return %returnhash;
                   6152:     }
                   6153: 
1.1118    foxr     6154:     # get the data again
                   6155: 
1.731     albertel 6156:     if (!$args->{'one_time'}) {
                   6157: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   6158:     }
1.811     albertel 6159: 
1.34      www      6160:     if ($chome ne 'no_host') {
1.302     albertel 6161:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 6162:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     6163: 	   my $username = $env{'user.name'}; # Defult username
                   6164: 	   if(defined $args->{'user'}) {
                   6165: 	       $username = $args->{'user'};
                   6166: 	   }
1.129     albertel 6167:            $returnhash{'home'}= $chome;
                   6168: 	   $returnhash{'domain'} = $cdomain;
                   6169: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  6170:            if (!defined($returnhash{'type'})) {
                   6171:                $returnhash{'type'} = 'Course';
                   6172:            }
1.130     albertel 6173:            while (my ($name,$value) = each %returnhash) {
1.53      www      6174:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 6175:            }
1.270     www      6176:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     6177:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     6178: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      6179:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   6180:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   6181:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      6182:        }
                   6183:     }
1.731     albertel 6184:     if (!$args->{'one_time'}) {
1.949     raeburn  6185: 	&appenv(\%envhash);
1.731     albertel 6186:     }
1.302     albertel 6187:     return %returnhash;
1.461     www      6188: }
                   6189: 
1.1080    raeburn  6190: sub update_released_required {
                   6191:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   6192:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   6193:         $cid = $env{'request.course.id'};
                   6194:         $cdom = $env{'course.'.$cid.'.domain'};
                   6195:         $cnum = $env{'course.'.$cid.'.num'};
                   6196:         $chome = $env{'course.'.$cid.'.home'};
                   6197:     }
                   6198:     if ($needsrelease) {
                   6199:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   6200:         my $needsupdate;
                   6201:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   6202:             $needsupdate = 1;
                   6203:         } else {
                   6204:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   6205:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   6206:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   6207:                 $needsupdate = 1;
                   6208:             }
                   6209:         }
                   6210:         if ($needsupdate) {
                   6211:             my %needshash = (
                   6212:                              'internal.releaserequired' => $needsrelease,
                   6213:                             );
                   6214:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   6215:             if ($putresult eq 'ok') {
                   6216:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   6217:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6218:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   6219:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   6220:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   6221:                 }
                   6222:             }
                   6223:         }
                   6224:     }
                   6225:     return;
                   6226: }
                   6227: 
1.461     www      6228: # -------------------------------------------------See if a user is privileged
                   6229: 
                   6230: sub privileged {
1.1219    raeburn  6231:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 6232:     my $now = time;
1.1219    raeburn  6233:     my $roles;
                   6234:     if (ref($possroles) eq 'ARRAY') {
                   6235:         $roles = $possroles; 
                   6236:     } else {
                   6237:         $roles = ['dc','su'];
                   6238:     }
                   6239:     if (ref($possdomains) eq 'ARRAY') {
                   6240:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   6241:         foreach my $dom (@{$possdomains}) {
                   6242:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   6243:                 (ref($privileged{$dom}) eq 'HASH')) {
                   6244:                 foreach my $role (@{$roles}) {
                   6245:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6246:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   6247:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   6248:                             return 1 unless (($end && $end < $now) ||
                   6249:                                              ($start && $start > $now));
                   6250:                         }
                   6251:                     }
                   6252:                 }
                   6253:             }
                   6254:         }
                   6255:     } else {
                   6256:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   6257:         my $now = time;
1.1170    droeschl 6258: 
1.1275    musolffc 6259:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170    droeschl 6260:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  6261:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 6262:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  6263:                         or ($tstart && $tstart > $now);
1.1170    droeschl 6264:             }
1.1219    raeburn  6265:         }
                   6266:     }
                   6267:     return 0;
                   6268: }
1.1170    droeschl 6269: 
1.1219    raeburn  6270: sub privileged_by_domain {
                   6271:     my ($domains,$roles) = @_;
                   6272:     my %privileged = ();
                   6273:     my $cachetime = 60*60*24;
                   6274:     my $now = time;
                   6275:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   6276:         return %privileged;
                   6277:     }
                   6278:     foreach my $dom (@{$domains}) {
                   6279:         next if (ref($privileged{$dom}) eq 'HASH');
                   6280:         my $needroles;
                   6281:         foreach my $role (@{$roles}) {
                   6282:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   6283:             if (defined($cached)) {
                   6284:                 if (ref($result) eq 'HASH') {
                   6285:                     $privileged{$dom}{$role} = $result;
                   6286:                 }
                   6287:             } else {
                   6288:                 $needroles = 1;
                   6289:             }
                   6290:         }
                   6291:         if ($needroles) {
                   6292:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   6293:             $privileged{$dom} = {};
                   6294:             foreach my $server (keys(%dompersonnel)) {
                   6295:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   6296:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   6297:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   6298:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   6299:                         next if ($end && $end < $now);
                   6300:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   6301:                             $dompersonnel{$server}{$item};
                   6302:                     }
                   6303:                 }
                   6304:             }
                   6305:             if (ref($privileged{$dom}) eq 'HASH') {
                   6306:                 foreach my $role (@{$roles}) {
                   6307:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   6308:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   6309:                     } else {
                   6310:                         my %hash = ();
                   6311:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   6312:                     }
                   6313:                 }
                   6314:             }
                   6315:         }
                   6316:     }
                   6317:     return %privileged;
1.9       www      6318: }
1.1       albertel 6319: 
1.103     harris41 6320: # -------------------------------------------------------- Get user privileges
1.11      www      6321: 
                   6322: sub rolesinit {
1.1169    droeschl 6323:     my ($domain, $username) = @_;
                   6324:     my %userroles = ('user.login.time' => time);
                   6325:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   6326: 
                   6327:     # firstaccess and timerinterval are related to timed maps/resources. 
                   6328:     # also, blocking can be triggered by an activating timer
                   6329:     # it's saved in the user's %env.
                   6330:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   6331:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   6332:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
                   6333:         %timerintchk, %timerintenv);
                   6334: 
1.1162    raeburn  6335:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 6336:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  6337:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   6338:     }
1.1169    droeschl 6339: 
1.1162    raeburn  6340:     foreach my $key (keys(%timerinterval)) {
                   6341:         my ($cid,$rest) = split(/\0/,$key);
                   6342:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   6343:     }
1.1169    droeschl 6344: 
1.11      www      6345:     my %allroles=();
1.1162    raeburn  6346:     my %allgroups=();
1.11      www      6347: 
1.1274    raeburn  6348:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169    droeschl 6349:         my $role = $rolesdump{$area};
                   6350:         $area =~ s/\_\w\w$//;
                   6351: 
                   6352:         my ($trole, $tend, $tstart, $group_privs);
                   6353: 
                   6354:         if ($role =~ /^cr/) {
                   6355:         # Custom role, defined by a user 
                   6356:         # e.g., user.role.cr/msu/smith/mynewrole
                   6357:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   6358:                 $trole = $1;
                   6359:                 ($tend, $tstart) = split('_', $2);
                   6360:             } else {
                   6361:                 $trole = $role;
                   6362:             }
                   6363:         } elsif ($role =~ m|^gr/|) {
                   6364:         # Role of member in a group, defined within a course/community
                   6365:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   6366:             ($trole, $tend, $tstart) = split(/_/, $role);
                   6367:             next if $tstart eq '-1';
                   6368:             ($trole, $group_privs) = split(/\//, $trole);
                   6369:             $group_privs = &unescape($group_privs);
                   6370:         } else {
                   6371:         # Just a normal role, defined in roles.tab
                   6372:             ($trole, $tend, $tstart) = split(/_/,$role);
                   6373:         }
                   6374: 
                   6375:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   6376:                  $username);
                   6377:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   6378: 
                   6379:         # role expired or not available yet?
                   6380:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   6381:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   6382: 
                   6383:         next if $area eq '' or $trole eq '';
                   6384: 
                   6385:         my $spec = "$trole.$area";
                   6386:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   6387: 
                   6388:         if ($trole =~ /^cr\//) {
                   6389:         # Custom role, defined by a user
                   6390:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   6391:         } elsif ($trole eq 'gr') {
                   6392:         # Role of a member in a group, defined within a course/community
                   6393:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   6394:             next;
                   6395:         } else {
                   6396:         # Normal role, defined in roles.tab
                   6397:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   6398:         }
                   6399: 
                   6400:         my $cid = $tdomain.'_'.$trest;
                   6401:         unless ($firstaccchk{$cid}) {
                   6402:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   6403:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   6404:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   6405:                         $coursetimerstarts{$cid}{$item}; 
                   6406:                 }
                   6407:             }
                   6408:             $firstaccchk{$cid} = 1;
                   6409:         }
                   6410:         unless ($timerintchk{$cid}) {
                   6411:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   6412:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   6413:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   6414:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  6415:                 }
1.12      www      6416:             }
1.1169    droeschl 6417:             $timerintchk{$cid} = 1;
1.191     harris41 6418:         }
1.11      www      6419:     }
1.1169    droeschl 6420: 
1.1341    raeburn  6421:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
                   6422:                                                           \%allroles, \%allgroups);
1.1169    droeschl 6423:     $env{'user.adv'} = $userroles{'user.adv'};
1.1341    raeburn  6424:     $env{'user.rar'} = $userroles{'user.rar'};
1.1169    droeschl 6425: 
1.1162    raeburn  6426:     return (\%userroles,\%firstaccenv,\%timerintenv);
1.11      www      6427: }
                   6428: 
1.567     raeburn  6429: sub set_arearole {
1.1215    raeburn  6430:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   6431:     unless ($nolog) {
1.567     raeburn  6432: # log the associated role with the area
1.1215    raeburn  6433:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   6434:     }
1.743     albertel 6435:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  6436: }
                   6437: 
                   6438: sub custom_roleprivs {
                   6439:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   6440:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250    raeburn  6441:     my $homsvr = &homeserver($rauthor,$rdomain);
1.838     albertel 6442:     if (&hostname($homsvr) ne '') {
1.567     raeburn  6443:         my ($rdummy,$roledef)=
                   6444:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   6445:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   6446:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   6447:             if (defined($syspriv)) {
1.1043    raeburn  6448:                 if ($trest =~ /^$match_community$/) {
                   6449:                     $syspriv =~ s/bre\&S//; 
                   6450:                 }
1.567     raeburn  6451:                 $$allroles{'cm./'}.=':'.$syspriv;
                   6452:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   6453:             }
                   6454:             if ($tdomain ne '') {
                   6455:                 if (defined($dompriv)) {
                   6456:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   6457:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   6458:                 }
                   6459:                 if (($trest ne '') && (defined($coursepriv))) {
1.1332    raeburn  6460:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
                   6461:                         my $rolename = $1;
                   6462:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
                   6463:                     }
1.567     raeburn  6464:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   6465:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   6466:                 }
                   6467:             }
                   6468:         }
                   6469:     }
                   6470: }
                   6471: 
1.1332    raeburn  6472: sub course_adhocrole_privs {
                   6473:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
                   6474:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
                   6475:     if ($overrides{"internal.adhocpriv.$rolename"}) {
                   6476:         my (%currprivs,%storeprivs);
                   6477:         foreach my $item (split(/:/,$coursepriv)) {
                   6478:             my ($priv,$restrict) = split(/\&/,$item);
                   6479:             $currprivs{$priv} = $restrict;
                   6480:         }
                   6481:         my (%possadd,%possremove,%full);
                   6482:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   6483:             my ($priv,$restrict)=split(/\&/,$item);
                   6484:             $full{$priv} = $restrict;
                   6485:         }
                   6486:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
                   6487:              next if ($item eq '');
                   6488:              my ($rule,$rest) = split(/=/,$item);
                   6489:              next unless (($rule eq 'off') || ($rule eq 'on'));
                   6490:              foreach my $priv (split(/:/,$rest)) {
                   6491:                  if ($priv ne '') {
                   6492:                      if ($rule eq 'off') {
                   6493:                          $possremove{$priv} = 1;
                   6494:                      } else {
                   6495:                          $possadd{$priv} = 1;
                   6496:                      }
                   6497:                  }
                   6498:              }
                   6499:          }
                   6500:          foreach my $priv (sort(keys(%full))) {
                   6501:              if (exists($currprivs{$priv})) {
                   6502:                  unless (exists($possremove{$priv})) {
                   6503:                      $storeprivs{$priv} = $currprivs{$priv};
                   6504:                  }
                   6505:              } elsif (exists($possadd{$priv})) {
                   6506:                  $storeprivs{$priv} = $full{$priv};
                   6507:              }
                   6508:          }
                   6509:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
                   6510:      }
                   6511:      return $coursepriv;
                   6512: }
                   6513: 
1.678     raeburn  6514: sub group_roleprivs {
                   6515:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   6516:     my $access = 1;
                   6517:     my $now = time;
                   6518:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   6519:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   6520:     if ($access) {
1.811     albertel 6521:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  6522:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   6523:     }
                   6524: }
1.567     raeburn  6525: 
                   6526: sub standard_roleprivs {
                   6527:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   6528:     if (defined($pr{$trole.':s'})) {
                   6529:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   6530:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   6531:     }
                   6532:     if ($tdomain ne '') {
                   6533:         if (defined($pr{$trole.':d'})) {
                   6534:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   6535:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   6536:         }
                   6537:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   6538:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   6539:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   6540:         }
                   6541:     }
                   6542: }
                   6543: 
                   6544: sub set_userprivs {
1.1064    raeburn  6545:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  6546:     my $author=0;
                   6547:     my $adv=0;
1.1341    raeburn  6548:     my $rar=0;
1.678     raeburn  6549:     my %grouproles = ();
                   6550:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  6551:         my @groupkeys; 
1.1000    raeburn  6552:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  6553:             push(@groupkeys,$role);
                   6554:         }
                   6555:         if (ref($groups_roles) eq 'HASH') {
                   6556:             foreach my $key (keys(%{$groups_roles})) {
                   6557:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   6558:                     push(@groupkeys,$key);
                   6559:                 }
                   6560:             }
                   6561:         }
                   6562:         if (@groupkeys > 0) {
                   6563:             foreach my $role (@groupkeys) {
                   6564:                 my ($trole,$area,$sec,$extendedarea);
                   6565:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   6566:                     $trole = $1;
                   6567:                     $area = $2;
                   6568:                     $sec = $3;
                   6569:                     $extendedarea = $area.$sec;
                   6570:                     if (exists($$allgroups{$area})) {
                   6571:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   6572:                             my $spec = $trole.'.'.$extendedarea;
                   6573:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  6574:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  6575:                         }
1.678     raeburn  6576:                     }
                   6577:                 }
                   6578:             }
                   6579:         }
                   6580:     }
1.800     albertel 6581:     foreach my $group (keys(%grouproles)) {
                   6582:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  6583:     }
1.800     albertel 6584:     foreach my $role (keys(%{$allroles})) {
                   6585:         my %thesepriv;
1.941     raeburn  6586:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 6587:         foreach my $item (split(/:/,$$allroles{$role})) {
                   6588:             if ($item ne '') {
                   6589:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  6590:                 if ($restrictions eq '') {
                   6591:                     $thesepriv{$privilege}='F';
                   6592:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   6593:                     $thesepriv{$privilege}.=$restrictions;
                   6594:                 }
                   6595:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341    raeburn  6596:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567     raeburn  6597:             }
                   6598:         }
                   6599:         my $thesestr='';
1.1104    raeburn  6600:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 6601: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   6602: 	}
                   6603:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  6604:     }
1.1341    raeburn  6605:     return ($author,$adv,$rar);
1.567     raeburn  6606: }
                   6607: 
1.994     raeburn  6608: sub role_status {
1.1104    raeburn  6609:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  6610:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250    raeburn  6611:         my ($one,$two) = split(m{\./},$rolekey,2);
                   6612:         (undef,undef,$$role) = split(/\./,$one,3);
1.994     raeburn  6613:         unless (!defined($$role) || $$role eq '') {
1.1251    raeburn  6614:             $$where = '/'.$two;
1.994     raeburn  6615:             $$trolecode=$$role.'.'.$$where;
                   6616:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   6617:             $$tstatus='is';
1.1104    raeburn  6618:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  6619:                 $$tstatus='future';
1.1034    raeburn  6620:                 if ($$tstart<$now) {
                   6621:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  6622:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  6623:                             my (%allroles,%allgroups,$group_privs,
                   6624:                                 %groups_roles,@rolecodes);
1.1002    raeburn  6625:                             my %userroles = (
                   6626:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   6627:                             );
1.1064    raeburn  6628:                             @rolecodes = ('cm'); 
1.1002    raeburn  6629:                             my $spec=$$role.'.'.$$where;
                   6630:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   6631:                             if ($$role =~ /^cr\//) {
                   6632:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  6633:                                 push(@rolecodes,'cr');
1.1002    raeburn  6634:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  6635:                                 push(@rolecodes,$$role);
1.1002    raeburn  6636:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   6637:                                                     $env{'user.name'});
1.1064    raeburn  6638:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  6639:                                 (undef,my $group_privs) = split(/\//,$trole);
                   6640:                                 $group_privs = &unescape($group_privs);
                   6641:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  6642:                                 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  6643:                                 &get_groups_roles($tdomain,$trest,
                   6644:                                                   \%course_roles,\@rolecodes,
                   6645:                                                   \%groups_roles);
1.1002    raeburn  6646:                             } else {
1.1064    raeburn  6647:                                 push(@rolecodes,$$role);
1.1002    raeburn  6648:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   6649:                             }
1.1341    raeburn  6650:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
                   6651:                                                                    \%groups_roles);
1.1064    raeburn  6652:                             &appenv(\%userroles,\@rolecodes);
1.1342    raeburn  6653:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002    raeburn  6654:                         }
                   6655:                     }
1.1034    raeburn  6656:                     $$tstatus = 'is';
1.1002    raeburn  6657:                 }
1.994     raeburn  6658:             }
                   6659:             if ($$tend) {
1.1104    raeburn  6660:                 if ($$tend<$update) {
1.994     raeburn  6661:                     $$tstatus='expired';
                   6662:                 } elsif ($$tend<$now) {
                   6663:                     $$tstatus='will_not';
                   6664:                 }
                   6665:             }
                   6666:         }
                   6667:     }
                   6668: }
                   6669: 
1.1104    raeburn  6670: sub get_groups_roles {
                   6671:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   6672:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   6673:                   (ref($rolecodes) eq 'ARRAY') && 
                   6674:                   (ref($groups_roles) eq 'HASH')); 
                   6675:     if (keys(%{$cdom_courseroles}) > 0) {
                   6676:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   6677:         if ($cdom ne '' && $cnum ne '') {
                   6678:             foreach my $key (keys(%{$cdom_courseroles})) {
                   6679:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   6680:                     my $crsrole = $1;
                   6681:                     my $crssec = $2;
                   6682:                     if ($crsrole =~ /^cr/) {
                   6683:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   6684:                             push(@{$rolecodes},'cr');
                   6685:                         }
                   6686:                     } else {
                   6687:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   6688:                             push(@{$rolecodes},$crsrole);
                   6689:                         }
                   6690:                     }
                   6691:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   6692:                     if ($crssec ne '') {
                   6693:                         $rolekey .= "/$crssec";
                   6694:                     }
                   6695:                     $rolekey .= './';
                   6696:                     $groups_roles->{$rolekey} = $rolecodes;
                   6697:                 }
                   6698:             }
                   6699:         }
                   6700:     }
                   6701:     return;
                   6702: }
                   6703: 
                   6704: sub delete_env_groupprivs {
                   6705:     my ($where,$courseroles,$possroles) = @_;
                   6706:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   6707:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   6708:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   6709:         %{$courseroles->{$udom}} =
                   6710:             &get_my_roles('','','userroles',['active'],
                   6711:                           $possroles,[$udom],1);
                   6712:     }
                   6713:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   6714:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   6715:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   6716:             my $area = '/'.$cdom.'/'.$cnum;
                   6717:             my $privkey = "user.priv.$crsrole.$area";
                   6718:             if ($crssec ne '') {
                   6719:                 $privkey .= '/'.$crssec;
                   6720:             }
                   6721:             $privkey .= ".$area/$group";
                   6722:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   6723:         }
                   6724:     }
                   6725:     return;
                   6726: }
                   6727: 
1.994     raeburn  6728: sub check_adhoc_privs {
1.1329    raeburn  6729:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994     raeburn  6730:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329    raeburn  6731:     if ($sec) {
                   6732:         $cckey .= '/'.$sec;
                   6733:     } 
1.1185    raeburn  6734:     my $setprivs;
1.994     raeburn  6735:     if ($env{$cckey}) {
                   6736:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  6737:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  6738:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329    raeburn  6739:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  6740:             $setprivs = 1;
1.994     raeburn  6741:         }
                   6742:     } else {
1.1330    raeburn  6743:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  6744:         $setprivs = 1;
1.994     raeburn  6745:     }
1.1185    raeburn  6746:     return $setprivs;
1.994     raeburn  6747: }
                   6748: 
                   6749: sub set_adhoc_privileges {
1.1326    raeburn  6750: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329    raeburn  6751:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994     raeburn  6752:     my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329    raeburn  6753:     if ($sec ne '') {
                   6754:         $area .= '/'.$sec;
                   6755:     }
1.994     raeburn  6756:     my $spec = $role.'.'.$area;
                   6757:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  6758:                                   $env{'user.name'},1);
1.1326    raeburn  6759:     my %rolehash = ();
1.1332    raeburn  6760:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
                   6761:         my $rolename = $1;
1.1326    raeburn  6762:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332    raeburn  6763:         my %domdef = &get_domain_defaults($dcdom);
                   6764:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   6765:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   6766:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
                   6767:             }
                   6768:         }
1.1326    raeburn  6769:     } else {
                   6770:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
                   6771:     }
1.1341    raeburn  6772:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994     raeburn  6773:     &appenv(\%userroles,[$role,'cm']);
1.1342    raeburn  6774:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1402    raeburn  6775:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
                   6776:             ($caller eq 'tiny')) {
1.1088    raeburn  6777:         &appenv( {'request.role'        => $spec,
                   6778:                   'request.role.domain' => $dcdom,
1.1332    raeburn  6779:                   'request.course.sec'  => $sec,
1.1088    raeburn  6780:                  }
                   6781:                );
                   6782:         my $tadv=0;
                   6783:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   6784:         &appenv({'request.role.adv'    => $tadv});
                   6785:     }
1.994     raeburn  6786: }
                   6787: 
1.12      www      6788: # --------------------------------------------------------------- get interface
                   6789: 
                   6790: sub get {
1.131     albertel 6791:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      6792:    my $items='';
1.800     albertel 6793:    foreach my $item (@$storearr) {
                   6794:        $items.=&escape($item).'&';
1.191     harris41 6795:    }
1.12      www      6796:    $items=~s/\&$//;
1.620     albertel 6797:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6798:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 6799:    my $uhome=&homeserver($uname,$udomain);
                   6800: 
1.133     albertel 6801:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      6802:    my @pairs=split(/\&/,$rep);
1.273     albertel 6803:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   6804:      return @pairs;
                   6805:    }
1.15      www      6806:    my %returnhash=();
1.42      www      6807:    my $i=0;
1.800     albertel 6808:    foreach my $item (@$storearr) {
                   6809:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      6810:       $i++;
1.191     harris41 6811:    }
1.15      www      6812:    return %returnhash;
1.27      www      6813: }
                   6814: 
                   6815: # --------------------------------------------------------------- del interface
                   6816: 
                   6817: sub del {
1.133     albertel 6818:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      6819:    my $items='';
1.800     albertel 6820:    foreach my $item (@$storearr) {
                   6821:        $items.=&escape($item).'&';
1.191     harris41 6822:    }
1.984     neumanie 6823: 
1.27      www      6824:    $items=~s/\&$//;
1.620     albertel 6825:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6826:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 6827:    my $uhome=&homeserver($uname,$udomain);
                   6828:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      6829: }
                   6830: 
                   6831: # -------------------------------------------------------------- dump interface
                   6832: 
1.1180    droeschl 6833: sub unserialize {
                   6834:     my ($rep, $escapedkeys) = @_;
                   6835: 
                   6836:     return {} if $rep =~ /^error/;
                   6837: 
                   6838:     my %returnhash=();
1.1252    raeburn  6839: 	foreach my $item (split(/\&/,$rep)) {
1.1180    droeschl 6840: 	    my ($key, $value) = split(/=/, $item, 2);
                   6841: 	    $key = unescape($key) unless $escapedkeys;
                   6842: 	    next if $key =~ /^error: 2 /;
1.1252    raeburn  6843: 	    $returnhash{$key} = &thaw_unescape($value);
1.1180    droeschl 6844: 	}
                   6845:     #return %returnhash;
                   6846:     return \%returnhash;
                   6847: }        
                   6848: 
                   6849: # see Lond::dump_with_regexp
                   6850: # if $escapedkeys hash keys won't get unescaped.
1.15      www      6851: sub dump {
1.1180    droeschl 6852:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755     albertel 6853:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6854:     if (!$uname) { $uname=$env{'user.name'}; }
                   6855:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 6856: 
1.1266    raeburn  6857:     if ($regexp) {
                   6858:         $regexp=&escape($regexp);
                   6859:     } else {
                   6860:         $regexp='.';
                   6861:     }
1.1180    droeschl 6862:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   6863:         # user is hosted on this machine
1.1266    raeburn  6864:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  6865:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 6866:         return %{unserialize($reply, $escapedkeys)};
                   6867:     }
1.1166    raeburn  6868:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755     albertel 6869:     my @pairs=split(/\&/,$rep);
                   6870:     my %returnhash=();
1.1098    foxr     6871:     if (!($rep =~ /^error/ )) {
                   6872: 	foreach my $item (@pairs) {
                   6873: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 6874:         $key = unescape($key) unless $escapedkeys;
                   6875:         #$key = &unescape($key);
1.1098    foxr     6876: 	    next if ($key =~ /^error: 2 /);
                   6877: 	    $returnhash{$key}=&thaw_unescape($value);
                   6878: 	}
1.755     albertel 6879:     }
                   6880:     return %returnhash;
1.407     www      6881: }
                   6882: 
1.1098    foxr     6883: 
1.717     albertel 6884: # --------------------------------------------------------- dumpstore interface
                   6885: 
                   6886: sub dumpstore {
                   6887:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 6888:    # same as dump but keys must be escaped. They may contain colon separated
                   6889:    # lists of values that may themself contain colons (e.g. symbs).
                   6890:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 6891: }
                   6892: 
1.407     www      6893: # -------------------------------------------------------------- keys interface
                   6894: 
                   6895: sub getkeys {
                   6896:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 6897:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6898:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      6899:    my $uhome=&homeserver($uname,$udomain);
                   6900:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   6901:    my @keyarray=();
1.800     albertel 6902:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  6903:       next if ($key =~ /^error: 2 /);
1.800     albertel 6904:       push(@keyarray,&unescape($key));
1.407     www      6905:    }
                   6906:    return @keyarray;
1.318     matthew  6907: }
                   6908: 
1.319     matthew  6909: # --------------------------------------------------------------- currentdump
                   6910: sub currentdump {
1.328     matthew  6911:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 6912:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   6913:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   6914:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  6915:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 6916:    my $rep;
                   6917: 
                   6918:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   6919:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   6920:                    $courseid)));
                   6921:    } else {
                   6922:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   6923:    }
                   6924: 
1.318     matthew  6925:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  6926:    #
1.318     matthew  6927:    my %returnhash=();
1.319     matthew  6928:    #
1.1343    raeburn  6929:    if ($rep eq 'unknown_cmd') {
1.319     matthew  6930:        # an old lond will not know currentdump
                   6931:        # Do a dump and make it look like a currentdump
1.822     albertel 6932:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  6933:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   6934:        my %hash = @tmp;
                   6935:        @tmp=();
1.424     matthew  6936:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  6937:    } else {
                   6938:        my @pairs=split(/\&/,$rep);
1.800     albertel 6939:        foreach my $pair (@pairs) {
                   6940:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  6941:            my ($symb,$param) = split(/:/,$key);
                   6942:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 6943:                                                         &thaw_unescape($value);
1.319     matthew  6944:        }
1.191     harris41 6945:    }
1.12      www      6946:    return %returnhash;
1.424     matthew  6947: }
                   6948: 
                   6949: sub convert_dump_to_currentdump{
                   6950:     my %hash = %{shift()};
                   6951:     my %returnhash;
                   6952:     # Code ripped from lond, essentially.  The only difference
                   6953:     # here is the unescaping done by lonnet::dump().  Conceivably
                   6954:     # we might run in to problems with parameter names =~ /^v\./
                   6955:     while (my ($key,$value) = each(%hash)) {
                   6956:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 6957: 	$symb  = &unescape($symb);
                   6958: 	$param = &unescape($param);
1.424     matthew  6959:         next if ($v eq 'version' || $symb eq 'keys');
                   6960:         next if (exists($returnhash{$symb}) &&
                   6961:                  exists($returnhash{$symb}->{$param}) &&
                   6962:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   6963:         $returnhash{$symb}->{$param}=$value;
                   6964:         $returnhash{$symb}->{'v.'.$param}=$v;
                   6965:     }
                   6966:     #
                   6967:     # Remove all of the keys in the hashes which keep track of
                   6968:     # the version of the parameter.
                   6969:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   6970:         # use a foreach because we are going to delete from the hash.
                   6971:         foreach my $key (keys(%$param_hash)) {
                   6972:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   6973:         }
                   6974:     }
                   6975:     return \%returnhash;
1.12      www      6976: }
                   6977: 
1.627     albertel 6978: # ------------------------------------------------------ critical inc interface
                   6979: 
                   6980: sub cinc {
                   6981:     return &inc(@_,'critical');
                   6982: }
                   6983: 
1.449     matthew  6984: # --------------------------------------------------------------- inc interface
                   6985: 
                   6986: sub inc {
1.627     albertel 6987:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 6988:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6989:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  6990:     my $uhome=&homeserver($uname,$udomain);
                   6991:     my $items='';
                   6992:     if (! ref($store)) {
                   6993:         # got a single value, so use that instead
                   6994:         $items = &escape($store).'=&';
                   6995:     } elsif (ref($store) eq 'SCALAR') {
                   6996:         $items = &escape($$store).'=&';        
                   6997:     } elsif (ref($store) eq 'ARRAY') {
                   6998:         $items = join('=&',map {&escape($_);} @{$store});
                   6999:     } elsif (ref($store) eq 'HASH') {
                   7000:         while (my($key,$value) = each(%{$store})) {
                   7001:             $items.= &escape($key).'='.&escape($value).'&';
                   7002:         }
                   7003:     }
                   7004:     $items=~s/\&$//;
1.627     albertel 7005:     if ($critical) {
                   7006: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7007:     } else {
                   7008: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   7009:     }
1.449     matthew  7010: }
                   7011: 
1.12      www      7012: # --------------------------------------------------------------- put interface
                   7013: 
                   7014: sub put {
1.134     albertel 7015:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7016:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7017:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7018:    my $uhome=&homeserver($uname,$udomain);
1.12      www      7019:    my $items='';
1.800     albertel 7020:    foreach my $item (keys(%$storehash)) {
                   7021:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7022:    }
1.12      www      7023:    $items=~s/\&$//;
1.134     albertel 7024:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      7025: }
                   7026: 
1.631     albertel 7027: # ------------------------------------------------------------ newput interface
                   7028: 
                   7029: sub newput {
                   7030:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   7031:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7032:    if (!$uname) { $uname=$env{'user.name'}; }
                   7033:    my $uhome=&homeserver($uname,$udomain);
                   7034:    my $items='';
                   7035:    foreach my $key (keys(%$storehash)) {
                   7036:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7037:    }
                   7038:    $items=~s/\&$//;
                   7039:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   7040: }
                   7041: 
                   7042: # ---------------------------------------------------------  putstore interface
                   7043: 
1.524     raeburn  7044: sub putstore {
1.1269    raeburn  7045:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620     albertel 7046:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7047:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  7048:    my $uhome=&homeserver($uname,$udomain);
                   7049:    my $items='';
1.715     albertel 7050:    foreach my $key (keys(%$storehash)) {
                   7051:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  7052:    }
1.715     albertel 7053:    $items=~s/\&$//;
1.716     albertel 7054:    my $esc_symb=&escape($symb);
                   7055:    my $esc_v=&escape($version);
1.715     albertel 7056:    my $reply =
1.716     albertel 7057:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 7058: 	      $uhome);
1.1269    raeburn  7059:    if (($tolog) && ($reply eq 'ok')) {
                   7060:        my $namevalue='';
                   7061:        foreach my $key (keys(%{$storehash})) {
                   7062:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
                   7063:        }
                   7064:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
                   7065:                      '&host='.&escape($perlvar{'lonHostID'}).
                   7066:                      '&version='.$esc_v.
                   7067:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
                   7068:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
                   7069:    }
1.715     albertel 7070:    if ($reply eq 'unknown_cmd') {
1.716     albertel 7071:        # gfall back to way things use to be done
1.715     albertel 7072:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   7073: 			    $uname);
1.524     raeburn  7074:    }
1.715     albertel 7075:    return $reply;
                   7076: }
                   7077: 
                   7078: sub old_putstore {
1.716     albertel 7079:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   7080:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7081:     if (!$uname) { $uname=$env{'user.name'}; }
                   7082:     my $uhome=&homeserver($uname,$udomain);
                   7083:     my %newstorehash;
1.800     albertel 7084:     foreach my $item (keys(%$storehash)) {
                   7085: 	my $key = $version.':'.&escape($symb).':'.$item;
                   7086: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 7087:     }
                   7088:     my $items='';
                   7089:     my %allitems = ();
1.800     albertel 7090:     foreach my $item (keys(%newstorehash)) {
                   7091: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 7092: 	    my $key = $1.':keys:'.$2;
                   7093: 	    $allitems{$key} .= $3.':';
                   7094: 	}
1.800     albertel 7095: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 7096:     }
1.800     albertel 7097:     foreach my $item (keys(%allitems)) {
                   7098: 	$allitems{$item} =~ s/\:$//;
                   7099: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 7100:     }
                   7101:     $items=~s/\&$//;
                   7102:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  7103: }
                   7104: 
1.47      www      7105: # ------------------------------------------------------ critical put interface
                   7106: 
                   7107: sub cput {
1.134     albertel 7108:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 7109:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7110:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 7111:    my $uhome=&homeserver($uname,$udomain);
1.47      www      7112:    my $items='';
1.800     albertel 7113:    foreach my $item (keys(%$storehash)) {
                   7114:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 7115:    }
1.47      www      7116:    $items=~s/\&$//;
1.134     albertel 7117:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7118: }
                   7119: 
                   7120: # -------------------------------------------------------------- eget interface
                   7121: 
                   7122: sub eget {
1.133     albertel 7123:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      7124:    my $items='';
1.800     albertel 7125:    foreach my $item (@$storearr) {
                   7126:        $items.=&escape($item).'&';
1.191     harris41 7127:    }
1.12      www      7128:    $items=~s/\&$//;
1.620     albertel 7129:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   7130:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 7131:    my $uhome=&homeserver($uname,$udomain);
                   7132:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      7133:    my @pairs=split(/\&/,$rep);
                   7134:    my %returnhash=();
1.42      www      7135:    my $i=0;
1.800     albertel 7136:    foreach my $item (@$storearr) {
                   7137:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      7138:       $i++;
1.191     harris41 7139:    }
1.12      www      7140:    return %returnhash;
                   7141: }
                   7142: 
1.667     albertel 7143: # ------------------------------------------------------------ tmpput interface
                   7144: sub tmpput {
1.802     raeburn  7145:     my ($storehash,$server,$context)=@_;
1.667     albertel 7146:     my $items='';
1.800     albertel 7147:     foreach my $item (keys(%$storehash)) {
                   7148: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 7149:     }
                   7150:     $items=~s/\&$//;
1.802     raeburn  7151:     if (defined($context)) {
                   7152:         $items .= ':'.&escape($context);
                   7153:     }
1.667     albertel 7154:     return &reply("tmpput:$items",$server);
                   7155: }
                   7156: 
                   7157: # ------------------------------------------------------------ tmpget interface
                   7158: sub tmpget {
1.688     albertel 7159:     my ($token,$server)=@_;
                   7160:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7161:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 7162:     my %returnhash;
1.1328    raeburn  7163:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
                   7164:         return %returnhash;
                   7165:     }
1.667     albertel 7166:     foreach my $item (split(/\&/,$rep)) {
                   7167: 	my ($key,$value)=split(/=/,$item);
                   7168: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   7169:     }
                   7170:     return %returnhash;
                   7171: }
                   7172: 
1.1113    raeburn  7173: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 7174: sub tmpdel {
                   7175:     my ($token,$server)=@_;
                   7176:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   7177:     return &reply("tmpdel:$token",$server);
                   7178: }
                   7179: 
1.1198    raeburn  7180: # ------------------------------------------------------------ get_timebased_id 
                   7181: 
                   7182: sub get_timebased_id {
                   7183:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   7184:         $maxtries) = @_;
                   7185:     my ($newid,$error,$dellock);
                   7186:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   7187:         return ('','ok','invalid call to get suffix');
                   7188:     }
                   7189: 
                   7190: # set defaults for any optional args for which values were not supplied
                   7191:     if ($who eq '') {
                   7192:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   7193:     }
                   7194:     if (!$locktries) {
                   7195:         $locktries = 3;
                   7196:     }
                   7197:     if (!$maxtries) {
                   7198:         $maxtries = 10;
                   7199:     }
                   7200:     
                   7201:     if (($cdom eq '') || ($cnum eq '')) {
                   7202:         if ($env{'request.course.id'}) {
                   7203:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7204:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7205:         }
                   7206:         if (($cdom eq '') || ($cnum eq '')) {
                   7207:             return ('','ok','call to get suffix not in course context');
                   7208:         }
                   7209:     }
                   7210: 
                   7211: # construct locking item
                   7212:     my $lockhash = {
                   7213:                       $prefix."\0".'locked_'.$keyid => $who,
                   7214:                    };
                   7215:     my $tries = 0;
                   7216: 
                   7217: # attempt to get lock on nohist_$namespace file
                   7218:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   7219:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   7220:         $tries ++;
                   7221:         sleep 1;
                   7222:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   7223:     }
                   7224: 
                   7225: # attempt to get unique identifier, based on current timestamp
                   7226:     if ($gotlock eq 'ok') {
                   7227:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
                   7228:         my $id = time;
                   7229:         $newid = $id;
1.1268    raeburn  7230:         if ($idtype eq 'addcode') {
                   7231:             $newid .= &sixnum_code();
                   7232:         }
1.1198    raeburn  7233:         my $idtries = 0;
                   7234:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   7235:             if ($idtype eq 'concat') {
                   7236:                 $newid = $id.$idtries;
1.1268    raeburn  7237:             } elsif ($idtype eq 'addcode') {
                   7238:                 $newid = $newid.&sixnum_code();
1.1198    raeburn  7239:             } else {
                   7240:                 $newid ++;
                   7241:             }
                   7242:             $idtries ++;
                   7243:         }
                   7244:         if (!exists($inuse{$prefix."\0".$newid})) {
                   7245:             my %new_item =  (
                   7246:                               $prefix."\0".$newid => $who,
                   7247:                             );
                   7248:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
                   7249:                                                  $cdom,$cnum);
                   7250:             if ($putresult ne 'ok') {
                   7251:                 undef($newid);
                   7252:                 $error = 'error saving new item: '.$putresult;
                   7253:             }
                   7254:         } else {
1.1268    raeburn  7255:              undef($newid);
1.1198    raeburn  7256:              $error = ('error: no unique suffix available for the new item ');
                   7257:         }
                   7258: #  remove lock
                   7259:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   7260:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   7261:     } else {
                   7262:         $error = "error: could not obtain lockfile\n";
                   7263:         $dellock = 'ok';
1.1276    raeburn  7264:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
                   7265:             $dellock = 'nolock';
                   7266:         }
1.1198    raeburn  7267:     }
                   7268:     return ($newid,$dellock,$error);
                   7269: }
                   7270: 
1.1268    raeburn  7271: sub sixnum_code {
                   7272:     my $code;
                   7273:     for (0..6) {
                   7274:         $code .= int( rand(9) );
                   7275:     }
                   7276:     return $code;
                   7277: }
                   7278: 
1.765     albertel 7279: # -------------------------------------------------- portfolio access checking
                   7280: 
                   7281: sub portfolio_access {
1.1270    raeburn  7282:     my ($requrl,$clientip) = @_;
1.765     albertel 7283:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270    raeburn  7284:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814     raeburn  7285:     if ($result) {
                   7286:         my %setters;
                   7287:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   7288:             my ($startblock,$endblock) =
                   7289:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   7290:             if ($startblock && $endblock) {
                   7291:                 return 'B';
                   7292:             }
                   7293:         } else {
                   7294:             my ($startblock,$endblock) =
                   7295:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   7296:             if ($startblock && $endblock) {
                   7297:                 return 'B';
                   7298:             }
                   7299:         }
                   7300:     }
1.765     albertel 7301:     if ($result eq 'ok') {
1.766     albertel 7302:        return 'F';
1.765     albertel 7303:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 7304:        return 'A';
1.765     albertel 7305:     }
1.766     albertel 7306:     return '';
1.765     albertel 7307: }
                   7308: 
                   7309: sub get_portfolio_access {
1.1270    raeburn  7310:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767     albertel 7311: 
                   7312:     if (!ref($access_hash)) {
                   7313: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   7314: 	my %access_controls = &get_access_controls($current_perms,$group,
                   7315: 						   $file_name);
                   7316: 	$access_hash = $access_controls{$file_name};
                   7317:     }
                   7318: 
1.1270    raeburn  7319:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765     albertel 7320:     my $now = time;
                   7321:     if (ref($access_hash) eq 'HASH') {
                   7322:         foreach my $key (keys(%{$access_hash})) {
                   7323:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   7324:             if ($start > $now) {
                   7325:                 next;
                   7326:             }
                   7327:             if ($end && $end<$now) {
                   7328:                 next;
                   7329:             }
                   7330:             if ($scope eq 'public') {
                   7331:                 $public = $key;
                   7332:                 last;
                   7333:             } elsif ($scope eq 'guest') {
                   7334:                 $guest = $key;
                   7335:             } elsif ($scope eq 'domains') {
                   7336:                 push(@domains,$key);
                   7337:             } elsif ($scope eq 'users') {
                   7338:                 push(@users,$key);
                   7339:             } elsif ($scope eq 'course') {
                   7340:                 push(@courses,$key);
                   7341:             } elsif ($scope eq 'group') {
                   7342:                 push(@groups,$key);
1.1270    raeburn  7343:             } elsif ($scope eq 'ip') {
                   7344:                 push(@ips,$key);
1.765     albertel 7345:             }
                   7346:         }
                   7347:         if ($public) {
                   7348:             return 'ok';
1.1270    raeburn  7349:         } elsif (@ips > 0) {
                   7350:             my $allowed;
                   7351:             foreach my $ipkey (@ips) {
                   7352:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
                   7353:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
                   7354:                         $allowed = 1;
                   7355:                         last; 
                   7356:                     }
                   7357:                 }
                   7358:             }
                   7359:             if ($allowed) {
                   7360:                 return 'ok';
                   7361:             }
1.765     albertel 7362:         }
                   7363:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   7364:             if ($guest) {
                   7365:                 return $guest;
                   7366:             }
                   7367:         } else {
                   7368:             if (@domains > 0) {
                   7369:                 foreach my $domkey (@domains) {
                   7370:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   7371:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   7372:                             return 'ok';
                   7373:                         }
                   7374:                     }
                   7375:                 }
                   7376:             }
                   7377:             if (@users > 0) {
                   7378:                 foreach my $userkey (@users) {
1.865     raeburn  7379:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   7380:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   7381:                             if (ref($item) eq 'HASH') {
                   7382:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   7383:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   7384:                                     return 'ok';
                   7385:                                 }
                   7386:                             }
                   7387:                         }
                   7388:                     } 
1.765     albertel 7389:                 }
                   7390:             }
                   7391:             my %roleshash;
                   7392:             my @courses_and_groups = @courses;
                   7393:             push(@courses_and_groups,@groups); 
                   7394:             if (@courses_and_groups > 0) {
                   7395:                 my (%allgroups,%allroles); 
                   7396:                 my ($start,$end,$role,$sec,$group);
                   7397:                 foreach my $envkey (%env) {
1.1060    raeburn  7398:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7399:                         my $cid = $2.'_'.$3; 
                   7400:                         if ($1 eq 'gr') {
                   7401:                             $group = $4;
                   7402:                             $allgroups{$cid}{$group} = $env{$envkey};
                   7403:                         } else {
                   7404:                             if ($4 eq '') {
                   7405:                                 $sec = 'none';
                   7406:                             } else {
                   7407:                                 $sec = $4;
                   7408:                             }
                   7409:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7410:                         }
1.811     albertel 7411:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 7412:                         my $cid = $2.'_'.$3;
                   7413:                         if ($4 eq '') {
                   7414:                             $sec = 'none';
                   7415:                         } else {
                   7416:                             $sec = $4;
                   7417:                         }
                   7418:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   7419:                     }
                   7420:                 }
                   7421:                 if (keys(%allroles) == 0) {
                   7422:                     return;
                   7423:                 }
                   7424:                 foreach my $key (@courses_and_groups) {
                   7425:                     my %content = %{$$access_hash{$key}};
                   7426:                     my $cnum = $content{'number'};
                   7427:                     my $cdom = $content{'domain'};
                   7428:                     my $cid = $cdom.'_'.$cnum;
                   7429:                     if (!exists($allroles{$cid})) {
                   7430:                         next;
                   7431:                     }    
                   7432:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   7433:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   7434:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   7435:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   7436:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   7437:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   7438:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   7439:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   7440:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   7441:                                         if (grep/^all$/,@sections) {
                   7442:                                             return 'ok';
                   7443:                                         } else {
                   7444:                                             if (grep/^$sec$/,@sections) {
                   7445:                                                 return 'ok';
                   7446:                                             }
                   7447:                                         }
                   7448:                                     }
                   7449:                                 }
                   7450:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   7451:                                     if (grep/^none$/,@groups) {
                   7452:                                         return 'ok';
                   7453:                                     }
                   7454:                                 } else {
                   7455:                                     if (grep/^all$/,@groups) {
                   7456:                                         return 'ok';
                   7457:                                     } 
                   7458:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   7459:                                         if (grep/^$group$/,@groups) {
                   7460:                                             return 'ok';
                   7461:                                         }
                   7462:                                     }
                   7463:                                 } 
                   7464:                             }
                   7465:                         }
                   7466:                     }
                   7467:                 }
                   7468:             }
                   7469:             if ($guest) {
                   7470:                 return $guest;
                   7471:             }
                   7472:         }
                   7473:     }
                   7474:     return;
                   7475: }
                   7476: 
                   7477: sub course_group_datechecker {
                   7478:     my ($dates,$now,$status) = @_;
                   7479:     my ($start,$end) = split(/\./,$dates);
                   7480:     if (!$start && !$end) {
                   7481:         return 'ok';
                   7482:     }
                   7483:     if (grep/^active$/,@{$status}) {
                   7484:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   7485:             return 'ok';
                   7486:         }
                   7487:     }
                   7488:     if (grep/^previous$/,@{$status}) {
                   7489:         if ($end > $now ) {
                   7490:             return 'ok';
                   7491:         }
                   7492:     }
                   7493:     if (grep/^future$/,@{$status}) {
                   7494:         if ($start > $now) {
                   7495:             return 'ok';
                   7496:         }
                   7497:     }
                   7498:     return; 
                   7499: }
                   7500: 
                   7501: sub parse_portfolio_url {
                   7502:     my ($url) = @_;
                   7503: 
                   7504:     my ($type,$udom,$unum,$group,$file_name);
                   7505:     
1.823     albertel 7506:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 7507: 	$type = 1;
                   7508:         $udom = $1;
                   7509:         $unum = $2;
                   7510:         $file_name = $3;
1.823     albertel 7511:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 7512: 	$type = 2;
                   7513:         $udom = $1;
                   7514:         $unum = $2;
                   7515:         $group = $3;
                   7516:         $file_name = $3.'/'.$4;
                   7517:     }
                   7518:     if (wantarray) {
                   7519: 	return ($type,$udom,$unum,$file_name,$group);
                   7520:     }
                   7521:     return $type;
                   7522: }
                   7523: 
                   7524: sub is_portfolio_url {
                   7525:     my ($url) = @_;
                   7526:     return scalar(&parse_portfolio_url($url));
                   7527: }
                   7528: 
1.798     raeburn  7529: sub is_portfolio_file {
                   7530:     my ($file) = @_;
1.820     raeburn  7531:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  7532:         return 1;
                   7533:     }
                   7534:     return;
                   7535: }
                   7536: 
1.976     raeburn  7537: sub usertools_access {
1.1084    raeburn  7538:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  7539:     my ($access,%tools);
                   7540:     if ($context eq '') {
                   7541:         $context = 'tools';
                   7542:     }
                   7543:     if ($context eq 'requestcourses') {
                   7544:         %tools = (
                   7545:                       official   => 1,
                   7546:                       unofficial => 1,
1.1006    raeburn  7547:                       community  => 1,
1.1246    raeburn  7548:                       textbook   => 1,
1.1305    raeburn  7549:                       placement  => 1,
1.1368    raeburn  7550:                       lti        => 1,
1.985     raeburn  7551:                  );
1.1183    raeburn  7552:     } elsif ($context eq 'requestauthor') {
                   7553:         %tools = (
                   7554:                       requestauthor => 1,
                   7555:                  );
1.985     raeburn  7556:     } else {
                   7557:         %tools = (
                   7558:                       aboutme   => 1,
                   7559:                       blog      => 1,
1.1177    raeburn  7560:                       webdav    => 1,
1.985     raeburn  7561:                       portfolio => 1,
                   7562:                  );
                   7563:     }
1.976     raeburn  7564:     return if (!defined($tools{$tool}));
                   7565: 
1.1242    raeburn  7566:     if (($udom eq '') || ($uname eq '')) {
1.976     raeburn  7567:         $udom = $env{'user.domain'};
                   7568:         $uname = $env{'user.name'};
                   7569:     }
                   7570: 
1.978     raeburn  7571:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   7572:         if ($action ne 'reload') {
1.985     raeburn  7573:             if ($context eq 'requestcourses') {
                   7574:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  7575:             } elsif ($context eq 'requestauthor') {
                   7576:                 return $env{'environment.canrequest.author'};
1.985     raeburn  7577:             } else {
                   7578:                 return $env{'environment.availabletools.'.$tool};
                   7579:             }
                   7580:         }
1.976     raeburn  7581:     }
                   7582: 
1.1183    raeburn  7583:     my ($toolstatus,$inststatus,$envkey);
                   7584:     if ($context eq 'requestauthor') {
                   7585:         $envkey = $context; 
                   7586:     } else {
                   7587:         $envkey = $context.'.'.$tool;
                   7588:     }
1.976     raeburn  7589: 
1.985     raeburn  7590:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   7591:          ($action ne 'reload')) {
1.1183    raeburn  7592:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  7593:         $inststatus = $env{'environment.inststatus'};
                   7594:     } else {
1.1084    raeburn  7595:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  7596:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  7597:             $inststatus = $userenvref->{'inststatus'};
                   7598:         } else {
1.1183    raeburn  7599:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   7600:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  7601:             $inststatus = $userenv{'inststatus'};
                   7602:         }
1.976     raeburn  7603:     }
                   7604: 
                   7605:     if ($toolstatus ne '') {
                   7606:         if ($toolstatus) {
                   7607:             $access = 1;
                   7608:         } else {
                   7609:             $access = 0;
                   7610:         }
                   7611:         return $access;
                   7612:     }
                   7613: 
1.1084    raeburn  7614:     my ($is_adv,%domdef);
                   7615:     if (ref($is_advref) eq 'HASH') {
                   7616:         $is_adv = $is_advref->{'is_adv'};
                   7617:     } else {
                   7618:         $is_adv = &is_advanced_user($udom,$uname);
                   7619:     }
                   7620:     if (ref($domdefref) eq 'HASH') {
                   7621:         %domdef = %{$domdefref};
                   7622:     } else {
                   7623:         %domdef = &get_domain_defaults($udom);
                   7624:     }
1.976     raeburn  7625:     if (ref($domdef{$tool}) eq 'HASH') {
                   7626:         if ($is_adv) {
                   7627:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   7628:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   7629:                     $access = 1;
                   7630:                 } else {
                   7631:                     $access = 0;
                   7632:                 }
                   7633:                 return $access;
                   7634:             }
                   7635:         }
                   7636:         if ($inststatus ne '') {
                   7637:             my ($hasaccess,$hasnoaccess);
                   7638:             foreach my $affiliation (split(/:/,$inststatus)) {
                   7639:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   7640:                     if ($domdef{$tool}{$affiliation}) {
                   7641:                         $hasaccess = 1;
                   7642:                     } else {
                   7643:                         $hasnoaccess = 1;
                   7644:                     }
                   7645:                 }
                   7646:             }
                   7647:             if ($hasaccess || $hasnoaccess) {
                   7648:                 if ($hasaccess) {
                   7649:                     $access = 1;
                   7650:                 } elsif ($hasnoaccess) {
                   7651:                     $access = 0; 
                   7652:                 }
                   7653:                 return $access;
                   7654:             }
                   7655:         } else {
                   7656:             if ($domdef{$tool}{'default'} ne '') {
                   7657:                 if ($domdef{$tool}{'default'}) {
                   7658:                     $access = 1;
                   7659:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   7660:                     $access = 0;
                   7661:                 }
                   7662:                 return $access;
                   7663:             }
                   7664:         }
                   7665:     } else {
1.1177    raeburn  7666:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  7667:             $access = 1;
                   7668:         } else {
                   7669:             $access = 0;
                   7670:         }
1.976     raeburn  7671:         return $access;
                   7672:     }
                   7673: }
                   7674: 
1.1050    raeburn  7675: sub is_course_owner {
                   7676:     my ($cdom,$cnum,$udom,$uname) = @_;
                   7677:     if (($udom eq '') || ($uname eq '')) {
                   7678:         $udom = $env{'user.domain'};
                   7679:         $uname = $env{'user.name'};
                   7680:     }
                   7681:     unless (($udom eq '') || ($uname eq '')) {
                   7682:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   7683:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   7684:                 return 1;
                   7685:             } else {
                   7686:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   7687:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   7688:                     return 1;
                   7689:                 }
                   7690:             }
                   7691:         }
                   7692:     }
                   7693:     return;
                   7694: }
                   7695: 
1.976     raeburn  7696: sub is_advanced_user {
                   7697:     my ($udom,$uname) = @_;
1.1085    raeburn  7698:     if ($udom ne '' && $uname ne '') {
                   7699:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  7700:             if (wantarray) {
                   7701:                 return ($env{'user.adv'},$env{'user.author'});
                   7702:             } else {
                   7703:                 return $env{'user.adv'};
                   7704:             }
1.1085    raeburn  7705:         }
                   7706:     }
1.976     raeburn  7707:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   7708:     my %allroles;
1.1128    raeburn  7709:     my ($is_adv,$is_author);
1.976     raeburn  7710:     foreach my $role (keys(%roleshash)) {
                   7711:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   7712:         my $area = '/'.$tdomain.'/'.$trest;
                   7713:         if ($sec ne '') {
                   7714:             $area .= '/'.$sec;
                   7715:         }
                   7716:         if (($area ne '') && ($trole ne '')) {
                   7717:             my $spec=$trole.'.'.$area;
                   7718:             if ($trole =~ /^cr\//) {
                   7719:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   7720:             } elsif ($trole ne 'gr') {
                   7721:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   7722:             }
1.1128    raeburn  7723:             if ($trole eq 'au') {
                   7724:                 $is_author = 1;
                   7725:             }
1.976     raeburn  7726:         }
                   7727:     }
                   7728:     foreach my $role (keys(%allroles)) {
                   7729:         last if ($is_adv);
                   7730:         foreach my $item (split(/:/,$allroles{$role})) {
                   7731:             if ($item ne '') {
                   7732:                 my ($privilege,$restrictions)=split(/&/,$item);
                   7733:                 if ($privilege eq 'adv') {
                   7734:                     $is_adv = 1;
                   7735:                     last;
                   7736:                 }
                   7737:             }
                   7738:         }
                   7739:     }
1.1128    raeburn  7740:     if (wantarray) {
                   7741:         return ($is_adv,$is_author);
                   7742:     }
1.976     raeburn  7743:     return $is_adv;
                   7744: }
1.798     raeburn  7745: 
1.1035    raeburn  7746: sub check_can_request {
1.1368    raeburn  7747:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035    raeburn  7748:     my $canreq = 0;
1.1368    raeburn  7749:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   7750:         $uname = $env{'user.name'};
                   7751:         $udom = $env{'user.domain'};
                   7752:     }
1.1035    raeburn  7753:     my ($types,$typename) = &Apache::loncommon::course_types();
                   7754:     my @options = ('approval','validate','autolimit');
                   7755:     my $optregex = join('|',@options);
                   7756:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   7757:         foreach my $type (@{$types}) {
1.1368    raeburn  7758:             if (&usertools_access($uname,$udom,$type,undef,
                   7759:                                   'requestcourses')) {
1.1035    raeburn  7760:                 $canreq ++;
1.1036    raeburn  7761:                 if (ref($request_domains) eq 'HASH') {
1.1368    raeburn  7762:                     push(@{$request_domains->{$type}},$udom);
1.1036    raeburn  7763:                 }
1.1368    raeburn  7764:                 if ($dom eq $udom) {
1.1035    raeburn  7765:                     $can_request->{$type} = 1;
                   7766:                 }
                   7767:             }
1.1368    raeburn  7768:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
                   7769:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035    raeburn  7770:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   7771:                 if (@curr > 0) {
1.1036    raeburn  7772:                     foreach my $item (@curr) {
                   7773:                         if (ref($request_domains) eq 'HASH') {
                   7774:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   7775:                             if ($otherdom ne '') {
                   7776:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   7777:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   7778:                                         push(@{$request_domains->{$type}},$otherdom);
                   7779:                                     }
                   7780:                                 } else {
                   7781:                                     push(@{$request_domains->{$type}},$otherdom);
                   7782:                                 }
                   7783:                             }
                   7784:                         }
                   7785:                     }
1.1368    raeburn  7786:                     unless ($dom eq $env{'user.domain'}) {
1.1036    raeburn  7787:                         $canreq ++;
1.1035    raeburn  7788:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   7789:                             $can_request->{$type} = 1;
                   7790:                         }
                   7791:                     }
                   7792:                 }
                   7793:             }
                   7794:         }
                   7795:     }
                   7796:     return $canreq;
                   7797: }
                   7798: 
1.341     www      7799: # ---------------------------------------------- Custom access rule evaluation
                   7800: 
                   7801: sub customaccess {
                   7802:     my ($priv,$uri)=@_;
1.807     albertel 7803:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      7804:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 7805:     $udom = &LONCAPA::clean_domain($udom);
                   7806:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      7807:     my $access=0;
1.800     albertel 7808:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 7809: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   7810: 	if ($type eq 'user') {
                   7811: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 7812: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 7813: 		if ($tdom) {
                   7814: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   7815: 		}
1.896     albertel 7816: 		if ($tuname) {
                   7817: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 7818: 		}
                   7819: 		$access=($effect eq 'allow');
                   7820: 		last;
                   7821: 	    }
                   7822: 	} else {
                   7823: 	    if ($role) {
                   7824: 		if ($role ne $urole) { next; }
                   7825: 	    }
                   7826: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   7827: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   7828: 		if ($tdom) {
                   7829: 		    if ($tdom ne $udom) { next; }
                   7830: 		}
                   7831: 		if ($tcrs) {
                   7832: 		    if ($tcrs ne $ucrs) { next; }
                   7833: 		}
                   7834: 		if ($tsec) {
                   7835: 		    if ($tsec ne $usec) { next; }
                   7836: 		}
                   7837: 		$access=($effect eq 'allow');
                   7838: 		last;
                   7839: 	    }
                   7840: 	    if ($realm eq '' && $role eq '') {
                   7841: 		$access=($effect eq 'allow');
                   7842: 	    }
1.402     bowersj2 7843: 	}
1.341     www      7844:     }
                   7845:     return $access;
                   7846: }
                   7847: 
1.103     harris41 7848: # ------------------------------------------------- Check for a user privilege
1.12      www      7849: 
                   7850: sub allowed {
1.1281    raeburn  7851:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
1.705     albertel 7852:     my $ver_orguri=$uri;
1.439     www      7853:     $uri=&deversion($uri);
1.152     www      7854:     my $orguri=$uri;
1.52      www      7855:     $uri=&declutter($uri);
1.809     raeburn  7856: 
1.810     raeburn  7857:     if ($priv eq 'evb') {
                   7858: # Evade communication block restrictions for specified role in a course
                   7859:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   7860:             return $1;
                   7861:         } else {
                   7862:             return;
                   7863:         }
                   7864:     }
                   7865: 
1.620     albertel 7866:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      7867: # Free bre access to adm and meta resources
1.1343    raeburn  7868:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
1.769     albertel 7869: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   7870: 	&& ($priv eq 'bre')) {
1.14      www      7871: 	return 'F';
1.159     www      7872:     }
                   7873: 
1.545     banghart 7874: # Free bre access to user's own portfolio contents
1.714     raeburn  7875:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  7876:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  7877: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  7878:         my %setters;
                   7879:         my ($startblock,$endblock) = 
                   7880:             &Apache::loncommon::blockcheck(\%setters,'port');
                   7881:         if ($startblock && $endblock) {
                   7882:             return 'B';
                   7883:         } else {
                   7884:             return 'F';
                   7885:         }
1.545     banghart 7886:     }
                   7887: 
1.762     raeburn  7888: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  7889:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   7890:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   7891:         if (exists($env{'request.course.id'})) {
                   7892:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7893:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7894:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   7895:                 my $courseprivid=$env{'request.course.id'};
                   7896:                 $courseprivid=~s/\_/\//;
                   7897:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   7898:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   7899:                     return $1; 
1.762     raeburn  7900:                 } else {
                   7901:                     if ($env{'request.course.sec'}) {
                   7902:                         $courseprivid.='/'.$env{'request.course.sec'};
                   7903:                     }
                   7904:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   7905:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   7906:                         return $2;
                   7907:                     }
1.714     raeburn  7908:                 }
                   7909:             }
                   7910:         }
                   7911:     }
                   7912: 
1.159     www      7913: # Free bre to public access
                   7914: 
                   7915:     if ($priv eq 'bre') {
1.1362    raeburn  7916:         my $copyright;
                   7917:         unless ($uri =~ /ext\.tool/) {
                   7918:             $copyright=&metadata($uri,'copyright');
                   7919:         }
1.620     albertel 7920: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      7921:            return 'F'; 
                   7922:         }
1.238     www      7923:         if ($copyright eq 'priv') {
                   7924:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 7925: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      7926: 		return '';
                   7927:             }
                   7928:         }
                   7929:         if ($copyright eq 'domain') {
                   7930:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 7931: 	    unless (($env{'user.domain'} eq $1) ||
                   7932:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      7933: 		return '';
                   7934:             }
1.262     matthew  7935:         }
1.620     albertel 7936:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  7937:             # Library role, so allow browsing of resources in this domain.
                   7938:             return 'F';
1.238     www      7939:         }
1.341     www      7940:         if ($copyright eq 'custom') {
                   7941: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   7942:         }
1.14      www      7943:     }
1.264     matthew  7944:     # Domain coordinator is trying to create a course
1.620     albertel 7945:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  7946:         # uri is the requested domain in this case.
                   7947:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  7948:         # a role of dc for the domain in question.
1.620     albertel 7949:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  7950:     }
1.29      www      7951: 
1.52      www      7952:     my $thisallowed='';
                   7953:     my $statecond=0;
                   7954:     my $courseprivid='';
                   7955: 
1.1039    raeburn  7956:     my $ownaccess;
1.1043    raeburn  7957:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  7958:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   7959:         if ($uri eq '') {
                   7960:             $ownaccess = 1;
                   7961:         } else {
                   7962:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   7963:                 my $udom = $env{'user.domain'};
                   7964:                 my $uname = $env{'user.name'};
                   7965:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   7966:                     $ownaccess = 1;
1.1040    raeburn  7967:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  7968:                     unless ($uri =~ m{\.\./}) {
                   7969:                         $ownaccess = 1;
                   7970:                     }
                   7971:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   7972:                     my $now = time;
                   7973:                     if ($uri =~ m{^([^/]+)/?$}) {
                   7974:                         my $adom = $1;
                   7975:                         foreach my $key (keys(%env)) {
1.1042    raeburn  7976:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  7977:                                 my ($start,$end) = split('.',$env{$key});
                   7978:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   7979:                                     $ownaccess = 1;
                   7980:                                     last;
                   7981:                                 }
                   7982:                             }
                   7983:                         }
                   7984:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   7985:                         my $adom = $1;
                   7986:                         my $aname = $2;
1.1042    raeburn  7987:                         foreach my $role ('ca','aa') { 
                   7988:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   7989:                                 my ($start,$end) =
                   7990:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   7991:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   7992:                                     $ownaccess = 1;
                   7993:                                     last;
                   7994:                                 }
1.1039    raeburn  7995:                             }
                   7996:                         }
                   7997:                     }
                   7998:                 }
                   7999:             }
                   8000:         }
                   8001:     }
                   8002: 
1.52      www      8003: # Course
                   8004: 
1.620     albertel 8005:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8006:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8007:             $thisallowed.=$1;
                   8008:         }
1.52      www      8009:     }
1.29      www      8010: 
1.52      www      8011: # Domain
                   8012: 
1.620     albertel 8013:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 8014:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8015:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8016:             $thisallowed.=$1;
                   8017:         }
1.12      www      8018:     }
1.52      www      8019: 
1.1141    raeburn  8020: # User who is not author or co-author might still be able to edit
                   8021: # resource of an author in the domain (e.g., if Domain Coordinator).
                   8022:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   8023:         (&allowed('mdc',$env{'request.course.id'}))) {
                   8024:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   8025:             $thisallowed.=$1;
                   8026:         }
                   8027:     }
                   8028: 
1.52      www      8029: # Course: uri itself is a course
1.66      www      8030:     my $courseuri=$uri;
                   8031:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      8032:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      8033: 
1.620     albertel 8034:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 8035:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  8036:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  8037:             $thisallowed.=$1;
                   8038:         }
1.12      www      8039:     }
1.29      www      8040: 
1.665     albertel 8041: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 8042: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 8043:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 8044: 	$thisallowed='';
1.671     raeburn  8045:         my ($match)=&is_on_map($uri);
                   8046:         if ($match) {
                   8047:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   8048:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1281    raeburn  8049:                 my $value = $1;
1.1402    raeburn  8050:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
                   8051:                 if ($deeplinkblock) {
                   8052:                     $thisallowed='D';
                   8053:                 } elsif ($noblockcheck) {
1.1281    raeburn  8054:                     $thisallowed.=$value;
1.1162    raeburn  8055:                 } else {
1.1281    raeburn  8056:                     my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   8057:                     if (@blockers > 0) {
                   8058:                         $thisallowed = 'B';
                   8059:                     } else {
                   8060:                         $thisallowed.=$value;
                   8061:                     }
1.1162    raeburn  8062:                 }
1.671     raeburn  8063:             }
                   8064:         } else {
1.705     albertel 8065:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  8066:             if ($refuri) {
                   8067:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  8068:                     $thisallowed='F';
1.671     raeburn  8069:                 } else {
                   8070:                     $refuri=&declutter($refuri);
                   8071:                     my ($match) = &is_on_map($refuri);
                   8072:                     if ($match) {
1.1402    raeburn  8073:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8074:                         if ($deeplinkblock) {
                   8075:                             $thisallowed='D';
                   8076:                         } elsif ($noblockcheck) {
1.1281    raeburn  8077:                             $thisallowed='F';
1.1162    raeburn  8078:                         } else {
1.1281    raeburn  8079:                             my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   8080:                             if (@blockers > 0) {
                   8081:                                 $thisallowed = 'B';
                   8082:                             } else {
                   8083:                                 $thisallowed='F';
                   8084:                             }
1.1162    raeburn  8085:                         }
1.671     raeburn  8086:                     }
1.669     raeburn  8087:                 }
1.671     raeburn  8088:             }
                   8089:         }
1.314     www      8090:     }
1.492     albertel 8091: 
1.766     albertel 8092:     if ($priv eq 'bre'
                   8093: 	&& $thisallowed ne 'F' 
                   8094: 	&& $thisallowed ne '2'
                   8095: 	&& &is_portfolio_url($uri)) {
1.1270    raeburn  8096: 	$thisallowed = &portfolio_access($uri,$clientip);
1.766     albertel 8097:     }
1.1250    raeburn  8098: 
1.52      www      8099: # Full access at system, domain or course-wide level? Exit.
1.29      www      8100:     if ($thisallowed=~/F/) {
                   8101: 	return 'F';
                   8102:     }
                   8103: 
1.52      www      8104: # If this is generating or modifying users, exit with special codes
1.29      www      8105: 
1.643     www      8106:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   8107: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 8108: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      8109: # no author name given, so this just checks on the general right to make a co-author in this domain
                   8110: 	    unless ($auname) { return $thisallowed; }
                   8111: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 8112: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   8113: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   8114: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   8115: 	}
1.52      www      8116: 	return $thisallowed;
                   8117:     }
                   8118: #
1.103     harris41 8119: # Gathered so far: system, domain and course wide privileges
1.52      www      8120: #
                   8121: # Course: See if uri or referer is an individual resource that is part of 
                   8122: # the course
                   8123: 
1.620     albertel 8124:     if ($env{'request.course.id'}) {
1.232     www      8125: 
1.620     albertel 8126:        $courseprivid=$env{'request.course.id'};
                   8127:        if ($env{'request.course.sec'}) {
                   8128:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      8129:        }
                   8130:        $courseprivid=~s/\_/\//;
                   8131:        my $checkreferer=1;
1.232     www      8132:        my ($match,$cond)=&is_on_map($uri);
                   8133:        if ($match) {
                   8134:            $statecond=$cond;
1.620     albertel 8135:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8136:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8137:                my $value = $1;
                   8138:                if ($priv eq 'bre') {
1.1281    raeburn  8139:                    if ($noblockcheck) {
                   8140:                        $thisallowed.=$value;
1.1162    raeburn  8141:                    } else {
1.1281    raeburn  8142:                        my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   8143:                        if (@blockers > 0) {
                   8144:                            $thisallowed = 'B';
                   8145:                        } else {
                   8146:                            $thisallowed.=$value;
                   8147:                        }
1.1162    raeburn  8148:                    }
                   8149:                } else {
                   8150:                    $thisallowed.=$value;
                   8151:                }
1.52      www      8152:                $checkreferer=0;
                   8153:            }
1.29      www      8154:        }
1.83      www      8155:        
1.148     www      8156:        if ($checkreferer) {
1.620     albertel 8157: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      8158:             unless ($refuri) {
1.800     albertel 8159:                 foreach my $key (keys(%env)) {
                   8160: 		    if ($key=~/^httpref\..*\*/) {
                   8161: 			my $pattern=$key;
1.156     www      8162:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      8163:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   8164:                         $pattern=~s/\//\\\//g;
1.152     www      8165:                         if ($orguri=~/$pattern/) {
1.800     albertel 8166: 			    $refuri=$env{$key};
1.148     www      8167:                         }
                   8168:                     }
1.191     harris41 8169:                 }
1.148     www      8170:             }
1.232     www      8171: 
1.148     www      8172:          if ($refuri) { 
1.152     www      8173: 	  $refuri=&declutter($refuri);
1.232     www      8174:           my ($match,$cond)=&is_on_map($refuri);
                   8175:             if ($match) {
                   8176:               my $refstatecond=$cond;
1.620     albertel 8177:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 8178:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  8179:                   my $value = $1;
                   8180:                   if ($priv eq 'bre') {
1.1402    raeburn  8181:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
                   8182:                       if ($deeplinkblock) {
                   8183:                           $thisallowed = 'D';
                   8184:                       } elsif ($noblockcheck) {
1.1281    raeburn  8185:                           $thisallowed.=$value;
1.1162    raeburn  8186:                       } else {
1.1281    raeburn  8187:                           my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   8188:                           if (@blockers > 0) {
                   8189:                               $thisallowed = 'B';
                   8190:                           } else {
                   8191:                               $thisallowed.=$value;
                   8192:                           }
1.1162    raeburn  8193:                       }
                   8194:                   } else {
                   8195:                       $thisallowed.=$value;
                   8196:                   }
1.53      www      8197:                   $uri=$refuri;
                   8198:                   $statecond=$refstatecond;
1.52      www      8199:               }
                   8200:           }
1.148     www      8201:         }
1.29      www      8202:        }
1.52      www      8203:    }
1.29      www      8204: 
1.52      www      8205: #
1.103     harris41 8206: # Gathered now: all privileges that could apply, and condition number
1.52      www      8207: # 
                   8208: #
                   8209: # Full or no access?
                   8210: #
1.29      www      8211: 
1.52      www      8212:     if ($thisallowed=~/F/) {
                   8213: 	return 'F';
                   8214:     }
1.29      www      8215: 
1.52      www      8216:     unless ($thisallowed) {
                   8217:         return '';
                   8218:     }
1.29      www      8219: 
1.52      www      8220: # Restrictions exist, deal with them
                   8221: #
                   8222: #   C:according to course preferences
                   8223: #   R:according to resource settings
                   8224: #   L:unless locked
                   8225: #   X:according to user session state
                   8226: #
                   8227: 
                   8228: # Possibly locked functionality, check all courses
1.54      www      8229: # Locks might take effect only after 10 minutes cache expiration for other
                   8230: # courses, and 2 minutes for current course
1.52      www      8231: 
                   8232:     my $envkey;
                   8233:     if ($thisallowed=~/L/) {
1.1000    raeburn  8234:         foreach $envkey (keys(%env)) {
1.54      www      8235:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   8236:                my $courseid=$2;
                   8237:                my $roleid=$1.'.'.$2;
1.92      www      8238:                $courseid=~s/^\///;
1.54      www      8239:                my $expiretime=600;
1.620     albertel 8240:                if ($env{'request.role'} eq $roleid) {
1.54      www      8241: 		  $expiretime=120;
                   8242:                }
                   8243: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   8244:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 8245:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 8246: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      8247:                }
1.620     albertel 8248:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8249:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   8250: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   8251:                        &log($env{'user.domain'},$env{'user.name'},
                   8252:                             $env{'user.home'},
1.57      www      8253:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      8254:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8255:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8256: 		       return '';
                   8257:                    }
                   8258:                }
1.620     albertel 8259:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   8260:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   8261: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   8262:                        &log($env{'user.domain'},$env{'user.name'},
                   8263:                             $env{'user.home'},
1.57      www      8264:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      8265:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 8266:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      8267: 		       return '';
                   8268:                    }
                   8269:                }
                   8270: 	   }
1.29      www      8271:        }
1.52      www      8272:     }
                   8273:    
                   8274: #
                   8275: # Rest of the restrictions depend on selected course
                   8276: #
                   8277: 
1.620     albertel 8278:     unless ($env{'request.course.id'}) {
1.766     albertel 8279: 	if ($thisallowed eq 'A') {
                   8280: 	    return 'A';
1.814     raeburn  8281:         } elsif ($thisallowed eq 'B') {
                   8282:             return 'B';
1.766     albertel 8283: 	} else {
                   8284: 	    return '1';
                   8285: 	}
1.52      www      8286:     }
1.29      www      8287: 
1.52      www      8288: #
                   8289: # Now user is definitely in a course
                   8290: #
1.53      www      8291: 
                   8292: 
                   8293: # Course preferences
                   8294: 
                   8295:    if ($thisallowed=~/C/) {
1.620     albertel 8296:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   8297:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   8298:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 8299: 	   =~/\Q$rolecode\E/) {
1.1304    raeburn  8300: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8301: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8302: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   8303: 			$env{'request.course.id'});
                   8304: 	   }
1.237     www      8305:            return '';
                   8306:        }
                   8307: 
1.620     albertel 8308:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 8309: 	   =~/\Q$unamedom\E/) {
1.1304    raeburn  8310: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 8311: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   8312: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   8313: 			$env{'request.course.id'});
                   8314: 	   }
1.54      www      8315:            return '';
                   8316:        }
1.53      www      8317:    }
                   8318: 
                   8319: # Resource preferences
                   8320: 
                   8321:    if ($thisallowed=~/R/) {
1.620     albertel 8322:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 8323:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  8324: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 8325: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   8326: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   8327: 	   }
                   8328: 	   return '';
1.54      www      8329:        }
1.53      www      8330:    }
1.30      www      8331: 
1.246     www      8332: # Restricted by state or randomout?
1.30      www      8333: 
1.52      www      8334:    if ($thisallowed=~/X/) {
1.620     albertel 8335:       if ($env{'acc.randomout'}) {
1.579     albertel 8336: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 8337:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      8338:             return ''; 
                   8339:          }
1.247     www      8340:       }
                   8341:       if (&condval($statecond)) {
1.52      www      8342: 	 return '2';
                   8343:       } else {
                   8344:          return '';
                   8345:       }
                   8346:    }
1.30      www      8347: 
1.766     albertel 8348:     if ($thisallowed eq 'A') {
                   8349: 	return 'A';
1.814     raeburn  8350:     } elsif ($thisallowed eq 'B') {
                   8351:         return 'B';
1.1402    raeburn  8352:     } elsif ($thisallowed eq 'D') {
                   8353:         return 'D';
1.766     albertel 8354:     }
1.52      www      8355:    return 'F';
1.232     www      8356: }
1.1162    raeburn  8357: 
1.1192    raeburn  8358: # ------------------------------------------- Check construction space access
                   8359: 
                   8360: sub constructaccess {
                   8361:     my ($url,$setpriv)=@_;
                   8362: 
                   8363: # We do not allow editing of previous versions of files
                   8364:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   8365: 
                   8366: # Get username and domain from URL
                   8367:     my ($ownername,$ownerdomain,$ownerhome);
                   8368: 
                   8369:     ($ownerdomain,$ownername) =
1.1325    raeburn  8370:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192    raeburn  8371: 
                   8372: # The URL does not really point to any authorspace, forget it
                   8373:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   8374: 
                   8375: # Now we need to see if the user has access to the authorspace of
                   8376: # $ownername at $ownerdomain
                   8377: 
                   8378:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   8379: # Real author for this?
                   8380:        $ownerhome = $env{'user.home'};
                   8381:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   8382:           return ($ownername,$ownerdomain,$ownerhome);
                   8383:        }
                   8384:     } else {
                   8385: # Co-author for this?
                   8386:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   8387:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   8388:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   8389:             return ($ownername,$ownerdomain,$ownerhome);
                   8390:         }
1.1312    raeburn  8391:         if ($env{'request.course.id'}) {
                   8392:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
                   8393:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
                   8394:                 if (&allowed('mdc',$env{'request.course.id'})) {
                   8395:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
                   8396:                     return ($ownername,$ownerdomain,$ownerhome);
                   8397:                 }
                   8398:             }
                   8399:         }
1.1192    raeburn  8400:     }
                   8401: 
                   8402: # We don't have any access right now. If we are not possibly going to do anything about this,
                   8403: # we might as well leave
                   8404:    unless ($setpriv) { return ''; }
                   8405: 
                   8406: # Backdoor access?
                   8407:     my $allowed=&allowed('eco',$ownerdomain);
                   8408: # Nope
                   8409:     unless ($allowed) { return ''; }
                   8410: # Looks like we may have access, but could be locked by the owner of the construction space
                   8411:     if ($allowed eq 'U') {
                   8412:         my %blocked=&get('environment',['domcoord.author'],
                   8413:                          $ownerdomain,$ownername);
                   8414: # Is blocked by owner
                   8415:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   8416:     }
                   8417:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   8418: # Grant temporary access
                   8419:         my $then=$env{'user.login.time'};
1.1209    raeburn  8420:         my $update=$env{'user.update.time'};
1.1192    raeburn  8421:         if (!$update) { $update = $then; }
                   8422:         my $refresh=$env{'user.refresh.time'};
                   8423:         if (!$refresh) { $refresh = $update; }
                   8424:         my $now = time;
                   8425:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   8426:                            $now,'ca','constructaccess');
                   8427:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   8428:         return($ownername,$ownerdomain,$ownerhome);
                   8429:     }
                   8430: # No business here
                   8431:     return '';
                   8432: }
                   8433: 
1.1282    raeburn  8434: # ----------------------------------------------------------- Content Blocking
                   8435: 
                   8436: {
                   8437: # Caches for faster Course Contents display where content blocking
                   8438: # is in operation (i.e., interval param set) for timed quiz.
                   8439: #
                   8440: # User for whom data are being temporarily cached.
                   8441: my $cacheduser='';
                   8442: # Cached blockers for this user (a hash of blocking items). 
                   8443: my %cachedblockers=();
                   8444: # When the data were last cached.
                   8445: my $cachedlast='';
                   8446: 
                   8447: sub load_all_blockers {
                   8448:     my ($uname,$udom,$blocks)=@_;
                   8449:     if (($uname ne '') && ($udom ne '')) { 
                   8450:         if (($cacheduser eq $uname.':'.$udom) &&
                   8451:             (abs($cachedlast-time)<5)) {
                   8452:             return;
                   8453:         }
                   8454:     }
                   8455:     $cachedlast=time;
                   8456:     $cacheduser=$uname.':'.$udom;
                   8457:     %cachedblockers = &get_commblock_resources($blocks);
                   8458: }
                   8459: 
1.1162    raeburn  8460: sub get_comm_blocks {
                   8461:     my ($cdom,$cnum) = @_;
                   8462:     if ($cdom eq '' || $cnum eq '') {
                   8463:         return unless ($env{'request.course.id'});
                   8464:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8465:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8466:     }
                   8467:     my %commblocks;
                   8468:     my $hashid=$cdom.'_'.$cnum;
                   8469:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   8470:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   8471:         %commblocks = %{$blocksref};
                   8472:     } else {
                   8473:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   8474:         my $cachetime = 600;
                   8475:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   8476:     }
                   8477:     return %commblocks;
                   8478: }
                   8479: 
1.1282    raeburn  8480: sub get_commblock_resources {
                   8481:     my ($blocks) = @_;
                   8482:     my %blockers = ();
                   8483:     return %blockers unless ($env{'request.course.id'});
                   8484:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
1.1162    raeburn  8485:     my %commblocks;
                   8486:     if (ref($blocks) eq 'HASH') {
                   8487:         %commblocks = %{$blocks};
                   8488:     } else {
                   8489:         %commblocks = &get_comm_blocks();
                   8490:     }
1.1282    raeburn  8491:     return %blockers unless (keys(%commblocks) > 0); 
                   8492:     my $navmap = Apache::lonnavmaps::navmap->new();
                   8493:     return %blockers unless (ref($navmap));
1.1162    raeburn  8494:     my $now = time;
                   8495:     foreach my $block (keys(%commblocks)) {
                   8496:         if ($block =~ /^(\d+)____(\d+)$/) {
                   8497:             my ($start,$end) = ($1,$2);
                   8498:             if ($start <= $now && $end >= $now) {
                   8499:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   8500:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   8501:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282    raeburn  8502:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   8503:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
1.1162    raeburn  8504:                             }
                   8505:                         }
                   8506:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282    raeburn  8507:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   8508:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162    raeburn  8509:                             }
                   8510:                         }
                   8511:                     }
                   8512:                 }
                   8513:             }
                   8514:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   8515:             my $item = $1;
1.1163    raeburn  8516:             my @to_test;
1.1162    raeburn  8517:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   8518:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1282    raeburn  8519:                     my @interval;
                   8520:                     my $type = 'map';
                   8521:                     if ($item eq 'course') {
                   8522:                         $type = 'course';
                   8523:                         @interval=&EXT("resource.0.interval");
                   8524:                     } else {
                   8525:                         if ($item =~ /___\d+___/) {
                   8526:                             $type = 'resource';
                   8527:                             @interval=&EXT("resource.0.interval",$item);
                   8528:                             if (ref($navmap)) {                        
                   8529:                                 my $res = $navmap->getBySymb($item); 
                   8530:                                 push(@to_test,$res);
                   8531:                             }
1.1162    raeburn  8532:                         } else {
1.1282    raeburn  8533:                             my $mapsymb = &symbread($item,1);
                   8534:                             if ($mapsymb) {
                   8535:                                 if (ref($navmap)) {
                   8536:                                     my $mapres = $navmap->getBySymb($mapsymb);
                   8537:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
                   8538:                                     foreach my $res (@to_test) {
                   8539:                                         my $symb = $res->symb();
                   8540:                                         next if ($symb eq $mapsymb);
                   8541:                                         if ($symb ne '') {
                   8542:                                             @interval=&EXT("resource.0.interval",$symb);
                   8543:                                             if ($interval[1] eq 'map') {
                   8544:                                                 last;
1.1162    raeburn  8545:                                             }
                   8546:                                         }
                   8547:                                     }
                   8548:                                 }
                   8549:                             }
                   8550:                         }
1.1282    raeburn  8551:                     }
1.1310    raeburn  8552:                     if ($interval[0] =~ /^(\d+)/) {
1.1308    raeburn  8553:                         my $timelimit = $1; 
1.1282    raeburn  8554:                         my $first_access;
                   8555:                         if ($type eq 'resource') {
                   8556:                             $first_access=&get_first_access($interval[1],$item);
                   8557:                         } elsif ($type eq 'map') {
                   8558:                             $first_access=&get_first_access($interval[1],undef,$item);
                   8559:                         } else {
                   8560:                             $first_access=&get_first_access($interval[1]);
                   8561:                         }
                   8562:                         if ($first_access) {
1.1292    raeburn  8563:                             my $timesup = $first_access+$timelimit;
1.1282    raeburn  8564:                             if ($timesup > $now) {
                   8565:                                 my $activeblock;
                   8566:                                 foreach my $res (@to_test) {
1.1283    raeburn  8567:                                     if ($res->answerable()) {
1.1282    raeburn  8568:                                         $activeblock = 1;
                   8569:                                         last;
                   8570:                                     }
                   8571:                                 }
                   8572:                                 if ($activeblock) {
                   8573:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   8574:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   8575:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
                   8576:                                          }
                   8577:                                     }
                   8578:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   8579:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   8580:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163    raeburn  8581:                                         }
1.1162    raeburn  8582:                                     }
                   8583:                                 }
                   8584:                             }
                   8585:                         }
                   8586:                     }
                   8587:                 }
                   8588:             }
                   8589:         }
                   8590:     }
1.1282    raeburn  8591:     return %blockers;
1.1162    raeburn  8592: }
                   8593: 
1.1282    raeburn  8594: sub has_comm_blocking {
                   8595:     my ($priv,$symb,$uri,$blocks) = @_;
                   8596:     my @blockers;
                   8597:     return unless ($env{'request.course.id'});
                   8598:     return unless ($priv eq 'bre');
                   8599:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
                   8600:     return if ($env{'request.state'} eq 'construct');
                   8601:     &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
                   8602:     return unless (keys(%cachedblockers) > 0);
                   8603:     my (%possibles,@symbs);
                   8604:     if (!$symb) {
                   8605:         $symb = &symbread($uri,1,1,1,\%possibles);
1.1162    raeburn  8606:     }
1.1282    raeburn  8607:     if ($symb) {
                   8608:         @symbs = ($symb);
                   8609:     } elsif (keys(%possibles)) { 
                   8610:         @symbs = keys(%possibles);
                   8611:     }
                   8612:     my $noblock;
                   8613:     foreach my $symb (@symbs) {
                   8614:         last if ($noblock);
                   8615:         my ($map,$resid,$resurl)=&decode_symb($symb);
                   8616:         foreach my $block (keys(%cachedblockers)) {
                   8617:             if ($block =~ /^firstaccess____(.+)$/) {
                   8618:                 my $item = $1;
                   8619:                 if (($item eq $map) || ($item eq $symb)) {
                   8620:                     $noblock = 1;
                   8621:                     last;
                   8622:                 }
                   8623:             }
                   8624:             if (ref($cachedblockers{$block}) eq 'HASH') {
                   8625:                 if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
                   8626:                     if ($cachedblockers{$block}{'resources'}{$symb}) {
                   8627:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   8628:                             push(@blockers,$block);
                   8629:                         }
                   8630:                     }
                   8631:                 }
1.1162    raeburn  8632:             }
1.1282    raeburn  8633:             if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
                   8634:                 if ($cachedblockers{$block}{'maps'}{$map}) {
                   8635:                     unless (grep(/^\Q$block\E$/,@blockers)) {
                   8636:                         push(@blockers,$block);
                   8637:                     }
                   8638:                 }
1.1162    raeburn  8639:             }
                   8640:         }
                   8641:     }
1.1282    raeburn  8642:     return if ($noblock);
                   8643:     return @blockers;
                   8644: }
1.1162    raeburn  8645: }
                   8646: 
1.1402    raeburn  8647: sub deeplink_check {
                   8648:     my ($priv,$symb,$uri) = @_;
                   8649:     return unless ($env{'request.course.id'});
                   8650:     return unless ($priv eq 'bre');
                   8651:     return if ($env{'request.state'} eq 'construct');
                   8652:     return if ($env{'request.role.adv'});
                   8653:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8654:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8655:     my (%possibles,@symbs);
                   8656:     if (!$symb) {
                   8657:         $symb = &symbread($uri,1,1,1,\%possibles);
                   8658:     }
                   8659:     if ($symb) {
                   8660:         @symbs = ($symb);
                   8661:     } elsif (keys(%possibles)) {
                   8662:         @symbs = keys(%possibles);
                   8663:     }
                   8664: 
                   8665:     my ($login,$switchrole,$allow);
                   8666:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
                   8667:         my $key = $1;
                   8668:         my $tinyurl;
                   8669:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
                   8670:         if (defined($cached)) {
                   8671:              $tinyurl = $result;
                   8672:         } else {
                   8673:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
                   8674:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
                   8675:              if ($currtiny{$key} ne '') {
                   8676:                  $tinyurl = $currtiny{$key};
                   8677:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
                   8678:              }
                   8679:         }
                   8680:         if ($tinyurl ne '') {
                   8681:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
                   8682:             if ($cnumreq eq $cnum) {
                   8683:                 $login = $posslogin;
                   8684:             } else {
                   8685:                 $switchrole = 1;
                   8686:             }
                   8687:         }
                   8688:     }
                   8689:     foreach my $symb (@symbs) {
                   8690:         last if ($allow);
                   8691:         my $deeplink = &EXT("resource.0.deeplink",$symb);
                   8692:         if ($deeplink eq '') {
                   8693:             $allow = 1;
                   8694:         } else {
                   8695:             my ($listed,$scope,$access) = split(/,/,$deeplink);
                   8696:             if ($access eq 'any') {
                   8697:                 $allow = 1;
                   8698:             } elsif ($login) {
                   8699:                 if ($access eq 'only') {
                   8700:                     if ($scope eq 'res') {
                   8701:                         if ($symb eq $login) {
                   8702:                             $allow = 1;
                   8703:                         }
                   8704:                     } elsif ($scope eq 'map') {
                   8705: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
                   8706:                     } elsif ($scope eq 'rec') {
                   8707: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
                   8708:                     }
                   8709:                 } else {
                   8710:                     my ($acctype,$item) = split(/:/,$access);
                   8711:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
                   8712:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
                   8713:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
                   8714:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
                   8715:                                 $allow = 1;
                   8716:                             }
                   8717:                         }
                   8718:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
                   8719:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
                   8720:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
                   8721:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
                   8722:                                 $allow = 1;
                   8723:                             }
                   8724:                         }
                   8725:                     }
                   8726:                 }
                   8727:             }
                   8728:         }
                   8729:     }
                   8730:     return if ($allow);
                   8731:     return 1;
                   8732: }
                   8733: 
1.1282    raeburn  8734: # -------------------------------- Deversion and split uri into path an filename   
                   8735: 
1.1133    foxr     8736: #
1.1282    raeburn  8737: #   Removes the version from a URI and
1.1133    foxr     8738: #   splits it in to its filename and path to the filename.
                   8739: #   Seems like File::Basename could have done this more clearly.
                   8740: #   Parameters:
                   8741: #      $uri   - input URI
                   8742: #   Returns:
                   8743: #     Two element list consisting of 
                   8744: #     $pathname  - the URI up to and excluding the trailing /
                   8745: #     $filename  - The part of the URI following the last /
                   8746: #  NOTE:
                   8747: #    Another realization of this is simply:
                   8748: #    use File::Basename;
                   8749: #    ...
                   8750: #    $uri = shift;
                   8751: #    $filename = basename($uri);
                   8752: #    $path     = dirname($uri);
                   8753: #    return ($filename, $path);
                   8754: #
                   8755: #     The implementation below is probably faster however.
                   8756: #
1.710     albertel 8757: sub split_uri_for_cond {
                   8758:     my $uri=&deversion(&declutter(shift));
                   8759:     my @uriparts=split(/\//,$uri);
                   8760:     my $filename=pop(@uriparts);
                   8761:     my $pathname=join('/',@uriparts);
                   8762:     return ($pathname,$filename);
                   8763: }
1.232     www      8764: # --------------------------------------------------- Is a resource on the map?
                   8765: 
                   8766: sub is_on_map {
1.710     albertel 8767:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 8768:     #Trying to find the conditional for the file
1.620     albertel 8769:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 8770: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      8771:     if ($match) {
1.289     bowersj2 8772: 	return (1,$1);
                   8773:     } else {
1.434     www      8774: 	return (0,0);
1.289     bowersj2 8775:     }
1.12      www      8776: }
                   8777: 
1.427     www      8778: # --------------------------------------------------------- Get symb from alias
                   8779: 
                   8780: sub get_symb_from_alias {
                   8781:     my $symb=shift;
                   8782:     my ($map,$resid,$url)=&decode_symb($symb);
                   8783: # Already is a symb
                   8784:     if ($url) { return $symb; }
                   8785: # Must be an alias
                   8786:     my $aliassymb='';
                   8787:     my %bighash;
1.620     albertel 8788:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      8789:                             &GDBM_READER(),0640)) {
                   8790:         my $rid=$bighash{'mapalias_'.$symb};
                   8791: 	if ($rid) {
                   8792: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 8793: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   8794: 				    $resid,$bighash{'src_'.$rid});
1.427     www      8795: 	}
                   8796:         untie %bighash;
                   8797:     }
                   8798:     return $aliassymb;
                   8799: }
                   8800: 
1.12      www      8801: # ----------------------------------------------------------------- Define Role
                   8802: 
                   8803: sub definerole {
                   8804:   if (allowed('mcr','/')) {
1.1326    raeburn  8805:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800     albertel 8806:     foreach my $role (split(':',$sysrole)) {
                   8807: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8808:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   8809:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   8810: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      8811:                return "refused:s:$crole&$cqual"; 
                   8812:             }
                   8813:         }
1.191     harris41 8814:     }
1.800     albertel 8815:     foreach my $role (split(':',$domrole)) {
                   8816: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8817:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   8818:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   8819: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      8820:                return "refused:d:$crole&$cqual"; 
                   8821:             }
                   8822:         }
1.191     harris41 8823:     }
1.800     albertel 8824:     foreach my $role (split(':',$courole)) {
                   8825: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8826:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   8827:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   8828: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      8829:                return "refused:c:$crole&$cqual"; 
                   8830:             }
                   8831:         }
1.191     harris41 8832:     }
1.1326    raeburn  8833:     my $uhome;
                   8834:     if (($uname ne '') && ($udom ne '')) {
                   8835:         $uhome = &homeserver($uname,$udom);
                   8836:         return $uhome if ($uhome eq 'no_host');
                   8837:     } else {
                   8838:         $uname = $env{'user.name'};
                   8839:         $udom = $env{'user.domain'};
                   8840:         $uhome = $env{'user.home'};
                   8841:     }
1.620     albertel 8842:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326    raeburn  8843:                 "$udom:$uname:rolesdef_$rolename=".
1.21      www      8844:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326    raeburn  8845:     return reply($command,$uhome);
1.12      www      8846:   } else {
                   8847:     return 'refused';
                   8848:   }
1.105     harris41 8849: }
                   8850: 
                   8851: # ---------------- Make a metadata query against the network of library servers
                   8852: 
                   8853: sub metadata_query {
1.1237    raeburn  8854:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120     harris41 8855:     my %rhash;
1.845     albertel 8856:     my %libserv = &all_library();
1.244     matthew  8857:     my @server_list = (defined($server_array) ? @$server_array
                   8858:                                               : keys(%libserv) );
                   8859:     for my $server (@server_list) {
1.1237    raeburn  8860:         my $domains = ''; 
                   8861:         if (ref($domains_hash) eq 'HASH') {
                   8862:             $domains = $domains_hash->{$server}; 
                   8863:         }
1.118     harris41 8864: 	unless ($custom or $customshow) {
1.1237    raeburn  8865: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118     harris41 8866: 	    $rhash{$server}=$reply;
                   8867: 	}
                   8868: 	else {
                   8869: 	    my $reply=&reply("querysend:".&escape($query).':'.
1.1237    raeburn  8870: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118     harris41 8871: 			     $server);
                   8872: 	    $rhash{$server}=$reply;
                   8873: 	}
1.112     harris41 8874:     }
1.118     harris41 8875:     return \%rhash;
1.240     www      8876: }
                   8877: 
                   8878: # ----------------------------------------- Send log queries and wait for reply
                   8879: 
                   8880: sub log_query {
                   8881:     my ($uname,$udom,$query,%filters)=@_;
                   8882:     my $uhome=&homeserver($uname,$udom);
                   8883:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 8884:     my $uhost=&hostname($uhome);
1.800     albertel 8885:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      8886:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   8887:                        $uhome);
1.479     albertel 8888:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      8889:     return get_query_reply($queryid);
                   8890: }
                   8891: 
1.818     raeburn  8892: # -------------------------- Update MySQL table for portfolio file
                   8893: 
                   8894: sub update_portfolio_table {
1.821     raeburn  8895:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  8896:     if ($group ne '') {
                   8897:         $file_name =~s /^\Q$group\E//;
                   8898:     }
1.818     raeburn  8899:     my $homeserver = &homeserver($uname,$udom);
                   8900:     my $queryid=
1.821     raeburn  8901:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   8902:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  8903:     my $reply = &get_query_reply($queryid);
                   8904:     return $reply;
                   8905: }
                   8906: 
1.899     raeburn  8907: # -------------------------- Update MySQL allusers table
                   8908: 
                   8909: sub update_allusers_table {
                   8910:     my ($uname,$udom,$names) = @_;
                   8911:     my $homeserver = &homeserver($uname,$udom);
                   8912:     my $queryid=
                   8913:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   8914:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   8915:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   8916:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   8917:                'generation='.&escape($names->{'generation'}).'%%'.
                   8918:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   8919:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  8920:     return;
1.899     raeburn  8921: }
                   8922: 
1.508     raeburn  8923: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  8924: 
                   8925: sub fetch_enrollment_query {
1.511     raeburn  8926:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317    raeburn  8927:     my ($homeserver,$sleep,$loopmax);
1.547     raeburn  8928:     my $maxtries = 1;
1.508     raeburn  8929:     if ($context eq 'automated') {
                   8930:         $homeserver = $perlvar{'lonHostID'};
1.1317    raeburn  8931:         $sleep = 2;
                   8932:         $loopmax = 100;
1.547     raeburn  8933:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  8934:     } else {
                   8935:         $homeserver = &homeserver($cnum,$dom);
                   8936:     }
1.838     albertel 8937:     my $host=&hostname($homeserver);
1.506     raeburn  8938:     my $cmd = '';
1.1000    raeburn  8939:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 8940:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  8941:     }
                   8942:     $cmd =~ s/%%$//;
                   8943:     $cmd = &escape($cmd);
                   8944:     my $query = 'fetchenrollment';
1.620     albertel 8945:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  8946:     unless ($queryid=~/^\Q$host\E\_/) { 
                   8947:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   8948:         return 'error: '.$queryid;
                   8949:     }
1.1317    raeburn  8950:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  8951:     my $tries = 1;
                   8952:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317    raeburn  8953:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  8954:         $tries ++;
                   8955:     }
1.526     raeburn  8956:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 8957:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  8958:     } else {
1.901     albertel 8959:         my @responses = split(/:/,$reply);
1.1322    raeburn  8960:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
1.800     albertel 8961:             foreach my $line (@responses) {
                   8962:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  8963:                 $$replyref{$key} = $value;
                   8964:             }
                   8965:         } else {
1.1117    foxr     8966:             my $pathname = LONCAPA::tempdir();
1.800     albertel 8967:             foreach my $line (@responses) {
                   8968:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  8969:                 $$replyref{$key} = $value;
                   8970:                 if ($value > 0) {
1.800     albertel 8971:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   8972:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  8973:                         my $destname = $pathname.'/'.$filename;
                   8974:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  8975:                         if ($xml_classlist =~ /^error/) {
                   8976:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   8977:                         } else {
1.1359    raeburn  8978:                             if ( open(FILE,">",$destname) ) {
1.506     raeburn  8979:                                 print FILE &unescape($xml_classlist);
                   8980:                                 close(FILE);
1.526     raeburn  8981:                             } else {
                   8982:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  8983:                             }
                   8984:                         }
                   8985:                     }
                   8986:                 }
                   8987:             }
                   8988:         }
                   8989:         return 'ok';
                   8990:     }
                   8991:     return 'error';
                   8992: }
                   8993: 
1.242     www      8994: sub get_query_reply {
1.1317    raeburn  8995:     my ($queryid,$sleep,$loopmax) = @_;;
                   8996:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
                   8997:         $sleep = 0.2;
                   8998:     }
                   8999:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
                   9000:         $loopmax = 100;
                   9001:     }
1.1117    foxr     9002:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      9003:     my $reply='';
1.1317    raeburn  9004:     for (1..$loopmax) {
                   9005: 	sleep($sleep);
1.240     www      9006:         if (-e $replyfile.'.end') {
1.1359    raeburn  9007: 	    if (open(my $fh,"<",$replyfile)) {
1.904     albertel 9008: 		$reply = join('',<$fh>);
                   9009: 		close($fh);
1.240     www      9010: 	   } else { return 'error: reply_file_error'; }
1.242     www      9011:            return &unescape($reply);
                   9012: 	}
1.240     www      9013:     }
1.242     www      9014:     return 'timeout:'.$queryid;
1.240     www      9015: }
                   9016: 
                   9017: sub courselog_query {
1.241     www      9018: #
                   9019: # possible filters:
                   9020: # url: url or symb
                   9021: # username
                   9022: # domain
                   9023: # action: view, submit, grade
                   9024: # start: timestamp
                   9025: # end: timestamp
                   9026: #
1.240     www      9027:     my (%filters)=@_;
1.620     albertel 9028:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      9029:     if ($filters{'url'}) {
                   9030: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   9031:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   9032:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   9033:     }
1.620     albertel 9034:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   9035:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      9036:     return &log_query($cname,$cdom,'courselog',%filters);
                   9037: }
                   9038: 
                   9039: sub userlog_query {
1.858     raeburn  9040: #
                   9041: # possible filters:
                   9042: # action: log check role
                   9043: # start: timestamp
                   9044: # end: timestamp
                   9045: #
1.240     www      9046:     my ($uname,$udom,%filters)=@_;
                   9047:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      9048: }
                   9049: 
1.506     raeburn  9050: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   9051: 
                   9052: sub auto_run {
1.508     raeburn  9053:     my ($cnum,$cdom) = @_;
1.876     raeburn  9054:     my $response = 0;
                   9055:     my $settings;
                   9056:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   9057:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   9058:         $settings = $domconfig{'autoenroll'};
                   9059:         if ($settings->{'run'} eq '1') {
                   9060:             $response = 1;
                   9061:         }
                   9062:     } else {
1.934     raeburn  9063:         my $homeserver;
                   9064:         if (&is_course($cdom,$cnum)) {
                   9065:             $homeserver = &homeserver($cnum,$cdom);
                   9066:         } else {
                   9067:             $homeserver = &domain($cdom,'primary');
                   9068:         }
                   9069:         if ($homeserver ne 'no_host') {
                   9070:             $response = &reply('autorun:'.$cdom,$homeserver);
                   9071:         }
1.876     raeburn  9072:     }
1.506     raeburn  9073:     return $response;
                   9074: }
1.776     albertel 9075: 
1.506     raeburn  9076: sub auto_get_sections {
1.508     raeburn  9077:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  9078:     my $homeserver;
                   9079:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   9080:         $homeserver = &homeserver($cnum,$cdom);
                   9081:     }
                   9082:     if (!defined($homeserver)) { 
                   9083:         if ($cdom =~ /^$match_domain$/) {
                   9084:             $homeserver = &domain($cdom,'primary');
                   9085:         }
                   9086:     }
                   9087:     my @secs;
                   9088:     if (defined($homeserver)) {
                   9089:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   9090:         unless ($response eq 'refused') {
                   9091:             @secs = split(/:/,$response);
                   9092:         }
1.506     raeburn  9093:     }
                   9094:     return @secs;
                   9095: }
1.776     albertel 9096: 
1.506     raeburn  9097: sub auto_new_course {
1.1099    raeburn  9098:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  9099:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  9100:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  9101:     return $response;
                   9102: }
1.776     albertel 9103: 
1.506     raeburn  9104: sub auto_validate_courseID {
1.508     raeburn  9105:     my ($cnum,$cdom,$inst_course_id) = @_;
                   9106:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  9107:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  9108:     return $response;
                   9109: }
1.776     albertel 9110: 
1.1007    raeburn  9111: sub auto_validate_instcode {
1.1020    raeburn  9112:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  9113:     my ($homeserver,$response);
                   9114:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9115:         $homeserver = &homeserver($cnum,$cdom);
                   9116:     }
                   9117:     if (!defined($homeserver)) {
                   9118:         if ($cdom =~ /^$match_domain$/) {
                   9119:             $homeserver = &domain($cdom,'primary');
                   9120:         }
                   9121:     }
1.1065    raeburn  9122:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   9123:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  9124:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   9125:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  9126: }
                   9127: 
1.506     raeburn  9128: sub auto_create_password {
1.873     raeburn  9129:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   9130:     my ($homeserver,$response);
1.506     raeburn  9131:     my $create_passwd = 0;
                   9132:     my $authchk = '';
1.873     raeburn  9133:     if ($udom =~ /^$match_domain$/) {
                   9134:         $homeserver = &domain($udom,'primary');
                   9135:     }
                   9136:     if ($homeserver eq '') {
                   9137:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   9138:             $homeserver = &homeserver($cnum,$cdom);
                   9139:         }
                   9140:     }
                   9141:     if ($homeserver eq '') {
                   9142:         $authchk = 'nodomain';
1.506     raeburn  9143:     } else {
1.873     raeburn  9144:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   9145:         if ($response eq 'refused') {
                   9146:             $authchk = 'refused';
                   9147:         } else {
1.901     albertel 9148:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  9149:         }
1.506     raeburn  9150:     }
                   9151:     return ($authparam,$create_passwd,$authchk);
                   9152: }
                   9153: 
1.706     raeburn  9154: sub auto_photo_permission {
                   9155:     my ($cnum,$cdom,$students) = @_;
                   9156:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 9157:     my ($outcome,$perm_reqd,$conditions) = 
                   9158: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 9159:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9160: 	return (undef,undef);
                   9161:     }
1.706     raeburn  9162:     return ($outcome,$perm_reqd,$conditions);
                   9163: }
                   9164: 
                   9165: sub auto_checkphotos {
                   9166:     my ($uname,$udom,$pid) = @_;
                   9167:     my $homeserver = &homeserver($uname,$udom);
                   9168:     my ($result,$resulttype);
                   9169:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 9170: 				   &escape($uname).':'.&escape($pid),
                   9171: 				   $homeserver));
1.709     albertel 9172:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9173: 	return (undef,undef);
                   9174:     }
1.706     raeburn  9175:     if ($outcome) {
                   9176:         ($result,$resulttype) = split(/:/,$outcome);
                   9177:     } 
                   9178:     return ($result,$resulttype);
                   9179: }
                   9180: 
                   9181: sub auto_photochoice {
                   9182:     my ($cnum,$cdom) = @_;
                   9183:     my $homeserver = &homeserver($cnum,$cdom);
                   9184:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 9185: 						       &escape($cdom),
                   9186: 						       $homeserver)));
1.709     albertel 9187:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   9188: 	return (undef,undef);
                   9189:     }
1.706     raeburn  9190:     return ($update,$comment);
                   9191: }
                   9192: 
                   9193: sub auto_photoupdate {
                   9194:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   9195:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 9196:     my $host=&hostname($homeserver);
1.706     raeburn  9197:     my $cmd = '';
                   9198:     my $maxtries = 1;
1.800     albertel 9199:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   9200:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  9201:     }
                   9202:     $cmd =~ s/%%$//;
                   9203:     $cmd = &escape($cmd);
                   9204:     my $query = 'institutionalphotos';
                   9205:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   9206:     unless ($queryid=~/^\Q$host\E\_/) {
                   9207:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   9208:         return 'error: '.$queryid;
                   9209:     }
                   9210:     my $reply = &get_query_reply($queryid);
                   9211:     my $tries = 1;
                   9212:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   9213:         $reply = &get_query_reply($queryid);
                   9214:         $tries ++;
                   9215:     }
                   9216:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   9217:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   9218:     } else {
                   9219:         my @responses = split(/:/,$reply);
                   9220:         my $outcome = shift(@responses); 
                   9221:         foreach my $item (@responses) {
                   9222:             my ($key,$value) = split(/=/,$item);
                   9223:             $$photo{$key} = $value;
                   9224:         }
                   9225:         return $outcome;
                   9226:     }
                   9227:     return 'error';
                   9228: }
                   9229: 
1.521     raeburn  9230: sub auto_instcode_format {
1.793     albertel 9231:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   9232: 	$cat_order) = @_;
1.521     raeburn  9233:     my $courses = '';
1.772     raeburn  9234:     my @homeservers;
1.521     raeburn  9235:     if ($caller eq 'global') {
1.841     albertel 9236: 	my %servers = &get_servers($codedom,'library');
                   9237: 	foreach my $tryserver (keys(%servers)) {
                   9238: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9239: 		push(@homeservers,$tryserver);
                   9240: 	    }
1.584     raeburn  9241:         }
1.1022    raeburn  9242:     } elsif ($caller eq 'requests') {
                   9243:         if ($codedom =~ /^$match_domain$/) {
                   9244:             my $chome = &domain($codedom,'primary');
                   9245:             unless ($chome eq 'no_host') {
                   9246:                 push(@homeservers,$chome);
                   9247:             }
                   9248:         }
1.521     raeburn  9249:     } else {
1.772     raeburn  9250:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  9251:     }
1.793     albertel 9252:     foreach my $code (keys(%{$instcodes})) {
                   9253:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  9254:     }
                   9255:     chop($courses);
1.772     raeburn  9256:     my $ok_response = 0;
                   9257:     my $response;
                   9258:     while (@homeservers > 0 && $ok_response == 0) {
                   9259:         my $server = shift(@homeservers); 
                   9260:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   9261:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   9262:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 9263: 		split(/:/,$response);
1.772     raeburn  9264:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   9265:             push(@{$codetitles},&str2array($codetitles_str));
                   9266:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   9267:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   9268:             $ok_response = 1;
                   9269:         }
                   9270:     }
                   9271:     if ($ok_response) {
1.521     raeburn  9272:         return 'ok';
1.772     raeburn  9273:     } else {
                   9274:         return $response;
1.521     raeburn  9275:     }
                   9276: }
                   9277: 
1.792     raeburn  9278: sub auto_instcode_defaults {
                   9279:     my ($domain,$returnhash,$code_order) = @_;
                   9280:     my @homeservers;
1.841     albertel 9281: 
                   9282:     my %servers = &get_servers($domain,'library');
                   9283:     foreach my $tryserver (keys(%servers)) {
                   9284: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9285: 	    push(@homeservers,$tryserver);
                   9286: 	}
1.792     raeburn  9287:     }
1.841     albertel 9288: 
1.792     raeburn  9289:     my $response;
1.841     albertel 9290:     foreach my $server (@homeservers) {
1.792     raeburn  9291:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 9292:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   9293: 	
                   9294: 	foreach my $pair (split(/\&/,$response)) {
                   9295: 	    my ($name,$value)=split(/\=/,$pair);
                   9296: 	    if ($name eq 'code_order') {
                   9297: 		@{$code_order} = split(/\&/,&unescape($value));
                   9298: 	    } else {
                   9299: 		$returnhash->{&unescape($name)}=&unescape($value);
                   9300: 	    }
                   9301: 	}
                   9302: 	return 'ok';
1.792     raeburn  9303:     }
1.841     albertel 9304: 
                   9305:     return $response;
1.1003    raeburn  9306: }
                   9307: 
                   9308: sub auto_possible_instcodes {
1.1007    raeburn  9309:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   9310:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   9311:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   9312:         return;
                   9313:     }
1.1003    raeburn  9314:     my (@homeservers,$uhome);
                   9315:     if (defined(&domain($domain,'primary'))) {
                   9316:         $uhome=&domain($domain,'primary');
                   9317:         push(@homeservers,&domain($domain,'primary'));
                   9318:     } else {
                   9319:         my %servers = &get_servers($domain,'library');
                   9320:         foreach my $tryserver (keys(%servers)) {
                   9321:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   9322:                 push(@homeservers,$tryserver);
                   9323:             }
                   9324:         }
                   9325:     }
                   9326:     my $response;
                   9327:     foreach my $server (@homeservers) {
                   9328:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   9329:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  9330:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   9331:             split(':',$response);
                   9332:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   9333:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  9334:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  9335:             my ($name,$value)=split('=',$item);
                   9336:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  9337:         }
                   9338:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  9339:             my ($name,$value)=split('=',$item);
                   9340:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  9341:         }
                   9342:         return 'ok';
                   9343:     }
                   9344:     return $response;
                   9345: }
1.792     raeburn  9346: 
1.1010    raeburn  9347: sub auto_courserequest_checks {
                   9348:     my ($dom) = @_;
1.1020    raeburn  9349:     my ($homeserver,%validations);
                   9350:     if ($dom =~ /^$match_domain$/) {
                   9351:         $homeserver = &domain($dom,'primary');
                   9352:     }
                   9353:     unless ($homeserver eq 'no_host') {
                   9354:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   9355:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9356:             my @items = split(/&/,$response);
                   9357:             foreach my $item (@items) {
                   9358:                 my ($key,$value) = split('=',$item);
                   9359:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   9360:             }
                   9361:         }
                   9362:     }
1.1010    raeburn  9363:     return %validations; 
                   9364: }
                   9365: 
1.1020    raeburn  9366: sub auto_courserequest_validation {
1.1255    raeburn  9367:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020    raeburn  9368:     my ($homeserver,$response);
                   9369:     if ($dom =~ /^$match_domain$/) {
                   9370:         $homeserver = &domain($dom,'primary');
                   9371:     }
1.1255    raeburn  9372:     unless ($homeserver eq 'no_host') {
                   9373:         my $customdata;
                   9374:         if (ref($custominfo) eq 'HASH') {
                   9375:             $customdata = &freeze_escape($custominfo);
                   9376:         }
1.1020    raeburn  9377:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  9378:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255    raeburn  9379:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
                   9380:                                     $customdata,$homeserver));
1.1020    raeburn  9381:     }
                   9382:     return $response;
                   9383: }
                   9384: 
1.777     albertel 9385: sub auto_validate_class_sec {
1.918     raeburn  9386:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  9387:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  9388:     my $ownerlist;
                   9389:     if (ref($owners) eq 'ARRAY') {
                   9390:         $ownerlist = join(',',@{$owners});
                   9391:     } else {
                   9392:         $ownerlist = $owners;
                   9393:     }
1.773     raeburn  9394:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  9395:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  9396:     return $response;
                   9397: }
                   9398: 
1.1367    raeburn  9399: sub auto_validate_instclasses {
                   9400:     my ($cdom,$cnum,$owners,$classesref) = @_;
                   9401:     my ($homeserver,%validations);
                   9402:     $homeserver = &homeserver($cnum,$cdom);
                   9403:     unless ($homeserver eq 'no_host') {
                   9404:         my $ownerlist;
                   9405:         if (ref($owners) eq 'ARRAY') {
                   9406:             $ownerlist = join(',',@{$owners});
                   9407:         } else {
                   9408:             $ownerlist = $owners;
                   9409:         }
                   9410:         if (ref($classesref) eq 'HASH') {
                   9411:             my $classes = &freeze_escape($classesref);
                   9412:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
                   9413:                                 ':'.$cdom.':'.$classes,$homeserver);
                   9414:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9415:                 my @items = split(/&/,$response);
                   9416:                 foreach my $item (@items) {
                   9417:                     my ($key,$value) = split('=',$item);
                   9418:                     $validations{&unescape($key)} = &thaw_unescape($value);
                   9419:                 }
                   9420:             }
                   9421:         }
                   9422:     }
                   9423:     return %validations;
                   9424: }
                   9425: 
1.1248    raeburn  9426: sub auto_crsreq_update {
                   9427:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257    raeburn  9428:         $code,$accessstart,$accessend,$inbound) = @_;
1.1248    raeburn  9429:     my ($homeserver,%crsreqresponse);
                   9430:     if ($cdom =~ /^$match_domain$/) {
                   9431:         $homeserver = &domain($cdom,'primary');
                   9432:     }
                   9433:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   9434:         my $info;
                   9435:         if (ref($inbound) eq 'HASH') {
                   9436:             $info = &freeze_escape($inbound);
                   9437:         }
                   9438:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
                   9439:                             ':'.&escape($action).':'.&escape($ownername).':'.
                   9440:                             &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257    raeburn  9441:                             &escape($title).':'.&escape($code).':'.
                   9442:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
                   9443:                             $homeserver);
1.1248    raeburn  9444:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   9445:             my @items = split(/&/,$response);
                   9446:             foreach my $item (@items) {
                   9447:                 my ($key,$value) = split('=',$item);
                   9448:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
                   9449:             }
                   9450:         }
                   9451:     }
                   9452:     return \%crsreqresponse;
                   9453: }
                   9454: 
1.1305    raeburn  9455: sub auto_export_grades {
1.1309    raeburn  9456:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
                   9457:     my ($homeserver,%exportresponse);
                   9458:     if ($cdom =~ /^$match_domain$/) {
                   9459:         $homeserver = &domain($cdom,'primary');
                   9460:     }
                   9461:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   9462:         my $info;
                   9463:         if (ref($inforef) eq 'HASH') {
                   9464:             $info = &freeze_escape($inforef);
                   9465:         }
                   9466:         if (ref($gradesref) eq 'HASH') {
                   9467:             my $grades = &freeze_escape($gradesref);
                   9468:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
                   9469:                                 $info.':'.$grades,$homeserver);
                   9470:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
                   9471:                 my @items = split(/&/,$response);
                   9472:                 foreach my $item (@items) {
                   9473:                     my ($key,$value) = split('=',$item);
                   9474:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
                   9475:                 }
                   9476:             }
                   9477:         }
                   9478:     }
                   9479:     return \%exportresponse;
1.1305    raeburn  9480: }
                   9481: 
1.1287    raeburn  9482: sub check_instcode_cloning {
                   9483:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
                   9484:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   9485:         return;
                   9486:     }
                   9487:     my $canclone;
                   9488:     if (@{$code_order} > 0) {
                   9489:         my $instcoderegexp ='^';
                   9490:         my @clonecodes = split(/\&/,$cloner);
                   9491:         foreach my $item (@{$code_order}) {
                   9492:             if (grep(/^\Q$item\E=/,@clonecodes)) {
                   9493:                 foreach my $pair (@clonecodes) {
                   9494:                     my ($key,$val) = split(/\=/,$pair,2);
                   9495:                     $val = &unescape($val);
                   9496:                     if ($key eq $item) {
                   9497:                         $instcoderegexp .= '('.$val.')';
                   9498:                         last;
                   9499:                     }
                   9500:                 }
                   9501:             } else {
                   9502:                 $instcoderegexp .= $codedefaults->{$item};
                   9503:             }
                   9504:         }
                   9505:         $instcoderegexp .= '$';
                   9506:         my (@from,@to);
                   9507:         eval {
                   9508:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   9509:                (@to) = ($clonetocode =~ /$instcoderegexp/);
                   9510:         };
                   9511:         if ((@from > 0) && (@to > 0)) {
                   9512:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   9513:             if (!@diffs) {
                   9514:                 $canclone = 1;
                   9515:             }
                   9516:         }
                   9517:     }
                   9518:     return $canclone;
                   9519: }
                   9520: 
                   9521: sub default_instcode_cloning {
                   9522:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
                   9523:     my (%codedefaults,@code_order,$canclone);
                   9524:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
                   9525:         %codedefaults = %{$codedefaultsref};
                   9526:         @code_order = @{$codeorderref};
                   9527:     } elsif ($clonedom) {
                   9528:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
                   9529:     }
                   9530:     if (($domdefclone) && (@code_order)) {
                   9531:         my @clonecodes = split(/\+/,$domdefclone);
                   9532:         my $instcoderegexp ='^';
                   9533:         foreach my $item (@code_order) {
                   9534:             if (grep(/^\Q$item\E$/,@clonecodes)) {
                   9535:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
                   9536:             } else {
                   9537:                 $instcoderegexp .= $codedefaults{$item};
                   9538:             }
                   9539:         }
                   9540:         $instcoderegexp .= '$';
                   9541:         my (@from,@to);
                   9542:         eval {
                   9543:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   9544:             (@to) = ($clonetocode =~ /$instcoderegexp/);
                   9545:         };
                   9546:         if ((@from > 0) && (@to > 0)) {
                   9547:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   9548:             if (!@diffs) {
                   9549:                 $canclone = 1;
                   9550:             }
                   9551:         }
                   9552:     }
                   9553:     return $canclone;
                   9554: }
                   9555: 
1.679     raeburn  9556: # ------------------------------------------------------- Course Group routines
                   9557: 
                   9558: sub get_coursegroups {
1.809     raeburn  9559:     my ($cdom,$cnum,$group,$namespace) = @_;
                   9560:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  9561: }
                   9562: 
1.679     raeburn  9563: sub modify_coursegroup {
                   9564:     my ($cdom,$cnum,$groupsettings) = @_;
                   9565:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   9566: }
                   9567: 
1.809     raeburn  9568: sub toggle_coursegroup_status {
                   9569:     my ($cdom,$cnum,$group,$action) = @_;
                   9570:     my ($from_namespace,$to_namespace);
                   9571:     if ($action eq 'delete') {
                   9572:         $from_namespace = 'coursegroups';
                   9573:         $to_namespace = 'deleted_groups';
                   9574:     } else {
                   9575:         $from_namespace = 'deleted_groups';
                   9576:         $to_namespace = 'coursegroups';
                   9577:     }
                   9578:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  9579:     if (my $tmp = &error(%curr_group)) {
                   9580:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   9581:         return ('read error',$tmp);
                   9582:     } else {
                   9583:         my %savedsettings = %curr_group; 
1.809     raeburn  9584:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  9585:         my $deloutcome;
                   9586:         if ($result eq 'ok') {
1.809     raeburn  9587:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  9588:         } else {
                   9589:             return ('write error',$result);
                   9590:         }
                   9591:         if ($deloutcome eq 'ok') {
                   9592:             return 'ok';
                   9593:         } else {
                   9594:             return ('delete error',$deloutcome);
                   9595:         }
                   9596:     }
                   9597: }
                   9598: 
1.679     raeburn  9599: sub modify_group_roles {
1.957     raeburn  9600:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  9601:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   9602:     my $role = 'gr/'.&escape($userprivs);
                   9603:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  9604:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  9605:     if ($result eq 'ok') {
                   9606:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   9607:     }
1.679     raeburn  9608:     return $result;
                   9609: }
                   9610: 
                   9611: sub modify_coursegroup_membership {
                   9612:     my ($cdom,$cnum,$membership) = @_;
                   9613:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   9614:     return $result;
                   9615: }
                   9616: 
1.682     raeburn  9617: sub get_active_groups {
                   9618:     my ($udom,$uname,$cdom,$cnum) = @_;
                   9619:     my $now = time;
                   9620:     my %groups = ();
                   9621:     foreach my $key (keys(%env)) {
1.811     albertel 9622:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  9623:             my ($start,$end) = split(/\./,$env{$key});
                   9624:             if (($end!=0) && ($end<$now)) { next; }
                   9625:             if (($start!=0) && ($start>$now)) { next; }
                   9626:             if ($1 eq $cdom && $2 eq $cnum) {
                   9627:                 $groups{$3} = $env{$key} ;
                   9628:             }
                   9629:         }
                   9630:     }
                   9631:     return %groups;
                   9632: }
                   9633: 
1.683     raeburn  9634: sub get_group_membership {
                   9635:     my ($cdom,$cnum,$group) = @_;
                   9636:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   9637: }
                   9638: 
                   9639: sub get_users_groups {
                   9640:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  9641:     my @usersgroups;
1.683     raeburn  9642:     my $cachetime=1800;
                   9643: 
                   9644:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  9645:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   9646:     if (defined($cached)) {
1.734     albertel 9647:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  9648:     } else {  
                   9649:         $grouplist = '';
1.816     raeburn  9650:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  9651:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  9652:         my $access_end = $env{'course.'.$courseid.
                   9653:                               '.default_enrollment_end_date'};
                   9654:         my $now = time;
                   9655:         foreach my $key (keys(%roleshash)) {
                   9656:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   9657:                 my $group = $1;
                   9658:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   9659:                     my $start = $2;
                   9660:                     my $end = $1;
                   9661:                     if ($start == -1) { next; } # deleted from group
                   9662:                     if (($start!=0) && ($start>$now)) { next; }
                   9663:                     if (($end!=0) && ($end<$now)) {
                   9664:                         if ($access_end && $access_end < $now) {
                   9665:                             if ($access_end - $end < 86400) {
                   9666:                                 push(@usersgroups,$group);
1.733     raeburn  9667:                             }
                   9668:                         }
1.817     raeburn  9669:                         next;
1.733     raeburn  9670:                     }
1.817     raeburn  9671:                     push(@usersgroups,$group);
1.683     raeburn  9672:                 }
                   9673:             }
                   9674:         }
1.817     raeburn  9675:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   9676:         $grouplist = join(':',@usersgroups);
                   9677:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  9678:     }
1.733     raeburn  9679:     return @usersgroups;
1.683     raeburn  9680: }
                   9681: 
                   9682: sub devalidate_getgroups_cache {
                   9683:     my ($udom,$uname,$cdom,$cnum)=@_;
                   9684:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 9685: 
1.683     raeburn  9686:     my $hashid="$udom:$uname:$courseid";
                   9687:     &devalidate_cache_new('getgroups',$hashid);
                   9688: }
                   9689: 
1.12      www      9690: # ------------------------------------------------------------------ Plain Text
                   9691: 
                   9692: sub plaintext {
1.988     raeburn  9693:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  9694:     if ($short =~ m{^cr/}) {
1.758     albertel 9695: 	return (split('/',$short))[-1];
                   9696:     }
1.742     raeburn  9697:     if (!defined($cid)) {
                   9698:         $cid = $env{'request.course.id'};
                   9699:     }
                   9700:     my %rolenames = (
1.1008    raeburn  9701:                       Course    => 'std',
                   9702:                       Community => 'alt1',
1.1305    raeburn  9703:                       Placement => 'std',
1.742     raeburn  9704:                     );
1.1037    raeburn  9705:     if ($cid ne '') {
                   9706:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   9707:             unless ($forcedefault) {
                   9708:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   9709:                 &Apache::lonlocal::mt_escape(\$roletext);
                   9710:                 return &Apache::lonlocal::mt($roletext);
                   9711:             }
                   9712:         }
                   9713:     }
                   9714:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   9715:         (defined($rolenames{$type})) && 
                   9716:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  9717:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  9718:     } elsif ($cid ne '') {
                   9719:         my $crstype = $env{'course.'.$cid.'.type'};
                   9720:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   9721:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   9722:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   9723:         }
1.742     raeburn  9724:     }
1.1037    raeburn  9725:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      9726: }
                   9727: 
                   9728: # ----------------------------------------------------------------- Assign Role
                   9729: 
                   9730: sub assignrole {
1.957     raeburn  9731:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   9732:         $context)=@_;
1.21      www      9733:     my $mrole;
                   9734:     if ($role =~ /^cr\//) {
1.393     www      9735:         my $cwosec=$url;
1.811     albertel 9736:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      9737: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  9738:            my $refused = 1;
                   9739:            if ($context eq 'requestcourses') {
                   9740:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   9741:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   9742:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   9743:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   9744:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9745:                            if ($crsenv{'internal.courseowner'} eq
                   9746:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   9747:                                $refused = '';
                   9748:                            }
                   9749:                        }
                   9750:                    }
                   9751:                }
                   9752:            }
                   9753:            if ($refused) {
                   9754:                &logthis('Refused custom assignrole: '.
                   9755:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   9756:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   9757:                return 'refused';
                   9758:            }
1.104     www      9759:         }
1.21      www      9760:         $mrole='cr';
1.678     raeburn  9761:     } elsif ($role =~ /^gr\//) {
                   9762:         my $cwogrp=$url;
1.811     albertel 9763:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  9764:         unless (&allowed('mdg',$cwogrp)) {
                   9765:             &logthis('Refused group assignrole: '.
                   9766:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   9767:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   9768:             return 'refused';
                   9769:         }
                   9770:         $mrole='gr';
1.21      www      9771:     } else {
1.82      www      9772:         my $cwosec=$url;
1.811     albertel 9773:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  9774:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   9775:             my $refused;
                   9776:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   9777:                 if (!(&allowed('c'.$role,$url))) {
                   9778:                     $refused = 1;
                   9779:                 }
                   9780:             } else {
                   9781:                 $refused = 1;
                   9782:             }
1.947     raeburn  9783:             if ($refused) {
1.1045    raeburn  9784:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
1.1377    raeburn  9785:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
1.1045    raeburn  9786:                     my %crsenv;
                   9787:                     if ($role eq 'cc' || $role eq 'co') {
                   9788:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9789:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   9790:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   9791:                                 if ($crsenv{'internal.courseowner'} eq 
                   9792:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   9793:                                     $refused = '';
                   9794:                                 }
                   9795:                             }
                   9796:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   9797:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   9798:                                 if ($crsenv{'internal.courseowner'} eq 
                   9799:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   9800:                                     $refused = '';
                   9801:                                 }
                   9802:                             }
                   9803:                         }
                   9804:                     }
1.1368    raeburn  9805:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   9806:                     if ($role eq 'st') {
                   9807:                         $refused = '';
1.1377    raeburn  9808:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
1.1368    raeburn  9809:                         $refused = '';
                   9810:                     }
1.1017    raeburn  9811:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  9812:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  9813:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  9814:                         my $wrongcc;
                   9815:                         if ($cnum =~ /^$match_community$/) {
                   9816:                             $wrongcc = 1 if ($role eq 'cc');
                   9817:                         } else {
                   9818:                             $wrongcc = 1 if ($role eq 'co');
                   9819:                         }
                   9820:                         unless ($wrongcc) {
                   9821:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9822:                             if ($crsenv{'internal.courseowner'} eq 
                   9823:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   9824:                                 $refused = '';
                   9825:                             }
1.1017    raeburn  9826:                         }
                   9827:                     }
1.1183    raeburn  9828:                 } elsif ($context eq 'requestauthor') {
                   9829:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   9830:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   9831:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   9832:                             $refused = '';
                   9833:                         } else {
                   9834:                             my %domdefaults = &get_domain_defaults($udom);
                   9835:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   9836:                                 my $checkbystatus;
                   9837:                                 if ($env{'user.adv'}) { 
                   9838:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   9839:                                     if ($disposition eq 'automatic') {
                   9840:                                         $refused = '';
                   9841:                                     } elsif ($disposition eq '') {
                   9842:                                         $checkbystatus = 1;
                   9843:                                     } 
                   9844:                                 } else {
                   9845:                                     $checkbystatus = 1;
                   9846:                                 }
                   9847:                                 if ($checkbystatus) {
                   9848:                                     if ($env{'environment.inststatus'}) {
                   9849:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   9850:                                         foreach my $type (@inststatuses) {
                   9851:                                             if (($type ne '') &&
                   9852:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   9853:                                                 $refused = '';
                   9854:                                             }
                   9855:                                         }
                   9856:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   9857:                                         $refused = '';
                   9858:                                     }
                   9859:                                 }
                   9860:                             }
                   9861:                         }
                   9862:                     }
1.1017    raeburn  9863:                 }
                   9864:                 if ($refused) {
1.947     raeburn  9865:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   9866:                              ' '.$role.' '.$end.' '.$start.' by '.
                   9867: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   9868:                     return 'refused';
                   9869:                 }
1.932     raeburn  9870:             }
1.1131    raeburn  9871:         } elsif ($role eq 'au') {
                   9872:             if ($url ne '/'.$udom.'/') {
                   9873:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   9874:                          ' to assign author role for '.$uname.':'.$udom.
                   9875:                          ' in domain: '.$url.' refused (wrong domain).');
                   9876:                 return 'refused';
                   9877:             }
1.104     www      9878:         }
1.21      www      9879:         $mrole=$role;
                   9880:     }
1.620     albertel 9881:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      9882:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      9883:     if ($end) { $command.='_'.$end; }
1.21      www      9884:     if ($start) {
                   9885: 	if ($end) { 
1.81      www      9886:            $command.='_'.$start; 
1.21      www      9887:         } else {
1.81      www      9888:            $command.='_0_'.$start;
1.21      www      9889:         }
                   9890:     }
1.739     raeburn  9891:     my $origstart = $start;
                   9892:     my $origend = $end;
1.957     raeburn  9893:     my $delflag;
1.357     www      9894: # actually delete
                   9895:     if ($deleteflag) {
1.373     www      9896: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      9897: # modify command to delete the role
1.620     albertel 9898:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      9899:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 9900: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      9901: # set start and finish to negative values for userrolelog
                   9902:            $start=-1;
                   9903:            $end=-1;
1.957     raeburn  9904:            $delflag = 1;
1.357     www      9905:         }
                   9906:     }
                   9907: # send command
1.349     www      9908:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      9909: # log new user role if status is ok
1.349     www      9910:     if ($answer eq 'ok') {
1.663     raeburn  9911: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  9912:         if (($role eq 'cc') || ($role eq 'in') ||
                   9913:             ($role eq 'ep') || ($role eq 'ad') ||
                   9914:             ($role eq 'ta') || ($role eq 'st') ||
                   9915:             ($role=~/^cr/) || ($role eq 'gr') ||
                   9916:             ($role eq 'co')) {
1.1200    raeburn  9917: # for course roles, perform group memberships changes triggered by role change.
                   9918:             unless ($role =~ /^gr/) {
                   9919:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   9920:                                                  $origstart,$selfenroll,$context);
                   9921:             }
1.1184    raeburn  9922:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9923:                            $selfenroll,$context);
                   9924:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334    raeburn  9925:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
                   9926:                  ($role eq 'da')) {
1.1184    raeburn  9927:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9928:                            $context);
                   9929:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
                   9930:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9931:                              $context); 
                   9932:         }
1.1053    raeburn  9933:         if ($role eq 'cc') {
                   9934:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   9935:         }
1.349     www      9936:     }
                   9937:     return $answer;
1.169     harris41 9938: }
                   9939: 
1.1053    raeburn  9940: sub autoupdate_coowners {
                   9941:     my ($url,$end,$start,$uname,$udom) = @_;
                   9942:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   9943:     if (($cdom ne '') && ($cnum ne '')) {
                   9944:         my $now = time;
                   9945:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   9946:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   9947:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   9948:             my $instcode = $coursehash{'internal.coursecode'};
                   9949:             if ($instcode ne '') {
1.1056    raeburn  9950:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   9951:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  9952:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  9953:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   9954:                         if ($result eq 'valid') {
                   9955:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  9956:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   9957:                                     push(@newcoowners,$coowner);
                   9958:                                 }
                   9959:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   9960:                                     push(@newcoowners,$uname.':'.$udom);
                   9961:                                 }
                   9962:                                 @newcoowners = sort(@newcoowners);
                   9963:                             } else {
                   9964:                                 push(@newcoowners,$uname.':'.$udom);
                   9965:                             }
                   9966:                         } else {
                   9967:                             if ($coursehash{'internal.co-owners'}) {
                   9968:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   9969:                                     unless ($coowner eq $uname.':'.$udom) {
                   9970:                                         push(@newcoowners,$coowner);
                   9971:                                     }
                   9972:                                 }
                   9973:                                 unless (@newcoowners > 0) {
                   9974:                                     $delcoowners = 1;
                   9975:                                     $coowners = '';
                   9976:                                 }
                   9977:                             }
                   9978:                         }
                   9979:                         if (@newcoowners || $delcoowners) {
                   9980:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   9981:                                             $delcoowners,@newcoowners);
                   9982:                         }
                   9983:                     }
                   9984:                 }
                   9985:             }
                   9986:         }
                   9987:     }
                   9988: }
                   9989: 
                   9990: sub store_coowners {
                   9991:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   9992:     my $cid = $cdom.'_'.$cnum;
                   9993:     my ($coowners,$delresult,$putresult);
                   9994:     if (@newcoowners) {
                   9995:         $coowners = join(',',@newcoowners);
                   9996:         my %coownershash = (
                   9997:                             'internal.co-owners' => $coowners,
                   9998:                            );
                   9999:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   10000:         if ($putresult eq 'ok') {
                   10001:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   10002:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   10003:             }
                   10004:         }
                   10005:     }
                   10006:     if ($delcoowners) {
                   10007:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   10008:         if ($delresult eq 'ok') {
                   10009:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   10010:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   10011:             }
                   10012:         }
                   10013:     }
                   10014:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   10015:         my %crsinfo =
                   10016:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   10017:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   10018:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   10019:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   10020:         }
                   10021:     }
                   10022: }
                   10023: 
1.169     harris41 10024: # -------------------------------------------------- Modify user authentication
1.197     www      10025: # Overrides without validation
                   10026: 
1.169     harris41 10027: sub modifyuserauth {
                   10028:     my ($udom,$uname,$umode,$upass)=@_;
                   10029:     my $uhome=&homeserver($uname,$udom);
1.197     www      10030:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   10031:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 10032:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10033:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 10034:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   10035: 		     &escape($upass),$uhome);
1.620     albertel 10036:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      10037:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   10038:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   10039:     &log($udom,,$uname,$uhome,
1.620     albertel 10040:         'Authentication changed by '.$env{'user.domain'}.', '.
                   10041:                                      $env{'user.name'}.', '.$umode.
1.197     www      10042:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 10043:     unless ($reply eq 'ok') {
1.197     www      10044:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 10045: 	return 'error: '.$reply;
                   10046:     }   
1.170     harris41 10047:     return 'ok';
1.80      www      10048: }
                   10049: 
1.81      www      10050: # --------------------------------------------------------------- Modify a user
1.80      www      10051: 
1.81      www      10052: sub modifyuser {
1.206     matthew  10053:     my ($udom,    $uname, $uid,
                   10054:         $umode,   $upass, $first,
                   10055:         $middle,  $last,  $gene,
1.1058    raeburn  10056:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 10057:     $udom= &LONCAPA::clean_domain($udom);
                   10058:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  10059:     my $showcandelete = 'none';
                   10060:     if (ref($candelete) eq 'ARRAY') {
                   10061:         if (@{$candelete} > 0) {
                   10062:             $showcandelete = join(', ',@{$candelete});
                   10063:         }
                   10064:     }
1.81      www      10065:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      10066:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  10067: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  10068:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   10069:                                      ' desiredhome not specified'). 
1.620     albertel 10070:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   10071:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 10072:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  10073:     my $newuser;
                   10074:     if ($uhome eq 'no_host') {
                   10075:         $newuser = 1;
1.1368    raeburn  10076:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
                   10077:                 ($umode eq 'lti')) {
                   10078:             return 'error: more information needed to create new user';
                   10079:         }
1.1075    raeburn  10080:     }
1.80      www      10081: # ----------------------------------------------------------------- Create User
1.406     albertel 10082:     if (($uhome eq 'no_host') && 
1.1368    raeburn  10083: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80      www      10084:         my $unhome='';
1.844     albertel 10085:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  10086:             $unhome = $desiredhome;
1.620     albertel 10087: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   10088: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  10089:         } else { # load balancing routine for determining $unhome
1.81      www      10090:             my $loadm=10000000;
1.841     albertel 10091: 	    my %servers = &get_servers($udom,'library');
                   10092: 	    foreach my $tryserver (keys(%servers)) {
                   10093: 		my $answer=reply('load',$tryserver);
                   10094: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   10095: 		    $loadm=$answer;
                   10096: 		    $unhome=$tryserver;
                   10097: 		}
1.80      www      10098: 	    }
                   10099:         }
                   10100:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  10101: 	    return 'error: unable to find a home server for '.$uname.
                   10102:                    ' in domain '.$udom;
1.80      www      10103:         }
                   10104:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   10105:                          &escape($upass),$unhome);
                   10106: 	unless ($reply eq 'ok') {
                   10107:             return 'error: '.$reply;
                   10108:         }   
1.230     stredwic 10109:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      10110:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  10111: 	    return 'error: unable verify users home machine.';
1.80      www      10112:         }
1.209     matthew  10113:     }   # End of creation of new user
1.80      www      10114: # ---------------------------------------------------------------------- Add ID
                   10115:     if ($uid) {
                   10116:        $uid=~tr/A-Z/a-z/;
                   10117:        my %uidhash=&idrget($udom,$uname);
1.196     www      10118:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   10119:          && (!$forceid)) {
1.80      www      10120: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  10121: 	      return 'error: user id "'.$uid.'" does not match '.
                   10122:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      10123:           }
                   10124:        } else {
1.1300    raeburn  10125: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
1.80      www      10126:        }
                   10127:     }
                   10128: # -------------------------------------------------------------- Add names, etc
1.313     matthew  10129:     my @tmp=&get('environment',
1.899     raeburn  10130: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  10131:                     'permanentemail','inststatus'],
1.134     albertel 10132: 		   $udom,$uname);
1.1075    raeburn  10133:     my (%names,%oldnames);
1.313     matthew  10134:     if ($tmp[0] =~ m/^error:.*/) { 
                   10135:         %names=(); 
                   10136:     } else {
                   10137:         %names = @tmp;
1.1075    raeburn  10138:         %oldnames = %names;
1.313     matthew  10139:     }
1.388     www      10140: #
1.1058    raeburn  10141: # If name, email and/or uid are blank (e.g., because an uploaded file
                   10142: # of users did not contain them), do not overwrite existing values
                   10143: # unless field is in $candelete array ref.  
                   10144: #
                   10145: 
                   10146:     my @fields = ('firstname','middlename','lastname','generation',
                   10147:                   'permanentemail','id');
                   10148:     my %newvalues;
                   10149:     if (ref($candelete) eq 'ARRAY') {
                   10150:         foreach my $field (@fields) {
                   10151:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   10152:                 if ($field eq 'firstname') {
                   10153:                     $names{$field} = $first;
                   10154:                 } elsif ($field eq 'middlename') {
                   10155:                     $names{$field} = $middle;
                   10156:                 } elsif ($field eq 'lastname') {
                   10157:                     $names{$field} = $last;
                   10158:                 } elsif ($field eq 'generation') { 
                   10159:                     $names{$field} = $gene;
                   10160:                 } elsif ($field eq 'permanentemail') {
                   10161:                     $names{$field} = $email;
                   10162:                 } elsif ($field eq 'id') {
                   10163:                     $names{$field}  = $uid;
                   10164:                 }
                   10165:             }
                   10166:         }
                   10167:     }
1.388     www      10168:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  10169:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      10170:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  10171:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      10172:     if ($email) {
                   10173:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  10174:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      10175:     }
1.899     raeburn  10176:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  10177:     if (defined($inststatus)) {
                   10178:         $names{'inststatus'} = '';
                   10179:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   10180:         if (ref($usertypes) eq 'HASH') {
                   10181:             my @okstatuses; 
                   10182:             foreach my $item (split(/:/,$inststatus)) {
                   10183:                 if (defined($usertypes->{$item})) {
                   10184:                     push(@okstatuses,$item);  
                   10185:                 }
                   10186:             }
                   10187:             if (@okstatuses) {
                   10188:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   10189:             }
                   10190:         }
                   10191:     }
1.1075    raeburn  10192:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  10193:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  10194:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  10195:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   10196:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   10197:     } else {
                   10198:         $logmsg .= ' during self creation';
                   10199:     }
1.1075    raeburn  10200:     my $changed;
                   10201:     if ($newuser) {
                   10202:         $changed = 1;
                   10203:     } else {
                   10204:         foreach my $field (@fields) {
                   10205:             if ($names{$field} ne $oldnames{$field}) {
                   10206:                 $changed = 1;
                   10207:                 last;
                   10208:             }
                   10209:         }
                   10210:     }
                   10211:     unless ($changed) {
                   10212:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   10213:         &logthis($logmsg);
                   10214:         return 'ok';
                   10215:     }
                   10216:     my $reply = &put('environment', \%names, $udom,$uname);
                   10217:     if ($reply ne 'ok') { 
                   10218:         return 'error: '.$reply;
                   10219:     }
1.1087    raeburn  10220:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   10221:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   10222:     }
1.1075    raeburn  10223:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   10224:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   10225:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  10226:     &logthis($logmsg);
1.134     albertel 10227:     return 'ok';
1.80      www      10228: }
                   10229: 
1.81      www      10230: # -------------------------------------------------------------- Modify student
1.80      www      10231: 
1.81      www      10232: sub modifystudent {
                   10233:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  10234:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314    raeburn  10235:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455     albertel 10236:     if (!$cid) {
1.620     albertel 10237: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 10238: 	    return 'not_in_class';
                   10239: 	}
1.80      www      10240:     }
                   10241: # --------------------------------------------------------------- Make the user
1.81      www      10242:     my $reply=&modifyuser
1.209     matthew  10243: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  10244:          $desiredhome,$email,$inststatus);
1.80      www      10245:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  10246:     # This will cause &modify_student_enrollment to get the uid from the
1.1235    raeburn  10247:     # student's environment
1.297     matthew  10248:     $uid = undef if (!$forceid);
1.455     albertel 10249:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  10250:                                         $gene,$usec,$end,$start,$type,$locktype,
1.1314    raeburn  10251:                                         $cid,$selfenroll,$context,$credits,$instsec);
1.297     matthew  10252:     return $reply;
                   10253: }
                   10254: 
                   10255: sub modify_student_enrollment {
1.1216    raeburn  10256:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314    raeburn  10257:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455     albertel 10258:     my ($cdom,$cnum,$chome);
                   10259:     if (!$cid) {
1.620     albertel 10260: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 10261: 	    return 'not_in_class';
                   10262: 	}
1.620     albertel 10263: 	$cdom=$env{'course.'.$cid.'.domain'};
                   10264: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 10265:     } else {
                   10266: 	($cdom,$cnum)=split(/_/,$cid);
                   10267:     }
1.620     albertel 10268:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 10269:     if (!$chome) {
1.457     raeburn  10270: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  10271:     }
1.455     albertel 10272:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  10273:     # Make sure the user exists
1.81      www      10274:     my $uhome=&homeserver($uname,$udom);
                   10275:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   10276: 	return 'error: no such user';
                   10277:     }
1.297     matthew  10278:     # Get student data if we were not given enough information
                   10279:     if (!defined($first)  || $first  eq '' || 
                   10280:         !defined($last)   || $last   eq '' || 
                   10281:         !defined($uid)    || $uid    eq '' || 
                   10282:         !defined($middle) || $middle eq '' || 
                   10283:         !defined($gene)   || $gene   eq '') {
1.294     matthew  10284:         # They did not supply us with enough data to enroll the student, so
                   10285:         # we need to pick up more information.
1.297     matthew  10286:         my %tmp = &get('environment',
1.294     matthew  10287:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  10288:                        ,$udom,$uname);
                   10289: 
1.800     albertel 10290:         #foreach my $key (keys(%tmp)) {
                   10291:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 10292:         #}
1.294     matthew  10293:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   10294:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   10295:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  10296:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  10297:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   10298:     }
1.556     albertel 10299:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  10300:     my $user = "$uname:$udom";
                   10301:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 10302:     my $reply=cput('classlist',
1.1148    raeburn  10303: 		   {$user => 
1.1314    raeburn  10304: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487     albertel 10305: 		   $cdom,$cnum);
1.1148    raeburn  10306:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   10307:         &devalidate_getsection_cache($udom,$uname,$cid);
                   10308:     } else { 
1.81      www      10309: 	return 'error: '.$reply;
                   10310:     }
1.297     matthew  10311:     # Add student role to user
1.83      www      10312:     my $uurl='/'.$cid;
1.81      www      10313:     $uurl=~s/\_/\//g;
                   10314:     if ($usec) {
                   10315: 	$uurl.='/'.$usec;
                   10316:     }
1.1148    raeburn  10317:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   10318:                              $selfenroll,$context);
                   10319:     if ($result ne 'ok') {
                   10320:         if ($old_entry{$user} ne '') {
                   10321:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   10322:         } else {
                   10323:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   10324:         }
                   10325:     }
                   10326:     return $result; 
1.21      www      10327: }
                   10328: 
1.556     albertel 10329: sub format_name {
                   10330:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   10331:     my $name;
                   10332:     if ($first ne 'lastname') {
                   10333: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   10334:     } else {
                   10335: 	if ($lastname=~/\S/) {
                   10336: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   10337: 	    $name=~s/\s+,/,/;
                   10338: 	} else {
                   10339: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   10340: 	}
                   10341:     }
                   10342:     $name=~s/^\s+//;
                   10343:     $name=~s/\s+$//;
                   10344:     $name=~s/\s+/ /g;
                   10345:     return $name;
                   10346: }
                   10347: 
1.84      www      10348: # ------------------------------------------------- Write to course preferences
                   10349: 
                   10350: sub writecoursepref {
                   10351:     my ($courseid,%prefs)=@_;
                   10352:     $courseid=~s/^\///;
                   10353:     $courseid=~s/\_/\//g;
                   10354:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   10355:     my $chome=homeserver($cnum,$cdomain);
                   10356:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   10357: 	return 'error: no such course';
                   10358:     }
                   10359:     my $cstring='';
1.800     albertel 10360:     foreach my $pref (keys(%prefs)) {
                   10361: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 10362:     }
1.84      www      10363:     $cstring=~s/\&$//;
                   10364:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   10365: }
                   10366: 
                   10367: # ---------------------------------------------------------- Make/modify course
                   10368: 
                   10369: sub createcourse {
1.741     raeburn  10370:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  10371:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      10372:     $url=&declutter($url);
                   10373:     my $cid='';
1.1028    raeburn  10374:     if ($context eq 'requestcourses') {
                   10375:         my $can_create = 0;
                   10376:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   10377:         if ($udom eq $ownerdom) {
                   10378:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   10379:                                   $context)) {
                   10380:                 $can_create = 1;
                   10381:             }
                   10382:         } else {
                   10383:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   10384:                                            $category);
                   10385:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   10386:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   10387:                 if (@curr > 0) {
                   10388:                     my @options = qw(approval validate autolimit);
                   10389:                     my $optregex = join('|',@options);
                   10390:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   10391:                         $can_create = 1;
                   10392:                     }
                   10393:                 }
                   10394:             }
                   10395:         }
                   10396:         if ($can_create) {
                   10397:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   10398:                 unless (&allowed('ccc',$udom)) {
                   10399:                     return 'refused'; 
                   10400:                 }
1.1017    raeburn  10401:             }
                   10402:         } else {
                   10403:             return 'refused';
                   10404:         }
1.1028    raeburn  10405:     } elsif (!&allowed('ccc',$udom)) {
                   10406:         return 'refused';
1.84      www      10407:     }
1.1011    raeburn  10408: # --------------------------------------------------------------- Get Unique ID
                   10409:     my $uname;
                   10410:     if ($cnum =~ /^$match_courseid$/) {
                   10411:         my $chome=&homeserver($cnum,$udom,'true');
                   10412:         if (($chome eq '') || ($chome eq 'no_host')) {
                   10413:             $uname = $cnum;
                   10414:         } else {
1.1038    raeburn  10415:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  10416:         }
                   10417:     } else {
1.1038    raeburn  10418:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  10419:     }
                   10420:     return $uname if ($uname =~ /^error/);
                   10421: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  10422:     if (!defined($course_server)) {
                   10423:         if (defined(&domain($udom,'primary'))) {
                   10424:             $course_server = &domain($udom,'primary');
                   10425:         } else {
                   10426:             $course_server = $env{'user.home'}; 
                   10427:         }
                   10428:     }
                   10429:     my %host_servers =
                   10430:         &Apache::lonnet::get_servers($udom,'library');
                   10431:     unless ($host_servers{$course_server}) {
                   10432:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  10433:     }
1.84      www      10434: # ------------------------------------------------------------- Make the course
                   10435:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  10436:                       $course_server);
1.84      www      10437:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  10438:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      10439:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   10440: 	return 'error: no such course';
                   10441:     }
1.271     www      10442: # ----------------------------------------------------------------- Course made
1.516     raeburn  10443: # log existence
1.1029    raeburn  10444:     my $now = time;
1.918     raeburn  10445:     my $newcourse = {
                   10446:                     $udom.'_'.$uname => {
1.921     raeburn  10447:                                      description => $description,
                   10448:                                      inst_code   => $inst_code,
                   10449:                                      owner       => $course_owner,
                   10450:                                      type        => $crstype,
1.1029    raeburn  10451:                                      creator     => $env{'user.name'}.':'.
                   10452:                                                     $env{'user.domain'},
                   10453:                                      created     => $now,
                   10454:                                      context     => $context,
1.918     raeburn  10455:                                                 },
                   10456:                     };
1.921     raeburn  10457:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      10458: # set toplevel url
1.271     www      10459:     my $topurl=$url;
                   10460:     unless ($nonstandard) {
                   10461: # ------------------------------------------ For standard courses, make top url
                   10462:         my $mapurl=&clutter($url);
1.278     www      10463:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 10464:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      10465: <map>
                   10466: <resource id="1" type="start"></resource>
                   10467: <resource id="2" src="$mapurl"></resource>
                   10468: <resource id="3" type="finish"></resource>
                   10469: <link index="1" from="1" to="2"></link>
                   10470: <link index="2" from="2" to="3"></link>
                   10471: </map>
                   10472: ENDINITMAP
                   10473:         $topurl=&declutter(
1.638     albertel 10474:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      10475:                           );
                   10476:     }
                   10477: # ----------------------------------------------------------- Write preferences
1.84      www      10478:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  10479:                      ('description'              => $description,
                   10480:                       'url'                      => $topurl,
                   10481:                       'internal.creator'         => $env{'user.name'}.':'.
                   10482:                                                     $env{'user.domain'},
                   10483:                       'internal.created'         => $now,
                   10484:                       'internal.creationcontext' => $context)
                   10485:                     );
1.84      www      10486:     return '/'.$udom.'/'.$uname;
                   10487: }
                   10488: 
1.1011    raeburn  10489: # ------------------------------------------------------------------- Create ID
                   10490: sub generate_coursenum {
1.1038    raeburn  10491:     my ($udom,$crstype) = @_;
1.1011    raeburn  10492:     my $domdesc = &domain($udom);
                   10493:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  10494:     my $first;
                   10495:     if ($crstype eq 'Community') {
                   10496:         $first = '0';
                   10497:     } else {
                   10498:         $first = int(1+rand(9)); 
                   10499:     } 
                   10500:     my $uname=$first.
1.1011    raeburn  10501:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   10502:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   10503:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   10504: # ----------------------------------------------- Make sure that does not exist
                   10505:     my $uhome=&homeserver($uname,$udom,'true');
                   10506:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  10507:         if ($crstype eq 'Community') {
                   10508:             $first = '0';
                   10509:         } else {
                   10510:             $first = int(1+rand(9));
                   10511:         }
                   10512:         $uname=$first.
1.1011    raeburn  10513:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   10514:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   10515:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   10516:         $uhome=&homeserver($uname,$udom,'true');
                   10517:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   10518:             return 'error: unable to generate unique course-ID';
                   10519:         }
                   10520:     }
                   10521:     return $uname;
                   10522: }
                   10523: 
1.813     albertel 10524: sub is_course {
1.1167    droeschl 10525:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   10526:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   10527: 
1.1381    raeburn  10528:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
                   10529:     my $uhome=&homeserver($cnum,$cdom);
                   10530:     my $iscourse;
                   10531:     if (grep { $_ eq $uhome } current_machine_ids()) {
1.1382    raeburn  10532:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
1.1381    raeburn  10533:     } else {
                   10534:         my $hashid = $cdom.':'.$cnum;
                   10535:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
                   10536:         unless (defined($cached)) {
                   10537:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
                   10538:                                         $cnum,undef,undef,'.');
                   10539:             $iscourse = 0;
                   10540:             if (exists($courses{$cdom.'_'.$cnum})) {
                   10541:                 $iscourse = 1;
                   10542:             }
                   10543:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
                   10544:         }
                   10545:     }
                   10546:     return unless ($iscourse);
1.1167    droeschl 10547:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 10548: }
                   10549: 
1.1015    raeburn  10550: sub store_userdata {
                   10551:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  10552:     my $result;
1.1016    raeburn  10553:     if ($datakey ne '') {
1.1013    raeburn  10554:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  10555:             if ($udom eq '' || $uname eq '') {
                   10556:                 $udom = $env{'user.domain'};
                   10557:                 $uname = $env{'user.name'};
                   10558:             }
                   10559:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  10560:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   10561:                 $result = 'error: no_host';
                   10562:             } else {
                   10563:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   10564:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   10565: 
                   10566:                 my $namevalue='';
                   10567:                 foreach my $key (keys(%{$storehash})) {
                   10568:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   10569:                 }
                   10570:                 $namevalue=~s/\&$//;
1.1224    raeburn  10571:                 unless ($namespace eq 'courserequests') {
                   10572:                     $datakey = &escape($datakey);
                   10573:                 }
1.1105    raeburn  10574:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   10575:                                   $namevalue,$uhome);
1.1013    raeburn  10576:             }
                   10577:         } else {
                   10578:             $result = 'error: data to store was not a hash reference'; 
                   10579:         }
                   10580:     } else {
                   10581:         $result= 'error: invalid requestkey'; 
                   10582:     }
                   10583:     return $result;
                   10584: }
                   10585: 
1.21      www      10586: # ---------------------------------------------------------- Assign Custom Role
                   10587: 
                   10588: sub assigncustomrole {
1.957     raeburn  10589:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10590:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  10591:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      10592: }
                   10593: 
                   10594: # ----------------------------------------------------------------- Revoke Role
                   10595: 
                   10596: sub revokerole {
1.957     raeburn  10597:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10598:     my $now=time;
1.965     raeburn  10599:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      10600: }
                   10601: 
                   10602: # ---------------------------------------------------------- Revoke Custom Role
                   10603: 
                   10604: sub revokecustomrole {
1.957     raeburn  10605:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      10606:     my $now=time;
1.357     www      10607:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  10608:            $deleteflag,$selfenroll,$context);
1.17      www      10609: }
                   10610: 
1.533     banghart 10611: # ------------------------------------------------------------ Disk usage
1.535     albertel 10612: sub diskusage {
1.955     raeburn  10613:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   10614:     $directorypath =~ s/\/$//;
                   10615:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   10616:                        .&escape($getpropath).':'.&escape($uname).':'
                   10617:                        .&escape($udom),homeserver($uname,$udom));
                   10618:     if ($listing eq 'unknown_cmd') {
                   10619:         if ($getpropath) {
                   10620:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   10621:         }
                   10622:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   10623:     }
1.514     albertel 10624:     return $listing;
1.512     banghart 10625: }
                   10626: 
1.566     banghart 10627: sub is_locked {
1.1096    raeburn  10628:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 10629:     my @check;
                   10630:     my $is_locked;
1.1093    raeburn  10631:     push (@check,$file_name);
1.613     albertel 10632:     my %locked = &get('file_permissions',\@check,
1.620     albertel 10633: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 10634:     my ($tmp)=keys(%locked);
                   10635:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  10636:     
1.566     banghart 10637:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  10638:         $is_locked = 'false';
                   10639:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  10640:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  10641:                $is_locked = 'true';
1.1096    raeburn  10642:                if (ref($which) eq 'ARRAY') {
                   10643:                    push(@{$which},$entry);
                   10644:                } else {
                   10645:                    last;
                   10646:                }
1.745     raeburn  10647:            }
                   10648:        }
1.566     banghart 10649:     } else {
                   10650:         $is_locked = 'false';
                   10651:     }
1.1093    raeburn  10652:     return $is_locked;
1.566     banghart 10653: }
                   10654: 
1.759     albertel 10655: sub declutter_portfile {
                   10656:     my ($file) = @_;
1.833     albertel 10657:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 10658:     return $file;
                   10659: }
                   10660: 
1.559     banghart 10661: # ------------------------------------------------------------- Mark as Read Only
                   10662: 
                   10663: sub mark_as_readonly {
                   10664:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 10665:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 10666:     my ($tmp)=keys(%current_permissions);
                   10667:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 10668:     foreach my $file (@{$files}) {
1.759     albertel 10669: 	$file = &declutter_portfile($file);
1.561     banghart 10670:         push(@{$current_permissions{$file}},$what);
1.559     banghart 10671:     }
1.613     albertel 10672:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 10673:     return;
                   10674: }
                   10675: 
1.572     banghart 10676: # ------------------------------------------------------------Save Selected Files
                   10677: 
                   10678: sub save_selected_files {
                   10679:     my ($user, $path, @files) = @_;
                   10680:     my $filename = $user."savedfiles";
1.573     banghart 10681:     my @other_files = &files_not_in_path($user, $path);
1.1359    raeburn  10682:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.573     banghart 10683:     foreach my $file (@files) {
1.620     albertel 10684:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 10685:     }
                   10686:     foreach my $file (@other_files) {
1.574     banghart 10687:         print (OUT $file."\n");
1.572     banghart 10688:     }
1.574     banghart 10689:     close (OUT);
1.572     banghart 10690:     return 'ok';
                   10691: }
                   10692: 
1.574     banghart 10693: sub clear_selected_files {
                   10694:     my ($user) = @_;
                   10695:     my $filename = $user."savedfiles";
1.1359    raeburn  10696:     open (OUT,'>',LONCAPA::tempdir().$filename);
1.574     banghart 10697:     print (OUT undef);
                   10698:     close (OUT);
                   10699:     return ("ok");    
                   10700: }
                   10701: 
1.572     banghart 10702: sub files_in_path {
                   10703:     my ($user, $path) = @_;
                   10704:     my $filename = $user."savedfiles";
                   10705:     my %return_files;
1.1359    raeburn  10706:     open (IN,'<',LONCAPA::tempdir().$filename);
1.573     banghart 10707:     while (my $line_in = <IN>) {
1.574     banghart 10708:         chomp ($line_in);
                   10709:         my @paths_and_file = split (m!/!, $line_in);
                   10710:         my $file_part = pop (@paths_and_file);
                   10711:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 10712:         $path_part.='/';
                   10713:         my $path_and_file = $path_part.$file_part;
                   10714:         if ($path_part eq $path) {
                   10715:             $return_files{$file_part}= 'selected';
                   10716:         }
                   10717:     }
1.574     banghart 10718:     close (IN);
                   10719:     return (\%return_files);
1.572     banghart 10720: }
                   10721: 
                   10722: # called in portfolio select mode, to show files selected NOT in current directory
                   10723: sub files_not_in_path {
                   10724:     my ($user, $path) = @_;
                   10725:     my $filename = $user."savedfiles";
                   10726:     my @return_files;
                   10727:     my $path_part;
1.1359    raeburn  10728:     open(IN, '<',LONCAPA::tempdir().$filename);
1.800     albertel 10729:     while (my $line = <IN>) {
1.572     banghart 10730:         #ok, I know it's clunky, but I want it to work
1.800     albertel 10731:         my @paths_and_file = split(m|/|, $line);
                   10732:         my $file_part = pop(@paths_and_file);
                   10733:         chomp($file_part);
                   10734:         my $path_part = join('/', @paths_and_file);
1.572     banghart 10735:         $path_part .= '/';
                   10736:         my $path_and_file = $path_part.$file_part;
                   10737:         if ($path_part ne $path) {
1.800     albertel 10738:             push(@return_files, ($path_and_file));
1.572     banghart 10739:         }
                   10740:     }
1.800     albertel 10741:     close(OUT);
1.574     banghart 10742:     return (@return_files);
1.572     banghart 10743: }
                   10744: 
1.1273    raeburn  10745: #------------------------------Submitted/Handedback Portfolio Files Versioning
                   10746:  
                   10747: sub portfiles_versioning {
                   10748:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
                   10749:     my $portfolio_root = '/userfiles/portfolio';
                   10750:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
                   10751:     foreach my $file (@{$portfiles}) {
                   10752:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
                   10753:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   10754:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
                   10755:         my $getpropath = 1;
                   10756:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
                   10757:                                              $stu_name,$getpropath);
                   10758:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
                   10759:         my $new_answer = 
                   10760:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
                   10761:         if ($new_answer ne 'problem getting file') {
                   10762:             push(@{$versioned_portfiles}, $directory.$new_answer);
                   10763:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
                   10764:                               [$symb,$env{'request.course.id'},'graded']);
                   10765:         }
                   10766:     }
                   10767: }
                   10768: 
                   10769: sub get_next_version {
                   10770:     my ($answer_name, $answer_ext, $dir_list) = @_;
                   10771:     my $version;
                   10772:     if (ref($dir_list) eq 'ARRAY') {
                   10773:         foreach my $row (@{$dir_list}) {
                   10774:             my ($file) = split(/\&/,$row,2);
                   10775:             my ($file_name,$file_version,$file_ext) =
                   10776:                 &file_name_version_ext($file);
                   10777:             if (($file_name eq $answer_name) &&
                   10778:                 ($file_ext eq $answer_ext)) {
                   10779:                      # gets here if filename and extension match,
                   10780:                      # regardless of version
                   10781:                 if ($file_version ne '') {
                   10782:                     # a versioned file is found  so save it for later
                   10783:                     if ($file_version > $version) {
                   10784:                         $version = $file_version;
                   10785:                     }
                   10786:                 }
                   10787:             }
                   10788:         }
                   10789:     }
                   10790:     $version ++;
                   10791:     return($version);
                   10792: }
                   10793: 
                   10794: sub version_selected_portfile {
                   10795:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   10796:     my ($answer_name,$answer_ver,$answer_ext) =
                   10797:         &file_name_version_ext($file_name);
                   10798:     my $new_answer;
                   10799:     $env{'form.copy'} =
                   10800:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   10801:     if($env{'form.copy'} eq '-1') {
                   10802:         $new_answer = 'problem getting file';
                   10803:     } else {
                   10804:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   10805:         my $copy_result = 
                   10806:             &finishuserfileupload($stu_name,$domain,'copy',
                   10807:                                   '/portfolio'.$directory.$new_answer);
                   10808:     }
                   10809:     undef($env{'form.copy'});
                   10810:     return ($new_answer);
                   10811: }
                   10812: 
                   10813: sub file_name_version_ext {
                   10814:     my ($file)=@_;
                   10815:     my @file_parts = split(/\./, $file);
                   10816:     my ($name,$version,$ext);
                   10817:     if (@file_parts > 1) {
                   10818:         $ext=pop(@file_parts);
                   10819:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   10820:             $version=pop(@file_parts);
                   10821:         }
                   10822:         $name=join('.',@file_parts);
                   10823:     } else {
                   10824:         $name=join('.',@file_parts);
                   10825:     }
                   10826:     return($name,$version,$ext);
                   10827: }
                   10828: 
1.745     raeburn  10829: #----------------------------------------------Get portfolio file permissions
1.629     banghart 10830: 
1.745     raeburn  10831: sub get_portfile_permissions {
                   10832:     my ($domain,$user) = @_;
1.613     albertel 10833:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 10834:     my ($tmp)=keys(%current_permissions);
                   10835:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  10836:     return \%current_permissions;
                   10837: }
                   10838: 
                   10839: #---------------------------------------------Get portfolio file access controls
                   10840: 
1.749     raeburn  10841: sub get_access_controls {
1.745     raeburn  10842:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 10843:     my %access;
                   10844:     my $real_file = $file;
                   10845:     $file =~ s/\.meta$//;
1.745     raeburn  10846:     if (defined($file)) {
1.749     raeburn  10847:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   10848:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 10849:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  10850:             }
                   10851:         }
1.745     raeburn  10852:     } else {
1.749     raeburn  10853:         foreach my $key (keys(%{$current_permissions})) {
                   10854:             if ($key =~ /\0accesscontrol$/) {
                   10855:                 if (defined($group)) {
                   10856:                     if ($key !~ m-^\Q$group\E/-) {
                   10857:                         next;
                   10858:                     }
                   10859:                 }
                   10860:                 my ($fullpath) = split(/\0/,$key);
                   10861:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   10862:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   10863:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   10864:                     }
                   10865:                 }
                   10866:             }
                   10867:         }
                   10868:     }
                   10869:     return %access;
                   10870: }
                   10871: 
                   10872: sub modify_access_controls {
                   10873:     my ($file_name,$changes,$domain,$user)=@_;
                   10874:     my ($outcome,$deloutcome);
                   10875:     my %store_permissions;
                   10876:     my %new_values;
                   10877:     my %new_control;
                   10878:     my %translation;
                   10879:     my @deletions = ();
                   10880:     my $now = time;
                   10881:     if (exists($$changes{'activate'})) {
                   10882:         if (ref($$changes{'activate'}) eq 'HASH') {
                   10883:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   10884:             my $numnew = scalar(@newitems);
                   10885:             for (my $i=0; $i<$numnew; $i++) {
                   10886:                 my $newkey = $newitems[$i];
                   10887:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  10888:                 if ($newkey =~ /^\d+:/) { 
                   10889:                     $newkey =~ s/^(\d+)/$newid/;
                   10890:                     $translation{$1} = $newid;
                   10891:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   10892:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   10893:                     $translation{$1} = $newid;
                   10894:                 }
1.749     raeburn  10895:                 $new_values{$file_name."\0".$newkey} = 
                   10896:                                           $$changes{'activate'}{$newitems[$i]};
                   10897:                 $new_control{$newkey} = $now;
                   10898:             }
                   10899:         }
                   10900:     }
                   10901:     my %todelete;
                   10902:     my %changed_items;
                   10903:     foreach my $action ('delete','update') {
                   10904:         if (exists($$changes{$action})) {
                   10905:             if (ref($$changes{$action}) eq 'HASH') {
                   10906:                 foreach my $key (keys(%{$$changes{$action}})) {
                   10907:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   10908:                     if ($action eq 'delete') { 
                   10909:                         $todelete{$itemnum} = 1;
                   10910:                     } else {
                   10911:                         $changed_items{$itemnum} = $key;
                   10912:                     }
                   10913:                 }
1.745     raeburn  10914:             }
                   10915:         }
1.749     raeburn  10916:     }
                   10917:     # get lock on access controls for file.
                   10918:     my $lockhash = {
                   10919:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   10920:                                                        ':'.$env{'user.domain'},
                   10921:                    }; 
                   10922:     my $tries = 0;
                   10923:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   10924:    
1.1288    damieng  10925:     while (($gotlock ne 'ok') && $tries < 10) {
1.749     raeburn  10926:         $tries ++;
1.1289    damieng  10927:         sleep(0.1);
1.749     raeburn  10928:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   10929:     }
                   10930:     if ($gotlock eq 'ok') {
                   10931:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   10932:         my ($tmp)=keys(%curr_permissions);
                   10933:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   10934:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   10935:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   10936:             if (ref($curr_controls) eq 'HASH') {
                   10937:                 foreach my $control_item (keys(%{$curr_controls})) {
                   10938:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   10939:                     if (defined($todelete{$itemnum})) {
                   10940:                         push(@deletions,$file_name."\0".$control_item);
                   10941:                     } else {
                   10942:                         if (defined($changed_items{$itemnum})) {
                   10943:                             $new_control{$changed_items{$itemnum}} = $now;
                   10944:                             push(@deletions,$file_name."\0".$control_item);
                   10945:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   10946:                         } else {
                   10947:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   10948:                         }
                   10949:                     }
1.745     raeburn  10950:                 }
                   10951:             }
                   10952:         }
1.970     raeburn  10953:         my ($group);
                   10954:         if (&is_course($domain,$user)) {
                   10955:             ($group,my $file) = split(/\//,$file_name,2);
                   10956:         }
1.749     raeburn  10957:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   10958:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   10959:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   10960:         #  remove lock
                   10961:         my @del_lock = ($file_name."\0".'locked_access_records');
                   10962:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  10963:         my $sqlresult =
1.970     raeburn  10964:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  10965:                                     $group);
1.749     raeburn  10966:     } else {
                   10967:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  10968:     }
1.749     raeburn  10969:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  10970: }
                   10971: 
1.827     raeburn  10972: sub make_public_indefinitely {
1.1271    raeburn  10973:     my (@requrl) = @_;
                   10974:     return &automated_portfile_access('public',\@requrl);
                   10975: }
                   10976: 
                   10977: sub automated_portfile_access {
                   10978:     my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273    raeburn  10979:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
                   10980:         return 'invalid';
                   10981:     }
1.1271    raeburn  10982:     my %urls;
                   10983:     if (ref($addsref) eq 'ARRAY') {
                   10984:         foreach my $requrl (@{$addsref}) {
                   10985:             if (&is_portfolio_url($requrl)) {
                   10986:                 unless (exists($urls{$requrl})) {
                   10987:                     $urls{$requrl} = 'add';
                   10988:                 }
                   10989:             }
                   10990:         }
                   10991:     }
                   10992:     if (ref($delsref) eq 'ARRAY') {
                   10993:         foreach my $requrl (@{$delsref}) { 
                   10994:             if (&is_portfolio_url($requrl)) {
                   10995:                 unless (exists($urls{$requrl})) {
                   10996:                     $urls{$requrl} = 'delete'; 
                   10997:                 }
                   10998:             }
                   10999:         }
                   11000:     }
                   11001:     unless (keys(%urls)) {
                   11002:         return 'invalid';
                   11003:     }
                   11004:     my $ip;
                   11005:     if ($accesstype eq 'ip') {
                   11006:         if (ref($info) eq 'HASH') {
                   11007:             if ($info->{'ip'} ne '') {
                   11008:                 $ip = $info->{'ip'};
                   11009:             }
                   11010:         }
                   11011:         if ($ip eq '') {
                   11012:             return 'invalid';
                   11013:         }
                   11014:     }
                   11015:     my $errors;
1.827     raeburn  11016:     my $now = time;
1.1271    raeburn  11017:     my %current_perms;
                   11018:     foreach my $requrl (sort(keys(%urls))) {
                   11019:         my $action;
                   11020:         if ($urls{$requrl} eq 'add') {
                   11021:             $action = 'activate';
                   11022:         } else {
                   11023:             $action = 'none';
                   11024:         }
                   11025:         my $aclnum = 0;
1.827     raeburn  11026:         my (undef,$udom,$unum,$file_name,$group) =
                   11027:             &parse_portfolio_url($requrl);
1.1271    raeburn  11028:         unless (exists($current_perms{$unum.':'.$udom})) {
                   11029:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
                   11030:         }
                   11031:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827     raeburn  11032:                                                    $group,$file_name);
                   11033:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   11034:             my ($num,$scope,$end,$start) = 
                   11035:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271    raeburn  11036:             if ($scope eq $accesstype) {
                   11037:                 if (($start <= $now) && ($end == 0)) {
                   11038:                     if ($accesstype eq 'ip') {
                   11039:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
                   11040:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
                   11041:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
                   11042:                                     if ($urls{$requrl} eq 'add') {
                   11043:                                         $action = 'none';
                   11044:                                         last;
                   11045:                                     } else {
                   11046:                                         $action = 'delete';
                   11047:                                         $aclnum = $num;
                   11048:                                         last;
                   11049:                                     }
                   11050:                                 }
                   11051:                             }
                   11052:                         }
                   11053:                     } elsif ($accesstype eq 'public') {
                   11054:                         if ($urls{$requrl} eq 'add') {
                   11055:                             $action = 'none';
                   11056:                             last;
                   11057:                         } else {
                   11058:                             $action = 'delete';
                   11059:                             $aclnum = $num;
                   11060:                             last;
                   11061:                         }
                   11062:                     }
                   11063:                 } elsif ($accesstype eq 'public') {
1.827     raeburn  11064:                     $action = 'update';
                   11065:                     $aclnum = $num;
1.1271    raeburn  11066:                     last;
1.827     raeburn  11067:                 }
                   11068:             }
                   11069:         }
                   11070:         if ($action eq 'none') {
1.1271    raeburn  11071:             next;
1.827     raeburn  11072:         } else {
                   11073:             my %changes;
                   11074:             my $newend = 0;
                   11075:             my $newstart = $now;
1.1271    raeburn  11076:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827     raeburn  11077:             $changes{$action}{$newkey} = {
1.1271    raeburn  11078:                 type => $accesstype,
1.827     raeburn  11079:                 time => {
                   11080:                     start => $newstart,
                   11081:                     end   => $newend,
                   11082:                 },
                   11083:             };
1.1271    raeburn  11084:             if ($accesstype eq 'ip') {
                   11085:                 $changes{$action}{$newkey}{'ip'} = [$ip];
                   11086:             }
1.827     raeburn  11087:             my ($outcome,$deloutcome,$new_values,$translation) =
                   11088:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271    raeburn  11089:             unless ($outcome eq 'ok') {
                   11090:                 $errors .= $outcome.' ';
                   11091:             }
1.827     raeburn  11092:         }
1.1271    raeburn  11093:     }
                   11094:     if ($errors) {
                   11095:         $errors =~ s/\s$//;
                   11096:         return $errors;
1.827     raeburn  11097:     } else {
1.1271    raeburn  11098:         return 'ok';
1.827     raeburn  11099:     }
                   11100: }
                   11101: 
1.745     raeburn  11102: #------------------------------------------------------Get Marked as Read Only
                   11103: 
                   11104: sub get_marked_as_readonly {
                   11105:     my ($domain,$user,$what,$group) = @_;
                   11106:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 11107:     my @readonly_files;
1.629     banghart 11108:     my $cmp1=$what;
                   11109:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  11110:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11111:         if (defined($group)) {
                   11112:             if ($file_name !~ m-^\Q$group\E/-) {
                   11113:                 next;
                   11114:             }
                   11115:         }
1.561     banghart 11116:         if (ref($value) eq "ARRAY"){
                   11117:             foreach my $stored_what (@{$value}) {
1.629     banghart 11118:                 my $cmp2=$stored_what;
1.759     albertel 11119:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  11120:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  11121:                 }
1.629     banghart 11122:                 if ($cmp1 eq $cmp2) {
1.561     banghart 11123:                     push(@readonly_files, $file_name);
1.745     raeburn  11124:                     last;
1.563     banghart 11125:                 } elsif (!defined($what)) {
                   11126:                     push(@readonly_files, $file_name);
1.745     raeburn  11127:                     last;
1.561     banghart 11128:                 }
                   11129:             }
1.745     raeburn  11130:         }
1.561     banghart 11131:     }
                   11132:     return @readonly_files;
                   11133: }
1.577     banghart 11134: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 11135: 
1.577     banghart 11136: sub get_marked_as_readonly_hash {
1.745     raeburn  11137:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 11138:     my %readonly_files;
1.745     raeburn  11139:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   11140:         if (defined($group)) {
                   11141:             if ($file_name !~ m-^\Q$group\E/-) {
                   11142:                 next;
                   11143:             }
                   11144:         }
1.577     banghart 11145:         if (ref($value) eq "ARRAY"){
                   11146:             foreach my $stored_what (@{$value}) {
1.745     raeburn  11147:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 11148:                     foreach my $lock_descriptor(@{$stored_what}) {
                   11149:                         if ($lock_descriptor eq 'graded') {
                   11150:                             $readonly_files{$file_name} = 'graded';
                   11151:                         } elsif ($lock_descriptor eq 'handback') {
                   11152:                             $readonly_files{$file_name} = 'handback';
                   11153:                         } else {
                   11154:                             if (!exists($readonly_files{$file_name})) {
                   11155:                                 $readonly_files{$file_name} = 'locked';
                   11156:                             }
                   11157:                         }
1.745     raeburn  11158:                     }
1.750     banghart 11159:                 } 
1.577     banghart 11160:             }
                   11161:         } 
                   11162:     }
                   11163:     return %readonly_files;
                   11164: }
1.559     banghart 11165: # ------------------------------------------------------------ Unmark as Read Only
                   11166: 
                   11167: sub unmark_as_readonly {
1.629     banghart 11168:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   11169:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  11170:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 11171:     $file_name = &declutter_portfile($file_name);
1.634     albertel 11172:     my $symb_crs = $what;
                   11173:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  11174:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 11175:     my ($tmp)=keys(%current_permissions);
                   11176:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  11177:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 11178:     foreach my $file (@readonly_files) {
1.759     albertel 11179: 	my $clean_file = &declutter_portfile($file);
                   11180: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 11181: 	my $current_locks = $current_permissions{$file};
1.563     banghart 11182:         my @new_locks;
                   11183:         my @del_keys;
                   11184:         if (ref($current_locks) eq "ARRAY"){
                   11185:             foreach my $locker (@{$current_locks}) {
1.632     albertel 11186:                 my $compare=$locker;
1.749     raeburn  11187:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  11188:                     $compare=join('',@{$locker});
1.746     raeburn  11189:                     if ($compare ne $symb_crs) {
                   11190:                         push(@new_locks, $locker);
                   11191:                     }
1.563     banghart 11192:                 }
                   11193:             }
1.650     albertel 11194:             if (scalar(@new_locks) > 0) {
1.563     banghart 11195:                 $current_permissions{$file} = \@new_locks;
                   11196:             } else {
                   11197:                 push(@del_keys, $file);
1.613     albertel 11198:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 11199:                 delete($current_permissions{$file});
1.563     banghart 11200:             }
                   11201:         }
1.561     banghart 11202:     }
1.613     albertel 11203:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 11204:     return;
                   11205: }
1.512     banghart 11206: 
1.17      www      11207: # ------------------------------------------------------------ Directory lister
                   11208: 
                   11209: sub dirlist {
1.955     raeburn  11210:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      11211:     $uri=~s/^\///;
                   11212:     $uri=~s/\/$//;
1.253     stredwic 11213:     my ($udom, $uname);
1.955     raeburn  11214:     if ($getuserdir) {
1.253     stredwic 11215:         $udom = $userdomain;
                   11216:         $uname = $username;
1.955     raeburn  11217:     } else {
                   11218:         (undef,$udom,$uname)=split(/\//,$uri);
                   11219:         if(defined($userdomain)) {
                   11220:             $udom = $userdomain;
                   11221:         }
                   11222:         if(defined($username)) {
                   11223:             $uname = $username;
                   11224:         }
1.253     stredwic 11225:     }
1.955     raeburn  11226:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 11227: 
1.955     raeburn  11228:     $dirRoot = $perlvar{'lonDocRoot'};
                   11229:     if (defined($getpropath)) {
                   11230:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 11231:         $dirRoot =~ s/\/$//;
1.955     raeburn  11232:     } elsif (defined($getuserdir)) {
                   11233:         my $subdir=$uname.'__';
                   11234:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   11235:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   11236:                    ."/$udom/$subdir/$uname";
                   11237:     } elsif (defined($alternateRoot)) {
                   11238:         $dirRoot = $alternateRoot;
1.751     banghart 11239:     }
1.253     stredwic 11240: 
                   11241:     if($udom) {
                   11242:         if($uname) {
1.1135    raeburn  11243:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  11244:             if ($uhome eq 'no_host') {
                   11245:                 return ([],'no_host');
                   11246:             }
1.955     raeburn  11247:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  11248:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  11249:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  11250:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  11251:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  11252:             } else {
                   11253:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11254:             }
1.605     matthew  11255:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  11256:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  11257:                 @listing_results = split(/:/,$listing);
                   11258:             } else {
                   11259:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11260:             }
1.1135    raeburn  11261:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  11262:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   11263:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   11264:                 return ([],$listing);
                   11265:             } else {
                   11266:                 return (\@listing_results);
1.1135    raeburn  11267:             }
1.955     raeburn  11268:         } elsif(!$alternateRoot) {
1.1136    raeburn  11269:             my (%allusers,%listerror);
1.841     albertel 11270: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  11271:  	    foreach my $tryserver (keys(%servers)) {
                   11272:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   11273:                                   &escape($udom),$tryserver);
                   11274:                 if ($listing eq 'unknown_cmd') {
                   11275: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   11276: 				      $udom, $tryserver);
                   11277:                 } else {
                   11278:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   11279:                 }
1.841     albertel 11280: 		if ($listing eq 'unknown_cmd') {
                   11281: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   11282: 				      $udom, $tryserver);
                   11283: 		    @listing_results = split(/:/,$listing);
                   11284: 		} else {
                   11285: 		    @listing_results =
                   11286: 			map { &unescape($_); } split(/:/,$listing);
                   11287: 		}
1.1136    raeburn  11288:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   11289:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   11290:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   11291:                     $listerror{$tryserver} = $listing;
                   11292:                 } else {
1.841     albertel 11293: 		    foreach my $line (@listing_results) {
                   11294: 			my ($entry) = split(/&/,$line,2);
                   11295: 			$allusers{$entry} = 1;
                   11296: 		    }
                   11297: 		}
1.253     stredwic 11298:             }
1.1136    raeburn  11299:             my @alluserslist=();
1.800     albertel 11300:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  11301:                 push(@alluserslist,$user.'&user');
1.253     stredwic 11302:             }
1.1318    droeschl 11303: 
                   11304:             if (!%listerror) {
                   11305:                 # no errors
                   11306:                 return (\@alluserslist);
                   11307:             } elsif (scalar(keys(%servers)) == 1) {
                   11308:                 # one library server, one error 
                   11309:                 my ($key) = keys(%listerror);
                   11310:                 return (\@alluserslist, $listerror{$key});
                   11311:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
                   11312:                 # con_lost indicates that we might miss data from at least one
                   11313:                 # library server
                   11314:                 return (\@alluserslist, 'con_lost');
                   11315:             } else {
                   11316:                 # multiple library servers and no con_lost -> data should be
                   11317:                 # complete. 
                   11318:                 return (\@alluserslist);
                   11319:             }
                   11320: 
1.253     stredwic 11321:         } else {
1.1136    raeburn  11322:             return ([],'missing username');
1.253     stredwic 11323:         }
1.955     raeburn  11324:     } elsif(!defined($getpropath)) {
1.1136    raeburn  11325:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   11326:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   11327:         return (\@all_domains);
1.955     raeburn  11328:     } else {
1.1136    raeburn  11329:         return ([],'missing domain');
1.275     stredwic 11330:     }
                   11331: }
                   11332: 
                   11333: # --------------------------------------------- GetFileTimestamp
                   11334: # This function utilizes dirlist and returns the date stamp for
                   11335: # when it was last modified.  It will also return an error of -1
                   11336: # if an error occurs
                   11337: 
                   11338: sub GetFileTimestamp {
1.955     raeburn  11339:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 11340:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   11341:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  11342:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   11343:                                     undef,$getuserdir);
                   11344:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   11345:         return -1;
                   11346:     }
                   11347:     if (ref($fileref) eq 'ARRAY') {
                   11348:         my @stats = split('&',$fileref->[0]);
1.375     matthew  11349:         # @stats contains first the filename, then the stat output
                   11350:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 11351:     } else {
                   11352:         return -1;
1.253     stredwic 11353:     }
1.26      www      11354: }
                   11355: 
1.712     albertel 11356: sub stat_file {
                   11357:     my ($uri) = @_;
1.787     albertel 11358:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 11359: 
1.955     raeburn  11360:     my ($udom,$uname,$file);
1.712     albertel 11361:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   11362: 	($udom,$uname,$file) =
1.811     albertel 11363: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 11364: 	$file = 'userfiles/'.$file;
                   11365:     }
                   11366:     if ($uri =~ m-^/res/-) {
                   11367: 	($udom,$uname) = 
1.807     albertel 11368: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 11369: 	$file = $uri;
                   11370:     }
                   11371: 
                   11372:     if (!$udom || !$uname || !$file) {
                   11373: 	# unable to handle the uri
                   11374: 	return ();
                   11375:     }
1.956     raeburn  11376:     my $getpropath;
                   11377:     if ($file =~ /^userfiles\//) {
                   11378:         $getpropath = 1;
                   11379:     }
1.1136    raeburn  11380:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   11381:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   11382:         return ();
                   11383:     } else {
                   11384:         if (ref($listref) eq 'ARRAY') {
                   11385:             my @stats = split('&',$listref->[0]);
                   11386: 	    shift(@stats); #filename is first
                   11387: 	    return @stats;
                   11388:         }
1.712     albertel 11389:     }
                   11390:     return ();
                   11391: }
                   11392: 
1.1313    raeburn  11393: # --------------------------------------------------------- recursedirs
                   11394: # Recursive function to traverse either a specific user's Authoring Space
                   11395: # or corresponding Published Resource Space, and populate the hash ref:
                   11396: # $dirhashref with URLs of all directories, and if $filehashref hash
                   11397: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
                   11398: # or .rights files in resource space, and .meta, .save, .log, and .bak
                   11399: # files in Authoring Space.
                   11400: #
                   11401: # Inputs:
                   11402: #
                   11403: # $is_home - true if current server is home server for user's space
                   11404: # $context - either: priv, or res respectively for Authoring or Resource Space.
                   11405: # $docroot - Document root (i.e., /home/httpd/html
                   11406: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
                   11407: # $relpath - Current path (relative to top level).
                   11408: # $dirhashref - reference to hash to populate with URLs of directories (Required)
                   11409: # $filehashref - reference to hash to populate with URLs of files (Optional)
                   11410: #
                   11411: # Returns: nothing
                   11412: #
                   11413: # Side Effects: populates $dirhashref, and $filehashref (if provided).
                   11414: #
                   11415: # Currently used by interface/londocs.pm to create linked select boxes for
                   11416: # directory and filename to import a Course "Author" resource into a course, and
                   11417: # also to create linked select boxes for Authoring Space and Directory to choose
                   11418: # save location for creation of a new "standard" problem from the Course Editor.
                   11419: #
                   11420: 
                   11421: sub recursedirs {
                   11422:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
                   11423:     return unless (ref($dirhashref) eq 'HASH');
                   11424:     my $currpath = $docroot.$toppath;
                   11425:     if ($relpath) {
                   11426:         $currpath .= "/$relpath";
                   11427:     }
                   11428:     my $savefile;
                   11429:     if (ref($filehashref)) {
                   11430:         $savefile = 1;
                   11431:     }
                   11432:     if ($is_home) {
                   11433:         if (opendir(my $dirh,$currpath)) {
                   11434:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
                   11435:                 next if ($item eq '');
                   11436:                 if (-d "$currpath/$item") {
                   11437:                     my $newpath;
                   11438:                     if ($relpath) {
                   11439:                         $newpath = "$relpath/$item";
                   11440:                     } else {
                   11441:                         $newpath = $item;
                   11442:                     }
                   11443:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   11444:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   11445:                 } elsif ($savefile) {
                   11446:                     if ($context eq 'priv') {
                   11447:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   11448:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   11449:                         }
                   11450:                     } else {
                   11451:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
                   11452:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   11453:                         }
                   11454:                     }
                   11455:                 }
                   11456:             }
                   11457:             closedir($dirh);
                   11458:         }
                   11459:     } else {
                   11460:         my ($dirlistref,$listerror) =
                   11461:             &dirlist($toppath.$relpath);
                   11462:         my @dir_lines;
                   11463:         my $dirptr=16384;
                   11464:         if (ref($dirlistref) eq 'ARRAY') {
                   11465:             foreach my $dir_line (sort
                   11466:                               {
                   11467:                                   my ($afile)=split('&',$a,2);
                   11468:                                   my ($bfile)=split('&',$b,2);
                   11469:                                   return (lc($afile) cmp lc($bfile));
                   11470:                               } (@{$dirlistref})) {
                   11471:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
                   11472:                     split(/\&/,$dir_line,16);
                   11473:                 $item =~ s/\s+$//;
                   11474:                 next if (($item =~ /^\.\.?$/) || ($obs));
                   11475:                 if ($dirptr&$testdir) {
                   11476:                     my $newpath;
                   11477:                     if ($relpath) {
                   11478:                         $newpath = "$relpath/$item";
                   11479:                     } else {
                   11480:                         $relpath = '/';
                   11481:                         $newpath = $item;
                   11482:                     }
                   11483:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   11484:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   11485:                 } elsif ($savefile) {
                   11486:                     if ($context eq 'priv') {
                   11487:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   11488:                             $filehashref->{$relpath}{$item} = 1;
                   11489:                         }
                   11490:                     } else {
                   11491:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
                   11492:                             $filehashref->{$relpath}{$item} = 1;
                   11493:                         }
                   11494:                     }
                   11495:                 }
                   11496:             }
                   11497:         }
                   11498:     }
                   11499:     return;
                   11500: }
                   11501: 
1.26      www      11502: # -------------------------------------------------------- Value of a Condition
                   11503: 
1.713     albertel 11504: # gets the value of a specific preevaluated condition
                   11505: #    stored in the string  $env{user.state.<cid>}
                   11506: # or looks up a condition reference in the bighash and if if hasn't
                   11507: # already been evaluated recurses into docondval to get the value of
                   11508: # the condition, then memoizing it to 
                   11509: #   $env{user.state.<cid>.<condition>}
1.40      www      11510: sub directcondval {
                   11511:     my $number=shift;
1.620     albertel 11512:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 11513: 	&Apache::lonuserstate::evalstate();
                   11514:     }
1.713     albertel 11515:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   11516: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   11517:     } elsif ($number =~ /^_/) {
                   11518: 	my $sub_condition;
                   11519: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   11520: 		&GDBM_READER(),0640)) {
                   11521: 	    $sub_condition=$bighash{'conditions'.$number};
                   11522: 	    untie(%bighash);
                   11523: 	}
                   11524: 	my $value = &docondval($sub_condition);
1.949     raeburn  11525: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 11526: 	return $value;
                   11527:     }
1.620     albertel 11528:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   11529:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      11530:     } else {
                   11531:        return 2;
                   11532:     }
                   11533: }
                   11534: 
1.713     albertel 11535: # get the collection of conditions for this resource
1.26      www      11536: sub condval {
                   11537:     my $condidx=shift;
1.54      www      11538:     my $allpathcond='';
1.713     albertel 11539:     foreach my $cond (split(/\|/,$condidx)) {
                   11540: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   11541: 	    $allpathcond.=
                   11542: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   11543: 	}
1.191     harris41 11544:     }
1.54      www      11545:     $allpathcond=~s/\|$//;
1.713     albertel 11546:     return &docondval($allpathcond);
                   11547: }
                   11548: 
                   11549: #evaluates an expression of conditions
                   11550: sub docondval {
                   11551:     my ($allpathcond) = @_;
                   11552:     my $result=0;
                   11553:     if ($env{'request.course.id'}
                   11554: 	&& defined($allpathcond)) {
                   11555: 	my $operand='|';
                   11556: 	my @stack;
                   11557: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   11558: 	    if ($chunk eq '(') {
                   11559: 		push @stack,($operand,$result);
                   11560: 	    } elsif ($chunk eq ')') {
                   11561: 		my $before=pop @stack;
                   11562: 		if (pop @stack eq '&') {
                   11563: 		    $result=$result>$before?$before:$result;
                   11564: 		} else {
                   11565: 		    $result=$result>$before?$result:$before;
                   11566: 		}
                   11567: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   11568: 		$operand=$chunk;
                   11569: 	    } else {
                   11570: 		my $new=directcondval($chunk);
                   11571: 		if ($operand eq '&') {
                   11572: 		    $result=$result>$new?$new:$result;
                   11573: 		} else {
                   11574: 		    $result=$result>$new?$result:$new;
                   11575: 		}
                   11576: 	    }
                   11577: 	}
1.26      www      11578:     }
                   11579:     return $result;
1.421     albertel 11580: }
                   11581: 
                   11582: # ---------------------------------------------------- Devalidate courseresdata
                   11583: 
                   11584: sub devalidatecourseresdata {
                   11585:     my ($coursenum,$coursedomain)=@_;
                   11586:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 11587:     &devalidate_cache_new('courseres',$hashid);
1.28      www      11588: }
                   11589: 
1.763     www      11590: 
1.200     www      11591: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     11592: #
                   11593: #  Parameters:
                   11594: #      $coursenum    - Number of the course.
                   11595: #      $coursedomain - Domain at which the course was created.
                   11596: #  Returns:
                   11597: #     A hash of the course parameters along (I think) with timestamps
                   11598: #     and version info.
1.877     foxr     11599: 
1.624     albertel 11600: sub get_courseresdata {
                   11601:     my ($coursenum,$coursedomain)=@_;
1.200     www      11602:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   11603:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 11604:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 11605:     my %dumpreply;
1.417     albertel 11606:     unless (defined($cached)) {
1.624     albertel 11607: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 11608: 	$result=\%dumpreply;
1.251     albertel 11609: 	my ($tmp) = keys(%dumpreply);
                   11610: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 11611: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 11612: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   11613: 	    return $tmp;
1.416     albertel 11614: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 11615: 	    $result=undef;
1.599     albertel 11616: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 11617: 	}
                   11618:     }
1.624     albertel 11619:     return $result;
                   11620: }
                   11621: 
1.633     albertel 11622: sub devalidateuserresdata {
                   11623:     my ($uname,$udom)=@_;
                   11624:     my $hashid="$udom:$uname";
                   11625:     &devalidate_cache_new('userres',$hashid);
                   11626: }
                   11627: 
1.624     albertel 11628: sub get_userresdata {
                   11629:     my ($uname,$udom)=@_;
                   11630:     #most student don\'t have any data set, check if there is some data
                   11631:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   11632: 
                   11633:     my $hashid="$udom:$uname";
                   11634:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   11635:     if (!defined($cached)) {
                   11636: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   11637: 	$result=\%resourcedata;
                   11638: 	&do_cache_new('userres',$hashid,$result,600);
                   11639:     }
                   11640:     my ($tmp)=keys(%$result);
                   11641:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   11642: 	return $result;
                   11643:     }
                   11644:     #error 2 occurs when the .db doesn't exist
                   11645:     if ($tmp!~/error: 2 /) {
1.1294    raeburn  11646:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
                   11647: 	    &logthis("<font color=\"blue\">WARNING:".
                   11648: 		     " Trying to get resource data for ".
                   11649: 		     $uname." at ".$udom.": ".
                   11650: 		     $tmp."</font>");
                   11651:         }
1.624     albertel 11652:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 11653: 	#&EXT_cache_set($udom,$uname);
                   11654: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 11655: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 11656:     }
                   11657:     return $tmp;
                   11658: }
1.879     foxr     11659: #----------------------------------------------- resdata - return resource data
                   11660: #  Purpose:
                   11661: #    Return resource data for either users or for a course.
                   11662: #  Parameters:
                   11663: #     $name      - Course/user name.
                   11664: #     $domain    - Name of the domain the user/course is registered on.
1.1311    raeburn  11665: #     $type      - Type of thing $name is (must be 'course' or 'user')
1.1301    raeburn  11666: #     $mapp      - decluttered URL of enclosing map  
                   11667: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
                   11668: #     $recurseup - Ref to array of map URLs, starting with map containing
                   11669: #                  $mapp up through hierarchy of nested maps to top level map.  
                   11670: #     $courseid  - CourseID (first part of param identifier).
                   11671: #     $modifier  - Middle part of param identifier.
                   11672: #     $what      - Last part of param identifier.
1.879     foxr     11673: #     @which     - Array of names of resources desired.
                   11674: #  Returns:
                   11675: #     The value of the first reasource in @which that is found in the
                   11676: #     resource hash.
                   11677: #  Exceptional Conditions:
                   11678: #     If the $type passed in is not valid (not the string 'course' or 
                   11679: #     'user', an undefined  reference is returned.
                   11680: #     If none of the resources are found, an undef is returned
1.624     albertel 11681: sub resdata {
1.1301    raeburn  11682:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
                   11683:         $modifier,$what,@which)=@_;
1.624     albertel 11684:     my $result;
                   11685:     if ($type eq 'course') {
                   11686: 	$result=&get_courseresdata($name,$domain);
                   11687:     } elsif ($type eq 'user') {
                   11688: 	$result=&get_userresdata($name,$domain);
                   11689:     }
                   11690:     if (!ref($result)) { return $result; }    
1.251     albertel 11691:     foreach my $item (@which) {
1.1301    raeburn  11692:         if ($item->[1] eq 'course') {
                   11693:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
                   11694:                 unless ($$recursed) {
1.1302    raeburn  11695:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301    raeburn  11696:                     $$recursed = 1;
                   11697:                 }
                   11698:                 foreach my $item (@${recurseup}) {
                   11699:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
                   11700:                     last if (defined($result->{$norecursechk}));
                   11701:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
                   11702:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
                   11703:                 }
                   11704:             }
                   11705:         }
                   11706:         if (defined($result->{$item->[0]})) {
1.927     albertel 11707: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 11708: 	}
1.250     albertel 11709:     }
1.291     albertel 11710:     return undef;
1.200     www      11711: }
                   11712: 
1.1360    raeburn  11713: sub get_domain_lti {
                   11714:     my ($cdom,$context) = @_;
                   11715:     my ($name,%lti);
                   11716:     if ($context eq 'consumer') {
                   11717:         $name = 'ltitools';
                   11718:     } elsif ($context eq 'provider') {
                   11719:         $name = 'lti';
                   11720:     } else {
                   11721:         return %lti;
                   11722:     }
                   11723:     my ($result,$cached)=&is_cached_new($name,$cdom);
1.1298    raeburn  11724:     if (defined($cached)) {
                   11725:         if (ref($result) eq 'HASH') {
1.1360    raeburn  11726:             %lti = %{$result};
1.1298    raeburn  11727:         }
                   11728:     } else {
1.1360    raeburn  11729:         my %domconfig = &get_dom('configuration',[$name],$cdom);
                   11730:         if (ref($domconfig{$name}) eq 'HASH') {
                   11731:             %lti = %{$domconfig{$name}};
                   11732:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
                   11733:             if (ref($encdomconfig{$name}) eq 'HASH') {
                   11734:                 foreach my $id (keys(%lti)) {
                   11735:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
1.1344    raeburn  11736:                         foreach my $item ('key','secret') {
1.1360    raeburn  11737:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
1.1344    raeburn  11738:                         }
                   11739:                     }
                   11740:                 }
                   11741:             }
1.1298    raeburn  11742:         }
                   11743:         my $cachetime = 24*60*60;
1.1360    raeburn  11744:         &do_cache_new($name,$cdom,\%lti,$cachetime);
1.1298    raeburn  11745:     }
1.1360    raeburn  11746:     return %lti;
1.1298    raeburn  11747: }
                   11748: 
1.1236    raeburn  11749: sub get_numsuppfiles {
                   11750:     my ($cnum,$cdom,$ignorecache)=@_;
                   11751:     my $hashid=$cnum.':'.$cdom;
                   11752:     my ($suppcount,$cached);
                   11753:     unless ($ignorecache) {
                   11754:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
                   11755:     }
                   11756:     unless (defined($cached)) {
                   11757:         my $chome=&homeserver($cnum,$cdom);
                   11758:         unless ($chome eq 'no_host') {
1.1366    raeburn  11759:             ($suppcount,my $supptools,my $errors) = (0,0,0);
1.1236    raeburn  11760:             my $suppmap = 'supplemental.sequence';
1.1366    raeburn  11761:             ($suppcount,$supptools,$errors) =
                   11762:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
                   11763:                                                          $supptools,$errors);
1.1236    raeburn  11764:         }
                   11765:         &do_cache_new('suppcount',$hashid,$suppcount,600);
                   11766:     }
                   11767:     return $suppcount;
                   11768: }
                   11769: 
1.379     matthew  11770: #
                   11771: # EXT resource caching routines
                   11772: #
                   11773: 
1.1302    raeburn  11774: {
                   11775: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
                   11776: #
                   11777: # The course for which we cache
                   11778: my $cachedmapkey='';
                   11779: # The cached recursive maps for this course
                   11780: my %cachedmaps=();
                   11781: # When this was last done
                   11782: my $cachedmaptime='';
                   11783: 
1.379     matthew  11784: sub clear_EXT_cache_status {
1.383     albertel 11785:     &delenv('cache.EXT.');
1.379     matthew  11786: }
                   11787: 
                   11788: sub EXT_cache_status {
                   11789:     my ($target_domain,$target_user) = @_;
1.383     albertel 11790:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 11791:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  11792:         # We know already the user has no data
                   11793:         return 1;
                   11794:     } else {
                   11795:         return 0;
                   11796:     }
                   11797: }
                   11798: 
                   11799: sub EXT_cache_set {
                   11800:     my ($target_domain,$target_user) = @_;
1.383     albertel 11801:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  11802:     #&appenv({$cachename => time});
1.379     matthew  11803: }
                   11804: 
1.28      www      11805: # --------------------------------------------------------- Value of a Variable
1.58      www      11806: sub EXT {
1.715     albertel 11807: 
1.1228    raeburn  11808:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68      www      11809:     unless ($varname) { return ''; }
1.218     albertel 11810:     #get real user name/domain, courseid and symb
                   11811:     my $courseid;
1.359     albertel 11812:     my $publicuser;
1.427     www      11813:     if ($symbparm) {
                   11814: 	$symbparm=&get_symb_from_alias($symbparm);
                   11815:     }
1.218     albertel 11816:     if (!($uname && $udom)) {
1.790     albertel 11817:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 11818:       if (!$symbparm) {	$symbparm=$cursymb; }
                   11819:     } else {
1.620     albertel 11820: 	$courseid=$env{'request.course.id'};
1.218     albertel 11821:     }
1.48      www      11822:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   11823:     my $rest;
1.320     albertel 11824:     if (defined($therest[0])) {
1.48      www      11825:        $rest=join('.',@therest);
                   11826:     } else {
                   11827:        $rest='';
                   11828:     }
1.320     albertel 11829: 
1.57      www      11830:     my $qualifierrest=$qualifier;
                   11831:     if ($rest) { $qualifierrest.='.'.$rest; }
                   11832:     my $spacequalifierrest=$space;
                   11833:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      11834:     if ($realm eq 'user') {
1.48      www      11835: # --------------------------------------------------------------- user.resource
                   11836: 	if ($space eq 'resource') {
1.651     albertel 11837: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   11838: 		  || defined($Apache::lonhomework::parsing_a_task))
                   11839: 		 &&
1.744     albertel 11840: 		 ($symbparm eq &symbread()) ) {	
                   11841: 		# if we are in the middle of processing the resource the
                   11842: 		# get the value we are planning on committing
                   11843:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   11844:                     return $Apache::lonhomework::results{$qualifierrest};
                   11845:                 } else {
                   11846:                     return $Apache::lonhomework::history{$qualifierrest};
                   11847:                 }
1.335     albertel 11848: 	    } else {
1.359     albertel 11849: 		my %restored;
1.620     albertel 11850: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 11851: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   11852: 		} else {
                   11853: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   11854: 		}
1.335     albertel 11855: 		return $restored{$qualifierrest};
                   11856: 	    }
1.48      www      11857: # ----------------------------------------------------------------- user.access
                   11858:         } elsif ($space eq 'access') {
1.218     albertel 11859: 	    # FIXME - not supporting calls for a specific user
1.48      www      11860:             return &allowed($qualifier,$rest);
                   11861: # ------------------------------------------ user.preferences, user.environment
                   11862:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 11863: 	    if (($uname eq $env{'user.name'}) &&
                   11864: 		($udom eq $env{'user.domain'})) {
                   11865: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 11866: 	    } else {
1.359     albertel 11867: 		my %returnhash;
                   11868: 		if (!$publicuser) {
                   11869: 		    %returnhash=&userenvironment($udom,$uname,
                   11870: 						 $qualifierrest);
                   11871: 		}
1.218     albertel 11872: 		return $returnhash{$qualifierrest};
                   11873: 	    }
1.48      www      11874: # ----------------------------------------------------------------- user.course
                   11875:         } elsif ($space eq 'course') {
1.218     albertel 11876: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 11877:             return $env{join('.',('request.course',$qualifier))};
1.48      www      11878: # ------------------------------------------------------------------- user.role
                   11879:         } elsif ($space eq 'role') {
1.218     albertel 11880: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 11881:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      11882:             if ($qualifier eq 'value') {
                   11883: 		return $role;
                   11884:             } elsif ($qualifier eq 'extent') {
                   11885:                 return $where;
                   11886:             }
                   11887: # ----------------------------------------------------------------- user.domain
                   11888:         } elsif ($space eq 'domain') {
1.218     albertel 11889:             return $udom;
1.48      www      11890: # ------------------------------------------------------------------- user.name
                   11891:         } elsif ($space eq 'name') {
1.218     albertel 11892:             return $uname;
1.48      www      11893: # ---------------------------------------------------- Any other user namespace
1.29      www      11894:         } else {
1.359     albertel 11895: 	    my %reply;
                   11896: 	    if (!$publicuser) {
                   11897: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   11898: 	    }
                   11899: 	    return $reply{$qualifierrest};
1.48      www      11900:         }
1.236     www      11901:     } elsif ($realm eq 'query') {
                   11902: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 11903:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   11904: 						[$spacequalifierrest]);
1.620     albertel 11905: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      11906:    } elsif ($realm eq 'request') {
1.48      www      11907: # ------------------------------------------------------------- request.browser
                   11908:         if ($space eq 'browser') {
1.1145    bisitz   11909:             return $env{'browser.'.$qualifier};
1.57      www      11910: # ------------------------------------------------------------ request.filename
                   11911:         } else {
1.620     albertel 11912:             return $env{'request.'.$spacequalifierrest};
1.29      www      11913:         }
1.28      www      11914:     } elsif ($realm eq 'course') {
1.48      www      11915: # ---------------------------------------------------------- course.description
1.620     albertel 11916:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      11917:     } elsif ($realm eq 'resource') {
1.165     www      11918: 
1.620     albertel 11919: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 11920: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   11921: 	}
1.693     albertel 11922: 
1.1232    raeburn  11923:         if ($qualifier eq '') {
                   11924: 	    if ($space eq 'title') {
                   11925: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   11926: 	        return &gettitle($symbparm);
                   11927: 	    }
1.693     albertel 11928: 	
1.1232    raeburn  11929: 	    if ($space eq 'map') {
                   11930: 	        my ($map) = &decode_symb($symbparm);
                   11931: 	        return &symbread($map);
                   11932: 	    }
                   11933:             if ($space eq 'maptitle') {
                   11934:                 my ($map) = &decode_symb($symbparm);
                   11935:                 return &gettitle($map);
                   11936:             }
                   11937: 	    if ($space eq 'filename') {
                   11938: 	        if ($symbparm) {
                   11939: 		    return &clutter((&decode_symb($symbparm))[2]);
                   11940: 	        }
                   11941: 	        return &hreflocation('',$env{'request.filename'});
1.905     albertel 11942: 	    }
1.1232    raeburn  11943: 
1.1233    raeburn  11944:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
                   11945:                 if ($space eq 'visibleparts') {
                   11946:                     my $navmap = Apache::lonnavmaps::navmap->new();
                   11947:                     my $item;
                   11948:                     if (ref($navmap)) {
                   11949:                         my $res = $navmap->getBySymb($symbparm);
                   11950:                         my $parts = $res->parts();
                   11951:                         if (ref($parts) eq 'ARRAY') {
                   11952:                             $item = join(',',@{$parts});
                   11953:                         }
                   11954:                         undef($navmap);
1.1232    raeburn  11955:                     }
1.1233    raeburn  11956:                     return $item;
1.1232    raeburn  11957:                 }
                   11958:             }
                   11959:         }
1.693     albertel 11960: 
1.1301    raeburn  11961: 	my ($section, $group, @groups, @recurseup, $recursed);
                   11962: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228    raeburn  11963:         if (($courseid eq '') && ($cid)) {
                   11964:             $courseid = $cid;
                   11965:         }
                   11966: 	if (($symbparm && $courseid) && 
                   11967: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      11968: 
1.218     albertel 11969: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      11970: 
1.60      www      11971: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 11972: 	    my $symbp=$symbparm;
1.1301    raeburn  11973: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 11974: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301    raeburn  11975:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218     albertel 11976: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620     albertel 11977: 	    if (($env{'user.name'} eq $uname) &&
                   11978: 		($env{'user.domain'} eq $udom)) {
                   11979: 		$section=$env{'request.course.sec'};
1.733     raeburn  11980:                 @groups = split(/:/,$env{'request.course.groups'});  
                   11981:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 11982: 	    } else {
1.539     albertel 11983: 		if (! defined($usection)) {
1.551     albertel 11984: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 11985: 		} else {
                   11986: 		    $section = $usection;
                   11987: 		}
1.733     raeburn  11988:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 11989: 	    }
                   11990: 
                   11991: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   11992: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301    raeburn  11993:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218     albertel 11994: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   11995: 
1.593     albertel 11996: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 11997: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.1301    raeburn  11998:             $courseleveli=$courseid.'.'.$recurseparm;
1.593     albertel 11999: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      12000: 
1.60      www      12001: # ----------------------------------------------------------- first, check user
1.624     albertel 12002: 
1.1301    raeburn  12003: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
                   12004:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
1.927     albertel 12005: 				       ([$courselevelr,'resource'],
                   12006: 					[$courselevelm,'map'     ],
1.1301    raeburn  12007:                                         [$courseleveli,'map'     ],
1.927     albertel 12008: 					[$courselevel, 'course'  ]));
1.931     albertel 12009: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      12010: 
1.594     albertel 12011: # ------------------------------------------------ second, check some of course
1.684     raeburn  12012:             my $coursereply;
1.691     raeburn  12013:             if (@groups > 0) {
                   12014:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301    raeburn  12015:                                        $recurseparm,$mapparm,$spacequalifierrest,
                   12016:                                        $mapp,\$recursed,\@recurseup);
                   12017:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
1.684     raeburn  12018:             }
1.96      www      12019: 
1.684     raeburn  12020: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 12021: 				  $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  12022: 				  'course',$mapp,\$recursed,\@recurseup,
                   12023:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
1.927     albertel 12024: 				  ([$seclevelr,   'resource'],
                   12025: 				   [$seclevelm,   'map'     ],
1.1301    raeburn  12026:                                    [$secleveli,   'map'     ],
1.927     albertel 12027: 				   [$seclevel,    'course'  ],
                   12028: 				   [$courselevelr,'resource']));
                   12029: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      12030: 
1.60      www      12031: # ------------------------------------------------------ third, check map parms
1.218     albertel 12032: 	    my %parmhash=();
                   12033: 	    my $thisparm='';
                   12034: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 12035: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 12036: 		    &GDBM_READER(),0640)) {
1.218     albertel 12037: 		$thisparm=$parmhash{$symbparm};
                   12038: 		untie(%parmhash);
                   12039: 	    }
1.927     albertel 12040: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 12041: 	}
1.594     albertel 12042: # ------------------------------------------ fourth, look in resource metadata
1.1301    raeburn  12043:  
                   12044:         my $what = $spacequalifierrest;
                   12045: 	$what=~s/\./\_/;
1.282     albertel 12046: 	my $filename;
                   12047: 	if (!$symbparm) { $symbparm=&symbread(); }
                   12048: 	if ($symbparm) {
1.409     www      12049: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 12050: 	} else {
1.620     albertel 12051: 	    $filename=$env{'request.filename'};
1.282     albertel 12052: 	}
1.1362    raeburn  12053:         my $toolsymb;
                   12054:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
                   12055:             $toolsymb = $symbparm;
                   12056:         }
                   12057: 	my $metadata=&metadata($filename,$what,$toolsymb);
1.927     albertel 12058: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1362    raeburn  12059: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927     albertel 12060: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      12061: 
1.1301    raeburn  12062: # ----------------------------------------------- fifth, look in rest of course
1.593     albertel 12063: 	if ($symbparm && defined($courseid) && 
1.620     albertel 12064: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 12065: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   12066: 				     $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  12067: 				     'course',$mapp,\$recursed,\@recurseup,
                   12068:                                      $courseid,'.',$spacequalifierrest,
1.927     albertel 12069: 				     ([$courselevelm,'map'   ],
1.1301    raeburn  12070:                                       [$courseleveli,'map'   ],
1.927     albertel 12071: 				      [$courselevel, 'course']));
                   12072: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 12073: 	}
1.145     www      12074: # ------------------------------------------------------------------ Cascade up
1.218     albertel 12075: 	unless ($space eq '0') {
1.336     albertel 12076: 	    my @parts=split(/_/,$space);
                   12077: 	    my $id=pop(@parts);
                   12078: 	    my $part=join('_',@parts);
                   12079: 	    if ($part eq '') { $part='0'; }
1.927     albertel 12080: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 12081: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  12082: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 12083: 	}
1.395     albertel 12084: 	if ($recurse) { return undef; }
1.1362    raeburn  12085: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927     albertel 12086: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      12087: # ---------------------------------------------------- Any other user namespace
                   12088:     } elsif ($realm eq 'environment') {
                   12089: # ----------------------------------------------------------------- environment
1.620     albertel 12090: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   12091: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 12092: 	} else {
1.770     albertel 12093: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   12094: 		return '';
                   12095: 	    }
1.219     albertel 12096: 	    my %returnhash=&userenvironment($udom,$uname,
                   12097: 					    $spacequalifierrest);
                   12098: 	    return $returnhash{$spacequalifierrest};
                   12099: 	}
1.28      www      12100:     } elsif ($realm eq 'system') {
1.48      www      12101: # ----------------------------------------------------------------- system.time
                   12102: 	if ($space eq 'time') {
                   12103: 	    return time;
                   12104:         }
1.696     albertel 12105:     } elsif ($realm eq 'server') {
                   12106: # ----------------------------------------------------------------- system.time
                   12107: 	if ($space eq 'name') {
                   12108: 	    return $ENV{'SERVER_NAME'};
                   12109:         }
1.28      www      12110:     }
1.48      www      12111:     return '';
1.61      www      12112: }
                   12113: 
1.927     albertel 12114: sub get_reply {
                   12115:     my ($reply_value) = @_;
1.940     raeburn  12116:     if (ref($reply_value) eq 'ARRAY') {
                   12117:         if (wantarray) {
                   12118: 	    return @$reply_value;
                   12119:         }
                   12120:         return $reply_value->[0];
                   12121:     } else {
                   12122:         return $reply_value;
1.927     albertel 12123:     }
                   12124: }
                   12125: 
1.691     raeburn  12126: sub check_group_parms {
1.1301    raeburn  12127:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
                   12128:         $recursed,$recurseupref) = @_;
                   12129:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
                   12130:                   [$what,'course']);
                   12131:     my $coursereply;
1.691     raeburn  12132:     foreach my $group (@{$groups}) {
1.1301    raeburn  12133:         my @groupitems = ();
1.691     raeburn  12134:         foreach my $level (@levels) {
1.927     albertel 12135:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   12136:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  12137:         }
1.1301    raeburn  12138:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   12139:                                    $env{'course.'.$courseid.'.domain'},
                   12140:                                    'course',$mapp,$recursed,$recurseupref,
                   12141:                                    $courseid,'.['.$group.'].',$what,
                   12142:                                    @groupitems);
                   12143:         last if (defined($coursereply));
1.691     raeburn  12144:     }
                   12145:     return $coursereply;
                   12146: }
                   12147: 
1.1301    raeburn  12148: sub get_map_hierarchy {
1.1302    raeburn  12149:     my ($mapname,$courseid) = @_;
                   12150:     my @recurseup = ();
1.1301    raeburn  12151:     if ($mapname) {
1.1302    raeburn  12152:         if (($cachedmapkey eq $courseid) &&
                   12153:             (abs($cachedmaptime-time)<5)) {
                   12154:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
                   12155:                 return @{$cachedmaps{$mapname}};
                   12156:             }
                   12157:         }
1.1301    raeburn  12158:         my $navmap = Apache::lonnavmaps::navmap->new();
                   12159:         if (ref($navmap)) {
                   12160:             @recurseup = $navmap->recurseup_maps($mapname);
                   12161:             undef($navmap);
1.1302    raeburn  12162:             $cachedmaps{$mapname} = \@recurseup;
                   12163:             $cachedmaptime=time;
                   12164:             $cachedmapkey=$courseid;
1.1301    raeburn  12165:         }
                   12166:     }
                   12167:     return @recurseup;
                   12168: }
                   12169: 
1.1302    raeburn  12170: }
                   12171: 
1.691     raeburn  12172: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  12173:     my ($courseid,@groups) = @_;
                   12174:     @groups = sort(@groups);
1.691     raeburn  12175:     return @groups;
                   12176: }
                   12177: 
1.395     albertel 12178: sub packages_tab_default {
1.1362    raeburn  12179:     my ($uri,$varname,$toolsymb)=@_;
1.395     albertel 12180:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 12181: 
                   12182:     my (@extension,@specifics,$do_default);
1.1362    raeburn  12183:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395     albertel 12184: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 12185: 	if ($pack_type eq 'default') {
                   12186: 	    $do_default=1;
                   12187: 	} elsif ($pack_type eq 'extension') {
                   12188: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 12189: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 12190: 	    # only look at packages defaults for packages that this id is
1.738     albertel 12191: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   12192: 	}
                   12193:     }
                   12194:     # first look for a package that matches the requested part id
                   12195:     foreach my $package (@specifics) {
                   12196: 	my (undef,$pack_type,$pack_part)=@{$package};
                   12197: 	next if ($pack_part ne $part);
                   12198: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12199: 	    return $packagetab{"$pack_type&$name&default"};
                   12200: 	}
                   12201:     }
                   12202:     # look for any possible matching non extension_ package
                   12203:     foreach my $package (@specifics) {
                   12204: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 12205: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12206: 	    return $packagetab{"$pack_type&$name&default"};
                   12207: 	}
1.585     albertel 12208: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 12209: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   12210: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 12211: 	}
                   12212:     }
1.738     albertel 12213:     # look for any posible extension_ match
                   12214:     foreach my $package (@extension) {
                   12215: 	my ($package,$pack_type)=@{$package};
                   12216: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   12217: 	    return $packagetab{"$pack_type&$name&default"};
                   12218: 	}
                   12219: 	if (defined($packagetab{$package."&$name&default"})) {
                   12220: 	    return $packagetab{$package."&$name&default"};
                   12221: 	}
                   12222:     }
                   12223:     # look for a global default setting
                   12224:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   12225: 	return $packagetab{"default&$name&default"};
                   12226:     }
1.395     albertel 12227:     return undef;
                   12228: }
                   12229: 
1.334     albertel 12230: sub add_prefix_and_part {
                   12231:     my ($prefix,$part)=@_;
                   12232:     my $keyroot;
                   12233:     if (defined($prefix) && $prefix !~ /^__/) {
                   12234: 	# prefix that has a part already
                   12235: 	$keyroot=$prefix;
                   12236:     } elsif (defined($prefix)) {
                   12237: 	# prefix that is missing a part
                   12238: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   12239:     } else {
                   12240: 	# no prefix at all
                   12241: 	if (defined($part)) { $keyroot='_'.$part; }
                   12242:     }
                   12243:     return $keyroot;
                   12244: }
                   12245: 
1.71      www      12246: # ---------------------------------------------------------------- Get metadata
                   12247: 
1.599     albertel 12248: my %metaentry;
1.1070    www      12249: my %importedpartids;
1.1369    raeburn  12250: my %importedrespids;
1.71      www      12251: sub metadata {
1.1362    raeburn  12252:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71      www      12253:     $uri=&declutter($uri);
1.288     albertel 12254:     # if it is a non metadata possible uri return quickly
1.529     albertel 12255:     if (($uri eq '') || 
                   12256: 	(($uri =~ m|^/*adm/|) && 
1.1343    raeburn  12257: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108    raeburn  12258:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 12259: 	return undef;
                   12260:     }
1.1261    raeburn  12261:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
1.924     albertel 12262: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 12263: 	return undef;
1.288     albertel 12264:     }
1.73      www      12265:     my $filename=$uri;
                   12266:     $uri=~s/\.meta$//;
1.172     www      12267: #
                   12268: # Is the metadata already cached?
1.177     www      12269: # Look at timestamp of caching
1.172     www      12270: # Everything is cached by the main uri, libraries are never directly cached
                   12271: #
1.428     albertel 12272:     if (!defined($liburi)) {
1.599     albertel 12273: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 12274: 	if (defined($cached)) { return $result->{':'.$what}; }
                   12275:     }
1.1362    raeburn  12276: 
                   12277: #
                   12278: # If the uri is for an external tool the file from
                   12279: # which metadata should be retrieved depends on whether
                   12280: # the tool had been configured to be gradable (set in the Course
                   12281: # Editor or Resource Editor).
                   12282: #
                   12283: # If a valid symb has been included as the third arg in the call
                   12284: # to &metadata() that can be used to retrieve the value of
                   12285: # parameter_0_gradable set for the resource, and included in the
                   12286: # uploaded map containing the tool. The value is retrieved via
                   12287: # &EXT(), if a valid symb is available.  Otherwise the value of
                   12288: # gradable in the exttool_$marker.db file for the tool instance
1.1364    raeburn  12289: # is retrieved via &get().
                   12290: #
                   12291: # When lonuserstate::traceroute() calls lonnet::EXT() for 
                   12292: # hiddenresource and encrypturl (during course initialization)
                   12293: # the map-level parameter for resource.0.gradable included in the 
                   12294: # uploaded map containing the tool will not yet have been stored
                   12295: # in the user_course_parms.db file for the user's session, so in 
                   12296: # this case fall back to retrieving gradable status from the
                   12297: # exttool_$marker.db file.
1.1362    raeburn  12298: #
                   12299: # In order to avoid an infinite loop, &metadata() will return
                   12300: # before a call to &EXT(), if the uri is for an external tool
                   12301: # and the $what for which metadata is being requested is
                   12302: # parameter_0_gradable or 0_gradable.
                   12303: #
                   12304: 
                   12305:     if ($uri =~ /ext\.tool$/) {
                   12306:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
                   12307:             return;
                   12308:         } else {
                   12309:             my ($checked,$use_passback);
                   12310:             if ($toolsymb ne '') {
                   12311:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
1.1364    raeburn  12312:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
1.1362    raeburn  12313:                     $checked = 1;
                   12314:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
                   12315:                         $use_passback = 1;
                   12316:                     }
                   12317:                 }
                   12318:             }
                   12319:             unless ($checked) {
                   12320:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
                   12321:                 $marker=~s/\D//g;
1.1363    raeburn  12322:                 if ($marker) {
1.1362    raeburn  12323:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
                   12324:                     $use_passback = $toolsettings{'gradable'};
                   12325:                 }
                   12326:             }
                   12327:             if ($use_passback) {
                   12328:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
                   12329:             } else {
                   12330:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
                   12331:             }
                   12332:         }
                   12333:     }
                   12334: 
1.428     albertel 12335:     {
1.1069    www      12336: # Imported parts would go here
1.1369    raeburn  12337:         my @origfiletagids=();
1.1069    www      12338:         my $importedparts=0;
1.1369    raeburn  12339: 
                   12340: # Imported responseids would go here
                   12341:         my $importedresponses=0;
1.172     www      12342: #
                   12343: # Is this a recursive call for a library?
                   12344: #
1.599     albertel 12345: #	if (! exists($metacache{$uri})) {
                   12346: #	    $metacache{$uri}={};
                   12347: #	}
1.924     albertel 12348: 	my $cachetime = 60*60;
1.171     www      12349:         if ($liburi) {
                   12350: 	    $liburi=&declutter($liburi);
                   12351:             $filename=$liburi;
1.401     bowersj2 12352:         } else {
1.599     albertel 12353: 	    &devalidate_cache_new('meta',$uri);
                   12354: 	    undef(%metaentry);
1.401     bowersj2 12355: 	}
1.140     www      12356:         my %metathesekeys=();
1.73      www      12357:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 12358: 	my $metastring;
1.1140    www      12359: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 12360: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 12361: 	    $metastring = 
1.929     albertel 12362: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 12363: 					  ('grade_target' => 'meta'));
                   12364: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  12365: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   12366:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 12367: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 12368: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 12369: 	    $metastring=&getfile($file);
1.489     albertel 12370: 	}
1.208     albertel 12371:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      12372:         my $token;
1.140     www      12373:         undef %metathesekeys;
1.71      www      12374:         while ($token=$parser->get_token) {
1.339     albertel 12375: 	    if ($token->[0] eq 'S') {
                   12376: 		if (defined($token->[2]->{'package'})) {
1.172     www      12377: #
                   12378: # This is a package - get package info
                   12379: #
1.339     albertel 12380: 		    my $package=$token->[2]->{'package'};
                   12381: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12382: 		    if (defined($token->[2]->{'id'})) { 
                   12383: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   12384: 		    }
1.599     albertel 12385: 		    if ($metaentry{':packages'}) {
                   12386: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 12387: 		    } else {
1.599     albertel 12388: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 12389: 		    }
1.736     albertel 12390: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 12391: 			my $part=$keyroot;
                   12392: 			$part=~s/^\_//;
1.736     albertel 12393: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   12394: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   12395: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 12396: 			    # ignore package.tab specified default values
                   12397:                             # here &package_tab_default() will fetch those
                   12398: 			    if ($subp eq 'default') { next; }
1.736     albertel 12399: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 12400: 			    my $unikey;
                   12401: 			    if ($pack =~ /_0$/) {
                   12402: 				$unikey='parameter_0_'.$name;
                   12403: 				$part=0;
                   12404: 			    } else {
                   12405: 				$unikey='parameter'.$keyroot.'_'.$name;
                   12406: 			    }
1.339     albertel 12407: 			    if ($subp eq 'display') {
                   12408: 				$value.=' [Part: '.$part.']';
                   12409: 			    }
1.599     albertel 12410: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 12411: 			    $metathesekeys{$unikey}=1;
1.599     albertel 12412: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   12413: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 12414: 			    }
1.599     albertel 12415: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   12416: 				$metaentry{':'.$unikey}=
                   12417: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 12418: 			    }
1.339     albertel 12419: 			}
                   12420: 		    }
                   12421: 		} else {
1.172     www      12422: #
                   12423: # This is not a package - some other kind of start tag
1.339     albertel 12424: #
                   12425: 		    my $entry=$token->[1];
1.1068    www      12426: 		    my $unikey='';
1.175     www      12427: 
1.339     albertel 12428: 		    if ($entry eq 'import') {
1.175     www      12429: #
                   12430: # Importing a library here
1.339     albertel 12431: #
1.1067    www      12432:                         my $location=$parser->get_text('/import');
                   12433:                         my $dir=$filename;
                   12434:                         $dir=~s|[^/]*$||;
                   12435:                         $location=&filelocation($dir,$location);
1.1369    raeburn  12436: 
                   12437:                         my $importid=$token->[2]->{'id'};
1.1068    www      12438:                         my $importmode=$token->[2]->{'importmode'};
1.1369    raeburn  12439: #
                   12440: # Check metadata for imported file to
                   12441: # see if it contained response items
                   12442: #
1.1372    raeburn  12443:                         my ($origfile,@libfilekeys);
1.1370    raeburn  12444:                         my %currmetaentry = %metaentry;
1.1372    raeburn  12445:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
                   12446:                                                            $depthcount+1));
                   12447:                         if (grep(/^responseorder$/,@libfilekeys)) {
                   12448:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
                   12449:                                                              undef,$depthcount+1);
                   12450:                             if ($libresponseorder ne '') {
                   12451:                                 if ($#origfiletagids<0) {
                   12452:                                     undef(%importedrespids);
                   12453:                                     undef(%importedpartids);
                   12454:                                 }
1.1373    raeburn  12455:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
                   12456:                                 if (@respids) {
                   12457:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
                   12458:                                 }
                   12459:                                 if ($importedrespids{$importid} ne '') {
1.1372    raeburn  12460:                                     $importedresponses = 1;
1.1369    raeburn  12461: # We need to get the original file and the imported file to get the response order correct
                   12462: # Load and inspect original file
1.1372    raeburn  12463:                                     if ($#origfiletagids<0) {
                   12464:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   12465:                                         $origfile=&getfile($origfilelocation);
                   12466:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12467:                                     }
1.1369    raeburn  12468:                                 }
                   12469:                             }
                   12470:                         }
1.1370    raeburn  12471: # Do not overwrite contents of %metaentry hash for resource itself with 
                   12472: # hash populated for imported library file
                   12473:                         %metaentry = %currmetaentry;
                   12474:                         undef(%currmetaentry);
1.1374    raeburn  12475:                         if ($importmode eq 'part') {
1.1068    www      12476: # Import as part(s)
1.1069    www      12477:                            $importedparts=1;
                   12478: # We need to get the original file and the imported file to get the part order correct
                   12479: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1369    raeburn  12480: # Load and inspect original file if we didn't do that already
                   12481:                            if ($#origfiletagids<0) {
                   12482:                                undef(%importedrespids);
                   12483:                                undef(%importedpartids);
                   12484:                                if ($origfile eq '') {
                   12485:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   12486:                                    $origfile=&getfile($origfilelocation);
                   12487:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12488:                                }
1.1070    www      12489:                            }
1.1372    raeburn  12490:                            my @impfilepartids;
                   12491: # If <partorder> tag is included in metadata for the imported file
                   12492: # get the parts in the imported file from that.
                   12493:                            if (grep(/^partorder$/,@libfilekeys)) {
                   12494:                                %currmetaentry = %metaentry;
                   12495:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   12496:                                                             $depthcount+1);
                   12497:                                %metaentry = %currmetaentry;
                   12498:                                undef(%currmetaentry);
                   12499:                                if ($libpartorder ne '') {
                   12500:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
                   12501:                                }
                   12502:                            } else {
                   12503: # If no <partorder> tag available, load and inspect imported file
                   12504:                                my $impfile=&getfile($location);
                   12505:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   12506:                            }
1.1069    www      12507:                            if ($#impfilepartids>=0) {
                   12508: # This problem had parts
1.1070    www      12509:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      12510:                            } else {
                   12511: # Importing by turning a single problem into a problem part
                   12512: # It gets the import-tags ID as part-ID
                   12513:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      12514:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      12515:                            }
1.1068    www      12516:                         } else {
1.1374    raeburn  12517: # Import as problem or as normal import
                   12518:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12519:                             unless ($importmode eq 'problem') {
1.1068    www      12520: # Normal import
1.1374    raeburn  12521:                                 if (defined($token->[2]->{'id'})) {
                   12522:                                     $unikey.='_'.$token->[2]->{'id'};
                   12523:                                 }
                   12524:                             }
                   12525: # Check metadata for imported file to
                   12526: # see if it contained parts
                   12527:                             if (grep(/^partorder$/,@libfilekeys)) {
                   12528:                                 %currmetaentry = %metaentry;
                   12529:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
                   12530:                                                              $depthcount+1);
                   12531:                                 %metaentry = %currmetaentry;
                   12532:                                 undef(%currmetaentry);
                   12533:                                 if ($libpartorder ne '') {
                   12534:                                     $importedparts = 1;
                   12535:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
                   12536:                                 }
                   12537:                             }
1.1067    www      12538:                         }
1.339     albertel 12539: 			if ($depthcount<20) {
1.736     albertel 12540: 			    my $metadata = 
1.1362    raeburn  12541: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736     albertel 12542: 					  $depthcount+1);
                   12543: 			    foreach my $meta (split(',',$metadata)) {
                   12544: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   12545: 				$metathesekeys{$meta}=1;
1.339     albertel 12546: 			    }
1.1068    www      12547:                         }
1.1067    www      12548: 		    } else {
                   12549: #
                   12550: # Not importing, some other kind of non-package, non-library start tag
                   12551: # 
                   12552:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   12553:                         if (defined($token->[2]->{'id'})) {
                   12554:                             $unikey.='_'.$token->[2]->{'id'};
                   12555:                         }
1.339     albertel 12556: 			if (defined($token->[2]->{'name'})) { 
                   12557: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   12558: 			}
                   12559: 			$metathesekeys{$unikey}=1;
1.736     albertel 12560: 			foreach my $param (@{$token->[3]}) {
                   12561: 			    $metaentry{':'.$unikey.'.'.$param} =
                   12562: 				$token->[2]->{$param};
1.339     albertel 12563: 			}
                   12564: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 12565: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 12566: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   12567: 		 # only ws inside the tag, and not in default, so use default
                   12568: 		 # as value
1.599     albertel 12569: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 12570: 			} elsif ( $internaltext =~ /\S/ ) {
                   12571: 		  # something interesting inside the tag
                   12572: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 12573: 			} else {
1.908     albertel 12574: 		  # no interesting values, don't set a default
1.339     albertel 12575: 			}
1.172     www      12576: # end of not-a-package not-a-library import
1.339     albertel 12577: 		    }
1.172     www      12578: # end of not-a-package start tag
1.339     albertel 12579: 		}
1.172     www      12580: # the next is the end of "start tag"
1.339     albertel 12581: 	    }
                   12582: 	}
1.483     albertel 12583: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 12584: 	$extension = lc($extension);
                   12585: 	if ($extension eq 'htm') { $extension='html'; }
                   12586: 
1.737     albertel 12587: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 12588: 	    #no specific packages #how's our extension
                   12589: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 12590: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 12591: 					 \%metathesekeys);
                   12592: 	}
1.883     albertel 12593: 
                   12594: 	if (!exists($metaentry{':packages'})
                   12595: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 12596: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 12597: 		#no specific packages well let's get default then
                   12598: 		if ($key!~/^default&/) { next; }
1.488     albertel 12599: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 12600: 					     \%metathesekeys);
                   12601: 	    }
                   12602: 	}
1.338     www      12603: # are there custom rights to evaluate
1.599     albertel 12604: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 12605: 
1.338     www      12606:     #
                   12607:     # Importing a rights file here
1.339     albertel 12608:     #
                   12609: 	    unless ($depthcount) {
1.599     albertel 12610: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 12611: 		my $dir=$filename;
                   12612: 		$dir=~s|[^/]*$||;
                   12613: 		$location=&filelocation($dir,$location);
1.736     albertel 12614: 		my $rights_metadata =
1.1362    raeburn  12615: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736     albertel 12616: 			      $depthcount+1);
                   12617: 		foreach my $rights (split(',',$rights_metadata)) {
                   12618: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   12619: 		    $metathesekeys{$rights}=1;
1.339     albertel 12620: 		}
                   12621: 	    }
                   12622: 	}
1.737     albertel 12623: 	# uniqifiy package listing
                   12624: 	my %seen;
                   12625: 	my @uniq_packages =
                   12626: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   12627: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   12628: 
1.1369    raeburn  12629:         if (($importedresponses) || ($importedparts)) {
                   12630:             if ($importedparts) {
1.1070    www      12631: # We had imported parts and need to rebuild partorder
1.1369    raeburn  12632:                 $metaentry{':partorder'}='';
                   12633:                 $metathesekeys{'partorder'}=1;
                   12634:             }
                   12635:             if ($importedresponses) {
                   12636: # We had imported responses and need to rebuil responseorder
                   12637:                 $metaentry{':responseorder'}='';
                   12638:                 $metathesekeys{'responseorder'}=1;
                   12639:             }
                   12640:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
                   12641:                 my $origid = $origfiletagids[$index+1];
                   12642:                 if ($origfiletagids[$index] eq 'part') {
                   12643: # Original part, part of the problem
                   12644:                     if ($importedparts) {
                   12645:                         $metaentry{':partorder'}.=','.$origid;
                   12646:                     }
                   12647:                 } elsif ($origfiletagids[$index] eq 'import') {
                   12648:                     if ($importedparts) {
                   12649: # We have imported parts at this position
1.1373    raeburn  12650:                         if ($importedpartids{$origid} ne '') {
                   12651:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
                   12652:                         }
1.1369    raeburn  12653:                     }
                   12654:                     if ($importedresponses) {
                   12655: # We have imported responses at this position
1.1373    raeburn  12656:                         if ($importedrespids{$origid} ne '') {
                   12657:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
1.1369    raeburn  12658:                         }
                   12659:                     }
                   12660:                 } else {
                   12661: # Original response item, part of the problem
                   12662:                     if ($importedresponses) {
                   12663:                         $metaentry{':responseorder'}.=','.$origid;
                   12664:                     }
                   12665:                 }
                   12666:             }
                   12667:             if ($importedparts) {
                   12668:                 $metaentry{':partorder'}=~s/^\,//;
                   12669:             }
                   12670:             if ($importedresponses) {
                   12671:                 $metaentry{':responseorder'}=~s/^\,//;
                   12672:             }
1.1070    www      12673:         }
1.737     albertel 12674: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 12675: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274    raeburn  12676: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.1371    raeburn  12677:         unless ($liburi) {
                   12678: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
                   12679:         }
1.177     www      12680: # this is the end of "was not already recently cached
1.71      www      12681:     }
1.599     albertel 12682:     return $metaentry{':'.$what};
1.261     albertel 12683: }
                   12684: 
1.488     albertel 12685: sub metadata_create_package_def {
1.483     albertel 12686:     my ($uri,$key,$package,$metathesekeys)=@_;
                   12687:     my ($pack,$name,$subp)=split(/\&/,$key);
                   12688:     if ($subp eq 'default') { next; }
                   12689:     
1.599     albertel 12690:     if (defined($metaentry{':packages'})) {
                   12691: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 12692:     } else {
1.599     albertel 12693: 	$metaentry{':packages'}=$package;
1.483     albertel 12694:     }
                   12695:     my $value=$packagetab{$key};
                   12696:     my $unikey;
                   12697:     $unikey='parameter_0_'.$name;
1.599     albertel 12698:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 12699:     $$metathesekeys{$unikey}=1;
1.599     albertel 12700:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   12701: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 12702:     }
1.599     albertel 12703:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   12704: 	$metaentry{':'.$unikey}=
                   12705: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 12706:     }
                   12707: }
                   12708: 
1.261     albertel 12709: sub metadata_generate_part0 {
                   12710:     my ($metadata,$metacache,$uri) = @_;
                   12711:     my %allnames;
1.737     albertel 12712:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 12713: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 12714: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   12715: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 12716: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 12717: 	    $allnames{$name}=$part;
                   12718: 	  }
                   12719: 	}
                   12720:     }
                   12721:     foreach my $name (keys(%allnames)) {
                   12722:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 12723:       my $key=":parameter_0_$name";
1.261     albertel 12724:       $$metacache{"$key.part"}='0';
                   12725:       $$metacache{"$key.name"}=$name;
1.428     albertel 12726:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 12727: 					   $allnames{$name}.'_'.$name.
                   12728: 					   '.type'};
1.428     albertel 12729:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 12730: 			     '.display'};
1.644     www      12731:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 12732:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 12733:       $$metacache{"$key.display"}=$olddis;
                   12734:     }
1.71      www      12735: }
                   12736: 
1.764     albertel 12737: # ------------------------------------------------------ Devalidate title cache
                   12738: 
                   12739: sub devalidate_title_cache {
                   12740:     my ($url)=@_;
                   12741:     if (!$env{'request.course.id'}) { return; }
                   12742:     my $symb=&symbread($url);
                   12743:     if (!$symb) { return; }
                   12744:     my $key=$env{'request.course.id'}."\0".$symb;
                   12745:     &devalidate_cache_new('title',$key);
                   12746: }
                   12747: 
1.1014    droeschl 12748: # ------------------------------------------------- Get the title of a course
                   12749: 
                   12750: sub current_course_title {
                   12751:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   12752: }
1.301     www      12753: # ------------------------------------------------- Get the title of a resource
                   12754: 
                   12755: sub gettitle {
                   12756:     my $urlsymb=shift;
                   12757:     my $symb=&symbread($urlsymb);
1.534     albertel 12758:     if ($symb) {
1.620     albertel 12759: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 12760: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 12761: 	if (defined($cached)) { 
                   12762: 	    return $result;
                   12763: 	}
1.534     albertel 12764: 	my ($map,$resid,$url)=&decode_symb($symb);
                   12765: 	my $title='';
1.907     albertel 12766: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   12767: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   12768: 	} else {
                   12769: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   12770: 		    &GDBM_READER(),0640)) {
                   12771: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   12772: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   12773: 		untie(%bighash);
                   12774: 	    }
1.534     albertel 12775: 	}
                   12776: 	$title=~s/\&colon\;/\:/gs;
                   12777: 	if ($title) {
1.1159    www      12778: # Remember both $symb and $title for dynamic metadata
                   12779:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      12780:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      12781: # Cache this title and then return it
1.599     albertel 12782: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 12783: 	}
                   12784: 	$urlsymb=$url;
                   12785:     }
                   12786:     my $title=&metadata($urlsymb,'title');
                   12787:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   12788:     return $title;
1.301     www      12789: }
1.613     albertel 12790: 
1.614     albertel 12791: sub get_slot {
                   12792:     my ($which,$cnum,$cdom)=@_;
                   12793:     if (!$cnum || !$cdom) {
1.790     albertel 12794: 	(undef,my $courseid)=&whichuser();
1.620     albertel 12795: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   12796: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 12797:     }
1.703     albertel 12798:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   12799:     my %slotinfo;
                   12800:     if (exists($remembered{$key})) {
                   12801: 	$slotinfo{$which} = $remembered{$key};
                   12802:     } else {
                   12803: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   12804: 	&Apache::lonhomework::showhash(%slotinfo);
                   12805: 	my ($tmp)=keys(%slotinfo);
                   12806: 	if ($tmp=~/^error:/) { return (); }
                   12807: 	$remembered{$key} = $slotinfo{$which};
                   12808:     }
1.616     albertel 12809:     if (ref($slotinfo{$which}) eq 'HASH') {
                   12810: 	return %{$slotinfo{$which}};
                   12811:     }
                   12812:     return $slotinfo{$which};
1.614     albertel 12813: }
1.1150    raeburn  12814: 
                   12815: sub get_reservable_slots {
                   12816:     my ($cnum,$cdom,$uname,$udom) = @_;
                   12817:     my $now = time;
                   12818:     my $reservable_info;
                   12819:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   12820:     if (exists($remembered{$key})) {
                   12821:         $reservable_info = $remembered{$key};
                   12822:     } else {
                   12823:         my %resv;
                   12824:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   12825:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   12826:         $reservable_info = \%resv;
                   12827:         $remembered{$key} = $reservable_info;
                   12828:     }
                   12829:     return $reservable_info;
                   12830: }
                   12831: 
                   12832: sub get_course_slots {
                   12833:     my ($cnum,$cdom) = @_;
                   12834:     my $hashid=$cnum.':'.$cdom;
                   12835:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   12836:     if (defined($cached)) {
                   12837:         if (ref($result) eq 'HASH') {
                   12838:             return %{$result};
                   12839:         }
                   12840:     } else {
                   12841:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   12842:         my ($tmp) = keys(%slots);
                   12843:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  12844:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  12845:             return %slots;
                   12846:         }
                   12847:     }
                   12848:     return;
                   12849: }
                   12850: 
                   12851: sub devalidate_slots_cache {
                   12852:     my ($cnum,$cdom)=@_;
                   12853:     my $hashid=$cnum.':'.$cdom;
                   12854:     &devalidate_cache_new('allslots',$hashid);
                   12855: }
                   12856: 
1.1181    raeburn  12857: sub get_coursechange {
                   12858:     my ($cdom,$cnum) = @_;
                   12859:     if ($cdom eq '' || $cnum eq '') {
                   12860:         return unless ($env{'request.course.id'});
                   12861:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   12862:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   12863:     }
                   12864:     my $hashid=$cdom.'_'.$cnum;
                   12865:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   12866:     if ((defined($cached)) && ($change ne '')) {
                   12867:         return $change;
                   12868:     } else {
                   12869:         my %crshash;
                   12870:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   12871:         if ($crshash{'internal.contentchange'} eq '') {
                   12872:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   12873:             if ($change eq '') {
                   12874:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   12875:                 $change = $crshash{'internal.created'};
                   12876:             }
                   12877:         } else {
                   12878:             $change = $crshash{'internal.contentchange'};
                   12879:         }
                   12880:         my $cachetime = 600;
                   12881:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   12882:     }
                   12883:     return $change;
                   12884: }
                   12885: 
                   12886: sub devalidate_coursechange_cache {
                   12887:     my ($cnum,$cdom)=@_;
                   12888:     my $hashid=$cnum.':'.$cdom;
                   12889:     &devalidate_cache_new('crschange',$hashid);
                   12890: }
                   12891: 
1.31      www      12892: # ------------------------------------------------- Update symbolic store links
                   12893: 
                   12894: sub symblist {
                   12895:     my ($mapname,%newhash)=@_;
1.438     www      12896:     $mapname=&deversion(&declutter($mapname));
1.31      www      12897:     my %hash;
1.620     albertel 12898:     if (($env{'request.course.fn'}) && (%newhash)) {
                   12899:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 12900:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  12901: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 12902: 		next if ($url eq 'last_known'
                   12903: 			 && $env{'form.no_update_last_known'});
                   12904: 		$hash{declutter($url)}=&encode_symb($mapname,
                   12905: 						    $newhash{$url}->[1],
                   12906: 						    $newhash{$url}->[0]);
1.191     harris41 12907:             }
1.31      www      12908:             if (untie(%hash)) {
                   12909: 		return 'ok';
                   12910:             }
                   12911:         }
                   12912:     }
                   12913:     return 'error';
1.212     www      12914: }
                   12915: 
                   12916: # --------------------------------------------------------------- Verify a symb
                   12917: 
                   12918: sub symbverify {
1.1190    raeburn  12919:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      12920:     my $thisfn=$thisurl;
1.439     www      12921:     $thisfn=&declutter($thisfn);
1.215     www      12922: # direct jump to resource in page or to a sequence - will construct own symbs
                   12923:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   12924: # check URL part
1.409     www      12925:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      12926: 
1.431     www      12927:     unless ($url eq $thisfn) { return 0; }
1.213     www      12928: 
1.216     www      12929:     $symb=&symbclean($symb);
1.510     www      12930:     $thisurl=&deversion($thisurl);
1.439     www      12931:     $thisfn=&deversion($thisfn);
1.213     www      12932: 
                   12933:     my %bighash;
                   12934:     my $okay=0;
1.431     www      12935: 
1.620     albertel 12936:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 12937:                             &GDBM_READER(),0640)) {
1.1204    raeburn  12938:         my $noclutter;
1.1032    raeburn  12939:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   12940:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  12941:             if ($map =~ m{^uploaded/.+\.page$}) {
                   12942:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   12943:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   12944:                 $noclutter = 1;
                   12945:             }
                   12946:         }
                   12947:         my $ids;
                   12948:         if ($noclutter) {
                   12949:             $ids=$bighash{'ids_'.$thisurl};
                   12950:         } else {
                   12951:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  12952:         }
1.1102    raeburn  12953:         unless ($ids) {
                   12954:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   12955:             $ids=$bighash{$idkey};
1.216     www      12956:         }
                   12957:         if ($ids) {
                   12958: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  12959:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  12960:                 $symb =~ s/\?.+$//;
1.1202    raeburn  12961:             }
1.800     albertel 12962: 	    foreach my $id (split(/\,/,$ids)) {
                   12963: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      12964:                if (
                   12965:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  12966:    eq $symb) {
                   12967:                    if (ref($encstate)) {
                   12968:                        $$encstate = $bighash{'encrypted_'.$id};
                   12969:                    }
1.620     albertel 12970: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  12971: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   12972:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  12973: 		       $okay=1;
1.1202    raeburn  12974:                        last;
1.582     albertel 12975: 		   }
                   12976: 	       }
1.216     www      12977: 	   }
                   12978:         }
1.213     www      12979: 	untie(%bighash);
                   12980:     }
                   12981:     return $okay;
1.31      www      12982: }
                   12983: 
1.210     www      12984: # --------------------------------------------------------------- Clean-up symb
                   12985: 
                   12986: sub symbclean {
                   12987:     my $symb=shift;
1.568     albertel 12988:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      12989: # remove version from map
                   12990:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      12991: 
1.210     www      12992: # remove version from URL
                   12993:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      12994: 
1.507     www      12995: # remove wrapper
                   12996: 
1.510     www      12997:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 12998:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      12999:     return $symb;
1.409     www      13000: }
                   13001: 
                   13002: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 13003: 
                   13004: sub encode_symb {
                   13005:     my ($map,$resid,$url)=@_;
                   13006:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   13007: }
1.409     www      13008: 
                   13009: sub decode_symb {
1.568     albertel 13010:     my $symb=shift;
                   13011:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   13012:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      13013:     return (&fixversion($map),$resid,&fixversion($url));
                   13014: }
                   13015: 
                   13016: sub fixversion {
                   13017:     my $fn=shift;
1.609     banghart 13018:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      13019:     my %bighash;
                   13020:     my $uri=&clutter($fn);
1.620     albertel 13021:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      13022: # is this cached?
1.599     albertel 13023:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      13024:     if (defined($cached)) { return $result; }
                   13025: # unfortunately not cached, or expired
1.620     albertel 13026:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      13027: 	    &GDBM_READER(),0640)) {
                   13028:  	if ($bighash{'version_'.$uri}) {
                   13029:  	    my $version=$bighash{'version_'.$uri};
1.444     www      13030:  	    unless (($version eq 'mostrecent') || 
                   13031: 		    ($version==&getversion($uri))) {
1.440     www      13032:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   13033:  	    }
                   13034:  	}
                   13035:  	untie %bighash;
1.413     www      13036:     }
1.599     albertel 13037:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      13038: }
                   13039: 
                   13040: sub deversion {
                   13041:     my $url=shift;
                   13042:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   13043:     return $url;
1.210     www      13044: }
                   13045: 
1.31      www      13046: # ------------------------------------------------------ Return symb list entry
                   13047: 
                   13048: sub symbread {
1.1282    raeburn  13049:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
1.1265    raeburn  13050:     my $cache_str='request.symbread.cached.'.$thisfn;
1.1282    raeburn  13051:     if (defined($env{$cache_str})) {
                   13052:         if ($ignorecachednull) {
                   13053:             return $env{$cache_str} unless ($env{$cache_str} eq '');
                   13054:         } else {
                   13055:             return $env{$cache_str};
                   13056:         }
                   13057:     }
1.242     www      13058: # no filename provided? try from environment
1.1265    raeburn  13059:     unless ($thisfn) {
1.620     albertel 13060:         if ($env{'request.symb'}) {
                   13061: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 13062: 	}
1.620     albertel 13063: 	$thisfn=$env{'request.filename'};
1.44      www      13064:     }
1.569     albertel 13065:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      13066: # is that filename actually a symb? Verify, clean, and return
                   13067:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 13068: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 13069: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 13070: 	}
1.242     www      13071:     }
1.44      www      13072:     $thisfn=declutter($thisfn);
1.31      www      13073:     my %hash;
1.37      www      13074:     my %bighash;
                   13075:     my $syval='';
1.620     albertel 13076:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  13077:         my $targetfn = $thisfn;
1.609     banghart 13078:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  13079:             $targetfn = 'adm/wrapper/'.$thisfn;
                   13080:         }
1.687     albertel 13081: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   13082: 	    $targetfn=$1;
                   13083: 	}
1.620     albertel 13084:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 13085:                       &GDBM_READER(),0640)) {
1.481     raeburn  13086: 	    $syval=$hash{$targetfn};
1.37      www      13087:             untie(%hash);
                   13088:         }
                   13089: # ---------------------------------------------------------- There was an entry
                   13090:         if ($syval) {
1.601     albertel 13091: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 13092: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  13093: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 13094: 		    #return $env{$cache_str}='';
1.601     albertel 13095: 		#}    
                   13096: 		#$syval.=$1;
                   13097: 	    #}
1.37      www      13098:         } else {
                   13099: # ------------------------------------------------------- Was not in symb table
1.620     albertel 13100:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 13101:                             &GDBM_READER(),0640)) {
1.37      www      13102: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      13103:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      13104:               unless ($ids) { 
                   13105:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      13106:               }
                   13107:               unless ($ids) {
                   13108: # alias?
                   13109: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      13110:               }
1.37      www      13111:               if ($ids) {
                   13112: # ------------------------------------------------------------------- Has ID(s)
                   13113:                  my @possibilities=split(/\,/,$ids);
1.39      www      13114:                  if ($#possibilities==0) {
                   13115: # ----------------------------------------------- There is only one possibility
1.37      www      13116: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 13117: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   13118: 						    $resid,$thisfn);
1.1282    raeburn  13119:                      if (ref($possibles) eq 'HASH') {
                   13120:                          $possibles->{$syval} = 1;    
                   13121:                      }
                   13122:                      if ($checkforblock) {
                   13123:                          my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
                   13124:                          if (@blockers) {
                   13125:                              $syval = '';
                   13126:                              return;
                   13127:                          }
                   13128:                      }
                   13129:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
1.39      www      13130: # ------------------------------------------ There is more than one possibility
                   13131:                      my $realpossible=0;
1.800     albertel 13132:                      foreach my $id (@possibilities) {
                   13133: 			 my $file=$bighash{'src_'.$id};
1.1282    raeburn  13134:                          my $canaccess;
                   13135:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
                   13136:                              $canaccess = 1;
                   13137:                          } else { 
                   13138:                              $canaccess = &allowed('bre',$file);
                   13139:                          }
                   13140:                          if ($canaccess) {
                   13141:          		     my ($mapid,$resid)=split(/\./,$id);
                   13142:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
                   13143:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
                   13144: 						             $resid,$thisfn);
                   13145:                                  if (ref($possibles) eq 'HASH') {
                   13146:                                      $possibles->{$syval} = 1;
                   13147:                                  }
                   13148:                                  if ($checkforblock) {
                   13149:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
                   13150:                                      unless (@blockers > 0) {
                   13151:                                          $syval = $poss_syval;
                   13152:                                          $realpossible++;
                   13153:                                      }
                   13154:                                  } else {
                   13155:                                      $syval = $poss_syval;
                   13156:                                      $realpossible++;
                   13157:                                  }
                   13158:                              }
1.39      www      13159: 			 }
1.191     harris41 13160:                      }
1.39      www      13161: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      13162:                  } else {
                   13163:                      $syval='';
1.37      www      13164:                  }
                   13165: 	      }
1.1282    raeburn  13166:               untie(%bighash);
1.481     raeburn  13167:            }
1.31      www      13168:         }
1.62      www      13169:         if ($syval) {
1.620     albertel 13170: 	    return $env{$cache_str}=$syval;
1.62      www      13171:         }
1.31      www      13172:     }
1.949     raeburn  13173:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 13174:     return $env{$cache_str}='';
1.31      www      13175: }
                   13176: 
                   13177: # ---------------------------------------------------------- Return random seed
                   13178: 
1.32      www      13179: sub numval {
                   13180:     my $txt=shift;
                   13181:     $txt=~tr/A-J/0-9/;
                   13182:     $txt=~tr/a-j/0-9/;
                   13183:     $txt=~tr/K-T/0-9/;
                   13184:     $txt=~tr/k-t/0-9/;
                   13185:     $txt=~tr/U-Z/0-5/;
                   13186:     $txt=~tr/u-z/0-5/;
                   13187:     $txt=~s/\D//g;
1.564     albertel 13188:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      13189:     return int($txt);
1.368     albertel 13190: }
                   13191: 
1.484     albertel 13192: sub numval2 {
                   13193:     my $txt=shift;
                   13194:     $txt=~tr/A-J/0-9/;
                   13195:     $txt=~tr/a-j/0-9/;
                   13196:     $txt=~tr/K-T/0-9/;
                   13197:     $txt=~tr/k-t/0-9/;
                   13198:     $txt=~tr/U-Z/0-5/;
                   13199:     $txt=~tr/u-z/0-5/;
                   13200:     $txt=~s/\D//g;
                   13201:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   13202:     my $total;
                   13203:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 13204:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 13205:     return int($total);
                   13206: }
                   13207: 
1.575     albertel 13208: sub numval3 {
                   13209:     use integer;
                   13210:     my $txt=shift;
                   13211:     $txt=~tr/A-J/0-9/;
                   13212:     $txt=~tr/a-j/0-9/;
                   13213:     $txt=~tr/K-T/0-9/;
                   13214:     $txt=~tr/k-t/0-9/;
                   13215:     $txt=~tr/U-Z/0-5/;
                   13216:     $txt=~tr/u-z/0-5/;
                   13217:     $txt=~s/\D//g;
                   13218:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   13219:     my $total;
                   13220:     foreach my $val (@txts) { $total+=$val; }
                   13221:     if ($_64bit) { $total=(($total<<32)>>32); }
                   13222:     return $total;
                   13223: }
                   13224: 
1.675     albertel 13225: sub digest {
                   13226:     my ($data)=@_;
                   13227:     my $digest=&Digest::MD5::md5($data);
                   13228:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   13229:     my ($e,$f);
                   13230:     {
                   13231:         use integer;
                   13232:         $e=($a+$b);
                   13233:         $f=($c+$d);
                   13234:         if ($_64bit) {
                   13235:             $e=(($e<<32)>>32);
                   13236:             $f=(($f<<32)>>32);
                   13237:         }
                   13238:     }
                   13239:     if (wantarray) {
                   13240: 	return ($e,$f);
                   13241:     } else {
                   13242: 	my $g;
                   13243: 	{
                   13244: 	    use integer;
                   13245: 	    $g=($e+$f);
                   13246: 	    if ($_64bit) {
                   13247: 		$g=(($g<<32)>>32);
                   13248: 	    }
                   13249: 	}
                   13250: 	return $g;
                   13251:     }
                   13252: }
                   13253: 
1.368     albertel 13254: sub latest_rnd_algorithm_id {
1.675     albertel 13255:     return '64bit5';
1.366     albertel 13256: }
1.32      www      13257: 
1.503     albertel 13258: sub get_rand_alg {
                   13259:     my ($courseid)=@_;
1.790     albertel 13260:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 13261:     if ($courseid) {
1.620     albertel 13262: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 13263:     }
                   13264:     return &latest_rnd_algorithm_id();
                   13265: }
                   13266: 
1.562     albertel 13267: sub validCODE {
                   13268:     my ($CODE)=@_;
                   13269:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   13270:     return 0;
                   13271: }
                   13272: 
1.491     albertel 13273: sub getCODE {
1.620     albertel 13274:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 13275:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   13276: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   13277: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 13278: 	return $Apache::lonhomework::history{'resource.CODE'};
                   13279:     }
                   13280:     return undef;
                   13281: }
1.1133    foxr     13282: #
                   13283: #  Determines the random seed for a specific context:
                   13284: #
                   13285: # parameters:
                   13286: #   symb      - in course context the symb for the seed.
                   13287: #   course_id - The course id of the form domain_coursenum.
                   13288: #   domain    - Domain for the user.
                   13289: #   course    - Course for the user.
                   13290: #   cenv      - environment of the course.
                   13291: #
                   13292: # NOTE:
                   13293: #   All parameters are picked out of the environment if missing
                   13294: #   or not defined.
                   13295: #   If a symb cannot be determined the current time is used instead.
                   13296: #
                   13297: #  For a given well defined symb, courside, domain, username,
                   13298: #  and course environment, the seed is reproducible.
                   13299: #
1.31      www      13300: sub rndseed {
1.1133    foxr     13301:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 13302:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 13303:     if (!defined($symb)) {
1.366     albertel 13304: 	unless ($symb=$wsymb) { return time; }
                   13305:     }
1.1146    foxr     13306:     if (!defined $courseid) { 
                   13307: 	$courseid=$wcourseid; 
                   13308:     }
                   13309:     if (!defined $domain) { $domain=$wdomain; }
                   13310:     if (!defined $username) { $username=$wusername }
1.1133    foxr     13311: 
                   13312:     my $which;
                   13313:     if (defined($cenv->{'rndseed'})) {
                   13314: 	$which = $cenv->{'rndseed'};
                   13315:     } else {
                   13316: 	$which =&get_rand_alg($courseid);
                   13317:     }
1.491     albertel 13318:     if (defined(&getCODE())) {
1.1133    foxr     13319: 
1.675     albertel 13320: 	if ($which eq '64bit5') {
                   13321: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   13322: 	} elsif ($which eq '64bit4') {
1.575     albertel 13323: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   13324: 	} else {
                   13325: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   13326: 	}
1.675     albertel 13327:     } elsif ($which eq '64bit5') {
                   13328: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 13329:     } elsif ($which eq '64bit4') {
                   13330: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 13331:     } elsif ($which eq '64bit3') {
                   13332: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 13333:     } elsif ($which eq '64bit2') {
                   13334: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 13335:     } elsif ($which eq '64bit') {
                   13336: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   13337:     }
                   13338:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   13339: }
                   13340: 
                   13341: sub rndseed_32bit {
                   13342:     my ($symb,$courseid,$domain,$username)=@_;
                   13343:     {
                   13344: 	use integer;
                   13345: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   13346: 	my $symbseed=numval($symb) << 22;
                   13347: 	my $namechck=unpack("%32C*",$username) << 17;
                   13348: 	my $nameseed=numval($username) << 12;
                   13349: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   13350: 	my $courseseed=unpack("%32C*",$courseid);
                   13351: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 13352: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13353: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 13354: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 13355: 	return $num;
                   13356:     }
                   13357: }
                   13358: 
                   13359: sub rndseed_64bit {
                   13360:     my ($symb,$courseid,$domain,$username)=@_;
                   13361:     {
                   13362: 	use integer;
                   13363: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   13364: 	my $symbseed=numval($symb) << 10;
                   13365: 	my $namechck=unpack("%32S*",$username);
                   13366: 	
                   13367: 	my $nameseed=numval($username) << 21;
                   13368: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   13369: 	my $courseseed=unpack("%32S*",$courseid);
                   13370: 	
                   13371: 	my $num1=$symbchck+$symbseed+$namechck;
                   13372: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13373: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13374: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 13375: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 13376: 	return "$num1,$num2";
1.155     albertel 13377:     }
1.366     albertel 13378: }
                   13379: 
1.443     albertel 13380: sub rndseed_64bit2 {
                   13381:     my ($symb,$courseid,$domain,$username)=@_;
                   13382:     {
                   13383: 	use integer;
                   13384: 	# strings need to be an even # of cahracters long, it it is odd the
                   13385:         # last characters gets thrown away
                   13386: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13387: 	my $symbseed=numval($symb) << 10;
                   13388: 	my $namechck=unpack("%32S*",$username.' ');
                   13389: 	
                   13390: 	my $nameseed=numval($username) << 21;
1.501     albertel 13391: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13392: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13393: 	
                   13394: 	my $num1=$symbchck+$symbseed+$namechck;
                   13395: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13396: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13397: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 13398: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 13399: 	return "$num1,$num2";
                   13400:     }
                   13401: }
                   13402: 
                   13403: sub rndseed_64bit3 {
                   13404:     my ($symb,$courseid,$domain,$username)=@_;
                   13405:     {
                   13406: 	use integer;
                   13407: 	# strings need to be an even # of cahracters long, it it is odd the
                   13408:         # last characters gets thrown away
                   13409: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13410: 	my $symbseed=numval2($symb) << 10;
                   13411: 	my $namechck=unpack("%32S*",$username.' ');
                   13412: 	
                   13413: 	my $nameseed=numval2($username) << 21;
1.443     albertel 13414: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13415: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13416: 	
                   13417: 	my $num1=$symbchck+$symbseed+$namechck;
                   13418: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13419: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13420: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 13421: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      13422: 	
1.503     albertel 13423: 	return "$num1:$num2";
1.443     albertel 13424:     }
                   13425: }
                   13426: 
1.575     albertel 13427: sub rndseed_64bit4 {
                   13428:     my ($symb,$courseid,$domain,$username)=@_;
                   13429:     {
                   13430: 	use integer;
                   13431: 	# strings need to be an even # of cahracters long, it it is odd the
                   13432:         # last characters gets thrown away
                   13433: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   13434: 	my $symbseed=numval3($symb) << 10;
                   13435: 	my $namechck=unpack("%32S*",$username.' ');
                   13436: 	
                   13437: 	my $nameseed=numval3($username) << 21;
                   13438: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   13439: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13440: 	
                   13441: 	my $num1=$symbchck+$symbseed+$namechck;
                   13442: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 13443: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   13444: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 13445: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      13446: 	
1.575     albertel 13447: 	return "$num1:$num2";
                   13448:     }
                   13449: }
                   13450: 
1.675     albertel 13451: sub rndseed_64bit5 {
                   13452:     my ($symb,$courseid,$domain,$username)=@_;
                   13453:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   13454:     return "$num1:$num2";
                   13455: }
                   13456: 
1.366     albertel 13457: sub rndseed_CODE_64bit {
                   13458:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 13459:     {
1.366     albertel 13460: 	use integer;
1.443     albertel 13461: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 13462: 	my $symbseed=numval2($symb);
1.491     albertel 13463: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   13464: 	my $CODEseed=numval(&getCODE());
1.443     albertel 13465: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 13466: 	my $num1=$symbseed+$CODEchck;
                   13467: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 13468: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   13469: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 13470: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   13471: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 13472: 	return "$num1:$num2";
1.366     albertel 13473:     }
                   13474: }
                   13475: 
1.575     albertel 13476: sub rndseed_CODE_64bit4 {
                   13477:     my ($symb,$courseid,$domain,$username)=@_;
                   13478:     {
                   13479: 	use integer;
                   13480: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   13481: 	my $symbseed=numval3($symb);
                   13482: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   13483: 	my $CODEseed=numval3(&getCODE());
                   13484: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   13485: 	my $num1=$symbseed+$CODEchck;
                   13486: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 13487: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   13488: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 13489: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   13490: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   13491: 	return "$num1:$num2";
                   13492:     }
                   13493: }
                   13494: 
1.675     albertel 13495: sub rndseed_CODE_64bit5 {
                   13496:     my ($symb,$courseid,$domain,$username)=@_;
                   13497:     my $code = &getCODE();
                   13498:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   13499:     return "$num1:$num2";
                   13500: }
                   13501: 
1.366     albertel 13502: sub setup_random_from_rndseed {
                   13503:     my ($rndseed)=@_;
1.503     albertel 13504:     if ($rndseed =~/([,:])/) {
1.1262    raeburn  13505:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
                   13506:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
                   13507:             &Math::Random::random_set_seed_from_phrase($rndseed);
                   13508:         } else {
                   13509:             &Math::Random::random_set_seed($num1,$num2);
                   13510:         }
1.366     albertel 13511:     } else {
                   13512: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 13513:     }
1.36      albertel 13514: }
                   13515: 
1.474     albertel 13516: sub latest_receipt_algorithm_id {
1.835     albertel 13517:     return 'receipt3';
1.474     albertel 13518: }
                   13519: 
1.480     www      13520: sub recunique {
                   13521:     my $fucourseid=shift;
                   13522:     my $unique;
1.835     albertel 13523:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   13524: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 13525: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      13526:     } else {
                   13527: 	$unique=$perlvar{'lonReceipt'};
                   13528:     }
                   13529:     return unpack("%32C*",$unique);
                   13530: }
                   13531: 
                   13532: sub recprefix {
                   13533:     my $fucourseid=shift;
                   13534:     my $prefix;
1.835     albertel 13535:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   13536: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 13537: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      13538:     } else {
                   13539: 	$prefix=$perlvar{'lonHostID'};
                   13540:     }
                   13541:     return unpack("%32C*",$prefix);
                   13542: }
                   13543: 
1.76      www      13544: sub ireceipt {
1.474     albertel 13545:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 13546: 
                   13547:     my $return =&recprefix($fucourseid).'-';
                   13548: 
                   13549:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   13550: 	$env{'request.state'} eq 'construct') {
                   13551: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   13552: 	return $return;
                   13553:     }
                   13554: 
1.76      www      13555:     my $cuname=unpack("%32C*",$funame);
                   13556:     my $cudom=unpack("%32C*",$fudom);
                   13557:     my $cucourseid=unpack("%32C*",$fucourseid);
                   13558:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      13559:     my $cunique=&recunique($fucourseid);
1.474     albertel 13560:     my $cpart=unpack("%32S*",$part);
1.835     albertel 13561:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   13562: 
1.790     albertel 13563: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 13564: 			       
                   13565: 	$return.= ($cunique%$cuname+
                   13566: 		   $cunique%$cudom+
                   13567: 		   $cusymb%$cuname+
                   13568: 		   $cusymb%$cudom+
                   13569: 		   $cucourseid%$cuname+
                   13570: 		   $cucourseid%$cudom+
                   13571: 		   $cpart%$cuname+
                   13572: 		   $cpart%$cudom);
                   13573:     } else {
                   13574: 	$return.= ($cunique%$cuname+
                   13575: 		   $cunique%$cudom+
                   13576: 		   $cusymb%$cuname+
                   13577: 		   $cusymb%$cudom+
                   13578: 		   $cucourseid%$cuname+
                   13579: 		   $cucourseid%$cudom);
                   13580:     }
                   13581:     return $return;
1.76      www      13582: }
                   13583: 
                   13584: sub receipt {
1.474     albertel 13585:     my ($part)=@_;
1.790     albertel 13586:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 13587:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      13588: }
1.260     ng       13589: 
1.790     albertel 13590: sub whichuser {
                   13591:     my ($passedsymb)=@_;
                   13592:     my ($symb,$courseid,$domain,$name,$publicuser);
                   13593:     if (defined($env{'form.grade_symb'})) {
                   13594: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   13595: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   13596: 	if (!$allowed &&
                   13597: 	    exists($env{'request.course.sec'}) &&
                   13598: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   13599: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   13600: 			      '/'.$env{'request.course.sec'});
                   13601: 	}
                   13602: 	if ($allowed) {
                   13603: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   13604: 	    $courseid=$tmp_courseid;
                   13605: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   13606: 	    ($name)=&get_env_multiple('form.grade_username');
                   13607: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   13608: 	}
                   13609:     }
                   13610:     if (!$passedsymb) {
                   13611: 	$symb=&symbread();
                   13612:     } else {
                   13613: 	$symb=$passedsymb;
                   13614:     }
                   13615:     $courseid=$env{'request.course.id'};
                   13616:     $domain=$env{'user.domain'};
                   13617:     $name=$env{'user.name'};
                   13618:     if ($name eq 'public' && $domain eq 'public') {
                   13619: 	if (!defined($env{'form.username'})) {
                   13620: 	    $env{'form.username'}.=time.rand(10000000);
                   13621: 	}
                   13622: 	$name.=$env{'form.username'};
                   13623:     }
                   13624:     return ($symb,$courseid,$domain,$name,$publicuser);
                   13625: 
                   13626: }
                   13627: 
1.36      albertel 13628: # ------------------------------------------------------------ Serves up a file
1.472     albertel 13629: # returns either the contents of the file or 
                   13630: # -1 if the file doesn't exist
1.481     raeburn  13631: #
                   13632: # if the target is a file that was uploaded via DOCS, 
                   13633: # a check will be made to see if a current copy exists on the local server,
                   13634: # if it does this will be served, otherwise a copy will be retrieved from
                   13635: # the home server for the course and stored in /home/httpd/html/userfiles on
                   13636: # the local server.   
1.472     albertel 13637: 
1.36      albertel 13638: sub getfile {
1.538     albertel 13639:     my ($file) = @_;
1.609     banghart 13640:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 13641:     &repcopy($file);
                   13642:     return &readfile($file);
                   13643: }
                   13644: 
                   13645: sub repcopy_userfile {
                   13646:     my ($file)=@_;
1.1142    raeburn  13647:     my $londocroot = $perlvar{'lonDocRoot'};
                   13648:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  13649:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 13650:     my ($cdom,$cnum,$filename) = 
1.811     albertel 13651: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 13652:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   13653:     if (-e "$file") {
1.828     www      13654: # we already have a local copy, check it out
1.538     albertel 13655: 	my @fileinfo = stat($file);
1.828     www      13656: 	my $rtncode;
                   13657: 	my $info;
1.538     albertel 13658: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 13659: 	if ($lwpresp ne 'ok') {
1.828     www      13660: # there is no such file anymore, even though we had a local copy
1.482     albertel 13661: 	    if ($rtncode eq '404') {
1.538     albertel 13662: 		unlink($file);
1.482     albertel 13663: 	    }
                   13664: 	    return -1;
                   13665: 	}
                   13666: 	if ($info < $fileinfo[9]) {
1.828     www      13667: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  13668: 	    return 'ok';
1.828     www      13669: 	} else {
                   13670: # the file is outdated, get rid of it
                   13671: 	    unlink($file);
1.482     albertel 13672: 	}
1.828     www      13673:     }
                   13674: # one way or the other, at this point, we don't have the file
                   13675: # construct the correct path for the file
                   13676:     my @parts = ($cdom,$cnum); 
                   13677:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   13678: 	push @parts, split(/\//,$1);
                   13679:     }
                   13680:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   13681:     foreach my $part (@parts) {
                   13682: 	$path .= '/'.$part;
                   13683: 	if (!-e $path) {
                   13684: 	    mkdir($path,0770);
1.482     albertel 13685: 	}
                   13686:     }
1.828     www      13687: # now the path exists for sure
                   13688: # get a user agent
                   13689:     my $transferfile=$file.'.in.transfer';
                   13690: # FIXME: this should flock
                   13691:     if (-e $transferfile) { return 'ok'; }
                   13692:     my $request;
                   13693:     $uri=~s/^\///;
1.980     raeburn  13694:     my $homeserver = &homeserver($cnum,$cdom);
1.1398    raeburn  13695:     my $hostname = &hostname($homeserver);
1.980     raeburn  13696:     my $protocol = $protocol{$homeserver};
                   13697:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  13698:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.1345    raeburn  13699:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
1.828     www      13700: # did it work?
                   13701:     if ($response->is_error()) {
                   13702: 	unlink($transferfile);
                   13703: 	&logthis("Userfile repcopy failed for $uri");
                   13704: 	return -1;
                   13705:     }
                   13706: # worked, rename the transfer file
                   13707:     rename($transferfile,$file);
1.607     raeburn  13708:     return 'ok';
1.481     raeburn  13709: }
                   13710: 
1.517     albertel 13711: sub tokenwrapper {
                   13712:     my $uri=shift;
1.980     raeburn  13713:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 13714:     $uri=~s|^/||;
1.620     albertel 13715:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 13716:     my $token=$1;
1.552     albertel 13717:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   13718:     if ($udom && $uname && $file) {
                   13719: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  13720:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  13721:         my $homeserver = &homeserver($uname,$udom);
1.1397    raeburn  13722:         my $hostname = &hostname($homeserver);
1.980     raeburn  13723:         my $protocol = $protocol{$homeserver};
                   13724:         $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  13725:         return $protocol.'://'.$hostname.'/'.$uri.
1.517     albertel 13726:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   13727:                                '&tokenissued='.$perlvar{'lonHostID'};
                   13728:     } else {
                   13729:         return '/adm/notfound.html';
                   13730:     }
                   13731: }
                   13732: 
1.828     www      13733: # call with reqtype HEAD: get last modification time
                   13734: # call with reqtype GET: get the file contents
                   13735: # Do not call this with reqtype GET for large files! It loads everything into memory
                   13736: #
1.481     raeburn  13737: sub getuploaded {
                   13738:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   13739:     $uri=~s/^\///;
1.980     raeburn  13740:     my $homeserver = &homeserver($cnum,$cdom);
1.1397    raeburn  13741:     my $hostname = &hostname($homeserver);
1.980     raeburn  13742:     my $protocol = $protocol{$homeserver};
                   13743:     $protocol = 'http' if ($protocol ne 'https');
1.1397    raeburn  13744:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481     raeburn  13745:     my $request=new HTTP::Request($reqtype,$uri);
1.1345    raeburn  13746:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
1.481     raeburn  13747:     $$rtncode = $response->code;
1.482     albertel 13748:     if (! $response->is_success()) {
                   13749: 	return 'failed';
                   13750:     }      
                   13751:     if ($reqtype eq 'HEAD') {
1.486     www      13752: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 13753:     } elsif ($reqtype eq 'GET') {
                   13754: 	$$info = $response->content;
1.472     albertel 13755:     }
1.482     albertel 13756:     return 'ok';
1.36      albertel 13757: }
                   13758: 
1.481     raeburn  13759: sub readfile {
                   13760:     my $file = shift;
                   13761:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   13762:     my $fh;
1.1359    raeburn  13763:     open($fh,"<",$file);
1.481     raeburn  13764:     my $a='';
1.800     albertel 13765:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  13766:     return $a;
                   13767: }
                   13768: 
1.36      albertel 13769: sub filelocation {
1.590     banghart 13770:     my ($dir,$file) = @_;
                   13771:     my $location;
                   13772:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 13773: 
                   13774:     if ($file =~ m-^/adm/-) {
                   13775: 	$file=~s-^/adm/wrapper/-/-;
                   13776: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   13777:     }
1.882     albertel 13778: 
1.1139    www      13779:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  13780:         $location = $file;
1.609     banghart 13781:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 13782:         my ($udom,$uname,$filename)=
1.811     albertel 13783:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 13784:         my $home=&homeserver($uname,$udom);
                   13785:         my $is_me=0;
                   13786:         my @ids=&current_machine_ids();
                   13787:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   13788:         if ($is_me) {
1.1117    foxr     13789:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 13790:         } else {
                   13791:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   13792:   	      $udom.'/'.$uname.'/'.$filename;
                   13793:         }
1.882     albertel 13794:     } elsif ($file =~ m-^/adm/-) {
                   13795: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 13796:     } else {
                   13797:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      13798:         $file=~s:^/(res|priv)/:/:;
                   13799:         my $space=$1;
1.590     banghart 13800:         if ( !( $file =~ m:^/:) ) {
                   13801:             $location = $dir. '/'.$file;
                   13802:         } else {
1.1142    raeburn  13803:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 13804:         }
1.59      albertel 13805:     }
1.590     banghart 13806:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 13807:     while ($location=~m{/\.\./}) {
                   13808: 	if ($location =~ m{/[^/]+/\.\./}) {
                   13809: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   13810: 	} else {
                   13811: 	    $location=~ s{/\.\./}{/}g;
                   13812: 	}
                   13813:     } #remove dir/..
1.590     banghart 13814:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   13815:     return $location;
1.46      www      13816: }
1.36      albertel 13817: 
1.46      www      13818: sub hreflocation {
                   13819:     my ($dir,$file)=@_;
1.980     raeburn  13820:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 13821: 	$file=filelocation($dir,$file);
1.700     albertel 13822:     } elsif ($file=~m-^/adm/-) {
                   13823: 	$file=~s-^/adm/wrapper/-/-;
                   13824: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 13825:     }
                   13826:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   13827: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   13828:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  13829: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   13830: 	        {/uploaded/$1/$2/}x;
1.46      www      13831:     }
1.913     albertel 13832:     if ($file=~ m{^/userfiles/}) {
                   13833: 	$file =~ s{^/userfiles/}{/uploaded/};
                   13834:     }
1.462     albertel 13835:     return $file;
1.465     albertel 13836: }
                   13837: 
1.1139    www      13838: 
                   13839: 
                   13840: 
                   13841: 
1.465     albertel 13842: sub current_machine_domains {
1.853     albertel 13843:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   13844: }
                   13845: 
                   13846: sub machine_domains {
                   13847:     my ($hostname) = @_;
1.465     albertel 13848:     my @domains;
1.838     albertel 13849:     my %hostname = &all_hostnames();
1.465     albertel 13850:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  13851: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 13852: 	if ($hostname eq $name) {
1.844     albertel 13853: 	    push(@domains,&host_domain($id));
1.465     albertel 13854: 	}
                   13855:     }
                   13856:     return @domains;
                   13857: }
                   13858: 
                   13859: sub current_machine_ids {
1.853     albertel 13860:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   13861: }
                   13862: 
                   13863: sub machine_ids {
                   13864:     my ($hostname) = @_;
                   13865:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 13866:     my @ids;
1.888     albertel 13867:     my %name_to_host = &all_names();
1.889     albertel 13868:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   13869: 	return @{ $name_to_host{$hostname} };
                   13870:     }
                   13871:     return;
1.31      www      13872: }
                   13873: 
1.824     raeburn  13874: sub additional_machine_domains {
                   13875:     my @domains;
1.1359    raeburn  13876:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
1.824     raeburn  13877:     while( my $line = <$fh>) {
                   13878:         $line =~ s/\s//g;
                   13879:         push(@domains,$line);
                   13880:     }
                   13881:     return @domains;
                   13882: }
                   13883: 
                   13884: sub default_login_domain {
                   13885:     my $domain = $perlvar{'lonDefDomain'};
                   13886:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   13887:     foreach my $posdom (&current_machine_domains(),
                   13888:                         &additional_machine_domains()) {
                   13889:         if (lc($posdom) eq lc($testdomain)) {
                   13890:             $domain=$posdom;
                   13891:             last;
                   13892:         }
                   13893:     }
                   13894:     return $domain;
                   13895: }
                   13896: 
1.1398    raeburn  13897: sub uses_sts {
                   13898:     my ($ignore_cache) = @_;
                   13899:     my $lonhost = $perlvar{'lonHostID'};
                   13900:     my $hostname = &hostname($lonhost);
                   13901:     my $sts_on;
                   13902:     if ($protocol{$lonhost} eq 'https') {
                   13903:         my $cachetime = 12*3600;
                   13904:         if (!$ignore_cache) {
                   13905:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
                   13906:             if (defined($cached)) {
                   13907:                 return $sts_on;
                   13908:             }
                   13909:         }
                   13910:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
                   13911:         my $request=new HTTP::Request('HEAD',$url);
                   13912:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
                   13913:         if ($response->is_success) {
                   13914:             my $has_sts = $response->header('Strict-Transport-Security');
                   13915:             if ($has_sts eq '') {
                   13916:                 $sts_on = 0;
                   13917:             } else {
                   13918:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
                   13919:                     my $maxage = $1;
                   13920:                     if ($maxage) {
                   13921:                         $sts_on = 1;
                   13922:                     } else {
                   13923:                         $sts_on = 0;
                   13924:                     }
                   13925:                 } else {
                   13926:                     $sts_on = 0;
                   13927:                 }
                   13928:             }
                   13929:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
                   13930:         }
                   13931:     }
                   13932:     return;
                   13933: }
                   13934: 
1.31      www      13935: # ------------------------------------------------------------- Declutters URLs
                   13936: 
                   13937: sub declutter {
                   13938:     my $thisfn=shift;
1.569     albertel 13939:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261    raeburn  13940:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
                   13941:         $thisfn=~s{^/home/httpd/html}{};
                   13942:     }
1.31      www      13943:     $thisfn=~s/^\///;
1.697     albertel 13944:     $thisfn=~s|^adm/wrapper/||;
                   13945:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      13946:     $thisfn=~s/^res\///;
1.1172    bisitz   13947:     $thisfn=~s/^priv\///;
1.1032    raeburn  13948:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   13949:         $thisfn=~s/\?.+$//;
                   13950:     }
1.268     www      13951:     return $thisfn;
                   13952: }
                   13953: 
                   13954: # ------------------------------------------------------------- Clutter up URLs
                   13955: 
                   13956: sub clutter {
                   13957:     my $thisfn='/'.&declutter(shift);
1.887     albertel 13958:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 13959: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      13960:        $thisfn='/res'.$thisfn; 
                   13961:     }
1.1031    raeburn  13962:     if ($thisfn !~m|^/adm|) {
                   13963: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 13964: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 13965: 	} else {
                   13966: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   13967: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 13968: 	    if ($embstyle eq 'ssi'
                   13969: 		|| ($embstyle eq 'hdn')
                   13970: 		|| ($embstyle eq 'rat')
                   13971: 		|| ($embstyle eq 'prv')
                   13972: 		|| ($embstyle eq 'ign')) {
                   13973: 		#do nothing with these
                   13974: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 13975: 		|| ($embstyle eq 'emb')
                   13976: 		|| ($embstyle eq 'wrp')) {
                   13977: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 13978: 	    } elsif ($embstyle eq 'unk'
                   13979: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 13980: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 13981: 	    } else {
1.718     www      13982: #		&logthis("Got a blank emb style");
1.695     albertel 13983: 	    }
1.694     albertel 13984: 	}
1.1343    raeburn  13985:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298    raeburn  13986:         $thisfn='/adm/wrapper'.$thisfn;
1.694     albertel 13987:     }
1.31      www      13988:     return $thisfn;
1.12      www      13989: }
                   13990: 
1.787     albertel 13991: sub clutter_with_no_wrapper {
                   13992:     my $uri = &clutter(shift);
                   13993:     if ($uri =~ m-^/adm/-) {
                   13994: 	$uri =~ s-^/adm/wrapper/-/-;
                   13995: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   13996:     }
                   13997:     return $uri;
                   13998: }
                   13999: 
1.557     albertel 14000: sub freeze_escape {
                   14001:     my ($value)=@_;
                   14002:     if (ref($value)) {
                   14003: 	$value=&nfreeze($value);
                   14004: 	return '__FROZEN__'.&escape($value);
                   14005:     }
                   14006:     return &escape($value);
                   14007: }
                   14008: 
1.11      www      14009: 
1.557     albertel 14010: sub thaw_unescape {
                   14011:     my ($value)=@_;
                   14012:     if ($value =~ /^__FROZEN__/) {
                   14013: 	substr($value,0,10,undef);
                   14014: 	$value=&unescape($value);
                   14015: 	return &thaw($value);
                   14016:     }
                   14017:     return &unescape($value);
                   14018: }
                   14019: 
1.436     albertel 14020: sub correct_line_ends {
                   14021:     my ($result)=@_;
                   14022:     $$result =~s/\r\n/\n/mg;
                   14023:     $$result =~s/\r/\n/mg;
1.415     albertel 14024: }
1.1       albertel 14025: # ================================================================ Main Program
                   14026: 
1.184     www      14027: sub goodbye {
1.204     albertel 14028:    &logthis("Starting Shut down");
1.443     albertel 14029: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 14030:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 14031: #converted
1.599     albertel 14032: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 14033:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   14034: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   14035: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 14036: #1.1 only
1.870     albertel 14037: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   14038: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   14039: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   14040: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   14041:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 14042:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   14043:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      14044:    &flushcourselogs();
                   14045:    &logthis("Shutting down");
                   14046: }
                   14047: 
1.852     albertel 14048: sub get_dns {
1.1210    raeburn  14049:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 14050:     if (!$ignore_cache) {
                   14051: 	my ($content,$cached)=
                   14052: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   14053: 	if ($cached) {
1.1210    raeburn  14054: 	    &$func($content,$hashref);
1.869     albertel 14055: 	    return;
                   14056: 	}
                   14057:     }
                   14058: 
                   14059:     my %alldns;
1.1379    raeburn  14060:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
                   14061:         foreach my $dns (<$config>) {
                   14062: 	    next if ($dns !~ /^\^(\S*)/x);
                   14063:             my $line = $1;
                   14064:             my ($host,$protocol) = split(/:/,$line);
                   14065:             if ($protocol ne 'https') {
                   14066:                 $protocol = 'http';
                   14067:             }
                   14068: 	    $alldns{$host} = $protocol;
1.979     raeburn  14069:         }
1.1379    raeburn  14070:         close($config);
1.869     albertel 14071:     }
                   14072:     while (%alldns) {
1.1263    raeburn  14073: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
1.979     raeburn  14074: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.1345    raeburn  14075:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
1.979     raeburn  14076:         delete($alldns{$dns});
1.852     albertel 14077: 	next if ($response->is_error());
1.1379    raeburn  14078:         if ($url eq '/adm/dns/loncapaCRL') {
                   14079:             return &$func($response);
                   14080:         } else {
                   14081: 	    my @content = split("\n",$response->content);
                   14082: 	    unless ($nocache) {
                   14083: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
                   14084: 	    }
                   14085: 	    &$func(\@content,$hashref);
                   14086:             return;
                   14087:         }
                   14088:     }
                   14089:     my $which = (split('/',$url,4))[3];
                   14090:     if ($which eq 'loncapaCRL') {
                   14091:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
                   14092:         if (-e $diskfile) {
                   14093:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
                   14094:         } else {
                   14095:             &logthis("unable to contact DNS, no on disk file $diskfile available");
                   14096:         }
                   14097:     } else {
                   14098:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   14099:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
                   14100:             my @content = <$config>;
                   14101:             close($config);
                   14102:             &$func(\@content,$hashref);
                   14103:         }
1.852     albertel 14104:     }
1.1210    raeburn  14105:     return;
                   14106: }
                   14107: 
                   14108: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  14109: sub parse_dns_checksums_tab {
1.1210    raeburn  14110:     my ($lines,$hashref) = @_;
1.1264    raeburn  14111:     my $lonhost = $perlvar{'lonHostID'};
                   14112:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1210    raeburn  14113:     my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264    raeburn  14114:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
                   14115:     my $webconfdir = '/etc/httpd/conf';
                   14116:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
                   14117:         $webconfdir = '/etc/apache2';
                   14118:     } elsif ($distro =~ /^sles(\d+)$/) {
                   14119:         if ($1 >= 10) {
                   14120:             $webconfdir = '/etc/apache2';
                   14121:         }
                   14122:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
                   14123:         if ($1 >= 10.0) {
                   14124:             $webconfdir = '/etc/apache2';
                   14125:         }
                   14126:     }
1.1210    raeburn  14127:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   14128:     my (%chksum,%revnum);
                   14129:     if (ref($lines) eq 'ARRAY') {
                   14130:         chomp(@{$lines});
1.1238    raeburn  14131:         my $version = shift(@{$lines});
                   14132:         if ($version eq $release) {  
1.1210    raeburn  14133:             foreach my $line (@{$lines}) {
1.1238    raeburn  14134:                 my ($file,$version,$shasum) = split(/,/,$line);
1.1264    raeburn  14135:                 if ($file =~ m{^/etc/httpd/conf}) {
                   14136:                     if ($webconfdir eq '/etc/apache2') {
                   14137:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
                   14138:                     }
                   14139:                 }
1.1238    raeburn  14140:                 $chksum{$file} = $shasum;
                   14141:                 $revnum{$file} = $version;
1.1210    raeburn  14142:             }
                   14143:             if (ref($hashref) eq 'HASH') {
                   14144:                 %{$hashref} = (
                   14145:                                 sums     => \%chksum,
                   14146:                                 versions => \%revnum,
                   14147:                               );
                   14148:             }
                   14149:         }
                   14150:     }
1.869     albertel 14151:     return;
1.852     albertel 14152: }
1.1210    raeburn  14153: 
                   14154: sub fetch_dns_checksums {
1.1238    raeburn  14155:     my %checksums;
                   14156:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1260    raeburn  14157:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238    raeburn  14158:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   14159:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211    raeburn  14160:              \%checksums);
1.1210    raeburn  14161:     return \%checksums;
                   14162: }
                   14163: 
1.1379    raeburn  14164: sub fetch_crl_pemfile {
                   14165:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
                   14166: }
                   14167: 
                   14168: sub save_crl_pem {
                   14169:     my ($response) = @_;
1.1380    raeburn  14170:     my ($msg,$hadchanges);
1.1379    raeburn  14171:     if (ref($response)) {
                   14172:         my $now = time;
                   14173:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
                   14174:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
                   14175:         if (open(my $fh,'>',"$tmpcrl")) {
                   14176:             print $fh $response->content;
                   14177:             close($fh);
                   14178:             if (-e $lonca) {
                   14179:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
                   14180:                     my $check = <PIPE>;
                   14181:                     close(PIPE);
                   14182:                     chomp($check);
                   14183:                     if ($check eq 'verify OK') {
                   14184:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
1.1380    raeburn  14185:                         my $backup;
1.1379    raeburn  14186:                         if (-e $dest) {
1.1380    raeburn  14187:                             if (&File::Copy::move($dest,"$dest.bak")) {
                   14188:                                 $backup = 'ok';
                   14189:                             }
1.1379    raeburn  14190:                         }
                   14191:                         if (&File::Copy::move($tmpcrl,$dest)) {
                   14192:                             $msg = 'ok';
1.1380    raeburn  14193:                             if ($backup) {
                   14194:                                 my (%oldnums,%newnums);
                   14195:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
                   14196:                                     while (<PIPE>) {
                   14197:                                         $oldnums{(split(/:/))[1]} = 1;
                   14198:                                     }
                   14199:                                     close(PIPE);
                   14200:                                 }
                   14201:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
                   14202:                                     while(<PIPE>) {
                   14203:                                         $newnums{(split(/:/))[1]} = 1;
                   14204:                                     }
                   14205:                                     close(PIPE);
                   14206:                                 }
                   14207:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
                   14208:                                     unless (exists($oldnums{$key})) {
                   14209:                                         $hadchanges = 1;
                   14210:                                         last;
                   14211:                                     }
                   14212:                                 }
                   14213:                                 unless ($hadchanges) {
                   14214:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
                   14215:                                         unless (exists($newnums{$key})) {
                   14216:                                             $hadchanges = 1;
                   14217:                                             last;
                   14218:                                         }
                   14219:                                     }
                   14220:                                 }
                   14221:                             }
1.1379    raeburn  14222:                         }
                   14223:                     } else {
                   14224:                         unlink($tmpcrl);
                   14225:                     }
                   14226:                 } else {
                   14227:                     unlink($tmpcrl);
                   14228:                 }
                   14229:             } else {
                   14230:                 unlink($tmpcrl);
                   14231:             }
                   14232:         }
                   14233:     }
1.1380    raeburn  14234:     return ($msg,$hadchanges);
1.1379    raeburn  14235: }
                   14236: 
1.327     albertel 14237: # ------------------------------------------------------------ Read domain file
                   14238: {
1.852     albertel 14239:     my $loaded;
1.846     albertel 14240:     my %domain;
                   14241: 
1.852     albertel 14242:     sub parse_domain_tab {
                   14243: 	my ($lines) = @_;
                   14244: 	foreach my $line (@$lines) {
                   14245: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      14246: 
1.846     albertel 14247: 	    chomp($line);
1.852     albertel 14248: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 14249: 	    my %this_domain;
                   14250: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   14251: 			       'lang_def', 'city', 'longi', 'lati',
                   14252: 			       'primary') {
                   14253: 		$this_domain{$field} = shift(@elements);
                   14254: 	    }
                   14255: 	    $domain{$name} = \%this_domain;
1.852     albertel 14256: 	}
                   14257:     }
1.864     albertel 14258: 
                   14259:     sub reset_domain_info {
                   14260: 	undef($loaded);
                   14261: 	undef(%domain);
                   14262:     }
                   14263: 
1.852     albertel 14264:     sub load_domain_tab {
1.1293    raeburn  14265: 	my ($ignore_cache,$nocache) = @_;
                   14266: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852     albertel 14267: 	my $fh;
1.1359    raeburn  14268: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852     albertel 14269: 	    my @lines = <$fh>;
                   14270: 	    &parse_domain_tab(\@lines);
1.448     albertel 14271: 	}
1.852     albertel 14272: 	close($fh);
                   14273: 	$loaded = 1;
1.327     albertel 14274:     }
1.846     albertel 14275: 
                   14276:     sub domain {
1.852     albertel 14277: 	&load_domain_tab() if (!$loaded);
                   14278: 
1.846     albertel 14279: 	my ($name,$what) = @_;
                   14280: 	return if ( !exists($domain{$name}) );
                   14281: 
                   14282: 	if (!$what) {
                   14283: 	    return $domain{$name}{'description'};
                   14284: 	}
                   14285: 	return $domain{$name}{$what};
                   14286:     }
1.974     raeburn  14287: 
                   14288:     sub domain_info {
                   14289:         &load_domain_tab() if (!$loaded);
                   14290:         return %domain;
                   14291:     }
                   14292: 
1.327     albertel 14293: }
                   14294: 
                   14295: 
1.1       albertel 14296: # ------------------------------------------------------------- Read hosts file
                   14297: {
1.838     albertel 14298:     my %hostname;
1.844     albertel 14299:     my %hostdom;
1.845     albertel 14300:     my %libserv;
1.852     albertel 14301:     my $loaded;
1.888     albertel 14302:     my %name_to_host;
1.1074    raeburn  14303:     my %internetdom;
1.1107    raeburn  14304:     my %LC_dns_serv;
1.852     albertel 14305: 
                   14306:     sub parse_hosts_tab {
                   14307: 	my ($file) = @_;
                   14308: 	foreach my $configline (@$file) {
                   14309: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  14310:             chomp($configline);
                   14311: 	    if ($configline =~ /^\^/) {
                   14312:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   14313:                     $LC_dns_serv{$1} = 1;
                   14314:                 }
                   14315:                 next;
                   14316:             }
1.1074    raeburn  14317: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 14318: 	    $name=~s/\s//g;
                   14319: 	    if ($id && $domain && $role && $name) {
1.1351    raeburn  14320:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
                   14321:                     my $curr = $hostname{$id};
                   14322:                     my $skip;
                   14323:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
                   14324:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
                   14325:                             $skip = 1;
                   14326:                         } else {
                   14327:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
                   14328:                         }
                   14329:                     }
                   14330:                     unless ($skip) {
                   14331:                         push(@{$name_to_host{$name}},$id);
                   14332:                     }
                   14333:                 } else {
                   14334:                     push(@{$name_to_host{$name}},$id);
                   14335:                 }
1.852     albertel 14336: 		$hostname{$id}=$name;
                   14337: 		$hostdom{$id}=$domain;
                   14338: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  14339:                 if (defined($protocol)) {
                   14340:                     if ($protocol eq 'https') {
                   14341:                         $protocol{$id} = $protocol;
                   14342:                     } else {
                   14343:                         $protocol{$id} = 'http'; 
                   14344:                     }
1.968     raeburn  14345:                 } else {
1.969     raeburn  14346:                     $protocol{$id} = 'http';
1.968     raeburn  14347:                 }
1.1074    raeburn  14348:                 if (defined($intdom)) {
                   14349:                     $internetdom{$id} = $intdom;
                   14350:                 }
1.852     albertel 14351: 	    }
                   14352: 	}
                   14353:     }
1.864     albertel 14354:     
                   14355:     sub reset_hosts_info {
1.897     albertel 14356: 	&purge_remembered();
1.864     albertel 14357: 	&reset_domain_info();
                   14358: 	&reset_hosts_ip_info();
1.1339    raeburn  14359:         undef(%internetdom);
1.892     albertel 14360: 	undef(%name_to_host);
1.864     albertel 14361: 	undef(%hostname);
                   14362: 	undef(%hostdom);
                   14363: 	undef(%libserv);
                   14364: 	undef($loaded);
                   14365:     }
1.1       albertel 14366: 
1.852     albertel 14367:     sub load_hosts_tab {
1.1293    raeburn  14368: 	my ($ignore_cache,$nocache) = @_;
                   14369: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1359    raeburn  14370: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852     albertel 14371: 	my @config = <$config>;
                   14372: 	&parse_hosts_tab(\@config);
                   14373: 	close($config);
                   14374: 	$loaded=1;
1.1       albertel 14375:     }
1.852     albertel 14376: 
1.838     albertel 14377:     sub hostname {
1.852     albertel 14378: 	&load_hosts_tab() if (!$loaded);
                   14379: 
1.838     albertel 14380: 	my ($lonid) = @_;
                   14381: 	return $hostname{$lonid};
                   14382:     }
1.845     albertel 14383: 
1.838     albertel 14384:     sub all_hostnames {
1.852     albertel 14385: 	&load_hosts_tab() if (!$loaded);
                   14386: 
1.838     albertel 14387: 	return %hostname;
                   14388:     }
1.845     albertel 14389: 
1.888     albertel 14390:     sub all_names {
1.1293    raeburn  14391:         my ($ignore_cache,$nocache) = @_;
                   14392: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888     albertel 14393: 
                   14394: 	return %name_to_host;
                   14395:     }
                   14396: 
1.974     raeburn  14397:     sub all_host_domain {
                   14398:         &load_hosts_tab() if (!$loaded);
                   14399:         return %hostdom;
                   14400:     }
                   14401: 
1.1339    raeburn  14402:     sub all_host_intdom {
                   14403:         &load_hosts_tab() if (!$loaded);
                   14404:         return %internetdom;
                   14405:     }
                   14406: 
1.845     albertel 14407:     sub is_library {
1.852     albertel 14408: 	&load_hosts_tab() if (!$loaded);
                   14409: 
1.845     albertel 14410: 	return exists($libserv{$_[0]});
                   14411:     }
                   14412: 
                   14413:     sub all_library {
1.852     albertel 14414: 	&load_hosts_tab() if (!$loaded);
                   14415: 
1.845     albertel 14416: 	return %libserv;
                   14417:     }
                   14418: 
1.1062    droeschl 14419:     sub unique_library {
                   14420: 	#2x reverse removes all hostnames that appear more than once
                   14421:         my %unique = reverse &all_library();
                   14422:         return reverse %unique;
                   14423:     }
                   14424: 
1.841     albertel 14425:     sub get_servers {
1.852     albertel 14426: 	&load_hosts_tab() if (!$loaded);
                   14427: 
1.841     albertel 14428: 	my ($domain,$type) = @_;
                   14429: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   14430: 	                                          : %hostname;
                   14431: 	my %result;
1.842     albertel 14432: 	if (ref($domain) eq 'ARRAY') {
                   14433: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 14434: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 14435: 		    $result{$host} = $hostname;
                   14436: 		}
                   14437: 	    }
                   14438: 	} else {
                   14439: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   14440: 		if ($hostdom{$host} eq $domain) {
                   14441: 		    $result{$host} = $hostname;
                   14442: 		}
1.841     albertel 14443: 	    }
                   14444: 	}
                   14445: 	return %result;
                   14446:     }
1.845     albertel 14447: 
1.1062    droeschl 14448:     sub get_unique_servers {
                   14449:         my %unique = reverse &get_servers(@_);
                   14450: 	return reverse %unique;
                   14451:     }
                   14452: 
1.844     albertel 14453:     sub host_domain {
1.852     albertel 14454: 	&load_hosts_tab() if (!$loaded);
                   14455: 
1.844     albertel 14456: 	my ($lonid) = @_;
                   14457: 	return $hostdom{$lonid};
                   14458:     }
                   14459: 
1.841     albertel 14460:     sub all_domains {
1.852     albertel 14461: 	&load_hosts_tab() if (!$loaded);
                   14462: 
1.841     albertel 14463: 	my %seen;
                   14464: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   14465: 	return @uniq;
                   14466:     }
1.1074    raeburn  14467: 
                   14468:     sub internet_dom {
                   14469:         &load_hosts_tab() if (!$loaded);
                   14470: 
                   14471:         my ($lonid) = @_;
                   14472:         return $internetdom{$lonid};
                   14473:     }
1.1107    raeburn  14474: 
                   14475:     sub is_LC_dns {
                   14476:         &load_hosts_tab() if (!$loaded);
                   14477: 
                   14478:         my ($hostname) = @_;
                   14479:         return exists($LC_dns_serv{$hostname});
                   14480:     }
                   14481: 
1.1       albertel 14482: }
                   14483: 
1.847     albertel 14484: { 
                   14485:     my %iphost;
1.856     albertel 14486:     my %name_to_ip;
                   14487:     my %lonid_to_ip;
1.869     albertel 14488: 
1.847     albertel 14489:     sub get_hosts_from_ip {
                   14490: 	my ($ip) = @_;
                   14491: 	my %iphosts = &get_iphost();
                   14492: 	if (ref($iphosts{$ip})) {
                   14493: 	    return @{$iphosts{$ip}};
                   14494: 	}
                   14495: 	return;
1.839     albertel 14496:     }
1.864     albertel 14497:     
                   14498:     sub reset_hosts_ip_info {
                   14499: 	undef(%iphost);
                   14500: 	undef(%name_to_ip);
                   14501: 	undef(%lonid_to_ip);
                   14502:     }
1.856     albertel 14503: 
                   14504:     sub get_host_ip {
                   14505: 	my ($lonid) = @_;
                   14506: 	if (exists($lonid_to_ip{$lonid})) {
                   14507: 	    return $lonid_to_ip{$lonid};
                   14508: 	}
                   14509: 	my $name=&hostname($lonid);
                   14510:    	my $ip = gethostbyname($name);
                   14511: 	return if (!$ip || length($ip) ne 4);
                   14512: 	$ip=inet_ntoa($ip);
                   14513: 	$name_to_ip{$name}   = $ip;
                   14514: 	$lonid_to_ip{$lonid} = $ip;
                   14515: 	return $ip;
                   14516:     }
1.847     albertel 14517:     
                   14518:     sub get_iphost {
1.1293    raeburn  14519: 	my ($ignore_cache,$nocache) = @_;
1.894     albertel 14520: 
1.869     albertel 14521: 	if (!$ignore_cache) {
                   14522: 	    if (%iphost) {
                   14523: 		return %iphost;
                   14524: 	    }
                   14525: 	    my ($ip_info,$cached)=
                   14526: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   14527: 	    if ($cached) {
                   14528: 		%iphost      = %{$ip_info->[0]};
                   14529: 		%name_to_ip  = %{$ip_info->[1]};
                   14530: 		%lonid_to_ip = %{$ip_info->[2]};
                   14531: 		return %iphost;
                   14532: 	    }
                   14533: 	}
1.894     albertel 14534: 
                   14535: 	# get yesterday's info for fallback
                   14536: 	my %old_name_to_ip;
                   14537: 	my ($ip_info,$cached)=
                   14538: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   14539: 	if ($cached) {
                   14540: 	    %old_name_to_ip = %{$ip_info->[1]};
                   14541: 	}
                   14542: 
1.1293    raeburn  14543: 	my %name_to_host = &all_names($ignore_cache,$nocache);
1.888     albertel 14544: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 14545: 	    my $ip;
                   14546: 	    if (!exists($name_to_ip{$name})) {
                   14547: 		$ip = gethostbyname($name);
                   14548: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 14549: 		    if (defined($old_name_to_ip{$name})) {
                   14550: 			$ip = $old_name_to_ip{$name};
                   14551: 			&logthis("Can't find $name defaulting to old $ip");
                   14552: 		    } else {
                   14553: 			&logthis("Name $name no IP found");
                   14554: 			next;
                   14555: 		    }
                   14556: 		} else {
                   14557: 		    $ip=inet_ntoa($ip);
1.847     albertel 14558: 		}
                   14559: 		$name_to_ip{$name} = $ip;
                   14560: 	    } else {
                   14561: 		$ip = $name_to_ip{$name};
1.653     albertel 14562: 	    }
1.888     albertel 14563: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   14564: 		$lonid_to_ip{$id} = $ip;
                   14565: 	    }
                   14566: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 14567: 	}
1.1293    raeburn  14568:         unless ($nocache) {
                   14569: 	    &do_cache_new('iphost','iphost',
                   14570: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   14571: 		          48*60*60);
                   14572:         }
1.869     albertel 14573: 
1.847     albertel 14574: 	return %iphost;
1.598     albertel 14575:     }
                   14576: 
1.992     raeburn  14577:     #
                   14578:     #  Given a DNS returns the loncapa host name for that DNS 
                   14579:     # 
                   14580:     sub host_from_dns {
                   14581:         my ($dns) = @_;
                   14582:         my @hosts;
                   14583:         my $ip;
                   14584: 
1.993     raeburn  14585:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  14586:             $ip = $name_to_ip{$dns};
                   14587:         }
                   14588:         if (!$ip) {
                   14589:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   14590:             if (length($ip) == 4) { 
                   14591: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   14592:             }
                   14593:         }
                   14594:         if ($ip) {
                   14595: 	    @hosts = get_hosts_from_ip($ip);
                   14596: 	    return $hosts[0];
                   14597:         }
                   14598:         return undef;
1.986     foxr     14599:     }
1.992     raeburn  14600: 
1.1074    raeburn  14601:     sub get_internet_names {
                   14602:         my ($lonid) = @_;
                   14603:         return if ($lonid eq '');
                   14604:         my ($idnref,$cached)=
                   14605:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   14606:         if ($cached) {
                   14607:             return $idnref;
                   14608:         }
                   14609:         my $ip = &get_host_ip($lonid);
                   14610:         my @hosts = &get_hosts_from_ip($ip);
                   14611:         my %iphost = &get_iphost();
                   14612:         my (@idns,%seen);
                   14613:         foreach my $id (@hosts) {
                   14614:             my $dom = &host_domain($id);
                   14615:             my $prim_id = &domain($dom,'primary');
                   14616:             my $prim_ip = &get_host_ip($prim_id);
                   14617:             next if ($seen{$prim_ip});
                   14618:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   14619:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   14620:                     my $intdom = &internet_dom($id);
                   14621:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   14622:                         push(@idns,$intdom);
                   14623:                     }
                   14624:                 }
                   14625:             }
                   14626:             $seen{$prim_ip} = 1;
                   14627:         }
1.1219    raeburn  14628:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  14629:     }
                   14630: 
1.986     foxr     14631: }
                   14632: 
1.1079    raeburn  14633: sub all_loncaparevs {
1.1249    raeburn  14634:     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  14635: }
                   14636: 
1.1227    raeburn  14637: # ---------------------------------------------------------- Read loncaparev table
                   14638: {
                   14639:     sub load_loncaparevs { 
                   14640:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1359    raeburn  14641:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1227    raeburn  14642:                 while (my $configline=<$config>) {
                   14643:                     chomp($configline);
                   14644:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   14645:                     $loncaparevs{$hostid}=$loncaparev;
                   14646:                 }
                   14647:                 close($config);
                   14648:             }
                   14649:         }
                   14650:     }
                   14651: }
                   14652: 
                   14653: # ---------------------------------------------------------- Read serverhostID table
                   14654: {
                   14655:     sub load_serverhomeIDs {
                   14656:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1359    raeburn  14657:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1227    raeburn  14658:                 while (my $configline=<$config>) {
                   14659:                     chomp($configline);
                   14660:                     my ($name,$id)=split(/:/,$configline);
                   14661:                     $serverhomeIDs{$name}=$id;
                   14662:                 }
                   14663:                 close($config);
                   14664:             }
                   14665:         }
                   14666:     }
                   14667: }
                   14668: 
                   14669: 
1.862     albertel 14670: BEGIN {
                   14671: 
                   14672: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   14673:     unless ($readit) {
                   14674: {
                   14675:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   14676:     %perlvar = (%perlvar,%{$configvars});
                   14677: }
                   14678: 
                   14679: 
1.1       albertel 14680: # ------------------------------------------------------ Read spare server file
                   14681: {
1.1359    raeburn  14682:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 14683: 
                   14684:     while (my $configline=<$config>) {
                   14685:        chomp($configline);
1.284     matthew  14686:        if ($configline) {
1.784     albertel 14687: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 14688: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 14689: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 14690:        }
                   14691:     }
1.448     albertel 14692:     close($config);
1.1       albertel 14693: }
1.11      www      14694: # ------------------------------------------------------------ Read permissions
                   14695: {
1.1359    raeburn  14696:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11      www      14697: 
                   14698:     while (my $configline=<$config>) {
1.448     albertel 14699: 	chomp($configline);
                   14700: 	if ($configline) {
                   14701: 	    my ($role,$perm)=split(/ /,$configline);
                   14702: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   14703: 	}
1.11      www      14704:     }
1.448     albertel 14705:     close($config);
1.11      www      14706: }
                   14707: 
                   14708: # -------------------------------------------- Read plain texts for permissions
                   14709: {
1.1359    raeburn  14710:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      14711: 
                   14712:     while (my $configline=<$config>) {
1.448     albertel 14713: 	chomp($configline);
                   14714: 	if ($configline) {
1.742     raeburn  14715: 	    my ($short,@plain)=split(/:/,$configline);
                   14716:             %{$prp{$short}} = ();
                   14717: 	    if (@plain > 0) {
                   14718:                 $prp{$short}{'std'} = $plain[0];
                   14719:                 for (my $i=1; $i<@plain; $i++) {
                   14720:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   14721:                 }
                   14722:             }
1.448     albertel 14723: 	}
1.135     www      14724:     }
1.448     albertel 14725:     close($config);
1.135     www      14726: }
                   14727: 
                   14728: # ---------------------------------------------------------- Read package table
                   14729: {
1.1359    raeburn  14730:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135     www      14731: 
                   14732:     while (my $configline=<$config>) {
1.483     albertel 14733: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 14734: 	chomp($configline);
                   14735: 	my ($short,$plain)=split(/:/,$configline);
                   14736: 	my ($pack,$name)=split(/\&/,$short);
                   14737: 	if ($plain ne '') {
                   14738: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   14739: 	    $packagetab{$short}=$plain; 
                   14740: 	}
1.11      www      14741:     }
1.448     albertel 14742:     close($config);
1.329     matthew  14743: }
                   14744: 
1.1073    raeburn  14745: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  14746: 
                   14747: &load_loncaparevs();
1.1073    raeburn  14748: 
1.1074    raeburn  14749: # ---------------------------------------------------------- Read serverhostID table
                   14750: 
1.1227    raeburn  14751: &load_serverhomeIDs();
                   14752: 
                   14753: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  14754: {
                   14755:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   14756:     if (-e $file) {
                   14757:         my $parser = HTML::LCParser->new($file);
                   14758:         while (my $token = $parser->get_token()) {
                   14759:             if ($token->[0] eq 'S') {
                   14760:                 my $item = $token->[1];
                   14761:                 my $name = $token->[2]{'name'};
                   14762:                 my $value = $token->[2]{'value'};
1.1285    raeburn  14763:                 my $valuematch = $token->[2]{'valuematch'};
1.1303    raeburn  14764:                 my $namematch = $token->[2]{'namematch'};
                   14765:                 if ($item eq 'parameter') {
                   14766:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
                   14767:                         my $release = $parser->get_text();
                   14768:                         $release =~ s/(^\s*|\s*$ )//gx;
                   14769:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
                   14770:                     }
                   14771:                 } elsif ($item ne '' && $name ne '') {
1.1079    raeburn  14772:                     my $release = $parser->get_text();
                   14773:                     $release =~ s/(^\s*|\s*$ )//gx;
1.1303    raeburn  14774:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079    raeburn  14775:                 }
                   14776:             }
                   14777:         }
                   14778:     }
1.1073    raeburn  14779: }
                   14780: 
1.1138    raeburn  14781: # ---------------------------------------------------------- Read managers table
                   14782: {
                   14783:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1359    raeburn  14784:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138    raeburn  14785:             while (my $configline=<$config>) {
                   14786:                 chomp($configline);
                   14787:                 next if ($configline =~ /^\#/);
                   14788:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   14789:                     $managerstab{$configline} = 1;
                   14790:                 }
                   14791:             }
                   14792:             close($config);
                   14793:         }
                   14794:     }
                   14795: }
                   14796: 
1.329     matthew  14797: # ------------- set up temporary directory
                   14798: {
1.1117    foxr     14799:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  14800: 
1.11      www      14801: }
                   14802: 
1.794     albertel 14803: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   14804: 				'compress_threshold'=> 20_000,
                   14805:  			        });
1.185     www      14806: 
1.281     www      14807: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      14808: $dumpcount=0;
1.958     www      14809: $locknum=0;
1.22      www      14810: 
1.163     harris41 14811: &logtouch();
1.672     albertel 14812: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      14813: $readit=1;
1.564     albertel 14814:     {
                   14815: 	use integer;
                   14816: 	my $test=(2**32)+1;
1.568     albertel 14817: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 14818: 	&logthis(" Detected 64bit platform ($_64bit)");
                   14819:     }
1.195     www      14820: }
1.1       albertel 14821: }
1.179     www      14822: 
1.1       albertel 14823: 1;
1.191     harris41 14824: __END__
                   14825: 
1.243     albertel 14826: =pod
                   14827: 
1.191     harris41 14828: =head1 NAME
                   14829: 
1.243     albertel 14830: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 14831: 
                   14832: =head1 SYNOPSIS
                   14833: 
1.243     albertel 14834: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 14835: 
                   14836:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   14837: 
1.243     albertel 14838: Common parameters:
                   14839: 
                   14840: =over 4
                   14841: 
                   14842: =item *
                   14843: 
                   14844: $uname : an internal username (if $cname expecting a course Id specifically)
                   14845: 
                   14846: =item *
                   14847: 
                   14848: $udom : a domain (if $cdom expecting a course's domain specifically)
                   14849: 
                   14850: =item *
                   14851: 
                   14852: $symb : a resource instance identifier
                   14853: 
                   14854: =item *
                   14855: 
                   14856: $namespace : the name of a .db file that contains the data needed or
                   14857: being set.
                   14858: 
                   14859: =back
                   14860: 
1.394     bowersj2 14861: =head1 OVERVIEW
1.191     harris41 14862: 
1.394     bowersj2 14863: lonnet provides subroutines which interact with the
                   14864: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   14865: about classes, users, and resources.
1.243     albertel 14866: 
                   14867: For many of these objects you can also use this to store data about
                   14868: them or modify them in various ways.
1.191     harris41 14869: 
1.394     bowersj2 14870: =head2 Symbs
1.191     harris41 14871: 
1.394     bowersj2 14872: To identify a specific instance of a resource, LON-CAPA uses symbols
                   14873: or "symbs"X<symb>. These identifiers are built from the URL of the
                   14874: map, the resource number of the resource in the map, and the URL of
                   14875: the resource itself. The latter is somewhat redundant, but might help
                   14876: if maps change.
                   14877: 
                   14878: An example is
                   14879: 
                   14880:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   14881: 
                   14882: The respective map entry is
                   14883: 
                   14884:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   14885:   title="Problem 2">
                   14886:  </resource>
                   14887: 
                   14888: Symbs are used by the random number generator, as well as to store and
                   14889: restore data specific to a certain instance of for example a problem.
                   14890: 
                   14891: =head2 Storing And Retrieving Data
                   14892: 
                   14893: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   14894: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   14895: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   14896: is is the non-critical message twin of cstore. These functions are for
                   14897: handlers to store a perl hash to a user's permanent data space in an
                   14898: easy manner, and to retrieve it again on another call. It is expected
                   14899: that a handler would use this once at the beginning to retrieve data,
                   14900: and then again once at the end to send only the new data back.
                   14901: 
                   14902: The data is stored in the user's data directory on the user's
                   14903: homeserver under the ID of the course.
                   14904: 
                   14905: The hash that is returned by restore will have all of the previous
                   14906: value for all of the elements of the hash.
                   14907: 
                   14908: Example:
                   14909: 
                   14910:  #creating a hash
                   14911:  my %hash;
                   14912:  $hash{'foo'}='bar';
                   14913: 
                   14914:  #storing it
                   14915:  &Apache::lonnet::cstore(\%hash);
                   14916: 
                   14917:  #changing a value
                   14918:  $hash{'foo'}='notbar';
                   14919: 
                   14920:  #adding a new value
                   14921:  $hash{'bar'}='foo';
                   14922:  &Apache::lonnet::cstore(\%hash);
                   14923: 
                   14924:  #retrieving the hash
                   14925:  my %history=&Apache::lonnet::restore();
                   14926: 
                   14927:  #print the hash
                   14928:  foreach my $key (sort(keys(%history))) {
                   14929:    print("\%history{$key} = $history{$key}");
                   14930:  }
                   14931: 
                   14932: Will print out:
1.191     harris41 14933: 
1.394     bowersj2 14934:  %history{1:foo} = bar
                   14935:  %history{1:keys} = foo:timestamp
                   14936:  %history{1:timestamp} = 990455579
                   14937:  %history{2:bar} = foo
                   14938:  %history{2:foo} = notbar
                   14939:  %history{2:keys} = foo:bar:timestamp
                   14940:  %history{2:timestamp} = 990455580
                   14941:  %history{bar} = foo
                   14942:  %history{foo} = notbar
                   14943:  %history{timestamp} = 990455580
                   14944:  %history{version} = 2
                   14945: 
                   14946: Note that the special hash entries C<keys>, C<version> and
                   14947: C<timestamp> were added to the hash. C<version> will be equal to the
                   14948: total number of versions of the data that have been stored. The
                   14949: C<timestamp> attribute will be the UNIX time the hash was
                   14950: stored. C<keys> is available in every historical section to list which
                   14951: keys were added or changed at a specific historical revision of a
                   14952: hash.
                   14953: 
                   14954: B<Warning>: do not store the hash that restore returns directly. This
                   14955: will cause a mess since it will restore the historical keys as if the
                   14956: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 14957: 
1.394     bowersj2 14958: Calling convention:
1.191     harris41 14959: 
1.1225    raeburn  14960:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269    raeburn  14961:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191     harris41 14962: 
1.394     bowersj2 14963: For more detailed information, see lonnet specific documentation.
1.191     harris41 14964: 
1.394     bowersj2 14965: =head1 RETURN MESSAGES
1.191     harris41 14966: 
1.394     bowersj2 14967: =over 4
1.191     harris41 14968: 
1.394     bowersj2 14969: =item * B<con_lost>: unable to contact remote host
1.191     harris41 14970: 
1.394     bowersj2 14971: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   14972: when the connection is brought back up
1.191     harris41 14973: 
1.394     bowersj2 14974: =item * B<con_failed>: unable to contact remote host and unable to save message
                   14975: for later delivery
1.191     harris41 14976: 
1.967     bisitz   14977: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 14978: 
1.394     bowersj2 14979: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 14980: that was requested
1.191     harris41 14981: 
1.243     albertel 14982: =back
1.191     harris41 14983: 
1.243     albertel 14984: =head1 PUBLIC SUBROUTINES
1.191     harris41 14985: 
1.243     albertel 14986: =head2 Session Environment Functions
1.191     harris41 14987: 
1.243     albertel 14988: =over 4
1.191     harris41 14989: 
1.394     bowersj2 14990: =item * 
                   14991: X<appenv()>
1.949     raeburn  14992: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 14993: the user envirnoment file, and will be restored for each access this
1.620     albertel 14994: user makes during this session, also modifies the %env for the current
1.949     raeburn  14995: process. Optional rolesarrayref - if defined contains a reference to an array
                   14996: of roles which are exempt from the restriction on modifying user.role entries 
                   14997: in the user's environment.db and in %env.    
1.191     harris41 14998: 
                   14999: =item *
1.394     bowersj2 15000: X<delenv()>
1.987     raeburn  15001: B<delenv($delthis,$regexp)>: removes all items from the session
                   15002: environment file that begin with $delthis. If the 
                   15003: optional second arg - $regexp - is true, $delthis is treated as a 
                   15004: regular expression, otherwise \Q$delthis\E is used. 
                   15005: The values are also deleted from the current processes %env.
1.191     harris41 15006: 
1.795     albertel 15007: =item * get_env_multiple($name) 
                   15008: 
                   15009: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   15010: values may be defined and end up as an array ref.
                   15011: 
                   15012: returns an array of values
                   15013: 
1.243     albertel 15014: =back
                   15015: 
                   15016: =head2 User Information
1.191     harris41 15017: 
1.243     albertel 15018: =over 4
1.191     harris41 15019: 
                   15020: =item *
1.394     bowersj2 15021: X<queryauthenticate()>
                   15022: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 15023: authentication scheme
                   15024: 
                   15025: =item *
1.394     bowersj2 15026: X<authenticate()>
1.1073    raeburn  15027: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 15028: authenticate user from domain's lib servers (first use the current
                   15029: one). C<$upass> should be the users password.
1.1073    raeburn  15030: $checkdefauth is optional (value is 1 if a check should be made to
                   15031:    authenticate user using default authentication method, and allow
                   15032:    account creation if username does not have account in the domain).
                   15033: $clientcancheckhost is optional (value is 1 if checking whether the
                   15034:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 15035: 
                   15036: =item *
1.394     bowersj2 15037: X<homeserver()>
                   15038: B<homeserver($uname,$udom)>: find the server which has
                   15039: the user's directory and files (there must be only one), this caches
                   15040: the answer, and also caches if there is a borken connection.
1.191     harris41 15041: 
                   15042: =item *
1.394     bowersj2 15043: X<idget()>
1.1300    raeburn  15044: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
                   15045: a list of student/employee IDs or clicker IDs
                   15046: (student/employee IDs are a unique resource in a domain, there must be 
                   15047: only 1 ID per username, and only 1 username per ID in a specific domain).
                   15048: clickerIDs are not necessarily unique, as students might share clickers.
                   15049: (returns hash: id=>name,id=>name)
1.191     harris41 15050: 
                   15051: =item *
1.394     bowersj2 15052: X<idrget()>
                   15053: B<idrget($udom,@unames)>: find the IDs behind a list of
                   15054: usernames (returns hash: name=>id,name=>id)
1.191     harris41 15055: 
                   15056: =item *
1.394     bowersj2 15057: X<idput()>
1.1300    raeburn  15058: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
                   15059: names and associated student/employee IDs or clicker IDs.
                   15060: 
                   15061: =item *
                   15062: X<iddel()>
                   15063: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
                   15064: student/employee ID or clicker ID username look-ups from domain.
                   15065: The homeserver ($uhome) and namespace ($namespace) are optional.
                   15066: If no $uhome is provided, it will be determined usig &homeserver()
                   15067: for each user.  If no $namespace is provided, the default is ids.
                   15068: 
                   15069: =item *
                   15070: X<updateclickers()>
                   15071: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
                   15072: clicker ID-to-username look-ups in clickers.db on library server.
                   15073: Permitted actions are add or del (i.e., add or delete). The 
                   15074: clickers.db contains clickerID as keys (escaped), and each corresponding
                   15075: value is an escaped comma-separated list of usernames (for whom the
                   15076: library server is the homeserver), who registered that particular ID.
                   15077: If $critical is true, the update will be sent via &critical, otherwise
                   15078: &reply() will be used.
1.191     harris41 15079: 
                   15080: =item *
1.394     bowersj2 15081: X<rolesinit()>
1.1169    droeschl 15082: B<rolesinit($udom,$username)>: get user privileges.
                   15083: returns user role, first access and timer interval hashes
1.243     albertel 15084: 
                   15085: =item *
1.1171    droeschl 15086: X<privileged()>
                   15087: B<privileged($username,$domain)>: returns a true if user has a
                   15088: privileged and active role (i.e. su or dc), false otherwise.
                   15089: 
                   15090: =item *
1.551     albertel 15091: X<getsection()>
                   15092: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 15093: course $cname, return section name/number or '' for "not in course"
                   15094: and '-1' for "no section"
                   15095: 
                   15096: =item *
1.394     bowersj2 15097: X<userenvironment()>
                   15098: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 15099: passed in @what from the requested user's environment, returns a hash
                   15100: 
1.858     raeburn  15101: =item * 
                   15102: X<userlog_query()>
1.859     albertel 15103: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   15104: activity.log file. %filters defines filters applied when parsing the
                   15105: log file. These can be start or end timestamps, or the type of action
                   15106: - log to look for Login or Logout events, check for Checkin or
                   15107: Checkout, role for role selection. The response is in the form
                   15108: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   15109: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  15110: 
1.243     albertel 15111: =back
                   15112: 
                   15113: =head2 User Roles
                   15114: 
                   15115: =over 4
                   15116: 
                   15117: =item *
                   15118: 
1.1284    raeburn  15119: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
                   15120: returns codes for allowed actions.
                   15121: 
                   15122: The first argument is required, all others are optional.
                   15123: 
                   15124: $priv is the privilege being checked.
                   15125: $uri contains additional information about what is being checked for access (e.g.,
                   15126: URL, course ID etc.). 
                   15127: $symb is the unique resource instance identifier in a course; if needed,
                   15128: but not provided, it will be retrieved via a call to &symbread(). 
                   15129: $role is the role for which a priv is being checked (only used if priv is evb). 
                   15130: $clientip is the user's IP address (only used when checking for access to portfolio 
                   15131: files).
                   15132: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
                   15133: prevents recursive calls to &allowed.
                   15134: 
1.243     albertel 15135:  F: full access
                   15136:  U,I,K: authentication modes (cxx only)
                   15137:  '': forbidden
                   15138:  1: user needs to choose course
                   15139:  2: browse allowed
1.766     albertel 15140:  A: passphrase authentication needed
1.1284    raeburn  15141:  B: access temporarily blocked because of a blocking event in a course.
1.1402    raeburn  15142:  D: access blocked because access is required via session initiated via deep-link 
1.243     albertel 15143: 
                   15144: =item *
                   15145: 
1.1192    raeburn  15146: constructaccess($url,$setpriv) : check for access to construction space URL
                   15147: 
                   15148: See if the owner domain and name in the URL match those in the
                   15149: expected environment.  If so, return three element list
                   15150: ($ownername,$ownerdomain,$ownerhome).
                   15151: 
                   15152: Otherwise return the null string.
                   15153: 
                   15154: If second argument 'setpriv' is true, it assigns the privileges,
                   15155: and returns the same three element list, unless the owner has
                   15156: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   15157: in which case the null string is returned.
                   15158: 
                   15159: =item *
                   15160: 
1.1326    raeburn  15161: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
                   15162: define a custom role rolename set privileges in format of lonTabs/roles.tab
                   15163: for system, domain, and course level. $uname and $udom are optional (current
                   15164: user's username and domain will be used when either of $uname or $udom are absent.
1.243     albertel 15165: 
                   15166: =item *
                   15167: 
1.988     raeburn  15168: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   15169: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  15170: $type is Course (default) or Community.
1.988     raeburn  15171: If $forcedefault evaluates to true, text returned will be default 
                   15172: text for $type. Otherwise, if this is a course, the text returned 
                   15173: will be a custom name for the role (if defined in the course's 
                   15174: environment).  If no custom name is defined the default is returned.
                   15175:    
1.832     raeburn  15176: =item *
                   15177: 
1.1219    raeburn  15178: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  15179: All arguments are optional. Returns a hash of a roles, either for
                   15180: co-author/assistant author roles for a user's Construction Space
1.906     albertel 15181: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  15182: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   15183: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   15184: For each key, value is set to colon-separated start and end times for
                   15185: the role.  If no username and domain are specified, will default to
1.934     raeburn  15186: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  15187: of role statuses (active, future or previous), roles 
                   15188: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   15189: to restrict the list of roles reported. If no array ref is 
                   15190: provided for types, will default to return only active roles.
1.834     albertel 15191: 
1.1195    raeburn  15192: =item *
                   15193: 
                   15194: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  15195: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   15196: 
                   15197: Additional optional arguments are: $type (if role checking is to be restricted 
                   15198: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  15199: available roles) or future (roles available in the future), and
                   15200: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  15201: have active Domain Coordinator role in course's domain or in additional
                   15202: domains (specified in 'Domains to check for privileged users' in course
                   15203: environment -- set via:  Course Settings -> Classlists and staff listing).
                   15204: 
                   15205: =item *
                   15206: 
                   15207: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   15208: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   15209: $possdomains and $possroles are optional array refs -- to domains to check and
                   15210: roles to check.  If $possdomains is not specified, a dump will be done of the
                   15211: users' roles.db to check for a dc or su role in any domain. This can be
                   15212: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   15213: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   15214: this then allows &privileged_by_domain() to be used, which caches the identity
                   15215: of privileged users, eliminating the need for repeated calls to &dump().
                   15216: 
                   15217: =item *
                   15218: 
                   15219: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   15220: where the outer hash keys are domains specified in the $possdomains array ref,
                   15221: next inner hash keys are privileged roles specified in the $roles array ref,
                   15222: and the innermost hash contains key = value pairs for username:domain = end:start
                   15223: for active or future "privileged" users with that role in that domain. To avoid
                   15224: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   15225: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  15226: 
1.243     albertel 15227: =back
                   15228: 
                   15229: =head2 User Modification
                   15230: 
                   15231: =over 4
                   15232: 
                   15233: =item *
                   15234: 
1.957     raeburn  15235: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 15236: user for the level given by URL.  Optional start and end dates (leave empty
                   15237: string or zero for "no date")
1.191     harris41 15238: 
                   15239: =item *
                   15240: 
1.243     albertel 15241: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   15242: change a users, password, possible return values are: ok,
                   15243: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   15244: refused
1.191     harris41 15245: 
                   15246: =item *
                   15247: 
1.243     albertel 15248: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 15249: 
                   15250: =item *
                   15251: 
1.1058    raeburn  15252: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   15253:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   15254: 
                   15255: will update user information (firstname,middlename,lastname,generation,
                   15256: permanentemail), and if forceid is true, student/employee ID also.
                   15257: A user's institutional affiliation(s) can also be updated.
                   15258: User information fields will not be overwritten with empty entries 
                   15259: unless the field is included in the $candelete array reference.
                   15260: This array is included when a single user is modified via "Manage Users",
                   15261: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 15262: 
                   15263: =item *
                   15264: 
1.286     matthew  15265: modifystudent
                   15266: 
1.957     raeburn  15267: modify a student's enrollment and identification information.
1.1235    raeburn  15268: The course id is resolved based on the current user's environment.  
                   15269: This means the invoking user must be a course coordinator or otherwise
1.286     matthew  15270: associated with a course.
                   15271: 
1.297     matthew  15272: This call is essentially a wrapper for lonnet::modifyuser and
                   15273: lonnet::modify_student_enrollment
1.286     matthew  15274: 
                   15275: Inputs: 
                   15276: 
                   15277: =over 4
                   15278: 
1.957     raeburn  15279: =item B<$udom> Student's loncapa domain
1.286     matthew  15280: 
1.957     raeburn  15281: =item B<$uname> Student's loncapa login name
1.286     matthew  15282: 
1.964     bisitz   15283: =item B<$uid> Student/Employee ID
1.286     matthew  15284: 
1.957     raeburn  15285: =item B<$umode> Student's authentication mode
1.286     matthew  15286: 
1.957     raeburn  15287: =item B<$upass> Student's password
1.286     matthew  15288: 
1.957     raeburn  15289: =item B<$first> Student's first name
1.286     matthew  15290: 
1.957     raeburn  15291: =item B<$middle> Student's middle name
1.286     matthew  15292: 
1.957     raeburn  15293: =item B<$last> Student's last name
1.286     matthew  15294: 
1.957     raeburn  15295: =item B<$gene> Student's generation
1.286     matthew  15296: 
1.957     raeburn  15297: =item B<$usec> Student's section in course
1.286     matthew  15298: 
                   15299: =item B<$end> Unix time of the roles expiration
                   15300: 
                   15301: =item B<$start> Unix time of the roles start date
                   15302: 
                   15303: =item B<$forceid> If defined, allow $uid to be changed
                   15304: 
                   15305: =item B<$desiredhome> server to use as home server for student
                   15306: 
1.957     raeburn  15307: =item B<$email> Student's permanent e-mail address
                   15308: 
                   15309: =item B<$type> Type of enrollment (auto or manual)
                   15310: 
1.963     raeburn  15311: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   15312: 
                   15313: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  15314: 
1.963     raeburn  15315: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  15316: 
1.963     raeburn  15317: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  15318: 
1.1216    raeburn  15319: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   15320: 
                   15321: =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  15322: 
1.286     matthew  15323: =back
1.297     matthew  15324: 
                   15325: =item *
                   15326: 
                   15327: modify_student_enrollment
                   15328: 
1.1235    raeburn  15329: Change a student's enrollment status in a class.  The environment variable
1.297     matthew  15330: 'role.request.course' must be defined for this function to proceed.
                   15331: 
                   15332: Inputs:
                   15333: 
                   15334: =over 4
                   15335: 
1.1235    raeburn  15336: =item $udom, student's domain
1.297     matthew  15337: 
1.1235    raeburn  15338: =item $uname, student's name
1.297     matthew  15339: 
1.1235    raeburn  15340: =item $uid, student's user id
1.297     matthew  15341: 
1.1235    raeburn  15342: =item $first, student's first name
1.297     matthew  15343: 
                   15344: =item $middle
                   15345: 
                   15346: =item $last
                   15347: 
                   15348: =item $gene
                   15349: 
                   15350: =item $usec
                   15351: 
                   15352: =item $end
                   15353: 
                   15354: =item $start
                   15355: 
1.957     raeburn  15356: =item $type
                   15357: 
                   15358: =item $locktype
                   15359: 
                   15360: =item $cid
                   15361: 
                   15362: =item $selfenroll
                   15363: 
                   15364: =item $context
                   15365: 
1.1216    raeburn  15366: =item $credits, number of credits student will earn from this class
                   15367: 
1.1314    raeburn  15368: =item $instsec, institutional course section code for student
                   15369: 
1.297     matthew  15370: =back
                   15371: 
1.191     harris41 15372: 
                   15373: =item *
                   15374: 
1.243     albertel 15375: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   15376: custom role; give a custom role to a user for the level given by URL.  Specify
                   15377: name and domain of role author, and role name
1.191     harris41 15378: 
                   15379: =item *
                   15380: 
1.243     albertel 15381: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 15382: 
                   15383: =item *
                   15384: 
1.243     albertel 15385: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   15386: 
                   15387: =back
                   15388: 
                   15389: =head2 Course Infomation
                   15390: 
                   15391: =over 4
1.191     harris41 15392: 
                   15393: =item *
                   15394: 
1.1118    foxr     15395: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 15396: specified course id, including all environment settings for the
                   15397: course, the description of the course will be in the hash under the
                   15398: key 'description'
1.191     harris41 15399: 
1.1118    foxr     15400: $options is an optional parameter that if supplied is a hash reference that controls
                   15401: what how this function works.  It has the following key/values:
                   15402: 
                   15403: =over 4
                   15404: 
                   15405: =item freshen_cache
                   15406: 
                   15407: If defined, and the environment cache for the course is valid, it is 
                   15408: returned in the returned hash.
                   15409: 
                   15410: =item one_time
                   15411: 
                   15412: If defined, the last cache time is set to _now_
                   15413: 
                   15414: =item user
                   15415: 
                   15416: If defined, the supplied username is used instead of the current user.
                   15417: 
                   15418: 
                   15419: =back
                   15420: 
1.191     harris41 15421: =item *
                   15422: 
1.624     albertel 15423: resdata($name,$domain,$type,@which) : request for current parameter
                   15424: setting for a specific $type, where $type is either 'course' or 'user',
                   15425: @what should be a list of parameters to ask about. This routine caches
1.1235    raeburn  15426: answers for 10 minutes.
1.243     albertel 15427: 
1.877     foxr     15428: =item *
                   15429: 
                   15430: get_courseresdata($courseid, $domain) : dump the entire course resource
                   15431: data base, returning a hash that is keyed by the resource name and has
                   15432: values that are the resource value.  I believe that the timestamps and
                   15433: versions are also returned.
                   15434: 
1.1236    raeburn  15435: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
                   15436: supplemental content area. This routine caches the number of files for 
                   15437: 10 minutes.
                   15438: 
1.243     albertel 15439: =back
                   15440: 
                   15441: =head2 Course Modification
                   15442: 
                   15443: =over 4
1.191     harris41 15444: 
                   15445: =item *
                   15446: 
1.243     albertel 15447: writecoursepref($courseid,%prefs) : write preferences (environment
                   15448: database) for a course
1.191     harris41 15449: 
                   15450: =item *
                   15451: 
1.1011    raeburn  15452: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   15453: 
                   15454: =item *
                   15455: 
1.1038    raeburn  15456: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 15457: 
1.1167    droeschl 15458: =item *
                   15459: 
                   15460: is_course($courseid), is_course($cdom, $cnum)
                   15461: 
                   15462: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   15463: two component version $cdom, $cnum. It checks if the specified course exists.
                   15464: 
                   15465: Returns:
                   15466:     undef if the course doesn't exist, otherwise
                   15467:     in scalar context the combined courseid.
                   15468:     in list context the two components of the course identifier, domain and 
                   15469:     courseid.    
                   15470: 
1.243     albertel 15471: =back
                   15472: 
1.1401    raeburn  15473: =head2 Bubblesheet Configuration
                   15474: 
                   15475: =over 4
                   15476: 
                   15477: =item *
                   15478: 
                   15479: get_scantron_config($which)
                   15480: 
                   15481: $which - the name of the configuration to parse from the file.
                   15482: 
                   15483: Parses and returns the bubblesheet configuration line selected as a
                   15484: hash of configuration file fields.
                   15485: 
                   15486: 
                   15487: Returns:
                   15488:     If the named configuration is not in the file, an empty
                   15489:     hash is returned.
                   15490: 
                   15491:     a hash with the fields
                   15492:       name         - internal name for the this configuration setup
                   15493:       description  - text to display to operator that describes this config
                   15494:       CODElocation - if 0 or the string 'none'
                   15495:                           - no CODE exists for this config
                   15496:                      if -1 || the string 'letter'
                   15497:                           - a CODE exists for this config and is
                   15498:                             a string of letters
                   15499:                      Unsupported value (but planned for future support)
                   15500:                           if a positive integer
                   15501:                                - The CODE exists as the first n items from
                   15502:                                  the question section of the form
                   15503:                           if the string 'number'
                   15504:                                - The CODE exists for this config and is
                   15505:                                  a string of numbers
                   15506:       CODEstart   - (only matter if a CODE exists) column in the line where
                   15507:                      the CODE starts
                   15508:       CODElength  - length of the CODE
                   15509:       IDstart     - column where the student/employee ID starts
                   15510:       IDlength    - length of the student/employee ID info
                   15511:       Qstart      - column where the information from the bubbled
                   15512:                     'questions' start
                   15513:       Qlength     - number of columns comprising a single bubble line from
                   15514:                     the sheet. (usually either 1 or 10)
                   15515:       Qon         - either a single character representing the character used
                   15516:                     to signal a bubble was chosen in the positional setup, or
                   15517:                     the string 'letter' if the letter of the chosen bubble is
                   15518:                     in the final, or 'number' if a number representing the
                   15519:                     chosen bubble is in the file (1->A 0->J)
                   15520:       Qoff        - the character used to represent that a bubble was
                   15521:                     left blank
                   15522:       PaperID     - if the scanning process generates a unique number for each
                   15523:                     sheet scanned the column that this ID number starts in
                   15524:       PaperIDlength - number of columns that comprise the unique ID number
                   15525:                       for the sheet of paper
                   15526:       FirstName   - column that the first name starts in
                   15527:       FirstNameLength - number of columns that the first name spans
                   15528: 
                   15529:       LastName    - column that the last name starts in
                   15530:       LastNameLength - number of columns that the last name spans
                   15531:       BubblesPerRow - number of bubbles available in each row used to
                   15532:                       bubble an answer. (If not specified, 10 assumed).
                   15533: 
                   15534: 
                   15535: =item *
                   15536: 
                   15537: get_scantronformat_file($cdom)
                   15538: 
                   15539: $cdom - the course's domain (optional); if not supplied, uses
                   15540: domain for current $env{'request.course.id'}.
                   15541: 
                   15542: Returns an array containing lines from the scantron format file for
                   15543: the domain of the course.
                   15544: 
                   15545: If a url for a custom.tab file is listed in domain's configuration.db,
                   15546: lines are from this file.
                   15547: 
                   15548: Otherwise, if a default.tab has been published in RES space by the
                   15549: domainconfig user, lines are from this file.
                   15550: 
                   15551: Otherwise, fall back to getting lines from the legacy file on the
                   15552: local server:  /home/httpd/lonTabs/default_scantronformat.tab
                   15553: 
                   15554: =back
                   15555: 
1.243     albertel 15556: =head2 Resource Subroutines
                   15557: 
                   15558: =over 4
1.191     harris41 15559: 
                   15560: =item *
                   15561: 
1.243     albertel 15562: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 15563: 
                   15564: =item *
                   15565: 
1.243     albertel 15566: repcopy($filename) : subscribes to the requested file, and attempts to
                   15567: replicate from the owning library server, Might return
1.607     raeburn  15568: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   15569: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 15570: resource. Expects the local filesystem pathname
                   15571: (/home/httpd/html/res/....)
                   15572: 
                   15573: =back
                   15574: 
                   15575: =head2 Resource Information
                   15576: 
                   15577: =over 4
1.191     harris41 15578: 
                   15579: =item *
                   15580: 
1.1228    raeburn  15581: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   15582: and returns the value of a variety of different possible values,
                   15583: $varname should be a request string, and the other parameters can be
                   15584: used to specify who and what one is asking about. Ordinarily, $cid 
                   15585: does not need to be specified, as it is retrived from 
                   15586: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   15587: within lonuserstate::loadmap() when initializing a course, before
                   15588: $env{'request.course.id'} has been set, so it needs to be provided
                   15589: in that one case.
1.243     albertel 15590: 
                   15591: Possible values for $varname are environment.lastname (or other item
                   15592: from the envirnment hash), user.name (or someother aspect about the
                   15593: user), resource.0.maxtries (or some other part and parameter of a
                   15594: resource)
1.204     albertel 15595: 
                   15596: =item *
                   15597: 
1.243     albertel 15598: directcondval($number) : get current value of a condition; reads from a state
                   15599: string
1.204     albertel 15600: 
                   15601: =item *
                   15602: 
1.243     albertel 15603: condval($condidx) : value of condition index based on state
1.204     albertel 15604: 
                   15605: =item *
                   15606: 
1.1362    raeburn  15607: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243     albertel 15608: resource's metadata, $what should be either a specific key, or either
                   15609: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1362    raeburn  15610: packages that this resource currently uses, the last 3 arguments are 
                   15611: only used internally for recursive metadata.
                   15612: 
                   15613: the toolsymb is only used where the uri is for an external tool (for which
                   15614: the uri as well as the symb are guaranteed to be unique).
1.243     albertel 15615: 
1.1371    raeburn  15616: this function automatically caches all requests except any made recursively
                   15617: to retrieve a list of metadata keys for an imported library file ($liburi is 
                   15618: defined).
1.191     harris41 15619: 
                   15620: =item *
                   15621: 
1.243     albertel 15622: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   15623: network of library servers; returns file handle of where SQL and regex results
                   15624: will be stored for query
1.191     harris41 15625: 
                   15626: =item *
                   15627: 
1.1282    raeburn  15628: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
                   15629: return symbolic list entry (all arguments optional). 
                   15630: 
                   15631: Args: filename is the filename (including path) for the file for which a symb 
                   15632: is required; donotrecurse, if true will prevent calls to allowed() being made 
                   15633: to check access status if more than one resource was found in the bighash 
                   15634: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
                   15635: a randompick); ignorecachednull, if true will prevent a symb of '' being 
                   15636: returned if $env{$cache_str} is defined as ''; checkforblock if true will
                   15637: cause possible symbs to be checked to determine if they are subject to content
                   15638: blocking, if so they will not be included as possible symbs; possibles is a
                   15639: ref to a hash, which, as a side effect, will be populated with all possible 
                   15640: symbs (content blocking not tested).
                   15641:  
1.243     albertel 15642: returns the data handle
1.191     harris41 15643: 
                   15644: =item *
                   15645: 
1.1190    raeburn  15646: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   15647: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 15648: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  15649: on failure, user must be in a course, as it assumes the existence of
                   15650: the course initial hash, and uses $env('request.course.id'}.  The third
                   15651: arg is an optional reference to a scalar.  If this arg is passed in the 
                   15652: call to symbverify, it will be set to 1 if the symb has been set to be 
                   15653: encrypted; otherwise it will be null.  
1.191     harris41 15654: 
                   15655: =item *
                   15656: 
1.243     albertel 15657: symbclean($symb) : removes versions numbers from a symb, returns the
                   15658: cleaned symb
1.191     harris41 15659: 
                   15660: =item *
                   15661: 
1.243     albertel 15662: is_on_map($uri) : checks if the $uri is somewhere on the current
                   15663: course map, user must be in a course for it to work.
1.191     harris41 15664: 
                   15665: =item *
                   15666: 
1.243     albertel 15667: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 15668: 
                   15669: =item *
                   15670: 
1.243     albertel 15671: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   15672: a random seed, all arguments are optional, if they aren't sent it uses the
                   15673: environment to derive them. Note: if symb isn't sent and it can't get one
                   15674: from &symbread it will use the current time as its return value
1.191     harris41 15675: 
                   15676: =item *
                   15677: 
1.243     albertel 15678: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   15679: unfakeable, receipt
1.191     harris41 15680: 
                   15681: =item *
                   15682: 
1.620     albertel 15683: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 15684: 
                   15685: =item *
                   15686: 
1.243     albertel 15687: countacc($url) : count the number of accesses to a given URL
1.191     harris41 15688: 
                   15689: =item *
                   15690: 
1.243     albertel 15691: 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 15692: 
                   15693: =item *
                   15694: 
1.243     albertel 15695: 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 15696: 
                   15697: =item *
                   15698: 
1.243     albertel 15699: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 15700: 
                   15701: =item *
                   15702: 
1.243     albertel 15703: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   15704: forcing spreadsheet to reevaluate the resource scores next time.
                   15705: 
1.1195    raeburn  15706: =item * 
                   15707: 
1.1196    raeburn  15708: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   15709: when viewing in course context.
1.1195    raeburn  15710: 
1.1196    raeburn  15711:  input: six args -- filename (decluttered), course number, course domain,
                   15712:                     url, symb (if registered) and group (if this is a 
                   15713:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  15714: 
1.1196    raeburn  15715:  output: array of five scalars --
1.1195    raeburn  15716:          $cfile -- url for file editing if editable on current server
                   15717:          $home -- homeserver of resource (i.e., for author if published,
                   15718:                                           or course if uploaded.).
                   15719:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  15720:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   15721:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  15722: 
                   15723: =item *
                   15724: 
                   15725: is_course_upload($file,$cnum,$cdom)
                   15726: 
                   15727: Used in course context to determine if current file was uploaded to 
                   15728: the course (i.e., would be found in /userfiles/docs on the course's 
                   15729: homeserver.
                   15730: 
                   15731:   input: 3 args -- filename (decluttered), course number and course domain.
                   15732:   output: boolean -- 1 if file was uploaded.
                   15733: 
1.243     albertel 15734: =back
                   15735: 
                   15736: =head2 Storing/Retreiving Data
                   15737: 
                   15738: =over 4
1.191     harris41 15739: 
                   15740: =item *
                   15741: 
1.1269    raeburn  15742: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
                   15743: permanently for this url; hashref needs to be given and should be a \%hashname;
                   15744: the remaining args aren't required and if they aren't passed or are '' they will
                   15745: be derived from the env (with the exception of $laststore, which is an 
                   15746: optional arg used when a user's submission is stored in grading).
                   15747: $laststore is $version=$timestamp, where $version is the most recent version
                   15748: number retrieved for the corresponding $symb in the $namespace db file, and
                   15749: $timestamp is the timestamp for that transaction (UNIX time).
                   15750: $laststore is currently only passed when cstore() is called by 
                   15751: structuretags::finalize_storage().
1.191     harris41 15752: 
                   15753: =item *
                   15754: 
1.1269    raeburn  15755: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
                   15756: but uses critical subroutine
1.191     harris41 15757: 
                   15758: =item *
                   15759: 
1.243     albertel 15760: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   15761: all args are optional
1.191     harris41 15762: 
                   15763: =item *
                   15764: 
1.717     albertel 15765: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   15766: dumps the complete (or key matching regexp) namespace into a hash
                   15767: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   15768: normally &store()ed into
                   15769: 
                   15770: $range should be either an integer '100' (give me the first 100
                   15771:                                            matching records)
                   15772:               or be  two integers sperated by a - with no spaces
                   15773:                  '30-50' (give me the 30th through the 50th matching
                   15774:                           records)
                   15775: 
                   15776: 
                   15777: =item *
                   15778: 
1.1269    raeburn  15779: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717     albertel 15780: replaces a &store() version of data with a replacement set of data
                   15781: for a particular resource in a namespace passed in the $storehash hash 
1.1269    raeburn  15782: reference. If $tolog is true, the transaction is logged in the courselog
                   15783: with an action=PUTSTORE.
1.717     albertel 15784: 
                   15785: =item *
                   15786: 
1.243     albertel 15787: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   15788: works very similar to store/cstore, but all data is stored in a
                   15789: temporary location and can be reset using tmpreset, $storehash should
                   15790: be a hash reference, returns nothing on success
1.191     harris41 15791: 
                   15792: =item *
                   15793: 
1.243     albertel 15794: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   15795: similar to restore, but all data is stored in a temporary location and
                   15796: can be reset using tmpreset. Returns a hash of values on success,
                   15797: error string otherwise.
1.191     harris41 15798: 
                   15799: =item *
                   15800: 
1.243     albertel 15801: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   15802: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 15803: 
                   15804: =item *
                   15805: 
1.243     albertel 15806: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   15807: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 15808: 
                   15809: =item *
                   15810: 
1.243     albertel 15811: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   15812: namesp ($udom and $uname are optional)
1.191     harris41 15813: 
                   15814: =item *
                   15815: 
1.702     albertel 15816: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 15817: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 15818: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  15819: 
1.702     albertel 15820: $range should be either an integer '100' (give me the first 100
                   15821:                                            matching records)
                   15822:               or be  two integers sperated by a - with no spaces
                   15823:                  '30-50' (give me the 30th through the 50th matching
                   15824:                           records)
1.449     matthew  15825: =item *
                   15826: 
                   15827: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   15828: $store can be a scalar, an array reference, or if the amount to be 
                   15829: incremented is > 1, a hash reference.
                   15830: 
                   15831: ($udom and $uname are optional)
1.191     harris41 15832: 
                   15833: =item *
                   15834: 
1.243     albertel 15835: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   15836: ($udom and $uname are optional)
1.191     harris41 15837: 
                   15838: =item *
                   15839: 
1.243     albertel 15840: cput($namespace,$storehash,$udom,$uname) : critical put
                   15841: ($udom and $uname are optional)
1.191     harris41 15842: 
                   15843: =item *
                   15844: 
1.748     albertel 15845: newput($namespace,$storehash,$udom,$uname) :
                   15846: 
                   15847: Attempts to store the items in the $storehash, but only if they don't
                   15848: currently exist, if this succeeds you can be certain that you have 
                   15849: successfully created a new key value pair in the $namespace db.
                   15850: 
                   15851: 
                   15852: Args:
                   15853:  $namespace: name of database to store values to
                   15854:  $storehash: hashref to store to the db
                   15855:  $udom: (optional) domain of user containing the db
                   15856:  $uname: (optional) name of user caontaining the db
                   15857: 
                   15858: Returns:
                   15859:  'ok' -> succeeded in storing all keys of $storehash
                   15860:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   15861:                         least <key> already existed in the db (other
                   15862:                         requested keys may also already exist)
1.967     bisitz   15863:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 15864:  'con_lost' -> unable to contact request server
                   15865:  'refused' -> action was not allowed by remote machine
                   15866: 
                   15867: 
                   15868: =item *
                   15869: 
1.243     albertel 15870: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   15871: reference filled in from namesp (encrypts the return communication)
                   15872: ($udom and $uname are optional)
1.191     harris41 15873: 
                   15874: =item *
                   15875: 
1.243     albertel 15876: log($udom,$name,$home,$message) : write to permanent log for user; use
                   15877: critical subroutine
                   15878: 
1.806     raeburn  15879: =item *
                   15880: 
1.860     raeburn  15881: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   15882: array reference filled in from namespace found in domain level on either
                   15883: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  15884: 
                   15885: =item *
                   15886: 
1.860     raeburn  15887: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   15888: domain level either on specified domain server ($uhome) or primary domain 
                   15889: server ($udom and $uhome are optional)
1.806     raeburn  15890: 
1.943     raeburn  15891: =item * 
                   15892: 
1.1240    raeburn  15893: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
                   15894: for: authentication, language, quotas, timezone, date locale, and portal URL in
                   15895: the target domain.
                   15896: 
                   15897: May also include additional key => value pairs for the following groups:
                   15898: 
                   15899: =over
                   15900: 
                   15901: =item
                   15902: disk quotas (MB allocated by default to portfolios and authoring spaces).
                   15903: 
                   15904: =over
                   15905: 
                   15906: =item defaultquota, authorquota
                   15907: 
                   15908: =back
                   15909: 
                   15910: =item
                   15911: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
                   15912: portfolio for users).
                   15913: 
                   15914: =over
                   15915: 
                   15916: =item
                   15917: aboutme, blog, webdav, portfolio
                   15918: 
                   15919: =back
                   15920: 
                   15921: =item
                   15922: requestcourses: ability to request courses, and how requests are processed.
                   15923: 
                   15924: =over
                   15925: 
                   15926: =item
1.1305    raeburn  15927: official, unofficial, community, textbook, placement
1.1240    raeburn  15928: 
                   15929: =back
                   15930: 
                   15931: =item
                   15932: inststatus: types of institutional affiliation, and order in which they are displayed.
                   15933: 
                   15934: =over
                   15935: 
                   15936: =item
1.1256    raeburn  15937: inststatustypes, inststatusorder, inststatusguest
1.1240    raeburn  15938: 
                   15939: =back
                   15940: 
                   15941: =item
                   15942: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
                   15943: for course's uploaded content.
                   15944: 
                   15945: =over
                   15946: 
                   15947: =item
1.1246    raeburn  15948: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
1.1305    raeburn  15949: communityquota, textbookquota, placementquota
1.1240    raeburn  15950: 
                   15951: =back
                   15952: 
                   15953: =item
                   15954: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
                   15955: on your servers.
                   15956: 
                   15957: =over
                   15958: 
                   15959: =item 
                   15960: remotesessions, hostedsessions
                   15961: 
                   15962: =back
                   15963: 
                   15964: =back
                   15965: 
                   15966: In cases where a domain coordinator has never used the "Set Domain Configuration"
                   15967: utility to create a configuration.db file on a domain's primary library server 
                   15968: only the following domain defaults: auth_def, auth_arg_def, lang_def
                   15969: -- corresponding values are authentication type (internal, krb4, krb5,
                   15970: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
                   15971: will be available. Values are retrieved from cache (if current), unless the
                   15972: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
                   15973: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
                   15974: 
                   15975: Typical usage:
1.943     raeburn  15976: 
1.1240    raeburn  15977: %domdefaults = &get_domain_defaults($target_domain);
1.943     raeburn  15978: 
1.243     albertel 15979: =back
                   15980: 
                   15981: =head2 Network Status Functions
                   15982: 
                   15983: =over 4
1.191     harris41 15984: 
                   15985: =item *
                   15986: 
1.1137    raeburn  15987: dirlist() : return directory list based on URI (first arg).
                   15988: 
                   15989: Inputs: 1 required, 5 optional.
                   15990: 
                   15991: =over
                   15992: 
                   15993: =item 
                   15994: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   15995: 
                   15996: =item
                   15997: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   15998: 
                   15999: =item
                   16000: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   16001: 
                   16002: =item
                   16003: $getpropath - boolean: 1 if prepend path using &propath(). 
                   16004: 
                   16005: =item
                   16006: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   16007: 
                   16008: =item 
                   16009: $alternateRoot - path to prepend in place of path from $uri.
                   16010: 
                   16011: =back
                   16012: 
                   16013: Returns: Array of up to two items.
                   16014: 
                   16015: =over
                   16016: 
                   16017: a reference to an array of files/subdirectories
                   16018: 
                   16019: =over
                   16020: 
                   16021: Each element in the array of files/subdirectories is a & separated list of
                   16022: item name and the result of running stat on the item.  If dirlist was requested
                   16023: for a file instead of a directory, the item name will be ''. For a directory 
                   16024: listing, if the item is a metadata file, the element will end &N&M 
                   16025: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   16026: default copyright set (1).  
                   16027: 
                   16028: =back
                   16029: 
                   16030: a scalar containing error condition (if encountered).
                   16031: 
                   16032: =over
                   16033: 
                   16034: =item 
                   16035: no_host (no homeserver identified for $username:$domain).
                   16036: 
                   16037: =item 
                   16038: no_such_host (server contacted for listing not identified as valid host).
                   16039: 
                   16040: =item 
                   16041: con_lost (connection to remote server failed).
                   16042: 
                   16043: =item 
                   16044: refused (invalid $username:$domain received on lond side).
                   16045: 
                   16046: =item 
                   16047: no_such_dir (directory at specified path on lond side does not exist). 
                   16048: 
                   16049: =item 
                   16050: empty (directory at specified path on lond side is empty).
                   16051: 
                   16052: =over
                   16053: 
                   16054: This is currently not encountered because the &ls3, &ls2, 
                   16055: &ls (_handler) routines on the lond side do not filter out
                   16056: . and .. from a directory listing. 
                   16057: 
                   16058: =back
                   16059: 
                   16060: =back
                   16061: 
                   16062: =back
1.191     harris41 16063: 
                   16064: =item *
                   16065: 
1.243     albertel 16066: spareserver() : find server with least workload from spare.tab
                   16067: 
1.986     foxr     16068: 
                   16069: =item *
                   16070: 
                   16071: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   16072: if there is no corresponding loncapa host.
                   16073: 
1.243     albertel 16074: =back
                   16075: 
1.986     foxr     16076: 
1.243     albertel 16077: =head2 Apache Request
                   16078: 
                   16079: =over 4
1.191     harris41 16080: 
                   16081: =item *
                   16082: 
1.243     albertel 16083: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   16084: localhost, posts hash
                   16085: 
                   16086: =back
                   16087: 
                   16088: =head2 Data to String to Data
                   16089: 
                   16090: =over 4
1.191     harris41 16091: 
                   16092: =item *
                   16093: 
1.243     albertel 16094: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   16095: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 16096: 
                   16097: =item *
                   16098: 
1.243     albertel 16099: hashref2str($hashref) : convert a hashref into a string complete with
                   16100: escaping and '=' and '&' separators, supports elements that are
                   16101: arrayrefs and hashrefs
1.191     harris41 16102: 
                   16103: =item *
                   16104: 
1.243     albertel 16105: arrayref2str($arrayref) : convert an arrayref into a string complete
                   16106: with escaping and '&' separators, supports elements that are arrayrefs
                   16107: and hashrefs
1.191     harris41 16108: 
                   16109: =item *
                   16110: 
1.243     albertel 16111: str2hash($string) : convert string to hash using unescaping and
                   16112: splitting on '=' and '&', supports elements that are arrayrefs and
                   16113: hashrefs
1.191     harris41 16114: 
                   16115: =item *
                   16116: 
1.243     albertel 16117: str2array($string) : convert string to hash using unescaping and
                   16118: splitting on '&', supports elements that are arrayrefs and hashrefs
                   16119: 
                   16120: =back
                   16121: 
                   16122: =head2 Logging Routines
                   16123: 
                   16124: 
                   16125: These routines allow one to make log messages in the lonnet.log and
                   16126: lonnet.perm logfiles.
1.191     harris41 16127: 
1.1119    foxr     16128: =over 4
                   16129: 
1.191     harris41 16130: =item *
                   16131: 
1.243     albertel 16132: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 16133: 
                   16134: =item *
                   16135: 
1.243     albertel 16136: logthis() : append message to the normal lonnet.log file, it gets
                   16137: preiodically rolled over and deleted.
1.191     harris41 16138: 
                   16139: =item *
                   16140: 
1.243     albertel 16141: logperm() : append a permanent message to lonnet.perm.log, this log
                   16142: file never gets deleted by any automated portion of the system, only
                   16143: messages of critical importance should go in here.
                   16144: 
1.1119    foxr     16145: 
1.243     albertel 16146: =back
                   16147: 
                   16148: =head2 General File Helper Routines
                   16149: 
                   16150: =over 4
1.191     harris41 16151: 
                   16152: =item *
                   16153: 
1.481     raeburn  16154: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   16155: (a) files in /uploaded
                   16156:   (i) If a local copy of the file exists - 
                   16157:       compares modification date of local copy with last-modified date for 
                   16158:       definitive version stored on home server for course. If local copy is 
                   16159:       stale, requests a new version from the home server and stores it. 
                   16160:       If the original has been removed from the home server, then local copy 
                   16161:       is unlinked.
                   16162:   (ii) If local copy does not exist -
                   16163:       requests the file from the home server and stores it. 
                   16164:   
                   16165:   If $caller is 'uploadrep':  
                   16166:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   16167:     for request for files originally uploaded via DOCS. 
                   16168:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   16169:   
                   16170:   Otherwise:
                   16171:      This indicates a call from the content generation phase of the request.
                   16172:      -  returns the entire contents of the file or -1.
                   16173:      
                   16174: (b) files in /res
                   16175:    - returns the entire contents of a file or -1; 
                   16176:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 16177: 
1.712     albertel 16178: 
                   16179: =item *
                   16180: 
                   16181: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   16182:                   reference
                   16183: 
                   16184: returns either a stat() list of data about the file or an empty list
                   16185: if the file doesn't exist or couldn't find out about it (connection
                   16186: problems or user unknown)
                   16187: 
1.191     harris41 16188: =item *
                   16189: 
1.243     albertel 16190: filelocation($dir,$file) : returns file system location of a file
                   16191: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   16192: directory that relative $file lookups are to looked in ($dir of /a/dir
                   16193: and a file of ../bob will become /a/bob)
1.191     harris41 16194: 
                   16195: =item *
                   16196: 
                   16197: hreflocation($dir,$file) : returns file system location or a URL; same as
                   16198: filelocation except for hrefs
                   16199: 
                   16200: =item *
                   16201: 
1.1261    raeburn  16202: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
                   16203: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191     harris41 16204: 
1.243     albertel 16205: =back
                   16206: 
1.608     albertel 16207: =head2 Usererfile file routines (/uploaded*)
                   16208: 
                   16209: =over 4
                   16210: 
                   16211: =item *
                   16212: 
                   16213: userfileupload(): main rotine for putting a file in a user or course's
                   16214:                   filespace, arguments are,
                   16215: 
1.620     albertel 16216:  formname - required - this is the name of the element in $env where the
1.608     albertel 16217:            filename, and the contents of the file to create/modifed exist
1.620     albertel 16218:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   16219:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  16220:  context - if coursedoc, store the file in the course of the active role
                   16221:              of the current user; 
                   16222:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   16223:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 16224:  subdir - required - subdirectory to put the file in under ../userfiles/
                   16225:          if undefined, it will be placed in "unknown"
                   16226: 
                   16227:  (This routine calls clean_filename() to remove any dangerous
                   16228:  characters from the filename, and then calls finuserfileupload() to
                   16229:  complete the transaction)
                   16230: 
                   16231:  returns either the url of the uploaded file (/uploaded/....) if successful
                   16232:  and /adm/notfound.html if unsuccessful
                   16233: 
                   16234: =item *
                   16235: 
                   16236: clean_filename(): routine for cleaing a filename up for storage in
                   16237:                  userfile space, argument is:
                   16238: 
                   16239:  filename - proposed filename
                   16240: 
                   16241: returns: the new clean filename
                   16242: 
                   16243: =item *
                   16244: 
1.1090    raeburn  16245: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 16246: userspace, probably shouldn't be called directly
                   16247: 
                   16248:   docuname: username or courseid of destination for the file
                   16249:   docudom: domain of user/course of destination for the file
                   16250:   formname: same as for userfileupload()
1.1090    raeburn  16251:   fname: filename (including subdirectories) for the file
                   16252:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1401    raeburn  16253:           if hashref, and context is scantron, will convert csv format to standard format
1.1090    raeburn  16254:   allfiles: reference to hash used to store objects found by parser
                   16255:   codebase: reference to hash used for codebases of java objects found by parser
                   16256:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   16257:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   16258:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   16259:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   16260:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   16261:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  16262:   mimetype: reference to scalar to accommodate mime type determined
                   16263:             from File::MMagic if $parser = parse.
1.608     albertel 16264: 
                   16265:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  16266:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   16267:  was 'overwrite').
                   16268:  
1.608     albertel 16269: 
                   16270: =item *
                   16271: 
                   16272: renameuserfile(): renames an existing userfile to a new name
                   16273: 
                   16274:   Args:
                   16275:    docuname: username or courseid of destination for the file
                   16276:    docudom: domain of user/course of destination for the file
                   16277:    old: current file name (including any subdirs under userfiles)
                   16278:    new: desired file name (including any subdirs under userfiles)
                   16279: 
                   16280: =item *
                   16281: 
                   16282: mkdiruserfile(): creates a directory is a userfiles dir
                   16283: 
                   16284:   Args:
                   16285:    docuname: username or courseid of destination for the file
                   16286:    docudom: domain of user/course of destination for the file
                   16287:    dir: dir to create (including any subdirs under userfiles)
                   16288: 
                   16289: =item *
                   16290: 
                   16291: removeuserfile(): removes a file that exists in userfiles
                   16292: 
                   16293:   Args:
                   16294:    docuname: username or courseid of destination for the file
                   16295:    docudom: domain of user/course of destination for the file
                   16296:    fname: filname to delete (including any subdirs under userfiles)
                   16297: 
                   16298: =item *
                   16299: 
                   16300: removeuploadedurl(): convience function for removeuserfile()
                   16301: 
                   16302:   Args:
                   16303:    url:  a full /uploaded/... url to delete
                   16304: 
1.747     albertel 16305: =item * 
                   16306: 
                   16307: get_portfile_permissions():
                   16308:   Args:
                   16309:     domain: domain of user or course contain the portfolio files
                   16310:     user: name of user or num of course contain the portfolio files
                   16311:   Returns:
                   16312:     hashref of a dump of the proper file_permissions.db
                   16313:    
                   16314: 
                   16315: =item * 
                   16316: 
                   16317: get_access_controls():
                   16318: 
                   16319: Args:
                   16320:   current_permissions: the hash ref returned from get_portfile_permissions()
                   16321:   group: (optional) the group you want the files associated with
                   16322:   file: (optional) the file you want access info on
                   16323: 
                   16324: Returns:
1.749     raeburn  16325:     a hash (keys are file names) of hashes containing
                   16326:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   16327:         values are XML containing access control settings (see below) 
1.747     albertel 16328: 
                   16329: Internal notes:
                   16330: 
1.749     raeburn  16331:  access controls are stored in file_permissions.db as key=value pairs.
                   16332:     key -> path to file/file_name\0uniqueID:scope_end_start
                   16333:         where scope -> public,guest,course,group,domains or users.
                   16334:               end -> UNIX time for end of access (0 -> no end date)
                   16335:               start -> UNIX time for start of access
                   16336: 
                   16337:     value -> XML description of access control
                   16338:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   16339:             <start></start>
                   16340:             <end></end>
                   16341: 
                   16342:             <password></password>  for scope type = guest
                   16343: 
                   16344:             <domain></domain>     for scope type = course or group
                   16345:             <number></number>
                   16346:             <roles id="">
                   16347:              <role></role>
                   16348:              <access></access>
                   16349:              <section></section>
                   16350:              <group></group>
                   16351:             </roles>
                   16352: 
                   16353:             <dom></dom>         for scope type = domains
                   16354: 
                   16355:             <users>             for scope type = users
                   16356:              <user>
                   16357:               <uname></uname>
                   16358:               <udom></udom>
                   16359:              </user>
                   16360:             </users>
                   16361:            </scope> 
                   16362:               
                   16363:  Access data is also aggregated for each file in an additional key=value pair:
                   16364:  key -> path to file/file_name\0accesscontrol 
                   16365:  value -> reference to hash
                   16366:           hash contains key = value pairs
                   16367:           where key = uniqueID:scope_end_start
                   16368:                 value = UNIX time record was last updated
                   16369: 
                   16370:           Used to improve speed of look-ups of access controls for each file.  
                   16371:  
                   16372:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   16373: 
1.1198    raeburn  16374: =item *
                   16375: 
1.749     raeburn  16376: modify_access_controls():
                   16377: 
                   16378: Modifies access controls for a portfolio file
                   16379: Args
                   16380: 1. file name
                   16381: 2. reference to hash of required changes,
                   16382: 3. domain
                   16383: 4. username
                   16384:   where domain,username are the domain of the portfolio owner 
                   16385:   (either a user or a course) 
                   16386: 
                   16387: Returns:
                   16388: 1. result of additions or updates ('ok' or 'error', with error message). 
                   16389: 2. result of deletions ('ok' or 'error', with error message).
                   16390: 3. reference to hash of any new or updated access controls.
                   16391: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   16392:    key = integer (inbound ID)
1.1198    raeburn  16393:    value = uniqueID
                   16394: 
                   16395: =item *
                   16396: 
                   16397: get_timebased_id():
                   16398: 
                   16399: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   16400: course via the Course Editor (e.g., folders, composite pages, 
                   16401: group bulletin boards).
                   16402: 
                   16403: Args: (first three required; six others optional)
                   16404: 
                   16405: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   16406:    docssequence, or name of group
                   16407: 
                   16408: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   16409:    e.g., num, boardids
                   16410: 
                   16411: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   16412:    file will be named nohist_namespace.db
                   16413: 
                   16414: 4. cdom: domain of course; default is current course domain from %env
                   16415: 
                   16416: 5. cnum: course number; default is current course number from %env
                   16417: 
                   16418: 6. idtype: set to concat if an additional digit is to be appended to the 
                   16419:    unix timestamp to form the suffix, if the plain timestamp is already
                   16420:    in use.  Default is to not do this, but simply increment the unix 
                   16421:    timestamp by 1 until a unique key is obtained.
                   16422: 
                   16423: 7. who: holder of locking key; defaults to user:domain for user.
                   16424: 
                   16425: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   16426:    retrying); default is 3.
                   16427: 
                   16428: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   16429: 
                   16430: Returns:
                   16431: 
                   16432: 1. suffix obtained (numeric)
                   16433: 
                   16434: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   16435: 
                   16436: 3. error: contains (localized) error message if an error occurred.
                   16437: 
1.747     albertel 16438: 
1.608     albertel 16439: =back
                   16440: 
1.243     albertel 16441: =head2 HTTP Helper Routines
                   16442: 
                   16443: =over 4
                   16444: 
1.191     harris41 16445: =item *
                   16446: 
                   16447: escape() : unpack non-word characters into CGI-compatible hex codes
                   16448: 
                   16449: =item *
                   16450: 
                   16451: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   16452: 
1.243     albertel 16453: =back
                   16454: 
                   16455: =head1 PRIVATE SUBROUTINES
                   16456: 
                   16457: =head2 Underlying communication routines (Shouldn't call)
                   16458: 
                   16459: =over 4
                   16460: 
                   16461: =item *
                   16462: 
                   16463: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   16464: 
                   16465: =item *
                   16466: 
                   16467: reply() : uses subreply to send a message to remote machine, logs all failures
                   16468: 
                   16469: =item *
                   16470: 
                   16471: critical() : passes a critical message to another server; if cannot
                   16472: get through then place message in connection buffer directory and
                   16473: returns con_delayed, if incapable of saving message, returns
                   16474: con_failed
                   16475: 
                   16476: =item *
                   16477: 
                   16478: reconlonc() : tries to reconnect lonc client processes.
                   16479: 
                   16480: =back
                   16481: 
                   16482: =head2 Resource Access Logging
                   16483: 
                   16484: =over 4
                   16485: 
                   16486: =item *
                   16487: 
                   16488: flushcourselogs() : flush (save) buffer logs and access logs
                   16489: 
                   16490: =item *
                   16491: 
                   16492: courselog($what) : save message for course in hash
                   16493: 
                   16494: =item *
                   16495: 
                   16496: courseacclog($what) : save message for course using &courselog().  Perform
                   16497: special processing for specific resource types (problems, exams, quizzes, etc).
                   16498: 
1.191     harris41 16499: =item *
                   16500: 
                   16501: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   16502: as a PerlChildExitHandler
1.243     albertel 16503: 
                   16504: =back
                   16505: 
                   16506: =head2 Other
                   16507: 
                   16508: =over 4
                   16509: 
                   16510: =item *
                   16511: 
                   16512: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 16513: 
                   16514: =back
                   16515: 
                   16516: =cut
1.877     foxr     16517: 

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